diff --git a/.github/workflows/build_langsmith_pyo3_wheels.yml b/.github/workflows/build_langsmith_pyo3_wheels.yml deleted file mode 100644 index 23dc10cb70..0000000000 --- a/.github/workflows/build_langsmith_pyo3_wheels.yml +++ /dev/null @@ -1,249 +0,0 @@ -# This file is based on a starter autogenerated by maturin v1.7.4, -# which can be optained by running `maturin generate-ci github`. -# -# The autogenerated file makes some assumptions that don't work for this repo. -# For example, it assumes that the PyO3 project is the only piece of code in the repo. -# This is why the file has been hand-edited after generation. -# -# To find the changes applied on top of the autogenerated contents, diff this file -# versus commit `c746f00fadb0c84d769be117643683d60eb07ba3` which has the raw autogenerated output. -name: Build langsmith-pyo3 wheels - -# Our wheels build depends on both the `langsmith-pyo3` crate itself and all its dependencies: -# - The Rust workspace-level dependency specification in `rust/Cargo.toml` and `rust/Cargo.lock`. -# - Other local Rust crates, like `langsmith-tracing-client`. -# - The vendored `orjson` and `pyo3` workspaces. -on: - push: - branches: - - main - tags: - - 'langsmith-pyo3==*' - paths: - - "rust/**" - - "vendor/orjson/**" - - "vendor/pyo3/**" - - ".github/workflows/build_langsmith_pyo3_wheels.yml" - pull_request: - paths: - - "rust/**" - - "vendor/orjson/**" - - "vendor/pyo3/**" - - ".github/workflows/build_langsmith_pyo3_wheels.yml" - workflow_dispatch: - -permissions: - contents: read - -env: - RUST_VERSION: '1.82' # Be careful, "stable" gets you "whatever GitHub ships", which is quite old. - # Building wheels for Python 3.13 on Windows is broken at the moment with a linking error, - # so we build 3.13 wheels for other platforms explicitly instead of adding 3.13 here. - # https://github.com/PyO3/maturin-action/issues/292 - SUPPORTED_PYTHON_VERSIONS: 'python3.9 python3.10 python3.11 python3.12' - WORKING_DIRECTORY: rust/crates/langsmith-pyo3 - -jobs: - linux: - runs-on: ${{ matrix.platform.runner }} - strategy: - matrix: - platform: - - runner: ubuntu-latest - target: x86_64 - - runner: ubuntu-latest - target: aarch64 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: 3.x - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - working-directory: ${{ env.WORKING_DIRECTORY }} - rust-toolchain: ${{ env.RUST_VERSION }} - target: ${{ matrix.platform.target }} - args: '--release --out dist --interpreter ${{ env.SUPPORTED_PYTHON_VERSIONS }} python3.13' - sccache: 'true' - manylinux: '2_28' # The default is 'auto' AKA '2014', which is too old for us. - # Workaround for missing `pip` in manylinux_2_28: - # https://github.com/PyO3/maturin-action/issues/249 - before-script-linux: '(python3 -m pip --version || python3 -m ensurepip)' - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels-linux-${{ matrix.platform.target }} - path: ${{ env.WORKING_DIRECTORY }}/dist - if-no-files-found: error - - musllinux: - runs-on: ${{ matrix.platform.runner }} - strategy: - matrix: - platform: - - runner: ubuntu-latest - target: x86_64 - - runner: ubuntu-latest - target: aarch64 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: 3.x - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - working-directory: ${{ env.WORKING_DIRECTORY }} - rust-toolchain: ${{ env.RUST_VERSION }} - target: ${{ matrix.platform.target }} - args: '--release --out dist --interpreter ${{ env.SUPPORTED_PYTHON_VERSIONS }} python3.13' - sccache: 'true' - manylinux: musllinux_1_2 - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels-musllinux-${{ matrix.platform.target }} - path: ${{ env.WORKING_DIRECTORY }}/dist - if-no-files-found: error - - windows: - runs-on: ${{ matrix.platform.runner }} - strategy: - matrix: - platform: - - runner: windows-latest - target: x64 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: 3.x - architecture: ${{ matrix.platform.target }} - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - working-directory: ${{ env.WORKING_DIRECTORY }} - rust-toolchain: ${{ env.RUST_VERSION }} - target: ${{ matrix.platform.target }} - args: '--release --out dist --interpreter ${{ env.SUPPORTED_PYTHON_VERSIONS }}' - sccache: 'true' - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels-windows-${{ matrix.platform.target }} - path: ${{ env.WORKING_DIRECTORY }}/dist - if-no-files-found: error - - macos: - runs-on: ${{ matrix.platform.runner }} - strategy: - matrix: - platform: - - runner: macos-15 - target: aarch64 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: 3.x - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - working-directory: ${{ env.WORKING_DIRECTORY }} - rust-toolchain: ${{ env.RUST_VERSION }} - target: ${{ matrix.platform.target }} - args: '--release --out dist --interpreter ${{ env.SUPPORTED_PYTHON_VERSIONS }} python3.13' - sccache: 'true' - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels-${{ matrix.platform.runner }}-${{ matrix.platform.target }} - path: ${{ env.WORKING_DIRECTORY }}/dist - if-no-files-found: error - - sdist: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Build sdist - uses: PyO3/maturin-action@v1 - with: - working-directory: ${{ env.WORKING_DIRECTORY }} - rust-toolchain: ${{ env.RUST_VERSION }} - command: sdist - args: --out dist - - name: Upload sdist - uses: actions/upload-artifact@v4 - with: - name: wheels-sdist - path: ${{ env.WORKING_DIRECTORY }}/dist - if-no-files-found: error - - release: - name: Release - runs-on: ubuntu-latest - if: ${{ startsWith(github.ref, 'refs/tags/langsmith-pyo3==') || github.event_name == 'workflow_dispatch' }} - needs: [linux, musllinux, windows, macos, sdist] - permissions: - # Use to sign the release artifacts - id-token: write - # Used to upload release artifacts - contents: write - # Used to generate artifact attestation - attestations: write - steps: - - uses: actions/download-artifact@v4 - with: - path: ${{ env.WORKING_DIRECTORY }} - - name: Move wheels to 'dist' dir - run: | - set -euxo pipefail - - # Show what wheels got built. - cd "$WORKING_DIRECTORY" - ls -alh wheels-* - - # Wipe the `dist` directory if it already existed, so we don't accidentally publish - # something we didn't intend to. Then, move the wheels into a fresh `dist` directory. - rm -rf dist - mkdir dist - mv wheels-*/* dist/ - ls -alh dist/ - - name: Ensure the tagged version matches the wheel versions - if: startsWith(github.ref, 'refs/tags/langsmith-pyo3==') - env: - REF_NAME: ${{ github.ref }} - run: | - cd "$WORKING_DIRECTORY" - - # - Look up the first wheel file - # - Select just the filename, - # for example `langsmith_pyo3-0.1.0rc1-cp313-cp313-manylinux_2_34_x86_64.whl` - # - Split on dashes, and extract the version field (the second field). - EXPECTED_VERSION="$(find dist/ -name 'langsmith_pyo3-*.whl' -printf '%f\n' | head -1 | cut -d- -f2)" - EXPECTED_REF="refs/tags/langsmith-pyo3==${EXPECTED_VERSION}" - - if [[ "$REF_NAME" != "$EXPECTED_REF" ]]; then - echo 'Current tag does not match the expected tag for the wheel versions being published!' - echo "Expected ref name: ${EXPECTED_REF}" - echo "Actual ref name: ${REF_NAME}" - echo '' - echo 'Proceeding here would tag one version, then publish a different one.' - echo "We don't want that, so we'll exit and refuse to publish instead." - echo '' - echo "Please make sure you've updated the version inside the pyo3 project's Cargo.toml" - echo 'to match the tag you pushed. Ensure that Cargo.toml contains a version number' - echo 'that follows Rust formatting rules (e.g. "0.1.2-rc3"), and that the tag follows' - echo 'Python version formatting rules (e.g. "0.1.2rc3").' - exit 1 - fi - - name: Generate artifact attestation - uses: actions/attest-build-provenance@v1 - with: - subject-path: ${{ env.WORKING_DIRECTORY }}/dist/* - - name: Publish package distributions to PyPI - if: startsWith(github.ref, 'refs/tags/langsmith-pyo3==') - uses: pypa/gh-action-pypi-publish@release/v1 - with: - packages-dir: ${{ env.WORKING_DIRECTORY }}/dist diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d749813d6..132a3402ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,8 +25,6 @@ permissions: contents: read env: - RUST_VERSION: "1.82" - RUST_WORKSPACE_PATH: "rust" NODE_VERSION: "22.4.1" jobs: @@ -39,7 +37,6 @@ jobs: outputs: python: ${{ steps.filter.outputs.python }} js: ${{ steps.filter.outputs.js }} - rust: ${{ steps.filter.outputs.rust }} steps: - uses: actions/checkout@v4 - uses: dorny/paths-filter@v3 @@ -52,11 +49,6 @@ jobs: js: - 'js/**' - '.github/**' - rust: - - 'rust/**' - - 'vendor/orjson/**' - - 'vendor/pyo3/**' - - '.github/**' # ============================================================================ # Python CI @@ -374,91 +366,6 @@ jobs: - name: Test Metro exports run: docker compose -f js/internal/environment_tests/docker-compose.yml run test-exports-metro - # ============================================================================ - # Rust CI - # ============================================================================ - rust-lint: - name: "Rust Check lint and rustfmt" - needs: changes - if: needs.changes.outputs.rust == 'true' - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - persist-credentials: false - - name: Install rust - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - toolchain: "${{ env.RUST_VERSION }}" - components: rustfmt, clippy - cache-workspaces: "${{ env.RUST_WORKSPACE_PATH }} -> target" - rustflags: "" - - name: cargo clippy - working-directory: ${{ env.RUST_WORKSPACE_PATH }} - run: cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings --allow deprecated - - name: cargo fmt - working-directory: ${{ env.RUST_WORKSPACE_PATH }} - run: cargo fmt -- --check - - name: cargo doc - working-directory: ${{ env.RUST_WORKSPACE_PATH }} - env: - RUSTDOCFLAGS: -D warnings - run: cargo doc --workspace --no-deps --document-private-items - - rust-test: - name: "Rust cargo test" - needs: changes - if: needs.changes.outputs.rust == 'true' - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - persist-credentials: false - - name: Install rust - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - toolchain: "${{ env.RUST_VERSION }}" - cache-workspaces: "${{ env.RUST_WORKSPACE_PATH }} -> target" - rustflags: "" - - name: cargo test - working-directory: ${{ env.RUST_WORKSPACE_PATH }} - run: | - set -euxo pipefail - # We exclude `langsmith-pyo3` since its tests: - # - Have to be run under `nextest`, since they require one-process-per-test. - # - Require a non-default configuration in order to run. - cargo test --workspace --exclude langsmith-pyo3 --all-features - - rust-pyo3-test: - name: "Rust langsmith-pyo3 tests" - needs: changes - if: needs.changes.outputs.rust == 'true' - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - persist-credentials: false - - name: Install rust - uses: actions-rust-lang/setup-rust-toolchain@v1 - with: - toolchain: "${{ env.RUST_VERSION }}" - cache-workspaces: "${{ env.RUST_WORKSPACE_PATH }} -> target" - rustflags: "" - - name: Install cargo-nextest - uses: taiki-e/install-action@v2 - with: - tool: cargo-nextest - - name: cargo test - working-directory: "${{ env.RUST_WORKSPACE_PATH }}/crates/langsmith-pyo3" - run: | - set -euxo pipefail - # See the langsmith-pyo3 README.md file for an explanation - # of why tests have to run under nextest & without default features. - cargo nextest run --no-default-features - # ============================================================================ # Integration Tests # ============================================================================ @@ -649,10 +556,6 @@ jobs: - js-exports-webpack - js-exports-esbuild - js-exports-metro - # Rust - - rust-lint - - rust-test - - rust-pyo3-test # Integration - integration-changes - python-integration diff --git a/.github/workflows/link-check.yml b/.github/workflows/link-check.yml index 840898cc25..a687394f03 100644 --- a/.github/workflows/link-check.yml +++ b/.github/workflows/link-check.yml @@ -21,6 +21,6 @@ jobs: uses: gaurav-nelson/github-action-markdown-link-check@v1 with: # Do not check the `vendor/` directory. - folder-path: .github,js,openapi,python,rust + folder-path: .github,js,openapi,python file-path: ./README.md config-file: .mlc_config.json diff --git a/rust/Cargo.lock b/rust/Cargo.lock deleted file mode 100644 index 94f61e7a69..0000000000 --- a/rust/Cargo.lock +++ /dev/null @@ -1,2504 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "addr2line" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "anes" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" - -[[package]] -name = "anstyle" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" - -[[package]] -name = "assert-json-diff" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" -dependencies = [ - "serde", - "serde_json", -] - -[[package]] -name = "associative-cache" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b993cd767a2bc7307dd87622311ca22c44329cc7a21366206bfa0896827b2bad" - -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - -[[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[package]] -name = "backtrace" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-targets 0.52.6", -] - -[[package]] -name = "base64" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" - -[[package]] -name = "bitflags" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" - -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - -[[package]] -name = "bytecount" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" - -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - -[[package]] -name = "bytes" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" - -[[package]] -name = "cast" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" - -[[package]] -name = "castaway" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5" -dependencies = [ - "rustversion", -] - -[[package]] -name = "cc" -version = "1.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" -dependencies = [ - "jobserver", - "libc", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "cfg_aliases" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" - -[[package]] -name = "chrono" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets 0.52.6", -] - -[[package]] -name = "ciborium" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" -dependencies = [ - "ciborium-io", - "ciborium-ll", - "serde", -] - -[[package]] -name = "ciborium-io" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" - -[[package]] -name = "ciborium-ll" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" -dependencies = [ - "ciborium-io", - "half", -] - -[[package]] -name = "clap" -version = "4.5.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.5.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1" -dependencies = [ - "anstyle", - "clap_lex", -] - -[[package]] -name = "clap_lex" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" - -[[package]] -name = "colored" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" -dependencies = [ - "lazy_static", - "windows-sys 0.48.0", -] - -[[package]] -name = "compact_str" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" -dependencies = [ - "castaway", - "cfg-if", - "itoa", - "rustversion", - "ryu", - "serde", - "static_assertions", -] - -[[package]] -name = "core-foundation" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - -[[package]] -name = "crc32fast" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "criterion" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" -dependencies = [ - "anes", - "cast", - "ciborium", - "clap", - "criterion-plot", - "futures", - "is-terminal", - "itertools", - "num-traits", - "once_cell", - "oorandom", - "plotters", - "rayon", - "regex", - "serde", - "serde_derive", - "serde_json", - "tinytemplate", - "tokio", - "walkdir", -] - -[[package]] -name = "criterion-plot" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" -dependencies = [ - "cast", - "itertools", -] - -[[package]] -name = "crossbeam-channel" -version = "0.5.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ba6d68e24814cb8de6bb986db8222d3a027d15872cabc0d18817bc3c0e4471" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-deque" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" - -[[package]] -name = "crunchy" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - -[[package]] -name = "displaydoc" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "either" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" - -[[package]] -name = "encoding_rs" -version = "0.8.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - -[[package]] -name = "errno" -version = "0.3.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" -dependencies = [ - "libc", - "windows-sys 0.59.0", -] - -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - -[[package]] -name = "flate2" -version = "1.0.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "futures" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" -dependencies = [ - "futures-core", - "futures-sink", -] - -[[package]] -name = "futures-core" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" - -[[package]] -name = "futures-executor" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-io" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" - -[[package]] -name = "futures-macro" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "futures-sink" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" - -[[package]] -name = "futures-task" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" - -[[package]] -name = "futures-util" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" -dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-macro", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - -[[package]] -name = "getrandom" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" -dependencies = [ - "cfg-if", - "js-sys", - "libc", - "wasi", - "wasm-bindgen", -] - -[[package]] -name = "gimli" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" - -[[package]] -name = "h2" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" -dependencies = [ - "atomic-waker", - "bytes", - "fnv", - "futures-core", - "futures-sink", - "http", - "indexmap", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "half" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" -dependencies = [ - "cfg-if", - "crunchy", -] - -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "hermit-abi" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" - -[[package]] -name = "http" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" -dependencies = [ - "bytes", - "http", -] - -[[package]] -name = "http-body-util" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" - -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - -[[package]] -name = "hyper" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97818827ef4f364230e16705d4706e2897df2bb60617d6ca15d598025a3c481f" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "h2", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "smallvec", - "tokio", - "want", -] - -[[package]] -name = "hyper-rustls" -version = "0.27.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" -dependencies = [ - "futures-util", - "http", - "hyper", - "hyper-util", - "rustls", - "rustls-pki-types", - "tokio", - "tokio-rustls", - "tower-service", - "webpki-roots", -] - -[[package]] -name = "hyper-util" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df2dcfbe0677734ab2f3ffa7fa7bfd4706bfdc1ef393f2ee30184aed67e631b4" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "hyper", - "pin-project-lite", - "socket2", - "tokio", - "tower-service", - "tracing", -] - -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - -[[package]] -name = "icu_collections" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" -dependencies = [ - "displaydoc", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_locid" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" -dependencies = [ - "displaydoc", - "litemap", - "tinystr", - "writeable", - "zerovec", -] - -[[package]] -name = "icu_locid_transform" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_locid_transform_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_locid_transform_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" - -[[package]] -name = "icu_normalizer" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_normalizer_data", - "icu_properties", - "icu_provider", - "smallvec", - "utf16_iter", - "utf8_iter", - "write16", - "zerovec", -] - -[[package]] -name = "icu_normalizer_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" - -[[package]] -name = "icu_properties" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5" -dependencies = [ - "displaydoc", - "icu_collections", - "icu_locid_transform", - "icu_properties_data", - "icu_provider", - "tinystr", - "zerovec", -] - -[[package]] -name = "icu_properties_data" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" - -[[package]] -name = "icu_provider" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" -dependencies = [ - "displaydoc", - "icu_locid", - "icu_provider_macros", - "stable_deref_trait", - "tinystr", - "writeable", - "yoke", - "zerofrom", - "zerovec", -] - -[[package]] -name = "icu_provider_macros" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "idna" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e" -dependencies = [ - "idna_adapter", - "smallvec", - "utf8_iter", -] - -[[package]] -name = "idna_adapter" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71" -dependencies = [ - "icu_normalizer", - "icu_properties", -] - -[[package]] -name = "indexmap" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "indoc" -version = "2.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" - -[[package]] -name = "ipnet" -version = "2.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" - -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - -[[package]] -name = "itoa" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" - -[[package]] -name = "itoap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8" - -[[package]] -name = "jiff" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db69f08d4fb10524cacdb074c10b296299d71274ddbc830a8ee65666867002e9" - -[[package]] -name = "jobserver" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" -dependencies = [ - "libc", -] - -[[package]] -name = "js-sys" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" -dependencies = [ - "once_cell", - "wasm-bindgen", -] - -[[package]] -name = "langsmith-pyo3" -version = "0.1.0-rc5" -dependencies = [ - "langsmith-tracing-client", - "orjson", - "pyo3", - "pyo3-ffi", - "serde", - "serde_json", -] - -[[package]] -name = "langsmith-tracing-client" -version = "0.1.0" -dependencies = [ - "chrono", - "criterion", - "crossbeam-channel", - "fastrand", - "flate2", - "futures", - "http", - "mockito", - "multer", - "rayon", - "reqwest", - "serde", - "serde_json", - "tempfile", - "thiserror", - "tokio", - "tokio-util", - "ureq", - "uuid", - "zstd", -] - -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - -[[package]] -name = "libc" -version = "0.2.167" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" - -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - -[[package]] -name = "litemap" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" - -[[package]] -name = "lock_api" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" -dependencies = [ - "autocfg", - "scopeguard", -] - -[[package]] -name = "log" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "memoffset" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" -dependencies = [ - "autocfg", -] - -[[package]] -name = "mime" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" - -[[package]] -name = "mime_guess" -version = "2.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" -dependencies = [ - "mime", - "unicase", -] - -[[package]] -name = "miniz_oxide" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" -dependencies = [ - "adler2", -] - -[[package]] -name = "mio" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" -dependencies = [ - "libc", - "wasi", - "windows-sys 0.52.0", -] - -[[package]] -name = "mockito" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "652cd6d169a36eaf9d1e6bce1a221130439a966d7f27858af66a33a66e9c4ee2" -dependencies = [ - "assert-json-diff", - "bytes", - "colored", - "futures-util", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-util", - "log", - "rand", - "regex", - "serde_json", - "serde_urlencoded", - "similar", - "tokio", -] - -[[package]] -name = "multer" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b" -dependencies = [ - "bytes", - "encoding_rs", - "futures-util", - "http", - "httparse", - "memchr", - "mime", - "spin", - "version_check", -] - -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - -[[package]] -name = "object" -version = "0.36.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" -dependencies = [ - "memchr", -] - -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - -[[package]] -name = "oorandom" -version = "11.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" - -[[package]] -name = "orjson" -version = "3.10.11" -dependencies = [ - "associative-cache", - "bytecount", - "cc", - "compact_str", - "encoding_rs", - "half", - "itoa", - "itoap", - "jiff", - "once_cell", - "pyo3-build-config", - "pyo3-ffi", - "ryu", - "serde", - "serde_json", - "simdutf8", - "smallvec", - "uuid", - "version_check", - "xxhash-rust", -] - -[[package]] -name = "parking_lot" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets 0.52.6", -] - -[[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - -[[package]] -name = "pin-project-lite" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "pkg-config" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" - -[[package]] -name = "plotters" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" -dependencies = [ - "num-traits", - "plotters-backend", - "plotters-svg", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "plotters-backend" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" - -[[package]] -name = "plotters-svg" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" -dependencies = [ - "plotters-backend", -] - -[[package]] -name = "portable-atomic" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" - -[[package]] -name = "ppv-lite86" -version = "0.2.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" -dependencies = [ - "zerocopy", -] - -[[package]] -name = "proc-macro2" -version = "1.0.92" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "pyo3" -version = "0.23.4" -dependencies = [ - "cfg-if", - "indoc", - "libc", - "memoffset", - "once_cell", - "portable-atomic", - "pyo3-build-config", - "pyo3-ffi", - "pyo3-macros", - "unindent", -] - -[[package]] -name = "pyo3-build-config" -version = "0.23.4" -dependencies = [ - "once_cell", - "target-lexicon", -] - -[[package]] -name = "pyo3-ffi" -version = "0.23.4" -dependencies = [ - "libc", - "pyo3-build-config", -] - -[[package]] -name = "pyo3-macros" -version = "0.23.4" -dependencies = [ - "proc-macro2", - "pyo3-macros-backend", - "quote", - "syn", -] - -[[package]] -name = "pyo3-macros-backend" -version = "0.23.4" -dependencies = [ - "heck", - "proc-macro2", - "pyo3-build-config", - "quote", - "syn", -] - -[[package]] -name = "quinn" -version = "0.11.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62e96808277ec6f97351a2380e6c25114bc9e67037775464979f3037c92d05ef" -dependencies = [ - "bytes", - "pin-project-lite", - "quinn-proto", - "quinn-udp", - "rustc-hash", - "rustls", - "socket2", - "thiserror", - "tokio", - "tracing", -] - -[[package]] -name = "quinn-proto" -version = "0.11.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2fe5ef3495d7d2e377ff17b1a8ce2ee2ec2a18cde8b6ad6619d65d0701c135d" -dependencies = [ - "bytes", - "getrandom", - "rand", - "ring", - "rustc-hash", - "rustls", - "rustls-pki-types", - "slab", - "thiserror", - "tinyvec", - "tracing", - "web-time", -] - -[[package]] -name = "quinn-udp" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5a626c6807713b15cac82a6acaccd6043c9a5408c24baae07611fec3f243da" -dependencies = [ - "cfg_aliases", - "libc", - "once_cell", - "socket2", - "tracing", - "windows-sys 0.59.0", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - -[[package]] -name = "rayon" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" -dependencies = [ - "crossbeam-deque", - "crossbeam-utils", -] - -[[package]] -name = "redox_syscall" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" -dependencies = [ - "bitflags", -] - -[[package]] -name = "regex" -version = "1.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" - -[[package]] -name = "reqwest" -version = "0.12.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" -dependencies = [ - "base64", - "bytes", - "encoding_rs", - "futures-channel", - "futures-core", - "futures-util", - "h2", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-rustls", - "hyper-util", - "ipnet", - "js-sys", - "log", - "mime", - "mime_guess", - "once_cell", - "percent-encoding", - "pin-project-lite", - "quinn", - "rustls", - "rustls-pemfile", - "rustls-pki-types", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper", - "system-configuration", - "tokio", - "tokio-rustls", - "tokio-util", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "wasm-streams", - "web-sys", - "webpki-roots", - "windows-registry", -] - -[[package]] -name = "ring" -version = "0.17.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" -dependencies = [ - "cc", - "cfg-if", - "getrandom", - "libc", - "spin", - "untrusted", - "windows-sys 0.52.0", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - -[[package]] -name = "rustc-hash" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" - -[[package]] -name = "rustix" -version = "0.38.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] - -[[package]] -name = "rustls" -version = "0.23.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "934b404430bb06b3fae2cba809eb45a1ab1aecd64491213d7c3301b88393f8d1" -dependencies = [ - "log", - "once_cell", - "ring", - "rustls-pki-types", - "rustls-webpki", - "subtle", - "zeroize", -] - -[[package]] -name = "rustls-pemfile" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" -dependencies = [ - "rustls-pki-types", -] - -[[package]] -name = "rustls-pki-types" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16f1201b3c9a7ee8039bcadc17b7e605e2945b27eee7631788c1bd2b0643674b" -dependencies = [ - "web-time", -] - -[[package]] -name = "rustls-webpki" -version = "0.102.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" -dependencies = [ - "ring", - "rustls-pki-types", - "untrusted", -] - -[[package]] -name = "rustversion" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" - -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" - -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "serde" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.133" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "signal-hook-registry" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" -dependencies = [ - "libc", -] - -[[package]] -name = "simdutf8" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" - -[[package]] -name = "similar" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" - -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - -[[package]] -name = "socket2" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" - -[[package]] -name = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - -[[package]] -name = "subtle" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" - -[[package]] -name = "syn" -version = "2.0.90" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "sync_wrapper" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" -dependencies = [ - "futures-core", -] - -[[package]] -name = "synstructure" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "system-configuration" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" -dependencies = [ - "bitflags", - "core-foundation", - "system-configuration-sys", -] - -[[package]] -name = "system-configuration-sys" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "target-lexicon" -version = "0.12.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" - -[[package]] -name = "tempfile" -version = "3.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" -dependencies = [ - "cfg-if", - "fastrand", - "once_cell", - "rustix", - "windows-sys 0.59.0", -] - -[[package]] -name = "thiserror" -version = "2.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f49a1853cf82743e3b7950f77e0f4d622ca36cf4317cba00c767838bac8d490" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "2.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8381894bb3efe0c4acac3ded651301ceee58a15d47c2e34885ed1908ad667061" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tinystr" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" -dependencies = [ - "displaydoc", - "zerovec", -] - -[[package]] -name = "tinytemplate" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" -dependencies = [ - "serde", - "serde_json", -] - -[[package]] -name = "tinyvec" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - -[[package]] -name = "tokio" -version = "1.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" -dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "parking_lot", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", - "windows-sys 0.52.0", -] - -[[package]] -name = "tokio-macros" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tokio-rustls" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" -dependencies = [ - "rustls", - "rustls-pki-types", - "tokio", -] - -[[package]] -name = "tokio-util" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "tower-service" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" - -[[package]] -name = "tracing" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" -dependencies = [ - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" -dependencies = [ - "once_cell", -] - -[[package]] -name = "try-lock" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" - -[[package]] -name = "unicase" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" - -[[package]] -name = "unicode-ident" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" - -[[package]] -name = "unindent" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" - -[[package]] -name = "untrusted" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" - -[[package]] -name = "ureq" -version = "2.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" -dependencies = [ - "base64", - "flate2", - "log", - "once_cell", - "rustls", - "rustls-pki-types", - "url", - "webpki-roots", -] - -[[package]] -name = "url" -version = "2.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - -[[package]] -name = "utf16_iter" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" - -[[package]] -name = "utf8_iter" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" - -[[package]] -name = "uuid" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" -dependencies = [ - "getrandom", -] - -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - -[[package]] -name = "walkdir" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" -dependencies = [ - "same-file", - "winapi-util", -] - -[[package]] -name = "want" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" -dependencies = [ - "try-lock", -] - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "wasm-bindgen" -version = "0.2.97" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.97" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" -dependencies = [ - "cfg-if", - "js-sys", - "once_cell", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.97" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.97" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.97" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" - -[[package]] -name = "wasm-streams" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" -dependencies = [ - "futures-util", - "js-sys", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", -] - -[[package]] -name = "web-sys" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "web-time" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - -[[package]] -name = "webpki-roots" -version = "0.26.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e" -dependencies = [ - "rustls-pki-types", -] - -[[package]] -name = "winapi-util" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" -dependencies = [ - "windows-sys 0.59.0", -] - -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-registry" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" -dependencies = [ - "windows-result", - "windows-strings", - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-result" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-strings" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" -dependencies = [ - "windows-result", - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets 0.48.5", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "write16" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" - -[[package]] -name = "writeable" -version = "0.5.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" - -[[package]] -name = "xxhash-rust" -version = "0.8.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a5cbf750400958819fb6178eaa83bee5cd9c29a26a40cc241df8c70fdd46984" - -[[package]] -name = "yoke" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" -dependencies = [ - "serde", - "stable_deref_trait", - "yoke-derive", - "zerofrom", -] - -[[package]] -name = "yoke-derive" -version = "0.7.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "synstructure", -] - -[[package]] -name = "zerocopy" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" -dependencies = [ - "byteorder", - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "zerofrom" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" -dependencies = [ - "zerofrom-derive", -] - -[[package]] -name = "zerofrom-derive" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "synstructure", -] - -[[package]] -name = "zeroize" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" - -[[package]] -name = "zerovec" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079" -dependencies = [ - "yoke", - "zerofrom", - "zerovec-derive", -] - -[[package]] -name = "zerovec-derive" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "zstd" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcf2b778a664581e31e389454a7072dab1647606d44f7feea22cd5abb9c9f3f9" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "7.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54a3ab4db68cea366acc5c897c7b4d4d1b8994a9cd6e6f841f8964566a419059" -dependencies = [ - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.13+zstd.1.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa" -dependencies = [ - "cc", - "pkg-config", -] diff --git a/rust/Cargo.toml b/rust/Cargo.toml deleted file mode 100644 index 736435552b..0000000000 --- a/rust/Cargo.toml +++ /dev/null @@ -1,32 +0,0 @@ -[workspace] -members = [ - "crates/langsmith-pyo3", - "crates/langsmith-tracing-client", -] -resolver = "2" - -[workspace.lints] - -[workspace.dependencies] -chrono = "0.4.38" -crossbeam-channel = "0.5.14" -fastrand = "2.3.0" -flate2 = "1.0.34" -futures = "0.3.31" -http = "1.2.0" -rayon = "1.10.0" -serde = { version = "1.0.210", features = ["derive"] } -serde_json = "1.0.128" -tempfile = "3.13.0" -thiserror = "2.0" -tokio = { version = "1", features = ["full"] } -tokio-util = "0.7.12" -ureq = "2.10.1" -uuid = { version = "1.11.0", features = ["v4"] } -zstd = { version = "0.13.2", features = ["zstdmt"] } - -# Use rustls instead of OpenSSL, because OpenSSL is a nightmare when compiling across platforms. -# OpenSSL is a default feature, so we have to disable all default features, then re-add -# everything except `default-tls` which pulls in OpenSSL: -# https://github.com/seanmonstar/reqwest/blob/e2d4b14c695acaebe531af7ce4712e182278f38a/Cargo.toml#L30 -reqwest = { version = "0.12.8", default-features = false, features = ["rustls-tls", "charset", "http2", "macos-system-configuration", "multipart", "stream", "blocking"] } diff --git a/rust/crates/langsmith-pyo3/.cargo/config.toml b/rust/crates/langsmith-pyo3/.cargo/config.toml deleted file mode 100644 index 9413bcd4b3..0000000000 --- a/rust/crates/langsmith-pyo3/.cargo/config.toml +++ /dev/null @@ -1,7 +0,0 @@ -# Required flags to enable building langsmith_pyo3 on macOS: -# https://pyo3.rs/v0.23.1/building-and-distribution#macos -[target.aarch64-apple-darwin] -rustflags = [ - "-C", "link-arg=-undefined", - "-C", "link-arg=dynamic_lookup", -] diff --git a/rust/crates/langsmith-pyo3/.gitignore b/rust/crates/langsmith-pyo3/.gitignore deleted file mode 100644 index c8f044299d..0000000000 --- a/rust/crates/langsmith-pyo3/.gitignore +++ /dev/null @@ -1,72 +0,0 @@ -/target - -# Byte-compiled / optimized / DLL files -__pycache__/ -.pytest_cache/ -*.py[cod] - -# C extensions -*.so - -# Distribution / packaging -.Python -.venv/ -env/ -bin/ -build/ -develop-eggs/ -dist/ -eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -include/ -man/ -venv/ -*.egg-info/ -.installed.cfg -*.egg - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt -pip-selfcheck.json - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.cache -nosetests.xml -coverage.xml - -# Translations -*.mo - -# Mr Developer -.mr.developer.cfg -.project -.pydevproject - -# Rope -.ropeproject - -# Django stuff: -*.log -*.pot - -.DS_Store - -# Sphinx documentation -docs/_build/ - -# PyCharm -.idea/ - -# VSCode -.vscode/ - -# Pyenv -.python-version diff --git a/rust/crates/langsmith-pyo3/Cargo.toml b/rust/crates/langsmith-pyo3/Cargo.toml deleted file mode 100644 index 7ebf2e0891..0000000000 --- a/rust/crates/langsmith-pyo3/Cargo.toml +++ /dev/null @@ -1,45 +0,0 @@ -[package] -name = "langsmith-pyo3" -version = "0.1.0-rc5" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[lib] -# The name of the native library. This is the name which will be used in Python to import the -# library (i.e. `from langsmith_pyo3 import *`). If you change this, you must also change -# the name of the top `#[pymodule]` in `src/lib.rs`. -name = "langsmith_pyo3" - -# "cdylib" is necessary to produce a shared library for Python to import from. -# "rlib" is what Rust uses to use this crate as a library, for example in tests, benchmarks, -# examples, or Rust binaries built using this library. -# -# Downstream *Rust* code (including any code in `bin/`, `examples/`, and `tests/`) will not be able -# to `use langsmith_pyo3;` unless the "rlib" or "lib" crate type is also included. -crate-type = ["cdylib", "rlib"] - -[dependencies] -pyo3 = { path = "../../../vendor/pyo3" } -pyo3-ffi = { path = "../../../vendor/pyo3/pyo3-ffi" } -serde = { workspace = true } -serde_json = { workspace = true } -langsmith-tracing-client = { path = "../langsmith-tracing-client" } -orjson = { path = "../../../vendor/orjson", default-features = false } - -[features] -# The `extension-module` feature is necessary to produce a "manylinux" portable Python library. -# Otherwise PyO3 will link to its own Python interpreter and run our code inside it, -# which will make the library only work with *that specific* Python build and no other. -# Not just that *Python version* but also glibc-vs-musl, etc. -# More info: -# - https://pyo3.rs/v0.22.5/building-and-distribution#the-extension-module-feature -# - https://peps.python.org/pep-0513/ -# -# However, it makes `cargo test` fail with linker errors since Python symbols are missing, -# since the test binary isn't run as part of a Python binary: -# https://pyo3.rs/v0.13.2/faq#i-cant-run-cargo-test-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror -# -# PyO3's recommended workaround is to run tests with `--no-default-features`. -extension-module = ["pyo3/extension-module", "pyo3-ffi/extension-module", "orjson/extension-module"] -default = ["extension-module"] diff --git a/rust/crates/langsmith-pyo3/README.md b/rust/crates/langsmith-pyo3/README.md deleted file mode 100644 index 5ccd88dda9..0000000000 --- a/rust/crates/langsmith-pyo3/README.md +++ /dev/null @@ -1,40 +0,0 @@ -# langsmith-pyo3 - -Python bindings for LangSmith internals. - -## Development - -Requires a recent Rust build. `rustup upgrade stable` should do it. - -Relies on `maturin`, `uvx`, and `cargo-nextest`. - -To bootstrap, run: -``` -cargo install --locked cargo-nextest -uv venv --seed -source .venv/bin/activate -pip install patchelf -``` - -To develop, run `uvx maturin develop` which will build and install the Rust code directly into the current virtualenv. - -To build wheels, run `uvx maturin build`. - -To make performance-optimized builds, append `--release` to either command. - -To run install these bindings into another virtualenv (e.g. to run benchmarks), -activate that virtualenv, then `cd` to this directory and run `uvx maturin develop --release`. -When that command completes, the virtualenv will have an optimized build -of `langsmith-pyo3` installed. - -### Testing - -Do not run `cargo test`, *IT WILL NOT WORK*. You will get an inscrutable linker error like `undefined symbol: _Py_CheckFunctionResult` or `linker command failed with exit code 1`. - -Instead, run: `cargo nextest run --no-default-features` - -TL;DR on why: -- This package assumes it's compiled into a Python library, but Rust tests don't run a Python environment. That won't work. -- The `--no-default-features` flag is configured to include Python into the built code, which Rust tests will then run. (Full details [here](https://pyo3.rs/v0.13.2/faq#i-cant-run-cargo-test-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror).) -- Rust tests run in parallel, but Python and `orjson` assume they own the entire process. That won't work. -- `cargo nextest` lets us run each test in its own process, satisfying Python's and `orjson`'s assumptions. diff --git a/rust/crates/langsmith-pyo3/pyproject.toml b/rust/crates/langsmith-pyo3/pyproject.toml deleted file mode 100644 index 7cd710d0be..0000000000 --- a/rust/crates/langsmith-pyo3/pyproject.toml +++ /dev/null @@ -1,16 +0,0 @@ -[build-system] -requires = ["maturin>=1.7,<2.0"] -build-backend = "maturin" - -[project] -name = "langsmith-pyo3" -requires-python = ">=3.9" -classifiers = [ - "Programming Language :: Rust", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", -] -dynamic = ["version"] - -[tool.maturin] -features = ["pyo3/extension-module"] diff --git a/rust/crates/langsmith-pyo3/src/blocking_tracing_client.rs b/rust/crates/langsmith-pyo3/src/blocking_tracing_client.rs deleted file mode 100644 index a6a2b19331..0000000000 --- a/rust/crates/langsmith-pyo3/src/blocking_tracing_client.rs +++ /dev/null @@ -1,85 +0,0 @@ -use std::{sync::Arc, time::Duration}; - -use pyo3::prelude::*; - -use langsmith_tracing_client::client::{ - ClientConfig as RustClientConfig, TracingClient as RustTracingClient, -}; - -pub(super) fn register(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} - -// `frozen` here means "immutable from Python". Rust-side mutation is allowed. -// Since this type is also `Sync`, that means we can operate on `BlockingTracingClient` values -// without holding the Python GIL. -#[pyclass(frozen)] -pub struct BlockingTracingClient { - client: Arc, -} - -#[pymethods] -impl BlockingTracingClient { - #[new] - pub fn new( - endpoint: String, - api_key: String, - queue_capacity: usize, - batch_size: usize, - batch_timeout_millis: u64, - compression_level: i32, - ) -> PyResult { - let config = RustClientConfig { - endpoint, - api_key, - queue_capacity, - send_at_batch_size: batch_size, - send_at_batch_time: Duration::from_millis(batch_timeout_millis), - compression_level, - headers: None, // TODO: support custom headers - }; - - let client = RustTracingClient::new(config) - .map_err(|e| Python::with_gil(|py| into_py_err(py, e)))?; - - Ok(Self { client: Arc::from(client) }) - } - - // N.B.: `slf.get()` below is only valid if the `Self` type is `Sync` and `pyclass(frozen)`, - // which is enforced at compile-time. - pub fn create_run( - slf: &Bound<'_, Self>, - run: super::py_run::RunCreateExtended, - ) -> PyResult<()> { - let unpacked = slf.get(); - Python::allow_threads(slf.py(), || unpacked.client.submit_run_create(run.into_inner())) - .map_err(|e| into_py_err(slf.py(), e)) - } - - // N.B.: `slf.get()` below is only valid if the `Self` type is `Sync` and `pyclass(frozen)`, - // which is enforced at compile-time. - pub fn update_run( - slf: &Bound<'_, Self>, - run: super::py_run::RunUpdateExtended, - ) -> PyResult<()> { - let unpacked = slf.get(); - Python::allow_threads(slf.py(), || unpacked.client.submit_run_update(run.into_inner())) - .map_err(|e| into_py_err(slf.py(), e)) - } -} - -fn into_py_err(py: Python<'_>, e: langsmith_tracing_client::client::TracingClientError) -> PyErr { - crate::errors::TracingClientError::new_err(format!("{e}").into_py(py)) -} - -impl Drop for BlockingTracingClient { - fn drop(&mut self) { - if Arc::strong_count(&self.client) == 1 { - // This is the only copy of the client in Python, - // so let it drain its in-progress requests before proceeding. - // This runs when Python runs GC on the client, such as when the application is exiting. - self.client.drain().expect("draining failed"); - } - } -} diff --git a/rust/crates/langsmith-pyo3/src/errors.rs b/rust/crates/langsmith-pyo3/src/errors.rs deleted file mode 100644 index 8aa074c848..0000000000 --- a/rust/crates/langsmith-pyo3/src/errors.rs +++ /dev/null @@ -1,13 +0,0 @@ -use pyo3::{ - create_exception, - types::{PyModule, PyModuleMethods}, - Bound, PyResult, Python, -}; - -pub(super) fn register(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add("TracingClientError", py.get_type_bound::())?; - - Ok(()) -} - -create_exception!(langsmith_pyo3, TracingClientError, pyo3::exceptions::PyException); diff --git a/rust/crates/langsmith-pyo3/src/lib.rs b/rust/crates/langsmith-pyo3/src/lib.rs deleted file mode 100644 index 5a03f0daed..0000000000 --- a/rust/crates/langsmith-pyo3/src/lib.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![allow(deprecated)] - -use pyo3::{pymodule, types::PyModule, Bound, PyResult, Python}; - -mod blocking_tracing_client; -mod errors; -mod py_run; -mod serialization; - -#[pymodule] -fn langsmith_pyo3(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - // Initialize orjson internal data structures. - orjson::init_typerefs(); - - blocking_tracing_client::register(py, m)?; - errors::register(py, m)?; - Ok(()) -} - -#[cfg(test)] -pub(crate) mod test_infra; diff --git a/rust/crates/langsmith-pyo3/src/py_run.rs b/rust/crates/langsmith-pyo3/src/py_run.rs deleted file mode 100644 index 31ceb6cdf0..0000000000 --- a/rust/crates/langsmith-pyo3/src/py_run.rs +++ /dev/null @@ -1,460 +0,0 @@ -use std::ffi::CStr; - -use langsmith_tracing_client::client::{Attachment, RunIO, TimeValue}; -use pyo3::{ - types::{ - PyAnyMethods as _, PyDateTime, PyMapping, PyMappingMethods, PySequence, PyString, PyTuple, - }, - Bound, FromPyObject, PyAny, PyResult, -}; - -use crate::{errors::TracingClientError, serialization}; - -#[derive(Debug)] -pub struct RunCreateExtended(langsmith_tracing_client::client::RunCreateExtended); - -impl RunCreateExtended { - #[inline] - pub(crate) fn into_inner(self) -> langsmith_tracing_client::client::RunCreateExtended { - self.0 - } -} - -impl FromPyObject<'_> for RunCreateExtended { - fn extract_bound(value: &Bound<'_, PyAny>) -> PyResult { - // Perform a runtime check that we've successfully located the `langsmith` Python code - // used to transform Python objects which aren't natively serializeable by `orjson`. - // - // This assertion ensures that we won't later fail to serialize e.g. Pydantic objects. - serialization::assert_orjson_default_is_present(); - - let run_create = value.extract::()?.into_inner(); - - let attachments = { - if let Ok(attachments_value) = value.get_item(pyo3::intern!(value.py(), "attachments")) - { - extract_attachments(&attachments_value)? - } else { - None - } - }; - - let io = RunIO { - inputs: serialize_optional_dict_value(value, pyo3::intern!(value.py(), "inputs"))?, - outputs: serialize_optional_dict_value(value, pyo3::intern!(value.py(), "outputs"))?, - }; - - Ok(Self(langsmith_tracing_client::client::RunCreateExtended { - run_create, - io, - attachments, - })) - } -} - -#[derive(Debug)] -pub struct RunUpdateExtended(langsmith_tracing_client::client::RunUpdateExtended); - -impl RunUpdateExtended { - #[inline] - pub(crate) fn into_inner(self) -> langsmith_tracing_client::client::RunUpdateExtended { - self.0 - } -} - -impl FromPyObject<'_> for RunUpdateExtended { - fn extract_bound(value: &Bound<'_, PyAny>) -> PyResult { - // Perform a runtime check that we've successfully located the `langsmith` Python code - // used to transform Python objects which aren't natively serializeable by `orjson`. - // - // This assertion ensures that we won't later fail to serialize e.g. Pydantic objects. - serialization::assert_orjson_default_is_present(); - - let run_update = value.extract::()?.into_inner(); - - // TODO: attachments are WIP at the moment, ignore them here for now. - // - // let attachments = { - // if let Ok(attachments_value) = value.get_item(pyo3::intern!(value.py(), "attachments")) - // { - // extract_attachments(&attachments_value)? - // } else { - // None - // } - // }; - let attachments = None; - - let io = RunIO { - inputs: serialize_optional_dict_value(value, pyo3::intern!(value.py(), "inputs"))?, - outputs: serialize_optional_dict_value(value, pyo3::intern!(value.py(), "outputs"))?, - }; - - Ok(Self(langsmith_tracing_client::client::RunUpdateExtended { - run_update, - io, - attachments, - })) - } -} - -#[derive(Debug)] -pub(crate) struct RunCreate(langsmith_tracing_client::client::RunCreate); - -impl RunCreate { - #[inline] - pub(crate) fn into_inner(self) -> langsmith_tracing_client::client::RunCreate { - self.0 - } -} - -impl FromPyObject<'_> for RunCreate { - fn extract_bound(value: &Bound<'_, PyAny>) -> PyResult { - let common = RunCommon::extract_bound(value)?.into_inner(); - let name = value.get_item(pyo3::intern!(value.py(), "name"))?.extract::()?; - - let start_time = - extract_time_value(&value.get_item(pyo3::intern!(value.py(), "start_time"))?)?; - - let end_time = { - match value.get_item(pyo3::intern!(value.py(), "end_time")) { - Ok(py_end_time) => { - if py_end_time.is_none() { - None - } else { - Some(extract_time_value(&py_end_time)?) - } - } - Err(_) => None, - } - }; - - let run_type = - value.get_item(pyo3::intern!(value.py(), "run_type"))?.extract::()?; - let reference_example_id = extract_string_like_or_none( - get_optional_value_from_mapping( - value, - pyo3::intern!(value.py(), "reference_example_id"), - ) - .as_ref(), - )?; - - Ok(Self(langsmith_tracing_client::client::RunCreate { - common, - name, - start_time, - end_time, - run_type, - reference_example_id, - })) - } -} - -#[derive(Debug)] -pub(crate) struct RunUpdate(langsmith_tracing_client::client::RunUpdate); - -impl FromPyObject<'_> for RunUpdate { - fn extract_bound(value: &Bound<'_, PyAny>) -> PyResult { - let common = RunCommon::extract_bound(value)?.into_inner(); - - let end_time = extract_time_value(&value.get_item(pyo3::intern!(value.py(), "end_time"))?)?; - - Ok(Self(langsmith_tracing_client::client::RunUpdate { common, end_time })) - } -} - -impl RunUpdate { - #[inline] - pub(crate) fn into_inner(self) -> langsmith_tracing_client::client::RunUpdate { - self.0 - } -} - -#[derive(Debug)] -pub(crate) struct RunCommon(langsmith_tracing_client::client::RunCommon); - -impl RunCommon { - #[inline] - pub(crate) fn into_inner(self) -> langsmith_tracing_client::client::RunCommon { - self.0 - } -} - -impl FromPyObject<'_> for RunCommon { - fn extract_bound(value: &Bound<'_, PyAny>) -> PyResult { - let id = extract_string_like(&value.get_item(pyo3::intern!(value.py(), "id"))?)?; - let trace_id = - extract_string_like(&value.get_item(pyo3::intern!(value.py(), "trace_id"))?)?; - - let dotted_order = value.get_item(pyo3::intern!(value.py(), "dotted_order"))?.extract()?; - let parent_run_id = extract_string_like_or_none( - get_optional_value_from_mapping(value, pyo3::intern!(value.py(), "parent_run_id")) - .as_ref(), - )?; - - let extra = extract_optional_value_from_mapping(value, pyo3::intern!(value.py(), "extra"))?; - - let error = extract_string_like_or_none( - get_optional_value_from_mapping(value, pyo3::intern!(value.py(), "error")).as_ref(), - )?; - - let serialized = - extract_optional_value_from_mapping(value, pyo3::intern!(value.py(), "serialized"))?; - let events = - extract_optional_value_from_mapping(value, pyo3::intern!(value.py(), "events"))?; - let tags = extract_optional_value_from_mapping(value, pyo3::intern!(value.py(), "tags"))?; - - let session_id = extract_string_like_or_none( - get_optional_value_from_mapping(value, pyo3::intern!(value.py(), "session_id")) - .as_ref(), - )?; - let session_name = extract_string_like_or_none( - get_optional_value_from_mapping(value, pyo3::intern!(value.py(), "session_name")) - .as_ref(), - )?; - - Ok(Self(langsmith_tracing_client::client::RunCommon { - id, - trace_id, - dotted_order, - parent_run_id, - extra, - error, - serialized, - events, - tags, - session_id, - session_name, - })) - } -} - -fn extract_attachments(value: &Bound<'_, PyAny>) -> PyResult>> { - if value.is_none() { - return Ok(None); - } - - let mapping = value.downcast::()?; - - let size = mapping.len()?; - if size == 0 { - return Ok(None); - } - - let mut attachments = Vec::with_capacity(size); - - for result in mapping.items()?.iter()? { - let key_value_pair = result?; - - let key_item = key_value_pair.get_item(0)?; - let key = key_item.extract::<&str>()?; - - // Each value in the attachments dict is a (mime_type, bytes) tuple. - let value = key_value_pair.get_item(1)?; - let value_tuple = value.downcast_exact::()?; - let mime_type_value = value_tuple.get_item(0)?; - let bytes_value = value_tuple.get_item(1)?; - - attachments.push(Attachment { - // TODO: It's unclear whether the key in the attachments dict is - // the `filename`` or the `ref_name`, and where the other one is coming from. - ref_name: key.to_string(), - filename: key.to_string(), - data: bytes_value.extract()?, - content_type: mime_type_value.extract()?, - }); - } - - Ok(Some(attachments)) -} - -/// Get an optional string from a Python `None`, string, or string-like object such as a UUID value. -fn extract_string_like_or_none(value: Option<&Bound<'_, PyAny>>) -> PyResult> { - match value { - None => Ok(None), - Some(val) if val.is_none() => Ok(None), - Some(val) => extract_string_like(val).map(Option::Some), - } -} - -/// Get a string from a Python string or string-like object, such as a UUID value. -fn extract_string_like(value: &Bound<'_, PyAny>) -> PyResult { - match value.extract::() { - Ok(s) => Ok(s), - Err(e) => { - // PyO3 doesn't have a Rust-native representation of Python's UUID object yet. - // However, orjson supports serializing UUID objects, so the easiest way to get - // a Rust string from a Python UUID object is to serialize the UUID to a JSON string - // and then parse out the string. - let Ok(buffer) = serialization::dumps(value.as_ptr()) else { - // orjson failed to deserialize the object. The fact that orjson is involved - // is an internal implementation detail, so return the original error instead. - // It looks like this: - // `'SomeType' object cannot be converted to 'PyString'` - return Err(e); - }; - - let content = CStr::from_bytes_until_nul(&buffer) - .expect("not a valid C string, this should never happen") - .to_str() - .expect("not a valid UTF-8 string, this should never happen"); - - // orjson serialized buffers are null-terminated, so strip the trailing - // If the remaining value didn't start or end with a quote, it wasn't string-like. - // It might have been a number, dict, or list -- none of those are legal here. - // Raise the original error again, for the same reason as above. - let string_content = - content.strip_prefix('"').and_then(|s| s.strip_suffix('"')).ok_or(e)?.to_string(); - Ok(string_content) - } - } -} - -fn extract_time_value(value: &Bound<'_, PyAny>) -> PyResult { - if let Ok(string) = value.extract::() { - return Ok(TimeValue::String(string)); - } - - let datetime = value.downcast::()?; - let isoformat = - datetime.call_method0(pyo3::intern!(value.py(), "isoformat"))?.extract::()?; - Ok(TimeValue::String(isoformat)) -} - -fn get_optional_value_from_mapping<'py>( - mapping: &Bound<'py, PyAny>, - key: &Bound<'py, PyString>, -) -> Option> { - mapping.get_item(key).ok() -} - -fn serialize_optional_dict_value( - mapping: &Bound<'_, PyAny>, - key: &Bound<'_, PyString>, -) -> PyResult>> { - match mapping.get_item(key) { - Ok(value) => { - if value.is_none() { - return Ok(None); - } - serialization::dumps(value.as_ptr()) - .map(Option::Some) - .map_err(TracingClientError::new_err) - } - Err(_) => Ok(None), - } -} - -// TODO: `Option` seems suspect as a type, since `Value` can be null already. -// It might be unnecessarily large and slowing us down for no reason. -fn extract_optional_value_from_mapping( - mapping: &Bound<'_, PyAny>, - key: &Bound<'_, PyString>, -) -> PyResult> { - match mapping.get_item(key) { - Ok(value) => { - if value.is_none() { - return Ok(None); - } - extract_value(&value).map(Option::Some) - } - Err(_) => Ok(None), - } -} - -fn extract_value(value: &Bound<'_, PyAny>) -> PyResult { - if value.is_none() { - Ok(serde_json::Value::Null) - } else if let Ok(number) = value.extract::() { - Ok(number.into()) - } else if let Ok(number) = value.extract::() { - Ok(number.into()) - } else if let Ok(float) = value.extract::() { - Ok(serde_json::Number::from_f64(float).map(serde_json::Value::from).unwrap_or_default()) - } else if let Ok(string) = value.extract::<&str>() { - Ok(string.into()) - } else if let Ok(bool) = value.extract::() { - Ok(bool.into()) - } else if let Ok(sequence) = value.downcast::() { - let mut array = Vec::with_capacity(sequence.len()?); - - for elem in sequence.iter()? { - array.push(extract_value(&elem?)?); - } - - Ok(serde_json::Value::Array(array)) - } else if let Ok(mapping) = value.downcast::() { - let mut dict = serde_json::Map::with_capacity(mapping.len()?); - - for result in mapping.items()?.iter()? { - let key_value_pair = result?; - - let key_item = key_value_pair.get_item(0)?; - let value = extract_value(&key_value_pair.get_item(1)?)?; - - // We error on non-string-like keys here. - let key = extract_string_like(&key_item)?; - dict.insert(key, value); - } - - Ok(dict.into()) - } else if let Ok(string_like) = extract_string_like(value) { - // This allows us to support Python `UUID` objects by serializing them to strings. - Ok(string_like.into()) - } else { - unreachable!("failed to convert python data {value} to sonic_rs::Value") - } -} - -#[cfg(test)] -mod tests { - use crate::test_infra::with_python_interpreter; - use pyo3::{prelude::*, types::PyDict}; - - #[pyfunction] - fn extract_uuid(uuid_value: &Bound<'_, PyAny>, string_value: &str) { - let extracted = super::extract_string_like(uuid_value).expect("extraction failed"); - assert_eq!(extracted.as_str(), string_value); - } - - #[test] - fn test_uuid_value_extraction() { - fn inner(py: Python<'_>) -> PyResult<()> { - // This call only works correctly "the first time". - // We use `cargo-nextest` to ensure we run each test in its own process. - // Otherwise, tests will suffer unpredictable errors. - orjson::init_typerefs(); - - // Create a new test module. - let test_module = PyModule::new_bound(py, "test_module")?; - test_module.add_function(pyo3::wrap_pyfunction!(extract_uuid, &test_module)?)?; - - // Get `sys.modules`, then insert our module into it. - let sys = PyModule::import_bound(py, "sys")?; - let py_modules: Bound<'_, PyDict> = sys.getattr("modules")?.downcast_into()?; - py_modules.set_item("test_module", test_module)?; - - // Now we can import and run our python code. - let python_code = "\ -import uuid -import test_module - -uuid_to_test = uuid.uuid4() - -test_module.extract_uuid(uuid_to_test, str(uuid_to_test)) - "; - Python::run_bound(py, python_code, None, None)?; - - Ok(()) - } - - with_python_interpreter(inner).expect("encountered an unexpected error") - } - - /// Just to ensure that running multiple tests works fine. - /// If Python or orjson are initialized more than once per process, - /// either this test or another test will fail. - #[test] - fn other_test() { - test_uuid_value_extraction(); - } -} diff --git a/rust/crates/langsmith-pyo3/src/serialization/mod.rs b/rust/crates/langsmith-pyo3/src/serialization/mod.rs deleted file mode 100644 index 44458338ab..0000000000 --- a/rust/crates/langsmith-pyo3/src/serialization/mod.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::ptr::NonNull; - -use pyo3::types::PyAnyMethods as _; - -mod writer; - -thread_local! { - static ORJSON_DEFAULT: Result, String> = { - pyo3::Python::with_gil(|py| { - let module = match py.import("langsmith._internal._serde") { - Ok(m) => m, - Err(e) => { - let _ = py.import("langsmith").map_err(|_| "failed to import `langsmith` package; please make sure `langsmith-pyo3` is only used via the `langsmith` package".to_string())?; - return Err(format!("Failed to import `langsmith._internal._serde` even though `langsmith` can be imported. Did internal `langsmith` package structure change? Underlying error: {e}")); - } - }; - - let function = module.getattr("_serialize_json").map_err(|e| format!("`_serialize_json` function not found; underlying error: {e}"))?.as_ptr(); - Ok(NonNull::new(function).expect("function was null, which shouldn't ever happen")) - }) - } -} - -/// Perform a runtime check that we've successfully located the `langsmith` Python code -/// used to transform Python objects which aren't natively serializeable by `orjson`. -/// -/// This assertion ensures that we won't later fail to serialize e.g. Pydantic objects. -/// -/// The cost of this call is trivial: just one easily branch-predictable comparison on -/// an already-initialized thread-local. -pub(crate) fn assert_orjson_default_is_present() { - ORJSON_DEFAULT.with(|res| { - if let Err(e) = res { - panic!("{e}"); - } - }) -} - -pub(crate) fn dumps(ptr: *mut pyo3_ffi::PyObject) -> Result, String> { - let mut writer = writer::BufWriter::new(); - - ORJSON_DEFAULT.with(|default| { - let obj = orjson::PyObjectSerializer::new( - ptr, - orjson::SerializerState::new(Default::default()), - default.as_ref().cloned().ok(), - ); - - let res = orjson::to_writer(&mut writer, &obj); - match res { - Ok(_) => Ok(writer.finish()), - Err(err) => { - // Make sure we drop the allocated buffer. - let _ = writer.into_inner(); - Err(err.to_string()) - } - } - }) -} diff --git a/rust/crates/langsmith-pyo3/src/serialization/writer.rs b/rust/crates/langsmith-pyo3/src/serialization/writer.rs deleted file mode 100644 index 2d0136d91e..0000000000 --- a/rust/crates/langsmith-pyo3/src/serialization/writer.rs +++ /dev/null @@ -1,216 +0,0 @@ -use std::io::Error; - -/// Writer that points directly to a `[u8]` buffer. -/// -/// # Safety -/// The external interface of this type is fully safe. -/// -/// An internal invariant is that the `(buf, len, cap)` fields must be equivalent to a valid `Vec` -/// both at the start and conclusion of any method call: -/// - `self.buf` points to valid, initialized memory to which `Self` has exclusive access -/// - `self.len` is a legal offset that remains in bounds when used with `self.buf`. -/// - `self.cap` accurately shows how much capacity the `self.buf` memory region has. -pub(super) struct BufWriter { - buf: *mut u8, - len: usize, - cap: usize, -} - -impl BufWriter { - const DEFAULT_CAPACITY: usize = 1024; - - pub(super) fn new() -> Self { - let buffer = Vec::with_capacity(Self::DEFAULT_CAPACITY); - - let (buf, len, cap) = { - // Prevent the `Vec` from being dropped at the end of this scope. - let mut buffer = std::mem::ManuallyDrop::new(buffer); - - // Get the `Vec`'s components. - let buf = buffer.as_mut_ptr(); - let len = buffer.len(); - let cap = buffer.capacity(); - - // We now own the `Vec`'s backing data. - // The `Vec` goes out of scope but will not be dropped - // due to being wrapped in `ManuallyDrop`. - (buf, len, cap) - }; - - // SAFETY: These values are derived from a valid `Vec` that has not been dropped. - Self { buf, len, cap } - } - - pub(super) fn finish(mut self) -> Vec { - let end_length = self.len + 1; - if end_length >= self.cap { - self.reserve_at_least_capacity(end_length); - } - - // SAFETY: We just ensured there's enough room in the buffer. - unsafe { - core::ptr::write(self.buffer_ptr(), 0); - } - - // Difference with orjson: Python doesn't count the terminating `'\0'` in the length - // of the string, but `Vec` does. So orjson doesn't add one here, but we must - // or else we'll get unsoundness and a memory safety violation. - self.len = end_length; - - self.into_inner() - } - - pub(super) fn into_inner(self) -> Vec { - // SAFETY: We constructed the `Vec` in `Self::new()`, - // and maintained the `Vec` invariants throughout all `Self` methods. - unsafe { Vec::from_raw_parts(self.buf, self.len, self.cap) } - } - - fn buffer_ptr(&self) -> *mut u8 { - // SAFETY: The length must be in bounds at all times, or else we've already violated - // another invariant elsewhere. - unsafe { self.buf.add(self.len) } - } - - #[inline] - pub fn reserve_at_least_capacity(&mut self, cap: usize) { - // SAFETY: The buffer used to be a vector, and is exclusively owned by us. - // The `&mut self` here guarantees there can't be another mutable reference to it. - // It's safe to turn it back into a `Vec` and ask the `Vec` to resize itself. - // After resizing, we deconstruct the `Vec` *and* ensure it isn't dropped, - // meaning that the memory is still live and not use-after-free'd. - unsafe { - // SAFETY: `self`'s `(buf, len, cap)` are no longer valid for accessing data - // after the next line, until they are reassigned new values. - let mut buffer = Vec::from_raw_parts(self.buf, self.len, self.cap); - - buffer.reserve(cap - self.cap); - - // Prevent the `Vec` from being dropped at the end of this scope. - let mut buffer = std::mem::ManuallyDrop::new(buffer); - - // Get the `Vec`'s components. - let buf = buffer.as_mut_ptr(); - let len = buffer.len(); - let cap = buffer.capacity(); - - // SAFETY: `self`'s `(buf, len, cap)` values are valid again from this point onward. - // We own the `Vec`'s backing data. The `Vec` goes out of scope but - // will not be dropped due to being wrapped in `ManuallyDrop`. - (self.buf, self.len, self.cap) = (buf, len, cap); - } - } -} - -impl std::io::Write for BufWriter { - fn write(&mut self, buf: &[u8]) -> std::io::Result { - let _ = self.write_all(buf); - Ok(buf.len()) - } - - fn write_all(&mut self, buf: &[u8]) -> Result<(), Error> { - let to_write = buf.len(); - let end_length = self.len + to_write; - if end_length >= self.cap { - self.reserve_at_least_capacity(end_length); - } - - // SAFETY: We never expose pointers to our internal buffer through the API, - // so we couldn't have gotten an overlapping buffer here. - unsafe { - core::ptr::copy_nonoverlapping(buf.as_ptr(), self.buffer_ptr(), to_write); - }; - self.len = end_length; - - Ok(()) - } - - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } -} - -impl orjson::WriteExt for &mut BufWriter { - #[inline(always)] - fn as_mut_buffer_ptr(&mut self) -> *mut u8 { - self.buffer_ptr() - } - - #[inline(always)] - fn reserve(&mut self, len: usize) { - let end_length = self.len + len; - if end_length >= self.cap { - self.reserve_at_least_capacity(end_length); - } - } - - #[inline] - fn has_capacity(&mut self, len: usize) -> bool { - self.len + len <= self.cap - } - - #[inline(always)] - fn set_written(&mut self, len: usize) { - self.len += len; - } - - fn write_str(&mut self, val: &str) -> Result<(), Error> { - let to_write = val.len(); - let end_length = self.len + to_write + 2; - if end_length >= self.cap { - self.reserve_at_least_capacity(end_length); - } - - // SAFETY: We ensured there's enough room in the buffer. The write is non-overlapping - // because we never hand out pointers to our internal buffer via our API. - unsafe { - let ptr = self.buffer_ptr(); - core::ptr::write(ptr, b'"'); - core::ptr::copy_nonoverlapping(val.as_ptr(), ptr.add(1), to_write); - core::ptr::write(ptr.add(to_write + 1), b'"'); - }; - self.len = end_length; - Ok(()) - } - - /// # Safety - /// - /// The caller must ensure they've reserved sufficient space in advance. - unsafe fn write_reserved_fragment(&mut self, val: &[u8]) -> Result<(), Error> { - let to_write = val.len(); - // SAFETY: The write is non-overlapping because we never hand out pointers to - // our internal buffer via our API. We must have enough space since the caller - // is required to have made sure of that already. - unsafe { - core::ptr::copy_nonoverlapping(val.as_ptr(), self.buffer_ptr(), to_write); - }; - self.len += to_write; - Ok(()) - } - - /// # Safety - /// - /// The caller must ensure they've reserved sufficient space in advance. - #[inline(always)] - unsafe fn write_reserved_punctuation(&mut self, val: u8) -> Result<(), Error> { - // SAFETY: We must have enough space since the caller - // is required to have made sure of that already. - unsafe { core::ptr::write(self.buffer_ptr(), val) }; - self.len += 1; - Ok(()) - } - - /// # Safety - /// - /// The caller must ensure they've reserved sufficient space in advance. - #[inline(always)] - unsafe fn write_reserved_indent(&mut self, len: usize) -> Result<(), Error> { - // SAFETY: We must have enough space since the caller - // is required to have made sure of that already. - unsafe { - core::ptr::write_bytes(self.buffer_ptr(), b' ', len); - }; - self.len += len; - Ok(()) - } -} diff --git a/rust/crates/langsmith-pyo3/src/test_infra.rs b/rust/crates/langsmith-pyo3/src/test_infra.rs deleted file mode 100644 index 6f86991349..0000000000 --- a/rust/crates/langsmith-pyo3/src/test_infra.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::sync::Mutex; - -use pyo3::{PyResult, Python}; - -// Python assumes it's the "only Python in the process", -// so embedding multiple Python interpreters in the same process will fail -// with obscure and difficult-to-debug errors. -// -// By default, `cargo test` runs tests on multiple threads. -// If multiple threads want to run a Python-related test, -// we have to make sure they don't attempt to initialize -// multiple Python interpreters concurrently. -// -// We use this mutex to coordinate creating Python interpreters. -static PYTHON_INTERPRETER: Mutex<()> = Mutex::new(()); - -pub(crate) fn with_python_interpreter(inner: F) -> Result<(), String> -where - F: for<'py> FnOnce(Python<'py>) -> PyResult<()>, -{ - let lock = PYTHON_INTERPRETER.lock().expect("lock was poisoned"); - - // SAFETY: - // - We have acquired the interpreter mutex. - // No other Python interpreters can be in existence. - // - We do not return any Python-owned data, since we turn the `PyResult<()>`'s error case - // into a Rust string. - let outcome = unsafe { - pyo3::with_embedded_python_interpreter(move |py| { - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<(), String> { - let outcome = inner(py); - outcome.map_err(|e| e.to_string()) - })) - }) - }; - drop(lock); - - match outcome { - Ok(r) => r, - Err(panicked) => { - // Running the closure caused a panic, - // so resume panicking now that we've cleaned up Python. - std::panic::resume_unwind(panicked) - } - } -} diff --git a/rust/crates/langsmith-tracing-client/Cargo.toml b/rust/crates/langsmith-tracing-client/Cargo.toml deleted file mode 100644 index f8be256d02..0000000000 --- a/rust/crates/langsmith-tracing-client/Cargo.toml +++ /dev/null @@ -1,36 +0,0 @@ -[package] -name = "langsmith-tracing-client" -version = "0.1.0" -edition = "2021" - -[dependencies] -tokio = { workspace = true } -uuid = { workspace = true } -serde_json = { workspace = true } -serde = { workspace = true } -chrono = { workspace = true } -reqwest = { workspace = true } -thiserror = { workspace = true } -tokio-util = { workspace = true } -tempfile = { workspace = true } -futures = { workspace = true } -rayon = { workspace = true } -ureq = { workspace = true } -flate2 = { workspace = true } -fastrand = { workspace = true } -crossbeam-channel = { workspace = true } -http = { workspace = true } -zstd = { workspace = true } - -[dev-dependencies] -multer = "3.1.0" -mockito = "1.5.0" -criterion = { version = "0.5.1", features = ["async_tokio"] } - -[[bench]] -name = "tracing_client_benchmark" -harness = false - -[[bench]] -name = "json_serialization_benchmark" -harness = false diff --git a/rust/crates/langsmith-tracing-client/benches/json_serialization_benchmark.rs b/rust/crates/langsmith-tracing-client/benches/json_serialization_benchmark.rs deleted file mode 100644 index 3f61adc35d..0000000000 --- a/rust/crates/langsmith-tracing-client/benches/json_serialization_benchmark.rs +++ /dev/null @@ -1,237 +0,0 @@ -use rayon::prelude::*; -use std::io::prelude::*; -use std::io::Cursor; -use std::time::Instant; -// use serde_json::Value; -use criterion::{criterion_group, criterion_main, Criterion}; -use mockito::Server; -use reqwest::blocking::multipart::{Form, Part}; -use serde_json::Value; -use uuid::Uuid; - -fn create_json_with_large_array(len: usize) -> Value { - let large_array: Vec = (0..len) - .map(|i| { - serde_json::json!({ - "index": i, - "data": format!("This is element number {}", i), - "nested": { - "id": i, - "value": format!("Nested value for element {}", i), - } - }) - }) - .collect(); - - serde_json::json!({ - "name": "Huge JSON", - "description": "This is a very large JSON object for benchmarking purposes.", - "array": large_array, - "metadata": { - "created_at": "2024-10-22T19:00:00Z", - "author": "Rust Program", - "version": 1.0 - } - }) -} - -fn create_json_with_large_strings(len: usize) -> Value { - let large_string = "a".repeat(len); - serde_json::json!({ - "name": "Huge JSON", - "description": "This is a very large JSON object for benchmarking purposes.", - "key1": large_string.clone(), - "key2": large_string.clone(), - "key3": large_string.clone(), - "metadata": { - "created_at": "2024-10-22T19:00:00Z", - "author": "Rust Program", - "version": 1.0 - } - }) -} - -// Sequential processing -fn benchmark_sequential(data: &[Value]) -> Vec> { - data.iter() - .map(|json| { - let data = serde_json::to_vec(json).expect("Failed to serialize JSON"); - // gzip the data using flate2 - let mut encoder = - flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast()); - encoder.write_all(&data).expect("Failed to compress data"); - encoder.finish().expect("Failed to finish compression") - }) - .collect() -} - -// Parallel processing -fn benchmark_parallel(data: &[Value]) -> Vec> { - data.par_iter() - .map(|json| { - let data = serde_json::to_vec(json).expect("Failed to serialize JSON"); - // gzip the data using flate2 - let mut encoder = - flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast()); - encoder.write_all(&data).expect("Failed to compress data"); - encoder.finish().expect("Failed to finish compression") - }) - .collect() -} - -fn benchmark_gzip_only_parallel(data: &Vec>) -> Vec> { - data.par_iter() - .map(|data| { - // gzip the data using flate2 - let mut encoder = - flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast()); - encoder.write_all(data).expect("Failed to compress data"); - encoder.finish().expect("Failed to finish compression") - }) - .collect() -} - -// into par iter -fn benchmark_json_only_parallel(data: &[Value]) -> Vec> { - data.par_iter() - .map(|json| serde_json::to_vec(json).expect("Failed to serialize JSON")) - .collect() -} - -fn json_benchmark_large_array(c: &mut Criterion) { - let num_json_objects = 2000; - let json_length = 5000; - let data: Vec = - (0..num_json_objects).map(|_| create_json_with_large_array(json_length)).collect(); - - let serialized_data = benchmark_json_only_parallel(&data); - - let mut group = c.benchmark_group("json_benchmark_large_array"); - group.bench_function("sequential serialization with gzip", |b| { - b.iter_with_large_drop(|| benchmark_sequential(&data)) - }); - group.bench_function("parallel serialization with gzip", |b| { - b.iter_with_large_drop(|| benchmark_parallel(&data)) - }); - group.bench_function("parallel serialization only", |b| { - b.iter_with_large_drop(|| benchmark_json_only_parallel(&data)) - }); - group.bench_function("parallel gzip only", |b| { - b.iter_with_large_drop(|| benchmark_gzip_only_parallel(&serialized_data)) - }); -} - -#[expect(dead_code)] -fn json_benchmark_large_strings(c: &mut Criterion) { - let num_json_objects = 100; - let json_length = 100_000; - let data: Vec = - (0..num_json_objects).map(|_| create_json_with_large_strings(json_length)).collect(); - - let mut group = c.benchmark_group("json_benchmark_large_strings"); - group.bench_function("sequential serialization", |b| { - b.iter_with_large_drop(|| benchmark_sequential(&data)) - }); - group.bench_function("parallel serialization", |b| { - b.iter_with_large_drop(|| benchmark_parallel(&data)) - }); -} - -#[expect(dead_code)] -fn hitting_mock_server_benchmark(c: &mut Criterion) { - let server = { - let mut server = Server::new(); - server.mock("POST", "/runs/multipart").with_status(202).create(); - server - }; - - let mut group = c.benchmark_group("hitting_mock_server_benchmark"); - let reqwest = reqwest::blocking::Client::new(); - group.bench_function("hitting mock server with reqwest", |b| { - b.iter_custom(|_iters| { - let num_json_objects = 300; - let json_length = 3000; - let data: Vec = - (0..num_json_objects).map(|_| create_json_with_large_array(json_length)).collect(); - - let bytes: Vec = data - .par_iter() - .map(|json| { - let data = serde_json::to_vec(json).expect("Failed to serialize JSON"); - Part::bytes(data) - .file_name("part".to_string()) - .mime_str("application/json") - .unwrap() - }) - .collect(); - - let mut form = Form::new(); - for (i, part) in bytes.into_iter().enumerate() { - let part_name = format!("part{}", i); - form = form.part(part_name, part); - } - - let start = Instant::now(); - let response = reqwest - .post(format!("{}/runs/multipart", server.url())) - .multipart(form) - .send() - .unwrap(); - assert_eq!(response.status(), 202); - start.elapsed() - }); - }); - - // now let's try ureq - let ureq = ureq::Agent::new(); - group.bench_function("hitting mock server with ureq", |b| { - b.iter_custom(|_iters| { - let num_json_objects = 300; - let json_length = 3000; - let data: Vec = - (0..num_json_objects).map(|_| create_json_with_large_array(json_length)).collect(); - - let bytes: Vec> = data - .par_iter() - .map(|json| serde_json::to_vec(json).expect("Failed to serialize JSON")) - .collect(); - - let mut multipart_body = Vec::new(); - let boundary = format!("------------------------{}", Uuid::new_v4()); - - for (i, data_bytes) in bytes.iter().enumerate() { - multipart_body.extend_from_slice(format!("--{}\r\n", boundary).as_bytes()); - multipart_body.extend_from_slice( - format!("Content-Disposition: form-data; name=\"part{}\"\r\n", i).as_bytes(), - ); - multipart_body.extend_from_slice(b"Content-Type: application/json\r\n\r\n"); - multipart_body.extend_from_slice(data_bytes); - multipart_body.extend_from_slice(b"\r\n"); - } - multipart_body.extend_from_slice(format!("--{}--\r\n", boundary).as_bytes()); - - // Convert the multipart body to a Cursor for reading - let body_reader = Cursor::new(multipart_body); - - // Send the request - let start = Instant::now(); - let response = ureq - .post(&format!("{}/runs/multipart", server.url())) - .set("Content-Type", &format!("multipart/form-data; boundary={}", boundary)) - .send(body_reader); - - assert_eq!(response.unwrap().status(), 202); - start.elapsed() - }); - }); -} - -criterion_group! { - name = benches; - config = Criterion::default().sample_size(10); - targets = json_benchmark_large_array -} -criterion_main!(benches); - -// fn main() { -// } diff --git a/rust/crates/langsmith-tracing-client/benches/tracing_client_benchmark.rs b/rust/crates/langsmith-tracing-client/benches/tracing_client_benchmark.rs deleted file mode 100644 index 19eb2c02e8..0000000000 --- a/rust/crates/langsmith-tracing-client/benches/tracing_client_benchmark.rs +++ /dev/null @@ -1,151 +0,0 @@ -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; -use langsmith_tracing_client::client::{ - Attachment, ClientConfig as BlockingClientConfig, RunCommon, RunCreate, RunCreateExtended, - RunIO, TimeValue, TracingClient as BlockingTracingClient, -}; -use mockito::Server; -use serde_json::{json, Value}; -use std::time::Duration; - -fn create_mock_client_config_sync(server_url: &str, batch_size: usize) -> BlockingClientConfig { - BlockingClientConfig { - endpoint: server_url.to_string(), - api_key: "anything".into(), - queue_capacity: 1_000_000, - send_at_batch_size: batch_size, - send_at_batch_time: Duration::from_secs(1), - headers: Default::default(), - compression_level: 1, - } -} - -fn create_run_create( - attachments: Option>, - inputs: Option, - outputs: Option, -) -> RunCreateExtended { - RunCreateExtended { - run_create: RunCreate { - common: RunCommon { - id: String::from("test_id"), - trace_id: String::from("trace_id"), - dotted_order: String::from("1.1"), - parent_run_id: None, - extra: Some(json!({"extra_data": "value"})), - error: None, - serialized: Some(json!({"key": "value"})), - events: Some(json!([{ "event": "event_data" }])), - tags: Some(json!(["tag1", "tag2"])), - session_id: None, - session_name: Some("Session Name".to_string()), - }, - name: String::from("Run Name"), - start_time: TimeValue::UnsignedInt(1697462400000), - end_time: Some(TimeValue::UnsignedInt(1697466000000)), - run_type: String::from("chain"), - reference_example_id: None, - }, - attachments, - io: RunIO { - inputs: inputs.map(|i| serde_json::to_vec(&i).unwrap()), - outputs: outputs.map(|i| serde_json::to_vec(&i).unwrap()), - }, - } -} - -fn create_large_json(len: usize) -> Value { - let large_array: Vec = (0..len) - .map(|i| { - json!({ - "index": i, - "data": format!("This is element number {}", i), - "nested": { - "id": i, - "value": format!("Nested value for element {}", i), - } - }) - }) - .collect(); - - json!({ - "name": "Huge JSON", - "description": "This is a very large JSON object for benchmarking purposes.", - "array": large_array, - "metadata": { - "created_at": "2024-10-22T19:00:00Z", - "author": "Rust Program", - "version": 1.0 - } - }) -} - -#[expect(unused_variables, clippy::single_element_loop)] -fn bench_run_create_sync_iter_custom(c: &mut Criterion) { - let server = { - let mut server = Server::new(); - server.mock("POST", "/runs/multipart").with_status(202).create(); - server - }; - - let mut group = c.benchmark_group("run_create_custom_iter"); - let server_url = server.url(); - for batch_size in [100] { - for json_len in [5_000] { - for num_runs in [1_000] { - group.bench_function( - BenchmarkId::new( - "run_create_sync", - format!("batch_{}_json_{}_runs_{}", batch_size, json_len, num_runs), - ), - |b| { - b.iter_custom(|iters| { - let mut elapsed_time = Duration::default(); - let server_url = server_url.clone(); - for _ in 0..iters { - let runs: Vec = (0..num_runs) - .map(|i| { - let mut run = create_run_create( - None, - Some(create_large_json(json_len)), - Some(create_large_json(json_len)), - ); - run.run_create.common.id = format!("test_id_{}", i); - run - }) - .collect(); - let client_config = - create_mock_client_config_sync(&server_url, batch_size); - let client = BlockingTracingClient::new(client_config).unwrap(); - - let start = std::time::Instant::now(); - for run in runs { - std::hint::black_box( - client.submit_run_create(std::hint::black_box(run)), - ) - .unwrap(); - } - - // shutdown the client to flush the queue - let start_shutdown = std::time::Instant::now(); - std::hint::black_box(client.shutdown()).unwrap(); - // println!("Elapsed time for shutdown: {:?}", start_shutdown.elapsed()); - elapsed_time += start.elapsed(); - println!("Elapsed time: {:?}", elapsed_time); - } - elapsed_time - }) - }, - ); - } - } - } - group.finish(); -} - -criterion_group! { - name = benches; - config = Criterion::default().sample_size(10); - targets = bench_run_create_sync_iter_custom -} - -criterion_main!(benches); diff --git a/rust/crates/langsmith-tracing-client/src/client/errors.rs b/rust/crates/langsmith-tracing-client/src/client/errors.rs deleted file mode 100644 index 8a345dea49..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/errors.rs +++ /dev/null @@ -1,31 +0,0 @@ -use thiserror::Error; - -#[derive(Error, Debug)] -pub enum TracingClientError { - #[error("Queue is full")] - QueueFull, - - #[error("Serialization error: {0}")] - SerializationError(#[from] serde_json::Error), - - #[error("HTTP error: {0}")] - HttpError(reqwest::StatusCode), - - #[error("Request error: {0}")] - RequestError(#[from] reqwest::Error), - - #[error("Channel send error")] - ChannelSendError, - - #[error("Unexpected shutdown")] - UnexpectedShutdown, - - #[error("IO error")] - IoError(String), -} - -impl From for TracingClientError { - fn from(value: std::io::Error) -> Self { - Self::IoError(value.to_string()) - } -} diff --git a/rust/crates/langsmith-tracing-client/src/client/mod.rs b/rust/crates/langsmith-tracing-client/src/client/mod.rs deleted file mode 100644 index 7a41594544..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -mod errors; -mod run; - -mod streaming; - -pub use errors::TracingClientError; -pub use run::{ - Attachment, RunCommon, RunCreate, RunCreateExtended, RunIO, RunUpdate, RunUpdateExtended, - TimeValue, -}; -pub use streaming::{ClientConfig, TracingClient}; diff --git a/rust/crates/langsmith-tracing-client/src/client/run.rs b/rust/crates/langsmith-tracing-client/src/client/run.rs deleted file mode 100644 index 5eb6f58ccc..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/run.rs +++ /dev/null @@ -1,124 +0,0 @@ -use serde::{Deserialize, Serialize}; -use serde_json::Value; - -// Map attachment ref to tuple of filename, optional bytes -#[derive(Debug)] -pub struct Attachment { - pub ref_name: String, - pub filename: String, - pub data: Option>, - pub content_type: String, -} - -// Must support both string (Py) and unsigned int (JS) -#[derive(Serialize, Deserialize, PartialEq, Debug)] -#[serde(untagged)] -pub enum TimeValue { - String(String), - UnsignedInt(u64), -} - -#[derive(PartialEq, Debug)] -pub struct RunIO { - pub inputs: Option>, - pub outputs: Option>, -} - -impl RunIO { - #[allow(dead_code)] - #[inline] - pub(crate) fn merge(&mut self, other: RunIO) { - if other.inputs.is_some() { - self.inputs = other.inputs; - } - if other.outputs.is_some() { - self.outputs = other.outputs; - } - } -} - -#[derive(Serialize, Deserialize, PartialEq, Debug)] -pub struct RunCommon { - pub id: String, - pub trace_id: String, - pub dotted_order: String, - pub parent_run_id: Option, - pub extra: Option, - pub error: Option, - pub serialized: Option, - pub events: Option, - pub tags: Option, - pub session_id: Option, - pub session_name: Option, -} - -impl RunCommon { - #[allow(dead_code)] - #[inline] - pub(crate) fn merge(&mut self, other: RunCommon) { - if other.parent_run_id.is_some() { - self.parent_run_id = other.parent_run_id; - } - if other.extra.is_some() { - self.extra = other.extra; - } - if other.error.is_some() { - self.error = other.error; - } - if other.serialized.is_some() { - self.serialized = other.serialized; - } - if other.events.is_some() { - self.events = other.events; - } - if other.tags.is_some() { - self.tags = other.tags; - } - if other.session_id.is_some() { - self.session_id = other.session_id; - } - if other.session_name.is_some() { - self.session_name = other.session_name; - } - } -} - -#[derive(Serialize, Deserialize, PartialEq, Debug)] -pub struct RunCreate { - #[serde(flatten)] - pub common: RunCommon, - pub name: String, - pub start_time: TimeValue, - pub end_time: Option, - pub run_type: String, - pub reference_example_id: Option, -} - -#[derive(Serialize, Deserialize, PartialEq, Debug)] -pub struct RunUpdate { - #[serde(flatten)] - pub common: RunCommon, - pub end_time: TimeValue, -} - -#[derive(Debug)] -pub struct RunCreateExtended { - pub run_create: RunCreate, - pub io: RunIO, - pub attachments: Option>, -} - -#[derive(Debug)] -pub struct RunUpdateExtended { - pub run_update: RunUpdate, - pub io: RunIO, - pub attachments: Option>, -} - -#[derive(Debug)] -pub(crate) enum QueuedRun { - Create(RunCreateExtended), - Update(RunUpdateExtended), - Drain, // Like `Shutdown`, but explicitly sends a message confirming draining is complete. - Shutdown, -} diff --git a/rust/crates/langsmith-tracing-client/src/client/streaming/mod.rs b/rust/crates/langsmith-tracing-client/src/client/streaming/mod.rs deleted file mode 100644 index 7d75e5e2c4..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/streaming/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod multipart_writer; -mod processor; -mod tracing_client; - -pub use processor::RunProcessor; -pub use tracing_client::{ClientConfig, TracingClient}; diff --git a/rust/crates/langsmith-tracing-client/src/client/streaming/multipart_writer.rs b/rust/crates/langsmith-tracing-client/src/client/streaming/multipart_writer.rs deleted file mode 100644 index 4283eaef58..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/streaming/multipart_writer.rs +++ /dev/null @@ -1,163 +0,0 @@ -use std::{borrow::Cow, io::Write, path::Path}; - -pub struct StreamingMultipart { - writer: W, - boundary: String, - empty: bool, -} - -impl StreamingMultipart { - pub(super) fn new(writer: W) -> Self { - let boundary = generate_boundary(); - Self { writer, boundary, empty: true } - } - - #[inline(always)] - pub(super) fn get_ref(&self) -> &W { - &self.writer - } - - #[inline(always)] - pub(super) fn is_empty(&self) -> bool { - self.empty - } - - #[inline(always)] - pub(super) fn boundary(&self) -> &str { - &self.boundary - } - - /// Implement field value escaping as specified in the HTTP5 standard: - /// - /// - /// > - Field names, field values for non-file fields, and filenames for file fields, - /// > in the generated multipart/form-data resource must be set to the result of encoding - /// > the corresponding entry's name or value with encoding, converted to a byte sequence. - /// > - /// > - For field names and filenames for file fields, the result of the encoding in the - /// > previous bullet point must be escaped by replacing any 0x0A (LF) bytes with - /// > the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`. - /// > The user agent must not perform any other escapes. - fn escape_field_value(value: &str) -> Cow<'_, str> { - if value.contains(['"', '\n', '\r']) { - value.replace('"', "%22").replace('\n', "%0A").replace('\r', "%0D").into() - } else { - Cow::Borrowed(value) - } - } - - pub(super) fn json_part( - &mut self, - name: &str, - serialized: &[u8], - ) -> Result<(), std::io::Error> { - self.empty = false; - let boundary = self.boundary.as_str(); - let length = serialized.len(); - let name = Self::escape_field_value(name); - - // The HTTP5 standard prohibits the `Content-Type` header on multipart form parts - // that do not correspond to a file upload: - // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data - // - // However, the current server-side implementation *requires* this header, - // so we add it anyway. - // - // `Content-Length` is explicitly prohibited in multipart parts by RFC 7578: - // https://datatracker.ietf.org/doc/html/rfc7578#section-4.8 - // - // However, the server-side implementation *requires* that the length is specified, - // so we add it anyway. - write!( - self.writer, - "--{boundary}\r\n\ -Content-Disposition: form-data; name=\"{name}\"\r\n\ -Content-Type: application/json\r\n\ -Content-Length: {length}\r\n\ -\r\n" - )?; - - self.writer.write_all(serialized)?; - write!(self.writer, "\r\n") - } - - pub(super) fn file_part_from_bytes( - &mut self, - name: &str, - file_name: &str, - content_type: &str, - contents: &[u8], - ) -> Result<(), std::io::Error> { - self.empty = false; - let boundary = self.boundary.as_str(); - let length = contents.len(); - let name = Self::escape_field_value(name); - let file_name = Self::escape_field_value(file_name); - - // `Content-Length` is explicitly prohibited in multipart parts by RFC 7578: - // https://datatracker.ietf.org/doc/html/rfc7578#section-4.8 - // - // However, the server-side implementation *requires* that the length is specified, - // so we add it anyway. - write!( - self.writer, - "--{boundary}\r\n\ -Content-Disposition: form-data; name=\"{name}\"; filename=\"{file_name}\"\r\n\ -Content-Type: {content_type}\r\n\ -Content-Length: {length}\r\n\ -\r\n" - )?; - - self.writer.write_all(contents)?; - - write!(self.writer, "\r\n") - } - - pub(super) fn file_part_from_path( - &mut self, - name: &str, - file_name: &str, - content_type: &str, - path: &Path, - ) -> Result<(), std::io::Error> { - self.empty = false; - let boundary = self.boundary.as_str(); - let name = Self::escape_field_value(name); - let file_name = Self::escape_field_value(file_name); - - let mut file = std::fs::File::open(path)?; - let metadata = file.metadata()?; - let file_size = metadata.len(); - - // `Content-Length` is explicitly prohibited in multipart parts by RFC 7578: - // https://datatracker.ietf.org/doc/html/rfc7578#section-4.8 - // - // However, the server-side implementation *requires* that the length is specified, - // so we add it anyway. - write!( - self.writer, - "--{boundary}\r\n\ -Content-Disposition: form-data; name=\"{name}\"; filename=\"{file_name}\"\r\n\ -Content-Type: {content_type}\r\n\ -Content-Length: {file_size}\r\n\ -\r\n" - )?; - - std::io::copy(&mut file, &mut self.writer)?; - - write!(self.writer, "\r\n") - } - - pub(super) fn finish(mut self) -> Result { - let boundary = self.boundary.as_str(); - write!(&mut self.writer, "--{boundary}--\r\n")?; - Ok(self.writer) - } -} - -fn generate_boundary() -> String { - let chars: [char; 16] = std::array::from_fn(|_| fastrand::alphanumeric()); - let mut boundary = String::with_capacity(16); - boundary.extend(chars); - boundary -} diff --git a/rust/crates/langsmith-tracing-client/src/client/streaming/processor.rs b/rust/crates/langsmith-tracing-client/src/client/streaming/processor.rs deleted file mode 100644 index 10fb0ebedc..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/streaming/processor.rs +++ /dev/null @@ -1,249 +0,0 @@ -use std::{ - path::Path, - sync::Arc, - time::{Duration, Instant}, -}; - -use crate::client::{run::QueuedRun, RunCreateExtended, RunUpdateExtended, TracingClientError}; - -use super::{multipart_writer::StreamingMultipart, ClientConfig}; - -pub struct RunProcessor { - receiver: crossbeam_channel::Receiver, - drain_sender: crossbeam_channel::Sender<()>, - http_client: reqwest::blocking::Client, - config: Arc, - compression_workers: u32, - multipart_stream: StreamingMultipart>>, -} - -impl RunProcessor { - fn make_stream( - compression_level: i32, - n_workers: u32, - ) -> StreamingMultipart>> { - // Unfortunately, the `reqwest` API doesn't seem to allow reusing this buffer - // since `Into` requires `&'static [u8]` and can't take an arbitrary lifetime. - let buffer = Vec::with_capacity(8192); - - let mut compressor = zstd::stream::write::Encoder::new(buffer, compression_level) - .expect("failed to construct compressor"); - compressor.multithread(n_workers).expect("failed to enable multithreading in compressor"); - - StreamingMultipart::new(compressor) - } - - pub(crate) fn new( - receiver: crossbeam_channel::Receiver, - drain_sender: crossbeam_channel::Sender<()>, - config: Arc, - ) -> Self { - let http_client = reqwest::blocking::Client::new(); - - // We want to use as many threads as available cores to compress data. - // However, we have to be mindful of special values in the zstd library: - // - A setting of `0` here means "use the current thread only." - // - A setting of `1` means "use a separate thread, but only one." - // - // `1` isn't a useful setting for us, so turn `1` into `0` while - // keeping higher numbers the same. - let compression_workers = match std::thread::available_parallelism() { - Ok(num) => { - if num.get() == 1 { - 0 - } else { - num.get() as u32 - } - } - Err(_) => { - // We failed to query the available number of cores. - // Use only the current single thread, to be safe. - 0 - } - }; - - let multipart_stream = Self::make_stream(config.compression_level, compression_workers); - - Self { receiver, drain_sender, http_client, config, compression_workers, multipart_stream } - } - - pub(crate) fn run(&mut self) -> Result<(), TracingClientError> { - let mut last_send_time = Instant::now(); - - loop { - let queued_run = { self.receiver.recv_timeout(Duration::from_millis(100)) }; - - match queued_run { - Ok(queued_run) => match queued_run { - QueuedRun::Shutdown => { - self.send_pending_data()?; - break; - } - QueuedRun::Drain => { - self.send_pending_data()?; - self.drain_sender.send(()).expect("drain_sender should never fail"); - break; - } - _ => { - self.process_new_data(queued_run)?; - if self.multipart_stream.get_ref().get_ref().len() - >= self.config.send_at_batch_size - { - self.send_pending_data()?; - last_send_time = Instant::now(); - } - } - }, - Err(crossbeam_channel::RecvTimeoutError::Timeout) => { - if self.multipart_stream.is_empty() - && last_send_time.elapsed() >= self.config.send_at_batch_time - { - self.send_pending_data()?; - last_send_time = Instant::now(); - } - } - Err(crossbeam_channel::RecvTimeoutError::Disconnected) => { - // The sending part of the channel was dropped, so no further data is incoming. - self.send_pending_data()?; - break; - } - } - } - - Ok(()) - } - - fn send_pending_data(&mut self) -> Result<(), TracingClientError> { - if self.multipart_stream.is_empty() { - return Ok(()); - } - - // Start a new compression stream for future payload data, and replace the current one. - let next_stream = - Self::make_stream(self.config.compression_level, self.compression_workers); - let compressed_stream = std::mem::replace(&mut self.multipart_stream, next_stream); - - let content_type = - format!("multipart/form-data; boundary={}", compressed_stream.boundary()); - let compressed_payload = compressed_stream.finish()?.finish()?; - - let response = self - .http_client - .post(format!("{}/runs/multipart", self.config.endpoint)) - .headers(self.config.headers.as_ref().cloned().unwrap_or_default()) - .header(http::header::CONTENT_TYPE, content_type) - .header(http::header::CONTENT_ENCODING, "zstd") - .body(compressed_payload) - .send()?; - - if response.status().is_success() { - Ok(()) - } else { - let status = response.status(); - Err(TracingClientError::HttpError(status)) - } - } - - fn process_new_data(&mut self, queued_run: QueuedRun) -> Result<(), TracingClientError> { - match queued_run { - QueuedRun::Create(run_create_extended) => { - let RunCreateExtended { run_create, io, attachments } = run_create_extended; - let run_id = run_create.common.id.clone(); - - // Collect JSON data. - let part_name = format!("post.{}", run_id); - let serialized = serde_json::to_vec(&run_create).expect("serialization_failed"); - self.multipart_stream.json_part(&part_name, &serialized)?; - - // Ensure that pre-formatted JSON data represented as bytes - // doesn't end in trailing null bytes, since they are unnecessary - // in HTTP multipart requests. - if let Some(mut inputs) = io.inputs { - if inputs.last() == Some(&0) { - inputs.pop().expect("popping trailing null byte failed"); - } - let part_name = format!("post.{}.inputs", run_id); - self.multipart_stream.json_part(&part_name, &inputs)?; - } - if let Some(mut outputs) = io.outputs { - if outputs.last() == Some(&0) { - outputs.pop().expect("popping trailing null byte failed"); - } - let part_name = format!("post.{}.outputs", run_id); - self.multipart_stream.json_part(&part_name, &outputs)?; - } - - if let Some(attachments) = attachments { - for attachment in attachments { - let part_name = format!("attachment.{}.{}", run_id, attachment.ref_name); - if let Some(data) = attachment.data { - self.multipart_stream.file_part_from_bytes( - &part_name, - &attachment.ref_name, - &attachment.content_type, - &data, - )?; - } else { - self.multipart_stream.file_part_from_path( - &part_name, - &attachment.ref_name, - &attachment.content_type, - Path::new(&attachment.filename), - )?; - } - } - } - - Ok(()) - } - QueuedRun::Update(run_update_extended) => { - let RunUpdateExtended { run_update, io, attachments } = run_update_extended; - let run_id = run_update.common.id.clone(); - - // Collect JSON data. - let part_name = format!("patch.{}", run_id); - let serialized = serde_json::to_vec(&run_update).expect("serialization_failed"); - self.multipart_stream.json_part(&part_name, &serialized)?; - - // Ensure that pre-formatted JSON data represented as bytes - // doesn't end in trailing null bytes, since they are unnecessary - // in HTTP multipart requests. - if let Some(mut outputs) = io.outputs { - if outputs.last() == Some(&0) { - outputs.pop().expect("popping trailing null byte failed"); - } - - let part_name = format!("patch.{}.outputs", run_id); - self.multipart_stream.json_part(&part_name, &outputs)?; - } - - if let Some(attachments) = attachments { - for attachment in attachments { - let part_name = format!("attachment.{}.{}", run_id, attachment.ref_name); - if let Some(data) = attachment.data { - self.multipart_stream.file_part_from_bytes( - &part_name, - &attachment.ref_name, - &attachment.content_type, - &data, - )?; - } else { - self.multipart_stream.file_part_from_path( - &part_name, - &attachment.ref_name, - &attachment.content_type, - Path::new(&attachment.filename), - )?; - } - } - } - - Ok(()) - } - QueuedRun::Drain => { - unreachable!("drain message that wasn't handled earlier"); - } - QueuedRun::Shutdown => Err(TracingClientError::UnexpectedShutdown), - } - } -} diff --git a/rust/crates/langsmith-tracing-client/src/client/streaming/tracing_client.rs b/rust/crates/langsmith-tracing-client/src/client/streaming/tracing_client.rs deleted file mode 100644 index 9b496a0c07..0000000000 --- a/rust/crates/langsmith-tracing-client/src/client/streaming/tracing_client.rs +++ /dev/null @@ -1,84 +0,0 @@ -use std::{sync::Arc, thread, time::Duration}; - -use reqwest::header::{HeaderMap, HeaderValue}; - -use crate::client::{run::QueuedRun, RunCreateExtended, RunUpdateExtended, TracingClientError}; - -#[derive(Clone)] -pub struct ClientConfig { - pub endpoint: String, - pub api_key: String, - pub queue_capacity: usize, - pub send_at_batch_size: usize, - pub send_at_batch_time: Duration, - pub headers: Option, - pub compression_level: i32, -} - -pub struct TracingClient { - sender: crossbeam_channel::Sender, - drain: crossbeam_channel::Receiver<()>, - worker: thread::JoinHandle<()>, -} - -impl TracingClient { - pub fn new(mut config: ClientConfig) -> Result { - let (sender, receiver) = crossbeam_channel::bounded(config.queue_capacity); - let (drain_sender, drain_receiver) = crossbeam_channel::bounded(1); - - // Ensure our headers include the API key. - config.headers.get_or_insert_with(Default::default).append( - "X-API-KEY", - HeaderValue::from_str(&config.api_key).expect("failed to convert API key into header"), - ); - - // We're going to share the config across threads. - // It's immutable from this point onward, so Arc it for efficiency. - let config = Arc::from(config); - - let worker = thread::spawn(move || { - let mut processor = super::RunProcessor::new(receiver, drain_sender, config); - processor.run().expect("run failed") - }); - - Ok(Self { sender, drain: drain_receiver, worker }) - } - - pub fn submit_run_create(&self, run: RunCreateExtended) -> Result<(), TracingClientError> { - let queued_run = QueuedRun::Create(run); - - self.sender.send(queued_run).map_err(|_| TracingClientError::QueueFull) - } - - pub fn submit_run_update(&self, run: RunUpdateExtended) -> Result<(), TracingClientError> { - let queued_run = QueuedRun::Update(run); - - self.sender.send(queued_run).map_err(|_| TracingClientError::QueueFull) - } - - /// Complete all in-progress requests, then allow the worker threads to exit. - /// - /// Convenience function for the PyO3 bindings, which cannot use [`Self::shutdown`] - /// due to its by-value `self`. This means we cannot `.join()` the threads, - /// but the client is nevertheless unusable after this call. - /// - /// Sending further data after a [`Self::drain()`] call has unspecified behavior. - /// It will not cause *undefined behavior* in the programming language sense, - /// but it may e.g. cause errors, panics, or even silently fail, with no guarantees. - pub fn drain(&self) -> Result<(), TracingClientError> { - self.sender.send(QueuedRun::Drain).map_err(|_| TracingClientError::QueueFull)?; - self.drain.recv().expect("failed to receive drained message"); - - Ok(()) - } - - pub fn shutdown(self) -> Result<(), TracingClientError> { - // Send a Shutdown message to worker thread - self.sender.send(QueuedRun::Shutdown).map_err(|_| TracingClientError::QueueFull)?; - - // Wait for worker thread to finish - self.worker.join().unwrap(); - - Ok(()) - } -} diff --git a/rust/crates/langsmith-tracing-client/src/lib.rs b/rust/crates/langsmith-tracing-client/src/lib.rs deleted file mode 100644 index b9babe5bc1..0000000000 --- a/rust/crates/langsmith-tracing-client/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod client; diff --git a/rust/crates/langsmith-tracing-client/tests/test_run.rs b/rust/crates/langsmith-tracing-client/tests/test_run.rs deleted file mode 100644 index 23d23e97b9..0000000000 --- a/rust/crates/langsmith-tracing-client/tests/test_run.rs +++ /dev/null @@ -1,108 +0,0 @@ -use langsmith_tracing_client::client::{RunCommon, RunCreate, RunUpdate, TimeValue}; -use serde_json::{json, to_string, Value}; - -#[test] -fn test_run_common() { - let run_common = RunCommon { - id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - trace_id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - dotted_order: String::from("1.1"), - parent_run_id: None, - extra: Some(json!({"extra_data": "value"})), - error: Some(String::from("error message")), - serialized: Some(json!({"key": "value"})), - events: Some(Value::from(vec![json!({"event": "event_data"})])), - tags: None, - session_id: Some("efghijkl-7654-3210-fedc-ba9876543210".to_string()), - session_name: None, - }; - - let serialized = to_string(&run_common).unwrap(); - assert!(serialized.contains("\"dotted_order\":\"1.1\"")); -} - -#[test] -fn test_run_create_with_string_time() { - let run_common = RunCommon { - id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - trace_id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - dotted_order: String::from("1.1"), - parent_run_id: None, - extra: None, - error: None, - serialized: None, - events: Some(Value::from(vec![json!({"event": "event_data"})])), - tags: Some(json!({"tag": "value"})), - session_id: None, - session_name: Some("Session Name".to_string()), - }; - - let run_create = RunCreate { - common: run_common, - name: String::from("Run Name"), - start_time: TimeValue::String("2024-10-16T12:00:00Z".to_string()), - end_time: Some(TimeValue::String("2024-10-16T14:00:00Z".to_string())), - run_type: String::from("test_run_type"), - reference_example_id: None, - }; - - let serialized = to_string(&run_create).unwrap(); - assert!(serialized.contains("\"name\":\"Run Name\"")); - assert!(serialized.contains("\"start_time\":\"2024-10-16T12:00:00Z\"")); -} - -#[test] -fn test_run_create_with_timestamp() { - let run_common = RunCommon { - id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - trace_id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - dotted_order: String::from("1.1"), - parent_run_id: None, - extra: Some(json!({"extra_data": "value"})), - error: None, - serialized: Some(json!({"key": "value"})), - events: None, - tags: Some(json!({"tag": "value"})), - session_id: None, - session_name: None, - }; - - let run_create = RunCreate { - common: run_common, - name: String::from("Run Name"), - start_time: TimeValue::UnsignedInt(1697462400000), - end_time: Some(TimeValue::UnsignedInt(1697466000000)), - run_type: String::from("test_run_type"), - reference_example_id: None, - }; - - let serialized = to_string(&run_create).unwrap(); - assert!(serialized.contains("\"name\":\"Run Name\"")); - assert!(serialized.contains("\"start_time\":1697462400000")); -} - -#[test] -fn test_run_update() { - let run_common = RunCommon { - id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - trace_id: String::from("fedcba98-7654-3210-fedc-ba9876543210"), - dotted_order: String::from("1.1"), - parent_run_id: None, - extra: None, - error: None, - serialized: None, - events: None, - tags: Some(json!({"tag": "value"})), - session_id: None, - session_name: None, - }; - - let run_update = RunUpdate { - common: run_common, - end_time: TimeValue::String("2024-10-16T14:00:00Z".to_string()), - }; - - let serialized = to_string(&run_update).unwrap(); - assert!(serialized.contains("\"dotted_order\":\"1.1\"")); - assert!(serialized.contains("\"end_time\":\"2024-10-16T14:00:00Z\"")); -} diff --git a/rust/rustfmt.toml b/rust/rustfmt.toml deleted file mode 100644 index 4fe7c4450d..0000000000 --- a/rust/rustfmt.toml +++ /dev/null @@ -1,2 +0,0 @@ -# Rust code formatting; see https://rust-lang.github.io/rustfmt -use_small_heuristics = "Max" diff --git a/vendor/orjson-info.txt b/vendor/orjson-info.txt deleted file mode 100644 index f3a84a4ca9..0000000000 --- a/vendor/orjson-info.txt +++ /dev/null @@ -1,28 +0,0 @@ -cloned from commit 8ece0d4bec4553934aa5883c15efdc3ccc49515f - -then change crate-type to add "rlib" type so it can be used directly from Rust - -then apply diff: - -diff --git a/Cargo.toml b/Cargo.toml -index 516f4d0..b1b2727 100644 ---- a/Cargo.toml -+++ b/Cargo.toml -@@ -65,7 +65,7 @@ itoa = { version = "1", default-features = false } - itoap = { version = "1", default-features = false, features = ["std", "simd"] } - jiff = { version = "^0.1", default-features = false, features = ["alloc"] } - once_cell = { version = "1", default-features = false, features = ["alloc", "race"] } --pyo3-ffi = { path = "include/pyo3/pyo3-ffi", default-features = false, features = ["extension-module"]} -+pyo3-ffi = { path = "../pyo3/pyo3-ffi", default-features = false, features = ["extension-module"]} - ryu = { version = "1", default-features = false } - serde = { version = "1", default-features = false } - serde_json = { version = "1", default-features = false, features = ["std", "float_roundtrip"] } -@@ -77,7 +77,7 @@ xxhash-rust = { version = "^0.8", default-features = false, features = ["xxh3"] - - [build-dependencies] - cc = { version = "1" } --pyo3-build-config = { path = "include/pyo3/pyo3-build-config" } -+pyo3-build-config = { path = "../pyo3/pyo3-build-config" } - version_check = { version = "0.9" } - - [profile.dev] diff --git a/vendor/orjson/.gitattributes b/vendor/orjson/.gitattributes deleted file mode 100644 index 66bcdac69c..0000000000 --- a/vendor/orjson/.gitattributes +++ /dev/null @@ -1,3 +0,0 @@ -include/pyo3/pyo3-build-config/**/* linguist-vendored -include/pyo3/pyo3-ffi/**/* linguist-vendored -include/yyjson/* linguist-vendored diff --git a/vendor/orjson/.github/workflows/artifact.yaml b/vendor/orjson/.github/workflows/artifact.yaml deleted file mode 100644 index eae5339dda..0000000000 --- a/vendor/orjson/.github/workflows/artifact.yaml +++ /dev/null @@ -1,571 +0,0 @@ -name: artifact -on: push -env: - CARGO_UNSTABLE_SPARSE_REGISTRY: "true" - PIP_DISABLE_PIP_VERSION_CHECK: "1" - RUST_TOOLCHAIN: "nightly-2024-09-25" - UNSAFE_PYO3_BUILD_FREE_THREADED: "1" - UNSAFE_PYO3_SKIP_VERSION_CHECK: "1" - UV_LINK_MODE: "copy" -jobs: - - sdist: - runs-on: ubuntu-22.04 - strategy: - fail-fast: false - env: - RUST_TOOLCHAIN: "1.72" # MSRV - steps: - - name: rustup stable - run: | - curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain "${RUST_TOOLCHAIN}" -y - rustup default "${RUST_TOOLCHAIN}" - - - uses: actions/checkout@v4 - - - run: python3 -m pip install --user --upgrade pip "maturin>=1,<2" wheel - - - name: Vendor dependencies - run: | - maturin build - cargo fetch - mkdir .cargo - cp ci/sdist.toml .cargo/config.toml - cargo vendor include/cargo --versioned-dirs - - - run: maturin sdist --out=dist - - - run: python3 -m pip install --user dist/orjson*.tar.gz - env: - CARGO_NET_OFFLINE: "true" - - - run: python3 -m pip install --user -r test/requirements.txt -r integration/requirements.txt mypy - - - run: pytest -v test - env: - PYTHONMALLOC: "debug" - - - run: ./integration/run thread - - run: ./integration/run http - - run: ./integration/run init - - run: ./integration/run typestubs - - - name: Store sdist - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_sdist - path: dist - overwrite: true - retention-days: 1 - - manylinux_amd64: - runs-on: ubuntu-24.04 - timeout-minutes: 10 - strategy: - fail-fast: false - matrix: - python: [ - { interpreter: 'python3.13', package: 'python3.13', compatibility: "manylinux_2_17" }, - { interpreter: 'python3.12', package: 'python3.12', compatibility: "manylinux_2_17" }, - { interpreter: 'python3.11', package: 'python3.11', compatibility: "manylinux_2_17" }, - { interpreter: 'python3.10', package: 'python3.10', compatibility: "manylinux_2_17" }, - { interpreter: 'python3.9', package: 'python3.9', compatibility: "manylinux_2_17" }, - { interpreter: 'python3.8', package: 'python3.8', compatibility: "manylinux_2_17" }, - ] - arch: [ - { - cc: "clang", - cflags: "-Os -fstrict-aliasing -fno-plt -flto=full -emit-llvm", - features: "avx512,no-panic,unstable-simd,yyjson", - ldflags: "-fuse-ld=lld -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow", - rustflags: "-C linker=clang -C link-arg=-fuse-ld=lld -C linker-plugin-lto -C lto=fat -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=4 -D warnings", - tag: null, - target: "x86_64-unknown-linux-gnu", - }, - ] - env: - PYTHON: "${{ matrix.python.interpreter }}" - PYTHON_PACKAGE: "${{ matrix.python.package }}" - TARGET: "${{ matrix.arch.target }}" - CC: "${{ matrix.arch.cc }}" - VENV: ".venv" - FEATURES: "${{ matrix.arch.features }}" - CFLAGS: "${{ matrix.arch.cflags }}" - LDFLAGS: "${{ matrix.arch.ldflags }}" - RUSTFLAGS: "${{ matrix.arch.rustflags }}" - CARGO_TARGET_DIR: "/tmp/orjson" - COMPATIBILITY: "${{ matrix.python.compatibility }}" - steps: - - - name: cpuinfo - run: cat /proc/cpuinfo - - - uses: actions/checkout@v4 - - - name: setup-qemu-container - uses: sandervocke/setup-qemu-container@v1 - with: - container: registry.fedoraproject.org/fedora:42 - arch: ${{ matrix.arch.tag }} - initial_delay: 5s - podman_args: "-v .:/orjson -v /tmp:/tmp --workdir /orjson" - - - name: setup-shell-wrapper - uses: sandervocke/setup-shell-wrapper@v1 - - - name: Build and test - shell: wrap-shell {0} - env: - WRAP_SHELL: run-in-container.sh - run: | - set -eou pipefail - - mkdir dist - - export PATH="/root/.cargo/bin:/home/runner/work/orjson/orjson/.venv:/home/runner/.cargo/bin:$PATH" - - ./script/install-fedora - - source "${VENV}/bin/activate" - - maturin build --release --strip \ - --features="${FEATURES}" \ - --compatibility="${COMPATIBILITY}" \ - --interpreter="${PYTHON}" \ - --target="${TARGET}" - - uv pip install ${CARGO_TARGET_DIR}/wheels/orjson*.whl - - pytest -v test - ./integration/run thread - ./integration/run http - ./integration/run init - - cp ${CARGO_TARGET_DIR}/wheels/orjson*.whl dist - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_manylinux_2_17_amd64_${{ matrix.python.interpreter }} - path: dist - overwrite: true - retention-days: 1 - - - manylinux_aarch64: - runs-on: ubuntu-24.04 - timeout-minutes: 45 - strategy: - fail-fast: false - matrix: - python: [ - { interpreter: 'python3.13', package: 'python3.13', compatibility: "manylinux_2_17" }, - ] - arch: [ - { - cc: "clang", - cflags: "-Os -fstrict-aliasing -fno-plt -flto=full -emit-llvm", - features: "no-panic,unstable-simd,yyjson", - ldflags: "-fuse-ld=lld -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow", - rustflags: "-C linker=clang -C link-arg=-fuse-ld=lld -C linker-plugin-lto -C lto=fat -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=4 -D warnings", - tag: "aarch64", - target: "aarch64-unknown-linux-gnu", - }, - ] - env: - PYTHON: "${{ matrix.python.interpreter }}" - PYTHON_PACKAGE: "${{ matrix.python.package }}" - TARGET: "${{ matrix.arch.target }}" - CC: "${{ matrix.arch.cc }}" - VENV: ".venv" - FEATURES: "${{ matrix.arch.features }}" - CFLAGS: "${{ matrix.arch.cflags }}" - LDFLAGS: "${{ matrix.arch.ldflags }}" - RUSTFLAGS: "${{ matrix.arch.rustflags }}" - CARGO_TARGET_DIR: "/tmp/orjson" - COMPATIBILITY: "${{ matrix.python.compatibility }}" - steps: - - - name: cpuinfo - run: cat /proc/cpuinfo - - - uses: actions/checkout@v4 - - - name: setup-qemu-container - uses: sandervocke/setup-qemu-container@v1 - with: - container: registry.fedoraproject.org/fedora:42 - arch: ${{ matrix.arch.tag }} - podman_args: "-v .:/orjson -v /tmp:/tmp --workdir /orjson" - - - name: setup-shell-wrapper - uses: sandervocke/setup-shell-wrapper@v1 - - - name: Build and test - shell: wrap-shell {0} - env: - WRAP_SHELL: run-in-container.sh - run: | - set -eou pipefail - - mkdir dist - - export PATH="/root/.cargo/bin:/home/runner/work/orjson/orjson/.venv:/home/runner/.cargo/bin:$PATH" - - ./script/install-fedora - - source "${VENV}/bin/activate" - - maturin build --release --strip \ - --features="${FEATURES}" \ - --compatibility="${COMPATIBILITY}" \ - --interpreter="${PYTHON}" \ - --target="${TARGET}" - - uv pip install ${CARGO_TARGET_DIR}/wheels/orjson*.whl - - pytest -v test - - cp ${CARGO_TARGET_DIR}/wheels/orjson*.whl dist - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_manylinux_aarch64_${{ matrix.python.interpreter }} - path: dist - overwrite: true - retention-days: 1 - - musllinux: - runs-on: ubuntu-24.04 - strategy: - fail-fast: false - matrix: - python: [ - { version: '3.13' }, - { version: '3.12' }, - { version: '3.11' }, - { version: '3.10' }, - { version: '3.9' }, - { version: '3.8' }, - ] - platform: - - target: aarch64-unknown-linux-musl - arch: aarch64 - platform: linux/arm64 - features: no-panic,unstable-simd,unwind,yyjson - - target: x86_64-unknown-linux-musl - arch: x86_64 - platform: linux/amd64 - features: avx512,no-panic,unstable-simd,unwind,yyjson - steps: - - uses: actions/checkout@v4 - - - name: build-std - run: | - mkdir .cargo - cp ci/config.toml .cargo/config.toml - - - name: Build - uses: PyO3/maturin-action@v1 - env: - CC: "gcc" - CFLAGS: "-Os" - LDFLAGS: "-Wl,--as-needed" - RUSTFLAGS: "-C lto=fat -Z mir-opt-level=4 -Z threads=2 -D warnings -C target-feature=-crt-static" - with: - rust-toolchain: "${{ env.RUST_TOOLCHAIN }}" - rustup-components: rust-src - target: "${{ matrix.platform.target }}" - manylinux: musllinux_1_2 - args: --release --strip --out=dist --features=${{ matrix.platform.features }} -i python${{ matrix.python.version }} - - - name: QEMU - if: matrix.platform.arch != 'x86_64' - uses: docker/setup-qemu-action@v3 - with: - image: tonistiigi/binfmt:qemu-v8.1.5 - platforms: ${{ matrix.platform.platform }} - - - name: Test - uses: addnab/docker-run-action@v3 - with: - image: quay.io/pypa/musllinux_1_2_${{ matrix.platform.arch }}:latest - options: -v ${{ github.workspace }}:/io -w /io - run: | - apk add tzdata - sed -i '/^psutil/d' test/requirements.txt # missing 3.11, 3.12 wheels - sed -i '/^numpy/d' test/requirements.txt - - python${{ matrix.python.version }} -m venv venv - venv/bin/pip install -U pip wheel - venv/bin/pip install -r test/requirements.txt - venv/bin/pip install orjson --no-index --find-links dist/ --force-reinstall - venv/bin/python -m pytest -v test - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_musllinux_${{ matrix.platform.arch }}_${{ matrix.python.version }} - path: dist - overwrite: true - retention-days: 1 - - manylinux_non_amd64: - runs-on: ubuntu-24.04 - strategy: - fail-fast: false - matrix: - python: [ - { version: '3.12', abi: 'cp312-cp312' }, - { version: '3.11', abi: 'cp311-cp311' }, - { version: '3.10', abi: 'cp310-cp310' }, - { version: '3.9', abi: 'cp39-cp39' }, - { version: '3.8', abi: 'cp38-cp38' }, - ] - target: [ - { - arch: 'aarch64', - cflags: '-Os -flto=full -fstrict-aliasing', - features: 'no-panic,unstable-simd,yyjson', - rustflags: '-Z mir-opt-level=4 -C lto=fat -D warnings', - target: 'aarch64-unknown-linux-gnu', - }, - { - arch: 'armv7', - cflags: '-Os -flto=full -fstrict-aliasing', - features: 'no-panic,yyjson', # no SIMD - rustflags: '-Z mir-opt-level=4 -C lto=fat -D warnings -C opt-level=s', - target: 'armv7-unknown-linux-gnueabihf', - }, - { - arch: 'ppc64le', - cflags: '-Os -flto=full -fstrict-aliasing', - features: 'no-panic,unstable-simd,yyjson', - rustflags: '-Z mir-opt-level=4 -C lto=fat -D warnings', - target: 'powerpc64le-unknown-linux-gnu', - }, - { - arch: 's390x', - cflags: '-Os -flto=full -fstrict-aliasing -march=z10', - features: 'no-panic,yyjson', - rustflags: '-Z mir-opt-level=4 -C lto=fat -D warnings -C target-cpu=z10', - target: 's390x-unknown-linux-gnu', - }, - ] - steps: - - uses: actions/checkout@v4 - - - name: build-std - run: | - mkdir .cargo - cp ci/config.toml .cargo/config.toml - - - name: Build - uses: PyO3/maturin-action@v1 - env: - PYO3_CROSS_LIB_DIR: "/opt/python/${{ matrix.python.abi }}" - CFLAGS: "${{ matrix.target.cflags }}" - LDFLAGS: "-Wl,--as-needed" - RUSTFLAGS: "${{ matrix.target.rustflags }}" - with: - target: "${{ matrix.target.target }}" - rust-toolchain: "${{ env.RUST_TOOLCHAIN }}" - rustup-components: rust-src - manylinux: auto - args: --release --strip --out=dist --features=${{ matrix.target.features }} -i python${{ matrix.python.version }} - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_manylinux_2_17_${{ matrix.target.arch }}_${{ matrix.python.version }} - path: dist - overwrite: true - retention-days: 1 - - macos_aarch64: - runs-on: macos-14 - strategy: - fail-fast: false - matrix: - python: [ - { version: '3.13', macosx_target: "10.15" }, - { version: '3.12', macosx_target: "10.15" }, - { version: '3.11', macosx_target: "10.15" }, - { version: '3.10', macosx_target: "10.15" }, - ] - env: - CC: "clang" - CFLAGS: "-Os -fstrict-aliasing -flto=full" - LDFLAGS: "-Wl,--as-needed" - CFLAGS_x86_64_apple_darwin: "-O2 -fstrict-aliasing -flto=full -march=x86-64-v2 -mtune=generic" - CFLAGS_aarch64_apple_darwin: "-O2 -fstrict-aliasing -flto=full -mcpu=apple-m1 -mtune=generic" - RUSTFLAGS: "-C lto=fat -Z mir-opt-level=4 -Z threads=3 -D warnings" - PATH: "/Users/runner/work/orjson/orjson/.venv/bin:/Users/runner/.cargo/bin:/usr/local/opt/curl/bin:/usr/local/bin:/usr/local/sbin:/Users/runner/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin" - steps: - - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: "${{ matrix.python.version }}" - allow-prereleases: true - - - uses: dtolnay/rust-toolchain@master - with: - toolchain: "${{ env.RUST_TOOLCHAIN }}" - targets: "aarch64-apple-darwin, x86_64-apple-darwin" - components: "rust-src" - - - name: Build environment - run: | - cargo fetch --target aarch64-apple-darwin & - - curl -LsSf https://astral.sh/uv/install.sh | sh - uv venv --python python${{ matrix.python.version }} - uv pip install --upgrade "maturin>=1,<2" -r test/requirements.txt -r integration/requirements.txt - - mkdir .cargo - cp ci/config.toml .cargo/config.toml - - - name: maturin - run: | - PATH=$HOME/.cargo/bin:$PATH \ - MACOSX_DEPLOYMENT_TARGET="${{ matrix.python.macosx_target }}" \ - PYO3_CROSS_LIB_DIR=$(python -c "import sysconfig;print(sysconfig.get_config_var('LIBDIR'))") \ - maturin build --release --strip \ - --features=no-panic,unstable-simd,yyjson \ - --interpreter python${{ matrix.python.version }} \ - --target=universal2-apple-darwin - uv pip install target/wheels/orjson*.whl - - - run: pytest -v test - env: - PYTHONMALLOC: "debug" - - - run: source .venv/bin/activate && ./integration/run thread - - run: source .venv/bin/activate && ./integration/run http - - run: source .venv/bin/activate && ./integration/run init - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_universal2_aarch64_${{ matrix.python.version }} - path: target/wheels - overwrite: true - retention-days: 1 - - macos_amd64: - runs-on: macos-13 - strategy: - fail-fast: false - matrix: - python: [ - { version: '3.9', macosx_target: "10.15" }, - { version: '3.8', macosx_target: "10.15" }, - ] - env: - CC: "clang" - CFLAGS: "-Os -fstrict-aliasing -flto=full" - LDFLAGS: "-Wl,--as-needed" - CFLAGS_x86_64_apple_darwin: "-O2 -fstrict-aliasing -flto=full -march=x86-64-v2 -mtune=generic" - CFLAGS_aarch64_apple_darwin: "-O2 -fstrict-aliasing -flto=full -mcpu=apple-m1 -mtune=generic" - RUSTFLAGS: "-C lto=fat -Z mir-opt-level=4 -Z threads=3 -D warnings" - PATH: "/Users/runner/work/orjson/orjson/.venv/bin:/Users/runner/.cargo/bin:/usr/local/opt/curl/bin:/usr/local/bin:/usr/local/sbin:/Users/runner/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin" - steps: - - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: "${{ matrix.python.version }}" - - - uses: dtolnay/rust-toolchain@master - with: - toolchain: "${{ env.RUST_TOOLCHAIN }}" - targets: "aarch64-apple-darwin, x86_64-apple-darwin" - components: "rust-src" - - - name: Build environment - run: | - cargo fetch --target aarch64-apple-darwin & - - curl -LsSf https://astral.sh/uv/install.sh | sh - uv venv --python python${{ matrix.python.version }} - uv pip install --upgrade "maturin>=1,<2" -r test/requirements.txt -r integration/requirements.txt - - mkdir .cargo - cp ci/config.toml .cargo/config.toml - - - name: maturin - run: | - PATH=$HOME/.cargo/bin:$PATH \ - MACOSX_DEPLOYMENT_TARGET="${{ matrix.python.macosx_target }}" \ - PYO3_CROSS_LIB_DIR=$(python -c "import sysconfig;print(sysconfig.get_config_var('LIBDIR'))") \ - maturin build --release --strip \ - --features=no-panic,unstable-simd,yyjson \ - --interpreter python${{ matrix.python.version }} \ - --target=universal2-apple-darwin - uv pip install target/wheels/orjson*.whl - - - run: pytest -v test - env: - PYTHONMALLOC: "debug" - - - run: source .venv/bin/activate && ./integration/run thread - - run: source .venv/bin/activate && ./integration/run http - - run: source .venv/bin/activate && ./integration/run init - - - name: Store wheels - if: "startsWith(github.ref, 'refs/tags/')" - uses: actions/upload-artifact@v4 - with: - name: orjson_universal2_amd64_${{ matrix.python.version }} - path: target/wheels - overwrite: true - retention-days: 1 - - pypi: - name: PyPI - runs-on: ubuntu-24.04 - if: "startsWith(github.ref, 'refs/tags/')" - needs: [ - macos_aarch64, - macos_amd64, - manylinux_aarch64, - manylinux_amd64, - manylinux_non_amd64, - musllinux, - sdist, - ] - environment: - name: pypi - url: https://pypi.org/p/orjson - permissions: - id-token: write - steps: - - uses: actions/download-artifact@v4 - with: - merge-multiple: true - path: dist/ - pattern: orjson_* - - - run: ls -1 dist/ - - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - - name: Publish distribution to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 - with: - attestations: true - packages-dir: dist - skip-existing: true - verbose: true diff --git a/vendor/orjson/.github/workflows/debug.yaml b/vendor/orjson/.github/workflows/debug.yaml deleted file mode 100644 index 863135acd3..0000000000 --- a/vendor/orjson/.github/workflows/debug.yaml +++ /dev/null @@ -1,62 +0,0 @@ -name: debug -on: push -jobs: - - debug: - runs-on: ubuntu-22.04 - strategy: - fail-fast: false - matrix: - profile: [ - { rust: "1.72", features: "" }, - { rust: "1.72", features: "--features=yyjson" }, - { rust: "nightly-2024-09-25", features: "--features=yyjson,unstable-simd" }, - { rust: "nightly-2024-09-25", features: "--features=avx512,yyjson,unstable-simd" }, - ] - python: [ - { version: '3.13' }, - { version: '3.12' }, - { version: '3.8' }, - ] - env: - CC: "gcc" - CFLAGS: "-Os -fstrict-aliasing" - LDFLAGS: "-Wl,--as-needed" - CARGO_UNSTABLE_SPARSE_REGISTRY: "true" - UNSAFE_PYO3_SKIP_VERSION_CHECK: "1" - steps: - - run: curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain ${{ matrix.profile.rust }} --profile minimal -y - - - uses: actions/setup-python@v5 - with: - python-version: '${{ matrix.python.version }}' - - - run: python -m pip install --user --upgrade pip "maturin>=1,<2" wheel - - - uses: actions/checkout@v4 - - - name: build - run: | - PATH="$HOME/.cargo/bin:$PATH" maturin build --release \ - --out=dist \ - --profile=dev \ - --interpreter python${{ matrix.python.version }} \ - --target=x86_64-unknown-linux-gnu ${{ matrix.profile.features }} - - - run: python -m pip install --user dist/orjson*.whl - - run: python -m pip install --user -r test/requirements.txt -r integration/requirements.txt - - - run: pytest -s -rxX -v test - timeout-minutes: 4 - env: - PYTHONMALLOC: "debug" - - - run: ./integration/run thread - timeout-minutes: 2 - - - run: ./integration/run http - if: matrix.python.version != '3.13' # alpha 5 httpx type issue - timeout-minutes: 2 - - - run: ./integration/run init - timeout-minutes: 2 diff --git a/vendor/orjson/.github/workflows/lint.yaml b/vendor/orjson/.github/workflows/lint.yaml deleted file mode 100644 index e97ed4b533..0000000000 --- a/vendor/orjson/.github/workflows/lint.yaml +++ /dev/null @@ -1,18 +0,0 @@ -name: lint -on: push -jobs: - lint: - runs-on: ubuntu-24.04 - steps: - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - run: curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=stable --profile=default -y - - run: pip install -U ruff==0.7.1 mypy==1.13.0 - - - uses: actions/checkout@v4 - - - run: cargo fmt - - run: ./script/lint - - - run: git diff --exit-code diff --git a/vendor/orjson/.github/workflows/stale.yaml b/vendor/orjson/.github/workflows/stale.yaml deleted file mode 100644 index ef9d5310c9..0000000000 --- a/vendor/orjson/.github/workflows/stale.yaml +++ /dev/null @@ -1,16 +0,0 @@ -name: stale -on: - schedule: - - cron: '0 8 * * *' -permissions: - issues: write - pull-requests: write - -jobs: - stale: - runs-on: ubuntu-latest - steps: - - uses: actions/stale@v8 - with: - days-before-stale: 7 - days-before-close: 1 diff --git a/vendor/orjson/.gitignore b/vendor/orjson/.gitignore deleted file mode 100644 index fdf3c2def8..0000000000 --- a/vendor/orjson/.gitignore +++ /dev/null @@ -1,12 +0,0 @@ -*.patch -/.benchmarks -/.coverage -/.mypy_cache -/.pytest_cache -/.venv* -/build -/include/cargo -/perf.* -/target -/vendor -__pycache__ diff --git a/vendor/orjson/CHANGELOG.md b/vendor/orjson/CHANGELOG.md deleted file mode 100644 index 87bb23cf3f..0000000000 --- a/vendor/orjson/CHANGELOG.md +++ /dev/null @@ -1,1236 +0,0 @@ -# Changelog - -## 3.10.11 - -### Changed - -- Improve performance of UUIDs. -- Publish PyPI wheels with trusted publishing and PEP 740 attestations. -- Include text of licenses for vendored dependencies. - -## 3.10.10 - -### Fixed - -- Fix `int` serialization on `s390x`. This was introduced in 3.10.8. - -### Changed - -- Publish aarch64 manylinux_2_17 wheel for 3.13 to PyPI. - - -## 3.10.9 - -### Fixed - -- Fix `int` serialization on 32-bit Python 3.8, 3.9, 3.10. This was -introduced in 3.10.8. - - -## 3.10.8 - -### Changed - -- `int` serialization no longer chains `OverflowError` to the -the `__cause__` attribute of `orjson.JSONEncodeError` when range exceeded. -- Compatibility with CPython 3.14 alpha 1. -- Improve performance. - - -## 3.10.7 - 2024-08-08 - -### Changed - -- Improve performance of stable Rust amd64 builds. - - -## 3.10.6 - 2024-07-02 - -### Changed - -- Improve performance. - - -## 3.10.5 - 2024-06-13 - -### Changed - -- Improve performance. - - -## 3.10.4 - 2024-06-10 - -### Changed - -- Improve performance. - - -## 3.10.3 - 2024-05-03 - -### Changed - -- `manylinux` amd64 builds include runtime-detected AVX-512 `str` -implementation. -- Tests now compatible with numpy v2. - - -## 3.10.2 - 2024-05-01 - -### Fixed - -- Fix crash serializing `str` introduced in 3.10.1. - -### Changed - -- Improve performance. -- Drop support for arm7. - - -## 3.10.1 - 2024-04-15 - -### Fixed - -- Serializing `numpy.ndarray` with non-native endianness raises -`orjson.JSONEncodeError`. - -### Changed - -- Improve performance of serializing. - - -## 3.10.0 - 2024-03-27 - -### Changed - -- Support serializing `numpy.float16` (`numpy.half`). -- sdist uses metadata 2.3 instead of 2.1. -- Improve Windows PyPI builds. - - -## 3.9.15 - 2024-02-23 - -### Fixed - -- Implement recursion limit of 1024 on `orjson.loads()`. -- Use byte-exact read on `str` formatting SIMD path to avoid crash. - - -## 3.9.14 - 2024-02-14 - -### Fixed - -- Fix crash serializing `str` introduced in 3.9.11. - -### Changed - -- Build now depends on Rust 1.72 or later. - - -## 3.9.13 - 2024-02-03 - -### Fixed - -- Serialization `str` escape uses only 128-bit SIMD. -- Fix compatibility with CPython 3.13 alpha 3. - -### Changed - -- Publish `musllinux_1_2` instead of `musllinux_1_1` wheels. -- Serialization uses small integer optimization in CPython 3.12 or later. - - -## 3.9.12 - 2024-01-18 - -### Changed - -- Update benchmarks in README. - -### Fixed - -- Minimal `musllinux_1_1` build due to sporadic CI failure. - - -## 3.9.11 - 2024-01-18 - -### Changed - -- Improve performance of serializing. `str` is significantly faster. Documents -using `dict`, `list`, and `tuple` are somewhat faster. - - -## 3.9.10 - 2023-10-26 - -### Fixed - -- Fix debug assert failure on 3.12 `--profile=dev` build. - - -## 3.9.9 - 2023-10-12 - -### Changed - -- `orjson` module metadata explicitly marks subinterpreters as not supported. - - -## 3.9.8 - 2023-10-10 - -### Changed - -- Improve performance. -- Drop support for Python 3.7. - - -## 3.9.7 - 2023-09-08 - -### Fixed - -- Fix crash in `orjson.loads()` due to non-reentrant handling of persistent -buffer. This was introduced in 3.9.3. -- Handle some FFI removals in CPython 3.13. - - -## 3.9.6 - 2023-09-07 - -### Fixed - -- Fix numpy reference leak on unsupported array dtype. -- Fix numpy.datetime64 reference handling. - -### Changed - -- Minor performance improvements. - - -## 3.9.5 - 2023-08-16 - -### Fixed - -- Remove futex from module import and initialization path. - - -## 3.9.4 - 2023-08-07 - -### Fixed - -- Fix hash builder using default values. -- Fix non-release builds of orjson copying large deserialization buffer -from stack to heap. This was introduced in 3.9.3. - - -## 3.9.3 - 2023-08-06 - -### Fixed - -- Fix compatibility with CPython 3.12. - -### Changed - -- Support i686/x86 32-bit Python installs on Windows. - - -## 3.9.2 - 2023-07-07 - -### Fixed - -- Fix the `__cause__` exception on `orjson.JSONEncodeError` possibly being -denormalized, i.e., of type `str` instead of `Exception`. - - -## 3.9.1 - 2023-06-09 - -### Fixed - -- Fix memory leak on chained tracebacks of exceptions raised in `default`. This -was introduced in 3.8.12. - - -## 3.9.0 - 2023-06-01 - -### Added - -- `orjson.Fragment` includes already-serialized JSON in a document. - - -## 3.8.14 - 2023-05-25 - -### Changed - -- PyPI `manylinux` wheels are compiled for `x86-64` instead of `x86-64-v2`. - - -## 3.8.13 - 2023-05-23 - -### Changed - -- Source distribution contains all source code required for an offline build. -- PyPI macOS wheels use a `MACOSX_DEPLOYMENT_TARGET` of 10.15 instead of 11. -- Build uses maturin v1. - - -## 3.8.12 - 2023-05-07 - -### Changed - -- Exceptions raised in `default` are now chained as the `__cause__` attribute -on `orjson.JSONEncodeError`. - - -## 3.8.11 - 2023-04-27 - -### Changed - -- `orjson.loads()` on an empty document has a specific error message. -- PyPI `manylinux_2_28_x86_64` wheels are compiled for `x86-64-v2`. -- PyPI macOS wheels are only `universal2` and compiled for -`x86-64-v2` and `apple-m1`. - - -## 3.8.10 - 2023-04-09 - -### Fixed - -- Fix compatibility with CPython 3.12.0a7. -- Fix compatibility with big-endian architectures. -- Fix crash in serialization. - -### Changed - -- Publish musllinux 3.11 wheels. -- Publish s390x wheels. - - -## 3.8.9 - 2023-03-28 - -### Fixed - -- Fix parallel initialization of orjson. - - -## 3.8.8 - 2023-03-20 - -### Changed - -- Publish ppc64le wheels. - - -## 3.8.7 - 2023-02-28 - -### Fixed - -- Use serialization backend introduced in 3.8.4 only on well-tested -platforms such as glibc, macOS by default. - - -## 3.8.6 - 2023-02-09 - -### Fixed - -- Fix crash serializing when using musl libc. - -### Changed - -- Make `python-dateutil` optional in tests. -- Handle failure to load system timezones in tests. - - -## 3.8.5 - 2023-01-10 - -### Fixed - -- Fix `orjson.dumps()` invalid output on Windows. - - -## 3.8.4 - 2023-01-04 - -### Changed - -- Improve performance. - - -## 3.8.3 - 2022-12-02 - -### Fixed - -- `orjson.dumps()` accepts `option=None` per `Optional[int]` type. - - -## 3.8.2 - 2022-11-20 - -### Fixed - -- Fix tests on 32-bit for `numpy.intp` and `numpy.uintp`. - -### Changed - -- Build now depends on rustc 1.60 or later. -- Support building with maturin 0.13 or 0.14. - - -## 3.8.1 - 2022-10-25 - -### Changed - -- Build maintenance for Python 3.11. - - -## 3.8.0 - 2022-08-27 - -### Changed - -- Support serializing `numpy.int16` and `numpy.uint16`. - - -## 3.7.12 - 2022-08-14 - -### Fixed - -- Fix datetime regression tests for tzinfo 2022b. - -### Changed - -- Improve performance. - - -## 3.7.11 - 2022-07-31 - -### Fixed - -- Revert `dict` iterator implementation introduced in 3.7.9. - - -## 3.7.10 - 2022-07-30 - -### Fixed - -- Fix serializing `dict` with deleted final item. This was introduced in 3.7.9. - - -## 3.7.9 - 2022-07-29 - -### Changed - -- Improve performance of serializing. -- Improve performance of serializing pretty-printed (`orjson.OPT_INDENT_2`) -to be much nearer to compact. -- Improve performance of deserializing `str` input. -- orjson now requires Rust 1.57 instead of 1.54 to build. - - -## 3.7.8 - 2022-07-19 - -### Changed - -- Build makes best effort instead of requiring "--features". -- Build using maturin 0.13. - - -## 3.7.7 - 2022-07-06 - -### Changed - -- Support Python 3.11. - - -## 3.7.6 - 2022-07-03 - -### Changed - -- Handle unicode changes in CPython 3.12. -- Build PyPI macOS wheels on 10.15 instead of 12 for compatibility. - - -## 3.7.5 - 2022-06-28 - -### Fixed - -- Fix issue serializing dicts that had keys popped and replaced. This was -introduced in 3.7.4. - - -## 3.7.4 - 2022-06-28 - -### Changed - -- Improve performance. - -### Fixed - -- Fix deallocation of `orjson.JSONDecodeError`. - - -## 3.7.3 - 2022-06-23 - - -## Changed - -- Improve build. -- Publish aarch64 musllinux wheels. - - -## 3.7.2 - 2022-06-07 - - -## Changed - -- Improve deserialization performance. - - -## 3.7.1 - 2022-06-03 - -### Fixed - -- Type stubs for `orjson.JSONDecodeError` now inherit from -`json.JSONDecodeError` instead of `ValueError` -- Null-terminate the internal buffer of `orjson.dumps()` output. - - -## 3.7.0 - 2022-06-03 - -### Changed - -- Improve deserialization performance significantly through the use of a new -backend. PyPI wheels for manylinux_2_28 and macOS have it enabled. Packagers -are advised to see the README. - - -## 3.6.9 - 2022-06-01 - -### Changed - -- Improve serialization and deserialization performance. - - -## 3.6.8 - 2022-04-15 - -### Fixed - -- Fix serialization of `numpy.datetime64("NaT")` to raise on an -unsupported type. - - -## 3.6.7 - 2022-02-14 - -### Changed - -- Improve performance of deserializing almost-empty documents. -- Publish arm7l `manylinux_2_17` wheels to PyPI. -- Publish amd4 `musllinux_1_1` wheels to PyPI. - -### Fixed - -- Fix build requiring `python` on `PATH`. - - -## 3.6.6 - 2022-01-21 - -### Changed - -- Improve performance of serializing `datetime.datetime` using `tzinfo` that -are `zoneinfo.ZoneInfo`. - -### Fixed - -- Fix invalid indexing in line and column number reporting in -`JSONDecodeError`. -- Fix `orjson.OPT_STRICT_INTEGER` not raising an error on -values exceeding a 64-bit integer maximum. - - -## 3.6.5 - 2021-12-05 - -### Fixed - -- Fix build on macOS aarch64 CPython 3.10. -- Fix build issue on 32-bit. - - -## 3.6.4 - 2021-10-01 - -### Fixed - -- Fix serialization of `dataclass` inheriting from `abc.ABC` and -using `__slots__`. -- Decrement refcount for numpy `PyArrayInterface`. -- Fix build on recent versions of Rust nightly. - - -## 3.6.3 - 2021-08-20 - -### Fixed - -- Fix build on aarch64 using the Rust stable channel. - - -## 3.6.2 - 2021-08-17 - -### Changed - -- `orjson` now compiles on Rust stable 1.54.0 or above. Use of some SIMD -usage is now disabled by default and packagers are advised to add -`--cargo-extra-args="--features=unstable-simd"` to the `maturin build` command - if they continue to use nightly. -- `orjson` built with `--features=unstable-simd` adds UTF-8 validation -implementations that use AVX2 or SSE4.2. -- Drop support for Python 3.6. - - -## 3.6.1 - 2021-08-04 - -### Changed - -- `orjson` now includes a `pyi` type stubs file. -- Publish manylinux_2_24 wheels instead of manylinux2014. - -### Fixed - -- Fix compilation on latest Rust nightly. - - -## 3.6.0 - 2021-07-08 - -### Added - -- `orjson.dumps()` serializes `numpy.datetime64` instances as RFC 3339 -strings. - - -## 3.5.4 - 2021-06-30 - -### Fixed - -- Fix memory leak serializing `datetime.datetime` with `tzinfo`. -- Fix wrong error message when serializing an unsupported numpy type -without default specified. - -### Changed - -- Publish python3.10 and python3.9 manylinux_2_24 wheels. - - -## 3.5.3 - 2021-06-01 - -### Fixed - -- `orjson.JSONDecodeError` now has `pos`, `lineno`, and `colno`. -- Fix build on recent versions of Rust nightly. - - -## 3.5.2 - 2021-04-15 - -### Changed - -- Improve serialization and deserialization performance. -- `orjson.dumps()` serializes individual `numpy.bool_` objects. - - -## 3.5.1 - 2021-03-06 - -### Changed - -- Publish `universal2` wheels for macOS supporting Apple Silicon (aarch64). - - -## 3.5.0 - 2021-02-24 - -### Added - -- `orjson.loads()` supports reading from `memoryview` objects. - -### Fixed - -- `datetime.datetime` and `datetime.date` zero pad years less than 1000 to -four digits. -- sdist pins maturin 0.9.0 to avoid breaks in later 0.9.x. - -### Changed - -- `orjson.dumps()` when given a non-C contiguous `numpy.ndarray` has -an error message suggesting to use `default`. - - -## 3.4.8 - 2021-02-04 - -### Fixed - -- aarch64 manylinux2014 wheels are now compatible with glibc 2.17. - -### Changed - -- Fix build warnings on ppcle64. - - -## 3.4.7 - 2021-01-19 - -### Changed - -- Use vectorcall APIs for method calls on python3.9 and above. -- Publish python3.10 wheels for Linux on amd64 and aarch64. - - -## 3.4.6 - 2020-12-07 - -### Fixed - -- Fix compatibility with debug builds of CPython. - - -## 3.4.5 - 2020-12-02 - -### Fixed - -- Fix deserializing long strings on processors without AVX2. - - -## 3.4.4 - 2020-11-25 - -### Changed - -- `orjson.dumps()` serializes integers up to a 64-bit unsigned integer's -maximum. It was previously the maximum of a 64-bit signed integer. - - -## 3.4.3 - 2020-10-30 - -### Fixed - -- Fix regression in parsing similar `dict` keys. - - -## 3.4.2 - 2020-10-29 - -### Changed - -- Improve deserialization performance. -- Publish Windows python3.9 wheel. -- Disable unsupported SIMD features on non-x86, non-ARM targets - - -## 3.4.1 - 2020-10-20 - -### Fixed - -- Fix `orjson.dumps.__module__` and `orjson.loads.__module__` not being the -`str` "orjson". - -### Changed - -- Publish macos python3.9 wheel. -- More packaging documentation. - - -## 3.4.0 - 2020-09-25 - -### Added - -- Serialize `numpy.uint8` and `numpy.int8` instances. - -### Fixed - -- Fix serializing `numpy.empty()` instances. - -### Changed - -- No longer publish `manylinux1` wheels due to tooling dropping support. - - -## 3.3.1 - 2020-08-17 - -### Fixed - -- Fix failure to deserialize some latin1 strings on some platforms. This -was introduced in 3.2.0. -- Fix annotation of optional parameters on `orjson.dumps()` for `help()`. - -### Changed - -- Publish `manylinux2014` wheels for amd64 in addition to `manylinux1`. - - -## 3.3.0 - 2020-07-24 - -### Added - -- `orjson.dumps()` now serializes individual numpy floats and integers, e.g., -`numpy.float64(1.0)`. -- `orjson.OPT_PASSTHROUGH_DATACLASS` causes `orjson.dumps()` to pass -`dataclasses.dataclass` instances to `default`. - - -## 3.2.2 - 2020-07-13 - -### Fixed - -- Fix serializing `dataclasses.dataclass` that have no attributes. - -### Changed - -- Improve deserialization performance of `str`. - - -## 3.2.1 - 2020-07-03 - -### Fixed - -- Fix `orjson.dumps(..., **{})` raising `TypeError` on python3.6. - - -## 3.2.0 - 2020-06-30 - -### Added - -- `orjson.OPT_APPEND_NEWLINE` appends a newline to output. - -### Changed - -- Improve deserialization performance of `str`. - - -## 3.1.2 - 2020-06-23 - -### Fixed - -- Fix serializing zero-dimension `numpy.ndarray`. - - -## 3.1.1 - 2020-06-20 - -### Fixed - -- Fix repeated serialization of `str` that are ASCII-only and have a legacy -(non-compact) layout. - - -## 3.1.0 - 2020-06-08 - -### Added - -- `orjson.OPT_PASSTHROUGH_SUBCLASS` causes `orjson.dumps()` to pass -subclasses of builtin types to `default` so the caller can customize the -output. -- `orjson.OPT_PASSTHROUGH_DATETIME` causes `orjson.dumps()` to pass -`datetime` objects to `default` so the caller can customize the -output. - - -## 3.0.2 - 2020-05-27 - -### Changed - -- `orjson.dumps()` does not serialize `dataclasses.dataclass` attributes -that begin with a leading underscore, e.g., `_attr`. This is because of the -Python idiom that a leading underscores marks an attribute as "private." -- `orjson.dumps()` does not serialize `dataclasses.dataclass` attributes that -are `InitVar` or `ClassVar` whether using `__slots__` or not. - - -## 3.0.1 - 2020-05-19 - -### Fixed - -- `orjson.dumps()` raises an exception if the object to be serialized -is not given as a positional argument. `orjson.dumps({})` is intended and ok -while `orjson.dumps(obj={})` is an error. This makes it consistent with the -documentation, `help()` annotation, and type annotation. -- Fix orphan reference in exception creation that leaks memory until the -garbage collector runs. - -### Changed - -- Improve serialization performance marginally by using the fastcall/vectorcall -calling convention on python3.7 and above. -- Reduce build time. - - -## 3.0.0 - 2020-05-01 - -### Added - -- `orjson.dumps()` serializes subclasses of `str`, `int`, `list`, and `dict`. - -### Changed - -- `orjson.dumps()` serializes `dataclasses.dataclass` and `uuid.UUID` -instances by default. The options `OPT_SERIALIZE_DATACLASS` and -`OPT_SERIALIZE_UUID` can still be specified but have no effect. - - -## 2.6.8 - 2020-04-30 - -### Changed - -- The source distribution vendors a forked dependency. - - -## 2.6.7 - 2020-04-30 - -### Fixed - -- Fix integer overflows in debug builds. - -### Changed - -- The source distribution sets the recommended RUSTFLAGS in `.cargo/config`. - - -## 2.6.6 - 2020-04-24 - -### Fixed - -- Import `numpy` only on first use of `OPT_SERIALIZE_NUMPY` to reduce -interpreter start time when not used. -- Reduce build time by half. - - -## 2.6.5 - 2020-04-08 - -### Fixed - -- Fix deserialization raising `JSONDecodeError` on some valid negative -floats with large exponents. - - -## 2.6.4 - 2020-04-08 - -### Changed - -- Improve deserialization performance of floats by about 40%. - - -## 2.6.3 - 2020-04-01 - -### Changed - -- Serialize `enum.Enum` objects. -- Minor performance improvements. - - -## 2.6.2 - 2020-03-27 - -### Changed - -- Publish python3.9 `manylinux2014` wheel instead of `manylinux1` for `x86_64`. -- Publish python3.9 `manylinux2014` wheel for `aarch64`. - -### Fixed - -- Fix compilation failure on 32-bit. - - -## 2.6.1 - 2020-03-19 - -### Changed - -- Serialization is 10-20% faster and uses about 50% less memory by writing -directly to the returned `bytes` object. - - -## 2.6.0 - 2020-03-10 - -### Added - -- `orjson.dumps()` pretty prints with an indentation of two spaces if -`option=orjson.OPT_INDENT_2` is specified. - - -## 2.5.2 - 2020-03-07 - -### Changed - -- Publish `manylinux2014` wheels for `aarch64`. -- numpy support now includes `numpy.uint32` and `numpy.uint64`. - - -## 2.5.1 - 2020-02-24 - -### Changed - -- `manylinux1` wheels for 3.6, 3.7, and 3.8 are now compliant with the spec by -not depending on glibc 2.18. - - -## 2.5.0 - 2020-02-19 - -### Added - -- `orjson.dumps()` serializes `dict` keys of type other than `str` if -`option=orjson.OPT_NON_STR_KEYS` is specified. - - -## 2.4.0 - 2020-02-14 - -### Added - -- `orjson.dumps()` serializes `numpy.ndarray` instances if -`option=orjson.OPT_SERIALIZE_NUMPY` is specified. - -### Fixed - -- Fix `dataclasses.dataclass` attributes that are `dict` to be effected by -`orjson.OPT_SORT_KEYS`. - - -## 2.3.0 - 2020-02-12 - -### Added - -- `orjson.dumps()` serializes `dict` instances sorted by keys, equivalent to -`sort_keys` in other implementations, if `option=orjson.OPT_SORT_KEYS` is -specified. - -### Changed - -- `dataclasses.dataclass` instances without `__slots__` now serialize faster. - -### Fixed - -- Fix documentation on `default`, in particular documenting the need to raise -an exception if the type cannot be handled. - - -## 2.2.2 - 2020-02-10 - -### Changed - -- Performance improvements to serializing a list containing elements of the -same type. - - -## 2.2.1 - 2020-02-04 - -### Fixed - -- `orjson.loads()` rejects floats that do not have a digit following -the decimal, e.g., `-2.`, `2.e-3`. - -### Changed - -- Build Linux, macOS, and Windows wheels on more recent distributions. - - -## 2.2.0 - 2020-01-22 - -### Added - -- `orjson.dumps()` serializes `uuid.UUID` instances if -`option=orjson.OPT_SERIALIZE_UUID` is specified. - -### Changed - -- Minor performance improvements. -- Publish Python 3.9 wheel for Linux. - - -## 2.1.4 - 2020-01-08 - -### Fixed - -- Specify a text signature for `orjson.loads()`. - -### Changed - -- Improve documentation. - - -## 2.1.3 - 2019-11-12 - -### Changed - -- Publish Python 3.8 wheels for macOS and Windows. - - -## 2.1.2 - 2019-11-07 - -### Changed - -- The recursion limit of `default` on `orjson.dumps()` has been increased from -5 to 254. - - -## 2.1.1 - 2019-10-29 - -### Changed - -- Publish `manylinux1` wheels instead of `manylinux2010`. - - -## 2.1.0 - 2019-10-24 - -### Added - -- `orjson.dumps()` serializes `dataclasses.dataclass` instances if -`option=orjson.OPT_SERIALIZE_DATACLASS` is specified. -- `orjson.dumps()` accepts `orjson.OPT_UTC_Z` to serialize UTC as "Z" instead -of "+00:00". -- `orjson.dumps()` accepts `orjson.OPT_OMIT_MICROSECONDS` to not serialize -the `microseconds` attribute of `datetime.datetime` and `datetime.time` -instances. -- `orjson.loads()` accepts `bytearray`. - -### Changed - -- Drop support for Python 3.5. -- Publish `manylinux2010` wheels instead of `manylinux1`. - - -## 2.0.11 - 2019-10-01 - -### Changed - -- Publish Python 3.8 wheel for Linux. - - -## 2.0.10 - 2019-09-25 - -### Changed - -- Performance improvements and lower memory usage in deserialization -by creating only one `str` object for repeated map keys. - - -## 2.0.9 - 2019-09-22 - -### Changed - -- Minor performance improvements. - -### Fixed - -- Fix inaccurate zero padding in serialization of microseconds on -`datetime.time` objects. - - -## 2.0.8 - 2019-09-18 - -### Fixed - -- Fix inaccurate zero padding in serialization of microseconds on -`datetime.datetime` objects. - - -## 2.0.7 - 2019-08-29 - -### Changed - -- Publish PEP 517 source distribution. - -### Fixed - -- `orjson.dumps()` raises `JSONEncodeError` on circular references. - - -## 2.0.6 - 2019-05-11 - -### Changed - -- Performance improvements. - - -## 2.0.5 - 2019-04-19 - -### Fixed - -- Fix inaccuracy in deserializing some `float` values, e.g., -31.245270191439438 was parsed to 31.24527019143944. Serialization was -unaffected. - - -## 2.0.4 - 2019-04-02 - -### Changed - -- `orjson.dumps()` now serializes `datetime.datetime` objects without a -`tzinfo` rather than raising `JSONEncodeError`. - - -## 2.0.3 - 2019-03-23 - -### Changed - -- `orjson.loads()` uses SSE2 to validate `bytes` input. - - -## 2.0.2 - 2019-03-12 - -### Changed - -- Support Python 3.5. - - -## 2.0.1 - 2019-02-05 - -### Changed - -- Publish Windows wheel. - - -## 2.0.0 - 2019-01-28 - -### Added - -- `orjson.dumps()` accepts a `default` callable to serialize arbitrary -types. -- `orjson.dumps()` accepts `datetime.datetime`, `datetime.date`, -and `datetime.time`. Each is serialized to an RFC 3339 string. -- `orjson.dumps(..., option=orjson.OPT_NAIVE_UTC)` allows serializing -`datetime.datetime` objects that do not have a timezone set as UTC. -- `orjson.dumps(..., option=orjson.OPT_STRICT_INTEGER)` available to -raise an error on integer values outside the 53-bit range of all JSON -implementations. - -### Changed - -- `orjson.dumps()` no longer accepts `bytes`. - - -## 1.3.1 - 2019-01-03 - -### Fixed - -- Handle invalid UTF-8 in str. - - -## 1.3.0 - 2019-01-02 - -### Changed - -- Performance improvements of 15-25% on serialization, 10% on deserialization. - - -## 1.2.1 - 2018-12-31 - -### Fixed - -- Fix memory leak in deserializing dict. - - -## 1.2.0 - 2018-12-16 - -### Changed - -- Performance improvements. - - -## 1.1.0 - 2018-12-04 - -### Changed - -- Performance improvements. - -### Fixed - -- Dict key can only be str. - - -## 1.0.1 - 2018-11-26 - -### Fixed - -- pyo3 bugfix update. - - -## 1.0.0 - 2018-11-23 - -### Added - -- `orjson.dumps()` function. -- `orjson.loads()` function. diff --git a/vendor/orjson/CONTRIBUTING.md b/vendor/orjson/CONTRIBUTING.md deleted file mode 100644 index 5deec78f92..0000000000 --- a/vendor/orjson/CONTRIBUTING.md +++ /dev/null @@ -1,3 +0,0 @@ -orjson is stable. Contributions will be reviewed only if they are well -documented, tested to the same standard as the rest of the library, not -a breaking change, and important to other users. diff --git a/vendor/orjson/Cargo.lock b/vendor/orjson/Cargo.lock deleted file mode 100644 index fde9db8d5b..0000000000 --- a/vendor/orjson/Cargo.lock +++ /dev/null @@ -1,317 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "associative-cache" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b993cd767a2bc7307dd87622311ca22c44329cc7a21366206bfa0896827b2bad" - -[[package]] -name = "bytecount" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" - -[[package]] -name = "castaway" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5" -dependencies = [ - "rustversion", -] - -[[package]] -name = "cc" -version = "1.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" -dependencies = [ - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "compact_str" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" -dependencies = [ - "castaway", - "cfg-if", - "itoa", - "rustversion", - "ryu", - "serde", - "static_assertions", -] - -[[package]] -name = "crunchy" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" - -[[package]] -name = "encoding_rs" -version = "0.8.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "gimli" -version = "0.30.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e1d97fbe9722ba9bbd0c97051c2956e726562b61f86a25a4360398a40edfc9" - -[[package]] -name = "half" -version = "2.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" -dependencies = [ - "cfg-if", - "crunchy", -] - -[[package]] -name = "itoa" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -dependencies = [ - "no-panic", -] - -[[package]] -name = "itoap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9028f49264629065d057f340a86acb84867925865f73bbf8d47b4d149a7e88b8" - -[[package]] -name = "jiff" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d9d414fc817d3e3d62b2598616733f76c4cc74fbac96069674739b881295c8" - -[[package]] -name = "libc" -version = "0.2.161" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "no-panic" -version = "0.1.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8540b7d99a20166178b42a05776aef900cdbfec397f861dfc7819bf1d7760b3d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - -[[package]] -name = "orjson" -version = "3.10.11" -dependencies = [ - "associative-cache", - "bytecount", - "cc", - "compact_str", - "encoding_rs", - "half", - "itoa", - "itoap", - "jiff", - "once_cell", - "pyo3-build-config", - "pyo3-ffi", - "ryu", - "serde", - "serde_json", - "simdutf8", - "smallvec", - "unwinding", - "uuid", - "version_check", - "xxhash-rust", -] - -[[package]] -name = "proc-macro2" -version = "1.0.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "pyo3-build-config" -version = "0.23.0-dev" -dependencies = [ - "once_cell", - "target-lexicon", -] - -[[package]] -name = "pyo3-ffi" -version = "0.23.0-dev" -dependencies = [ - "libc", - "pyo3-build-config", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rustversion" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" - -[[package]] -name = "ryu" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" -dependencies = [ - "no-panic", -] - -[[package]] -name = "serde" -version = "1.0.214" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.214" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.132" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "simdutf8" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" - -[[package]] -name = "smallvec" -version = "1.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" - -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - -[[package]] -name = "syn" -version = "2.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89275301d38033efb81a6e60e3497e734dfcc62571f2854bf4b16690398824c" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "target-lexicon" -version = "0.12.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" - -[[package]] -name = "unicode-ident" -version = "1.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" - -[[package]] -name = "unwinding" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc55842d0db6329a669d55a623c674b02d677b16bfb2d24857d4089d41eba882" -dependencies = [ - "gimli", -] - -[[package]] -name = "uuid" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" - -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - -[[package]] -name = "xxhash-rust" -version = "0.8.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a5cbf750400958819fb6178eaa83bee5cd9c29a26a40cc241df8c70fdd46984" diff --git a/vendor/orjson/Cargo.toml b/vendor/orjson/Cargo.toml deleted file mode 100644 index 36cdcd2233..0000000000 --- a/vendor/orjson/Cargo.toml +++ /dev/null @@ -1,105 +0,0 @@ -[package] -name = "orjson" -version = "3.10.11" -authors = ["ijl "] -description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" -edition = "2021" -resolver = "2" -rust-version = "1.72" -license = "Apache-2.0 OR MIT" -repository = "https://github.com/ijl/orjson" -homepage = "https://github.com/ijl/orjson" -readme = "README.md" -keywords = ["fast", "json", "dataclass", "dataclasses", "datetime", "rfc", "8259", "3339"] -include = [ - "Cargo.toml", - "CHANGELOG.md", - "data", - "include", - "LICENSE-APACHE", - "LICENSE-MIT", - "pyproject.toml", - "README.md", - "src", - "test/*.py", - "test/requirements.txt", -] - -[lib] -name = "orjson" -crate-type = ["cdylib", "rlib"] - -[features] -# Do not disable this in production! Only useful if you want to use orjson inside `cargo test`. -default = ["extension-module"] - -# Use SIMD intrinsics. This requires Rust on the nightly channel. -unstable-simd = [] - -# Include runtime-detected functions that use AVX512VL. Requires unstable-simd and amd64. -avx512 = [] - -no-panic = [ - "itoa/no-panic", - "ryu/no-panic", -] - -# Avoid bundling libgcc on musl. -unwind = ["unwinding"] - -# Build yyjson as a backend and panic if it fails. The default is to attempt -# to build and on failure fall back to another backend. -yyjson = [] - -# Features detected by build.rs. Do not specify. -inline_int = [] -intrinsics = [] -optimize = [] - -# Exposed for testing purposes only, so it can be disabled during `cargo test`. -extension-module = ["pyo3-ffi/extension-module"] - -[dependencies] -associative-cache = { version = "2", default-features = false } -bytecount = { version = "^0.6.7", default-features = false, features = ["runtime-dispatch-simd"] } -compact_str = { version = "0.8", default-features = false, features = ["serde"] } -encoding_rs = { version = "0.8", default-features = false } -half = { version = "2", default-features = false, features = ["std"] } -itoa = { version = "1", default-features = false } -itoap = { version = "1", default-features = false, features = ["std", "simd"] } -jiff = { version = "^0.1", default-features = false, features = ["alloc"] } -once_cell = { version = "1", default-features = false, features = ["alloc", "race"] } -pyo3-ffi = { path = "../pyo3/pyo3-ffi", default-features = false} -ryu = { version = "1", default-features = false } -serde = { version = "1", default-features = false } -serde_json = { version = "1", default-features = false, features = ["std", "float_roundtrip"] } -simdutf8 = { version = "0.1", default-features = false, features = ["std", "public_imp", "aarch64_neon"] } -smallvec = { version = "^1.11", default-features = false, features = ["union", "write"] } -unwinding = { version = "=0.2.2", default-features = false, features = ["unwinder"], optional = true } -uuid = { version = "1", default-features = false } -xxhash-rust = { version = "^0.8", default-features = false, features = ["xxh3"] } - -[build-dependencies] -cc = { version = "1" } -pyo3-build-config = { path = "../pyo3/pyo3-build-config" } -version_check = { version = "0.9" } - -[profile.dev] -codegen-units = 1 -debug = 2 -debug-assertions = true -incremental = false -lto = "off" -opt-level = 3 -overflow-checks = true - -[profile.release] -codegen-units = 1 -debug = false -incremental = false -lto = "thin" -opt-level = 3 -panic = "abort" - -[profile.release.build-override] -opt-level = 0 diff --git a/vendor/orjson/LICENSE-APACHE b/vendor/orjson/LICENSE-APACHE deleted file mode 100644 index 16fe87b06e..0000000000 --- a/vendor/orjson/LICENSE-APACHE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - -Copyright [yyyy] [name of copyright owner] - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/vendor/orjson/LICENSE-MIT b/vendor/orjson/LICENSE-MIT deleted file mode 100644 index 31aa79387f..0000000000 --- a/vendor/orjson/LICENSE-MIT +++ /dev/null @@ -1,23 +0,0 @@ -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/vendor/orjson/README.md b/vendor/orjson/README.md deleted file mode 100644 index 942597eff0..0000000000 --- a/vendor/orjson/README.md +++ /dev/null @@ -1,1250 +0,0 @@ -# orjson - -orjson is a fast, correct JSON library for Python. It -[benchmarks](https://github.com/ijl/orjson?tab=readme-ov-file#performance) as the fastest Python -library for JSON and is more correct than the standard json library or other -third-party libraries. It serializes -[dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass), -[datetime](https://github.com/ijl/orjson?tab=readme-ov-file#datetime), -[numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy), and -[UUID](https://github.com/ijl/orjson?tab=readme-ov-file#uuid) instances natively. - -Its features and drawbacks compared to other Python JSON libraries: - -* serializes `dataclass` instances 40-50x as fast as other libraries -* serializes `datetime`, `date`, and `time` instances to RFC 3339 format, -e.g., "1970-01-01T00:00:00+00:00" -* serializes `numpy.ndarray` instances 4-12x as fast with 0.3x the memory -usage of other libraries -* pretty prints 10x to 20x as fast as the standard library -* serializes to `bytes` rather than `str`, i.e., is not a drop-in replacement -* serializes `str` without escaping unicode to ASCII, e.g., "好" rather than -"\\\u597d" -* serializes `float` 10x as fast and deserializes twice as fast as other -libraries -* serializes subclasses of `str`, `int`, `list`, and `dict` natively, -requiring `default` to specify how to serialize others -* serializes arbitrary types using a `default` hook -* has strict UTF-8 conformance, more correct than the standard library -* has strict JSON conformance in not supporting Nan/Infinity/-Infinity -* has an option for strict JSON conformance on 53-bit integers with default -support for 64-bit -* does not provide `load()` or `dump()` functions for reading from/writing to -file-like objects - -orjson supports CPython 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, and 3.14. - -It distributes amd64/x86_64, aarch64/armv8, arm7, POWER/ppc64le, and s390x -wheels for Linux, amd64 and aarch64 wheels for macOS, and amd64 -and i686/x86 wheels for Windows. - -orjson does not and will not support PyPy, embedded Python builds for -Android/iOS, or PEP 554 subinterpreters. - -Releases follow semantic versioning and serializing a new object type -without an opt-in flag is considered a breaking change. - -orjson is licensed under both the Apache 2.0 and MIT licenses. The -repository and issue tracker is -[github.com/ijl/orjson](https://github.com/ijl/orjson), and patches may be -submitted there. There is a -[CHANGELOG](https://github.com/ijl/orjson/blob/master/CHANGELOG.md) -available in the repository. - -1. [Usage](https://github.com/ijl/orjson?tab=readme-ov-file#usage) - 1. [Install](https://github.com/ijl/orjson?tab=readme-ov-file#install) - 2. [Quickstart](https://github.com/ijl/orjson?tab=readme-ov-file#quickstart) - 3. [Migrating](https://github.com/ijl/orjson?tab=readme-ov-file#migrating) - 4. [Serialize](https://github.com/ijl/orjson?tab=readme-ov-file#serialize) - 1. [default](https://github.com/ijl/orjson?tab=readme-ov-file#default) - 2. [option](https://github.com/ijl/orjson?tab=readme-ov-file#option) - 3. [Fragment](https://github.com/ijl/orjson?tab=readme-ov-file#fragment) - 5. [Deserialize](https://github.com/ijl/orjson?tab=readme-ov-file#deserialize) -2. [Types](https://github.com/ijl/orjson?tab=readme-ov-file#types) - 1. [dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass) - 2. [datetime](https://github.com/ijl/orjson?tab=readme-ov-file#datetime) - 3. [enum](https://github.com/ijl/orjson?tab=readme-ov-file#enum) - 4. [float](https://github.com/ijl/orjson?tab=readme-ov-file#float) - 5. [int](https://github.com/ijl/orjson?tab=readme-ov-file#int) - 6. [numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy) - 7. [str](https://github.com/ijl/orjson?tab=readme-ov-file#str) - 8. [uuid](https://github.com/ijl/orjson?tab=readme-ov-file#uuid) -3. [Testing](https://github.com/ijl/orjson?tab=readme-ov-file#testing) -4. [Performance](https://github.com/ijl/orjson?tab=readme-ov-file#performance) - 1. [Latency](https://github.com/ijl/orjson?tab=readme-ov-file#latency) - 2. [Memory](https://github.com/ijl/orjson?tab=readme-ov-file#memory) - 3. [Reproducing](https://github.com/ijl/orjson?tab=readme-ov-file#reproducing) -5. [Questions](https://github.com/ijl/orjson?tab=readme-ov-file#questions) -6. [Packaging](https://github.com/ijl/orjson?tab=readme-ov-file#packaging) -7. [License](https://github.com/ijl/orjson?tab=readme-ov-file#license) - -## Usage - -### Install - -To install a wheel from PyPI, install the `orjson` package. - -In `requirements.in` or `requirements.txt` format, specify: - -```txt -orjson>=3.10,<4 -``` - -In Poetry, specify: - -```toml -orjson = "^3" -``` - -To build a wheel, see [packaging](https://github.com/ijl/orjson?tab=readme-ov-file#packaging). - -### Quickstart - -This is an example of serializing, with options specified, and deserializing: - -```python ->>> import orjson, datetime, numpy ->>> data = { - "type": "job", - "created_at": datetime.datetime(1970, 1, 1), - "status": "🆗", - "payload": numpy.array([[1, 2], [3, 4]]), -} ->>> orjson.dumps(data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_SERIALIZE_NUMPY) -b'{"type":"job","created_at":"1970-01-01T00:00:00+00:00","status":"\xf0\x9f\x86\x97","payload":[[1,2],[3,4]]}' ->>> orjson.loads(_) -{'type': 'job', 'created_at': '1970-01-01T00:00:00+00:00', 'status': '🆗', 'payload': [[1, 2], [3, 4]]} -``` - -### Migrating - -orjson version 3 serializes more types than version 2. Subclasses of `str`, -`int`, `dict`, and `list` are now serialized. This is faster and more similar -to the standard library. It can be disabled with -`orjson.OPT_PASSTHROUGH_SUBCLASS`.`dataclasses.dataclass` instances -are now serialized by default and cannot be customized in a -`default` function unless `option=orjson.OPT_PASSTHROUGH_DATACLASS` is -specified. `uuid.UUID` instances are serialized by default. -For any type that is now serialized, -implementations in a `default` function and options enabling them can be -removed but do not need to be. There was no change in deserialization. - -To migrate from the standard library, the largest difference is that -`orjson.dumps` returns `bytes` and `json.dumps` returns a `str`. Users with -`dict` objects using non-`str` keys should specify -`option=orjson.OPT_NON_STR_KEYS`. `sort_keys` is replaced by -`option=orjson.OPT_SORT_KEYS`. `indent` is replaced by -`option=orjson.OPT_INDENT_2` and other levels of indentation are not -supported. - -### Serialize - -```python -def dumps( - __obj: Any, - default: Optional[Callable[[Any], Any]] = ..., - option: Optional[int] = ..., -) -> bytes: ... -``` - -`dumps()` serializes Python objects to JSON. - -It natively serializes -`str`, `dict`, `list`, `tuple`, `int`, `float`, `bool`, `None`, -`dataclasses.dataclass`, `typing.TypedDict`, `datetime.datetime`, -`datetime.date`, `datetime.time`, `uuid.UUID`, `numpy.ndarray`, and -`orjson.Fragment` instances. It supports arbitrary types through `default`. It -serializes subclasses of `str`, `int`, `dict`, `list`, -`dataclasses.dataclass`, and `enum.Enum`. It does not serialize subclasses -of `tuple` to avoid serializing `namedtuple` objects as arrays. To avoid -serializing subclasses, specify the option `orjson.OPT_PASSTHROUGH_SUBCLASS`. - -The output is a `bytes` object containing UTF-8. - -The global interpreter lock (GIL) is held for the duration of the call. - -It raises `JSONEncodeError` on an unsupported type. This exception message -describes the invalid object with the error message -`Type is not JSON serializable: ...`. To fix this, specify -[default](https://github.com/ijl/orjson?tab=readme-ov-file#default). - -It raises `JSONEncodeError` on a `str` that contains invalid UTF-8. - -It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or, -with `OPT_STRICT_INTEGER`, 53 bits. - -It raises `JSONEncodeError` if a `dict` has a key of a type other than `str`, -unless `OPT_NON_STR_KEYS` is specified. - -It raises `JSONEncodeError` if the output of `default` recurses to handling by -`default` more than 254 levels deep. - -It raises `JSONEncodeError` on circular references. - -It raises `JSONEncodeError` if a `tzinfo` on a datetime object is -unsupported. - -`JSONEncodeError` is a subclass of `TypeError`. This is for compatibility -with the standard library. - -If the failure was caused by an exception in `default` then -`JSONEncodeError` chains the original exception as `__cause__`. - -#### default - -To serialize a subclass or arbitrary types, specify `default` as a -callable that returns a supported type. `default` may be a function, -lambda, or callable class instance. To specify that a type was not -handled by `default`, raise an exception such as `TypeError`. - -```python ->>> import orjson, decimal ->>> -def default(obj): - if isinstance(obj, decimal.Decimal): - return str(obj) - raise TypeError - ->>> orjson.dumps(decimal.Decimal("0.0842389659712649442845")) -JSONEncodeError: Type is not JSON serializable: decimal.Decimal ->>> orjson.dumps(decimal.Decimal("0.0842389659712649442845"), default=default) -b'"0.0842389659712649442845"' ->>> orjson.dumps({1, 2}, default=default) -orjson.JSONEncodeError: Type is not JSON serializable: set -``` - -The `default` callable may return an object that itself -must be handled by `default` up to 254 times before an exception -is raised. - -It is important that `default` raise an exception if a type cannot be handled. -Python otherwise implicitly returns `None`, which appears to the caller -like a legitimate value and is serialized: - -```python ->>> import orjson, json, rapidjson ->>> -def default(obj): - if isinstance(obj, decimal.Decimal): - return str(obj) - ->>> orjson.dumps({"set":{1, 2}}, default=default) -b'{"set":null}' ->>> json.dumps({"set":{1, 2}}, default=default) -'{"set":null}' ->>> rapidjson.dumps({"set":{1, 2}}, default=default) -'{"set":null}' -``` - -#### option - -To modify how data is serialized, specify `option`. Each `option` is an integer -constant in `orjson`. To specify multiple options, mask them together, e.g., -`option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC`. - -##### OPT_APPEND_NEWLINE - -Append `\n` to the output. This is a convenience and optimization for the -pattern of `dumps(...) + "\n"`. `bytes` objects are immutable and this -pattern copies the original contents. - -```python ->>> import orjson ->>> orjson.dumps([]) -b"[]" ->>> orjson.dumps([], option=orjson.OPT_APPEND_NEWLINE) -b"[]\n" -``` - -##### OPT_INDENT_2 - -Pretty-print output with an indent of two spaces. This is equivalent to -`indent=2` in the standard library. Pretty printing is slower and the output -larger. orjson is the fastest compared library at pretty printing and has -much less of a slowdown to pretty print than the standard library does. This -option is compatible with all other options. - -```python ->>> import orjson ->>> orjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}) -b'{"a":"b","c":{"d":true},"e":[1,2]}' ->>> orjson.dumps( - {"a": "b", "c": {"d": True}, "e": [1, 2]}, - option=orjson.OPT_INDENT_2 -) -b'{\n "a": "b",\n "c": {\n "d": true\n },\n "e": [\n 1,\n 2\n ]\n}' -``` - -If displayed, the indentation and linebreaks appear like this: - -```json -{ - "a": "b", - "c": { - "d": true - }, - "e": [ - 1, - 2 - ] -} -``` - -This measures serializing the github.json fixture as compact (52KiB) or -pretty (64KiB): - -| Library | compact (ms) | pretty (ms) | vs. orjson | -|------------|----------------|---------------|--------------| -| orjson | 0.03 | 0.04 | 1 | -| ujson | 0.18 | 0.19 | 4.6 | -| rapidjson | 0.1 | 0.12 | 2.9 | -| simplejson | 0.25 | 0.89 | 21.4 | -| json | 0.18 | 0.71 | 17 | - -This measures serializing the citm_catalog.json fixture, more of a worst -case due to the amount of nesting and newlines, as compact (489KiB) or -pretty (1.1MiB): - -| Library | compact (ms) | pretty (ms) | vs. orjson | -|------------|----------------|---------------|--------------| -| orjson | 0.59 | 0.71 | 1 | -| ujson | 2.9 | 3.59 | 5 | -| rapidjson | 1.81 | 2.8 | 3.9 | -| simplejson | 10.43 | 42.13 | 59.1 | -| json | 4.16 | 33.42 | 46.9 | - -This can be reproduced using the `pyindent` script. - -##### OPT_NAIVE_UTC - -Serialize `datetime.datetime` objects without a `tzinfo` as UTC. This -has no effect on `datetime.datetime` objects that have `tzinfo` set. - -```python ->>> import orjson, datetime ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0), - ) -b'"1970-01-01T00:00:00"' ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0), - option=orjson.OPT_NAIVE_UTC, - ) -b'"1970-01-01T00:00:00+00:00"' -``` - -##### OPT_NON_STR_KEYS - -Serialize `dict` keys of type other than `str`. This allows `dict` keys -to be one of `str`, `int`, `float`, `bool`, `None`, `datetime.datetime`, -`datetime.date`, `datetime.time`, `enum.Enum`, and `uuid.UUID`. For comparison, -the standard library serializes `str`, `int`, `float`, `bool` or `None` by -default. orjson benchmarks as being faster at serializing non-`str` keys -than other libraries. This option is slower for `str` keys than the default. - -```python ->>> import orjson, datetime, uuid ->>> orjson.dumps( - {uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"): [1, 2, 3]}, - option=orjson.OPT_NON_STR_KEYS, - ) -b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":[1,2,3]}' ->>> orjson.dumps( - {datetime.datetime(1970, 1, 1, 0, 0, 0): [1, 2, 3]}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_NAIVE_UTC, - ) -b'{"1970-01-01T00:00:00+00:00":[1,2,3]}' -``` - -These types are generally serialized how they would be as -values, e.g., `datetime.datetime` is still an RFC 3339 string and respects -options affecting it. The exception is that `int` serialization does not -respect `OPT_STRICT_INTEGER`. - -This option has the risk of creating duplicate keys. This is because non-`str` -objects may serialize to the same `str` as an existing key, e.g., -`{"1": true, 1: false}`. The last key to be inserted to the `dict` will be -serialized last and a JSON deserializer will presumably take the last -occurrence of a key (in the above, `false`). The first value will be lost. - -This option is compatible with `orjson.OPT_SORT_KEYS`. If sorting is used, -note the sort is unstable and will be unpredictable for duplicate keys. - -```python ->>> import orjson, datetime ->>> orjson.dumps( - {"other": 1, datetime.date(1970, 1, 5): 2, datetime.date(1970, 1, 3): 3}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS -) -b'{"1970-01-03":3,"1970-01-05":2,"other":1}' -``` - -This measures serializing 589KiB of JSON comprising a `list` of 100 `dict` -in which each `dict` has both 365 randomly-sorted `int` keys representing epoch -timestamps as well as one `str` key and the value for each key is a -single integer. In "str keys", the keys were converted to `str` before -serialization, and orjson still specifes `option=orjson.OPT_NON_STR_KEYS` -(which is always somewhat slower). - -| Library | str keys (ms) | int keys (ms) | int keys sorted (ms) | -|------------|-----------------|-----------------|------------------------| -| orjson | 1.53 | 2.16 | 4.29 | -| ujson | 3.07 | 5.65 | | -| rapidjson | 4.29 | | | -| simplejson | 11.24 | 14.50 | 21.86 | -| json | 7.17 | 8.49 | | - -ujson is blank for sorting because it segfaults. json is blank because it -raises `TypeError` on attempting to sort before converting all keys to `str`. -rapidjson is blank because it does not support non-`str` keys. This can -be reproduced using the `pynonstr` script. - -##### OPT_OMIT_MICROSECONDS - -Do not serialize the `microsecond` field on `datetime.datetime` and -`datetime.time` instances. - -```python ->>> import orjson, datetime ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0, 1), - ) -b'"1970-01-01T00:00:00.000001"' ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0, 1), - option=orjson.OPT_OMIT_MICROSECONDS, - ) -b'"1970-01-01T00:00:00"' -``` - -##### OPT_PASSTHROUGH_DATACLASS - -Passthrough `dataclasses.dataclass` instances to `default`. This allows -customizing their output but is much slower. - - -```python ->>> import orjson, dataclasses ->>> -@dataclasses.dataclass -class User: - id: str - name: str - password: str - -def default(obj): - if isinstance(obj, User): - return {"id": obj.id, "name": obj.name} - raise TypeError - ->>> orjson.dumps(User("3b1", "asd", "zxc")) -b'{"id":"3b1","name":"asd","password":"zxc"}' ->>> orjson.dumps(User("3b1", "asd", "zxc"), option=orjson.OPT_PASSTHROUGH_DATACLASS) -TypeError: Type is not JSON serializable: User ->>> orjson.dumps( - User("3b1", "asd", "zxc"), - option=orjson.OPT_PASSTHROUGH_DATACLASS, - default=default, - ) -b'{"id":"3b1","name":"asd"}' -``` - -##### OPT_PASSTHROUGH_DATETIME - -Passthrough `datetime.datetime`, `datetime.date`, and `datetime.time` instances -to `default`. This allows serializing datetimes to a custom format, e.g., -HTTP dates: - -```python ->>> import orjson, datetime ->>> -def default(obj): - if isinstance(obj, datetime.datetime): - return obj.strftime("%a, %d %b %Y %H:%M:%S GMT") - raise TypeError - ->>> orjson.dumps({"created_at": datetime.datetime(1970, 1, 1)}) -b'{"created_at":"1970-01-01T00:00:00"}' ->>> orjson.dumps({"created_at": datetime.datetime(1970, 1, 1)}, option=orjson.OPT_PASSTHROUGH_DATETIME) -TypeError: Type is not JSON serializable: datetime.datetime ->>> orjson.dumps( - {"created_at": datetime.datetime(1970, 1, 1)}, - option=orjson.OPT_PASSTHROUGH_DATETIME, - default=default, - ) -b'{"created_at":"Thu, 01 Jan 1970 00:00:00 GMT"}' -``` - -This does not affect datetimes in `dict` keys if using OPT_NON_STR_KEYS. - -##### OPT_PASSTHROUGH_SUBCLASS - -Passthrough subclasses of builtin types to `default`. - -```python ->>> import orjson ->>> -class Secret(str): - pass - -def default(obj): - if isinstance(obj, Secret): - return "******" - raise TypeError - ->>> orjson.dumps(Secret("zxc")) -b'"zxc"' ->>> orjson.dumps(Secret("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS) -TypeError: Type is not JSON serializable: Secret ->>> orjson.dumps(Secret("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS, default=default) -b'"******"' -``` - -This does not affect serializing subclasses as `dict` keys if using -OPT_NON_STR_KEYS. - -##### OPT_SERIALIZE_DATACLASS - -This is deprecated and has no effect in version 3. In version 2 this was -required to serialize `dataclasses.dataclass` instances. For more, see -[dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass). - -##### OPT_SERIALIZE_NUMPY - -Serialize `numpy.ndarray` instances. For more, see -[numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy). - -##### OPT_SERIALIZE_UUID - -This is deprecated and has no effect in version 3. In version 2 this was -required to serialize `uuid.UUID` instances. For more, see -[UUID](https://github.com/ijl/orjson?tab=readme-ov-file#UUID). - -##### OPT_SORT_KEYS - -Serialize `dict` keys in sorted order. The default is to serialize in an -unspecified order. This is equivalent to `sort_keys=True` in the standard -library. - -This can be used to ensure the order is deterministic for hashing or tests. -It has a substantial performance penalty and is not recommended in general. - -```python ->>> import orjson ->>> orjson.dumps({"b": 1, "c": 2, "a": 3}) -b'{"b":1,"c":2,"a":3}' ->>> orjson.dumps({"b": 1, "c": 2, "a": 3}, option=orjson.OPT_SORT_KEYS) -b'{"a":3,"b":1,"c":2}' -``` - -This measures serializing the twitter.json fixture unsorted and sorted: - -| Library | unsorted (ms) | sorted (ms) | vs. orjson | -|------------|-----------------|---------------|--------------| -| orjson | 0.32 | 0.54 | 1 | -| ujson | 1.6 | 2.07 | 3.8 | -| rapidjson | 1.12 | 1.65 | 3.1 | -| simplejson | 2.25 | 3.13 | 5.8 | -| json | 1.78 | 2.32 | 4.3 | - -The benchmark can be reproduced using the `pysort` script. - -The sorting is not collation/locale-aware: - -```python ->>> import orjson ->>> orjson.dumps({"a": 1, "ä": 2, "A": 3}, option=orjson.OPT_SORT_KEYS) -b'{"A":3,"a":1,"\xc3\xa4":2}' -``` - -This is the same sorting behavior as the standard library, rapidjson, -simplejson, and ujson. - -`dataclass` also serialize as maps but this has no effect on them. - -##### OPT_STRICT_INTEGER - -Enforce 53-bit limit on integers. The limit is otherwise 64 bits, the same as -the Python standard library. For more, see [int](https://github.com/ijl/orjson?tab=readme-ov-file#int). - -##### OPT_UTC_Z - -Serialize a UTC timezone on `datetime.datetime` instances as `Z` instead -of `+00:00`. - -```python ->>> import orjson, datetime, zoneinfo ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=zoneinfo.ZoneInfo("UTC")), - ) -b'"1970-01-01T00:00:00+00:00"' ->>> orjson.dumps( - datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=zoneinfo.ZoneInfo("UTC")), - option=orjson.OPT_UTC_Z - ) -b'"1970-01-01T00:00:00Z"' -``` - -#### Fragment - -`orjson.Fragment` includes already-serialized JSON in a document. This is an -efficient way to include JSON blobs from a cache, JSONB field, or separately -serialized object without first deserializing to Python objects via `loads()`. - -```python ->>> import orjson ->>> orjson.dumps({"key": "zxc", "data": orjson.Fragment(b'{"a": "b", "c": 1}')}) -b'{"key":"zxc","data":{"a": "b", "c": 1}}' -``` - -It does no reformatting: `orjson.OPT_INDENT_2` will not affect a -compact blob nor will a pretty-printed JSON blob be rewritten as compact. - -The input must be `bytes` or `str` and given as a positional argument. - -This raises `orjson.JSONEncodeError` if a `str` is given and the input is -not valid UTF-8. It otherwise does no validation and it is possible to -write invalid JSON. This does not escape characters. The implementation is -tested to not crash if given invalid strings or invalid JSON. - -This is similar to `RawJSON` in rapidjson. - -### Deserialize - -```python -def loads(__obj: Union[bytes, bytearray, memoryview, str]) -> Any: ... -``` - -`loads()` deserializes JSON to Python objects. It deserializes to `dict`, -`list`, `int`, `float`, `str`, `bool`, and `None` objects. - -`bytes`, `bytearray`, `memoryview`, and `str` input are accepted. If the input -exists as a `memoryview`, `bytearray`, or `bytes` object, it is recommended to -pass these directly rather than creating an unnecessary `str` object. That is, -`orjson.loads(b"{}")` instead of `orjson.loads(b"{}".decode("utf-8"))`. This -has lower memory usage and lower latency. - -The input must be valid UTF-8. - -orjson maintains a cache of map keys for the duration of the process. This -causes a net reduction in memory usage by avoiding duplicate strings. The -keys must be at most 64 bytes to be cached and 2048 entries are stored. - -The global interpreter lock (GIL) is held for the duration of the call. - -It raises `JSONDecodeError` if given an invalid type or invalid -JSON. This includes if the input contains `NaN`, `Infinity`, or `-Infinity`, -which the standard library allows, but is not valid JSON. - -It raises `JSONDecodeError` if a combination of array or object recurses -1024 levels deep. - -`JSONDecodeError` is a subclass of `json.JSONDecodeError` and `ValueError`. -This is for compatibility with the standard library. - -## Types - -### dataclass - -orjson serializes instances of `dataclasses.dataclass` natively. It serializes -instances 40-50x as fast as other libraries and avoids a severe slowdown seen -in other libraries compared to serializing `dict`. - -It is supported to pass all variants of dataclasses, including dataclasses -using `__slots__`, frozen dataclasses, those with optional or default -attributes, and subclasses. There is a performance benefit to not -using `__slots__`. - -| Library | dict (ms) | dataclass (ms) | vs. orjson | -|------------|-------------|------------------|--------------| -| orjson | 1.40 | 1.60 | 1 | -| ujson | | | | -| rapidjson | 3.64 | 68.48 | 42 | -| simplejson | 14.21 | 92.18 | 57 | -| json | 13.28 | 94.90 | 59 | - -This measures serializing 555KiB of JSON, orjson natively and other libraries -using `default` to serialize the output of `dataclasses.asdict()`. This can be -reproduced using the `pydataclass` script. - -Dataclasses are serialized as maps, with every attribute serialized and in -the order given on class definition: - -```python ->>> import dataclasses, orjson, typing - -@dataclasses.dataclass -class Member: - id: int - active: bool = dataclasses.field(default=False) - -@dataclasses.dataclass -class Object: - id: int - name: str - members: typing.List[Member] - ->>> orjson.dumps(Object(1, "a", [Member(1, True), Member(2)])) -b'{"id":1,"name":"a","members":[{"id":1,"active":true},{"id":2,"active":false}]}' -``` - -### datetime - -orjson serializes `datetime.datetime` objects to -[RFC 3339](https://tools.ietf.org/html/rfc3339) format, -e.g., "1970-01-01T00:00:00+00:00". This is a subset of ISO 8601 and is -compatible with `isoformat()` in the standard library. - -```python ->>> import orjson, datetime, zoneinfo ->>> orjson.dumps( - datetime.datetime(2018, 12, 1, 2, 3, 4, 9, tzinfo=zoneinfo.ZoneInfo("Australia/Adelaide")) -) -b'"2018-12-01T02:03:04.000009+10:30"' ->>> orjson.dumps( - datetime.datetime(2100, 9, 1, 21, 55, 2).replace(tzinfo=zoneinfo.ZoneInfo("UTC")) -) -b'"2100-09-01T21:55:02+00:00"' ->>> orjson.dumps( - datetime.datetime(2100, 9, 1, 21, 55, 2) -) -b'"2100-09-01T21:55:02"' -``` - -`datetime.datetime` supports instances with a `tzinfo` that is `None`, -`datetime.timezone.utc`, a timezone instance from the python3.9+ `zoneinfo` -module, or a timezone instance from the third-party `pendulum`, `pytz`, or -`dateutil`/`arrow` libraries. - -It is fastest to use the standard library's `zoneinfo.ZoneInfo` for timezones. - -`datetime.time` objects must not have a `tzinfo`. - -```python ->>> import orjson, datetime ->>> orjson.dumps(datetime.time(12, 0, 15, 290)) -b'"12:00:15.000290"' -``` - -`datetime.date` objects will always serialize. - -```python ->>> import orjson, datetime ->>> orjson.dumps(datetime.date(1900, 1, 2)) -b'"1900-01-02"' -``` - -Errors with `tzinfo` result in `JSONEncodeError` being raised. - -To disable serialization of `datetime` objects specify the option -`orjson.OPT_PASSTHROUGH_DATETIME`. - -To use "Z" suffix instead of "+00:00" to indicate UTC ("Zulu") time, use the option -`orjson.OPT_UTC_Z`. - -To assume datetimes without timezone are UTC, use the option `orjson.OPT_NAIVE_UTC`. - -### enum - -orjson serializes enums natively. Options apply to their values. - -```python ->>> import enum, datetime, orjson ->>> -class DatetimeEnum(enum.Enum): - EPOCH = datetime.datetime(1970, 1, 1, 0, 0, 0) ->>> orjson.dumps(DatetimeEnum.EPOCH) -b'"1970-01-01T00:00:00"' ->>> orjson.dumps(DatetimeEnum.EPOCH, option=orjson.OPT_NAIVE_UTC) -b'"1970-01-01T00:00:00+00:00"' -``` - -Enums with members that are not supported types can be serialized using -`default`: - -```python ->>> import enum, orjson ->>> -class Custom: - def __init__(self, val): - self.val = val - -def default(obj): - if isinstance(obj, Custom): - return obj.val - raise TypeError - -class CustomEnum(enum.Enum): - ONE = Custom(1) - ->>> orjson.dumps(CustomEnum.ONE, default=default) -b'1' -``` - -### float - -orjson serializes and deserializes double precision floats with no loss of -precision and consistent rounding. - -`orjson.dumps()` serializes Nan, Infinity, and -Infinity, which are not -compliant JSON, as `null`: - -```python ->>> import orjson, ujson, rapidjson, json ->>> orjson.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) -b'[null,null,null]' ->>> ujson.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) -OverflowError: Invalid Inf value when encoding double ->>> rapidjson.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) -'[NaN,Infinity,-Infinity]' ->>> json.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) -'[NaN, Infinity, -Infinity]' -``` - -### int - -orjson serializes and deserializes 64-bit integers by default. The range -supported is a signed 64-bit integer's minimum (-9223372036854775807) to -an unsigned 64-bit integer's maximum (18446744073709551615). This -is widely compatible, but there are implementations -that only support 53-bits for integers, e.g., -web browsers. For those implementations, `dumps()` can be configured to -raise a `JSONEncodeError` on values exceeding the 53-bit range. - -```python ->>> import orjson ->>> orjson.dumps(9007199254740992) -b'9007199254740992' ->>> orjson.dumps(9007199254740992, option=orjson.OPT_STRICT_INTEGER) -JSONEncodeError: Integer exceeds 53-bit range ->>> orjson.dumps(-9007199254740992, option=orjson.OPT_STRICT_INTEGER) -JSONEncodeError: Integer exceeds 53-bit range -``` - -### numpy - -orjson natively serializes `numpy.ndarray` and individual -`numpy.float64`, `numpy.float32`, `numpy.float16` (`numpy.half`), -`numpy.int64`, `numpy.int32`, `numpy.int16`, `numpy.int8`, -`numpy.uint64`, `numpy.uint32`, `numpy.uint16`, `numpy.uint8`, -`numpy.uintp`, `numpy.intp`, `numpy.datetime64`, and `numpy.bool` -instances. - -orjson is compatible with both numpy v1 and v2. - -orjson is faster than all compared libraries at serializing -numpy instances. Serializing numpy data requires specifying -`option=orjson.OPT_SERIALIZE_NUMPY`. - -```python ->>> import orjson, numpy ->>> orjson.dumps( - numpy.array([[1, 2, 3], [4, 5, 6]]), - option=orjson.OPT_SERIALIZE_NUMPY, -) -b'[[1,2,3],[4,5,6]]' -``` - -The array must be a contiguous C array (`C_CONTIGUOUS`) and one of the -supported datatypes. - -Note a difference between serializing `numpy.float32` using `ndarray.tolist()` -or `orjson.dumps(..., option=orjson.OPT_SERIALIZE_NUMPY)`: `tolist()` converts -to a `double` before serializing and orjson's native path does not. This -can result in different rounding. - -`numpy.datetime64` instances are serialized as RFC 3339 strings and -datetime options affect them. - -```python ->>> import orjson, numpy ->>> orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=orjson.OPT_SERIALIZE_NUMPY, -) -b'"2021-01-01T00:00:00.172000"' ->>> orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=( - orjson.OPT_SERIALIZE_NUMPY | - orjson.OPT_NAIVE_UTC | - orjson.OPT_OMIT_MICROSECONDS - ), -) -b'"2021-01-01T00:00:00+00:00"' -``` - -If an array is not a contiguous C array, contains an unsupported datatype, -or contains a `numpy.datetime64` using an unsupported representation -(e.g., picoseconds), orjson falls through to `default`. In `default`, -`obj.tolist()` can be specified. - -If an array is not in the native endianness, e.g., an array of big-endian values -on a little-endian system, `orjson.JSONEncodeError` is raised. - -If an array is malformed, `orjson.JSONEncodeError` is raised. - -This measures serializing 92MiB of JSON from an `numpy.ndarray` with -dimensions of `(50000, 100)` and `numpy.float64` values: - -| Library | Latency (ms) | RSS diff (MiB) | vs. orjson | -|------------|----------------|------------------|--------------| -| orjson | 194 | 99 | 1.0 | -| ujson | | | | -| rapidjson | 3,048 | 309 | 15.7 | -| simplejson | 3,023 | 297 | 15.6 | -| json | 3,133 | 297 | 16.1 | - -This measures serializing 100MiB of JSON from an `numpy.ndarray` with -dimensions of `(100000, 100)` and `numpy.int32` values: - -| Library | Latency (ms) | RSS diff (MiB) | vs. orjson | -|------------|----------------|------------------|--------------| -| orjson | 178 | 115 | 1.0 | -| ujson | | | | -| rapidjson | 1,512 | 551 | 8.5 | -| simplejson | 1,606 | 504 | 9.0 | -| json | 1,506 | 503 | 8.4 | - -This measures serializing 105MiB of JSON from an `numpy.ndarray` with -dimensions of `(100000, 200)` and `numpy.bool` values: - -| Library | Latency (ms) | RSS diff (MiB) | vs. orjson | -|------------|----------------|------------------|--------------| -| orjson | 157 | 120 | 1.0 | -| ujson | | | | -| rapidjson | 710 | 327 | 4.5 | -| simplejson | 931 | 398 | 5.9 | -| json | 996 | 400 | 6.3 | - -In these benchmarks, orjson serializes natively, ujson is blank because it -does not support a `default` parameter, and the other libraries serialize -`ndarray.tolist()` via `default`. The RSS column measures peak memory -usage during serialization. This can be reproduced using the `pynumpy` script. - -orjson does not have an installation or compilation dependency on numpy. The -implementation is independent, reading `numpy.ndarray` using -`PyArrayInterface`. - -### str - -orjson is strict about UTF-8 conformance. This is stricter than the standard -library's json module, which will serialize and deserialize UTF-16 surrogates, -e.g., "\ud800", that are invalid UTF-8. - -If `orjson.dumps()` is given a `str` that does not contain valid UTF-8, -`orjson.JSONEncodeError` is raised. If `loads()` receives invalid UTF-8, -`orjson.JSONDecodeError` is raised. - -orjson and rapidjson are the only compared JSON libraries to consistently -error on bad input. - -```python ->>> import orjson, ujson, rapidjson, json ->>> orjson.dumps('\ud800') -JSONEncodeError: str is not valid UTF-8: surrogates not allowed ->>> ujson.dumps('\ud800') -UnicodeEncodeError: 'utf-8' codec ... ->>> rapidjson.dumps('\ud800') -UnicodeEncodeError: 'utf-8' codec ... ->>> json.dumps('\ud800') -'"\\ud800"' ->>> orjson.loads('"\\ud800"') -JSONDecodeError: unexpected end of hex escape at line 1 column 8: line 1 column 1 (char 0) ->>> ujson.loads('"\\ud800"') -'' ->>> rapidjson.loads('"\\ud800"') -ValueError: Parse error at offset 1: The surrogate pair in string is invalid. ->>> json.loads('"\\ud800"') -'\ud800' -``` - -To make a best effort at deserializing bad input, first decode `bytes` using -the `replace` or `lossy` argument for `errors`: - -```python ->>> import orjson ->>> orjson.loads(b'"\xed\xa0\x80"') -JSONDecodeError: str is not valid UTF-8: surrogates not allowed ->>> orjson.loads(b'"\xed\xa0\x80"'.decode("utf-8", "replace")) -'���' -``` - -### uuid - -orjson serializes `uuid.UUID` instances to -[RFC 4122](https://tools.ietf.org/html/rfc4122) format, e.g., -"f81d4fae-7dec-11d0-a765-00a0c91e6bf6". - -``` python ->>> import orjson, uuid ->>> orjson.dumps(uuid.UUID('f81d4fae-7dec-11d0-a765-00a0c91e6bf6')) -b'"f81d4fae-7dec-11d0-a765-00a0c91e6bf6"' ->>> orjson.dumps(uuid.uuid5(uuid.NAMESPACE_DNS, "python.org")) -b'"886313e1-3b8a-5372-9b90-0c9aee199e5d"' -``` - -## Testing - -The library has comprehensive tests. There are tests against fixtures in the -[JSONTestSuite](https://github.com/nst/JSONTestSuite) and -[nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) -repositories. It is tested to not crash against the -[Big List of Naughty Strings](https://github.com/minimaxir/big-list-of-naughty-strings). -It is tested to not leak memory. It is tested to not crash -against and not accept invalid UTF-8. There are integration tests -exercising the library's use in web servers (gunicorn using multiprocess/forked -workers) and when -multithreaded. It also uses some tests from the ultrajson library. - -orjson is the most correct of the compared libraries. This graph shows how each -library handles a combined 342 JSON fixtures from the -[JSONTestSuite](https://github.com/nst/JSONTestSuite) and -[nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) tests: - -| Library | Invalid JSON documents not rejected | Valid JSON documents not deserialized | -|------------|---------------------------------------|-----------------------------------------| -| orjson | 0 | 0 | -| ujson | 31 | 0 | -| rapidjson | 6 | 0 | -| simplejson | 10 | 0 | -| json | 17 | 0 | - -This shows that all libraries deserialize valid JSON but only orjson -correctly rejects the given invalid JSON fixtures. Errors are largely due to -accepting invalid strings and numbers. - -The graph above can be reproduced using the `pycorrectness` script. - -## Performance - -Serialization and deserialization performance of orjson is better than -ultrajson, rapidjson, simplejson, or json. The benchmarks are done on -fixtures of real data: - -* twitter.json, 631.5KiB, results of a search on Twitter for "一", containing -CJK strings, dictionaries of strings and arrays of dictionaries, indented. - -* github.json, 55.8KiB, a GitHub activity feed, containing dictionaries of -strings and arrays of dictionaries, not indented. - -* citm_catalog.json, 1.7MiB, concert data, containing nested dictionaries of -strings and arrays of integers, indented. - -* canada.json, 2.2MiB, coordinates of the Canadian border in GeoJSON -format, containing floats and arrays, indented. - -### Latency - -![Serialization](doc/serialization.png) - -![Deserialization](doc/deserialization.png) - -#### twitter.json serialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 0.1 | 8377 | 1 | -| ujson | 0.9 | 1088 | 7.3 | -| rapidjson | 0.8 | 1228 | 6.8 | -| simplejson | 1.9 | 531 | 15.6 | -| json | 1.4 | 744 | 11.3 | - -#### twitter.json deserialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 0.6 | 1811 | 1 | -| ujson | 1.2 | 814 | 2.1 | -| rapidjson | 2.1 | 476 | 3.8 | -| simplejson | 1.6 | 626 | 3 | -| json | 1.8 | 557 | 3.3 | - -#### github.json serialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 0.01 | 104424 | 1 | -| ujson | 0.09 | 10594 | 9.8 | -| rapidjson | 0.07 | 13667 | 7.6 | -| simplejson | 0.2 | 5051 | 20.6 | -| json | 0.14 | 7133 | 14.6 | - -#### github.json deserialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 0.05 | 20069 | 1 | -| ujson | 0.11 | 8913 | 2.3 | -| rapidjson | 0.13 | 8077 | 2.6 | -| simplejson | 0.11 | 9342 | 2.1 | -| json | 0.11 | 9291 | 2.2 | - -#### citm_catalog.json serialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 0.3 | 3757 | 1 | -| ujson | 1.7 | 598 | 6.3 | -| rapidjson | 1.3 | 768 | 4.9 | -| simplejson | 8.3 | 120 | 31.1 | -| json | 3 | 331 | 11.3 | - -#### citm_catalog.json deserialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 1.4 | 730 | 1 | -| ujson | 2.6 | 384 | 1.9 | -| rapidjson | 4 | 246 | 3 | -| simplejson | 3.7 | 271 | 2.7 | -| json | 3.7 | 267 | 2.7 | - -#### canada.json serialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 2.4 | 410 | 1 | -| ujson | 9.6 | 104 | 3.9 | -| rapidjson | 28.7 | 34 | 11.8 | -| simplejson | 49.3 | 20 | 20.3 | -| json | 30.6 | 32 | 12.6 | - -#### canada.json deserialization - -| Library | Median latency (milliseconds) | Operations per second | Relative (latency) | -|------------|---------------------------------|-------------------------|----------------------| -| orjson | 3 | 336 | 1 | -| ujson | 7.1 | 141 | 2.4 | -| rapidjson | 20.1 | 49 | 6.7 | -| simplejson | 16.8 | 59 | 5.6 | -| json | 18.2 | 55 | 6.1 | - -### Memory - -orjson as of 3.7.0 has higher baseline memory usage than other libraries -due to a persistent buffer used for parsing. Incremental memory usage when -deserializing is similar to the standard library and other third-party -libraries. - -This measures, in the first column, RSS after importing a library and reading -the fixture, and in the second column, increases in RSS after repeatedly -calling `loads()` on the fixture. - -#### twitter.json - -| Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) | -|------------|----------------------------|---------------------------------| -| orjson | 15.7 | 3.4 | -| ujson | 16.4 | 3.4 | -| rapidjson | 16.6 | 4.4 | -| simplejson | 14.5 | 1.8 | -| json | 13.9 | 1.8 | - -#### github.json - -| Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) | -|------------|----------------------------|---------------------------------| -| orjson | 15.2 | 0.4 | -| ujson | 15.4 | 0.4 | -| rapidjson | 15.7 | 0.5 | -| simplejson | 13.7 | 0.2 | -| json | 13.3 | 0.1 | - -#### citm_catalog.json - -| Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) | -|------------|----------------------------|---------------------------------| -| orjson | 16.8 | 10.1 | -| ujson | 17.3 | 10.2 | -| rapidjson | 17.6 | 28.7 | -| simplejson | 15.8 | 30.1 | -| json | 14.8 | 20.5 | - -#### canada.json - -| Library | import, read() RSS (MiB) | loads() increase in RSS (MiB) | -|------------|----------------------------|---------------------------------| -| orjson | 17.2 | 22.1 | -| ujson | 17.4 | 18.3 | -| rapidjson | 18 | 23.5 | -| simplejson | 15.7 | 21.4 | -| json | 15.4 | 20.4 | - -### Reproducing - -The above was measured using Python 3.11.9 on Linux (amd64) with -orjson 3.10.6, ujson 5.10.0, python-rapidson 1.18, and simplejson 3.19.2. - -The latency results can be reproduced using the `pybench` and `graph` -scripts. The memory results can be reproduced using the `pymem` script. - -## Questions - -### Why can't I install it from PyPI? - -Probably `pip` needs to be upgraded to version 20.3 or later to support -the latest manylinux_x_y or universal2 wheel formats. - -### "Cargo, the Rust package manager, is not installed or is not on PATH." - -This happens when there are no binary wheels (like manylinux) for your -platform on PyPI. You can install [Rust](https://www.rust-lang.org/) through -`rustup` or a package manager and then it will compile. - -### Will it deserialize to dataclasses, UUIDs, decimals, etc or support object_hook? - -No. This requires a schema specifying what types are expected and how to -handle errors etc. This is addressed by data validation libraries a -level above this. - -### Will it serialize to `str`? - -No. `bytes` is the correct type for a serialized blob. - -### Will it support NDJSON or JSONL? - -No. [orjsonl](https://github.com/umarbutler/orjsonl) may be appropriate. - -### Will it support JSON5 or RJSON? - -No, it supports RFC 8259. - -## Packaging - -To package orjson requires at least [Rust](https://www.rust-lang.org/) 1.72 -and the [maturin](https://github.com/PyO3/maturin) build tool. The recommended -build command is: - -```sh -maturin build --release --strip -``` - -It benefits from also having a C build environment to compile a faster -deserialization backend. See this project's `manylinux_2_28` builds for an -example using clang and LTO. - -The project's own CI tests against `nightly-2024-09-25` and stable 1.72. It -is prudent to pin the nightly version because that channel can introduce -breaking changes. - -orjson is tested for amd64 on Linux and cross-compiles for aarch64, arm7, -ppc64le, and s390x. It is tested for either aarch64 or amd64 on macOS and -cross-compiles for the other, depending on version. For Windows it is -tested on amd64 and i686. - -There are no runtime dependencies other than libc. - -The source distribution on PyPI contains all dependencies' source and can be -built without network access. The file can be downloaded from -`https://files.pythonhosted.org/packages/source/o/orjson/orjson-${version}.tar.gz`. - -orjson's tests are included in the source distribution on PyPI. The -requirements to run the tests are specified in `test/requirements.txt`. The -tests should be run as part of the build. It can be run with -`pytest -q test`. - -## License - -orjson was written by ijl <>, copyright 2018 - 2024, available -to you under either the Apache 2 license or MIT license at your choice. diff --git a/vendor/orjson/bench/__init__.py b/vendor/orjson/bench/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/orjson/bench/benchmark_dumps.py b/vendor/orjson/bench/benchmark_dumps.py deleted file mode 100644 index d5be6e7d8e..0000000000 --- a/vendor/orjson/bench/benchmark_dumps.py +++ /dev/null @@ -1,19 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from json import loads as json_loads - -import pytest - -from .data import fixtures, libraries -from .util import read_fixture_obj - - -@pytest.mark.parametrize("library", libraries) -@pytest.mark.parametrize("fixture", fixtures) -def test_dumps(benchmark, fixture, library): - dumper, loader = libraries[library] - benchmark.group = f"{fixture} serialization" - benchmark.extra_info["lib"] = library - data = read_fixture_obj(f"{fixture}.xz") - benchmark.extra_info["correct"] = json_loads(dumper(data)) == data # type: ignore - benchmark(dumper, data) diff --git a/vendor/orjson/bench/benchmark_empty.py b/vendor/orjson/bench/benchmark_empty.py deleted file mode 100644 index 7d0a945ecc..0000000000 --- a/vendor/orjson/bench/benchmark_empty.py +++ /dev/null @@ -1,16 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from json import loads as json_loads - -import pytest - -from .data import libraries - - -@pytest.mark.parametrize("data", ["[]", "{}", '""']) -@pytest.mark.parametrize("library", libraries) -def test_empty(benchmark, data, library): - dumper, loader = libraries[library] - correct = json_loads(dumper(loader(data))) == json_loads(data) # type: ignore - benchmark.extra_info["correct"] = correct - benchmark(loader, data) diff --git a/vendor/orjson/bench/benchmark_loads.py b/vendor/orjson/bench/benchmark_loads.py deleted file mode 100644 index 89f417d32c..0000000000 --- a/vendor/orjson/bench/benchmark_loads.py +++ /dev/null @@ -1,20 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from json import loads as json_loads - -import pytest - -from .data import fixtures, libraries -from .util import read_fixture - - -@pytest.mark.parametrize("fixture", fixtures) -@pytest.mark.parametrize("library", libraries) -def test_loads(benchmark, fixture, library): - dumper, loader = libraries[library] - benchmark.group = f"{fixture} deserialization" - benchmark.extra_info["lib"] = library - data = read_fixture(f"{fixture}.xz") - correct = json_loads(dumper(loader(data))) == json_loads(data) # type: ignore - benchmark.extra_info["correct"] = correct - benchmark(loader, data) diff --git a/vendor/orjson/bench/data.py b/vendor/orjson/bench/data.py deleted file mode 100644 index 079957a6dd..0000000000 --- a/vendor/orjson/bench/data.py +++ /dev/null @@ -1,47 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from json import dumps as _json_dumps -from json import loads as json_loads - -from rapidjson import dumps as _rapidjson_dumps -from rapidjson import loads as rapidjson_loads -from simplejson import dumps as _simplejson_dumps -from simplejson import loads as simplejson_loads -from ujson import dumps as _ujson_dumps -from ujson import loads as ujson_loads - -from orjson import dumps as orjson_dumps -from orjson import loads as orjson_loads - - -def ujson_dumps(obj): - return _ujson_dumps(obj).encode("utf-8") - - -def rapidjson_dumps(obj): - return _rapidjson_dumps(obj).encode("utf-8") - - -def json_dumps(obj): - return _json_dumps(obj).encode("utf-8") - - -def simplejson_dumps(obj): - return _simplejson_dumps(obj).encode("utf-8") - - -libraries = { - "orjson": (orjson_dumps, orjson_loads), - "ujson": (ujson_dumps, ujson_loads), - "json": (json_dumps, json_loads), - "rapidjson": (rapidjson_dumps, rapidjson_loads), - "simplejson": (simplejson_dumps, simplejson_loads), -} - - -fixtures = [ - "canada.json", - "citm_catalog.json", - "github.json", - "twitter.json", -] diff --git a/vendor/orjson/bench/requirements.txt b/vendor/orjson/bench/requirements.txt deleted file mode 100644 index 40d524ca29..0000000000 --- a/vendor/orjson/bench/requirements.txt +++ /dev/null @@ -1,9 +0,0 @@ -memory-profiler -pandas; python_version<"3.13" -pytest-benchmark -pytest-random-order -python-rapidjson -seaborn; python_version<"3.13" -simplejson -tabulate -ujson diff --git a/vendor/orjson/bench/run_default b/vendor/orjson/bench/run_default deleted file mode 100755 index e587b5a648..0000000000 --- a/vendor/orjson/bench/run_default +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import sys -import os - -os.sched_setaffinity(os.getpid(), {0, 1}) - -from orjson import dumps, OPT_SERIALIZE_NUMPY - -class Custom: - pass - - -def default(_): - return None - -n = int(sys.argv[1]) if len(sys.argv) >= 2 else 10000 - -obj = [[Custom()] * 1000] * 10 -for _ in range(n): - dumps(obj, default, OPT_SERIALIZE_NUMPY) diff --git a/vendor/orjson/bench/run_func b/vendor/orjson/bench/run_func deleted file mode 100755 index 8cfe60d52b..0000000000 --- a/vendor/orjson/bench/run_func +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import sys -import lzma -import os -import gc - -gc.disable() - -os.sched_setaffinity(os.getpid(), {0, 1}) - -from orjson import dumps, loads - -filename = sys.argv[1] -n = int(sys.argv[3]) if len(sys.argv) >= 4 else 1000 - -with lzma.open(filename, "r") as fileh: - file_bytes = fileh.read() - -if sys.argv[2] == "dumps": - file_obj = loads(file_bytes) - for _ in range(n): - dumps(file_obj) -elif sys.argv[2] == "loads": - for _ in range(n): - loads(file_bytes) diff --git a/vendor/orjson/bench/run_mem b/vendor/orjson/bench/run_mem deleted file mode 100755 index e62e755ac7..0000000000 --- a/vendor/orjson/bench/run_mem +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import sys -import lzma -import gc - -import psutil - -filename = sys.argv[1] - -with lzma.open(filename, "r") as fileh: - fixture = fileh.read() - -proc = psutil.Process() - -lib_name = sys.argv[2] -if lib_name == "json": - from json import dumps, loads -elif lib_name == "orjson": - from orjson import dumps, loads -elif lib_name == "rapidjson": - from rapidjson import dumps, loads -elif lib_name == "simplejson": - from simplejson import dumps, loads -elif lib_name == "ujson": - from ujson import dumps, loads -else: - raise NotImplementedError - -gc.collect() - -mem_before = proc.memory_info().rss - -for _ in range(100): - val = loads(fixture) - -mem_after = proc.memory_info().rss - -mem_diff = mem_after - mem_before - -from json import loads as json_loads - -correct = 1 if (json_loads(fixture) == json_loads(dumps(loads(fixture)))) else 0 - -print(f"{mem_before},{mem_diff},{correct}") diff --git a/vendor/orjson/bench/util.py b/vendor/orjson/bench/util.py deleted file mode 100644 index 7771ab60a8..0000000000 --- a/vendor/orjson/bench/util.py +++ /dev/null @@ -1,29 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import lzma -import os -from functools import lru_cache -from pathlib import Path -from typing import Any - -import orjson - -dirname = os.path.join(os.path.dirname(__file__), "../data") - -if hasattr(os, "sched_setaffinity"): - os.sched_setaffinity(os.getpid(), {0, 1}) - - -@lru_cache(maxsize=None) -def read_fixture(filename: str) -> bytes: - path = Path(dirname, filename) - if path.suffix == ".xz": - contents = lzma.decompress(path.read_bytes()) - else: - contents = path.read_bytes() - return contents - - -@lru_cache(maxsize=None) -def read_fixture_obj(filename: str) -> Any: - return orjson.loads(read_fixture(filename)) diff --git a/vendor/orjson/build.rs b/vendor/orjson/build.rs deleted file mode 100644 index ad6d5b9ced..0000000000 --- a/vendor/orjson/build.rs +++ /dev/null @@ -1,84 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use std::env; - -fn main() { - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=include/yyjson/*"); - println!("cargo:rerun-if-env-changed=CC"); - println!("cargo:rerun-if-env-changed=CFLAGS"); - println!("cargo:rerun-if-env-changed=LDFLAGS"); - println!("cargo:rerun-if-env-changed=ORJSON_DISABLE_AVX512"); - println!("cargo:rerun-if-env-changed=ORJSON_DISABLE_SIMD"); - println!("cargo:rerun-if-env-changed=ORJSON_DISABLE_YYJSON"); - println!("cargo:rerun-if-env-changed=RUSTFLAGS"); - println!("cargo:rustc-check-cfg=cfg(intrinsics)"); - println!("cargo:rustc-check-cfg=cfg(optimize)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_10)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_11)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_12)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_13)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_14)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_8)"); - println!("cargo:rustc-check-cfg=cfg(Py_3_9)"); - println!("cargo:rustc-check-cfg=cfg(Py_GIL_DISABLED)"); - - let python_config = pyo3_build_config::get(); - for cfg in python_config.build_script_outputs() { - println!("{cfg}"); - } - - if let Some(true) = version_check::supports_feature("core_intrinsics") { - println!("cargo:rustc-cfg=feature=\"intrinsics\""); - } - - if let Some(true) = version_check::supports_feature("optimize_attribute") { - println!("cargo:rustc-cfg=feature=\"optimize\""); - } - - #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] - if env::var("ORJSON_DISABLE_SIMD").is_err() { - // auto build unstable SIMD on nightly - if let Some(true) = version_check::supports_feature("portable_simd") { - println!("cargo:rustc-cfg=feature=\"unstable-simd\""); - } - // auto build AVX512 on x86-64-v4 or supporting native targets - #[cfg(all(target_arch = "x86_64", target_feature = "avx512vl"))] - if let Some(true) = version_check::supports_feature("stdarch_x86_avx512") { - if env::var("ORJSON_DISABLE_AVX512").is_err() { - println!("cargo:rustc-cfg=feature=\"avx512\""); - } - } - } - - #[cfg(all( - target_pointer_width = "64", - any(target_arch = "x86_64", target_arch = "aarch64") - ))] - println!("cargo:rustc-cfg=feature=\"inline_int\""); - - if env::var("ORJSON_DISABLE_YYJSON").is_ok() { - if env::var("CARGO_FEATURE_YYJSON").is_ok() { - panic!("ORJSON_DISABLE_YYJSON and --features=yyjson both enabled.") - } - } else { - match cc::Build::new() - .file("include/yyjson/yyjson.c") - .include("include/yyjson") - .define("YYJSON_DISABLE_NON_STANDARD", "1") - .define("YYJSON_DISABLE_UTF8_VALIDATION", "1") - .define("YYJSON_DISABLE_UTILS", "1") - .define("YYJSON_DISABLE_WRITER", "1") - .try_compile("yyjson") - { - Ok(_) => { - println!("cargo:rustc-cfg=feature=\"yyjson\""); - } - Err(_) => { - if env::var("CARGO_FEATURE_YYJSON").is_ok() { - panic!("yyjson was enabled but the build failed. To build with a different backend do not specify the feature.") - } - } - } - } -} diff --git a/vendor/orjson/ci/azure-pipelines.yml b/vendor/orjson/ci/azure-pipelines.yml deleted file mode 100644 index 8382734e3d..0000000000 --- a/vendor/orjson/ci/azure-pipelines.yml +++ /dev/null @@ -1,196 +0,0 @@ -variables: - toolchain: nightly-2024-09-25 - -jobs: - -- job: win_python313_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.13.0\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.13.0' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python312_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.12.2\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.12.2' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python311_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.11.4\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.11.4' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python310_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.10.8\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.10.8' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python39_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.9.13\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.9.13' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python38_amd64 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.8.10\x64\python.exe - rustup: https://win.rustup.rs/x86_64 - target: x86_64-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.8.10' - addToPath: true - architecture: 'x64' - - checkout: self - - template: ./azure-win.yml - -- job: win_python313_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.13.0\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.13.0' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml - -- job: win_python312_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.12.2\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.12.2' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml - -- job: win_python311_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.11.4\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.11.4' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml - -- job: win_python310_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.10.8\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.10.8' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml - -- job: win_python39_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.9.13\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.9.13' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml - -- job: win_python38_x86 - pool: - vmImage: windows-2022 - variables: - interpreter: C:\hostedtoolcache\windows\Python\3.8.10\x86\python.exe - rustup: https://win.rustup.rs/x86 - target: i686-pc-windows-msvc - steps: - - task: UsePythonVersion@0 - inputs: - versionSpec: '3.8.10' - addToPath: true - architecture: 'x86' - - checkout: self - - template: ./azure-win.yml diff --git a/vendor/orjson/ci/azure-win.yml b/vendor/orjson/ci/azure-win.yml deleted file mode 100644 index 655eab96cb..0000000000 --- a/vendor/orjson/ci/azure-win.yml +++ /dev/null @@ -1,41 +0,0 @@ -parameters: - interpreter: '' - rustup: '' - target: '' - toolchain: '' - -steps: -- script: | - curl $(rustup) -o rustup-init.exe - rustup-init.exe -y --default-host $(target) --default-toolchain $(toolchain)-$(target) --profile minimal - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - rustup default $(toolchain)-$(target) - rustup component add rust-src - mkdir .cargo - cp ci/config.toml .cargo/config.toml - echo "##vso[task.setvariable variable=PATH;]%PATH%;%USERPROFILE%\.cargo\bin" - displayName: rustup -- script: python.exe -m pip install --upgrade pip "maturin>=1,<2" wheel - displayName: build dependencies -- script: python.exe -m pip install -r test\requirements.txt -r integration\requirements.txt - displayName: test dependencies -- script: maturin.exe build --release --features=no-panic,unstable-simd,yyjson --strip --interpreter $(interpreter) --target $(target) - displayName: build - env: - CFLAGS: "-Os -flto" - LDFLAGS: "-Wl,--as-needed" - RUSTFLAGS: "-C lto=fat -Z mir-opt-level=4 -D warnings" - CARGO_UNSTABLE_SPARSE_REGISTRY: "true" - UNSAFE_PYO3_SKIP_VERSION_CHECK: "1" -- script: python.exe -m pip install orjson --no-index --find-links=D:\a\1\s\target\wheels - displayName: install -- script: python.exe -m pytest -s -rxX -v test - env: - PYTHONMALLOC: "debug" - displayName: pytest -- script: python.exe integration\thread - displayName: thread -- script: python.exe integration\init - displayName: init -- bash: ./ci/deploy /d/a/1/s/target/wheels/*.whl - displayName: deploy diff --git a/vendor/orjson/ci/config.toml b/vendor/orjson/ci/config.toml deleted file mode 100644 index 0823eb5a49..0000000000 --- a/vendor/orjson/ci/config.toml +++ /dev/null @@ -1,11 +0,0 @@ -[unstable] -build-std = ["core", "std", "alloc", "proc_macro", "panic_abort"] -build-std-features = ["panic_immediate_abort"] - -[target.x86_64-apple-darwin] -linker = "clang" -rustflags = ["-C", "target-cpu=x86-64-v2", "-Z", "tune-cpu=generic"] - -[target.aarch64-apple-darwin] -linker = "clang" -rustflags = ["-C", "target-cpu=apple-m1"] diff --git a/vendor/orjson/ci/deploy b/vendor/orjson/ci/deploy deleted file mode 100755 index 1b34b539f0..0000000000 --- a/vendor/orjson/ci/deploy +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -if [ -z ${DRONE_TAG+x} ]; then - tag=$(git name-rev --tags --name-only $(git rev-parse HEAD)) -else - tag="$DRONE_TAG" -fi - -echo "$tag" - -if [[ "$tag" == "undefined" ]]; then - echo "not on a tag" - exit 0 -fi - -maturin upload --skip-existing "$1" diff --git a/vendor/orjson/ci/sdist.toml b/vendor/orjson/ci/sdist.toml deleted file mode 100644 index 0e35b81fcb..0000000000 --- a/vendor/orjson/ci/sdist.toml +++ /dev/null @@ -1,5 +0,0 @@ -[source.crates-io] -replace-with = "vendored-sources" - -[source.vendored-sources] -directory = "include/cargo" diff --git a/vendor/orjson/data/blns.txt.xz b/vendor/orjson/data/blns.txt.xz deleted file mode 100644 index 4abd62e360..0000000000 Binary files a/vendor/orjson/data/blns.txt.xz and /dev/null differ diff --git a/vendor/orjson/data/canada.json.xz b/vendor/orjson/data/canada.json.xz deleted file mode 100644 index 1a583cf8f8..0000000000 Binary files a/vendor/orjson/data/canada.json.xz and /dev/null differ diff --git a/vendor/orjson/data/citm_catalog.json.xz b/vendor/orjson/data/citm_catalog.json.xz deleted file mode 100644 index 2d8e9cf1a1..0000000000 Binary files a/vendor/orjson/data/citm_catalog.json.xz and /dev/null differ diff --git a/vendor/orjson/data/github.json.xz b/vendor/orjson/data/github.json.xz deleted file mode 100644 index d601a039c6..0000000000 Binary files a/vendor/orjson/data/github.json.xz and /dev/null differ diff --git a/vendor/orjson/data/issue331_1.json.xz b/vendor/orjson/data/issue331_1.json.xz deleted file mode 100644 index c61ee5fe27..0000000000 Binary files a/vendor/orjson/data/issue331_1.json.xz and /dev/null differ diff --git a/vendor/orjson/data/issue331_2.json.xz b/vendor/orjson/data/issue331_2.json.xz deleted file mode 100644 index ad742f03dd..0000000000 Binary files a/vendor/orjson/data/issue331_2.json.xz and /dev/null differ diff --git a/vendor/orjson/data/jsonchecker/fail01.json b/vendor/orjson/data/jsonchecker/fail01.json deleted file mode 100644 index 6216b865f1..0000000000 --- a/vendor/orjson/data/jsonchecker/fail01.json +++ /dev/null @@ -1 +0,0 @@ -"A JSON payload should be an object or array, not a string." \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail02.json b/vendor/orjson/data/jsonchecker/fail02.json deleted file mode 100644 index 6b7c11e5a5..0000000000 --- a/vendor/orjson/data/jsonchecker/fail02.json +++ /dev/null @@ -1 +0,0 @@ -["Unclosed array" \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail03.json b/vendor/orjson/data/jsonchecker/fail03.json deleted file mode 100644 index 168c81eb78..0000000000 --- a/vendor/orjson/data/jsonchecker/fail03.json +++ /dev/null @@ -1 +0,0 @@ -{unquoted_key: "keys must be quoted"} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail04.json b/vendor/orjson/data/jsonchecker/fail04.json deleted file mode 100644 index 9de168bf34..0000000000 --- a/vendor/orjson/data/jsonchecker/fail04.json +++ /dev/null @@ -1 +0,0 @@ -["extra comma",] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail05.json b/vendor/orjson/data/jsonchecker/fail05.json deleted file mode 100644 index ddf3ce3d24..0000000000 --- a/vendor/orjson/data/jsonchecker/fail05.json +++ /dev/null @@ -1 +0,0 @@ -["double extra comma",,] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail06.json b/vendor/orjson/data/jsonchecker/fail06.json deleted file mode 100644 index ed91580e1b..0000000000 --- a/vendor/orjson/data/jsonchecker/fail06.json +++ /dev/null @@ -1 +0,0 @@ -[ , "<-- missing value"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail07.json b/vendor/orjson/data/jsonchecker/fail07.json deleted file mode 100644 index 8a96af3e4e..0000000000 --- a/vendor/orjson/data/jsonchecker/fail07.json +++ /dev/null @@ -1 +0,0 @@ -["Comma after the close"], \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail08.json b/vendor/orjson/data/jsonchecker/fail08.json deleted file mode 100644 index b28479c6ec..0000000000 --- a/vendor/orjson/data/jsonchecker/fail08.json +++ /dev/null @@ -1 +0,0 @@ -["Extra close"]] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail09.json b/vendor/orjson/data/jsonchecker/fail09.json deleted file mode 100644 index 5815574f36..0000000000 --- a/vendor/orjson/data/jsonchecker/fail09.json +++ /dev/null @@ -1 +0,0 @@ -{"Extra comma": true,} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail10.json b/vendor/orjson/data/jsonchecker/fail10.json deleted file mode 100644 index 5d8c0047bd..0000000000 --- a/vendor/orjson/data/jsonchecker/fail10.json +++ /dev/null @@ -1 +0,0 @@ -{"Extra value after close": true} "misplaced quoted value" \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail11.json b/vendor/orjson/data/jsonchecker/fail11.json deleted file mode 100644 index 76eb95b458..0000000000 --- a/vendor/orjson/data/jsonchecker/fail11.json +++ /dev/null @@ -1 +0,0 @@ -{"Illegal expression": 1 + 2} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail12.json b/vendor/orjson/data/jsonchecker/fail12.json deleted file mode 100644 index 77580a4522..0000000000 --- a/vendor/orjson/data/jsonchecker/fail12.json +++ /dev/null @@ -1 +0,0 @@ -{"Illegal invocation": alert()} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail13.json b/vendor/orjson/data/jsonchecker/fail13.json deleted file mode 100644 index 379406b59b..0000000000 --- a/vendor/orjson/data/jsonchecker/fail13.json +++ /dev/null @@ -1 +0,0 @@ -{"Numbers cannot have leading zeroes": 013} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail14.json b/vendor/orjson/data/jsonchecker/fail14.json deleted file mode 100644 index 0ed366b38a..0000000000 --- a/vendor/orjson/data/jsonchecker/fail14.json +++ /dev/null @@ -1 +0,0 @@ -{"Numbers cannot be hex": 0x14} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail15.json b/vendor/orjson/data/jsonchecker/fail15.json deleted file mode 100644 index fc8376b605..0000000000 --- a/vendor/orjson/data/jsonchecker/fail15.json +++ /dev/null @@ -1 +0,0 @@ -["Illegal backslash escape: \x15"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail16.json b/vendor/orjson/data/jsonchecker/fail16.json deleted file mode 100644 index 3fe21d4b53..0000000000 --- a/vendor/orjson/data/jsonchecker/fail16.json +++ /dev/null @@ -1 +0,0 @@ -[\naked] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail17.json b/vendor/orjson/data/jsonchecker/fail17.json deleted file mode 100644 index 62b9214aed..0000000000 --- a/vendor/orjson/data/jsonchecker/fail17.json +++ /dev/null @@ -1 +0,0 @@ -["Illegal backslash escape: \017"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail18.json b/vendor/orjson/data/jsonchecker/fail18.json deleted file mode 100644 index edac92716f..0000000000 --- a/vendor/orjson/data/jsonchecker/fail18.json +++ /dev/null @@ -1 +0,0 @@ -[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail19.json b/vendor/orjson/data/jsonchecker/fail19.json deleted file mode 100644 index 3b9c46fa9a..0000000000 --- a/vendor/orjson/data/jsonchecker/fail19.json +++ /dev/null @@ -1 +0,0 @@ -{"Missing colon" null} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail20.json b/vendor/orjson/data/jsonchecker/fail20.json deleted file mode 100644 index 27c1af3e72..0000000000 --- a/vendor/orjson/data/jsonchecker/fail20.json +++ /dev/null @@ -1 +0,0 @@ -{"Double colon":: null} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail21.json b/vendor/orjson/data/jsonchecker/fail21.json deleted file mode 100644 index 62474573b2..0000000000 --- a/vendor/orjson/data/jsonchecker/fail21.json +++ /dev/null @@ -1 +0,0 @@ -{"Comma instead of colon", null} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail22.json b/vendor/orjson/data/jsonchecker/fail22.json deleted file mode 100644 index a7752581bc..0000000000 --- a/vendor/orjson/data/jsonchecker/fail22.json +++ /dev/null @@ -1 +0,0 @@ -["Colon instead of comma": false] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail23.json b/vendor/orjson/data/jsonchecker/fail23.json deleted file mode 100644 index 494add1ca1..0000000000 --- a/vendor/orjson/data/jsonchecker/fail23.json +++ /dev/null @@ -1 +0,0 @@ -["Bad value", truth] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail24.json b/vendor/orjson/data/jsonchecker/fail24.json deleted file mode 100644 index caff239bfc..0000000000 --- a/vendor/orjson/data/jsonchecker/fail24.json +++ /dev/null @@ -1 +0,0 @@ -['single quote'] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail25.json b/vendor/orjson/data/jsonchecker/fail25.json deleted file mode 100644 index 8b7ad23e01..0000000000 --- a/vendor/orjson/data/jsonchecker/fail25.json +++ /dev/null @@ -1 +0,0 @@ -[" tab character in string "] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail26.json b/vendor/orjson/data/jsonchecker/fail26.json deleted file mode 100644 index 845d26a6a5..0000000000 --- a/vendor/orjson/data/jsonchecker/fail26.json +++ /dev/null @@ -1 +0,0 @@ -["tab\ character\ in\ string\ "] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail27.json b/vendor/orjson/data/jsonchecker/fail27.json deleted file mode 100644 index 6b01a2ca4a..0000000000 --- a/vendor/orjson/data/jsonchecker/fail27.json +++ /dev/null @@ -1,2 +0,0 @@ -["line -break"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail28.json b/vendor/orjson/data/jsonchecker/fail28.json deleted file mode 100644 index 621a0101c6..0000000000 --- a/vendor/orjson/data/jsonchecker/fail28.json +++ /dev/null @@ -1,2 +0,0 @@ -["line\ -break"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail29.json b/vendor/orjson/data/jsonchecker/fail29.json deleted file mode 100644 index 47ec421bb6..0000000000 --- a/vendor/orjson/data/jsonchecker/fail29.json +++ /dev/null @@ -1 +0,0 @@ -[0e] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail30.json b/vendor/orjson/data/jsonchecker/fail30.json deleted file mode 100644 index 8ab0bc4b8b..0000000000 --- a/vendor/orjson/data/jsonchecker/fail30.json +++ /dev/null @@ -1 +0,0 @@ -[0e+] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail31.json b/vendor/orjson/data/jsonchecker/fail31.json deleted file mode 100644 index 1cce602b51..0000000000 --- a/vendor/orjson/data/jsonchecker/fail31.json +++ /dev/null @@ -1 +0,0 @@ -[0e+-1] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail32.json b/vendor/orjson/data/jsonchecker/fail32.json deleted file mode 100644 index 45cba7396f..0000000000 --- a/vendor/orjson/data/jsonchecker/fail32.json +++ /dev/null @@ -1 +0,0 @@ -{"Comma instead if closing brace": true, \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/fail33.json b/vendor/orjson/data/jsonchecker/fail33.json deleted file mode 100644 index ca5eb19dc9..0000000000 --- a/vendor/orjson/data/jsonchecker/fail33.json +++ /dev/null @@ -1 +0,0 @@ -["mismatch"} \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/pass01.json b/vendor/orjson/data/jsonchecker/pass01.json deleted file mode 100644 index 70e2685436..0000000000 --- a/vendor/orjson/data/jsonchecker/pass01.json +++ /dev/null @@ -1,58 +0,0 @@ -[ - "JSON Test Pattern pass1", - {"object with 1 member":["array with 1 element"]}, - {}, - [], - -42, - true, - false, - null, - { - "integer": 1234567890, - "real": -9876.543210, - "e": 0.123456789e-12, - "E": 1.234567890E+34, - "": 23456789012E66, - "zero": 0, - "one": 1, - "space": " ", - "quote": "\"", - "backslash": "\\", - "controls": "\b\f\n\r\t", - "slash": "/ & \/", - "alpha": "abcdefghijklmnopqrstuvwyz", - "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", - "digit": "0123456789", - "0123456789": "digit", - "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", - "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", - "true": true, - "false": false, - "null": null, - "array":[ ], - "object":{ }, - "address": "50 St. James Street", - "url": "http://www.JSON.org/", - "comment": "// /* */": " ", - " s p a c e d " :[1,2 , 3 - -, - -4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], - "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", - "quotes": "" \u0022 %22 0x22 034 "", - "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" -: "A key can be any string" - }, - 0.5 ,98.6 -, -99.44 -, - -1066, -1e1, -0.1e1, -1e-1, -1e00,2e+00,2e-00 -,"rosebud"] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/pass02.json b/vendor/orjson/data/jsonchecker/pass02.json deleted file mode 100644 index d3c63c7ad8..0000000000 --- a/vendor/orjson/data/jsonchecker/pass02.json +++ /dev/null @@ -1 +0,0 @@ -[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] \ No newline at end of file diff --git a/vendor/orjson/data/jsonchecker/pass03.json b/vendor/orjson/data/jsonchecker/pass03.json deleted file mode 100644 index 4528d51f1a..0000000000 --- a/vendor/orjson/data/jsonchecker/pass03.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "JSON Test Pattern pass3": { - "The outermost value": "must be an object or array.", - "In this test": "It is an object." - } -} diff --git a/vendor/orjson/data/parsing/i_number_double_huge_neg_exp.json b/vendor/orjson/data/parsing/i_number_double_huge_neg_exp.json deleted file mode 100644 index ae4c7b71f6..0000000000 --- a/vendor/orjson/data/parsing/i_number_double_huge_neg_exp.json +++ /dev/null @@ -1 +0,0 @@ -[123.456e-789] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_huge_exp.json b/vendor/orjson/data/parsing/i_number_huge_exp.json deleted file mode 100644 index 9b5efa2368..0000000000 --- a/vendor/orjson/data/parsing/i_number_huge_exp.json +++ /dev/null @@ -1 +0,0 @@ -[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_neg_int_huge_exp.json b/vendor/orjson/data/parsing/i_number_neg_int_huge_exp.json deleted file mode 100644 index 3abd58a5c4..0000000000 --- a/vendor/orjson/data/parsing/i_number_neg_int_huge_exp.json +++ /dev/null @@ -1 +0,0 @@ -[-1e+9999] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_pos_double_huge_exp.json b/vendor/orjson/data/parsing/i_number_pos_double_huge_exp.json deleted file mode 100644 index e10a7eb62a..0000000000 --- a/vendor/orjson/data/parsing/i_number_pos_double_huge_exp.json +++ /dev/null @@ -1 +0,0 @@ -[1.5e+9999] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_real_neg_overflow.json b/vendor/orjson/data/parsing/i_number_real_neg_overflow.json deleted file mode 100644 index 3d628a9943..0000000000 --- a/vendor/orjson/data/parsing/i_number_real_neg_overflow.json +++ /dev/null @@ -1 +0,0 @@ -[-123123e100000] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_real_pos_overflow.json b/vendor/orjson/data/parsing/i_number_real_pos_overflow.json deleted file mode 100644 index 54d7d3dcdb..0000000000 --- a/vendor/orjson/data/parsing/i_number_real_pos_overflow.json +++ /dev/null @@ -1 +0,0 @@ -[123123e100000] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_real_underflow.json b/vendor/orjson/data/parsing/i_number_real_underflow.json deleted file mode 100644 index c5236eb26b..0000000000 --- a/vendor/orjson/data/parsing/i_number_real_underflow.json +++ /dev/null @@ -1 +0,0 @@ -[123e-10000000] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_too_big_neg_int.json b/vendor/orjson/data/parsing/i_number_too_big_neg_int.json deleted file mode 100644 index dfa3846197..0000000000 --- a/vendor/orjson/data/parsing/i_number_too_big_neg_int.json +++ /dev/null @@ -1 +0,0 @@ -[-123123123123123123123123123123] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_too_big_pos_int.json b/vendor/orjson/data/parsing/i_number_too_big_pos_int.json deleted file mode 100644 index 338a8c3c0b..0000000000 --- a/vendor/orjson/data/parsing/i_number_too_big_pos_int.json +++ /dev/null @@ -1 +0,0 @@ -[100000000000000000000] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_number_very_big_negative_int.json b/vendor/orjson/data/parsing/i_number_very_big_negative_int.json deleted file mode 100644 index e2d9738c23..0000000000 --- a/vendor/orjson/data/parsing/i_number_very_big_negative_int.json +++ /dev/null @@ -1 +0,0 @@ -[-237462374673276894279832749832423479823246327846] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_object_key_lone_2nd_surrogate.json b/vendor/orjson/data/parsing/i_object_key_lone_2nd_surrogate.json deleted file mode 100644 index 5be7ebaf98..0000000000 --- a/vendor/orjson/data/parsing/i_object_key_lone_2nd_surrogate.json +++ /dev/null @@ -1 +0,0 @@ -{"\uDFAA":0} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_1st_surrogate_but_2nd_missing.json b/vendor/orjson/data/parsing/i_string_1st_surrogate_but_2nd_missing.json deleted file mode 100644 index 3b9e37c67a..0000000000 --- a/vendor/orjson/data/parsing/i_string_1st_surrogate_but_2nd_missing.json +++ /dev/null @@ -1 +0,0 @@ -["\uDADA"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_1st_valid_surrogate_2nd_invalid.json b/vendor/orjson/data/parsing/i_string_1st_valid_surrogate_2nd_invalid.json deleted file mode 100644 index 487592832e..0000000000 --- a/vendor/orjson/data/parsing/i_string_1st_valid_surrogate_2nd_invalid.json +++ /dev/null @@ -1 +0,0 @@ -["\uD888\u1234"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_UTF-16LE_with_BOM.json b/vendor/orjson/data/parsing/i_string_UTF-16LE_with_BOM.json deleted file mode 100644 index 2a79c0629a..0000000000 Binary files a/vendor/orjson/data/parsing/i_string_UTF-16LE_with_BOM.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/i_string_UTF-8_invalid_sequence.json b/vendor/orjson/data/parsing/i_string_UTF-8_invalid_sequence.json deleted file mode 100644 index e2a968a159..0000000000 --- a/vendor/orjson/data/parsing/i_string_UTF-8_invalid_sequence.json +++ /dev/null @@ -1 +0,0 @@ -["日шú"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_UTF8_surrogate_U+D800.json b/vendor/orjson/data/parsing/i_string_UTF8_surrogate_U+D800.json deleted file mode 100644 index 916bff920f..0000000000 --- a/vendor/orjson/data/parsing/i_string_UTF8_surrogate_U+D800.json +++ /dev/null @@ -1 +0,0 @@ -["í €"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_incomplete_surrogate_and_escape_valid.json b/vendor/orjson/data/parsing/i_string_incomplete_surrogate_and_escape_valid.json deleted file mode 100644 index 3cb11d2294..0000000000 --- a/vendor/orjson/data/parsing/i_string_incomplete_surrogate_and_escape_valid.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\n"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_incomplete_surrogate_pair.json b/vendor/orjson/data/parsing/i_string_incomplete_surrogate_pair.json deleted file mode 100644 index 38ec23bb0a..0000000000 --- a/vendor/orjson/data/parsing/i_string_incomplete_surrogate_pair.json +++ /dev/null @@ -1 +0,0 @@ -["\uDd1ea"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_incomplete_surrogates_escape_valid.json b/vendor/orjson/data/parsing/i_string_incomplete_surrogates_escape_valid.json deleted file mode 100644 index c9cd6f6c31..0000000000 --- a/vendor/orjson/data/parsing/i_string_incomplete_surrogates_escape_valid.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\uD800\n"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_invalid_lonely_surrogate.json b/vendor/orjson/data/parsing/i_string_invalid_lonely_surrogate.json deleted file mode 100644 index 3abbd8d8d7..0000000000 --- a/vendor/orjson/data/parsing/i_string_invalid_lonely_surrogate.json +++ /dev/null @@ -1 +0,0 @@ -["\ud800"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_invalid_surrogate.json b/vendor/orjson/data/parsing/i_string_invalid_surrogate.json deleted file mode 100644 index ffddc04f52..0000000000 --- a/vendor/orjson/data/parsing/i_string_invalid_surrogate.json +++ /dev/null @@ -1 +0,0 @@ -["\ud800abc"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_invalid_utf-8.json b/vendor/orjson/data/parsing/i_string_invalid_utf-8.json deleted file mode 100644 index 8e45a7ecae..0000000000 --- a/vendor/orjson/data/parsing/i_string_invalid_utf-8.json +++ /dev/null @@ -1 +0,0 @@ -["ÿ"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_inverted_surrogates_U+1D11E.json b/vendor/orjson/data/parsing/i_string_inverted_surrogates_U+1D11E.json deleted file mode 100644 index 0d5456cc38..0000000000 --- a/vendor/orjson/data/parsing/i_string_inverted_surrogates_U+1D11E.json +++ /dev/null @@ -1 +0,0 @@ -["\uDd1e\uD834"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_iso_latin_1.json b/vendor/orjson/data/parsing/i_string_iso_latin_1.json deleted file mode 100644 index 9389c98231..0000000000 --- a/vendor/orjson/data/parsing/i_string_iso_latin_1.json +++ /dev/null @@ -1 +0,0 @@ -["é"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_lone_second_surrogate.json b/vendor/orjson/data/parsing/i_string_lone_second_surrogate.json deleted file mode 100644 index 1dbd397f39..0000000000 --- a/vendor/orjson/data/parsing/i_string_lone_second_surrogate.json +++ /dev/null @@ -1 +0,0 @@ -["\uDFAA"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_lone_utf8_continuation_byte.json b/vendor/orjson/data/parsing/i_string_lone_utf8_continuation_byte.json deleted file mode 100644 index 729337c0a3..0000000000 --- a/vendor/orjson/data/parsing/i_string_lone_utf8_continuation_byte.json +++ /dev/null @@ -1 +0,0 @@ -["�"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_not_in_unicode_range.json b/vendor/orjson/data/parsing/i_string_not_in_unicode_range.json deleted file mode 100644 index df90a2916c..0000000000 --- a/vendor/orjson/data/parsing/i_string_not_in_unicode_range.json +++ /dev/null @@ -1 +0,0 @@ -["ô¿¿¿"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_overlong_sequence_2_bytes.json b/vendor/orjson/data/parsing/i_string_overlong_sequence_2_bytes.json deleted file mode 100644 index c8cee5e0a1..0000000000 --- a/vendor/orjson/data/parsing/i_string_overlong_sequence_2_bytes.json +++ /dev/null @@ -1 +0,0 @@ -["À¯"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes.json b/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes.json deleted file mode 100644 index 9a91da791a..0000000000 --- a/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes.json +++ /dev/null @@ -1 +0,0 @@ -["üƒ¿¿¿¿"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes_null.json b/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes_null.json deleted file mode 100644 index d24fffdd98..0000000000 --- a/vendor/orjson/data/parsing/i_string_overlong_sequence_6_bytes_null.json +++ /dev/null @@ -1 +0,0 @@ -["ü€€€€€"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_truncated-utf-8.json b/vendor/orjson/data/parsing/i_string_truncated-utf-8.json deleted file mode 100644 index 63c7777fb1..0000000000 --- a/vendor/orjson/data/parsing/i_string_truncated-utf-8.json +++ /dev/null @@ -1 +0,0 @@ -["àÿ"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/i_string_utf16BE_no_BOM.json b/vendor/orjson/data/parsing/i_string_utf16BE_no_BOM.json deleted file mode 100644 index 57e5392ff6..0000000000 Binary files a/vendor/orjson/data/parsing/i_string_utf16BE_no_BOM.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/i_string_utf16LE_no_BOM.json b/vendor/orjson/data/parsing/i_string_utf16LE_no_BOM.json deleted file mode 100644 index c49c1b25d8..0000000000 Binary files a/vendor/orjson/data/parsing/i_string_utf16LE_no_BOM.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/i_structure_500_nested_arrays.json.xz b/vendor/orjson/data/parsing/i_structure_500_nested_arrays.json.xz deleted file mode 100644 index ad22c1c9e5..0000000000 Binary files a/vendor/orjson/data/parsing/i_structure_500_nested_arrays.json.xz and /dev/null differ diff --git a/vendor/orjson/data/parsing/i_structure_UTF-8_BOM_empty_object.json b/vendor/orjson/data/parsing/i_structure_UTF-8_BOM_empty_object.json deleted file mode 100644 index 22fdca1b26..0000000000 --- a/vendor/orjson/data/parsing/i_structure_UTF-8_BOM_empty_object.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_1_true_without_comma.json b/vendor/orjson/data/parsing/n_array_1_true_without_comma.json deleted file mode 100644 index c14e3f6b1e..0000000000 --- a/vendor/orjson/data/parsing/n_array_1_true_without_comma.json +++ /dev/null @@ -1 +0,0 @@ -[1 true] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_a_invalid_utf8.json b/vendor/orjson/data/parsing/n_array_a_invalid_utf8.json deleted file mode 100644 index 38a86e2e65..0000000000 --- a/vendor/orjson/data/parsing/n_array_a_invalid_utf8.json +++ /dev/null @@ -1 +0,0 @@ -[aå] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_colon_instead_of_comma.json b/vendor/orjson/data/parsing/n_array_colon_instead_of_comma.json deleted file mode 100644 index 0d02ad4483..0000000000 --- a/vendor/orjson/data/parsing/n_array_colon_instead_of_comma.json +++ /dev/null @@ -1 +0,0 @@ -["": 1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_comma_after_close.json b/vendor/orjson/data/parsing/n_array_comma_after_close.json deleted file mode 100644 index 2ccba8d950..0000000000 --- a/vendor/orjson/data/parsing/n_array_comma_after_close.json +++ /dev/null @@ -1 +0,0 @@ -[""], \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_comma_and_number.json b/vendor/orjson/data/parsing/n_array_comma_and_number.json deleted file mode 100644 index d2c84e374a..0000000000 --- a/vendor/orjson/data/parsing/n_array_comma_and_number.json +++ /dev/null @@ -1 +0,0 @@ -[,1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_double_comma.json b/vendor/orjson/data/parsing/n_array_double_comma.json deleted file mode 100644 index 0431712bc1..0000000000 --- a/vendor/orjson/data/parsing/n_array_double_comma.json +++ /dev/null @@ -1 +0,0 @@ -[1,,2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_double_extra_comma.json b/vendor/orjson/data/parsing/n_array_double_extra_comma.json deleted file mode 100644 index 3f01d31292..0000000000 --- a/vendor/orjson/data/parsing/n_array_double_extra_comma.json +++ /dev/null @@ -1 +0,0 @@ -["x",,] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_extra_close.json b/vendor/orjson/data/parsing/n_array_extra_close.json deleted file mode 100644 index c12f9fae1c..0000000000 --- a/vendor/orjson/data/parsing/n_array_extra_close.json +++ /dev/null @@ -1 +0,0 @@ -["x"]] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_extra_comma.json b/vendor/orjson/data/parsing/n_array_extra_comma.json deleted file mode 100644 index 5f8ce18e4b..0000000000 --- a/vendor/orjson/data/parsing/n_array_extra_comma.json +++ /dev/null @@ -1 +0,0 @@ -["",] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_incomplete.json b/vendor/orjson/data/parsing/n_array_incomplete.json deleted file mode 100644 index cc65b0b512..0000000000 --- a/vendor/orjson/data/parsing/n_array_incomplete.json +++ /dev/null @@ -1 +0,0 @@ -["x" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_incomplete_invalid_value.json b/vendor/orjson/data/parsing/n_array_incomplete_invalid_value.json deleted file mode 100644 index c21a8f6cff..0000000000 --- a/vendor/orjson/data/parsing/n_array_incomplete_invalid_value.json +++ /dev/null @@ -1 +0,0 @@ -[x \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_inner_array_no_comma.json b/vendor/orjson/data/parsing/n_array_inner_array_no_comma.json deleted file mode 100644 index c70b716471..0000000000 --- a/vendor/orjson/data/parsing/n_array_inner_array_no_comma.json +++ /dev/null @@ -1 +0,0 @@ -[3[4]] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_invalid_utf8.json b/vendor/orjson/data/parsing/n_array_invalid_utf8.json deleted file mode 100644 index 6099d3441a..0000000000 --- a/vendor/orjson/data/parsing/n_array_invalid_utf8.json +++ /dev/null @@ -1 +0,0 @@ -[ÿ] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_items_separated_by_semicolon.json b/vendor/orjson/data/parsing/n_array_items_separated_by_semicolon.json deleted file mode 100644 index d4bd7314ca..0000000000 --- a/vendor/orjson/data/parsing/n_array_items_separated_by_semicolon.json +++ /dev/null @@ -1 +0,0 @@ -[1:2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_just_comma.json b/vendor/orjson/data/parsing/n_array_just_comma.json deleted file mode 100644 index 9d7077c680..0000000000 --- a/vendor/orjson/data/parsing/n_array_just_comma.json +++ /dev/null @@ -1 +0,0 @@ -[,] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_just_minus.json b/vendor/orjson/data/parsing/n_array_just_minus.json deleted file mode 100644 index 29501c6ca2..0000000000 --- a/vendor/orjson/data/parsing/n_array_just_minus.json +++ /dev/null @@ -1 +0,0 @@ -[-] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_missing_value.json b/vendor/orjson/data/parsing/n_array_missing_value.json deleted file mode 100644 index 3a6ba86f3a..0000000000 --- a/vendor/orjson/data/parsing/n_array_missing_value.json +++ /dev/null @@ -1 +0,0 @@ -[ , ""] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_newlines_unclosed.json b/vendor/orjson/data/parsing/n_array_newlines_unclosed.json deleted file mode 100644 index 6466800652..0000000000 --- a/vendor/orjson/data/parsing/n_array_newlines_unclosed.json +++ /dev/null @@ -1,3 +0,0 @@ -["a", -4 -,1, \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_number_and_comma.json b/vendor/orjson/data/parsing/n_array_number_and_comma.json deleted file mode 100644 index 13f6f1d18a..0000000000 --- a/vendor/orjson/data/parsing/n_array_number_and_comma.json +++ /dev/null @@ -1 +0,0 @@ -[1,] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_number_and_several_commas.json b/vendor/orjson/data/parsing/n_array_number_and_several_commas.json deleted file mode 100644 index 0ac408cb8a..0000000000 --- a/vendor/orjson/data/parsing/n_array_number_and_several_commas.json +++ /dev/null @@ -1 +0,0 @@ -[1,,] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_spaces_vertical_tab_formfeed.json b/vendor/orjson/data/parsing/n_array_spaces_vertical_tab_formfeed.json deleted file mode 100644 index 6cd7cf5855..0000000000 --- a/vendor/orjson/data/parsing/n_array_spaces_vertical_tab_formfeed.json +++ /dev/null @@ -1 +0,0 @@ -[" a"\f] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_star_inside.json b/vendor/orjson/data/parsing/n_array_star_inside.json deleted file mode 100644 index 5a5194647a..0000000000 --- a/vendor/orjson/data/parsing/n_array_star_inside.json +++ /dev/null @@ -1 +0,0 @@ -[*] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_unclosed.json b/vendor/orjson/data/parsing/n_array_unclosed.json deleted file mode 100644 index 0607330590..0000000000 --- a/vendor/orjson/data/parsing/n_array_unclosed.json +++ /dev/null @@ -1 +0,0 @@ -["" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_unclosed_trailing_comma.json b/vendor/orjson/data/parsing/n_array_unclosed_trailing_comma.json deleted file mode 100644 index 6604698ffc..0000000000 --- a/vendor/orjson/data/parsing/n_array_unclosed_trailing_comma.json +++ /dev/null @@ -1 +0,0 @@ -[1, \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_unclosed_with_new_lines.json b/vendor/orjson/data/parsing/n_array_unclosed_with_new_lines.json deleted file mode 100644 index 4f61de3fb1..0000000000 --- a/vendor/orjson/data/parsing/n_array_unclosed_with_new_lines.json +++ /dev/null @@ -1,3 +0,0 @@ -[1, -1 -,1 \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_array_unclosed_with_object_inside.json b/vendor/orjson/data/parsing/n_array_unclosed_with_object_inside.json deleted file mode 100644 index 043a87e2db..0000000000 --- a/vendor/orjson/data/parsing/n_array_unclosed_with_object_inside.json +++ /dev/null @@ -1 +0,0 @@ -[{} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_incomplete_false.json b/vendor/orjson/data/parsing/n_incomplete_false.json deleted file mode 100644 index eb18c6a143..0000000000 --- a/vendor/orjson/data/parsing/n_incomplete_false.json +++ /dev/null @@ -1 +0,0 @@ -[fals] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_incomplete_null.json b/vendor/orjson/data/parsing/n_incomplete_null.json deleted file mode 100644 index c18ef53851..0000000000 --- a/vendor/orjson/data/parsing/n_incomplete_null.json +++ /dev/null @@ -1 +0,0 @@ -[nul] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_incomplete_true.json b/vendor/orjson/data/parsing/n_incomplete_true.json deleted file mode 100644 index f451ac6d2e..0000000000 --- a/vendor/orjson/data/parsing/n_incomplete_true.json +++ /dev/null @@ -1 +0,0 @@ -[tru] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_multidigit_number_then_00.json b/vendor/orjson/data/parsing/n_multidigit_number_then_00.json deleted file mode 100644 index c22507b864..0000000000 Binary files a/vendor/orjson/data/parsing/n_multidigit_number_then_00.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_number_++.json b/vendor/orjson/data/parsing/n_number_++.json deleted file mode 100644 index bdb62aaf4d..0000000000 --- a/vendor/orjson/data/parsing/n_number_++.json +++ /dev/null @@ -1 +0,0 @@ -[++1234] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_+1.json b/vendor/orjson/data/parsing/n_number_+1.json deleted file mode 100644 index 3cbe58c92b..0000000000 --- a/vendor/orjson/data/parsing/n_number_+1.json +++ /dev/null @@ -1 +0,0 @@ -[+1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_+Inf.json b/vendor/orjson/data/parsing/n_number_+Inf.json deleted file mode 100644 index 871ae14d53..0000000000 --- a/vendor/orjson/data/parsing/n_number_+Inf.json +++ /dev/null @@ -1 +0,0 @@ -[+Inf] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_-01.json b/vendor/orjson/data/parsing/n_number_-01.json deleted file mode 100644 index 0df32bac80..0000000000 --- a/vendor/orjson/data/parsing/n_number_-01.json +++ /dev/null @@ -1 +0,0 @@ -[-01] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_-1.0..json b/vendor/orjson/data/parsing/n_number_-1.0..json deleted file mode 100644 index 7cf55a85a7..0000000000 --- a/vendor/orjson/data/parsing/n_number_-1.0..json +++ /dev/null @@ -1 +0,0 @@ -[-1.0.] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_-2..json b/vendor/orjson/data/parsing/n_number_-2..json deleted file mode 100644 index 9be84365d0..0000000000 --- a/vendor/orjson/data/parsing/n_number_-2..json +++ /dev/null @@ -1 +0,0 @@ -[-2.] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_-NaN.json b/vendor/orjson/data/parsing/n_number_-NaN.json deleted file mode 100644 index f61615d404..0000000000 --- a/vendor/orjson/data/parsing/n_number_-NaN.json +++ /dev/null @@ -1 +0,0 @@ -[-NaN] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_.-1.json b/vendor/orjson/data/parsing/n_number_.-1.json deleted file mode 100644 index 1c9f2dd1b7..0000000000 --- a/vendor/orjson/data/parsing/n_number_.-1.json +++ /dev/null @@ -1 +0,0 @@ -[.-1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_.2e-3.json b/vendor/orjson/data/parsing/n_number_.2e-3.json deleted file mode 100644 index c6c976f257..0000000000 --- a/vendor/orjson/data/parsing/n_number_.2e-3.json +++ /dev/null @@ -1 +0,0 @@ -[.2e-3] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0.1.2.json b/vendor/orjson/data/parsing/n_number_0.1.2.json deleted file mode 100644 index c83a25621e..0000000000 --- a/vendor/orjson/data/parsing/n_number_0.1.2.json +++ /dev/null @@ -1 +0,0 @@ -[0.1.2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0.3e+.json b/vendor/orjson/data/parsing/n_number_0.3e+.json deleted file mode 100644 index a55a1bfefa..0000000000 --- a/vendor/orjson/data/parsing/n_number_0.3e+.json +++ /dev/null @@ -1 +0,0 @@ -[0.3e+] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0.3e.json b/vendor/orjson/data/parsing/n_number_0.3e.json deleted file mode 100644 index 3dd5df4b3a..0000000000 --- a/vendor/orjson/data/parsing/n_number_0.3e.json +++ /dev/null @@ -1 +0,0 @@ -[0.3e] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0.e1.json b/vendor/orjson/data/parsing/n_number_0.e1.json deleted file mode 100644 index c92c71ccb2..0000000000 --- a/vendor/orjson/data/parsing/n_number_0.e1.json +++ /dev/null @@ -1 +0,0 @@ -[0.e1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0_capital_E+.json b/vendor/orjson/data/parsing/n_number_0_capital_E+.json deleted file mode 100644 index 3ba2c7d6d0..0000000000 --- a/vendor/orjson/data/parsing/n_number_0_capital_E+.json +++ /dev/null @@ -1 +0,0 @@ -[0E+] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0_capital_E.json b/vendor/orjson/data/parsing/n_number_0_capital_E.json deleted file mode 100644 index 5301840d1c..0000000000 --- a/vendor/orjson/data/parsing/n_number_0_capital_E.json +++ /dev/null @@ -1 +0,0 @@ -[0E] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0e+.json b/vendor/orjson/data/parsing/n_number_0e+.json deleted file mode 100644 index 8ab0bc4b8b..0000000000 --- a/vendor/orjson/data/parsing/n_number_0e+.json +++ /dev/null @@ -1 +0,0 @@ -[0e+] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_0e.json b/vendor/orjson/data/parsing/n_number_0e.json deleted file mode 100644 index 47ec421bb6..0000000000 --- a/vendor/orjson/data/parsing/n_number_0e.json +++ /dev/null @@ -1 +0,0 @@ -[0e] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_1.0e+.json b/vendor/orjson/data/parsing/n_number_1.0e+.json deleted file mode 100644 index cd84b9f69e..0000000000 --- a/vendor/orjson/data/parsing/n_number_1.0e+.json +++ /dev/null @@ -1 +0,0 @@ -[1.0e+] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_1.0e-.json b/vendor/orjson/data/parsing/n_number_1.0e-.json deleted file mode 100644 index 4eb7afa0f9..0000000000 --- a/vendor/orjson/data/parsing/n_number_1.0e-.json +++ /dev/null @@ -1 +0,0 @@ -[1.0e-] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_1.0e.json b/vendor/orjson/data/parsing/n_number_1.0e.json deleted file mode 100644 index 21753f4c74..0000000000 --- a/vendor/orjson/data/parsing/n_number_1.0e.json +++ /dev/null @@ -1 +0,0 @@ -[1.0e] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_1_000.json b/vendor/orjson/data/parsing/n_number_1_000.json deleted file mode 100644 index 7b18b66b38..0000000000 --- a/vendor/orjson/data/parsing/n_number_1_000.json +++ /dev/null @@ -1 +0,0 @@ -[1 000.0] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_1eE2.json b/vendor/orjson/data/parsing/n_number_1eE2.json deleted file mode 100644 index 4318a341d7..0000000000 --- a/vendor/orjson/data/parsing/n_number_1eE2.json +++ /dev/null @@ -1 +0,0 @@ -[1eE2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_2.e+3.json b/vendor/orjson/data/parsing/n_number_2.e+3.json deleted file mode 100644 index 4442f394dd..0000000000 --- a/vendor/orjson/data/parsing/n_number_2.e+3.json +++ /dev/null @@ -1 +0,0 @@ -[2.e+3] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_2.e-3.json b/vendor/orjson/data/parsing/n_number_2.e-3.json deleted file mode 100644 index a65060edfc..0000000000 --- a/vendor/orjson/data/parsing/n_number_2.e-3.json +++ /dev/null @@ -1 +0,0 @@ -[2.e-3] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_2.e3.json b/vendor/orjson/data/parsing/n_number_2.e3.json deleted file mode 100644 index 66f7cf701b..0000000000 --- a/vendor/orjson/data/parsing/n_number_2.e3.json +++ /dev/null @@ -1 +0,0 @@ -[2.e3] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_9.e+.json b/vendor/orjson/data/parsing/n_number_9.e+.json deleted file mode 100644 index 732a7b11ce..0000000000 --- a/vendor/orjson/data/parsing/n_number_9.e+.json +++ /dev/null @@ -1 +0,0 @@ -[9.e+] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_Inf.json b/vendor/orjson/data/parsing/n_number_Inf.json deleted file mode 100644 index c40c734c3c..0000000000 --- a/vendor/orjson/data/parsing/n_number_Inf.json +++ /dev/null @@ -1 +0,0 @@ -[Inf] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_NaN.json b/vendor/orjson/data/parsing/n_number_NaN.json deleted file mode 100644 index 4992317909..0000000000 --- a/vendor/orjson/data/parsing/n_number_NaN.json +++ /dev/null @@ -1 +0,0 @@ -[NaN] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_U+FF11_fullwidth_digit_one.json b/vendor/orjson/data/parsing/n_number_U+FF11_fullwidth_digit_one.json deleted file mode 100644 index b14587e5eb..0000000000 --- a/vendor/orjson/data/parsing/n_number_U+FF11_fullwidth_digit_one.json +++ /dev/null @@ -1 +0,0 @@ -[1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_expression.json b/vendor/orjson/data/parsing/n_number_expression.json deleted file mode 100644 index 76fdbc8a49..0000000000 --- a/vendor/orjson/data/parsing/n_number_expression.json +++ /dev/null @@ -1 +0,0 @@ -[1+2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_hex_1_digit.json b/vendor/orjson/data/parsing/n_number_hex_1_digit.json deleted file mode 100644 index 3b214880c6..0000000000 --- a/vendor/orjson/data/parsing/n_number_hex_1_digit.json +++ /dev/null @@ -1 +0,0 @@ -[0x1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_hex_2_digits.json b/vendor/orjson/data/parsing/n_number_hex_2_digits.json deleted file mode 100644 index 83e516ab0e..0000000000 --- a/vendor/orjson/data/parsing/n_number_hex_2_digits.json +++ /dev/null @@ -1 +0,0 @@ -[0x42] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_infinity.json b/vendor/orjson/data/parsing/n_number_infinity.json deleted file mode 100644 index 8c2baf783a..0000000000 --- a/vendor/orjson/data/parsing/n_number_infinity.json +++ /dev/null @@ -1 +0,0 @@ -[Infinity] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_invalid+-.json b/vendor/orjson/data/parsing/n_number_invalid+-.json deleted file mode 100644 index 1cce602b51..0000000000 --- a/vendor/orjson/data/parsing/n_number_invalid+-.json +++ /dev/null @@ -1 +0,0 @@ -[0e+-1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_invalid-negative-real.json b/vendor/orjson/data/parsing/n_number_invalid-negative-real.json deleted file mode 100644 index 5fc3c1efb6..0000000000 --- a/vendor/orjson/data/parsing/n_number_invalid-negative-real.json +++ /dev/null @@ -1 +0,0 @@ -[-123.123foo] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-bigger-int.json b/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-bigger-int.json deleted file mode 100644 index 3b97e580e8..0000000000 --- a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-bigger-int.json +++ /dev/null @@ -1 +0,0 @@ -[123å] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-exponent.json b/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-exponent.json deleted file mode 100644 index ea35d723cd..0000000000 --- a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-exponent.json +++ /dev/null @@ -1 +0,0 @@ -[1e1å] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-int.json b/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-int.json deleted file mode 100644 index 371226e4cd..0000000000 --- a/vendor/orjson/data/parsing/n_number_invalid-utf-8-in-int.json +++ /dev/null @@ -1 +0,0 @@ -[0å] diff --git a/vendor/orjson/data/parsing/n_number_minus_infinity.json b/vendor/orjson/data/parsing/n_number_minus_infinity.json deleted file mode 100644 index cf4133d22d..0000000000 --- a/vendor/orjson/data/parsing/n_number_minus_infinity.json +++ /dev/null @@ -1 +0,0 @@ -[-Infinity] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_minus_sign_with_trailing_garbage.json b/vendor/orjson/data/parsing/n_number_minus_sign_with_trailing_garbage.json deleted file mode 100644 index a6d8e78e7c..0000000000 --- a/vendor/orjson/data/parsing/n_number_minus_sign_with_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -[-foo] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_minus_space_1.json b/vendor/orjson/data/parsing/n_number_minus_space_1.json deleted file mode 100644 index 9a5ebedf67..0000000000 --- a/vendor/orjson/data/parsing/n_number_minus_space_1.json +++ /dev/null @@ -1 +0,0 @@ -[- 1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_neg_int_starting_with_zero.json b/vendor/orjson/data/parsing/n_number_neg_int_starting_with_zero.json deleted file mode 100644 index 67af0960af..0000000000 --- a/vendor/orjson/data/parsing/n_number_neg_int_starting_with_zero.json +++ /dev/null @@ -1 +0,0 @@ -[-012] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_neg_real_without_int_part.json b/vendor/orjson/data/parsing/n_number_neg_real_without_int_part.json deleted file mode 100644 index 1f2a43496e..0000000000 --- a/vendor/orjson/data/parsing/n_number_neg_real_without_int_part.json +++ /dev/null @@ -1 +0,0 @@ -[-.123] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_neg_with_garbage_at_end.json b/vendor/orjson/data/parsing/n_number_neg_with_garbage_at_end.json deleted file mode 100644 index 2aa73119fb..0000000000 --- a/vendor/orjson/data/parsing/n_number_neg_with_garbage_at_end.json +++ /dev/null @@ -1 +0,0 @@ -[-1x] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_real_garbage_after_e.json b/vendor/orjson/data/parsing/n_number_real_garbage_after_e.json deleted file mode 100644 index 9213dfca8d..0000000000 --- a/vendor/orjson/data/parsing/n_number_real_garbage_after_e.json +++ /dev/null @@ -1 +0,0 @@ -[1ea] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_real_with_invalid_utf8_after_e.json b/vendor/orjson/data/parsing/n_number_real_with_invalid_utf8_after_e.json deleted file mode 100644 index 1e52ef964c..0000000000 --- a/vendor/orjson/data/parsing/n_number_real_with_invalid_utf8_after_e.json +++ /dev/null @@ -1 +0,0 @@ -[1eå] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_real_without_fractional_part.json b/vendor/orjson/data/parsing/n_number_real_without_fractional_part.json deleted file mode 100644 index 1de287cf89..0000000000 --- a/vendor/orjson/data/parsing/n_number_real_without_fractional_part.json +++ /dev/null @@ -1 +0,0 @@ -[1.] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_starting_with_dot.json b/vendor/orjson/data/parsing/n_number_starting_with_dot.json deleted file mode 100644 index f682dbdce0..0000000000 --- a/vendor/orjson/data/parsing/n_number_starting_with_dot.json +++ /dev/null @@ -1 +0,0 @@ -[.123] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_with_alpha.json b/vendor/orjson/data/parsing/n_number_with_alpha.json deleted file mode 100644 index 1e42d81822..0000000000 --- a/vendor/orjson/data/parsing/n_number_with_alpha.json +++ /dev/null @@ -1 +0,0 @@ -[1.2a-3] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_with_alpha_char.json b/vendor/orjson/data/parsing/n_number_with_alpha_char.json deleted file mode 100644 index b79daccb8a..0000000000 --- a/vendor/orjson/data/parsing/n_number_with_alpha_char.json +++ /dev/null @@ -1 +0,0 @@ -[1.8011670033376514H-308] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_number_with_leading_zero.json b/vendor/orjson/data/parsing/n_number_with_leading_zero.json deleted file mode 100644 index 7106da1f3b..0000000000 --- a/vendor/orjson/data/parsing/n_number_with_leading_zero.json +++ /dev/null @@ -1 +0,0 @@ -[012] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_bad_value.json b/vendor/orjson/data/parsing/n_object_bad_value.json deleted file mode 100644 index a03a8c03b7..0000000000 --- a/vendor/orjson/data/parsing/n_object_bad_value.json +++ /dev/null @@ -1 +0,0 @@ -["x", truth] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_bracket_key.json b/vendor/orjson/data/parsing/n_object_bracket_key.json deleted file mode 100644 index cc443b4832..0000000000 --- a/vendor/orjson/data/parsing/n_object_bracket_key.json +++ /dev/null @@ -1 +0,0 @@ -{[: "x"} diff --git a/vendor/orjson/data/parsing/n_object_comma_instead_of_colon.json b/vendor/orjson/data/parsing/n_object_comma_instead_of_colon.json deleted file mode 100644 index 8d56377087..0000000000 --- a/vendor/orjson/data/parsing/n_object_comma_instead_of_colon.json +++ /dev/null @@ -1 +0,0 @@ -{"x", null} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_double_colon.json b/vendor/orjson/data/parsing/n_object_double_colon.json deleted file mode 100644 index 80e8c7b89a..0000000000 --- a/vendor/orjson/data/parsing/n_object_double_colon.json +++ /dev/null @@ -1 +0,0 @@ -{"x"::"b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_emoji.json b/vendor/orjson/data/parsing/n_object_emoji.json deleted file mode 100644 index cb4078eaa1..0000000000 --- a/vendor/orjson/data/parsing/n_object_emoji.json +++ /dev/null @@ -1 +0,0 @@ -{🇨🇭} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_garbage_at_end.json b/vendor/orjson/data/parsing/n_object_garbage_at_end.json deleted file mode 100644 index 80c42cbadc..0000000000 --- a/vendor/orjson/data/parsing/n_object_garbage_at_end.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"a" 123} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_key_with_single_quotes.json b/vendor/orjson/data/parsing/n_object_key_with_single_quotes.json deleted file mode 100644 index 77c3275996..0000000000 --- a/vendor/orjson/data/parsing/n_object_key_with_single_quotes.json +++ /dev/null @@ -1 +0,0 @@ -{key: 'value'} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json b/vendor/orjson/data/parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json deleted file mode 100644 index aa2cb637cd..0000000000 --- a/vendor/orjson/data/parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json +++ /dev/null @@ -1 +0,0 @@ -{"¹":"0",} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_missing_colon.json b/vendor/orjson/data/parsing/n_object_missing_colon.json deleted file mode 100644 index b98eff62da..0000000000 --- a/vendor/orjson/data/parsing/n_object_missing_colon.json +++ /dev/null @@ -1 +0,0 @@ -{"a" b} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_missing_key.json b/vendor/orjson/data/parsing/n_object_missing_key.json deleted file mode 100644 index b4fb0f528e..0000000000 --- a/vendor/orjson/data/parsing/n_object_missing_key.json +++ /dev/null @@ -1 +0,0 @@ -{:"b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_missing_semicolon.json b/vendor/orjson/data/parsing/n_object_missing_semicolon.json deleted file mode 100644 index e3451384f8..0000000000 --- a/vendor/orjson/data/parsing/n_object_missing_semicolon.json +++ /dev/null @@ -1 +0,0 @@ -{"a" "b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_missing_value.json b/vendor/orjson/data/parsing/n_object_missing_value.json deleted file mode 100644 index 3ef538a60e..0000000000 --- a/vendor/orjson/data/parsing/n_object_missing_value.json +++ /dev/null @@ -1 +0,0 @@ -{"a": \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_no-colon.json b/vendor/orjson/data/parsing/n_object_no-colon.json deleted file mode 100644 index f3797b3576..0000000000 --- a/vendor/orjson/data/parsing/n_object_no-colon.json +++ /dev/null @@ -1 +0,0 @@ -{"a" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_non_string_key.json b/vendor/orjson/data/parsing/n_object_non_string_key.json deleted file mode 100644 index b9945b34b4..0000000000 --- a/vendor/orjson/data/parsing/n_object_non_string_key.json +++ /dev/null @@ -1 +0,0 @@ -{1:1} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_non_string_key_but_huge_number_instead.json b/vendor/orjson/data/parsing/n_object_non_string_key_but_huge_number_instead.json deleted file mode 100644 index b37fa86c0e..0000000000 --- a/vendor/orjson/data/parsing/n_object_non_string_key_but_huge_number_instead.json +++ /dev/null @@ -1 +0,0 @@ -{9999E9999:1} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_repeated_null_null.json b/vendor/orjson/data/parsing/n_object_repeated_null_null.json deleted file mode 100644 index f7d2959d0d..0000000000 --- a/vendor/orjson/data/parsing/n_object_repeated_null_null.json +++ /dev/null @@ -1 +0,0 @@ -{null:null,null:null} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_several_trailing_commas.json b/vendor/orjson/data/parsing/n_object_several_trailing_commas.json deleted file mode 100644 index 3c9afe8dc9..0000000000 --- a/vendor/orjson/data/parsing/n_object_several_trailing_commas.json +++ /dev/null @@ -1 +0,0 @@ -{"id":0,,,,,} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_single_quote.json b/vendor/orjson/data/parsing/n_object_single_quote.json deleted file mode 100644 index e5cdf976ad..0000000000 --- a/vendor/orjson/data/parsing/n_object_single_quote.json +++ /dev/null @@ -1 +0,0 @@ -{'a':0} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_trailing_comma.json b/vendor/orjson/data/parsing/n_object_trailing_comma.json deleted file mode 100644 index a4b0250945..0000000000 --- a/vendor/orjson/data/parsing/n_object_trailing_comma.json +++ /dev/null @@ -1 +0,0 @@ -{"id":0,} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_trailing_comment.json b/vendor/orjson/data/parsing/n_object_trailing_comment.json deleted file mode 100644 index a372c6553d..0000000000 --- a/vendor/orjson/data/parsing/n_object_trailing_comment.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}/**/ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_trailing_comment_open.json b/vendor/orjson/data/parsing/n_object_trailing_comment_open.json deleted file mode 100644 index d557f41ca4..0000000000 --- a/vendor/orjson/data/parsing/n_object_trailing_comment_open.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}/**// \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open.json b/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open.json deleted file mode 100644 index e335136c07..0000000000 --- a/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}// \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open_incomplete.json b/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open_incomplete.json deleted file mode 100644 index d892e49f17..0000000000 --- a/vendor/orjson/data/parsing/n_object_trailing_comment_slash_open_incomplete.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}/ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_two_commas_in_a_row.json b/vendor/orjson/data/parsing/n_object_two_commas_in_a_row.json deleted file mode 100644 index 7c639ae649..0000000000 --- a/vendor/orjson/data/parsing/n_object_two_commas_in_a_row.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b",,"c":"d"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_unquoted_key.json b/vendor/orjson/data/parsing/n_object_unquoted_key.json deleted file mode 100644 index 8ba137293c..0000000000 --- a/vendor/orjson/data/parsing/n_object_unquoted_key.json +++ /dev/null @@ -1 +0,0 @@ -{a: "b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_unterminated-value.json b/vendor/orjson/data/parsing/n_object_unterminated-value.json deleted file mode 100644 index 7fe699a6a3..0000000000 --- a/vendor/orjson/data/parsing/n_object_unterminated-value.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"a \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_with_single_string.json b/vendor/orjson/data/parsing/n_object_with_single_string.json deleted file mode 100644 index d63f7fbb7e..0000000000 --- a/vendor/orjson/data/parsing/n_object_with_single_string.json +++ /dev/null @@ -1 +0,0 @@ -{ "foo" : "bar", "a" } \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_object_with_trailing_garbage.json b/vendor/orjson/data/parsing/n_object_with_trailing_garbage.json deleted file mode 100644 index 787c8f0a8c..0000000000 --- a/vendor/orjson/data/parsing/n_object_with_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}# \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_single_space.json b/vendor/orjson/data/parsing/n_single_space.json deleted file mode 100644 index 0519ecba6e..0000000000 --- a/vendor/orjson/data/parsing/n_single_space.json +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape.json b/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape.json deleted file mode 100644 index acec66d8f4..0000000000 --- a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u.json b/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u.json deleted file mode 100644 index e834b05e96..0000000000 --- a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\u"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1.json b/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1.json deleted file mode 100644 index a04cd34892..0000000000 --- a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\u1"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1x.json b/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1x.json deleted file mode 100644 index bfbd234098..0000000000 --- a/vendor/orjson/data/parsing/n_string_1_surrogate_then_escape_u1x.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\u1x"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_accentuated_char_no_quotes.json b/vendor/orjson/data/parsing/n_string_accentuated_char_no_quotes.json deleted file mode 100644 index fd6895693f..0000000000 --- a/vendor/orjson/data/parsing/n_string_accentuated_char_no_quotes.json +++ /dev/null @@ -1 +0,0 @@ -[é] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_backslash_00.json b/vendor/orjson/data/parsing/n_string_backslash_00.json deleted file mode 100644 index b5bf267b5d..0000000000 Binary files a/vendor/orjson/data/parsing/n_string_backslash_00.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_string_escape_x.json b/vendor/orjson/data/parsing/n_string_escape_x.json deleted file mode 100644 index fae291938d..0000000000 --- a/vendor/orjson/data/parsing/n_string_escape_x.json +++ /dev/null @@ -1 +0,0 @@ -["\x00"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_escaped_backslash_bad.json b/vendor/orjson/data/parsing/n_string_escaped_backslash_bad.json deleted file mode 100644 index 016fcb47ef..0000000000 --- a/vendor/orjson/data/parsing/n_string_escaped_backslash_bad.json +++ /dev/null @@ -1 +0,0 @@ -["\\\"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_escaped_ctrl_char_tab.json b/vendor/orjson/data/parsing/n_string_escaped_ctrl_char_tab.json deleted file mode 100644 index f35ea382bb..0000000000 --- a/vendor/orjson/data/parsing/n_string_escaped_ctrl_char_tab.json +++ /dev/null @@ -1 +0,0 @@ -["\ "] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_escaped_emoji.json b/vendor/orjson/data/parsing/n_string_escaped_emoji.json deleted file mode 100644 index a277754213..0000000000 --- a/vendor/orjson/data/parsing/n_string_escaped_emoji.json +++ /dev/null @@ -1 +0,0 @@ -["\🌀"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_incomplete_escape.json b/vendor/orjson/data/parsing/n_string_incomplete_escape.json deleted file mode 100644 index 3415c33ca8..0000000000 --- a/vendor/orjson/data/parsing/n_string_incomplete_escape.json +++ /dev/null @@ -1 +0,0 @@ -["\"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_incomplete_escaped_character.json b/vendor/orjson/data/parsing/n_string_incomplete_escaped_character.json deleted file mode 100644 index 0f2197ea29..0000000000 --- a/vendor/orjson/data/parsing/n_string_incomplete_escaped_character.json +++ /dev/null @@ -1 +0,0 @@ -["\u00A"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_incomplete_surrogate.json b/vendor/orjson/data/parsing/n_string_incomplete_surrogate.json deleted file mode 100644 index 75504a6561..0000000000 --- a/vendor/orjson/data/parsing/n_string_incomplete_surrogate.json +++ /dev/null @@ -1 +0,0 @@ -["\uD834\uDd"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_incomplete_surrogate_escape_invalid.json b/vendor/orjson/data/parsing/n_string_incomplete_surrogate_escape_invalid.json deleted file mode 100644 index bd9656060d..0000000000 --- a/vendor/orjson/data/parsing/n_string_incomplete_surrogate_escape_invalid.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\uD800\x"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_invalid-utf-8-in-escape.json b/vendor/orjson/data/parsing/n_string_invalid-utf-8-in-escape.json deleted file mode 100644 index 0c43006430..0000000000 --- a/vendor/orjson/data/parsing/n_string_invalid-utf-8-in-escape.json +++ /dev/null @@ -1 +0,0 @@ -["\uå"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_invalid_backslash_esc.json b/vendor/orjson/data/parsing/n_string_invalid_backslash_esc.json deleted file mode 100644 index d1eb60921a..0000000000 --- a/vendor/orjson/data/parsing/n_string_invalid_backslash_esc.json +++ /dev/null @@ -1 +0,0 @@ -["\a"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_invalid_unicode_escape.json b/vendor/orjson/data/parsing/n_string_invalid_unicode_escape.json deleted file mode 100644 index 7608cb6ba8..0000000000 --- a/vendor/orjson/data/parsing/n_string_invalid_unicode_escape.json +++ /dev/null @@ -1 +0,0 @@ -["\uqqqq"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_invalid_utf8_after_escape.json b/vendor/orjson/data/parsing/n_string_invalid_utf8_after_escape.json deleted file mode 100644 index 2f757a25b5..0000000000 --- a/vendor/orjson/data/parsing/n_string_invalid_utf8_after_escape.json +++ /dev/null @@ -1 +0,0 @@ -["\å"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_leading_uescaped_thinspace.json b/vendor/orjson/data/parsing/n_string_leading_uescaped_thinspace.json deleted file mode 100644 index 7b297c6365..0000000000 --- a/vendor/orjson/data/parsing/n_string_leading_uescaped_thinspace.json +++ /dev/null @@ -1 +0,0 @@ -[\u0020"asd"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_no_quotes_with_bad_escape.json b/vendor/orjson/data/parsing/n_string_no_quotes_with_bad_escape.json deleted file mode 100644 index 01bc70abae..0000000000 --- a/vendor/orjson/data/parsing/n_string_no_quotes_with_bad_escape.json +++ /dev/null @@ -1 +0,0 @@ -[\n] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_single_doublequote.json b/vendor/orjson/data/parsing/n_string_single_doublequote.json deleted file mode 100644 index 9d68933c44..0000000000 --- a/vendor/orjson/data/parsing/n_string_single_doublequote.json +++ /dev/null @@ -1 +0,0 @@ -" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_single_quote.json b/vendor/orjson/data/parsing/n_string_single_quote.json deleted file mode 100644 index caff239bfc..0000000000 --- a/vendor/orjson/data/parsing/n_string_single_quote.json +++ /dev/null @@ -1 +0,0 @@ -['single quote'] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_single_string_no_double_quotes.json b/vendor/orjson/data/parsing/n_string_single_string_no_double_quotes.json deleted file mode 100644 index f2ba8f84ab..0000000000 --- a/vendor/orjson/data/parsing/n_string_single_string_no_double_quotes.json +++ /dev/null @@ -1 +0,0 @@ -abc \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_start_escape_unclosed.json b/vendor/orjson/data/parsing/n_string_start_escape_unclosed.json deleted file mode 100644 index db62a46fcb..0000000000 --- a/vendor/orjson/data/parsing/n_string_start_escape_unclosed.json +++ /dev/null @@ -1 +0,0 @@ -["\ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_unescaped_crtl_char.json b/vendor/orjson/data/parsing/n_string_unescaped_crtl_char.json deleted file mode 100644 index 9f21348071..0000000000 Binary files a/vendor/orjson/data/parsing/n_string_unescaped_crtl_char.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_string_unescaped_newline.json b/vendor/orjson/data/parsing/n_string_unescaped_newline.json deleted file mode 100644 index 700d360869..0000000000 --- a/vendor/orjson/data/parsing/n_string_unescaped_newline.json +++ /dev/null @@ -1,2 +0,0 @@ -["new -line"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_unescaped_tab.json b/vendor/orjson/data/parsing/n_string_unescaped_tab.json deleted file mode 100644 index 160264a2d9..0000000000 --- a/vendor/orjson/data/parsing/n_string_unescaped_tab.json +++ /dev/null @@ -1 +0,0 @@ -[" "] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_unicode_CapitalU.json b/vendor/orjson/data/parsing/n_string_unicode_CapitalU.json deleted file mode 100644 index 17332bb174..0000000000 --- a/vendor/orjson/data/parsing/n_string_unicode_CapitalU.json +++ /dev/null @@ -1 +0,0 @@ -"\UA66D" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_string_with_trailing_garbage.json b/vendor/orjson/data/parsing/n_string_with_trailing_garbage.json deleted file mode 100644 index efe3bd272c..0000000000 --- a/vendor/orjson/data/parsing/n_string_with_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -""x \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_100000_opening_arrays.json.xz b/vendor/orjson/data/parsing/n_structure_100000_opening_arrays.json.xz deleted file mode 100644 index 55b4040c9e..0000000000 Binary files a/vendor/orjson/data/parsing/n_structure_100000_opening_arrays.json.xz and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_structure_U+2060_word_joined.json b/vendor/orjson/data/parsing/n_structure_U+2060_word_joined.json deleted file mode 100644 index 81156a6996..0000000000 --- a/vendor/orjson/data/parsing/n_structure_U+2060_word_joined.json +++ /dev/null @@ -1 +0,0 @@ -[⁠] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_UTF8_BOM_no_data.json b/vendor/orjson/data/parsing/n_structure_UTF8_BOM_no_data.json deleted file mode 100644 index 5f282702bb..0000000000 --- a/vendor/orjson/data/parsing/n_structure_UTF8_BOM_no_data.json +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_angle_bracket_..json b/vendor/orjson/data/parsing/n_structure_angle_bracket_..json deleted file mode 100644 index a56fef0b02..0000000000 --- a/vendor/orjson/data/parsing/n_structure_angle_bracket_..json +++ /dev/null @@ -1 +0,0 @@ -<.> \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_angle_bracket_null.json b/vendor/orjson/data/parsing/n_structure_angle_bracket_null.json deleted file mode 100644 index 617f262549..0000000000 --- a/vendor/orjson/data/parsing/n_structure_angle_bracket_null.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_array_trailing_garbage.json b/vendor/orjson/data/parsing/n_structure_array_trailing_garbage.json deleted file mode 100644 index 5a745e6f3c..0000000000 --- a/vendor/orjson/data/parsing/n_structure_array_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -[1]x \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_array_with_extra_array_close.json b/vendor/orjson/data/parsing/n_structure_array_with_extra_array_close.json deleted file mode 100644 index 6cfb1398d2..0000000000 --- a/vendor/orjson/data/parsing/n_structure_array_with_extra_array_close.json +++ /dev/null @@ -1 +0,0 @@ -[1]] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_array_with_unclosed_string.json b/vendor/orjson/data/parsing/n_structure_array_with_unclosed_string.json deleted file mode 100644 index ba6b1788b6..0000000000 --- a/vendor/orjson/data/parsing/n_structure_array_with_unclosed_string.json +++ /dev/null @@ -1 +0,0 @@ -["asd] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_ascii-unicode-identifier.json b/vendor/orjson/data/parsing/n_structure_ascii-unicode-identifier.json deleted file mode 100644 index ef2ab62fe7..0000000000 --- a/vendor/orjson/data/parsing/n_structure_ascii-unicode-identifier.json +++ /dev/null @@ -1 +0,0 @@ -aå \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_capitalized_True.json b/vendor/orjson/data/parsing/n_structure_capitalized_True.json deleted file mode 100644 index 7cd88469ab..0000000000 --- a/vendor/orjson/data/parsing/n_structure_capitalized_True.json +++ /dev/null @@ -1 +0,0 @@ -[True] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_close_unopened_array.json b/vendor/orjson/data/parsing/n_structure_close_unopened_array.json deleted file mode 100644 index d2af0c646a..0000000000 --- a/vendor/orjson/data/parsing/n_structure_close_unopened_array.json +++ /dev/null @@ -1 +0,0 @@ -1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_comma_instead_of_closing_brace.json b/vendor/orjson/data/parsing/n_structure_comma_instead_of_closing_brace.json deleted file mode 100644 index ac61b82006..0000000000 --- a/vendor/orjson/data/parsing/n_structure_comma_instead_of_closing_brace.json +++ /dev/null @@ -1 +0,0 @@ -{"x": true, \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_double_array.json b/vendor/orjson/data/parsing/n_structure_double_array.json deleted file mode 100644 index 058d1626e5..0000000000 --- a/vendor/orjson/data/parsing/n_structure_double_array.json +++ /dev/null @@ -1 +0,0 @@ -[][] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_end_array.json b/vendor/orjson/data/parsing/n_structure_end_array.json deleted file mode 100644 index 54caf60b13..0000000000 --- a/vendor/orjson/data/parsing/n_structure_end_array.json +++ /dev/null @@ -1 +0,0 @@ -] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_incomplete_UTF8_BOM.json b/vendor/orjson/data/parsing/n_structure_incomplete_UTF8_BOM.json deleted file mode 100644 index bfcdd514f3..0000000000 --- a/vendor/orjson/data/parsing/n_structure_incomplete_UTF8_BOM.json +++ /dev/null @@ -1 +0,0 @@ -ï»{} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_lone-invalid-utf-8.json b/vendor/orjson/data/parsing/n_structure_lone-invalid-utf-8.json deleted file mode 100644 index 8b1296cad2..0000000000 --- a/vendor/orjson/data/parsing/n_structure_lone-invalid-utf-8.json +++ /dev/null @@ -1 +0,0 @@ -å \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_lone-open-bracket.json b/vendor/orjson/data/parsing/n_structure_lone-open-bracket.json deleted file mode 100644 index 8e2f0bef13..0000000000 --- a/vendor/orjson/data/parsing/n_structure_lone-open-bracket.json +++ /dev/null @@ -1 +0,0 @@ -[ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_no_data.json b/vendor/orjson/data/parsing/n_structure_no_data.json deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/orjson/data/parsing/n_structure_null-byte-outside-string.json b/vendor/orjson/data/parsing/n_structure_null-byte-outside-string.json deleted file mode 100644 index 326db14422..0000000000 Binary files a/vendor/orjson/data/parsing/n_structure_null-byte-outside-string.json and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_structure_number_with_trailing_garbage.json b/vendor/orjson/data/parsing/n_structure_number_with_trailing_garbage.json deleted file mode 100644 index 0746539d24..0000000000 --- a/vendor/orjson/data/parsing/n_structure_number_with_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -2@ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_object_followed_by_closing_object.json b/vendor/orjson/data/parsing/n_structure_object_followed_by_closing_object.json deleted file mode 100644 index aa9ebaec57..0000000000 --- a/vendor/orjson/data/parsing/n_structure_object_followed_by_closing_object.json +++ /dev/null @@ -1 +0,0 @@ -{}} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_object_unclosed_no_value.json b/vendor/orjson/data/parsing/n_structure_object_unclosed_no_value.json deleted file mode 100644 index 17d045147f..0000000000 --- a/vendor/orjson/data/parsing/n_structure_object_unclosed_no_value.json +++ /dev/null @@ -1 +0,0 @@ -{"": \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_object_with_comment.json b/vendor/orjson/data/parsing/n_structure_object_with_comment.json deleted file mode 100644 index ed1b569b70..0000000000 --- a/vendor/orjson/data/parsing/n_structure_object_with_comment.json +++ /dev/null @@ -1 +0,0 @@ -{"a":/*comment*/"b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_object_with_trailing_garbage.json b/vendor/orjson/data/parsing/n_structure_object_with_trailing_garbage.json deleted file mode 100644 index 9ca2336d74..0000000000 --- a/vendor/orjson/data/parsing/n_structure_object_with_trailing_garbage.json +++ /dev/null @@ -1 +0,0 @@ -{"a": true} "x" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_array_apostrophe.json b/vendor/orjson/data/parsing/n_structure_open_array_apostrophe.json deleted file mode 100644 index 8bebe3af09..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_array_apostrophe.json +++ /dev/null @@ -1 +0,0 @@ -[' \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_array_comma.json b/vendor/orjson/data/parsing/n_structure_open_array_comma.json deleted file mode 100644 index 6295fdc36d..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_array_comma.json +++ /dev/null @@ -1 +0,0 @@ -[, \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_array_object.json.xz b/vendor/orjson/data/parsing/n_structure_open_array_object.json.xz deleted file mode 100644 index 0d07f57007..0000000000 Binary files a/vendor/orjson/data/parsing/n_structure_open_array_object.json.xz and /dev/null differ diff --git a/vendor/orjson/data/parsing/n_structure_open_array_open_object.json b/vendor/orjson/data/parsing/n_structure_open_array_open_object.json deleted file mode 100644 index 7a63c8c57c..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_array_open_object.json +++ /dev/null @@ -1 +0,0 @@ -[{ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_array_open_string.json b/vendor/orjson/data/parsing/n_structure_open_array_open_string.json deleted file mode 100644 index 9822a6baf7..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_array_open_string.json +++ /dev/null @@ -1 +0,0 @@ -["a \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_array_string.json b/vendor/orjson/data/parsing/n_structure_open_array_string.json deleted file mode 100644 index 42a6193620..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_array_string.json +++ /dev/null @@ -1 +0,0 @@ -["a" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object.json b/vendor/orjson/data/parsing/n_structure_open_object.json deleted file mode 100644 index 81750b96f9..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object.json +++ /dev/null @@ -1 +0,0 @@ -{ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object_close_array.json b/vendor/orjson/data/parsing/n_structure_open_object_close_array.json deleted file mode 100644 index eebc700a10..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object_close_array.json +++ /dev/null @@ -1 +0,0 @@ -{] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object_comma.json b/vendor/orjson/data/parsing/n_structure_open_object_comma.json deleted file mode 100644 index 47bc9106f8..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object_comma.json +++ /dev/null @@ -1 +0,0 @@ -{, \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object_open_array.json b/vendor/orjson/data/parsing/n_structure_open_object_open_array.json deleted file mode 100644 index 381ede5dea..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object_open_array.json +++ /dev/null @@ -1 +0,0 @@ -{[ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object_open_string.json b/vendor/orjson/data/parsing/n_structure_open_object_open_string.json deleted file mode 100644 index 328c30cd78..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object_open_string.json +++ /dev/null @@ -1 +0,0 @@ -{"a \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_object_string_with_apostrophes.json b/vendor/orjson/data/parsing/n_structure_open_object_string_with_apostrophes.json deleted file mode 100644 index 9dba17090c..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_object_string_with_apostrophes.json +++ /dev/null @@ -1 +0,0 @@ -{'a' \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_open_open.json b/vendor/orjson/data/parsing/n_structure_open_open.json deleted file mode 100644 index 841fd5f864..0000000000 --- a/vendor/orjson/data/parsing/n_structure_open_open.json +++ /dev/null @@ -1 +0,0 @@ -["\{["\{["\{["\{ \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_single_eacute.json b/vendor/orjson/data/parsing/n_structure_single_eacute.json deleted file mode 100644 index 92a39f398b..0000000000 --- a/vendor/orjson/data/parsing/n_structure_single_eacute.json +++ /dev/null @@ -1 +0,0 @@ -é \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_single_star.json b/vendor/orjson/data/parsing/n_structure_single_star.json deleted file mode 100644 index f59ec20aab..0000000000 --- a/vendor/orjson/data/parsing/n_structure_single_star.json +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_trailing_#.json b/vendor/orjson/data/parsing/n_structure_trailing_#.json deleted file mode 100644 index 8986110875..0000000000 --- a/vendor/orjson/data/parsing/n_structure_trailing_#.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b"}#{} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_uescaped_LF_before_string.json b/vendor/orjson/data/parsing/n_structure_uescaped_LF_before_string.json deleted file mode 100644 index df2f0f2422..0000000000 --- a/vendor/orjson/data/parsing/n_structure_uescaped_LF_before_string.json +++ /dev/null @@ -1 +0,0 @@ -[\u000A""] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unclosed_array.json b/vendor/orjson/data/parsing/n_structure_unclosed_array.json deleted file mode 100644 index 11209515c8..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unclosed_array.json +++ /dev/null @@ -1 +0,0 @@ -[1 \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unclosed_array_partial_null.json b/vendor/orjson/data/parsing/n_structure_unclosed_array_partial_null.json deleted file mode 100644 index 0d591762c1..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unclosed_array_partial_null.json +++ /dev/null @@ -1 +0,0 @@ -[ false, nul \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_false.json b/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_false.json deleted file mode 100644 index a2ff8504a9..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_false.json +++ /dev/null @@ -1 +0,0 @@ -[ true, fals \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_true.json b/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_true.json deleted file mode 100644 index 3149e8f5a7..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unclosed_array_unfinished_true.json +++ /dev/null @@ -1 +0,0 @@ -[ false, tru \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unclosed_object.json b/vendor/orjson/data/parsing/n_structure_unclosed_object.json deleted file mode 100644 index 694d69dbd0..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unclosed_object.json +++ /dev/null @@ -1 +0,0 @@ -{"asd":"asd" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_unicode-identifier.json b/vendor/orjson/data/parsing/n_structure_unicode-identifier.json deleted file mode 100644 index 7284aea33d..0000000000 --- a/vendor/orjson/data/parsing/n_structure_unicode-identifier.json +++ /dev/null @@ -1 +0,0 @@ -å \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_whitespace_U+2060_word_joiner.json b/vendor/orjson/data/parsing/n_structure_whitespace_U+2060_word_joiner.json deleted file mode 100644 index 81156a6996..0000000000 --- a/vendor/orjson/data/parsing/n_structure_whitespace_U+2060_word_joiner.json +++ /dev/null @@ -1 +0,0 @@ -[⁠] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/n_structure_whitespace_formfeed.json b/vendor/orjson/data/parsing/n_structure_whitespace_formfeed.json deleted file mode 100644 index a9ea535d1b..0000000000 --- a/vendor/orjson/data/parsing/n_structure_whitespace_formfeed.json +++ /dev/null @@ -1 +0,0 @@ -[ ] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_arraysWithSpaces.json b/vendor/orjson/data/parsing/y_array_arraysWithSpaces.json deleted file mode 100644 index 582290798f..0000000000 --- a/vendor/orjson/data/parsing/y_array_arraysWithSpaces.json +++ /dev/null @@ -1 +0,0 @@ -[[] ] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_empty-string.json b/vendor/orjson/data/parsing/y_array_empty-string.json deleted file mode 100644 index 93b6be2bcc..0000000000 --- a/vendor/orjson/data/parsing/y_array_empty-string.json +++ /dev/null @@ -1 +0,0 @@ -[""] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_empty.json b/vendor/orjson/data/parsing/y_array_empty.json deleted file mode 100644 index 0637a088a0..0000000000 --- a/vendor/orjson/data/parsing/y_array_empty.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_ending_with_newline.json b/vendor/orjson/data/parsing/y_array_ending_with_newline.json deleted file mode 100644 index eac5f7b46e..0000000000 --- a/vendor/orjson/data/parsing/y_array_ending_with_newline.json +++ /dev/null @@ -1 +0,0 @@ -["a"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_false.json b/vendor/orjson/data/parsing/y_array_false.json deleted file mode 100644 index 67b2f07601..0000000000 --- a/vendor/orjson/data/parsing/y_array_false.json +++ /dev/null @@ -1 +0,0 @@ -[false] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_heterogeneous.json b/vendor/orjson/data/parsing/y_array_heterogeneous.json deleted file mode 100644 index d3c1e26484..0000000000 --- a/vendor/orjson/data/parsing/y_array_heterogeneous.json +++ /dev/null @@ -1 +0,0 @@ -[null, 1, "1", {}] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_null.json b/vendor/orjson/data/parsing/y_array_null.json deleted file mode 100644 index 500db4a86a..0000000000 --- a/vendor/orjson/data/parsing/y_array_null.json +++ /dev/null @@ -1 +0,0 @@ -[null] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_with_1_and_newline.json b/vendor/orjson/data/parsing/y_array_with_1_and_newline.json deleted file mode 100644 index 994825500a..0000000000 --- a/vendor/orjson/data/parsing/y_array_with_1_and_newline.json +++ /dev/null @@ -1,2 +0,0 @@ -[1 -] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_with_leading_space.json b/vendor/orjson/data/parsing/y_array_with_leading_space.json deleted file mode 100644 index 18bfe6422c..0000000000 --- a/vendor/orjson/data/parsing/y_array_with_leading_space.json +++ /dev/null @@ -1 +0,0 @@ - [1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_with_several_null.json b/vendor/orjson/data/parsing/y_array_with_several_null.json deleted file mode 100644 index 99f6c5d1d8..0000000000 --- a/vendor/orjson/data/parsing/y_array_with_several_null.json +++ /dev/null @@ -1 +0,0 @@ -[1,null,null,null,2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_array_with_trailing_space.json b/vendor/orjson/data/parsing/y_array_with_trailing_space.json deleted file mode 100644 index de9e7a9449..0000000000 --- a/vendor/orjson/data/parsing/y_array_with_trailing_space.json +++ /dev/null @@ -1 +0,0 @@ -[2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number.json b/vendor/orjson/data/parsing/y_number.json deleted file mode 100644 index e5f5cc3340..0000000000 --- a/vendor/orjson/data/parsing/y_number.json +++ /dev/null @@ -1 +0,0 @@ -[123e65] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_0e+1.json b/vendor/orjson/data/parsing/y_number_0e+1.json deleted file mode 100644 index d1d3967065..0000000000 --- a/vendor/orjson/data/parsing/y_number_0e+1.json +++ /dev/null @@ -1 +0,0 @@ -[0e+1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_0e1.json b/vendor/orjson/data/parsing/y_number_0e1.json deleted file mode 100644 index 3283a79365..0000000000 --- a/vendor/orjson/data/parsing/y_number_0e1.json +++ /dev/null @@ -1 +0,0 @@ -[0e1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_after_space.json b/vendor/orjson/data/parsing/y_number_after_space.json deleted file mode 100644 index 623570d960..0000000000 --- a/vendor/orjson/data/parsing/y_number_after_space.json +++ /dev/null @@ -1 +0,0 @@ -[ 4] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_double_close_to_zero.json b/vendor/orjson/data/parsing/y_number_double_close_to_zero.json deleted file mode 100644 index 96555ff782..0000000000 --- a/vendor/orjson/data/parsing/y_number_double_close_to_zero.json +++ /dev/null @@ -1 +0,0 @@ -[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] diff --git a/vendor/orjson/data/parsing/y_number_int_with_exp.json b/vendor/orjson/data/parsing/y_number_int_with_exp.json deleted file mode 100644 index a4ca9e754f..0000000000 --- a/vendor/orjson/data/parsing/y_number_int_with_exp.json +++ /dev/null @@ -1 +0,0 @@ -[20e1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_minus_zero.json b/vendor/orjson/data/parsing/y_number_minus_zero.json deleted file mode 100644 index 37af1312ab..0000000000 --- a/vendor/orjson/data/parsing/y_number_minus_zero.json +++ /dev/null @@ -1 +0,0 @@ -[-0] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_negative_int.json b/vendor/orjson/data/parsing/y_number_negative_int.json deleted file mode 100644 index 8e30f8bd96..0000000000 --- a/vendor/orjson/data/parsing/y_number_negative_int.json +++ /dev/null @@ -1 +0,0 @@ -[-123] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_negative_one.json b/vendor/orjson/data/parsing/y_number_negative_one.json deleted file mode 100644 index 99d21a2a0f..0000000000 --- a/vendor/orjson/data/parsing/y_number_negative_one.json +++ /dev/null @@ -1 +0,0 @@ -[-1] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_negative_zero.json b/vendor/orjson/data/parsing/y_number_negative_zero.json deleted file mode 100644 index 37af1312ab..0000000000 --- a/vendor/orjson/data/parsing/y_number_negative_zero.json +++ /dev/null @@ -1 +0,0 @@ -[-0] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_capital_e.json b/vendor/orjson/data/parsing/y_number_real_capital_e.json deleted file mode 100644 index 6edbdfcb18..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_capital_e.json +++ /dev/null @@ -1 +0,0 @@ -[1E22] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_capital_e_neg_exp.json b/vendor/orjson/data/parsing/y_number_real_capital_e_neg_exp.json deleted file mode 100644 index 0a01bd3ef4..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_capital_e_neg_exp.json +++ /dev/null @@ -1 +0,0 @@ -[1E-2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_capital_e_pos_exp.json b/vendor/orjson/data/parsing/y_number_real_capital_e_pos_exp.json deleted file mode 100644 index 5a8fc09724..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_capital_e_pos_exp.json +++ /dev/null @@ -1 +0,0 @@ -[1E+2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_exponent.json b/vendor/orjson/data/parsing/y_number_real_exponent.json deleted file mode 100644 index da2522d61a..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_exponent.json +++ /dev/null @@ -1 +0,0 @@ -[123e45] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_fraction_exponent.json b/vendor/orjson/data/parsing/y_number_real_fraction_exponent.json deleted file mode 100644 index 3944a7a454..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_fraction_exponent.json +++ /dev/null @@ -1 +0,0 @@ -[123.456e78] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_neg_exp.json b/vendor/orjson/data/parsing/y_number_real_neg_exp.json deleted file mode 100644 index ca40d3c255..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_neg_exp.json +++ /dev/null @@ -1 +0,0 @@ -[1e-2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_real_pos_exponent.json b/vendor/orjson/data/parsing/y_number_real_pos_exponent.json deleted file mode 100644 index 343601d51f..0000000000 --- a/vendor/orjson/data/parsing/y_number_real_pos_exponent.json +++ /dev/null @@ -1 +0,0 @@ -[1e+2] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_simple_int.json b/vendor/orjson/data/parsing/y_number_simple_int.json deleted file mode 100644 index e47f69afcf..0000000000 --- a/vendor/orjson/data/parsing/y_number_simple_int.json +++ /dev/null @@ -1 +0,0 @@ -[123] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_number_simple_real.json b/vendor/orjson/data/parsing/y_number_simple_real.json deleted file mode 100644 index b02878e5fc..0000000000 --- a/vendor/orjson/data/parsing/y_number_simple_real.json +++ /dev/null @@ -1 +0,0 @@ -[123.456789] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object.json b/vendor/orjson/data/parsing/y_object.json deleted file mode 100644 index 78262eda3f..0000000000 --- a/vendor/orjson/data/parsing/y_object.json +++ /dev/null @@ -1 +0,0 @@ -{"asd":"sdf", "dfg":"fgh"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_basic.json b/vendor/orjson/data/parsing/y_object_basic.json deleted file mode 100644 index 646bbe7bb1..0000000000 --- a/vendor/orjson/data/parsing/y_object_basic.json +++ /dev/null @@ -1 +0,0 @@ -{"asd":"sdf"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_duplicated_key.json b/vendor/orjson/data/parsing/y_object_duplicated_key.json deleted file mode 100644 index bbc2e1ce43..0000000000 --- a/vendor/orjson/data/parsing/y_object_duplicated_key.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b","a":"c"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_duplicated_key_and_value.json b/vendor/orjson/data/parsing/y_object_duplicated_key_and_value.json deleted file mode 100644 index 211581c207..0000000000 --- a/vendor/orjson/data/parsing/y_object_duplicated_key_and_value.json +++ /dev/null @@ -1 +0,0 @@ -{"a":"b","a":"b"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_empty.json b/vendor/orjson/data/parsing/y_object_empty.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/vendor/orjson/data/parsing/y_object_empty.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_empty_key.json b/vendor/orjson/data/parsing/y_object_empty_key.json deleted file mode 100644 index c0013d3b8b..0000000000 --- a/vendor/orjson/data/parsing/y_object_empty_key.json +++ /dev/null @@ -1 +0,0 @@ -{"":0} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_escaped_null_in_key.json b/vendor/orjson/data/parsing/y_object_escaped_null_in_key.json deleted file mode 100644 index 593f0f67f9..0000000000 --- a/vendor/orjson/data/parsing/y_object_escaped_null_in_key.json +++ /dev/null @@ -1 +0,0 @@ -{"foo\u0000bar": 42} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_extreme_numbers.json b/vendor/orjson/data/parsing/y_object_extreme_numbers.json deleted file mode 100644 index a0d3531c32..0000000000 --- a/vendor/orjson/data/parsing/y_object_extreme_numbers.json +++ /dev/null @@ -1 +0,0 @@ -{ "min": -1.0e+28, "max": 1.0e+28 } \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_long_strings.json b/vendor/orjson/data/parsing/y_object_long_strings.json deleted file mode 100644 index bdc4a08719..0000000000 --- a/vendor/orjson/data/parsing/y_object_long_strings.json +++ /dev/null @@ -1 +0,0 @@ -{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_simple.json b/vendor/orjson/data/parsing/y_object_simple.json deleted file mode 100644 index dacac917fb..0000000000 --- a/vendor/orjson/data/parsing/y_object_simple.json +++ /dev/null @@ -1 +0,0 @@ -{"a":[]} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_string_unicode.json b/vendor/orjson/data/parsing/y_object_string_unicode.json deleted file mode 100644 index 8effdb297c..0000000000 --- a/vendor/orjson/data/parsing/y_object_string_unicode.json +++ /dev/null @@ -1 +0,0 @@ -{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_object_with_newlines.json b/vendor/orjson/data/parsing/y_object_with_newlines.json deleted file mode 100644 index 246ec6b34d..0000000000 --- a/vendor/orjson/data/parsing/y_object_with_newlines.json +++ /dev/null @@ -1,3 +0,0 @@ -{ -"a": "b" -} \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_1_2_3_bytes_UTF-8_sequences.json b/vendor/orjson/data/parsing/y_string_1_2_3_bytes_UTF-8_sequences.json deleted file mode 100644 index 9967ddeb8b..0000000000 --- a/vendor/orjson/data/parsing/y_string_1_2_3_bytes_UTF-8_sequences.json +++ /dev/null @@ -1 +0,0 @@ -["\u0060\u012a\u12AB"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_accepted_surrogate_pair.json b/vendor/orjson/data/parsing/y_string_accepted_surrogate_pair.json deleted file mode 100644 index 996875cc8c..0000000000 --- a/vendor/orjson/data/parsing/y_string_accepted_surrogate_pair.json +++ /dev/null @@ -1 +0,0 @@ -["\uD801\udc37"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_accepted_surrogate_pairs.json b/vendor/orjson/data/parsing/y_string_accepted_surrogate_pairs.json deleted file mode 100644 index 3401021ece..0000000000 --- a/vendor/orjson/data/parsing/y_string_accepted_surrogate_pairs.json +++ /dev/null @@ -1 +0,0 @@ -["\ud83d\ude39\ud83d\udc8d"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_allowed_escapes.json b/vendor/orjson/data/parsing/y_string_allowed_escapes.json deleted file mode 100644 index 7f495532fb..0000000000 --- a/vendor/orjson/data/parsing/y_string_allowed_escapes.json +++ /dev/null @@ -1 +0,0 @@ -["\"\\\/\b\f\n\r\t"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_backslash_and_u_escaped_zero.json b/vendor/orjson/data/parsing/y_string_backslash_and_u_escaped_zero.json deleted file mode 100644 index d4439eda73..0000000000 --- a/vendor/orjson/data/parsing/y_string_backslash_and_u_escaped_zero.json +++ /dev/null @@ -1 +0,0 @@ -["\\u0000"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_backslash_doublequotes.json b/vendor/orjson/data/parsing/y_string_backslash_doublequotes.json deleted file mode 100644 index ae03243b67..0000000000 --- a/vendor/orjson/data/parsing/y_string_backslash_doublequotes.json +++ /dev/null @@ -1 +0,0 @@ -["\""] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_comments.json b/vendor/orjson/data/parsing/y_string_comments.json deleted file mode 100644 index 2260c20c2f..0000000000 --- a/vendor/orjson/data/parsing/y_string_comments.json +++ /dev/null @@ -1 +0,0 @@ -["a/*b*/c/*d//e"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_double_escape_a.json b/vendor/orjson/data/parsing/y_string_double_escape_a.json deleted file mode 100644 index 6715d6f404..0000000000 --- a/vendor/orjson/data/parsing/y_string_double_escape_a.json +++ /dev/null @@ -1 +0,0 @@ -["\\a"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_double_escape_n.json b/vendor/orjson/data/parsing/y_string_double_escape_n.json deleted file mode 100644 index 44ca56c4d9..0000000000 --- a/vendor/orjson/data/parsing/y_string_double_escape_n.json +++ /dev/null @@ -1 +0,0 @@ -["\\n"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_escaped_control_character.json b/vendor/orjson/data/parsing/y_string_escaped_control_character.json deleted file mode 100644 index 5b014a9c25..0000000000 --- a/vendor/orjson/data/parsing/y_string_escaped_control_character.json +++ /dev/null @@ -1 +0,0 @@ -["\u0012"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_escaped_noncharacter.json b/vendor/orjson/data/parsing/y_string_escaped_noncharacter.json deleted file mode 100644 index 2ff52e2c50..0000000000 --- a/vendor/orjson/data/parsing/y_string_escaped_noncharacter.json +++ /dev/null @@ -1 +0,0 @@ -["\uFFFF"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_in_array.json b/vendor/orjson/data/parsing/y_string_in_array.json deleted file mode 100644 index 21d7ae4cd8..0000000000 --- a/vendor/orjson/data/parsing/y_string_in_array.json +++ /dev/null @@ -1 +0,0 @@ -["asd"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_in_array_with_leading_space.json b/vendor/orjson/data/parsing/y_string_in_array_with_leading_space.json deleted file mode 100644 index 9e1887c1e4..0000000000 --- a/vendor/orjson/data/parsing/y_string_in_array_with_leading_space.json +++ /dev/null @@ -1 +0,0 @@ -[ "asd"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_last_surrogates_1_and_2.json b/vendor/orjson/data/parsing/y_string_last_surrogates_1_and_2.json deleted file mode 100644 index 3919cef765..0000000000 --- a/vendor/orjson/data/parsing/y_string_last_surrogates_1_and_2.json +++ /dev/null @@ -1 +0,0 @@ -["\uDBFF\uDFFF"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_nbsp_uescaped.json b/vendor/orjson/data/parsing/y_string_nbsp_uescaped.json deleted file mode 100644 index 2085ab1a1c..0000000000 --- a/vendor/orjson/data/parsing/y_string_nbsp_uescaped.json +++ /dev/null @@ -1 +0,0 @@ -["new\u00A0line"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json b/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json deleted file mode 100644 index 059e4d9dd0..0000000000 --- a/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json +++ /dev/null @@ -1 +0,0 @@ -["􏿿"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+FFFF.json b/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+FFFF.json deleted file mode 100644 index 4c913bd41a..0000000000 --- a/vendor/orjson/data/parsing/y_string_nonCharacterInUTF-8_U+FFFF.json +++ /dev/null @@ -1 +0,0 @@ -["￿"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_null_escape.json b/vendor/orjson/data/parsing/y_string_null_escape.json deleted file mode 100644 index c1ad844043..0000000000 --- a/vendor/orjson/data/parsing/y_string_null_escape.json +++ /dev/null @@ -1 +0,0 @@ -["\u0000"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_one-byte-utf-8.json b/vendor/orjson/data/parsing/y_string_one-byte-utf-8.json deleted file mode 100644 index 157185923a..0000000000 --- a/vendor/orjson/data/parsing/y_string_one-byte-utf-8.json +++ /dev/null @@ -1 +0,0 @@ -["\u002c"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_pi.json b/vendor/orjson/data/parsing/y_string_pi.json deleted file mode 100644 index 9df11ae88b..0000000000 --- a/vendor/orjson/data/parsing/y_string_pi.json +++ /dev/null @@ -1 +0,0 @@ -["π"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_reservedCharacterInUTF-8_U+1BFFF.json b/vendor/orjson/data/parsing/y_string_reservedCharacterInUTF-8_U+1BFFF.json deleted file mode 100644 index 10a33a1717..0000000000 --- a/vendor/orjson/data/parsing/y_string_reservedCharacterInUTF-8_U+1BFFF.json +++ /dev/null @@ -1 +0,0 @@ -["𛿿"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_simple_ascii.json b/vendor/orjson/data/parsing/y_string_simple_ascii.json deleted file mode 100644 index 8cadf7d051..0000000000 --- a/vendor/orjson/data/parsing/y_string_simple_ascii.json +++ /dev/null @@ -1 +0,0 @@ -["asd "] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_space.json b/vendor/orjson/data/parsing/y_string_space.json deleted file mode 100644 index efd782cc32..0000000000 --- a/vendor/orjson/data/parsing/y_string_space.json +++ /dev/null @@ -1 +0,0 @@ -" " \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json b/vendor/orjson/data/parsing/y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json deleted file mode 100644 index 7620b66559..0000000000 --- a/vendor/orjson/data/parsing/y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json +++ /dev/null @@ -1 +0,0 @@ -["\uD834\uDd1e"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_three-byte-utf-8.json b/vendor/orjson/data/parsing/y_string_three-byte-utf-8.json deleted file mode 100644 index 108f1d67df..0000000000 --- a/vendor/orjson/data/parsing/y_string_three-byte-utf-8.json +++ /dev/null @@ -1 +0,0 @@ -["\u0821"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_two-byte-utf-8.json b/vendor/orjson/data/parsing/y_string_two-byte-utf-8.json deleted file mode 100644 index 461503c310..0000000000 --- a/vendor/orjson/data/parsing/y_string_two-byte-utf-8.json +++ /dev/null @@ -1 +0,0 @@ -["\u0123"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_u+2028_line_sep.json b/vendor/orjson/data/parsing/y_string_u+2028_line_sep.json deleted file mode 100644 index 897b6021af..0000000000 --- a/vendor/orjson/data/parsing/y_string_u+2028_line_sep.json +++ /dev/null @@ -1 +0,0 @@ -["
"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_u+2029_par_sep.json b/vendor/orjson/data/parsing/y_string_u+2029_par_sep.json deleted file mode 100644 index 8cd998c89e..0000000000 --- a/vendor/orjson/data/parsing/y_string_u+2029_par_sep.json +++ /dev/null @@ -1 +0,0 @@ -["
"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_uEscape.json b/vendor/orjson/data/parsing/y_string_uEscape.json deleted file mode 100644 index f7b41a02fa..0000000000 --- a/vendor/orjson/data/parsing/y_string_uEscape.json +++ /dev/null @@ -1 +0,0 @@ -["\u0061\u30af\u30EA\u30b9"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_uescaped_newline.json b/vendor/orjson/data/parsing/y_string_uescaped_newline.json deleted file mode 100644 index 3a5a220b69..0000000000 --- a/vendor/orjson/data/parsing/y_string_uescaped_newline.json +++ /dev/null @@ -1 +0,0 @@ -["new\u000Aline"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unescaped_char_delete.json b/vendor/orjson/data/parsing/y_string_unescaped_char_delete.json deleted file mode 100644 index 7d064f4987..0000000000 --- a/vendor/orjson/data/parsing/y_string_unescaped_char_delete.json +++ /dev/null @@ -1 +0,0 @@ -[""] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode.json b/vendor/orjson/data/parsing/y_string_unicode.json deleted file mode 100644 index 3598095b79..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode.json +++ /dev/null @@ -1 +0,0 @@ -["\uA66D"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicodeEscapedBackslash.json b/vendor/orjson/data/parsing/y_string_unicodeEscapedBackslash.json deleted file mode 100644 index 0bb3b51e7e..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicodeEscapedBackslash.json +++ /dev/null @@ -1 +0,0 @@ -["\u005C"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_2.json b/vendor/orjson/data/parsing/y_string_unicode_2.json deleted file mode 100644 index a7dcb97683..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_2.json +++ /dev/null @@ -1 +0,0 @@ -["⍂㈴⍂"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+10FFFE_nonchar.json b/vendor/orjson/data/parsing/y_string_unicode_U+10FFFE_nonchar.json deleted file mode 100644 index 9a8370b96a..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+10FFFE_nonchar.json +++ /dev/null @@ -1 +0,0 @@ -["\uDBFF\uDFFE"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+1FFFE_nonchar.json b/vendor/orjson/data/parsing/y_string_unicode_U+1FFFE_nonchar.json deleted file mode 100644 index c51f8ae45c..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+1FFFE_nonchar.json +++ /dev/null @@ -1 +0,0 @@ -["\uD83F\uDFFE"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json b/vendor/orjson/data/parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json deleted file mode 100644 index 626d5f8157..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json +++ /dev/null @@ -1 +0,0 @@ -["\u200B"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+2064_invisible_plus.json b/vendor/orjson/data/parsing/y_string_unicode_U+2064_invisible_plus.json deleted file mode 100644 index 1e23972c65..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+2064_invisible_plus.json +++ /dev/null @@ -1 +0,0 @@ -["\u2064"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+FDD0_nonchar.json b/vendor/orjson/data/parsing/y_string_unicode_U+FDD0_nonchar.json deleted file mode 100644 index 18ef151b4f..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+FDD0_nonchar.json +++ /dev/null @@ -1 +0,0 @@ -["\uFDD0"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_U+FFFE_nonchar.json b/vendor/orjson/data/parsing/y_string_unicode_U+FFFE_nonchar.json deleted file mode 100644 index 13d261fdad..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_U+FFFE_nonchar.json +++ /dev/null @@ -1 +0,0 @@ -["\uFFFE"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_unicode_escaped_double_quote.json b/vendor/orjson/data/parsing/y_string_unicode_escaped_double_quote.json deleted file mode 100644 index 4e6257856d..0000000000 --- a/vendor/orjson/data/parsing/y_string_unicode_escaped_double_quote.json +++ /dev/null @@ -1 +0,0 @@ -["\u0022"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_utf8.json b/vendor/orjson/data/parsing/y_string_utf8.json deleted file mode 100644 index 40878435f9..0000000000 --- a/vendor/orjson/data/parsing/y_string_utf8.json +++ /dev/null @@ -1 +0,0 @@ -["€𝄞"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_string_with_del_character.json b/vendor/orjson/data/parsing/y_string_with_del_character.json deleted file mode 100644 index 8bd24907d9..0000000000 --- a/vendor/orjson/data/parsing/y_string_with_del_character.json +++ /dev/null @@ -1 +0,0 @@ -["aa"] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_false.json b/vendor/orjson/data/parsing/y_structure_lonely_false.json deleted file mode 100644 index 02e4a84d62..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_false.json +++ /dev/null @@ -1 +0,0 @@ -false \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_int.json b/vendor/orjson/data/parsing/y_structure_lonely_int.json deleted file mode 100644 index f70d7bba4a..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_int.json +++ /dev/null @@ -1 +0,0 @@ -42 \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_negative_real.json b/vendor/orjson/data/parsing/y_structure_lonely_negative_real.json deleted file mode 100644 index b5135a207d..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_negative_real.json +++ /dev/null @@ -1 +0,0 @@ --0.1 \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_null.json b/vendor/orjson/data/parsing/y_structure_lonely_null.json deleted file mode 100644 index ec747fa47d..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_null.json +++ /dev/null @@ -1 +0,0 @@ -null \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_string.json b/vendor/orjson/data/parsing/y_structure_lonely_string.json deleted file mode 100644 index b6e982ca96..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_string.json +++ /dev/null @@ -1 +0,0 @@ -"asd" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_lonely_true.json b/vendor/orjson/data/parsing/y_structure_lonely_true.json deleted file mode 100644 index f32a5804e2..0000000000 --- a/vendor/orjson/data/parsing/y_structure_lonely_true.json +++ /dev/null @@ -1 +0,0 @@ -true \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_string_empty.json b/vendor/orjson/data/parsing/y_structure_string_empty.json deleted file mode 100644 index 3cc762b550..0000000000 --- a/vendor/orjson/data/parsing/y_structure_string_empty.json +++ /dev/null @@ -1 +0,0 @@ -"" \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_trailing_newline.json b/vendor/orjson/data/parsing/y_structure_trailing_newline.json deleted file mode 100644 index 0c3426d4c2..0000000000 --- a/vendor/orjson/data/parsing/y_structure_trailing_newline.json +++ /dev/null @@ -1 +0,0 @@ -["a"] diff --git a/vendor/orjson/data/parsing/y_structure_true_in_array.json b/vendor/orjson/data/parsing/y_structure_true_in_array.json deleted file mode 100644 index de601e305f..0000000000 --- a/vendor/orjson/data/parsing/y_structure_true_in_array.json +++ /dev/null @@ -1 +0,0 @@ -[true] \ No newline at end of file diff --git a/vendor/orjson/data/parsing/y_structure_whitespace_array.json b/vendor/orjson/data/parsing/y_structure_whitespace_array.json deleted file mode 100644 index 2bedf7f2de..0000000000 --- a/vendor/orjson/data/parsing/y_structure_whitespace_array.json +++ /dev/null @@ -1 +0,0 @@ - [] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip01.json b/vendor/orjson/data/roundtrip/roundtrip01.json deleted file mode 100644 index 500db4a86a..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip01.json +++ /dev/null @@ -1 +0,0 @@ -[null] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip02.json b/vendor/orjson/data/roundtrip/roundtrip02.json deleted file mode 100644 index de601e305f..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip02.json +++ /dev/null @@ -1 +0,0 @@ -[true] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip03.json b/vendor/orjson/data/roundtrip/roundtrip03.json deleted file mode 100644 index 67b2f07601..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip03.json +++ /dev/null @@ -1 +0,0 @@ -[false] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip04.json b/vendor/orjson/data/roundtrip/roundtrip04.json deleted file mode 100644 index 6e7ea636ee..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip04.json +++ /dev/null @@ -1 +0,0 @@ -[0] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip05.json b/vendor/orjson/data/roundtrip/roundtrip05.json deleted file mode 100644 index 6dfd29830e..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip05.json +++ /dev/null @@ -1 +0,0 @@ -["foo"] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip06.json b/vendor/orjson/data/roundtrip/roundtrip06.json deleted file mode 100644 index 0637a088a0..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip06.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip07.json b/vendor/orjson/data/roundtrip/roundtrip07.json deleted file mode 100644 index 9e26dfeeb6..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip07.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip08.json b/vendor/orjson/data/roundtrip/roundtrip08.json deleted file mode 100644 index bfa34124ca..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip08.json +++ /dev/null @@ -1 +0,0 @@ -[0,1] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip09.json b/vendor/orjson/data/roundtrip/roundtrip09.json deleted file mode 100644 index 9f5dd4e3d9..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip09.json +++ /dev/null @@ -1 +0,0 @@ -{"foo":"bar"} \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip10.json b/vendor/orjson/data/roundtrip/roundtrip10.json deleted file mode 100644 index 2355b4df27..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip10.json +++ /dev/null @@ -1 +0,0 @@ -{"a":null,"foo":"bar"} \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip11.json b/vendor/orjson/data/roundtrip/roundtrip11.json deleted file mode 100644 index 99d21a2a0f..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip11.json +++ /dev/null @@ -1 +0,0 @@ -[-1] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip12.json b/vendor/orjson/data/roundtrip/roundtrip12.json deleted file mode 100644 index 56c78bef1d..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip12.json +++ /dev/null @@ -1 +0,0 @@ -[-2147483648] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip13.json b/vendor/orjson/data/roundtrip/roundtrip13.json deleted file mode 100644 index 029580f6fc..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip13.json +++ /dev/null @@ -1 +0,0 @@ -[-1234567890123456789] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip14.json b/vendor/orjson/data/roundtrip/roundtrip14.json deleted file mode 100644 index d865800090..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip14.json +++ /dev/null @@ -1 +0,0 @@ -[-9223372036854775808] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip15.json b/vendor/orjson/data/roundtrip/roundtrip15.json deleted file mode 100644 index bace2a0be1..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip15.json +++ /dev/null @@ -1 +0,0 @@ -[1] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip16.json b/vendor/orjson/data/roundtrip/roundtrip16.json deleted file mode 100644 index dfe696dbd6..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip16.json +++ /dev/null @@ -1 +0,0 @@ -[2147483647] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip17.json b/vendor/orjson/data/roundtrip/roundtrip17.json deleted file mode 100644 index 6640b07fb5..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip17.json +++ /dev/null @@ -1 +0,0 @@ -[4294967295] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip18.json b/vendor/orjson/data/roundtrip/roundtrip18.json deleted file mode 100644 index a3ab143b04..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip18.json +++ /dev/null @@ -1 +0,0 @@ -[1234567890123456789] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip19.json b/vendor/orjson/data/roundtrip/roundtrip19.json deleted file mode 100644 index 8ab4a5078a..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip19.json +++ /dev/null @@ -1 +0,0 @@ -[9223372036854775807] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip20.json b/vendor/orjson/data/roundtrip/roundtrip20.json deleted file mode 100644 index 92df1df1d1..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip20.json +++ /dev/null @@ -1 +0,0 @@ -[0.0] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip21.json b/vendor/orjson/data/roundtrip/roundtrip21.json deleted file mode 100644 index cfef81540e..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip21.json +++ /dev/null @@ -1 +0,0 @@ -[-0.0] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip22.json b/vendor/orjson/data/roundtrip/roundtrip22.json deleted file mode 100644 index a7b7eefc5c..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip22.json +++ /dev/null @@ -1 +0,0 @@ -[1.2345] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip23.json b/vendor/orjson/data/roundtrip/roundtrip23.json deleted file mode 100644 index b553e84b7c..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip23.json +++ /dev/null @@ -1 +0,0 @@ -[-1.2345] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip24.json b/vendor/orjson/data/roundtrip/roundtrip24.json deleted file mode 100644 index f01efb6d5e..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip24.json +++ /dev/null @@ -1 +0,0 @@ -[5e-324] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip25.json b/vendor/orjson/data/roundtrip/roundtrip25.json deleted file mode 100644 index cdef14d38b..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip25.json +++ /dev/null @@ -1 +0,0 @@ -[2.225073858507201e-308] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip26.json b/vendor/orjson/data/roundtrip/roundtrip26.json deleted file mode 100644 index f4121b787d..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip26.json +++ /dev/null @@ -1 +0,0 @@ -[2.2250738585072014e-308] \ No newline at end of file diff --git a/vendor/orjson/data/roundtrip/roundtrip27.json b/vendor/orjson/data/roundtrip/roundtrip27.json deleted file mode 100644 index 17ce5211c8..0000000000 --- a/vendor/orjson/data/roundtrip/roundtrip27.json +++ /dev/null @@ -1 +0,0 @@ -[1.7976931348623157e308] \ No newline at end of file diff --git a/vendor/orjson/data/transform/number_1.0.json b/vendor/orjson/data/transform/number_1.0.json deleted file mode 100644 index e7a19a6ece..0000000000 --- a/vendor/orjson/data/transform/number_1.0.json +++ /dev/null @@ -1 +0,0 @@ -[1.0] \ No newline at end of file diff --git a/vendor/orjson/data/transform/number_1.000000000000000005.json b/vendor/orjson/data/transform/number_1.000000000000000005.json deleted file mode 100644 index c73b7cfcab..0000000000 --- a/vendor/orjson/data/transform/number_1.000000000000000005.json +++ /dev/null @@ -1 +0,0 @@ -[1.000000000000000005] \ No newline at end of file diff --git a/vendor/orjson/data/transform/number_1000000000000000.json b/vendor/orjson/data/transform/number_1000000000000000.json deleted file mode 100644 index cd38afa776..0000000000 --- a/vendor/orjson/data/transform/number_1000000000000000.json +++ /dev/null @@ -1 +0,0 @@ -[1000000000000000] diff --git a/vendor/orjson/data/transform/number_10000000000000000999.json b/vendor/orjson/data/transform/number_10000000000000000999.json deleted file mode 100644 index 946d13d369..0000000000 --- a/vendor/orjson/data/transform/number_10000000000000000999.json +++ /dev/null @@ -1 +0,0 @@ -[10000000000000000999] \ No newline at end of file diff --git a/vendor/orjson/data/transform/number_1e-999.json b/vendor/orjson/data/transform/number_1e-999.json deleted file mode 100644 index c8ed222fc9..0000000000 --- a/vendor/orjson/data/transform/number_1e-999.json +++ /dev/null @@ -1 +0,0 @@ -[1E-999] \ No newline at end of file diff --git a/vendor/orjson/data/transform/number_1e6.json b/vendor/orjson/data/transform/number_1e6.json deleted file mode 100644 index 1a8b0f78b6..0000000000 --- a/vendor/orjson/data/transform/number_1e6.json +++ /dev/null @@ -1 +0,0 @@ -[1E6] \ No newline at end of file diff --git a/vendor/orjson/data/transform/object_key_nfc_nfd.json b/vendor/orjson/data/transform/object_key_nfc_nfd.json deleted file mode 100644 index e4cbc1dc11..0000000000 --- a/vendor/orjson/data/transform/object_key_nfc_nfd.json +++ /dev/null @@ -1 +0,0 @@ -{"é":"NFC","é":"NFD"} \ No newline at end of file diff --git a/vendor/orjson/data/transform/object_key_nfd_nfc.json b/vendor/orjson/data/transform/object_key_nfd_nfc.json deleted file mode 100644 index b04ece18df..0000000000 --- a/vendor/orjson/data/transform/object_key_nfd_nfc.json +++ /dev/null @@ -1 +0,0 @@ -{"é":"NFD","é":"NFC"} \ No newline at end of file diff --git a/vendor/orjson/data/transform/object_same_key_different_values.json b/vendor/orjson/data/transform/object_same_key_different_values.json deleted file mode 100644 index 0c4547df37..0000000000 --- a/vendor/orjson/data/transform/object_same_key_different_values.json +++ /dev/null @@ -1 +0,0 @@ -{"a":1,"a":2} \ No newline at end of file diff --git a/vendor/orjson/data/transform/object_same_key_same_value.json b/vendor/orjson/data/transform/object_same_key_same_value.json deleted file mode 100644 index e1070184de..0000000000 --- a/vendor/orjson/data/transform/object_same_key_same_value.json +++ /dev/null @@ -1 +0,0 @@ -{"a":1,"a":1} \ No newline at end of file diff --git a/vendor/orjson/data/transform/object_same_key_unclear_values.json b/vendor/orjson/data/transform/object_same_key_unclear_values.json deleted file mode 100644 index 8a76bd4ffe..0000000000 --- a/vendor/orjson/data/transform/object_same_key_unclear_values.json +++ /dev/null @@ -1 +0,0 @@ -{"a":0, "a":-0} diff --git a/vendor/orjson/data/transform/string_1_escaped_invalid_codepoint.json b/vendor/orjson/data/transform/string_1_escaped_invalid_codepoint.json deleted file mode 100644 index 8e6247319f..0000000000 --- a/vendor/orjson/data/transform/string_1_escaped_invalid_codepoint.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_1_invalid_codepoint.json b/vendor/orjson/data/transform/string_1_invalid_codepoint.json deleted file mode 100644 index 916bff920f..0000000000 --- a/vendor/orjson/data/transform/string_1_invalid_codepoint.json +++ /dev/null @@ -1 +0,0 @@ -["í €"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_2_escaped_invalid_codepoints.json b/vendor/orjson/data/transform/string_2_escaped_invalid_codepoints.json deleted file mode 100644 index 93568e2c77..0000000000 --- a/vendor/orjson/data/transform/string_2_escaped_invalid_codepoints.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\uD800"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_2_invalid_codepoints.json b/vendor/orjson/data/transform/string_2_invalid_codepoints.json deleted file mode 100644 index 043a72e813..0000000000 --- a/vendor/orjson/data/transform/string_2_invalid_codepoints.json +++ /dev/null @@ -1 +0,0 @@ -["í €í €"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_3_escaped_invalid_codepoints.json b/vendor/orjson/data/transform/string_3_escaped_invalid_codepoints.json deleted file mode 100644 index 407dc65750..0000000000 --- a/vendor/orjson/data/transform/string_3_escaped_invalid_codepoints.json +++ /dev/null @@ -1 +0,0 @@ -["\uD800\uD800\uD800"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_3_invalid_codepoints.json b/vendor/orjson/data/transform/string_3_invalid_codepoints.json deleted file mode 100644 index 2fcb0927d1..0000000000 --- a/vendor/orjson/data/transform/string_3_invalid_codepoints.json +++ /dev/null @@ -1 +0,0 @@ -["í €í €í €"] \ No newline at end of file diff --git a/vendor/orjson/data/transform/string_with_escaped_NULL.json b/vendor/orjson/data/transform/string_with_escaped_NULL.json deleted file mode 100644 index 8ca2be59e6..0000000000 --- a/vendor/orjson/data/transform/string_with_escaped_NULL.json +++ /dev/null @@ -1 +0,0 @@ -["A\u0000B"] \ No newline at end of file diff --git a/vendor/orjson/data/twitter.json.xz b/vendor/orjson/data/twitter.json.xz deleted file mode 100644 index 230cb578fc..0000000000 Binary files a/vendor/orjson/data/twitter.json.xz and /dev/null differ diff --git a/vendor/orjson/doc/deserialization.png b/vendor/orjson/doc/deserialization.png deleted file mode 100644 index d83f5d518c..0000000000 Binary files a/vendor/orjson/doc/deserialization.png and /dev/null differ diff --git a/vendor/orjson/doc/serialization.png b/vendor/orjson/doc/serialization.png deleted file mode 100644 index be98efd8be..0000000000 Binary files a/vendor/orjson/doc/serialization.png and /dev/null differ diff --git a/vendor/orjson/include/pyo3/LICENSE-APACHE b/vendor/orjson/include/pyo3/LICENSE-APACHE deleted file mode 100644 index 72207b851d..0000000000 --- a/vendor/orjson/include/pyo3/LICENSE-APACHE +++ /dev/null @@ -1,178 +0,0 @@ -Copyright (c) 2017-present PyO3 Project and Contributors. https://github.com/PyO3 - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS diff --git a/vendor/orjson/include/pyo3/LICENSE-MIT b/vendor/orjson/include/pyo3/LICENSE-MIT deleted file mode 100644 index cd0ad009a8..0000000000 --- a/vendor/orjson/include/pyo3/LICENSE-MIT +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2023-present PyO3 Project and Contributors. https://github.com/PyO3 - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/Cargo.toml b/vendor/orjson/include/pyo3/pyo3-build-config/Cargo.toml deleted file mode 100644 index a5549df3ec..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/Cargo.toml +++ /dev/null @@ -1,42 +0,0 @@ -[package] -name = "pyo3-build-config" -version = "0.23.0-dev" -description = "Build configuration for the PyO3 ecosystem" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" - -[dependencies] -once_cell = "1" -python3-dll-a = { version = "0.2.6", optional = true } -target-lexicon = "0.12.14" - -[build-dependencies] -python3-dll-a = { version = "0.2.6", optional = true } -target-lexicon = "0.12.14" - -[features] -default = [] - -# Attempt to resolve a Python interpreter config for building in the build -# script. If this feature isn't enabled, the build script no-ops. -resolve-config = [] - -# This feature is enabled by pyo3 when building an extension module. -extension-module = [] - -# These features are enabled by pyo3 when building Stable ABI extension modules. -abi3 = [] -abi3-py37 = ["abi3-py38"] -abi3-py38 = ["abi3-py39"] -abi3-py39 = ["abi3-py310"] -abi3-py310 = ["abi3-py311"] -abi3-py311 = ["abi3-py312"] -abi3-py312 = ["abi3"] - -[package.metadata.docs.rs] -features = ["resolve-config"] diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-APACHE b/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-MIT b/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/build.rs b/vendor/orjson/include/pyo3/pyo3-build-config/build.rs deleted file mode 100644 index a6e767edcf..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/build.rs +++ /dev/null @@ -1,58 +0,0 @@ -// Import some modules from this crate inline to generate the build config. -// Allow dead code because not all code in the modules is used in this build script. - -#[path = "src/impl_.rs"] -#[allow(dead_code)] -mod impl_; - -#[path = "src/errors.rs"] -#[allow(dead_code)] -mod errors; - -use std::{env, path::Path}; - -use errors::{Context, Result}; -use impl_::{make_interpreter_config, InterpreterConfig}; - -fn configure(interpreter_config: Option, name: &str) -> Result { - let target = Path::new(&env::var_os("OUT_DIR").unwrap()).join(name); - if let Some(config) = interpreter_config { - config - .to_writer(&mut std::fs::File::create(&target).with_context(|| { - format!("failed to write config file at {}", target.display()) - })?)?; - Ok(true) - } else { - std::fs::File::create(&target) - .with_context(|| format!("failed to create new file at {}", target.display()))?; - Ok(false) - } -} - -fn generate_build_configs() -> Result<()> { - // If PYO3_CONFIG_FILE is set, copy it into the crate. - let configured = configure( - InterpreterConfig::from_pyo3_config_file_env().transpose()?, - "pyo3-build-config-file.txt", - )?; - - if configured { - // Don't bother trying to find an interpreter on the host system - // if the user-provided config file is present. - configure(None, "pyo3-build-config.txt")?; - } else { - configure(Some(make_interpreter_config()?), "pyo3-build-config.txt")?; - } - Ok(()) -} - -fn main() { - if std::env::var("CARGO_FEATURE_RESOLVE_CONFIG").is_ok() { - if let Err(e) = generate_build_configs() { - eprintln!("error: {}", e.report()); - std::process::exit(1) - } - } else { - eprintln!("resolve-config feature not enabled; build script in no-op mode"); - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/src/errors.rs b/vendor/orjson/include/pyo3/pyo3-build-config/src/errors.rs deleted file mode 100644 index 87c59a998b..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/src/errors.rs +++ /dev/null @@ -1,153 +0,0 @@ -/// A simple macro for returning an error. Resembles anyhow::bail. -#[macro_export] -#[doc(hidden)] -macro_rules! bail { - ($($args: tt)+) => { return Err(format!($($args)+).into()) }; -} - -/// A simple macro for checking a condition. Resembles anyhow::ensure. -#[macro_export] -#[doc(hidden)] -macro_rules! ensure { - ($condition:expr, $($args: tt)+) => { if !($condition) { bail!($($args)+) } }; -} - -/// Show warning. -#[macro_export] -#[doc(hidden)] -macro_rules! warn { - ($($args: tt)+) => { - println!("{}", $crate::format_warn!($($args)+)) - }; -} - -/// Format warning into string. -#[macro_export] -#[doc(hidden)] -macro_rules! format_warn { - ($($args: tt)+) => { - format!("cargo:warning={}", format_args!($($args)+)) - }; -} - -/// A simple error implementation which allows chaining of errors, inspired somewhat by anyhow. -#[derive(Debug)] -pub struct Error { - value: String, - source: Option>, -} - -/// Error report inspired by -/// -pub struct ErrorReport<'a>(&'a Error); - -impl Error { - pub fn report(&self) -> ErrorReport<'_> { - ErrorReport(self) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.value) - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.source.as_deref() - } -} - -impl std::fmt::Display for ErrorReport<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use std::error::Error; - self.0.fmt(f)?; - let mut source = self.0.source(); - if source.is_some() { - writeln!(f, "\ncaused by:")?; - let mut index = 0; - while let Some(some_source) = source { - writeln!(f, " - {}: {}", index, some_source)?; - source = some_source.source(); - index += 1; - } - } - Ok(()) - } -} - -impl From for Error { - fn from(value: String) -> Self { - Self { - value, - source: None, - } - } -} - -impl From<&'_ str> for Error { - fn from(value: &str) -> Self { - value.to_string().into() - } -} - -impl From for Error { - fn from(value: std::convert::Infallible) -> Self { - match value {} - } -} - -pub type Result = std::result::Result; - -pub trait Context { - fn context(self, message: impl Into) -> Result; - fn with_context(self, message: impl FnOnce() -> String) -> Result; -} - -impl Context for Result -where - E: std::error::Error + 'static, -{ - fn context(self, message: impl Into) -> Result { - self.map_err(|error| Error { - value: message.into(), - source: Some(Box::new(error)), - }) - } - - fn with_context(self, message: impl FnOnce() -> String) -> Result { - self.map_err(|error| Error { - value: message(), - source: Some(Box::new(error)), - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn error_report() { - let error: Result<()> = Err(Error::from("there was an internal error")) - .with_context(|| format!("failed to do {}", "something difficult")) - .context("things went wrong"); - - assert_eq!( - error - .unwrap_err() - .report() - .to_string() - .split('\n') - .collect::>(), - vec![ - "things went wrong", - "caused by:", - " - 0: failed to do something difficult", - " - 1: there was an internal error", - "" - ] - ); - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/src/impl_.rs b/vendor/orjson/include/pyo3/pyo3-build-config/src/impl_.rs deleted file mode 100644 index ec65259115..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/src/impl_.rs +++ /dev/null @@ -1,2851 +0,0 @@ -//! Main implementation module included in both the `pyo3-build-config` library crate -//! and its build script. - -// Optional python3.dll import library generator for Windows -#[cfg(feature = "python3-dll-a")] -#[path = "import_lib.rs"] -mod import_lib; - -use std::{ - collections::{HashMap, HashSet}, - env, - ffi::{OsStr, OsString}, - fmt::Display, - fs::{self, DirEntry}, - io::{BufRead, BufReader, Read, Write}, - path::{Path, PathBuf}, - process::{Command, Stdio}, - str, - str::FromStr, -}; - -pub use target_lexicon::Triple; - -use target_lexicon::{Environment, OperatingSystem}; - -use crate::{ - bail, ensure, - errors::{Context, Error, Result}, - warn, -}; - -/// Minimum Python version PyO3 supports. -pub(crate) const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 }; - -/// GraalPy may implement the same CPython version over multiple releases. -const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion { - major: 24, - minor: 0, -}; - -/// Maximum Python version that can be used as minimum required Python version with abi3. -pub(crate) const ABI3_MAX_MINOR: u8 = 12; - -/// Gets an environment variable owned by cargo. -/// -/// Environment variables set by cargo are expected to be valid UTF8. -pub fn cargo_env_var(var: &str) -> Option { - env::var_os(var).map(|os_string| os_string.to_str().unwrap().into()) -} - -/// Gets an external environment variable, and registers the build script to rerun if -/// the variable changes. -pub fn env_var(var: &str) -> Option { - if cfg!(feature = "resolve-config") { - println!("cargo:rerun-if-env-changed={}", var); - } - env::var_os(var) -} - -/// Gets the compilation target triple from environment variables set by Cargo. -/// -/// Must be called from a crate build script. -pub fn target_triple_from_env() -> Triple { - env::var("TARGET") - .expect("target_triple_from_env() must be called from a build script") - .parse() - .expect("Unrecognized TARGET environment variable value") -} - -/// Configuration needed by PyO3 to build for the correct Python implementation. -/// -/// Usually this is queried directly from the Python interpreter, or overridden using the -/// `PYO3_CONFIG_FILE` environment variable. -/// -/// When the `PYO3_NO_PYTHON` variable is set, or during cross compile situations, then alternative -/// strategies are used to populate this type. -#[cfg_attr(test, derive(Debug, PartialEq, Eq))] -pub struct InterpreterConfig { - /// The Python implementation flavor. - /// - /// Serialized to `implementation`. - pub implementation: PythonImplementation, - - /// Python `X.Y` version. e.g. `3.9`. - /// - /// Serialized to `version`. - pub version: PythonVersion, - - /// Whether link library is shared. - /// - /// Serialized to `shared`. - pub shared: bool, - - /// Whether linking against the stable/limited Python 3 API. - /// - /// Serialized to `abi3`. - pub abi3: bool, - - /// The name of the link library defining Python. - /// - /// This effectively controls the `cargo:rustc-link-lib=` value to - /// control how libpython is linked. Values should not contain the `lib` - /// prefix. - /// - /// Serialized to `lib_name`. - pub lib_name: Option, - - /// The directory containing the Python library to link against. - /// - /// The effectively controls the `cargo:rustc-link-search=native=` value - /// to add an additional library search path for the linker. - /// - /// Serialized to `lib_dir`. - pub lib_dir: Option, - - /// Path of host `python` executable. - /// - /// This is a valid executable capable of running on the host/building machine. - /// For configurations derived by invoking a Python interpreter, it was the - /// executable invoked. - /// - /// Serialized to `executable`. - pub executable: Option, - - /// Width in bits of pointers on the target machine. - /// - /// Serialized to `pointer_width`. - pub pointer_width: Option, - - /// Additional relevant Python build flags / configuration settings. - /// - /// Serialized to `build_flags`. - pub build_flags: BuildFlags, - - /// Whether to suppress emitting of `cargo:rustc-link-*` lines from the build script. - /// - /// Typically, `pyo3`'s build script will emit `cargo:rustc-link-lib=` and - /// `cargo:rustc-link-search=` lines derived from other fields in this struct. In - /// advanced building configurations, the default logic to derive these lines may not - /// be sufficient. This field can be set to `Some(true)` to suppress the emission - /// of these lines. - /// - /// If suppression is enabled, `extra_build_script_lines` should contain equivalent - /// functionality or else a build failure is likely. - pub suppress_build_script_link_lines: bool, - - /// Additional lines to `println!()` from Cargo build scripts. - /// - /// This field can be populated to enable the `pyo3` crate to emit additional lines from its - /// its Cargo build script. - /// - /// This crate doesn't populate this field itself. Rather, it is intended to be used with - /// externally provided config files to give them significant control over how the crate - /// is build/configured. - /// - /// Serialized to multiple `extra_build_script_line` values. - pub extra_build_script_lines: Vec, -} - -impl InterpreterConfig { - #[doc(hidden)] - pub fn build_script_outputs(&self) -> Vec { - // This should have been checked during pyo3-build-config build time. - assert!(self.version >= MINIMUM_SUPPORTED_VERSION); - - let mut out = vec![]; - - for i in MINIMUM_SUPPORTED_VERSION.minor..=self.version.minor { - out.push(format!("cargo:rustc-cfg=Py_3_{}", i)); - } - - match self.implementation { - PythonImplementation::CPython => {} - PythonImplementation::PyPy => out.push("cargo:rustc-cfg=PyPy".to_owned()), - PythonImplementation::GraalPy => out.push("cargo:rustc-cfg=GraalPy".to_owned()), - } - - // If Py_GIL_DISABLED is set, do not build with limited API support - if self.abi3 && !self.build_flags.0.contains(&BuildFlag::Py_GIL_DISABLED) { - out.push("cargo:rustc-cfg=Py_LIMITED_API".to_owned()); - } - - for flag in &self.build_flags.0 { - match flag { - BuildFlag::Py_GIL_DISABLED => { - out.push("cargo:rustc-cfg=Py_GIL_DISABLED".to_owned()) - } - flag => out.push(format!("cargo:rustc-cfg=py_sys_config=\"{}\"", flag)), - } - } - - out - } - - #[doc(hidden)] - pub fn from_interpreter(interpreter: impl AsRef) -> Result { - const SCRIPT: &str = r#" -# Allow the script to run on Python 2, so that nicer error can be printed later. -from __future__ import print_function - -import os.path -import platform -import struct -import sys -from sysconfig import get_config_var, get_platform - -PYPY = platform.python_implementation() == "PyPy" -GRAALPY = platform.python_implementation() == "GraalVM" - -if GRAALPY: - graalpy_ver = map(int, __graalpython__.get_graalvm_version().split('.')); - print("graalpy_major", next(graalpy_ver)) - print("graalpy_minor", next(graalpy_ver)) - -# sys.base_prefix is missing on Python versions older than 3.3; this allows the script to continue -# so that the version mismatch can be reported in a nicer way later. -base_prefix = getattr(sys, "base_prefix", None) - -if base_prefix: - # Anaconda based python distributions have a static python executable, but include - # the shared library. Use the shared library for embedding to avoid rust trying to - # LTO the static library (and failing with newer gcc's, because it is old). - ANACONDA = os.path.exists(os.path.join(base_prefix, "conda-meta")) -else: - ANACONDA = False - -def print_if_set(varname, value): - if value is not None: - print(varname, value) - -# Windows always uses shared linking -WINDOWS = platform.system() == "Windows" - -# macOS framework packages use shared linking -FRAMEWORK = bool(get_config_var("PYTHONFRAMEWORK")) - -# unix-style shared library enabled -SHARED = bool(get_config_var("Py_ENABLE_SHARED")) - -print("implementation", platform.python_implementation()) -print("version_major", sys.version_info[0]) -print("version_minor", sys.version_info[1]) -print("shared", PYPY or GRAALPY or ANACONDA or WINDOWS or FRAMEWORK or SHARED) -print_if_set("ld_version", get_config_var("LDVERSION")) -print_if_set("libdir", get_config_var("LIBDIR")) -print_if_set("base_prefix", base_prefix) -print("executable", sys.executable) -print("calcsize_pointer", struct.calcsize("P")) -print("mingw", get_platform().startswith("mingw")) -print("ext_suffix", get_config_var("EXT_SUFFIX")) -"#; - let output = run_python_script(interpreter.as_ref(), SCRIPT)?; - let map: HashMap = parse_script_output(&output); - - ensure!( - !map.is_empty(), - "broken Python interpreter: {}", - interpreter.as_ref().display() - ); - - if let Some(value) = map.get("graalpy_major") { - let graalpy_version = PythonVersion { - major: value - .parse() - .context("failed to parse GraalPy major version")?, - minor: map["graalpy_minor"] - .parse() - .context("failed to parse GraalPy minor version")?, - }; - ensure!( - graalpy_version >= MINIMUM_SUPPORTED_VERSION_GRAALPY, - "At least GraalPy version {} needed, got {}", - MINIMUM_SUPPORTED_VERSION_GRAALPY, - graalpy_version - ); - }; - - let shared = map["shared"].as_str() == "True"; - - let version = PythonVersion { - major: map["version_major"] - .parse() - .context("failed to parse major version")?, - minor: map["version_minor"] - .parse() - .context("failed to parse minor version")?, - }; - - let abi3 = is_abi3(); - - let implementation = map["implementation"].parse()?; - - let lib_name = if cfg!(windows) { - default_lib_name_windows( - version, - implementation, - abi3, - map["mingw"].as_str() == "True", - // This is the best heuristic currently available to detect debug build - // on Windows from sysconfig - e.g. ext_suffix may be - // `_d.cp312-win_amd64.pyd` for 3.12 debug build - map["ext_suffix"].starts_with("_d."), - ) - } else { - default_lib_name_unix( - version, - implementation, - map.get("ld_version").map(String::as_str), - ) - }; - - let lib_dir = if cfg!(windows) { - map.get("base_prefix") - .map(|base_prefix| format!("{}\\libs", base_prefix)) - } else { - map.get("libdir").cloned() - }; - - // The reason we don't use platform.architecture() here is that it's not - // reliable on macOS. See https://stackoverflow.com/a/1405971/823869. - // Similarly, sys.maxsize is not reliable on Windows. See - // https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os/1405971#comment6209952_1405971 - // and https://stackoverflow.com/a/3411134/823869. - let calcsize_pointer: u32 = map["calcsize_pointer"] - .parse() - .context("failed to parse calcsize_pointer")?; - - Ok(InterpreterConfig { - version, - implementation, - shared, - abi3, - lib_name: Some(lib_name), - lib_dir, - executable: map.get("executable").cloned(), - pointer_width: Some(calcsize_pointer * 8), - build_flags: BuildFlags::from_interpreter(interpreter)?, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }) - } - - /// Generate from parsed sysconfigdata file - /// - /// Use [`parse_sysconfigdata`] to generate a hash map of configuration values which may be - /// used to build an [`InterpreterConfig`]. - pub fn from_sysconfigdata(sysconfigdata: &Sysconfigdata) -> Result { - macro_rules! get_key { - ($sysconfigdata:expr, $key:literal) => { - $sysconfigdata - .get_value($key) - .ok_or(concat!($key, " not found in sysconfigdata file")) - }; - } - - macro_rules! parse_key { - ($sysconfigdata:expr, $key:literal) => { - get_key!($sysconfigdata, $key)? - .parse() - .context(concat!("could not parse value of ", $key)) - }; - } - - let soabi = get_key!(sysconfigdata, "SOABI")?; - let implementation = PythonImplementation::from_soabi(soabi)?; - let version = parse_key!(sysconfigdata, "VERSION")?; - let shared = match sysconfigdata.get_value("Py_ENABLE_SHARED") { - Some("1") | Some("true") | Some("True") => true, - Some("0") | Some("false") | Some("False") => false, - _ => bail!("expected a bool (1/true/True or 0/false/False) for Py_ENABLE_SHARED"), - }; - // macOS framework packages use shared linking (PYTHONFRAMEWORK is the framework name, hence the empty check) - let framework = match sysconfigdata.get_value("PYTHONFRAMEWORK") { - Some(s) => !s.is_empty(), - _ => false, - }; - let lib_dir = get_key!(sysconfigdata, "LIBDIR").ok().map(str::to_string); - let lib_name = Some(default_lib_name_unix( - version, - implementation, - sysconfigdata.get_value("LDVERSION"), - )); - let pointer_width = parse_key!(sysconfigdata, "SIZEOF_VOID_P") - .map(|bytes_width: u32| bytes_width * 8) - .ok(); - let build_flags = BuildFlags::from_sysconfigdata(sysconfigdata); - - Ok(InterpreterConfig { - implementation, - version, - shared: shared || framework, - abi3: is_abi3(), - lib_dir, - lib_name, - executable: None, - pointer_width, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }) - } - - /// Import an externally-provided config file. - /// - /// The `abi3` features, if set, may apply an `abi3` constraint to the Python version. - #[allow(dead_code)] // only used in build.rs - pub(super) fn from_pyo3_config_file_env() -> Option> { - cargo_env_var("PYO3_CONFIG_FILE").map(|path| { - let path = Path::new(&path); - println!("cargo:rerun-if-changed={}", path.display()); - // Absolute path is necessary because this build script is run with a cwd different to the - // original `cargo build` instruction. - ensure!( - path.is_absolute(), - "PYO3_CONFIG_FILE must be an absolute path" - ); - - let mut config = InterpreterConfig::from_path(path) - .context("failed to parse contents of PYO3_CONFIG_FILE")?; - // If the abi3 feature is enabled, the minimum Python version is constrained by the abi3 - // feature. - // - // TODO: abi3 is a property of the build mode, not the interpreter. Should this be - // removed from `InterpreterConfig`? - config.abi3 |= is_abi3(); - config.fixup_for_abi3_version(get_abi3_version())?; - - Ok(config) - }) - } - - #[doc(hidden)] - pub fn from_path(path: impl AsRef) -> Result { - let path = path.as_ref(); - let config_file = std::fs::File::open(path) - .with_context(|| format!("failed to open PyO3 config file at {}", path.display()))?; - let reader = std::io::BufReader::new(config_file); - InterpreterConfig::from_reader(reader) - } - - #[doc(hidden)] - pub fn from_cargo_dep_env() -> Option> { - cargo_env_var("DEP_PYTHON_PYO3_CONFIG") - .map(|buf| InterpreterConfig::from_reader(&*unescape(&buf))) - } - - #[doc(hidden)] - pub fn from_reader(reader: impl Read) -> Result { - let reader = BufReader::new(reader); - let lines = reader.lines(); - - macro_rules! parse_value { - ($variable:ident, $value:ident) => { - $variable = Some($value.trim().parse().context(format!( - concat!( - "failed to parse ", - stringify!($variable), - " from config value '{}'" - ), - $value - ))?) - }; - } - - let mut implementation = None; - let mut version = None; - let mut shared = None; - let mut abi3 = None; - let mut lib_name = None; - let mut lib_dir = None; - let mut executable = None; - let mut pointer_width = None; - let mut build_flags = None; - let mut suppress_build_script_link_lines = None; - let mut extra_build_script_lines = vec![]; - - for (i, line) in lines.enumerate() { - let line = line.context("failed to read line from config")?; - let mut split = line.splitn(2, '='); - let (key, value) = ( - split - .next() - .expect("first splitn value should always be present"), - split - .next() - .ok_or_else(|| format!("expected key=value pair on line {}", i + 1))?, - ); - match key { - "implementation" => parse_value!(implementation, value), - "version" => parse_value!(version, value), - "shared" => parse_value!(shared, value), - "abi3" => parse_value!(abi3, value), - "lib_name" => parse_value!(lib_name, value), - "lib_dir" => parse_value!(lib_dir, value), - "executable" => parse_value!(executable, value), - "pointer_width" => parse_value!(pointer_width, value), - "build_flags" => parse_value!(build_flags, value), - "suppress_build_script_link_lines" => { - parse_value!(suppress_build_script_link_lines, value) - } - "extra_build_script_line" => { - extra_build_script_lines.push(value.to_string()); - } - unknown => warn!("unknown config key `{}`", unknown), - } - } - - let version = version.ok_or("missing value for version")?; - let implementation = implementation.unwrap_or(PythonImplementation::CPython); - let abi3 = abi3.unwrap_or(false); - // Fixup lib_name if it's not set - let lib_name = lib_name.or_else(|| { - if let Ok(Ok(target)) = env::var("TARGET").map(|target| target.parse::()) { - default_lib_name_for_target(version, implementation, abi3, &target) - } else { - None - } - }); - - Ok(InterpreterConfig { - implementation, - version, - shared: shared.unwrap_or(true), - abi3, - lib_name, - lib_dir, - executable, - pointer_width, - build_flags: build_flags.unwrap_or_default(), - suppress_build_script_link_lines: suppress_build_script_link_lines.unwrap_or(false), - extra_build_script_lines, - }) - } - - #[cfg(feature = "python3-dll-a")] - #[allow(clippy::unnecessary_wraps)] - pub fn generate_import_libs(&mut self) -> Result<()> { - // Auto generate python3.dll import libraries for Windows targets. - if self.lib_dir.is_none() { - let target = target_triple_from_env(); - let py_version = if self.abi3 { None } else { Some(self.version) }; - self.lib_dir = - import_lib::generate_import_lib(&target, self.implementation, py_version)?; - } - Ok(()) - } - - #[cfg(not(feature = "python3-dll-a"))] - #[allow(clippy::unnecessary_wraps)] - pub fn generate_import_libs(&mut self) -> Result<()> { - Ok(()) - } - - #[doc(hidden)] - /// Serialize the `InterpreterConfig` and print it to the environment for Cargo to pass along - /// to dependent packages during build time. - /// - /// NB: writing to the cargo environment requires the - /// [`links`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key) - /// manifest key to be set. In this case that means this is called by the `pyo3-ffi` crate and - /// available for dependent package build scripts in `DEP_PYTHON_PYO3_CONFIG`. See - /// documentation for the - /// [`DEP__`](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts) - /// environment variable. - pub fn to_cargo_dep_env(&self) -> Result<()> { - let mut buf = Vec::new(); - self.to_writer(&mut buf)?; - // escape newlines in env var - println!("cargo:PYO3_CONFIG={}", escape(&buf)); - Ok(()) - } - - #[doc(hidden)] - pub fn to_writer(&self, mut writer: impl Write) -> Result<()> { - macro_rules! write_line { - ($value:ident) => { - writeln!(writer, "{}={}", stringify!($value), self.$value).context(concat!( - "failed to write ", - stringify!($value), - " to config" - )) - }; - } - - macro_rules! write_option_line { - ($value:ident) => { - if let Some(value) = &self.$value { - writeln!(writer, "{}={}", stringify!($value), value).context(concat!( - "failed to write ", - stringify!($value), - " to config" - )) - } else { - Ok(()) - } - }; - } - - write_line!(implementation)?; - write_line!(version)?; - write_line!(shared)?; - write_line!(abi3)?; - write_option_line!(lib_name)?; - write_option_line!(lib_dir)?; - write_option_line!(executable)?; - write_option_line!(pointer_width)?; - write_line!(build_flags)?; - write_line!(suppress_build_script_link_lines)?; - for line in &self.extra_build_script_lines { - writeln!(writer, "extra_build_script_line={}", line) - .context("failed to write extra_build_script_line")?; - } - Ok(()) - } - - /// Run a python script using the [`InterpreterConfig::executable`]. - /// - /// # Panics - /// - /// This function will panic if the [`executable`](InterpreterConfig::executable) is `None`. - pub fn run_python_script(&self, script: &str) -> Result { - run_python_script_with_envs( - Path::new(self.executable.as_ref().expect("no interpreter executable")), - script, - std::iter::empty::<(&str, &str)>(), - ) - } - - /// Run a python script using the [`InterpreterConfig::executable`] with additional - /// environment variables (e.g. PYTHONPATH) set. - /// - /// # Panics - /// - /// This function will panic if the [`executable`](InterpreterConfig::executable) is `None`. - pub fn run_python_script_with_envs(&self, script: &str, envs: I) -> Result - where - I: IntoIterator, - K: AsRef, - V: AsRef, - { - run_python_script_with_envs( - Path::new(self.executable.as_ref().expect("no interpreter executable")), - script, - envs, - ) - } - - /// Lowers the configured version to the abi3 version, if set. - fn fixup_for_abi3_version(&mut self, abi3_version: Option) -> Result<()> { - // PyPy doesn't support abi3; don't adjust the version - if self.implementation.is_pypy() || self.implementation.is_graalpy() { - return Ok(()); - } - - if let Some(version) = abi3_version { - ensure!( - version <= self.version, - "cannot set a minimum Python version {} higher than the interpreter version {} \ - (the minimum Python version is implied by the abi3-py3{} feature)", - version, - self.version, - version.minor, - ); - - self.version = version; - } - - Ok(()) - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct PythonVersion { - pub major: u8, - pub minor: u8, -} - -impl PythonVersion { - const PY37: Self = PythonVersion { major: 3, minor: 7 }; -} - -impl Display for PythonVersion { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}.{}", self.major, self.minor) - } -} - -impl FromStr for PythonVersion { - type Err = crate::errors::Error; - - fn from_str(value: &str) -> Result { - let mut split = value.splitn(2, '.'); - let (major, minor) = ( - split - .next() - .expect("first splitn value should always be present"), - split.next().ok_or("expected major.minor version")?, - ); - Ok(Self { - major: major.parse().context("failed to parse major version")?, - minor: minor.parse().context("failed to parse minor version")?, - }) - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum PythonImplementation { - CPython, - PyPy, - GraalPy, -} - -impl PythonImplementation { - #[doc(hidden)] - pub fn is_pypy(self) -> bool { - self == PythonImplementation::PyPy - } - - #[doc(hidden)] - pub fn is_graalpy(self) -> bool { - self == PythonImplementation::GraalPy - } - - #[doc(hidden)] - pub fn from_soabi(soabi: &str) -> Result { - if soabi.starts_with("pypy") { - Ok(PythonImplementation::PyPy) - } else if soabi.starts_with("cpython") { - Ok(PythonImplementation::CPython) - } else if soabi.starts_with("graalpy") { - Ok(PythonImplementation::GraalPy) - } else { - bail!("unsupported Python interpreter"); - } - } -} - -impl Display for PythonImplementation { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - PythonImplementation::CPython => write!(f, "CPython"), - PythonImplementation::PyPy => write!(f, "PyPy"), - PythonImplementation::GraalPy => write!(f, "GraalVM"), - } - } -} - -impl FromStr for PythonImplementation { - type Err = Error; - fn from_str(s: &str) -> Result { - match s { - "CPython" => Ok(PythonImplementation::CPython), - "PyPy" => Ok(PythonImplementation::PyPy), - "GraalVM" => Ok(PythonImplementation::GraalPy), - _ => bail!("unknown interpreter: {}", s), - } - } -} - -/// Checks if we should look for a Python interpreter installation -/// to get the target interpreter configuration. -/// -/// Returns `false` if `PYO3_NO_PYTHON` environment variable is set. -fn have_python_interpreter() -> bool { - env_var("PYO3_NO_PYTHON").is_none() -} - -/// Checks if `abi3` or any of the `abi3-py3*` features is enabled for the PyO3 crate. -/// -/// Must be called from a PyO3 crate build script. -fn is_abi3() -> bool { - cargo_env_var("CARGO_FEATURE_ABI3").is_some() - || env_var("PYO3_USE_ABI3_FORWARD_COMPATIBILITY").map_or(false, |os_str| os_str == "1") -} - -/// Gets the minimum supported Python version from PyO3 `abi3-py*` features. -/// -/// Must be called from a PyO3 crate build script. -pub fn get_abi3_version() -> Option { - let minor_version = (MINIMUM_SUPPORTED_VERSION.minor..=ABI3_MAX_MINOR) - .find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some()); - minor_version.map(|minor| PythonVersion { major: 3, minor }) -} - -/// Checks if the `extension-module` feature is enabled for the PyO3 crate. -/// -/// Must be called from a PyO3 crate build script. -pub fn is_extension_module() -> bool { - cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some() -} - -/// Checks if we need to link to `libpython` for the current build target. -/// -/// Must be called from a PyO3 crate build script. -pub fn is_linking_libpython() -> bool { - is_linking_libpython_for_target(&target_triple_from_env()) -} - -/// Checks if we need to link to `libpython` for the target. -/// -/// Must be called from a PyO3 crate build script. -fn is_linking_libpython_for_target(target: &Triple) -> bool { - target.operating_system == OperatingSystem::Windows - // See https://github.com/PyO3/pyo3/issues/4068#issuecomment-2051159852 - || target.operating_system == OperatingSystem::Aix - || target.environment == Environment::Android - || target.environment == Environment::Androideabi - || !is_extension_module() -} - -/// Checks if we need to discover the Python library directory -/// to link the extension module binary. -/// -/// Must be called from a PyO3 crate build script. -fn require_libdir_for_target(target: &Triple) -> bool { - let is_generating_libpython = cfg!(feature = "python3-dll-a") - && target.operating_system == OperatingSystem::Windows - && is_abi3(); - - is_linking_libpython_for_target(target) && !is_generating_libpython -} - -/// Configuration needed by PyO3 to cross-compile for a target platform. -/// -/// Usually this is collected from the environment (i.e. `PYO3_CROSS_*` and `CARGO_CFG_TARGET_*`) -/// when a cross-compilation configuration is detected. -#[derive(Debug, PartialEq, Eq)] -pub struct CrossCompileConfig { - /// The directory containing the Python library to link against. - pub lib_dir: Option, - - /// The version of the Python library to link against. - version: Option, - - /// The target Python implementation hint (CPython, PyPy, GraalPy, ...) - implementation: Option, - - /// The compile target triple (e.g. aarch64-unknown-linux-gnu) - target: Triple, -} - -impl CrossCompileConfig { - /// Creates a new cross compile config struct from PyO3 environment variables - /// and the build environment when cross compilation mode is detected. - /// - /// Returns `None` when not cross compiling. - fn try_from_env_vars_host_target( - env_vars: CrossCompileEnvVars, - host: &Triple, - target: &Triple, - ) -> Result> { - if env_vars.any() || Self::is_cross_compiling_from_to(host, target) { - let lib_dir = env_vars.lib_dir_path()?; - let version = env_vars.parse_version()?; - let implementation = env_vars.parse_implementation()?; - let target = target.clone(); - - Ok(Some(CrossCompileConfig { - lib_dir, - version, - implementation, - target, - })) - } else { - Ok(None) - } - } - - /// Checks if compiling on `host` for `target` required "real" cross compilation. - /// - /// Returns `false` if the target Python interpreter can run on the host. - fn is_cross_compiling_from_to(host: &Triple, target: &Triple) -> bool { - // Not cross-compiling if arch-vendor-os is all the same - // e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host - // x86_64-pc-windows-gnu on x86_64-pc-windows-msvc host - let mut compatible = host.architecture == target.architecture - && host.vendor == target.vendor - && host.operating_system == target.operating_system; - - // Not cross-compiling to compile for 32-bit Python from windows 64-bit - compatible |= target.operating_system == OperatingSystem::Windows - && host.operating_system == OperatingSystem::Windows; - - // Not cross-compiling to compile for x86-64 Python from macOS arm64 and vice versa - compatible |= target.operating_system == OperatingSystem::Darwin - && host.operating_system == OperatingSystem::Darwin; - - !compatible - } - - /// Converts `lib_dir` member field to an UTF-8 string. - /// - /// The conversion can not fail because `PYO3_CROSS_LIB_DIR` variable - /// is ensured contain a valid UTF-8 string. - fn lib_dir_string(&self) -> Option { - self.lib_dir - .as_ref() - .map(|s| s.to_str().unwrap().to_owned()) - } -} - -/// PyO3-specific cross compile environment variable values -struct CrossCompileEnvVars { - /// `PYO3_CROSS` - pyo3_cross: Option, - /// `PYO3_CROSS_LIB_DIR` - pyo3_cross_lib_dir: Option, - /// `PYO3_CROSS_PYTHON_VERSION` - pyo3_cross_python_version: Option, - /// `PYO3_CROSS_PYTHON_IMPLEMENTATION` - pyo3_cross_python_implementation: Option, -} - -impl CrossCompileEnvVars { - /// Grabs the PyO3 cross-compile variables from the environment. - /// - /// Registers the build script to rerun if any of the variables changes. - fn from_env() -> Self { - CrossCompileEnvVars { - pyo3_cross: env_var("PYO3_CROSS"), - pyo3_cross_lib_dir: env_var("PYO3_CROSS_LIB_DIR"), - pyo3_cross_python_version: env_var("PYO3_CROSS_PYTHON_VERSION"), - pyo3_cross_python_implementation: env_var("PYO3_CROSS_PYTHON_IMPLEMENTATION"), - } - } - - /// Checks if any of the variables is set. - fn any(&self) -> bool { - self.pyo3_cross.is_some() - || self.pyo3_cross_lib_dir.is_some() - || self.pyo3_cross_python_version.is_some() - || self.pyo3_cross_python_implementation.is_some() - } - - /// Parses `PYO3_CROSS_PYTHON_VERSION` environment variable value - /// into `PythonVersion`. - fn parse_version(&self) -> Result> { - let version = self - .pyo3_cross_python_version - .as_ref() - .map(|os_string| { - let utf8_str = os_string - .to_str() - .ok_or("PYO3_CROSS_PYTHON_VERSION is not valid a UTF-8 string")?; - utf8_str - .parse() - .context("failed to parse PYO3_CROSS_PYTHON_VERSION") - }) - .transpose()?; - - Ok(version) - } - - /// Parses `PYO3_CROSS_PYTHON_IMPLEMENTATION` environment variable value - /// into `PythonImplementation`. - fn parse_implementation(&self) -> Result> { - let implementation = self - .pyo3_cross_python_implementation - .as_ref() - .map(|os_string| { - let utf8_str = os_string - .to_str() - .ok_or("PYO3_CROSS_PYTHON_IMPLEMENTATION is not valid a UTF-8 string")?; - utf8_str - .parse() - .context("failed to parse PYO3_CROSS_PYTHON_IMPLEMENTATION") - }) - .transpose()?; - - Ok(implementation) - } - - /// Converts the stored `PYO3_CROSS_LIB_DIR` variable value (if any) - /// into a `PathBuf` instance. - /// - /// Ensures that the path is a valid UTF-8 string. - fn lib_dir_path(&self) -> Result> { - let lib_dir = self.pyo3_cross_lib_dir.as_ref().map(PathBuf::from); - - if let Some(dir) = lib_dir.as_ref() { - ensure!( - dir.to_str().is_some(), - "PYO3_CROSS_LIB_DIR variable value is not a valid UTF-8 string" - ); - } - - Ok(lib_dir) - } -} - -/// Detect whether we are cross compiling and return an assembled CrossCompileConfig if so. -/// -/// This function relies on PyO3 cross-compiling environment variables: -/// -/// * `PYO3_CROSS`: If present, forces PyO3 to configure as a cross-compilation. -/// * `PYO3_CROSS_LIB_DIR`: If present, must be set to the directory containing -/// the target's libpython DSO and the associated `_sysconfigdata*.py` file for -/// Unix-like targets, or the Python DLL import libraries for the Windows target. -/// * `PYO3_CROSS_PYTHON_VERSION`: Major and minor version (e.g. 3.9) of the target Python -/// installation. This variable is only needed if PyO3 cannnot determine the version to target -/// from `abi3-py3*` features, or if there are multiple versions of Python present in -/// `PYO3_CROSS_LIB_DIR`. -/// -/// See the [PyO3 User Guide](https://pyo3.rs/) for more info on cross-compiling. -pub fn cross_compiling_from_to( - host: &Triple, - target: &Triple, -) -> Result> { - let env_vars = CrossCompileEnvVars::from_env(); - CrossCompileConfig::try_from_env_vars_host_target(env_vars, host, target) -} - -/// Detect whether we are cross compiling from Cargo and `PYO3_CROSS_*` environment -/// variables and return an assembled `CrossCompileConfig` if so. -/// -/// This must be called from PyO3's build script, because it relies on environment -/// variables such as `CARGO_CFG_TARGET_OS` which aren't available at any other time. -pub fn cross_compiling_from_cargo_env() -> Result> { - let env_vars = CrossCompileEnvVars::from_env(); - let host = Triple::host(); - let target = target_triple_from_env(); - - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) -} - -#[allow(non_camel_case_types)] -#[derive(Debug, Clone, Hash, PartialEq, Eq)] -pub enum BuildFlag { - Py_DEBUG, - Py_REF_DEBUG, - Py_TRACE_REFS, - Py_GIL_DISABLED, - COUNT_ALLOCS, - Other(String), -} - -impl Display for BuildFlag { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - BuildFlag::Other(flag) => write!(f, "{}", flag), - _ => write!(f, "{:?}", self), - } - } -} - -impl FromStr for BuildFlag { - type Err = std::convert::Infallible; - fn from_str(s: &str) -> Result { - match s { - "Py_DEBUG" => Ok(BuildFlag::Py_DEBUG), - "Py_REF_DEBUG" => Ok(BuildFlag::Py_REF_DEBUG), - "Py_TRACE_REFS" => Ok(BuildFlag::Py_TRACE_REFS), - "Py_GIL_DISABLED" => Ok(BuildFlag::Py_GIL_DISABLED), - "COUNT_ALLOCS" => Ok(BuildFlag::COUNT_ALLOCS), - other => Ok(BuildFlag::Other(other.to_owned())), - } - } -} - -/// A list of python interpreter compile-time preprocessor defines. -/// -/// PyO3 will pick these up and pass to rustc via `--cfg=py_sys_config={varname}`; -/// this allows using them conditional cfg attributes in the .rs files, so -/// -/// ```rust -/// #[cfg(py_sys_config="{varname}")] -/// # struct Foo; -/// ``` -/// -/// is the equivalent of `#ifdef {varname}` in C. -/// -/// see Misc/SpecialBuilds.txt in the python source for what these mean. -#[cfg_attr(test, derive(Debug, PartialEq, Eq))] -#[derive(Clone, Default)] -pub struct BuildFlags(pub HashSet); - -impl BuildFlags { - const ALL: [BuildFlag; 5] = [ - BuildFlag::Py_DEBUG, - BuildFlag::Py_REF_DEBUG, - BuildFlag::Py_TRACE_REFS, - BuildFlag::Py_GIL_DISABLED, - BuildFlag::COUNT_ALLOCS, - ]; - - pub fn new() -> Self { - BuildFlags(HashSet::new()) - } - - fn from_sysconfigdata(config_map: &Sysconfigdata) -> Self { - Self( - BuildFlags::ALL - .iter() - .filter(|flag| { - config_map - .get_value(flag.to_string()) - .map_or(false, |value| value == "1") - }) - .cloned() - .collect(), - ) - .fixup() - } - - /// Examine python's compile flags to pass to cfg by launching - /// the interpreter and printing variables of interest from - /// sysconfig.get_config_vars. - fn from_interpreter(interpreter: impl AsRef) -> Result { - // sysconfig is missing all the flags on windows, so we can't actually - // query the interpreter directly for its build flags. - if cfg!(windows) { - return Ok(Self::new()); - } - - let mut script = String::from("import sysconfig\n"); - script.push_str("config = sysconfig.get_config_vars()\n"); - - for k in &BuildFlags::ALL { - use std::fmt::Write; - writeln!(&mut script, "print(config.get('{}', '0'))", k).unwrap(); - } - - let stdout = run_python_script(interpreter.as_ref(), &script)?; - let split_stdout: Vec<&str> = stdout.trim_end().lines().collect(); - ensure!( - split_stdout.len() == BuildFlags::ALL.len(), - "Python stdout len didn't return expected number of lines: {}", - split_stdout.len() - ); - let flags = BuildFlags::ALL - .iter() - .zip(split_stdout) - .filter(|(_, flag_value)| *flag_value == "1") - .map(|(flag, _)| flag.clone()) - .collect(); - - Ok(Self(flags).fixup()) - } - - fn fixup(mut self) -> Self { - if self.0.contains(&BuildFlag::Py_DEBUG) { - self.0.insert(BuildFlag::Py_REF_DEBUG); - } - - self - } -} - -impl Display for BuildFlags { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut first = true; - for flag in &self.0 { - if first { - first = false; - } else { - write!(f, ",")?; - } - write!(f, "{}", flag)?; - } - Ok(()) - } -} - -impl FromStr for BuildFlags { - type Err = std::convert::Infallible; - - fn from_str(value: &str) -> Result { - let mut flags = HashSet::new(); - for flag in value.split_terminator(',') { - flags.insert(flag.parse().unwrap()); - } - Ok(BuildFlags(flags)) - } -} - -fn parse_script_output(output: &str) -> HashMap { - output - .lines() - .filter_map(|line| { - let mut i = line.splitn(2, ' '); - Some((i.next()?.into(), i.next()?.into())) - }) - .collect() -} - -/// Parsed data from Python sysconfigdata file -/// -/// A hash map of all values from a sysconfigdata file. -pub struct Sysconfigdata(HashMap); - -impl Sysconfigdata { - pub fn get_value>(&self, k: S) -> Option<&str> { - self.0.get(k.as_ref()).map(String::as_str) - } - - #[allow(dead_code)] - fn new() -> Self { - Sysconfigdata(HashMap::new()) - } - - #[allow(dead_code)] - fn insert>(&mut self, k: S, v: S) { - self.0.insert(k.into(), v.into()); - } -} - -/// Parse sysconfigdata file -/// -/// The sysconfigdata is simply a dictionary containing all the build time variables used for the -/// python executable and library. This function necessitates a python interpreter on the host -/// machine to work. Here it is read into a `Sysconfigdata` (hash map), which can be turned into an -/// [`InterpreterConfig`] using -/// [`from_sysconfigdata`](InterpreterConfig::from_sysconfigdata). -pub fn parse_sysconfigdata(sysconfigdata_path: impl AsRef) -> Result { - let sysconfigdata_path = sysconfigdata_path.as_ref(); - let mut script = fs::read_to_string(sysconfigdata_path).with_context(|| { - format!( - "failed to read config from {}", - sysconfigdata_path.display() - ) - })?; - script += r#" -for key, val in build_time_vars.items(): - print(key, val) -"#; - - let output = run_python_script(&find_interpreter()?, &script)?; - - Ok(Sysconfigdata(parse_script_output(&output))) -} - -fn starts_with(entry: &DirEntry, pat: &str) -> bool { - let name = entry.file_name(); - name.to_string_lossy().starts_with(pat) -} -fn ends_with(entry: &DirEntry, pat: &str) -> bool { - let name = entry.file_name(); - name.to_string_lossy().ends_with(pat) -} - -/// Finds the sysconfigdata file when the target Python library directory is set. -/// -/// Returns `None` if the library directory is not available, and a runtime error -/// when no or multiple sysconfigdata files are found. -fn find_sysconfigdata(cross: &CrossCompileConfig) -> Result> { - let mut sysconfig_paths = find_all_sysconfigdata(cross)?; - if sysconfig_paths.is_empty() { - if let Some(lib_dir) = cross.lib_dir.as_ref() { - bail!("Could not find _sysconfigdata*.py in {}", lib_dir.display()); - } else { - // Continue with the default configuration when PYO3_CROSS_LIB_DIR is not set. - return Ok(None); - } - } else if sysconfig_paths.len() > 1 { - let mut error_msg = String::from( - "Detected multiple possible Python versions. Please set either the \ - PYO3_CROSS_PYTHON_VERSION variable to the wanted version or the \ - _PYTHON_SYSCONFIGDATA_NAME variable to the wanted sysconfigdata file name.\n\n\ - sysconfigdata files found:", - ); - for path in sysconfig_paths { - use std::fmt::Write; - write!(&mut error_msg, "\n\t{}", path.display()).unwrap(); - } - bail!("{}\n", error_msg); - } - - Ok(Some(sysconfig_paths.remove(0))) -} - -/// Finds `_sysconfigdata*.py` files for detected Python interpreters. -/// -/// From the python source for `_sysconfigdata*.py` is always going to be located at -/// `build/lib.{PLATFORM}-{PY_MINOR_VERSION}` when built from source. The [exact line][1] is defined as: -/// -/// ```py -/// pybuilddir = 'build/lib.%s-%s' % (get_platform(), sys.version_info[:2]) -/// ``` -/// -/// Where get_platform returns a kebab-case formatted string containing the os, the architecture and -/// possibly the os' kernel version (not the case on linux). However, when installed using a package -/// manager, the `_sysconfigdata*.py` file is installed in the `${PREFIX}/lib/python3.Y/` directory. -/// The `_sysconfigdata*.py` is generally in a sub-directory of the location of `libpython3.Y.so`. -/// So we must find the file in the following possible locations: -/// -/// ```sh -/// # distribution from package manager, (lib_dir may or may not include lib/) -/// ${INSTALL_PREFIX}/lib/python3.Y/_sysconfigdata*.py -/// ${INSTALL_PREFIX}/lib/libpython3.Y.so -/// ${INSTALL_PREFIX}/lib/python3.Y/config-3.Y-${HOST_TRIPLE}/libpython3.Y.so -/// -/// # Built from source from host -/// ${CROSS_COMPILED_LOCATION}/build/lib.linux-x86_64-Y/_sysconfigdata*.py -/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so -/// -/// # if cross compiled, kernel release is only present on certain OS targets. -/// ${CROSS_COMPILED_LOCATION}/build/lib.{OS}(-{OS-KERNEL-RELEASE})?-{ARCH}-Y/_sysconfigdata*.py -/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so -/// -/// # PyPy includes a similar file since v73 -/// ${INSTALL_PREFIX}/lib/pypy3.Y/_sysconfigdata.py -/// ${INSTALL_PREFIX}/lib_pypy/_sysconfigdata.py -/// ``` -/// -/// [1]: https://github.com/python/cpython/blob/3.5/Lib/sysconfig.py#L389 -/// -/// Returns an empty vector when the target Python library directory -/// is not set via `PYO3_CROSS_LIB_DIR`. -pub fn find_all_sysconfigdata(cross: &CrossCompileConfig) -> Result> { - let sysconfig_paths = if let Some(lib_dir) = cross.lib_dir.as_ref() { - search_lib_dir(lib_dir, cross).with_context(|| { - format!( - "failed to search the lib dir at 'PYO3_CROSS_LIB_DIR={}'", - lib_dir.display() - ) - })? - } else { - return Ok(Vec::new()); - }; - - let sysconfig_name = env_var("_PYTHON_SYSCONFIGDATA_NAME"); - let mut sysconfig_paths = sysconfig_paths - .iter() - .filter_map(|p| { - let canonical = fs::canonicalize(p).ok(); - match &sysconfig_name { - Some(_) => canonical.filter(|p| p.file_stem() == sysconfig_name.as_deref()), - None => canonical, - } - }) - .collect::>(); - - sysconfig_paths.sort(); - sysconfig_paths.dedup(); - - Ok(sysconfig_paths) -} - -fn is_pypy_lib_dir(path: &str, v: &Option) -> bool { - let pypy_version_pat = if let Some(v) = v { - format!("pypy{}", v) - } else { - "pypy3.".into() - }; - path == "lib_pypy" || path.starts_with(&pypy_version_pat) -} - -fn is_graalpy_lib_dir(path: &str, v: &Option) -> bool { - let graalpy_version_pat = if let Some(v) = v { - format!("graalpy{}", v) - } else { - "graalpy2".into() - }; - path == "lib_graalpython" || path.starts_with(&graalpy_version_pat) -} - -fn is_cpython_lib_dir(path: &str, v: &Option) -> bool { - let cpython_version_pat = if let Some(v) = v { - format!("python{}", v) - } else { - "python3.".into() - }; - path.starts_with(&cpython_version_pat) -} - -/// recursive search for _sysconfigdata, returns all possibilities of sysconfigdata paths -fn search_lib_dir(path: impl AsRef, cross: &CrossCompileConfig) -> Result> { - let mut sysconfig_paths = vec![]; - for f in fs::read_dir(path.as_ref()).with_context(|| { - format!( - "failed to list the entries in '{}'", - path.as_ref().display() - ) - })? { - sysconfig_paths.extend(match &f { - // Python 3.7+ sysconfigdata with platform specifics - Ok(f) if starts_with(f, "_sysconfigdata_") && ends_with(f, "py") => vec![f.path()], - Ok(f) if f.metadata().map_or(false, |metadata| metadata.is_dir()) => { - let file_name = f.file_name(); - let file_name = file_name.to_string_lossy(); - if file_name == "build" || file_name == "lib" { - search_lib_dir(f.path(), cross)? - } else if file_name.starts_with("lib.") { - // check if right target os - if !file_name.contains(&cross.target.operating_system.to_string()) { - continue; - } - // Check if right arch - if !file_name.contains(&cross.target.architecture.to_string()) { - continue; - } - search_lib_dir(f.path(), cross)? - } else if is_cpython_lib_dir(&file_name, &cross.version) - || is_pypy_lib_dir(&file_name, &cross.version) - || is_graalpy_lib_dir(&file_name, &cross.version) - { - search_lib_dir(f.path(), cross)? - } else { - continue; - } - } - _ => continue, - }); - } - // If we got more than one file, only take those that contain the arch name. - // For ubuntu 20.04 with host architecture x86_64 and a foreign architecture of armhf - // this reduces the number of candidates to 1: - // - // $ find /usr/lib/python3.8/ -name '_sysconfigdata*.py' -not -lname '*' - // /usr/lib/python3.8/_sysconfigdata__x86_64-linux-gnu.py - // /usr/lib/python3.8/_sysconfigdata__arm-linux-gnueabihf.py - if sysconfig_paths.len() > 1 { - let temp = sysconfig_paths - .iter() - .filter(|p| { - p.to_string_lossy() - .contains(&cross.target.architecture.to_string()) - }) - .cloned() - .collect::>(); - if !temp.is_empty() { - sysconfig_paths = temp; - } - } - - Ok(sysconfig_paths) -} - -/// Find cross compilation information from sysconfigdata file -/// -/// first find sysconfigdata file which follows the pattern [`_sysconfigdata_{abi}_{platform}_{multiarch}`][1] -/// -/// [1]: https://github.com/python/cpython/blob/3.8/Lib/sysconfig.py#L348 -/// -/// Returns `None` when the target Python library directory is not set. -fn cross_compile_from_sysconfigdata( - cross_compile_config: &CrossCompileConfig, -) -> Result> { - if let Some(path) = find_sysconfigdata(cross_compile_config)? { - let data = parse_sysconfigdata(path)?; - let mut config = InterpreterConfig::from_sysconfigdata(&data)?; - if let Some(cross_lib_dir) = cross_compile_config.lib_dir_string() { - config.lib_dir = Some(cross_lib_dir) - } - - Ok(Some(config)) - } else { - Ok(None) - } -} - -/// Generates "default" cross compilation information for the target. -/// -/// This should work for most CPython extension modules when targeting -/// Windows, macOS and Linux. -/// -/// Must be called from a PyO3 crate build script. -#[allow(unused_mut)] -fn default_cross_compile(cross_compile_config: &CrossCompileConfig) -> Result { - let version = cross_compile_config - .version - .or_else(get_abi3_version) - .ok_or_else(|| - format!( - "PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified \ - when cross-compiling and PYO3_CROSS_LIB_DIR is not set.\n\ - = help: see the PyO3 user guide for more information: https://pyo3.rs/v{}/building-and-distribution.html#cross-compiling", - env!("CARGO_PKG_VERSION") - ) - )?; - - let abi3 = is_abi3(); - let implementation = cross_compile_config - .implementation - .unwrap_or(PythonImplementation::CPython); - - let lib_name = - default_lib_name_for_target(version, implementation, abi3, &cross_compile_config.target); - - let mut lib_dir = cross_compile_config.lib_dir_string(); - - // Auto generate python3.dll import libraries for Windows targets. - #[cfg(feature = "python3-dll-a")] - if lib_dir.is_none() { - let py_version = if abi3 { None } else { Some(version) }; - lib_dir = self::import_lib::generate_import_lib( - &cross_compile_config.target, - cross_compile_config - .implementation - .unwrap_or(PythonImplementation::CPython), - py_version, - )?; - } - - Ok(InterpreterConfig { - implementation, - version, - shared: true, - abi3, - lib_name, - lib_dir, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }) -} - -/// Generates "default" interpreter configuration when compiling "abi3" extensions -/// without a working Python interpreter. -/// -/// `version` specifies the minimum supported Stable ABI CPython version. -/// -/// This should work for most CPython extension modules when compiling on -/// Windows, macOS and Linux. -/// -/// Must be called from a PyO3 crate build script. -fn default_abi3_config(host: &Triple, version: PythonVersion) -> InterpreterConfig { - // FIXME: PyPy & GraalPy do not support the Stable ABI. - let implementation = PythonImplementation::CPython; - let abi3 = true; - - let lib_name = if host.operating_system == OperatingSystem::Windows { - Some(default_lib_name_windows( - version, - implementation, - abi3, - false, - false, - )) - } else { - None - }; - - InterpreterConfig { - implementation, - version, - shared: true, - abi3, - lib_name, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } -} - -/// Detects the cross compilation target interpreter configuration from all -/// available sources (PyO3 environment variables, Python sysconfigdata, etc.). -/// -/// Returns the "default" target interpreter configuration for Windows and -/// when no target Python interpreter is found. -/// -/// Must be called from a PyO3 crate build script. -fn load_cross_compile_config( - cross_compile_config: CrossCompileConfig, -) -> Result { - let windows = cross_compile_config.target.operating_system == OperatingSystem::Windows; - - let config = if windows || !have_python_interpreter() { - // Load the defaults for Windows even when `PYO3_CROSS_LIB_DIR` is set - // since it has no sysconfigdata files in it. - // Also, do not try to look for sysconfigdata when `PYO3_NO_PYTHON` variable is set. - default_cross_compile(&cross_compile_config)? - } else if let Some(config) = cross_compile_from_sysconfigdata(&cross_compile_config)? { - // Try to find and parse sysconfigdata files on other targets. - config - } else { - // Fall back to the defaults when nothing else can be done. - default_cross_compile(&cross_compile_config)? - }; - - if config.lib_name.is_some() && config.lib_dir.is_none() { - warn!( - "The output binary will link to libpython, \ - but PYO3_CROSS_LIB_DIR environment variable is not set. \ - Ensure that the target Python library directory is \ - in the rustc native library search path." - ); - } - - Ok(config) -} - -// Link against python3.lib for the stable ABI on Windows. -// See https://www.python.org/dev/peps/pep-0384/#linkage -// -// This contains only the limited ABI symbols. -const WINDOWS_ABI3_LIB_NAME: &str = "python3"; - -fn default_lib_name_for_target( - version: PythonVersion, - implementation: PythonImplementation, - abi3: bool, - target: &Triple, -) -> Option { - if target.operating_system == OperatingSystem::Windows { - Some(default_lib_name_windows( - version, - implementation, - abi3, - false, - false, - )) - } else if is_linking_libpython_for_target(target) { - Some(default_lib_name_unix(version, implementation, None)) - } else { - None - } -} - -fn default_lib_name_windows( - version: PythonVersion, - implementation: PythonImplementation, - abi3: bool, - mingw: bool, - debug: bool, -) -> String { - if debug { - // CPython bug: linking against python3_d.dll raises error - // https://github.com/python/cpython/issues/101614 - format!("python{}{}_d", version.major, version.minor) - } else if abi3 && !(implementation.is_pypy() || implementation.is_graalpy()) { - WINDOWS_ABI3_LIB_NAME.to_owned() - } else if mingw { - // https://packages.msys2.org/base/mingw-w64-python - format!("python{}.{}", version.major, version.minor) - } else { - format!("python{}{}", version.major, version.minor) - } -} - -fn default_lib_name_unix( - version: PythonVersion, - implementation: PythonImplementation, - ld_version: Option<&str>, -) -> String { - match implementation { - PythonImplementation::CPython => match ld_version { - Some(ld_version) => format!("python{}", ld_version), - None => { - if version > PythonVersion::PY37 { - // PEP 3149 ABI version tags are finally gone - format!("python{}.{}", version.major, version.minor) - } else { - // Work around https://bugs.python.org/issue36707 - format!("python{}.{}m", version.major, version.minor) - } - } - }, - PythonImplementation::PyPy => match ld_version { - Some(ld_version) => format!("pypy{}-c", ld_version), - None => format!("pypy{}.{}-c", version.major, version.minor), - }, - - PythonImplementation::GraalPy => "python-native".to_string(), - } -} - -/// Run a python script using the specified interpreter binary. -fn run_python_script(interpreter: &Path, script: &str) -> Result { - run_python_script_with_envs(interpreter, script, std::iter::empty::<(&str, &str)>()) -} - -/// Run a python script using the specified interpreter binary with additional environment -/// variables (e.g. PYTHONPATH) set. -fn run_python_script_with_envs(interpreter: &Path, script: &str, envs: I) -> Result -where - I: IntoIterator, - K: AsRef, - V: AsRef, -{ - let out = Command::new(interpreter) - .env("PYTHONIOENCODING", "utf-8") - .envs(envs) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::inherit()) - .spawn() - .and_then(|mut child| { - child - .stdin - .as_mut() - .expect("piped stdin") - .write_all(script.as_bytes())?; - child.wait_with_output() - }); - - match out { - Err(err) => bail!( - "failed to run the Python interpreter at {}: {}", - interpreter.display(), - err - ), - Ok(ok) if !ok.status.success() => bail!("Python script failed"), - Ok(ok) => Ok(String::from_utf8(ok.stdout) - .context("failed to parse Python script output as utf-8")?), - } -} - -fn venv_interpreter(virtual_env: &OsStr, windows: bool) -> PathBuf { - if windows { - Path::new(virtual_env).join("Scripts").join("python.exe") - } else { - Path::new(virtual_env).join("bin").join("python") - } -} - -fn conda_env_interpreter(conda_prefix: &OsStr, windows: bool) -> PathBuf { - if windows { - Path::new(conda_prefix).join("python.exe") - } else { - Path::new(conda_prefix).join("bin").join("python") - } -} - -fn get_env_interpreter() -> Option { - match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) { - // Use cfg rather than CARGO_CFG_TARGET_OS because this affects where files are located on the - // build host - (Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))), - (None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))), - (Some(_), Some(_)) => { - warn!( - "Both VIRTUAL_ENV and CONDA_PREFIX are set. PyO3 will ignore both of these for \ - locating the Python interpreter until you unset one of them." - ); - None - } - (None, None) => None, - } -} - -/// Attempts to locate a python interpreter. -/// -/// Locations are checked in the order listed: -/// 1. If `PYO3_PYTHON` is set, this interpreter is used. -/// 2. If in a virtualenv, that environment's interpreter is used. -/// 3. `python`, if this is functional a Python 3.x interpreter -/// 4. `python3`, as above -pub fn find_interpreter() -> Result { - // Trigger rebuilds when `PYO3_ENVIRONMENT_SIGNATURE` env var value changes - // See https://github.com/PyO3/pyo3/issues/2724 - println!("cargo:rerun-if-env-changed=PYO3_ENVIRONMENT_SIGNATURE"); - - if let Some(exe) = env_var("PYO3_PYTHON") { - Ok(exe.into()) - } else if let Some(env_interpreter) = get_env_interpreter() { - Ok(env_interpreter) - } else { - println!("cargo:rerun-if-env-changed=PATH"); - ["python", "python3"] - .iter() - .find(|bin| { - if let Ok(out) = Command::new(bin).arg("--version").output() { - // begin with `Python 3.X.X :: additional info` - out.stdout.starts_with(b"Python 3") - || out.stderr.starts_with(b"Python 3") - || out.stdout.starts_with(b"GraalPy 3") - } else { - false - } - }) - .map(PathBuf::from) - .ok_or_else(|| "no Python 3.x interpreter found".into()) - } -} - -/// Locates and extracts the build host Python interpreter configuration. -/// -/// Lowers the configured Python version to `abi3_version` if required. -fn get_host_interpreter(abi3_version: Option) -> Result { - let interpreter_path = find_interpreter()?; - - let mut interpreter_config = InterpreterConfig::from_interpreter(interpreter_path)?; - interpreter_config.fixup_for_abi3_version(abi3_version)?; - - Ok(interpreter_config) -} - -/// Generates an interpreter config suitable for cross-compilation. -/// -/// This must be called from PyO3's build script, because it relies on environment variables such as -/// CARGO_CFG_TARGET_OS which aren't available at any other time. -pub fn make_cross_compile_config() -> Result> { - let interpreter_config = if let Some(cross_config) = cross_compiling_from_cargo_env()? { - let mut interpreter_config = load_cross_compile_config(cross_config)?; - interpreter_config.fixup_for_abi3_version(get_abi3_version())?; - Some(interpreter_config) - } else { - None - }; - - Ok(interpreter_config) -} - -/// Generates an interpreter config which will be hard-coded into the pyo3-build-config crate. -/// Only used by `pyo3-build-config` build script. -#[allow(dead_code, unused_mut)] -pub fn make_interpreter_config() -> Result { - let host = Triple::host(); - let abi3_version = get_abi3_version(); - - // See if we can safely skip the Python interpreter configuration detection. - // Unix "abi3" extension modules can usually be built without any interpreter. - let need_interpreter = abi3_version.is_none() || require_libdir_for_target(&host); - - if have_python_interpreter() { - match get_host_interpreter(abi3_version) { - Ok(interpreter_config) => return Ok(interpreter_config), - // Bail if the interpreter configuration is required to build. - Err(e) if need_interpreter => return Err(e), - _ => { - // Fall back to the "abi3" defaults just as if `PYO3_NO_PYTHON` - // environment variable was set. - warn!("Compiling without a working Python interpreter."); - } - } - } else { - ensure!( - abi3_version.is_some(), - "An abi3-py3* feature must be specified when compiling without a Python interpreter." - ); - }; - - let mut interpreter_config = default_abi3_config(&host, abi3_version.unwrap()); - - // Auto generate python3.dll import libraries for Windows targets. - #[cfg(feature = "python3-dll-a")] - { - let py_version = if interpreter_config.abi3 { - None - } else { - Some(interpreter_config.version) - }; - interpreter_config.lib_dir = self::import_lib::generate_import_lib( - &host, - interpreter_config.implementation, - py_version, - )?; - } - - Ok(interpreter_config) -} - -fn escape(bytes: &[u8]) -> String { - let mut escaped = String::with_capacity(2 * bytes.len()); - - for byte in bytes { - const LUT: &[u8; 16] = b"0123456789abcdef"; - - escaped.push(LUT[(byte >> 4) as usize] as char); - escaped.push(LUT[(byte & 0x0F) as usize] as char); - } - - escaped -} - -fn unescape(escaped: &str) -> Vec { - assert!(escaped.len() % 2 == 0, "invalid hex encoding"); - - let mut bytes = Vec::with_capacity(escaped.len() / 2); - - for chunk in escaped.as_bytes().chunks_exact(2) { - fn unhex(hex: u8) -> u8 { - match hex { - b'a'..=b'f' => hex - b'a' + 10, - b'0'..=b'9' => hex - b'0', - _ => panic!("invalid hex encoding"), - } - } - - bytes.push(unhex(chunk[0]) << 4 | unhex(chunk[1])); - } - - bytes -} - -#[cfg(test)] -mod tests { - use target_lexicon::triple; - - use super::*; - - #[test] - fn test_config_file_roundtrip() { - let config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: Some(32), - executable: Some("executable".into()), - implementation: PythonImplementation::CPython, - lib_name: Some("lib_name".into()), - lib_dir: Some("lib_dir".into()), - shared: true, - version: MINIMUM_SUPPORTED_VERSION, - suppress_build_script_link_lines: true, - extra_build_script_lines: vec!["cargo:test1".to_string(), "cargo:test2".to_string()], - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - - // And some different options, for variety - - let config = InterpreterConfig { - abi3: false, - build_flags: { - let mut flags = HashSet::new(); - flags.insert(BuildFlag::Py_DEBUG); - flags.insert(BuildFlag::Other(String::from("Py_SOME_FLAG"))); - BuildFlags(flags) - }, - pointer_width: None, - executable: None, - implementation: PythonImplementation::PyPy, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { - major: 3, - minor: 10, - }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - } - - #[test] - fn test_config_file_roundtrip_with_escaping() { - let config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: Some(32), - executable: Some("executable".into()), - implementation: PythonImplementation::CPython, - lib_name: Some("lib_name".into()), - lib_dir: Some("lib_dir\\n".into()), - shared: true, - version: MINIMUM_SUPPORTED_VERSION, - suppress_build_script_link_lines: true, - extra_build_script_lines: vec!["cargo:test1".to_string(), "cargo:test2".to_string()], - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - let buf = unescape(&escape(&buf)); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - } - - #[test] - fn test_config_file_defaults() { - // Only version is required - assert_eq!( - InterpreterConfig::from_reader("version=3.7".as_bytes()).unwrap(), - InterpreterConfig { - version: PythonVersion { major: 3, minor: 7 }, - implementation: PythonImplementation::CPython, - shared: true, - abi3: false, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ) - } - - #[test] - fn test_config_file_unknown_keys() { - // ext_suffix is unknown to pyo3-build-config, but it shouldn't error - assert_eq!( - InterpreterConfig::from_reader("version=3.7\next_suffix=.python37.so".as_bytes()) - .unwrap(), - InterpreterConfig { - version: PythonVersion { major: 3, minor: 7 }, - implementation: PythonImplementation::CPython, - shared: true, - abi3: false, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ) - } - - #[test] - fn build_flags_default() { - assert_eq!(BuildFlags::default(), BuildFlags::new()); - } - - #[test] - fn build_flags_from_sysconfigdata() { - let mut sysconfigdata = Sysconfigdata::new(); - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - HashSet::new() - ); - - for flag in &BuildFlags::ALL { - sysconfigdata.insert(flag.to_string(), "0".into()); - } - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - HashSet::new() - ); - - let mut expected_flags = HashSet::new(); - for flag in &BuildFlags::ALL { - sysconfigdata.insert(flag.to_string(), "1".into()); - expected_flags.insert(flag.clone()); - } - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - expected_flags - ); - } - - #[test] - fn build_flags_fixup() { - let mut build_flags = BuildFlags::new(); - - build_flags = build_flags.fixup(); - assert!(build_flags.0.is_empty()); - - build_flags.0.insert(BuildFlag::Py_DEBUG); - - build_flags = build_flags.fixup(); - - // Py_DEBUG implies Py_REF_DEBUG - assert!(build_flags.0.contains(&BuildFlag::Py_REF_DEBUG)); - } - - #[test] - fn parse_script_output() { - let output = "foo bar\nbar foobar\n\n"; - let map = super::parse_script_output(output); - assert_eq!(map.len(), 2); - assert_eq!(map["foo"], "bar"); - assert_eq!(map["bar"], "foobar"); - } - - #[test] - fn config_from_interpreter() { - // Smoke test to just see whether this works - // - // PyO3's CI is dependent on Python being installed, so this should be reliable. - assert!(make_interpreter_config().is_ok()) - } - - #[test] - fn config_from_empty_sysconfigdata() { - let sysconfigdata = Sysconfigdata::new(); - assert!(InterpreterConfig::from_sysconfigdata(&sysconfigdata).is_err()); - } - - #[test] - fn config_from_sysconfigdata() { - let mut sysconfigdata = Sysconfigdata::new(); - // these are the minimal values required such that InterpreterConfig::from_sysconfigdata - // does not error - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - sysconfigdata.insert("Py_ENABLE_SHARED", "1"); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: true, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn config_from_sysconfigdata_framework() { - let mut sysconfigdata = Sysconfigdata::new(); - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - // PYTHONFRAMEWORK should override Py_ENABLE_SHARED - sysconfigdata.insert("Py_ENABLE_SHARED", "0"); - sysconfigdata.insert("PYTHONFRAMEWORK", "Python"); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: true, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - - sysconfigdata = Sysconfigdata::new(); - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - // An empty PYTHONFRAMEWORK means it is not a framework - sysconfigdata.insert("Py_ENABLE_SHARED", "0"); - sysconfigdata.insert("PYTHONFRAMEWORK", ""); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: false, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn windows_hardcoded_abi3_compile() { - let host = triple!("x86_64-pc-windows-msvc"); - let min_version = "3.7".parse().unwrap(); - - assert_eq!( - default_abi3_config(&host, min_version), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: true, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn unix_hardcoded_abi3_compile() { - let host = triple!("x86_64-unknown-linux-gnu"); - let min_version = "3.9".parse().unwrap(); - - assert_eq!( - default_abi3_config(&host, min_version), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: true, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn windows_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("C:\\some\\path".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.7".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("i686-pc-windows-msvc"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: false, - lib_name: Some("python37".into()), - lib_dir: Some("C:\\some\\path".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn mingw_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("/usr/lib/mingw".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.8".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("i686-pc-windows-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 8 }, - shared: true, - abi3: false, - lib_name: Some("python38".into()), - lib_dir: Some("/usr/lib/mingw".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn unix_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("/usr/arm64/lib".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.9".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("aarch64-unknown-linux-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: false, - lib_name: Some("python3.9".into()), - lib_dir: Some("/usr/arm64/lib".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn pypy_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_implementation: Some("PyPy".into()), - pyo3_cross_python_version: Some("3.10".into()), - }; - - let triple = triple!("x86_64-unknown-linux-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &triple, &triple) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::PyPy, - version: PythonVersion { - major: 3, - minor: 10 - }, - shared: true, - abi3: false, - lib_name: Some("pypy3.10-c".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ); - } - - #[test] - fn default_lib_name_windows() { - use PythonImplementation::*; - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - false, - false, - ), - "python39", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - false, - false, - ), - "python3", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - true, - false, - ), - "python3.9", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - true, - false, - ), - "python3", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - PyPy, - true, - false, - false, - ), - "python39", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - false, - true, - ), - "python39_d", - ); - // abi3 debug builds on windows use version-specific lib - // to workaround https://github.com/python/cpython/issues/101614 - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - false, - true, - ), - "python39_d", - ); - } - - #[test] - fn default_lib_name_unix() { - use PythonImplementation::*; - // Defaults to python3.7m for CPython 3.7 - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 7 }, CPython, None), - "python3.7m", - ); - // Defaults to pythonX.Y for CPython 3.8+ - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 8 }, CPython, None), - "python3.8", - ); - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 9 }, CPython, None), - "python3.9", - ); - // Can use ldversion to override for CPython - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 9 }, - CPython, - Some("3.7md") - ), - "python3.7md", - ); - - // PyPy 3.9 includes ldversion - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 9 }, PyPy, None), - "pypy3.9-c", - ); - - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 9 }, PyPy, Some("3.9d")), - "pypy3.9d-c", - ); - } - - #[test] - fn parse_cross_python_version() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: Some("3.9".into()), - pyo3_cross_python_implementation: None, - }; - - assert_eq!( - env_vars.parse_version().unwrap(), - Some(PythonVersion { major: 3, minor: 9 }) - ); - - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: None, - pyo3_cross_python_implementation: None, - }; - - assert_eq!(env_vars.parse_version().unwrap(), None); - - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: Some("100".into()), - pyo3_cross_python_implementation: None, - }; - - assert!(env_vars.parse_version().is_err()); - } - - #[test] - fn interpreter_version_reduced_to_abi3() { - let mut config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: None, - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { major: 3, minor: 7 }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - - config - .fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 7 })) - .unwrap(); - assert_eq!(config.version, PythonVersion { major: 3, minor: 7 }); - } - - #[test] - fn abi3_version_cannot_be_higher_than_interpreter() { - let mut config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::new(), - pointer_width: None, - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { major: 3, minor: 7 }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - - assert!(config - .fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 8 })) - .unwrap_err() - .to_string() - .contains( - "cannot set a minimum Python version 3.8 higher than the interpreter version 3.7" - )); - } - - #[test] - #[cfg(all( - target_os = "linux", - target_arch = "x86_64", - feature = "resolve-config" - ))] - fn parse_sysconfigdata() { - // A best effort attempt to get test coverage for the sysconfigdata parsing. - // Might not complete successfully depending on host installation; that's ok as long as - // CI demonstrates this path is covered! - - let interpreter_config = crate::get(); - - let lib_dir = match &interpreter_config.lib_dir { - Some(lib_dir) => Path::new(lib_dir), - // Don't know where to search for sysconfigdata; never mind. - None => return, - }; - - let cross = CrossCompileConfig { - lib_dir: Some(lib_dir.into()), - version: Some(interpreter_config.version), - implementation: Some(interpreter_config.implementation), - target: triple!("x86_64-unknown-linux-gnu"), - }; - - let sysconfigdata_path = match find_sysconfigdata(&cross) { - Ok(Some(path)) => path, - // Couldn't find a matching sysconfigdata; never mind! - _ => return, - }; - let sysconfigdata = super::parse_sysconfigdata(sysconfigdata_path).unwrap(); - let parsed_config = InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(); - - assert_eq!( - parsed_config, - InterpreterConfig { - abi3: false, - build_flags: BuildFlags(interpreter_config.build_flags.0.clone()), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: interpreter_config.lib_dir.to_owned(), - lib_name: interpreter_config.lib_name.to_owned(), - shared: true, - version: interpreter_config.version, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - } - ) - } - - #[test] - fn test_venv_interpreter() { - let base = OsStr::new("base"); - assert_eq!( - venv_interpreter(base, true), - PathBuf::from_iter(&["base", "Scripts", "python.exe"]) - ); - assert_eq!( - venv_interpreter(base, false), - PathBuf::from_iter(&["base", "bin", "python"]) - ); - } - - #[test] - fn test_conda_env_interpreter() { - let base = OsStr::new("base"); - assert_eq!( - conda_env_interpreter(base, true), - PathBuf::from_iter(&["base", "python.exe"]) - ); - assert_eq!( - conda_env_interpreter(base, false), - PathBuf::from_iter(&["base", "bin", "python"]) - ); - } - - #[test] - fn test_not_cross_compiling_from_to() { - assert!(cross_compiling_from_to( - &triple!("x86_64-unknown-linux-gnu"), - &triple!("x86_64-unknown-linux-gnu"), - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-apple-darwin"), - &triple!("x86_64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("aarch64-apple-darwin"), - &triple!("x86_64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-apple-darwin"), - &triple!("aarch64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-pc-windows-msvc"), - &triple!("i686-pc-windows-msvc") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-unknown-linux-gnu"), - &triple!("x86_64-unknown-linux-musl") - ) - .unwrap() - .is_none()); - } - - #[test] - fn test_run_python_script() { - // as above, this should be okay in CI where Python is presumed installed - let interpreter = make_interpreter_config() - .expect("could not get InterpreterConfig from installed interpreter"); - let out = interpreter - .run_python_script("print(2 + 2)") - .expect("failed to run Python script"); - assert_eq!(out.trim_end(), "4"); - } - - #[test] - fn test_run_python_script_with_envs() { - // as above, this should be okay in CI where Python is presumed installed - let interpreter = make_interpreter_config() - .expect("could not get InterpreterConfig from installed interpreter"); - let out = interpreter - .run_python_script_with_envs( - "import os; print(os.getenv('PYO3_TEST'))", - vec![("PYO3_TEST", "42")], - ) - .expect("failed to run Python script"); - assert_eq!(out.trim_end(), "42"); - } - - #[test] - fn test_build_script_outputs_base() { - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - ] - ); - - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::PyPy, - ..interpreter_config - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=PyPy".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_abi3() { - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: true, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=Py_LIMITED_API".to_owned(), - ] - ); - - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::PyPy, - ..interpreter_config - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=PyPy".to_owned(), - "cargo:rustc-cfg=Py_LIMITED_API".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_gil_disabled() { - let mut build_flags = BuildFlags::default(); - build_flags.0.insert(BuildFlag::Py_GIL_DISABLED); - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { - major: 3, - minor: 13, - }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=Py_3_10".to_owned(), - "cargo:rustc-cfg=Py_3_11".to_owned(), - "cargo:rustc-cfg=Py_3_12".to_owned(), - "cargo:rustc-cfg=Py_3_13".to_owned(), - "cargo:rustc-cfg=Py_GIL_DISABLED".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_debug() { - let mut build_flags = BuildFlags::default(); - build_flags.0.insert(BuildFlag::Py_DEBUG); - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=py_sys_config=\"Py_DEBUG\"".to_owned(), - ] - ); - } - - #[test] - fn test_find_sysconfigdata_in_invalid_lib_dir() { - let e = find_all_sysconfigdata(&CrossCompileConfig { - lib_dir: Some(PathBuf::from("/abc/123/not/a/real/path")), - version: None, - implementation: None, - target: triple!("x86_64-unknown-linux-gnu"), - }) - .unwrap_err(); - - // actual error message is platform-dependent, so just check the context we add - assert!(e.report().to_string().starts_with( - "failed to search the lib dir at 'PYO3_CROSS_LIB_DIR=/abc/123/not/a/real/path'\n\ - caused by:\n \ - - 0: failed to list the entries in '/abc/123/not/a/real/path'\n \ - - 1: \ - " - )); - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/src/import_lib.rs b/vendor/orjson/include/pyo3/pyo3-build-config/src/import_lib.rs deleted file mode 100644 index 0925a861b5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/src/import_lib.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! Optional `python3.dll` import library generator for Windows - -use std::env; -use std::path::PathBuf; - -use python3_dll_a::ImportLibraryGenerator; -use target_lexicon::{Architecture, OperatingSystem, Triple}; - -use super::{PythonImplementation, PythonVersion}; -use crate::errors::{Context, Error, Result}; - -/// Generates the `python3.dll` or `pythonXY.dll` import library for Windows targets. -/// -/// Places the generated import library into the build script output directory -/// and returns the full library directory path. -/// -/// Does nothing if the target OS is not Windows. -pub(super) fn generate_import_lib( - target: &Triple, - py_impl: PythonImplementation, - py_version: Option, -) -> Result> { - if target.operating_system != OperatingSystem::Windows { - return Ok(None); - } - - let out_dir = - env::var_os("OUT_DIR").expect("generate_import_lib() must be called from a build script"); - - // Put the newly created import library into the build script output directory. - let mut out_lib_dir = PathBuf::from(out_dir); - out_lib_dir.push("lib"); - - // Convert `Architecture` enum to rustc `target_arch` option format. - let arch = match target.architecture { - // i686, i586, etc. - Architecture::X86_32(_) => "x86".to_string(), - other => other.to_string(), - }; - - let env = target.environment.to_string(); - let implementation = match py_impl { - PythonImplementation::CPython => python3_dll_a::PythonImplementation::CPython, - PythonImplementation::PyPy => python3_dll_a::PythonImplementation::PyPy, - PythonImplementation::GraalPy => { - return Err(Error::from("No support for GraalPy on Windows")) - } - }; - - ImportLibraryGenerator::new(&arch, &env) - .version(py_version.map(|v| (v.major, v.minor))) - .implementation(implementation) - .generate(&out_lib_dir) - .context("failed to generate python3.dll import library")?; - - let out_lib_dir_string = out_lib_dir - .to_str() - .ok_or("build directory is not a valid UTF-8 string")? - .to_owned(); - - Ok(Some(out_lib_dir_string)) -} diff --git a/vendor/orjson/include/pyo3/pyo3-build-config/src/lib.rs b/vendor/orjson/include/pyo3/pyo3-build-config/src/lib.rs deleted file mode 100644 index 6e295c9968..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-build-config/src/lib.rs +++ /dev/null @@ -1,295 +0,0 @@ -//! Configuration used by PyO3 for conditional support of varying Python versions. -//! -//! This crate exposes functionality to be called from build scripts to simplify building crates -//! which depend on PyO3. -//! -//! It used internally by the PyO3 crate's build script to apply the same configuration. - -#![warn(elided_lifetimes_in_paths, unused_lifetimes)] - -mod errors; -mod impl_; - -#[cfg(feature = "resolve-config")] -use std::{ - io::Cursor, - path::{Path, PathBuf}, -}; - -use std::{env, process::Command, str::FromStr}; - -use once_cell::sync::OnceCell; - -pub use impl_::{ - cross_compiling_from_to, find_all_sysconfigdata, parse_sysconfigdata, BuildFlag, BuildFlags, - CrossCompileConfig, InterpreterConfig, PythonImplementation, PythonVersion, Triple, -}; -use target_lexicon::OperatingSystem; - -/// Adds all the [`#[cfg]` flags](index.html) to the current compilation. -/// -/// This should be called from a build script. -/// -/// The full list of attributes added are the following: -/// -/// | Flag | Description | -/// | ---- | ----------- | -/// | `#[cfg(Py_3_7)]`, `#[cfg(Py_3_8)]`, `#[cfg(Py_3_9)]`, `#[cfg(Py_3_10)]` | These attributes mark code only for a given Python version and up. For example, `#[cfg(Py_3_7)]` marks code which can run on Python 3.7 **and newer**. | -/// | `#[cfg(Py_LIMITED_API)]` | This marks code which is run when compiling with PyO3's `abi3` feature enabled. | -/// | `#[cfg(PyPy)]` | This marks code which is run when compiling for PyPy. | -/// | `#[cfg(GraalPy)]` | This marks code which is run when compiling for GraalPy. | -/// -/// For examples of how to use these attributes, -#[doc = concat!("[see PyO3's guide](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/building-and-distribution/multiple_python_versions.html)")] -/// . -#[cfg(feature = "resolve-config")] -pub fn use_pyo3_cfgs() { - print_expected_cfgs(); - for cargo_command in get().build_script_outputs() { - println!("{}", cargo_command) - } -} - -/// Adds linker arguments suitable for PyO3's `extension-module` feature. -/// -/// This should be called from a build script. -/// -/// The following link flags are added: -/// - macOS: `-undefined dynamic_lookup` -/// - wasm32-unknown-emscripten: `-sSIDE_MODULE=2 -sWASM_BIGINT` -/// -/// All other platforms currently are no-ops, however this may change as necessary -/// in future. -pub fn add_extension_module_link_args() { - _add_extension_module_link_args(&impl_::target_triple_from_env(), std::io::stdout()) -} - -fn _add_extension_module_link_args(triple: &Triple, mut writer: impl std::io::Write) { - if triple.operating_system == OperatingSystem::Darwin { - writeln!(writer, "cargo:rustc-cdylib-link-arg=-undefined").unwrap(); - writeln!(writer, "cargo:rustc-cdylib-link-arg=dynamic_lookup").unwrap(); - } else if triple == &Triple::from_str("wasm32-unknown-emscripten").unwrap() { - writeln!(writer, "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2").unwrap(); - writeln!(writer, "cargo:rustc-cdylib-link-arg=-sWASM_BIGINT").unwrap(); - } -} - -/// Loads the configuration determined from the build environment. -/// -/// Because this will never change in a given compilation run, this is cached in a `once_cell`. -#[cfg(feature = "resolve-config")] -pub fn get() -> &'static InterpreterConfig { - static CONFIG: OnceCell = OnceCell::new(); - CONFIG.get_or_init(|| { - // Check if we are in a build script and cross compiling to a different target. - let cross_compile_config_path = resolve_cross_compile_config_path(); - let cross_compiling = cross_compile_config_path - .as_ref() - .map(|path| path.exists()) - .unwrap_or(false); - - // CONFIG_FILE is generated in build.rs, so it's content can vary - #[allow(unknown_lints, clippy::const_is_empty)] - if let Some(interpreter_config) = InterpreterConfig::from_cargo_dep_env() { - interpreter_config - } else if !CONFIG_FILE.is_empty() { - InterpreterConfig::from_reader(Cursor::new(CONFIG_FILE)) - } else if cross_compiling { - InterpreterConfig::from_path(cross_compile_config_path.as_ref().unwrap()) - } else { - InterpreterConfig::from_reader(Cursor::new(HOST_CONFIG)) - } - .expect("failed to parse PyO3 config") - }) -} - -/// Build configuration provided by `PYO3_CONFIG_FILE`. May be empty if env var not set. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -const CONFIG_FILE: &str = include_str!(concat!(env!("OUT_DIR"), "/pyo3-build-config-file.txt")); - -/// Build configuration discovered by `pyo3-build-config` build script. Not aware of -/// cross-compilation settings. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -const HOST_CONFIG: &str = include_str!(concat!(env!("OUT_DIR"), "/pyo3-build-config.txt")); - -/// Returns the path where PyO3's build.rs writes its cross compile configuration. -/// -/// The config file will be named `$OUT_DIR//pyo3-build-config.txt`. -/// -/// Must be called from a build script, returns `None` if not. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -fn resolve_cross_compile_config_path() -> Option { - env::var_os("TARGET").map(|target| { - let mut path = PathBuf::from(env!("OUT_DIR")); - path.push(Path::new(&target)); - path.push("pyo3-build-config.txt"); - path - }) -} - -/// Use certain features if we detect the compiler being used supports them. -/// -/// Features may be removed or added as MSRV gets bumped or new features become available, -/// so this function is unstable. -#[doc(hidden)] -pub fn print_feature_cfgs() { - let rustc_minor_version = rustc_minor_version().unwrap_or(0); - - // invalid_from_utf8 lint was added in Rust 1.74 - if rustc_minor_version >= 74 { - println!("cargo:rustc-cfg=invalid_from_utf8_lint"); - } - - if rustc_minor_version >= 79 { - println!("cargo:rustc-cfg=c_str_lit"); - } - - // Actually this is available on 1.78, but we should avoid - // https://github.com/rust-lang/rust/issues/124651 just in case - if rustc_minor_version >= 79 { - println!("cargo:rustc-cfg=diagnostic_namespace"); - } -} - -/// Registers `pyo3`s config names as reachable cfg expressions -/// -/// - -/// - -#[doc(hidden)] -pub fn print_expected_cfgs() { - if rustc_minor_version().map_or(false, |version| version < 80) { - // rustc 1.80.0 stabilized `rustc-check-cfg` feature, don't emit before - return; - } - - println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)"); - println!("cargo:rustc-check-cfg=cfg(Py_GIL_DISABLED)"); - println!("cargo:rustc-check-cfg=cfg(PyPy)"); - println!("cargo:rustc-check-cfg=cfg(GraalPy)"); - println!("cargo:rustc-check-cfg=cfg(py_sys_config, values(\"Py_DEBUG\", \"Py_REF_DEBUG\", \"Py_TRACE_REFS\", \"COUNT_ALLOCS\"))"); - println!("cargo:rustc-check-cfg=cfg(invalid_from_utf8_lint)"); - println!("cargo:rustc-check-cfg=cfg(pyo3_disable_reference_pool)"); - println!("cargo:rustc-check-cfg=cfg(pyo3_leak_on_drop_without_reference_pool)"); - println!("cargo:rustc-check-cfg=cfg(diagnostic_namespace)"); - println!("cargo:rustc-check-cfg=cfg(c_str_lit)"); - - // allow `Py_3_*` cfgs from the minimum supported version up to the - // maximum minor version (+1 for development for the next) - for i in impl_::MINIMUM_SUPPORTED_VERSION.minor..=impl_::ABI3_MAX_MINOR + 1 { - println!("cargo:rustc-check-cfg=cfg(Py_3_{i})"); - } - println!("cargo::rustc-check-cfg=cfg(Py_3_14)"); -} - -/// Private exports used in PyO3's build.rs -/// -/// Please don't use these - they could change at any time. -#[doc(hidden)] -pub mod pyo3_build_script_impl { - #[cfg(feature = "resolve-config")] - use crate::errors::{Context, Result}; - - #[cfg(feature = "resolve-config")] - use super::*; - - pub mod errors { - pub use crate::errors::*; - } - pub use crate::impl_::{ - cargo_env_var, env_var, is_linking_libpython, make_cross_compile_config, InterpreterConfig, - PythonVersion, - }; - - /// Gets the configuration for use from PyO3's build script. - /// - /// Differs from .get() above only in the cross-compile case, where PyO3's build script is - /// required to generate a new config (as it's the first build script which has access to the - /// correct value for CARGO_CFG_TARGET_OS). - #[cfg(feature = "resolve-config")] - pub fn resolve_interpreter_config() -> Result { - // CONFIG_FILE is generated in build.rs, so it's content can vary - #[allow(unknown_lints, clippy::const_is_empty)] - if !CONFIG_FILE.is_empty() { - let mut interperter_config = InterpreterConfig::from_reader(Cursor::new(CONFIG_FILE))?; - interperter_config.generate_import_libs()?; - Ok(interperter_config) - } else if let Some(interpreter_config) = make_cross_compile_config()? { - // This is a cross compile and need to write the config file. - let path = resolve_cross_compile_config_path() - .expect("resolve_interpreter_config() must be called from a build script"); - let parent_dir = path.parent().ok_or_else(|| { - format!( - "failed to resolve parent directory of config file {}", - path.display() - ) - })?; - std::fs::create_dir_all(parent_dir).with_context(|| { - format!( - "failed to create config file directory {}", - parent_dir.display() - ) - })?; - interpreter_config.to_writer(&mut std::fs::File::create(&path).with_context( - || format!("failed to create config file at {}", path.display()), - )?)?; - Ok(interpreter_config) - } else { - InterpreterConfig::from_reader(Cursor::new(HOST_CONFIG)) - } - } -} - -fn rustc_minor_version() -> Option { - static RUSTC_MINOR_VERSION: OnceCell> = OnceCell::new(); - *RUSTC_MINOR_VERSION.get_or_init(|| { - let rustc = env::var_os("RUSTC")?; - let output = Command::new(rustc).arg("--version").output().ok()?; - let version = core::str::from_utf8(&output.stdout).ok()?; - let mut pieces = version.split('.'); - if pieces.next() != Some("rustc 1") { - return None; - } - pieces.next()?.parse().ok() - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn extension_module_link_args() { - let mut buf = Vec::new(); - - // Does nothing on non-mac - _add_extension_module_link_args( - &Triple::from_str("x86_64-pc-windows-msvc").unwrap(), - &mut buf, - ); - assert_eq!(buf, Vec::new()); - - _add_extension_module_link_args( - &Triple::from_str("x86_64-apple-darwin").unwrap(), - &mut buf, - ); - assert_eq!( - std::str::from_utf8(&buf).unwrap(), - "cargo:rustc-cdylib-link-arg=-undefined\n\ - cargo:rustc-cdylib-link-arg=dynamic_lookup\n" - ); - - buf.clear(); - _add_extension_module_link_args( - &Triple::from_str("wasm32-unknown-emscripten").unwrap(), - &mut buf, - ); - assert_eq!( - std::str::from_utf8(&buf).unwrap(), - "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2\n\ - cargo:rustc-cdylib-link-arg=-sWASM_BIGINT\n" - ); - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS b/vendor/orjson/include/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS deleted file mode 100644 index 4502d7774e..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS +++ /dev/null @@ -1,6 +0,0 @@ -This is a Rust reimplementation of the CPython public header files as necessary -for binary compatibility, with additional metadata to support PyPy. - -For original implementations please see: - - https://github.com/python/cpython - - https://foss.heptapod.net/pypy/pypy diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/Cargo.toml b/vendor/orjson/include/pyo3/pyo3-ffi/Cargo.toml deleted file mode 100644 index e569ca1db5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/Cargo.toml +++ /dev/null @@ -1,44 +0,0 @@ -[package] -name = "pyo3-ffi" -version = "0.23.0-dev" -description = "Python-API bindings for the PyO3 ecosystem" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" -links = "python" - -[dependencies] -libc = "0.2.62" - -[features] - -default = [] - -# Use this feature when building an extension module. -# It tells the linker to keep the python symbols unresolved, -# so that the module can also be used with statically linked python interpreters. -extension-module = ["pyo3-build-config/extension-module"] - -# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more. -abi3 = ["pyo3-build-config/abi3"] - -# With abi3, we can manually set the minimum Python version. -abi3-py37 = ["abi3-py38", "pyo3-build-config/abi3-py37"] -abi3-py38 = ["abi3-py39", "pyo3-build-config/abi3-py38"] -abi3-py39 = ["abi3-py310", "pyo3-build-config/abi3-py39"] -abi3-py310 = ["abi3-py311", "pyo3-build-config/abi3-py310"] -abi3-py311 = ["abi3-py312", "pyo3-build-config/abi3-py311"] -abi3-py312 = ["abi3", "pyo3-build-config/abi3-py312"] - -# Automatically generates `python3.dll` import libraries for Windows targets. -generate-import-lib = ["pyo3-build-config/python3-dll-a"] - -[dev-dependencies] -paste = "1" - -[build-dependencies] -pyo3-build-config = { path = "../pyo3-build-config", version = "=0.23.0-dev", features = ["resolve-config"] } diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-APACHE b/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-MIT b/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/build.rs b/vendor/orjson/include/pyo3/pyo3-ffi/build.rs deleted file mode 100644 index b0f1c28d44..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/build.rs +++ /dev/null @@ -1,251 +0,0 @@ -use pyo3_build_config::{ - bail, ensure, print_feature_cfgs, - pyo3_build_script_impl::{ - cargo_env_var, env_var, errors::Result, is_linking_libpython, resolve_interpreter_config, - InterpreterConfig, PythonVersion, - }, - warn, BuildFlag, PythonImplementation, -}; -use std::ops::Not; - -/// Minimum Python version PyO3 supports. -struct SupportedVersions { - min: PythonVersion, - max: PythonVersion, -} - -const SUPPORTED_VERSIONS_CPYTHON: SupportedVersions = SupportedVersions { - min: PythonVersion { major: 3, minor: 7 }, - max: PythonVersion { - major: 3, - minor: 13, - }, -}; - -const SUPPORTED_VERSIONS_PYPY: SupportedVersions = SupportedVersions { - min: PythonVersion { major: 3, minor: 7 }, - max: PythonVersion { - major: 3, - minor: 10, - }, -}; - -const SUPPORTED_VERSIONS_GRAALPY: SupportedVersions = SupportedVersions { - min: PythonVersion { - major: 3, - minor: 10, - }, - max: PythonVersion { - major: 3, - minor: 11, - }, -}; - -fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> { - // This is an undocumented env var which is only really intended to be used in CI / for testing - // and development. - if std::env::var("UNSAFE_PYO3_SKIP_VERSION_CHECK").as_deref() == Ok("1") { - return Ok(()); - } - - match interpreter_config.implementation { - PythonImplementation::CPython => { - let versions = SUPPORTED_VERSIONS_CPYTHON; - ensure!( - interpreter_config.version >= versions.min, - "the configured Python interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - ensure!( - interpreter_config.version <= versions.max || env_var("PYO3_USE_ABI3_FORWARD_COMPATIBILITY").map_or(false, |os_str| os_str == "1"), - "the configured Python interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}\n\ - = help: set PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1 to suppress this check and build anyway using the stable ABI", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap(), - ); - } - PythonImplementation::PyPy => { - let versions = SUPPORTED_VERSIONS_PYPY; - ensure!( - interpreter_config.version >= versions.min, - "the configured PyPy interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - // PyO3 does not support abi3, so we cannot offer forward compatibility - ensure!( - interpreter_config.version <= versions.max, - "the configured PyPy interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap() - ); - } - PythonImplementation::GraalPy => { - let versions = SUPPORTED_VERSIONS_GRAALPY; - ensure!( - interpreter_config.version >= versions.min, - "the configured GraalPy interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - // GraalPy does not support abi3, so we cannot offer forward compatibility - ensure!( - interpreter_config.version <= versions.max, - "the configured GraalPy interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap() - ); - } - } - - if interpreter_config.abi3 { - match interpreter_config.implementation { - PythonImplementation::CPython => {} - PythonImplementation::PyPy => warn!( - "PyPy does not yet support abi3 so the build artifacts will be version-specific. \ - See https://foss.heptapod.net/pypy/pypy/-/issues/3397 for more information." - ), - PythonImplementation::GraalPy => warn!( - "GraalPy does not support abi3 so the build artifacts will be version-specific." - ), - } - } - - Ok(()) -} - -fn ensure_gil_enabled(interpreter_config: &InterpreterConfig) -> Result<()> { - let gil_enabled = interpreter_config - .build_flags - .0 - .contains(&BuildFlag::Py_GIL_DISABLED) - .not(); - ensure!( - gil_enabled || std::env::var("UNSAFE_PYO3_BUILD_FREE_THREADED").map_or(false, |os_str| os_str == "1"), - "the Python interpreter was built with the GIL disabled, which is not yet supported by PyO3\n\ - = help: see https://github.com/PyO3/pyo3/issues/4265 for more information\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}\n\ - = help: set UNSAFE_PYO3_BUILD_FREE_THREADED=1 to suppress this check and build anyway for free-threaded Python", - std::env::var("CARGO_PKG_VERSION").unwrap() - ); - if !gil_enabled && interpreter_config.abi3 { - warn!( - "The free-threaded build of CPython does not yet support abi3 so the build artifacts will be version-specific." - ) - } - - Ok(()) -} - -fn ensure_target_pointer_width(interpreter_config: &InterpreterConfig) -> Result<()> { - if let Some(pointer_width) = interpreter_config.pointer_width { - // Try to check whether the target architecture matches the python library - let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH") - .unwrap() - .as_str() - { - "64" => 64, - "32" => 32, - x => bail!("unexpected Rust target pointer width: {}", x), - }; - - ensure!( - rust_target == pointer_width, - "your Rust target architecture ({}-bit) does not match your python interpreter ({}-bit)", - rust_target, - pointer_width - ); - } - Ok(()) -} - -fn emit_link_config(interpreter_config: &InterpreterConfig) -> Result<()> { - let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap(); - - println!( - "cargo:rustc-link-lib={link_model}{alias}{lib_name}", - link_model = if interpreter_config.shared { - "" - } else { - "static=" - }, - alias = if target_os == "windows" { - "pythonXY:" - } else { - "" - }, - lib_name = interpreter_config.lib_name.as_ref().ok_or( - "attempted to link to Python shared library but config does not contain lib_name" - )?, - ); - - if let Some(lib_dir) = &interpreter_config.lib_dir { - println!("cargo:rustc-link-search=native={}", lib_dir); - } - - Ok(()) -} - -/// Prepares the PyO3 crate for compilation. -/// -/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX -/// for users. -/// -/// Emits the cargo configuration based on this config as well as a few checks of the Rust compiler -/// version to enable features which aren't supported on MSRV. -fn configure_pyo3() -> Result<()> { - let interpreter_config = resolve_interpreter_config()?; - - if env_var("PYO3_PRINT_CONFIG").map_or(false, |os_str| os_str == "1") { - print_config_and_exit(&interpreter_config); - } - - ensure_python_version(&interpreter_config)?; - ensure_target_pointer_width(&interpreter_config)?; - ensure_gil_enabled(&interpreter_config)?; - - // Serialize the whole interpreter config into DEP_PYTHON_PYO3_CONFIG env var. - interpreter_config.to_cargo_dep_env()?; - - if is_linking_libpython() && !interpreter_config.suppress_build_script_link_lines { - emit_link_config(&interpreter_config)?; - } - - for cfg in interpreter_config.build_script_outputs() { - println!("{}", cfg) - } - - // Extra lines come last, to support last write wins. - for line in &interpreter_config.extra_build_script_lines { - println!("{}", line); - } - - // Emit cfgs like `invalid_from_utf8_lint` - print_feature_cfgs(); - - Ok(()) -} - -fn print_config_and_exit(config: &InterpreterConfig) { - println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --"); - config - .to_writer(std::io::stdout()) - .expect("failed to print config to stdout"); - println!("\nnote: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config"); - std::process::exit(101); -} - -fn main() { - pyo3_build_config::print_expected_cfgs(); - if let Err(e) = configure_pyo3() { - eprintln!("error: {}", e.report()); - std::process::exit(1) - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/abstract_.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/abstract_.rs deleted file mode 100644 index 1899545011..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/abstract_.rs +++ /dev/null @@ -1,333 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -#[inline] -#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h -pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int { - PyObject_SetAttrString(o, attr_name, std::ptr::null_mut()) -} - -#[inline] -#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h -pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int { - PyObject_SetAttr(o, attr_name, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(all( - not(PyPy), - not(GraalPy), - any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10 - ))] - #[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")] - pub fn PyObject_CallNoArgs(func: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Call")] - pub fn PyObject_Call( - callable_object: *mut PyObject, - args: *mut PyObject, - kw: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallObject")] - pub fn PyObject_CallObject( - callable_object: *mut PyObject, - args: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunction")] - pub fn PyObject_CallFunction( - callable_object: *mut PyObject, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethod")] - pub fn PyObject_CallMethod( - o: *mut PyObject, - method: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallFunction_SizeT")] - pub fn _PyObject_CallFunction_SizeT( - callable_object: *mut PyObject, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallMethod_SizeT")] - pub fn _PyObject_CallMethod_SizeT( - o: *mut PyObject, - method: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunctionObjArgs")] - pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethodObjArgs")] - pub fn PyObject_CallMethodObjArgs( - o: *mut PyObject, - method: *mut PyObject, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Type")] - pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Size")] - pub fn PyObject_Size(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -pub unsafe fn PyObject_Length(o: *mut PyObject) -> Py_ssize_t { - PyObject_Size(o) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_GetItem")] - pub fn PyObject_GetItem(o: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetItem")] - pub fn PyObject_SetItem(o: *mut PyObject, key: *mut PyObject, v: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_DelItemString")] - pub fn PyObject_DelItemString(o: *mut PyObject, key: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_DelItem")] - pub fn PyObject_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_Format")] - pub fn PyObject_Format(obj: *mut PyObject, format_spec: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetIter")] - pub fn PyObject_GetIter(arg1: *mut PyObject) -> *mut PyObject; -} - -// Before 3.8 PyIter_Check was defined in CPython as a macro, -// but the implementation of that in PyO3 did not work, see -// https://github.com/PyO3/pyo3/pull/2914 -// -// This is a slow implementation which should function equivalently. -#[cfg(not(any(Py_3_8, PyPy)))] -#[inline] -pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int { - crate::PyObject_HasAttrString(crate::Py_TYPE(o).cast(), c_str!("__next__").as_ptr()) -} - -extern "C" { - #[cfg(any(Py_3_8, PyPy))] - #[cfg_attr(PyPy, link_name = "PyPyIter_Check")] - pub fn PyIter_Check(obj: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyIter_Next")] - pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject; - #[cfg(all(not(PyPy), Py_3_10))] - #[cfg_attr(PyPy, link_name = "PyPyIter_Send")] - pub fn PyIter_Send(iter: *mut PyObject, arg: *mut PyObject, presult: *mut *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPyNumber_Check")] - pub fn PyNumber_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Add")] - pub fn PyNumber_Add(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Subtract")] - pub fn PyNumber_Subtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Multiply")] - pub fn PyNumber_Multiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_MatrixMultiply")] - pub fn PyNumber_MatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_FloorDivide")] - pub fn PyNumber_FloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_TrueDivide")] - pub fn PyNumber_TrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Remainder")] - pub fn PyNumber_Remainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Divmod")] - pub fn PyNumber_Divmod(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Power")] - pub fn PyNumber_Power(o1: *mut PyObject, o2: *mut PyObject, o3: *mut PyObject) - -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Negative")] - pub fn PyNumber_Negative(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Positive")] - pub fn PyNumber_Positive(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Absolute")] - pub fn PyNumber_Absolute(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Invert")] - pub fn PyNumber_Invert(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Lshift")] - pub fn PyNumber_Lshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Rshift")] - pub fn PyNumber_Rshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_And")] - pub fn PyNumber_And(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Xor")] - pub fn PyNumber_Xor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Or")] - pub fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; -} - -// Defined as this macro in Python limited API, but relies on -// non-limited PyTypeObject. Don't expose this since it cannot be used. -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -#[inline] -pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int { - let tp_as_number = (*Py_TYPE(o)).tp_as_number; - (!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()) as c_int -} - -extern "C" { - #[cfg(any(all(Py_3_8, Py_LIMITED_API), PyPy))] - #[link_name = "PyPyIndex_Check"] - pub fn PyIndex_Check(o: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyNumber_Index")] - pub fn PyNumber_Index(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_AsSsize_t")] - pub fn PyNumber_AsSsize_t(o: *mut PyObject, exc: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Long")] - pub fn PyNumber_Long(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Float")] - pub fn PyNumber_Float(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAdd")] - pub fn PyNumber_InPlaceAdd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceSubtract")] - pub fn PyNumber_InPlaceSubtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMultiply")] - pub fn PyNumber_InPlaceMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMatrixMultiply")] - pub fn PyNumber_InPlaceMatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceFloorDivide")] - pub fn PyNumber_InPlaceFloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceTrueDivide")] - pub fn PyNumber_InPlaceTrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRemainder")] - pub fn PyNumber_InPlaceRemainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlacePower")] - pub fn PyNumber_InPlacePower( - o1: *mut PyObject, - o2: *mut PyObject, - o3: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceLshift")] - pub fn PyNumber_InPlaceLshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRshift")] - pub fn PyNumber_InPlaceRshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAnd")] - pub fn PyNumber_InPlaceAnd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceXor")] - pub fn PyNumber_InPlaceXor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceOr")] - pub fn PyNumber_InPlaceOr(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - pub fn PyNumber_ToBase(n: *mut PyObject, base: c_int) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPySequence_Check")] - pub fn PySequence_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_Size")] - pub fn PySequence_Size(o: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPySequence_Length"] - pub fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t { - PySequence_Size(o) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySequence_Concat")] - pub fn PySequence_Concat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_Repeat")] - pub fn PySequence_Repeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_GetItem")] - pub fn PySequence_GetItem(o: *mut PyObject, i: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_GetSlice")] - pub fn PySequence_GetSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_SetItem")] - pub fn PySequence_SetItem(o: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_DelItem")] - pub fn PySequence_DelItem(o: *mut PyObject, i: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_SetSlice")] - pub fn PySequence_SetSlice( - o: *mut PyObject, - i1: Py_ssize_t, - i2: Py_ssize_t, - v: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_DelSlice")] - pub fn PySequence_DelSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_Tuple")] - pub fn PySequence_Tuple(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_List")] - pub fn PySequence_List(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_Fast")] - pub fn PySequence_Fast(o: *mut PyObject, m: *const c_char) -> *mut PyObject; - // skipped PySequence_Fast_GET_SIZE - // skipped PySequence_Fast_GET_ITEM - // skipped PySequence_Fast_GET_ITEMS - pub fn PySequence_Count(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPySequence_Contains")] - pub fn PySequence_Contains(seq: *mut PyObject, ob: *mut PyObject) -> c_int; -} - -#[inline] -pub unsafe fn PySequence_In(o: *mut PyObject, value: *mut PyObject) -> c_int { - PySequence_Contains(o, value) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySequence_Index")] - pub fn PySequence_Index(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceConcat")] - pub fn PySequence_InPlaceConcat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceRepeat")] - pub fn PySequence_InPlaceRepeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Check")] - pub fn PyMapping_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Size")] - pub fn PyMapping_Size(o: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPyMapping_Length"] - pub fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t { - PyMapping_Size(o) -} - -#[inline] -pub unsafe fn PyMapping_DelItemString(o: *mut PyObject, key: *mut c_char) -> c_int { - PyObject_DelItemString(o, key) -} - -#[inline] -pub unsafe fn PyMapping_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int { - PyObject_DelItem(o, key) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKeyString")] - pub fn PyMapping_HasKeyString(o: *mut PyObject, key: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKey")] - pub fn PyMapping_HasKey(o: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Keys")] - pub fn PyMapping_Keys(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Values")] - pub fn PyMapping_Values(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Items")] - pub fn PyMapping_Items(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_GetItemString")] - pub fn PyMapping_GetItemString(o: *mut PyObject, key: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_SetItemString")] - pub fn PyMapping_SetItemString( - o: *mut PyObject, - key: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsInstance")] - pub fn PyObject_IsInstance(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsSubclass")] - pub fn PyObject_IsSubclass(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/bltinmodule.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/bltinmodule.rs deleted file mode 100644 index cd5be0439a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/bltinmodule.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::object::PyTypeObject; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyFilter_Type: PyTypeObject; - pub static mut PyMap_Type: PyTypeObject; - pub static mut PyZip_Type: PyTypeObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/boolobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/boolobject.rs deleted file mode 100644 index eec9da707a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/boolobject.rs +++ /dev/null @@ -1,60 +0,0 @@ -#[cfg(not(GraalPy))] -use crate::longobject::PyLongObject; -use crate::object::*; -use std::os::raw::{c_int, c_long}; -use std::ptr::addr_of_mut; - -#[inline] -pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyBool_Type)) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_FalseStruct")] - static mut _Py_FalseStruct: PyLongObject; - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_TrueStruct")] - static mut _Py_TrueStruct: PyLongObject; - - #[cfg(GraalPy)] - static mut _Py_FalseStructReference: *mut PyObject; - #[cfg(GraalPy)] - static mut _Py_TrueStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_False() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_FalseStruct) as *mut PyObject; - #[cfg(GraalPy)] - return _Py_FalseStructReference; -} - -#[inline] -pub unsafe fn Py_True() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_TrueStruct) as *mut PyObject; - #[cfg(GraalPy)] - return _Py_TrueStructReference; -} - -#[inline] -pub unsafe fn Py_IsTrue(x: *mut PyObject) -> c_int { - Py_Is(x, Py_True()) -} - -#[inline] -pub unsafe fn Py_IsFalse(x: *mut PyObject) -> c_int { - Py_Is(x, Py_False()) -} - -// skipped Py_RETURN_TRUE -// skipped Py_RETURN_FALSE - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBool_FromLong")] - pub fn PyBool_FromLong(arg1: c_long) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/bytearrayobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/bytearrayobject.rs deleted file mode 100644 index 24a97bcc31..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/bytearrayobject.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -#[repr(C)] -pub struct PyByteArrayObject { - pub ob_base: PyVarObject, - pub ob_alloc: Py_ssize_t, - pub ob_bytes: *mut c_char, - pub ob_start: *mut c_char, - #[cfg(Py_3_9)] - pub ob_exports: Py_ssize_t, - #[cfg(not(Py_3_9))] - pub ob_exports: c_int, -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -opaque_struct!(PyByteArrayObject); - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Type")] - pub static mut PyByteArray_Type: PyTypeObject; - - pub static mut PyByteArrayIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyByteArray_Type)) -} - -#[inline] -pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyByteArray_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyByteArray_FromObject")] - pub fn PyByteArray_FromObject(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Concat")] - pub fn PyByteArray_Concat(a: *mut PyObject, b: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_FromStringAndSize")] - pub fn PyByteArray_FromStringAndSize(string: *const c_char, len: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Size")] - pub fn PyByteArray_Size(bytearray: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_AsString")] - pub fn PyByteArray_AsString(bytearray: *mut PyObject) -> *mut c_char; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Resize")] - pub fn PyByteArray_Resize(bytearray: *mut PyObject, len: Py_ssize_t) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/bytesobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/bytesobject.rs deleted file mode 100644 index 0fd327b6fc..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/bytesobject.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBytes_Type")] - pub static mut PyBytes_Type: PyTypeObject; - pub static mut PyBytesIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS) -} - -#[inline] -pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyBytes_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromStringAndSize")] - pub fn PyBytes_FromStringAndSize(arg1: *const c_char, arg2: Py_ssize_t) -> *mut PyObject; - pub fn PyBytes_FromString(arg1: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromObject")] - pub fn PyBytes_FromObject(arg1: *mut PyObject) -> *mut PyObject; - // skipped PyBytes_FromFormatV - //#[cfg_attr(PyPy, link_name = "PyPyBytes_FromFormatV")] - //pub fn PyBytes_FromFormatV(arg1: *const c_char, arg2: va_list) - // -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromFormat")] - pub fn PyBytes_FromFormat(arg1: *const c_char, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_Size")] - pub fn PyBytes_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBytes_AsString")] - pub fn PyBytes_AsString(arg1: *mut PyObject) -> *mut c_char; - pub fn PyBytes_Repr(arg1: *mut PyObject, arg2: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_Concat")] - pub fn PyBytes_Concat(arg1: *mut *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyBytes_ConcatAndDel")] - pub fn PyBytes_ConcatAndDel(arg1: *mut *mut PyObject, arg2: *mut PyObject); - pub fn PyBytes_DecodeEscape( - arg1: *const c_char, - arg2: Py_ssize_t, - arg3: *const c_char, - arg4: Py_ssize_t, - arg5: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_AsStringAndSize")] - pub fn PyBytes_AsStringAndSize( - obj: *mut PyObject, - s: *mut *mut c_char, - len: *mut Py_ssize_t, - ) -> c_int; -} - -// skipped F_LJUST -// skipped F_SIGN -// skipped F_BLANK -// skipped F_ALT -// skipped F_ZERO diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/ceval.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/ceval.rs deleted file mode 100644 index d1839a108c..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/ceval.rs +++ /dev/null @@ -1,139 +0,0 @@ -use crate::object::PyObject; -use crate::pystate::PyThreadState; -use std::os::raw::{c_char, c_int, c_void}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyEval_EvalCode")] - pub fn PyEval_EvalCode( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - - pub fn PyEval_EvalCodeEx( - co: *mut PyObject, - globals: *mut PyObject, - locals: *mut PyObject, - args: *const *mut PyObject, - argc: c_int, - kwds: *const *mut PyObject, - kwdc: c_int, - defs: *const *mut PyObject, - defc: c_int, - kwdefs: *mut PyObject, - closure: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallObjectWithKeywords")] - pub fn PyEval_CallObjectWithKeywords( - func: *mut PyObject, - obj: *mut PyObject, - kwargs: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(not(Py_3_13))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyEval_CallObject(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { - #[allow(deprecated)] - PyEval_CallObjectWithKeywords(func, arg, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallFunction")] - pub fn PyEval_CallFunction(obj: *mut PyObject, format: *const c_char, ...) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallMethod")] - pub fn PyEval_CallMethod( - obj: *mut PyObject, - methodname: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetBuiltins")] - pub fn PyEval_GetBuiltins() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetGlobals")] - pub fn PyEval_GetGlobals() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetLocals")] - pub fn PyEval_GetLocals() -> *mut PyObject; - pub fn PyEval_GetFrame() -> *mut crate::PyFrameObject; - #[cfg_attr(PyPy, link_name = "PyPy_AddPendingCall")] - pub fn Py_AddPendingCall( - func: Option c_int>, - arg: *mut c_void, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPy_MakePendingCalls")] - pub fn Py_MakePendingCalls() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPy_SetRecursionLimit")] - pub fn Py_SetRecursionLimit(arg1: c_int); - #[cfg_attr(PyPy, link_name = "PyPy_GetRecursionLimit")] - pub fn Py_GetRecursionLimit() -> c_int; - fn _Py_CheckRecursiveCall(_where: *mut c_char) -> c_int; -} - -extern "C" { - #[cfg(Py_3_9)] - #[cfg_attr(PyPy, link_name = "PyPy_EnterRecursiveCall")] - pub fn Py_EnterRecursiveCall(arg1: *const c_char) -> c_int; - #[cfg(Py_3_9)] - #[cfg_attr(PyPy, link_name = "PyPy_LeaveRecursiveCall")] - pub fn Py_LeaveRecursiveCall(); -} - -extern "C" { - pub fn PyEval_GetFuncName(arg1: *mut PyObject) -> *const c_char; - pub fn PyEval_GetFuncDesc(arg1: *mut PyObject) -> *const c_char; - pub fn PyEval_GetCallStats(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyEval_EvalFrame(arg1: *mut crate::PyFrameObject) -> *mut PyObject; - pub fn PyEval_EvalFrameEx(f: *mut crate::PyFrameObject, exc: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_SaveThread")] - pub fn PyEval_SaveThread() -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyEval_RestoreThread")] - pub fn PyEval_RestoreThread(arg1: *mut PyThreadState); -} - -extern "C" { - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "PyPyEval_ThreadsInitialized")] - #[cfg_attr( - Py_3_9, - deprecated( - note = "Deprecated in Python 3.9, this function always returns true in Python 3.7 or newer." - ) - )] - pub fn PyEval_ThreadsInitialized() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyEval_InitThreads")] - #[cfg_attr( - Py_3_9, - deprecated( - note = "Deprecated in Python 3.9, this function does nothing in Python 3.7 or newer." - ) - )] - pub fn PyEval_InitThreads(); - pub fn PyEval_AcquireLock(); - pub fn PyEval_ReleaseLock(); - #[cfg_attr(PyPy, link_name = "PyPyEval_AcquireThread")] - pub fn PyEval_AcquireThread(tstate: *mut PyThreadState); - #[cfg_attr(PyPy, link_name = "PyPyEval_ReleaseThread")] - pub fn PyEval_ReleaseThread(tstate: *mut PyThreadState); - #[cfg(not(Py_3_8))] - pub fn PyEval_ReInitThreads(); -} - -// skipped Py_BEGIN_ALLOW_THREADS -// skipped Py_BLOCK_THREADS -// skipped Py_UNBLOCK_THREADS -// skipped Py_END_ALLOW_THREADS -// skipped FVC_MASK -// skipped FVC_NONE -// skipped FVC_STR -// skipped FVC_REPR -// skipped FVC_ASCII -// skipped FVS_MASK -// skipped FVS_HAVE_SPEC diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/code.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/code.rs deleted file mode 100644 index d28f68cded..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/code.rs +++ /dev/null @@ -1,4 +0,0 @@ -// This header doesn't exist in CPython, but Include/cpython/code.h does. We add -// this here so that PyCodeObject has a definition under the limited API. - -opaque_struct!(PyCodeObject); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/codecs.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/codecs.rs deleted file mode 100644 index 2fd214cbbf..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/codecs.rs +++ /dev/null @@ -1,58 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn PyCodec_Register(search_function: *mut PyObject) -> c_int; - #[cfg(Py_3_10)] - #[cfg(not(PyPy))] - pub fn PyCodec_Unregister(search_function: *mut PyObject) -> c_int; - // skipped non-limited _PyCodec_Lookup from Include/codecs.h - // skipped non-limited _PyCodec_Forget from Include/codecs.h - pub fn PyCodec_KnownEncoding(encoding: *const c_char) -> c_int; - pub fn PyCodec_Encode( - object: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_Decode( - object: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - // skipped non-limited _PyCodec_LookupTextEncoding from Include/codecs.h - // skipped non-limited _PyCodec_EncodeText from Include/codecs.h - // skipped non-limited _PyCodec_DecodeText from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalDecoder from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalEncoder from Include/codecs.h - pub fn PyCodec_Encoder(encoding: *const c_char) -> *mut PyObject; - pub fn PyCodec_Decoder(encoding: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCodec_IncrementalEncoder")] - pub fn PyCodec_IncrementalEncoder( - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCodec_IncrementalDecoder")] - pub fn PyCodec_IncrementalDecoder( - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_StreamReader( - encoding: *const c_char, - stream: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_StreamWriter( - encoding: *const c_char, - stream: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_RegisterError(name: *const c_char, error: *mut PyObject) -> c_int; - pub fn PyCodec_LookupError(name: *const c_char) -> *mut PyObject; - pub fn PyCodec_StrictErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_IgnoreErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_ReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_XMLCharRefReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_BackslashReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - // skipped non-limited PyCodec_NameReplaceErrors from Include/codecs.h - // skipped non-limited Py_hexdigits from Include/codecs.h -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/mod.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/mod.rs deleted file mode 100644 index 11f2912848..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/mod.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! C API Compatibility Shims -//! -//! Some CPython C API functions added in recent versions of Python are -//! inherently safer to use than older C API constructs. This module -//! exposes functions available on all Python versions that wrap the -//! old C API on old Python versions and wrap the function directly -//! on newer Python versions. - -// Unless otherwise noted, the compatibility shims are adapted from -// the pythoncapi-compat project: https://github.com/python/pythoncapi-compat - -/// Internal helper macro which defines compatibility shims for C API functions, deferring to a -/// re-export when that's available. -macro_rules! compat_function { - ( - originally_defined_for($cfg:meta); - - $(#[$attrs:meta])* - pub unsafe fn $name:ident($($arg_names:ident: $arg_types:ty),* $(,)?) -> $ret:ty $body:block - ) => { - // Define as a standalone function under docsrs cfg so that this shows as a unique function in the docs, - // not a re-export (the re-export has the wrong visibility) - #[cfg(any(docsrs, not($cfg)))] - #[cfg_attr(docsrs, doc(cfg(all())))] - $(#[$attrs])* - pub unsafe fn $name( - $($arg_names: $arg_types,)* - ) -> $ret $body - - #[cfg(all($cfg, not(docsrs)))] - pub use $crate::$name; - - #[cfg(test)] - paste::paste! { - // Test that the compat function does not overlap with the original function. If the - // cfgs line up, then the the two glob imports will resolve to the same item via the - // re-export. If the cfgs mismatch, then the use of $name will be ambiguous in cases - // where the function is defined twice, and the test will fail to compile. - #[allow(unused_imports)] - mod [] { - use $crate::*; - use $crate::compat::*; - - #[test] - fn test_export() { - let _ = $name; - } - } - } - }; -} - -mod py_3_10; -mod py_3_13; -mod py_3_9; - -pub use self::py_3_10::*; -pub use self::py_3_13::*; -pub use self::py_3_9::*; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_10.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_10.rs deleted file mode 100644 index c6e8c2cb5c..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_10.rs +++ /dev/null @@ -1,19 +0,0 @@ -compat_function!( - originally_defined_for(Py_3_10); - - #[inline] - pub unsafe fn Py_NewRef(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::Py_INCREF(obj); - obj - } -); - -compat_function!( - originally_defined_for(Py_3_10); - - #[inline] - pub unsafe fn Py_XNewRef(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::Py_XINCREF(obj); - obj - } -); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_13.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_13.rs deleted file mode 100644 index 9f44ced6f3..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_13.rs +++ /dev/null @@ -1,85 +0,0 @@ -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyDict_GetItemRef( - dp: *mut crate::PyObject, - key: *mut crate::PyObject, - result: *mut *mut crate::PyObject, - ) -> std::os::raw::c_int { - use crate::{compat::Py_NewRef, PyDict_GetItemWithError, PyErr_Occurred}; - - let item = PyDict_GetItemWithError(dp, key); - if !item.is_null() { - *result = Py_NewRef(item); - return 1; // found - } - *result = std::ptr::null_mut(); - if PyErr_Occurred().is_null() { - return 0; // not found - } - -1 - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyList_GetItemRef( - arg1: *mut crate::PyObject, - arg2: crate::Py_ssize_t, - ) -> *mut crate::PyObject { - use crate::{PyList_GetItem, Py_XINCREF}; - - let item = PyList_GetItem(arg1, arg2); - Py_XINCREF(item); - item - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyImport_AddModuleRef( - name: *const std::os::raw::c_char, - ) -> *mut crate::PyObject { - use crate::{compat::Py_XNewRef, PyImport_AddModule}; - - Py_XNewRef(PyImport_AddModule(name)) - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyWeakref_GetRef( - reference: *mut crate::PyObject, - pobj: *mut *mut crate::PyObject, - ) -> std::os::raw::c_int { - use crate::{ - compat::Py_NewRef, PyErr_SetString, PyExc_TypeError, PyWeakref_Check, - PyWeakref_GetObject, Py_None, - }; - - if !reference.is_null() && PyWeakref_Check(reference) == 0 { - *pobj = std::ptr::null_mut(); - PyErr_SetString(PyExc_TypeError, c_str!("expected a weakref").as_ptr()); - return -1; - } - let obj = PyWeakref_GetObject(reference); - if obj.is_null() { - // SystemError if reference is NULL - *pobj = std::ptr::null_mut(); - return -1; - } - if obj == Py_None() { - *pobj = std::ptr::null_mut(); - return 0; - } - *pobj = Py_NewRef(obj); - 1 - } -); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_9.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_9.rs deleted file mode 100644 index 285f2b2ae7..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/compat/py_3_9.rs +++ /dev/null @@ -1,21 +0,0 @@ -compat_function!( - originally_defined_for(all( - not(PyPy), - not(GraalPy), - any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10 - )); - - #[inline] - pub unsafe fn PyObject_CallNoArgs(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::PyObject_CallObject(obj, std::ptr::null_mut()) - } -); - -compat_function!( - originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy)))); - - #[inline] - pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject { - crate::PyObject_CallMethodObjArgs(obj, name, std::ptr::null_mut::()) - } -); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/compile.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/compile.rs deleted file mode 100644 index 189a1a8b8a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/compile.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::os::raw::c_int; - -pub const Py_single_input: c_int = 256; -pub const Py_file_input: c_int = 257; -pub const Py_eval_input: c_int = 258; -#[cfg(Py_3_8)] -pub const Py_func_type_input: c_int = 345; - -#[cfg(Py_3_9)] -pub const Py_fstring_input: c_int = 800; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/complexobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/complexobject.rs deleted file mode 100644 index 283bacf6e8..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/complexobject.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_double, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyComplex_Type")] - pub static mut PyComplex_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyComplex_Type)) -} - -#[inline] -pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int { - Py_IS_TYPE(op, addr_of_mut!(PyComplex_Type)) -} - -extern "C" { - // skipped non-limited PyComplex_FromCComplex - #[cfg_attr(PyPy, link_name = "PyPyComplex_FromDoubles")] - pub fn PyComplex_FromDoubles(real: c_double, imag: c_double) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyComplex_RealAsDouble")] - pub fn PyComplex_RealAsDouble(op: *mut PyObject) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyComplex_ImagAsDouble")] - pub fn PyComplex_ImagAsDouble(op: *mut PyObject) -> c_double; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/context.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/context.rs deleted file mode 100644 index 210d6e21f0..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/context.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::object::{PyObject, PyTypeObject, Py_TYPE}; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -extern "C" { - pub static mut PyContext_Type: PyTypeObject; - // skipped non-limited opaque PyContext - pub static mut PyContextVar_Type: PyTypeObject; - // skipped non-limited opaque PyContextVar - pub static mut PyContextToken_Type: PyTypeObject; - // skipped non-limited opaque PyContextToken -} - -#[inline] -pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContext_Type)) as c_int -} - -#[inline] -pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContextVar_Type)) as c_int -} - -#[inline] -pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContextToken_Type)) as c_int -} - -extern "C" { - pub fn PyContext_New() -> *mut PyObject; - pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject; - pub fn PyContext_CopyCurrent() -> *mut PyObject; - - pub fn PyContext_Enter(ctx: *mut PyObject) -> c_int; - pub fn PyContext_Exit(ctx: *mut PyObject) -> c_int; - - pub fn PyContextVar_New(name: *const c_char, def: *mut PyObject) -> *mut PyObject; - pub fn PyContextVar_Get( - var: *mut PyObject, - default_value: *mut PyObject, - value: *mut *mut PyObject, - ) -> c_int; - pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject; - pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int; - // skipped non-limited _PyContext_NewHamtForTests -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/abstract_.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/abstract_.rs deleted file mode 100644 index 83295e58f6..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/abstract_.rs +++ /dev/null @@ -1,332 +0,0 @@ -use crate::{PyObject, Py_ssize_t}; -use std::os::raw::{c_char, c_int}; - -#[cfg(not(Py_3_11))] -use crate::Py_buffer; - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -use crate::{ - vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check, - PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL, -}; -#[cfg(Py_3_8)] -use libc::size_t; - -extern "C" { - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _PyStack_AsDict(values: *const *mut PyObject, kwnames: *mut PyObject) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -const _PY_FASTCALL_SMALL_STACK: size_t = 5; - -extern "C" { - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _Py_CheckFunctionResult( - tstate: *mut PyThreadState, - callable: *mut PyObject, - result: *mut PyObject, - where_: *const c_char, - ) -> *mut PyObject; - - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _PyObject_MakeTpCall( - tstate: *mut PyThreadState, - callable: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, - keywords: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(Py_3_8)] -pub const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = - 1 << (8 * std::mem::size_of::() as size_t - 1); - -#[cfg(Py_3_8)] -#[inline(always)] -pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t { - let n = n & !PY_VECTORCALL_ARGUMENTS_OFFSET; - n.try_into().expect("cannot fail due to mask") -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option { - assert!(!callable.is_null()); - let tp = crate::Py_TYPE(callable); - if PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL) == 0 { - return None; - } - assert!(PyCallable_Check(callable) > 0); - let offset = (*tp).tp_vectorcall_offset; - assert!(offset > 0); - let ptr = callable.cast::().offset(offset).cast(); - *ptr -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_VectorcallTstate( - tstate: *mut PyThreadState, - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, -) -> *mut PyObject { - assert!(kwnames.is_null() || PyTuple_Check(kwnames) > 0); - assert!(!args.is_null() || PyVectorcall_NARGS(nargsf) == 0); - - match PyVectorcall_Function(callable) { - None => { - let nargs = PyVectorcall_NARGS(nargsf); - _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames) - } - Some(func) => { - let res = func(callable, args, nargsf, kwnames); - _Py_CheckFunctionResult(tstate, callable, res, std::ptr::null_mut()) - } - } -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_Vectorcall( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, -) -> *mut PyObject { - _PyObject_VectorcallTstate(PyThreadState_GET(), callable, args, nargsf, kwnames) -} - -extern "C" { - #[cfg(all(PyPy, Py_3_8))] - #[cfg_attr(not(Py_3_9), link_name = "_PyPyObject_Vectorcall")] - #[cfg_attr(Py_3_9, link_name = "PyPyObject_Vectorcall")] - pub fn PyObject_Vectorcall( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_8)] - #[cfg_attr( - all(not(any(PyPy, GraalPy)), not(Py_3_9)), - link_name = "_PyObject_VectorcallDict" - )] - #[cfg_attr(all(PyPy, not(Py_3_9)), link_name = "_PyPyObject_VectorcallDict")] - #[cfg_attr(all(PyPy, Py_3_9), link_name = "PyPyObject_VectorcallDict")] - pub fn PyObject_VectorcallDict( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwdict: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_8)] - #[cfg_attr(not(any(Py_3_9, PyPy)), link_name = "_PyVectorcall_Call")] - #[cfg_attr(PyPy, link_name = "PyPyVectorcall_Call")] - pub fn PyVectorcall_Call( - callable: *mut PyObject, - tuple: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCallTstate( - tstate: *mut PyThreadState, - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_VectorcallTstate(tstate, func, args, nargs as size_t, std::ptr::null_mut()) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCall( - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject { - _PyObject_VectorcallTstate( - PyThreadState_GET(), - func, - std::ptr::null_mut(), - 0, - std::ptr::null_mut(), - ) -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "_PyPyObject_CallNoArg"] - pub fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { - assert!(!arg.is_null()); - let args_array = [std::ptr::null_mut(), arg]; - let args = args_array.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET - let tstate = PyThreadState_GET(); - let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - _PyObject_VectorcallTstate(tstate, func, args, nargsf, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] - pub fn PyObject_VectorcallMethod( - name: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallMethodNoArgs( - self_: *mut PyObject, - name: *mut PyObject, -) -> *mut PyObject { - PyObject_VectorcallMethod( - name, - &self_, - 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, - std::ptr::null_mut(), - ) -} - -#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallMethodOneArg( - self_: *mut PyObject, - name: *mut PyObject, - arg: *mut PyObject, -) -> *mut PyObject { - let args = [self_, arg]; - assert!(!arg.is_null()); - PyObject_VectorcallMethod( - name, - args.as_ptr(), - 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, - std::ptr::null_mut(), - ) -} - -// skipped _PyObject_VectorcallMethodId -// skipped _PyObject_CallMethodIdNoArgs -// skipped _PyObject_CallMethodIdOneArg - -// skipped _PyObject_HasLen - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_LengthHint")] - pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t; - - #[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11 - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] - pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int; -} - -#[cfg(not(any(Py_3_9, PyPy)))] -#[inline] -pub unsafe fn PyObject_CheckBuffer(o: *mut PyObject) -> c_int { - let tp_as_buffer = (*crate::Py_TYPE(o)).tp_as_buffer; - (!tp_as_buffer.is_null() && (*tp_as_buffer).bf_getbuffer.is_some()) as c_int -} - -#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11 -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")] - pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")] - pub fn PyBuffer_GetPointer( - view: *mut Py_buffer, - indices: *mut Py_ssize_t, - ) -> *mut std::os::raw::c_void; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")] - pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")] - pub fn PyBuffer_ToContiguous( - buf: *mut std::os::raw::c_void, - view: *mut Py_buffer, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")] - pub fn PyBuffer_FromContiguous( - view: *mut Py_buffer, - buf: *mut std::os::raw::c_void, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")] - pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int; - pub fn PyBuffer_FillContiguousStrides( - ndims: c_int, - shape: *mut Py_ssize_t, - strides: *mut Py_ssize_t, - itemsize: c_int, - fort: c_char, - ); - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")] - pub fn PyBuffer_FillInfo( - view: *mut Py_buffer, - o: *mut PyObject, - buf: *mut std::os::raw::c_void, - len: Py_ssize_t, - readonly: c_int, - flags: c_int, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")] - pub fn PyBuffer_Release(view: *mut Py_buffer); -} - -// PyIter_Check defined in ffi/abstract_.rs -// PyIndex_Check defined in ffi/abstract_.rs -// Not defined here because this file is not compiled under the -// limited API, but the macros need to be defined for 3.6, 3.7 which -// predate the limited API changes. - -// skipped PySequence_ITEM - -pub const PY_ITERSEARCH_COUNT: c_int = 1; -pub const PY_ITERSEARCH_INDEX: c_int = 2; -pub const PY_ITERSEARCH_CONTAINS: c_int = 3; - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PySequence_IterSearch( - seq: *mut PyObject, - obj: *mut PyObject, - operation: c_int, - ) -> Py_ssize_t; -} - -// skipped _PyObject_RealIsInstance -// skipped _PyObject_RealIsSubclass - -// skipped _PySequence_BytesToCharpArray - -// skipped _Py_FreeCharPArray - -// skipped _Py_add_one_to_index_F -// skipped _Py_add_one_to_index_C - -// skipped _Py_convert_optional_to_ssize_t - -// skipped _PyNumber_Index(*mut PyObject o) diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/bytesobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/bytesobject.rs deleted file mode 100644 index 306702de25..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/bytesobject.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::object::*; -use crate::Py_ssize_t; -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -#[repr(C)] -pub struct PyBytesObject { - pub ob_base: PyVarObject, - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated in Python 3.11 and will be removed in a future version.") - )] - pub ob_shash: crate::Py_hash_t, - pub ob_sval: [c_char; 1], -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -opaque_struct!(PyBytesObject); - -extern "C" { - #[cfg_attr(PyPy, link_name = "_PyPyBytes_Resize")] - pub fn _PyBytes_Resize(bytes: *mut *mut PyObject, newsize: Py_ssize_t) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/ceval.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/ceval.rs deleted file mode 100644 index 6df10627d2..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/ceval.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::cpython::pystate::Py_tracefunc; -use crate::object::{freefunc, PyObject}; -use std::os::raw::c_int; - -extern "C" { - // skipped non-limited _PyEval_CallTracing - - #[cfg(not(Py_3_11))] - pub fn _PyEval_EvalFrameDefault(arg1: *mut crate::PyFrameObject, exc: c_int) -> *mut PyObject; - - #[cfg(Py_3_11)] - pub fn _PyEval_EvalFrameDefault( - tstate: *mut crate::PyThreadState, - frame: *mut crate::_PyInterpreterFrame, - exc: c_int, - ) -> *mut crate::PyObject; - - pub fn _PyEval_RequestCodeExtraIndex(func: freefunc) -> c_int; - pub fn PyEval_SetProfile(trace_func: Option, arg1: *mut PyObject); - pub fn PyEval_SetTrace(trace_func: Option, arg1: *mut PyObject); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/code.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/code.rs deleted file mode 100644 index 230096ca37..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/code.rs +++ /dev/null @@ -1,336 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; - -#[allow(unused_imports)] -use std::os::raw::{c_char, c_int, c_short, c_uchar, c_void}; -#[cfg(not(any(PyPy, GraalPy)))] -use std::ptr::addr_of_mut; - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy)), not(Py_3_11)))] -opaque_struct!(_PyOpcache); - -#[cfg(Py_3_12)] -pub const _PY_MONITORING_LOCAL_EVENTS: usize = 10; -#[cfg(Py_3_12)] -pub const _PY_MONITORING_UNGROUPED_EVENTS: usize = 15; -#[cfg(Py_3_12)] -pub const _PY_MONITORING_EVENTS: usize = 17; - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _Py_LocalMonitors { - pub tools: [u8; if cfg!(Py_3_13) { - _PY_MONITORING_LOCAL_EVENTS - } else { - _PY_MONITORING_UNGROUPED_EVENTS - }], -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _Py_GlobalMonitors { - pub tools: [u8; _PY_MONITORING_UNGROUPED_EVENTS], -} - -// skipped _Py_CODEUNIT - -// skipped _Py_OPCODE -// skipped _Py_OPARG - -// skipped _py_make_codeunit - -// skipped _py_set_opcode - -// skipped _Py_MAKE_CODEUNIT -// skipped _Py_SET_OPCODE - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoCached { - pub _co_code: *mut PyObject, - pub _co_varnames: *mut PyObject, - pub _co_cellvars: *mut PyObject, - pub _co_freevars: *mut PyObject, -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoLineInstrumentationData { - pub original_opcode: u8, - pub line_delta: i8, -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoMonitoringData { - pub local_monitors: _Py_LocalMonitors, - pub active_monitors: _Py_LocalMonitors, - pub tools: *mut u8, - pub lines: *mut _PyCoLineInstrumentationData, - pub line_tools: *mut u8, - pub per_instruction_opcodes: *mut u8, - pub per_instruction_tools: *mut u8, -} - -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_7)))] -opaque_struct!(PyCodeObject); - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_7, not(Py_3_8)))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_argcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_nlocals: c_int, - pub co_stacksize: c_int, - pub co_flags: c_int, - pub co_firstlineno: c_int, - pub co_code: *mut PyObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_varnames: *mut PyObject, - pub co_freevars: *mut PyObject, - pub co_cellvars: *mut PyObject, - pub co_cell2arg: *mut Py_ssize_t, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - pub co_lnotab: *mut PyObject, - pub co_zombieframe: *mut c_void, - pub co_weakreflist: *mut PyObject, - pub co_extra: *mut c_void, -} - -#[cfg(Py_3_13)] -opaque_struct!(_PyExecutorArray); - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_8, not(Py_3_11)))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_argcount: c_int, - pub co_posonlyargcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_nlocals: c_int, - pub co_stacksize: c_int, - pub co_flags: c_int, - pub co_firstlineno: c_int, - pub co_code: *mut PyObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_varnames: *mut PyObject, - pub co_freevars: *mut PyObject, - pub co_cellvars: *mut PyObject, - pub co_cell2arg: *mut Py_ssize_t, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - #[cfg(not(Py_3_10))] - pub co_lnotab: *mut PyObject, - #[cfg(Py_3_10)] - pub co_linetable: *mut PyObject, - pub co_zombieframe: *mut c_void, - pub co_weakreflist: *mut PyObject, - pub co_extra: *mut c_void, - pub co_opcache_map: *mut c_uchar, - pub co_opcache: *mut _PyOpcache, - pub co_opcache_flag: c_int, - pub co_opcache_size: c_uchar, -} - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_11))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyVarObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_exceptiontable: *mut PyObject, - pub co_flags: c_int, - #[cfg(not(Py_3_12))] - pub co_warmup: c_int, - - pub co_argcount: c_int, - pub co_posonlyargcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_stacksize: c_int, - pub co_firstlineno: c_int, - - pub co_nlocalsplus: c_int, - #[cfg(Py_3_12)] - pub co_framesize: c_int, - pub co_nlocals: c_int, - #[cfg(not(Py_3_12))] - pub co_nplaincellvars: c_int, - pub co_ncellvars: c_int, - pub co_nfreevars: c_int, - #[cfg(Py_3_12)] - pub co_version: u32, - - pub co_localsplusnames: *mut PyObject, - pub co_localspluskinds: *mut PyObject, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - pub co_qualname: *mut PyObject, - pub co_linetable: *mut PyObject, - pub co_weakreflist: *mut PyObject, - #[cfg(not(Py_3_12))] - pub _co_code: *mut PyObject, - #[cfg(not(Py_3_12))] - pub _co_linearray: *mut c_char, - #[cfg(Py_3_13)] - pub co_executors: *mut _PyExecutorArray, - #[cfg(Py_3_12)] - pub _co_cached: *mut _PyCoCached, - #[cfg(Py_3_12)] - pub _co_instrumentation_version: u64, - #[cfg(Py_3_12)] - pub _co_monitoring: *mut _PyCoMonitoringData, - pub _co_firsttraceable: c_int, - pub co_extra: *mut c_void, - pub co_code_adaptive: [c_char; 1], -} - -#[cfg(PyPy)] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_name: *mut PyObject, - pub co_filename: *mut PyObject, - pub co_argcount: c_int, - pub co_flags: c_int, -} - -/* Masks for co_flags */ -pub const CO_OPTIMIZED: c_int = 0x0001; -pub const CO_NEWLOCALS: c_int = 0x0002; -pub const CO_VARARGS: c_int = 0x0004; -pub const CO_VARKEYWORDS: c_int = 0x0008; -pub const CO_NESTED: c_int = 0x0010; -pub const CO_GENERATOR: c_int = 0x0020; -/* The CO_NOFREE flag is set if there are no free or cell variables. - This information is redundant, but it allows a single flag test - to determine whether there is any extra work to be done when the - call frame it setup. -*/ -pub const CO_NOFREE: c_int = 0x0040; -/* The CO_COROUTINE flag is set for coroutine functions (defined with -``async def`` keywords) */ -pub const CO_COROUTINE: c_int = 0x0080; -pub const CO_ITERABLE_COROUTINE: c_int = 0x0100; -pub const CO_ASYNC_GENERATOR: c_int = 0x0200; - -pub const CO_FUTURE_DIVISION: c_int = 0x2000; -pub const CO_FUTURE_ABSOLUTE_IMPORT: c_int = 0x4000; /* do absolute imports by default */ -pub const CO_FUTURE_WITH_STATEMENT: c_int = 0x8000; -pub const CO_FUTURE_PRINT_FUNCTION: c_int = 0x1_0000; -pub const CO_FUTURE_UNICODE_LITERALS: c_int = 0x2_0000; - -pub const CO_FUTURE_BARRY_AS_BDFL: c_int = 0x4_0000; -pub const CO_FUTURE_GENERATOR_STOP: c_int = 0x8_0000; -// skipped CO_FUTURE_ANNOTATIONS -// skipped CO_CELL_NOT_AN_ARG - -pub const CO_MAXBLOCKS: usize = 20; - -#[cfg(not(any(PyPy, GraalPy)))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCode_Type: PyTypeObject; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCode_Type)) as c_int -} - -#[inline] -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10, not(Py_3_11)))] -pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t { - crate::PyTuple_GET_SIZE((*op).co_freevars) -} - -#[inline] -#[cfg(all(not(Py_3_10), Py_3_11, not(any(PyPy, GraalPy))))] -pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> c_int { - (*op).co_nfreevars -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyCode_Check"] - pub fn PyCode_Check(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyCode_GetNumFree"] - pub fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t; -} - -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyCode_New")] - pub fn PyCode_New( - argcount: c_int, - kwonlyargcount: c_int, - nlocals: c_int, - stacksize: c_int, - flags: c_int, - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - varnames: *mut PyObject, - freevars: *mut PyObject, - cellvars: *mut PyObject, - filename: *mut PyObject, - name: *mut PyObject, - firstlineno: c_int, - lnotab: *mut PyObject, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - #[cfg(Py_3_8)] - pub fn PyCode_NewWithPosOnlyArgs( - argcount: c_int, - posonlyargcount: c_int, - kwonlyargcount: c_int, - nlocals: c_int, - stacksize: c_int, - flags: c_int, - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - varnames: *mut PyObject, - freevars: *mut PyObject, - cellvars: *mut PyObject, - filename: *mut PyObject, - name: *mut PyObject, - firstlineno: c_int, - lnotab: *mut PyObject, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")] - pub fn PyCode_NewEmpty( - filename: *const c_char, - funcname: *const c_char, - firstlineno: c_int, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int; - // skipped PyCodeAddressRange "for internal use only" - // skipped _PyCode_CheckLineNumber - // skipped _PyCode_ConstantKey - pub fn PyCode_Optimize( - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - lnotab: *mut PyObject, - ) -> *mut PyObject; - pub fn _PyCode_GetExtra( - code: *mut PyObject, - index: Py_ssize_t, - extra: *const *mut c_void, - ) -> c_int; - pub fn _PyCode_SetExtra(code: *mut PyObject, index: Py_ssize_t, extra: *mut c_void) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/compile.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/compile.rs deleted file mode 100644 index 79f06c9200..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/compile.rs +++ /dev/null @@ -1,108 +0,0 @@ -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::object::PyObject; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::pyarena::*; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::pythonrun::*; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::PyCodeObject; -#[cfg(not(any(PyPy, Py_3_10)))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -// skipped non-limited PyCF_MASK -// skipped non-limited PyCF_MASK_OBSOLETE -// skipped non-limited PyCF_SOURCE_IS_UTF8 -// skipped non-limited PyCF_DONT_IMPLY_DEDENT -// skipped non-limited PyCF_ONLY_AST -// skipped non-limited PyCF_IGNORE_COOKIE -// skipped non-limited PyCF_TYPE_COMMENTS -// skipped non-limited PyCF_ALLOW_TOP_LEVEL_AWAIT -// skipped non-limited PyCF_COMPILE_MASK - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyCompilerFlags { - pub cf_flags: c_int, - #[cfg(Py_3_8)] - pub cf_feature_version: c_int, -} - -// skipped non-limited _PyCompilerFlags_INIT - -#[cfg(all(Py_3_12, not(any(Py_3_13, PyPy, GraalPy))))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCompilerSrcLocation { - pub lineno: c_int, - pub end_lineno: c_int, - pub col_offset: c_int, - pub end_col_offset: c_int, -} - -// skipped SRC_LOCATION_FROM_AST - -#[cfg(not(any(PyPy, GraalPy, Py_3_13)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyFutureFeatures { - pub ff_features: c_int, - #[cfg(not(Py_3_12))] - pub ff_lineno: c_int, - #[cfg(Py_3_12)] - pub ff_location: _PyCompilerSrcLocation, -} - -pub const FUTURE_NESTED_SCOPES: &str = "nested_scopes"; -pub const FUTURE_GENERATORS: &str = "generators"; -pub const FUTURE_DIVISION: &str = "division"; -pub const FUTURE_ABSOLUTE_IMPORT: &str = "absolute_import"; -pub const FUTURE_WITH_STATEMENT: &str = "with_statement"; -pub const FUTURE_PRINT_FUNCTION: &str = "print_function"; -pub const FUTURE_UNICODE_LITERALS: &str = "unicode_literals"; -pub const FUTURE_BARRY_AS_BDFL: &str = "barry_as_FLUFL"; -pub const FUTURE_GENERATOR_STOP: &str = "generator_stop"; -// skipped non-limited FUTURE_ANNOTATIONS - -extern "C" { - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyNode_Compile(arg1: *mut _node, arg2: *const c_char) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyAST_CompileEx( - _mod: *mut _mod, - filename: *const c_char, - flags: *mut PyCompilerFlags, - optimize: c_int, - arena: *mut PyArena, - ) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyAST_CompileObject( - _mod: *mut _mod, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - optimize: c_int, - arena: *mut PyArena, - ) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyFuture_FromAST(_mod: *mut _mod, filename: *const c_char) -> *mut PyFutureFeatures; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyFuture_FromASTObject( - _mod: *mut _mod, - filename: *mut PyObject, - ) -> *mut PyFutureFeatures; - - // skipped non-limited _Py_Mangle - // skipped non-limited PY_INVALID_STACK_EFFECT - - pub fn PyCompile_OpcodeStackEffect(opcode: c_int, oparg: c_int) -> c_int; - - #[cfg(Py_3_8)] - pub fn PyCompile_OpcodeStackEffectWithJump(opcode: c_int, oparg: c_int, jump: c_int) -> c_int; - - // skipped non-limited _PyASTOptimizeState - // skipped non-limited _PyAST_Optimize -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/complexobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/complexobject.rs deleted file mode 100644 index 255f9c2703..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/complexobject.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::PyObject; -use std::os::raw::c_double; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct Py_complex { - pub real: c_double, - pub imag: c_double, -} - -// skipped private function _Py_c_sum -// skipped private function _Py_c_diff -// skipped private function _Py_c_neg -// skipped private function _Py_c_prod -// skipped private function _Py_c_quot -// skipped private function _Py_c_pow -// skipped private function _Py_c_abs - -#[repr(C)] -pub struct PyComplexObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub cval: Py_complex, -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyComplex_FromCComplex")] - pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyComplex_AsCComplex")] - pub fn PyComplex_AsCComplex(op: *mut PyObject) -> Py_complex; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/critical_section.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/critical_section.rs deleted file mode 100644 index 97b2f5e055..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/critical_section.rs +++ /dev/null @@ -1,30 +0,0 @@ -#[cfg(Py_GIL_DISABLED)] -use crate::PyMutex; -use crate::PyObject; - -#[repr(C)] -#[cfg(Py_GIL_DISABLED)] -pub struct PyCriticalSection { - _cs_prev: usize, - _cs_mutex: *mut PyMutex, -} - -#[repr(C)] -#[cfg(Py_GIL_DISABLED)] -pub struct PyCriticalSection2 { - _cs_base: PyCriticalSection, - _cs_mutex2: *mut PyMutex, -} - -#[cfg(not(Py_GIL_DISABLED))] -opaque_struct!(PyCriticalSection); - -#[cfg(not(Py_GIL_DISABLED))] -opaque_struct!(PyCriticalSection2); - -extern "C" { - pub fn PyCriticalSection_Begin(c: *mut PyCriticalSection, op: *mut PyObject); - pub fn PyCriticalSection_End(c: *mut PyCriticalSection); - pub fn PyCriticalSection2_Begin(c: *mut PyCriticalSection2, a: *mut PyObject, b: *mut PyObject); - pub fn PyCriticalSection2_End(c: *mut PyCriticalSection2); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/descrobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/descrobject.rs deleted file mode 100644 index 1b5ee466c8..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/descrobject.rs +++ /dev/null @@ -1,78 +0,0 @@ -use crate::{PyGetSetDef, PyMethodDef, PyObject, PyTypeObject}; -use std::os::raw::{c_char, c_int, c_void}; - -pub type wrapperfunc = Option< - unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - wrapped: *mut c_void, - ) -> *mut PyObject, ->; - -pub type wrapperfunc_kwds = Option< - unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - wrapped: *mut c_void, - kwds: *mut PyObject, - ) -> *mut PyObject, ->; - -#[repr(C)] -pub struct wrapperbase { - pub name: *const c_char, - pub offset: c_int, - pub function: *mut c_void, - pub wrapper: wrapperfunc, - pub doc: *const c_char, - pub flags: c_int, - pub name_strobj: *mut PyObject, -} - -pub const PyWrapperFlag_KEYWORDS: c_int = 1; - -#[repr(C)] -pub struct PyDescrObject { - pub ob_base: PyObject, - pub d_type: *mut PyTypeObject, - pub d_name: *mut PyObject, - pub d_qualname: *mut PyObject, -} - -// skipped non-limited PyDescr_TYPE -// skipped non-limited PyDescr_NAME - -#[repr(C)] -pub struct PyMethodDescrObject { - pub d_common: PyDescrObject, - pub d_method: *mut PyMethodDef, - #[cfg(all(not(PyPy), Py_3_8))] - pub vectorcall: Option, -} - -#[repr(C)] -pub struct PyMemberDescrObject { - pub d_common: PyDescrObject, - pub d_member: *mut PyGetSetDef, -} - -#[repr(C)] -pub struct PyGetSetDescrObject { - pub d_common: PyDescrObject, - pub d_getset: *mut PyGetSetDef, -} - -#[repr(C)] -pub struct PyWrapperDescrObject { - pub d_common: PyDescrObject, - pub d_base: *mut wrapperbase, - pub d_wrapped: *mut c_void, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PyMethodWrapper_Type: PyTypeObject; -} - -// skipped non-limited PyDescr_NewWrapper -// skipped non-limited PyDescr_IsData diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/dictobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/dictobject.rs deleted file mode 100644 index 79dcbfdb62..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/dictobject.rs +++ /dev/null @@ -1,80 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; - -opaque_struct!(PyDictKeysObject); - -#[cfg(Py_3_11)] -opaque_struct!(PyDictValues); - -#[cfg(not(GraalPy))] -#[repr(C)] -#[derive(Debug)] -pub struct PyDictObject { - pub ob_base: PyObject, - pub ma_used: Py_ssize_t, - #[cfg_attr( - Py_3_12, - deprecated(note = "Deprecated in Python 3.12 and will be removed in the future.") - )] - pub ma_version_tag: u64, - pub ma_keys: *mut PyDictKeysObject, - #[cfg(not(Py_3_11))] - pub ma_values: *mut *mut PyObject, - #[cfg(Py_3_11)] - pub ma_values: *mut PyDictValues, -} - -extern "C" { - // skipped _PyDict_GetItem_KnownHash - // skipped _PyDict_GetItemIdWithError - // skipped _PyDict_GetItemStringWithError - // skipped PyDict_SetDefault - pub fn _PyDict_SetItem_KnownHash( - mp: *mut PyObject, - key: *mut PyObject, - item: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; - // skipped _PyDict_DelItem_KnownHash - // skipped _PyDict_DelItemIf - // skipped _PyDict_NewKeysForClass - pub fn _PyDict_Next( - mp: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - value: *mut *mut PyObject, - hash: *mut crate::Py_hash_t, - ) -> c_int; - // skipped PyDict_GET_SIZE - // skipped _PyDict_ContainsId - pub fn _PyDict_NewPresized(minused: Py_ssize_t) -> *mut PyObject; - // skipped _PyDict_MaybeUntrack - // skipped _PyDict_HasOnlyStringKeys - // skipped _PyDict_KeysSize - // skipped _PyDict_SizeOf - // skipped _PyDict_Pop - // skipped _PyDict_Pop_KnownHash - // skipped _PyDict_FromKeys - // skipped _PyDict_HasSplitTable - // skipped _PyDict_MergeEx - // skipped _PyDict_SetItemId - // skipped _PyDict_DelItemId - // skipped _PyDict_DebugMallocStats - // skipped _PyObjectDict_SetItem - // skipped _PyDict_LoadGlobal - // skipped _PyDict_GetItemHint - // skipped _PyDictViewObject - // skipped _PyDictView_New - // skipped _PyDictView_Intersect - - #[cfg(Py_3_10)] - pub fn _PyDict_Contains_KnownHash( - op: *mut PyObject, - key: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; - - #[cfg(not(Py_3_10))] - pub fn _PyDict_Contains(mp: *mut PyObject, key: *mut PyObject, hash: Py_ssize_t) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/floatobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/floatobject.rs deleted file mode 100644 index 8c7ee88543..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/floatobject.rs +++ /dev/null @@ -1,33 +0,0 @@ -#[cfg(GraalPy)] -use crate::PyFloat_AsDouble; -use crate::{PyFloat_Check, PyObject}; -use std::os::raw::c_double; - -#[repr(C)] -pub struct PyFloatObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub ob_fval: c_double, -} - -#[inline] -pub unsafe fn _PyFloat_CAST(op: *mut PyObject) -> *mut PyFloatObject { - debug_assert_eq!(PyFloat_Check(op), 1); - op.cast() -} - -#[inline] -pub unsafe fn PyFloat_AS_DOUBLE(op: *mut PyObject) -> c_double { - #[cfg(not(GraalPy))] - return (*_PyFloat_CAST(op)).ob_fval; - #[cfg(GraalPy)] - return PyFloat_AsDouble(op); -} - -// skipped PyFloat_Pack2 -// skipped PyFloat_Pack4 -// skipped PyFloat_Pack8 - -// skipped PyFloat_Unpack2 -// skipped PyFloat_Unpack4 -// skipped PyFloat_Unpack8 diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/frameobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/frameobject.rs deleted file mode 100644 index 6d2346f928..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/frameobject.rs +++ /dev/null @@ -1,96 +0,0 @@ -#[cfg(not(GraalPy))] -use crate::cpython::code::PyCodeObject; -use crate::object::*; -#[cfg(not(GraalPy))] -use crate::pystate::PyThreadState; -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub type PyFrameState = c_char; - -#[repr(C)] -#[derive(Copy, Clone)] -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub struct PyTryBlock { - pub b_type: c_int, - pub b_handler: c_int, - pub b_level: c_int, -} - -#[repr(C)] -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub struct PyFrameObject { - pub ob_base: PyVarObject, - pub f_back: *mut PyFrameObject, - pub f_code: *mut PyCodeObject, - pub f_builtins: *mut PyObject, - pub f_globals: *mut PyObject, - pub f_locals: *mut PyObject, - pub f_valuestack: *mut *mut PyObject, - - #[cfg(not(Py_3_10))] - pub f_stacktop: *mut *mut PyObject, - pub f_trace: *mut PyObject, - #[cfg(Py_3_10)] - pub f_stackdepth: c_int, - pub f_trace_lines: c_char, - pub f_trace_opcodes: c_char, - - pub f_gen: *mut PyObject, - - pub f_lasti: c_int, - pub f_lineno: c_int, - pub f_iblock: c_int, - #[cfg(not(Py_3_10))] - pub f_executing: c_char, - #[cfg(Py_3_10)] - pub f_state: PyFrameState, - pub f_blockstack: [PyTryBlock; crate::CO_MAXBLOCKS], - pub f_localsplus: [*mut PyObject; 1], -} - -#[cfg(any(PyPy, GraalPy, Py_3_11))] -opaque_struct!(PyFrameObject); - -// skipped _PyFrame_IsRunnable -// skipped _PyFrame_IsExecuting -// skipped _PyFrameHasCompleted - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyFrame_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyFrame_Type)) as c_int -} - -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyFrame_New")] - pub fn PyFrame_New( - tstate: *mut PyThreadState, - code: *mut PyCodeObject, - globals: *mut PyObject, - locals: *mut PyObject, - ) -> *mut PyFrameObject; - // skipped _PyFrame_New_NoTrack - - pub fn PyFrame_BlockSetup(f: *mut PyFrameObject, _type: c_int, handler: c_int, level: c_int); - #[cfg(not(any(PyPy, GraalPy, Py_3_11)))] - pub fn PyFrame_BlockPop(f: *mut PyFrameObject) -> *mut PyTryBlock; - - pub fn PyFrame_LocalsToFast(f: *mut PyFrameObject, clear: c_int); - pub fn PyFrame_FastToLocalsWithError(f: *mut PyFrameObject) -> c_int; - pub fn PyFrame_FastToLocals(f: *mut PyFrameObject); - - // skipped _PyFrame_DebugMallocStats - // skipped PyFrame_GetBack - - #[cfg(not(Py_3_9))] - pub fn PyFrame_ClearFreeList() -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/funcobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/funcobject.rs deleted file mode 100644 index 25de30d57f..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/funcobject.rs +++ /dev/null @@ -1,108 +0,0 @@ -use std::os::raw::c_int; -#[cfg(not(all(PyPy, not(Py_3_8))))] -use std::ptr::addr_of_mut; - -use crate::PyObject; - -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_10)))] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_code: *mut PyObject, - pub func_globals: *mut PyObject, - pub func_defaults: *mut PyObject, - pub func_kwdefaults: *mut PyObject, - pub func_closure: *mut PyObject, - pub func_doc: *mut PyObject, - pub func_name: *mut PyObject, - pub func_dict: *mut PyObject, - pub func_weakreflist: *mut PyObject, - pub func_module: *mut PyObject, - pub func_annotations: *mut PyObject, - pub func_qualname: *mut PyObject, - #[cfg(Py_3_8)] - pub vectorcall: Option, -} - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10))] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_globals: *mut PyObject, - pub func_builtins: *mut PyObject, - pub func_name: *mut PyObject, - pub func_qualname: *mut PyObject, - pub func_code: *mut PyObject, - pub func_defaults: *mut PyObject, - pub func_kwdefaults: *mut PyObject, - pub func_closure: *mut PyObject, - pub func_doc: *mut PyObject, - pub func_dict: *mut PyObject, - pub func_weakreflist: *mut PyObject, - pub func_module: *mut PyObject, - pub func_annotations: *mut PyObject, - #[cfg(Py_3_12)] - pub func_typeparams: *mut PyObject, - pub vectorcall: Option, - #[cfg(Py_3_11)] - pub func_version: u32, -} - -#[cfg(PyPy)] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_name: *mut PyObject, -} - -#[cfg(GraalPy)] -pub struct PyFunctionObject { - pub ob_base: PyObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(all(PyPy, not(Py_3_8))))] - #[cfg_attr(PyPy, link_name = "PyPyFunction_Type")] - pub static mut PyFunction_Type: crate::PyTypeObject; -} - -#[cfg(not(all(PyPy, not(Py_3_8))))] -#[inline] -pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int { - (crate::Py_TYPE(op) == addr_of_mut!(PyFunction_Type)) as c_int -} - -extern "C" { - pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_NewWithQualName( - code: *mut PyObject, - globals: *mut PyObject, - qualname: *mut PyObject, - ) -> *mut PyObject; - pub fn PyFunction_GetCode(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetGlobals(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetModule(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetDefaults(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetDefaults(op: *mut PyObject, defaults: *mut PyObject) -> c_int; - pub fn PyFunction_GetKwDefaults(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetKwDefaults(op: *mut PyObject, defaults: *mut PyObject) -> c_int; - pub fn PyFunction_GetClosure(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetClosure(op: *mut PyObject, closure: *mut PyObject) -> c_int; - pub fn PyFunction_GetAnnotations(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetAnnotations(op: *mut PyObject, annotations: *mut PyObject) -> c_int; -} - -// skipped _PyFunction_Vectorcall -// skipped PyFunction_GET_CODE -// skipped PyFunction_GET_GLOBALS -// skipped PyFunction_GET_MODULE -// skipped PyFunction_GET_DEFAULTS -// skipped PyFunction_GET_KW_DEFAULTS -// skipped PyFunction_GET_CLOSURE -// skipped PyFunction_GET_ANNOTATIONS - -// skipped PyClassMethod_Type -// skipped PyStaticMethod_Type -// skipped PyClassMethod_New -// skipped PyStaticMethod_New diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/genobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/genobject.rs deleted file mode 100644 index 17348b2f7b..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/genobject.rs +++ /dev/null @@ -1,98 +0,0 @@ -use crate::object::*; -use crate::PyFrameObject; -#[cfg(not(any(PyPy, GraalPy)))] -use crate::_PyErr_StackItem; -#[cfg(Py_3_11)] -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -pub struct PyGenObject { - pub ob_base: PyObject, - #[cfg(not(Py_3_11))] - pub gi_frame: *mut PyFrameObject, - #[cfg(not(Py_3_10))] - pub gi_running: c_int, - #[cfg(not(Py_3_12))] - pub gi_code: *mut PyObject, - pub gi_weakreflist: *mut PyObject, - pub gi_name: *mut PyObject, - pub gi_qualname: *mut PyObject, - pub gi_exc_state: _PyErr_StackItem, - #[cfg(Py_3_11)] - pub gi_origin_or_finalizer: *mut PyObject, - #[cfg(Py_3_11)] - pub gi_hooks_inited: c_char, - #[cfg(Py_3_11)] - pub gi_closed: c_char, - #[cfg(Py_3_11)] - pub gi_running_async: c_char, - #[cfg(Py_3_11)] - pub gi_frame_state: i8, - #[cfg(Py_3_11)] - pub gi_iframe: [*mut PyObject; 1], -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyGen_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyGen_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyGen_Type)) -} - -#[inline] -pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyGen_Type)) as c_int -} - -extern "C" { - pub fn PyGen_New(frame: *mut PyFrameObject) -> *mut PyObject; - // skipped PyGen_NewWithQualName - // skipped _PyGen_SetStopIterationValue - // skipped _PyGen_FetchStopIterationValue - // skipped _PyGen_yf - // skipped _PyGen_Finalize - #[cfg(not(any(Py_3_9, PyPy)))] - #[deprecated(note = "This function was never documented in the Python API.")] - pub fn PyGen_NeedsFinalizing(op: *mut PyGenObject) -> c_int; -} - -// skipped PyCoroObject - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCoro_Type: PyTypeObject; - pub static mut _PyCoroWrapper_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyCoro_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyCoro_Type)) -} - -// skipped _PyCoro_GetAwaitableIter -// skipped PyCoro_New - -// skipped PyAsyncGenObject - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyAsyncGen_Type: PyTypeObject; - // skipped _PyAsyncGenASend_Type - // skipped _PyAsyncGenWrappedValue_Type - // skipped _PyAsyncGenAThrow_Type -} - -// skipped PyAsyncGen_New - -#[inline] -pub unsafe fn PyAsyncGen_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyAsyncGen_Type)) -} - -// skipped _PyAsyncGenValueWrapperNew diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/import.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/import.rs deleted file mode 100644 index 697d68a419..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/import.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::{PyInterpreterState, PyObject}; -#[cfg(not(PyPy))] -use std::os::raw::c_uchar; -use std::os::raw::{c_char, c_int}; - -// skipped PyInit__imp - -extern "C" { - pub fn _PyImport_IsInitialized(state: *mut PyInterpreterState) -> c_int; - // skipped _PyImport_GetModuleId - pub fn _PyImport_SetModule(name: *mut PyObject, module: *mut PyObject) -> c_int; - pub fn _PyImport_SetModuleString(name: *const c_char, module: *mut PyObject) -> c_int; - pub fn _PyImport_AcquireLock(); - pub fn _PyImport_ReleaseLock() -> c_int; - #[cfg(not(Py_3_9))] - pub fn _PyImport_FindBuiltin(name: *const c_char, modules: *mut PyObject) -> *mut PyObject; - #[cfg(not(Py_3_11))] - pub fn _PyImport_FindExtensionObject(a: *mut PyObject, b: *mut PyObject) -> *mut PyObject; - pub fn _PyImport_FixupBuiltin( - module: *mut PyObject, - name: *const c_char, - modules: *mut PyObject, - ) -> c_int; - pub fn _PyImport_FixupExtensionObject( - a: *mut PyObject, - b: *mut PyObject, - c: *mut PyObject, - d: *mut PyObject, - ) -> c_int; -} - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _inittab { - pub name: *const c_char, - pub initfunc: Option *mut PyObject>, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(PyPy))] - pub static mut PyImport_Inittab: *mut _inittab; -} - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyImport_ExtendInittab(newtab: *mut _inittab) -> c_int; -} - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _frozen { - pub name: *const c_char, - pub code: *const c_uchar, - pub size: c_int, - #[cfg(Py_3_11)] - pub is_package: c_int, - #[cfg(all(Py_3_11, not(Py_3_13)))] - pub get_code: Option *mut PyObject>, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(PyPy))] - pub static mut PyImport_FrozenModules: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenBootstrap: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenStdlib: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenTest: *const _frozen; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/initconfig.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/initconfig.rs deleted file mode 100644 index 321d200e14..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/initconfig.rs +++ /dev/null @@ -1,223 +0,0 @@ -/* --- PyStatus ----------------------------------------------- */ - -use crate::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_ulong}; - -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum _PyStatus_TYPE { - _PyStatus_TYPE_OK = 0, - _PyStatus_TYPE_ERROR = 1, - _PyStatus_TYPE_EXIT = 2, -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStatus { - pub _type: _PyStatus_TYPE, - pub func: *const c_char, - pub err_msg: *const c_char, - pub exitcode: c_int, -} - -extern "C" { - pub fn PyStatus_Ok() -> PyStatus; - pub fn PyStatus_Error(err_msg: *const c_char) -> PyStatus; - pub fn PyStatus_NoMemory() -> PyStatus; - pub fn PyStatus_Exit(exitcode: c_int) -> PyStatus; - pub fn PyStatus_IsError(err: PyStatus) -> c_int; - pub fn PyStatus_IsExit(err: PyStatus) -> c_int; - pub fn PyStatus_Exception(err: PyStatus) -> c_int; -} - -/* --- PyWideStringList ------------------------------------------------ */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyWideStringList { - pub length: Py_ssize_t, - pub items: *mut *mut wchar_t, -} - -extern "C" { - pub fn PyWideStringList_Append(list: *mut PyWideStringList, item: *const wchar_t) -> PyStatus; - pub fn PyWideStringList_Insert( - list: *mut PyWideStringList, - index: Py_ssize_t, - item: *const wchar_t, - ) -> PyStatus; -} - -/* --- PyPreConfig ----------------------------------------------- */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyPreConfig { - pub _config_init: c_int, - pub parse_argv: c_int, - pub isolated: c_int, - pub use_environment: c_int, - pub configure_locale: c_int, - pub coerce_c_locale: c_int, - pub coerce_c_locale_warn: c_int, - - #[cfg(windows)] - pub legacy_windows_fs_encoding: c_int, - - pub utf8_mode: c_int, - pub dev_mode: c_int, - pub allocator: c_int, -} - -extern "C" { - pub fn PyPreConfig_InitPythonConfig(config: *mut PyPreConfig); - pub fn PyPreConfig_InitIsolatedConfig(config: *mut PyPreConfig); -} - -/* --- PyConfig ---------------------------------------------- */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyConfig { - pub _config_init: c_int, - pub isolated: c_int, - pub use_environment: c_int, - pub dev_mode: c_int, - pub install_signal_handlers: c_int, - pub use_hash_seed: c_int, - pub hash_seed: c_ulong, - pub faulthandler: c_int, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub _use_peg_parser: c_int, - pub tracemalloc: c_int, - #[cfg(Py_3_12)] - pub perf_profiling: c_int, - pub import_time: c_int, - #[cfg(Py_3_11)] - pub code_debug_ranges: c_int, - pub show_ref_count: c_int, - #[cfg(not(Py_3_9))] - pub show_alloc_count: c_int, - pub dump_refs: c_int, - #[cfg(Py_3_11)] - pub dump_refs_file: *mut wchar_t, - pub malloc_stats: c_int, - pub filesystem_encoding: *mut wchar_t, - pub filesystem_errors: *mut wchar_t, - pub pycache_prefix: *mut wchar_t, - pub parse_argv: c_int, - #[cfg(Py_3_10)] - pub orig_argv: PyWideStringList, - pub argv: PyWideStringList, - #[cfg(not(Py_3_10))] - pub program_name: *mut wchar_t, - pub xoptions: PyWideStringList, - pub warnoptions: PyWideStringList, - pub site_import: c_int, - pub bytes_warning: c_int, - #[cfg(Py_3_10)] - pub warn_default_encoding: c_int, - pub inspect: c_int, - pub interactive: c_int, - pub optimization_level: c_int, - pub parser_debug: c_int, - pub write_bytecode: c_int, - pub verbose: c_int, - pub quiet: c_int, - pub user_site_directory: c_int, - pub configure_c_stdio: c_int, - pub buffered_stdio: c_int, - pub stdio_encoding: *mut wchar_t, - pub stdio_errors: *mut wchar_t, - - #[cfg(windows)] - pub legacy_windows_stdio: c_int, - - pub check_hash_pycs_mode: *mut wchar_t, - #[cfg(Py_3_11)] - pub use_frozen_modules: c_int, - #[cfg(Py_3_11)] - pub safe_path: c_int, - #[cfg(Py_3_12)] - pub int_max_str_digits: c_int, - #[cfg(Py_3_13)] - pub cpu_count: c_int, - #[cfg(Py_GIL_DISABLED)] - pub enable_gil: c_int, - pub pathconfig_warnings: c_int, - #[cfg(Py_3_10)] - pub program_name: *mut wchar_t, - pub pythonpath_env: *mut wchar_t, - pub home: *mut wchar_t, - #[cfg(Py_3_10)] - pub platlibdir: *mut wchar_t, - - pub module_search_paths_set: c_int, - pub module_search_paths: PyWideStringList, - #[cfg(Py_3_11)] - pub stdlib_dir: *mut wchar_t, - pub executable: *mut wchar_t, - pub base_executable: *mut wchar_t, - pub prefix: *mut wchar_t, - pub base_prefix: *mut wchar_t, - pub exec_prefix: *mut wchar_t, - pub base_exec_prefix: *mut wchar_t, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub platlibdir: *mut wchar_t, - pub skip_source_first_line: c_int, - pub run_command: *mut wchar_t, - pub run_module: *mut wchar_t, - pub run_filename: *mut wchar_t, - #[cfg(Py_3_13)] - pub sys_path_0: *mut wchar_t, - pub _install_importlib: c_int, - pub _init_main: c_int, - #[cfg(all(Py_3_9, not(Py_3_12)))] - pub _isolated_interpreter: c_int, - #[cfg(Py_3_11)] - pub _is_python_build: c_int, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub _orig_argv: PyWideStringList, - #[cfg(all(Py_3_13, py_sys_config = "Py_DEBUG"))] - pub run_presite: *mut wchar_t, -} - -extern "C" { - pub fn PyConfig_InitPythonConfig(config: *mut PyConfig); - pub fn PyConfig_InitIsolatedConfig(config: *mut PyConfig); - pub fn PyConfig_Clear(config: *mut PyConfig); - pub fn PyConfig_SetString( - config: *mut PyConfig, - config_str: *mut *mut wchar_t, - str: *const wchar_t, - ) -> PyStatus; - pub fn PyConfig_SetBytesString( - config: *mut PyConfig, - config_str: *mut *mut wchar_t, - str: *const c_char, - ) -> PyStatus; - pub fn PyConfig_Read(config: *mut PyConfig) -> PyStatus; - pub fn PyConfig_SetBytesArgv( - config: *mut PyConfig, - argc: Py_ssize_t, - argv: *mut *const c_char, - ) -> PyStatus; - pub fn PyConfig_SetArgv( - config: *mut PyConfig, - argc: Py_ssize_t, - argv: *mut *const wchar_t, - ) -> PyStatus; - pub fn PyConfig_SetWideStringList( - config: *mut PyConfig, - list: *mut PyWideStringList, - length: Py_ssize_t, - items: *mut *mut wchar_t, - ) -> PyStatus; -} - -/* --- Helper functions --------------------------------------- */ - -extern "C" { - pub fn Py_GetArgcArgv(argc: *mut c_int, argv: *mut *mut *mut wchar_t); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/listobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/listobject.rs deleted file mode 100644 index 963ddfbea8..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/listobject.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::object::*; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -pub struct PyListObject { - pub ob_base: PyVarObject, - pub ob_item: *mut *mut PyObject, - pub allocated: Py_ssize_t, -} - -#[cfg(any(PyPy, GraalPy))] -pub struct PyListObject { - pub ob_base: PyObject, -} - -// skipped _PyList_Extend -// skipped _PyList_DebugMallocStats -// skipped _PyList_CAST (used inline below) - -/// Macro, trading safety for speed -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - *(*(op as *mut PyListObject)).ob_item.offset(i) -} - -/// Macro, *only* to be used to fill in brand new lists -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - *(*(op as *mut PyListObject)).ob_item.offset(i) = v; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyList_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { - Py_SIZE(op) -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/lock.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/lock.rs deleted file mode 100644 index c451666e76..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/lock.rs +++ /dev/null @@ -1,23 +0,0 @@ -use std::marker::PhantomPinned; -use std::sync::atomic::AtomicU8; - -#[repr(transparent)] -#[derive(Debug)] -pub struct PyMutex { - pub(crate) _bits: AtomicU8, - pub(crate) _pin: PhantomPinned, -} - -impl PyMutex { - pub const fn new() -> PyMutex { - PyMutex { - _bits: AtomicU8::new(0), - _pin: PhantomPinned, - } - } -} - -extern "C" { - pub fn PyMutex_Lock(m: *mut PyMutex); - pub fn PyMutex_Unlock(m: *mut PyMutex); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/longobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/longobject.rs deleted file mode 100644 index 45acaae577..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/longobject.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::longobject::*; -use crate::object::*; -#[cfg(Py_3_13)] -use crate::pyport::Py_ssize_t; -use libc::size_t; -#[cfg(Py_3_13)] -use std::os::raw::c_void; -use std::os::raw::{c_int, c_uchar}; - -#[cfg(Py_3_13)] -extern "C" { - pub fn PyLong_FromUnicodeObject(u: *mut PyObject, base: c_int) -> *mut PyObject; -} - -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_DEFAULTS: c_int = -1; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_BIG_ENDIAN: c_int = 0; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_LITTLE_ENDIAN: c_int = 1; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_NATIVE_ENDIAN: c_int = 3; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_UNSIGNED_BUFFER: c_int = 4; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_REJECT_NEGATIVE: c_int = 8; - -extern "C" { - // skipped _PyLong_Sign - - #[cfg(Py_3_13)] - pub fn PyLong_AsNativeBytes( - v: *mut PyObject, - buffer: *mut c_void, - n_bytes: Py_ssize_t, - flags: c_int, - ) -> Py_ssize_t; - - #[cfg(Py_3_13)] - pub fn PyLong_FromNativeBytes( - buffer: *const c_void, - n_bytes: size_t, - flags: c_int, - ) -> *mut PyObject; - - #[cfg(Py_3_13)] - pub fn PyLong_FromUnsignedNativeBytes( - buffer: *const c_void, - n_bytes: size_t, - flags: c_int, - ) -> *mut PyObject; - - // skipped PyUnstable_Long_IsCompact - // skipped PyUnstable_Long_CompactValue - - #[cfg_attr(PyPy, link_name = "_PyPyLong_FromByteArray")] - pub fn _PyLong_FromByteArray( - bytes: *const c_uchar, - n: size_t, - little_endian: c_int, - is_signed: c_int, - ) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "_PyPyLong_AsByteArrayO")] - pub fn _PyLong_AsByteArray( - v: *mut PyLongObject, - bytes: *mut c_uchar, - n: size_t, - little_endian: c_int, - is_signed: c_int, - ) -> c_int; - - // skipped _PyLong_GCD -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/methodobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/methodobject.rs deleted file mode 100644 index 97ad9ce35f..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/methodobject.rs +++ /dev/null @@ -1,71 +0,0 @@ -use crate::object::*; -#[cfg(not(GraalPy))] -use crate::{PyCFunctionObject, PyMethodDefPointer, METH_METHOD, METH_STATIC}; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(GraalPy))] -pub struct PyCMethodObject { - pub func: PyCFunctionObject, - pub mm_class: *mut PyTypeObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCMethod_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyCMethod_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCMethod_Type)) as c_int -} - -#[inline] -pub unsafe fn PyCMethod_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyCMethod_Type)) -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_FUNCTION(func: *mut PyObject) -> PyMethodDefPointer { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - (*(*func).m_ml).ml_meth -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_SELF(func: *mut PyObject) -> *mut PyObject { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - if (*(*func).m_ml).ml_flags & METH_STATIC != 0 { - std::ptr::null_mut() - } else { - (*func).m_self - } -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_FLAGS(func: *mut PyObject) -> c_int { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - (*(*func).m_ml).ml_flags -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_CLASS(func: *mut PyObject) -> *mut PyTypeObject { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - if (*(*func).m_ml).ml_flags & METH_METHOD != 0 { - let func = func.cast::(); - (*func).mm_class - } else { - std::ptr::null_mut() - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/mod.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/mod.rs deleted file mode 100644 index fe909f0cee..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/mod.rs +++ /dev/null @@ -1,84 +0,0 @@ -pub(crate) mod abstract_; -// skipped bytearrayobject.h -pub(crate) mod bytesobject; -#[cfg(not(PyPy))] -pub(crate) mod ceval; -pub(crate) mod code; -pub(crate) mod compile; -pub(crate) mod complexobject; -#[cfg(Py_3_13)] -pub(crate) mod critical_section; -pub(crate) mod descrobject; -#[cfg(not(PyPy))] -pub(crate) mod dictobject; -// skipped fileobject.h -// skipped fileutils.h -pub(crate) mod frameobject; -pub(crate) mod funcobject; -pub(crate) mod genobject; -pub(crate) mod import; -#[cfg(all(Py_3_8, not(PyPy)))] -pub(crate) mod initconfig; -// skipped interpreteridobject.h -pub(crate) mod listobject; -#[cfg(Py_3_13)] -pub(crate) mod lock; -pub(crate) mod longobject; -#[cfg(all(Py_3_9, not(PyPy)))] -pub(crate) mod methodobject; -pub(crate) mod object; -pub(crate) mod objimpl; -pub(crate) mod pydebug; -pub(crate) mod pyerrors; -#[cfg(all(Py_3_8, not(PyPy)))] -pub(crate) mod pylifecycle; -pub(crate) mod pymem; -pub(crate) mod pystate; -pub(crate) mod pythonrun; -// skipped sysmodule.h -pub(crate) mod floatobject; -pub(crate) mod pyframe; -pub(crate) mod tupleobject; -pub(crate) mod unicodeobject; -pub(crate) mod weakrefobject; - -pub use self::abstract_::*; -pub use self::bytesobject::*; -#[cfg(not(PyPy))] -pub use self::ceval::*; -pub use self::code::*; -pub use self::compile::*; -pub use self::complexobject::*; -#[cfg(Py_3_13)] -pub use self::critical_section::*; -pub use self::descrobject::*; -#[cfg(not(PyPy))] -pub use self::dictobject::*; -pub use self::floatobject::*; -pub use self::frameobject::*; -pub use self::funcobject::*; -pub use self::genobject::*; -pub use self::import::*; -#[cfg(all(Py_3_8, not(PyPy)))] -pub use self::initconfig::*; -pub use self::listobject::*; -#[cfg(Py_3_13)] -pub use self::lock::*; -pub use self::longobject::*; -#[cfg(all(Py_3_9, not(PyPy)))] -pub use self::methodobject::*; -pub use self::object::*; -pub use self::objimpl::*; -pub use self::pydebug::*; -pub use self::pyerrors::*; -#[cfg(Py_3_11)] -pub use self::pyframe::*; -#[cfg(all(Py_3_8, not(PyPy)))] -pub use self::pylifecycle::*; -pub use self::pymem::*; -pub use self::pystate::*; -pub use self::pythonrun::*; -pub use self::tupleobject::*; -pub use self::unicodeobject::*; -#[cfg(not(any(PyPy, GraalPy)))] -pub use self::weakrefobject::*; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/object.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/object.rs deleted file mode 100644 index 35ddf25a2d..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/object.rs +++ /dev/null @@ -1,400 +0,0 @@ -#[cfg(Py_3_8)] -use crate::vectorcallfunc; -use crate::{object, PyGetSetDef, PyMemberDef, PyMethodDef, PyObject, Py_ssize_t}; -use std::mem; -use std::os::raw::{c_char, c_int, c_uint, c_void}; - -// skipped private _Py_NewReference -// skipped private _Py_NewReferenceNoTotal -// skipped private _Py_ResurrectReference - -// skipped private _Py_GetGlobalRefTotal -// skipped private _Py_GetRefTotal -// skipped private _Py_GetLegacyRefTotal -// skipped private _PyInterpreterState_GetRefTotal - -// skipped private _Py_Identifier - -// skipped private _Py_static_string_init -// skipped private _Py_static_string -// skipped private _Py_IDENTIFIER - -#[cfg(not(Py_3_11))] // moved to src/buffer.rs from Python -mod bufferinfo { - use crate::Py_ssize_t; - use std::os::raw::{c_char, c_int, c_void}; - use std::ptr; - - #[repr(C)] - #[derive(Copy, Clone)] - pub struct Py_buffer { - pub buf: *mut c_void, - /// Owned reference - pub obj: *mut crate::PyObject, - pub len: Py_ssize_t, - pub itemsize: Py_ssize_t, - pub readonly: c_int, - pub ndim: c_int, - pub format: *mut c_char, - pub shape: *mut Py_ssize_t, - pub strides: *mut Py_ssize_t, - pub suboffsets: *mut Py_ssize_t, - pub internal: *mut c_void, - #[cfg(PyPy)] - pub flags: c_int, - #[cfg(PyPy)] - pub _strides: [Py_ssize_t; PyBUF_MAX_NDIM as usize], - #[cfg(PyPy)] - pub _shape: [Py_ssize_t; PyBUF_MAX_NDIM as usize], - } - - impl Py_buffer { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - Py_buffer { - buf: ptr::null_mut(), - obj: ptr::null_mut(), - len: 0, - itemsize: 0, - readonly: 0, - ndim: 0, - format: ptr::null_mut(), - shape: ptr::null_mut(), - strides: ptr::null_mut(), - suboffsets: ptr::null_mut(), - internal: ptr::null_mut(), - #[cfg(PyPy)] - flags: 0, - #[cfg(PyPy)] - _strides: [0; PyBUF_MAX_NDIM as usize], - #[cfg(PyPy)] - _shape: [0; PyBUF_MAX_NDIM as usize], - } - } - } - - pub type getbufferproc = unsafe extern "C" fn( - arg1: *mut crate::PyObject, - arg2: *mut Py_buffer, - arg3: c_int, - ) -> c_int; - pub type releasebufferproc = - unsafe extern "C" fn(arg1: *mut crate::PyObject, arg2: *mut Py_buffer); - - /// Maximum number of dimensions - pub const PyBUF_MAX_NDIM: c_int = if cfg!(PyPy) { 36 } else { 64 }; - - /* Flags for getting buffers */ - pub const PyBUF_SIMPLE: c_int = 0; - pub const PyBUF_WRITABLE: c_int = 0x0001; - /* we used to include an E, backwards compatible alias */ - pub const PyBUF_WRITEABLE: c_int = PyBUF_WRITABLE; - pub const PyBUF_FORMAT: c_int = 0x0004; - pub const PyBUF_ND: c_int = 0x0008; - pub const PyBUF_STRIDES: c_int = 0x0010 | PyBUF_ND; - pub const PyBUF_C_CONTIGUOUS: c_int = 0x0020 | PyBUF_STRIDES; - pub const PyBUF_F_CONTIGUOUS: c_int = 0x0040 | PyBUF_STRIDES; - pub const PyBUF_ANY_CONTIGUOUS: c_int = 0x0080 | PyBUF_STRIDES; - pub const PyBUF_INDIRECT: c_int = 0x0100 | PyBUF_STRIDES; - - pub const PyBUF_CONTIG: c_int = PyBUF_ND | PyBUF_WRITABLE; - pub const PyBUF_CONTIG_RO: c_int = PyBUF_ND; - - pub const PyBUF_STRIDED: c_int = PyBUF_STRIDES | PyBUF_WRITABLE; - pub const PyBUF_STRIDED_RO: c_int = PyBUF_STRIDES; - - pub const PyBUF_RECORDS: c_int = PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT; - pub const PyBUF_RECORDS_RO: c_int = PyBUF_STRIDES | PyBUF_FORMAT; - - pub const PyBUF_FULL: c_int = PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT; - pub const PyBUF_FULL_RO: c_int = PyBUF_INDIRECT | PyBUF_FORMAT; - - pub const PyBUF_READ: c_int = 0x100; - pub const PyBUF_WRITE: c_int = 0x200; -} - -#[cfg(not(Py_3_11))] -pub use self::bufferinfo::*; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyNumberMethods { - pub nb_add: Option, - pub nb_subtract: Option, - pub nb_multiply: Option, - pub nb_remainder: Option, - pub nb_divmod: Option, - pub nb_power: Option, - pub nb_negative: Option, - pub nb_positive: Option, - pub nb_absolute: Option, - pub nb_bool: Option, - pub nb_invert: Option, - pub nb_lshift: Option, - pub nb_rshift: Option, - pub nb_and: Option, - pub nb_xor: Option, - pub nb_or: Option, - pub nb_int: Option, - pub nb_reserved: *mut c_void, - pub nb_float: Option, - pub nb_inplace_add: Option, - pub nb_inplace_subtract: Option, - pub nb_inplace_multiply: Option, - pub nb_inplace_remainder: Option, - pub nb_inplace_power: Option, - pub nb_inplace_lshift: Option, - pub nb_inplace_rshift: Option, - pub nb_inplace_and: Option, - pub nb_inplace_xor: Option, - pub nb_inplace_or: Option, - pub nb_floor_divide: Option, - pub nb_true_divide: Option, - pub nb_inplace_floor_divide: Option, - pub nb_inplace_true_divide: Option, - pub nb_index: Option, - pub nb_matrix_multiply: Option, - pub nb_inplace_matrix_multiply: Option, -} - -#[repr(C)] -#[derive(Clone)] -pub struct PySequenceMethods { - pub sq_length: Option, - pub sq_concat: Option, - pub sq_repeat: Option, - pub sq_item: Option, - pub was_sq_slice: *mut c_void, - pub sq_ass_item: Option, - pub was_sq_ass_slice: *mut c_void, - pub sq_contains: Option, - pub sq_inplace_concat: Option, - pub sq_inplace_repeat: Option, -} - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyMappingMethods { - pub mp_length: Option, - pub mp_subscript: Option, - pub mp_ass_subscript: Option, -} - -#[cfg(Py_3_10)] -pub type sendfunc = unsafe extern "C" fn( - iter: *mut PyObject, - value: *mut PyObject, - result: *mut *mut PyObject, -) -> object::PySendResult; - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyAsyncMethods { - pub am_await: Option, - pub am_aiter: Option, - pub am_anext: Option, - #[cfg(Py_3_10)] - pub am_send: Option, -} - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyBufferProcs { - pub bf_getbuffer: Option, - pub bf_releasebuffer: Option, -} - -pub type printfunc = - unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut ::libc::FILE, arg3: c_int) -> c_int; - -#[repr(C)] -#[derive(Debug)] -pub struct PyTypeObject { - pub ob_base: object::PyVarObject, - #[cfg(GraalPy)] - pub ob_size: Py_ssize_t, - pub tp_name: *const c_char, - pub tp_basicsize: Py_ssize_t, - pub tp_itemsize: Py_ssize_t, - pub tp_dealloc: Option, - #[cfg(not(Py_3_8))] - pub tp_print: Option, - #[cfg(Py_3_8)] - pub tp_vectorcall_offset: Py_ssize_t, - pub tp_getattr: Option, - pub tp_setattr: Option, - pub tp_as_async: *mut PyAsyncMethods, - pub tp_repr: Option, - pub tp_as_number: *mut PyNumberMethods, - pub tp_as_sequence: *mut PySequenceMethods, - pub tp_as_mapping: *mut PyMappingMethods, - pub tp_hash: Option, - pub tp_call: Option, - pub tp_str: Option, - pub tp_getattro: Option, - pub tp_setattro: Option, - pub tp_as_buffer: *mut PyBufferProcs, - #[cfg(not(Py_GIL_DISABLED))] - pub tp_flags: std::os::raw::c_ulong, - #[cfg(Py_GIL_DISABLED)] - pub tp_flags: crate::impl_::AtomicCULong, - pub tp_doc: *const c_char, - pub tp_traverse: Option, - pub tp_clear: Option, - pub tp_richcompare: Option, - pub tp_weaklistoffset: Py_ssize_t, - pub tp_iter: Option, - pub tp_iternext: Option, - pub tp_methods: *mut PyMethodDef, - pub tp_members: *mut PyMemberDef, - pub tp_getset: *mut PyGetSetDef, - pub tp_base: *mut PyTypeObject, - pub tp_dict: *mut object::PyObject, - pub tp_descr_get: Option, - pub tp_descr_set: Option, - pub tp_dictoffset: Py_ssize_t, - pub tp_init: Option, - pub tp_alloc: Option, - pub tp_new: Option, - pub tp_free: Option, - pub tp_is_gc: Option, - pub tp_bases: *mut object::PyObject, - pub tp_mro: *mut object::PyObject, - pub tp_cache: *mut object::PyObject, - pub tp_subclasses: *mut object::PyObject, - pub tp_weaklist: *mut object::PyObject, - pub tp_del: Option, - pub tp_version_tag: c_uint, - pub tp_finalize: Option, - #[cfg(Py_3_8)] - pub tp_vectorcall: Option, - #[cfg(Py_3_12)] - pub tp_watched: c_char, - #[cfg(any(all(PyPy, Py_3_8, not(Py_3_10)), all(not(PyPy), Py_3_8, not(Py_3_9))))] - pub tp_print: Option, - #[cfg(all(PyPy, not(Py_3_10)))] - pub tp_pypy_flags: std::os::raw::c_long, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_allocs: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_frees: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_maxalloc: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_prev: *mut PyTypeObject, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_next: *mut PyTypeObject, -} - -#[cfg(Py_3_11)] -#[repr(C)] -#[derive(Clone)] -struct _specialization_cache { - getitem: *mut PyObject, - #[cfg(Py_3_12)] - getitem_version: u32, - #[cfg(Py_3_13)] - init: *mut PyObject, -} - -#[repr(C)] -pub struct PyHeapTypeObject { - pub ht_type: PyTypeObject, - pub as_async: PyAsyncMethods, - pub as_number: PyNumberMethods, - pub as_mapping: PyMappingMethods, - pub as_sequence: PySequenceMethods, - pub as_buffer: PyBufferProcs, - pub ht_name: *mut object::PyObject, - pub ht_slots: *mut object::PyObject, - pub ht_qualname: *mut object::PyObject, - #[cfg(not(PyPy))] - pub ht_cached_keys: *mut c_void, - #[cfg(Py_3_9)] - pub ht_module: *mut object::PyObject, - #[cfg(Py_3_11)] - _ht_tpname: *mut c_char, - #[cfg(Py_3_11)] - _spec_cache: _specialization_cache, -} - -impl Default for PyHeapTypeObject { - #[inline] - fn default() -> Self { - unsafe { mem::zeroed() } - } -} - -#[inline] -#[cfg(not(Py_3_11))] -pub unsafe fn PyHeapType_GET_MEMBERS(etype: *mut PyHeapTypeObject) -> *mut PyMemberDef { - let py_type = object::Py_TYPE(etype as *mut object::PyObject); - let ptr = etype.offset((*py_type).tp_basicsize); - ptr as *mut PyMemberDef -} - -// skipped private _PyType_Name -// skipped private _PyType_Lookup -// skipped private _PyType_LookupRef - -extern "C" { - #[cfg(Py_3_12)] - pub fn PyType_GetDict(o: *mut PyTypeObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyObject_Print")] - pub fn PyObject_Print(o: *mut PyObject, fp: *mut ::libc::FILE, flags: c_int) -> c_int; - - // skipped private _Py_BreakPoint - // skipped private _PyObject_Dump - - // skipped _PyObject_GetAttrId - - // skipped private _PyObject_GetDictPtr - pub fn PyObject_CallFinalizer(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFinalizerFromDealloc")] - pub fn PyObject_CallFinalizerFromDealloc(arg1: *mut PyObject) -> c_int; - - // skipped private _PyObject_GenericGetAttrWithDict - // skipped private _PyObject_GenericSetAttrWithDict - // skipped private _PyObject_FunctionStr -} - -// skipped Py_SETREF -// skipped Py_XSETREF - -// skipped private _PyObject_ASSERT_FROM -// skipped private _PyObject_ASSERT_WITH_MSG -// skipped private _PyObject_ASSERT -// skipped private _PyObject_ASSERT_FAILED_MSG -// skipped private _PyObject_AssertFailed - -// skipped private _PyTrash_begin -// skipped private _PyTrash_end - -// skipped _PyTrash_thread_deposit_object -// skipped _PyTrash_thread_destroy_chain - -// skipped Py_TRASHCAN_BEGIN -// skipped Py_TRASHCAN_END - -// skipped PyObject_GetItemData - -// skipped PyObject_VisitManagedDict -// skipped _PyObject_SetManagedDict -// skipped PyObject_ClearManagedDict - -// skipped TYPE_MAX_WATCHERS - -// skipped PyType_WatchCallback -// skipped PyType_AddWatcher -// skipped PyType_ClearWatcher -// skipped PyType_Watch -// skipped PyType_Unwatch - -// skipped PyUnstable_Type_AssignVersionTag - -// skipped PyRefTracerEvent - -// skipped PyRefTracer -// skipped PyRefTracer_SetTracer -// skipped PyRefTracer_GetTracer diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/objimpl.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/objimpl.rs deleted file mode 100644 index 3e0270ddc8..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/objimpl.rs +++ /dev/null @@ -1,71 +0,0 @@ -use libc::size_t; -use std::os::raw::c_int; - -#[cfg(not(any(PyPy, GraalPy)))] -use std::os::raw::c_void; - -use crate::object::*; - -// skipped _PyObject_SIZE -// skipped _PyObject_VAR_SIZE - -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _Py_GetAllocatedBlocks() -> crate::Py_ssize_t; -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyObjectArenaAllocator { - pub ctx: *mut c_void, - pub alloc: Option *mut c_void>, - pub free: Option, -} - -#[cfg(not(any(PyPy, GraalPy)))] -impl Default for PyObjectArenaAllocator { - #[inline] - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyObject_GetArenaAllocator(allocator: *mut PyObjectArenaAllocator); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyObject_SetArenaAllocator(allocator: *mut PyObjectArenaAllocator); - - #[cfg(Py_3_9)] - pub fn PyObject_IS_GC(o: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(Py_3_9))] -pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int { - (crate::PyType_IS_GC(Py_TYPE(o)) != 0 - && match (*Py_TYPE(o)).tp_is_gc { - Some(tp_is_gc) => tp_is_gc(o) != 0, - None => true, - }) as c_int -} - -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _PyObject_GC_Malloc(size: size_t) -> *mut PyObject; - pub fn _PyObject_GC_Calloc(size: size_t) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int { - ((*t).tp_weaklistoffset > 0) as c_int -} - -#[inline] -pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject { - let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset; - o.offset(weaklistoffset) as *mut *mut PyObject -} - -// skipped PyUnstable_Object_GC_NewWithExtraData diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pydebug.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pydebug.rs deleted file mode 100644 index a42848e8fd..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pydebug.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::os::raw::{c_char, c_int}; - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_DebugFlag")] - pub static mut Py_DebugFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_VerboseFlag")] - pub static mut Py_VerboseFlag: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_QuietFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_InteractiveFlag")] - pub static mut Py_InteractiveFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_InspectFlag")] - pub static mut Py_InspectFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_OptimizeFlag")] - pub static mut Py_OptimizeFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_NoSiteFlag")] - pub static mut Py_NoSiteFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_BytesWarningFlag")] - pub static mut Py_BytesWarningFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_UseClassExceptionsFlag")] - pub static mut Py_UseClassExceptionsFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_FrozenFlag")] - pub static mut Py_FrozenFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_IgnoreEnvironmentFlag")] - pub static mut Py_IgnoreEnvironmentFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_DontWriteBytecodeFlag")] - pub static mut Py_DontWriteBytecodeFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_NoUserSiteDirectory")] - pub static mut Py_NoUserSiteDirectory: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_UnbufferedStdioFlag: c_int; - #[cfg_attr(PyPy, link_name = "PyPy_HashRandomizationFlag")] - pub static mut Py_HashRandomizationFlag: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_IsolatedFlag: c_int; - #[cfg(windows)] - #[deprecated(note = "Python 3.12")] - pub static mut Py_LegacyWindowsFSEncodingFlag: c_int; - #[cfg(windows)] - #[deprecated(note = "Python 3.12")] - pub static mut Py_LegacyWindowsStdioFlag: c_int; -} - -extern "C" { - #[cfg(Py_3_11)] - pub fn Py_GETENV(name: *const c_char) -> *mut c_char; -} - -#[cfg(not(Py_3_11))] -#[inline(always)] -pub unsafe fn Py_GETENV(name: *const c_char) -> *mut c_char { - #[allow(deprecated)] - if Py_IgnoreEnvironmentFlag != 0 { - std::ptr::null_mut() - } else { - libc::getenv(name) - } -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyerrors.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyerrors.rs deleted file mode 100644 index ca08b44a95..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyerrors.rs +++ /dev/null @@ -1,188 +0,0 @@ -use crate::PyObject; -#[cfg(not(any(PyPy, GraalPy)))] -use crate::Py_ssize_t; - -#[repr(C)] -#[derive(Debug)] -pub struct PyBaseExceptionObject { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub dict: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub args: *mut PyObject, - #[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))] - pub notes: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub traceback: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub context: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub cause: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub suppress_context: char, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySyntaxErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub msg: *mut PyObject, - pub filename: *mut PyObject, - pub lineno: *mut PyObject, - pub offset: *mut PyObject, - #[cfg(Py_3_10)] - pub end_lineno: *mut PyObject, - #[cfg(Py_3_10)] - pub end_offset: *mut PyObject, - pub text: *mut PyObject, - pub print_file_and_line: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyImportErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub msg: *mut PyObject, - pub name: *mut PyObject, - pub path: *mut PyObject, - #[cfg(Py_3_12)] - pub name_from: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyUnicodeErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub encoding: *mut PyObject, - pub object: *mut PyObject, - pub start: Py_ssize_t, - pub end: Py_ssize_t, - pub reason: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySystemExitObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub code: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyOSErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub myerrno: *mut PyObject, - pub strerror: *mut PyObject, - pub filename: *mut PyObject, - pub filename2: *mut PyObject, - #[cfg(windows)] - pub winerror: *mut PyObject, - pub written: Py_ssize_t, -} - -#[repr(C)] -#[derive(Debug)] -pub struct PyStopIterationObject { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub dict: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub args: *mut PyObject, - #[cfg(all(Py_3_11, not(any(PyPy, GraalPy))))] - pub notes: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub traceback: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub context: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub cause: *mut PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub suppress_context: char, - - pub value: *mut PyObject, -} - -// skipped _PyErr_ChainExceptions - -// skipped PyNameErrorObject -// skipped PyAttributeErrorObject - -// skipped PyEnvironmentErrorObject -// skipped PyWindowsErrorObject - -// skipped _PyErr_SetKeyError -// skipped _PyErr_GetTopmostException -// skipped _PyErr_GetExcInfo - -// skipped PyErr_SetFromErrnoWithUnicodeFilename - -// skipped _PyErr_FormatFromCause - -// skipped PyErr_SetFromWindowsErrWithUnicodeFilename -// skipped PyErr_SetExcFromWindowsErrWithUnicodeFilename - -// skipped _PyErr_TrySetFromCause - -// skipped PySignal_SetWakeupFd -// skipped _PyErr_CheckSignals - -// skipped PyErr_SyntaxLocationObject -// skipped PyErr_RangedSyntaxLocationObject -// skipped PyErr_ProgramTextObject - -// skipped _PyErr_ProgramDecodedTextObject -// skipped _PyUnicodeTranslateError_Create -// skipped _PyErr_WriteUnraisableMsg -// skipped _Py_FatalErrorFunc -// skipped _Py_FatalErrorFormat -// skipped Py_FatalError diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyframe.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyframe.rs deleted file mode 100644 index d0cfa0a2c6..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pyframe.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(Py_3_11)] -opaque_struct!(_PyInterpreterFrame); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs deleted file mode 100644 index c259c369ef..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::{PyConfig, PyPreConfig, PyStatus, Py_ssize_t}; -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -// "private" functions in cpython/pylifecycle.h accepted in PEP 587 -extern "C" { - // skipped _Py_SetStandardStreamEncoding; - pub fn Py_PreInitialize(src_config: *const PyPreConfig) -> PyStatus; - pub fn Py_PreInitializeFromBytesArgs( - src_config: *const PyPreConfig, - argc: Py_ssize_t, - argv: *mut *mut c_char, - ) -> PyStatus; - pub fn Py_PreInitializeFromArgs( - src_config: *const PyPreConfig, - argc: Py_ssize_t, - argv: *mut *mut wchar_t, - ) -> PyStatus; - pub fn _Py_IsCoreInitialized() -> c_int; - - pub fn Py_InitializeFromConfig(config: *const PyConfig) -> PyStatus; - pub fn _Py_InitializeMain() -> PyStatus; - - pub fn Py_RunMain() -> c_int; - - pub fn Py_ExitStatusException(status: PyStatus) -> !; - - // skipped _Py_RestoreSignals - - // skipped Py_FdIsInteractive - // skipped _Py_FdIsInteractive - - // skipped _Py_SetProgramFullPath - - // skipped _Py_gitidentifier - // skipped _Py_getversion - - // skipped _Py_IsFinalizing - - // skipped _PyOS_URandom - // skipped _PyOS_URandomNonblock - - // skipped _Py_CoerceLegacyLocale - // skipped _Py_LegacyLocaleDetected - // skipped _Py_SetLocaleFromEnv - -} - -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_DEFAULT_GIL: c_int = 0; -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_SHARED_GIL: c_int = 1; -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_OWN_GIL: c_int = 2; - -#[cfg(Py_3_12)] -#[repr(C)] -pub struct PyInterpreterConfig { - pub use_main_obmalloc: c_int, - pub allow_fork: c_int, - pub allow_exec: c_int, - pub allow_threads: c_int, - pub allow_daemon_threads: c_int, - pub check_multi_interp_extensions: c_int, - pub gil: c_int, -} - -#[cfg(Py_3_12)] -pub const _PyInterpreterConfig_INIT: PyInterpreterConfig = PyInterpreterConfig { - use_main_obmalloc: 0, - allow_fork: 0, - allow_exec: 0, - allow_threads: 1, - allow_daemon_threads: 0, - check_multi_interp_extensions: 1, - gil: PyInterpreterConfig_OWN_GIL, -}; - -#[cfg(Py_3_12)] -pub const _PyInterpreterConfig_LEGACY_INIT: PyInterpreterConfig = PyInterpreterConfig { - use_main_obmalloc: 1, - allow_fork: 1, - allow_exec: 1, - allow_threads: 1, - allow_daemon_threads: 1, - check_multi_interp_extensions: 0, - gil: PyInterpreterConfig_SHARED_GIL, -}; - -extern "C" { - #[cfg(Py_3_12)] - pub fn Py_NewInterpreterFromConfig( - tstate_p: *mut *mut crate::PyThreadState, - config: *const PyInterpreterConfig, - ) -> PyStatus; -} - -// skipped atexit_datacallbackfunc -// skipped _Py_AtExit diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pymem.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pymem.rs deleted file mode 100644 index c400fa30b0..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pymem.rs +++ /dev/null @@ -1,49 +0,0 @@ -use libc::size_t; -use std::os::raw::c_void; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")] - pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")] - pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")] - pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")] - pub fn PyMem_RawFree(ptr: *mut c_void); - - // skipped _PyMem_GetCurrentAllocatorName - // skipped _PyMem_RawStrdup - // skipped _PyMem_Strdup - // skipped _PyMem_RawWcsdup -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub enum PyMemAllocatorDomain { - PYMEM_DOMAIN_RAW, - PYMEM_DOMAIN_MEM, - PYMEM_DOMAIN_OBJ, -} - -// skipped PyMemAllocatorName -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyMemAllocatorEx { - pub ctx: *mut c_void, - pub malloc: Option *mut c_void>, - pub calloc: - Option *mut c_void>, - pub realloc: - Option *mut c_void>, - pub free: Option, -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_SetupDebugHooks(); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pystate.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pystate.rs deleted file mode 100644 index 5481265b55..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pystate.rs +++ /dev/null @@ -1,112 +0,0 @@ -#[cfg(not(PyPy))] -use crate::PyThreadState; -use crate::{PyFrameObject, PyInterpreterState, PyObject}; -use std::os::raw::c_int; - -// skipped _PyInterpreterState_RequiresIDRef -// skipped _PyInterpreterState_RequireIDRef - -// skipped _PyInterpreterState_GetMainModule - -pub type Py_tracefunc = unsafe extern "C" fn( - obj: *mut PyObject, - frame: *mut PyFrameObject, - what: c_int, - arg: *mut PyObject, -) -> c_int; - -pub const PyTrace_CALL: c_int = 0; -pub const PyTrace_EXCEPTION: c_int = 1; -pub const PyTrace_LINE: c_int = 2; -pub const PyTrace_RETURN: c_int = 3; -pub const PyTrace_C_CALL: c_int = 4; -pub const PyTrace_C_EXCEPTION: c_int = 5; -pub const PyTrace_C_RETURN: c_int = 6; -pub const PyTrace_OPCODE: c_int = 7; - -// skipped PyTraceInfo -// skipped CFrame - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _PyErr_StackItem { - #[cfg(not(Py_3_11))] - pub exc_type: *mut PyObject, - pub exc_value: *mut PyObject, - #[cfg(not(Py_3_11))] - pub exc_traceback: *mut PyObject, - pub previous_item: *mut _PyErr_StackItem, -} - -// skipped _PyStackChunk -// skipped _ts (aka PyThreadState) - -extern "C" { - // skipped _PyThreadState_Prealloc - // skipped _PyThreadState_UncheckedGet - // skipped _PyThreadState_GetDict - - #[cfg_attr(PyPy, link_name = "PyPyGILState_Check")] - pub fn PyGILState_Check() -> c_int; - - // skipped _PyGILState_GetInterpreterStateUnsafe - // skipped _PyThread_CurrentFrames - // skipped _PyThread_CurrentExceptions - - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Main() -> *mut PyInterpreterState; - #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Head")] - pub fn PyInterpreterState_Head() -> *mut PyInterpreterState; - #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Next")] - pub fn PyInterpreterState_Next(interp: *mut PyInterpreterState) -> *mut PyInterpreterState; - #[cfg(not(PyPy))] - pub fn PyInterpreterState_ThreadHead(interp: *mut PyInterpreterState) -> *mut PyThreadState; - #[cfg(not(PyPy))] - pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState; - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")] - pub fn PyThreadState_DeleteCurrent(); -} - -#[cfg(all(Py_3_9, not(Py_3_11)))] -pub type _PyFrameEvalFunction = extern "C" fn( - *mut crate::PyThreadState, - *mut crate::PyFrameObject, - c_int, -) -> *mut crate::object::PyObject; - -#[cfg(Py_3_11)] -pub type _PyFrameEvalFunction = extern "C" fn( - *mut crate::PyThreadState, - *mut crate::_PyInterpreterFrame, - c_int, -) -> *mut crate::object::PyObject; - -#[cfg(Py_3_9)] -extern "C" { - /// Get the frame evaluation function. - pub fn _PyInterpreterState_GetEvalFrameFunc( - interp: *mut PyInterpreterState, - ) -> _PyFrameEvalFunction; - - ///Set the frame evaluation function. - pub fn _PyInterpreterState_SetEvalFrameFunc( - interp: *mut PyInterpreterState, - eval_frame: _PyFrameEvalFunction, - ); -} - -// skipped _PyInterpreterState_GetConfig -// skipped _PyInterpreterState_GetConfigCopy -// skipped _PyInterpreterState_SetConfig -// skipped _Py_GetConfig - -// skipped _PyCrossInterpreterData -// skipped _PyObject_GetCrossInterpreterData -// skipped _PyCrossInterpreterData_NewObject -// skipped _PyCrossInterpreterData_Release -// skipped _PyObject_CheckCrossInterpreterData -// skipped crossinterpdatafunc -// skipped _PyCrossInterpreterData_RegisterClass -// skipped _PyCrossInterpreterData_Lookup diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pythonrun.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pythonrun.rs deleted file mode 100644 index fe78f55ca0..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/pythonrun.rs +++ /dev/null @@ -1,250 +0,0 @@ -use crate::object::*; -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API, Py_3_10)))] -use crate::pyarena::PyArena; -use crate::PyCompilerFlags; -#[cfg(not(any(PyPy, GraalPy, Py_3_10)))] -use crate::{_mod, _node}; -use libc::FILE; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn PyRun_SimpleStringFlags(arg1: *const c_char, arg2: *mut PyCompilerFlags) -> c_int; - pub fn _PyRun_SimpleFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_AnyFileExFlags( - fp: *mut FILE, - filename: *const c_char, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn _PyRun_AnyFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_SimpleFileExFlags( - fp: *mut FILE, - filename: *const c_char, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveOneFlags( - fp: *mut FILE, - filename: *const c_char, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveOneObject( - fp: *mut FILE, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveLoopFlags( - fp: *mut FILE, - filename: *const c_char, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn _PyRun_InteractiveLoopObject( - fp: *mut FILE, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> c_int; - - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromString( - s: *const c_char, - filename: *const c_char, - start: c_int, - flags: *mut PyCompilerFlags, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromStringObject( - s: *const c_char, - filename: *mut PyObject, - start: c_int, - flags: *mut PyCompilerFlags, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromFile( - fp: *mut FILE, - filename: *const c_char, - enc: *const c_char, - start: c_int, - ps1: *const c_char, - ps2: *const c_char, - flags: *mut PyCompilerFlags, - errcode: *mut c_int, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromFileObject( - fp: *mut FILE, - filename: *mut PyObject, - enc: *const c_char, - start: c_int, - ps1: *const c_char, - ps2: *const c_char, - flags: *mut PyCompilerFlags, - errcode: *mut c_int, - arena: *mut PyArena, - ) -> *mut _mod; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRun_StringFlags")] - pub fn PyRun_StringFlags( - arg1: *const c_char, - arg2: c_int, - arg3: *mut PyObject, - arg4: *mut PyObject, - arg5: *mut PyCompilerFlags, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileExFlags( - fp: *mut FILE, - filename: *const c_char, - start: c_int, - globals: *mut PyObject, - locals: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> *mut PyObject; - - #[cfg(not(any(PyPy, GraalPy)))] - pub fn Py_CompileStringExFlags( - str: *const c_char, - filename: *const c_char, - start: c_int, - flags: *mut PyCompilerFlags, - optimize: c_int, - ) -> *mut PyObject; - #[cfg(not(Py_LIMITED_API))] - pub fn Py_CompileStringObject( - str: *const c_char, - filename: *mut PyObject, - start: c_int, - flags: *mut PyCompilerFlags, - optimize: c_int, - ) -> *mut PyObject; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject { - Py_CompileStringExFlags(string, p, s, std::ptr::null_mut(), -1) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut PyCompilerFlags, -) -> *mut PyObject { - Py_CompileStringExFlags(string, p, s, f, -1) -} - -// skipped _Py_SourceAsString - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRun_String")] - pub fn PyRun_String( - string: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFile(fp: *mut FILE, name: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFileEx(fp: *mut FILE, name: *const c_char, closeit: c_int) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFileFlags( - arg1: *mut FILE, - arg2: *const c_char, - arg3: *mut PyCompilerFlags, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyRun_SimpleString")] - pub fn PyRun_SimpleString(s: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_SimpleFile(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_SimpleFileEx(f: *mut FILE, p: *const c_char, c: c_int) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_InteractiveOne(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_InteractiveLoop(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyRun_File")] - pub fn PyRun_File( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileEx( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - c: c_int, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileFlags( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> *mut PyObject; -} - -// skipped macro PyRun_String -// skipped macro PyRun_AnyFile -// skipped macro PyRun_AnyFileEx -// skipped macro PyRun_AnyFileFlags - -extern "C" { - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseStringFlags( - arg1: *const c_char, - arg2: c_int, - arg3: c_int, - ) -> *mut _node; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseStringFlagsFilename( - arg1: *const c_char, - arg2: *const c_char, - arg3: c_int, - arg4: c_int, - ) -> *mut _node; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseFileFlags( - arg1: *mut FILE, - arg2: *const c_char, - arg3: c_int, - arg4: c_int, - ) -> *mut _node; - - #[cfg(PyPy)] - #[cfg_attr(PyPy, link_name = "PyPy_CompileStringFlags")] - pub fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut PyCompilerFlags, - ) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/tupleobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/tupleobject.rs deleted file mode 100644 index 1d988d2bef..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/tupleobject.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::object::*; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; - -#[repr(C)] -pub struct PyTupleObject { - pub ob_base: PyVarObject, - #[cfg(not(GraalPy))] - pub ob_item: [*mut PyObject; 1], -} - -// skipped _PyTuple_Resize -// skipped _PyTuple_MaybeUntrack - -// skipped _PyTuple_CAST - -/// Macro, trading safety for speed -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { - Py_SIZE(op) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - *(*(op as *mut PyTupleObject)).ob_item.as_ptr().offset(i) -} - -/// Macro, *only* to be used to fill in brand new tuples -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyTuple_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - *(*(op as *mut PyTupleObject)).ob_item.as_mut_ptr().offset(i) = v; -} - -// skipped _PyTuple_DebugMallocStats diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs deleted file mode 100644 index 1414b4ceb3..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs +++ /dev/null @@ -1,789 +0,0 @@ -#[cfg(not(any(PyPy, GraalPy)))] -use crate::Py_hash_t; -use crate::{PyObject, Py_UCS1, Py_UCS2, Py_UCS4, Py_ssize_t}; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_uint, c_void}; - -// skipped Py_UNICODE_ISSPACE() -// skipped Py_UNICODE_ISLOWER() -// skipped Py_UNICODE_ISUPPER() -// skipped Py_UNICODE_ISTITLE() -// skipped Py_UNICODE_ISLINEBREAK -// skipped Py_UNICODE_TOLOWER -// skipped Py_UNICODE_TOUPPER -// skipped Py_UNICODE_TOTITLE -// skipped Py_UNICODE_ISDECIMAL -// skipped Py_UNICODE_ISDIGIT -// skipped Py_UNICODE_ISNUMERIC -// skipped Py_UNICODE_ISPRINTABLE -// skipped Py_UNICODE_TODECIMAL -// skipped Py_UNICODE_TODIGIT -// skipped Py_UNICODE_TONUMERIC -// skipped Py_UNICODE_ISALPHA -// skipped Py_UNICODE_ISALNUM -// skipped Py_UNICODE_COPY -// skipped Py_UNICODE_FILL -// skipped Py_UNICODE_IS_SURROGATE -// skipped Py_UNICODE_IS_HIGH_SURROGATE -// skipped Py_UNICODE_IS_LOW_SURROGATE -// skipped Py_UNICODE_JOIN_SURROGATES -// skipped Py_UNICODE_HIGH_SURROGATE -// skipped Py_UNICODE_LOW_SURROGATE - -// generated by bindgen v0.63.0 (with small adaptations) -#[repr(C)] -struct BitfieldUnit { - storage: Storage, -} - -impl BitfieldUnit { - #[inline] - pub const fn new(storage: Storage) -> Self { - Self { storage } - } -} - -#[cfg(not(GraalPy))] -impl BitfieldUnit -where - Storage: AsRef<[u8]> + AsMut<[u8]>, -{ - #[inline] - fn get_bit(&self, index: usize) -> bool { - debug_assert!(index / 8 < self.storage.as_ref().len()); - let byte_index = index / 8; - let byte = self.storage.as_ref()[byte_index]; - let bit_index = if cfg!(target_endian = "big") { - 7 - (index % 8) - } else { - index % 8 - }; - let mask = 1 << bit_index; - byte & mask == mask - } - - #[inline] - fn set_bit(&mut self, index: usize, val: bool) { - debug_assert!(index / 8 < self.storage.as_ref().len()); - let byte_index = index / 8; - let byte = &mut self.storage.as_mut()[byte_index]; - let bit_index = if cfg!(target_endian = "big") { - 7 - (index % 8) - } else { - index % 8 - }; - let mask = 1 << bit_index; - if val { - *byte |= mask; - } else { - *byte &= !mask; - } - } - - #[inline] - fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { - debug_assert!(bit_width <= 64); - debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); - debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); - let mut val = 0; - for i in 0..(bit_width as usize) { - if self.get_bit(i + bit_offset) { - let index = if cfg!(target_endian = "big") { - bit_width as usize - 1 - i - } else { - i - }; - val |= 1 << index; - } - } - val - } - - #[inline] - fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) { - debug_assert!(bit_width <= 64); - debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); - debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); - for i in 0..(bit_width as usize) { - let mask = 1 << i; - let val_bit_is_set = val & mask == mask; - let index = if cfg!(target_endian = "big") { - bit_width as usize - 1 - i - } else { - i - }; - self.set_bit(index + bit_offset, val_bit_is_set); - } - } -} - -#[cfg(not(GraalPy))] -const STATE_INTERNED_INDEX: usize = 0; -#[cfg(not(GraalPy))] -const STATE_INTERNED_WIDTH: u8 = 2; - -#[cfg(not(GraalPy))] -const STATE_KIND_INDEX: usize = STATE_INTERNED_WIDTH as usize; -#[cfg(not(GraalPy))] -const STATE_KIND_WIDTH: u8 = 3; - -#[cfg(not(GraalPy))] -const STATE_COMPACT_INDEX: usize = (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH) as usize; -#[cfg(not(GraalPy))] -const STATE_COMPACT_WIDTH: u8 = 1; - -#[cfg(not(GraalPy))] -const STATE_ASCII_INDEX: usize = - (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH) as usize; -#[cfg(not(GraalPy))] -const STATE_ASCII_WIDTH: u8 = 1; - -#[cfg(not(any(Py_3_12, GraalPy)))] -const STATE_READY_INDEX: usize = - (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH + STATE_ASCII_WIDTH) as usize; -#[cfg(not(any(Py_3_12, GraalPy)))] -const STATE_READY_WIDTH: u8 = 1; - -// generated by bindgen v0.63.0 (with small adaptations) -// The same code is generated for Python 3.7, 3.8, 3.9, 3.10, and 3.11, but the "ready" field -// has been removed from Python 3.12. - -/// Wrapper around the `PyASCIIObject.state` bitfield with getters and setters that work -/// on most little- and big-endian architectures. -/// -/// Memory layout of C bitfields is implementation defined, so these functions are still -/// unsafe. Users must verify that they work as expected on the architectures they target. -#[repr(C)] -#[repr(align(4))] -struct PyASCIIObjectState { - bitfield_align: [u8; 0], - bitfield: BitfieldUnit<[u8; 4usize]>, -} - -// c_uint and u32 are not necessarily the same type on all targets / architectures -#[cfg(not(GraalPy))] -#[allow(clippy::useless_transmute)] -impl PyASCIIObjectState { - #[inline] - unsafe fn interned(&self) -> c_uint { - std::mem::transmute( - self.bitfield - .get(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH) as u32, - ) - } - - #[inline] - unsafe fn set_interned(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH, val as u64) - } - - #[inline] - unsafe fn kind(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_KIND_INDEX, STATE_KIND_WIDTH) as u32) - } - - #[inline] - unsafe fn set_kind(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_KIND_INDEX, STATE_KIND_WIDTH, val as u64) - } - - #[inline] - unsafe fn compact(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH) as u32) - } - - #[inline] - unsafe fn set_compact(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH, val as u64) - } - - #[inline] - unsafe fn ascii(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_ASCII_INDEX, STATE_ASCII_WIDTH) as u32) - } - - #[inline] - unsafe fn set_ascii(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_ASCII_INDEX, STATE_ASCII_WIDTH, val as u64) - } - - #[cfg(not(Py_3_12))] - #[inline] - unsafe fn ready(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_READY_INDEX, STATE_READY_WIDTH) as u32) - } - - #[cfg(not(Py_3_12))] - #[inline] - unsafe fn set_ready(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_READY_INDEX, STATE_READY_WIDTH, val as u64) - } -} - -impl From for PyASCIIObjectState { - #[inline] - fn from(value: u32) -> Self { - PyASCIIObjectState { - bitfield_align: [], - bitfield: BitfieldUnit::new(value.to_ne_bytes()), - } - } -} - -impl From for u32 { - #[inline] - fn from(value: PyASCIIObjectState) -> Self { - u32::from_ne_bytes(value.bitfield.storage) - } -} - -#[repr(C)] -pub struct PyASCIIObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub length: Py_ssize_t, - #[cfg(not(any(PyPy, GraalPy)))] - pub hash: Py_hash_t, - /// A bit field with various properties. - /// - /// Rust doesn't expose bitfields. So we have accessor functions for - /// retrieving values. - /// - /// unsigned int interned:2; // SSTATE_* constants. - /// unsigned int kind:3; // PyUnicode_*_KIND constants. - /// unsigned int compact:1; - /// unsigned int ascii:1; - /// unsigned int ready:1; - /// unsigned int :24; - #[cfg(not(GraalPy))] - pub state: u32, - #[cfg(not(any(Py_3_12, GraalPy)))] - pub wstr: *mut wchar_t, -} - -/// Interacting with the bitfield is not actually well-defined, so we mark these APIs unsafe. -#[cfg(not(GraalPy))] -impl PyASCIIObject { - #[cfg_attr(not(Py_3_12), allow(rustdoc::broken_intra_doc_links))] // SSTATE_INTERNED_IMMORTAL_STATIC requires 3.12 - /// Get the `interned` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns one of: [`SSTATE_NOT_INTERNED`], [`SSTATE_INTERNED_MORTAL`], - /// [`SSTATE_INTERNED_IMMORTAL`], or [`SSTATE_INTERNED_IMMORTAL_STATIC`]. - #[inline] - pub unsafe fn interned(&self) -> c_uint { - PyASCIIObjectState::from(self.state).interned() - } - - #[cfg_attr(not(Py_3_12), allow(rustdoc::broken_intra_doc_links))] // SSTATE_INTERNED_IMMORTAL_STATIC requires 3.12 - /// Set the `interned` field of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is not [`SSTATE_NOT_INTERNED`], - /// [`SSTATE_INTERNED_MORTAL`], [`SSTATE_INTERNED_IMMORTAL`], or - /// [`SSTATE_INTERNED_IMMORTAL_STATIC`] is invalid. - #[inline] - pub unsafe fn set_interned(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_interned(val); - self.state = u32::from(state); - } - - /// Get the `kind` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns one of: - #[cfg_attr(not(Py_3_12), doc = "[`PyUnicode_WCHAR_KIND`], ")] - /// [`PyUnicode_1BYTE_KIND`], [`PyUnicode_2BYTE_KIND`], or [`PyUnicode_4BYTE_KIND`]. - #[inline] - pub unsafe fn kind(&self) -> c_uint { - PyASCIIObjectState::from(self.state).kind() - } - - /// Set the `kind` field of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is not - #[cfg_attr(not(Py_3_12), doc = "[`PyUnicode_WCHAR_KIND`], ")] - /// [`PyUnicode_1BYTE_KIND`], [`PyUnicode_2BYTE_KIND`], or [`PyUnicode_4BYTE_KIND`] is invalid. - #[inline] - pub unsafe fn set_kind(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_kind(val); - self.state = u32::from(state); - } - - /// Get the `compact` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[inline] - pub unsafe fn compact(&self) -> c_uint { - PyASCIIObjectState::from(self.state).compact() - } - - /// Set the `compact` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[inline] - pub unsafe fn set_compact(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_compact(val); - self.state = u32::from(state); - } - - /// Get the `ascii` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[inline] - pub unsafe fn ascii(&self) -> c_uint { - PyASCIIObjectState::from(self.state).ascii() - } - - /// Set the `ascii` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[inline] - pub unsafe fn set_ascii(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_ascii(val); - self.state = u32::from(state); - } - - /// Get the `ready` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[cfg(not(Py_3_12))] - #[inline] - pub unsafe fn ready(&self) -> c_uint { - PyASCIIObjectState::from(self.state).ready() - } - - /// Set the `ready` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[cfg(not(Py_3_12))] - #[inline] - pub unsafe fn set_ready(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_ready(val); - self.state = u32::from(state); - } -} - -#[repr(C)] -pub struct PyCompactUnicodeObject { - pub _base: PyASCIIObject, - #[cfg(not(GraalPy))] - pub utf8_length: Py_ssize_t, - #[cfg(not(GraalPy))] - pub utf8: *mut c_char, - #[cfg(not(any(Py_3_12, GraalPy)))] - pub wstr_length: Py_ssize_t, -} - -#[repr(C)] -pub union PyUnicodeObjectData { - pub any: *mut c_void, - pub latin1: *mut Py_UCS1, - pub ucs2: *mut Py_UCS2, - pub ucs4: *mut Py_UCS4, -} - -#[repr(C)] -pub struct PyUnicodeObject { - pub _base: PyCompactUnicodeObject, - #[cfg(not(GraalPy))] - pub data: PyUnicodeObjectData, -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PyUnicode_CheckConsistency(op: *mut PyObject, check_content: c_int) -> c_int; -} - -// skipped PyUnicode_GET_SIZE -// skipped PyUnicode_GET_DATA_SIZE -// skipped PyUnicode_AS_UNICODE -// skipped PyUnicode_AS_DATA - -pub const SSTATE_NOT_INTERNED: c_uint = 0; -pub const SSTATE_INTERNED_MORTAL: c_uint = 1; -pub const SSTATE_INTERNED_IMMORTAL: c_uint = 2; -#[cfg(Py_3_12)] -pub const SSTATE_INTERNED_IMMORTAL_STATIC: c_uint = 3; - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_ASCII(op: *mut PyObject) -> c_uint { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).ascii() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_COMPACT(op: *mut PyObject) -> c_uint { - (*(op as *mut PyASCIIObject)).compact() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_COMPACT_ASCII(op: *mut PyObject) -> c_uint { - ((*(op as *mut PyASCIIObject)).ascii() != 0 && PyUnicode_IS_COMPACT(op) != 0).into() -} - -#[cfg(not(Py_3_12))] -#[deprecated(note = "Removed in Python 3.12")] -pub const PyUnicode_WCHAR_KIND: c_uint = 0; - -pub const PyUnicode_1BYTE_KIND: c_uint = 1; -pub const PyUnicode_2BYTE_KIND: c_uint = 2; -pub const PyUnicode_4BYTE_KIND: c_uint = 4; - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_1BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS1 { - PyUnicode_DATA(op) as *mut Py_UCS1 -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_2BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS2 { - PyUnicode_DATA(op) as *mut Py_UCS2 -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_4BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS4 { - PyUnicode_DATA(op) as *mut Py_UCS4 -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_KIND(op: *mut PyObject) -> c_uint { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).kind() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void { - if PyUnicode_IS_ASCII(op) != 0 { - (op as *mut PyASCIIObject).offset(1) as *mut c_void - } else { - (op as *mut PyCompactUnicodeObject).offset(1) as *mut c_void - } -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn _PyUnicode_NONCOMPACT_DATA(op: *mut PyObject) -> *mut c_void { - debug_assert!(!(*(op as *mut PyUnicodeObject)).data.any.is_null()); - - (*(op as *mut PyUnicodeObject)).data.any -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_DATA(op: *mut PyObject) -> *mut c_void { - debug_assert!(crate::PyUnicode_Check(op) != 0); - - if PyUnicode_IS_COMPACT(op) != 0 { - _PyUnicode_COMPACT_DATA(op) - } else { - _PyUnicode_NONCOMPACT_DATA(op) - } -} - -// skipped PyUnicode_WRITE -// skipped PyUnicode_READ -// skipped PyUnicode_READ_CHAR - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_GET_LENGTH(op: *mut PyObject) -> Py_ssize_t { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).length -} - -#[cfg(any(Py_3_12, GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_READY(_op: *mut PyObject) -> c_uint { - // kept in CPython for backwards compatibility - 1 -} - -#[cfg(not(any(GraalPy, Py_3_12)))] -#[inline] -pub unsafe fn PyUnicode_IS_READY(op: *mut PyObject) -> c_uint { - (*(op as *mut PyASCIIObject)).ready() -} - -#[cfg(any(Py_3_12, GraalPy))] -#[inline] -pub unsafe fn PyUnicode_READY(_op: *mut PyObject) -> c_int { - 0 -} - -#[cfg(not(any(Py_3_12, GraalPy)))] -#[inline] -pub unsafe fn PyUnicode_READY(op: *mut PyObject) -> c_int { - debug_assert!(crate::PyUnicode_Check(op) != 0); - - if PyUnicode_IS_READY(op) != 0 { - 0 - } else { - _PyUnicode_Ready(op) - } -} - -// skipped PyUnicode_MAX_CHAR_VALUE -// skipped _PyUnicode_get_wstr_length -// skipped PyUnicode_WSTR_LENGTH - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyUnicode_New")] - pub fn PyUnicode_New(size: Py_ssize_t, maxchar: Py_UCS4) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyUnicode_Ready")] - pub fn _PyUnicode_Ready(unicode: *mut PyObject) -> c_int; - - // skipped _PyUnicode_Copy - - #[cfg(not(PyPy))] - pub fn PyUnicode_CopyCharacters( - to: *mut PyObject, - to_start: Py_ssize_t, - from: *mut PyObject, - from_start: Py_ssize_t, - how_many: Py_ssize_t, - ) -> Py_ssize_t; - - // skipped _PyUnicode_FastCopyCharacters - - #[cfg(not(PyPy))] - pub fn PyUnicode_Fill( - unicode: *mut PyObject, - start: Py_ssize_t, - length: Py_ssize_t, - fill_char: Py_UCS4, - ) -> Py_ssize_t; - - // skipped _PyUnicode_FastFill - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromUnicode")] - pub fn PyUnicode_FromUnicode(u: *const wchar_t, size: Py_ssize_t) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromKindAndData")] - pub fn PyUnicode_FromKindAndData( - kind: c_int, - buffer: *const c_void, - size: Py_ssize_t, - ) -> *mut PyObject; - - // skipped _PyUnicode_FromASCII - // skipped _PyUnicode_FindMaxChar - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicode")] - pub fn PyUnicode_AsUnicode(unicode: *mut PyObject) -> *mut wchar_t; - - // skipped _PyUnicode_AsUnicode - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicodeAndSize")] - pub fn PyUnicode_AsUnicodeAndSize( - unicode: *mut PyObject, - size: *mut Py_ssize_t, - ) -> *mut wchar_t; - - // skipped PyUnicode_GetMax -} - -// skipped _PyUnicodeWriter -// skipped _PyUnicodeWriter_Init -// skipped _PyUnicodeWriter_Prepare -// skipped _PyUnicodeWriter_PrepareInternal -// skipped _PyUnicodeWriter_PrepareKind -// skipped _PyUnicodeWriter_PrepareKindInternal -// skipped _PyUnicodeWriter_WriteChar -// skipped _PyUnicodeWriter_WriteStr -// skipped _PyUnicodeWriter_WriteSubstring -// skipped _PyUnicodeWriter_WriteASCIIString -// skipped _PyUnicodeWriter_WriteLatin1String -// skipped _PyUnicodeWriter_Finish -// skipped _PyUnicodeWriter_Dealloc -// skipped _PyUnicode_FormatAdvancedWriter - -extern "C" { - // skipped _PyUnicode_AsStringAndSize - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8")] - pub fn PyUnicode_AsUTF8(unicode: *mut PyObject) -> *const c_char; - - // skipped _PyUnicode_AsString - - pub fn PyUnicode_Encode( - s: *const wchar_t, - size: Py_ssize_t, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeUTF7( - data: *const wchar_t, - length: Py_ssize_t, - base64SetO: c_int, - base64WhiteSpace: c_int, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF7 - // skipped _PyUnicode_AsUTF8String - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeUTF8")] - pub fn PyUnicode_EncodeUTF8( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeUTF32( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - byteorder: c_int, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF32 - - pub fn PyUnicode_EncodeUTF16( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - byteorder: c_int, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF16 - // skipped _PyUnicode_DecodeUnicodeEscape - - pub fn PyUnicode_EncodeUnicodeEscape(data: *const wchar_t, length: Py_ssize_t) - -> *mut PyObject; - - pub fn PyUnicode_EncodeRawUnicodeEscape( - data: *const wchar_t, - length: Py_ssize_t, - ) -> *mut PyObject; - - // skipped _PyUnicode_AsLatin1String - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeLatin1")] - pub fn PyUnicode_EncodeLatin1( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_AsASCIIString - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeASCII")] - pub fn PyUnicode_EncodeASCII( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeCharmap( - data: *const wchar_t, - length: Py_ssize_t, - mapping: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeCharmap - - pub fn PyUnicode_TranslateCharmap( - data: *const wchar_t, - length: Py_ssize_t, - table: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped PyUnicode_EncodeMBCS - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeDecimal")] - pub fn PyUnicode_EncodeDecimal( - s: *mut wchar_t, - length: Py_ssize_t, - output: *mut c_char, - errors: *const c_char, - ) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_TransformDecimalToASCII")] - pub fn PyUnicode_TransformDecimalToASCII(s: *mut wchar_t, length: Py_ssize_t) -> *mut PyObject; - - // skipped _PyUnicode_TransformDecimalAndSpaceToASCII -} - -// skipped _PyUnicode_JoinArray -// skipped _PyUnicode_EqualToASCIIId -// skipped _PyUnicode_EqualToASCIIString -// skipped _PyUnicode_XStrip -// skipped _PyUnicode_InsertThousandsGrouping - -// skipped _Py_ascii_whitespace - -// skipped _PyUnicode_IsLowercase -// skipped _PyUnicode_IsUppercase -// skipped _PyUnicode_IsTitlecase -// skipped _PyUnicode_IsXidStart -// skipped _PyUnicode_IsXidContinue -// skipped _PyUnicode_IsWhitespace -// skipped _PyUnicode_IsLinebreak -// skipped _PyUnicode_ToLowercase -// skipped _PyUnicode_ToUppercase -// skipped _PyUnicode_ToTitlecase -// skipped _PyUnicode_ToLowerFull -// skipped _PyUnicode_ToTitleFull -// skipped _PyUnicode_ToUpperFull -// skipped _PyUnicode_ToFoldedFull -// skipped _PyUnicode_IsCaseIgnorable -// skipped _PyUnicode_IsCased -// skipped _PyUnicode_ToDecimalDigit -// skipped _PyUnicode_ToDigit -// skipped _PyUnicode_ToNumeric -// skipped _PyUnicode_IsDecimalDigit -// skipped _PyUnicode_IsDigit -// skipped _PyUnicode_IsNumeric -// skipped _PyUnicode_IsPrintable -// skipped _PyUnicode_IsAlpha -// skipped Py_UNICODE_strlen -// skipped Py_UNICODE_strcpy -// skipped Py_UNICODE_strcat -// skipped Py_UNICODE_strncpy -// skipped Py_UNICODE_strcmp -// skipped Py_UNICODE_strncmp -// skipped Py_UNICODE_strchr -// skipped Py_UNICODE_strrchr -// skipped _PyUnicode_FormatLong -// skipped PyUnicode_AsUnicodeCopy -// skipped _PyUnicode_FromId -// skipped _PyUnicode_EQ -// skipped _PyUnicode_ScanIdentifier diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs deleted file mode 100644 index 88bb501bcc..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs +++ /dev/null @@ -1,17 +0,0 @@ -#[cfg(not(any(PyPy, GraalPy)))] -pub struct _PyWeakReference { - pub ob_base: crate::PyObject, - pub wr_object: *mut crate::PyObject, - pub wr_callback: *mut crate::PyObject, - pub hash: crate::Py_hash_t, - pub wr_prev: *mut crate::PyWeakReference, - pub wr_next: *mut crate::PyWeakReference, - #[cfg(Py_3_11)] - pub vectorcall: Option, - #[cfg(all(Py_3_13, Py_GIL_DISABLED))] - pub weakrefs_lock: *mut crate::PyMutex, -} - -// skipped _PyWeakref_GetWeakrefCount -// skipped _PyWeakref_ClearRef -// skipped PyWeakRef_GET_OBJECT diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/datetime.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/datetime.rs deleted file mode 100644 index 7283b6d4e5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/datetime.rs +++ /dev/null @@ -1,746 +0,0 @@ -//! FFI bindings to the functions and structs defined in `datetime.h` -//! -//! This is the unsafe thin wrapper around the [CPython C API](https://docs.python.org/3/c-api/datetime.html), -//! and covers the various date and time related objects in the Python `datetime` -//! standard library module. - -#[cfg(GraalPy)] -use crate::{PyLong_AsLong, PyLong_Check, PyObject_GetAttrString, Py_DecRef}; -use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_TYPE}; -use std::cell::UnsafeCell; -#[cfg(not(GraalPy))] -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr; -#[cfg(not(PyPy))] -use {crate::PyCapsule_Import, std::ffi::CString}; -#[cfg(not(any(PyPy, GraalPy)))] -use {crate::Py_hash_t, std::os::raw::c_uchar}; -// Type struct wrappers -const _PyDateTime_DATE_DATASIZE: usize = 4; -const _PyDateTime_TIME_DATASIZE: usize = 6; -const _PyDateTime_DATETIME_DATASIZE: usize = 10; - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.timedelta`. -pub struct PyDateTime_Delta { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub hashcode: Py_hash_t, - #[cfg(not(GraalPy))] - pub days: c_int, - #[cfg(not(GraalPy))] - pub seconds: c_int, - #[cfg(not(GraalPy))] - pub microseconds: c_int, -} - -// skipped non-limited PyDateTime_TZInfo -// skipped non-limited _PyDateTime_BaseTZInfo - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.time` without a `tzinfo` member. -pub struct _PyDateTime_BaseTime { - pub ob_base: PyObject, - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - pub data: [c_uchar; _PyDateTime_TIME_DATASIZE], -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.time`. -pub struct PyDateTime_Time { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub hashcode: Py_hash_t, - #[cfg(not(GraalPy))] - pub hastzinfo: c_char, - #[cfg(not(any(PyPy, GraalPy)))] - pub data: [c_uchar; _PyDateTime_TIME_DATASIZE], - #[cfg(not(any(PyPy, GraalPy)))] - pub fold: c_uchar, - /// # Safety - /// - /// Care should be taken when reading this field. If the time does not have a - /// tzinfo then CPython may allocate as a `_PyDateTime_BaseTime` without this field. - #[cfg(not(GraalPy))] - pub tzinfo: *mut PyObject, -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.date` -pub struct PyDateTime_Date { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub hashcode: Py_hash_t, - #[cfg(not(any(PyPy, GraalPy)))] - pub hastzinfo: c_char, - #[cfg(not(any(PyPy, GraalPy)))] - pub data: [c_uchar; _PyDateTime_DATE_DATASIZE], -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.datetime` without a `tzinfo` member. -pub struct _PyDateTime_BaseDateTime { - pub ob_base: PyObject, - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE], -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.datetime`. -pub struct PyDateTime_DateTime { - pub ob_base: PyObject, - #[cfg(not(any(PyPy, GraalPy)))] - pub hashcode: Py_hash_t, - #[cfg(not(GraalPy))] - pub hastzinfo: c_char, - #[cfg(not(any(PyPy, GraalPy)))] - pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE], - #[cfg(not(any(PyPy, GraalPy)))] - pub fold: c_uchar, - /// # Safety - /// - /// Care should be taken when reading this field. If the time does not have a - /// tzinfo then CPython may allocate as a `_PyDateTime_BaseDateTime` without this field. - #[cfg(not(GraalPy))] - pub tzinfo: *mut PyObject, -} - -// skipped non-limited _PyDateTime_HAS_TZINFO - -// Accessor functions for PyDateTime_Date and PyDateTime_DateTime -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the year component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer greater than 0. -pub unsafe fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int { - // This should work for Date or DateTime - let data = (*(o as *mut PyDateTime_Date)).data; - c_int::from(data[0]) << 8 | c_int::from(data[1]) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the month component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer in the range `[1, 12]`. -pub unsafe fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int { - let data = (*(o as *mut PyDateTime_Date)).data; - c_int::from(data[2]) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the day component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[1, 31]`. -pub unsafe fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int { - let data = (*(o as *mut PyDateTime_Date)).data; - c_int::from(data[3]) -} - -// Accessor macros for times -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_HOUR { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 0]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_MINUTE { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 1]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_SECOND { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 2]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_MICROSECOND { - ($o: expr, $offset:expr) => { - (c_int::from((*$o).data[$offset + 3]) << 16) - | (c_int::from((*$o).data[$offset + 4]) << 8) - | (c_int::from((*$o).data[$offset + 5])) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_FOLD { - ($o: expr) => { - (*$o).fold - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_TZINFO { - ($o: expr) => { - if (*$o).hastzinfo != 0 { - (*$o).tzinfo - } else { - $crate::Py_None() - } - }; -} - -// Accessor functions for DateTime -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the hour component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 23]` -pub unsafe fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int { - _PyDateTime_GET_HOUR!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the minute component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MINUTE!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the second component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_SECOND!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the microsecond component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 999999]` -pub unsafe fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MICROSECOND!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the fold component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 1]` -pub unsafe fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_uchar { - _PyDateTime_GET_FOLD!(o as *mut PyDateTime_DateTime) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the tzinfo component of a `PyDateTime_DateTime`. -/// Returns a pointer to a `PyObject` that should be either NULL or an instance -/// of a `datetime.tzinfo` subclass. -pub unsafe fn PyDateTime_DATE_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - _PyDateTime_GET_TZINFO!(o as *mut PyDateTime_DateTime) -} - -// Accessor functions for Time -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the hour component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 23]` -pub unsafe fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int { - _PyDateTime_GET_HOUR!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the minute component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MINUTE!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the second component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_SECOND!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the microsecond component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 999999]` -pub unsafe fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MICROSECOND!((o as *mut PyDateTime_Time), 0) -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[inline] -/// Retrieve the fold component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 1]` -pub unsafe fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_uchar { - _PyDateTime_GET_FOLD!(o as *mut PyDateTime_Time) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the tzinfo component of a `PyDateTime_Time`. -/// Returns a pointer to a `PyObject` that should be either NULL or an instance -/// of a `datetime.tzinfo` subclass. -pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - _PyDateTime_GET_TZINFO!(o as *mut PyDateTime_Time) -} - -// Accessor functions -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _access_field { - ($obj:expr, $type: ident, $field:ident) => { - (*($obj as *mut $type)).$field - }; -} - -// Accessor functions for PyDateTime_Delta -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _access_delta_field { - ($obj:expr, $field:ident) => { - _access_field!($obj, PyDateTime_Delta, $field) - }; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the days component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [-999999999, 999999999]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, days) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the seconds component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [0, 86399]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, seconds) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the seconds component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [0, 999999]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, microseconds) -} - -// Accessor functions for GraalPy. The macros on GraalPy work differently, -// but copying them seems suboptimal -#[inline] -#[cfg(GraalPy)] -pub unsafe fn _get_attr(obj: *mut PyObject, field: &std::ffi::CStr) -> c_int { - let result = PyObject_GetAttrString(obj, field.as_ptr()); - Py_DecRef(result); // the original macros are borrowing - if PyLong_Check(result) == 1 { - PyLong_AsLong(result) as c_int - } else { - 0 - } -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("year")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("month")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("day")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("hour")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("minute")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("second")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microsecond")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("fold")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - let res = PyObject_GetAttrString(o, c_str!("tzinfo").as_ptr().cast()); - Py_DecRef(res); // the original macros are borrowing - res -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("hour")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("minute")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("second")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microsecond")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("fold")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - let res = PyObject_GetAttrString(o, c_str!("tzinfo").as_ptr().cast()); - Py_DecRef(res); // the original macros are borrowing - res -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("days")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("seconds")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microseconds")) -} - -#[cfg(PyPy)] -extern "C" { - // skipped _PyDateTime_HAS_TZINFO (not in PyPy) - #[link_name = "PyPyDateTime_GET_YEAR"] - pub fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_MONTH"] - pub fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_DAY"] - pub fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int; - - #[link_name = "PyPyDateTime_DATE_GET_HOUR"] - pub fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_MINUTE"] - pub fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_SECOND"] - pub fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_MICROSECOND"] - pub fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_FOLD"] - pub fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_int; - // skipped PyDateTime_DATE_GET_TZINFO (not in PyPy) - - #[link_name = "PyPyDateTime_TIME_GET_HOUR"] - pub fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_MINUTE"] - pub fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_SECOND"] - pub fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_MICROSECOND"] - pub fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_FOLD"] - pub fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_int; - // skipped PyDateTime_TIME_GET_TZINFO (not in PyPy) - - #[link_name = "PyPyDateTime_DELTA_GET_DAYS"] - pub fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DELTA_GET_SECONDS"] - pub fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DELTA_GET_MICROSECONDS"] - pub fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int; -} - -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct PyDateTime_CAPI { - pub DateType: *mut PyTypeObject, - pub DateTimeType: *mut PyTypeObject, - pub TimeType: *mut PyTypeObject, - pub DeltaType: *mut PyTypeObject, - pub TZInfoType: *mut PyTypeObject, - pub TimeZone_UTC: *mut PyObject, - pub Date_FromDate: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub DateTime_FromDateAndTime: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Time_FromTime: unsafe extern "C" fn( - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Delta_FromDelta: unsafe extern "C" fn( - days: c_int, - seconds: c_int, - microseconds: c_int, - normalize: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub TimeZone_FromTimeZone: - unsafe extern "C" fn(offset: *mut PyObject, name: *mut PyObject) -> *mut PyObject, - - pub DateTime_FromTimestamp: unsafe extern "C" fn( - cls: *mut PyTypeObject, - args: *mut PyObject, - kwargs: *mut PyObject, - ) -> *mut PyObject, - pub Date_FromTimestamp: - unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject, - pub DateTime_FromDateAndTimeAndFold: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - fold: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Time_FromTimeAndFold: unsafe extern "C" fn( - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - fold: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, -} - -// Python already shares this object between threads, so it's no more evil for us to do it too! -unsafe impl Sync for PyDateTime_CAPI {} - -/// Returns a pointer to a `PyDateTime_CAPI` instance -/// -/// # Note -/// This function will return a null pointer until -/// `PyDateTime_IMPORT` is called -#[inline] -pub unsafe fn PyDateTimeAPI() -> *mut PyDateTime_CAPI { - *PyDateTimeAPI_impl.0.get() -} - -#[inline] -pub unsafe fn PyDateTime_TimeZone_UTC() -> *mut PyObject { - (*PyDateTimeAPI()).TimeZone_UTC -} - -/// Populates the `PyDateTimeAPI` object -pub unsafe fn PyDateTime_IMPORT() { - // PyPy expects the C-API to be initialized via PyDateTime_Import, so trying to use - // `PyCapsule_Import` will behave unexpectedly in pypy. - #[cfg(PyPy)] - let py_datetime_c_api = PyDateTime_Import(); - - #[cfg(not(PyPy))] - let py_datetime_c_api = { - // PyDateTime_CAPSULE_NAME is a macro in C - let PyDateTime_CAPSULE_NAME = CString::new("datetime.datetime_CAPI").unwrap(); - - PyCapsule_Import(PyDateTime_CAPSULE_NAME.as_ptr(), 1) as *mut PyDateTime_CAPI - }; - - *PyDateTimeAPI_impl.0.get() = py_datetime_c_api; -} - -// skipped non-limited PyDateTime_TimeZone_UTC - -/// Type Check macros -/// -/// These are bindings around the C API typecheck macros, all of them return -/// `1` if True and `0` if False. In all type check macros, the argument (`op`) -/// must not be `NULL`. -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DateType` or subtype. -pub unsafe fn PyDate_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DateType`. -pub unsafe fn PyDate_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DateType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DateTimeType` or subtype. -pub unsafe fn PyDateTime_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateTimeType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DateTimeType`. -pub unsafe fn PyDateTime_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DateTimeType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.TimeType` or subtype. -pub unsafe fn PyTime_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).TimeType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.TimeType`. -pub unsafe fn PyTime_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).TimeType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DetaType` or subtype. -pub unsafe fn PyDelta_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DeltaType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DeltaType`. -pub unsafe fn PyDelta_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DeltaType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.TZInfoType` or subtype. -pub unsafe fn PyTZInfo_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).TZInfoType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.TZInfoType`. -pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).TZInfoType) as c_int -} - -// skipped non-limited PyDate_FromDate -// skipped non-limited PyDateTime_FromDateAndTime -// skipped non-limited PyDateTime_FromDateAndTimeAndFold -// skipped non-limited PyTime_FromTime -// skipped non-limited PyTime_FromTimeAndFold -// skipped non-limited PyDelta_FromDSU - -pub unsafe fn PyTimeZone_FromOffset(offset: *mut PyObject) -> *mut PyObject { - ((*PyDateTimeAPI()).TimeZone_FromTimeZone)(offset, std::ptr::null_mut()) -} - -pub unsafe fn PyTimeZone_FromOffsetAndName( - offset: *mut PyObject, - name: *mut PyObject, -) -> *mut PyObject { - ((*PyDateTimeAPI()).TimeZone_FromTimeZone)(offset, name) -} - -#[cfg(not(PyPy))] -pub unsafe fn PyDateTime_FromTimestamp(args: *mut PyObject) -> *mut PyObject { - let f = (*PyDateTimeAPI()).DateTime_FromTimestamp; - f((*PyDateTimeAPI()).DateTimeType, args, std::ptr::null_mut()) -} - -#[cfg(not(PyPy))] -pub unsafe fn PyDate_FromTimestamp(args: *mut PyObject) -> *mut PyObject { - let f = (*PyDateTimeAPI()).Date_FromTimestamp; - f((*PyDateTimeAPI()).DateType, args) -} - -#[cfg(PyPy)] -extern "C" { - #[link_name = "PyPyDate_FromTimestamp"] - pub fn PyDate_FromTimestamp(args: *mut PyObject) -> *mut PyObject; - #[link_name = "PyPyDateTime_FromTimestamp"] - pub fn PyDateTime_FromTimestamp(args: *mut PyObject) -> *mut PyObject; -} - -#[cfg(PyPy)] -extern "C" { - #[link_name = "_PyPyDateTime_Import"] - pub fn PyDateTime_Import() -> *mut PyDateTime_CAPI; -} - -// Rust specific implementation details - -struct PyDateTimeAPISingleton(UnsafeCell<*mut PyDateTime_CAPI>); -unsafe impl Sync for PyDateTimeAPISingleton {} - -static PyDateTimeAPI_impl: PyDateTimeAPISingleton = - PyDateTimeAPISingleton(UnsafeCell::new(ptr::null_mut())); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/descrobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/descrobject.rs deleted file mode 100644 index f4a5ce00bf..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/descrobject.rs +++ /dev/null @@ -1,133 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::object::{PyObject, PyTypeObject}; -use crate::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr; - -pub type getter = unsafe extern "C" fn(slf: *mut PyObject, closure: *mut c_void) -> *mut PyObject; -pub type setter = - unsafe extern "C" fn(slf: *mut PyObject, value: *mut PyObject, closure: *mut c_void) -> c_int; - -/// Represents the [PyGetSetDef](https://docs.python.org/3/c-api/structures.html#c.PyGetSetDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must ensure that -/// `name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct PyGetSetDef { - pub name: *const c_char, - pub get: Option, - pub set: Option, - pub doc: *const c_char, - pub closure: *mut c_void, -} - -impl Default for PyGetSetDef { - fn default() -> PyGetSetDef { - PyGetSetDef { - name: ptr::null(), - get: None, - set: None, - doc: ptr::null(), - closure: ptr::null_mut(), - } - } -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyClassMethodDescr_Type")] - pub static mut PyClassMethodDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyGetSetDescr_Type")] - pub static mut PyGetSetDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyMemberDescr_Type")] - pub static mut PyMemberDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyMethodDescr_Type")] - pub static mut PyMethodDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyWrapperDescr_Type")] - pub static mut PyWrapperDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyDictProxy_Type")] - pub static mut PyDictProxy_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyProperty_Type")] - pub static mut PyProperty_Type: PyTypeObject; -} - -extern "C" { - pub fn PyDescr_NewMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewClassMethod")] - pub fn PyDescr_NewClassMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) - -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewMember")] - pub fn PyDescr_NewMember(arg1: *mut PyTypeObject, arg2: *mut PyMemberDef) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewGetSet")] - pub fn PyDescr_NewGetSet(arg1: *mut PyTypeObject, arg2: *mut PyGetSetDef) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyDictProxy_New")] - pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; -} - -/// Represents the [PyMemberDef](https://docs.python.org/3/c-api/structures.html#c.PyMemberDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must always ensure that -/// `name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct PyMemberDef { - pub name: *const c_char, - pub type_code: c_int, - pub offset: Py_ssize_t, - pub flags: c_int, - pub doc: *const c_char, -} - -impl Default for PyMemberDef { - fn default() -> PyMemberDef { - PyMemberDef { - name: ptr::null_mut(), - type_code: 0, - offset: 0, - flags: 0, - doc: ptr::null_mut(), - } - } -} - -/* Types */ -pub const Py_T_SHORT: c_int = 0; -pub const Py_T_INT: c_int = 1; -pub const Py_T_LONG: c_int = 2; -pub const Py_T_FLOAT: c_int = 3; -pub const Py_T_DOUBLE: c_int = 4; -pub const Py_T_STRING: c_int = 5; -#[deprecated(note = "Use Py_T_OBJECT_EX instead")] -pub const _Py_T_OBJECT: c_int = 6; -pub const Py_T_CHAR: c_int = 7; -pub const Py_T_BYTE: c_int = 8; -pub const Py_T_UBYTE: c_int = 9; -pub const Py_T_USHORT: c_int = 10; -pub const Py_T_UINT: c_int = 11; -pub const Py_T_ULONG: c_int = 12; -pub const Py_T_STRING_INPLACE: c_int = 13; -pub const Py_T_BOOL: c_int = 14; -pub const Py_T_OBJECT_EX: c_int = 16; -pub const Py_T_LONGLONG: c_int = 17; -pub const Py_T_ULONGLONG: c_int = 18; -pub const Py_T_PYSSIZET: c_int = 19; -#[deprecated(note = "Value is always none")] -pub const _Py_T_NONE: c_int = 20; - -/* Flags */ -pub const Py_READONLY: c_int = 1; -#[cfg(Py_3_10)] -pub const Py_AUDIT_READ: c_int = 2; // Added in 3.10, harmless no-op before that -#[deprecated] -pub const _Py_WRITE_RESTRICTED: c_int = 4; // Deprecated, no-op. Do not reuse the value. -pub const Py_RELATIVE_OFFSET: c_int = 8; - -extern "C" { - pub fn PyMember_GetOne(addr: *const c_char, l: *mut PyMemberDef) -> *mut PyObject; - pub fn PyMember_SetOne(addr: *mut c_char, l: *mut PyMemberDef, value: *mut PyObject) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/dictobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/dictobject.rs deleted file mode 100644 index 710be80243..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/dictobject.rs +++ /dev/null @@ -1,121 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyDict_Type")] - pub static mut PyDict_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS) -} - -#[inline] -pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDict_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyDict_New")] - pub fn PyDict_New() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItem")] - pub fn PyDict_GetItem(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemWithError")] - pub fn PyDict_GetItemWithError(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_SetItem")] - pub fn PyDict_SetItem(mp: *mut PyObject, key: *mut PyObject, item: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_DelItem")] - pub fn PyDict_DelItem(mp: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Clear")] - pub fn PyDict_Clear(mp: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyDict_Next")] - pub fn PyDict_Next( - mp: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - value: *mut *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Keys")] - pub fn PyDict_Keys(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Values")] - pub fn PyDict_Values(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Items")] - pub fn PyDict_Items(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Size")] - pub fn PyDict_Size(mp: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyDict_Copy")] - pub fn PyDict_Copy(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Contains")] - pub fn PyDict_Contains(mp: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Update")] - pub fn PyDict_Update(mp: *mut PyObject, other: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Merge")] - pub fn PyDict_Merge(mp: *mut PyObject, other: *mut PyObject, _override: c_int) -> c_int; - pub fn PyDict_MergeFromSeq2(d: *mut PyObject, seq2: *mut PyObject, _override: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemString")] - pub fn PyDict_GetItemString(dp: *mut PyObject, key: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_SetItemString")] - pub fn PyDict_SetItemString( - dp: *mut PyObject, - key: *const c_char, - item: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_DelItemString")] - pub fn PyDict_DelItemString(dp: *mut PyObject, key: *const c_char) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemRef")] - pub fn PyDict_GetItemRef( - dp: *mut PyObject, - key: *mut PyObject, - result: *mut *mut PyObject, - ) -> c_int; - // skipped 3.10 / ex-non-limited PyObject_GenericGetDict -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyDictKeys_Type: PyTypeObject; - pub static mut PyDictValues_Type: PyTypeObject; - pub static mut PyDictItems_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictKeys_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictValues_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictItems_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictViewSet_Check(op: *mut PyObject) -> c_int { - (PyDictKeys_Check(op) != 0 || PyDictItems_Check(op) != 0) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyDictIterKey_Type: PyTypeObject; - pub static mut PyDictIterValue_Type: PyTypeObject; - pub static mut PyDictIterItem_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterKey_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterValue_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterItem_Type: PyTypeObject; -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985) -opaque_struct!(PyDictObject); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/enumobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/enumobject.rs deleted file mode 100644 index e3f187d16c..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/enumobject.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::object::PyTypeObject; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyEnum_Type: PyTypeObject; - pub static mut PyReversed_Type: PyTypeObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/fileobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/fileobject.rs deleted file mode 100644 index 91618ab7fd..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/fileobject.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int}; - -pub const PY_STDIOTEXTMODE: &str = "b"; - -extern "C" { - pub fn PyFile_FromFd( - arg1: c_int, - arg2: *const c_char, - arg3: *const c_char, - arg4: c_int, - arg5: *const c_char, - arg6: *const c_char, - arg7: *const c_char, - arg8: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFile_GetLine")] - pub fn PyFile_GetLine(arg1: *mut PyObject, arg2: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFile_WriteObject")] - pub fn PyFile_WriteObject(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyFile_WriteString")] - pub fn PyFile_WriteString(arg1: *const c_char, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyFile_AsFileDescriptor")] - pub fn PyObject_AsFileDescriptor(arg1: *mut PyObject) -> c_int; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[deprecated(note = "Python 3.12")] - pub static mut Py_FileSystemDefaultEncoding: *const c_char; - #[deprecated(note = "Python 3.12")] - pub static mut Py_FileSystemDefaultEncodeErrors: *const c_char; - #[deprecated(note = "Python 3.12")] - pub static mut Py_HasFileSystemDefaultEncoding: c_int; - // skipped 3.12-deprecated Py_UTF8Mode -} - -// skipped _PyIsSelectable_fd diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/fileutils.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/fileutils.rs deleted file mode 100644 index 3f053b72b5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/fileutils.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::pyport::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::c_char; - -extern "C" { - pub fn Py_DecodeLocale(arg1: *const c_char, size: *mut Py_ssize_t) -> *mut wchar_t; - - pub fn Py_EncodeLocale(text: *const wchar_t, error_pos: *mut Py_ssize_t) -> *mut c_char; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/floatobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/floatobject.rs deleted file mode 100644 index 65fc1d4c31..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/floatobject.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_double, c_int}; -use std::ptr::addr_of_mut; - -#[cfg(Py_LIMITED_API)] -// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985) -opaque_struct!(PyFloatObject); - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyFloat_Type")] - pub static mut PyFloat_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyFloat_Type)) -} - -#[inline] -pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyFloat_Type)) as c_int -} - -// skipped Py_RETURN_NAN -// skipped Py_RETURN_INF - -extern "C" { - pub fn PyFloat_GetMax() -> c_double; - pub fn PyFloat_GetMin() -> c_double; - pub fn PyFloat_GetInfo() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_FromString")] - pub fn PyFloat_FromString(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_FromDouble")] - pub fn PyFloat_FromDouble(arg1: c_double) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_AsDouble")] - pub fn PyFloat_AsDouble(arg1: *mut PyObject) -> c_double; -} - -// skipped non-limited _PyFloat_Pack2 -// skipped non-limited _PyFloat_Pack4 -// skipped non-limited _PyFloat_Pack8 -// skipped non-limited _PyFloat_Unpack2 -// skipped non-limited _PyFloat_Unpack4 -// skipped non-limited _PyFloat_Unpack8 -// skipped non-limited _PyFloat_DebugMallocStats -// skipped non-limited _PyFloat_FormatAdvancedWriter diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/impl_/mod.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/impl_/mod.rs deleted file mode 100644 index 3058e852e6..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/impl_/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -#[cfg(Py_GIL_DISABLED)] -mod atomic_c_ulong { - pub struct GetAtomicCULong(); - - pub trait AtomicCULongType { - type Type; - } - impl AtomicCULongType for GetAtomicCULong<32> { - type Type = std::sync::atomic::AtomicU32; - } - impl AtomicCULongType for GetAtomicCULong<64> { - type Type = std::sync::atomic::AtomicU64; - } - - pub type TYPE = - () * 8 }> as AtomicCULongType>::Type; -} - -/// Typedef for an atomic integer to match the platform-dependent c_ulong type. -#[cfg(Py_GIL_DISABLED)] -#[doc(hidden)] -pub type AtomicCULong = atomic_c_ulong::TYPE; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/import.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/import.rs deleted file mode 100644 index e15a37b0a7..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/import.rs +++ /dev/null @@ -1,84 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int, c_long}; - -extern "C" { - pub fn PyImport_GetMagicNumber() -> c_long; - pub fn PyImport_GetMagicTag() -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyImport_ExecCodeModule")] - pub fn PyImport_ExecCodeModule(name: *const c_char, co: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ExecCodeModuleEx")] - pub fn PyImport_ExecCodeModuleEx( - name: *const c_char, - co: *mut PyObject, - pathname: *const c_char, - ) -> *mut PyObject; - pub fn PyImport_ExecCodeModuleWithPathnames( - name: *const c_char, - co: *mut PyObject, - pathname: *const c_char, - cpathname: *const c_char, - ) -> *mut PyObject; - pub fn PyImport_ExecCodeModuleObject( - name: *mut PyObject, - co: *mut PyObject, - pathname: *mut PyObject, - cpathname: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_GetModuleDict")] - pub fn PyImport_GetModuleDict() -> *mut PyObject; - // skipped Python 3.7 / ex-non-limited PyImport_GetModule - pub fn PyImport_AddModuleObject(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_AddModule")] - pub fn PyImport_AddModule(name: *const c_char) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyImport_AddModuleRef")] - pub fn PyImport_AddModuleRef(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModule")] - pub fn PyImport_ImportModule(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleNoBlock")] - pub fn PyImport_ImportModuleNoBlock(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleLevel")] - pub fn PyImport_ImportModuleLevel( - name: *const c_char, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, - level: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleLevelObject")] - pub fn PyImport_ImportModuleLevelObject( - name: *mut PyObject, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, - level: c_int, - ) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyImport_ImportModuleEx( - name: *const c_char, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, -) -> *mut PyObject { - PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0) -} - -extern "C" { - pub fn PyImport_GetImporter(path: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_Import")] - pub fn PyImport_Import(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ReloadModule")] - pub fn PyImport_ReloadModule(m: *mut PyObject) -> *mut PyObject; - #[cfg(not(Py_3_9))] - #[deprecated(note = "Removed in Python 3.9 as it was \"For internal use only\".")] - pub fn PyImport_Cleanup(); - pub fn PyImport_ImportFrozenModuleObject(name: *mut PyObject) -> c_int; - pub fn PyImport_ImportFrozenModule(name: *const c_char) -> c_int; - - pub fn PyImport_AppendInittab( - name: *const c_char, - initfunc: Option *mut PyObject>, - ) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/intrcheck.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/intrcheck.rs deleted file mode 100644 index 3d8a58f7f2..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/intrcheck.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::os::raw::c_int; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_InterruptOccurred")] - pub fn PyOS_InterruptOccurred() -> c_int; - #[cfg(not(Py_3_10))] - #[deprecated(note = "Not documented in Python API; see Python 3.10 release notes")] - pub fn PyOS_InitInterrupts(); - - pub fn PyOS_BeforeFork(); - pub fn PyOS_AfterFork_Parent(); - pub fn PyOS_AfterFork_Child(); - #[deprecated(note = "use PyOS_AfterFork_Child instead")] - #[cfg_attr(PyPy, link_name = "PyPyOS_AfterFork")] - pub fn PyOS_AfterFork(); - - // skipped non-limited _PyOS_IsMainThread - // skipped non-limited Windows _PyOS_SigintEvent -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/iterobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/iterobject.rs deleted file mode 100644 index aa0c7b26db..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/iterobject.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PySeqIter_Type: PyTypeObject; - pub static mut PyCallIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PySeqIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PySeqIter_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySeqIter_New")] - pub fn PySeqIter_New(arg1: *mut PyObject) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyCallIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCallIter_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCallIter_New")] - pub fn PyCallIter_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/lib.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/lib.rs deleted file mode 100644 index c615740112..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/lib.rs +++ /dev/null @@ -1,461 +0,0 @@ -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -//! Raw FFI declarations for Python's C API. -//! -//! PyO3 can be used to write native Python modules or run Python code and modules from Rust. -//! -//! This crate just provides low level bindings to the Python interpreter. -//! It is meant for advanced users only - regular PyO3 users shouldn't -//! need to interact with this crate at all. -//! -//! The contents of this crate are not documented here, as it would entail -//! basically copying the documentation from CPython. Consult the [Python/C API Reference -//! Manual][capi] for up-to-date documentation. -//! -//! # Safety -//! -//! The functions in this crate lack individual safety documentation, but -//! generally the following apply: -//! - Pointer arguments have to point to a valid Python object of the correct type, -//! although null pointers are sometimes valid input. -//! - The vast majority can only be used safely while the GIL is held. -//! - Some functions have additional safety requirements, consult the -//! [Python/C API Reference Manual][capi] -//! for more information. -//! -//! -//! # Feature flags -//! -//! PyO3 uses [feature flags] to enable you to opt-in to additional functionality. For a detailed -//! description, see the [Features chapter of the guide]. -//! -//! ## Optional feature flags -//! -//! The following features customize PyO3's behavior: -//! -//! - `abi3`: Restricts PyO3's API to a subset of the full Python API which is guaranteed by -//! [PEP 384] to be forward-compatible with future Python versions. -//! - `extension-module`: This will tell the linker to keep the Python symbols unresolved, so that -//! your module can also be used with statically linked Python interpreters. Use this feature when -//! building an extension module. -//! -//! ## `rustc` environment flags -//! -//! PyO3 uses `rustc`'s `--cfg` flags to enable or disable code used for different Python versions. -//! If you want to do this for your own crate, you can do so with the [`pyo3-build-config`] crate. -//! -//! - `Py_3_7`, `Py_3_8`, `Py_3_9`, `Py_3_10`: Marks code that is only enabled when -//! compiling for a given minimum Python version. -//! - `Py_LIMITED_API`: Marks code enabled when the `abi3` feature flag is enabled. -//! - `PyPy` - Marks code enabled when compiling for PyPy. -//! -//! # Minimum supported Rust and Python versions -//! -//! `pyo3-ffi` supports the following Python distributions: -//! - CPython 3.7 or greater -//! - PyPy 7.3 (Python 3.9+) -//! - GraalPy 24.0 or greater (Python 3.10+) -//! -//! # Example: Building Python Native modules -//! -//! PyO3 can be used to generate a native Python module. The easiest way to try this out for the -//! first time is to use [`maturin`]. `maturin` is a tool for building and publishing Rust-based -//! Python packages with minimal configuration. The following steps set up some files for an example -//! Python module, install `maturin`, and then show how to build and import the Python module. -//! -//! First, create a new folder (let's call it `string_sum`) containing the following two files: -//! -//! **`Cargo.toml`** -//! -//! ```toml -//! [lib] -//! name = "string_sum" -//! # "cdylib" is necessary to produce a shared library for Python to import from. -//! # -//! # Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able -//! # to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: -//! # crate-type = ["cdylib", "rlib"] -//! crate-type = ["cdylib"] -//! -//! [dependencies.pyo3-ffi] -#![doc = concat!("version = \"", env!("CARGO_PKG_VERSION"), "\"")] -//! features = ["extension-module"] -//! ``` -//! -//! **`src/lib.rs`** -//! ```rust -//! use std::os::raw::c_char; -//! use std::ptr; -//! -//! use pyo3_ffi::*; -//! -//! static mut MODULE_DEF: PyModuleDef = PyModuleDef { -//! m_base: PyModuleDef_HEAD_INIT, -//! m_name: c_str!("string_sum").as_ptr(), -//! m_doc: c_str!("A Python module written in Rust.").as_ptr(), -//! m_size: 0, -//! m_methods: unsafe { METHODS.as_mut_ptr().cast() }, -//! m_slots: std::ptr::null_mut(), -//! m_traverse: None, -//! m_clear: None, -//! m_free: None, -//! }; -//! -//! static mut METHODS: [PyMethodDef; 2] = [ -//! PyMethodDef { -//! ml_name: c_str!("sum_as_string").as_ptr(), -//! ml_meth: PyMethodDefPointer { -//! PyCFunctionFast: sum_as_string, -//! }, -//! ml_flags: METH_FASTCALL, -//! ml_doc: c_str!("returns the sum of two integers as a string").as_ptr(), -//! }, -//! // A zeroed PyMethodDef to mark the end of the array. -//! PyMethodDef::zeroed() -//! ]; -//! -//! // The module initialization function, which must be named `PyInit_`. -//! #[allow(non_snake_case)] -//! #[no_mangle] -//! pub unsafe extern "C" fn PyInit_string_sum() -> *mut PyObject { -//! PyModule_Create(ptr::addr_of_mut!(MODULE_DEF)) -//! } -//! -//! pub unsafe extern "C" fn sum_as_string( -//! _self: *mut PyObject, -//! args: *mut *mut PyObject, -//! nargs: Py_ssize_t, -//! ) -> *mut PyObject { -//! if nargs != 2 { -//! PyErr_SetString( -//! PyExc_TypeError, -//! c_str!("sum_as_string() expected 2 positional arguments").as_ptr(), -//! ); -//! return std::ptr::null_mut(); -//! } -//! -//! let arg1 = *args; -//! if PyLong_Check(arg1) == 0 { -//! PyErr_SetString( -//! PyExc_TypeError, -//! c_str!("sum_as_string() expected an int for positional argument 1").as_ptr(), -//! ); -//! return std::ptr::null_mut(); -//! } -//! -//! let arg1 = PyLong_AsLong(arg1); -//! if !PyErr_Occurred().is_null() { -//! return ptr::null_mut(); -//! } -//! -//! let arg2 = *args.add(1); -//! if PyLong_Check(arg2) == 0 { -//! PyErr_SetString( -//! PyExc_TypeError, -//! c_str!("sum_as_string() expected an int for positional argument 2").as_ptr(), -//! ); -//! return std::ptr::null_mut(); -//! } -//! -//! let arg2 = PyLong_AsLong(arg2); -//! if !PyErr_Occurred().is_null() { -//! return ptr::null_mut(); -//! } -//! -//! match arg1.checked_add(arg2) { -//! Some(sum) => { -//! let string = sum.to_string(); -//! PyUnicode_FromStringAndSize(string.as_ptr().cast::(), string.len() as isize) -//! } -//! None => { -//! PyErr_SetString( -//! PyExc_OverflowError, -//! c_str!("arguments too large to add").as_ptr(), -//! ); -//! std::ptr::null_mut() -//! } -//! } -//! } -//! ``` -//! -//! With those two files in place, now `maturin` needs to be installed. This can be done using -//! Python's package manager `pip`. First, load up a new Python `virtualenv`, and install `maturin` -//! into it: -//! ```bash -//! $ cd string_sum -//! $ python -m venv .env -//! $ source .env/bin/activate -//! $ pip install maturin -//! ``` -//! -//! Now build and execute the module: -//! ```bash -//! $ maturin develop -//! # lots of progress output as maturin runs the compilation... -//! $ python -//! >>> import string_sum -//! >>> string_sum.sum_as_string(5, 20) -//! '25' -//! ``` -//! -//! As well as with `maturin`, it is possible to build using [setuptools-rust] or -//! [manually][manual_builds]. Both offer more flexibility than `maturin` but require further -//! configuration. -//! -//! -//! # Using Python from Rust -//! -//! To embed Python into a Rust binary, you need to ensure that your Python installation contains a -//! shared library. The following steps demonstrate how to ensure this (for Ubuntu). -//! -//! To install the Python shared library on Ubuntu: -//! ```bash -//! sudo apt install python3-dev -//! ``` -//! -//! While most projects use the safe wrapper provided by pyo3, -//! you can take a look at the [`orjson`] library as an example on how to use `pyo3-ffi` directly. -//! For those well versed in C and Rust the [tutorials] from the CPython documentation -//! can be easily converted to rust as well. -//! -//! [tutorials]: https://docs.python.org/3/extending/ -//! [`orjson`]: https://github.com/ijl/orjson -//! [capi]: https://docs.python.org/3/c-api/index.html -//! [`maturin`]: https://github.com/PyO3/maturin "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" -//! [`pyo3-build-config`]: https://docs.rs/pyo3-build-config -//! [feature flags]: https://doc.rust-lang.org/cargo/reference/features.html "Features - The Cargo Book" -#![doc = concat!("[manual_builds]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/building-and-distribution.html#manual-builds \"Manual builds - Building and Distribution - PyO3 user guide\"")] -//! [setuptools-rust]: https://github.com/PyO3/setuptools-rust "Setuptools plugin for Rust extensions" -//! [PEP 384]: https://www.python.org/dev/peps/pep-0384 "PEP 384 -- Defining a Stable ABI" -#![doc = concat!("[Features chapter of the guide]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#features-reference \"Features Reference - PyO3 user guide\"")] -#![allow( - missing_docs, - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - clippy::upper_case_acronyms, - clippy::missing_safety_doc -)] -#![warn(elided_lifetimes_in_paths, unused_lifetimes)] - -// Until `extern type` is stabilized, use the recommended approach to -// model opaque types: -// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs -macro_rules! opaque_struct { - ($name:ident) => { - #[repr(C)] - pub struct $name([u8; 0]); - }; -} - -/// This is a helper macro to create a `&'static CStr`. -/// -/// It can be used on all Rust versions supported by PyO3, unlike c"" literals which -/// were stabilised in Rust 1.77. -/// -/// Due to the nature of PyO3 making heavy use of C FFI interop with Python, it is -/// common for PyO3 to use CStr. -/// -/// Examples: -/// -/// ```rust -/// use std::ffi::CStr; -/// -/// const HELLO: &CStr = pyo3_ffi::c_str!("hello"); -/// static WORLD: &CStr = pyo3_ffi::c_str!("world"); -/// ``` -#[macro_export] -macro_rules! c_str { - ($s:expr) => { - $crate::_cstr_from_utf8_with_nul_checked(concat!($s, "\0")) - }; -} - -/// Private helper for `c_str!` macro. -#[doc(hidden)] -pub const fn _cstr_from_utf8_with_nul_checked(s: &str) -> &CStr { - // TODO: Replace this implementation with `CStr::from_bytes_with_nul` when MSRV above 1.72. - let bytes = s.as_bytes(); - let len = bytes.len(); - assert!( - !bytes.is_empty() && bytes[bytes.len() - 1] == b'\0', - "string is not nul-terminated" - ); - let mut i = 0; - let non_null_len = len - 1; - while i < non_null_len { - assert!(bytes[i] != b'\0', "string contains null bytes"); - i += 1; - } - - unsafe { CStr::from_bytes_with_nul_unchecked(bytes) } -} - -use std::ffi::CStr; - -pub mod compat; -mod impl_; - -pub use self::abstract_::*; -pub use self::bltinmodule::*; -pub use self::boolobject::*; -pub use self::bytearrayobject::*; -pub use self::bytesobject::*; -pub use self::ceval::*; -#[cfg(Py_LIMITED_API)] -pub use self::code::*; -pub use self::codecs::*; -pub use self::compile::*; -pub use self::complexobject::*; -#[cfg(all(Py_3_8, not(Py_LIMITED_API)))] -pub use self::context::*; -#[cfg(not(Py_LIMITED_API))] -pub use self::datetime::*; -pub use self::descrobject::*; -pub use self::dictobject::*; -pub use self::enumobject::*; -pub use self::fileobject::*; -pub use self::fileutils::*; -pub use self::floatobject::*; -pub use self::import::*; -pub use self::intrcheck::*; -pub use self::iterobject::*; -pub use self::listobject::*; -pub use self::longobject::*; -#[cfg(not(Py_LIMITED_API))] -pub use self::marshal::*; -pub use self::memoryobject::*; -pub use self::methodobject::*; -pub use self::modsupport::*; -pub use self::moduleobject::*; -pub use self::object::*; -pub use self::objimpl::*; -pub use self::osmodule::*; -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -pub use self::pyarena::*; -#[cfg(Py_3_11)] -pub use self::pybuffer::*; -pub use self::pycapsule::*; -pub use self::pyerrors::*; -pub use self::pyframe::*; -pub use self::pyhash::*; -pub use self::pylifecycle::*; -pub use self::pymem::*; -pub use self::pyport::*; -pub use self::pystate::*; -pub use self::pystrtod::*; -pub use self::pythonrun::*; -pub use self::rangeobject::*; -pub use self::setobject::*; -pub use self::sliceobject::*; -pub use self::structseq::*; -pub use self::sysmodule::*; -pub use self::traceback::*; -pub use self::tupleobject::*; -pub use self::typeslots::*; -pub use self::unicodeobject::*; -pub use self::warnings::*; -pub use self::weakrefobject::*; - -mod abstract_; -// skipped asdl.h -// skipped ast.h -mod bltinmodule; -mod boolobject; -mod bytearrayobject; -mod bytesobject; -// skipped cellobject.h -mod ceval; -// skipped classobject.h -#[cfg(Py_LIMITED_API)] -mod code; -mod codecs; -mod compile; -mod complexobject; -#[cfg(all(Py_3_8, not(Py_LIMITED_API)))] -mod context; // It's actually 3.7.1, but no cfg for patches. -#[cfg(not(Py_LIMITED_API))] -pub(crate) mod datetime; -mod descrobject; -mod dictobject; -// skipped dynamic_annotations.h -mod enumobject; -// skipped errcode.h -// skipped exports.h -mod fileobject; -mod fileutils; -mod floatobject; -// skipped empty frameobject.h -// skipped genericaliasobject.h -mod import; -// skipped interpreteridobject.h -mod intrcheck; -mod iterobject; -mod listobject; -// skipped longintrepr.h -mod longobject; -#[cfg(not(Py_LIMITED_API))] -pub mod marshal; -mod memoryobject; -mod methodobject; -mod modsupport; -mod moduleobject; -// skipped namespaceobject.h -mod object; -mod objimpl; -// skipped odictobject.h -// skipped opcode.h -// skipped osdefs.h -mod osmodule; -// skipped parser_interface.h -// skipped patchlevel.h -// skipped picklebufobject.h -// skipped pyctype.h -// skipped py_curses.h -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -mod pyarena; -#[cfg(Py_3_11)] -mod pybuffer; -mod pycapsule; -// skipped pydtrace.h -mod pyerrors; -// skipped pyexpat.h -// skipped pyfpe.h -mod pyframe; -mod pyhash; -mod pylifecycle; -// skipped pymacconfig.h -// skipped pymacro.h -// skipped pymath.h -mod pymem; -mod pyport; -mod pystate; -// skipped pystats.h -mod pythonrun; -// skipped pystrhex.h -// skipped pystrcmp.h -mod pystrtod; -// skipped pythread.h -// skipped pytime.h -mod rangeobject; -mod setobject; -mod sliceobject; -mod structseq; -mod sysmodule; -mod traceback; -// skipped tracemalloc.h -mod tupleobject; -mod typeslots; -mod unicodeobject; -mod warnings; -mod weakrefobject; - -// Additional headers that are not exported by Python.h -#[deprecated(note = "Python 3.12")] -pub mod structmember; - -// "Limited API" definitions matching Python's `include/cpython` directory. -#[cfg(not(Py_LIMITED_API))] -mod cpython; - -#[cfg(not(Py_LIMITED_API))] -pub use self::cpython::*; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/listobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/listobject.rs deleted file mode 100644 index 9d8b7ed6a5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/listobject.rs +++ /dev/null @@ -1,72 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyList_Type")] - pub static mut PyList_Type: PyTypeObject; - pub static mut PyListIter_Type: PyTypeObject; - pub static mut PyListRevIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyList_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LIST_SUBCLASS) -} - -#[inline] -pub unsafe fn PyList_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyList_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyList_New")] - pub fn PyList_New(size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_Size")] - pub fn PyList_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyList_GetItem")] - pub fn PyList_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyList_GetItemRef")] - pub fn PyList_GetItemRef(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_SetItem")] - pub fn PyList_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Insert")] - pub fn PyList_Insert(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Append")] - pub fn PyList_Append(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_GetSlice")] - pub fn PyList_GetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_SetSlice")] - pub fn PyList_SetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - arg4: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Sort")] - pub fn PyList_Sort(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Reverse")] - pub fn PyList_Reverse(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_AsTuple")] - pub fn PyList_AsTuple(arg1: *mut PyObject) -> *mut PyObject; - - // CPython macros exported as functions on PyPy or GraalPy - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_ITEM")] - #[cfg_attr(GraalPy, link_name = "PyList_GetItem")] - pub fn PyList_GET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg(PyPy)] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_SIZE")] - pub fn PyList_GET_SIZE(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_SET_ITEM")] - #[cfg_attr(GraalPy, link_name = "_PyList_SET_ITEM")] - pub fn PyList_SET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/longobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/longobject.rs deleted file mode 100644 index 68b4ecba54..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/longobject.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use libc::size_t; -use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong, c_ulong, c_ulonglong, c_void}; -use std::ptr::addr_of_mut; - -opaque_struct!(PyLongObject); - -#[inline] -pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS) -} - -#[inline] -pub unsafe fn PyLong_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyLong_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyLong_FromLong")] - pub fn PyLong_FromLong(arg1: c_long) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromUnsignedLong")] - pub fn PyLong_FromUnsignedLong(arg1: c_ulong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromSize_t")] - pub fn PyLong_FromSize_t(arg1: size_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromSsize_t")] - pub fn PyLong_FromSsize_t(arg1: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromDouble")] - pub fn PyLong_FromDouble(arg1: c_double) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLong")] - pub fn PyLong_AsLong(arg1: *mut PyObject) -> c_long; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongAndOverflow")] - pub fn PyLong_AsLongAndOverflow(arg1: *mut PyObject, arg2: *mut c_int) -> c_long; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsSsize_t")] - pub fn PyLong_AsSsize_t(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsSize_t")] - pub fn PyLong_AsSize_t(arg1: *mut PyObject) -> size_t; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLong")] - pub fn PyLong_AsUnsignedLong(arg1: *mut PyObject) -> c_ulong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongMask")] - pub fn PyLong_AsUnsignedLongMask(arg1: *mut PyObject) -> c_ulong; - // skipped non-limited _PyLong_AsInt - pub fn PyLong_GetInfo() -> *mut PyObject; - // skipped PyLong_AS_LONG - - // skipped PyLong_FromPid - // skipped PyLong_AsPid - // skipped _Py_PARSE_INTPTR - // skipped _Py_PARSE_UINTPTR - - // skipped non-limited _PyLong_UnsignedShort_Converter - // skipped non-limited _PyLong_UnsignedInt_Converter - // skipped non-limited _PyLong_UnsignedLong_Converter - // skipped non-limited _PyLong_UnsignedLongLong_Converter - // skipped non-limited _PyLong_Size_t_Converter - - // skipped non-limited _PyLong_DigitValue - // skipped non-limited _PyLong_Frexp - - #[cfg_attr(PyPy, link_name = "PyPyLong_AsDouble")] - pub fn PyLong_AsDouble(arg1: *mut PyObject) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromVoidPtr")] - pub fn PyLong_FromVoidPtr(arg1: *mut c_void) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsVoidPtr")] - pub fn PyLong_AsVoidPtr(arg1: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromLongLong")] - pub fn PyLong_FromLongLong(arg1: c_longlong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromUnsignedLongLong")] - pub fn PyLong_FromUnsignedLongLong(arg1: c_ulonglong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongLong")] - pub fn PyLong_AsLongLong(arg1: *mut PyObject) -> c_longlong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongLong")] - pub fn PyLong_AsUnsignedLongLong(arg1: *mut PyObject) -> c_ulonglong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongLongMask")] - pub fn PyLong_AsUnsignedLongLongMask(arg1: *mut PyObject) -> c_ulonglong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongLongAndOverflow")] - pub fn PyLong_AsLongLongAndOverflow(arg1: *mut PyObject, arg2: *mut c_int) -> c_longlong; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromString")] - pub fn PyLong_FromString( - arg1: *const c_char, - arg2: *mut *mut c_char, - arg3: c_int, - ) -> *mut PyObject; -} - -#[cfg(not(Py_LIMITED_API))] -extern "C" { - #[cfg_attr(PyPy, link_name = "_PyPyLong_NumBits")] - pub fn _PyLong_NumBits(obj: *mut PyObject) -> size_t; -} - -// skipped non-limited _PyLong_Format -// skipped non-limited _PyLong_FormatWriter -// skipped non-limited _PyLong_FormatBytesWriter -// skipped non-limited _PyLong_FormatAdvancedWriter - -extern "C" { - pub fn PyOS_strtoul(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_ulong; - pub fn PyOS_strtol(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_long; -} - -// skipped non-limited _PyLong_Rshift -// skipped non-limited _PyLong_Lshift diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/marshal.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/marshal.rs deleted file mode 100644 index 562f6f9162..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/marshal.rs +++ /dev/null @@ -1,19 +0,0 @@ -use super::{PyObject, Py_ssize_t}; -use std::os::raw::{c_char, c_int}; - -// skipped Py_MARSHAL_VERSION -// skipped PyMarshal_WriteLongToFile -// skipped PyMarshal_WriteObjectToFile - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMarshal_WriteObjectToString")] - pub fn PyMarshal_WriteObjectToString(object: *mut PyObject, version: c_int) -> *mut PyObject; - - // skipped non-limited PyMarshal_ReadLongFromFile - // skipped non-limited PyMarshal_ReadShortFromFile - // skipped non-limited PyMarshal_ReadObjectFromFile - // skipped non-limited PyMarshal_ReadLastObjectFromFile - - #[cfg_attr(PyPy, link_name = "PyPyMarshal_ReadObjectFromString")] - pub fn PyMarshal_ReadObjectFromString(data: *const c_char, len: Py_ssize_t) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/memoryobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/memoryobject.rs deleted file mode 100644 index b7ef9e2ef1..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/memoryobject.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(Py_LIMITED_API))] - pub static mut _PyManagedBuffer_Type: PyTypeObject; - - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_Type")] - pub static mut PyMemoryView_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyMemoryView_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyMemoryView_Type)) as c_int -} - -// skipped non-limited PyMemoryView_GET_BUFFER -// skipped non-limited PyMemeryView_GET_BASE - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromObject")] - pub fn PyMemoryView_FromObject(base: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromMemory")] - pub fn PyMemoryView_FromMemory( - mem: *mut c_char, - size: Py_ssize_t, - flags: c_int, - ) -> *mut PyObject; - #[cfg(any(Py_3_11, not(Py_LIMITED_API)))] - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromBuffer")] - pub fn PyMemoryView_FromBuffer(view: *const crate::Py_buffer) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_GetContiguous")] - pub fn PyMemoryView_GetContiguous( - base: *mut PyObject, - buffertype: c_int, - order: c_char, - ) -> *mut PyObject; -} - -// skipped remainder of file with comment: -/* The structs are declared here so that macros can work, but they shouldn't -be considered public. Don't access their fields directly, use the macros -and functions instead! */ diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/methodobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/methodobject.rs deleted file mode 100644 index bd21440918..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/methodobject.rs +++ /dev/null @@ -1,282 +0,0 @@ -use crate::object::{PyObject, PyTypeObject, Py_TYPE}; -#[cfg(Py_3_9)] -use crate::PyObject_TypeCheck; -use std::os::raw::{c_char, c_int, c_void}; -use std::{mem, ptr}; - -#[cfg(all(Py_3_9, not(Py_LIMITED_API), not(GraalPy)))] -pub struct PyCFunctionObject { - pub ob_base: PyObject, - pub m_ml: *mut PyMethodDef, - pub m_self: *mut PyObject, - pub m_module: *mut PyObject, - pub m_weakreflist: *mut PyObject, - #[cfg(not(PyPy))] - pub vectorcall: Option, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_Type")] - pub static mut PyCFunction_Type: PyTypeObject; -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == ptr::addr_of_mut!(PyCFunction_Type)) as c_int -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, ptr::addr_of_mut!(PyCFunction_Type)) -} - -#[cfg(not(Py_3_9))] -#[inline] -pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == ptr::addr_of_mut!(PyCFunction_Type)) as c_int -} - -pub type PyCFunction = - unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub type PyCFunctionFast = unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut *mut PyObject, - nargs: crate::pyport::Py_ssize_t, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -#[deprecated(note = "renamed to `PyCFunctionFast`")] -pub type _PyCFunctionFast = PyCFunctionFast; - -pub type PyCFunctionWithKeywords = unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - kwds: *mut PyObject, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub type PyCFunctionFastWithKeywords = unsafe extern "C" fn( - slf: *mut PyObject, - args: *const *mut PyObject, - nargs: crate::pyport::Py_ssize_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub type _PyCFunctionFastWithKeywords = PyCFunctionFastWithKeywords; - -#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] -pub type PyCMethod = unsafe extern "C" fn( - slf: *mut PyObject, - defining_class: *mut PyTypeObject, - args: *const *mut PyObject, - nargs: crate::pyport::Py_ssize_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_GetFunction")] - pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option; - pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject; - pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int; - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyCFunction_Call( - f: *mut PyObject, - args: *mut PyObject, - kwds: *mut PyObject, - ) -> *mut PyObject; -} - -/// Represents the [PyMethodDef](https://docs.python.org/3/c-api/structures.html#c.PyMethodDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must ensure that -/// `ml_name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq)] -pub struct PyMethodDef { - pub ml_name: *const c_char, - pub ml_meth: PyMethodDefPointer, - pub ml_flags: c_int, - pub ml_doc: *const c_char, -} - -impl PyMethodDef { - pub const fn zeroed() -> PyMethodDef { - PyMethodDef { - ml_name: ptr::null(), - ml_meth: PyMethodDefPointer { - Void: ptr::null_mut(), - }, - ml_flags: 0, - ml_doc: ptr::null(), - } - } -} - -impl Default for PyMethodDef { - fn default() -> PyMethodDef { - PyMethodDef { - ml_name: ptr::null(), - ml_meth: PyMethodDefPointer { - Void: ptr::null_mut(), - }, - ml_flags: 0, - ml_doc: ptr::null(), - } - } -} - -/// Function types used to implement Python callables. -/// -/// This function pointer must be accompanied by the correct [ml_flags](PyMethodDef::ml_flags), -/// otherwise the behavior is undefined. -/// -/// See the [Python C API documentation][1] for more information. -/// -/// [1]: https://docs.python.org/3/c-api/structures.html#implementing-functions-and-methods -#[repr(C)] -#[derive(Copy, Clone, Eq)] -pub union PyMethodDefPointer { - /// This variant corresponds with [`METH_VARARGS`] *or* [`METH_NOARGS`] *or* [`METH_O`]. - pub PyCFunction: PyCFunction, - - /// This variant corresponds with [`METH_VARARGS`] | [`METH_KEYWORDS`]. - pub PyCFunctionWithKeywords: PyCFunctionWithKeywords, - - /// This variant corresponds with [`METH_FASTCALL`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[deprecated(note = "renamed to `PyCFunctionFast`")] - pub _PyCFunctionFast: PyCFunctionFast, - - /// This variant corresponds with [`METH_FASTCALL`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub PyCFunctionFast: PyCFunctionFast, - - /// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub _PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords, - - /// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords, - - /// This variant corresponds with [`METH_METHOD`] | [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(all(Py_3_9, not(Py_LIMITED_API)))] - pub PyCMethod: PyCMethod, - - Void: *mut c_void, -} - -impl PyMethodDefPointer { - pub fn as_ptr(&self) -> *mut c_void { - unsafe { self.Void } - } - - pub fn is_null(&self) -> bool { - self.as_ptr().is_null() - } - - pub const fn zeroed() -> PyMethodDefPointer { - PyMethodDefPointer { - Void: ptr::null_mut(), - } - } -} - -impl PartialEq for PyMethodDefPointer { - fn eq(&self, other: &Self) -> bool { - unsafe { self.Void == other.Void } - } -} - -impl std::fmt::Pointer for PyMethodDefPointer { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let ptr = unsafe { self.Void }; - std::fmt::Pointer::fmt(&ptr, f) - } -} - -const _: () = - assert!(mem::size_of::() == mem::size_of::>()); - -#[cfg(not(Py_3_9))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_New")] - pub fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyCFunction_NewEx")] - pub fn PyCFunction_NewEx( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject { - PyCFunction_NewEx(ml, slf, std::ptr::null_mut()) -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_NewEx( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, -) -> *mut PyObject { - PyCMethod_New(ml, slf, module, std::ptr::null_mut()) -} - -#[cfg(Py_3_9)] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCMethod_New")] - pub fn PyCMethod_New( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject; -} - -/* Flag passed to newmethodobject */ -pub const METH_VARARGS: c_int = 0x0001; -pub const METH_KEYWORDS: c_int = 0x0002; -/* METH_NOARGS and METH_O must not be combined with the flags above. */ -pub const METH_NOARGS: c_int = 0x0004; -pub const METH_O: c_int = 0x0008; - -/* METH_CLASS and METH_STATIC are a little different; these control -the construction of methods for a class. These cannot be used for -functions in modules. */ -pub const METH_CLASS: c_int = 0x0010; -pub const METH_STATIC: c_int = 0x0020; - -/* METH_COEXIST allows a method to be entered eventhough a slot has -already filled the entry. When defined, the flag allows a separate -method, "__contains__" for example, to coexist with a defined -slot like sq_contains. */ - -pub const METH_COEXIST: c_int = 0x0040; - -/* METH_FASTCALL indicates the PEP 590 Vectorcall calling format. It may -be specified alone or with METH_KEYWORDS. */ -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub const METH_FASTCALL: c_int = 0x0080; - -// skipped METH_STACKLESS - -#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] -pub const METH_METHOD: c_int = 0x0200; - -extern "C" { - #[cfg(not(Py_3_9))] - pub fn PyCFunction_ClearFreeList() -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/modsupport.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/modsupport.rs deleted file mode 100644 index 6da2795bbc..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/modsupport.rs +++ /dev/null @@ -1,151 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::moduleobject::PyModuleDef; -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_long}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyArg_Parse")] - pub fn PyArg_Parse(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTuple")] - pub fn PyArg_ParseTuple(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTupleAndKeywords")] - pub fn PyArg_ParseTupleAndKeywords( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *const c_char, - #[cfg(not(Py_3_13))] arg4: *mut *mut c_char, - #[cfg(Py_3_13)] arg4: *const *const c_char, - ... - ) -> c_int; - - // skipped PyArg_VaParse - // skipped PyArg_VaParseTupleAndKeywords - - pub fn PyArg_ValidateKeywordArguments(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_UnpackTuple")] - pub fn PyArg_UnpackTuple( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: Py_ssize_t, - arg4: Py_ssize_t, - ... - ) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPy_BuildValue")] - pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject; - // skipped Py_VaBuildValue - - #[cfg(Py_3_13)] - pub fn PyModule_Add( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> core::ffi::c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyModule_AddObjectRef")] - pub fn PyModule_AddObjectRef( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddObject")] - pub fn PyModule_AddObject( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddIntConstant")] - pub fn PyModule_AddIntConstant( - module: *mut PyObject, - name: *const c_char, - value: c_long, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddStringConstant")] - pub fn PyModule_AddStringConstant( - module: *mut PyObject, - name: *const c_char, - value: *const c_char, - ) -> c_int; - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyModule_AddType")] - pub fn PyModule_AddType( - module: *mut PyObject, - type_: *mut crate::object::PyTypeObject, - ) -> c_int; - // skipped PyModule_AddIntMacro - // skipped PyModule_AddStringMacro - pub fn PyModule_SetDocString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - pub fn PyModule_AddFunctions(arg1: *mut PyObject, arg2: *mut PyMethodDef) -> c_int; - pub fn PyModule_ExecDef(module: *mut PyObject, def: *mut PyModuleDef) -> c_int; -} - -pub const Py_CLEANUP_SUPPORTED: i32 = 0x2_0000; - -pub const PYTHON_API_VERSION: i32 = 1013; -pub const PYTHON_ABI_VERSION: i32 = 3; - -extern "C" { - #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] - #[cfg_attr(PyPy, link_name = "PyPyModule_Create2")] - pub fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject; - - #[cfg(py_sys_config = "Py_TRACE_REFS")] - fn PyModule_Create2TraceRefs(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject; - - #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] - pub fn PyModule_FromDefAndSpec2( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, - ) -> *mut PyObject; - - #[cfg(py_sys_config = "Py_TRACE_REFS")] - fn PyModule_FromDefAndSpec2TraceRefs( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, - ) -> *mut PyObject; -} - -#[cfg(py_sys_config = "Py_TRACE_REFS")] -#[inline] -pub unsafe fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject { - PyModule_Create2TraceRefs(module, apiver) -} - -#[cfg(py_sys_config = "Py_TRACE_REFS")] -#[inline] -pub unsafe fn PyModule_FromDefAndSpec2( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, -) -> *mut PyObject { - PyModule_FromDefAndSpec2TraceRefs(def, spec, module_api_version) -} - -#[inline] -pub unsafe fn PyModule_Create(module: *mut PyModuleDef) -> *mut PyObject { - PyModule_Create2( - module, - if cfg!(Py_LIMITED_API) { - PYTHON_ABI_VERSION - } else { - PYTHON_API_VERSION - }, - ) -} - -#[inline] -pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject) -> *mut PyObject { - PyModule_FromDefAndSpec2( - def, - spec, - if cfg!(Py_LIMITED_API) { - PYTHON_ABI_VERSION - } else { - PYTHON_API_VERSION - }, - ) -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/moduleobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/moduleobject.rs deleted file mode 100644 index ff6458f4b1..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/moduleobject.rs +++ /dev/null @@ -1,112 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyModule_Type")] - pub static mut PyModule_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyModule_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyModule_Type)) -} - -#[inline] -pub unsafe fn PyModule_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyModule_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyModule_NewObject")] - pub fn PyModule_NewObject(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_New")] - pub fn PyModule_New(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetDict")] - pub fn PyModule_GetDict(arg1: *mut PyObject) -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyModule_GetNameObject(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetName")] - pub fn PyModule_GetName(arg1: *mut PyObject) -> *const c_char; - #[cfg(not(all(windows, PyPy)))] - #[deprecated(note = "Python 3.2")] - pub fn PyModule_GetFilename(arg1: *mut PyObject) -> *const c_char; - #[cfg(not(PyPy))] - pub fn PyModule_GetFilenameObject(arg1: *mut PyObject) -> *mut PyObject; - // skipped non-limited _PyModule_Clear - // skipped non-limited _PyModule_ClearDict - // skipped non-limited _PyModuleSpec_IsInitializing - #[cfg_attr(PyPy, link_name = "PyPyModule_GetDef")] - pub fn PyModule_GetDef(arg1: *mut PyObject) -> *mut PyModuleDef; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetState")] - pub fn PyModule_GetState(arg1: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyModuleDef_Init")] - pub fn PyModuleDef_Init(arg1: *mut PyModuleDef) -> *mut PyObject; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyModuleDef_Type: PyTypeObject; -} - -#[repr(C)] -pub struct PyModuleDef_Base { - pub ob_base: PyObject, - pub m_init: Option *mut PyObject>, - pub m_index: Py_ssize_t, - pub m_copy: *mut PyObject, -} - -#[allow(clippy::declare_interior_mutable_const)] -pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base { - ob_base: PyObject_HEAD_INIT, - m_init: None, - m_index: 0, - m_copy: std::ptr::null_mut(), -}; - -#[repr(C)] -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct PyModuleDef_Slot { - pub slot: c_int, - pub value: *mut c_void, -} - -impl Default for PyModuleDef_Slot { - fn default() -> PyModuleDef_Slot { - PyModuleDef_Slot { - slot: 0, - value: std::ptr::null_mut(), - } - } -} - -pub const Py_mod_create: c_int = 1; -pub const Py_mod_exec: c_int = 2; -#[cfg(Py_3_12)] -pub const Py_mod_multiple_interpreters: c_int = 3; - -#[cfg(Py_3_12)] -pub const Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED: *mut c_void = 0 as *mut c_void; -#[cfg(Py_3_12)] -pub const Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED: *mut c_void = 1 as *mut c_void; -#[cfg(Py_3_12)] -pub const Py_MOD_PER_INTERPRETER_GIL_SUPPORTED: *mut c_void = 2 as *mut c_void; - -// skipped non-limited _Py_mod_LAST_SLOT - -#[repr(C)] -pub struct PyModuleDef { - pub m_base: PyModuleDef_Base, - pub m_name: *const c_char, - pub m_doc: *const c_char, - pub m_size: Py_ssize_t, - pub m_methods: *mut PyMethodDef, - pub m_slots: *mut PyModuleDef_Slot, - pub m_traverse: Option, - pub m_clear: Option, - pub m_free: Option, -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/object.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/object.rs deleted file mode 100644 index d2fa19303c..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/object.rs +++ /dev/null @@ -1,942 +0,0 @@ -use crate::pyport::{Py_hash_t, Py_ssize_t}; -#[cfg(Py_GIL_DISABLED)] -use crate::PyMutex; -#[cfg(Py_GIL_DISABLED)] -use std::marker::PhantomPinned; -use std::mem; -use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void}; -use std::ptr; -#[cfg(Py_GIL_DISABLED)] -use std::sync::atomic::{AtomicIsize, AtomicU32, AtomicU8, Ordering::Relaxed}; - -#[cfg(Py_LIMITED_API)] -opaque_struct!(PyTypeObject); - -#[cfg(not(Py_LIMITED_API))] -pub use crate::cpython::object::PyTypeObject; - -#[cfg(Py_3_12)] -const _Py_IMMORTAL_REFCNT: Py_ssize_t = { - if cfg!(target_pointer_width = "64") { - c_uint::MAX as Py_ssize_t - } else { - // for 32-bit systems, use the lower 30 bits (see comment in CPython's object.h) - (c_uint::MAX >> 2) as Py_ssize_t - } -}; - -#[cfg(Py_GIL_DISABLED)] -const _Py_IMMORTAL_REFCNT_LOCAL: u32 = u32::MAX; - -#[allow(clippy::declare_interior_mutable_const)] -pub const PyObject_HEAD_INIT: PyObject = PyObject { - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_next: std::ptr::null_mut(), - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_prev: std::ptr::null_mut(), - #[cfg(Py_GIL_DISABLED)] - ob_tid: 0, - #[cfg(Py_GIL_DISABLED)] - _padding: 0, - #[cfg(Py_GIL_DISABLED)] - ob_mutex: PyMutex { - _bits: AtomicU8::new(0), - _pin: PhantomPinned, - }, - #[cfg(Py_GIL_DISABLED)] - ob_gc_bits: 0, - #[cfg(Py_GIL_DISABLED)] - ob_ref_local: AtomicU32::new(_Py_IMMORTAL_REFCNT_LOCAL), - #[cfg(Py_GIL_DISABLED)] - ob_ref_shared: AtomicIsize::new(0), - #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] - ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 }, - #[cfg(not(Py_3_12))] - ob_refcnt: 1, - #[cfg(PyPy)] - ob_pypy_link: 0, - ob_type: std::ptr::null_mut(), -}; - -// skipped PyObject_VAR_HEAD -// skipped Py_INVALID_SIZE - -// skipped private _Py_UNOWNED_TID - -#[cfg(Py_GIL_DISABLED)] -const _Py_REF_SHARED_SHIFT: isize = 2; -// skipped private _Py_REF_SHARED_FLAG_MASK - -// skipped private _Py_REF_SHARED_INIT -// skipped private _Py_REF_MAYBE_WEAKREF -// skipped private _Py_REF_QUEUED -// skipped private _Py_REF_MERGED - -// skipped private _Py_REF_SHARED - -#[repr(C)] -#[derive(Copy, Clone)] -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -/// This union is anonymous in CPython, so the name was given by PyO3 because -/// Rust unions need a name. -pub union PyObjectObRefcnt { - pub ob_refcnt: Py_ssize_t, - #[cfg(target_pointer_width = "64")] - pub ob_refcnt_split: [crate::PY_UINT32_T; 2], -} - -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -impl std::fmt::Debug for PyObjectObRefcnt { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", unsafe { self.ob_refcnt }) - } -} - -#[cfg(all(not(Py_3_12), not(Py_GIL_DISABLED)))] -pub type PyObjectObRefcnt = Py_ssize_t; - -#[repr(C)] -#[derive(Debug)] -pub struct PyObject { - #[cfg(py_sys_config = "Py_TRACE_REFS")] - pub _ob_next: *mut PyObject, - #[cfg(py_sys_config = "Py_TRACE_REFS")] - pub _ob_prev: *mut PyObject, - #[cfg(Py_GIL_DISABLED)] - pub ob_tid: libc::uintptr_t, - #[cfg(Py_GIL_DISABLED)] - pub _padding: u16, - #[cfg(Py_GIL_DISABLED)] - pub ob_mutex: PyMutex, // per-object lock - #[cfg(Py_GIL_DISABLED)] - pub ob_gc_bits: u8, // gc-related state - #[cfg(Py_GIL_DISABLED)] - pub ob_ref_local: AtomicU32, // local reference count - #[cfg(Py_GIL_DISABLED)] - pub ob_ref_shared: AtomicIsize, // shared reference count - #[cfg(not(Py_GIL_DISABLED))] - pub ob_refcnt: PyObjectObRefcnt, - #[cfg(PyPy)] - pub ob_pypy_link: Py_ssize_t, - pub ob_type: *mut PyTypeObject, -} - -// skipped private _PyObject_CAST - -#[repr(C)] -#[derive(Debug)] -pub struct PyVarObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub ob_size: Py_ssize_t, -} - -// skipped private _PyVarObject_CAST - -#[inline] -#[cfg(not(all(PyPy, Py_3_10)))] -#[cfg_attr(docsrs, doc(cfg(all())))] -pub unsafe fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int { - (x == y).into() -} - -#[cfg(all(PyPy, Py_3_10))] -#[cfg_attr(docsrs, doc(cfg(all())))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPy_Is")] - pub fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int; -} - -// skipped private _Py_GetThreadLocal_Addr - -// skipped private _Py_ThreadId - -// skipped private _Py_IsOwnedByCurrentThread - -#[inline] -pub unsafe fn Py_REFCNT(ob: *mut PyObject) -> Py_ssize_t { - #[cfg(Py_GIL_DISABLED)] - { - let local = (*ob).ob_ref_local.load(Relaxed); - if local == _Py_IMMORTAL_REFCNT_LOCAL { - return _Py_IMMORTAL_REFCNT; - } - let shared = (*ob).ob_ref_shared.load(Relaxed); - local as Py_ssize_t + Py_ssize_t::from(shared >> _Py_REF_SHARED_SHIFT) - } - - #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] - { - (*ob).ob_refcnt.ob_refcnt - } - - #[cfg(all(not(Py_GIL_DISABLED), not(Py_3_12), not(GraalPy)))] - { - (*ob).ob_refcnt - } - - #[cfg(all(not(Py_GIL_DISABLED), not(Py_3_12), GraalPy))] - { - _Py_REFCNT(ob) - } -} - -#[inline] -pub unsafe fn Py_TYPE(ob: *mut PyObject) -> *mut PyTypeObject { - #[cfg(not(GraalPy))] - return (*ob).ob_type; - #[cfg(GraalPy)] - return _Py_TYPE(ob); -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyLong_Type")] - pub static mut PyLong_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyBool_Type")] - pub static mut PyBool_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn Py_SIZE(ob: *mut PyObject) -> Py_ssize_t { - #[cfg(not(GraalPy))] - { - debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyLong_Type)); - debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyBool_Type)); - (*ob.cast::()).ob_size - } - #[cfg(GraalPy)] - _Py_SIZE(ob) -} - -#[inline(always)] -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -pub unsafe fn _Py_IsImmortal(op: *mut PyObject) -> c_int { - #[cfg(target_pointer_width = "64")] - { - (((*op).ob_refcnt.ob_refcnt as crate::PY_INT32_T) < 0) as c_int - } - - #[cfg(target_pointer_width = "32")] - { - ((*op).ob_refcnt.ob_refcnt == _Py_IMMORTAL_REFCNT) as c_int - } -} - -#[inline] -pub unsafe fn Py_IS_TYPE(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int { - (Py_TYPE(ob) == tp) as c_int -} - -// skipped _Py_SetRefCnt - -// skipped Py_SET_REFCNT - -// skipped Py_SET_TYPE - -// skipped Py_SET_SIZE - -pub type unaryfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type binaryfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject; -pub type ternaryfunc = - unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type inquiry = unsafe extern "C" fn(*mut PyObject) -> c_int; -pub type lenfunc = unsafe extern "C" fn(*mut PyObject) -> Py_ssize_t; -pub type ssizeargfunc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t) -> *mut PyObject; -pub type ssizessizeargfunc = - unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t) -> *mut PyObject; -pub type ssizeobjargproc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t, *mut PyObject) -> c_int; -pub type ssizessizeobjargproc = - unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t, arg4: *mut PyObject) -> c_int; -pub type objobjargproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; - -pub type objobjproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> c_int; -pub type visitproc = unsafe extern "C" fn(object: *mut PyObject, arg: *mut c_void) -> c_int; -pub type traverseproc = - unsafe extern "C" fn(slf: *mut PyObject, visit: visitproc, arg: *mut c_void) -> c_int; - -pub type freefunc = unsafe extern "C" fn(*mut c_void); -pub type destructor = unsafe extern "C" fn(*mut PyObject); -pub type getattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char) -> *mut PyObject; -pub type getattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject; -pub type setattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char, *mut PyObject) -> c_int; -pub type setattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type reprfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type hashfunc = unsafe extern "C" fn(*mut PyObject) -> Py_hash_t; -pub type richcmpfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, c_int) -> *mut PyObject; -pub type getiterfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type iternextfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type descrgetfunc = - unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type descrsetfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type initproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type newfunc = - unsafe extern "C" fn(*mut PyTypeObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type allocfunc = unsafe extern "C" fn(*mut PyTypeObject, Py_ssize_t) -> *mut PyObject; - -#[cfg(Py_3_8)] -pub type vectorcallfunc = unsafe extern "C" fn( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: libc::size_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyType_Slot { - pub slot: c_int, - pub pfunc: *mut c_void, -} - -impl Default for PyType_Slot { - fn default() -> PyType_Slot { - unsafe { mem::zeroed() } - } -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyType_Spec { - pub name: *const c_char, - pub basicsize: c_int, - pub itemsize: c_int, - pub flags: c_uint, - pub slots: *mut PyType_Slot, -} - -impl Default for PyType_Spec { - fn default() -> PyType_Spec { - unsafe { mem::zeroed() } - } -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyType_FromSpec")] - pub fn PyType_FromSpec(arg1: *mut PyType_Spec) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyType_FromSpecWithBases")] - pub fn PyType_FromSpecWithBases(arg1: *mut PyType_Spec, arg2: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyType_GetSlot")] - pub fn PyType_GetSlot(arg1: *mut PyTypeObject, arg2: c_int) -> *mut c_void; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_FromModuleAndSpec")] - pub fn PyType_FromModuleAndSpec( - module: *mut PyObject, - spec: *mut PyType_Spec, - bases: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModule")] - pub fn PyType_GetModule(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleState")] - pub fn PyType_GetModuleState(arg1: *mut PyTypeObject) -> *mut c_void; - - #[cfg(Py_3_11)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetName")] - pub fn PyType_GetName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_11)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetQualName")] - pub fn PyType_GetQualName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetFullyQualifiedName")] - pub fn PyType_GetFullyQualifiedName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleName")] - pub fn PyType_GetModuleName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyType_FromMetaclass")] - pub fn PyType_FromMetaclass( - metaclass: *mut PyTypeObject, - module: *mut PyObject, - spec: *mut PyType_Spec, - bases: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeData")] - pub fn PyObject_GetTypeData(obj: *mut PyObject, cls: *mut PyTypeObject) -> *mut c_void; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeDataSize")] - pub fn PyObject_GetTypeDataSize(cls: *mut PyTypeObject) -> Py_ssize_t; - - #[cfg_attr(PyPy, link_name = "PyPyType_IsSubtype")] - pub fn PyType_IsSubtype(a: *mut PyTypeObject, b: *mut PyTypeObject) -> c_int; -} - -#[inline] -pub unsafe fn PyObject_TypeCheck(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int { - (Py_IS_TYPE(ob, tp) != 0 || PyType_IsSubtype(Py_TYPE(ob), tp) != 0) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - /// built-in 'type' - #[cfg_attr(PyPy, link_name = "PyPyType_Type")] - pub static mut PyType_Type: PyTypeObject; - /// built-in 'object' - #[cfg_attr(PyPy, link_name = "PyPyBaseObject_Type")] - pub static mut PyBaseObject_Type: PyTypeObject; - /// built-in 'super' - pub static mut PySuper_Type: PyTypeObject; -} - -extern "C" { - pub fn PyType_GetFlags(arg1: *mut PyTypeObject) -> c_ulong; - - #[cfg_attr(PyPy, link_name = "PyPyType_Ready")] - pub fn PyType_Ready(t: *mut PyTypeObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyType_GenericAlloc")] - pub fn PyType_GenericAlloc(t: *mut PyTypeObject, nitems: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyType_GenericNew")] - pub fn PyType_GenericNew( - t: *mut PyTypeObject, - args: *mut PyObject, - kwds: *mut PyObject, - ) -> *mut PyObject; - pub fn PyType_ClearCache() -> c_uint; - #[cfg_attr(PyPy, link_name = "PyPyType_Modified")] - pub fn PyType_Modified(t: *mut PyTypeObject); - - #[cfg_attr(PyPy, link_name = "PyPyObject_Repr")] - pub fn PyObject_Repr(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Str")] - pub fn PyObject_Str(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_ASCII")] - pub fn PyObject_ASCII(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Bytes")] - pub fn PyObject_Bytes(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompare")] - pub fn PyObject_RichCompare( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompareBool")] - pub fn PyObject_RichCompareBool(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int) - -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttrString")] - pub fn PyObject_GetAttrString(arg1: *mut PyObject, arg2: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttrString")] - pub fn PyObject_SetAttrString( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: *mut PyObject, - ) -> c_int; - #[cfg(any(Py_3_13, PyPy))] // CPython defined in 3.12 as an inline function in abstract.h - #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttrString")] - pub fn PyObject_DelAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrString")] - pub fn PyObject_HasAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttr")] - pub fn PyObject_GetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttr")] - pub fn PyObject_GetOptionalAttr( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut *mut PyObject, - ) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttrString")] - pub fn PyObject_GetOptionalAttrString( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: *mut *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttr")] - pub fn PyObject_SetAttr(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) - -> c_int; - #[cfg(any(Py_3_13, PyPy))] // CPython defined in 3.12 as an inline function in abstract.h - #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttr")] - pub fn PyObject_DelAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttr")] - pub fn PyObject_HasAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrWithError")] - pub fn PyObject_HasAttrWithError(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrStringWithError")] - pub fn PyObject_HasAttrStringWithError(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_SelfIter")] - pub fn PyObject_SelfIter(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetAttr")] - pub fn PyObject_GenericGetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetAttr")] - pub fn PyObject_GenericSetAttr( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> c_int; - #[cfg(not(all(Py_LIMITED_API, not(Py_3_10))))] - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetDict")] - pub fn PyObject_GenericGetDict(arg1: *mut PyObject, arg2: *mut c_void) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetDict")] - pub fn PyObject_GenericSetDict( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut c_void, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_Hash")] - pub fn PyObject_Hash(arg1: *mut PyObject) -> Py_hash_t; - #[cfg_attr(PyPy, link_name = "PyPyObject_HashNotImplemented")] - pub fn PyObject_HashNotImplemented(arg1: *mut PyObject) -> Py_hash_t; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsTrue")] - pub fn PyObject_IsTrue(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_Not")] - pub fn PyObject_Not(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCallable_Check")] - pub fn PyCallable_Check(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_ClearWeakRefs")] - pub fn PyObject_ClearWeakRefs(arg1: *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPyObject_Dir")] - pub fn PyObject_Dir(arg1: *mut PyObject) -> *mut PyObject; - pub fn Py_ReprEnter(arg1: *mut PyObject) -> c_int; - pub fn Py_ReprLeave(arg1: *mut PyObject); -} - -// Flag bits for printing: -pub const Py_PRINT_RAW: c_int = 1; // No string quotes etc. - -// skipped because is a private API -// const _Py_TPFLAGS_STATIC_BUILTIN: c_ulong = 1 << 1; - -#[cfg(all(Py_3_12, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MANAGED_WEAKREF: c_ulong = 1 << 3; - -#[cfg(all(Py_3_11, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MANAGED_DICT: c_ulong = 1 << 4; - -#[cfg(all(Py_3_10, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_SEQUENCE: c_ulong = 1 << 5; - -#[cfg(all(Py_3_10, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MAPPING: c_ulong = 1 << 6; - -#[cfg(Py_3_10)] -pub const Py_TPFLAGS_DISALLOW_INSTANTIATION: c_ulong = 1 << 7; - -#[cfg(Py_3_10)] -pub const Py_TPFLAGS_IMMUTABLETYPE: c_ulong = 1 << 8; - -/// Set if the type object is dynamically allocated -pub const Py_TPFLAGS_HEAPTYPE: c_ulong = 1 << 9; - -/// Set if the type allows subclassing -pub const Py_TPFLAGS_BASETYPE: c_ulong = 1 << 10; - -/// Set if the type implements the vectorcall protocol (PEP 590) -#[cfg(any(Py_3_12, all(Py_3_8, not(Py_LIMITED_API))))] -pub const Py_TPFLAGS_HAVE_VECTORCALL: c_ulong = 1 << 11; -// skipped backwards-compatibility alias _Py_TPFLAGS_HAVE_VECTORCALL - -/// Set if the type is 'ready' -- fully initialized -pub const Py_TPFLAGS_READY: c_ulong = 1 << 12; - -/// Set while the type is being 'readied', to prevent recursive ready calls -pub const Py_TPFLAGS_READYING: c_ulong = 1 << 13; - -/// Objects support garbage collection (see objimp.h) -pub const Py_TPFLAGS_HAVE_GC: c_ulong = 1 << 14; - -const Py_TPFLAGS_HAVE_STACKLESS_EXTENSION: c_ulong = 0; - -#[cfg(Py_3_8)] -pub const Py_TPFLAGS_METHOD_DESCRIPTOR: c_ulong = 1 << 17; - -pub const Py_TPFLAGS_VALID_VERSION_TAG: c_ulong = 1 << 19; - -/* Type is abstract and cannot be instantiated */ -pub const Py_TPFLAGS_IS_ABSTRACT: c_ulong = 1 << 20; - -// skipped non-limited / 3.10 Py_TPFLAGS_HAVE_AM_SEND -#[cfg(Py_3_12)] -pub const Py_TPFLAGS_ITEMS_AT_END: c_ulong = 1 << 23; - -/* These flags are used to determine if a type is a subclass. */ -pub const Py_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; -pub const Py_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25; -pub const Py_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; -pub const Py_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27; -pub const Py_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; -pub const Py_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29; -pub const Py_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30; -pub const Py_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; - -pub const Py_TPFLAGS_DEFAULT: c_ulong = if cfg!(Py_3_10) { - Py_TPFLAGS_HAVE_STACKLESS_EXTENSION -} else { - Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | Py_TPFLAGS_HAVE_VERSION_TAG -}; - -pub const Py_TPFLAGS_HAVE_FINALIZE: c_ulong = 1; -pub const Py_TPFLAGS_HAVE_VERSION_TAG: c_ulong = 1 << 18; - -extern "C" { - #[cfg(all(py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_NegativeRefcount(filename: *const c_char, lineno: c_int, op: *mut PyObject); - #[cfg(all(Py_3_12, py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_INCREF_IncRefTotal(); - #[cfg(all(Py_3_12, py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_DECREF_DecRefTotal(); - - #[cfg_attr(PyPy, link_name = "_PyPy_Dealloc")] - fn _Py_Dealloc(arg1: *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPy_IncRef")] - #[cfg_attr(GraalPy, link_name = "_Py_IncRef")] - pub fn Py_IncRef(o: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPy_DecRef")] - #[cfg_attr(GraalPy, link_name = "_Py_DecRef")] - pub fn Py_DecRef(o: *mut PyObject); - - #[cfg(all(Py_3_10, not(PyPy)))] - fn _Py_IncRef(o: *mut PyObject); - #[cfg(all(Py_3_10, not(PyPy)))] - fn _Py_DecRef(o: *mut PyObject); - - #[cfg(GraalPy)] - fn _Py_REFCNT(arg1: *const PyObject) -> Py_ssize_t; - - #[cfg(GraalPy)] - fn _Py_TYPE(arg1: *const PyObject) -> *mut PyTypeObject; - - #[cfg(GraalPy)] - fn _Py_SIZE(arg1: *const PyObject) -> Py_ssize_t; -} - -#[inline(always)] -pub unsafe fn Py_INCREF(op: *mut PyObject) { - // On limited API, the free-threaded build, or with refcount debugging, let the interpreter do refcounting - // TODO: reimplement the logic in the header in the free-threaded build, for a little bit of performance. - #[cfg(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - py_sys_config = "Py_REF_DEBUG", - GraalPy - ))] - { - // _Py_IncRef was added to the ABI in 3.10; skips null checks - #[cfg(all(Py_3_10, not(PyPy)))] - { - _Py_IncRef(op); - } - - #[cfg(any(not(Py_3_10), PyPy))] - { - Py_IncRef(op); - } - } - - // version-specific builds are allowed to directly manipulate the reference count - #[cfg(not(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - py_sys_config = "Py_REF_DEBUG", - GraalPy - )))] - { - #[cfg(all(Py_3_12, target_pointer_width = "64"))] - { - let cur_refcnt = (*op).ob_refcnt.ob_refcnt_split[crate::PY_BIG_ENDIAN]; - let new_refcnt = cur_refcnt.wrapping_add(1); - if new_refcnt == 0 { - return; - } - (*op).ob_refcnt.ob_refcnt_split[crate::PY_BIG_ENDIAN] = new_refcnt; - } - - #[cfg(all(Py_3_12, target_pointer_width = "32"))] - { - if _Py_IsImmortal(op) != 0 { - return; - } - (*op).ob_refcnt.ob_refcnt += 1 - } - - #[cfg(not(Py_3_12))] - { - (*op).ob_refcnt += 1 - } - - // Skipped _Py_INCREF_STAT_INC - if anyone wants this, please file an issue - // or submit a PR supporting Py_STATS build option and pystats.h - } -} - -#[inline(always)] -#[cfg_attr( - all(py_sys_config = "Py_REF_DEBUG", Py_3_12, not(Py_LIMITED_API)), - track_caller -)] -pub unsafe fn Py_DECREF(op: *mut PyObject) { - // On limited API, the free-threaded build, or with refcount debugging, let the interpreter do refcounting - // On 3.12+ we implement refcount debugging to get better assertion locations on negative refcounts - // TODO: reimplement the logic in the header in the free-threaded build, for a little bit of performance. - #[cfg(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - all(py_sys_config = "Py_REF_DEBUG", not(Py_3_12)), - GraalPy - ))] - { - // _Py_DecRef was added to the ABI in 3.10; skips null checks - #[cfg(all(Py_3_10, not(PyPy)))] - { - _Py_DecRef(op); - } - - #[cfg(any(not(Py_3_10), PyPy))] - { - Py_DecRef(op); - } - } - - #[cfg(not(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - all(py_sys_config = "Py_REF_DEBUG", not(Py_3_12)), - GraalPy - )))] - { - #[cfg(Py_3_12)] - if _Py_IsImmortal(op) != 0 { - return; - } - - // Skipped _Py_DECREF_STAT_INC - if anyone needs this, please file an issue - // or submit a PR supporting Py_STATS build option and pystats.h - - #[cfg(py_sys_config = "Py_REF_DEBUG")] - _Py_DECREF_DecRefTotal(); - - #[cfg(Py_3_12)] - { - (*op).ob_refcnt.ob_refcnt -= 1; - - #[cfg(py_sys_config = "Py_REF_DEBUG")] - if (*op).ob_refcnt.ob_refcnt < 0 { - let location = std::panic::Location::caller(); - let filename = std::ffi::CString::new(location.file()).unwrap(); - _Py_NegativeRefcount(filename.as_ptr(), location.line() as i32, op); - } - - if (*op).ob_refcnt.ob_refcnt == 0 { - _Py_Dealloc(op); - } - } - - #[cfg(not(Py_3_12))] - { - (*op).ob_refcnt -= 1; - - if (*op).ob_refcnt == 0 { - _Py_Dealloc(op); - } - } - } -} - -#[inline] -pub unsafe fn Py_CLEAR(op: *mut *mut PyObject) { - let tmp = *op; - if !tmp.is_null() { - *op = ptr::null_mut(); - Py_DECREF(tmp); - } -} - -#[inline] -pub unsafe fn Py_XINCREF(op: *mut PyObject) { - if !op.is_null() { - Py_INCREF(op) - } -} - -#[inline] -pub unsafe fn Py_XDECREF(op: *mut PyObject) { - if !op.is_null() { - Py_DECREF(op) - } -} - -extern "C" { - #[cfg(all(Py_3_10, Py_LIMITED_API, not(PyPy)))] - #[cfg_attr(docsrs, doc(cfg(Py_3_10)))] - pub fn Py_NewRef(obj: *mut PyObject) -> *mut PyObject; - #[cfg(all(Py_3_10, Py_LIMITED_API, not(PyPy)))] - #[cfg_attr(docsrs, doc(cfg(Py_3_10)))] - pub fn Py_XNewRef(obj: *mut PyObject) -> *mut PyObject; -} - -// macro _Py_NewRef not public; reimplemented directly inside Py_NewRef here -// macro _Py_XNewRef not public; reimplemented directly inside Py_XNewRef here - -#[cfg(all(Py_3_10, any(not(Py_LIMITED_API), PyPy)))] -#[cfg_attr(docsrs, doc(cfg(Py_3_10)))] -#[inline] -pub unsafe fn Py_NewRef(obj: *mut PyObject) -> *mut PyObject { - Py_INCREF(obj); - obj -} - -#[cfg(all(Py_3_10, any(not(Py_LIMITED_API), PyPy)))] -#[cfg_attr(docsrs, doc(cfg(Py_3_10)))] -#[inline] -pub unsafe fn Py_XNewRef(obj: *mut PyObject) -> *mut PyObject { - Py_XINCREF(obj); - obj -} - -#[cfg(Py_3_13)] -pub const Py_CONSTANT_NONE: c_uint = 0; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_FALSE: c_uint = 1; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_TRUE: c_uint = 2; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ELLIPSIS: c_uint = 3; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_NOT_IMPLEMENTED: c_uint = 4; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ZERO: c_uint = 5; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ONE: c_uint = 6; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_STR: c_uint = 7; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_BYTES: c_uint = 8; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_TUPLE: c_uint = 9; - -extern "C" { - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPy_GetConstant")] - pub fn Py_GetConstant(constant_id: c_uint) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPy_GetConstantBorrowed")] - pub fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "_PyPy_NoneStruct")] - static mut _Py_NoneStruct: PyObject; - - #[cfg(GraalPy)] - static mut _Py_NoneStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_None() -> *mut PyObject { - #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))] - return Py_GetConstantBorrowed(Py_CONSTANT_NONE); - - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - return ptr::addr_of_mut!(_Py_NoneStruct); - - #[cfg(GraalPy)] - return _Py_NoneStructReference; -} - -#[inline] -pub unsafe fn Py_IsNone(x: *mut PyObject) -> c_int { - Py_Is(x, Py_None()) -} - -// skipped Py_RETURN_NONE - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "_PyPy_NotImplementedStruct")] - static mut _Py_NotImplementedStruct: PyObject; - - #[cfg(GraalPy)] - static mut _Py_NotImplementedStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_NotImplemented() -> *mut PyObject { - #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))] - return Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED); - - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - return ptr::addr_of_mut!(_Py_NotImplementedStruct); - - #[cfg(GraalPy)] - return _Py_NotImplementedStructReference; -} - -// skipped Py_RETURN_NOTIMPLEMENTED - -/* Rich comparison opcodes */ -pub const Py_LT: c_int = 0; -pub const Py_LE: c_int = 1; -pub const Py_EQ: c_int = 2; -pub const Py_NE: c_int = 3; -pub const Py_GT: c_int = 4; -pub const Py_GE: c_int = 5; - -#[cfg(Py_3_10)] -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum PySendResult { - PYGEN_RETURN = 0, - PYGEN_ERROR = -1, - PYGEN_NEXT = 1, -} - -// skipped Py_RETURN_RICHCOMPARE - -#[inline] -pub unsafe fn PyType_HasFeature(ty: *mut PyTypeObject, feature: c_ulong) -> c_int { - #[cfg(Py_LIMITED_API)] - let flags = PyType_GetFlags(ty); - - #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] - let flags = (*ty).tp_flags.load(std::sync::atomic::Ordering::Relaxed); - - #[cfg(all(not(Py_LIMITED_API), not(Py_GIL_DISABLED)))] - let flags = (*ty).tp_flags; - - ((flags & feature) != 0) as c_int -} - -#[inline] -pub unsafe fn PyType_FastSubclass(t: *mut PyTypeObject, f: c_ulong) -> c_int { - PyType_HasFeature(t, f) -} - -#[inline] -pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS) -} - -// skipped _PyType_CAST - -#[inline] -pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int { - Py_IS_TYPE(op, ptr::addr_of_mut!(PyType_Type)) -} - -extern "C" { - #[cfg(any(Py_3_13, all(Py_3_11, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleByDef")] - pub fn PyType_GetModuleByDef( - arg1: *mut crate::PyTypeObject, - arg2: *mut crate::PyModuleDef, - ) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/objimpl.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/objimpl.rs deleted file mode 100644 index 76835a6d15..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/objimpl.rs +++ /dev/null @@ -1,94 +0,0 @@ -use libc::size_t; -use std::os::raw::{c_int, c_void}; - -use crate::object::*; -use crate::pyport::Py_ssize_t; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_Malloc")] - pub fn PyObject_Malloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Calloc")] - pub fn PyObject_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Realloc")] - pub fn PyObject_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Free")] - pub fn PyObject_Free(ptr: *mut c_void); - - // skipped PyObject_MALLOC - // skipped PyObject_REALLOC - // skipped PyObject_FREE - // skipped PyObject_Del - // skipped PyObject_DEL - - #[cfg_attr(PyPy, link_name = "PyPyObject_Init")] - pub fn PyObject_Init(arg1: *mut PyObject, arg2: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_InitVar")] - pub fn PyObject_InitVar( - arg1: *mut PyVarObject, - arg2: *mut PyTypeObject, - arg3: Py_ssize_t, - ) -> *mut PyVarObject; - - // skipped PyObject_INIT - // skipped PyObject_INIT_VAR - - #[cfg_attr(PyPy, link_name = "_PyPyObject_New")] - pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyObject_NewVar")] - pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; - - // skipped PyObject_New - // skipped PyObject_NEW - // skipped PyObject_NewVar - // skipped PyObject_NEW_VAR - - pub fn PyGC_Collect() -> Py_ssize_t; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_Enable")] - pub fn PyGC_Enable() -> c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_Disable")] - pub fn PyGC_Disable() -> c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_IsEnabled")] - pub fn PyGC_IsEnabled() -> c_int; - - // skipped PyUnstable_GC_VisitObjects -} - -#[inline] -pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int { - PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC) -} - -extern "C" { - pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject; - - // skipped PyObject_GC_Resize - - #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_New")] - pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_NewVar")] - pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; - #[cfg(not(PyPy))] - pub fn PyObject_GC_Track(arg1: *mut c_void); - #[cfg(not(PyPy))] - pub fn PyObject_GC_UnTrack(arg1: *mut c_void); - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_Del")] - pub fn PyObject_GC_Del(arg1: *mut c_void); - - // skipped PyObject_GC_New - // skipped PyObject_GC_NewVar - - #[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsTracked")] - pub fn PyObject_GC_IsTracked(arg1: *mut PyObject) -> c_int; - #[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsFinalized")] - pub fn PyObject_GC_IsFinalized(arg1: *mut PyObject) -> c_int; -} - -// skipped Py_VISIT diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/osmodule.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/osmodule.rs deleted file mode 100644 index e6aefc362d..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/osmodule.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::object::PyObject; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_FSPath")] - pub fn PyOS_FSPath(path: *mut PyObject) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyarena.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pyarena.rs deleted file mode 100644 index 87d5f28a7a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyarena.rs +++ /dev/null @@ -1 +0,0 @@ -opaque_struct!(PyArena); diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pybuffer.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pybuffer.rs deleted file mode 100644 index 50bf4e6109..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pybuffer.rs +++ /dev/null @@ -1,134 +0,0 @@ -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct Py_buffer { - pub buf: *mut c_void, - /// Owned reference - pub obj: *mut crate::PyObject, - pub len: Py_ssize_t, - pub itemsize: Py_ssize_t, - pub readonly: c_int, - pub ndim: c_int, - pub format: *mut c_char, - pub shape: *mut Py_ssize_t, - pub strides: *mut Py_ssize_t, - pub suboffsets: *mut Py_ssize_t, - pub internal: *mut c_void, - #[cfg(PyPy)] - pub flags: c_int, - #[cfg(PyPy)] - pub _strides: [Py_ssize_t; PyBUF_MAX_NDIM], - #[cfg(PyPy)] - pub _shape: [Py_ssize_t; PyBUF_MAX_NDIM], -} - -impl Py_buffer { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - Py_buffer { - buf: ptr::null_mut(), - obj: ptr::null_mut(), - len: 0, - itemsize: 0, - readonly: 0, - ndim: 0, - format: ptr::null_mut(), - shape: ptr::null_mut(), - strides: ptr::null_mut(), - suboffsets: ptr::null_mut(), - internal: ptr::null_mut(), - #[cfg(PyPy)] - flags: 0, - #[cfg(PyPy)] - _strides: [0; PyBUF_MAX_NDIM], - #[cfg(PyPy)] - _shape: [0; PyBUF_MAX_NDIM], - } - } -} - -pub type getbufferproc = unsafe extern "C" fn(*mut PyObject, *mut crate::Py_buffer, c_int) -> c_int; -pub type releasebufferproc = unsafe extern "C" fn(*mut PyObject, *mut crate::Py_buffer); - -/* Return 1 if the getbuffer function is available, otherwise return 0. */ -extern "C" { - #[cfg(not(PyPy))] - pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")] - pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")] - pub fn PyBuffer_GetPointer(view: *const Py_buffer, indices: *const Py_ssize_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")] - pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")] - pub fn PyBuffer_ToContiguous( - buf: *mut c_void, - view: *const Py_buffer, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")] - pub fn PyBuffer_FromContiguous( - view: *const Py_buffer, - buf: *const c_void, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")] - pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int; - pub fn PyBuffer_FillContiguousStrides( - ndims: c_int, - shape: *mut Py_ssize_t, - strides: *mut Py_ssize_t, - itemsize: c_int, - fort: c_char, - ); - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")] - pub fn PyBuffer_FillInfo( - view: *mut Py_buffer, - o: *mut PyObject, - buf: *mut c_void, - len: Py_ssize_t, - readonly: c_int, - flags: c_int, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")] - pub fn PyBuffer_Release(view: *mut Py_buffer); -} - -/// Maximum number of dimensions -pub const PyBUF_MAX_NDIM: c_int = if cfg!(PyPy) { 36 } else { 64 }; - -/* Flags for getting buffers */ -pub const PyBUF_SIMPLE: c_int = 0; -pub const PyBUF_WRITABLE: c_int = 0x0001; -/* we used to include an E, backwards compatible alias */ -pub const PyBUF_WRITEABLE: c_int = PyBUF_WRITABLE; -pub const PyBUF_FORMAT: c_int = 0x0004; -pub const PyBUF_ND: c_int = 0x0008; -pub const PyBUF_STRIDES: c_int = 0x0010 | PyBUF_ND; -pub const PyBUF_C_CONTIGUOUS: c_int = 0x0020 | PyBUF_STRIDES; -pub const PyBUF_F_CONTIGUOUS: c_int = 0x0040 | PyBUF_STRIDES; -pub const PyBUF_ANY_CONTIGUOUS: c_int = 0x0080 | PyBUF_STRIDES; -pub const PyBUF_INDIRECT: c_int = 0x0100 | PyBUF_STRIDES; - -pub const PyBUF_CONTIG: c_int = PyBUF_ND | PyBUF_WRITABLE; -pub const PyBUF_CONTIG_RO: c_int = PyBUF_ND; - -pub const PyBUF_STRIDED: c_int = PyBUF_STRIDES | PyBUF_WRITABLE; -pub const PyBUF_STRIDED_RO: c_int = PyBUF_STRIDES; - -pub const PyBUF_RECORDS: c_int = PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT; -pub const PyBUF_RECORDS_RO: c_int = PyBUF_STRIDES | PyBUF_FORMAT; - -pub const PyBUF_FULL: c_int = PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT; -pub const PyBUF_FULL_RO: c_int = PyBUF_INDIRECT | PyBUF_FORMAT; - -pub const PyBUF_READ: c_int = 0x100; -pub const PyBUF_WRITE: c_int = 0x200; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pycapsule.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pycapsule.rs deleted file mode 100644 index 5b77841c23..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pycapsule.rs +++ /dev/null @@ -1,48 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCapsule_Type")] - pub static mut PyCapsule_Type: PyTypeObject; -} - -pub type PyCapsule_Destructor = unsafe extern "C" fn(o: *mut PyObject); - -#[inline] -pub unsafe fn PyCapsule_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyCapsule_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCapsule_New")] - pub fn PyCapsule_New( - pointer: *mut c_void, - name: *const c_char, - destructor: Option, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetPointer")] - pub fn PyCapsule_GetPointer(capsule: *mut PyObject, name: *const c_char) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetDestructor")] - pub fn PyCapsule_GetDestructor(capsule: *mut PyObject) -> Option; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetName")] - pub fn PyCapsule_GetName(capsule: *mut PyObject) -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetContext")] - pub fn PyCapsule_GetContext(capsule: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_IsValid")] - pub fn PyCapsule_IsValid(capsule: *mut PyObject, name: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetPointer")] - pub fn PyCapsule_SetPointer(capsule: *mut PyObject, pointer: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetDestructor")] - pub fn PyCapsule_SetDestructor( - capsule: *mut PyObject, - destructor: Option, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetName")] - pub fn PyCapsule_SetName(capsule: *mut PyObject, name: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetContext")] - pub fn PyCapsule_SetContext(capsule: *mut PyObject, context: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_Import")] - pub fn PyCapsule_Import(name: *const c_char, no_block: c_int) -> *mut c_void; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyerrors.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pyerrors.rs deleted file mode 100644 index 6c9313c4ab..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyerrors.rs +++ /dev/null @@ -1,363 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_SetNone")] - pub fn PyErr_SetNone(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetObject")] - pub fn PyErr_SetObject(arg1: *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetString")] - pub fn PyErr_SetString(exception: *mut PyObject, string: *const c_char); - #[cfg_attr(PyPy, link_name = "PyPyErr_Occurred")] - pub fn PyErr_Occurred() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_Clear")] - pub fn PyErr_Clear(); - #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_GetRaisedException() instead."))] - #[cfg_attr(PyPy, link_name = "PyPyErr_Fetch")] - pub fn PyErr_Fetch( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_SetRaisedException() instead."))] - #[cfg_attr(PyPy, link_name = "PyPyErr_Restore")] - pub fn PyErr_Restore(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_GetExcInfo")] - pub fn PyErr_GetExcInfo( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetExcInfo")] - pub fn PyErr_SetExcInfo(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPy_FatalError")] - pub fn Py_FatalError(message: *const c_char) -> !; - #[cfg_attr(PyPy, link_name = "PyPyErr_GivenExceptionMatches")] - pub fn PyErr_GivenExceptionMatches(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_ExceptionMatches")] - pub fn PyErr_ExceptionMatches(arg1: *mut PyObject) -> c_int; - #[cfg_attr( - Py_3_12, - deprecated( - note = "Use PyErr_GetRaisedException() instead, to avoid any possible de-normalization." - ) - )] - #[cfg_attr(PyPy, link_name = "PyPyErr_NormalizeException")] - pub fn PyErr_NormalizeException( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg(Py_3_12)] - pub fn PyErr_GetRaisedException() -> *mut PyObject; - #[cfg(Py_3_12)] - pub fn PyErr_SetRaisedException(exc: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyException_SetTraceback")] - pub fn PyException_SetTraceback(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyException_GetTraceback")] - pub fn PyException_GetTraceback(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_GetCause")] - pub fn PyException_GetCause(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_SetCause")] - pub fn PyException_SetCause(arg1: *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyException_GetContext")] - pub fn PyException_GetContext(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_SetContext")] - pub fn PyException_SetContext(arg1: *mut PyObject, arg2: *mut PyObject); - - #[cfg(PyPy)] - #[link_name = "PyPyExceptionInstance_Class"] - pub fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyExceptionClass_Check(x: *mut PyObject) -> c_int { - (PyType_Check(x) != 0 - && PyType_FastSubclass(x as *mut PyTypeObject, Py_TPFLAGS_BASE_EXC_SUBCLASS) != 0) - as c_int -} - -#[inline] -pub unsafe fn PyExceptionInstance_Check(x: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject { - Py_TYPE(x) as *mut PyObject -} - -// ported from cpython exception.c (line 2096) -#[cfg(PyPy)] -pub unsafe fn PyUnicodeDecodeError_Create( - encoding: *const c_char, - object: *const c_char, - length: Py_ssize_t, - start: Py_ssize_t, - end: Py_ssize_t, - reason: *const c_char, -) -> *mut PyObject { - crate::_PyObject_CallFunction_SizeT( - PyExc_UnicodeDecodeError, - c_str!("sy#nns").as_ptr(), - encoding, - object, - length, - start, - end, - reason, - ) -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyExc_BaseException")] - pub static mut PyExc_BaseException: *mut PyObject; - #[cfg(Py_3_11)] - pub static mut PyExc_BaseExceptionGroup: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_Exception")] - pub static mut PyExc_Exception: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_StopAsyncIteration")] - pub static mut PyExc_StopAsyncIteration: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_StopIteration")] - pub static mut PyExc_StopIteration: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_GeneratorExit")] - pub static mut PyExc_GeneratorExit: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ArithmeticError")] - pub static mut PyExc_ArithmeticError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_LookupError")] - pub static mut PyExc_LookupError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_AssertionError")] - pub static mut PyExc_AssertionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_AttributeError")] - pub static mut PyExc_AttributeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BufferError")] - pub static mut PyExc_BufferError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_EOFError")] - pub static mut PyExc_EOFError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FloatingPointError")] - pub static mut PyExc_FloatingPointError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_OSError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ImportError")] - pub static mut PyExc_ImportError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ModuleNotFoundError")] - pub static mut PyExc_ModuleNotFoundError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IndexError")] - pub static mut PyExc_IndexError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_KeyError")] - pub static mut PyExc_KeyError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_KeyboardInterrupt")] - pub static mut PyExc_KeyboardInterrupt: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_MemoryError")] - pub static mut PyExc_MemoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NameError")] - pub static mut PyExc_NameError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OverflowError")] - pub static mut PyExc_OverflowError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeError")] - pub static mut PyExc_RuntimeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RecursionError")] - pub static mut PyExc_RecursionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NotImplementedError")] - pub static mut PyExc_NotImplementedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxError")] - pub static mut PyExc_SyntaxError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IndentationError")] - pub static mut PyExc_IndentationError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TabError")] - pub static mut PyExc_TabError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ReferenceError")] - pub static mut PyExc_ReferenceError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SystemError")] - pub static mut PyExc_SystemError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SystemExit")] - pub static mut PyExc_SystemExit: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TypeError")] - pub static mut PyExc_TypeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnboundLocalError")] - pub static mut PyExc_UnboundLocalError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeError")] - pub static mut PyExc_UnicodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeEncodeError")] - pub static mut PyExc_UnicodeEncodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeDecodeError")] - pub static mut PyExc_UnicodeDecodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeTranslateError")] - pub static mut PyExc_UnicodeTranslateError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ValueError")] - pub static mut PyExc_ValueError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ZeroDivisionError")] - pub static mut PyExc_ZeroDivisionError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_BlockingIOError")] - pub static mut PyExc_BlockingIOError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BrokenPipeError")] - pub static mut PyExc_BrokenPipeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ChildProcessError")] - pub static mut PyExc_ChildProcessError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionError")] - pub static mut PyExc_ConnectionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionAbortedError")] - pub static mut PyExc_ConnectionAbortedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionRefusedError")] - pub static mut PyExc_ConnectionRefusedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionResetError")] - pub static mut PyExc_ConnectionResetError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FileExistsError")] - pub static mut PyExc_FileExistsError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FileNotFoundError")] - pub static mut PyExc_FileNotFoundError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_InterruptedError")] - pub static mut PyExc_InterruptedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IsADirectoryError")] - pub static mut PyExc_IsADirectoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NotADirectoryError")] - pub static mut PyExc_NotADirectoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_PermissionError")] - pub static mut PyExc_PermissionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ProcessLookupError")] - pub static mut PyExc_ProcessLookupError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TimeoutError")] - pub static mut PyExc_TimeoutError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_EnvironmentError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_IOError: *mut PyObject; - #[cfg(windows)] - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_WindowsError: *mut PyObject; - - pub static mut PyExc_RecursionErrorInst: *mut PyObject; - - /* Predefined warning categories */ - #[cfg_attr(PyPy, link_name = "PyPyExc_Warning")] - pub static mut PyExc_Warning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UserWarning")] - pub static mut PyExc_UserWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_DeprecationWarning")] - pub static mut PyExc_DeprecationWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_PendingDeprecationWarning")] - pub static mut PyExc_PendingDeprecationWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxWarning")] - pub static mut PyExc_SyntaxWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeWarning")] - pub static mut PyExc_RuntimeWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FutureWarning")] - pub static mut PyExc_FutureWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ImportWarning")] - pub static mut PyExc_ImportWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeWarning")] - pub static mut PyExc_UnicodeWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BytesWarning")] - pub static mut PyExc_BytesWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ResourceWarning")] - pub static mut PyExc_ResourceWarning: *mut PyObject; - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyExc_EncodingWarning")] - pub static mut PyExc_EncodingWarning: *mut PyObject; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_BadArgument")] - pub fn PyErr_BadArgument() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_NoMemory")] - pub fn PyErr_NoMemory() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrno")] - pub fn PyErr_SetFromErrno(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrnoWithFilenameObject")] - pub fn PyErr_SetFromErrnoWithFilenameObject( - arg1: *mut PyObject, - arg2: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetFromErrnoWithFilenameObjects( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetFromErrnoWithFilename( - exc: *mut PyObject, - filename: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_Format")] - pub fn PyErr_Format(exception: *mut PyObject, format: *const c_char, ...) -> *mut PyObject; - pub fn PyErr_SetImportErrorSubclass( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - arg4: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetImportError( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_BadInternalCall")] - pub fn PyErr_BadInternalCall(); - pub fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_NewException")] - pub fn PyErr_NewException( - name: *const c_char, - base: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_NewExceptionWithDoc")] - pub fn PyErr_NewExceptionWithDoc( - name: *const c_char, - doc: *const c_char, - base: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_WriteUnraisable")] - pub fn PyErr_WriteUnraisable(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_CheckSignals")] - pub fn PyErr_CheckSignals() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterrupt")] - pub fn PyErr_SetInterrupt(); - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterruptEx")] - pub fn PyErr_SetInterruptEx(signum: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocation")] - pub fn PyErr_SyntaxLocation(filename: *const c_char, lineno: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocationEx")] - pub fn PyErr_SyntaxLocationEx(filename: *const c_char, lineno: c_int, col_offset: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_ProgramText")] - pub fn PyErr_ProgramText(filename: *const c_char, lineno: c_int) -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyUnicodeDecodeError_Create( - encoding: *const c_char, - object: *const c_char, - length: Py_ssize_t, - start: Py_ssize_t, - end: Py_ssize_t, - reason: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeTranslateError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeTranslateError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; - pub fn PyUnicodeDecodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; - pub fn PyUnicodeTranslateError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyframe.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pyframe.rs deleted file mode 100644 index 4dd3d2b31a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyframe.rs +++ /dev/null @@ -1,16 +0,0 @@ -#[cfg(not(GraalPy))] -#[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] -use crate::PyCodeObject; -#[cfg(not(Py_LIMITED_API))] -use crate::PyFrameObject; -use std::os::raw::c_int; - -#[cfg(Py_LIMITED_API)] -opaque_struct!(PyFrameObject); - -extern "C" { - pub fn PyFrame_GetLineNumber(f: *mut PyFrameObject) -> c_int; - #[cfg(not(GraalPy))] - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - pub fn PyFrame_GetCode(f: *mut PyFrameObject) -> *mut PyCodeObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyhash.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pyhash.rs deleted file mode 100644 index 19459903c1..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyhash.rs +++ /dev/null @@ -1,55 +0,0 @@ -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use crate::pyport::{Py_hash_t, Py_ssize_t}; -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use std::os::raw::{c_char, c_void}; - -use std::os::raw::{c_int, c_ulong}; - -extern "C" { - // skipped non-limited _Py_HashDouble - // skipped non-limited _Py_HashPointer - // skipped non-limited _Py_HashPointerRaw - - #[cfg(not(all(Py_3_14, any(Py_LIMITED_API, PyPy, GraalPy))))] - pub fn _Py_HashBytes(src: *const c_void, len: Py_ssize_t) -> Py_hash_t; - - #[cfg(Py_3_14)] - pub fn Py_HashBuffer(ptr: *const c_void, len: Py_ssize_t) -> Py_hash_t; -} - -pub const _PyHASH_MULTIPLIER: c_ulong = 1000003; - -// skipped _PyHASH_BITS - -// skipped non-limited _Py_HashSecret_t - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyHash_FuncDef { - pub hash: Option Py_hash_t>, - pub name: *const c_char, - pub hash_bits: c_int, - pub seed_bits: c_int, -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -impl Default for PyHash_FuncDef { - #[inline] - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} - -extern "C" { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - pub fn PyHash_GetFuncDef() -> *mut PyHash_FuncDef; -} - -// skipped Py_HASH_CUTOFF - -pub const Py_HASH_EXTERNAL: c_int = 0; -pub const Py_HASH_SIPHASH24: c_int = 1; -pub const Py_HASH_FNV: c_int = 2; - -// skipped Py_HASH_ALGORITHM diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pylifecycle.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pylifecycle.rs deleted file mode 100644 index 3f051c54f7..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pylifecycle.rs +++ /dev/null @@ -1,92 +0,0 @@ -use crate::pystate::PyThreadState; - -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn Py_Initialize(); - pub fn Py_InitializeEx(arg1: c_int); - pub fn Py_Finalize(); - pub fn Py_FinalizeEx() -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPy_IsInitialized")] - pub fn Py_IsInitialized() -> c_int; - - pub fn Py_NewInterpreter() -> *mut PyThreadState; - pub fn Py_EndInterpreter(arg1: *mut PyThreadState); - - #[cfg_attr(PyPy, link_name = "PyPy_AtExit")] - pub fn Py_AtExit(func: Option) -> c_int; - - pub fn Py_Exit(arg1: c_int); - - pub fn Py_Main(argc: c_int, argv: *mut *mut wchar_t) -> c_int; - pub fn Py_BytesMain(argc: c_int, argv: *mut *mut c_char) -> c_int; - - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `PyConfig.program_name` instead.") - )] - pub fn Py_SetProgramName(arg1: *const wchar_t); - #[cfg_attr(PyPy, link_name = "PyPy_GetProgramName")] - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.executable` instead.") - )] - pub fn Py_GetProgramName() -> *mut wchar_t; - - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `PyConfig.home` instead.") - )] - pub fn Py_SetPythonHome(arg1: *const wchar_t); - #[cfg_attr( - Py_3_13, - deprecated( - note = "Deprecated since Python 3.13. Use `PyConfig.home` or the value of the `PYTHONHOME` environment variable instead." - ) - )] - pub fn Py_GetPythonHome() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.executable` instead.") - )] - pub fn Py_GetProgramFullPath() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.prefix` instead.") - )] - pub fn Py_GetPrefix() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.exec_prefix` instead.") - )] - pub fn Py_GetExecPrefix() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.path` instead.") - )] - pub fn Py_GetPath() -> *mut wchar_t; - #[cfg(not(Py_3_13))] - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `sys.path` instead.") - )] - pub fn Py_SetPath(arg1: *const wchar_t); - - // skipped _Py_CheckPython3 - - #[cfg_attr(PyPy, link_name = "PyPy_GetVersion")] - pub fn Py_GetVersion() -> *const c_char; - pub fn Py_GetPlatform() -> *const c_char; - pub fn Py_GetCopyright() -> *const c_char; - pub fn Py_GetCompiler() -> *const c_char; - pub fn Py_GetBuildInfo() -> *const c_char; -} - -type PyOS_sighandler_t = unsafe extern "C" fn(arg1: c_int); - -extern "C" { - pub fn PyOS_getsig(arg1: c_int) -> PyOS_sighandler_t; - pub fn PyOS_setsig(arg1: c_int, arg2: PyOS_sighandler_t) -> PyOS_sighandler_t; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pymem.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pymem.rs deleted file mode 100644 index ab0c3d74af..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pymem.rs +++ /dev/null @@ -1,13 +0,0 @@ -use libc::size_t; -use std::os::raw::c_void; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMem_Malloc")] - pub fn PyMem_Malloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Calloc")] - pub fn PyMem_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Realloc")] - pub fn PyMem_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Free")] - pub fn PyMem_Free(ptr: *mut c_void); -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyport.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pyport.rs deleted file mode 100644 index a144c67fb1..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pyport.rs +++ /dev/null @@ -1,25 +0,0 @@ -pub type PY_UINT32_T = u32; -pub type PY_UINT64_T = u64; - -pub type PY_INT32_T = i32; -pub type PY_INT64_T = i64; - -pub type Py_uintptr_t = ::libc::uintptr_t; -pub type Py_intptr_t = ::libc::intptr_t; -pub type Py_ssize_t = ::libc::ssize_t; - -pub type Py_hash_t = Py_ssize_t; -pub type Py_uhash_t = ::libc::size_t; - -pub const PY_SSIZE_T_MIN: Py_ssize_t = isize::MIN as Py_ssize_t; -pub const PY_SSIZE_T_MAX: Py_ssize_t = isize::MAX as Py_ssize_t; - -#[cfg(target_endian = "big")] -pub const PY_BIG_ENDIAN: usize = 1; -#[cfg(target_endian = "big")] -pub const PY_LITTLE_ENDIAN: usize = 0; - -#[cfg(target_endian = "little")] -pub const PY_BIG_ENDIAN: usize = 0; -#[cfg(target_endian = "little")] -pub const PY_LITTLE_ENDIAN: usize = 1; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pystate.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pystate.rs deleted file mode 100644 index 23aeea3a1d..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pystate.rs +++ /dev/null @@ -1,83 +0,0 @@ -use crate::moduleobject::PyModuleDef; -use crate::object::PyObject; -use std::os::raw::c_int; - -#[cfg(not(PyPy))] -use std::os::raw::c_long; - -pub const MAX_CO_EXTRA_USERS: c_int = 255; - -opaque_struct!(PyThreadState); -opaque_struct!(PyInterpreterState); - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyInterpreterState_New() -> *mut PyInterpreterState; - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState); - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState); - - #[cfg(all(Py_3_9, not(PyPy)))] - pub fn PyInterpreterState_Get() -> *mut PyInterpreterState; - - #[cfg(all(Py_3_8, not(PyPy)))] - pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject; - - #[cfg(not(PyPy))] - pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64; - - #[cfg_attr(PyPy, link_name = "PyPyState_AddModule")] - pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyState_RemoveModule")] - pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int; - - // only has PyPy prefix since 3.10 - #[cfg_attr(all(PyPy, Py_3_10), link_name = "PyPyState_FindModule")] - pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_New")] - pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")] - pub fn PyThreadState_Clear(arg1: *mut PyThreadState); - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")] - pub fn PyThreadState_Delete(arg1: *mut PyThreadState); - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")] - pub fn PyThreadState_Get() -> *mut PyThreadState; -} - -#[inline] -pub unsafe fn PyThreadState_GET() -> *mut PyThreadState { - PyThreadState_Get() -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")] - pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")] - pub fn PyThreadState_GetDict() -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int; -} - -// skipped non-limited / 3.9 PyThreadState_GetInterpreter -// skipped non-limited / 3.9 PyThreadState_GetFrame -// skipped non-limited / 3.9 PyThreadState_GetID - -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum PyGILState_STATE { - PyGILState_LOCKED, - PyGILState_UNLOCKED, -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")] - pub fn PyGILState_Ensure() -> PyGILState_STATE; - #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")] - pub fn PyGILState_Release(arg1: PyGILState_STATE); - #[cfg(not(PyPy))] - pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pystrtod.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pystrtod.rs deleted file mode 100644 index 1f027686bd..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pystrtod.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_double, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_string_to_double")] - pub fn PyOS_string_to_double( - str: *const c_char, - endptr: *mut *mut c_char, - overflow_exception: *mut PyObject, - ) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyOS_double_to_string")] - pub fn PyOS_double_to_string( - val: c_double, - format_code: c_char, - precision: c_int, - flags: c_int, - _type: *mut c_int, - ) -> *mut c_char; -} - -// skipped non-limited _Py_string_to_number_with_underscores -// skipped non-limited _Py_parse_inf_or_nan - -/* PyOS_double_to_string's "flags" parameter can be set to 0 or more of: */ -pub const Py_DTSF_SIGN: c_int = 0x01; /* always add the sign */ -pub const Py_DTSF_ADD_DOT_0: c_int = 0x02; /* if the result is an integer add ".0" */ -pub const Py_DTSF_ALT: c_int = 0x04; /* "alternate" formatting. it's format_code specific */ - -/* PyOS_double_to_string's "type", if non-NULL, will be set to one of: */ -pub const Py_DTST_FINITE: c_int = 0; -pub const Py_DTST_INFINITE: c_int = 1; -pub const Py_DTST_NAN: c_int = 2; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/pythonrun.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/pythonrun.rs deleted file mode 100644 index e7ea2d2efd..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/pythonrun.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::object::*; -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -use libc::FILE; -#[cfg(any(Py_LIMITED_API, not(Py_3_10), PyPy, GraalPy))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -extern "C" { - #[cfg(any(all(Py_LIMITED_API, not(PyPy)), GraalPy))] - pub fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyErr_Print")] - pub fn PyErr_Print(); - #[cfg_attr(PyPy, link_name = "PyPyErr_PrintEx")] - pub fn PyErr_PrintEx(arg1: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_Display")] - pub fn PyErr_Display(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - - #[cfg(Py_3_12)] - pub fn PyErr_DisplayException(exc: *mut PyObject); -} - -#[inline] -#[cfg(PyPy)] -pub unsafe fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject { - // PyPy's implementation of Py_CompileString always forwards to Py_CompileStringFlags; this - // is only available in the non-limited API and has a real definition for all versions in - // the cpython/ subdirectory. - #[cfg(Py_LIMITED_API)] - extern "C" { - #[link_name = "PyPy_CompileStringFlags"] - pub fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut std::os::raw::c_void, // Actually *mut Py_CompilerFlags in the real definition - ) -> *mut PyObject; - } - #[cfg(not(Py_LIMITED_API))] - use crate::Py_CompileStringFlags; - - Py_CompileStringFlags(string, p, s, std::ptr::null_mut()) -} - -// skipped PyOS_InputHook - -pub const PYOS_STACK_MARGIN: c_int = 2048; - -// skipped PyOS_CheckStack under Microsoft C - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -opaque_struct!(_mod); - -#[cfg(not(any(PyPy, Py_3_10)))] -opaque_struct!(symtable); -#[cfg(not(any(PyPy, Py_3_10)))] -opaque_struct!(_node); - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyParser_SimpleParseString(s: *const c_char, b: c_int) -> *mut _node { - #[allow(deprecated)] - crate::PyParser_SimpleParseStringFlags(s, b, 0) -} - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyParser_SimpleParseFile(fp: *mut FILE, s: *const c_char, b: c_int) -> *mut _node { - #[allow(deprecated)] - crate::PyParser_SimpleParseFileFlags(fp, s, b, 0) -} - -extern "C" { - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn Py_SymtableString( - str: *const c_char, - filename: *const c_char, - start: c_int, - ) -> *mut symtable; - #[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] - pub fn Py_SymtableStringObject( - str: *const c_char, - filename: *mut PyObject, - start: c_int, - ) -> *mut symtable; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/rangeobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/rangeobject.rs deleted file mode 100644 index 408b5cdc5a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/rangeobject.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRange_Type")] - pub static mut PyRange_Type: PyTypeObject; - pub static mut PyRangeIter_Type: PyTypeObject; - pub static mut PyLongRangeIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyRange_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyRange_Type)) as c_int -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/setobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/setobject.rs deleted file mode 100644 index 9d5351fc79..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/setobject.rs +++ /dev/null @@ -1,150 +0,0 @@ -use crate::object::*; -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use crate::pyport::Py_hash_t; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -pub const PySet_MINSIZE: usize = 8; - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct setentry { - pub key: *mut PyObject, - pub hash: Py_hash_t, -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySetObject { - pub ob_base: PyObject, - pub fill: Py_ssize_t, - pub used: Py_ssize_t, - pub mask: Py_ssize_t, - pub table: *mut setentry, - pub hash: Py_hash_t, - pub finger: Py_ssize_t, - pub smalltable: [setentry; PySet_MINSIZE], - pub weakreflist: *mut PyObject, -} - -// skipped -#[inline] -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_LIMITED_API)))] -pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t { - debug_assert_eq!(PyAnySet_Check(so), 1); - let so = so.cast::(); - (*so).used -} - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PySet_Dummy: *mut PyObject; -} - -extern "C" { - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "_PyPySet_NextEntry")] - pub fn _PySet_NextEntry( - set: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - hash: *mut super::Py_hash_t, - ) -> c_int; - - // skipped non-limited _PySet_Update -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySet_Type")] - pub static mut PySet_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_Type")] - pub static mut PyFrozenSet_Type: PyTypeObject; - pub static mut PySetIter_Type: PyTypeObject; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySet_New")] - pub fn PySet_New(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_New")] - pub fn PyFrozenSet_New(arg1: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPySet_Add")] - pub fn PySet_Add(set: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Clear")] - pub fn PySet_Clear(set: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Contains")] - pub fn PySet_Contains(anyset: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Discard")] - pub fn PySet_Discard(set: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Pop")] - pub fn PySet_Pop(set: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySet_Size")] - pub fn PySet_Size(anyset: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPyFrozenSet_CheckExact"] - pub fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) as c_int -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyFrozenSet_Check"] - pub fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type) - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyAnySet_CheckExact"] - pub fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PySet_Type) || Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) - as c_int -} - -#[inline] -pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int { - (PyAnySet_CheckExact(ob) != 0 - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PySet_Type)) != 0 - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int -} - -#[inline] -#[cfg(Py_3_10)] -pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int { - crate::Py_IS_TYPE(op, addr_of_mut!(PySet_Type)) -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPySet_Check"] - pub fn PySet_Check(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PySet_Type) - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PySet_Type)) != 0) as c_int -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/sliceobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/sliceobject.rs deleted file mode 100644 index a3ea153987..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/sliceobject.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_EllipsisObject")] - static mut _Py_EllipsisObject: PyObject; - - #[cfg(GraalPy)] - static mut _Py_EllipsisObjectReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_Ellipsis() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_EllipsisObject); - #[cfg(GraalPy)] - return _Py_EllipsisObjectReference; -} - -#[cfg(not(Py_LIMITED_API))] -#[repr(C)] -pub struct PySliceObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub start: *mut PyObject, - #[cfg(not(GraalPy))] - pub stop: *mut PyObject, - #[cfg(not(GraalPy))] - pub step: *mut PyObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_Type")] - pub static mut PySlice_Type: PyTypeObject; - pub static mut PyEllipsis_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PySlice_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PySlice_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_New")] - pub fn PySlice_New( - start: *mut PyObject, - stop: *mut PyObject, - step: *mut PyObject, - ) -> *mut PyObject; - - // skipped non-limited _PySlice_FromIndices - // skipped non-limited _PySlice_GetLongIndices - - #[cfg_attr(PyPy, link_name = "PyPySlice_GetIndices")] - pub fn PySlice_GetIndices( - r: *mut PyObject, - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - ) -> c_int; -} - -#[inline] -pub unsafe fn PySlice_GetIndicesEx( - slice: *mut PyObject, - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - slicelength: *mut Py_ssize_t, -) -> c_int { - if PySlice_Unpack(slice, start, stop, step) < 0 { - *slicelength = 0; - -1 - } else { - *slicelength = PySlice_AdjustIndices(length, start, stop, *step); - 0 - } -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_Unpack")] - pub fn PySlice_Unpack( - slice: *mut PyObject, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - ) -> c_int; - - #[cfg_attr(all(PyPy, Py_3_10), link_name = "PyPySlice_AdjustIndices")] - pub fn PySlice_AdjustIndices( - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: Py_ssize_t, - ) -> Py_ssize_t; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/structmember.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/structmember.rs deleted file mode 100644 index afae165c5b..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/structmember.rs +++ /dev/null @@ -1,33 +0,0 @@ -use std::os::raw::c_int; - -pub use crate::PyMemberDef; - -pub use crate::Py_T_BOOL as T_BOOL; -pub use crate::Py_T_BYTE as T_BYTE; -pub use crate::Py_T_CHAR as T_CHAR; -pub use crate::Py_T_DOUBLE as T_DOUBLE; -pub use crate::Py_T_FLOAT as T_FLOAT; -pub use crate::Py_T_INT as T_INT; -pub use crate::Py_T_LONG as T_LONG; -pub use crate::Py_T_LONGLONG as T_LONGLONG; -pub use crate::Py_T_OBJECT_EX as T_OBJECT_EX; -pub use crate::Py_T_SHORT as T_SHORT; -pub use crate::Py_T_STRING as T_STRING; -pub use crate::Py_T_STRING_INPLACE as T_STRING_INPLACE; -pub use crate::Py_T_UBYTE as T_UBYTE; -pub use crate::Py_T_UINT as T_UINT; -pub use crate::Py_T_ULONG as T_ULONG; -pub use crate::Py_T_ULONGLONG as T_ULONGLONG; -pub use crate::Py_T_USHORT as T_USHORT; -#[allow(deprecated)] -pub use crate::_Py_T_OBJECT as T_OBJECT; - -pub use crate::Py_T_PYSSIZET as T_PYSSIZET; -#[allow(deprecated)] -pub use crate::_Py_T_NONE as T_NONE; - -/* Flags */ -pub use crate::Py_READONLY as READONLY; -pub const READ_RESTRICTED: c_int = 2; -pub const PY_WRITE_RESTRICTED: c_int = 4; -pub const RESTRICTED: c_int = READ_RESTRICTED | PY_WRITE_RESTRICTED; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/structseq.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/structseq.rs deleted file mode 100644 index f8566787b5..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/structseq.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::object::{PyObject, PyTypeObject}; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStructSequence_Field { - pub name: *const c_char, - pub doc: *const c_char, -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStructSequence_Desc { - pub name: *const c_char, - pub doc: *const c_char, - pub fields: *mut PyStructSequence_Field, - pub n_in_sequence: c_int, -} - -// skipped PyStructSequence_UnnamedField; - -extern "C" { - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_InitType")] - pub fn PyStructSequence_InitType(_type: *mut PyTypeObject, desc: *mut PyStructSequence_Desc); - - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_InitType2")] - pub fn PyStructSequence_InitType2( - _type: *mut PyTypeObject, - desc: *mut PyStructSequence_Desc, - ) -> c_int; - - #[cfg(not(PyPy))] - pub fn PyStructSequence_NewType(desc: *mut PyStructSequence_Desc) -> *mut PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_New")] - pub fn PyStructSequence_New(_type: *mut PyTypeObject) -> *mut PyObject; -} - -#[cfg(not(Py_LIMITED_API))] -pub type PyStructSequence = crate::PyTupleObject; - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[inline] -pub unsafe fn PyStructSequence_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - crate::PyTuple_SET_ITEM(op, i, v) -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[inline] -pub unsafe fn PyStructSequence_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - crate::PyTuple_GET_ITEM(op, i) -} - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyStructSequence_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); - - #[cfg(not(PyPy))] - pub fn PyStructSequence_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/sysmodule.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/sysmodule.rs deleted file mode 100644 index 6f402197ec..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/sysmodule.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::object::PyObject; -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySys_GetObject")] - pub fn PySys_GetObject(arg1: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySys_SetObject")] - pub fn PySys_SetObject(arg1: *const c_char, arg2: *mut PyObject) -> c_int; - - #[cfg_attr( - Py_3_11, - deprecated( - note = "Deprecated in Python 3.11, use `PyConfig.argv` and `PyConfig.parse_argv` instead" - ) - )] - pub fn PySys_SetArgv(arg1: c_int, arg2: *mut *mut wchar_t); - #[cfg_attr( - Py_3_11, - deprecated( - note = "Deprecated in Python 3.11, use `PyConfig.argv` and `PyConfig.parse_argv` instead" - ) - )] - pub fn PySys_SetArgvEx(arg1: c_int, arg2: *mut *mut wchar_t, arg3: c_int); - pub fn PySys_SetPath(arg1: *const wchar_t); - - #[cfg_attr(PyPy, link_name = "PyPySys_WriteStdout")] - pub fn PySys_WriteStdout(format: *const c_char, ...); - #[cfg_attr(PyPy, link_name = "PyPySys_WriteStderr")] - pub fn PySys_WriteStderr(format: *const c_char, ...); - pub fn PySys_FormatStdout(format: *const c_char, ...); - pub fn PySys_FormatStderr(format: *const c_char, ...); - - #[cfg_attr( - Py_3_13, - deprecated( - note = "Deprecated since Python 3.13. Clear sys.warnoptions and warnings.filters instead." - ) - )] - pub fn PySys_ResetWarnOptions(); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_AddWarnOption(arg1: *const wchar_t); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_AddWarnOptionUnicode(arg1: *mut PyObject); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_HasWarnOptions() -> c_int; - - pub fn PySys_AddXOption(arg1: *const wchar_t); - pub fn PySys_GetXOptions() -> *mut PyObject; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/traceback.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/traceback.rs deleted file mode 100644 index 432b6980ef..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/traceback.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Here")] - pub fn PyTraceBack_Here(arg1: *mut crate::PyFrameObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Print")] - pub fn PyTraceBack_Print(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Type")] - pub static mut PyTraceBack_Type: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyTraceBack_Check"] - pub fn PyTraceBack_Check(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyTraceBack_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyTraceBack_Type)) as c_int -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/tupleobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/tupleobject.rs deleted file mode 100644 index d265c91a4b..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/tupleobject.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTuple_Type")] - pub static mut PyTuple_Type: PyTypeObject; - pub static mut PyTupleIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyTuple_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TUPLE_SUBCLASS) -} - -#[inline] -pub unsafe fn PyTuple_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyTuple_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTuple_New")] - pub fn PyTuple_New(size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_Size")] - pub fn PyTuple_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyTuple_GetItem")] - pub fn PyTuple_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_SetItem")] - pub fn PyTuple_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyTuple_GetSlice")] - pub fn PyTuple_GetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_Pack")] - pub fn PyTuple_Pack(arg1: Py_ssize_t, ...) -> *mut PyObject; - #[cfg(not(Py_3_9))] - pub fn PyTuple_ClearFreeList() -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/typeslots.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/typeslots.rs deleted file mode 100644 index da7c60b310..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/typeslots.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::os::raw::c_int; - -pub const Py_bf_getbuffer: c_int = 1; -pub const Py_bf_releasebuffer: c_int = 2; -pub const Py_mp_ass_subscript: c_int = 3; -pub const Py_mp_length: c_int = 4; -pub const Py_mp_subscript: c_int = 5; -pub const Py_nb_absolute: c_int = 6; -pub const Py_nb_add: c_int = 7; -pub const Py_nb_and: c_int = 8; -pub const Py_nb_bool: c_int = 9; -pub const Py_nb_divmod: c_int = 10; -pub const Py_nb_float: c_int = 11; -pub const Py_nb_floor_divide: c_int = 12; -pub const Py_nb_index: c_int = 13; -pub const Py_nb_inplace_add: c_int = 14; -pub const Py_nb_inplace_and: c_int = 15; -pub const Py_nb_inplace_floor_divide: c_int = 16; -pub const Py_nb_inplace_lshift: c_int = 17; -pub const Py_nb_inplace_multiply: c_int = 18; -pub const Py_nb_inplace_or: c_int = 19; -pub const Py_nb_inplace_power: c_int = 20; -pub const Py_nb_inplace_remainder: c_int = 21; -pub const Py_nb_inplace_rshift: c_int = 22; -pub const Py_nb_inplace_subtract: c_int = 23; -pub const Py_nb_inplace_true_divide: c_int = 24; -pub const Py_nb_inplace_xor: c_int = 25; -pub const Py_nb_int: c_int = 26; -pub const Py_nb_invert: c_int = 27; -pub const Py_nb_lshift: c_int = 28; -pub const Py_nb_multiply: c_int = 29; -pub const Py_nb_negative: c_int = 30; -pub const Py_nb_or: c_int = 31; -pub const Py_nb_positive: c_int = 32; -pub const Py_nb_power: c_int = 33; -pub const Py_nb_remainder: c_int = 34; -pub const Py_nb_rshift: c_int = 35; -pub const Py_nb_subtract: c_int = 36; -pub const Py_nb_true_divide: c_int = 37; -pub const Py_nb_xor: c_int = 38; -pub const Py_sq_ass_item: c_int = 39; -pub const Py_sq_concat: c_int = 40; -pub const Py_sq_contains: c_int = 41; -pub const Py_sq_inplace_concat: c_int = 42; -pub const Py_sq_inplace_repeat: c_int = 43; -pub const Py_sq_item: c_int = 44; -pub const Py_sq_length: c_int = 45; -pub const Py_sq_repeat: c_int = 46; -pub const Py_tp_alloc: c_int = 47; -pub const Py_tp_base: c_int = 48; -pub const Py_tp_bases: c_int = 49; -pub const Py_tp_call: c_int = 50; -pub const Py_tp_clear: c_int = 51; -pub const Py_tp_dealloc: c_int = 52; -pub const Py_tp_del: c_int = 53; -pub const Py_tp_descr_get: c_int = 54; -pub const Py_tp_descr_set: c_int = 55; -pub const Py_tp_doc: c_int = 56; -pub const Py_tp_getattr: c_int = 57; -pub const Py_tp_getattro: c_int = 58; -pub const Py_tp_hash: c_int = 59; -pub const Py_tp_init: c_int = 60; -pub const Py_tp_is_gc: c_int = 61; -pub const Py_tp_iter: c_int = 62; -pub const Py_tp_iternext: c_int = 63; -pub const Py_tp_methods: c_int = 64; -pub const Py_tp_new: c_int = 65; -pub const Py_tp_repr: c_int = 66; -pub const Py_tp_richcompare: c_int = 67; -pub const Py_tp_setattr: c_int = 68; -pub const Py_tp_setattro: c_int = 69; -pub const Py_tp_str: c_int = 70; -pub const Py_tp_traverse: c_int = 71; -pub const Py_tp_members: c_int = 72; -pub const Py_tp_getset: c_int = 73; -pub const Py_tp_free: c_int = 74; -pub const Py_nb_matrix_multiply: c_int = 75; -pub const Py_nb_inplace_matrix_multiply: c_int = 76; -pub const Py_am_await: c_int = 77; -pub const Py_am_aiter: c_int = 78; -pub const Py_am_anext: c_int = 79; -pub const Py_tp_finalize: c_int = 80; diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/unicodeobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/unicodeobject.rs deleted file mode 100644 index 1e0425ce2a..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/unicodeobject.rs +++ /dev/null @@ -1,353 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_void}; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `libc::wchar_t` instead.") -)] -pub type Py_UNICODE = wchar_t; - -pub type Py_UCS4 = u32; -pub type Py_UCS2 = u16; -pub type Py_UCS1 = u8; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Type")] - pub static mut PyUnicode_Type: PyTypeObject; - pub static mut PyUnicodeIter_Type: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyUnicode_Check"] - pub fn PyUnicode_Check(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyUnicode_CheckExact"] - pub fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyUnicode_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyUnicode_Type)) as c_int -} - -pub const Py_UNICODE_REPLACEMENT_CHARACTER: Py_UCS4 = 0xFFFD; - -extern "C" { - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromStringAndSize")] - pub fn PyUnicode_FromStringAndSize(u: *const c_char, size: Py_ssize_t) -> *mut PyObject; - pub fn PyUnicode_FromString(u: *const c_char) -> *mut PyObject; - - pub fn PyUnicode_Substring( - str: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - ) -> *mut PyObject; - pub fn PyUnicode_AsUCS4( - unicode: *mut PyObject, - buffer: *mut Py_UCS4, - buflen: Py_ssize_t, - copy_null: c_int, - ) -> *mut Py_UCS4; - pub fn PyUnicode_AsUCS4Copy(unicode: *mut PyObject) -> *mut Py_UCS4; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetLength")] - pub fn PyUnicode_GetLength(unicode: *mut PyObject) -> Py_ssize_t; - #[cfg(not(Py_3_12))] - #[deprecated(note = "Removed in Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetSize")] - pub fn PyUnicode_GetSize(unicode: *mut PyObject) -> Py_ssize_t; - pub fn PyUnicode_ReadChar(unicode: *mut PyObject, index: Py_ssize_t) -> Py_UCS4; - pub fn PyUnicode_WriteChar( - unicode: *mut PyObject, - index: Py_ssize_t, - character: Py_UCS4, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Resize")] - pub fn PyUnicode_Resize(unicode: *mut *mut PyObject, length: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromEncodedObject")] - pub fn PyUnicode_FromEncodedObject( - obj: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromObject")] - pub fn PyUnicode_FromObject(obj: *mut PyObject) -> *mut PyObject; - // #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormatV")] - // pub fn PyUnicode_FromFormatV(format: *const c_char, vargs: va_list) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormat")] - pub fn PyUnicode_FromFormat(format: *const c_char, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternInPlace")] - pub fn PyUnicode_InternInPlace(arg1: *mut *mut PyObject); - #[cfg(not(Py_3_12))] - #[cfg_attr(Py_3_10, deprecated(note = "Python 3.10"))] - pub fn PyUnicode_InternImmortal(arg1: *mut *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternFromString")] - pub fn PyUnicode_InternFromString(u: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromWideChar")] - pub fn PyUnicode_FromWideChar(w: *const wchar_t, size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideChar")] - pub fn PyUnicode_AsWideChar( - unicode: *mut PyObject, - w: *mut wchar_t, - size: Py_ssize_t, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideCharString")] - pub fn PyUnicode_AsWideCharString( - unicode: *mut PyObject, - size: *mut Py_ssize_t, - ) -> *mut wchar_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromOrdinal")] - pub fn PyUnicode_FromOrdinal(ordinal: c_int) -> *mut PyObject; - pub fn PyUnicode_ClearFreeList() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetDefaultEncoding")] - pub fn PyUnicode_GetDefaultEncoding() -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Decode")] - pub fn PyUnicode_Decode( - s: *const c_char, - size: Py_ssize_t, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsDecodedObject( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsDecodedUnicode( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedObject")] - pub fn PyUnicode_AsEncodedObject( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedString")] - pub fn PyUnicode_AsEncodedString( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsEncodedUnicode( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_BuildEncodingMap(string: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF7( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF7Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF8")] - pub fn PyUnicode_DecodeUTF8( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF8Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8String")] - pub fn PyUnicode_AsUTF8String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8AndSize")] - pub fn PyUnicode_AsUTF8AndSize(unicode: *mut PyObject, size: *mut Py_ssize_t) -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF32")] - pub fn PyUnicode_DecodeUTF32( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF32Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF32String")] - pub fn PyUnicode_AsUTF32String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF16")] - pub fn PyUnicode_DecodeUTF16( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF16Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF16String")] - pub fn PyUnicode_AsUTF16String(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeUnicodeEscape( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicodeEscapeString")] - pub fn PyUnicode_AsUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeRawUnicodeEscape( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsRawUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeLatin1")] - pub fn PyUnicode_DecodeLatin1( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsLatin1String")] - pub fn PyUnicode_AsLatin1String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeASCII")] - pub fn PyUnicode_DecodeASCII( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsASCIIString")] - pub fn PyUnicode_AsASCIIString(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeCharmap( - string: *const c_char, - length: Py_ssize_t, - mapping: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsCharmapString( - unicode: *mut PyObject, - mapping: *mut PyObject, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeLocaleAndSize( - str: *const c_char, - len: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeLocale(str: *const c_char, errors: *const c_char) -> *mut PyObject; - pub fn PyUnicode_EncodeLocale(unicode: *mut PyObject, errors: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSConverter")] - pub fn PyUnicode_FSConverter(arg1: *mut PyObject, arg2: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSDecoder")] - pub fn PyUnicode_FSDecoder(arg1: *mut PyObject, arg2: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefault")] - pub fn PyUnicode_DecodeFSDefault(s: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefaultAndSize")] - pub fn PyUnicode_DecodeFSDefaultAndSize(s: *const c_char, size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeFSDefault")] - pub fn PyUnicode_EncodeFSDefault(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Concat")] - pub fn PyUnicode_Concat(left: *mut PyObject, right: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_Append(pleft: *mut *mut PyObject, right: *mut PyObject); - pub fn PyUnicode_AppendAndDel(pleft: *mut *mut PyObject, right: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Split")] - pub fn PyUnicode_Split( - s: *mut PyObject, - sep: *mut PyObject, - maxsplit: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Splitlines")] - pub fn PyUnicode_Splitlines(s: *mut PyObject, keepends: c_int) -> *mut PyObject; - pub fn PyUnicode_Partition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_RPartition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_RSplit( - s: *mut PyObject, - sep: *mut PyObject, - maxsplit: Py_ssize_t, - ) -> *mut PyObject; - pub fn PyUnicode_Translate( - str: *mut PyObject, - table: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Join")] - pub fn PyUnicode_Join(separator: *mut PyObject, seq: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Tailmatch")] - pub fn PyUnicode_Tailmatch( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Find")] - pub fn PyUnicode_Find( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - pub fn PyUnicode_FindChar( - str: *mut PyObject, - ch: Py_UCS4, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Count")] - pub fn PyUnicode_Count( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Replace")] - pub fn PyUnicode_Replace( - str: *mut PyObject, - substr: *mut PyObject, - replstr: *mut PyObject, - maxcount: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Compare")] - pub fn PyUnicode_Compare(left: *mut PyObject, right: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_CompareWithASCIIString")] - pub fn PyUnicode_CompareWithASCIIString(left: *mut PyObject, right: *const c_char) -> c_int; - #[cfg(Py_3_13)] - pub fn PyUnicode_EqualToUTF8(unicode: *mut PyObject, string: *const c_char) -> c_int; - #[cfg(Py_3_13)] - pub fn PyUnicode_EqualToUTF8AndSize( - unicode: *mut PyObject, - string: *const c_char, - size: Py_ssize_t, - ) -> c_int; - - pub fn PyUnicode_RichCompare( - left: *mut PyObject, - right: *mut PyObject, - op: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Format")] - pub fn PyUnicode_Format(format: *mut PyObject, args: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_Contains(container: *mut PyObject, element: *mut PyObject) -> c_int; - pub fn PyUnicode_IsIdentifier(s: *mut PyObject) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/warnings.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/warnings.rs deleted file mode 100644 index d58f37435d..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/warnings.rs +++ /dev/null @@ -1,34 +0,0 @@ -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnEx")] - pub fn PyErr_WarnEx( - category: *mut PyObject, - message: *const c_char, - stack_level: Py_ssize_t, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnFormat")] - pub fn PyErr_WarnFormat( - category: *mut PyObject, - stack_level: Py_ssize_t, - format: *const c_char, - ... - ) -> c_int; - pub fn PyErr_ResourceWarning( - source: *mut PyObject, - stack_level: Py_ssize_t, - format: *const c_char, - ... - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnExplicit")] - pub fn PyErr_WarnExplicit( - category: *mut PyObject, - message: *const c_char, - filename: *const c_char, - lineno: c_int, - module: *const c_char, - registry: *mut PyObject, - ) -> c_int; -} diff --git a/vendor/orjson/include/pyo3/pyo3-ffi/src/weakrefobject.rs b/vendor/orjson/include/pyo3/pyo3-ffi/src/weakrefobject.rs deleted file mode 100644 index 305dc290fa..0000000000 --- a/vendor/orjson/include/pyo3/pyo3-ffi/src/weakrefobject.rs +++ /dev/null @@ -1,69 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -#[cfg(all(not(PyPy), Py_LIMITED_API, not(GraalPy)))] -opaque_struct!(PyWeakReference); - -#[cfg(all(not(PyPy), not(Py_LIMITED_API), not(GraalPy)))] -pub use crate::_PyWeakReference as PyWeakReference; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PyWeakref_RefType: PyTypeObject; - pub static mut _PyWeakref_ProxyType: PyTypeObject; - pub static mut _PyWeakref_CallableProxyType: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckRef"] - pub fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckRefExact"] - pub fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckProxy"] - pub fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(_PyWeakref_RefType)) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(_PyWeakref_RefType)) as c_int -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int { - ((Py_TYPE(op) == addr_of_mut!(_PyWeakref_ProxyType)) - || (Py_TYPE(op) == addr_of_mut!(_PyWeakref_CallableProxyType))) as c_int -} - -#[inline] -pub unsafe fn PyWeakref_Check(op: *mut PyObject) -> c_int { - (PyWeakref_CheckRef(op) != 0 || PyWeakref_CheckProxy(op) != 0) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewRef")] - pub fn PyWeakref_NewRef(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewProxy")] - pub fn PyWeakref_NewProxy(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetObject")] - #[cfg_attr( - Py_3_13, - deprecated(note = "deprecated since Python 3.13. Use `PyWeakref_GetRef` instead.") - )] - pub fn PyWeakref_GetObject(reference: *mut PyObject) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetRef")] - pub fn PyWeakref_GetRef(reference: *mut PyObject, pobj: *mut *mut PyObject) -> c_int; -} diff --git a/vendor/orjson/include/yyjson/yyjson.c b/vendor/orjson/include/yyjson/yyjson.c deleted file mode 100644 index 803c2f5666..0000000000 --- a/vendor/orjson/include/yyjson/yyjson.c +++ /dev/null @@ -1,9291 +0,0 @@ -/*============================================================================== - Copyright (c) 2020 YaoYuan - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - *============================================================================*/ - -#include "yyjson.h" -#include - - - -/*============================================================================== - * Warning Suppress - *============================================================================*/ - -#if defined(__clang__) -# pragma clang diagnostic ignored "-Wunused-function" -# pragma clang diagnostic ignored "-Wunused-parameter" -# pragma clang diagnostic ignored "-Wunused-label" -# pragma clang diagnostic ignored "-Wunused-macros" -# pragma clang diagnostic ignored "-Wunused-variable" -#elif defined(__GNUC__) -# pragma GCC diagnostic ignored "-Wunused-function" -# pragma GCC diagnostic ignored "-Wunused-parameter" -# pragma GCC diagnostic ignored "-Wunused-label" -# pragma GCC diagnostic ignored "-Wunused-macros" -# pragma GCC diagnostic ignored "-Wunused-variable" -#elif defined(_MSC_VER) -# pragma warning(disable:4100) /* unreferenced formal parameter */ -# pragma warning(disable:4101) /* unreferenced variable */ -# pragma warning(disable:4102) /* unreferenced label */ -# pragma warning(disable:4127) /* conditional expression is constant */ -# pragma warning(disable:4706) /* assignment within conditional expression */ -#endif - - - -/*============================================================================== - * Version - *============================================================================*/ - -uint32_t yyjson_version(void) { - return YYJSON_VERSION_HEX; -} - - - -/*============================================================================== - * Flags - *============================================================================*/ - -/* msvc intrinsic */ -#if YYJSON_MSC_VER >= 1400 -# include -# if defined(_M_AMD64) || defined(_M_ARM64) -# define MSC_HAS_BIT_SCAN_64 1 -# pragma intrinsic(_BitScanForward64) -# pragma intrinsic(_BitScanReverse64) -# else -# define MSC_HAS_BIT_SCAN_64 0 -# endif -# if defined(_M_AMD64) || defined(_M_ARM64) || \ - defined(_M_IX86) || defined(_M_ARM) -# define MSC_HAS_BIT_SCAN 1 -# pragma intrinsic(_BitScanForward) -# pragma intrinsic(_BitScanReverse) -# else -# define MSC_HAS_BIT_SCAN 0 -# endif -# if defined(_M_AMD64) -# define MSC_HAS_UMUL128 1 -# pragma intrinsic(_umul128) -# else -# define MSC_HAS_UMUL128 0 -# endif -#else -# define MSC_HAS_BIT_SCAN_64 0 -# define MSC_HAS_BIT_SCAN 0 -# define MSC_HAS_UMUL128 0 -#endif - -/* gcc builtin */ -#if yyjson_has_builtin(__builtin_clzll) || yyjson_gcc_available(3, 4, 0) -# define GCC_HAS_CLZLL 1 -#else -# define GCC_HAS_CLZLL 0 -#endif - -#if yyjson_has_builtin(__builtin_ctzll) || yyjson_gcc_available(3, 4, 0) -# define GCC_HAS_CTZLL 1 -#else -# define GCC_HAS_CTZLL 0 -#endif - -/* int128 type */ -#if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \ - (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)) -# define YYJSON_HAS_INT128 1 -#else -# define YYJSON_HAS_INT128 0 -#endif - -/* IEEE 754 floating-point binary representation */ -#if defined(__STDC_IEC_559__) || defined(__STDC_IEC_60559_BFP__) -# define YYJSON_HAS_IEEE_754 1 -#elif (FLT_RADIX == 2) && (DBL_MANT_DIG == 53) && (DBL_DIG == 15) && \ - (DBL_MIN_EXP == -1021) && (DBL_MAX_EXP == 1024) && \ - (DBL_MIN_10_EXP == -307) && (DBL_MAX_10_EXP == 308) -# define YYJSON_HAS_IEEE_754 1 -#else -# define YYJSON_HAS_IEEE_754 0 -#endif - -/* - Correct rounding in double number computations. - - On the x86 architecture, some compilers may use x87 FPU instructions for - floating-point arithmetic. The x87 FPU loads all floating point number as - 80-bit double-extended precision internally, then rounds the result to original - precision, which may produce inaccurate results. For a more detailed - explanation, see the paper: https://arxiv.org/abs/cs/0701192 - - Here are some examples of double precision calculation error: - - 2877.0 / 1e6 == 0.002877, but x87 returns 0.0028770000000000002 - 43683.0 * 1e21 == 4.3683e25, but x87 returns 4.3683000000000004e25 - - Here are some examples of compiler flags to generate x87 instructions on x86: - - clang -m32 -mno-sse - gcc/icc -m32 -mfpmath=387 - msvc /arch:SSE or /arch:IA32 - - If we are sure that there's no similar error described above, we can define the - YYJSON_DOUBLE_MATH_CORRECT as 1 to enable the fast path calculation. This is - not an accurate detection, it's just try to avoid the error at compile-time. - An accurate detection can be done at run-time: - - bool is_double_math_correct(void) { - volatile double r = 43683.0; - r *= 1e21; - return r == 4.3683e25; - } - - See also: utils.h in https://github.com/google/double-conversion/ - */ -#if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__) -# define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ -#endif - -#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1 -# define YYJSON_DOUBLE_MATH_CORRECT 0 -#elif defined(i386) || defined(__i386) || defined(__i386__) || \ - defined(_X86_) || defined(__X86__) || defined(_M_IX86) || \ - defined(__I86__) || defined(__IA32__) || defined(__THW_INTEL) -# if (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 2) || \ - (defined(__SSE2_MATH__) && __SSE2_MATH__) -# define YYJSON_DOUBLE_MATH_CORRECT 1 -# else -# define YYJSON_DOUBLE_MATH_CORRECT 0 -# endif -#elif defined(__mc68000__) || defined(__pnacl__) || defined(__native_client__) -# define YYJSON_DOUBLE_MATH_CORRECT 0 -#else -# define YYJSON_DOUBLE_MATH_CORRECT 1 -#endif - -/* endian */ -#if yyjson_has_include() -# include /* POSIX */ -#endif -#if yyjson_has_include() -# include /* Linux */ -#elif yyjson_has_include() -# include /* BSD, Android */ -#elif yyjson_has_include() -# include /* BSD, Darwin */ -#endif - -#define YYJSON_BIG_ENDIAN 4321 -#define YYJSON_LITTLE_ENDIAN 1234 - -#if defined(BYTE_ORDER) && BYTE_ORDER -# if defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN) -# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN -# elif defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN) -# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN -# endif - -#elif defined(__BYTE_ORDER) && __BYTE_ORDER -# if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) -# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN -# elif defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) -# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN -# endif - -#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ -# if defined(__ORDER_BIG_ENDIAN__) && \ - (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) -# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN -# elif defined(__ORDER_LITTLE_ENDIAN__) && \ - (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) -# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN -# endif - -#elif (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \ - defined(__i386) || defined(__i386__) || \ - defined(_X86_) || defined(__X86__) || \ - defined(_M_IX86) || defined(__THW_INTEL__) || \ - defined(__x86_64) || defined(__x86_64__) || \ - defined(__amd64) || defined(__amd64__) || \ - defined(_M_AMD64) || defined(_M_X64) || \ - defined(_M_ARM) || defined(_M_ARM64) || \ - defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \ - defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \ - defined(__EMSCRIPTEN__) || defined(__wasm__) || \ - defined(__loongarch__) -# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN - -#elif (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \ - defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ - defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \ - defined(__or1k__) || defined(__OR1K__) -# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN - -#else -# define YYJSON_ENDIAN 0 /* unknown endian, detect at run-time */ -#endif - -/* - This macro controls how yyjson handles unaligned memory accesses. - - By default, yyjson uses `memcpy()` for memory copying. This takes advantage of - the compiler's automatic optimizations to generate unaligned memory access - instructions when the target architecture supports it. - - However, for some older compilers or architectures where `memcpy()` isn't - optimized well and may generate unnecessary function calls, consider defining - this macro as 1. In such cases, yyjson switches to manual byte-by-byte access, - potentially improving performance. An example of the generated assembly code on - the ARM platform can be found here: https://godbolt.org/z/334jjhxPT - - As this flag has already been enabled for some common architectures in the - following code, users typically don't need to manually specify it. If users are - unsure about it, please review the generated assembly code or perform actual - benchmark to make an informed decision. - */ -#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS -# if defined(__ia64) || defined(_IA64) || defined(__IA64__) || \ - defined(__ia64__) || defined(_M_IA64) || defined(__itanium__) -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* Itanium */ -# elif (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) && \ - (defined(__GNUC__) || defined(__clang__)) && \ - (!defined(__ARM_FEATURE_UNALIGNED) || !__ARM_FEATURE_UNALIGNED) -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* ARM */ -# elif defined(__sparc) || defined(__sparc__) -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* SPARC */ -# elif defined(__mips) || defined(__mips__) || defined(__MIPS__) -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* MIPS */ -# elif defined(__m68k__) || defined(M68000) -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* M68K */ -# else -# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 0 -# endif -#endif - -/* - Estimated initial ratio of the JSON data (data_size / value_count). - For example: - - data: {"id":12345678,"name":"Harry"} - data_size: 30 - value_count: 5 - ratio: 6 - - yyjson uses dynamic memory with a growth factor of 1.5 when reading and writing - JSON, the ratios below are used to determine the initial memory size. - - A too large ratio will waste memory, and a too small ratio will cause multiple - memory growths and degrade performance. Currently, these ratios are generated - with some commonly used JSON datasets. - */ -#define YYJSON_READER_ESTIMATED_PRETTY_RATIO 16 -#define YYJSON_READER_ESTIMATED_MINIFY_RATIO 6 -#define YYJSON_WRITER_ESTIMATED_PRETTY_RATIO 32 -#define YYJSON_WRITER_ESTIMATED_MINIFY_RATIO 18 - -/* The initial and maximum size of the memory pool's chunk in yyjson_mut_doc. */ -#define YYJSON_MUT_DOC_STR_POOL_INIT_SIZE 0x100 -#define YYJSON_MUT_DOC_STR_POOL_MAX_SIZE 0x10000000 -#define YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE (0x10 * sizeof(yyjson_mut_val)) -#define YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE (0x1000000 * sizeof(yyjson_mut_val)) - -/* The minimum size of the dynamic allocator's chunk. */ -#define YYJSON_ALC_DYN_MIN_SIZE 0x1000 - -/* Default value for compile-time options. */ -#ifndef YYJSON_DISABLE_READER -#define YYJSON_DISABLE_READER 0 -#endif -#ifndef YYJSON_DISABLE_WRITER -#define YYJSON_DISABLE_WRITER 0 -#endif -#ifndef YYJSON_DISABLE_UTILS -#define YYJSON_DISABLE_UTILS 0 -#endif -#ifndef YYJSON_DISABLE_FAST_FP_CONV -#define YYJSON_DISABLE_FAST_FP_CONV 0 -#endif -#ifndef YYJSON_DISABLE_NON_STANDARD -#define YYJSON_DISABLE_NON_STANDARD 0 -#endif -#ifndef YYJSON_DISABLE_UTF8_VALIDATION -#define YYJSON_DISABLE_UTF8_VALIDATION 0 -#endif -#ifndef YYJSON_READER_CONTAINER_RECURSION_LIMIT -#define YYJSON_READER_CONTAINER_RECURSION_LIMIT 1024 -#endif - -/*============================================================================== - * Macros - *============================================================================*/ - -/* Macros used for loop unrolling and other purpose. */ -#define repeat2(x) { x x } -#define repeat3(x) { x x x } -#define repeat4(x) { x x x x } -#define repeat8(x) { x x x x x x x x } -#define repeat16(x) { x x x x x x x x x x x x x x x x } - -#define repeat2_incr(x) { x(0) x(1) } -#define repeat4_incr(x) { x(0) x(1) x(2) x(3) } -#define repeat8_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) } -#define repeat16_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ - x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) } - -#define repeat_in_1_18(x) { x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) \ - x(9) x(10) x(11) x(12) x(13) x(14) x(15) x(16) \ - x(17) x(18) } - -/* Macros used to provide branch prediction information for compiler. */ -#undef likely -#define likely(x) yyjson_likely(x) -#undef unlikely -#define unlikely(x) yyjson_unlikely(x) - -/* Macros used to provide inline information for compiler. */ -#undef static_inline -#define static_inline static yyjson_inline -#undef static_noinline -#define static_noinline static yyjson_noinline - -/* Macros for min and max. */ -#undef yyjson_min -#define yyjson_min(x, y) ((x) < (y) ? (x) : (y)) -#undef yyjson_max -#define yyjson_max(x, y) ((x) > (y) ? (x) : (y)) - -/* Used to write u64 literal for C89 which doesn't support "ULL" suffix. */ -#undef U64 -#define U64(hi, lo) ((((u64)hi##UL) << 32U) + lo##UL) - -/* Used to cast away (remove) const qualifier. */ -#define constcast(type) (type)(void *)(size_t)(const void *) - -/* flag test */ -#define has_read_flag(_flag) false -#define has_write_flag(_flag) unlikely(write_flag_eq(flg, YYJSON_WRITE_##_flag)) - -static_inline bool read_flag_eq(yyjson_read_flag flg, yyjson_read_flag chk) { -#if YYJSON_DISABLE_NON_STANDARD - if (chk == YYJSON_READ_ALLOW_INF_AND_NAN || - chk == YYJSON_READ_ALLOW_COMMENTS || - chk == YYJSON_READ_ALLOW_TRAILING_COMMAS || - chk == YYJSON_READ_ALLOW_INVALID_UNICODE) - return false; /* this should be evaluated at compile-time */ -#endif - return (flg & chk) != 0; -} - -static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { -#if YYJSON_DISABLE_NON_STANDARD - if (chk == YYJSON_WRITE_ALLOW_INF_AND_NAN || - chk == YYJSON_WRITE_ALLOW_INVALID_UNICODE) - return false; /* this should be evaluated at compile-time */ -#endif - return (flg & chk) != 0; -} - - - -/*============================================================================== - * Integer Constants - *============================================================================*/ - -/* U64 constant values */ -#undef U64_MAX -#define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF) -#undef I64_MAX -#define I64_MAX U64(0x7FFFFFFF, 0xFFFFFFFF) -#undef USIZE_MAX -#define USIZE_MAX ((usize)(~(usize)0)) - -/* Maximum number of digits for reading u32/u64/usize safety (not overflow). */ -#undef U32_SAFE_DIG -#define U32_SAFE_DIG 9 /* u32 max is 4294967295, 10 digits */ -#undef U64_SAFE_DIG -#define U64_SAFE_DIG 19 /* u64 max is 18446744073709551615, 20 digits */ -#undef USIZE_SAFE_DIG -#define USIZE_SAFE_DIG (sizeof(usize) == 8 ? U64_SAFE_DIG : U32_SAFE_DIG) - - - -/*============================================================================== - * IEEE-754 Double Number Constants - *============================================================================*/ - -/* Inf raw value (positive) */ -#define F64_RAW_INF U64(0x7FF00000, 0x00000000) - -/* NaN raw value (quiet NaN, no payload, no sign) */ -#if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008)) -#define F64_RAW_NAN U64(0x7FF7FFFF, 0xFFFFFFFF) -#else -#define F64_RAW_NAN U64(0x7FF80000, 0x00000000) -#endif - -/* double number bits */ -#define F64_BITS 64 - -/* double number exponent part bits */ -#define F64_EXP_BITS 11 - -/* double number significand part bits */ -#define F64_SIG_BITS 52 - -/* double number significand part bits (with 1 hidden bit) */ -#define F64_SIG_FULL_BITS 53 - -/* double number significand bit mask */ -#define F64_SIG_MASK U64(0x000FFFFF, 0xFFFFFFFF) - -/* double number exponent bit mask */ -#define F64_EXP_MASK U64(0x7FF00000, 0x00000000) - -/* double number exponent bias */ -#define F64_EXP_BIAS 1023 - -/* double number significant digits count in decimal */ -#define F64_DEC_DIG 17 - -/* max significant digits count in decimal when reading double number */ -#define F64_MAX_DEC_DIG 768 - -/* maximum decimal power of double number (1.7976931348623157e308) */ -#define F64_MAX_DEC_EXP 308 - -/* minimum decimal power of double number (4.9406564584124654e-324) */ -#define F64_MIN_DEC_EXP (-324) - -/* maximum binary power of double number */ -#define F64_MAX_BIN_EXP 1024 - -/* minimum binary power of double number */ -#define F64_MIN_BIN_EXP (-1021) - - - -/*============================================================================== - * Types - *============================================================================*/ - -/** Type define for primitive types. */ -typedef float f32; -typedef double f64; -typedef int8_t i8; -typedef uint8_t u8; -typedef int16_t i16; -typedef uint16_t u16; -typedef int32_t i32; -typedef uint32_t u32; -typedef int64_t i64; -typedef uint64_t u64; -typedef size_t usize; - -/** 128-bit integer, used by floating-point number reader and writer. */ -#if YYJSON_HAS_INT128 -__extension__ typedef __int128 i128; -__extension__ typedef unsigned __int128 u128; -#endif - -/** 16/32/64-bit vector */ -typedef struct v16 { char c[2]; } v16; -typedef struct v32 { char c[4]; } v32; -typedef struct v64 { char c[8]; } v64; - -/** 16/32/64-bit vector union */ -typedef union v16_uni { v16 v; u16 u; } v16_uni; -typedef union v32_uni { v32 v; u32 u; } v32_uni; -typedef union v64_uni { v64 v; u64 u; } v64_uni; - - - -/*============================================================================== - * Load/Store Utils - *============================================================================*/ - -#if YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS - -#define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x]; - -static_inline void byte_copy_2(void *dst, const void *src) { - repeat2_incr(byte_move_idx) -} - -static_inline void byte_copy_4(void *dst, const void *src) { - repeat4_incr(byte_move_idx) -} - -static_inline void byte_copy_8(void *dst, const void *src) { - repeat8_incr(byte_move_idx) -} - -static_inline void byte_copy_16(void *dst, const void *src) { - repeat16_incr(byte_move_idx) -} - -static_inline void byte_move_2(void *dst, const void *src) { - repeat2_incr(byte_move_idx) -} - -static_inline void byte_move_4(void *dst, const void *src) { - repeat4_incr(byte_move_idx) -} - -static_inline void byte_move_8(void *dst, const void *src) { - repeat8_incr(byte_move_idx) -} - -static_inline void byte_move_16(void *dst, const void *src) { - repeat16_incr(byte_move_idx) -} - -static_inline bool byte_match_2(void *buf, const char *pat) { - return - ((char *)buf)[0] == ((const char *)pat)[0] && - ((char *)buf)[1] == ((const char *)pat)[1]; -} - -static_inline bool byte_match_4(void *buf, const char *pat) { - return - ((char *)buf)[0] == ((const char *)pat)[0] && - ((char *)buf)[1] == ((const char *)pat)[1] && - ((char *)buf)[2] == ((const char *)pat)[2] && - ((char *)buf)[3] == ((const char *)pat)[3]; -} - -static_inline u16 byte_load_2(const void *src) { - v16_uni uni; - uni.v.c[0] = ((const char *)src)[0]; - uni.v.c[1] = ((const char *)src)[1]; - return uni.u; -} - -static_inline u32 byte_load_3(const void *src) { - v32_uni uni; - uni.v.c[0] = ((const char *)src)[0]; - uni.v.c[1] = ((const char *)src)[1]; - uni.v.c[2] = ((const char *)src)[2]; - uni.v.c[3] = 0; - return uni.u; -} - -static_inline u32 byte_load_4(const void *src) { - v32_uni uni; - uni.v.c[0] = ((const char *)src)[0]; - uni.v.c[1] = ((const char *)src)[1]; - uni.v.c[2] = ((const char *)src)[2]; - uni.v.c[3] = ((const char *)src)[3]; - return uni.u; -} - -#undef byte_move_expr - -#else - -static_inline void byte_copy_2(void *dst, const void *src) { - memcpy(dst, src, 2); -} - -static_inline void byte_copy_4(void *dst, const void *src) { - memcpy(dst, src, 4); -} - -static_inline void byte_copy_8(void *dst, const void *src) { - memcpy(dst, src, 8); -} - -static_inline void byte_copy_16(void *dst, const void *src) { - memcpy(dst, src, 16); -} - -static_inline void byte_move_2(void *dst, const void *src) { - u16 tmp; - memcpy(&tmp, src, 2); - memcpy(dst, &tmp, 2); -} - -static_inline void byte_move_4(void *dst, const void *src) { - u32 tmp; - memcpy(&tmp, src, 4); - memcpy(dst, &tmp, 4); -} - -static_inline void byte_move_8(void *dst, const void *src) { - u64 tmp; - memcpy(&tmp, src, 8); - memcpy(dst, &tmp, 8); -} - -static_inline void byte_move_16(void *dst, const void *src) { - char *pdst = (char *)dst; - const char *psrc = (const char *)src; - u64 tmp1, tmp2; - memcpy(&tmp1, psrc, 8); - memcpy(&tmp2, psrc + 8, 8); - memcpy(pdst, &tmp1, 8); - memcpy(pdst + 8, &tmp2, 8); -} - -static_inline bool byte_match_2(void *buf, const char *pat) { - v16_uni u1, u2; - memcpy(&u1, buf, 2); - memcpy(&u2, pat, 2); - return u1.u == u2.u; -} - -static_inline bool byte_match_4(void *buf, const char *pat) { - v32_uni u1, u2; - memcpy(&u1, buf, 4); - memcpy(&u2, pat, 4); - return u1.u == u2.u; -} - -static_inline u16 byte_load_2(const void *src) { - v16_uni uni; - memcpy(&uni, src, 2); - return uni.u; -} - -static_inline u32 byte_load_3(const void *src) { - v32_uni uni; - memcpy(&uni, src, 2); - uni.v.c[2] = ((const char *)src)[2]; - uni.v.c[3] = 0; - return uni.u; -} - -static_inline u32 byte_load_4(const void *src) { - v32_uni uni; - memcpy(&uni, src, 4); - return uni.u; -} - -#endif - - - -/*============================================================================== - * Number Utils - * These functions are used to detect and convert NaN and Inf numbers. - *============================================================================*/ - -/** Convert raw binary to double. */ -static_inline f64 f64_from_raw(u64 u) { - /* use memcpy to avoid violating the strict aliasing rule */ - f64 f; - memcpy(&f, &u, 8); - return f; -} - -/** Convert double to raw binary. */ -static_inline u64 f64_to_raw(f64 f) { - /* use memcpy to avoid violating the strict aliasing rule */ - u64 u; - memcpy(&u, &f, 8); - return u; -} - -/** Get raw 'infinity' with sign. */ -static_inline u64 f64_raw_get_inf(bool sign) { -#if YYJSON_HAS_IEEE_754 - return F64_RAW_INF | ((u64)sign << 63); -#elif defined(INFINITY) - return f64_to_raw(sign ? -INFINITY : INFINITY); -#else - return f64_to_raw(sign ? -HUGE_VAL : HUGE_VAL); -#endif -} - -/** Get raw 'nan' with sign. */ -static_inline u64 f64_raw_get_nan(bool sign) { -#if YYJSON_HAS_IEEE_754 - return F64_RAW_NAN | ((u64)sign << 63); -#elif defined(NAN) - return f64_to_raw(sign ? (f64)-NAN : (f64)NAN); -#else - return f64_to_raw((sign ? -0.0 : 0.0) / 0.0); -#endif -} - -/** - Convert normalized u64 (highest bit is 1) to f64. - - Some compiler (such as Microsoft Visual C++ 6.0) do not support converting - number from u64 to f64. This function will first convert u64 to i64 and then - to f64, with `to nearest` rounding mode. - */ -static_inline f64 normalized_u64_to_f64(u64 val) { -#if YYJSON_U64_TO_F64_NO_IMPL - i64 sig = (i64)((val >> 1) | (val & 1)); - return ((f64)sig) * (f64)2.0; -#else - return (f64)val; -#endif -} - - - -/*============================================================================== - * Size Utils - * These functions are used for memory allocation. - *============================================================================*/ - -/** Returns whether the size is overflow after increment. */ -static_inline bool size_add_is_overflow(usize size, usize add) { - return size > (size + add); -} - -/** Returns whether the size is power of 2 (size should not be 0). */ -static_inline bool size_is_pow2(usize size) { - return (size & (size - 1)) == 0; -} - -/** Align size upwards (may overflow). */ -static_inline usize size_align_up(usize size, usize align) { - if (size_is_pow2(align)) { - return (size + (align - 1)) & ~(align - 1); - } else { - return size + align - (size + align - 1) % align - 1; - } -} - -/** Align size downwards. */ -static_inline usize size_align_down(usize size, usize align) { - if (size_is_pow2(align)) { - return size & ~(align - 1); - } else { - return size - (size % align); - } -} - -/** Align address upwards (may overflow). */ -static_inline void *mem_align_up(void *mem, usize align) { - usize size; - memcpy(&size, &mem, sizeof(usize)); - size = size_align_up(size, align); - memcpy(&mem, &size, sizeof(usize)); - return mem; -} - - - -/*============================================================================== - * Bits Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ - -/** Returns the number of leading 0-bits in value (input should not be 0). */ -static_inline u32 u64_lz_bits(u64 v) { -#if GCC_HAS_CLZLL - return (u32)__builtin_clzll(v); -#elif MSC_HAS_BIT_SCAN_64 - unsigned long r; - _BitScanReverse64(&r, v); - return (u32)63 - (u32)r; -#elif MSC_HAS_BIT_SCAN - unsigned long hi, lo; - bool hi_set = _BitScanReverse(&hi, (u32)(v >> 32)) != 0; - _BitScanReverse(&lo, (u32)v); - hi |= 32; - return (u32)63 - (u32)(hi_set ? hi : lo); -#else - /* - branchless, use de Bruijn sequences - see: https://www.chessprogramming.org/BitScan - */ - const u8 table[64] = { - 63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2, - 9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1, - 17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18, - 38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0 - }; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v |= v >> 32; - return table[(v * U64(0x03F79D71, 0xB4CB0A89)) >> 58]; -#endif -} - -/** Returns the number of trailing 0-bits in value (input should not be 0). */ -static_inline u32 u64_tz_bits(u64 v) { -#if GCC_HAS_CTZLL - return (u32)__builtin_ctzll(v); -#elif MSC_HAS_BIT_SCAN_64 - unsigned long r; - _BitScanForward64(&r, v); - return (u32)r; -#elif MSC_HAS_BIT_SCAN - unsigned long lo, hi; - bool lo_set = _BitScanForward(&lo, (u32)(v)) != 0; - _BitScanForward(&hi, (u32)(v >> 32)); - hi += 32; - return lo_set ? lo : hi; -#else - /* - branchless, use de Bruijn sequences - see: https://www.chessprogramming.org/BitScan - */ - const u8 table[64] = { - 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, - 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, - 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, - 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 - }; - return table[((v & (~v + 1)) * U64(0x022FDD63, 0xCC95386D)) >> 58]; -#endif -} - - - -/*============================================================================== - * 128-bit Integer Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ - -/** Multiplies two 64-bit unsigned integers (a * b), - returns the 128-bit result as 'hi' and 'lo'. */ -static_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) { -#if YYJSON_HAS_INT128 - u128 m = (u128)a * b; - *hi = (u64)(m >> 64); - *lo = (u64)(m); -#elif MSC_HAS_UMUL128 - *lo = _umul128(a, b, hi); -#else - u32 a0 = (u32)(a), a1 = (u32)(a >> 32); - u32 b0 = (u32)(b), b1 = (u32)(b >> 32); - u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1; - u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1; - u64 m0 = p01 + (p00 >> 32); - u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32); - u64 m1 = p10 + m00; - u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32); - *hi = p11 + m01 + m11; - *lo = ((u64)m10 << 32) | (u32)p00; -#endif -} - -/** Multiplies two 64-bit unsigned integers and add a value (a * b + c), - returns the 128-bit result as 'hi' and 'lo'. */ -static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) { -#if YYJSON_HAS_INT128 - u128 m = (u128)a * b + c; - *hi = (u64)(m >> 64); - *lo = (u64)(m); -#else - u64 h, l, t; - u128_mul(a, b, &h, &l); - t = l + c; - h += (u64)(((t < l) | (t < c))); - *hi = h; - *lo = t; -#endif -} - - - -/*============================================================================== - * File Utils - * These functions are used to read and write JSON files. - *============================================================================*/ - -#define YYJSON_FOPEN_EXT -#if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ) -# if __GLIBC_PREREQ(2, 7) -# undef YYJSON_FOPEN_EXT -# define YYJSON_FOPEN_EXT "e" /* glibc extension to enable O_CLOEXEC */ -# endif -#endif - -static_inline FILE *fopen_safe(const char *path, const char *mode) { -#if YYJSON_MSC_VER >= 1400 - FILE *file = NULL; - if (fopen_s(&file, path, mode) != 0) return NULL; - return file; -#else - return fopen(path, mode); -#endif -} - -static_inline FILE *fopen_readonly(const char *path) { - return fopen_safe(path, "rb" YYJSON_FOPEN_EXT); -} - -static_inline FILE *fopen_writeonly(const char *path) { - return fopen_safe(path, "wb" YYJSON_FOPEN_EXT); -} - -static_inline usize fread_safe(void *buf, usize size, FILE *file) { -#if YYJSON_MSC_VER >= 1400 - return fread_s(buf, size, 1, size, file); -#else - return fread(buf, 1, size, file); -#endif -} - - - -/*============================================================================== - * Default Memory Allocator - * This is a simple libc memory allocator wrapper. - *============================================================================*/ - -static void *default_malloc(void *ctx, usize size) { - return malloc(size); -} - -static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) { - return realloc(ptr, size); -} - -static void default_free(void *ctx, void *ptr) { - free(ptr); -} - -static const yyjson_alc YYJSON_DEFAULT_ALC = { - default_malloc, - default_realloc, - default_free, - NULL -}; - - - -/*============================================================================== - * Null Memory Allocator - * - * This allocator is just a placeholder to ensure that the internal - * malloc/realloc/free function pointers are not null. - *============================================================================*/ - -static void *null_malloc(void *ctx, usize size) { - return NULL; -} - -static void *null_realloc(void *ctx, void *ptr, usize old_size, usize size) { - return NULL; -} - -static void null_free(void *ctx, void *ptr) { - return; -} - -static const yyjson_alc YYJSON_NULL_ALC = { - null_malloc, - null_realloc, - null_free, - NULL -}; - - - -/*============================================================================== - * Pool Memory Allocator - * - * This allocator is initialized with a fixed-size buffer. - * The buffer is split into multiple memory chunks for memory allocation. - *============================================================================*/ - -/** memory chunk header */ -typedef struct pool_chunk { - usize size; /* chunk memory size, include chunk header */ - struct pool_chunk *next; /* linked list, nullable */ - /* char mem[]; flexible array member */ -} pool_chunk; - -/** allocator ctx header */ -typedef struct pool_ctx { - usize size; /* total memory size, include ctx header */ - pool_chunk *free_list; /* linked list, nullable */ - /* pool_chunk chunks[]; flexible array member */ -} pool_ctx; - -/** align up the input size to chunk size */ -static_inline void pool_size_align(usize *size) { - *size = size_align_up(*size, sizeof(pool_chunk)) + sizeof(pool_chunk); -} - -static void *pool_malloc(void *ctx_ptr, usize size) { - /* assert(size != 0) */ - pool_ctx *ctx = (pool_ctx *)ctx_ptr; - pool_chunk *next, *prev = NULL, *cur = ctx->free_list; - - if (unlikely(size >= ctx->size)) return NULL; - pool_size_align(&size); - - while (cur) { - if (cur->size < size) { - /* not enough space, try next chunk */ - prev = cur; - cur = cur->next; - continue; - } - if (cur->size >= size + sizeof(pool_chunk) * 2) { - /* too much space, split this chunk */ - next = (pool_chunk *)(void *)((u8 *)cur + size); - next->size = cur->size - size; - next->next = cur->next; - cur->size = size; - } else { - /* just enough space, use whole chunk */ - next = cur->next; - } - if (prev) prev->next = next; - else ctx->free_list = next; - return (void *)(cur + 1); - } - return NULL; -} - -static void pool_free(void *ctx_ptr, void *ptr) { - /* assert(ptr != NULL) */ - pool_ctx *ctx = (pool_ctx *)ctx_ptr; - pool_chunk *cur = ((pool_chunk *)ptr) - 1; - pool_chunk *prev = NULL, *next = ctx->free_list; - - while (next && next < cur) { - prev = next; - next = next->next; - } - if (prev) prev->next = cur; - else ctx->free_list = cur; - cur->next = next; - - if (next && ((u8 *)cur + cur->size) == (u8 *)next) { - /* merge cur to higher chunk */ - cur->size += next->size; - cur->next = next->next; - } - if (prev && ((u8 *)prev + prev->size) == (u8 *)cur) { - /* merge cur to lower chunk */ - prev->size += cur->size; - prev->next = cur->next; - } -} - -static void *pool_realloc(void *ctx_ptr, void *ptr, - usize old_size, usize size) { - /* assert(ptr != NULL && size != 0 && old_size < size) */ - pool_ctx *ctx = (pool_ctx *)ctx_ptr; - pool_chunk *cur = ((pool_chunk *)ptr) - 1, *prev, *next, *tmp; - - /* check size */ - if (unlikely(size >= ctx->size)) return NULL; - pool_size_align(&old_size); - pool_size_align(&size); - if (unlikely(old_size == size)) return ptr; - - /* find next and prev chunk */ - prev = NULL; - next = ctx->free_list; - while (next && next < cur) { - prev = next; - next = next->next; - } - - if ((u8 *)cur + cur->size == (u8 *)next && cur->size + next->size >= size) { - /* merge to higher chunk if they are contiguous */ - usize free_size = cur->size + next->size - size; - if (free_size > sizeof(pool_chunk) * 2) { - tmp = (pool_chunk *)(void *)((u8 *)cur + size); - if (prev) prev->next = tmp; - else ctx->free_list = tmp; - tmp->next = next->next; - tmp->size = free_size; - cur->size = size; - } else { - if (prev) prev->next = next->next; - else ctx->free_list = next->next; - cur->size += next->size; - } - return ptr; - } else { - /* fallback to malloc and memcpy */ - void *new_ptr = pool_malloc(ctx_ptr, size - sizeof(pool_chunk)); - if (new_ptr) { - memcpy(new_ptr, ptr, cur->size - sizeof(pool_chunk)); - pool_free(ctx_ptr, ptr); - } - return new_ptr; - } -} - -bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) { - pool_chunk *chunk; - pool_ctx *ctx; - - if (unlikely(!alc)) return false; - *alc = YYJSON_NULL_ALC; - if (size < sizeof(pool_ctx) * 4) return false; - ctx = (pool_ctx *)mem_align_up(buf, sizeof(pool_ctx)); - if (unlikely(!ctx)) return false; - size -= (usize)((u8 *)ctx - (u8 *)buf); - size = size_align_down(size, sizeof(pool_ctx)); - - chunk = (pool_chunk *)(ctx + 1); - chunk->size = size - sizeof(pool_ctx); - chunk->next = NULL; - ctx->size = size; - ctx->free_list = chunk; - - alc->malloc = pool_malloc; - alc->realloc = pool_realloc; - alc->free = pool_free; - alc->ctx = (void *)ctx; - return true; -} - - - -/*============================================================================== - * Dynamic Memory Allocator - * - * This allocator allocates memory on demand and does not immediately release - * unused memory. Instead, it places the unused memory into a freelist for - * potential reuse in the future. It is only when the entire allocator is - * destroyed that all previously allocated memory is released at once. - *============================================================================*/ - -/** memory chunk header */ -typedef struct dyn_chunk { - usize size; /* chunk size, include header */ - struct dyn_chunk *next; - /* char mem[]; flexible array member */ -} dyn_chunk; - -/** allocator ctx header */ -typedef struct { - dyn_chunk free_list; /* dummy header, sorted from small to large */ - dyn_chunk used_list; /* dummy header */ -} dyn_ctx; - -/** align up the input size to chunk size */ -static_inline bool dyn_size_align(usize *size) { - usize alc_size = *size + sizeof(dyn_chunk); - alc_size = size_align_up(alc_size, YYJSON_ALC_DYN_MIN_SIZE); - if (unlikely(alc_size < *size)) return false; /* overflow */ - *size = alc_size; - return true; -} - -/** remove a chunk from list (the chunk must already be in the list) */ -static_inline void dyn_chunk_list_remove(dyn_chunk *list, dyn_chunk *chunk) { - dyn_chunk *prev = list, *cur; - for (cur = prev->next; cur; cur = cur->next) { - if (cur == chunk) { - prev->next = cur->next; - cur->next = NULL; - return; - } - prev = cur; - } -} - -/** add a chunk to list header (the chunk must not be in the list) */ -static_inline void dyn_chunk_list_add(dyn_chunk *list, dyn_chunk *chunk) { - chunk->next = list->next; - list->next = chunk; -} - -static void *dyn_malloc(void *ctx_ptr, usize size) { - /* assert(size != 0) */ - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *chunk, *prev, *next; - if (unlikely(!dyn_size_align(&size))) return NULL; - - /* freelist is empty, create new chunk */ - if (!ctx->free_list.next) { - chunk = (dyn_chunk *)def.malloc(def.ctx, size); - if (unlikely(!chunk)) return NULL; - chunk->size = size; - chunk->next = NULL; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - - /* find a large enough chunk, or resize the largest chunk */ - prev = &ctx->free_list; - while (true) { - chunk = prev->next; - if (chunk->size >= size) { /* enough size, reuse this chunk */ - prev->next = chunk->next; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - if (!chunk->next) { /* resize the largest chunk */ - chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); - if (unlikely(!chunk)) return NULL; - prev->next = NULL; - chunk->size = size; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - prev = chunk; - } -} - -static void *dyn_realloc(void *ctx_ptr, void *ptr, - usize old_size, usize size) { - /* assert(ptr != NULL && size != 0 && old_size < size) */ - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *prev, *next, *new_chunk; - dyn_chunk *chunk = (dyn_chunk *)ptr - 1; - if (unlikely(!dyn_size_align(&size))) return NULL; - if (chunk->size >= size) return ptr; - - dyn_chunk_list_remove(&ctx->used_list, chunk); - new_chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); - if (likely(new_chunk)) { - new_chunk->size = size; - chunk = new_chunk; - } - dyn_chunk_list_add(&ctx->used_list, chunk); - return new_chunk ? (void *)(new_chunk + 1) : NULL; -} - -static void dyn_free(void *ctx_ptr, void *ptr) { - /* assert(ptr != NULL) */ - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *chunk = (dyn_chunk *)ptr - 1, *prev; - - dyn_chunk_list_remove(&ctx->used_list, chunk); - for (prev = &ctx->free_list; prev; prev = prev->next) { - if (!prev->next || prev->next->size >= chunk->size) { - chunk->next = prev->next; - prev->next = chunk; - break; - } - } -} - -yyjson_alc *yyjson_alc_dyn_new(void) { - const yyjson_alc def = YYJSON_DEFAULT_ALC; - usize hdr_len = sizeof(yyjson_alc) + sizeof(dyn_ctx); - yyjson_alc *alc = (yyjson_alc *)def.malloc(def.ctx, hdr_len); - dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); - if (unlikely(!alc)) return NULL; - alc->malloc = dyn_malloc; - alc->realloc = dyn_realloc; - alc->free = dyn_free; - alc->ctx = alc + 1; - memset(ctx, 0, sizeof(*ctx)); - return alc; -} - -void yyjson_alc_dyn_free(yyjson_alc *alc) { - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); - dyn_chunk *chunk, *next; - if (unlikely(!alc)) return; - for (chunk = ctx->free_list.next; chunk; chunk = next) { - next = chunk->next; - def.free(def.ctx, chunk); - } - for (chunk = ctx->used_list.next; chunk; chunk = next) { - next = chunk->next; - def.free(def.ctx, chunk); - } - def.free(def.ctx, alc); -} - - - -/*============================================================================== - * JSON document and value - *============================================================================*/ - -static_inline void unsafe_yyjson_str_pool_release(yyjson_str_pool *pool, - yyjson_alc *alc) { - yyjson_str_chunk *chunk = pool->chunks, *next; - while (chunk) { - next = chunk->next; - alc->free(alc->ctx, chunk); - chunk = next; - } -} - -static_inline void unsafe_yyjson_val_pool_release(yyjson_val_pool *pool, - yyjson_alc *alc) { - yyjson_val_chunk *chunk = pool->chunks, *next; - while (chunk) { - next = chunk->next; - alc->free(alc->ctx, chunk); - chunk = next; - } -} - -bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, - const yyjson_alc *alc, usize len) { - yyjson_str_chunk *chunk; - usize size, max_len; - - /* create a new chunk */ - max_len = USIZE_MAX - sizeof(yyjson_str_chunk); - if (unlikely(len > max_len)) return false; - size = len + sizeof(yyjson_str_chunk); - size = yyjson_max(pool->chunk_size, size); - chunk = (yyjson_str_chunk *)alc->malloc(alc->ctx, size); - if (unlikely(!chunk)) return false; - - /* insert the new chunk as the head of the linked list */ - chunk->next = pool->chunks; - chunk->chunk_size = size; - pool->chunks = chunk; - pool->cur = (char *)chunk + sizeof(yyjson_str_chunk); - pool->end = (char *)chunk + size; - - /* the next chunk is twice the size of the current one */ - size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); - if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ - pool->chunk_size = size; - return true; -} - -bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, - const yyjson_alc *alc, usize count) { - yyjson_val_chunk *chunk; - usize size, max_count; - - /* create a new chunk */ - max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; - if (unlikely(count > max_count)) return false; - size = (count + 1) * sizeof(yyjson_mut_val); - size = yyjson_max(pool->chunk_size, size); - chunk = (yyjson_val_chunk *)alc->malloc(alc->ctx, size); - if (unlikely(!chunk)) return false; - - /* insert the new chunk as the head of the linked list */ - chunk->next = pool->chunks; - chunk->chunk_size = size; - pool->chunks = chunk; - pool->cur = (yyjson_mut_val *)(void *)((u8 *)chunk) + 1; - pool->end = (yyjson_mut_val *)(void *)((u8 *)chunk + size); - - /* the next chunk is twice the size of the current one */ - size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); - if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ - pool->chunk_size = size; - return true; -} - -bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len) { - usize max_size = USIZE_MAX - sizeof(yyjson_str_chunk); - if (!doc || !len || len > max_size) return false; - doc->str_pool.chunk_size = len + sizeof(yyjson_str_chunk); - return true; -} - -bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count) { - usize max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; - if (!doc || !count || count > max_count) return false; - doc->val_pool.chunk_size = (count + 1) * sizeof(yyjson_mut_val); - return true; -} - -void yyjson_mut_doc_free(yyjson_mut_doc *doc) { - if (doc) { - yyjson_alc alc = doc->alc; - memset(&doc->alc, 0, sizeof(alc)); - unsafe_yyjson_str_pool_release(&doc->str_pool, &alc); - unsafe_yyjson_val_pool_release(&doc->val_pool, &alc); - alc.free(alc.ctx, doc); - } -} - -yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc) { - yyjson_mut_doc *doc; - if (!alc) alc = &YYJSON_DEFAULT_ALC; - doc = (yyjson_mut_doc *)alc->malloc(alc->ctx, sizeof(yyjson_mut_doc)); - if (!doc) return NULL; - memset(doc, 0, sizeof(yyjson_mut_doc)); - - doc->alc = *alc; - doc->str_pool.chunk_size = YYJSON_MUT_DOC_STR_POOL_INIT_SIZE; - doc->str_pool.chunk_size_max = YYJSON_MUT_DOC_STR_POOL_MAX_SIZE; - doc->val_pool.chunk_size = YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE; - doc->val_pool.chunk_size_max = YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE; - return doc; -} - -yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc) { - yyjson_mut_doc *m_doc; - yyjson_mut_val *m_val; - - if (!doc || !doc->root) return NULL; - m_doc = yyjson_mut_doc_new(alc); - if (!m_doc) return NULL; - m_val = yyjson_val_mut_copy(m_doc, doc->root); - if (!m_val) { - yyjson_mut_doc_free(m_doc); - return NULL; - } - yyjson_mut_doc_set_root(m_doc, m_val); - return m_doc; -} - -yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, - const yyjson_alc *alc) { - yyjson_mut_doc *m_doc; - yyjson_mut_val *m_val; - - if (!doc) return NULL; - if (!doc->root) return yyjson_mut_doc_new(alc); - - m_doc = yyjson_mut_doc_new(alc); - if (!m_doc) return NULL; - m_val = yyjson_mut_val_mut_copy(m_doc, doc->root); - if (!m_val) { - yyjson_mut_doc_free(m_doc); - return NULL; - } - yyjson_mut_doc_set_root(m_doc, m_val); - return m_doc; -} - -yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *m_doc, - yyjson_val *i_vals) { - /* - The immutable object or array stores all sub-values in a contiguous memory, - We copy them to another contiguous memory as mutable values, - then reconnect the mutable values with the original relationship. - */ - usize i_vals_len; - yyjson_mut_val *m_vals, *m_val; - yyjson_val *i_val, *i_end; - - if (!m_doc || !i_vals) return NULL; - i_end = unsafe_yyjson_get_next(i_vals); - i_vals_len = (usize)(unsafe_yyjson_get_next(i_vals) - i_vals); - m_vals = unsafe_yyjson_mut_val(m_doc, i_vals_len); - if (!m_vals) return NULL; - i_val = i_vals; - m_val = m_vals; - - for (; i_val < i_end; i_val++, m_val++) { - yyjson_type type = unsafe_yyjson_get_type(i_val); - m_val->tag = i_val->tag; - m_val->uni.u64 = i_val->uni.u64; - if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { - const char *str = i_val->uni.str; - usize str_len = unsafe_yyjson_get_len(i_val); - m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); - if (!m_val->uni.str) return NULL; - } else if (type == YYJSON_TYPE_ARR) { - usize len = unsafe_yyjson_get_len(i_val); - if (len > 0) { - yyjson_val *ii_val = i_val + 1, *ii_next; - yyjson_mut_val *mm_val = m_val + 1, *mm_ctn = m_val, *mm_next; - while (len-- > 1) { - ii_next = unsafe_yyjson_get_next(ii_val); - mm_next = mm_val + (ii_next - ii_val); - mm_val->next = mm_next; - ii_val = ii_next; - mm_val = mm_next; - } - mm_val->next = mm_ctn + 1; - mm_ctn->uni.ptr = mm_val; - } - } else if (type == YYJSON_TYPE_OBJ) { - usize len = unsafe_yyjson_get_len(i_val); - if (len > 0) { - yyjson_val *ii_key = i_val + 1, *ii_nextkey; - yyjson_mut_val *mm_key = m_val + 1, *mm_ctn = m_val; - yyjson_mut_val *mm_nextkey; - while (len-- > 1) { - ii_nextkey = unsafe_yyjson_get_next(ii_key + 1); - mm_nextkey = mm_key + (ii_nextkey - ii_key); - mm_key->next = mm_key + 1; - mm_key->next->next = mm_nextkey; - ii_key = ii_nextkey; - mm_key = mm_nextkey; - } - mm_key->next = mm_key + 1; - mm_key->next->next = mm_ctn + 1; - mm_ctn->uni.ptr = mm_key; - } - } - } - - return m_vals; -} - -static yyjson_mut_val *unsafe_yyjson_mut_val_mut_copy(yyjson_mut_doc *m_doc, - yyjson_mut_val *m_vals) { - /* - The mutable object or array stores all sub-values in a circular linked - list, so we can traverse them in the same loop. The traversal starts from - the last item, continues with the first item in a list, and ends with the - second to last item, which needs to be linked to the last item to close the - circle. - */ - yyjson_mut_val *m_val = unsafe_yyjson_mut_val(m_doc, 1); - if (unlikely(!m_val)) return NULL; - m_val->tag = m_vals->tag; - - switch (unsafe_yyjson_get_type(m_vals)) { - case YYJSON_TYPE_OBJ: - case YYJSON_TYPE_ARR: - if (unsafe_yyjson_get_len(m_vals) > 0) { - yyjson_mut_val *last = (yyjson_mut_val *)m_vals->uni.ptr; - yyjson_mut_val *next = last->next, *prev; - prev = unsafe_yyjson_mut_val_mut_copy(m_doc, last); - if (!prev) return NULL; - m_val->uni.ptr = (void *)prev; - while (next != last) { - prev->next = unsafe_yyjson_mut_val_mut_copy(m_doc, next); - if (!prev->next) return NULL; - prev = prev->next; - next = next->next; - } - prev->next = (yyjson_mut_val *)m_val->uni.ptr; - } - break; - - case YYJSON_TYPE_RAW: - case YYJSON_TYPE_STR: { - const char *str = m_vals->uni.str; - usize str_len = unsafe_yyjson_get_len(m_vals); - m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); - if (!m_val->uni.str) return NULL; - break; - } - - default: - m_val->uni = m_vals->uni; - break; - } - - return m_val; -} - -yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, - yyjson_mut_val *val) { - if (doc && val) return unsafe_yyjson_mut_val_mut_copy(doc, val); - return NULL; -} - -/* Count the number of values and the total length of the strings. */ -static void yyjson_mut_stat(yyjson_mut_val *val, - usize *val_sum, usize *str_sum) { - yyjson_type type = unsafe_yyjson_get_type(val); - *val_sum += 1; - if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { - yyjson_mut_val *child = (yyjson_mut_val *)val->uni.ptr; - usize len = unsafe_yyjson_get_len(val), i; - len <<= (u8)(type == YYJSON_TYPE_OBJ); - *val_sum += len; - for (i = 0; i < len; i++) { - yyjson_type stype = unsafe_yyjson_get_type(child); - if (stype == YYJSON_TYPE_STR || stype == YYJSON_TYPE_RAW) { - *str_sum += unsafe_yyjson_get_len(child) + 1; - } else if (stype == YYJSON_TYPE_ARR || stype == YYJSON_TYPE_OBJ) { - yyjson_mut_stat(child, val_sum, str_sum); - *val_sum -= 1; - } - child = child->next; - } - } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { - *str_sum += unsafe_yyjson_get_len(val) + 1; - } -} - -/* Copy mutable values to immutable value pool. */ -static usize yyjson_imut_copy(yyjson_val **val_ptr, char **buf_ptr, - yyjson_mut_val *mval) { - yyjson_val *val = *val_ptr; - yyjson_type type = unsafe_yyjson_get_type(mval); - if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { - yyjson_mut_val *child = (yyjson_mut_val *)mval->uni.ptr; - usize len = unsafe_yyjson_get_len(mval), i; - usize val_sum = 1; - if (type == YYJSON_TYPE_OBJ) { - if (len) child = child->next->next; - len <<= 1; - } else { - if (len) child = child->next; - } - *val_ptr = val + 1; - for (i = 0; i < len; i++) { - val_sum += yyjson_imut_copy(val_ptr, buf_ptr, child); - child = child->next; - } - val->tag = mval->tag; - val->uni.ofs = val_sum * sizeof(yyjson_val); - return val_sum; - } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { - char *buf = *buf_ptr; - usize len = unsafe_yyjson_get_len(mval); - memcpy((void *)buf, (const void *)mval->uni.str, len); - buf[len] = '\0'; - val->tag = mval->tag; - val->uni.str = buf; - *val_ptr = val + 1; - *buf_ptr = buf + len + 1; - return 1; - } else { - val->tag = mval->tag; - val->uni = mval->uni; - *val_ptr = val + 1; - return 1; - } -} - -yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *mdoc, - const yyjson_alc *alc) { - if (!mdoc) return NULL; - return yyjson_mut_val_imut_copy(mdoc->root, alc); -} - -yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval, - const yyjson_alc *alc) { - usize val_num = 0, str_sum = 0, hdr_size, buf_size; - yyjson_doc *doc = NULL; - yyjson_val *val_hdr = NULL; - - /* This value should be NULL here. Setting a non-null value suppresses - warning from the clang analyzer. */ - char *str_hdr = (char *)(void *)&str_sum; - if (!mval) return NULL; - if (!alc) alc = &YYJSON_DEFAULT_ALC; - - /* traverse the input value to get pool size */ - yyjson_mut_stat(mval, &val_num, &str_sum); - - /* create doc and val pool */ - hdr_size = size_align_up(sizeof(yyjson_doc), sizeof(yyjson_val)); - buf_size = hdr_size + val_num * sizeof(yyjson_val); - doc = (yyjson_doc *)alc->malloc(alc->ctx, buf_size); - if (!doc) return NULL; - memset(doc, 0, sizeof(yyjson_doc)); - val_hdr = (yyjson_val *)(void *)((char *)(void *)doc + hdr_size); - doc->root = val_hdr; - doc->alc = *alc; - - /* create str pool */ - if (str_sum > 0) { - str_hdr = (char *)alc->malloc(alc->ctx, str_sum); - doc->str_pool = str_hdr; - if (!str_hdr) { - alc->free(alc->ctx, (void *)doc); - return NULL; - } - } - - /* copy vals and strs */ - doc->val_read = yyjson_imut_copy(&val_hdr, &str_hdr, mval); - doc->dat_read = str_sum + 1; - return doc; -} - -static_inline bool unsafe_yyjson_num_equals(void *lhs, void *rhs) { - yyjson_val_uni *luni = &((yyjson_val *)lhs)->uni; - yyjson_val_uni *runi = &((yyjson_val *)rhs)->uni; - yyjson_subtype lt = unsafe_yyjson_get_subtype(lhs); - yyjson_subtype rt = unsafe_yyjson_get_subtype(rhs); - if (lt == rt) return luni->u64 == runi->u64; - if (lt == YYJSON_SUBTYPE_SINT && rt == YYJSON_SUBTYPE_UINT) { - return luni->i64 >= 0 && luni->u64 == runi->u64; - } - if (lt == YYJSON_SUBTYPE_UINT && rt == YYJSON_SUBTYPE_SINT) { - return runi->i64 >= 0 && luni->u64 == runi->u64; - } - return false; -} - -static_inline bool unsafe_yyjson_str_equals(void *lhs, void *rhs) { - usize len = unsafe_yyjson_get_len(lhs); - if (len != unsafe_yyjson_get_len(rhs)) return false; - return !memcmp(unsafe_yyjson_get_str(lhs), - unsafe_yyjson_get_str(rhs), len); -} - -bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { - yyjson_type type = unsafe_yyjson_get_type(lhs); - if (type != unsafe_yyjson_get_type(rhs)) return false; - - switch (type) { - case YYJSON_TYPE_OBJ: { - usize len = unsafe_yyjson_get_len(lhs); - if (len != unsafe_yyjson_get_len(rhs)) return false; - if (len > 0) { - yyjson_obj_iter iter; - yyjson_obj_iter_init(rhs, &iter); - lhs = unsafe_yyjson_get_first(lhs); - while (len-- > 0) { - rhs = yyjson_obj_iter_getn(&iter, lhs->uni.str, - unsafe_yyjson_get_len(lhs)); - if (!rhs) return false; - if (!unsafe_yyjson_equals(lhs + 1, rhs)) return false; - lhs = unsafe_yyjson_get_next(lhs + 1); - } - } - /* yyjson allows duplicate keys, so the check may be inaccurate */ - return true; - } - - case YYJSON_TYPE_ARR: { - usize len = unsafe_yyjson_get_len(lhs); - if (len != unsafe_yyjson_get_len(rhs)) return false; - if (len > 0) { - lhs = unsafe_yyjson_get_first(lhs); - rhs = unsafe_yyjson_get_first(rhs); - while (len-- > 0) { - if (!unsafe_yyjson_equals(lhs, rhs)) return false; - lhs = unsafe_yyjson_get_next(lhs); - rhs = unsafe_yyjson_get_next(rhs); - } - } - return true; - } - - case YYJSON_TYPE_NUM: - return unsafe_yyjson_num_equals(lhs, rhs); - - case YYJSON_TYPE_RAW: - case YYJSON_TYPE_STR: - return unsafe_yyjson_str_equals(lhs, rhs); - - case YYJSON_TYPE_NULL: - case YYJSON_TYPE_BOOL: - return lhs->tag == rhs->tag; - - default: - return false; - } -} - -bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs) { - yyjson_type type = unsafe_yyjson_get_type(lhs); - if (type != unsafe_yyjson_get_type(rhs)) return false; - - switch (type) { - case YYJSON_TYPE_OBJ: { - usize len = unsafe_yyjson_get_len(lhs); - if (len != unsafe_yyjson_get_len(rhs)) return false; - if (len > 0) { - yyjson_mut_obj_iter iter; - yyjson_mut_obj_iter_init(rhs, &iter); - lhs = (yyjson_mut_val *)lhs->uni.ptr; - while (len-- > 0) { - rhs = yyjson_mut_obj_iter_getn(&iter, lhs->uni.str, - unsafe_yyjson_get_len(lhs)); - if (!rhs) return false; - if (!unsafe_yyjson_mut_equals(lhs->next, rhs)) return false; - lhs = lhs->next->next; - } - } - /* yyjson allows duplicate keys, so the check may be inaccurate */ - return true; - } - - case YYJSON_TYPE_ARR: { - usize len = unsafe_yyjson_get_len(lhs); - if (len != unsafe_yyjson_get_len(rhs)) return false; - if (len > 0) { - lhs = (yyjson_mut_val *)lhs->uni.ptr; - rhs = (yyjson_mut_val *)rhs->uni.ptr; - while (len-- > 0) { - if (!unsafe_yyjson_mut_equals(lhs, rhs)) return false; - lhs = lhs->next; - rhs = rhs->next; - } - } - return true; - } - - case YYJSON_TYPE_NUM: - return unsafe_yyjson_num_equals(lhs, rhs); - - case YYJSON_TYPE_RAW: - case YYJSON_TYPE_STR: - return unsafe_yyjson_str_equals(lhs, rhs); - - case YYJSON_TYPE_NULL: - case YYJSON_TYPE_BOOL: - return lhs->tag == rhs->tag; - - default: - return false; - } -} - - - -#if !YYJSON_DISABLE_UTILS - -/*============================================================================== - * JSON Pointer API (RFC 6901) - *============================================================================*/ - -/** - Get a token from JSON pointer string. - @param ptr [in,out] - in: string that points to current token prefix `/` - out: string that points to next token prefix `/`, or string end - @param end [in] end of the entire JSON Pointer string - @param len [out] unescaped token length - @param esc [out] number of escaped characters in this token - @return head of the token, or NULL if syntax error - */ -static_inline const char *ptr_next_token(const char **ptr, const char *end, - usize *len, usize *esc) { - const char *hdr = *ptr + 1; - const char *cur = hdr; - /* skip unescaped characters */ - while (cur < end && *cur != '/' && *cur != '~') cur++; - if (likely(cur == end || *cur != '~')) { - /* no escaped characters, return */ - *ptr = cur; - *len = (usize)(cur - hdr); - *esc = 0; - return hdr; - } else { - /* handle escaped characters */ - usize esc_num = 0; - while (cur < end && *cur != '/') { - if (*cur++ == '~') { - if (cur == end || (*cur != '0' && *cur != '1')) { - *ptr = cur - 1; - return NULL; - } - esc_num++; - } - } - *ptr = cur; - *len = (usize)(cur - hdr) - esc_num; - *esc = esc_num; - return hdr; - } -} - -/** - Convert token string to index. - @param cur [in] token head - @param len [in] token length - @param idx [out] the index number, or USIZE_MAX if token is '-' - @return true if token is a valid array index - */ -static_inline bool ptr_token_to_idx(const char *cur, usize len, usize *idx) { - const char *end = cur + len; - usize num = 0, add; - if (unlikely(len == 0 || len > USIZE_SAFE_DIG)) return false; - if (*cur == '0') { - if (unlikely(len > 1)) return false; - *idx = 0; - return true; - } - if (*cur == '-') { - if (unlikely(len > 1)) return false; - *idx = USIZE_MAX; - return true; - } - for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) { - num = num * 10 + add; - } - if (unlikely(num == 0 || cur < end)) return false; - *idx = num; - return true; -} - -/** - Compare JSON key with token. - @param key a string key (yyjson_val or yyjson_mut_val) - @param token a JSON pointer token - @param len unescaped token length - @param esc number of escaped characters in this token - @return true if `str` is equals to `token` - */ -static_inline bool ptr_token_eq(void *key, - const char *token, usize len, usize esc) { - yyjson_val *val = (yyjson_val *)key; - if (unsafe_yyjson_get_len(val) != len) return false; - if (likely(!esc)) { - return memcmp(val->uni.str, token, len) == 0; - } else { - const char *str = val->uni.str; - for (; len-- > 0; token++, str++) { - if (*token == '~') { - if (*str != (*++token == '0' ? '~' : '/')) return false; - } else { - if (*str != *token) return false; - } - } - return true; - } -} - -/** - Get a value from array by token. - @param arr an array, should not be NULL or non-array type - @param token a JSON pointer token - @param len unescaped token length - @param esc number of escaped characters in this token - @return value at index, or NULL if token is not index or index is out of range - */ -static_inline yyjson_val *ptr_arr_get(yyjson_val *arr, const char *token, - usize len, usize esc) { - yyjson_val *val = unsafe_yyjson_get_first(arr); - usize num = unsafe_yyjson_get_len(arr), idx = 0; - if (unlikely(num == 0)) return NULL; - if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; - if (unlikely(idx >= num)) return NULL; - if (unsafe_yyjson_arr_is_flat(arr)) { - return val + idx; - } else { - while (idx-- > 0) val = unsafe_yyjson_get_next(val); - return val; - } -} - -/** - Get a value from object by token. - @param obj [in] an object, should not be NULL or non-object type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @return value associated with the token, or NULL if no value - */ -static_inline yyjson_val *ptr_obj_get(yyjson_val *obj, const char *token, - usize len, usize esc) { - yyjson_val *key = unsafe_yyjson_get_first(obj); - usize num = unsafe_yyjson_get_len(obj); - if (unlikely(num == 0)) return NULL; - for (; num > 0; num--, key = unsafe_yyjson_get_next(key + 1)) { - if (ptr_token_eq(key, token, len, esc)) return key + 1; - } - return NULL; -} - -/** - Get a value from array by token. - @param arr [in] an array, should not be NULL or non-array type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param pre [out] previous (sibling) value of the returned value - @param last [out] whether index is last - @return value at index, or NULL if token is not index or index is out of range - */ -static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr, - const char *token, - usize len, usize esc, - yyjson_mut_val **pre, - bool *last) { - yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */ - usize num = unsafe_yyjson_get_len(arr), idx; - if (last) *last = false; - if (false) *pre = NULL; - if (unlikely(num == 0)) { - if (last && len == 1 && (*token == '0' || *token == '-')) *last = true; - return NULL; - } - if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; - if (last) *last = (idx == num || idx == USIZE_MAX); - if (unlikely(idx >= num)) return NULL; - while (idx-- > 0) val = val->next; - *pre = val; - return val->next; -} - -/** - Get a value from object by token. - @param obj [in] an object, should not be NULL or non-object type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param pre [out] previous (sibling) key of the returned value's key - @return value associated with the token, or NULL if no value - */ -static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj, - const char *token, - usize len, usize esc, - yyjson_mut_val **pre) { - yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key; - usize num = unsafe_yyjson_get_len(obj); - if (false) *pre = NULL; - if (unlikely(num == 0)) return NULL; - for (; num > 0; num--, pre_key = key) { - key = pre_key->next->next; - if (ptr_token_eq(key, token, len, esc)) { - *pre = pre_key; - return key->next; - } - } - return NULL; -} - -/** - Create a string value with JSON pointer token. - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param doc [in] used for memory allocation when creating value - @return new string value, or NULL if memory allocation failed - */ -static_inline yyjson_mut_val *ptr_new_key(const char *token, - usize len, usize esc, - yyjson_mut_doc *doc) { - const char *src = token; - if (likely(!esc)) { - return yyjson_mut_strncpy(doc, src, len); - } else { - const char *end = src + len + esc; - char *dst = unsafe_yyjson_mut_str_alc(doc, len + esc); - char *str = dst; - if (unlikely(!dst)) return NULL; - for (; src < end; src++, dst++) { - if (*src != '~') *dst = *src; - else *dst = (*++src == '0' ? '~' : '/'); - } - *dst = '\0'; - return yyjson_mut_strn(doc, str, len); - } -} - -/* macros for yyjson_ptr */ -#define return_err(_ret, _code, _pos, _msg) do { \ - if (err) { \ - err->code = YYJSON_PTR_ERR_##_code; \ - err->msg = _msg; \ - err->pos = (usize)(_pos); \ - } \ - return _ret; \ -} while (false) - -#define return_err_resolve(_ret, _pos) \ - return_err(_ret, RESOLVE, _pos, "JSON pointer cannot be resolved") -#define return_err_syntax(_ret, _pos) \ - return_err(_ret, SYNTAX, _pos, "invalid escaped character") -#define return_err_alloc(_ret) \ - return_err(_ret, MEMORY_ALLOCATION, 0, "failed to create value") - -yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, - const char *ptr, size_t ptr_len, - yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize len, esc; - yyjson_type type; - - while (true) { - token = ptr_next_token(&ptr, end, &len, &esc); - if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); - type = unsafe_yyjson_get_type(val); - if (type == YYJSON_TYPE_OBJ) { - val = ptr_obj_get(val, token, len, esc); - } else if (type == YYJSON_TYPE_ARR) { - val = ptr_arr_get(val, token, len, esc); - } else { - val = NULL; - } - if (!val) return_err_resolve(NULL, token - hdr); - if (ptr == end) return val; - } -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, - const char *ptr, - size_t ptr_len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize len, esc; - yyjson_mut_val *ctn, *pre = NULL; - yyjson_type type; - bool idx_is_last = false; - - while (true) { - token = ptr_next_token(&ptr, end, &len, &esc); - if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); - ctn = val; - type = unsafe_yyjson_get_type(val); - if (type == YYJSON_TYPE_OBJ) { - val = ptr_mut_obj_get(val, token, len, esc, &pre); - } else if (type == YYJSON_TYPE_ARR) { - val = ptr_mut_arr_get(val, token, len, esc, &pre, &idx_is_last); - } else { - val = NULL; - } - if (ctx && (ptr == end)) { - if (type == YYJSON_TYPE_OBJ || - (type == YYJSON_TYPE_ARR && (val || idx_is_last))) { - ctx->ctn = ctn; - ctx->pre = pre; - } - } - if (!val) return_err_resolve(NULL, token - hdr); - if (ptr == end) return val; - } -} - -bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, - const char *ptr, size_t ptr_len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, bool insert_new, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize token_len, esc, ctn_len; - yyjson_mut_val *ctn, *key, *pre = NULL; - yyjson_mut_val *sep_ctn = NULL, *sep_key = NULL, *sep_val = NULL; - yyjson_type ctn_type; - bool idx_is_last = false; - - /* skip exist parent nodes */ - while (true) { - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_syntax(false, ptr - hdr); - ctn = val; - ctn_type = unsafe_yyjson_get_type(ctn); - if (ctn_type == YYJSON_TYPE_OBJ) { - val = ptr_mut_obj_get(ctn, token, token_len, esc, &pre); - } else if (ctn_type == YYJSON_TYPE_ARR) { - val = ptr_mut_arr_get(ctn, token, token_len, esc, &pre, - &idx_is_last); - } else return_err_resolve(false, token - hdr); - if (!val) break; - if (ptr == end) break; /* is last token */ - } - - /* create parent nodes if not exist */ - if (unlikely(ptr != end)) { /* not last token */ - if (!create_parent) return_err_resolve(false, token - hdr); - - /* add value at last index if container is array */ - if (ctn_type == YYJSON_TYPE_ARR) { - if (!idx_is_last || !insert_new) { - return_err_resolve(false, token - hdr); - } - val = yyjson_mut_obj(doc); - if (!val) return_err_alloc(false); - - /* delay attaching until all operations are completed */ - sep_ctn = ctn; - sep_key = NULL; - sep_val = val; - - /* move to next token */ - ctn = val; - val = NULL; - ctn_type = YYJSON_TYPE_OBJ; - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_resolve(false, token - hdr); - } - - /* container is object, create parent nodes */ - while (ptr != end) { /* not last token */ - key = ptr_new_key(token, token_len, esc, doc); - if (!key) return_err_alloc(false); - val = yyjson_mut_obj(doc); - if (!val) return_err_alloc(false); - - /* delay attaching until all operations are completed */ - if (!sep_ctn) { - sep_ctn = ctn; - sep_key = key; - sep_val = val; - } else { - yyjson_mut_obj_add(ctn, key, val); - } - - /* move to next token */ - ctn = val; - val = NULL; - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_syntax(false, ptr - hdr); - } - } - - /* JSON pointer is resolved, insert or replace target value */ - ctn_len = unsafe_yyjson_get_len(ctn); - if (ctn_type == YYJSON_TYPE_OBJ) { - if (ctx) ctx->ctn = ctn; - if (!val || insert_new) { - /* insert new key-value pair */ - key = ptr_new_key(token, token_len, esc, doc); - if (unlikely(!key)) return_err_alloc(false); - if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : key; - unsafe_yyjson_mut_obj_add(ctn, key, new_val, ctn_len); - } else { - /* replace exist value */ - key = pre->next->next; - if (ctx) ctx->pre = pre; - if (ctx) ctx->old = val; - yyjson_mut_obj_put(ctn, key, new_val); - } - } else { - /* array */ - if (ctx && (val || idx_is_last)) ctx->ctn = ctn; - if (insert_new) { - /* append new value */ - if (val) { - pre->next = new_val; - new_val->next = val; - if (ctx) ctx->pre = pre; - unsafe_yyjson_set_len(ctn, ctn_len + 1); - } else if (idx_is_last) { - if (ctx) ctx->pre = ctn_len ? - (yyjson_mut_val *)ctn->uni.ptr : new_val; - yyjson_mut_arr_append(ctn, new_val); - } else { - return_err_resolve(false, token - hdr); - } - } else { - /* replace exist value */ - if (!val) return_err_resolve(false, token - hdr); - if (ctn_len > 1) { - new_val->next = val->next; - pre->next = new_val; - if (ctn->uni.ptr == val) ctn->uni.ptr = new_val; - } else { - new_val->next = new_val; - ctn->uni.ptr = new_val; - pre = new_val; - } - if (ctx) ctx->pre = pre; - if (ctx) ctx->old = val; - } - } - - /* all operations are completed, attach the new components to the target */ - if (unlikely(sep_ctn)) { - if (sep_key) yyjson_mut_obj_add(sep_ctn, sep_key, sep_val); - else yyjson_mut_arr_append(sep_ctn, sep_val); - } - return true; -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_mut_val *cur_val; - yyjson_ptr_ctx cur_ctx; - memset(&cur_ctx, 0, sizeof(cur_ctx)); - if (!ctx) ctx = &cur_ctx; - cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); - if (!cur_val) return NULL; - - if (yyjson_mut_is_obj(ctx->ctn)) { - yyjson_mut_val *key = ctx->pre->next->next; - yyjson_mut_obj_put(ctx->ctn, key, new_val); - } else { - yyjson_ptr_ctx_replace(ctx, new_val); - } - ctx->old = cur_val; - return cur_val; -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_mut_val *cur_val; - yyjson_ptr_ctx cur_ctx; - memset(&cur_ctx, 0, sizeof(cur_ctx)); - if (!ctx) ctx = &cur_ctx; - cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); - if (cur_val) { - if (yyjson_mut_is_obj(ctx->ctn)) { - yyjson_mut_val *key = ctx->pre->next->next; - yyjson_mut_obj_put(ctx->ctn, key, NULL); - } else { - yyjson_ptr_ctx_remove(ctx); - } - ctx->pre = NULL; - ctx->old = cur_val; - } - return cur_val; -} - -/* macros for yyjson_ptr */ -#undef return_err -#undef return_err_resolve -#undef return_err_syntax -#undef return_err_alloc - - - -/*============================================================================== - * JSON Patch API (RFC 6902) - *============================================================================*/ - -/* JSON Patch operation */ -typedef enum patch_op { - PATCH_OP_ADD, /* path, value */ - PATCH_OP_REMOVE, /* path */ - PATCH_OP_REPLACE, /* path, value */ - PATCH_OP_MOVE, /* from, path */ - PATCH_OP_COPY, /* from, path */ - PATCH_OP_TEST, /* path, value */ - PATCH_OP_NONE /* invalid */ -} patch_op; - -static patch_op patch_op_get(yyjson_val *op) { - const char *str = op->uni.str; - switch (unsafe_yyjson_get_len(op)) { - case 3: - if (!memcmp(str, "add", 3)) return PATCH_OP_ADD; - return PATCH_OP_NONE; - case 4: - if (!memcmp(str, "move", 4)) return PATCH_OP_MOVE; - if (!memcmp(str, "copy", 4)) return PATCH_OP_COPY; - if (!memcmp(str, "test", 4)) return PATCH_OP_TEST; - return PATCH_OP_NONE; - case 6: - if (!memcmp(str, "remove", 6)) return PATCH_OP_REMOVE; - return PATCH_OP_NONE; - case 7: - if (!memcmp(str, "replace", 7)) return PATCH_OP_REPLACE; - return PATCH_OP_NONE; - default: - return PATCH_OP_NONE; - } -} - -/* macros for yyjson_patch */ -#define return_err(_code, _msg) do { \ - if (err->ptr.code == YYJSON_PTR_ERR_MEMORY_ALLOCATION) { \ - err->code = YYJSON_PATCH_ERROR_MEMORY_ALLOCATION; \ - err->msg = _msg; \ - memset(&err->ptr, 0, sizeof(yyjson_ptr_err)); \ - } else { \ - err->code = YYJSON_PATCH_ERROR_##_code; \ - err->msg = _msg; \ - err->idx = iter.idx ? iter.idx - 1 : 0; \ - } \ - return NULL; \ -} while (false) - -#define return_err_copy() \ - return_err(MEMORY_ALLOCATION, "failed to copy value") -#define return_err_key(_key) \ - return_err(MISSING_KEY, "missing key " _key) -#define return_err_val(_key) \ - return_err(INVALID_MEMBER, "invalid member " _key) - -#define ptr_get(_ptr) yyjson_mut_ptr_getx( \ - root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) -#define ptr_add(_ptr, _val) yyjson_mut_ptr_addx( \ - root, _ptr->uni.str, _ptr##_len, _val, doc, false, NULL, &err->ptr) -#define ptr_remove(_ptr) yyjson_mut_ptr_removex( \ - root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) -#define ptr_replace(_ptr, _val)yyjson_mut_ptr_replacex( \ - root, _ptr->uni.str, _ptr##_len, _val, NULL, &err->ptr) - -yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, - yyjson_val *orig, - yyjson_val *patch, - yyjson_patch_err *err) { - - yyjson_mut_val *root; - yyjson_val *obj; - yyjson_arr_iter iter; - yyjson_patch_err err_tmp; - if (!err) err = &err_tmp; - memset(err, 0, sizeof(*err)); - memset(&iter, 0, sizeof(iter)); - - if (unlikely(!doc || !orig || !patch)) { - return_err(INVALID_PARAMETER, "input parameter is NULL"); - } - if (unlikely(!yyjson_is_arr(patch))) { - return_err(INVALID_PARAMETER, "input patch is not array"); - } - root = yyjson_val_mut_copy(doc, orig); - if (unlikely(!root)) return_err_copy(); - - /* iterate through the patch array */ - yyjson_arr_iter_init(patch, &iter); - while ((obj = yyjson_arr_iter_next(&iter))) { - patch_op op_enum; - yyjson_val *op, *path, *from = NULL, *value; - yyjson_mut_val *val = NULL, *test; - usize path_len, from_len = 0; - if (unlikely(!unsafe_yyjson_is_obj(obj))) { - return_err(INVALID_OPERATION, "JSON patch operation is not object"); - } - - /* get required member: op */ - op = yyjson_obj_get(obj, "op"); - if (unlikely(!op)) return_err_key("`op`"); - if (unlikely(!yyjson_is_str(op))) return_err_val("`op`"); - op_enum = patch_op_get(op); - - /* get required member: path */ - path = yyjson_obj_get(obj, "path"); - if (unlikely(!path)) return_err_key("`path`"); - if (unlikely(!yyjson_is_str(path))) return_err_val("`path`"); - path_len = unsafe_yyjson_get_len(path); - - /* get required member: value, from */ - switch ((int)op_enum) { - case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: - value = yyjson_obj_get(obj, "value"); - if (unlikely(!value)) return_err_key("`value`"); - val = yyjson_val_mut_copy(doc, value); - if (unlikely(!val)) return_err_copy(); - break; - case PATCH_OP_MOVE: case PATCH_OP_COPY: - from = yyjson_obj_get(obj, "from"); - if (unlikely(!from)) return_err_key("`from`"); - if (unlikely(!yyjson_is_str(from))) return_err_val("`from`"); - from_len = unsafe_yyjson_get_len(from); - break; - default: - break; - } - - /* perform an operation */ - switch ((int)op_enum) { - case PATCH_OP_ADD: /* add(path, val) */ - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_REMOVE: /* remove(path) */ - if (unlikely(!ptr_remove(path))) { - return_err(POINTER, "failed to remove `path`"); - } - break; - case PATCH_OP_REPLACE: /* replace(path, val) */ - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_replace(path, val))) { - return_err(POINTER, "failed to replace `path`"); - } - break; - case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ - if (unlikely(from_len == 0 && path_len == 0)) break; - val = ptr_remove(from); - if (unlikely(!val)) { - return_err(POINTER, "failed to remove `from`"); - } - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ - val = ptr_get(from); - if (unlikely(!val)) { - return_err(POINTER, "failed to get `from`"); - } - if (unlikely(path_len == 0)) { root = val; break; } - val = yyjson_mut_val_mut_copy(doc, val); - if (unlikely(!val)) return_err_copy(); - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ - test = ptr_get(path); - if (unlikely(!test)) { - return_err(POINTER, "failed to get `path`"); - } - if (unlikely(!yyjson_mut_equals(val, test))) { - return_err(EQUAL, "failed to test equal"); - } - break; - default: - return_err(INVALID_MEMBER, "unsupported `op`"); - } - } - return root; -} - -yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, - yyjson_mut_val *orig, - yyjson_mut_val *patch, - yyjson_patch_err *err) { - yyjson_mut_val *root, *obj; - yyjson_mut_arr_iter iter; - yyjson_patch_err err_tmp; - if (!err) err = &err_tmp; - memset(err, 0, sizeof(*err)); - memset(&iter, 0, sizeof(iter)); - - if (unlikely(!doc || !orig || !patch)) { - return_err(INVALID_PARAMETER, "input parameter is NULL"); - } - if (unlikely(!yyjson_mut_is_arr(patch))) { - return_err(INVALID_PARAMETER, "input patch is not array"); - } - root = yyjson_mut_val_mut_copy(doc, orig); - if (unlikely(!root)) return_err_copy(); - - /* iterate through the patch array */ - yyjson_mut_arr_iter_init(patch, &iter); - while ((obj = yyjson_mut_arr_iter_next(&iter))) { - patch_op op_enum; - yyjson_mut_val *op, *path, *from = NULL, *value; - yyjson_mut_val *val = NULL, *test; - usize path_len, from_len = 0; - if (!unsafe_yyjson_is_obj(obj)) { - return_err(INVALID_OPERATION, "JSON patch operation is not object"); - } - - /* get required member: op */ - op = yyjson_mut_obj_get(obj, "op"); - if (unlikely(!op)) return_err_key("`op`"); - if (unlikely(!yyjson_mut_is_str(op))) return_err_val("`op`"); - op_enum = patch_op_get((yyjson_val *)(void *)op); - - /* get required member: path */ - path = yyjson_mut_obj_get(obj, "path"); - if (unlikely(!path)) return_err_key("`path`"); - if (unlikely(!yyjson_mut_is_str(path))) return_err_val("`path`"); - path_len = unsafe_yyjson_get_len(path); - - /* get required member: value, from */ - switch ((int)op_enum) { - case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: - value = yyjson_mut_obj_get(obj, "value"); - if (unlikely(!value)) return_err_key("`value`"); - val = yyjson_mut_val_mut_copy(doc, value); - if (unlikely(!val)) return_err_copy(); - break; - case PATCH_OP_MOVE: case PATCH_OP_COPY: - from = yyjson_mut_obj_get(obj, "from"); - if (unlikely(!from)) return_err_key("`from`"); - if (unlikely(!yyjson_mut_is_str(from))) { - return_err_val("`from`"); - } - from_len = unsafe_yyjson_get_len(from); - break; - default: - break; - } - - /* perform an operation */ - switch ((int)op_enum) { - case PATCH_OP_ADD: /* add(path, val) */ - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_REMOVE: /* remove(path) */ - if (unlikely(!ptr_remove(path))) { - return_err(POINTER, "failed to remove `path`"); - } - break; - case PATCH_OP_REPLACE: /* replace(path, val) */ - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_replace(path, val))) { - return_err(POINTER, "failed to replace `path`"); - } - break; - case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ - if (unlikely(from_len == 0 && path_len == 0)) break; - val = ptr_remove(from); - if (unlikely(!val)) { - return_err(POINTER, "failed to remove `from`"); - } - if (unlikely(path_len == 0)) { root = val; break; } - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ - val = ptr_get(from); - if (unlikely(!val)) { - return_err(POINTER, "failed to get `from`"); - } - if (unlikely(path_len == 0)) { root = val; break; } - val = yyjson_mut_val_mut_copy(doc, val); - if (unlikely(!val)) return_err_copy(); - if (unlikely(!ptr_add(path, val))) { - return_err(POINTER, "failed to add `path`"); - } - break; - case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ - test = ptr_get(path); - if (unlikely(!test)) { - return_err(POINTER, "failed to get `path`"); - } - if (unlikely(!yyjson_mut_equals(val, test))) { - return_err(EQUAL, "failed to test equal"); - } - break; - default: - return_err(INVALID_MEMBER, "unsupported `op`"); - } - } - return root; -} - -/* macros for yyjson_patch */ -#undef return_err -#undef return_err_copy -#undef return_err_key -#undef return_err_val -#undef ptr_get -#undef ptr_add -#undef ptr_remove -#undef ptr_replace - - - -/*============================================================================== - * JSON Merge-Patch API (RFC 7386) - *============================================================================*/ - -yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, - yyjson_val *orig, - yyjson_val *patch) { - usize idx, max; - yyjson_val *key, *orig_val, *patch_val, local_orig; - yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; - - if (unlikely(!yyjson_is_obj(patch))) { - return yyjson_val_mut_copy(doc, patch); - } - - builder = yyjson_mut_obj(doc); - if (unlikely(!builder)) return NULL; - - memset(&local_orig, 0, sizeof(local_orig)); - if (!yyjson_is_obj(orig)) { - orig = &local_orig; - orig->tag = builder->tag; - orig->uni = builder->uni; - } - - /* If orig is contributing, copy any items not modified by the patch */ - if (orig != &local_orig) { - yyjson_obj_foreach(orig, idx, max, key, orig_val) { - patch_val = yyjson_obj_getn(patch, - unsafe_yyjson_get_str(key), - unsafe_yyjson_get_len(key)); - if (!patch_val) { - mut_key = yyjson_val_mut_copy(doc, key); - mut_val = yyjson_val_mut_copy(doc, orig_val); - if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; - } - } - } - - /* Merge items modified by the patch. */ - yyjson_obj_foreach(patch, idx, max, key, patch_val) { - /* null indicates the field is removed. */ - if (unsafe_yyjson_is_null(patch_val)) { - continue; - } - mut_key = yyjson_val_mut_copy(doc, key); - orig_val = yyjson_obj_getn(orig, - unsafe_yyjson_get_str(key), - unsafe_yyjson_get_len(key)); - merged_val = yyjson_merge_patch(doc, orig_val, patch_val); - if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; - } - - return builder; -} - -yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, - yyjson_mut_val *orig, - yyjson_mut_val *patch) { - usize idx, max; - yyjson_mut_val *key, *orig_val, *patch_val, local_orig; - yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; - - if (unlikely(!yyjson_mut_is_obj(patch))) { - return yyjson_mut_val_mut_copy(doc, patch); - } - - builder = yyjson_mut_obj(doc); - if (unlikely(!builder)) return NULL; - - memset(&local_orig, 0, sizeof(local_orig)); - if (!yyjson_mut_is_obj(orig)) { - orig = &local_orig; - orig->tag = builder->tag; - orig->uni = builder->uni; - } - - /* If orig is contributing, copy any items not modified by the patch */ - if (orig != &local_orig) { - yyjson_mut_obj_foreach(orig, idx, max, key, orig_val) { - patch_val = yyjson_mut_obj_getn(patch, - unsafe_yyjson_get_str(key), - unsafe_yyjson_get_len(key)); - if (!patch_val) { - mut_key = yyjson_mut_val_mut_copy(doc, key); - mut_val = yyjson_mut_val_mut_copy(doc, orig_val); - if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; - } - } - } - - /* Merge items modified by the patch. */ - yyjson_mut_obj_foreach(patch, idx, max, key, patch_val) { - /* null indicates the field is removed. */ - if (unsafe_yyjson_is_null(patch_val)) { - continue; - } - mut_key = yyjson_mut_val_mut_copy(doc, key); - orig_val = yyjson_mut_obj_getn(orig, - unsafe_yyjson_get_str(key), - unsafe_yyjson_get_len(key)); - merged_val = yyjson_mut_merge_patch(doc, orig_val, patch_val); - if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; - } - - return builder; -} - -#endif /* YYJSON_DISABLE_UTILS */ - - - -/*============================================================================== - * Power10 Lookup Table - * These data are used by the floating-point number reader and writer. - *============================================================================*/ - -#if (!YYJSON_DISABLE_READER || !YYJSON_DISABLE_WRITER) && \ - (!YYJSON_DISABLE_FAST_FP_CONV) - -/** Minimum decimal exponent in pow10_sig_table. */ -#define POW10_SIG_TABLE_MIN_EXP -343 - -/** Maximum decimal exponent in pow10_sig_table. */ -#define POW10_SIG_TABLE_MAX_EXP 324 - -/** Minimum exact decimal exponent in pow10_sig_table */ -#define POW10_SIG_TABLE_MIN_EXACT_EXP 0 - -/** Maximum exact decimal exponent in pow10_sig_table */ -#define POW10_SIG_TABLE_MAX_EXACT_EXP 55 - -/** Normalized significant 128 bits of pow10, no rounded up (size: 10.4KB). - This lookup table is used by both the double number reader and writer. - (generate with misc/make_tables.c) */ -static const u64 pow10_sig_table[] = { - U64(0xBF29DCAB, 0xA82FDEAE), U64(0x7432EE87, 0x3880FC33), /* ~= 10^-343 */ - U64(0xEEF453D6, 0x923BD65A), U64(0x113FAA29, 0x06A13B3F), /* ~= 10^-342 */ - U64(0x9558B466, 0x1B6565F8), U64(0x4AC7CA59, 0xA424C507), /* ~= 10^-341 */ - U64(0xBAAEE17F, 0xA23EBF76), U64(0x5D79BCF0, 0x0D2DF649), /* ~= 10^-340 */ - U64(0xE95A99DF, 0x8ACE6F53), U64(0xF4D82C2C, 0x107973DC), /* ~= 10^-339 */ - U64(0x91D8A02B, 0xB6C10594), U64(0x79071B9B, 0x8A4BE869), /* ~= 10^-338 */ - U64(0xB64EC836, 0xA47146F9), U64(0x9748E282, 0x6CDEE284), /* ~= 10^-337 */ - U64(0xE3E27A44, 0x4D8D98B7), U64(0xFD1B1B23, 0x08169B25), /* ~= 10^-336 */ - U64(0x8E6D8C6A, 0xB0787F72), U64(0xFE30F0F5, 0xE50E20F7), /* ~= 10^-335 */ - U64(0xB208EF85, 0x5C969F4F), U64(0xBDBD2D33, 0x5E51A935), /* ~= 10^-334 */ - U64(0xDE8B2B66, 0xB3BC4723), U64(0xAD2C7880, 0x35E61382), /* ~= 10^-333 */ - U64(0x8B16FB20, 0x3055AC76), U64(0x4C3BCB50, 0x21AFCC31), /* ~= 10^-332 */ - U64(0xADDCB9E8, 0x3C6B1793), U64(0xDF4ABE24, 0x2A1BBF3D), /* ~= 10^-331 */ - U64(0xD953E862, 0x4B85DD78), U64(0xD71D6DAD, 0x34A2AF0D), /* ~= 10^-330 */ - U64(0x87D4713D, 0x6F33AA6B), U64(0x8672648C, 0x40E5AD68), /* ~= 10^-329 */ - U64(0xA9C98D8C, 0xCB009506), U64(0x680EFDAF, 0x511F18C2), /* ~= 10^-328 */ - U64(0xD43BF0EF, 0xFDC0BA48), U64(0x0212BD1B, 0x2566DEF2), /* ~= 10^-327 */ - U64(0x84A57695, 0xFE98746D), U64(0x014BB630, 0xF7604B57), /* ~= 10^-326 */ - U64(0xA5CED43B, 0x7E3E9188), U64(0x419EA3BD, 0x35385E2D), /* ~= 10^-325 */ - U64(0xCF42894A, 0x5DCE35EA), U64(0x52064CAC, 0x828675B9), /* ~= 10^-324 */ - U64(0x818995CE, 0x7AA0E1B2), U64(0x7343EFEB, 0xD1940993), /* ~= 10^-323 */ - U64(0xA1EBFB42, 0x19491A1F), U64(0x1014EBE6, 0xC5F90BF8), /* ~= 10^-322 */ - U64(0xCA66FA12, 0x9F9B60A6), U64(0xD41A26E0, 0x77774EF6), /* ~= 10^-321 */ - U64(0xFD00B897, 0x478238D0), U64(0x8920B098, 0x955522B4), /* ~= 10^-320 */ - U64(0x9E20735E, 0x8CB16382), U64(0x55B46E5F, 0x5D5535B0), /* ~= 10^-319 */ - U64(0xC5A89036, 0x2FDDBC62), U64(0xEB2189F7, 0x34AA831D), /* ~= 10^-318 */ - U64(0xF712B443, 0xBBD52B7B), U64(0xA5E9EC75, 0x01D523E4), /* ~= 10^-317 */ - U64(0x9A6BB0AA, 0x55653B2D), U64(0x47B233C9, 0x2125366E), /* ~= 10^-316 */ - U64(0xC1069CD4, 0xEABE89F8), U64(0x999EC0BB, 0x696E840A), /* ~= 10^-315 */ - U64(0xF148440A, 0x256E2C76), U64(0xC00670EA, 0x43CA250D), /* ~= 10^-314 */ - U64(0x96CD2A86, 0x5764DBCA), U64(0x38040692, 0x6A5E5728), /* ~= 10^-313 */ - U64(0xBC807527, 0xED3E12BC), U64(0xC6050837, 0x04F5ECF2), /* ~= 10^-312 */ - U64(0xEBA09271, 0xE88D976B), U64(0xF7864A44, 0xC633682E), /* ~= 10^-311 */ - U64(0x93445B87, 0x31587EA3), U64(0x7AB3EE6A, 0xFBE0211D), /* ~= 10^-310 */ - U64(0xB8157268, 0xFDAE9E4C), U64(0x5960EA05, 0xBAD82964), /* ~= 10^-309 */ - U64(0xE61ACF03, 0x3D1A45DF), U64(0x6FB92487, 0x298E33BD), /* ~= 10^-308 */ - U64(0x8FD0C162, 0x06306BAB), U64(0xA5D3B6D4, 0x79F8E056), /* ~= 10^-307 */ - U64(0xB3C4F1BA, 0x87BC8696), U64(0x8F48A489, 0x9877186C), /* ~= 10^-306 */ - U64(0xE0B62E29, 0x29ABA83C), U64(0x331ACDAB, 0xFE94DE87), /* ~= 10^-305 */ - U64(0x8C71DCD9, 0xBA0B4925), U64(0x9FF0C08B, 0x7F1D0B14), /* ~= 10^-304 */ - U64(0xAF8E5410, 0x288E1B6F), U64(0x07ECF0AE, 0x5EE44DD9), /* ~= 10^-303 */ - U64(0xDB71E914, 0x32B1A24A), U64(0xC9E82CD9, 0xF69D6150), /* ~= 10^-302 */ - U64(0x892731AC, 0x9FAF056E), U64(0xBE311C08, 0x3A225CD2), /* ~= 10^-301 */ - U64(0xAB70FE17, 0xC79AC6CA), U64(0x6DBD630A, 0x48AAF406), /* ~= 10^-300 */ - U64(0xD64D3D9D, 0xB981787D), U64(0x092CBBCC, 0xDAD5B108), /* ~= 10^-299 */ - U64(0x85F04682, 0x93F0EB4E), U64(0x25BBF560, 0x08C58EA5), /* ~= 10^-298 */ - U64(0xA76C5823, 0x38ED2621), U64(0xAF2AF2B8, 0x0AF6F24E), /* ~= 10^-297 */ - U64(0xD1476E2C, 0x07286FAA), U64(0x1AF5AF66, 0x0DB4AEE1), /* ~= 10^-296 */ - U64(0x82CCA4DB, 0x847945CA), U64(0x50D98D9F, 0xC890ED4D), /* ~= 10^-295 */ - U64(0xA37FCE12, 0x6597973C), U64(0xE50FF107, 0xBAB528A0), /* ~= 10^-294 */ - U64(0xCC5FC196, 0xFEFD7D0C), U64(0x1E53ED49, 0xA96272C8), /* ~= 10^-293 */ - U64(0xFF77B1FC, 0xBEBCDC4F), U64(0x25E8E89C, 0x13BB0F7A), /* ~= 10^-292 */ - U64(0x9FAACF3D, 0xF73609B1), U64(0x77B19161, 0x8C54E9AC), /* ~= 10^-291 */ - U64(0xC795830D, 0x75038C1D), U64(0xD59DF5B9, 0xEF6A2417), /* ~= 10^-290 */ - U64(0xF97AE3D0, 0xD2446F25), U64(0x4B057328, 0x6B44AD1D), /* ~= 10^-289 */ - U64(0x9BECCE62, 0x836AC577), U64(0x4EE367F9, 0x430AEC32), /* ~= 10^-288 */ - U64(0xC2E801FB, 0x244576D5), U64(0x229C41F7, 0x93CDA73F), /* ~= 10^-287 */ - U64(0xF3A20279, 0xED56D48A), U64(0x6B435275, 0x78C1110F), /* ~= 10^-286 */ - U64(0x9845418C, 0x345644D6), U64(0x830A1389, 0x6B78AAA9), /* ~= 10^-285 */ - U64(0xBE5691EF, 0x416BD60C), U64(0x23CC986B, 0xC656D553), /* ~= 10^-284 */ - U64(0xEDEC366B, 0x11C6CB8F), U64(0x2CBFBE86, 0xB7EC8AA8), /* ~= 10^-283 */ - U64(0x94B3A202, 0xEB1C3F39), U64(0x7BF7D714, 0x32F3D6A9), /* ~= 10^-282 */ - U64(0xB9E08A83, 0xA5E34F07), U64(0xDAF5CCD9, 0x3FB0CC53), /* ~= 10^-281 */ - U64(0xE858AD24, 0x8F5C22C9), U64(0xD1B3400F, 0x8F9CFF68), /* ~= 10^-280 */ - U64(0x91376C36, 0xD99995BE), U64(0x23100809, 0xB9C21FA1), /* ~= 10^-279 */ - U64(0xB5854744, 0x8FFFFB2D), U64(0xABD40A0C, 0x2832A78A), /* ~= 10^-278 */ - U64(0xE2E69915, 0xB3FFF9F9), U64(0x16C90C8F, 0x323F516C), /* ~= 10^-277 */ - U64(0x8DD01FAD, 0x907FFC3B), U64(0xAE3DA7D9, 0x7F6792E3), /* ~= 10^-276 */ - U64(0xB1442798, 0xF49FFB4A), U64(0x99CD11CF, 0xDF41779C), /* ~= 10^-275 */ - U64(0xDD95317F, 0x31C7FA1D), U64(0x40405643, 0xD711D583), /* ~= 10^-274 */ - U64(0x8A7D3EEF, 0x7F1CFC52), U64(0x482835EA, 0x666B2572), /* ~= 10^-273 */ - U64(0xAD1C8EAB, 0x5EE43B66), U64(0xDA324365, 0x0005EECF), /* ~= 10^-272 */ - U64(0xD863B256, 0x369D4A40), U64(0x90BED43E, 0x40076A82), /* ~= 10^-271 */ - U64(0x873E4F75, 0xE2224E68), U64(0x5A7744A6, 0xE804A291), /* ~= 10^-270 */ - U64(0xA90DE353, 0x5AAAE202), U64(0x711515D0, 0xA205CB36), /* ~= 10^-269 */ - U64(0xD3515C28, 0x31559A83), U64(0x0D5A5B44, 0xCA873E03), /* ~= 10^-268 */ - U64(0x8412D999, 0x1ED58091), U64(0xE858790A, 0xFE9486C2), /* ~= 10^-267 */ - U64(0xA5178FFF, 0x668AE0B6), U64(0x626E974D, 0xBE39A872), /* ~= 10^-266 */ - U64(0xCE5D73FF, 0x402D98E3), U64(0xFB0A3D21, 0x2DC8128F), /* ~= 10^-265 */ - U64(0x80FA687F, 0x881C7F8E), U64(0x7CE66634, 0xBC9D0B99), /* ~= 10^-264 */ - U64(0xA139029F, 0x6A239F72), U64(0x1C1FFFC1, 0xEBC44E80), /* ~= 10^-263 */ - U64(0xC9874347, 0x44AC874E), U64(0xA327FFB2, 0x66B56220), /* ~= 10^-262 */ - U64(0xFBE91419, 0x15D7A922), U64(0x4BF1FF9F, 0x0062BAA8), /* ~= 10^-261 */ - U64(0x9D71AC8F, 0xADA6C9B5), U64(0x6F773FC3, 0x603DB4A9), /* ~= 10^-260 */ - U64(0xC4CE17B3, 0x99107C22), U64(0xCB550FB4, 0x384D21D3), /* ~= 10^-259 */ - U64(0xF6019DA0, 0x7F549B2B), U64(0x7E2A53A1, 0x46606A48), /* ~= 10^-258 */ - U64(0x99C10284, 0x4F94E0FB), U64(0x2EDA7444, 0xCBFC426D), /* ~= 10^-257 */ - U64(0xC0314325, 0x637A1939), U64(0xFA911155, 0xFEFB5308), /* ~= 10^-256 */ - U64(0xF03D93EE, 0xBC589F88), U64(0x793555AB, 0x7EBA27CA), /* ~= 10^-255 */ - U64(0x96267C75, 0x35B763B5), U64(0x4BC1558B, 0x2F3458DE), /* ~= 10^-254 */ - U64(0xBBB01B92, 0x83253CA2), U64(0x9EB1AAED, 0xFB016F16), /* ~= 10^-253 */ - U64(0xEA9C2277, 0x23EE8BCB), U64(0x465E15A9, 0x79C1CADC), /* ~= 10^-252 */ - U64(0x92A1958A, 0x7675175F), U64(0x0BFACD89, 0xEC191EC9), /* ~= 10^-251 */ - U64(0xB749FAED, 0x14125D36), U64(0xCEF980EC, 0x671F667B), /* ~= 10^-250 */ - U64(0xE51C79A8, 0x5916F484), U64(0x82B7E127, 0x80E7401A), /* ~= 10^-249 */ - U64(0x8F31CC09, 0x37AE58D2), U64(0xD1B2ECB8, 0xB0908810), /* ~= 10^-248 */ - U64(0xB2FE3F0B, 0x8599EF07), U64(0x861FA7E6, 0xDCB4AA15), /* ~= 10^-247 */ - U64(0xDFBDCECE, 0x67006AC9), U64(0x67A791E0, 0x93E1D49A), /* ~= 10^-246 */ - U64(0x8BD6A141, 0x006042BD), U64(0xE0C8BB2C, 0x5C6D24E0), /* ~= 10^-245 */ - U64(0xAECC4991, 0x4078536D), U64(0x58FAE9F7, 0x73886E18), /* ~= 10^-244 */ - U64(0xDA7F5BF5, 0x90966848), U64(0xAF39A475, 0x506A899E), /* ~= 10^-243 */ - U64(0x888F9979, 0x7A5E012D), U64(0x6D8406C9, 0x52429603), /* ~= 10^-242 */ - U64(0xAAB37FD7, 0xD8F58178), U64(0xC8E5087B, 0xA6D33B83), /* ~= 10^-241 */ - U64(0xD5605FCD, 0xCF32E1D6), U64(0xFB1E4A9A, 0x90880A64), /* ~= 10^-240 */ - U64(0x855C3BE0, 0xA17FCD26), U64(0x5CF2EEA0, 0x9A55067F), /* ~= 10^-239 */ - U64(0xA6B34AD8, 0xC9DFC06F), U64(0xF42FAA48, 0xC0EA481E), /* ~= 10^-238 */ - U64(0xD0601D8E, 0xFC57B08B), U64(0xF13B94DA, 0xF124DA26), /* ~= 10^-237 */ - U64(0x823C1279, 0x5DB6CE57), U64(0x76C53D08, 0xD6B70858), /* ~= 10^-236 */ - U64(0xA2CB1717, 0xB52481ED), U64(0x54768C4B, 0x0C64CA6E), /* ~= 10^-235 */ - U64(0xCB7DDCDD, 0xA26DA268), U64(0xA9942F5D, 0xCF7DFD09), /* ~= 10^-234 */ - U64(0xFE5D5415, 0x0B090B02), U64(0xD3F93B35, 0x435D7C4C), /* ~= 10^-233 */ - U64(0x9EFA548D, 0x26E5A6E1), U64(0xC47BC501, 0x4A1A6DAF), /* ~= 10^-232 */ - U64(0xC6B8E9B0, 0x709F109A), U64(0x359AB641, 0x9CA1091B), /* ~= 10^-231 */ - U64(0xF867241C, 0x8CC6D4C0), U64(0xC30163D2, 0x03C94B62), /* ~= 10^-230 */ - U64(0x9B407691, 0xD7FC44F8), U64(0x79E0DE63, 0x425DCF1D), /* ~= 10^-229 */ - U64(0xC2109436, 0x4DFB5636), U64(0x985915FC, 0x12F542E4), /* ~= 10^-228 */ - U64(0xF294B943, 0xE17A2BC4), U64(0x3E6F5B7B, 0x17B2939D), /* ~= 10^-227 */ - U64(0x979CF3CA, 0x6CEC5B5A), U64(0xA705992C, 0xEECF9C42), /* ~= 10^-226 */ - U64(0xBD8430BD, 0x08277231), U64(0x50C6FF78, 0x2A838353), /* ~= 10^-225 */ - U64(0xECE53CEC, 0x4A314EBD), U64(0xA4F8BF56, 0x35246428), /* ~= 10^-224 */ - U64(0x940F4613, 0xAE5ED136), U64(0x871B7795, 0xE136BE99), /* ~= 10^-223 */ - U64(0xB9131798, 0x99F68584), U64(0x28E2557B, 0x59846E3F), /* ~= 10^-222 */ - U64(0xE757DD7E, 0xC07426E5), U64(0x331AEADA, 0x2FE589CF), /* ~= 10^-221 */ - U64(0x9096EA6F, 0x3848984F), U64(0x3FF0D2C8, 0x5DEF7621), /* ~= 10^-220 */ - U64(0xB4BCA50B, 0x065ABE63), U64(0x0FED077A, 0x756B53A9), /* ~= 10^-219 */ - U64(0xE1EBCE4D, 0xC7F16DFB), U64(0xD3E84959, 0x12C62894), /* ~= 10^-218 */ - U64(0x8D3360F0, 0x9CF6E4BD), U64(0x64712DD7, 0xABBBD95C), /* ~= 10^-217 */ - U64(0xB080392C, 0xC4349DEC), U64(0xBD8D794D, 0x96AACFB3), /* ~= 10^-216 */ - U64(0xDCA04777, 0xF541C567), U64(0xECF0D7A0, 0xFC5583A0), /* ~= 10^-215 */ - U64(0x89E42CAA, 0xF9491B60), U64(0xF41686C4, 0x9DB57244), /* ~= 10^-214 */ - U64(0xAC5D37D5, 0xB79B6239), U64(0x311C2875, 0xC522CED5), /* ~= 10^-213 */ - U64(0xD77485CB, 0x25823AC7), U64(0x7D633293, 0x366B828B), /* ~= 10^-212 */ - U64(0x86A8D39E, 0xF77164BC), U64(0xAE5DFF9C, 0x02033197), /* ~= 10^-211 */ - U64(0xA8530886, 0xB54DBDEB), U64(0xD9F57F83, 0x0283FDFC), /* ~= 10^-210 */ - U64(0xD267CAA8, 0x62A12D66), U64(0xD072DF63, 0xC324FD7B), /* ~= 10^-209 */ - U64(0x8380DEA9, 0x3DA4BC60), U64(0x4247CB9E, 0x59F71E6D), /* ~= 10^-208 */ - U64(0xA4611653, 0x8D0DEB78), U64(0x52D9BE85, 0xF074E608), /* ~= 10^-207 */ - U64(0xCD795BE8, 0x70516656), U64(0x67902E27, 0x6C921F8B), /* ~= 10^-206 */ - U64(0x806BD971, 0x4632DFF6), U64(0x00BA1CD8, 0xA3DB53B6), /* ~= 10^-205 */ - U64(0xA086CFCD, 0x97BF97F3), U64(0x80E8A40E, 0xCCD228A4), /* ~= 10^-204 */ - U64(0xC8A883C0, 0xFDAF7DF0), U64(0x6122CD12, 0x8006B2CD), /* ~= 10^-203 */ - U64(0xFAD2A4B1, 0x3D1B5D6C), U64(0x796B8057, 0x20085F81), /* ~= 10^-202 */ - U64(0x9CC3A6EE, 0xC6311A63), U64(0xCBE33036, 0x74053BB0), /* ~= 10^-201 */ - U64(0xC3F490AA, 0x77BD60FC), U64(0xBEDBFC44, 0x11068A9C), /* ~= 10^-200 */ - U64(0xF4F1B4D5, 0x15ACB93B), U64(0xEE92FB55, 0x15482D44), /* ~= 10^-199 */ - U64(0x99171105, 0x2D8BF3C5), U64(0x751BDD15, 0x2D4D1C4A), /* ~= 10^-198 */ - U64(0xBF5CD546, 0x78EEF0B6), U64(0xD262D45A, 0x78A0635D), /* ~= 10^-197 */ - U64(0xEF340A98, 0x172AACE4), U64(0x86FB8971, 0x16C87C34), /* ~= 10^-196 */ - U64(0x9580869F, 0x0E7AAC0E), U64(0xD45D35E6, 0xAE3D4DA0), /* ~= 10^-195 */ - U64(0xBAE0A846, 0xD2195712), U64(0x89748360, 0x59CCA109), /* ~= 10^-194 */ - U64(0xE998D258, 0x869FACD7), U64(0x2BD1A438, 0x703FC94B), /* ~= 10^-193 */ - U64(0x91FF8377, 0x5423CC06), U64(0x7B6306A3, 0x4627DDCF), /* ~= 10^-192 */ - U64(0xB67F6455, 0x292CBF08), U64(0x1A3BC84C, 0x17B1D542), /* ~= 10^-191 */ - U64(0xE41F3D6A, 0x7377EECA), U64(0x20CABA5F, 0x1D9E4A93), /* ~= 10^-190 */ - U64(0x8E938662, 0x882AF53E), U64(0x547EB47B, 0x7282EE9C), /* ~= 10^-189 */ - U64(0xB23867FB, 0x2A35B28D), U64(0xE99E619A, 0x4F23AA43), /* ~= 10^-188 */ - U64(0xDEC681F9, 0xF4C31F31), U64(0x6405FA00, 0xE2EC94D4), /* ~= 10^-187 */ - U64(0x8B3C113C, 0x38F9F37E), U64(0xDE83BC40, 0x8DD3DD04), /* ~= 10^-186 */ - U64(0xAE0B158B, 0x4738705E), U64(0x9624AB50, 0xB148D445), /* ~= 10^-185 */ - U64(0xD98DDAEE, 0x19068C76), U64(0x3BADD624, 0xDD9B0957), /* ~= 10^-184 */ - U64(0x87F8A8D4, 0xCFA417C9), U64(0xE54CA5D7, 0x0A80E5D6), /* ~= 10^-183 */ - U64(0xA9F6D30A, 0x038D1DBC), U64(0x5E9FCF4C, 0xCD211F4C), /* ~= 10^-182 */ - U64(0xD47487CC, 0x8470652B), U64(0x7647C320, 0x0069671F), /* ~= 10^-181 */ - U64(0x84C8D4DF, 0xD2C63F3B), U64(0x29ECD9F4, 0x0041E073), /* ~= 10^-180 */ - U64(0xA5FB0A17, 0xC777CF09), U64(0xF4681071, 0x00525890), /* ~= 10^-179 */ - U64(0xCF79CC9D, 0xB955C2CC), U64(0x7182148D, 0x4066EEB4), /* ~= 10^-178 */ - U64(0x81AC1FE2, 0x93D599BF), U64(0xC6F14CD8, 0x48405530), /* ~= 10^-177 */ - U64(0xA21727DB, 0x38CB002F), U64(0xB8ADA00E, 0x5A506A7C), /* ~= 10^-176 */ - U64(0xCA9CF1D2, 0x06FDC03B), U64(0xA6D90811, 0xF0E4851C), /* ~= 10^-175 */ - U64(0xFD442E46, 0x88BD304A), U64(0x908F4A16, 0x6D1DA663), /* ~= 10^-174 */ - U64(0x9E4A9CEC, 0x15763E2E), U64(0x9A598E4E, 0x043287FE), /* ~= 10^-173 */ - U64(0xC5DD4427, 0x1AD3CDBA), U64(0x40EFF1E1, 0x853F29FD), /* ~= 10^-172 */ - U64(0xF7549530, 0xE188C128), U64(0xD12BEE59, 0xE68EF47C), /* ~= 10^-171 */ - U64(0x9A94DD3E, 0x8CF578B9), U64(0x82BB74F8, 0x301958CE), /* ~= 10^-170 */ - U64(0xC13A148E, 0x3032D6E7), U64(0xE36A5236, 0x3C1FAF01), /* ~= 10^-169 */ - U64(0xF18899B1, 0xBC3F8CA1), U64(0xDC44E6C3, 0xCB279AC1), /* ~= 10^-168 */ - U64(0x96F5600F, 0x15A7B7E5), U64(0x29AB103A, 0x5EF8C0B9), /* ~= 10^-167 */ - U64(0xBCB2B812, 0xDB11A5DE), U64(0x7415D448, 0xF6B6F0E7), /* ~= 10^-166 */ - U64(0xEBDF6617, 0x91D60F56), U64(0x111B495B, 0x3464AD21), /* ~= 10^-165 */ - U64(0x936B9FCE, 0xBB25C995), U64(0xCAB10DD9, 0x00BEEC34), /* ~= 10^-164 */ - U64(0xB84687C2, 0x69EF3BFB), U64(0x3D5D514F, 0x40EEA742), /* ~= 10^-163 */ - U64(0xE65829B3, 0x046B0AFA), U64(0x0CB4A5A3, 0x112A5112), /* ~= 10^-162 */ - U64(0x8FF71A0F, 0xE2C2E6DC), U64(0x47F0E785, 0xEABA72AB), /* ~= 10^-161 */ - U64(0xB3F4E093, 0xDB73A093), U64(0x59ED2167, 0x65690F56), /* ~= 10^-160 */ - U64(0xE0F218B8, 0xD25088B8), U64(0x306869C1, 0x3EC3532C), /* ~= 10^-159 */ - U64(0x8C974F73, 0x83725573), U64(0x1E414218, 0xC73A13FB), /* ~= 10^-158 */ - U64(0xAFBD2350, 0x644EEACF), U64(0xE5D1929E, 0xF90898FA), /* ~= 10^-157 */ - U64(0xDBAC6C24, 0x7D62A583), U64(0xDF45F746, 0xB74ABF39), /* ~= 10^-156 */ - U64(0x894BC396, 0xCE5DA772), U64(0x6B8BBA8C, 0x328EB783), /* ~= 10^-155 */ - U64(0xAB9EB47C, 0x81F5114F), U64(0x066EA92F, 0x3F326564), /* ~= 10^-154 */ - U64(0xD686619B, 0xA27255A2), U64(0xC80A537B, 0x0EFEFEBD), /* ~= 10^-153 */ - U64(0x8613FD01, 0x45877585), U64(0xBD06742C, 0xE95F5F36), /* ~= 10^-152 */ - U64(0xA798FC41, 0x96E952E7), U64(0x2C481138, 0x23B73704), /* ~= 10^-151 */ - U64(0xD17F3B51, 0xFCA3A7A0), U64(0xF75A1586, 0x2CA504C5), /* ~= 10^-150 */ - U64(0x82EF8513, 0x3DE648C4), U64(0x9A984D73, 0xDBE722FB), /* ~= 10^-149 */ - U64(0xA3AB6658, 0x0D5FDAF5), U64(0xC13E60D0, 0xD2E0EBBA), /* ~= 10^-148 */ - U64(0xCC963FEE, 0x10B7D1B3), U64(0x318DF905, 0x079926A8), /* ~= 10^-147 */ - U64(0xFFBBCFE9, 0x94E5C61F), U64(0xFDF17746, 0x497F7052), /* ~= 10^-146 */ - U64(0x9FD561F1, 0xFD0F9BD3), U64(0xFEB6EA8B, 0xEDEFA633), /* ~= 10^-145 */ - U64(0xC7CABA6E, 0x7C5382C8), U64(0xFE64A52E, 0xE96B8FC0), /* ~= 10^-144 */ - U64(0xF9BD690A, 0x1B68637B), U64(0x3DFDCE7A, 0xA3C673B0), /* ~= 10^-143 */ - U64(0x9C1661A6, 0x51213E2D), U64(0x06BEA10C, 0xA65C084E), /* ~= 10^-142 */ - U64(0xC31BFA0F, 0xE5698DB8), U64(0x486E494F, 0xCFF30A62), /* ~= 10^-141 */ - U64(0xF3E2F893, 0xDEC3F126), U64(0x5A89DBA3, 0xC3EFCCFA), /* ~= 10^-140 */ - U64(0x986DDB5C, 0x6B3A76B7), U64(0xF8962946, 0x5A75E01C), /* ~= 10^-139 */ - U64(0xBE895233, 0x86091465), U64(0xF6BBB397, 0xF1135823), /* ~= 10^-138 */ - U64(0xEE2BA6C0, 0x678B597F), U64(0x746AA07D, 0xED582E2C), /* ~= 10^-137 */ - U64(0x94DB4838, 0x40B717EF), U64(0xA8C2A44E, 0xB4571CDC), /* ~= 10^-136 */ - U64(0xBA121A46, 0x50E4DDEB), U64(0x92F34D62, 0x616CE413), /* ~= 10^-135 */ - U64(0xE896A0D7, 0xE51E1566), U64(0x77B020BA, 0xF9C81D17), /* ~= 10^-134 */ - U64(0x915E2486, 0xEF32CD60), U64(0x0ACE1474, 0xDC1D122E), /* ~= 10^-133 */ - U64(0xB5B5ADA8, 0xAAFF80B8), U64(0x0D819992, 0x132456BA), /* ~= 10^-132 */ - U64(0xE3231912, 0xD5BF60E6), U64(0x10E1FFF6, 0x97ED6C69), /* ~= 10^-131 */ - U64(0x8DF5EFAB, 0xC5979C8F), U64(0xCA8D3FFA, 0x1EF463C1), /* ~= 10^-130 */ - U64(0xB1736B96, 0xB6FD83B3), U64(0xBD308FF8, 0xA6B17CB2), /* ~= 10^-129 */ - U64(0xDDD0467C, 0x64BCE4A0), U64(0xAC7CB3F6, 0xD05DDBDE), /* ~= 10^-128 */ - U64(0x8AA22C0D, 0xBEF60EE4), U64(0x6BCDF07A, 0x423AA96B), /* ~= 10^-127 */ - U64(0xAD4AB711, 0x2EB3929D), U64(0x86C16C98, 0xD2C953C6), /* ~= 10^-126 */ - U64(0xD89D64D5, 0x7A607744), U64(0xE871C7BF, 0x077BA8B7), /* ~= 10^-125 */ - U64(0x87625F05, 0x6C7C4A8B), U64(0x11471CD7, 0x64AD4972), /* ~= 10^-124 */ - U64(0xA93AF6C6, 0xC79B5D2D), U64(0xD598E40D, 0x3DD89BCF), /* ~= 10^-123 */ - U64(0xD389B478, 0x79823479), U64(0x4AFF1D10, 0x8D4EC2C3), /* ~= 10^-122 */ - U64(0x843610CB, 0x4BF160CB), U64(0xCEDF722A, 0x585139BA), /* ~= 10^-121 */ - U64(0xA54394FE, 0x1EEDB8FE), U64(0xC2974EB4, 0xEE658828), /* ~= 10^-120 */ - U64(0xCE947A3D, 0xA6A9273E), U64(0x733D2262, 0x29FEEA32), /* ~= 10^-119 */ - U64(0x811CCC66, 0x8829B887), U64(0x0806357D, 0x5A3F525F), /* ~= 10^-118 */ - U64(0xA163FF80, 0x2A3426A8), U64(0xCA07C2DC, 0xB0CF26F7), /* ~= 10^-117 */ - U64(0xC9BCFF60, 0x34C13052), U64(0xFC89B393, 0xDD02F0B5), /* ~= 10^-116 */ - U64(0xFC2C3F38, 0x41F17C67), U64(0xBBAC2078, 0xD443ACE2), /* ~= 10^-115 */ - U64(0x9D9BA783, 0x2936EDC0), U64(0xD54B944B, 0x84AA4C0D), /* ~= 10^-114 */ - U64(0xC5029163, 0xF384A931), U64(0x0A9E795E, 0x65D4DF11), /* ~= 10^-113 */ - U64(0xF64335BC, 0xF065D37D), U64(0x4D4617B5, 0xFF4A16D5), /* ~= 10^-112 */ - U64(0x99EA0196, 0x163FA42E), U64(0x504BCED1, 0xBF8E4E45), /* ~= 10^-111 */ - U64(0xC06481FB, 0x9BCF8D39), U64(0xE45EC286, 0x2F71E1D6), /* ~= 10^-110 */ - U64(0xF07DA27A, 0x82C37088), U64(0x5D767327, 0xBB4E5A4C), /* ~= 10^-109 */ - U64(0x964E858C, 0x91BA2655), U64(0x3A6A07F8, 0xD510F86F), /* ~= 10^-108 */ - U64(0xBBE226EF, 0xB628AFEA), U64(0x890489F7, 0x0A55368B), /* ~= 10^-107 */ - U64(0xEADAB0AB, 0xA3B2DBE5), U64(0x2B45AC74, 0xCCEA842E), /* ~= 10^-106 */ - U64(0x92C8AE6B, 0x464FC96F), U64(0x3B0B8BC9, 0x0012929D), /* ~= 10^-105 */ - U64(0xB77ADA06, 0x17E3BBCB), U64(0x09CE6EBB, 0x40173744), /* ~= 10^-104 */ - U64(0xE5599087, 0x9DDCAABD), U64(0xCC420A6A, 0x101D0515), /* ~= 10^-103 */ - U64(0x8F57FA54, 0xC2A9EAB6), U64(0x9FA94682, 0x4A12232D), /* ~= 10^-102 */ - U64(0xB32DF8E9, 0xF3546564), U64(0x47939822, 0xDC96ABF9), /* ~= 10^-101 */ - U64(0xDFF97724, 0x70297EBD), U64(0x59787E2B, 0x93BC56F7), /* ~= 10^-100 */ - U64(0x8BFBEA76, 0xC619EF36), U64(0x57EB4EDB, 0x3C55B65A), /* ~= 10^-99 */ - U64(0xAEFAE514, 0x77A06B03), U64(0xEDE62292, 0x0B6B23F1), /* ~= 10^-98 */ - U64(0xDAB99E59, 0x958885C4), U64(0xE95FAB36, 0x8E45ECED), /* ~= 10^-97 */ - U64(0x88B402F7, 0xFD75539B), U64(0x11DBCB02, 0x18EBB414), /* ~= 10^-96 */ - U64(0xAAE103B5, 0xFCD2A881), U64(0xD652BDC2, 0x9F26A119), /* ~= 10^-95 */ - U64(0xD59944A3, 0x7C0752A2), U64(0x4BE76D33, 0x46F0495F), /* ~= 10^-94 */ - U64(0x857FCAE6, 0x2D8493A5), U64(0x6F70A440, 0x0C562DDB), /* ~= 10^-93 */ - U64(0xA6DFBD9F, 0xB8E5B88E), U64(0xCB4CCD50, 0x0F6BB952), /* ~= 10^-92 */ - U64(0xD097AD07, 0xA71F26B2), U64(0x7E2000A4, 0x1346A7A7), /* ~= 10^-91 */ - U64(0x825ECC24, 0xC873782F), U64(0x8ED40066, 0x8C0C28C8), /* ~= 10^-90 */ - U64(0xA2F67F2D, 0xFA90563B), U64(0x72890080, 0x2F0F32FA), /* ~= 10^-89 */ - U64(0xCBB41EF9, 0x79346BCA), U64(0x4F2B40A0, 0x3AD2FFB9), /* ~= 10^-88 */ - U64(0xFEA126B7, 0xD78186BC), U64(0xE2F610C8, 0x4987BFA8), /* ~= 10^-87 */ - U64(0x9F24B832, 0xE6B0F436), U64(0x0DD9CA7D, 0x2DF4D7C9), /* ~= 10^-86 */ - U64(0xC6EDE63F, 0xA05D3143), U64(0x91503D1C, 0x79720DBB), /* ~= 10^-85 */ - U64(0xF8A95FCF, 0x88747D94), U64(0x75A44C63, 0x97CE912A), /* ~= 10^-84 */ - U64(0x9B69DBE1, 0xB548CE7C), U64(0xC986AFBE, 0x3EE11ABA), /* ~= 10^-83 */ - U64(0xC24452DA, 0x229B021B), U64(0xFBE85BAD, 0xCE996168), /* ~= 10^-82 */ - U64(0xF2D56790, 0xAB41C2A2), U64(0xFAE27299, 0x423FB9C3), /* ~= 10^-81 */ - U64(0x97C560BA, 0x6B0919A5), U64(0xDCCD879F, 0xC967D41A), /* ~= 10^-80 */ - U64(0xBDB6B8E9, 0x05CB600F), U64(0x5400E987, 0xBBC1C920), /* ~= 10^-79 */ - U64(0xED246723, 0x473E3813), U64(0x290123E9, 0xAAB23B68), /* ~= 10^-78 */ - U64(0x9436C076, 0x0C86E30B), U64(0xF9A0B672, 0x0AAF6521), /* ~= 10^-77 */ - U64(0xB9447093, 0x8FA89BCE), U64(0xF808E40E, 0x8D5B3E69), /* ~= 10^-76 */ - U64(0xE7958CB8, 0x7392C2C2), U64(0xB60B1D12, 0x30B20E04), /* ~= 10^-75 */ - U64(0x90BD77F3, 0x483BB9B9), U64(0xB1C6F22B, 0x5E6F48C2), /* ~= 10^-74 */ - U64(0xB4ECD5F0, 0x1A4AA828), U64(0x1E38AEB6, 0x360B1AF3), /* ~= 10^-73 */ - U64(0xE2280B6C, 0x20DD5232), U64(0x25C6DA63, 0xC38DE1B0), /* ~= 10^-72 */ - U64(0x8D590723, 0x948A535F), U64(0x579C487E, 0x5A38AD0E), /* ~= 10^-71 */ - U64(0xB0AF48EC, 0x79ACE837), U64(0x2D835A9D, 0xF0C6D851), /* ~= 10^-70 */ - U64(0xDCDB1B27, 0x98182244), U64(0xF8E43145, 0x6CF88E65), /* ~= 10^-69 */ - U64(0x8A08F0F8, 0xBF0F156B), U64(0x1B8E9ECB, 0x641B58FF), /* ~= 10^-68 */ - U64(0xAC8B2D36, 0xEED2DAC5), U64(0xE272467E, 0x3D222F3F), /* ~= 10^-67 */ - U64(0xD7ADF884, 0xAA879177), U64(0x5B0ED81D, 0xCC6ABB0F), /* ~= 10^-66 */ - U64(0x86CCBB52, 0xEA94BAEA), U64(0x98E94712, 0x9FC2B4E9), /* ~= 10^-65 */ - U64(0xA87FEA27, 0xA539E9A5), U64(0x3F2398D7, 0x47B36224), /* ~= 10^-64 */ - U64(0xD29FE4B1, 0x8E88640E), U64(0x8EEC7F0D, 0x19A03AAD), /* ~= 10^-63 */ - U64(0x83A3EEEE, 0xF9153E89), U64(0x1953CF68, 0x300424AC), /* ~= 10^-62 */ - U64(0xA48CEAAA, 0xB75A8E2B), U64(0x5FA8C342, 0x3C052DD7), /* ~= 10^-61 */ - U64(0xCDB02555, 0x653131B6), U64(0x3792F412, 0xCB06794D), /* ~= 10^-60 */ - U64(0x808E1755, 0x5F3EBF11), U64(0xE2BBD88B, 0xBEE40BD0), /* ~= 10^-59 */ - U64(0xA0B19D2A, 0xB70E6ED6), U64(0x5B6ACEAE, 0xAE9D0EC4), /* ~= 10^-58 */ - U64(0xC8DE0475, 0x64D20A8B), U64(0xF245825A, 0x5A445275), /* ~= 10^-57 */ - U64(0xFB158592, 0xBE068D2E), U64(0xEED6E2F0, 0xF0D56712), /* ~= 10^-56 */ - U64(0x9CED737B, 0xB6C4183D), U64(0x55464DD6, 0x9685606B), /* ~= 10^-55 */ - U64(0xC428D05A, 0xA4751E4C), U64(0xAA97E14C, 0x3C26B886), /* ~= 10^-54 */ - U64(0xF5330471, 0x4D9265DF), U64(0xD53DD99F, 0x4B3066A8), /* ~= 10^-53 */ - U64(0x993FE2C6, 0xD07B7FAB), U64(0xE546A803, 0x8EFE4029), /* ~= 10^-52 */ - U64(0xBF8FDB78, 0x849A5F96), U64(0xDE985204, 0x72BDD033), /* ~= 10^-51 */ - U64(0xEF73D256, 0xA5C0F77C), U64(0x963E6685, 0x8F6D4440), /* ~= 10^-50 */ - U64(0x95A86376, 0x27989AAD), U64(0xDDE70013, 0x79A44AA8), /* ~= 10^-49 */ - U64(0xBB127C53, 0xB17EC159), U64(0x5560C018, 0x580D5D52), /* ~= 10^-48 */ - U64(0xE9D71B68, 0x9DDE71AF), U64(0xAAB8F01E, 0x6E10B4A6), /* ~= 10^-47 */ - U64(0x92267121, 0x62AB070D), U64(0xCAB39613, 0x04CA70E8), /* ~= 10^-46 */ - U64(0xB6B00D69, 0xBB55C8D1), U64(0x3D607B97, 0xC5FD0D22), /* ~= 10^-45 */ - U64(0xE45C10C4, 0x2A2B3B05), U64(0x8CB89A7D, 0xB77C506A), /* ~= 10^-44 */ - U64(0x8EB98A7A, 0x9A5B04E3), U64(0x77F3608E, 0x92ADB242), /* ~= 10^-43 */ - U64(0xB267ED19, 0x40F1C61C), U64(0x55F038B2, 0x37591ED3), /* ~= 10^-42 */ - U64(0xDF01E85F, 0x912E37A3), U64(0x6B6C46DE, 0xC52F6688), /* ~= 10^-41 */ - U64(0x8B61313B, 0xBABCE2C6), U64(0x2323AC4B, 0x3B3DA015), /* ~= 10^-40 */ - U64(0xAE397D8A, 0xA96C1B77), U64(0xABEC975E, 0x0A0D081A), /* ~= 10^-39 */ - U64(0xD9C7DCED, 0x53C72255), U64(0x96E7BD35, 0x8C904A21), /* ~= 10^-38 */ - U64(0x881CEA14, 0x545C7575), U64(0x7E50D641, 0x77DA2E54), /* ~= 10^-37 */ - U64(0xAA242499, 0x697392D2), U64(0xDDE50BD1, 0xD5D0B9E9), /* ~= 10^-36 */ - U64(0xD4AD2DBF, 0xC3D07787), U64(0x955E4EC6, 0x4B44E864), /* ~= 10^-35 */ - U64(0x84EC3C97, 0xDA624AB4), U64(0xBD5AF13B, 0xEF0B113E), /* ~= 10^-34 */ - U64(0xA6274BBD, 0xD0FADD61), U64(0xECB1AD8A, 0xEACDD58E), /* ~= 10^-33 */ - U64(0xCFB11EAD, 0x453994BA), U64(0x67DE18ED, 0xA5814AF2), /* ~= 10^-32 */ - U64(0x81CEB32C, 0x4B43FCF4), U64(0x80EACF94, 0x8770CED7), /* ~= 10^-31 */ - U64(0xA2425FF7, 0x5E14FC31), U64(0xA1258379, 0xA94D028D), /* ~= 10^-30 */ - U64(0xCAD2F7F5, 0x359A3B3E), U64(0x096EE458, 0x13A04330), /* ~= 10^-29 */ - U64(0xFD87B5F2, 0x8300CA0D), U64(0x8BCA9D6E, 0x188853FC), /* ~= 10^-28 */ - U64(0x9E74D1B7, 0x91E07E48), U64(0x775EA264, 0xCF55347D), /* ~= 10^-27 */ - U64(0xC6120625, 0x76589DDA), U64(0x95364AFE, 0x032A819D), /* ~= 10^-26 */ - U64(0xF79687AE, 0xD3EEC551), U64(0x3A83DDBD, 0x83F52204), /* ~= 10^-25 */ - U64(0x9ABE14CD, 0x44753B52), U64(0xC4926A96, 0x72793542), /* ~= 10^-24 */ - U64(0xC16D9A00, 0x95928A27), U64(0x75B7053C, 0x0F178293), /* ~= 10^-23 */ - U64(0xF1C90080, 0xBAF72CB1), U64(0x5324C68B, 0x12DD6338), /* ~= 10^-22 */ - U64(0x971DA050, 0x74DA7BEE), U64(0xD3F6FC16, 0xEBCA5E03), /* ~= 10^-21 */ - U64(0xBCE50864, 0x92111AEA), U64(0x88F4BB1C, 0xA6BCF584), /* ~= 10^-20 */ - U64(0xEC1E4A7D, 0xB69561A5), U64(0x2B31E9E3, 0xD06C32E5), /* ~= 10^-19 */ - U64(0x9392EE8E, 0x921D5D07), U64(0x3AFF322E, 0x62439FCF), /* ~= 10^-18 */ - U64(0xB877AA32, 0x36A4B449), U64(0x09BEFEB9, 0xFAD487C2), /* ~= 10^-17 */ - U64(0xE69594BE, 0xC44DE15B), U64(0x4C2EBE68, 0x7989A9B3), /* ~= 10^-16 */ - U64(0x901D7CF7, 0x3AB0ACD9), U64(0x0F9D3701, 0x4BF60A10), /* ~= 10^-15 */ - U64(0xB424DC35, 0x095CD80F), U64(0x538484C1, 0x9EF38C94), /* ~= 10^-14 */ - U64(0xE12E1342, 0x4BB40E13), U64(0x2865A5F2, 0x06B06FB9), /* ~= 10^-13 */ - U64(0x8CBCCC09, 0x6F5088CB), U64(0xF93F87B7, 0x442E45D3), /* ~= 10^-12 */ - U64(0xAFEBFF0B, 0xCB24AAFE), U64(0xF78F69A5, 0x1539D748), /* ~= 10^-11 */ - U64(0xDBE6FECE, 0xBDEDD5BE), U64(0xB573440E, 0x5A884D1B), /* ~= 10^-10 */ - U64(0x89705F41, 0x36B4A597), U64(0x31680A88, 0xF8953030), /* ~= 10^-9 */ - U64(0xABCC7711, 0x8461CEFC), U64(0xFDC20D2B, 0x36BA7C3D), /* ~= 10^-8 */ - U64(0xD6BF94D5, 0xE57A42BC), U64(0x3D329076, 0x04691B4C), /* ~= 10^-7 */ - U64(0x8637BD05, 0xAF6C69B5), U64(0xA63F9A49, 0xC2C1B10F), /* ~= 10^-6 */ - U64(0xA7C5AC47, 0x1B478423), U64(0x0FCF80DC, 0x33721D53), /* ~= 10^-5 */ - U64(0xD1B71758, 0xE219652B), U64(0xD3C36113, 0x404EA4A8), /* ~= 10^-4 */ - U64(0x83126E97, 0x8D4FDF3B), U64(0x645A1CAC, 0x083126E9), /* ~= 10^-3 */ - U64(0xA3D70A3D, 0x70A3D70A), U64(0x3D70A3D7, 0x0A3D70A3), /* ~= 10^-2 */ - U64(0xCCCCCCCC, 0xCCCCCCCC), U64(0xCCCCCCCC, 0xCCCCCCCC), /* ~= 10^-1 */ - U64(0x80000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^0 */ - U64(0xA0000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^1 */ - U64(0xC8000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^2 */ - U64(0xFA000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^3 */ - U64(0x9C400000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^4 */ - U64(0xC3500000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^5 */ - U64(0xF4240000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^6 */ - U64(0x98968000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^7 */ - U64(0xBEBC2000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^8 */ - U64(0xEE6B2800, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^9 */ - U64(0x9502F900, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^10 */ - U64(0xBA43B740, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^11 */ - U64(0xE8D4A510, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^12 */ - U64(0x9184E72A, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^13 */ - U64(0xB5E620F4, 0x80000000), U64(0x00000000, 0x00000000), /* == 10^14 */ - U64(0xE35FA931, 0xA0000000), U64(0x00000000, 0x00000000), /* == 10^15 */ - U64(0x8E1BC9BF, 0x04000000), U64(0x00000000, 0x00000000), /* == 10^16 */ - U64(0xB1A2BC2E, 0xC5000000), U64(0x00000000, 0x00000000), /* == 10^17 */ - U64(0xDE0B6B3A, 0x76400000), U64(0x00000000, 0x00000000), /* == 10^18 */ - U64(0x8AC72304, 0x89E80000), U64(0x00000000, 0x00000000), /* == 10^19 */ - U64(0xAD78EBC5, 0xAC620000), U64(0x00000000, 0x00000000), /* == 10^20 */ - U64(0xD8D726B7, 0x177A8000), U64(0x00000000, 0x00000000), /* == 10^21 */ - U64(0x87867832, 0x6EAC9000), U64(0x00000000, 0x00000000), /* == 10^22 */ - U64(0xA968163F, 0x0A57B400), U64(0x00000000, 0x00000000), /* == 10^23 */ - U64(0xD3C21BCE, 0xCCEDA100), U64(0x00000000, 0x00000000), /* == 10^24 */ - U64(0x84595161, 0x401484A0), U64(0x00000000, 0x00000000), /* == 10^25 */ - U64(0xA56FA5B9, 0x9019A5C8), U64(0x00000000, 0x00000000), /* == 10^26 */ - U64(0xCECB8F27, 0xF4200F3A), U64(0x00000000, 0x00000000), /* == 10^27 */ - U64(0x813F3978, 0xF8940984), U64(0x40000000, 0x00000000), /* == 10^28 */ - U64(0xA18F07D7, 0x36B90BE5), U64(0x50000000, 0x00000000), /* == 10^29 */ - U64(0xC9F2C9CD, 0x04674EDE), U64(0xA4000000, 0x00000000), /* == 10^30 */ - U64(0xFC6F7C40, 0x45812296), U64(0x4D000000, 0x00000000), /* == 10^31 */ - U64(0x9DC5ADA8, 0x2B70B59D), U64(0xF0200000, 0x00000000), /* == 10^32 */ - U64(0xC5371912, 0x364CE305), U64(0x6C280000, 0x00000000), /* == 10^33 */ - U64(0xF684DF56, 0xC3E01BC6), U64(0xC7320000, 0x00000000), /* == 10^34 */ - U64(0x9A130B96, 0x3A6C115C), U64(0x3C7F4000, 0x00000000), /* == 10^35 */ - U64(0xC097CE7B, 0xC90715B3), U64(0x4B9F1000, 0x00000000), /* == 10^36 */ - U64(0xF0BDC21A, 0xBB48DB20), U64(0x1E86D400, 0x00000000), /* == 10^37 */ - U64(0x96769950, 0xB50D88F4), U64(0x13144480, 0x00000000), /* == 10^38 */ - U64(0xBC143FA4, 0xE250EB31), U64(0x17D955A0, 0x00000000), /* == 10^39 */ - U64(0xEB194F8E, 0x1AE525FD), U64(0x5DCFAB08, 0x00000000), /* == 10^40 */ - U64(0x92EFD1B8, 0xD0CF37BE), U64(0x5AA1CAE5, 0x00000000), /* == 10^41 */ - U64(0xB7ABC627, 0x050305AD), U64(0xF14A3D9E, 0x40000000), /* == 10^42 */ - U64(0xE596B7B0, 0xC643C719), U64(0x6D9CCD05, 0xD0000000), /* == 10^43 */ - U64(0x8F7E32CE, 0x7BEA5C6F), U64(0xE4820023, 0xA2000000), /* == 10^44 */ - U64(0xB35DBF82, 0x1AE4F38B), U64(0xDDA2802C, 0x8A800000), /* == 10^45 */ - U64(0xE0352F62, 0xA19E306E), U64(0xD50B2037, 0xAD200000), /* == 10^46 */ - U64(0x8C213D9D, 0xA502DE45), U64(0x4526F422, 0xCC340000), /* == 10^47 */ - U64(0xAF298D05, 0x0E4395D6), U64(0x9670B12B, 0x7F410000), /* == 10^48 */ - U64(0xDAF3F046, 0x51D47B4C), U64(0x3C0CDD76, 0x5F114000), /* == 10^49 */ - U64(0x88D8762B, 0xF324CD0F), U64(0xA5880A69, 0xFB6AC800), /* == 10^50 */ - U64(0xAB0E93B6, 0xEFEE0053), U64(0x8EEA0D04, 0x7A457A00), /* == 10^51 */ - U64(0xD5D238A4, 0xABE98068), U64(0x72A49045, 0x98D6D880), /* == 10^52 */ - U64(0x85A36366, 0xEB71F041), U64(0x47A6DA2B, 0x7F864750), /* == 10^53 */ - U64(0xA70C3C40, 0xA64E6C51), U64(0x999090B6, 0x5F67D924), /* == 10^54 */ - U64(0xD0CF4B50, 0xCFE20765), U64(0xFFF4B4E3, 0xF741CF6D), /* == 10^55 */ - U64(0x82818F12, 0x81ED449F), U64(0xBFF8F10E, 0x7A8921A4), /* ~= 10^56 */ - U64(0xA321F2D7, 0x226895C7), U64(0xAFF72D52, 0x192B6A0D), /* ~= 10^57 */ - U64(0xCBEA6F8C, 0xEB02BB39), U64(0x9BF4F8A6, 0x9F764490), /* ~= 10^58 */ - U64(0xFEE50B70, 0x25C36A08), U64(0x02F236D0, 0x4753D5B4), /* ~= 10^59 */ - U64(0x9F4F2726, 0x179A2245), U64(0x01D76242, 0x2C946590), /* ~= 10^60 */ - U64(0xC722F0EF, 0x9D80AAD6), U64(0x424D3AD2, 0xB7B97EF5), /* ~= 10^61 */ - U64(0xF8EBAD2B, 0x84E0D58B), U64(0xD2E08987, 0x65A7DEB2), /* ~= 10^62 */ - U64(0x9B934C3B, 0x330C8577), U64(0x63CC55F4, 0x9F88EB2F), /* ~= 10^63 */ - U64(0xC2781F49, 0xFFCFA6D5), U64(0x3CBF6B71, 0xC76B25FB), /* ~= 10^64 */ - U64(0xF316271C, 0x7FC3908A), U64(0x8BEF464E, 0x3945EF7A), /* ~= 10^65 */ - U64(0x97EDD871, 0xCFDA3A56), U64(0x97758BF0, 0xE3CBB5AC), /* ~= 10^66 */ - U64(0xBDE94E8E, 0x43D0C8EC), U64(0x3D52EEED, 0x1CBEA317), /* ~= 10^67 */ - U64(0xED63A231, 0xD4C4FB27), U64(0x4CA7AAA8, 0x63EE4BDD), /* ~= 10^68 */ - U64(0x945E455F, 0x24FB1CF8), U64(0x8FE8CAA9, 0x3E74EF6A), /* ~= 10^69 */ - U64(0xB975D6B6, 0xEE39E436), U64(0xB3E2FD53, 0x8E122B44), /* ~= 10^70 */ - U64(0xE7D34C64, 0xA9C85D44), U64(0x60DBBCA8, 0x7196B616), /* ~= 10^71 */ - U64(0x90E40FBE, 0xEA1D3A4A), U64(0xBC8955E9, 0x46FE31CD), /* ~= 10^72 */ - U64(0xB51D13AE, 0xA4A488DD), U64(0x6BABAB63, 0x98BDBE41), /* ~= 10^73 */ - U64(0xE264589A, 0x4DCDAB14), U64(0xC696963C, 0x7EED2DD1), /* ~= 10^74 */ - U64(0x8D7EB760, 0x70A08AEC), U64(0xFC1E1DE5, 0xCF543CA2), /* ~= 10^75 */ - U64(0xB0DE6538, 0x8CC8ADA8), U64(0x3B25A55F, 0x43294BCB), /* ~= 10^76 */ - U64(0xDD15FE86, 0xAFFAD912), U64(0x49EF0EB7, 0x13F39EBE), /* ~= 10^77 */ - U64(0x8A2DBF14, 0x2DFCC7AB), U64(0x6E356932, 0x6C784337), /* ~= 10^78 */ - U64(0xACB92ED9, 0x397BF996), U64(0x49C2C37F, 0x07965404), /* ~= 10^79 */ - U64(0xD7E77A8F, 0x87DAF7FB), U64(0xDC33745E, 0xC97BE906), /* ~= 10^80 */ - U64(0x86F0AC99, 0xB4E8DAFD), U64(0x69A028BB, 0x3DED71A3), /* ~= 10^81 */ - U64(0xA8ACD7C0, 0x222311BC), U64(0xC40832EA, 0x0D68CE0C), /* ~= 10^82 */ - U64(0xD2D80DB0, 0x2AABD62B), U64(0xF50A3FA4, 0x90C30190), /* ~= 10^83 */ - U64(0x83C7088E, 0x1AAB65DB), U64(0x792667C6, 0xDA79E0FA), /* ~= 10^84 */ - U64(0xA4B8CAB1, 0xA1563F52), U64(0x577001B8, 0x91185938), /* ~= 10^85 */ - U64(0xCDE6FD5E, 0x09ABCF26), U64(0xED4C0226, 0xB55E6F86), /* ~= 10^86 */ - U64(0x80B05E5A, 0xC60B6178), U64(0x544F8158, 0x315B05B4), /* ~= 10^87 */ - U64(0xA0DC75F1, 0x778E39D6), U64(0x696361AE, 0x3DB1C721), /* ~= 10^88 */ - U64(0xC913936D, 0xD571C84C), U64(0x03BC3A19, 0xCD1E38E9), /* ~= 10^89 */ - U64(0xFB587849, 0x4ACE3A5F), U64(0x04AB48A0, 0x4065C723), /* ~= 10^90 */ - U64(0x9D174B2D, 0xCEC0E47B), U64(0x62EB0D64, 0x283F9C76), /* ~= 10^91 */ - U64(0xC45D1DF9, 0x42711D9A), U64(0x3BA5D0BD, 0x324F8394), /* ~= 10^92 */ - U64(0xF5746577, 0x930D6500), U64(0xCA8F44EC, 0x7EE36479), /* ~= 10^93 */ - U64(0x9968BF6A, 0xBBE85F20), U64(0x7E998B13, 0xCF4E1ECB), /* ~= 10^94 */ - U64(0xBFC2EF45, 0x6AE276E8), U64(0x9E3FEDD8, 0xC321A67E), /* ~= 10^95 */ - U64(0xEFB3AB16, 0xC59B14A2), U64(0xC5CFE94E, 0xF3EA101E), /* ~= 10^96 */ - U64(0x95D04AEE, 0x3B80ECE5), U64(0xBBA1F1D1, 0x58724A12), /* ~= 10^97 */ - U64(0xBB445DA9, 0xCA61281F), U64(0x2A8A6E45, 0xAE8EDC97), /* ~= 10^98 */ - U64(0xEA157514, 0x3CF97226), U64(0xF52D09D7, 0x1A3293BD), /* ~= 10^99 */ - U64(0x924D692C, 0xA61BE758), U64(0x593C2626, 0x705F9C56), /* ~= 10^100 */ - U64(0xB6E0C377, 0xCFA2E12E), U64(0x6F8B2FB0, 0x0C77836C), /* ~= 10^101 */ - U64(0xE498F455, 0xC38B997A), U64(0x0B6DFB9C, 0x0F956447), /* ~= 10^102 */ - U64(0x8EDF98B5, 0x9A373FEC), U64(0x4724BD41, 0x89BD5EAC), /* ~= 10^103 */ - U64(0xB2977EE3, 0x00C50FE7), U64(0x58EDEC91, 0xEC2CB657), /* ~= 10^104 */ - U64(0xDF3D5E9B, 0xC0F653E1), U64(0x2F2967B6, 0x6737E3ED), /* ~= 10^105 */ - U64(0x8B865B21, 0x5899F46C), U64(0xBD79E0D2, 0x0082EE74), /* ~= 10^106 */ - U64(0xAE67F1E9, 0xAEC07187), U64(0xECD85906, 0x80A3AA11), /* ~= 10^107 */ - U64(0xDA01EE64, 0x1A708DE9), U64(0xE80E6F48, 0x20CC9495), /* ~= 10^108 */ - U64(0x884134FE, 0x908658B2), U64(0x3109058D, 0x147FDCDD), /* ~= 10^109 */ - U64(0xAA51823E, 0x34A7EEDE), U64(0xBD4B46F0, 0x599FD415), /* ~= 10^110 */ - U64(0xD4E5E2CD, 0xC1D1EA96), U64(0x6C9E18AC, 0x7007C91A), /* ~= 10^111 */ - U64(0x850FADC0, 0x9923329E), U64(0x03E2CF6B, 0xC604DDB0), /* ~= 10^112 */ - U64(0xA6539930, 0xBF6BFF45), U64(0x84DB8346, 0xB786151C), /* ~= 10^113 */ - U64(0xCFE87F7C, 0xEF46FF16), U64(0xE6126418, 0x65679A63), /* ~= 10^114 */ - U64(0x81F14FAE, 0x158C5F6E), U64(0x4FCB7E8F, 0x3F60C07E), /* ~= 10^115 */ - U64(0xA26DA399, 0x9AEF7749), U64(0xE3BE5E33, 0x0F38F09D), /* ~= 10^116 */ - U64(0xCB090C80, 0x01AB551C), U64(0x5CADF5BF, 0xD3072CC5), /* ~= 10^117 */ - U64(0xFDCB4FA0, 0x02162A63), U64(0x73D9732F, 0xC7C8F7F6), /* ~= 10^118 */ - U64(0x9E9F11C4, 0x014DDA7E), U64(0x2867E7FD, 0xDCDD9AFA), /* ~= 10^119 */ - U64(0xC646D635, 0x01A1511D), U64(0xB281E1FD, 0x541501B8), /* ~= 10^120 */ - U64(0xF7D88BC2, 0x4209A565), U64(0x1F225A7C, 0xA91A4226), /* ~= 10^121 */ - U64(0x9AE75759, 0x6946075F), U64(0x3375788D, 0xE9B06958), /* ~= 10^122 */ - U64(0xC1A12D2F, 0xC3978937), U64(0x0052D6B1, 0x641C83AE), /* ~= 10^123 */ - U64(0xF209787B, 0xB47D6B84), U64(0xC0678C5D, 0xBD23A49A), /* ~= 10^124 */ - U64(0x9745EB4D, 0x50CE6332), U64(0xF840B7BA, 0x963646E0), /* ~= 10^125 */ - U64(0xBD176620, 0xA501FBFF), U64(0xB650E5A9, 0x3BC3D898), /* ~= 10^126 */ - U64(0xEC5D3FA8, 0xCE427AFF), U64(0xA3E51F13, 0x8AB4CEBE), /* ~= 10^127 */ - U64(0x93BA47C9, 0x80E98CDF), U64(0xC66F336C, 0x36B10137), /* ~= 10^128 */ - U64(0xB8A8D9BB, 0xE123F017), U64(0xB80B0047, 0x445D4184), /* ~= 10^129 */ - U64(0xE6D3102A, 0xD96CEC1D), U64(0xA60DC059, 0x157491E5), /* ~= 10^130 */ - U64(0x9043EA1A, 0xC7E41392), U64(0x87C89837, 0xAD68DB2F), /* ~= 10^131 */ - U64(0xB454E4A1, 0x79DD1877), U64(0x29BABE45, 0x98C311FB), /* ~= 10^132 */ - U64(0xE16A1DC9, 0xD8545E94), U64(0xF4296DD6, 0xFEF3D67A), /* ~= 10^133 */ - U64(0x8CE2529E, 0x2734BB1D), U64(0x1899E4A6, 0x5F58660C), /* ~= 10^134 */ - U64(0xB01AE745, 0xB101E9E4), U64(0x5EC05DCF, 0xF72E7F8F), /* ~= 10^135 */ - U64(0xDC21A117, 0x1D42645D), U64(0x76707543, 0xF4FA1F73), /* ~= 10^136 */ - U64(0x899504AE, 0x72497EBA), U64(0x6A06494A, 0x791C53A8), /* ~= 10^137 */ - U64(0xABFA45DA, 0x0EDBDE69), U64(0x0487DB9D, 0x17636892), /* ~= 10^138 */ - U64(0xD6F8D750, 0x9292D603), U64(0x45A9D284, 0x5D3C42B6), /* ~= 10^139 */ - U64(0x865B8692, 0x5B9BC5C2), U64(0x0B8A2392, 0xBA45A9B2), /* ~= 10^140 */ - U64(0xA7F26836, 0xF282B732), U64(0x8E6CAC77, 0x68D7141E), /* ~= 10^141 */ - U64(0xD1EF0244, 0xAF2364FF), U64(0x3207D795, 0x430CD926), /* ~= 10^142 */ - U64(0x8335616A, 0xED761F1F), U64(0x7F44E6BD, 0x49E807B8), /* ~= 10^143 */ - U64(0xA402B9C5, 0xA8D3A6E7), U64(0x5F16206C, 0x9C6209A6), /* ~= 10^144 */ - U64(0xCD036837, 0x130890A1), U64(0x36DBA887, 0xC37A8C0F), /* ~= 10^145 */ - U64(0x80222122, 0x6BE55A64), U64(0xC2494954, 0xDA2C9789), /* ~= 10^146 */ - U64(0xA02AA96B, 0x06DEB0FD), U64(0xF2DB9BAA, 0x10B7BD6C), /* ~= 10^147 */ - U64(0xC83553C5, 0xC8965D3D), U64(0x6F928294, 0x94E5ACC7), /* ~= 10^148 */ - U64(0xFA42A8B7, 0x3ABBF48C), U64(0xCB772339, 0xBA1F17F9), /* ~= 10^149 */ - U64(0x9C69A972, 0x84B578D7), U64(0xFF2A7604, 0x14536EFB), /* ~= 10^150 */ - U64(0xC38413CF, 0x25E2D70D), U64(0xFEF51385, 0x19684ABA), /* ~= 10^151 */ - U64(0xF46518C2, 0xEF5B8CD1), U64(0x7EB25866, 0x5FC25D69), /* ~= 10^152 */ - U64(0x98BF2F79, 0xD5993802), U64(0xEF2F773F, 0xFBD97A61), /* ~= 10^153 */ - U64(0xBEEEFB58, 0x4AFF8603), U64(0xAAFB550F, 0xFACFD8FA), /* ~= 10^154 */ - U64(0xEEAABA2E, 0x5DBF6784), U64(0x95BA2A53, 0xF983CF38), /* ~= 10^155 */ - U64(0x952AB45C, 0xFA97A0B2), U64(0xDD945A74, 0x7BF26183), /* ~= 10^156 */ - U64(0xBA756174, 0x393D88DF), U64(0x94F97111, 0x9AEEF9E4), /* ~= 10^157 */ - U64(0xE912B9D1, 0x478CEB17), U64(0x7A37CD56, 0x01AAB85D), /* ~= 10^158 */ - U64(0x91ABB422, 0xCCB812EE), U64(0xAC62E055, 0xC10AB33A), /* ~= 10^159 */ - U64(0xB616A12B, 0x7FE617AA), U64(0x577B986B, 0x314D6009), /* ~= 10^160 */ - U64(0xE39C4976, 0x5FDF9D94), U64(0xED5A7E85, 0xFDA0B80B), /* ~= 10^161 */ - U64(0x8E41ADE9, 0xFBEBC27D), U64(0x14588F13, 0xBE847307), /* ~= 10^162 */ - U64(0xB1D21964, 0x7AE6B31C), U64(0x596EB2D8, 0xAE258FC8), /* ~= 10^163 */ - U64(0xDE469FBD, 0x99A05FE3), U64(0x6FCA5F8E, 0xD9AEF3BB), /* ~= 10^164 */ - U64(0x8AEC23D6, 0x80043BEE), U64(0x25DE7BB9, 0x480D5854), /* ~= 10^165 */ - U64(0xADA72CCC, 0x20054AE9), U64(0xAF561AA7, 0x9A10AE6A), /* ~= 10^166 */ - U64(0xD910F7FF, 0x28069DA4), U64(0x1B2BA151, 0x8094DA04), /* ~= 10^167 */ - U64(0x87AA9AFF, 0x79042286), U64(0x90FB44D2, 0xF05D0842), /* ~= 10^168 */ - U64(0xA99541BF, 0x57452B28), U64(0x353A1607, 0xAC744A53), /* ~= 10^169 */ - U64(0xD3FA922F, 0x2D1675F2), U64(0x42889B89, 0x97915CE8), /* ~= 10^170 */ - U64(0x847C9B5D, 0x7C2E09B7), U64(0x69956135, 0xFEBADA11), /* ~= 10^171 */ - U64(0xA59BC234, 0xDB398C25), U64(0x43FAB983, 0x7E699095), /* ~= 10^172 */ - U64(0xCF02B2C2, 0x1207EF2E), U64(0x94F967E4, 0x5E03F4BB), /* ~= 10^173 */ - U64(0x8161AFB9, 0x4B44F57D), U64(0x1D1BE0EE, 0xBAC278F5), /* ~= 10^174 */ - U64(0xA1BA1BA7, 0x9E1632DC), U64(0x6462D92A, 0x69731732), /* ~= 10^175 */ - U64(0xCA28A291, 0x859BBF93), U64(0x7D7B8F75, 0x03CFDCFE), /* ~= 10^176 */ - U64(0xFCB2CB35, 0xE702AF78), U64(0x5CDA7352, 0x44C3D43E), /* ~= 10^177 */ - U64(0x9DEFBF01, 0xB061ADAB), U64(0x3A088813, 0x6AFA64A7), /* ~= 10^178 */ - U64(0xC56BAEC2, 0x1C7A1916), U64(0x088AAA18, 0x45B8FDD0), /* ~= 10^179 */ - U64(0xF6C69A72, 0xA3989F5B), U64(0x8AAD549E, 0x57273D45), /* ~= 10^180 */ - U64(0x9A3C2087, 0xA63F6399), U64(0x36AC54E2, 0xF678864B), /* ~= 10^181 */ - U64(0xC0CB28A9, 0x8FCF3C7F), U64(0x84576A1B, 0xB416A7DD), /* ~= 10^182 */ - U64(0xF0FDF2D3, 0xF3C30B9F), U64(0x656D44A2, 0xA11C51D5), /* ~= 10^183 */ - U64(0x969EB7C4, 0x7859E743), U64(0x9F644AE5, 0xA4B1B325), /* ~= 10^184 */ - U64(0xBC4665B5, 0x96706114), U64(0x873D5D9F, 0x0DDE1FEE), /* ~= 10^185 */ - U64(0xEB57FF22, 0xFC0C7959), U64(0xA90CB506, 0xD155A7EA), /* ~= 10^186 */ - U64(0x9316FF75, 0xDD87CBD8), U64(0x09A7F124, 0x42D588F2), /* ~= 10^187 */ - U64(0xB7DCBF53, 0x54E9BECE), U64(0x0C11ED6D, 0x538AEB2F), /* ~= 10^188 */ - U64(0xE5D3EF28, 0x2A242E81), U64(0x8F1668C8, 0xA86DA5FA), /* ~= 10^189 */ - U64(0x8FA47579, 0x1A569D10), U64(0xF96E017D, 0x694487BC), /* ~= 10^190 */ - U64(0xB38D92D7, 0x60EC4455), U64(0x37C981DC, 0xC395A9AC), /* ~= 10^191 */ - U64(0xE070F78D, 0x3927556A), U64(0x85BBE253, 0xF47B1417), /* ~= 10^192 */ - U64(0x8C469AB8, 0x43B89562), U64(0x93956D74, 0x78CCEC8E), /* ~= 10^193 */ - U64(0xAF584166, 0x54A6BABB), U64(0x387AC8D1, 0x970027B2), /* ~= 10^194 */ - U64(0xDB2E51BF, 0xE9D0696A), U64(0x06997B05, 0xFCC0319E), /* ~= 10^195 */ - U64(0x88FCF317, 0xF22241E2), U64(0x441FECE3, 0xBDF81F03), /* ~= 10^196 */ - U64(0xAB3C2FDD, 0xEEAAD25A), U64(0xD527E81C, 0xAD7626C3), /* ~= 10^197 */ - U64(0xD60B3BD5, 0x6A5586F1), U64(0x8A71E223, 0xD8D3B074), /* ~= 10^198 */ - U64(0x85C70565, 0x62757456), U64(0xF6872D56, 0x67844E49), /* ~= 10^199 */ - U64(0xA738C6BE, 0xBB12D16C), U64(0xB428F8AC, 0x016561DB), /* ~= 10^200 */ - U64(0xD106F86E, 0x69D785C7), U64(0xE13336D7, 0x01BEBA52), /* ~= 10^201 */ - U64(0x82A45B45, 0x0226B39C), U64(0xECC00246, 0x61173473), /* ~= 10^202 */ - U64(0xA34D7216, 0x42B06084), U64(0x27F002D7, 0xF95D0190), /* ~= 10^203 */ - U64(0xCC20CE9B, 0xD35C78A5), U64(0x31EC038D, 0xF7B441F4), /* ~= 10^204 */ - U64(0xFF290242, 0xC83396CE), U64(0x7E670471, 0x75A15271), /* ~= 10^205 */ - U64(0x9F79A169, 0xBD203E41), U64(0x0F0062C6, 0xE984D386), /* ~= 10^206 */ - U64(0xC75809C4, 0x2C684DD1), U64(0x52C07B78, 0xA3E60868), /* ~= 10^207 */ - U64(0xF92E0C35, 0x37826145), U64(0xA7709A56, 0xCCDF8A82), /* ~= 10^208 */ - U64(0x9BBCC7A1, 0x42B17CCB), U64(0x88A66076, 0x400BB691), /* ~= 10^209 */ - U64(0xC2ABF989, 0x935DDBFE), U64(0x6ACFF893, 0xD00EA435), /* ~= 10^210 */ - U64(0xF356F7EB, 0xF83552FE), U64(0x0583F6B8, 0xC4124D43), /* ~= 10^211 */ - U64(0x98165AF3, 0x7B2153DE), U64(0xC3727A33, 0x7A8B704A), /* ~= 10^212 */ - U64(0xBE1BF1B0, 0x59E9A8D6), U64(0x744F18C0, 0x592E4C5C), /* ~= 10^213 */ - U64(0xEDA2EE1C, 0x7064130C), U64(0x1162DEF0, 0x6F79DF73), /* ~= 10^214 */ - U64(0x9485D4D1, 0xC63E8BE7), U64(0x8ADDCB56, 0x45AC2BA8), /* ~= 10^215 */ - U64(0xB9A74A06, 0x37CE2EE1), U64(0x6D953E2B, 0xD7173692), /* ~= 10^216 */ - U64(0xE8111C87, 0xC5C1BA99), U64(0xC8FA8DB6, 0xCCDD0437), /* ~= 10^217 */ - U64(0x910AB1D4, 0xDB9914A0), U64(0x1D9C9892, 0x400A22A2), /* ~= 10^218 */ - U64(0xB54D5E4A, 0x127F59C8), U64(0x2503BEB6, 0xD00CAB4B), /* ~= 10^219 */ - U64(0xE2A0B5DC, 0x971F303A), U64(0x2E44AE64, 0x840FD61D), /* ~= 10^220 */ - U64(0x8DA471A9, 0xDE737E24), U64(0x5CEAECFE, 0xD289E5D2), /* ~= 10^221 */ - U64(0xB10D8E14, 0x56105DAD), U64(0x7425A83E, 0x872C5F47), /* ~= 10^222 */ - U64(0xDD50F199, 0x6B947518), U64(0xD12F124E, 0x28F77719), /* ~= 10^223 */ - U64(0x8A5296FF, 0xE33CC92F), U64(0x82BD6B70, 0xD99AAA6F), /* ~= 10^224 */ - U64(0xACE73CBF, 0xDC0BFB7B), U64(0x636CC64D, 0x1001550B), /* ~= 10^225 */ - U64(0xD8210BEF, 0xD30EFA5A), U64(0x3C47F7E0, 0x5401AA4E), /* ~= 10^226 */ - U64(0x8714A775, 0xE3E95C78), U64(0x65ACFAEC, 0x34810A71), /* ~= 10^227 */ - U64(0xA8D9D153, 0x5CE3B396), U64(0x7F1839A7, 0x41A14D0D), /* ~= 10^228 */ - U64(0xD31045A8, 0x341CA07C), U64(0x1EDE4811, 0x1209A050), /* ~= 10^229 */ - U64(0x83EA2B89, 0x2091E44D), U64(0x934AED0A, 0xAB460432), /* ~= 10^230 */ - U64(0xA4E4B66B, 0x68B65D60), U64(0xF81DA84D, 0x5617853F), /* ~= 10^231 */ - U64(0xCE1DE406, 0x42E3F4B9), U64(0x36251260, 0xAB9D668E), /* ~= 10^232 */ - U64(0x80D2AE83, 0xE9CE78F3), U64(0xC1D72B7C, 0x6B426019), /* ~= 10^233 */ - U64(0xA1075A24, 0xE4421730), U64(0xB24CF65B, 0x8612F81F), /* ~= 10^234 */ - U64(0xC94930AE, 0x1D529CFC), U64(0xDEE033F2, 0x6797B627), /* ~= 10^235 */ - U64(0xFB9B7CD9, 0xA4A7443C), U64(0x169840EF, 0x017DA3B1), /* ~= 10^236 */ - U64(0x9D412E08, 0x06E88AA5), U64(0x8E1F2895, 0x60EE864E), /* ~= 10^237 */ - U64(0xC491798A, 0x08A2AD4E), U64(0xF1A6F2BA, 0xB92A27E2), /* ~= 10^238 */ - U64(0xF5B5D7EC, 0x8ACB58A2), U64(0xAE10AF69, 0x6774B1DB), /* ~= 10^239 */ - U64(0x9991A6F3, 0xD6BF1765), U64(0xACCA6DA1, 0xE0A8EF29), /* ~= 10^240 */ - U64(0xBFF610B0, 0xCC6EDD3F), U64(0x17FD090A, 0x58D32AF3), /* ~= 10^241 */ - U64(0xEFF394DC, 0xFF8A948E), U64(0xDDFC4B4C, 0xEF07F5B0), /* ~= 10^242 */ - U64(0x95F83D0A, 0x1FB69CD9), U64(0x4ABDAF10, 0x1564F98E), /* ~= 10^243 */ - U64(0xBB764C4C, 0xA7A4440F), U64(0x9D6D1AD4, 0x1ABE37F1), /* ~= 10^244 */ - U64(0xEA53DF5F, 0xD18D5513), U64(0x84C86189, 0x216DC5ED), /* ~= 10^245 */ - U64(0x92746B9B, 0xE2F8552C), U64(0x32FD3CF5, 0xB4E49BB4), /* ~= 10^246 */ - U64(0xB7118682, 0xDBB66A77), U64(0x3FBC8C33, 0x221DC2A1), /* ~= 10^247 */ - U64(0xE4D5E823, 0x92A40515), U64(0x0FABAF3F, 0xEAA5334A), /* ~= 10^248 */ - U64(0x8F05B116, 0x3BA6832D), U64(0x29CB4D87, 0xF2A7400E), /* ~= 10^249 */ - U64(0xB2C71D5B, 0xCA9023F8), U64(0x743E20E9, 0xEF511012), /* ~= 10^250 */ - U64(0xDF78E4B2, 0xBD342CF6), U64(0x914DA924, 0x6B255416), /* ~= 10^251 */ - U64(0x8BAB8EEF, 0xB6409C1A), U64(0x1AD089B6, 0xC2F7548E), /* ~= 10^252 */ - U64(0xAE9672AB, 0xA3D0C320), U64(0xA184AC24, 0x73B529B1), /* ~= 10^253 */ - U64(0xDA3C0F56, 0x8CC4F3E8), U64(0xC9E5D72D, 0x90A2741E), /* ~= 10^254 */ - U64(0x88658996, 0x17FB1871), U64(0x7E2FA67C, 0x7A658892), /* ~= 10^255 */ - U64(0xAA7EEBFB, 0x9DF9DE8D), U64(0xDDBB901B, 0x98FEEAB7), /* ~= 10^256 */ - U64(0xD51EA6FA, 0x85785631), U64(0x552A7422, 0x7F3EA565), /* ~= 10^257 */ - U64(0x8533285C, 0x936B35DE), U64(0xD53A8895, 0x8F87275F), /* ~= 10^258 */ - U64(0xA67FF273, 0xB8460356), U64(0x8A892ABA, 0xF368F137), /* ~= 10^259 */ - U64(0xD01FEF10, 0xA657842C), U64(0x2D2B7569, 0xB0432D85), /* ~= 10^260 */ - U64(0x8213F56A, 0x67F6B29B), U64(0x9C3B2962, 0x0E29FC73), /* ~= 10^261 */ - U64(0xA298F2C5, 0x01F45F42), U64(0x8349F3BA, 0x91B47B8F), /* ~= 10^262 */ - U64(0xCB3F2F76, 0x42717713), U64(0x241C70A9, 0x36219A73), /* ~= 10^263 */ - U64(0xFE0EFB53, 0xD30DD4D7), U64(0xED238CD3, 0x83AA0110), /* ~= 10^264 */ - U64(0x9EC95D14, 0x63E8A506), U64(0xF4363804, 0x324A40AA), /* ~= 10^265 */ - U64(0xC67BB459, 0x7CE2CE48), U64(0xB143C605, 0x3EDCD0D5), /* ~= 10^266 */ - U64(0xF81AA16F, 0xDC1B81DA), U64(0xDD94B786, 0x8E94050A), /* ~= 10^267 */ - U64(0x9B10A4E5, 0xE9913128), U64(0xCA7CF2B4, 0x191C8326), /* ~= 10^268 */ - U64(0xC1D4CE1F, 0x63F57D72), U64(0xFD1C2F61, 0x1F63A3F0), /* ~= 10^269 */ - U64(0xF24A01A7, 0x3CF2DCCF), U64(0xBC633B39, 0x673C8CEC), /* ~= 10^270 */ - U64(0x976E4108, 0x8617CA01), U64(0xD5BE0503, 0xE085D813), /* ~= 10^271 */ - U64(0xBD49D14A, 0xA79DBC82), U64(0x4B2D8644, 0xD8A74E18), /* ~= 10^272 */ - U64(0xEC9C459D, 0x51852BA2), U64(0xDDF8E7D6, 0x0ED1219E), /* ~= 10^273 */ - U64(0x93E1AB82, 0x52F33B45), U64(0xCABB90E5, 0xC942B503), /* ~= 10^274 */ - U64(0xB8DA1662, 0xE7B00A17), U64(0x3D6A751F, 0x3B936243), /* ~= 10^275 */ - U64(0xE7109BFB, 0xA19C0C9D), U64(0x0CC51267, 0x0A783AD4), /* ~= 10^276 */ - U64(0x906A617D, 0x450187E2), U64(0x27FB2B80, 0x668B24C5), /* ~= 10^277 */ - U64(0xB484F9DC, 0x9641E9DA), U64(0xB1F9F660, 0x802DEDF6), /* ~= 10^278 */ - U64(0xE1A63853, 0xBBD26451), U64(0x5E7873F8, 0xA0396973), /* ~= 10^279 */ - U64(0x8D07E334, 0x55637EB2), U64(0xDB0B487B, 0x6423E1E8), /* ~= 10^280 */ - U64(0xB049DC01, 0x6ABC5E5F), U64(0x91CE1A9A, 0x3D2CDA62), /* ~= 10^281 */ - U64(0xDC5C5301, 0xC56B75F7), U64(0x7641A140, 0xCC7810FB), /* ~= 10^282 */ - U64(0x89B9B3E1, 0x1B6329BA), U64(0xA9E904C8, 0x7FCB0A9D), /* ~= 10^283 */ - U64(0xAC2820D9, 0x623BF429), U64(0x546345FA, 0x9FBDCD44), /* ~= 10^284 */ - U64(0xD732290F, 0xBACAF133), U64(0xA97C1779, 0x47AD4095), /* ~= 10^285 */ - U64(0x867F59A9, 0xD4BED6C0), U64(0x49ED8EAB, 0xCCCC485D), /* ~= 10^286 */ - U64(0xA81F3014, 0x49EE8C70), U64(0x5C68F256, 0xBFFF5A74), /* ~= 10^287 */ - U64(0xD226FC19, 0x5C6A2F8C), U64(0x73832EEC, 0x6FFF3111), /* ~= 10^288 */ - U64(0x83585D8F, 0xD9C25DB7), U64(0xC831FD53, 0xC5FF7EAB), /* ~= 10^289 */ - U64(0xA42E74F3, 0xD032F525), U64(0xBA3E7CA8, 0xB77F5E55), /* ~= 10^290 */ - U64(0xCD3A1230, 0xC43FB26F), U64(0x28CE1BD2, 0xE55F35EB), /* ~= 10^291 */ - U64(0x80444B5E, 0x7AA7CF85), U64(0x7980D163, 0xCF5B81B3), /* ~= 10^292 */ - U64(0xA0555E36, 0x1951C366), U64(0xD7E105BC, 0xC332621F), /* ~= 10^293 */ - U64(0xC86AB5C3, 0x9FA63440), U64(0x8DD9472B, 0xF3FEFAA7), /* ~= 10^294 */ - U64(0xFA856334, 0x878FC150), U64(0xB14F98F6, 0xF0FEB951), /* ~= 10^295 */ - U64(0x9C935E00, 0xD4B9D8D2), U64(0x6ED1BF9A, 0x569F33D3), /* ~= 10^296 */ - U64(0xC3B83581, 0x09E84F07), U64(0x0A862F80, 0xEC4700C8), /* ~= 10^297 */ - U64(0xF4A642E1, 0x4C6262C8), U64(0xCD27BB61, 0x2758C0FA), /* ~= 10^298 */ - U64(0x98E7E9CC, 0xCFBD7DBD), U64(0x8038D51C, 0xB897789C), /* ~= 10^299 */ - U64(0xBF21E440, 0x03ACDD2C), U64(0xE0470A63, 0xE6BD56C3), /* ~= 10^300 */ - U64(0xEEEA5D50, 0x04981478), U64(0x1858CCFC, 0xE06CAC74), /* ~= 10^301 */ - U64(0x95527A52, 0x02DF0CCB), U64(0x0F37801E, 0x0C43EBC8), /* ~= 10^302 */ - U64(0xBAA718E6, 0x8396CFFD), U64(0xD3056025, 0x8F54E6BA), /* ~= 10^303 */ - U64(0xE950DF20, 0x247C83FD), U64(0x47C6B82E, 0xF32A2069), /* ~= 10^304 */ - U64(0x91D28B74, 0x16CDD27E), U64(0x4CDC331D, 0x57FA5441), /* ~= 10^305 */ - U64(0xB6472E51, 0x1C81471D), U64(0xE0133FE4, 0xADF8E952), /* ~= 10^306 */ - U64(0xE3D8F9E5, 0x63A198E5), U64(0x58180FDD, 0xD97723A6), /* ~= 10^307 */ - U64(0x8E679C2F, 0x5E44FF8F), U64(0x570F09EA, 0xA7EA7648), /* ~= 10^308 */ - U64(0xB201833B, 0x35D63F73), U64(0x2CD2CC65, 0x51E513DA), /* ~= 10^309 */ - U64(0xDE81E40A, 0x034BCF4F), U64(0xF8077F7E, 0xA65E58D1), /* ~= 10^310 */ - U64(0x8B112E86, 0x420F6191), U64(0xFB04AFAF, 0x27FAF782), /* ~= 10^311 */ - U64(0xADD57A27, 0xD29339F6), U64(0x79C5DB9A, 0xF1F9B563), /* ~= 10^312 */ - U64(0xD94AD8B1, 0xC7380874), U64(0x18375281, 0xAE7822BC), /* ~= 10^313 */ - U64(0x87CEC76F, 0x1C830548), U64(0x8F229391, 0x0D0B15B5), /* ~= 10^314 */ - U64(0xA9C2794A, 0xE3A3C69A), U64(0xB2EB3875, 0x504DDB22), /* ~= 10^315 */ - U64(0xD433179D, 0x9C8CB841), U64(0x5FA60692, 0xA46151EB), /* ~= 10^316 */ - U64(0x849FEEC2, 0x81D7F328), U64(0xDBC7C41B, 0xA6BCD333), /* ~= 10^317 */ - U64(0xA5C7EA73, 0x224DEFF3), U64(0x12B9B522, 0x906C0800), /* ~= 10^318 */ - U64(0xCF39E50F, 0xEAE16BEF), U64(0xD768226B, 0x34870A00), /* ~= 10^319 */ - U64(0x81842F29, 0xF2CCE375), U64(0xE6A11583, 0x00D46640), /* ~= 10^320 */ - U64(0xA1E53AF4, 0x6F801C53), U64(0x60495AE3, 0xC1097FD0), /* ~= 10^321 */ - U64(0xCA5E89B1, 0x8B602368), U64(0x385BB19C, 0xB14BDFC4), /* ~= 10^322 */ - U64(0xFCF62C1D, 0xEE382C42), U64(0x46729E03, 0xDD9ED7B5), /* ~= 10^323 */ - U64(0x9E19DB92, 0xB4E31BA9), U64(0x6C07A2C2, 0x6A8346D1) /* ~= 10^324 */ -}; - -/** - Get the cached pow10 value from pow10_sig_table. - @param exp10 The exponent of pow(10, e). This value must in range - POW10_SIG_TABLE_MIN_EXP to POW10_SIG_TABLE_MAX_EXP. - @param hi The highest 64 bits of pow(10, e). - @param lo The lower 64 bits after `hi`. - */ -static_inline void pow10_table_get_sig(i32 exp10, u64 *hi, u64 *lo) { - i32 idx = exp10 - (POW10_SIG_TABLE_MIN_EXP); - *hi = pow10_sig_table[idx * 2]; - *lo = pow10_sig_table[idx * 2 + 1]; -} - -/** - Get the exponent (base 2) for highest 64 bits significand in pow10_sig_table. - */ -static_inline void pow10_table_get_exp(i32 exp10, i32 *exp2) { - /* e2 = floor(log2(pow(10, e))) - 64 + 1 */ - /* = floor(e * log2(10) - 63) */ - *exp2 = (exp10 * 217706 - 4128768) >> 16; -} - -#endif - - - -/*============================================================================== - * JSON Character Matcher - *============================================================================*/ - -/** Character type */ -typedef u8 char_type; - -/** Whitespace character: ' ', '\\t', '\\n', '\\r'. */ -static const char_type CHAR_TYPE_SPACE = 1 << 0; - -/** Number character: '-', [0-9]. */ -static const char_type CHAR_TYPE_NUMBER = 1 << 1; - -/** JSON Escaped character: '"', '\', [0x00-0x1F]. */ -static const char_type CHAR_TYPE_ESC_ASCII = 1 << 2; - -/** Non-ASCII character: [0x80-0xFF]. */ -static const char_type CHAR_TYPE_NON_ASCII = 1 << 3; - -/** JSON container character: '{', '['. */ -static const char_type CHAR_TYPE_CONTAINER = 1 << 4; - -/** Comment character: '/'. */ -static const char_type CHAR_TYPE_COMMENT = 1 << 5; - -/** Line end character: '\\n', '\\r', '\0'. */ -static const char_type CHAR_TYPE_LINE_END = 1 << 6; - -/** Hexadecimal numeric character: [0-9a-fA-F]. */ -static const char_type CHAR_TYPE_HEX = 1 << 7; - -/** Character type table (generate with misc/make_tables.c) */ -static const char_type char_table[256] = { - 0x44, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x05, 0x45, 0x04, 0x04, 0x45, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, - 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, - 0x82, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00, - 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 -}; - -/** Match a character with specified type. */ -static_inline bool char_is_type(u8 c, char_type type) { - return (char_table[c] & type) != 0; -} - -/** Match a whitespace: ' ', '\\t', '\\n', '\\r'. */ -static_inline bool char_is_space(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_SPACE); -} - -/** Match a whitespace or comment: ' ', '\\t', '\\n', '\\r', '/'. */ -static_inline bool char_is_space_or_comment(u8 c) { - return char_is_type(c, (char_type)(CHAR_TYPE_SPACE | CHAR_TYPE_COMMENT)); -} - -/** Match a JSON number: '-', [0-9]. */ -static_inline bool char_is_number(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_NUMBER); -} - -/** Match a JSON container: '{', '['. */ -static_inline bool char_is_container(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_CONTAINER); -} - -/** Match a stop character in ASCII string: '"', '\', [0x00-0x1F,0x80-0xFF]. */ -static_inline bool char_is_ascii_stop(u8 c) { - return char_is_type(c, (char_type)(CHAR_TYPE_ESC_ASCII | - CHAR_TYPE_NON_ASCII)); -} - -/** Match a line end character: '\\n', '\\r', '\0'. */ -static_inline bool char_is_line_end(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_LINE_END); -} - -/** Match a hexadecimal numeric character: [0-9a-fA-F]. */ -static_inline bool char_is_hex(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_HEX); -} - - - -/*============================================================================== - * Digit Character Matcher - *============================================================================*/ - -/** Digit type */ -typedef u8 digi_type; - -/** Digit: '0'. */ -static const digi_type DIGI_TYPE_ZERO = 1 << 0; - -/** Digit: [1-9]. */ -static const digi_type DIGI_TYPE_NONZERO = 1 << 1; - -/** Plus sign (positive): '+'. */ -static const digi_type DIGI_TYPE_POS = 1 << 2; - -/** Minus sign (negative): '-'. */ -static const digi_type DIGI_TYPE_NEG = 1 << 3; - -/** Decimal point: '.' */ -static const digi_type DIGI_TYPE_DOT = 1 << 4; - -/** Exponent sign: 'e, 'E'. */ -static const digi_type DIGI_TYPE_EXP = 1 << 5; - -/** Digit type table (generate with misc/make_tables.c) */ -static const digi_type digi_table[256] = { - 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, 0x04, 0x00, 0x08, 0x10, 0x00, - 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, - 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 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, 0x20, 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 -}; - -/** Match a character with specified type. */ -static_inline bool digi_is_type(u8 d, digi_type type) { - return (digi_table[d] & type) != 0; -} - -/** Match a sign: '+', '-' */ -static_inline bool digi_is_sign(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_POS | DIGI_TYPE_NEG)); -} - -/** Match a none zero digit: [1-9] */ -static_inline bool digi_is_nonzero(u8 d) { - return digi_is_type(d, (digi_type)DIGI_TYPE_NONZERO); -} - -/** Match a digit: [0-9] */ -static_inline bool digi_is_digit(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO)); -} - -/** Match an exponent sign: 'e', 'E'. */ -static_inline bool digi_is_exp(u8 d) { - return digi_is_type(d, (digi_type)DIGI_TYPE_EXP); -} - -/** Match a floating point indicator: '.', 'e', 'E'. */ -static_inline bool digi_is_fp(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_DOT | DIGI_TYPE_EXP)); -} - -/** Match a digit or floating point indicator: [0-9], '.', 'e', 'E'. */ -static_inline bool digi_is_digit_or_fp(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO | - DIGI_TYPE_DOT | DIGI_TYPE_EXP)); -} - - - -#if !YYJSON_DISABLE_READER - -/*============================================================================== - * Hex Character Reader - * This function is used by JSON reader to read escaped characters. - *============================================================================*/ - -/** - This table is used to convert 4 hex character sequence to a number. - A valid hex character [0-9A-Fa-f] will mapped to it's raw number [0x00, 0x0F], - an invalid hex character will mapped to [0xF0]. - (generate with misc/make_tables.c) - */ -static const u8 hex_conv_table[256] = { - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 -}; - -/** - Scans an escaped character sequence as a UTF-16 code unit (branchless). - e.g. "\\u005C" should pass "005C" as `cur`. - - This requires the string has 4-byte zero padding. - */ -static_inline bool read_hex_u16(const u8 *cur, u16 *val) { - u16 c0, c1, c2, c3, t0, t1; - c0 = hex_conv_table[cur[0]]; - c1 = hex_conv_table[cur[1]]; - c2 = hex_conv_table[cur[2]]; - c3 = hex_conv_table[cur[3]]; - t0 = (u16)((c0 << 8) | c2); - t1 = (u16)((c1 << 8) | c3); - *val = (u16)((t0 << 4) | t1); - return ((t0 | t1) & (u16)0xF0F0) == 0; -} - - - -/*============================================================================== - * JSON Reader Utils - * These functions are used by JSON reader to read literals and comments. - *============================================================================*/ - -/** Read 'true' literal, '*cur' should be 't'. */ -static_inline bool read_true(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur, "true"))) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; - *end = cur + 4; - return true; - } - return false; -} - -/** Read 'false' literal, '*cur' should be 'f'. */ -static_inline bool read_false(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur + 1, "alse"))) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; - *end = cur + 5; - return true; - } - return false; -} - -/** Read 'null' literal, '*cur' should be 'n'. */ -static_inline bool read_null(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur, "null"))) { - val->tag = YYJSON_TYPE_NULL; - *end = cur + 4; - return true; - } - return false; -} - -/** Read 'Inf' or 'Infinity' literal (ignoring case). */ -static_inline bool read_inf(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) { - u8 *hdr = *ptr - sign; - u8 *cur = *ptr; - u8 **end = ptr; - if ((cur[0] == 'I' || cur[0] == 'i') && - (cur[1] == 'N' || cur[1] == 'n') && - (cur[2] == 'F' || cur[2] == 'f')) { - if ((cur[3] == 'I' || cur[3] == 'i') && - (cur[4] == 'N' || cur[4] == 'n') && - (cur[5] == 'I' || cur[5] == 'i') && - (cur[6] == 'T' || cur[6] == 't') && - (cur[7] == 'Y' || cur[7] == 'y')) { - cur += 8; - } else { - cur += 3; - } - *end = cur; - if (false) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = cur; - val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; - val->uni.str = (const char *)hdr; - } else { - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.u64 = f64_raw_get_inf(sign); - } - return true; - } - return false; -} - -/** Read 'NaN' literal (ignoring case). */ -static_inline bool read_nan(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) { - u8 *hdr = *ptr - sign; - u8 *cur = *ptr; - u8 **end = ptr; - if ((cur[0] == 'N' || cur[0] == 'n') && - (cur[1] == 'A' || cur[1] == 'a') && - (cur[2] == 'N' || cur[2] == 'n')) { - cur += 3; - *end = cur; - if (false) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = cur; - val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; - val->uni.str = (const char *)hdr; - } else { - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.u64 = f64_raw_get_nan(sign); - } - return true; - } - return false; -} - -/** Read a JSON number as raw string. */ -static_noinline bool read_number_raw(u8 **ptr, - u8 **pre, - yyjson_read_flag flg, - yyjson_val *val, - const char **msg) { - -#define return_err(_pos, _msg) do { \ - *msg = _msg; \ - *end = _pos; \ - return false; \ -} while (false) - -#define return_raw() do { \ - val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ - val->uni.str = (const char *)hdr; \ - *pre = cur; *end = cur; return true; \ -} while (false) - - u8 *hdr = *ptr; - u8 *cur = *ptr; - u8 **end = ptr; - - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - - /* skip sign */ - cur += (*cur == '-'); - - /* read first digit, check leading zero */ - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after minus sign"); - } - - /* read integral part */ - if (*cur == '0') { - cur++; - if (unlikely(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (!digi_is_fp(*cur)) return_raw(); - } else { - while (digi_is_digit(*cur)) cur++; - if (!digi_is_fp(*cur)) return_raw(); - } - - /* read fraction part */ - if (*cur == '.') { - cur++; - if (!digi_is_digit(*cur++)) { - return_err(cur, "no digit after decimal point"); - } - while (digi_is_digit(*cur)) cur++; - } - - /* read exponent part */ - if (digi_is_exp(*cur)) { - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur++)) { - return_err(cur, "no digit after exponent sign"); - } - while (digi_is_digit(*cur)) cur++; - } - - return_raw(); - -#undef return_err -#undef return_raw -} - -/** - Skips spaces and comments as many as possible. - - It will return false in these cases: - 1. No character is skipped. The 'end' pointer is set as input cursor. - 2. A multiline comment is not closed. The 'end' pointer is set as the head - of this comment block. - */ -static_noinline bool skip_spaces_and_comments(u8 **ptr) { - u8 *hdr = *ptr; - u8 *cur = *ptr; - u8 **end = ptr; - while (true) { - if (byte_match_2(cur, "/*")) { - hdr = cur; - cur += 2; - while (true) { - if (byte_match_2(cur, "*/")) { - cur += 2; - break; - } - if (*cur == 0) { - *end = hdr; - return false; - } - cur++; - } - continue; - } - if (byte_match_2(cur, "//")) { - cur += 2; - while (!char_is_line_end(*cur)) cur++; - continue; - } - if (char_is_space(*cur)) { - cur += 1; - while (char_is_space(*cur)) cur++; - continue; - } - break; - } - *end = cur; - return hdr != cur; -} - -/** - Check truncated string. - Returns true if `cur` match `str` but is truncated. - */ -static_inline bool is_truncated_str(u8 *cur, u8 *end, - const char *str, - bool case_sensitive) { - usize len = strlen(str); - if (cur + len <= end || end <= cur) return false; - if (case_sensitive) { - return memcmp(cur, str, (usize)(end - cur)) == 0; - } - for (; cur < end; cur++, str++) { - if ((*cur != (u8)*str) && (*cur != (u8)*str - 'a' + 'A')) { - return false; - } - } - return true; -} - -/** - Check truncated JSON on parsing errors. - Returns true if the input is valid but truncated. - */ -static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *end, - yyjson_read_code code) { - if (cur >= end) return true; - if (code == YYJSON_READ_ERROR_LITERAL) { - if (is_truncated_str(cur, end, "true", true) || - is_truncated_str(cur, end, "false", true) || - is_truncated_str(cur, end, "null", true)) { - return true; - } - } - if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER || - code == YYJSON_READ_ERROR_INVALID_NUMBER || - code == YYJSON_READ_ERROR_LITERAL) { - if (false) { - if (*cur == '-') cur++; - if (is_truncated_str(cur, end, "infinity", false) || - is_truncated_str(cur, end, "nan", false)) { - return true; - } - } - } - if (code == YYJSON_READ_ERROR_UNEXPECTED_CONTENT) { - if (false) { - if (hdr + 3 <= cur && - is_truncated_str(cur - 3, end, "infinity", false)) { - return true; /* e.g. infin would be read as inf + in */ - } - } - } - if (code == YYJSON_READ_ERROR_INVALID_STRING) { - usize len = (usize)(end - cur); - - /* unicode escape sequence */ - if (*cur == '\\') { - if (len == 1) return true; - if (len <= 5) { - if (*++cur != 'u') return false; - for (++cur; cur < end; cur++) { - if (!char_is_hex(*cur)) return false; - } - return true; - } - return false; - } - - /* 2 to 4 bytes UTF-8, see `read_string()` for details. */ - if (*cur & 0x80) { - u8 c0 = cur[0], c1 = cur[1], c2 = cur[2]; - if (len == 1) { - /* 2 bytes UTF-8, truncated */ - if ((c0 & 0xE0) == 0xC0 && (c0 & 0x1E) != 0x00) return true; - /* 3 bytes UTF-8, truncated */ - if ((c0 & 0xF0) == 0xE0) return true; - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && (c0 & 0x07) <= 0x04) return true; - } - if (len == 2) { - /* 3 bytes UTF-8, truncated */ - if ((c0 & 0xF0) == 0xE0 && - (c1 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x0F) << 1) | ((c1 & 0x20) >> 5)); - return 0x01 <= pat && pat != 0x1B; - } - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && - (c1 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); - return 0x01 <= pat && pat <= 0x10; - } - } - if (len == 3) { - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && - (c1 & 0xC0) == 0x80 && - (c2 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); - return 0x01 <= pat && pat <= 0x10; - } - } - } - } - return false; -} - - - -#if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */ - -/*============================================================================== - * BigInt For Floating Point Number Reader - * - * The bigint algorithm is used by floating-point number reader to get correctly - * rounded result for numbers with lots of digits. This part of code is rarely - * used for common numbers. - *============================================================================*/ - -/** Maximum exponent of exact pow10 */ -#define U64_POW10_MAX_EXP 19 - -/** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */ -static const u64 u64_pow10_table[U64_POW10_MAX_EXP + 1] = { - U64(0x00000000, 0x00000001), U64(0x00000000, 0x0000000A), - U64(0x00000000, 0x00000064), U64(0x00000000, 0x000003E8), - U64(0x00000000, 0x00002710), U64(0x00000000, 0x000186A0), - U64(0x00000000, 0x000F4240), U64(0x00000000, 0x00989680), - U64(0x00000000, 0x05F5E100), U64(0x00000000, 0x3B9ACA00), - U64(0x00000002, 0x540BE400), U64(0x00000017, 0x4876E800), - U64(0x000000E8, 0xD4A51000), U64(0x00000918, 0x4E72A000), - U64(0x00005AF3, 0x107A4000), U64(0x00038D7E, 0xA4C68000), - U64(0x002386F2, 0x6FC10000), U64(0x01634578, 0x5D8A0000), - U64(0x0DE0B6B3, 0xA7640000), U64(0x8AC72304, 0x89E80000) -}; - -/** Maximum numbers of chunks used by a bigint (58 is enough here). */ -#define BIGINT_MAX_CHUNKS 64 - -/** Unsigned arbitrarily large integer */ -typedef struct bigint { - u32 used; /* used chunks count, should not be 0 */ - u64 bits[BIGINT_MAX_CHUNKS]; /* chunks */ -} bigint; - -/** - Evaluate 'big += val'. - @param big A big number (can be 0). - @param val An unsigned integer (can be 0). - */ -static_inline void bigint_add_u64(bigint *big, u64 val) { - u32 idx, max; - u64 num = big->bits[0]; - u64 add = num + val; - big->bits[0] = add; - if (likely((add >= num) || (add >= val))) return; - for ((void)(idx = 1), max = big->used; idx < max; idx++) { - if (likely(big->bits[idx] != U64_MAX)) { - big->bits[idx] += 1; - return; - } - big->bits[idx] = 0; - } - big->bits[big->used++] = 1; -} - -/** - Evaluate 'big *= val'. - @param big A big number (can be 0). - @param val An unsigned integer (cannot be 0). - */ -static_inline void bigint_mul_u64(bigint *big, u64 val) { - u32 idx = 0, max = big->used; - u64 hi, lo, carry = 0; - for (; idx < max; idx++) { - if (big->bits[idx]) break; - } - for (; idx < max; idx++) { - u128_mul_add(big->bits[idx], val, carry, &hi, &lo); - big->bits[idx] = lo; - carry = hi; - } - if (carry) big->bits[big->used++] = carry; -} - -/** - Evaluate 'big *= 2^exp'. - @param big A big number (can be 0). - @param exp An exponent integer (can be 0). - */ -static_inline void bigint_mul_pow2(bigint *big, u32 exp) { - u32 shft = exp % 64; - u32 move = exp / 64; - u32 idx = big->used; - if (unlikely(shft == 0)) { - for (; idx > 0; idx--) { - big->bits[idx + move - 1] = big->bits[idx - 1]; - } - big->used += move; - while (move) big->bits[--move] = 0; - } else { - big->bits[idx] = 0; - for (; idx > 0; idx--) { - u64 num = big->bits[idx] << shft; - num |= big->bits[idx - 1] >> (64 - shft); - big->bits[idx + move] = num; - } - big->bits[move] = big->bits[0] << shft; - big->used += move + (big->bits[big->used + move] > 0); - while (move) big->bits[--move] = 0; - } -} - -/** - Evaluate 'big *= 10^exp'. - @param big A big number (can be 0). - @param exp An exponent integer (cannot be 0). - */ -static_inline void bigint_mul_pow10(bigint *big, i32 exp) { - for (; exp >= U64_POW10_MAX_EXP; exp -= U64_POW10_MAX_EXP) { - bigint_mul_u64(big, u64_pow10_table[U64_POW10_MAX_EXP]); - } - if (exp) { - bigint_mul_u64(big, u64_pow10_table[exp]); - } -} - -/** - Compare two bigint. - @return -1 if 'a < b', +1 if 'a > b', 0 if 'a == b'. - */ -static_inline i32 bigint_cmp(bigint *a, bigint *b) { - u32 idx = a->used; - if (a->used < b->used) return -1; - if (a->used > b->used) return +1; - while (idx-- > 0) { - u64 av = a->bits[idx]; - u64 bv = b->bits[idx]; - if (av < bv) return -1; - if (av > bv) return +1; - } - return 0; -} - -/** - Evaluate 'big = val'. - @param big A big number (can be 0). - @param val An unsigned integer (can be 0). - */ -static_inline void bigint_set_u64(bigint *big, u64 val) { - big->used = 1; - big->bits[0] = val; -} - -/** Set a bigint with floating point number string. */ -static_noinline void bigint_set_buf(bigint *big, u64 sig, i32 *exp, - u8 *sig_cut, u8 *sig_end, u8 *dot_pos) { - - if (unlikely(!sig_cut)) { - /* no digit cut, set significant part only */ - bigint_set_u64(big, sig); - return; - - } else { - /* some digits were cut, read them from 'sig_cut' to 'sig_end' */ - u8 *hdr = sig_cut; - u8 *cur = hdr; - u32 len = 0; - u64 val = 0; - bool dig_big_cut = false; - bool has_dot = (hdr < dot_pos) & (dot_pos < sig_end); - u32 dig_len_total = U64_SAFE_DIG + (u32)(sig_end - hdr) - has_dot; - - sig -= (*sig_cut >= '5'); /* sig was rounded before */ - if (dig_len_total > F64_MAX_DEC_DIG) { - dig_big_cut = true; - sig_end -= dig_len_total - (F64_MAX_DEC_DIG + 1); - sig_end -= (dot_pos + 1 == sig_end); - dig_len_total = (F64_MAX_DEC_DIG + 1); - } - *exp -= (i32)dig_len_total - U64_SAFE_DIG; - - big->used = 1; - big->bits[0] = sig; - while (cur < sig_end) { - if (likely(cur != dot_pos)) { - val = val * 10 + (u8)(*cur++ - '0'); - len++; - if (unlikely(cur == sig_end && dig_big_cut)) { - /* The last digit must be non-zero, */ - /* set it to '1' for correct rounding. */ - val = val - (val % 10) + 1; - } - if (len == U64_SAFE_DIG || cur == sig_end) { - bigint_mul_pow10(big, (i32)len); - bigint_add_u64(big, val); - val = 0; - len = 0; - } - } else { - cur++; - } - } - } -} - - - -/*============================================================================== - * Diy Floating Point - *============================================================================*/ - -/** "Do It Yourself Floating Point" struct. */ -typedef struct diy_fp { - u64 sig; /* significand */ - i32 exp; /* exponent, base 2 */ - i32 pad; /* padding, useless */ -} diy_fp; - -/** Get cached rounded diy_fp with pow(10, e) The input value must in range - [POW10_SIG_TABLE_MIN_EXP, POW10_SIG_TABLE_MAX_EXP]. */ -static_inline diy_fp diy_fp_get_cached_pow10(i32 exp10) { - diy_fp fp; - u64 sig_ext; - pow10_table_get_sig(exp10, &fp.sig, &sig_ext); - pow10_table_get_exp(exp10, &fp.exp); - fp.sig += (sig_ext >> 63); - return fp; -} - -/** Returns fp * fp2. */ -static_inline diy_fp diy_fp_mul(diy_fp fp, diy_fp fp2) { - u64 hi, lo; - u128_mul(fp.sig, fp2.sig, &hi, &lo); - fp.sig = hi + (lo >> 63); - fp.exp += fp2.exp + 64; - return fp; -} - -/** Convert diy_fp to IEEE-754 raw value. */ -static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) { - u64 sig = fp.sig; - i32 exp = fp.exp; - u32 lz_bits; - if (unlikely(fp.sig == 0)) return 0; - - lz_bits = u64_lz_bits(sig); - sig <<= lz_bits; - sig >>= F64_BITS - F64_SIG_FULL_BITS; - exp -= (i32)lz_bits; - exp += F64_BITS - F64_SIG_FULL_BITS; - exp += F64_SIG_BITS; - - if (unlikely(exp >= F64_MAX_BIN_EXP)) { - /* overflow */ - return F64_RAW_INF; - } else if (likely(exp >= F64_MIN_BIN_EXP - 1)) { - /* normal */ - exp += F64_EXP_BIAS; - return ((u64)exp << F64_SIG_BITS) | (sig & F64_SIG_MASK); - } else if (likely(exp >= F64_MIN_BIN_EXP - F64_SIG_FULL_BITS)) { - /* subnormal */ - return sig >> (F64_MIN_BIN_EXP - exp - 1); - } else { - /* underflow */ - return 0; - } -} - - - -/*============================================================================== - * JSON Number Reader (IEEE-754) - *============================================================================*/ - -/** Maximum exact pow10 exponent for double value. */ -#define F64_POW10_EXP_MAX_EXACT 22 - -/** Cached pow10 table. */ -static const f64 f64_pow10_table[] = { - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, - 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 -}; - -/** - Read a JSON number. - - 1. This function assume that the floating-point number is in IEEE-754 format. - 2. This function support uint64/int64/double number. If an integer number - cannot fit in uint64/int64, it will returns as a double number. If a double - number is infinite, the return value is based on flag. - 3. This function (with inline attribute) may generate a lot of instructions. - */ -static_inline bool read_number(u8 **ptr, - yyjson_val *val, - const char **msg) { - -#define return_err(_pos, _msg) do { \ - *msg = _msg; \ - *end = _pos; \ - return false; \ -} while (false) - -#define return_0() do { \ - val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ - val->uni.u64 = 0; \ - *end = cur; return true; \ -} while (false) - -#define return_i64(_v) do { \ - val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ - val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ - *end = cur; return true; \ -} while (false) - -#define return_f64(_v) do { \ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ - val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ - *end = cur; return true; \ -} while (false) - -#define return_f64_bin(_v) do { \ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ - val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ - *end = cur; return true; \ -} while (false) - -#define return_inf() do { \ - if (false) return_f64_bin(F64_RAW_INF); \ - else return_err(hdr, "number is infinity when parsed as double"); \ -} while (false) - - u8 *sig_cut = NULL; /* significant part cutting position for long number */ - u8 *sig_end = NULL; /* significant part ending position */ - u8 *dot_pos = NULL; /* decimal point position */ - - u64 sig = 0; /* significant part of the number */ - i32 exp = 0; /* exponent part of the number */ - - bool exp_sign; /* temporary exponent sign from literal part */ - i64 exp_sig = 0; /* temporary exponent number from significant part */ - i64 exp_lit = 0; /* temporary exponent number from exponent literal part */ - u64 num; /* temporary number for reading */ - u8 *tmp; /* temporary cursor for reading */ - - u8 *hdr = *ptr; - u8 *cur = *ptr; - u8 **end = ptr; - bool sign; - - sign = (*hdr == '-'); - cur += sign; - - /* begin with a leading zero or non-digit */ - if (unlikely(!digi_is_nonzero(*cur))) { /* 0 or non-digit char */ - if (unlikely(*cur != '0')) { /* non-digit char */ - return_err(cur, "no digit after minus sign"); - } - /* begin with 0 */ - if (likely(!digi_is_digit_or_fp(*++cur))) return_0(); - if (likely(*cur == '.')) { - dot_pos = cur++; - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after decimal point"); - } - while (unlikely(*cur == '0')) cur++; - if (likely(digi_is_digit(*cur))) { - /* first non-zero digit after decimal point */ - sig = (u64)(*cur - '0'); /* read first digit */ - cur--; - goto digi_frac_1; /* continue read fraction part */ - } - } - if (unlikely(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (unlikely(digi_is_exp(*cur))) { /* 0 with any exponent is still 0 */ - cur += (usize)1 + digi_is_sign(cur[1]); - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after exponent sign"); - } - while (digi_is_digit(*++cur)); - } - return_f64_bin(0); - } - - /* begin with non-zero digit */ - sig = (u64)(*cur - '0'); - - /* - Read integral part, same as the following code. - - for (int i = 1; i <= 18; i++) { - num = cur[i] - '0'; - if (num <= 9) sig = num + sig * 10; - else goto digi_sepr_i; - } - */ -#define expr_intg(i) \ - if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ - else { goto digi_sepr_##i; } - repeat_in_1_18(expr_intg) -#undef expr_intg - - - cur += 19; /* skip continuous 19 digits */ - if (!digi_is_digit_or_fp(*cur)) { - /* this number is an integer consisting of 19 digits */ - if (sign && (sig > ((u64)1 << 63))) { /* overflow */ - return_f64(normalized_u64_to_f64(sig)); - } - return_i64(sig); - } - goto digi_intg_more; /* read more digits in integral part */ - - - /* process first non-digit character */ -#define expr_sepr(i) \ - digi_sepr_##i: \ - if (likely(!digi_is_fp(cur[i]))) { cur += i; return_i64(sig); } \ - dot_pos = cur + i; \ - if (likely(cur[i] == '.')) goto digi_frac_##i; \ - cur += i; sig_end = cur; goto digi_exp_more; - repeat_in_1_18(expr_sepr) -#undef expr_sepr - - - /* read fraction part */ -#define expr_frac(i) \ - digi_frac_##i: \ - if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \ - sig = num + sig * 10; \ - else { goto digi_stop_##i; } - repeat_in_1_18(expr_frac) -#undef expr_frac - - cur += 20; /* skip 19 digits and 1 decimal point */ - if (!digi_is_digit(*cur)) goto digi_frac_end; /* fraction part end */ - goto digi_frac_more; /* read more digits in fraction part */ - - - /* significant part end */ -#define expr_stop(i) \ - digi_stop_##i: \ - cur += i + 1; \ - goto digi_frac_end; - repeat_in_1_18(expr_stop) -#undef expr_stop - - - /* read more digits in integral part */ -digi_intg_more: - if (digi_is_digit(*cur)) { - if (!digi_is_digit_or_fp(cur[1])) { - /* this number is an integer consisting of 20 digits */ - num = (u64)(*cur - '0'); - if ((sig < (U64_MAX / 10)) || - (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { - sig = num + sig * 10; - cur++; - /* convert to double if overflow */ - if (sign) { - return_f64(normalized_u64_to_f64(sig)); - } - return_i64(sig); - } - } - } - - if (digi_is_exp(*cur)) { - dot_pos = cur; - goto digi_exp_more; - } - - if (*cur == '.') { - dot_pos = cur++; - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after decimal point"); - } - } - - - /* read more digits in fraction part */ -digi_frac_more: - sig_cut = cur; /* too large to fit in u64, excess digits need to be cut */ - sig += (*cur >= '5'); /* round */ - while (digi_is_digit(*++cur)); - if (!dot_pos) { - dot_pos = cur; - if (*cur == '.') { - if (!digi_is_digit(*++cur)) { - return_err(cur, "no digit after decimal point"); - } - while (digi_is_digit(*cur)) cur++; - } - } - exp_sig = (i64)(dot_pos - sig_cut); - exp_sig += (dot_pos < sig_cut); - - /* ignore trailing zeros */ - tmp = cur - 1; - while (*tmp == '0' || *tmp == '.') tmp--; - if (tmp < sig_cut) { - sig_cut = NULL; - } else { - sig_end = cur; - } - - if (digi_is_exp(*cur)) goto digi_exp_more; - goto digi_exp_finish; - - - /* fraction part end */ -digi_frac_end: - if (unlikely(dot_pos + 1 == cur)) { - return_err(cur, "no digit after decimal point"); - } - sig_end = cur; - exp_sig = -(i64)((u64)(cur - dot_pos) - 1); - if (likely(!digi_is_exp(*cur))) { - if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { - return_f64_bin(0); /* underflow */ - } - exp = (i32)exp_sig; - goto digi_finish; - } else { - goto digi_exp_more; - } - - - /* read exponent part */ -digi_exp_more: - exp_sign = (*++cur == '-'); - cur += digi_is_sign(*cur); - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after exponent sign"); - } - while (*cur == '0') cur++; - - /* read exponent literal */ - tmp = cur; - while (digi_is_digit(*cur)) { - exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10); - } - if (unlikely(cur - tmp >= U64_SAFE_DIG)) { - if (exp_sign) { - return_f64_bin(0); /* underflow */ - } else { - return_inf(); /* overflow */ - } - } - exp_sig += exp_sign ? -exp_lit : exp_lit; - - - /* validate exponent value */ -digi_exp_finish: - if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { - return_f64_bin(0); /* underflow */ - } - if (unlikely(exp_sig > F64_MAX_DEC_EXP)) { - return_inf(); /* overflow */ - } - exp = (i32)exp_sig; - - - /* all digit read finished */ -digi_finish: - - /* - Fast path 1: - - 1. The floating-point number calculation should be accurate, see the - comments of macro `YYJSON_DOUBLE_MATH_CORRECT`. - 2. Correct rounding should be performed (fegetround() == FE_TONEAREST). - 3. The input of floating point number calculation does not lose precision, - which means: 64 - leading_zero(input) - trailing_zero(input) < 53. - - We don't check all available inputs here, because that would make the code - more complicated, and not friendly to branch predictor. - */ -#if YYJSON_DOUBLE_MATH_CORRECT - if (sig < ((u64)1 << 53) && - exp >= -F64_POW10_EXP_MAX_EXACT && - exp <= +F64_POW10_EXP_MAX_EXACT) { - f64 dbl = (f64)sig; - if (exp < 0) { - dbl /= f64_pow10_table[-exp]; - } else { - dbl *= f64_pow10_table[+exp]; - } - return_f64(dbl); - } -#endif - - /* - Fast path 2: - - To keep it simple, we only accept normal number here, - let the slow path to handle subnormal and infinity number. - */ - if (likely(!sig_cut && - exp > -F64_MAX_DEC_EXP + 1 && - exp < +F64_MAX_DEC_EXP - 20)) { - /* - The result value is exactly equal to (sig * 10^exp), - the exponent part (10^exp) can be converted to (sig2 * 2^exp2). - - The sig2 can be an infinite length number, only the highest 128 bits - is cached in the pow10_sig_table. - - Now we have these bits: - sig1 (normalized 64bit) : aaaaaaaa - sig2 (higher 64bit) : bbbbbbbb - sig2_ext (lower 64bit) : cccccccc - sig2_cut (extra unknown bits) : dddddddddddd.... - - And the calculation process is: - ---------------------------------------- - aaaaaaaa * - bbbbbbbbccccccccdddddddddddd.... - ---------------------------------------- - abababababababab + - acacacacacacacac + - adadadadadadadadadad.... - ---------------------------------------- - [hi____][lo____] + - [hi2___][lo2___] + - [unknown___________....] - ---------------------------------------- - - The addition with carry may affect higher bits, but if there is a 0 - in higher bits, the bits higher than 0 will not be affected. - - `lo2` + `unknown` may get a carry bit and may affect `hi2`, the max - value of `hi2` is 0xFFFFFFFFFFFFFFFE, so `hi2` will not overflow. - - `lo` + `hi2` may also get a carry bit and may affect `hi`, but only - the highest significant 53 bits of `hi` is needed. If there is a 0 - in the lower bits of `hi`, then all the following bits can be dropped. - - To convert the result to IEEE-754 double number, we need to perform - correct rounding: - 1. if bit 54 is 0, round down, - 2. if bit 54 is 1 and any bit beyond bit 54 is 1, round up, - 3. if bit 54 is 1 and all bits beyond bit 54 are 0, round to even, - as the extra bits is unknown, this case will not be handled here. - */ - - u64 raw; - u64 sig1, sig2, sig2_ext, hi, lo, hi2, lo2, add, bits; - i32 exp2; - u32 lz; - bool exact = false, carry, round_up; - - /* convert (10^exp) to (sig2 * 2^exp2) */ - pow10_table_get_sig(exp, &sig2, &sig2_ext); - pow10_table_get_exp(exp, &exp2); - - /* normalize and multiply */ - lz = u64_lz_bits(sig); - sig1 = sig << lz; - exp2 -= (i32)lz; - u128_mul(sig1, sig2, &hi, &lo); - - /* - The `hi` is in range [0x4000000000000000, 0xFFFFFFFFFFFFFFFE], - To get normalized value, `hi` should be shifted to the left by 0 or 1. - - The highest significant 53 bits is used by IEEE-754 double number, - and the bit 54 is used to detect rounding direction. - - The lowest (64 - 54 - 1) bits is used to check whether it contains 0. - */ - bits = hi & (((u64)1 << (64 - 54 - 1)) - 1); - if (bits - 1 < (((u64)1 << (64 - 54 - 1)) - 2)) { - /* - (bits != 0 && bits != 0x1FF) => (bits - 1 < 0x1FF - 1) - The `bits` is not zero, so we don't need to check `round to even` - case. The `bits` contains bit `0`, so we can drop the extra bits - after `0`. - */ - exact = true; - - } else { - /* - (bits == 0 || bits == 0x1FF) - The `bits` is filled with all `0` or all `1`, so we need to check - lower bits with another 64-bit multiplication. - */ - u128_mul(sig1, sig2_ext, &hi2, &lo2); - - add = lo + hi2; - if (add + 1 > (u64)1) { - /* - (add != 0 && add != U64_MAX) => (add + 1 > 1) - The `add` is not zero, so we don't need to check `round to - even` case. The `add` contains bit `0`, so we can drop the - extra bits after `0`. The `hi` cannot be U64_MAX, so it will - not overflow. - */ - carry = add < lo || add < hi2; - hi += carry; - exact = true; - } - } - - if (exact) { - /* normalize */ - lz = hi < ((u64)1 << 63); - hi <<= lz; - exp2 -= (i32)lz; - exp2 += 64; - - /* test the bit 54 and get rounding direction */ - round_up = (hi & ((u64)1 << (64 - 54))) > (u64)0; - hi += (round_up ? ((u64)1 << (64 - 54)) : (u64)0); - - /* test overflow */ - if (hi < ((u64)1 << (64 - 54))) { - hi = ((u64)1 << 63); - exp2 += 1; - } - - /* This is a normal number, convert it to IEEE-754 format. */ - hi >>= F64_BITS - F64_SIG_FULL_BITS; - exp2 += F64_BITS - F64_SIG_FULL_BITS + F64_SIG_BITS; - exp2 += F64_EXP_BIAS; - raw = ((u64)exp2 << F64_SIG_BITS) | (hi & F64_SIG_MASK); - return_f64_bin(raw); - } - } - - /* - Slow path: read double number exactly with diyfp. - 1. Use cached diyfp to get an approximation value. - 2. Use bigcomp to check the approximation value if needed. - - This algorithm refers to google's double-conversion project: - https://github.com/google/double-conversion - */ - { - const i32 ERR_ULP_LOG = 3; - const i32 ERR_ULP = 1 << ERR_ULP_LOG; - const i32 ERR_CACHED_POW = ERR_ULP / 2; - const i32 ERR_MUL_FIXED = ERR_ULP / 2; - const i32 DIY_SIG_BITS = 64; - const i32 EXP_BIAS = F64_EXP_BIAS + F64_SIG_BITS; - const i32 EXP_SUBNORMAL = -EXP_BIAS + 1; - - u64 fp_err; - u32 bits; - i32 order_of_magnitude; - i32 effective_significand_size; - i32 precision_digits_count; - u64 precision_bits; - u64 half_way; - - u64 raw; - diy_fp fp, fp_upper; - bigint big_full, big_comp; - i32 cmp; - - fp.sig = sig; - fp.exp = 0; - fp_err = sig_cut ? (u64)(ERR_ULP / 2) : (u64)0; - - /* normalize */ - bits = u64_lz_bits(fp.sig); - fp.sig <<= bits; - fp.exp -= (i32)bits; - fp_err <<= bits; - - /* multiply and add error */ - fp = diy_fp_mul(fp, diy_fp_get_cached_pow10(exp)); - fp_err += (u64)ERR_CACHED_POW + (fp_err != 0) + (u64)ERR_MUL_FIXED; - - /* normalize */ - bits = u64_lz_bits(fp.sig); - fp.sig <<= bits; - fp.exp -= (i32)bits; - fp_err <<= bits; - - /* effective significand */ - order_of_magnitude = DIY_SIG_BITS + fp.exp; - if (likely(order_of_magnitude >= EXP_SUBNORMAL + F64_SIG_FULL_BITS)) { - effective_significand_size = F64_SIG_FULL_BITS; - } else if (order_of_magnitude <= EXP_SUBNORMAL) { - effective_significand_size = 0; - } else { - effective_significand_size = order_of_magnitude - EXP_SUBNORMAL; - } - - /* precision digits count */ - precision_digits_count = DIY_SIG_BITS - effective_significand_size; - if (unlikely(precision_digits_count + ERR_ULP_LOG >= DIY_SIG_BITS)) { - i32 shr = (precision_digits_count + ERR_ULP_LOG) - DIY_SIG_BITS + 1; - fp.sig >>= shr; - fp.exp += shr; - fp_err = (fp_err >> shr) + 1 + (u32)ERR_ULP; - precision_digits_count -= shr; - } - - /* half way */ - precision_bits = fp.sig & (((u64)1 << precision_digits_count) - 1); - precision_bits *= (u32)ERR_ULP; - half_way = (u64)1 << (precision_digits_count - 1); - half_way *= (u32)ERR_ULP; - - /* rounding */ - fp.sig >>= precision_digits_count; - fp.sig += (precision_bits >= half_way + fp_err); - fp.exp += precision_digits_count; - - /* get IEEE double raw value */ - raw = diy_fp_to_ieee_raw(fp); - if (unlikely(raw == F64_RAW_INF)) return_inf(); - if (likely(precision_bits <= half_way - fp_err || - precision_bits >= half_way + fp_err)) { - return_f64_bin(raw); /* number is accurate */ - } - /* now the number is the correct value, or the next lower value */ - - /* upper boundary */ - if (raw & F64_EXP_MASK) { - fp_upper.sig = (raw & F64_SIG_MASK) + ((u64)1 << F64_SIG_BITS); - fp_upper.exp = (i32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); - } else { - fp_upper.sig = (raw & F64_SIG_MASK); - fp_upper.exp = 1; - } - fp_upper.exp -= F64_EXP_BIAS + F64_SIG_BITS; - fp_upper.sig <<= 1; - fp_upper.exp -= 1; - fp_upper.sig += 1; /* add half ulp */ - - /* compare with bigint */ - bigint_set_buf(&big_full, sig, &exp, sig_cut, sig_end, dot_pos); - bigint_set_u64(&big_comp, fp_upper.sig); - if (exp >= 0) { - bigint_mul_pow10(&big_full, +exp); - } else { - bigint_mul_pow10(&big_comp, -exp); - } - if (fp_upper.exp > 0) { - bigint_mul_pow2(&big_comp, (u32)+fp_upper.exp); - } else { - bigint_mul_pow2(&big_full, (u32)-fp_upper.exp); - } - cmp = bigint_cmp(&big_full, &big_comp); - if (likely(cmp != 0)) { - /* round down or round up */ - raw += (cmp > 0); - } else { - /* falls midway, round to even */ - raw += (raw & 1); - } - - if (unlikely(raw == F64_RAW_INF)) return_inf(); - return_f64_bin(raw); - } - -#undef return_err -#undef return_inf -#undef return_0 -#undef return_i64 -#undef return_f64 -#undef return_f64_bin -#undef return_raw -} - - - -#else /* FP_READER */ - -/** - Read a JSON number. - This is a fallback function if the custom number reader is disabled. - This function use libc's strtod() to read floating-point number. - */ -static_inline bool read_number(u8 **ptr, - yyjson_val *val, - const char **msg) { - -#define return_err(_pos, _msg) do { \ - *msg = _msg; \ - *end = _pos; \ - return false; \ -} while (false) - -#define return_0() do { \ - val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ - val->uni.u64 = 0; \ - *end = cur; return true; \ -} while (false) - -#define return_i64(_v) do { \ - val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ - val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ - *end = cur; return true; \ -} while (false) - -#define return_f64(_v) do { \ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ - val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ - *end = cur; return true; \ -} while (false) - -#define return_f64_bin(_v) do { \ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ - val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ - *end = cur; return true; \ -} while (false) - -#define return_inf() do { \ - if (false) return_f64_bin(F64_RAW_INF); \ - else return_err(hdr, "number is infinity when parsed as double"); \ -} while (false) - - u64 sig, num; - u8 *hdr = *ptr; - u8 *cur = *ptr; - u8 **end = ptr; - u8 *dot = NULL; - u8 *f64_end = NULL; - bool sign; - - /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ - if (unlikely(false)) { - return read_number_raw(ptr, pre, flg, val, msg); - } - - sign = (*hdr == '-'); - cur += sign; - sig = (u8)(*cur - '0'); - - /* read first digit, check leading zero */ - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after minus sign"); - } - if (*cur == '0') { - cur++; - if (unlikely(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (!digi_is_fp(*cur)) return_0(); - goto read_double; - } - - /* read continuous digits, up to 19 characters */ -#define expr_intg(i) \ - if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ - else { cur += i; goto intg_end; } - repeat_in_1_18(expr_intg) -#undef expr_intg - - /* here are 19 continuous digits, skip them */ - cur += 19; - if (digi_is_digit(cur[0]) && !digi_is_digit_or_fp(cur[1])) { - /* this number is an integer consisting of 20 digits */ - num = (u8)(*cur - '0'); - if ((sig < (U64_MAX / 10)) || - (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { - sig = num + sig * 10; - cur++; - if (sign) { - if (false) return_raw(); - return_f64(normalized_u64_to_f64(sig)); - } - return_i64(sig); - } - } - -intg_end: - /* continuous digits ended */ - if (!digi_is_digit_or_fp(*cur)) { - /* this number is an integer consisting of 1 to 19 digits */ - if (sign && (sig > ((u64)1 << 63))) { - if (false) return_raw(); - return_f64(normalized_u64_to_f64(sig)); - } - return_i64(sig); - } - -read_double: - /* this number should be read as double */ - while (digi_is_digit(*cur)) cur++; - if (*cur == '.') { - /* skip fraction part */ - dot = cur; - cur++; - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after decimal point"); - } - cur++; - while (digi_is_digit(*cur)) cur++; - } - if (digi_is_exp(*cur)) { - /* skip exponent part */ - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after exponent sign"); - } - cur++; - while (digi_is_digit(*cur)) cur++; - } - - /* - libc's strtod() is used to parse the floating-point number. - - Note that the decimal point character used by strtod() is locale-dependent, - and the rounding direction may affected by fesetround(). - - For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the - decimal point, while other locales use ',' as the decimal point. - - Here strtod() is called twice for different locales, but if another thread - happens calls setlocale() between two strtod(), parsing may still fail. - */ - val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); - if (unlikely(f64_end != cur)) { - /* replace '.' with ',' for locale */ - bool cut = (*cur == ','); - if (cut) *cur = ' '; - if (dot) *dot = ','; - val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); - /* restore ',' to '.' */ - if (cut) *cur = ','; - if (dot) *dot = '.'; - if (unlikely(f64_end != cur)) { - return_err(hdr, "strtod() failed to parse the number"); - } - } - if (unlikely(val->uni.f64 >= HUGE_VAL || val->uni.f64 <= -HUGE_VAL)) { - return_inf(); - } - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - *end = cur; - return true; - -#undef return_err -#undef return_0 -#undef return_i64 -#undef return_f64 -#undef return_f64_bin -#undef return_inf -#undef return_raw -} - -#endif /* FP_READER */ - - - -/*============================================================================== - * JSON String Reader - *============================================================================*/ - -/** - Read a JSON string. - @param ptr The head pointer of string before '"' prefix (inout). - @param lst JSON last position. - @param inv Allow invalid unicode. - @param val The string value to be written. - @param msg The error message pointer. - @return Whether success. - */ -static_inline bool read_string(u8 **ptr, - u8 *lst, - yyjson_val *val, - const char **msg) { - /* - Each unicode code point is encoded as 1 to 4 bytes in UTF-8 encoding, - we use 4-byte mask and pattern value to validate UTF-8 byte sequence, - this requires the input data to have 4-byte zero padding. - --------------------------------------------------- - 1 byte - unicode range [U+0000, U+007F] - unicode min [.......0] - unicode max [.1111111] - bit pattern [0.......] - --------------------------------------------------- - 2 byte - unicode range [U+0080, U+07FF] - unicode min [......10 ..000000] - unicode max [...11111 ..111111] - bit require [...xxxx. ........] (1E 00) - bit mask [xxx..... xx......] (E0 C0) - bit pattern [110..... 10......] (C0 80) - --------------------------------------------------- - 3 byte - unicode range [U+0800, U+FFFF] - unicode min [........ ..100000 ..000000] - unicode max [....1111 ..111111 ..111111] - bit require [....xxxx ..x..... ........] (0F 20 00) - bit mask [xxxx.... xx...... xx......] (F0 C0 C0) - bit pattern [1110.... 10...... 10......] (E0 80 80) - --------------------------------------------------- - 3 byte invalid (reserved for surrogate halves) - unicode range [U+D800, U+DFFF] - unicode min [....1101 ..100000 ..000000] - unicode max [....1101 ..111111 ..111111] - bit mask [....xxxx ..x..... ........] (0F 20 00) - bit pattern [....1101 ..1..... ........] (0D 20 00) - --------------------------------------------------- - 4 byte - unicode range [U+10000, U+10FFFF] - unicode min [........ ...10000 ..000000 ..000000] - unicode max [.....100 ..001111 ..111111 ..111111] - bit require [.....xxx ..xx.... ........ ........] (07 30 00 00) - bit mask [xxxxx... xx...... xx...... xx......] (F8 C0 C0 C0) - bit pattern [11110... 10...... 10...... 10......] (F0 80 80 80) - --------------------------------------------------- - */ -#if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN - const u32 b1_mask = 0x80000000UL; - const u32 b1_patt = 0x00000000UL; - const u32 b2_mask = 0xE0C00000UL; - const u32 b2_patt = 0xC0800000UL; - const u32 b2_requ = 0x1E000000UL; - const u32 b3_mask = 0xF0C0C000UL; - const u32 b3_patt = 0xE0808000UL; - const u32 b3_requ = 0x0F200000UL; - const u32 b3_erro = 0x0D200000UL; - const u32 b4_mask = 0xF8C0C0C0UL; - const u32 b4_patt = 0xF0808080UL; - const u32 b4_requ = 0x07300000UL; - const u32 b4_err0 = 0x04000000UL; - const u32 b4_err1 = 0x03300000UL; -#elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN - const u32 b1_mask = 0x00000080UL; - const u32 b1_patt = 0x00000000UL; - const u32 b2_mask = 0x0000C0E0UL; - const u32 b2_patt = 0x000080C0UL; - const u32 b2_requ = 0x0000001EUL; - const u32 b3_mask = 0x00C0C0F0UL; - const u32 b3_patt = 0x008080E0UL; - const u32 b3_requ = 0x0000200FUL; - const u32 b3_erro = 0x0000200DUL; - const u32 b4_mask = 0xC0C0C0F8UL; - const u32 b4_patt = 0x808080F0UL; - const u32 b4_requ = 0x00003007UL; - const u32 b4_err0 = 0x00000004UL; - const u32 b4_err1 = 0x00003003UL; -#else - /* this should be evaluated at compile-time */ - v32_uni b1_mask_uni = {{ 0x80, 0x00, 0x00, 0x00 }}; - v32_uni b1_patt_uni = {{ 0x00, 0x00, 0x00, 0x00 }}; - v32_uni b2_mask_uni = {{ 0xE0, 0xC0, 0x00, 0x00 }}; - v32_uni b2_patt_uni = {{ 0xC0, 0x80, 0x00, 0x00 }}; - v32_uni b2_requ_uni = {{ 0x1E, 0x00, 0x00, 0x00 }}; - v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; - v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; - v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; - v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; - v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; - v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; - v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; - v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; - v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; - u32 b1_mask = b1_mask_uni.u; - u32 b1_patt = b1_patt_uni.u; - u32 b2_mask = b2_mask_uni.u; - u32 b2_patt = b2_patt_uni.u; - u32 b2_requ = b2_requ_uni.u; - u32 b3_mask = b3_mask_uni.u; - u32 b3_patt = b3_patt_uni.u; - u32 b3_requ = b3_requ_uni.u; - u32 b3_erro = b3_erro_uni.u; - u32 b4_mask = b4_mask_uni.u; - u32 b4_patt = b4_patt_uni.u; - u32 b4_requ = b4_requ_uni.u; - u32 b4_err0 = b4_err0_uni.u; - u32 b4_err1 = b4_err1_uni.u; -#endif - -#define is_valid_seq_1(uni) ( \ - ((uni & b1_mask) == b1_patt) \ -) - -#define is_valid_seq_2(uni) ( \ - ((uni & b2_mask) == b2_patt) && \ - ((uni & b2_requ)) \ -) - -#define is_valid_seq_3(uni) ( \ - ((uni & b3_mask) == b3_patt) && \ - ((tmp = (uni & b3_requ))) && \ - ((tmp != b3_erro)) \ -) - -#define is_valid_seq_4(uni) ( \ - ((uni & b4_mask) == b4_patt) && \ - ((tmp = (uni & b4_requ))) && \ - ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ -) - -#define return_err(_end, _msg) do { \ - *msg = _msg; \ - *end = _end; \ - return false; \ -} while (false) - - u8 *cur = *ptr; - u8 **end = ptr; - u8 *src = ++cur, *dst, *pos; - u16 hi, lo; - u32 uni, tmp; - -skip_ascii: - /* Most strings have no escaped characters, so we can jump them quickly. */ - -skip_ascii_begin: - /* - We want to make loop unrolling, as shown in the following code. Some - compiler may not generate instructions as expected, so we rewrite it with - explicit goto statements. We hope the compiler can generate instructions - like this: https://godbolt.org/z/8vjsYq - - while (true) repeat16({ - if (likely(!(char_is_ascii_stop(*src)))) src++; - else break; - }) - */ -#define expr_jump(i) \ - if (likely(!char_is_ascii_stop(src[i]))) {} \ - else goto skip_ascii_stop##i; - -#define expr_stop(i) \ - skip_ascii_stop##i: \ - src += i; \ - goto skip_ascii_end; - - repeat16_incr(expr_jump) - src += 16; - goto skip_ascii_begin; - repeat16_incr(expr_stop) - -#undef expr_jump -#undef expr_stop - -skip_ascii_end: - - /* - GCC may store src[i] in a register at each line of expr_jump(i) above. - These instructions are useless and will degrade performance. - This inline asm is a hint for gcc: "the memory has been modified, - do not cache it". - - MSVC, Clang, ICC can generate expected instructions without this hint. - */ -#if YYJSON_IS_REAL_GCC - __asm__ volatile("":"=m"(*src)); -#endif - if (likely(*src == '"')) { - val->tag = ((u64)(src - cur) << YYJSON_TAG_BIT) | - (u64)(YYJSON_TYPE_STR); - val->uni.str = (const char *)cur; - *src = '\0'; - *end = src + 1; - return true; - } - -skip_utf8: - if (*src & 0x80) { /* non-ASCII character */ - /* - Non-ASCII character appears here, which means that the text is likely - to be written in non-English or emoticons. According to some common - data set statistics, byte sequences of the same length may appear - consecutively. We process the byte sequences of the same length in each - loop, which is more friendly to branch prediction. - */ - pos = src; -#if YYJSON_DISABLE_UTF8_VALIDATION - while (true) repeat8({ - if (likely((*src & 0xF0) == 0xE0)) src += 3; - else break; - }) - if (*src < 0x80) goto skip_ascii; - while (true) repeat8({ - if (likely((*src & 0xE0) == 0xC0)) src += 2; - else break; - }) - while (true) repeat8({ - if (likely((*src & 0xF8) == 0xF0)) src += 4; - else break; - }) -#else - uni = byte_load_4(src); - while (is_valid_seq_3(uni)) { - src += 3; - uni = byte_load_4(src); - } - if (is_valid_seq_1(uni)) goto skip_ascii; - while (is_valid_seq_2(uni)) { - src += 2; - uni = byte_load_4(src); - } - while (is_valid_seq_4(uni)) { - src += 4; - uni = byte_load_4(src); - } -#endif - goto skip_ascii; - } - - /* The escape character appears, we need to copy it. */ - dst = src; -copy_escape: - if (likely(*src == '\\')) { - switch (*++src) { - case '"': *dst++ = '"'; src++; break; - case '\\': *dst++ = '\\'; src++; break; - case '/': *dst++ = '/'; src++; break; - case 'b': *dst++ = '\b'; src++; break; - case 'f': *dst++ = '\f'; src++; break; - case 'n': *dst++ = '\n'; src++; break; - case 'r': *dst++ = '\r'; src++; break; - case 't': *dst++ = '\t'; src++; break; - case 'u': - if (unlikely(!read_hex_u16(++src, &hi))) { - return_err(src - 2, "invalid escaped sequence in string"); - } - src += 4; - if (likely((hi & 0xF800) != 0xD800)) { - /* a BMP character */ - if (hi >= 0x800) { - *dst++ = (u8)(0xE0 | (hi >> 12)); - *dst++ = (u8)(0x80 | ((hi >> 6) & 0x3F)); - *dst++ = (u8)(0x80 | (hi & 0x3F)); - } else if (hi >= 0x80) { - *dst++ = (u8)(0xC0 | (hi >> 6)); - *dst++ = (u8)(0x80 | (hi & 0x3F)); - } else { - *dst++ = (u8)hi; - } - } else { - /* a non-BMP character, represented as a surrogate pair */ - if (unlikely((hi & 0xFC00) != 0xD800)) { - return_err(src - 6, "invalid high surrogate in string"); - } - if (unlikely(!byte_match_2(src, "\\u"))) { - return_err(src, "no low surrogate in string"); - } - if (unlikely(!read_hex_u16(src + 2, &lo))) { - return_err(src, "invalid escaped sequence in string"); - } - if (unlikely((lo & 0xFC00) != 0xDC00)) { - return_err(src, "invalid low surrogate in string"); - } - uni = ((((u32)hi - 0xD800) << 10) | - ((u32)lo - 0xDC00)) + 0x10000; - *dst++ = (u8)(0xF0 | (uni >> 18)); - *dst++ = (u8)(0x80 | ((uni >> 12) & 0x3F)); - *dst++ = (u8)(0x80 | ((uni >> 6) & 0x3F)); - *dst++ = (u8)(0x80 | (uni & 0x3F)); - src += 6; - } - break; - default: return_err(src, "invalid escaped character in string"); - } - } else if (likely(*src == '"')) { - val->tag = ((u64)(dst - cur) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = (const char *)cur; - *end = src + 1; - return true; - } else { - return_err(src, "unexpected control character in string"); - } - -copy_ascii: - /* - Copy continuous ASCII, loop unrolling, same as the following code: - - while (true) repeat16({ - if (unlikely(char_is_ascii_stop(*src))) break; - *dst++ = *src++; - }) - */ -#if YYJSON_IS_REAL_GCC -# define expr_jump(i) \ - if (likely(!(char_is_ascii_stop(src[i])))) {} \ - else { __asm__ volatile("":"=m"(src[i])); goto copy_ascii_stop_##i; } -#else -# define expr_jump(i) \ - if (likely(!(char_is_ascii_stop(src[i])))) {} \ - else { goto copy_ascii_stop_##i; } -#endif - repeat16_incr(expr_jump) -#undef expr_jump - - byte_move_16(dst, src); - src += 16; - dst += 16; - goto copy_ascii; - - /* - The memory will be moved forward by at least 1 byte. So the `byte_move` - can be one byte more than needed to reduce the number of instructions. - */ -copy_ascii_stop_0: - goto copy_utf8; -copy_ascii_stop_1: - byte_move_2(dst, src); - src += 1; - dst += 1; - goto copy_utf8; -copy_ascii_stop_2: - byte_move_2(dst, src); - src += 2; - dst += 2; - goto copy_utf8; -copy_ascii_stop_3: - byte_move_4(dst, src); - src += 3; - dst += 3; - goto copy_utf8; -copy_ascii_stop_4: - byte_move_4(dst, src); - src += 4; - dst += 4; - goto copy_utf8; -copy_ascii_stop_5: - byte_move_4(dst, src); - byte_move_2(dst + 4, src + 4); - src += 5; - dst += 5; - goto copy_utf8; -copy_ascii_stop_6: - byte_move_4(dst, src); - byte_move_2(dst + 4, src + 4); - src += 6; - dst += 6; - goto copy_utf8; -copy_ascii_stop_7: - byte_move_8(dst, src); - src += 7; - dst += 7; - goto copy_utf8; -copy_ascii_stop_8: - byte_move_8(dst, src); - src += 8; - dst += 8; - goto copy_utf8; -copy_ascii_stop_9: - byte_move_8(dst, src); - byte_move_2(dst + 8, src + 8); - src += 9; - dst += 9; - goto copy_utf8; -copy_ascii_stop_10: - byte_move_8(dst, src); - byte_move_2(dst + 8, src + 8); - src += 10; - dst += 10; - goto copy_utf8; -copy_ascii_stop_11: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - src += 11; - dst += 11; - goto copy_utf8; -copy_ascii_stop_12: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - src += 12; - dst += 12; - goto copy_utf8; -copy_ascii_stop_13: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - byte_move_2(dst + 12, src + 12); - src += 13; - dst += 13; - goto copy_utf8; -copy_ascii_stop_14: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - byte_move_2(dst + 12, src + 12); - src += 14; - dst += 14; - goto copy_utf8; -copy_ascii_stop_15: - byte_move_16(dst, src); - src += 15; - dst += 15; - goto copy_utf8; - -copy_utf8: - if (*src & 0x80) { /* non-ASCII character */ - pos = src; - uni = byte_load_4(src); -#if YYJSON_DISABLE_UTF8_VALIDATION - while (true) repeat4({ - if ((uni & b3_mask) == b3_patt) { - byte_copy_4(dst, &uni); - dst += 3; - src += 3; - uni = byte_load_4(src); - } else break; - }) - if ((uni & b1_mask) == b1_patt) goto copy_ascii; - while (true) repeat4({ - if ((uni & b2_mask) == b2_patt) { - byte_copy_2(dst, &uni); - dst += 2; - src += 2; - uni = byte_load_4(src); - } else break; - }) - while (true) repeat4({ - if ((uni & b4_mask) == b4_patt) { - byte_copy_4(dst, &uni); - dst += 4; - src += 4; - uni = byte_load_4(src); - } else break; - }) -#else - while (is_valid_seq_3(uni)) { - byte_copy_4(dst, &uni); - dst += 3; - src += 3; - uni = byte_load_4(src); - } - if (is_valid_seq_1(uni)) goto copy_ascii; - while (is_valid_seq_2(uni)) { - byte_copy_2(dst, &uni); - dst += 2; - src += 2; - uni = byte_load_4(src); - } - while (is_valid_seq_4(uni)) { - byte_copy_4(dst, &uni); - dst += 4; - src += 4; - uni = byte_load_4(src); - } -#endif - goto copy_ascii; - } - goto copy_escape; - -#undef return_err -#undef is_valid_seq_1 -#undef is_valid_seq_2 -#undef is_valid_seq_3 -#undef is_valid_seq_4 -} - - - -/*============================================================================== - * JSON Reader Implementation - * - * We use goto statements to build the finite state machine (FSM). - * The FSM's state was held by program counter (PC) and the 'goto' make the - * state transitions. - *============================================================================*/ - -/** Read single value JSON document. */ -static_noinline yyjson_doc *read_root_single(u8 *hdr, - u8 *cur, - u8 *end, - yyjson_alc alc, - yyjson_read_err *err) { - -#define return_err(_pos, _code, _msg) do { \ - if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ - } else { \ - err->pos = (usize)(_pos - hdr); \ - err->code = YYJSON_READ_ERROR_##_code; \ - err->msg = _msg; \ - } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ - return NULL; \ -} while (false) - - usize hdr_len; /* value count used by doc */ - usize alc_num; /* value count capacity */ - yyjson_val *val_hdr; /* the head of allocated values */ - yyjson_val *val; /* current value */ - yyjson_doc *doc; /* the JSON document, equals to val_hdr */ - const char *msg; /* error message */ - - hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); - hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; - alc_num = hdr_len + 1; /* single value */ - - val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_num * sizeof(yyjson_val)); - if (unlikely(!val_hdr)) goto fail_alloc; - val = val_hdr + hdr_len; - - if (char_is_number(*cur)) { - if (likely(read_number(&cur, val, &msg))) goto doc_end; - goto fail_number; - } - if (*cur == '"') { - if (likely(read_string(&cur, end, val, &msg))) goto doc_end; - goto fail_string; - } - if (*cur == 't') { - if (likely(read_true(&cur, val))) goto doc_end; - goto fail_literal; - } - if (*cur == 'f') { - if (likely(read_false(&cur, val))) goto doc_end; - goto fail_literal; - } - if (*cur == 'n') { - if (likely(read_null(&cur, val))) goto doc_end; - if (false) { - if (read_nan(false, &cur, 0, val)) goto doc_end; - } - goto fail_literal; - } - goto fail_character; - -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (false) { - if (!skip_spaces_and_comments(&cur)) { - if (byte_match_2(cur, "/*")) goto fail_comment; - } - } else { - while (char_is_space(*cur)) cur++; - } - if (unlikely(cur < end)) goto fail_garbage; - } - - doc = (yyjson_doc *)val_hdr; - doc->root = val_hdr + hdr_len; - doc->alc = alc; - doc->dat_read = (usize)(cur - hdr); - doc->val_read = 1; - doc->str_pool = (char *)hdr; - return doc; - -fail_string: - return_err(cur, INVALID_STRING, msg); -fail_number: - return_err(cur, INVALID_NUMBER, msg); -fail_alloc: - return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); -fail_literal: - return_err(cur, LITERAL, "invalid literal"); -fail_comment: - return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); -fail_character: - return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); -fail_recursion: - return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); - -#undef return_err -} - -/** Read JSON document (accept all style, but optimized for minify). */ -static_inline yyjson_doc *read_root_minify(u8 *hdr, - u8 *cur, - u8 *end, - yyjson_alc alc, - yyjson_read_err *err) { - -#define return_err(_pos, _code, _msg) do { \ - if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ - } else { \ - err->pos = (usize)(_pos - hdr); \ - err->code = YYJSON_READ_ERROR_##_code; \ - err->msg = _msg; \ - } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ - return NULL; \ -} while (false) - -#define val_incr() do { \ - val++; \ - if (unlikely(val >= val_end)) { \ - usize alc_old = alc_len; \ - alc_len += alc_len / 2; \ - if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ - val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ - alc_old * sizeof(yyjson_val), \ - alc_len * sizeof(yyjson_val)); \ - if ((!val_tmp)) goto fail_alloc; \ - val = val_tmp + (usize)(val - val_hdr); \ - ctn = val_tmp + (usize)(ctn - val_hdr); \ - val_hdr = val_tmp; \ - val_end = val_tmp + (alc_len - 2); \ - } \ -} while (false) - - usize dat_len; /* data length in bytes, hint for allocator */ - usize hdr_len; /* value count used by yyjson_doc */ - usize alc_len; /* value count allocated */ - usize alc_max; /* maximum value count for allocator */ - usize ctn_len; /* the number of elements in current container */ - yyjson_val *val_hdr; /* the head of allocated values */ - yyjson_val *val_end; /* the end of allocated values */ - yyjson_val *val_tmp; /* temporary pointer for realloc */ - yyjson_val *val; /* current JSON value */ - yyjson_val *ctn; /* current container */ - yyjson_val *ctn_parent; /* parent of current container */ - yyjson_doc *doc; /* the JSON document, equals to val_hdr */ - const char *msg; /* error message */ - - u32 container_depth = 0; /* limit on number of open array and map */ - bool raw; /* read number as raw */ - bool inv; /* allow invalid unicode */ - - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); - hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); - hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; - alc_max = USIZE_MAX / sizeof(yyjson_val); - alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4; - alc_len = yyjson_min(alc_len, alc_max); - - val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); - if (unlikely(!val_hdr)) goto fail_alloc; - val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ - val = val_hdr + hdr_len; - ctn = val; - ctn_len = 0; - - if (*cur++ == '{') { - ctn->tag = YYJSON_TYPE_OBJ; - ctn->uni.ofs = 0; - goto obj_key_begin; - } else { - ctn->tag = YYJSON_TYPE_ARR; - ctn->uni.ofs = 0; - goto arr_val_begin; - } - -arr_begin: - container_depth++; - if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { - goto fail_recursion; - } - - /* save current container */ - ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | - (ctn->tag & YYJSON_TAG_MASK); - - /* create a new array value, save parent container offset */ - val_incr(); - val->tag = YYJSON_TYPE_ARR; - val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); - - /* push the new array value as current container */ - ctn = val; - ctn_len = 0; - -arr_val_begin: - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; - } - if (char_is_number(*cur)) { - val_incr(); - ctn_len++; - if (likely(read_number(&cur, val, &msg))) goto arr_val_end; - goto fail_number; - } - if (*cur == '"') { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto arr_val_end; - goto fail_string; - } - if (*cur == 't') { - val_incr(); - ctn_len++; - if (likely(read_true(&cur, val))) goto arr_val_end; - goto fail_literal; - } - if (*cur == 'f') { - val_incr(); - ctn_len++; - if (likely(read_false(&cur, val))) goto arr_val_end; - goto fail_literal; - } - if (*cur == 'n') { - val_incr(); - ctn_len++; - if (likely(read_null(&cur, val))) goto arr_val_end; - goto fail_literal; - } - if (*cur == ']') { - cur++; - if (likely(ctn_len == 0)) goto arr_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_begin; - } - goto fail_character; - -arr_val_end: - if (*cur == ',') { - cur++; - goto arr_val_begin; - } - if (*cur == ']') { - cur++; - goto arr_end; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_end; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto arr_val_end; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -arr_end: - container_depth--; - - /* get parent container */ - ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); - - /* save the next sibling value offset */ - ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); - ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; - if (unlikely(ctn == ctn_parent)) goto doc_end; - - /* pop parent as current container */ - ctn = ctn_parent; - ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); - if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { - goto obj_val_end; - } else { - goto arr_val_end; - } - -obj_begin: - container_depth++; - if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { - goto fail_recursion; - } - - /* push container */ - ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | - (ctn->tag & YYJSON_TAG_MASK); - val_incr(); - val->tag = YYJSON_TYPE_OBJ; - /* offset to the parent */ - val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); - ctn = val; - ctn_len = 0; - -obj_key_begin: - if (likely(*cur == '"')) { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto obj_key_end; - goto fail_string; - } - if (likely(*cur == '}')) { - cur++; - if (likely(ctn_len == 0)) goto obj_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_begin; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto obj_key_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -obj_key_end: - if (*cur == ':') { - cur++; - goto obj_val_begin; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_end; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto obj_key_end; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -obj_val_begin: - if (*cur == '"') { - val++; - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto obj_val_end; - goto fail_string; - } - if (char_is_number(*cur)) { - val++; - ctn_len++; - if (likely(read_number(&cur, val, &msg))) goto obj_val_end; - goto fail_number; - } - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; - } - if (*cur == 't') { - val++; - ctn_len++; - if (likely(read_true(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (*cur == 'f') { - val++; - ctn_len++; - if (likely(read_false(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (*cur == 'n') { - val++; - ctn_len++; - if (likely(read_null(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_begin; - } - goto fail_character; - -obj_val_end: - if (likely(*cur == ',')) { - cur++; - goto obj_key_begin; - } - if (likely(*cur == '}')) { - cur++; - goto obj_end; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_end; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto obj_val_end; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -obj_end: - container_depth--; - - /* pop container */ - ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); - /* point to the next value */ - ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); - ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; - if (unlikely(ctn == ctn_parent)) goto doc_end; - ctn = ctn_parent; - ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); - if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { - goto obj_val_end; - } else { - goto arr_val_end; - } - -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (false) { - skip_spaces_and_comments(&cur); - if (byte_match_2(cur, "/*")) goto fail_comment; - } else { - while (char_is_space(*cur)) cur++; - } - if (unlikely(cur < end)) goto fail_garbage; - } - - doc = (yyjson_doc *)val_hdr; - doc->root = val_hdr + hdr_len; - doc->alc = alc; - doc->dat_read = (usize)(cur - hdr); - doc->val_read = (usize)((val - doc->root) + 1); - doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr; - return doc; - -fail_string: - return_err(cur, INVALID_STRING, msg); -fail_number: - return_err(cur, INVALID_NUMBER, msg); -fail_alloc: - return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); -fail_trailing_comma: - return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed"); -fail_literal: - return_err(cur, LITERAL, "invalid literal"); -fail_comment: - return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); -fail_character: - return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); -fail_recursion: - return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); - -#undef val_incr -#undef return_err -} - -/** Read JSON document (accept all style, but optimized for pretty). */ -static_inline yyjson_doc *read_root_pretty(u8 *hdr, - u8 *cur, - u8 *end, - yyjson_alc alc, - yyjson_read_err *err) { - -#define return_err(_pos, _code, _msg) do { \ - if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ - } else { \ - err->pos = (usize)(_pos - hdr); \ - err->code = YYJSON_READ_ERROR_##_code; \ - err->msg = _msg; \ - } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ - return NULL; \ -} while (false) - -#define val_incr() do { \ - val++; \ - if (unlikely(val >= val_end)) { \ - usize alc_old = alc_len; \ - alc_len += alc_len / 2; \ - if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ - val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ - alc_old * sizeof(yyjson_val), \ - alc_len * sizeof(yyjson_val)); \ - if ((!val_tmp)) goto fail_alloc; \ - val = val_tmp + (usize)(val - val_hdr); \ - ctn = val_tmp + (usize)(ctn - val_hdr); \ - val_hdr = val_tmp; \ - val_end = val_tmp + (alc_len - 2); \ - } \ -} while (false) - - usize dat_len; /* data length in bytes, hint for allocator */ - usize hdr_len; /* value count used by yyjson_doc */ - usize alc_len; /* value count allocated */ - usize alc_max; /* maximum value count for allocator */ - usize ctn_len; /* the number of elements in current container */ - yyjson_val *val_hdr; /* the head of allocated values */ - yyjson_val *val_end; /* the end of allocated values */ - yyjson_val *val_tmp; /* temporary pointer for realloc */ - yyjson_val *val; /* current JSON value */ - yyjson_val *ctn; /* current container */ - yyjson_val *ctn_parent; /* parent of current container */ - yyjson_doc *doc; /* the JSON document, equals to val_hdr */ - const char *msg; /* error message */ - - u32 container_depth = 0; /* limit on number of open array and map */ - - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); - hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); - hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; - alc_max = USIZE_MAX / sizeof(yyjson_val); - alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_PRETTY_RATIO) + 4; - alc_len = yyjson_min(alc_len, alc_max); - - val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); - if (unlikely(!val_hdr)) goto fail_alloc; - val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ - val = val_hdr + hdr_len; - ctn = val; - ctn_len = 0; - - if (*cur++ == '{') { - ctn->tag = YYJSON_TYPE_OBJ; - ctn->uni.ofs = 0; - if (*cur == '\n') cur++; - goto obj_key_begin; - } else { - ctn->tag = YYJSON_TYPE_ARR; - ctn->uni.ofs = 0; - if (*cur == '\n') cur++; - goto arr_val_begin; - } - -arr_begin: - container_depth++; - if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { - goto fail_recursion; - } - - /* save current container */ - ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | - (ctn->tag & YYJSON_TAG_MASK); - - /* create a new array value, save parent container offset */ - val_incr(); - val->tag = YYJSON_TYPE_ARR; - val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); - - /* push the new array value as current container */ - ctn = val; - ctn_len = 0; - if (*cur == '\n') cur++; - -arr_val_begin: -#if YYJSON_IS_REAL_GCC - while (true) repeat16({ - if (byte_match_2(cur, " ")) cur += 2; - else break; - }) -#else - while (true) repeat16({ - if (likely(byte_match_2(cur, " "))) cur += 2; - else break; - }) -#endif - - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; - } - if (char_is_number(*cur)) { - val_incr(); - ctn_len++; - if (likely(read_number(&cur, val, &msg))) goto arr_val_end; - goto fail_number; - } - if (*cur == '"') { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto arr_val_end; - goto fail_string; - } - if (*cur == 't') { - val_incr(); - ctn_len++; - if (likely(read_true(&cur, val))) goto arr_val_end; - goto fail_literal; - } - if (*cur == 'f') { - val_incr(); - ctn_len++; - if (likely(read_false(&cur, val))) goto arr_val_end; - goto fail_literal; - } - if (*cur == 'n') { - val_incr(); - ctn_len++; - if (likely(read_null(&cur, val))) goto arr_val_end; - if (false) { - if (read_nan(false, &cur, 0, val)) goto arr_val_end; - } - goto fail_literal; - } - if (*cur == ']') { - cur++; - if (likely(ctn_len == 0)) goto arr_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_begin; - } - goto fail_character; - -arr_val_end: - if (byte_match_2(cur, ",\n")) { - cur += 2; - goto arr_val_begin; - } - if (*cur == ',') { - cur++; - goto arr_val_begin; - } - if (*cur == ']') { - cur++; - goto arr_end; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_end; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto arr_val_end; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -arr_end: - container_depth--; - - /* get parent container */ - ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); - - /* save the next sibling value offset */ - ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); - ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; - if (unlikely(ctn == ctn_parent)) goto doc_end; - - /* pop parent as current container */ - ctn = ctn_parent; - ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); - if (*cur == '\n') cur++; - if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { - goto obj_val_end; - } else { - goto arr_val_end; - } - -obj_begin: - container_depth++; - if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { - goto fail_recursion; - } - - /* push container */ - ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | - (ctn->tag & YYJSON_TAG_MASK); - val_incr(); - val->tag = YYJSON_TYPE_OBJ; - /* offset to the parent */ - val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); - ctn = val; - ctn_len = 0; - if (*cur == '\n') cur++; - -obj_key_begin: -#if YYJSON_IS_REAL_GCC - while (true) repeat16({ - if (byte_match_2(cur, " ")) cur += 2; - else break; - }) -#else - while (true) repeat16({ - if (likely(byte_match_2(cur, " "))) cur += 2; - else break; - }) -#endif - if (likely(*cur == '"')) { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto obj_key_end; - goto fail_string; - } - if (likely(*cur == '}')) { - cur++; - if (likely(ctn_len == 0)) goto obj_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_begin; - } - if (false) { - if (skip_spaces_and_comments(&cur)) goto obj_key_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character; - -obj_key_end: - if (byte_match_2(cur, ": ")) { - cur += 2; - goto obj_val_begin; - } - if (*cur == ':') { - cur++; - goto obj_val_begin; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_end; - } - goto fail_character; - -obj_val_begin: - if (*cur == '"') { - val++; - ctn_len++; - if (likely(read_string(&cur, end, val, &msg))) goto obj_val_end; - goto fail_string; - } - if (char_is_number(*cur)) { - val++; - ctn_len++; - if (likely(read_number(&cur, val, &msg))) goto obj_val_end; - goto fail_number; - } - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; - } - if (*cur == 't') { - val++; - ctn_len++; - if (likely(read_true(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (*cur == 'f') { - val++; - ctn_len++; - if (likely(read_false(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (*cur == 'n') { - val++; - ctn_len++; - if (likely(read_null(&cur, val))) goto obj_val_end; - goto fail_literal; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_begin; - } - goto fail_character; - -obj_val_end: - if (byte_match_2(cur, ",\n")) { - cur += 2; - goto obj_key_begin; - } - if (likely(*cur == ',')) { - cur++; - goto obj_key_begin; - } - if (likely(*cur == '}')) { - cur++; - goto obj_end; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_end; - } - goto fail_character; - -obj_end: - container_depth--; - - /* pop container */ - ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); - /* point to the next value */ - ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); - ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; - if (unlikely(ctn == ctn_parent)) goto doc_end; - ctn = ctn_parent; - ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); - if (*cur == '\n') cur++; - if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { - goto obj_val_end; - } else { - goto arr_val_end; - } - -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (false) { - skip_spaces_and_comments(&cur); - if (byte_match_2(cur, "/*")) goto fail_comment; - } else { - while (char_is_space(*cur)) cur++; - } - if (unlikely(cur < end)) goto fail_garbage; - } - - doc = (yyjson_doc *)val_hdr; - doc->root = val_hdr + hdr_len; - doc->alc = alc; - doc->dat_read = (usize)(cur - hdr); - doc->val_read = (usize)((val - val_hdr)) - hdr_len + 1; - doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr; - return doc; - -fail_string: - return_err(cur, INVALID_STRING, msg); -fail_number: - return_err(cur, INVALID_NUMBER, msg); -fail_alloc: - return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); -fail_trailing_comma: - return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed"); -fail_literal: - return_err(cur, LITERAL, "invalid literal"); -fail_comment: - return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); -fail_character: - return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); -fail_recursion: - return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); - -#undef val_incr -#undef return_err -} - - - -/*============================================================================== - * JSON Reader Entrance - *============================================================================*/ - -yyjson_doc *yyjson_read_opts(char *dat, - usize len, - const yyjson_alc *alc_ptr, - yyjson_read_err *err) { - -#define return_err(_pos, _code, _msg) do { \ - err->pos = (usize)(_pos); \ - err->msg = _msg; \ - err->code = YYJSON_READ_ERROR_##_code; \ - if (!has_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ - return NULL; \ -} while (false) - yyjson_alc alc; - yyjson_doc *doc; - u8 *hdr = NULL, *end, *cur; - - if (!alc_ptr) { - alc = YYJSON_DEFAULT_ALC; - } else { - alc = *alc_ptr; - } - - hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); - end = hdr + len; - cur = hdr; - memcpy(hdr, dat, len); - memset(end, 0, YYJSON_PADDING_SIZE); - - /* skip empty contents before json document */ - if (unlikely(char_is_space_or_comment(*cur))) { - if (likely(char_is_space(*cur))) { - while (char_is_space(*++cur)); - } - if (unlikely(cur >= end)) { - return_err(0, EMPTY_CONTENT, "input data is empty"); - } - } - - /* read json document */ - if (likely(char_is_container(*cur))) { - if (char_is_space(cur[1]) && char_is_space(cur[2])) { - doc = read_root_pretty(hdr, cur, end, alc, err); - } else { - doc = read_root_minify(hdr, cur, end, alc, err); - } - } else { - doc = read_root_single(hdr, cur, end, alc, err); - } - - /* check result */ - if (unlikely(!doc)) { - alc.free(alc.ctx, (void *)hdr); - } - return doc; - -#undef return_err -} - -yyjson_doc *yyjson_read_file(const char *path, - yyjson_read_flag flg, - const yyjson_alc *alc_ptr, - yyjson_read_err *err) { -#define return_err(_code, _msg) do { \ - err->pos = 0; \ - err->msg = _msg; \ - err->code = YYJSON_READ_ERROR_##_code; \ - return NULL; \ -} while (false) - - - yyjson_doc *doc; - FILE *file; - - - if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); - - file = fopen_readonly(path); - if (unlikely(!file)) return_err(FILE_OPEN, "file opening failed"); - - doc = yyjson_read_fp(file, flg, alc_ptr, err); - fclose(file); - return doc; - -#undef return_err -} - -yyjson_doc *yyjson_read_fp(FILE *file, - yyjson_read_flag flg, - const yyjson_alc *alc_ptr, - yyjson_read_err *err) { -#define return_err(_code, _msg) do { \ - err->pos = 0; \ - err->msg = _msg; \ - err->code = YYJSON_READ_ERROR_##_code; \ - if (buf) alc.free(alc.ctx, buf); \ - return NULL; \ -} while (false) - - - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_doc *doc; - - long file_size = 0, file_pos; - void *buf = NULL; - usize buf_size = 0; - - /* validate input parameters */ - - if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL"); - - /* get current position */ - file_pos = ftell(file); - if (file_pos != -1) { - /* get total file size, may fail */ - if (fseek(file, 0, SEEK_END) == 0) file_size = ftell(file); - /* reset to original position, may fail */ - if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0; - /* get file size from current postion to end */ - if (file_size > 0) file_size -= file_pos; - } - - /* read file */ - if (file_size > 0) { - /* read the entire file in one call */ - buf_size = (usize)file_size + YYJSON_PADDING_SIZE; - buf = alc.malloc(alc.ctx, buf_size); - if (buf == NULL) { - return_err(MEMORY_ALLOCATION, "fail to alloc memory"); - } - if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { - return_err(FILE_READ, "file reading failed"); - } - } else { - /* failed to get file size, read it as a stream */ - usize chunk_min = (usize)64; - usize chunk_max = (usize)512 * 1024 * 1024; - usize chunk_now = chunk_min; - usize read_size; - void *tmp; - - buf_size = YYJSON_PADDING_SIZE; - while (true) { - if (buf_size + chunk_now < buf_size) { /* overflow */ - return_err(MEMORY_ALLOCATION, "fail to alloc memory"); - } - buf_size += chunk_now; - if (!buf) { - buf = alc.malloc(alc.ctx, buf_size); - if (!buf) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); - } else { - tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); - if (!tmp) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); - buf = tmp; - } - tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now; - read_size = fread_safe(tmp, chunk_now, file); - file_size += (long)read_size; - if (read_size != chunk_now) break; - - chunk_now *= 2; - if (chunk_now > chunk_max) chunk_now = chunk_max; - } - } - - /* read JSON */ - memset((u8 *)buf + file_size, 0, YYJSON_PADDING_SIZE); - flg |= YYJSON_READ_INSITU; - doc = yyjson_read_opts((char *)buf, (usize)file_size, &alc, err); - if (doc) { - doc->str_pool = (char *)buf; - return doc; - } else { - alc.free(alc.ctx, buf); - return NULL; - } - -#undef return_err -} - -const char *yyjson_read_number(const char *dat, - yyjson_val *val, - yyjson_read_flag flg, - const yyjson_alc *alc, - yyjson_read_err *err) { -#define return_err(_pos, _code, _msg) do { \ - err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \ - err->msg = _msg; \ - err->code = YYJSON_READ_ERROR_##_code; \ - return NULL; \ -} while (false) - - u8 *hdr = constcast(u8 *)dat, *cur = hdr; - bool raw; /* read number as raw */ - u8 *raw_end; /* raw end for null-terminator */ - u8 **pre; /* previous raw end pointer */ - const char *msg; - - -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - u8 buf[128]; - usize dat_len; -#endif - - - if (unlikely(!dat)) { - return_err(cur, INVALID_PARAMETER, "input data is NULL"); - } - if (unlikely(!val)) { - return_err(cur, INVALID_PARAMETER, "output value is NULL"); - } - -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - if (!alc) alc = &YYJSON_DEFAULT_ALC; - dat_len = strlen(dat); - if (dat_len < sizeof(buf)) { - memcpy(buf, dat, dat_len + 1); - hdr = buf; - cur = hdr; - } else { - hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1); - cur = hdr; - if (unlikely(!hdr)) { - return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); - } - memcpy(hdr, dat, dat_len + 1); - } - hdr[dat_len] = 0; -#endif - -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - if (!read_number(&cur, val, &msg)) { - if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); - return_err(cur, INVALID_NUMBER, msg); - } - if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); - if (yyjson_is_raw(val)) val->uni.str = dat; - return dat + (cur - hdr); -#else - if (!read_number(&cur, val, &msg)) { - return_err(cur, INVALID_NUMBER, msg); - } - return (const char *)cur; -#endif - -#undef return_err -} - -#endif /* YYJSON_DISABLE_READER */ - - - -#if !YYJSON_DISABLE_WRITER - -/*============================================================================== - * Integer Writer - * - * The maximum value of uint32_t is 4294967295 (10 digits), - * these digits are named as 'aabbccddee' here. - * - * Although most compilers may convert the "division by constant value" into - * "multiply and shift", manual conversion can still help some compilers - * generate fewer and better instructions. - * - * Reference: - * Division by Invariant Integers using Multiplication, 1994. - * https://gmplib.org/~tege/divcnst-pldi94.pdf - * Improved division by invariant integers, 2011. - * https://gmplib.org/~tege/division-paper.pdf - *============================================================================*/ - -/** Digit table from 00 to 99. */ -yyjson_align(2) -static const char digit_table[200] = { - '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', - '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', - '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', - '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', - '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', - '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', - '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', - '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', - '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', - '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', - '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', - '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', - '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', - '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', - '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', - '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', - '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', - '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', - '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', - '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' -}; - -static_inline u8 *write_u32_len_8(u32 val, u8 *buf) { - u32 aa, bb, cc, dd, aabb, ccdd; /* 8 digits: aabbccdd */ - aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ - ccdd = val - aabb * 10000; /* (val % 10000) */ - aa = (aabb * 5243) >> 19; /* (aabb / 100) */ - cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ - bb = aabb - aa * 100; /* (aabb % 100) */ - dd = ccdd - cc * 100; /* (ccdd % 100) */ - byte_copy_2(buf + 0, digit_table + aa * 2); - byte_copy_2(buf + 2, digit_table + bb * 2); - byte_copy_2(buf + 4, digit_table + cc * 2); - byte_copy_2(buf + 6, digit_table + dd * 2); - return buf + 8; -} - -static_inline u8 *write_u32_len_4(u32 val, u8 *buf) { - u32 aa, bb; /* 4 digits: aabb */ - aa = (val * 5243) >> 19; /* (val / 100) */ - bb = val - aa * 100; /* (val % 100) */ - byte_copy_2(buf + 0, digit_table + aa * 2); - byte_copy_2(buf + 2, digit_table + bb * 2); - return buf + 4; -} - -static_inline u8 *write_u32_len_1_8(u32 val, u8 *buf) { - u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; - - if (val < 100) { /* 1-2 digits: aa */ - lz = val < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + val * 2 + lz); - buf -= lz; - return buf + 2; - - } else if (val < 10000) { /* 3-4 digits: aabb */ - aa = (val * 5243) >> 19; /* (val / 100) */ - bb = val - aa * 100; /* (val % 100) */ - lz = aa < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + aa * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + bb * 2); - return buf + 4; - - } else if (val < 1000000) { /* 5-6 digits: aabbcc */ - aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ - bbcc = val - aa * 10000; /* (val % 10000) */ - bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ - cc = bbcc - bb * 100; /* (bbcc % 100) */ - lz = aa < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + aa * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + bb * 2); - byte_copy_2(buf + 4, digit_table + cc * 2); - return buf + 6; - - } else { /* 7-8 digits: aabbccdd */ - aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ - ccdd = val - aabb * 10000; /* (val % 10000) */ - aa = (aabb * 5243) >> 19; /* (aabb / 100) */ - cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ - bb = aabb - aa * 100; /* (aabb % 100) */ - dd = ccdd - cc * 100; /* (ccdd % 100) */ - lz = aa < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + aa * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + bb * 2); - byte_copy_2(buf + 4, digit_table + cc * 2); - byte_copy_2(buf + 6, digit_table + dd * 2); - return buf + 8; - } -} - -static_inline u8 *write_u64_len_5_8(u32 val, u8 *buf) { - u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; - - if (val < 1000000) { /* 5-6 digits: aabbcc */ - aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ - bbcc = val - aa * 10000; /* (val % 10000) */ - bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ - cc = bbcc - bb * 100; /* (bbcc % 100) */ - lz = aa < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + aa * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + bb * 2); - byte_copy_2(buf + 4, digit_table + cc * 2); - return buf + 6; - - } else { /* 7-8 digits: aabbccdd */ - aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ - ccdd = val - aabb * 10000; /* (val % 10000) */ - aa = (aabb * 5243) >> 19; /* (aabb / 100) */ - cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ - bb = aabb - aa * 100; /* (aabb % 100) */ - dd = ccdd - cc * 100; /* (ccdd % 100) */ - lz = aa < 10; /* leading zero: 0 or 1 */ - byte_copy_2(buf + 0, digit_table + aa * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + bb * 2); - byte_copy_2(buf + 4, digit_table + cc * 2); - byte_copy_2(buf + 6, digit_table + dd * 2); - return buf + 8; - } -} - -static_inline u8 *write_u64(u64 val, u8 *buf) { - u64 tmp, hgh; - u32 mid, low; - - if (val < 100000000) { /* 1-8 digits */ - buf = write_u32_len_1_8((u32)val, buf); - return buf; - - } else if (val < (u64)100000000 * 100000000) { /* 9-16 digits */ - hgh = val / 100000000; /* (val / 100000000) */ - low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ - buf = write_u32_len_1_8((u32)hgh, buf); - buf = write_u32_len_8(low, buf); - return buf; - - } else { /* 17-20 digits */ - tmp = val / 100000000; /* (val / 100000000) */ - low = (u32)(val - tmp * 100000000); /* (val % 100000000) */ - hgh = (u32)(tmp / 10000); /* (tmp / 10000) */ - mid = (u32)(tmp - hgh * 10000); /* (tmp % 10000) */ - buf = write_u64_len_5_8((u32)hgh, buf); - buf = write_u32_len_4(mid, buf); - buf = write_u32_len_8(low, buf); - return buf; - } -} - - - -/*============================================================================== - * Number Writer - *============================================================================*/ - -#if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ - -/** Trailing zero count table for number 0 to 99. - (generate with misc/make_tables.c) */ -static const u8 dec_trailing_zero_table[] = { - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -/** Write an unsigned integer with a length of 1 to 16. */ -static_inline u8 *write_u64_len_1_to_16(u64 val, u8 *buf) { - u64 hgh; - u32 low; - if (val < 100000000) { /* 1-8 digits */ - buf = write_u32_len_1_8((u32)val, buf); - return buf; - } else { /* 9-16 digits */ - hgh = val / 100000000; /* (val / 100000000) */ - low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ - buf = write_u32_len_1_8((u32)hgh, buf); - buf = write_u32_len_8(low, buf); - return buf; - } -} - -/** Write an unsigned integer with a length of 1 to 17. */ -static_inline u8 *write_u64_len_1_to_17(u64 val, u8 *buf) { - u64 hgh; - u32 mid, low, one; - if (val >= (u64)100000000 * 10000000) { /* len: 16 to 17 */ - hgh = val / 100000000; /* (val / 100000000) */ - low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ - one = (u32)(hgh / 100000000); /* (hgh / 100000000) */ - mid = (u32)(hgh - (u64)one * 100000000); /* (hgh % 100000000) */ - *buf = (u8)((u8)one + (u8)'0'); - buf += one > 0; - buf = write_u32_len_8(mid, buf); - buf = write_u32_len_8(low, buf); - return buf; - } else if (val >= (u64)100000000){ /* len: 9 to 15 */ - hgh = val / 100000000; /* (val / 100000000) */ - low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ - buf = write_u32_len_1_8((u32)hgh, buf); - buf = write_u32_len_8(low, buf); - return buf; - } else { /* len: 1 to 8 */ - buf = write_u32_len_1_8((u32)val, buf); - return buf; - } -} - -/** - Write an unsigned integer with a length of 15 to 17 with trailing zero trimmed. - These digits are named as "aabbccddeeffgghhii" here. - For example, input 1234567890123000, output "1234567890123". - */ -static_inline u8 *write_u64_len_15_to_17_trim(u8 *buf, u64 sig) { - bool lz; /* leading zero */ - u32 tz1, tz2, tz; /* trailing zero */ - - u32 abbccddee = (u32)(sig / 100000000); - u32 ffgghhii = (u32)(sig - (u64)abbccddee * 100000000); - u32 abbcc = abbccddee / 10000; /* (abbccddee / 10000) */ - u32 ddee = abbccddee - abbcc * 10000; /* (abbccddee % 10000) */ - u32 abb = (u32)(((u64)abbcc * 167773) >> 24); /* (abbcc / 100) */ - u32 a = (abb * 41) >> 12; /* (abb / 100) */ - u32 bb = abb - a * 100; /* (abb % 100) */ - u32 cc = abbcc - abb * 100; /* (abbcc % 100) */ - - /* write abbcc */ - buf[0] = (u8)(a + '0'); - buf += a > 0; - lz = bb < 10 && a == 0; - byte_copy_2(buf + 0, digit_table + bb * 2 + lz); - buf -= lz; - byte_copy_2(buf + 2, digit_table + cc * 2); - - if (ffgghhii) { - u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ - u32 ee = ddee - dd * 100; /* (ddee % 100) */ - u32 ffgg = (u32)(((u64)ffgghhii * 109951163) >> 40); /* (val / 10000) */ - u32 hhii = ffgghhii - ffgg * 10000; /* (val % 10000) */ - u32 ff = (ffgg * 5243) >> 19; /* (aabb / 100) */ - u32 gg = ffgg - ff * 100; /* (aabb % 100) */ - byte_copy_2(buf + 4, digit_table + dd * 2); - byte_copy_2(buf + 6, digit_table + ee * 2); - byte_copy_2(buf + 8, digit_table + ff * 2); - byte_copy_2(buf + 10, digit_table + gg * 2); - if (hhii) { - u32 hh = (hhii * 5243) >> 19; /* (ccdd / 100) */ - u32 ii = hhii - hh * 100; /* (ccdd % 100) */ - byte_copy_2(buf + 12, digit_table + hh * 2); - byte_copy_2(buf + 14, digit_table + ii * 2); - tz1 = dec_trailing_zero_table[hh]; - tz2 = dec_trailing_zero_table[ii]; - tz = ii ? tz2 : (tz1 + 2); - buf += 16 - tz; - return buf; - } else { - tz1 = dec_trailing_zero_table[ff]; - tz2 = dec_trailing_zero_table[gg]; - tz = gg ? tz2 : (tz1 + 2); - buf += 12 - tz; - return buf; - } - } else { - if (ddee) { - u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ - u32 ee = ddee - dd * 100; /* (ddee % 100) */ - byte_copy_2(buf + 4, digit_table + dd * 2); - byte_copy_2(buf + 6, digit_table + ee * 2); - tz1 = dec_trailing_zero_table[dd]; - tz2 = dec_trailing_zero_table[ee]; - tz = ee ? tz2 : (tz1 + 2); - buf += 8 - tz; - return buf; - } else { - tz1 = dec_trailing_zero_table[bb]; - tz2 = dec_trailing_zero_table[cc]; - tz = cc ? tz2 : (tz1 + tz2); - buf += 4 - tz; - return buf; - } - } -} - -/** Write a signed integer in the range -324 to 308. */ -static_inline u8 *write_f64_exp(i32 exp, u8 *buf) { - buf[0] = '-'; - buf += exp < 0; - exp = exp < 0 ? -exp : exp; - if (exp < 100) { - u32 lz = exp < 10; - byte_copy_2(buf + 0, digit_table + (u32)exp * 2 + lz); - return buf + 2 - lz; - } else { - u32 hi = ((u32)exp * 656) >> 16; /* exp / 100 */ - u32 lo = (u32)exp - hi * 100; /* exp % 100 */ - buf[0] = (u8)((u8)hi + (u8)'0'); - byte_copy_2(buf + 1, digit_table + lo * 2); - return buf + 3; - } -} - -/** Multiplies 128-bit integer and returns highest 64-bit rounded value. */ -static_inline u64 round_to_odd(u64 hi, u64 lo, u64 cp) { - u64 x_hi, x_lo, y_hi, y_lo; - u128_mul(cp, lo, &x_hi, &x_lo); - u128_mul_add(cp, hi, x_hi, &y_hi, &y_lo); - return y_hi | (y_lo > 1); -} - -/** - Convert double number from binary to decimal. - The output significand is shortest decimal but may have trailing zeros. - - This function use the Schubfach algorithm: - Raffaello Giulietti, The Schubfach way to render doubles (5th version), 2022. - https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb - https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-November/083536.html - https://github.com/openjdk/jdk/pull/3402 (Java implementation) - https://github.com/abolz/Drachennest (C++ implementation) - - See also: - Dragonbox: A New Floating-Point Binary-to-Decimal Conversion Algorithm, 2022. - https://github.com/jk-jeon/dragonbox/blob/master/other_files/Dragonbox.pdf - https://github.com/jk-jeon/dragonbox - - @param sig_raw The raw value of significand in IEEE 754 format. - @param exp_raw The raw value of exponent in IEEE 754 format. - @param sig_bin The decoded value of significand in binary. - @param exp_bin The decoded value of exponent in binary. - @param sig_dec The output value of significand in decimal. - @param exp_dec The output value of exponent in decimal. - @warning The input double number should not be 0, inf, nan. - */ -static_inline void f64_bin_to_dec(u64 sig_raw, u32 exp_raw, - u64 sig_bin, i32 exp_bin, - u64 *sig_dec, i32 *exp_dec) { - - bool is_even, regular_spacing, u_inside, w_inside, round_up; - u64 s, sp, cb, cbl, cbr, vb, vbl, vbr, pow10hi, pow10lo, upper, lower, mid; - i32 k, h, exp10; - - is_even = !(sig_bin & 1); - regular_spacing = (sig_raw == 0 && exp_raw > 1); - - cbl = 4 * sig_bin - 2 + regular_spacing; - cb = 4 * sig_bin; - cbr = 4 * sig_bin + 2; - - /* exp_bin: [-1074, 971] */ - /* k = regular_spacing ? floor(log10(pow(2, exp_bin))) */ - /* : floor(log10(pow(2, exp_bin) * 3.0 / 4.0)) */ - /* = regular_spacing ? floor(exp_bin * log10(2)) */ - /* : floor(exp_bin * log10(2) + log10(3.0 / 4.0)) */ - k = (i32)(exp_bin * 315653 - (regular_spacing ? 131237 : 0)) >> 20; - - /* k: [-324, 292] */ - /* h = exp_bin + floor(log2(pow(10, e))) */ - /* = exp_bin + floor(log2(10) * e) */ - exp10 = -k; - h = exp_bin + ((exp10 * 217707) >> 16) + 1; - - pow10_table_get_sig(exp10, &pow10hi, &pow10lo); - pow10lo += (exp10 < POW10_SIG_TABLE_MIN_EXACT_EXP || - exp10 > POW10_SIG_TABLE_MAX_EXACT_EXP); - vbl = round_to_odd(pow10hi, pow10lo, cbl << h); - vb = round_to_odd(pow10hi, pow10lo, cb << h); - vbr = round_to_odd(pow10hi, pow10lo, cbr << h); - - lower = vbl + !is_even; - upper = vbr - !is_even; - - s = vb / 4; - if (s >= 10) { - sp = s / 10; - u_inside = (lower <= 40 * sp); - w_inside = (upper >= 40 * sp + 40); - if (u_inside != w_inside) { - *sig_dec = sp + w_inside; - *exp_dec = k + 1; - return; - } - } - - u_inside = (lower <= 4 * s); - w_inside = (upper >= 4 * s + 4); - - mid = 4 * s + 2; - round_up = (vb > mid) || (vb == mid && (s & 1) != 0); - - *sig_dec = s + ((u_inside != w_inside) ? w_inside : round_up); - *exp_dec = k; -} - -/** - Write a double number (requires 32 bytes buffer). - - We follows the ECMAScript specification to print floating point numbers, - but with the following changes: - 1. Keep the negative sign of 0.0 to preserve input information. - 2. Keep decimal point to indicate the number is floating point. - 3. Remove positive sign of exponent part. - */ -static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { - u64 sig_bin, sig_dec, sig_raw; - i32 exp_bin, exp_dec, sig_len, dot_pos, i, max; - u32 exp_raw, hi, lo; - u8 *hdr, *num_hdr, *num_end, *dot_end; - bool sign; - - /* decode raw bytes from IEEE-754 double format. */ - sign = (bool)(raw >> (F64_BITS - 1)); - sig_raw = raw & F64_SIG_MASK; - exp_raw = (u32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); - - /* return inf and nan */ - if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { - if (has_write_flag(INF_AND_NAN_AS_NULL)) { - byte_copy_4(buf, "null"); - return buf + 4; - } - else if (has_write_flag(ALLOW_INF_AND_NAN)) { - if (sig_raw == 0) { - buf[0] = '-'; - buf += sign; - byte_copy_8(buf, "Infinity"); - buf += 8; - return buf; - } else { - byte_copy_4(buf, "NaN"); - return buf + 3; - } - } - return NULL; - } - - /* add sign for all finite double value, including 0.0 and inf */ - buf[0] = '-'; - buf += sign; - hdr = buf; - - /* return zero */ - if ((raw << 1) == 0) { - byte_copy_4(buf, "0.0"); - buf += 3; - return buf; - } - - if (likely(exp_raw != 0)) { - /* normal number */ - sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS); - exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS; - - /* fast path for small integer number without fraction */ - if (-F64_SIG_BITS <= exp_bin && exp_bin <= 0) { - if (u64_tz_bits(sig_bin) >= (u32)-exp_bin) { - /* number is integer in range 1 to 0x1FFFFFFFFFFFFF */ - sig_dec = sig_bin >> -exp_bin; - buf = write_u64_len_1_to_16(sig_dec, buf); - byte_copy_2(buf, ".0"); - buf += 2; - return buf; - } - } - - /* binary to decimal */ - f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); - - /* the sig length is 15 to 17 */ - sig_len = 17; - sig_len -= (sig_dec < (u64)100000000 * 100000000); - sig_len -= (sig_dec < (u64)100000000 * 10000000); - - /* the decimal point position relative to the first digit */ - dot_pos = sig_len + exp_dec; - - if (-6 < dot_pos && dot_pos <= 21) { - /* no need to write exponent part */ - if (dot_pos <= 0) { - /* dot before first digit */ - /* such as 0.1234, 0.000001234 */ - num_hdr = hdr + (2 - dot_pos); - num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec); - hdr[0] = '0'; - hdr[1] = '.'; - hdr += 2; - max = -dot_pos; - for (i = 0; i < max; i++) hdr[i] = '0'; - return num_end; - } else { - /* dot after first digit */ - /* such as 1.234, 1234.0, 123400000000000000000.0 */ - memset(hdr + 0, '0', 8); - memset(hdr + 8, '0', 8); - memset(hdr + 16, '0', 8); - num_hdr = hdr + 1; - num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec); - for (i = 0; i < dot_pos; i++) hdr[i] = hdr[i + 1]; - hdr[dot_pos] = '.'; - dot_end = hdr + dot_pos + 2; - return dot_end < num_end ? num_end : dot_end; - } - } else { - /* write with scientific notation */ - /* such as 1.234e56 */ - u8 *end = write_u64_len_15_to_17_trim(buf + 1, sig_dec); - end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ - exp_dec += sig_len - 1; - hdr[0] = hdr[1]; - hdr[1] = '.'; - end[0] = 'e'; - buf = write_f64_exp(exp_dec, end + 1); - return buf; - } - - } else { - /* subnormal number */ - sig_bin = sig_raw; - exp_bin = 1 - F64_EXP_BIAS - F64_SIG_BITS; - - /* binary to decimal */ - f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); - - /* write significand part */ - buf = write_u64_len_1_to_17(sig_dec, buf + 1); - hdr[0] = hdr[1]; - hdr[1] = '.'; - do { - buf--; - exp_dec++; - } while (*buf == '0'); - exp_dec += (i32)(buf - hdr - 2); - buf += (*buf != '.'); - buf[0] = 'e'; - buf++; - - /* write exponent part */ - buf[0] = '-'; - buf++; - exp_dec = -exp_dec; - hi = ((u32)exp_dec * 656) >> 16; /* exp / 100 */ - lo = (u32)exp_dec - hi * 100; /* exp % 100 */ - buf[0] = (u8)((u8)hi + (u8)'0'); - byte_copy_2(buf + 1, digit_table + lo * 2); - buf += 3; - return buf; - } -} - -#else /* FP_WRITER */ - -/** Write a double number (requires 32 bytes buffer). */ -static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { - /* - For IEEE 754, `DBL_DECIMAL_DIG` is 17 for round-trip. - For non-IEEE formats, 17 is used to avoid buffer overflow, - round-trip is not guaranteed. - */ -#if defined(DBL_DECIMAL_DIG) && DBL_DECIMAL_DIG != 17 - int dig = DBL_DECIMAL_DIG > 17 ? 17 : DBL_DECIMAL_DIG; -#else - int dig = 17; -#endif - - /* - The snprintf() function is locale-dependent. For currently known locales, - (en, zh, ja, ko, am, he, hi) use '.' as the decimal point, while other - locales use ',' as the decimal point. we need to replace ',' with '.' - to avoid the locale setting. - */ - f64 val = f64_from_raw(raw); -#if YYJSON_MSC_VER >= 1400 - int len = sprintf_s((char *)buf, 32, "%.*g", dig, val); -#elif defined(snprintf) || (YYJSON_STDC_VER >= 199901L) - int len = snprintf((char *)buf, 32, "%.*g", dig, val); -#else - int len = sprintf((char *)buf, "%.*g", dig, val); -#endif - - u8 *cur = buf; - if (unlikely(len < 1)) return NULL; - cur += (*cur == '-'); - if (unlikely(!digi_is_digit(*cur))) { - /* nan, inf, or bad output */ - if (has_write_flag(INF_AND_NAN_AS_NULL)) { - byte_copy_4(buf, "null"); - return buf + 4; - } - else if (has_write_flag(ALLOW_INF_AND_NAN)) { - if (*cur == 'i') { - byte_copy_8(cur, "Infinity"); - cur += 8; - return cur; - } else if (*cur == 'n') { - byte_copy_4(buf, "NaN"); - return buf + 3; - } - } - return NULL; - } else { - /* finite number */ - int i = 0; - bool fp = false; - for (; i < len; i++) { - if (buf[i] == ',') buf[i] = '.'; - if (digi_is_fp((u8)buf[i])) fp = true; - } - if (!fp) { - buf[len++] = '.'; - buf[len++] = '0'; - } - } - return buf + len; -} - -#endif /* FP_WRITER */ - -/** Write a JSON number (requires 32 bytes buffer). */ -static_inline u8 *write_number(u8 *cur, yyjson_val *val, - yyjson_write_flag flg) { - if (val->tag & YYJSON_SUBTYPE_REAL) { - u64 raw = val->uni.u64; - return write_f64_raw(cur, raw, flg); - } else { - u64 pos = val->uni.u64; - u64 neg = ~pos + 1; - usize sgn = ((val->tag & YYJSON_SUBTYPE_SINT) > 0) & ((i64)pos < 0); - *cur = '-'; - return write_u64(sgn ? neg : pos, cur + sgn); - } -} - - - -/*============================================================================== - * String Writer - *============================================================================*/ - -/** Character encode type, if (type > CHAR_ENC_ERR_1) bytes = type / 2; */ -typedef u8 char_enc_type; -#define CHAR_ENC_CPY_1 0 /* 1-byte UTF-8, copy. */ -#define CHAR_ENC_ERR_1 1 /* 1-byte UTF-8, error. */ -#define CHAR_ENC_ESC_A 2 /* 1-byte ASCII, escaped as '\x'. */ -#define CHAR_ENC_ESC_1 3 /* 1-byte UTF-8, escaped as '\uXXXX'. */ -#define CHAR_ENC_CPY_2 4 /* 2-byte UTF-8, copy. */ -#define CHAR_ENC_ESC_2 5 /* 2-byte UTF-8, escaped as '\uXXXX'. */ -#define CHAR_ENC_CPY_3 6 /* 3-byte UTF-8, copy. */ -#define CHAR_ENC_ESC_3 7 /* 3-byte UTF-8, escaped as '\uXXXX'. */ -#define CHAR_ENC_CPY_4 8 /* 4-byte UTF-8, copy. */ -#define CHAR_ENC_ESC_4 9 /* 4-byte UTF-8, escaped as '\uXXXX\uXXXX'. */ - -/** Character encode type table: don't escape unicode, don't escape '/'. - (generate with misc/make_tables.c) */ -static const char_enc_type enc_table_cpy[256] = { - 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 -}; - -/** Character encode type table: don't escape unicode, escape '/'. - (generate with misc/make_tables.c) */ -static const char_enc_type enc_table_cpy_slash[256] = { - 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 -}; - -/** Character encode type table: escape unicode, don't escape '/'. - (generate with misc/make_tables.c) */ -static const char_enc_type enc_table_esc[256] = { - 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 -}; - -/** Character encode type table: escape unicode, escape '/'. - (generate with misc/make_tables.c) */ -static const char_enc_type enc_table_esc_slash[256] = { - 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, - 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 -}; - -/** Escaped hex character table: ["00" "01" "02" ... "FD" "FE" "FF"]. - (generate with misc/make_tables.c) */ -yyjson_align(2) -static const u8 esc_hex_char_table[512] = { - '0', '0', '0', '1', '0', '2', '0', '3', - '0', '4', '0', '5', '0', '6', '0', '7', - '0', '8', '0', '9', '0', 'A', '0', 'B', - '0', 'C', '0', 'D', '0', 'E', '0', 'F', - '1', '0', '1', '1', '1', '2', '1', '3', - '1', '4', '1', '5', '1', '6', '1', '7', - '1', '8', '1', '9', '1', 'A', '1', 'B', - '1', 'C', '1', 'D', '1', 'E', '1', 'F', - '2', '0', '2', '1', '2', '2', '2', '3', - '2', '4', '2', '5', '2', '6', '2', '7', - '2', '8', '2', '9', '2', 'A', '2', 'B', - '2', 'C', '2', 'D', '2', 'E', '2', 'F', - '3', '0', '3', '1', '3', '2', '3', '3', - '3', '4', '3', '5', '3', '6', '3', '7', - '3', '8', '3', '9', '3', 'A', '3', 'B', - '3', 'C', '3', 'D', '3', 'E', '3', 'F', - '4', '0', '4', '1', '4', '2', '4', '3', - '4', '4', '4', '5', '4', '6', '4', '7', - '4', '8', '4', '9', '4', 'A', '4', 'B', - '4', 'C', '4', 'D', '4', 'E', '4', 'F', - '5', '0', '5', '1', '5', '2', '5', '3', - '5', '4', '5', '5', '5', '6', '5', '7', - '5', '8', '5', '9', '5', 'A', '5', 'B', - '5', 'C', '5', 'D', '5', 'E', '5', 'F', - '6', '0', '6', '1', '6', '2', '6', '3', - '6', '4', '6', '5', '6', '6', '6', '7', - '6', '8', '6', '9', '6', 'A', '6', 'B', - '6', 'C', '6', 'D', '6', 'E', '6', 'F', - '7', '0', '7', '1', '7', '2', '7', '3', - '7', '4', '7', '5', '7', '6', '7', '7', - '7', '8', '7', '9', '7', 'A', '7', 'B', - '7', 'C', '7', 'D', '7', 'E', '7', 'F', - '8', '0', '8', '1', '8', '2', '8', '3', - '8', '4', '8', '5', '8', '6', '8', '7', - '8', '8', '8', '9', '8', 'A', '8', 'B', - '8', 'C', '8', 'D', '8', 'E', '8', 'F', - '9', '0', '9', '1', '9', '2', '9', '3', - '9', '4', '9', '5', '9', '6', '9', '7', - '9', '8', '9', '9', '9', 'A', '9', 'B', - '9', 'C', '9', 'D', '9', 'E', '9', 'F', - 'A', '0', 'A', '1', 'A', '2', 'A', '3', - 'A', '4', 'A', '5', 'A', '6', 'A', '7', - 'A', '8', 'A', '9', 'A', 'A', 'A', 'B', - 'A', 'C', 'A', 'D', 'A', 'E', 'A', 'F', - 'B', '0', 'B', '1', 'B', '2', 'B', '3', - 'B', '4', 'B', '5', 'B', '6', 'B', '7', - 'B', '8', 'B', '9', 'B', 'A', 'B', 'B', - 'B', 'C', 'B', 'D', 'B', 'E', 'B', 'F', - 'C', '0', 'C', '1', 'C', '2', 'C', '3', - 'C', '4', 'C', '5', 'C', '6', 'C', '7', - 'C', '8', 'C', '9', 'C', 'A', 'C', 'B', - 'C', 'C', 'C', 'D', 'C', 'E', 'C', 'F', - 'D', '0', 'D', '1', 'D', '2', 'D', '3', - 'D', '4', 'D', '5', 'D', '6', 'D', '7', - 'D', '8', 'D', '9', 'D', 'A', 'D', 'B', - 'D', 'C', 'D', 'D', 'D', 'E', 'D', 'F', - 'E', '0', 'E', '1', 'E', '2', 'E', '3', - 'E', '4', 'E', '5', 'E', '6', 'E', '7', - 'E', '8', 'E', '9', 'E', 'A', 'E', 'B', - 'E', 'C', 'E', 'D', 'E', 'E', 'E', 'F', - 'F', '0', 'F', '1', 'F', '2', 'F', '3', - 'F', '4', 'F', '5', 'F', '6', 'F', '7', - 'F', '8', 'F', '9', 'F', 'A', 'F', 'B', - 'F', 'C', 'F', 'D', 'F', 'E', 'F', 'F' -}; - -/** Escaped single character table. (generate with misc/make_tables.c) */ -yyjson_align(2) -static const u8 esc_single_char_table[512] = { - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ', - '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' -}; - -/** Returns the encode table with options. */ -static_inline const char_enc_type *get_enc_table_with_flag( - yyjson_read_flag flg) { - if (has_write_flag(ESCAPE_UNICODE)) { - if (has_write_flag(ESCAPE_SLASHES)) { - return enc_table_esc_slash; - } else { - return enc_table_esc; - } - } else { - if (has_write_flag(ESCAPE_SLASHES)) { - return enc_table_cpy_slash; - } else { - return enc_table_cpy; - } - } -} - -/** Write raw string. */ -static_inline u8 *write_raw(u8 *cur, const u8 *raw, usize raw_len) { - memcpy(cur, raw, raw_len); - return cur + raw_len; -} - -/** - Write string no-escape. - @param cur Buffer cursor. - @param str A UTF-8 string, null-terminator is not required. - @param str_len Length of string in bytes. - @return The buffer cursor after string. - */ -static_inline u8 *write_string_noesc(u8 *cur, const u8 *str, usize str_len) { - *cur++ = '"'; - while (str_len >= 16) { - byte_copy_16(cur, str); - cur += 16; - str += 16; - str_len -= 16; - } - while (str_len >= 4) { - byte_copy_4(cur, str); - cur += 4; - str += 4; - str_len -= 4; - } - while (str_len) { - *cur++ = *str++; - str_len -= 1; - } - *cur++ = '"'; - return cur; -} - -/** - Write UTF-8 string (requires len * 6 + 2 bytes buffer). - @param cur Buffer cursor. - @param esc Escape unicode. - @param inv Allow invalid unicode. - @param str A UTF-8 string, null-terminator is not required. - @param str_len Length of string in bytes. - @param enc_table Encode type table for character. - @return The buffer cursor after string, or NULL on invalid unicode. - */ -static_inline u8 *write_string(u8 *cur, bool esc, bool inv, - const u8 *str, usize str_len, - const char_enc_type *enc_table) { - - /* UTF-8 character mask and pattern, see `read_string()` for details. */ -#if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN - const u16 b2_mask = 0xE0C0UL; - const u16 b2_patt = 0xC080UL; - const u16 b2_requ = 0x1E00UL; - const u32 b3_mask = 0xF0C0C000UL; - const u32 b3_patt = 0xE0808000UL; - const u32 b3_requ = 0x0F200000UL; - const u32 b3_erro = 0x0D200000UL; - const u32 b4_mask = 0xF8C0C0C0UL; - const u32 b4_patt = 0xF0808080UL; - const u32 b4_requ = 0x07300000UL; - const u32 b4_err0 = 0x04000000UL; - const u32 b4_err1 = 0x03300000UL; -#elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN - const u16 b2_mask = 0xC0E0UL; - const u16 b2_patt = 0x80C0UL; - const u16 b2_requ = 0x001EUL; - const u32 b3_mask = 0x00C0C0F0UL; - const u32 b3_patt = 0x008080E0UL; - const u32 b3_requ = 0x0000200FUL; - const u32 b3_erro = 0x0000200DUL; - const u32 b4_mask = 0xC0C0C0F8UL; - const u32 b4_patt = 0x808080F0UL; - const u32 b4_requ = 0x00003007UL; - const u32 b4_err0 = 0x00000004UL; - const u32 b4_err1 = 0x00003003UL; -#else - /* this should be evaluated at compile-time */ - v16_uni b2_mask_uni = {{ 0xE0, 0xC0 }}; - v16_uni b2_patt_uni = {{ 0xC0, 0x80 }}; - v16_uni b2_requ_uni = {{ 0x1E, 0x00 }}; - v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; - v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; - v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; - v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; - v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; - v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; - v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; - v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; - v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; - u16 b2_mask = b2_mask_uni.u; - u16 b2_patt = b2_patt_uni.u; - u16 b2_requ = b2_requ_uni.u; - u32 b3_mask = b3_mask_uni.u; - u32 b3_patt = b3_patt_uni.u; - u32 b3_requ = b3_requ_uni.u; - u32 b3_erro = b3_erro_uni.u; - u32 b4_mask = b4_mask_uni.u; - u32 b4_patt = b4_patt_uni.u; - u32 b4_requ = b4_requ_uni.u; - u32 b4_err0 = b4_err0_uni.u; - u32 b4_err1 = b4_err1_uni.u; -#endif - -#define is_valid_seq_2(uni) ( \ - ((uni & b2_mask) == b2_patt) && \ - ((uni & b2_requ)) \ -) - -#define is_valid_seq_3(uni) ( \ - ((uni & b3_mask) == b3_patt) && \ - ((tmp = (uni & b3_requ))) && \ - ((tmp != b3_erro)) \ -) - -#define is_valid_seq_4(uni) ( \ - ((uni & b4_mask) == b4_patt) && \ - ((tmp = (uni & b4_requ))) && \ - ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ -) - - /* The replacement character U+FFFD, used to indicate invalid character. */ - const v32 rep = {{ 'F', 'F', 'F', 'D' }}; - const v32 pre = {{ '\\', 'u', '0', '0' }}; - - const u8 *src = str; - const u8 *end = str + str_len; - *cur++ = '"'; - -copy_ascii: - /* - Copy continuous ASCII, loop unrolling, same as the following code: - - while (end > src) ( - if (unlikely(enc_table[*src])) break; - *cur++ = *src++; - ); - */ -#define expr_jump(i) \ - if (unlikely(enc_table[src[i]])) goto stop_char_##i; - -#define expr_stop(i) \ - stop_char_##i: \ - memcpy(cur, src, i); \ - cur += i; src += i; goto copy_utf8; - - while (end - src >= 16) { - repeat16_incr(expr_jump) - byte_copy_16(cur, src); - cur += 16; src += 16; - } - - while (end - src >= 4) { - repeat4_incr(expr_jump) - byte_copy_4(cur, src); - cur += 4; src += 4; - } - - while (end > src) { - expr_jump(0) - *cur++ = *src++; - } - - *cur++ = '"'; - return cur; - - repeat16_incr(expr_stop) - -#undef expr_jump -#undef expr_stop - -copy_utf8: - if (unlikely(src + 4 > end)) { - if (end == src) goto copy_end; - if (end - src < enc_table[*src] / 2) goto err_one; - } - switch (enc_table[*src]) { - case CHAR_ENC_CPY_1: { - *cur++ = *src++; - goto copy_ascii; - } - case CHAR_ENC_CPY_2: { - u16 v; -#if YYJSON_DISABLE_UTF8_VALIDATION - byte_copy_2(cur, src); -#else - v = byte_load_2(src); - if (unlikely(!is_valid_seq_2(v))) goto err_cpy; - byte_copy_2(cur, src); -#endif - cur += 2; - src += 2; - goto copy_utf8; - } - case CHAR_ENC_CPY_3: { - u32 v, tmp; -#if YYJSON_DISABLE_UTF8_VALIDATION - if (likely(src + 4 <= end)) { - byte_copy_4(cur, src); - } else { - byte_copy_2(cur, src); - cur[2] = src[2]; - } -#else - if (likely(src + 4 <= end)) { - v = byte_load_4(src); - if (unlikely(!is_valid_seq_3(v))) goto err_cpy; - byte_copy_4(cur, src); - } else { - v = byte_load_3(src); - if (unlikely(!is_valid_seq_3(v))) goto err_cpy; - byte_copy_4(cur, &v); - } -#endif - cur += 3; - src += 3; - goto copy_utf8; - } - case CHAR_ENC_CPY_4: { - u32 v, tmp; -#if YYJSON_DISABLE_UTF8_VALIDATION - byte_copy_4(cur, src); -#else - v = byte_load_4(src); - if (unlikely(!is_valid_seq_4(v))) goto err_cpy; - byte_copy_4(cur, src); -#endif - cur += 4; - src += 4; - goto copy_utf8; - } - case CHAR_ENC_ESC_A: { - byte_copy_2(cur, &esc_single_char_table[*src * 2]); - cur += 2; - src += 1; - goto copy_utf8; - } - case CHAR_ENC_ESC_1: { - byte_copy_4(cur + 0, &pre); - byte_copy_2(cur + 4, &esc_hex_char_table[*src * 2]); - cur += 6; - src += 1; - goto copy_utf8; - } - case CHAR_ENC_ESC_2: { - u16 u, v; -#if !YYJSON_DISABLE_UTF8_VALIDATION - v = byte_load_2(src); - if (unlikely(!is_valid_seq_2(v))) goto err_esc; -#endif - u = (u16)(((u16)(src[0] & 0x1F) << 6) | - ((u16)(src[1] & 0x3F) << 0)); - byte_copy_2(cur + 0, &pre); - byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); - byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); - cur += 6; - src += 2; - goto copy_utf8; - } - case CHAR_ENC_ESC_3: { - u16 u; - u32 v, tmp; -#if !YYJSON_DISABLE_UTF8_VALIDATION - v = byte_load_3(src); - if (unlikely(!is_valid_seq_3(v))) goto err_esc; -#endif - u = (u16)(((u16)(src[0] & 0x0F) << 12) | - ((u16)(src[1] & 0x3F) << 6) | - ((u16)(src[2] & 0x3F) << 0)); - byte_copy_2(cur + 0, &pre); - byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); - byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); - cur += 6; - src += 3; - goto copy_utf8; - } - case CHAR_ENC_ESC_4: { - u32 hi, lo, u, v, tmp; -#if !YYJSON_DISABLE_UTF8_VALIDATION - v = byte_load_4(src); - if (unlikely(!is_valid_seq_4(v))) goto err_esc; -#endif - u = ((u32)(src[0] & 0x07) << 18) | - ((u32)(src[1] & 0x3F) << 12) | - ((u32)(src[2] & 0x3F) << 6) | - ((u32)(src[3] & 0x3F) << 0); - u -= 0x10000; - hi = (u >> 10) + 0xD800; - lo = (u & 0x3FF) + 0xDC00; - byte_copy_2(cur + 0, &pre); - byte_copy_2(cur + 2, &esc_hex_char_table[(hi >> 8) * 2]); - byte_copy_2(cur + 4, &esc_hex_char_table[(hi & 0xFF) * 2]); - byte_copy_2(cur + 6, &pre); - byte_copy_2(cur + 8, &esc_hex_char_table[(lo >> 8) * 2]); - byte_copy_2(cur + 10, &esc_hex_char_table[(lo & 0xFF) * 2]); - cur += 12; - src += 4; - goto copy_utf8; - } - case CHAR_ENC_ERR_1: { - goto err_one; - } - default: break; - } - -copy_end: - *cur++ = '"'; - return cur; - -err_one: - if (esc) goto err_esc; - else goto err_cpy; - -err_cpy: - if (!inv) return NULL; - *cur++ = *src++; - goto copy_utf8; - -err_esc: - if (!inv) return NULL; - byte_copy_2(cur + 0, &pre); - byte_copy_4(cur + 2, &rep); - cur += 6; - src += 1; - goto copy_utf8; - -#undef is_valid_seq_2 -#undef is_valid_seq_3 -#undef is_valid_seq_4 -} - - - -/*============================================================================== - * Writer Utilities - *============================================================================*/ - -/** Write null (requires 8 bytes buffer). */ -static_inline u8 *write_null(u8 *cur) { - v64 v = {{ 'n', 'u', 'l', 'l', ',', '\n', 0, 0 }}; - byte_copy_8(cur, &v); - return cur + 4; -} - -/** Write bool (requires 8 bytes buffer). */ -static_inline u8 *write_bool(u8 *cur, bool val) { - v64 v0 = {{ 'f', 'a', 'l', 's', 'e', ',', '\n', 0 }}; - v64 v1 = {{ 't', 'r', 'u', 'e', ',', '\n', 0, 0 }}; - if (val) { - byte_copy_8(cur, &v1); - } else { - byte_copy_8(cur, &v0); - } - return cur + 5 - val; -} - -/** Write indent (requires level x 4 bytes buffer). - Param spaces should not larger than 4. */ -static_inline u8 *write_indent(u8 *cur, usize level, usize spaces) { - while (level-- > 0) { - byte_copy_4(cur, " "); - cur += spaces; - } - return cur; -} - -/** Write data to file pointer. */ -static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, - yyjson_write_err *err) { - if (fwrite(dat, len, 1, fp) != 1) { - err->msg = "file writing failed"; - err->code = YYJSON_WRITE_ERROR_FILE_WRITE; - return false; - } - return true; -} - -/** Write data to file. */ -static bool write_dat_to_file(const char *path, u8 *dat, usize len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - err->msg = _msg; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - if (file) fclose(file); \ - return false; \ -} while (false) - - FILE *file = fopen_writeonly(path); - if (file == NULL) { - return_err(FILE_OPEN, "file opening failed"); - } - if (fwrite(dat, len, 1, file) != 1) { - return_err(FILE_WRITE, "file writing failed"); - } - if (fclose(file) != 0) { - file = NULL; - return_err(FILE_WRITE, "file closing failed"); - } - return true; - -#undef return_err -} - - - -/*============================================================================== - * JSON Writer Implementation - *============================================================================*/ - -typedef struct yyjson_write_ctx { - usize tag; -} yyjson_write_ctx; - -static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx, - usize size, bool is_obj) { - ctx->tag = (size << 1) | (usize)is_obj; -} - -static_inline void yyjson_write_ctx_get(yyjson_write_ctx *ctx, - usize *size, bool *is_obj) { - usize tag = ctx->tag; - *size = tag >> 1; - *is_obj = (bool)(tag & 1); -} - -/** Write single JSON value. */ -static_inline u8 *yyjson_write_single(yyjson_val *val, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - if (hdr) alc.free(alc.ctx, (void *)hdr); \ - *dat_len = 0; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - err->msg = _msg; \ - return NULL; \ -} while (false) - -#define incr_len(_len) do { \ - hdr = (u8 *)alc.malloc(alc.ctx, _len); \ - if (!hdr) goto fail_alloc; \ - cur = hdr; \ -} while (false) - -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) - - u8 *hdr = NULL, *cur; - usize str_len; - const u8 *str_ptr; - const char_enc_type *enc_table = get_enc_table_with_flag(flg); - bool cpy = (enc_table == enc_table_cpy); - bool esc = has_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(NEWLINE_AT_END) != 0; - const usize end_len = 2; /* '\n' and '\0' */ - - switch (unsafe_yyjson_get_type(val)) { - case YYJSON_TYPE_RAW: - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len + end_len); - cur = write_raw(cur, str_ptr, str_len); - break; - - case YYJSON_TYPE_STR: - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 2 + end_len); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - break; - - case YYJSON_TYPE_NUM: - incr_len(32 + end_len); - cur = write_number(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - break; - - case YYJSON_TYPE_BOOL: - incr_len(8); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - break; - - case YYJSON_TYPE_NULL: - incr_len(8); - cur = write_null(cur); - break; - - case YYJSON_TYPE_ARR: - incr_len(2 + end_len); - byte_copy_2(cur, "[]"); - cur += 2; - break; - - case YYJSON_TYPE_OBJ: - incr_len(2 + end_len); - byte_copy_2(cur, "{}"); - cur += 2; - break; - - default: - goto fail_type; - } - - if (newline) *cur++ = '\n'; - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef check_str_len -#undef incr_len -} - -/** Write JSON document minify. - The root of this document should be a non-empty container. */ -static_inline u8 *yyjson_write_minify(const yyjson_val *root, - const yyjson_write_flag flg, - const yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - *dat_len = 0; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - err->msg = _msg; \ - if (hdr) alc.free(alc.ctx, hdr); \ - return NULL; \ -} while (false) - -#define incr_len(_len) do { \ - ext_len = (usize)(_len); \ - if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ - alc_inc = yyjson_max(alc_len / 2, ext_len); \ - alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ - if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ - goto fail_alloc; \ - alc_len += alc_inc; \ - tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ - if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ - ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ - ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ - end = tmp + alc_len; \ - hdr = tmp; \ - } \ -} while (false) - -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) - - yyjson_val *val; - yyjson_type val_type; - usize ctn_len, ctn_len_tmp; - bool ctn_obj, ctn_obj_tmp, is_key; - u8 *hdr, *cur, *end, *tmp; - yyjson_write_ctx *ctx, *ctx_tmp; - usize alc_len, alc_inc, ctx_len, ext_len, str_len; - const u8 *str_ptr; - const char_enc_type *enc_table = get_enc_table_with_flag(flg); - bool cpy = (enc_table == enc_table_cpy); - bool esc = has_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(NEWLINE_AT_END) != 0; - - alc_len = root->uni.ofs / sizeof(yyjson_val); - alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; - alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); - hdr = (u8 *)alc.malloc(alc.ctx, alc_len); - if (!hdr) goto fail_alloc; - cur = hdr; - end = hdr + alc_len; - ctx = (yyjson_write_ctx *)(void *)end; - -doc_begin: - val = constcast(yyjson_val *)root; - val_type = unsafe_yyjson_get_type(val); - ctn_obj = (val_type == YYJSON_TYPE_OBJ); - ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - val++; - -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = ((u8)ctn_obj & (u8)~ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - incr_len(32); - cur = write_number(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - incr_len(16); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - goto val_end; - } else { - /* push context, setup new container */ - yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - val++; - goto val_begin; - } - } - if (val_type == YYJSON_TYPE_BOOL) { - incr_len(16); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur++; - goto val_end; - } - if (val_type == YYJSON_TYPE_NULL) { - incr_len(16); - cur = write_null(cur); - cur++; - goto val_end; - } - if (val_type == YYJSON_TYPE_RAW) { - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len + 2); - cur = write_raw(cur, str_ptr, str_len); - *cur++ = ','; - goto val_end; - } - goto fail_type; - -val_end: - val++; - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - goto val_begin; - -ctn_end: - cur--; - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - *cur++ = ','; - if (unlikely((u8 *)ctx >= end)) goto doc_end; - yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); - ctn_len--; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } - -doc_end: - if (newline) { - incr_len(2); - *(cur - 1) = '\n'; - cur++; - } - *--cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len -} - -/** Write JSON document pretty. - The root of this document should be a non-empty container. */ -static_inline u8 *yyjson_write_pretty(const yyjson_val *root, - const yyjson_write_flag flg, - const yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - *dat_len = 0; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - err->msg = _msg; \ - if (hdr) alc.free(alc.ctx, hdr); \ - return NULL; \ -} while (false) - -#define incr_len(_len) do { \ - ext_len = (usize)(_len); \ - if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ - alc_inc = yyjson_max(alc_len / 2, ext_len); \ - alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ - if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ - goto fail_alloc; \ - alc_len += alc_inc; \ - tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ - if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ - ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ - ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ - end = tmp + alc_len; \ - hdr = tmp; \ - } \ -} while (false) - -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) - - yyjson_val *val; - yyjson_type val_type; - usize ctn_len, ctn_len_tmp; - bool ctn_obj, ctn_obj_tmp, is_key, no_indent; - u8 *hdr, *cur, *end, *tmp; - yyjson_write_ctx *ctx, *ctx_tmp; - usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; - const u8 *str_ptr; - const char_enc_type *enc_table = get_enc_table_with_flag(flg); - bool cpy = (enc_table == enc_table_cpy); - bool esc = has_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; - bool newline = has_write_flag(NEWLINE_AT_END) != 0; - - alc_len = root->uni.ofs / sizeof(yyjson_val); - alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; - alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); - hdr = (u8 *)alc.malloc(alc.ctx, alc_len); - if (!hdr) goto fail_alloc; - cur = hdr; - end = hdr + alc_len; - ctx = (yyjson_write_ctx *)(void *)end; - -doc_begin: - val = constcast(yyjson_val *)root; - val_type = unsafe_yyjson_get_type(val); - ctn_obj = (val_type == YYJSON_TYPE_OBJ); - ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - *cur++ = '\n'; - val++; - level = 1; - -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - *cur++ = is_key ? ' ' : '\n'; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(32 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_number(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } else { - /* push context, setup new container */ - incr_len(32 + (no_indent ? 0 : level * 4)); - yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - cur = write_indent(cur, no_indent ? 0 : level, spaces); - level++; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - *cur++ = '\n'; - val++; - goto val_begin; - } - } - if (val_type == YYJSON_TYPE_BOOL) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur += 2; - goto val_end; - } - if (val_type == YYJSON_TYPE_NULL) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_null(cur); - cur += 2; - goto val_end; - } - if (val_type == YYJSON_TYPE_RAW) { - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len + 3); - cur = write_raw(cur, str_ptr, str_len); - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } - goto fail_type; - -val_end: - val++; - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - goto val_begin; - -ctn_end: - cur -= 2; - *cur++ = '\n'; - incr_len(level * 4); - cur = write_indent(cur, --level, spaces); - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - if (unlikely((u8 *)ctx >= end)) goto doc_end; - yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); - ctn_len--; - *cur++ = ','; - *cur++ = '\n'; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } - -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; - } - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len -} - -char *yyjson_val_write_opts(const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - usize dummy_dat_len; - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_val *root = constcast(yyjson_val *)val; - - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_dat_len; - - if (unlikely(!root)) { - *dat_len = 0; - err->msg = "input JSON is NULL"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return NULL; - } - - if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { - return (char *)yyjson_write_single(root, flg, alc, dat_len, err); - } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { - return (char *)yyjson_write_pretty(root, flg, alc, dat_len, err); - } else { - return (char *)yyjson_write_minify(root, flg, alc, dat_len, err); - } -} - -char *yyjson_write_opts(const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - yyjson_val *root = doc ? doc->root : NULL; - return yyjson_val_write_opts(root, flg, alc_ptr, dat_len, err); -} - -bool yyjson_val_write_file(const char *path, - const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_val *root = constcast(yyjson_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!path || !*path)) { - err->msg = "input path is invalid"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return false; - } - - dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; -} - -bool yyjson_val_write_fp(FILE *fp, - const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_val *root = constcast(yyjson_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return false; - } - - dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; -} - -bool yyjson_write_file(const char *path, - const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_val *root = doc ? doc->root : NULL; - return yyjson_val_write_file(path, root, flg, alc_ptr, err); -} - -bool yyjson_write_fp(FILE *fp, - const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_val *root = doc ? doc->root : NULL; - return yyjson_val_write_fp(fp, root, flg, alc_ptr, err); -} - - - -/*============================================================================== - * Mutable JSON Writer Implementation - *============================================================================*/ - -typedef struct yyjson_mut_write_ctx { - usize tag; - yyjson_mut_val *ctn; -} yyjson_mut_write_ctx; - -static_inline void yyjson_mut_write_ctx_set(yyjson_mut_write_ctx *ctx, - yyjson_mut_val *ctn, - usize size, bool is_obj) { - ctx->tag = (size << 1) | (usize)is_obj; - ctx->ctn = ctn; -} - -static_inline void yyjson_mut_write_ctx_get(yyjson_mut_write_ctx *ctx, - yyjson_mut_val **ctn, - usize *size, bool *is_obj) { - usize tag = ctx->tag; - *size = tag >> 1; - *is_obj = (bool)(tag & 1); - *ctn = ctx->ctn; -} - -/** Get the estimated number of values for the mutable JSON document. */ -static_inline usize yyjson_mut_doc_estimated_val_num( - const yyjson_mut_doc *doc) { - usize sum = 0; - yyjson_val_chunk *chunk = doc->val_pool.chunks; - while (chunk) { - sum += chunk->chunk_size / sizeof(yyjson_mut_val) - 1; - if (chunk == doc->val_pool.chunks) { - sum -= (usize)(doc->val_pool.end - doc->val_pool.cur); - } - chunk = chunk->next; - } - return sum; -} - -/** Write single JSON value. */ -static_inline u8 *yyjson_mut_write_single(yyjson_mut_val *val, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - return yyjson_write_single((yyjson_val *)val, flg, alc, dat_len, err); -} - -/** Write JSON document minify. - The root of this document should be a non-empty container. */ -static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, - usize estimated_val_num, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - *dat_len = 0; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - err->msg = _msg; \ - if (hdr) alc.free(alc.ctx, hdr); \ - return NULL; \ -} while (false) - -#define incr_len(_len) do { \ - ext_len = (usize)(_len); \ - if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ - alc_inc = yyjson_max(alc_len / 2, ext_len); \ - alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ - if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ - goto fail_alloc; \ - alc_len += alc_inc; \ - tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ - if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ - ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ - ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ - end = tmp + alc_len; \ - hdr = tmp; \ - } \ -} while (false) - -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) - - yyjson_mut_val *val, *ctn; - yyjson_type val_type; - usize ctn_len, ctn_len_tmp; - bool ctn_obj, ctn_obj_tmp, is_key; - u8 *hdr, *cur, *end, *tmp; - yyjson_mut_write_ctx *ctx, *ctx_tmp; - usize alc_len, alc_inc, ctx_len, ext_len, str_len; - const u8 *str_ptr; - const char_enc_type *enc_table = get_enc_table_with_flag(flg); - bool cpy = (enc_table == enc_table_cpy); - bool esc = has_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(NEWLINE_AT_END) != 0; - - alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; - alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); - hdr = (u8 *)alc.malloc(alc.ctx, alc_len); - if (!hdr) goto fail_alloc; - cur = hdr; - end = hdr + alc_len; - ctx = (yyjson_mut_write_ctx *)(void *)end; - -doc_begin: - val = constcast(yyjson_mut_val *)root; - val_type = unsafe_yyjson_get_type(val); - ctn_obj = (val_type == YYJSON_TYPE_OBJ); - ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - ctn = val; - val = (yyjson_mut_val *)val->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = ((u8)ctn_obj & (u8)~ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - incr_len(32); - cur = write_number(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - incr_len(16); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - goto val_end; - } else { - /* push context, setup new container */ - yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - ctn = val; - val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - goto val_begin; - } - } - if (val_type == YYJSON_TYPE_BOOL) { - incr_len(16); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur++; - goto val_end; - } - if (val_type == YYJSON_TYPE_NULL) { - incr_len(16); - cur = write_null(cur); - cur++; - goto val_end; - } - if (val_type == YYJSON_TYPE_RAW) { - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len + 2); - cur = write_raw(cur, str_ptr, str_len); - *cur++ = ','; - goto val_end; - } - goto fail_type; - -val_end: - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - val = val->next; - goto val_begin; - -ctn_end: - cur--; - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - *cur++ = ','; - if (unlikely((u8 *)ctx >= end)) goto doc_end; - val = ctn->next; - yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); - ctn_len--; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } - -doc_end: - if (newline) { - incr_len(2); - *(cur - 1) = '\n'; - cur++; - } - *--cur = '\0'; - *dat_len = (usize)(cur - hdr); - err->code = YYJSON_WRITE_SUCCESS; - err->msg = "success"; - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len -} - -/** Write JSON document pretty. - The root of this document should be a non-empty container. */ -static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, - usize estimated_val_num, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - -#define return_err(_code, _msg) do { \ - *dat_len = 0; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - err->msg = _msg; \ - if (hdr) alc.free(alc.ctx, hdr); \ - return NULL; \ -} while (false) - -#define incr_len(_len) do { \ - ext_len = (usize)(_len); \ - if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ - alc_inc = yyjson_max(alc_len / 2, ext_len); \ - alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ - if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ - goto fail_alloc; \ - alc_len += alc_inc; \ - tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ - if (unlikely(!tmp)) goto fail_alloc; \ - ctx_len = (usize)(end - (u8 *)ctx); \ - ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ - memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ - ctx = ctx_tmp; \ - cur = tmp + (cur - hdr); \ - end = tmp + alc_len; \ - hdr = tmp; \ - } \ -} while (false) - -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) - - yyjson_mut_val *val, *ctn; - yyjson_type val_type; - usize ctn_len, ctn_len_tmp; - bool ctn_obj, ctn_obj_tmp, is_key, no_indent; - u8 *hdr, *cur, *end, *tmp; - yyjson_mut_write_ctx *ctx, *ctx_tmp; - usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; - const u8 *str_ptr; - const char_enc_type *enc_table = get_enc_table_with_flag(flg); - bool cpy = (enc_table == enc_table_cpy); - bool esc = has_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; - bool newline = has_write_flag(NEWLINE_AT_END) != 0; - - alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; - alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); - hdr = (u8 *)alc.malloc(alc.ctx, alc_len); - if (!hdr) goto fail_alloc; - cur = hdr; - end = hdr + alc_len; - ctx = (yyjson_mut_write_ctx *)(void *)end; - -doc_begin: - val = constcast(yyjson_mut_val *)root; - val_type = unsafe_yyjson_get_type(val); - ctn_obj = (val_type == YYJSON_TYPE_OBJ); - ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - *cur++ = '\n'; - ctn = val; - val = (yyjson_mut_val *)val->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - level = 1; - -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - *cur++ = is_key ? ' ' : '\n'; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(32 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_number(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } else { - /* push context, setup new container */ - incr_len(32 + (no_indent ? 0 : level * 4)); - yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - cur = write_indent(cur, no_indent ? 0 : level, spaces); - level++; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - *cur++ = '\n'; - ctn = val; - val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - goto val_begin; - } - } - if (val_type == YYJSON_TYPE_BOOL) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur += 2; - goto val_end; - } - if (val_type == YYJSON_TYPE_NULL) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_null(cur); - cur += 2; - goto val_end; - } - if (val_type == YYJSON_TYPE_RAW) { - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len + 3); - cur = write_raw(cur, str_ptr, str_len); - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } - goto fail_type; - -val_end: - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - val = val->next; - goto val_begin; - -ctn_end: - cur -= 2; - *cur++ = '\n'; - incr_len(level * 4); - cur = write_indent(cur, --level, spaces); - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - if (unlikely((u8 *)ctx >= end)) goto doc_end; - val = ctn->next; - yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); - ctn_len--; - *cur++ = ','; - *cur++ = '\n'; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } - -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; - } - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - err->code = YYJSON_WRITE_SUCCESS; - err->msg = "success"; - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len -} - -static char *yyjson_mut_write_opts_impl(const yyjson_mut_val *val, - usize estimated_val_num, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - usize dummy_dat_len; - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_dat_len; - - if (unlikely(!root)) { - *dat_len = 0; - err->msg = "input JSON is NULL"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return NULL; - } - - if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { - return (char *)yyjson_mut_write_single(root, flg, alc, dat_len, err); - } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { - return (char *)yyjson_mut_write_pretty(root, estimated_val_num, - flg, alc, dat_len, err); - } else { - return (char *)yyjson_mut_write_minify(root, estimated_val_num, - flg, alc, dat_len, err); - } -} - -char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - return yyjson_mut_write_opts_impl(val, 0, flg, alc_ptr, dat_len, err); -} - -char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - yyjson_mut_val *root; - usize estimated_val_num; - if (likely(doc)) { - root = doc->root; - estimated_val_num = yyjson_mut_doc_estimated_val_num(doc); - } else { - root = NULL; - estimated_val_num = 0; - } - return yyjson_mut_write_opts_impl(root, estimated_val_num, - flg, alc_ptr, dat_len, err); -} - -bool yyjson_mut_val_write_file(const char *path, - const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!path || !*path)) { - err->msg = "input path is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; - } - - dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; -} - -bool yyjson_mut_val_write_fp(FILE *fp, - const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_write_err dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; - } - - dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; -} - -bool yyjson_mut_write_file(const char *path, - const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_mut_val *root = doc ? doc->root : NULL; - return yyjson_mut_val_write_file(path, root, flg, alc_ptr, err); -} - -bool yyjson_mut_write_fp(FILE *fp, - const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_mut_val *root = doc ? doc->root : NULL; - return yyjson_mut_val_write_fp(fp, root, flg, alc_ptr, err); -} - -#endif /* YYJSON_DISABLE_WRITER */ diff --git a/vendor/orjson/include/yyjson/yyjson.h b/vendor/orjson/include/yyjson/yyjson.h deleted file mode 100644 index 210449d342..0000000000 --- a/vendor/orjson/include/yyjson/yyjson.h +++ /dev/null @@ -1,7924 +0,0 @@ -/*============================================================================== - Copyright (c) 2020 YaoYuan - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - *============================================================================*/ - -/** - @file yyjson.h - @date 2019-03-09 - @author YaoYuan - */ - -#ifndef YYJSON_H -#define YYJSON_H - - - -/*============================================================================== - * Header Files - *============================================================================*/ - -#include -#include -#include -#include -#include -#include - - - -/*============================================================================== - * Compile-time Options - *============================================================================*/ - -/* - Define as 1 to disable JSON reader if JSON parsing is not required. - - This will disable these functions at compile-time: - - yyjson_read() - - yyjson_read_opts() - - yyjson_read_file() - - yyjson_read_number() - - yyjson_mut_read_number() - - This will reduce the binary size by about 60%. - */ -#ifndef YYJSON_DISABLE_READER -#endif - -/* - Define as 1 to disable JSON writer if JSON serialization is not required. - - This will disable these functions at compile-time: - - yyjson_write() - - yyjson_write_file() - - yyjson_write_opts() - - yyjson_val_write() - - yyjson_val_write_file() - - yyjson_val_write_opts() - - yyjson_mut_write() - - yyjson_mut_write_file() - - yyjson_mut_write_opts() - - yyjson_mut_val_write() - - yyjson_mut_val_write_file() - - yyjson_mut_val_write_opts() - - This will reduce the binary size by about 30%. - */ -#ifndef YYJSON_DISABLE_WRITER -#endif - -/* - Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports. - - This will disable these functions at compile-time: - - yyjson_ptr_xxx() - - yyjson_mut_ptr_xxx() - - yyjson_doc_ptr_xxx() - - yyjson_mut_doc_ptr_xxx() - - yyjson_patch() - - yyjson_mut_patch() - - yyjson_merge_patch() - - yyjson_mut_merge_patch() - */ -#ifndef YYJSON_DISABLE_UTILS -#endif - -/* - Define as 1 to disable the fast floating-point number conversion in yyjson, - and use libc's `strtod/snprintf` instead. - - This will reduce the binary size by about 30%, but significantly slow down the - floating-point read/write speed. - */ -#ifndef YYJSON_DISABLE_FAST_FP_CONV -#endif - -/* - Define as 1 to disable non-standard JSON support at compile-time: - - Reading and writing inf/nan literal, such as `NaN`, `-Infinity`. - - Single line and multiple line comments. - - Single trailing comma at the end of an object or array. - - Invalid unicode in string value. - - This will also invalidate these run-time options: - - YYJSON_READ_ALLOW_INF_AND_NAN - - YYJSON_READ_ALLOW_COMMENTS - - YYJSON_READ_ALLOW_TRAILING_COMMAS - - YYJSON_READ_ALLOW_INVALID_UNICODE - - YYJSON_WRITE_ALLOW_INF_AND_NAN - - YYJSON_WRITE_ALLOW_INVALID_UNICODE - - This will reduce the binary size by about 10%, and speed up the reading and - writing speed by about 2% to 6%. - */ -#ifndef YYJSON_DISABLE_NON_STANDARD -#endif - -/* - Define as 1 to disable UTF-8 validation at compile time. - - If all input strings are guaranteed to be valid UTF-8 encoding (for example, - some language's String object has already validated the encoding), using this - flag can avoid redundant UTF-8 validation in yyjson. - - This flag can speed up the reading and writing speed of non-ASCII encoded - strings by about 3% to 7%. - - Note: If this flag is used while passing in illegal UTF-8 strings, the - following errors may occur: - - Escaped characters may be ignored when parsing JSON strings. - - Ending quotes may be ignored when parsing JSON strings, causing the string - to be concatenated to the next value. - - When accessing `yyjson_mut_val` for serialization, the string ending may be - accessed out of bounds, causing a segmentation fault. - */ -#ifndef YYJSON_DISABLE_UTF8_VALIDATION -#endif - -/* - Define as 1 to indicate that the target architecture does not support unaligned - memory access. Please refer to the comments in the C file for details. - */ -#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS -#endif - -/* Define as 1 to export symbols when building this library as Windows DLL. */ -#ifndef YYJSON_EXPORTS -#endif - -/* Define as 1 to import symbols when using this library as Windows DLL. */ -#ifndef YYJSON_IMPORTS -#endif - -/* Define as 1 to include for compiler which doesn't support C99. */ -#ifndef YYJSON_HAS_STDINT_H -#endif - -/* Define as 1 to include for compiler which doesn't support C99. */ -#ifndef YYJSON_HAS_STDBOOL_H -#endif - - - -/*============================================================================== - * Compiler Macros - *============================================================================*/ - -/** compiler version (MSVC) */ -#ifdef _MSC_VER -# define YYJSON_MSC_VER _MSC_VER -#else -# define YYJSON_MSC_VER 0 -#endif - -/** compiler version (GCC) */ -#ifdef __GNUC__ -# define YYJSON_GCC_VER __GNUC__ -# if defined(__GNUC_PATCHLEVEL__) -# define yyjson_gcc_available(major, minor, patch) \ - ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) \ - >= (major * 10000 + minor * 100 + patch)) -# else -# define yyjson_gcc_available(major, minor, patch) \ - ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) \ - >= (major * 10000 + minor * 100 + patch)) -# endif -#else -# define YYJSON_GCC_VER 0 -# define yyjson_gcc_available(major, minor, patch) 0 -#endif - -/** real gcc check */ -#if !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__ICC) && \ - defined(__GNUC__) -# define YYJSON_IS_REAL_GCC 1 -#else -# define YYJSON_IS_REAL_GCC 0 -#endif - -/** C version (STDC) */ -#if defined(__STDC__) && (__STDC__ >= 1) && defined(__STDC_VERSION__) -# define YYJSON_STDC_VER __STDC_VERSION__ -#else -# define YYJSON_STDC_VER 0 -#endif - -/** C++ version */ -#if defined(__cplusplus) -# define YYJSON_CPP_VER __cplusplus -#else -# define YYJSON_CPP_VER 0 -#endif - -/** compiler builtin check (since gcc 10.0, clang 2.6, icc 2021) */ -#ifndef yyjson_has_builtin -# ifdef __has_builtin -# define yyjson_has_builtin(x) __has_builtin(x) -# else -# define yyjson_has_builtin(x) 0 -# endif -#endif - -/** compiler attribute check (since gcc 5.0, clang 2.9, icc 17) */ -#ifndef yyjson_has_attribute -# ifdef __has_attribute -# define yyjson_has_attribute(x) __has_attribute(x) -# else -# define yyjson_has_attribute(x) 0 -# endif -#endif - -/** compiler feature check (since clang 2.6, icc 17) */ -#ifndef yyjson_has_feature -# ifdef __has_feature -# define yyjson_has_feature(x) __has_feature(x) -# else -# define yyjson_has_feature(x) 0 -# endif -#endif - -/** include check (since gcc 5.0, clang 2.7, icc 16, msvc 2017 15.3) */ -#ifndef yyjson_has_include -# ifdef __has_include -# define yyjson_has_include(x) __has_include(x) -# else -# define yyjson_has_include(x) 0 -# endif -#endif - -/** inline for compiler */ -#ifndef yyjson_inline -# if YYJSON_MSC_VER >= 1200 -# define yyjson_inline __forceinline -# elif defined(_MSC_VER) -# define yyjson_inline __inline -# elif yyjson_has_attribute(always_inline) || YYJSON_GCC_VER >= 4 -# define yyjson_inline __inline__ __attribute__((always_inline)) -# elif defined(__clang__) || defined(__GNUC__) -# define yyjson_inline __inline__ -# elif defined(__cplusplus) || YYJSON_STDC_VER >= 199901L -# define yyjson_inline inline -# else -# define yyjson_inline -# endif -#endif - -/** noinline for compiler */ -#ifndef yyjson_noinline -# if YYJSON_MSC_VER >= 1400 -# define yyjson_noinline __declspec(noinline) -# elif yyjson_has_attribute(noinline) || YYJSON_GCC_VER >= 4 -# define yyjson_noinline __attribute__((noinline)) -# else -# define yyjson_noinline -# endif -#endif - -/** align for compiler */ -#ifndef yyjson_align -# if YYJSON_MSC_VER >= 1300 -# define yyjson_align(x) __declspec(align(x)) -# elif yyjson_has_attribute(aligned) || defined(__GNUC__) -# define yyjson_align(x) __attribute__((aligned(x))) -# elif YYJSON_CPP_VER >= 201103L -# define yyjson_align(x) alignas(x) -# else -# define yyjson_align(x) -# endif -#endif - -/** likely for compiler */ -#ifndef yyjson_likely -# if yyjson_has_builtin(__builtin_expect) || \ - (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) -# define yyjson_likely(expr) __builtin_expect(!!(expr), 1) -# else -# define yyjson_likely(expr) (expr) -# endif -#endif - -/** unlikely for compiler */ -#ifndef yyjson_unlikely -# if yyjson_has_builtin(__builtin_expect) || \ - (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) -# define yyjson_unlikely(expr) __builtin_expect(!!(expr), 0) -# else -# define yyjson_unlikely(expr) (expr) -# endif -#endif - -/** compile-time constant check for compiler */ -#ifndef yyjson_constant_p -# if yyjson_has_builtin(__builtin_constant_p) || (YYJSON_GCC_VER >= 3) -# define YYJSON_HAS_CONSTANT_P 1 -# define yyjson_constant_p(value) __builtin_constant_p(value) -# else -# define YYJSON_HAS_CONSTANT_P 0 -# define yyjson_constant_p(value) 0 -# endif -#endif - -/** deprecate warning */ -#ifndef yyjson_deprecated -# if YYJSON_MSC_VER >= 1400 -# define yyjson_deprecated(msg) __declspec(deprecated(msg)) -# elif yyjson_has_feature(attribute_deprecated_with_message) || \ - (YYJSON_GCC_VER > 4 || (YYJSON_GCC_VER == 4 && __GNUC_MINOR__ >= 5)) -# define yyjson_deprecated(msg) __attribute__((deprecated(msg))) -# elif YYJSON_GCC_VER >= 3 -# define yyjson_deprecated(msg) __attribute__((deprecated)) -# else -# define yyjson_deprecated(msg) -# endif -#endif - -/** function export */ -#ifndef yyjson_api -# if defined(_WIN32) -# if defined(YYJSON_EXPORTS) && YYJSON_EXPORTS -# define yyjson_api __declspec(dllexport) -# elif defined(YYJSON_IMPORTS) && YYJSON_IMPORTS -# define yyjson_api __declspec(dllimport) -# else -# define yyjson_api -# endif -# elif yyjson_has_attribute(visibility) || YYJSON_GCC_VER >= 4 -# define yyjson_api __attribute__((visibility("default"))) -# else -# define yyjson_api -# endif -#endif - -/** inline function export */ -#ifndef yyjson_api_inline -# define yyjson_api_inline static yyjson_inline -#endif - -/** stdint (C89 compatible) */ -#if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \ - YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \ - defined(_STDINT_H) || defined(_STDINT_H_) || \ - defined(__CLANG_STDINT_H) || defined(_STDINT_H_INCLUDED) || \ - yyjson_has_include() -# include -#elif defined(_MSC_VER) -# if _MSC_VER < 1300 - typedef signed char int8_t; - typedef signed short int16_t; - typedef signed int int32_t; - typedef unsigned char uint8_t; - typedef unsigned short uint16_t; - typedef unsigned int uint32_t; - typedef signed __int64 int64_t; - typedef unsigned __int64 uint64_t; -# else - typedef signed __int8 int8_t; - typedef signed __int16 int16_t; - typedef signed __int32 int32_t; - typedef unsigned __int8 uint8_t; - typedef unsigned __int16 uint16_t; - typedef unsigned __int32 uint32_t; - typedef signed __int64 int64_t; - typedef unsigned __int64 uint64_t; -# endif -#else -# if UCHAR_MAX == 0xFFU - typedef signed char int8_t; - typedef unsigned char uint8_t; -# else -# error cannot find 8-bit integer type -# endif -# if USHRT_MAX == 0xFFFFU - typedef unsigned short uint16_t; - typedef signed short int16_t; -# elif UINT_MAX == 0xFFFFU - typedef unsigned int uint16_t; - typedef signed int int16_t; -# else -# error cannot find 16-bit integer type -# endif -# if UINT_MAX == 0xFFFFFFFFUL - typedef unsigned int uint32_t; - typedef signed int int32_t; -# elif ULONG_MAX == 0xFFFFFFFFUL - typedef unsigned long uint32_t; - typedef signed long int32_t; -# elif USHRT_MAX == 0xFFFFFFFFUL - typedef unsigned short uint32_t; - typedef signed short int32_t; -# else -# error cannot find 32-bit integer type -# endif -# if defined(__INT64_TYPE__) && defined(__UINT64_TYPE__) - typedef __INT64_TYPE__ int64_t; - typedef __UINT64_TYPE__ uint64_t; -# elif defined(__GNUC__) || defined(__clang__) -# if !defined(_SYS_TYPES_H) && !defined(__int8_t_defined) - __extension__ typedef long long int64_t; -# endif - __extension__ typedef unsigned long long uint64_t; -# elif defined(_LONG_LONG) || defined(__MWERKS__) || defined(_CRAYC) || \ - defined(__SUNPRO_C) || defined(__SUNPRO_CC) - typedef long long int64_t; - typedef unsigned long long uint64_t; -# elif (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || \ - defined(__WATCOM_INT64__) || defined (__alpha) || defined (__DECC) - typedef __int64 int64_t; - typedef unsigned __int64 uint64_t; -# else -# error cannot find 64-bit integer type -# endif -#endif - -/** stdbool (C89 compatible) */ -#if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \ - (yyjson_has_include() && !defined(__STRICT_ANSI__)) || \ - YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L -# include -#elif !defined(__bool_true_false_are_defined) -# define __bool_true_false_are_defined 1 -# if defined(__cplusplus) -# if defined(__GNUC__) && !defined(__STRICT_ANSI__) -# define _Bool bool -# if __cplusplus < 201103L -# define bool bool -# define false false -# define true true -# endif -# endif -# else -# define bool unsigned char -# define true 1 -# define false 0 -# endif -#endif - -/** char bit check */ -#if defined(CHAR_BIT) -# if CHAR_BIT != 8 -# error non 8-bit char is not supported -# endif -#endif - -/** - Microsoft Visual C++ 6.0 doesn't support converting number from u64 to f64: - error C2520: conversion from unsigned __int64 to double not implemented. - */ -#ifndef YYJSON_U64_TO_F64_NO_IMPL -# if (0 < YYJSON_MSC_VER) && (YYJSON_MSC_VER <= 1200) -# define YYJSON_U64_TO_F64_NO_IMPL 1 -# else -# define YYJSON_U64_TO_F64_NO_IMPL 0 -# endif -#endif - - - -/*============================================================================== - * Compile Hint Begin - *============================================================================*/ - -/* extern "C" begin */ -#ifdef __cplusplus -extern "C" { -#endif - -/* warning suppress begin */ -#if defined(__clang__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wunused-function" -# pragma clang diagnostic ignored "-Wunused-parameter" -#elif defined(__GNUC__) -# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -# pragma GCC diagnostic push -# endif -# pragma GCC diagnostic ignored "-Wunused-function" -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable:4800) /* 'int': forcing value to 'true' or 'false' */ -#endif - - - -/*============================================================================== - * Version - *============================================================================*/ - -/** The major version of yyjson. */ -#define YYJSON_VERSION_MAJOR 0 - -/** The minor version of yyjson. */ -#define YYJSON_VERSION_MINOR 9 - -/** The patch version of yyjson. */ -#define YYJSON_VERSION_PATCH 0 - -/** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */ -#define YYJSON_VERSION_HEX 0x000900 - -/** The version string of yyjson. */ -#define YYJSON_VERSION_STRING "0.9.0" - -/** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */ -yyjson_api uint32_t yyjson_version(void); - - - -/*============================================================================== - * JSON Types - *============================================================================*/ - -/** Type of a JSON value (3 bit). */ -typedef uint8_t yyjson_type; -/** No type, invalid. */ -#define YYJSON_TYPE_NONE ((uint8_t)0) /* _____000 */ -/** Raw string type, no subtype. */ -#define YYJSON_TYPE_RAW ((uint8_t)1) /* _____001 */ -/** Null type: `null` literal, no subtype. */ -#define YYJSON_TYPE_NULL ((uint8_t)2) /* _____010 */ -/** Boolean type, subtype: TRUE, FALSE. */ -#define YYJSON_TYPE_BOOL ((uint8_t)3) /* _____011 */ -/** Number type, subtype: UINT, SINT, REAL. */ -#define YYJSON_TYPE_NUM ((uint8_t)4) /* _____100 */ -/** String type, subtype: NONE, NOESC. */ -#define YYJSON_TYPE_STR ((uint8_t)5) /* _____101 */ -/** Array type, no subtype. */ -#define YYJSON_TYPE_ARR ((uint8_t)6) /* _____110 */ -/** Object type, no subtype. */ -#define YYJSON_TYPE_OBJ ((uint8_t)7) /* _____111 */ - -/** Subtype of a JSON value (2 bit). */ -typedef uint8_t yyjson_subtype; -/** No subtype. */ -#define YYJSON_SUBTYPE_NONE ((uint8_t)(0 << 3)) /* ___00___ */ -/** False subtype: `false` literal. */ -#define YYJSON_SUBTYPE_FALSE ((uint8_t)(0 << 3)) /* ___00___ */ -/** True subtype: `true` literal. */ -#define YYJSON_SUBTYPE_TRUE ((uint8_t)(1 << 3)) /* ___01___ */ -/** Unsigned integer subtype: `uint64_t`. */ -#define YYJSON_SUBTYPE_UINT ((uint8_t)(0 << 3)) /* ___00___ */ -/** Signed integer subtype: `int64_t`. */ -#define YYJSON_SUBTYPE_SINT ((uint8_t)(1 << 3)) /* ___01___ */ -/** Real number subtype: `double`. */ -#define YYJSON_SUBTYPE_REAL ((uint8_t)(2 << 3)) /* ___10___ */ -/** String that do not need to be escaped for writing (internal use). */ -#define YYJSON_SUBTYPE_NOESC ((uint8_t)(1 << 3)) /* ___01___ */ - -/** The mask used to extract the type of a JSON value. */ -#define YYJSON_TYPE_MASK ((uint8_t)0x07) /* _____111 */ -/** The number of bits used by the type. */ -#define YYJSON_TYPE_BIT ((uint8_t)3) -/** The mask used to extract the subtype of a JSON value. */ -#define YYJSON_SUBTYPE_MASK ((uint8_t)0x18) /* ___11___ */ -/** The number of bits used by the subtype. */ -#define YYJSON_SUBTYPE_BIT ((uint8_t)2) -/** The mask used to extract the reserved bits of a JSON value. */ -#define YYJSON_RESERVED_MASK ((uint8_t)0xE0) /* 111_____ */ -/** The number of reserved bits. */ -#define YYJSON_RESERVED_BIT ((uint8_t)3) -/** The mask used to extract the tag of a JSON value. */ -#define YYJSON_TAG_MASK ((uint8_t)0xFF) /* 11111111 */ -/** The number of bits used by the tag. */ -#define YYJSON_TAG_BIT ((uint8_t)8) - -/** Padding size for JSON reader. */ -#define YYJSON_PADDING_SIZE 4 - - - -/*============================================================================== - * Allocator - *============================================================================*/ - -/** - A memory allocator. - - Typically you don't need to use it, unless you want to customize your own - memory allocator. - */ -typedef struct yyjson_alc { - /** Same as libc's malloc(size), should not be NULL. */ - void *(*malloc)(void *ctx, size_t size); - /** Same as libc's realloc(ptr, size), should not be NULL. */ - void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t size); - /** Same as libc's free(ptr), should not be NULL. */ - void (*free)(void *ctx, void *ptr); - /** A context for malloc/realloc/free, can be NULL. */ - void *ctx; -} yyjson_alc; - -/** - A pool allocator uses fixed length pre-allocated memory. - - This allocator may be used to avoid malloc/realloc calls. The pre-allocated - memory should be held by the caller. The maximum amount of memory required to - read a JSON can be calculated using the `yyjson_read_max_memory_usage()` - function, but the amount of memory required to write a JSON cannot be directly - calculated. - - This is not a general-purpose allocator. It is designed to handle a single JSON - data at a time. If it is used for overly complex memory tasks, such as parsing - multiple JSON documents using the same allocator but releasing only a few of - them, it may cause memory fragmentation, resulting in performance degradation - and memory waste. - - @param alc The allocator to be initialized. - If this parameter is NULL, the function will fail and return false. - If `buf` or `size` is invalid, this will be set to an empty allocator. - @param buf The buffer memory for this allocator. - If this parameter is NULL, the function will fail and return false. - @param size The size of `buf`, in bytes. - If this parameter is less than 8 words (32/64 bytes on 32/64-bit OS), the - function will fail and return false. - @return true if the `alc` has been successfully initialized. - - @par Example - @code - // parse JSON with stack memory - char buf[1024]; - yyjson_alc alc; - yyjson_alc_pool_init(&alc, buf, 1024); - - const char *json = "{\"name\":\"Helvetica\",\"size\":16}" - yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL); - // the memory of `doc` is on the stack - @endcode - - @warning This Allocator is not thread-safe. - */ -yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size); - -/** - A dynamic allocator. - - This allocator has a similar usage to the pool allocator above. However, when - there is not enough memory, this allocator will dynamically request more memory - using libc's `malloc` function, and frees it all at once when it is destroyed. - - @return A new dynamic allocator, or NULL if memory allocation failed. - @note The returned value should be freed with `yyjson_alc_dyn_free()`. - - @warning This Allocator is not thread-safe. - */ -yyjson_api yyjson_alc *yyjson_alc_dyn_new(void); - -/** - Free a dynamic allocator which is created by `yyjson_alc_dyn_new()`. - @param alc The dynamic allocator to be destroyed. - */ -yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); - - - -/*============================================================================== - * JSON Structure - *============================================================================*/ - -/** - An immutable document for reading JSON. - This document holds memory for all its JSON values and strings. When it is no - longer used, the user should call `yyjson_doc_free()` to free its memory. - */ -typedef struct yyjson_doc yyjson_doc; - -/** - An immutable value for reading JSON. - A JSON Value has the same lifetime as its document. The memory is held by its - document and and cannot be freed alone. - */ -typedef struct yyjson_val yyjson_val; - -/** - A mutable document for building JSON. - This document holds memory for all its JSON values and strings. When it is no - longer used, the user should call `yyjson_mut_doc_free()` to free its memory. - */ -typedef struct yyjson_mut_doc yyjson_mut_doc; - -/** - A mutable value for building JSON. - A JSON Value has the same lifetime as its document. The memory is held by its - document and and cannot be freed alone. - */ -typedef struct yyjson_mut_val yyjson_mut_val; - - - -/*============================================================================== - * JSON Reader API - *============================================================================*/ - -/** Run-time options for JSON reader. */ -typedef uint32_t yyjson_read_flag; - -/** Default option (RFC 8259 compliant): - - Read positive integer as uint64_t. - - Read negative integer as int64_t. - - Read floating-point number as double with round-to-nearest mode. - - Read integer which cannot fit in uint64_t or int64_t as double. - - Report error if double number is infinity. - - Report error if string contains invalid UTF-8 character or BOM. - - Report error on trailing commas, comments, inf and nan literals. */ -static const yyjson_read_flag YYJSON_READ_NOFLAG = 0; - -/** Read the input data in-situ. - This option allows the reader to modify and use input data to store string - values, which can increase reading speed slightly. - The caller should hold the input data before free the document. - The input data must be padded by at least `YYJSON_PADDING_SIZE` bytes. - For example: `[1,2]` should be `[1,2]\0\0\0\0`, input length should be 5. */ -static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0; - -/** Stop when done instead of issuing an error if there's additional content - after a JSON document. This option may be used to parse small pieces of JSON - in larger data, such as `NDJSON`. */ -static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE = 1 << 1; - -/** Allow single trailing comma at the end of an object or array, - such as `[1,2,3,]`, `{"a":1,"b":2,}` (non-standard). */ -static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2; - -/** Allow C-style single line and multiple line comments (non-standard). */ -static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3; - -/** Allow inf/nan number and literal, case-insensitive, - such as 1e999, NaN, inf, -Infinity (non-standard). */ -static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4; - -/** Read all numbers as raw strings (value with `YYJSON_TYPE_RAW` type), - inf/nan literal is also read as raw with `ALLOW_INF_AND_NAN` flag. */ -static const yyjson_read_flag YYJSON_READ_NUMBER_AS_RAW = 1 << 5; - -/** Allow reading invalid unicode when parsing string values (non-standard). - Invalid characters will be allowed to appear in the string values, but - invalid escape sequences will still be reported as errors. - This flag does not affect the performance of correctly encoded strings. - - @warning Strings in JSON values may contain incorrect encoding when this - option is used, you need to handle these strings carefully to avoid security - risks. */ -static const yyjson_read_flag YYJSON_READ_ALLOW_INVALID_UNICODE = 1 << 6; - -/** Read big numbers as raw strings. These big numbers include integers that - cannot be represented by `int64_t` and `uint64_t`, and floating-point - numbers that cannot be represented by finite `double`. - The flag will be overridden by `YYJSON_READ_NUMBER_AS_RAW` flag. */ -static const yyjson_read_flag YYJSON_READ_BIGNUM_AS_RAW = 1 << 7; - - - -/** Result code for JSON reader. */ -typedef uint32_t yyjson_read_code; - -/** Success, no error. */ -static const yyjson_read_code YYJSON_READ_SUCCESS = 0; - -/** Invalid parameter, such as NULL input string or 0 input length. */ -static const yyjson_read_code YYJSON_READ_ERROR_INVALID_PARAMETER = 1; - -/** Memory allocation failure occurs. */ -static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2; - -/** Input JSON string is empty. */ -static const yyjson_read_code YYJSON_READ_ERROR_EMPTY_CONTENT = 3; - -/** Unexpected content after document, such as `[123]abc`. */ -static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CONTENT = 4; - -/** Unexpected ending, such as `[123`. */ -static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_END = 5; - -/** Unexpected character inside the document, such as `[abc]`. */ -static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CHARACTER = 6; - -/** Invalid JSON structure, such as `[1,]`. */ -static const yyjson_read_code YYJSON_READ_ERROR_JSON_STRUCTURE = 7; - -/** Invalid comment, such as unclosed multi-line comment. */ -static const yyjson_read_code YYJSON_READ_ERROR_INVALID_COMMENT = 8; - -/** Invalid number, such as `123.e12`, `000`. */ -static const yyjson_read_code YYJSON_READ_ERROR_INVALID_NUMBER = 9; - -/** Invalid string, such as invalid escaped character inside a string. */ -static const yyjson_read_code YYJSON_READ_ERROR_INVALID_STRING = 10; - -/** Invalid JSON literal, such as `truu`. */ -static const yyjson_read_code YYJSON_READ_ERROR_LITERAL = 11; - -/** Failed to open a file. */ -static const yyjson_read_code YYJSON_READ_ERROR_FILE_OPEN = 12; - -/** Failed to read a file. */ -static const yyjson_read_code YYJSON_READ_ERROR_FILE_READ = 13; - -/** Document exceeded YYJSON_READER_CONTAINER_RECURSION_LIMIT. */ -static const yyjson_read_code YYJSON_READ_ERROR_RECURSION_DEPTH = 14; - -/** Error information for JSON reader. */ -typedef struct yyjson_read_err { - /** Error code, see `yyjson_read_code` for all possible values. */ - yyjson_read_code code; - /** Error message, constant, no need to free (NULL if success). */ - const char *msg; - /** Error byte position for input data (0 if success). */ - size_t pos; -} yyjson_read_err; - - - -/** - Read JSON with options. - - This function is thread-safe when: - 1. The `dat` is not modified by other threads. - 2. The `alc` is thread-safe or NULL. - - @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. - If this parameter is NULL, the function will fail and return NULL. - The `dat` will not be modified without the flag `YYJSON_READ_INSITU`, so you - can pass a `const char *` string and case it to `char *` if you don't use - the `YYJSON_READ_INSITU` flag. - @param len The length of JSON data in bytes. - If this parameter is 0, the function will fail and return NULL. - @param alc The memory allocator used by JSON reader. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON document, or NULL if an error occurs. - When it's no longer needed, it should be freed with `yyjson_doc_free()`. - */ -yyjson_api yyjson_doc *yyjson_read_opts(char *dat, - size_t len, - const yyjson_alc *alc, - yyjson_read_err *err); - -/** - Read a JSON file. - - This function is thread-safe when: - 1. The file is not modified by other threads. - 2. The `alc` is thread-safe or NULL. - - @param path The JSON file's path. - If this path is NULL or invalid, the function will fail and return NULL. - @param flg The JSON read options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON reader. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON document, or NULL if an error occurs. - When it's no longer needed, it should be freed with `yyjson_doc_free()`. - - @warning On 32-bit operating system, files larger than 2GB may fail to read. - */ -yyjson_api yyjson_doc *yyjson_read_file(const char *path, - yyjson_read_flag flg, - const yyjson_alc *alc, - yyjson_read_err *err); - -/** - Read JSON from a file pointer. - - @param fp The file pointer. - The data will be read from the current position of the FILE to the end. - If this fp is NULL or invalid, the function will fail and return NULL. - @param flg The JSON read options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON reader. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON document, or NULL if an error occurs. - When it's no longer needed, it should be freed with `yyjson_doc_free()`. - - @warning On 32-bit operating system, files larger than 2GB may fail to read. - */ -yyjson_api yyjson_doc *yyjson_read_fp(FILE *fp, - yyjson_read_flag flg, - const yyjson_alc *alc, - yyjson_read_err *err); - -/** - Read a JSON string. - - This function is thread-safe. - - @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. - If this parameter is NULL, the function will fail and return NULL. - @param len The length of JSON data in bytes. - If this parameter is 0, the function will fail and return NULL. - @param flg The JSON read options. - Multiple options can be combined with `|` operator. 0 means no options. - @return A new JSON document, or NULL if an error occurs. - When it's no longer needed, it should be freed with `yyjson_doc_free()`. - */ -yyjson_api_inline yyjson_doc *yyjson_read(const char *dat, - size_t len, - yyjson_read_flag flg) { - flg &= ~YYJSON_READ_INSITU; /* const string cannot be modified */ - return yyjson_read_opts((char *)(void *)(size_t)(const void *)dat, - len, NULL, NULL); -} - -/** - Returns the size of maximum memory usage to read a JSON data. - - You may use this value to avoid malloc() or calloc() call inside the reader - to get better performance, or read multiple JSON with one piece of memory. - - @param len The length of JSON data in bytes. - @param flg The JSON read options. - @return The maximum memory size to read this JSON, or 0 if overflow. - - @par Example - @code - // read multiple JSON with same pre-allocated memory - - char *dat1, *dat2, *dat3; // JSON data - size_t len1, len2, len3; // JSON length - size_t max_len = MAX(len1, MAX(len2, len3)); - yyjson_doc *doc; - - // use one allocator for multiple JSON - size_t size = yyjson_read_max_memory_usage(max_len, 0); - void *buf = malloc(size); - yyjson_alc alc; - yyjson_alc_pool_init(&alc, buf, size); - - // no more alloc() or realloc() call during reading - doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL); - yyjson_doc_free(doc); - doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL); - yyjson_doc_free(doc); - doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL); - yyjson_doc_free(doc); - - free(buf); - @endcode - @see yyjson_alc_pool_init() - */ -yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, - yyjson_read_flag flg) { - /* - 1. The max value count is (json_size / 2 + 1), - for example: "[1,2,3,4]" size is 9, value count is 5. - 2. Some broken JSON may cost more memory during reading, but fail at end, - for example: "[[[[[[[[". - 3. yyjson use 16 bytes per value, see struct yyjson_val. - 4. yyjson use dynamic memory with a growth factor of 1.5. - - The max memory size is (json_size / 2 * 16 * 1.5 + padding). - */ - size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU); - size_t pad = 256; - size_t max = (size_t)(~(size_t)0); - if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len; - if (len >= (max - pad - mul) / mul) return 0; - return len * mul + pad; -} - -/** - Read a JSON number. - - This function is thread-safe when data is not modified by other threads. - - @param dat The JSON data (UTF-8 without BOM), null-terminator is required. - If this parameter is NULL, the function will fail and return NULL. - @param val The output value where result is stored. - If this parameter is NULL, the function will fail and return NULL. - The value will hold either UINT or SINT or REAL number; - @param flg The JSON read options. - Multiple options can be combined with `|` operator. 0 means no options. - Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. - @param alc The memory allocator used for long number. - It is only used when the built-in floating point reader is disabled. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return If successful, a pointer to the character after the last character - used in the conversion, NULL if an error occurs. - */ -yyjson_api const char *yyjson_read_number(const char *dat, - yyjson_val *val, - yyjson_read_flag flg, - const yyjson_alc *alc, - yyjson_read_err *err); - -/** - Read a JSON number. - - This function is thread-safe when data is not modified by other threads. - - @param dat The JSON data (UTF-8 without BOM), null-terminator is required. - If this parameter is NULL, the function will fail and return NULL. - @param val The output value where result is stored. - If this parameter is NULL, the function will fail and return NULL. - The value will hold either UINT or SINT or REAL number; - @param flg The JSON read options. - Multiple options can be combined with `|` operator. 0 means no options. - Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. - @param alc The memory allocator used for long number. - It is only used when the built-in floating point reader is disabled. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return If successful, a pointer to the character after the last character - used in the conversion, NULL if an error occurs. - */ -yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, - yyjson_mut_val *val, - yyjson_read_flag flg, - const yyjson_alc *alc, - yyjson_read_err *err) { - return yyjson_read_number(dat, (yyjson_val *)val, flg, alc, err); -} - - -/*============================================================================== - * JSON Writer API - *============================================================================*/ - -/** Run-time options for JSON writer. */ -typedef uint32_t yyjson_write_flag; - -/** Default option: - - Write JSON minify. - - Report error on inf or nan number. - - Report error on invalid UTF-8 string. - - Do not escape unicode or slash. */ -static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0; - -/** Write JSON pretty with 4 space indent. */ -static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0; - -/** Escape unicode as `uXXXX`, make the output ASCII only. */ -static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1; - -/** Escape '/' as '\/'. */ -static const yyjson_write_flag YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2; - -/** Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard). */ -static const yyjson_write_flag YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3; - -/** Write inf and nan number as null literal. - This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag. */ -static const yyjson_write_flag YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4; - -/** Allow invalid unicode when encoding string values (non-standard). - Invalid characters in string value will be copied byte by byte. - If `YYJSON_WRITE_ESCAPE_UNICODE` flag is also set, invalid character will be - escaped as `U+FFFD` (replacement character). - This flag does not affect the performance of correctly encoded strings. */ -static const yyjson_write_flag YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5; - -/** Write JSON pretty with 2 space indent. - This flag will override `YYJSON_WRITE_PRETTY` flag. */ -static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6; - -/** Adds a newline character `\n` at the end of the JSON. - This can be helpful for text editors or NDJSON. */ -static const yyjson_write_flag YYJSON_WRITE_NEWLINE_AT_END = 1 << 7; - - - -/** Result code for JSON writer */ -typedef uint32_t yyjson_write_code; - -/** Success, no error. */ -static const yyjson_write_code YYJSON_WRITE_SUCCESS = 0; - -/** Invalid parameter, such as NULL document. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_PARAMETER = 1; - -/** Memory allocation failure occurs. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2; - -/** Invalid value type in JSON document. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE = 3; - -/** NaN or Infinity number occurs. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_NAN_OR_INF = 4; - -/** Failed to open a file. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_OPEN = 5; - -/** Failed to write a file. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_WRITE = 6; - -/** Invalid unicode in string. */ -static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_STRING = 7; - -/** Error information for JSON writer. */ -typedef struct yyjson_write_err { - /** Error code, see `yyjson_write_code` for all possible values. */ - yyjson_write_code code; - /** Error message, constant, no need to free (NULL if success). */ - const char *msg; -} yyjson_write_err; - - - -/*============================================================================== - * JSON Document Writer API - *============================================================================*/ - -/** - Write a document to JSON string with options. - - This function is thread-safe when: - The `alc` is thread-safe or NULL. - - @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free() or alc->free(). - */ -yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - size_t *len, - yyjson_write_err *err); - -/** - Write a document to JSON file with options. - - This function is thread-safe when: - 1. The file is not accessed by other threads. - 2. The `alc` is thread-safe or NULL. - - @param path The JSON file's path. - If this path is NULL or invalid, the function will fail and return false. - If this file is not empty, the content will be discarded. - @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_write_file(const char *path, - const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a document to file pointer with options. - - @param fp The file pointer. - The data will be written to the current position of the file. - If this fp is NULL or invalid, the function will fail and return false. - @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_write_fp(FILE *fp, - const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a document to JSON string. - - This function is thread-safe. - - @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free(). - */ -yyjson_api_inline char *yyjson_write(const yyjson_doc *doc, - yyjson_write_flag flg, - size_t *len) { - return yyjson_write_opts(doc, flg, NULL, len, NULL); -} - - - -/** - Write a document to JSON string with options. - - This function is thread-safe when: - 1. The `doc` is not modified by other threads. - 2. The `alc` is thread-safe or NULL. - - @param doc The mutable JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free() or alc->free(). - */ -yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - size_t *len, - yyjson_write_err *err); - -/** - Write a document to JSON file with options. - - This function is thread-safe when: - 1. The file is not accessed by other threads. - 2. The `doc` is not modified by other threads. - 3. The `alc` is thread-safe or NULL. - - @param path The JSON file's path. - If this path is NULL or invalid, the function will fail and return false. - If this file is not empty, the content will be discarded. - @param doc The mutable JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_mut_write_file(const char *path, - const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a document to file pointer with options. - - @param fp The file pointer. - The data will be written to the current position of the file. - If this fp is NULL or invalid, the function will fail and return false. - @param doc The mutable JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_mut_write_fp(FILE *fp, - const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a document to JSON string. - - This function is thread-safe when: - The `doc` is not modified by other threads. - - @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free(). - */ -yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, - yyjson_write_flag flg, - size_t *len) { - return yyjson_mut_write_opts(doc, flg, NULL, len, NULL); -} - - - -/*============================================================================== - * JSON Value Writer API - *============================================================================*/ - -/** - Write a value to JSON string with options. - - This function is thread-safe when: - The `alc` is thread-safe or NULL. - - @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free() or alc->free(). - */ -yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - size_t *len, - yyjson_write_err *err); - -/** - Write a value to JSON file with options. - - This function is thread-safe when: - 1. The file is not accessed by other threads. - 2. The `alc` is thread-safe or NULL. - - @param path The JSON file's path. - If this path is NULL or invalid, the function will fail and return false. - If this file is not empty, the content will be discarded. - @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_val_write_file(const char *path, - const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a value to file pointer with options. - - @param fp The file pointer. - The data will be written to the current position of the file. - If this path is NULL or invalid, the function will fail and return false. - @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_val_write_fp(FILE *fp, - const yyjson_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a value to JSON string. - - This function is thread-safe. - - @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free(). - */ -yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, - yyjson_write_flag flg, - size_t *len) { - return yyjson_val_write_opts(val, flg, NULL, len, NULL); -} - -/** - Write a value to JSON string with options. - - This function is thread-safe when: - 1. The `val` is not modified by other threads. - 2. The `alc` is thread-safe or NULL. - - @param val The mutable JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free() or alc->free(). - */ -yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - size_t *len, - yyjson_write_err *err); - -/** - Write a value to JSON file with options. - - This function is thread-safe when: - 1. The file is not accessed by other threads. - 2. The `val` is not modified by other threads. - 3. The `alc` is thread-safe or NULL. - - @param path The JSON file's path. - If this path is NULL or invalid, the function will fail and return false. - If this file is not empty, the content will be discarded. - @param val The mutable JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_mut_val_write_file(const char *path, - const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a value to JSON file with options. - - @param fp The file pointer. - The data will be written to the current position of the file. - If this path is NULL or invalid, the function will fail and return false. - @param val The mutable JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param alc The memory allocator used by JSON writer. - Pass NULL to use the libc's default allocator. - @param err A pointer to receive error information. - Pass NULL if you don't need error information. - @return true if successful, false if an error occurs. - - @warning On 32-bit operating system, files larger than 2GB may fail to write. - */ -yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, - const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc, - yyjson_write_err *err); - -/** - Write a value to JSON string. - - This function is thread-safe when: - The `val` is not modified by other threads. - - @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. - @param flg The JSON write options. - Multiple options can be combined with `|` operator. 0 means no options. - @param len A pointer to receive output length in bytes (not including the - null-terminator). Pass NULL if you don't need length information. - @return A new JSON string, or NULL if an error occurs. - This string is encoded as UTF-8 with a null-terminator. - When it's no longer needed, it should be freed with free(). - */ -yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, - yyjson_write_flag flg, - size_t *len) { - return yyjson_mut_val_write_opts(val, flg, NULL, len, NULL); -} - - - -/*============================================================================== - * JSON Document API - *============================================================================*/ - -/** Returns the root value of this JSON document. - Returns NULL if `doc` is NULL. */ -yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc); - -/** Returns read size of input JSON data. - Returns 0 if `doc` is NULL. - For example: the read size of `[1,2,3]` is 7 bytes. */ -yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc); - -/** Returns total value count in this JSON document. - Returns 0 if `doc` is NULL. - For example: the value count of `[1,2,3]` is 4. */ -yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc); - -/** Release the JSON document and free the memory. - After calling this function, the `doc` and all values from the `doc` are no - longer available. This function will do nothing if the `doc` is NULL. */ -yyjson_api void yyjson_doc_free(yyjson_doc *doc); - - - -/*============================================================================== - * JSON Value Type API - *============================================================================*/ - -/** Returns whether the JSON value is raw. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_raw(yyjson_val *val); - -/** Returns whether the JSON value is `null`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_null(yyjson_val *val); - -/** Returns whether the JSON value is `true`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_true(yyjson_val *val); - -/** Returns whether the JSON value is `false`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_false(yyjson_val *val); - -/** Returns whether the JSON value is bool (true/false). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_bool(yyjson_val *val); - -/** Returns whether the JSON value is unsigned integer (uint64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_uint(yyjson_val *val); - -/** Returns whether the JSON value is signed integer (int64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_sint(yyjson_val *val); - -/** Returns whether the JSON value is integer (uint64_t/int64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_int(yyjson_val *val); - -/** Returns whether the JSON value is real number (double). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_real(yyjson_val *val); - -/** Returns whether the JSON value is number (uint64_t/int64_t/double). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_num(yyjson_val *val); - -/** Returns whether the JSON value is string. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_str(yyjson_val *val); - -/** Returns whether the JSON value is array. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_arr(yyjson_val *val); - -/** Returns whether the JSON value is object. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_obj(yyjson_val *val); - -/** Returns whether the JSON value is container (array/object). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val); - - - -/*============================================================================== - * JSON Value Content API - *============================================================================*/ - -/** Returns the JSON value's type. - Returns YYJSON_TYPE_NONE if `val` is NULL. */ -yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val); - -/** Returns the JSON value's subtype. - Returns YYJSON_SUBTYPE_NONE if `val` is NULL. */ -yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val); - -/** Returns the JSON value's tag. - Returns 0 if `val` is NULL. */ -yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val); - -/** Returns the JSON value's type description. - The return value should be one of these strings: "raw", "null", "string", - "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ -yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val); - -/** Returns the content if the value is raw. - Returns NULL if `val` is NULL or type is not raw. */ -yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val); - -/** Returns the content if the value is bool. - Returns NULL if `val` is NULL or type is not bool. */ -yyjson_api_inline bool yyjson_get_bool(yyjson_val *val); - -/** Returns the content and cast to uint64_t. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val); - -/** Returns the content and cast to int64_t. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val); - -/** Returns the content and cast to int. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline int yyjson_get_int(yyjson_val *val); - -/** Returns the content if the value is real number, or 0.0 on error. - Returns 0.0 if `val` is NULL or type is not real(double). */ -yyjson_api_inline double yyjson_get_real(yyjson_val *val); - -/** Returns the content and typecast to `double` if the value is number. - Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ -yyjson_api_inline double yyjson_get_num(yyjson_val *val); - -/** Returns the content if the value is string. - Returns NULL if `val` is NULL or type is not string. */ -yyjson_api_inline const char *yyjson_get_str(yyjson_val *val); - -/** Returns the content length (string length, array size, object size. - Returns 0 if `val` is NULL or type is not string/array/object. */ -yyjson_api_inline size_t yyjson_get_len(yyjson_val *val); - -/** Returns whether the JSON value is equals to a string. - Returns false if input is NULL or type is not string. */ -yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str); - -/** Returns whether the JSON value is equals to a string. - The `str` should be a UTF-8 string, null-terminator is not required. - Returns false if input is NULL or type is not string. */ -yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, - size_t len); - -/** Returns whether two JSON values are equal (deep compare). - Returns false if input is NULL. - @note the result may be inaccurate if object has duplicate keys. - @warning This function is recursive and may cause a stack overflow - if the object level is too deep. */ -yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); - -/** Set the value to raw. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, - const char *raw, size_t len); - -/** Set the value to null. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_null(yyjson_val *val); - -/** Set the value to bool. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num); - -/** Set the value to uint. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num); - -/** Set the value to sint. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num); - -/** Set the value to int. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num); - -/** Set the value to real. - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num); - -/** Set the value to string (null-terminated). - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str); - -/** Set the value to string (with length). - Returns false if input is NULL or `val` is object or array. - @warning This will modify the `immutable` value, use with caution. */ -yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, - const char *str, size_t len); - - - -/*============================================================================== - * JSON Array API - *============================================================================*/ - -/** Returns the number of elements in this array. - Returns 0 if `arr` is NULL or type is not array. */ -yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr); - -/** Returns the element at the specified position in this array. - Returns NULL if array is NULL/empty or the index is out of bounds. - @warning This function takes a linear search time if array is not flat. - For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat. */ -yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx); - -/** Returns the first element of this array. - Returns NULL if `arr` is NULL/empty or type is not array. */ -yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr); - -/** Returns the last element of this array. - Returns NULL if `arr` is NULL/empty or type is not array. - @warning This function takes a linear search time if array is not flat. - For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat.*/ -yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr); - - - -/*============================================================================== - * JSON Array Iterator API - *============================================================================*/ - -/** - A JSON array iterator. - - @par Example - @code - yyjson_val *val; - yyjson_arr_iter iter = yyjson_arr_iter_with(arr); - while ((val = yyjson_arr_iter_next(&iter))) { - your_func(val); - } - @endcode - */ -typedef struct yyjson_arr_iter { - size_t idx; /**< next value's index */ - size_t max; /**< maximum index (arr.size) */ - yyjson_val *cur; /**< next value */ -} yyjson_arr_iter; - -/** - Initialize an iterator for this array. - - @param arr The array to be iterated over. - If this parameter is NULL or not an array, `iter` will be set to empty. - @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. - @return true if the `iter` has been successfully initialized. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, - yyjson_arr_iter *iter); - -/** - Create an iterator with an array , same as `yyjson_arr_iter_init()`. - - @param arr The array to be iterated over. - If this parameter is NULL or not an array, an empty iterator will returned. - @return A new iterator for the array. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr); - -/** - Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. - */ -yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter); - -/** - Returns the next element in the iteration, or NULL on end. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter); - -/** - Macro for iterating over an array. - It works like iterator, but with a more intuitive API. - - @par Example - @code - size_t idx, max; - yyjson_val *val; - yyjson_arr_foreach(arr, idx, max, val) { - your_func(idx, val); - } - @endcode - */ -#define yyjson_arr_foreach(arr, idx, max, val) \ - for ((idx) = 0, \ - (max) = yyjson_arr_size(arr), \ - (val) = yyjson_arr_get_first(arr); \ - (idx) < (max); \ - (idx)++, \ - (val) = unsafe_yyjson_get_next(val)) - - - -/*============================================================================== - * JSON Object API - *============================================================================*/ - -/** Returns the number of key-value pairs in this object. - Returns 0 if `obj` is NULL or type is not object. */ -yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj); - -/** Returns the value to which the specified key is mapped. - Returns NULL if this object contains no mapping for the key. - Returns NULL if `obj/key` is NULL, or type is not object. - - The `key` should be a null-terminated UTF-8 string. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key); - -/** Returns the value to which the specified key is mapped. - Returns NULL if this object contains no mapping for the key. - Returns NULL if `obj/key` is NULL, or type is not object. - - The `key` should be a UTF-8 string, null-terminator is not required. - The `key_len` should be the length of the key, in bytes. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key, - size_t key_len); - - - -/*============================================================================== - * JSON Object Iterator API - *============================================================================*/ - -/** - A JSON object iterator. - - @par Example - @code - yyjson_val *key, *val; - yyjson_obj_iter iter = yyjson_obj_iter_with(obj); - while ((key = yyjson_obj_iter_next(&iter))) { - val = yyjson_obj_iter_get_val(key); - your_func(key, val); - } - @endcode - - If the ordering of the keys is known at compile-time, you can use this method - to speed up value lookups: - @code - // {"k1":1, "k2": 3, "k3": 3} - yyjson_val *key, *val; - yyjson_obj_iter iter = yyjson_obj_iter_with(obj); - yyjson_val *v1 = yyjson_obj_iter_get(&iter, "k1"); - yyjson_val *v3 = yyjson_obj_iter_get(&iter, "k3"); - @endcode - @see yyjson_obj_iter_get() and yyjson_obj_iter_getn() - */ -typedef struct yyjson_obj_iter { - size_t idx; /**< next key's index */ - size_t max; /**< maximum key index (obj.size) */ - yyjson_val *cur; /**< next key */ - yyjson_val *obj; /**< the object being iterated */ -} yyjson_obj_iter; - -/** - Initialize an iterator for this object. - - @param obj The object to be iterated over. - If this parameter is NULL or not an object, `iter` will be set to empty. - @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. - @return true if the `iter` has been successfully initialized. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, - yyjson_obj_iter *iter); - -/** - Create an iterator with an object, same as `yyjson_obj_iter_init()`. - - @param obj The object to be iterated over. - If this parameter is NULL or not an object, an empty iterator will returned. - @return A new iterator for the object. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj); - -/** - Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. - */ -yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter); - -/** - Returns the next key in the iteration, or NULL on end. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter); - -/** - Returns the value for key inside the iteration. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key); - -/** - Iterates to a specified key and returns the value. - - This function does the same thing as `yyjson_obj_get()`, but is much faster - if the ordering of the keys is known at compile-time and you are using the same - order to look up the values. If the key exists in this object, then the - iterator will stop at the next key, otherwise the iterator will not change and - NULL is returned. - - @param iter The object iterator, should not be NULL. - @param key The key, should be a UTF-8 string with null-terminator. - @return The value to which the specified key is mapped. - NULL if this object contains no mapping for the key or input is invalid. - - @warning This function takes a linear search time if the key is not nearby. - */ -yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, - const char *key); - -/** - Iterates to a specified key and returns the value. - - This function does the same thing as `yyjson_obj_getn()`, but is much faster - if the ordering of the keys is known at compile-time and you are using the same - order to look up the values. If the key exists in this object, then the - iterator will stop at the next key, otherwise the iterator will not change and - NULL is returned. - - @param iter The object iterator, should not be NULL. - @param key The key, should be a UTF-8 string, null-terminator is not required. - @param key_len The the length of `key`, in bytes. - @return The value to which the specified key is mapped. - NULL if this object contains no mapping for the key or input is invalid. - - @warning This function takes a linear search time if the key is not nearby. - */ -yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, - const char *key, - size_t key_len); - -/** - Macro for iterating over an object. - It works like iterator, but with a more intuitive API. - - @par Example - @code - size_t idx, max; - yyjson_val *key, *val; - yyjson_obj_foreach(obj, idx, max, key, val) { - your_func(key, val); - } - @endcode - */ -#define yyjson_obj_foreach(obj, idx, max, key, val) \ - for ((idx) = 0, \ - (max) = yyjson_obj_size(obj), \ - (key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \ - (val) = (key) + 1; \ - (idx) < (max); \ - (idx)++, \ - (key) = unsafe_yyjson_get_next(val), \ - (val) = (key) + 1) - - - -/*============================================================================== - * Mutable JSON Document API - *============================================================================*/ - -/** Returns the root value of this JSON document. - Returns NULL if `doc` is NULL. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc); - -/** Sets the root value of this JSON document. - Pass NULL to clear root value of the document. */ -yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, - yyjson_mut_val *root); - -/** - Set the string pool size for a mutable document. - This function does not allocate memory immediately, but uses the size when - the next memory allocation is needed. - - If the caller knows the approximate bytes of strings that the document needs to - store (e.g. copy string with `yyjson_mut_strcpy` function), setting a larger - size can avoid multiple memory allocations and improve performance. - - @param doc The mutable document. - @param len The desired string pool size in bytes (total string length). - @return true if successful, false if size is 0 or overflow. - */ -yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, - size_t len); - -/** - Set the value pool size for a mutable document. - This function does not allocate memory immediately, but uses the size when - the next memory allocation is needed. - - If the caller knows the approximate number of values that the document needs to - store (e.g. create new value with `yyjson_mut_xxx` functions), setting a larger - size can avoid multiple memory allocations and improve performance. - - @param doc The mutable document. - @param count The desired value pool size (number of `yyjson_mut_val`). - @return true if successful, false if size is 0 or overflow. - */ -yyjson_api bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, - size_t count); - -/** Release the JSON document and free the memory. - After calling this function, the `doc` and all values from the `doc` are no - longer available. This function will do nothing if the `doc` is NULL. */ -yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc); - -/** Creates and returns a new mutable JSON document, returns NULL on error. - If allocator is NULL, the default allocator will be used. */ -yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc); - -/** Copies and returns a new mutable document from input, returns NULL on error. - This makes a `deep-copy` on the immutable document. - If allocator is NULL, the default allocator will be used. - @note `imut_doc` -> `mut_doc`. */ -yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, - const yyjson_alc *alc); - -/** Copies and returns a new mutable document from input, returns NULL on error. - This makes a `deep-copy` on the mutable document. - If allocator is NULL, the default allocator will be used. - @note `mut_doc` -> `mut_doc`. */ -yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, - const yyjson_alc *alc); - -/** Copies and returns a new mutable value from input, returns NULL on error. - This makes a `deep-copy` on the immutable value. - The memory was managed by mutable document. - @note `imut_val` -> `mut_val`. */ -yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc, - yyjson_val *val); - -/** Copies and returns a new mutable value from input, returns NULL on error. - This makes a `deep-copy` on the mutable value. - The memory was managed by mutable document. - @note `mut_val` -> `mut_val`. - @warning This function is recursive and may cause a stack overflow - if the object level is too deep. */ -yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, - yyjson_mut_val *val); - -/** Copies and returns a new immutable document from input, - returns NULL on error. This makes a `deep-copy` on the mutable document. - The returned document should be freed with `yyjson_doc_free()`. - @note `mut_doc` -> `imut_doc`. - @warning This function is recursive and may cause a stack overflow - if the object level is too deep. */ -yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *doc, - const yyjson_alc *alc); - -/** Copies and returns a new immutable document from input, - returns NULL on error. This makes a `deep-copy` on the mutable value. - The returned document should be freed with `yyjson_doc_free()`. - @note `mut_val` -> `imut_doc`. - @warning This function is recursive and may cause a stack overflow - if the object level is too deep. */ -yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *val, - const yyjson_alc *alc); - - - -/*============================================================================== - * Mutable JSON Value Type API - *============================================================================*/ - -/** Returns whether the JSON value is raw. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val); - -/** Returns whether the JSON value is `null`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val); - -/** Returns whether the JSON value is `true`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val); - -/** Returns whether the JSON value is `false`. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val); - -/** Returns whether the JSON value is bool (true/false). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val); - -/** Returns whether the JSON value is unsigned integer (uint64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val); - -/** Returns whether the JSON value is signed integer (int64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val); - -/** Returns whether the JSON value is integer (uint64_t/int64_t). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val); - -/** Returns whether the JSON value is real number (double). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val); - -/** Returns whether the JSON value is number (uint/sint/real). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val); - -/** Returns whether the JSON value is string. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val); - -/** Returns whether the JSON value is array. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val); - -/** Returns whether the JSON value is object. - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val); - -/** Returns whether the JSON value is container (array/object). - Returns false if `val` is NULL. */ -yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val); - - - -/*============================================================================== - * Mutable JSON Value Content API - *============================================================================*/ - -/** Returns the JSON value's type. - Returns `YYJSON_TYPE_NONE` if `val` is NULL. */ -yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val); - -/** Returns the JSON value's subtype. - Returns `YYJSON_SUBTYPE_NONE` if `val` is NULL. */ -yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val); - -/** Returns the JSON value's tag. - Returns 0 if `val` is NULL. */ -yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val); - -/** Returns the JSON value's type description. - The return value should be one of these strings: "raw", "null", "string", - "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ -yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val); - -/** Returns the content if the value is raw. - Returns NULL if `val` is NULL or type is not raw. */ -yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val); - -/** Returns the content if the value is bool. - Returns NULL if `val` is NULL or type is not bool. */ -yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val); - -/** Returns the content and cast to uint64_t. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val); - -/** Returns the content and cast to int64_t. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val); - -/** Returns the content and cast to int. - Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val); - -/** Returns the content if the value is real number. - Returns 0.0 if `val` is NULL or type is not real(double). */ -yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val); - -/** Returns the content and typecast to `double` if the value is number. - Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ -yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val); - -/** Returns the content if the value is string. - Returns NULL if `val` is NULL or type is not string. */ -yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val); - -/** Returns the content length (string length, array size, object size. - Returns 0 if `val` is NULL or type is not string/array/object. */ -yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val); - -/** Returns whether the JSON value is equals to a string. - The `str` should be a null-terminated UTF-8 string. - Returns false if input is NULL or type is not string. */ -yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, - const char *str); - -/** Returns whether the JSON value is equals to a string. - The `str` should be a UTF-8 string, null-terminator is not required. - Returns false if input is NULL or type is not string. */ -yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, - const char *str, size_t len); - -/** Returns whether two JSON values are equal (deep compare). - Returns false if input is NULL. - @note the result may be inaccurate if object has duplicate keys. - @warning This function is recursive and may cause a stack overflow - if the object level is too deep. */ -yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, - yyjson_mut_val *rhs); - -/** Set the value to raw. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, - const char *raw, size_t len); - -/** Set the value to null. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val); - -/** Set the value to bool. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num); - -/** Set the value to uint. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num); - -/** Set the value to sint. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num); - -/** Set the value to int. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num); - -/** Set the value to real. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num); - -/** Set the value to string (null-terminated). - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, const char *str); - -/** Set the value to string (with length). - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, - const char *str, size_t len); - -/** Set the value to array. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val); - -/** Set the value to array. - Returns false if input is NULL. - @warning This function should not be used on an existing object or array. */ -yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val); - - - -/*============================================================================== - * Mutable JSON Value Creation API - *============================================================================*/ - -/** Creates and returns a raw value, returns NULL on error. - The `str` should be a null-terminated UTF-8 string. - - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, - const char *str); - -/** Creates and returns a raw value, returns NULL on error. - The `str` should be a UTF-8 string, null-terminator is not required. - - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, - const char *str, - size_t len); - -/** Creates and returns a raw value, returns NULL on error. - The `str` should be a null-terminated UTF-8 string. - The input string is copied and held by the document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, - const char *str); - -/** Creates and returns a raw value, returns NULL on error. - The `str` should be a UTF-8 string, null-terminator is not required. - The input string is copied and held by the document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, - const char *str, - size_t len); - -/** Creates and returns a null value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc); - -/** Creates and returns a true value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc); - -/** Creates and returns a false value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc); - -/** Creates and returns a bool value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, - bool val); - -/** Creates and returns an unsigned integer value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, - uint64_t num); - -/** Creates and returns a signed integer value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, - int64_t num); - -/** Creates and returns a signed integer value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, - int64_t num); - -/** Creates and returns an real number value, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, - double num); - -/** Creates and returns a string value, returns NULL on error. - The `str` should be a null-terminated UTF-8 string. - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, - const char *str); - -/** Creates and returns a string value, returns NULL on error. - The `str` should be a UTF-8 string, null-terminator is not required. - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, - const char *str, - size_t len); - -/** Creates and returns a string value, returns NULL on error. - The `str` should be a null-terminated UTF-8 string. - The input string is copied and held by the document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, - const char *str); - -/** Creates and returns a string value, returns NULL on error. - The `str` should be a UTF-8 string, null-terminator is not required. - The input string is copied and held by the document. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, - const char *str, - size_t len); - - - -/*============================================================================== - * Mutable JSON Array API - *============================================================================*/ - -/** Returns the number of elements in this array. - Returns 0 if `arr` is NULL or type is not array. */ -yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr); - -/** Returns the element at the specified position in this array. - Returns NULL if array is NULL/empty or the index is out of bounds. - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, - size_t idx); - -/** Returns the first element of this array. - Returns NULL if `arr` is NULL/empty or type is not array. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(yyjson_mut_val *arr); - -/** Returns the last element of this array. - Returns NULL if `arr` is NULL/empty or type is not array. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr); - - - -/*============================================================================== - * Mutable JSON Array Iterator API - *============================================================================*/ - -/** - A mutable JSON array iterator. - - @warning You should not modify the array while iterating over it, but you can - use `yyjson_mut_arr_iter_remove()` to remove current value. - - @par Example - @code - yyjson_mut_val *val; - yyjson_mut_arr_iter iter = yyjson_mut_arr_iter_with(arr); - while ((val = yyjson_mut_arr_iter_next(&iter))) { - your_func(val); - if (your_val_is_unused(val)) { - yyjson_mut_arr_iter_remove(&iter); - } - } - @endcode - */ -typedef struct yyjson_mut_arr_iter { - size_t idx; /**< next value's index */ - size_t max; /**< maximum index (arr.size) */ - yyjson_mut_val *cur; /**< current value */ - yyjson_mut_val *pre; /**< previous value */ - yyjson_mut_val *arr; /**< the array being iterated */ -} yyjson_mut_arr_iter; - -/** - Initialize an iterator for this array. - - @param arr The array to be iterated over. - If this parameter is NULL or not an array, `iter` will be set to empty. - @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. - @return true if the `iter` has been successfully initialized. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, - yyjson_mut_arr_iter *iter); - -/** - Create an iterator with an array , same as `yyjson_mut_arr_iter_init()`. - - @param arr The array to be iterated over. - If this parameter is NULL or not an array, an empty iterator will returned. - @return A new iterator for the array. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( - yyjson_mut_val *arr); - -/** - Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. - */ -yyjson_api_inline bool yyjson_mut_arr_iter_has_next( - yyjson_mut_arr_iter *iter); - -/** - Returns the next element in the iteration, or NULL on end. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( - yyjson_mut_arr_iter *iter); - -/** - Removes and returns current element in the iteration. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( - yyjson_mut_arr_iter *iter); - -/** - Macro for iterating over an array. - It works like iterator, but with a more intuitive API. - - @warning You should not modify the array while iterating over it. - - @par Example - @code - size_t idx, max; - yyjson_mut_val *val; - yyjson_mut_arr_foreach(arr, idx, max, val) { - your_func(idx, val); - } - @endcode - */ -#define yyjson_mut_arr_foreach(arr, idx, max, val) \ - for ((idx) = 0, \ - (max) = yyjson_mut_arr_size(arr), \ - (val) = yyjson_mut_arr_get_first(arr); \ - (idx) < (max); \ - (idx)++, \ - (val) = (val)->next) - - - -/*============================================================================== - * Mutable JSON Array Creation API - *============================================================================*/ - -/** - Creates and returns an empty mutable array. - @param doc A mutable document, used for memory allocation only. - @return The new array. NULL if input is NULL or memory allocation failed. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc); - -/** - Creates and returns a new mutable array with the given boolean values. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of boolean values. - @param count The value count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const bool vals[3] = { true, false, true }; - yyjson_mut_val *arr = yyjson_mut_arr_with_bool(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( - yyjson_mut_doc *doc, const bool *vals, size_t count); - -/** - Creates and returns a new mutable array with the given sint numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of sint numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const int64_t vals[3] = { -1, 0, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( - yyjson_mut_doc *doc, const int64_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given uint numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of uint numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const uint64_t vals[3] = { 0, 1, 0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_uint(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( - yyjson_mut_doc *doc, const uint64_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given real numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of real numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const double vals[3] = { 0.1, 0.2, 0.3 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_real(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( - yyjson_mut_doc *doc, const double *vals, size_t count); - -/** - Creates and returns a new mutable array with the given int8 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of int8 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const int8_t vals[3] = { -1, 0, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_sint8(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( - yyjson_mut_doc *doc, const int8_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given int16 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of int16 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const int16_t vals[3] = { -1, 0, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_sint16(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( - yyjson_mut_doc *doc, const int16_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given int32 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of int32 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const int32_t vals[3] = { -1, 0, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_sint32(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( - yyjson_mut_doc *doc, const int32_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given int64 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of int64 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const int64_t vals[3] = { -1, 0, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( - yyjson_mut_doc *doc, const int64_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given uint8 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of uint8 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const uint8_t vals[3] = { 0, 1, 0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_uint8(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( - yyjson_mut_doc *doc, const uint8_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given uint16 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of uint16 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const uint16_t vals[3] = { 0, 1, 0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_uint16(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( - yyjson_mut_doc *doc, const uint16_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given uint32 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of uint32 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const uint32_t vals[3] = { 0, 1, 0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_uint32(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( - yyjson_mut_doc *doc, const uint32_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given uint64 numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of uint64 numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const uint64_t vals[3] = { 0, 1, 0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_uint64(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( - yyjson_mut_doc *doc, const uint64_t *vals, size_t count); - -/** - Creates and returns a new mutable array with the given float numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of float numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const float vals[3] = { -1.0f, 0.0f, 1.0f }; - yyjson_mut_val *arr = yyjson_mut_arr_with_float(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( - yyjson_mut_doc *doc, const float *vals, size_t count); - -/** - Creates and returns a new mutable array with the given double numbers. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of double numbers. - @param count The number count. If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const double vals[3] = { -1.0, 0.0, 1.0 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_double(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( - yyjson_mut_doc *doc, const double *vals, size_t count); - -/** - Creates and returns a new mutable array with the given strings, these strings - will not be copied. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of UTF-8 null-terminator strings. - If this array contains NULL, the function will fail and return NULL. - @param count The number of values in `vals`. - If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @warning The input strings are not copied, you should keep these strings - unmodified for the lifetime of this JSON document. If these strings will be - modified, you should use `yyjson_mut_arr_with_strcpy()` instead. - - @par Example - @code - const char *vals[3] = { "a", "b", "c" }; - yyjson_mut_val *arr = yyjson_mut_arr_with_str(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( - yyjson_mut_doc *doc, const char **vals, size_t count); - -/** - Creates and returns a new mutable array with the given strings and string - lengths, these strings will not be copied. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of UTF-8 strings, null-terminator is not required. - If this array contains NULL, the function will fail and return NULL. - @param lens A C array of string lengths, in bytes. - @param count The number of strings in `vals`. - If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @warning The input strings are not copied, you should keep these strings - unmodified for the lifetime of this JSON document. If these strings will be - modified, you should use `yyjson_mut_arr_with_strncpy()` instead. - - @par Example - @code - const char *vals[3] = { "a", "bb", "c" }; - const size_t lens[3] = { 1, 2, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( - yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); - -/** - Creates and returns a new mutable array with the given strings, these strings - will be copied. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of UTF-8 null-terminator strings. - If this array contains NULL, the function will fail and return NULL. - @param count The number of values in `vals`. - If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const char *vals[3] = { "a", "b", "c" }; - yyjson_mut_val *arr = yyjson_mut_arr_with_strcpy(doc, vals, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( - yyjson_mut_doc *doc, const char **vals, size_t count); - -/** - Creates and returns a new mutable array with the given strings and string - lengths, these strings will be copied. - - @param doc A mutable document, used for memory allocation only. - If this parameter is NULL, the function will fail and return NULL. - @param vals A C array of UTF-8 strings, null-terminator is not required. - If this array contains NULL, the function will fail and return NULL. - @param lens A C array of string lengths, in bytes. - @param count The number of strings in `vals`. - If this value is 0, an empty array will return. - @return The new array. NULL if input is invalid or memory allocation failed. - - @par Example - @code - const char *vals[3] = { "a", "bb", "c" }; - const size_t lens[3] = { 1, 2, 1 }; - yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( - yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); - - - -/*============================================================================== - * Mutable JSON Array Modification API - *============================================================================*/ - -/** - Inserts a value into an array at a given index. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param val The value to be inserted. Returns false if it is NULL. - @param idx The index to which to insert the new value. - Returns false if the index is out of range. - @return Whether successful. - @warning This function takes a linear search time. - */ -yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, - yyjson_mut_val *val, size_t idx); - -/** - Inserts a value at the end of the array. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param val The value to be inserted. Returns false if it is NULL. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, - yyjson_mut_val *val); - -/** - Inserts a value at the head of the array. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param val The value to be inserted. Returns false if it is NULL. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, - yyjson_mut_val *val); - -/** - Replaces a value at index and returns old value. - @param arr The array to which the value is to be replaced. - Returns false if it is NULL or not an array. - @param idx The index to which to replace the value. - Returns false if the index is out of range. - @param val The new value to replace. Returns false if it is NULL. - @return Old value, or NULL on error. - @warning This function takes a linear search time. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, - size_t idx, - yyjson_mut_val *val); - -/** - Removes and returns a value at index. - @param arr The array from which the value is to be removed. - Returns false if it is NULL or not an array. - @param idx The index from which to remove the value. - Returns false if the index is out of range. - @return Old value, or NULL on error. - @warning This function takes a linear search time. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, - size_t idx); - -/** - Removes and returns the first value in this array. - @param arr The array from which the value is to be removed. - Returns false if it is NULL or not an array. - @return The first value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( - yyjson_mut_val *arr); - -/** - Removes and returns the last value in this array. - @param arr The array from which the value is to be removed. - Returns false if it is NULL or not an array. - @return The last value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( - yyjson_mut_val *arr); - -/** - Removes all values within a specified range in the array. - @param arr The array from which the value is to be removed. - Returns false if it is NULL or not an array. - @param idx The start index of the range (0 is the first). - @param len The number of items in the range (can be 0). - @return Whether successful. - @warning This function takes a linear search time. - */ -yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, - size_t idx, size_t len); - -/** - Removes all values in this array. - @param arr The array from which all of the values are to be removed. - Returns false if it is NULL or not an array. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr); - -/** - Rotates values in this array for the given number of times. - For example: `[1,2,3,4,5]` rotate 2 is `[3,4,5,1,2]`. - @param arr The array to be rotated. - @param idx Index (or times) to rotate. - @warning This function takes a linear search time. - */ -yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, - size_t idx); - - - -/*============================================================================== - * Mutable JSON Array Modification Convenience API - *============================================================================*/ - -/** - Adds a value at the end of the array. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param val The value to be inserted. Returns false if it is NULL. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, - yyjson_mut_val *val); - -/** - Adds a `null` value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, - yyjson_mut_val *arr); - -/** - Adds a `true` value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, - yyjson_mut_val *arr); - -/** - Adds a `false` value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, - yyjson_mut_val *arr); - -/** - Adds a bool value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param val The bool value to be added. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - bool val); - -/** - Adds an unsigned integer value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param num The number to be added. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - uint64_t num); - -/** - Adds a signed integer value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param num The number to be added. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - int64_t num); - -/** - Adds a integer value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param num The number to be added. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - int64_t num); - -/** - Adds a double value at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param num The number to be added. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - double num); - -/** - Adds a string value at the end of the array (no copy). - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param str A null-terminated UTF-8 string. - @return Whether successful. - @warning The input string is not copied, you should keep this string unmodified - for the lifetime of this JSON document. - */ -yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str); - -/** - Adds a string value at the end of the array (no copy). - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param str A UTF-8 string, null-terminator is not required. - @param len The length of the string, in bytes. - @return Whether successful. - @warning The input string is not copied, you should keep this string unmodified - for the lifetime of this JSON document. - */ -yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str, - size_t len); - -/** - Adds a string value at the end of the array (copied). - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param str A null-terminated UTF-8 string. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str); - -/** - Adds a string value at the end of the array (copied). - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @param str A UTF-8 string, null-terminator is not required. - @param len The length of the string, in bytes. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str, - size_t len); - -/** - Creates and adds a new array at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @return The new array, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, - yyjson_mut_val *arr); - -/** - Creates and adds a new object at the end of the array. - @param doc The `doc` is only used for memory allocation. - @param arr The array to which the value is to be inserted. - Returns false if it is NULL or not an array. - @return The new object, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, - yyjson_mut_val *arr); - - - -/*============================================================================== - * Mutable JSON Object API - *============================================================================*/ - -/** Returns the number of key-value pairs in this object. - Returns 0 if `obj` is NULL or type is not object. */ -yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj); - -/** Returns the value to which the specified key is mapped. - Returns NULL if this object contains no mapping for the key. - Returns NULL if `obj/key` is NULL, or type is not object. - - The `key` should be a null-terminated UTF-8 string. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, - const char *key); - -/** Returns the value to which the specified key is mapped. - Returns NULL if this object contains no mapping for the key. - Returns NULL if `obj/key` is NULL, or type is not object. - - The `key` should be a UTF-8 string, null-terminator is not required. - The `key_len` should be the length of the key, in bytes. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, - const char *key, - size_t key_len); - - - -/*============================================================================== - * Mutable JSON Object Iterator API - *============================================================================*/ - -/** - A mutable JSON object iterator. - - @warning You should not modify the object while iterating over it, but you can - use `yyjson_mut_obj_iter_remove()` to remove current value. - - @par Example - @code - yyjson_mut_val *key, *val; - yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); - while ((key = yyjson_mut_obj_iter_next(&iter))) { - val = yyjson_mut_obj_iter_get_val(key); - your_func(key, val); - if (your_val_is_unused(key, val)) { - yyjson_mut_obj_iter_remove(&iter); - } - } - @endcode - - If the ordering of the keys is known at compile-time, you can use this method - to speed up value lookups: - @code - // {"k1":1, "k2": 3, "k3": 3} - yyjson_mut_val *key, *val; - yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); - yyjson_mut_val *v1 = yyjson_mut_obj_iter_get(&iter, "k1"); - yyjson_mut_val *v3 = yyjson_mut_obj_iter_get(&iter, "k3"); - @endcode - @see `yyjson_mut_obj_iter_get()` and `yyjson_mut_obj_iter_getn()` - */ -typedef struct yyjson_mut_obj_iter { - size_t idx; /**< next key's index */ - size_t max; /**< maximum key index (obj.size) */ - yyjson_mut_val *cur; /**< current key */ - yyjson_mut_val *pre; /**< previous key */ - yyjson_mut_val *obj; /**< the object being iterated */ -} yyjson_mut_obj_iter; - -/** - Initialize an iterator for this object. - - @param obj The object to be iterated over. - If this parameter is NULL or not an array, `iter` will be set to empty. - @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. - @return true if the `iter` has been successfully initialized. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, - yyjson_mut_obj_iter *iter); - -/** - Create an iterator with an object, same as `yyjson_obj_iter_init()`. - - @param obj The object to be iterated over. - If this parameter is NULL or not an object, an empty iterator will returned. - @return A new iterator for the object. - - @note The iterator does not need to be destroyed. - */ -yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( - yyjson_mut_val *obj); - -/** - Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. - */ -yyjson_api_inline bool yyjson_mut_obj_iter_has_next( - yyjson_mut_obj_iter *iter); - -/** - Returns the next key in the iteration, or NULL on end. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( - yyjson_mut_obj_iter *iter); - -/** - Returns the value for key inside the iteration. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( - yyjson_mut_val *key); - -/** - Removes current key-value pair in the iteration, returns the removed value. - If `iter` is NULL, this function will return NULL. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( - yyjson_mut_obj_iter *iter); - -/** - Iterates to a specified key and returns the value. - - This function does the same thing as `yyjson_mut_obj_get()`, but is much faster - if the ordering of the keys is known at compile-time and you are using the same - order to look up the values. If the key exists in this object, then the - iterator will stop at the next key, otherwise the iterator will not change and - NULL is returned. - - @param iter The object iterator, should not be NULL. - @param key The key, should be a UTF-8 string with null-terminator. - @return The value to which the specified key is mapped. - NULL if this object contains no mapping for the key or input is invalid. - - @warning This function takes a linear search time if the key is not nearby. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( - yyjson_mut_obj_iter *iter, const char *key); - -/** - Iterates to a specified key and returns the value. - - This function does the same thing as `yyjson_mut_obj_getn()` but is much faster - if the ordering of the keys is known at compile-time and you are using the same - order to look up the values. If the key exists in this object, then the - iterator will stop at the next key, otherwise the iterator will not change and - NULL is returned. - - @param iter The object iterator, should not be NULL. - @param key The key, should be a UTF-8 string, null-terminator is not required. - @param key_len The the length of `key`, in bytes. - @return The value to which the specified key is mapped. - NULL if this object contains no mapping for the key or input is invalid. - - @warning This function takes a linear search time if the key is not nearby. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( - yyjson_mut_obj_iter *iter, const char *key, size_t key_len); - -/** - Macro for iterating over an object. - It works like iterator, but with a more intuitive API. - - @warning You should not modify the object while iterating over it. - - @par Example - @code - size_t idx, max; - yyjson_val *key, *val; - yyjson_obj_foreach(obj, idx, max, key, val) { - your_func(key, val); - } - @endcode - */ -#define yyjson_mut_obj_foreach(obj, idx, max, key, val) \ - for ((idx) = 0, \ - (max) = yyjson_mut_obj_size(obj), \ - (key) = (max) ? ((yyjson_mut_val *)(obj)->uni.ptr)->next->next : NULL, \ - (val) = (key) ? (key)->next : NULL; \ - (idx) < (max); \ - (idx)++, \ - (key) = (val)->next, \ - (val) = (key)->next) - - - -/*============================================================================== - * Mutable JSON Object Creation API - *============================================================================*/ - -/** Creates and returns a mutable object, returns NULL on error. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc); - -/** - Creates and returns a mutable object with keys and values, returns NULL on - error. The keys and values are not copied. The strings should be a - null-terminated UTF-8 string. - - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. - - @par Example - @code - const char *keys[2] = { "id", "name" }; - const char *vals[2] = { "01", "Harry" }; - yyjson_mut_val *obj = yyjson_mut_obj_with_str(doc, keys, vals, 2); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, - const char **keys, - const char **vals, - size_t count); - -/** - Creates and returns a mutable object with key-value pairs and pair count, - returns NULL on error. The keys and values are not copied. The strings should - be a null-terminated UTF-8 string. - - @warning The input string is not copied, you should keep this string - unmodified for the lifetime of this JSON document. - - @par Example - @code - const char *kv_pairs[4] = { "id", "01", "name", "Harry" }; - yyjson_mut_val *obj = yyjson_mut_obj_with_kv(doc, kv_pairs, 2); - @endcode - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, - const char **kv_pairs, - size_t pair_count); - - - -/*============================================================================== - * Mutable JSON Object Modification API - *============================================================================*/ - -/** - Adds a key-value pair at the end of the object. - This function allows duplicated key in one object. - @param obj The object to which the new key-value pair is to be added. - @param key The key, should be a string which is created by `yyjson_mut_str()`, - `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. - @param val The value to add to the object. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val); -/** - Sets a key-value pair at the end of the object. - This function may remove all key-value pairs for the given key before add. - @param obj The object to which the new key-value pair is to be added. - @param key The key, should be a string which is created by `yyjson_mut_str()`, - `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. - @param val The value to add to the object. If this value is null, the behavior - is same as `yyjson_mut_obj_remove()`. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val); - -/** - Inserts a key-value pair to the object at the given position. - This function allows duplicated key in one object. - @param obj The object to which the new key-value pair is to be added. - @param key The key, should be a string which is created by `yyjson_mut_str()`, - `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. - @param val The value to add to the object. - @param idx The index to which to insert the new pair. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val, - size_t idx); - -/** - Removes all key-value pair from the object with given key. - @param obj The object from which the key-value pair is to be removed. - @param key The key, should be a string value. - @return The first matched value, or NULL if no matched value. - @warning This function takes a linear search time. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, - yyjson_mut_val *key); - -/** - Removes all key-value pair from the object with given key. - @param obj The object from which the key-value pair is to be removed. - @param key The key, should be a UTF-8 string with null-terminator. - @return The first matched value, or NULL if no matched value. - @warning This function takes a linear search time. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( - yyjson_mut_val *obj, const char *key); - -/** - Removes all key-value pair from the object with given key. - @param obj The object from which the key-value pair is to be removed. - @param key The key, should be a UTF-8 string, null-terminator is not required. - @param key_len The length of the key. - @return The first matched value, or NULL if no matched value. - @warning This function takes a linear search time. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( - yyjson_mut_val *obj, const char *key, size_t key_len); - -/** - Removes all key-value pairs in this object. - @param obj The object from which all of the values are to be removed. - @return Whether successful. - */ -yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj); - -/** - Replaces value from the object with given key. - If the key is not exist, or the value is NULL, it will fail. - @param obj The object to which the value is to be replaced. - @param key The key, should be a string value. - @param val The value to replace into the object. - @return Whether successful. - @warning This function takes a linear search time. - */ -yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val); - -/** - Rotates key-value pairs in the object for the given number of times. - For example: `{"a":1,"b":2,"c":3,"d":4}` rotate 1 is - `{"b":2,"c":3,"d":4,"a":1}`. - @param obj The object to be rotated. - @param idx Index (or times) to rotate. - @return Whether successful. - @warning This function takes a linear search time. - */ -yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, - size_t idx); - - - -/*============================================================================== - * Mutable JSON Object Modification Convenience API - *============================================================================*/ - -/** Adds a `null` value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key); - -/** Adds a `true` value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key); - -/** Adds a `false` value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key); - -/** Adds a bool value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, bool val); - -/** Adds an unsigned integer value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, uint64_t val); - -/** Adds a signed integer value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, int64_t val); - -/** Adds an int value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, int64_t val); - -/** Adds a double value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, double val); - -/** Adds a string value at the end of the object. - The `key` and `val` should be null-terminated UTF-8 strings. - This function allows duplicated key in one object. - - @warning The key/value strings are not copied, you should keep these strings - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, const char *val); - -/** Adds a string value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - The `val` should be a UTF-8 string, null-terminator is not required. - The `len` should be the length of the `val`, in bytes. - This function allows duplicated key in one object. - - @warning The key/value strings are not copied, you should keep these strings - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - const char *val, size_t len); - -/** Adds a string value at the end of the object. - The `key` and `val` should be null-terminated UTF-8 strings. - The value string is copied. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - const char *val); - -/** Adds a string value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - The `val` should be a UTF-8 string, null-terminator is not required. - The `len` should be the length of the `val`, in bytes. - This function allows duplicated key in one object. - - @warning The key strings are not copied, you should keep these strings - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - const char *val, size_t len); - -/** - Creates and adds a new array to the target object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep these strings - unmodified for the lifetime of this JSON document. - @return The new array, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key); - -/** - Creates and adds a new object to the target object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep these strings - unmodified for the lifetime of this JSON document. - @return The new object, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key); - -/** Adds a JSON value at the end of the object. - The `key` should be a null-terminated UTF-8 string. - This function allows duplicated key in one object. - - @warning The key string is not copied, you should keep the string - unmodified for the lifetime of this JSON document. */ -yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - yyjson_mut_val *val); - -/** Removes all key-value pairs for the given key. - Returns the first value to which the specified key is mapped or NULL if this - object contains no mapping for the key. - The `key` should be a null-terminated UTF-8 string. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str( - yyjson_mut_val *obj, const char *key); - -/** Removes all key-value pairs for the given key. - Returns the first value to which the specified key is mapped or NULL if this - object contains no mapping for the key. - The `key` should be a UTF-8 string, null-terminator is not required. - The `len` should be the length of the key, in bytes. - - @warning This function takes a linear search time. */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( - yyjson_mut_val *obj, const char *key, size_t len); - -/** Replaces all matching keys with the new key. - Returns true if at least one key was renamed. - The `key` and `new_key` should be a null-terminated UTF-8 string. - The `new_key` is copied and held by doc. - - @warning This function takes a linear search time. - If `new_key` already exists, it will cause duplicate keys. - */ -yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - const char *new_key); - -/** Replaces all matching keys with the new key. - Returns true if at least one key was renamed. - The `key` and `new_key` should be a UTF-8 string, - null-terminator is not required. The `new_key` is copied and held by doc. - - @warning This function takes a linear search time. - If `new_key` already exists, it will cause duplicate keys. - */ -yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - size_t len, - const char *new_key, - size_t new_len); - - - -/*============================================================================== - * JSON Pointer API (RFC 6901) - * https://tools.ietf.org/html/rfc6901 - *============================================================================*/ - -/** JSON Pointer error code. */ -typedef uint32_t yyjson_ptr_code; - -/** No JSON pointer error. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_NONE = 0; - -/** Invalid input parameter, such as NULL input. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_PARAMETER = 1; - -/** JSON pointer syntax error, such as invalid escape, token no prefix. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_SYNTAX = 2; - -/** JSON pointer resolve failed, such as index out of range, key not found. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_RESOLVE = 3; - -/** Document's root is NULL, but it is required for the function call. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_NULL_ROOT = 4; - -/** Cannot set root as the target is not a document. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_SET_ROOT = 5; - -/** The memory allocation failed and a new value could not be created. */ -static const yyjson_ptr_code YYJSON_PTR_ERR_MEMORY_ALLOCATION = 6; - -/** Error information for JSON pointer. */ -typedef struct yyjson_ptr_err { - /** Error code, see `yyjson_ptr_code` for all possible values. */ - yyjson_ptr_code code; - /** Error message, constant, no need to free (NULL if no error). */ - const char *msg; - /** Error byte position for input JSON pointer (0 if no error). */ - size_t pos; -} yyjson_ptr_err; - -/** - A context for JSON pointer operation. - - This struct stores the context of JSON Pointer operation result. The struct - can be used with three helper functions: `ctx_append()`, `ctx_replace()`, and - `ctx_remove()`, which perform the corresponding operations on the container - without re-parsing the JSON Pointer. - - For example: - @code - // doc before: {"a":[0,1,null]} - // ptr: "/a/2" - val = yyjson_mut_doc_ptr_getx(doc, ptr, strlen(ptr), &ctx, &err); - if (yyjson_is_null(val)) { - yyjson_ptr_ctx_remove(&ctx); - } - // doc after: {"a":[0,1]} - @endcode - */ -typedef struct yyjson_ptr_ctx { - /** - The container (parent) of the target value. It can be either an array or - an object. If the target location has no value, but all its parent - containers exist, and the target location can be used to insert a new - value, then `ctn` is the parent container of the target location. - Otherwise, `ctn` is NULL. - */ - yyjson_mut_val *ctn; - /** - The previous sibling of the target value. It can be either a value in an - array or a key in an object. As the container is a `circular linked list` - of elements, `pre` is the previous node of the target value. If the - operation is `add` or `set`, then `pre` is the previous node of the new - value, not the original target value. If the target value does not exist, - `pre` is NULL. - */ - yyjson_mut_val *pre; - /** - The removed value if the operation is `set`, `replace` or `remove`. It can - be used to restore the original state of the document if needed. - */ - yyjson_mut_val *old; -} yyjson_ptr_ctx; - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, - const char *ptr); - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, - const char *ptr, size_t len); - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param err A pointer to store the error information, or NULL if not needed. - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, - const char *ptr, size_t len, - yyjson_ptr_err *err); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, - const char *ptr); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, - const char *ptr, size_t len); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param err A pointer to store the error information, or NULL if not needed. - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, - const char *ptr, size_t len, - yyjson_ptr_err *err); - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, - const char *ptr); - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, - const char *ptr, - size_t len); - -/** - Get value by a JSON Pointer. - @param doc The JSON document to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The value referenced by the JSON pointer. - NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, - const char *ptr); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, - const char *ptr, - size_t len); - -/** - Get value by a JSON Pointer. - @param val The JSON value to be queried. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The value referenced by the JSON pointer. - NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Add (insert) value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param new_val The value to be added. - @return true if JSON pointer is valid and new value is added, false otherwise. - @note The parent nodes will be created if they do not exist. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, - const char *ptr, - yyjson_mut_val *new_val); - -/** - Add (insert) value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be added. - @return true if JSON pointer is valid and new value is added, false otherwise. - @note The parent nodes will be created if they do not exist. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val); - -/** - Add (insert) value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be added. - @param create_parent Whether to create parent nodes if not exist. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return true if JSON pointer is valid and new value is added, false otherwise. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Add (insert) value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param doc Only used to create new values when needed. - @param new_val The value to be added. - @return true if JSON pointer is valid and new value is added, false otherwise. - @note The parent nodes will be created if they do not exist. - */ -yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, - const char *ptr, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc); - -/** - Add (insert) value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param doc Only used to create new values when needed. - @param new_val The value to be added. - @return true if JSON pointer is valid and new value is added, false otherwise. - @note The parent nodes will be created if they do not exist. - */ -yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc); - -/** - Add (insert) value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param doc Only used to create new values when needed. - @param new_val The value to be added. - @param create_parent Whether to create parent nodes if not exist. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return true if JSON pointer is valid and new value is added, false otherwise. - */ -yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Set value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param new_val The value to be set, pass NULL to remove. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note The parent nodes will be created if they do not exist. - If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, - const char *ptr, - yyjson_mut_val *new_val); - -/** - Set value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be set, pass NULL to remove. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note The parent nodes will be created if they do not exist. - If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val); - -/** - Set value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be set, pass NULL to remove. - @param create_parent Whether to create parent nodes if not exist. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Set value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param new_val The value to be set, pass NULL to remove. - @param doc Only used to create new values when needed. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note The parent nodes will be created if they do not exist. - If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, - const char *ptr, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc); - -/** - Set value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be set, pass NULL to remove. - @param doc Only used to create new values when needed. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note The parent nodes will be created if they do not exist. - If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc); - -/** - Set value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The value to be set, pass NULL to remove. - @param doc Only used to create new values when needed. - @param create_parent Whether to create parent nodes if not exist. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return true if JSON pointer is valid and new value is set, false otherwise. - @note If the target value already exists, it will be replaced by the new value. - */ -yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Replace value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param new_val The new value to replace the old one. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( - yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); - -/** - Replace value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The new value to replace the old one. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( - yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); - -/** - Replace value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The new value to replace the old one. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( - yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); - -/** - Replace value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @param new_val The new value to replace the old one. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( - yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val); - -/** - Replace value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The new value to replace the old one. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val); - -/** - Replace value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param new_val The new value to replace the old one. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The old value that was replaced, or NULL if not found. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); - -/** - Remove value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( - yyjson_mut_doc *doc, const char *ptr); - -/** - Remove value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( - yyjson_mut_doc *doc, const char *ptr, size_t len); - -/** - Remove value by a JSON pointer. - @param doc The target JSON document. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( - yyjson_mut_doc *doc, const char *ptr, size_t len, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); - -/** - Remove value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8 with null-terminator). - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, - const char *ptr); - -/** - Remove value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, - const char *ptr, - size_t len); - -/** - Remove value by a JSON pointer. - @param val The target JSON value. - @param ptr The JSON pointer string (UTF-8, null-terminator is not required). - @param len The length of `ptr` in bytes. - @param ctx A pointer to store the result context, or NULL if not needed. - @param err A pointer to store the error information, or NULL if not needed. - @return The removed value, or NULL on error. - */ -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/** - Append value by JSON pointer context. - @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. - @param key New key if `ctx->ctn` is object, or NULL if `ctx->ctn` is array. - @param val New value to be added. - @return true on success or false on fail. - */ -yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, - yyjson_mut_val *key, - yyjson_mut_val *val); - -/** - Replace value by JSON pointer context. - @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. - @param val New value to be replaced. - @return true on success or false on fail. - @note If success, the old value will be returned via `ctx->old`. - */ -yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, - yyjson_mut_val *val); - -/** - Remove value by JSON pointer context. - @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. - @return true on success or false on fail. - @note If success, the old value will be returned via `ctx->old`. - */ -yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx); - - - -/*============================================================================== - * JSON Patch API (RFC 6902) - * https://tools.ietf.org/html/rfc6902 - *============================================================================*/ - -/** Result code for JSON patch. */ -typedef uint32_t yyjson_patch_code; - -/** Success, no error. */ -static const yyjson_patch_code YYJSON_PATCH_SUCCESS = 0; - -/** Invalid parameter, such as NULL input or non-array patch. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_PARAMETER = 1; - -/** Memory allocation failure occurs. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_MEMORY_ALLOCATION = 2; - -/** JSON patch operation is not object type. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_OPERATION = 3; - -/** JSON patch operation is missing a required key. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_MISSING_KEY = 4; - -/** JSON patch operation member is invalid. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_MEMBER = 5; - -/** JSON patch operation `test` not equal. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_EQUAL = 6; - -/** JSON patch operation failed on JSON pointer. */ -static const yyjson_patch_code YYJSON_PATCH_ERROR_POINTER = 7; - -/** Error information for JSON patch. */ -typedef struct yyjson_patch_err { - /** Error code, see `yyjson_patch_code` for all possible values. */ - yyjson_patch_code code; - /** Index of the error operation (0 if no error). */ - size_t idx; - /** Error message, constant, no need to free (NULL if no error). */ - const char *msg; - /** JSON pointer error if `code == YYJSON_PATCH_ERROR_POINTER`. */ - yyjson_ptr_err ptr; -} yyjson_patch_err; - -/** - Creates and returns a patched JSON value (RFC 6902). - The memory of the returned value is allocated by the `doc`. - The `err` is used to receive error information, pass NULL if not needed. - Returns NULL if the patch could not be applied. - */ -yyjson_api yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, - yyjson_val *orig, - yyjson_val *patch, - yyjson_patch_err *err); - -/** - Creates and returns a patched JSON value (RFC 6902). - The memory of the returned value is allocated by the `doc`. - The `err` is used to receive error information, pass NULL if not needed. - Returns NULL if the patch could not be applied. - */ -yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, - yyjson_mut_val *orig, - yyjson_mut_val *patch, - yyjson_patch_err *err); - - - -/*============================================================================== - * JSON Merge-Patch API (RFC 7386) - * https://tools.ietf.org/html/rfc7386 - *============================================================================*/ - -/** - Creates and returns a merge-patched JSON value (RFC 7386). - The memory of the returned value is allocated by the `doc`. - Returns NULL if the patch could not be applied. - - @warning This function is recursive and may cause a stack overflow if the - object level is too deep. - */ -yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, - yyjson_val *orig, - yyjson_val *patch); - -/** - Creates and returns a merge-patched JSON value (RFC 7386). - The memory of the returned value is allocated by the `doc`. - Returns NULL if the patch could not be applied. - - @warning This function is recursive and may cause a stack overflow if the - object level is too deep. - */ -yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, - yyjson_mut_val *orig, - yyjson_mut_val *patch); - - - -/*============================================================================== - * JSON Structure (Implementation) - *============================================================================*/ - -/** Payload of a JSON value (8 bytes). */ -typedef union yyjson_val_uni { - uint64_t u64; - int64_t i64; - double f64; - const char *str; - void *ptr; - size_t ofs; -} yyjson_val_uni; - -/** - Immutable JSON value, 16 bytes. - */ -struct yyjson_val { - uint64_t tag; /**< type, subtype and length */ - yyjson_val_uni uni; /**< payload */ -}; - -struct yyjson_doc { - /** Root value of the document (nonnull). */ - yyjson_val *root; - /** Allocator used by document (nonnull). */ - yyjson_alc alc; - /** The total number of bytes read when parsing JSON (nonzero). */ - size_t dat_read; - /** The total number of value read when parsing JSON (nonzero). */ - size_t val_read; - /** The string pool used by JSON values (nullable). */ - char *str_pool; -}; - - - -/*============================================================================== - * Unsafe JSON Value API (Implementation) - *============================================================================*/ - -/* - Whether the string does not need to be escaped for serialization. - This function is used to optimize the writing speed of small constant strings. - This function works only if the compiler can evaluate it at compile time. - - Clang supports it since v8.0, - earlier versions do not support constant_p(strlen) and return false. - GCC supports it since at least v4.4, - earlier versions may compile it as run-time instructions. - ICC supports it since at least v16, - earlier versions are uncertain. - - @param str The C string. - @param len The returnd value from strlen(str). - */ -yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { -#if YYJSON_HAS_CONSTANT_P && \ - (!YYJSON_IS_REAL_GCC || yyjson_gcc_available(4, 4, 0)) - if (yyjson_constant_p(len) && len <= 32) { - /* - Same as the following loop: - - for (size_t i = 0; i < len; i++) { - char c = str[i]; - if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; - } - - GCC evaluates it at compile time only if the string length is within 17 - and -O3 (which turns on the -fpeel-loops flag) is used. - So the loop is unrolled for GCC. - */ -# define yyjson_repeat32_incr(x) \ - x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ - x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) \ - x(16) x(17) x(18) x(19) x(20) x(21) x(22) x(23) \ - x(24) x(25) x(26) x(27) x(28) x(29) x(30) x(31) -# define yyjson_check_char_noesc(i) \ - if (i < len) { \ - char c = str[i]; \ - if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; } - yyjson_repeat32_incr(yyjson_check_char_noesc) -# undef yyjson_repeat32_incr -# undef yyjson_check_char_noesc - return true; - } -#else - (void)str; - (void)len; -#endif - return false; -} - -yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) { - uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; - return (yyjson_type)(tag & YYJSON_TYPE_MASK); -} - -yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(void *val) { - uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; - return (yyjson_subtype)(tag & YYJSON_SUBTYPE_MASK); -} - -yyjson_api_inline uint8_t unsafe_yyjson_get_tag(void *val) { - uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; - return (uint8_t)(tag & YYJSON_TAG_MASK); -} - -yyjson_api_inline bool unsafe_yyjson_is_raw(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_RAW; -} - -yyjson_api_inline bool unsafe_yyjson_is_null(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL; -} - -yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL; -} - -yyjson_api_inline bool unsafe_yyjson_is_num(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM; -} - -yyjson_api_inline bool unsafe_yyjson_is_str(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR; -} - -yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR; -} - -yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) { - return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ; -} - -yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) { - uint8_t mask = YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ; - return (unsafe_yyjson_get_tag(val) & mask) == mask; -} - -yyjson_api_inline bool unsafe_yyjson_is_uint(void *val) { - const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - return unsafe_yyjson_get_tag(val) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_is_sint(void *val) { - const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - return unsafe_yyjson_get_tag(val) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_is_int(void *val) { - const uint8_t mask = YYJSON_TAG_MASK & (~YYJSON_SUBTYPE_SINT); - const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - return (unsafe_yyjson_get_tag(val) & mask) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_is_real(void *val) { - const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - return unsafe_yyjson_get_tag(val) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_is_true(void *val) { - const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; - return unsafe_yyjson_get_tag(val) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_is_false(void *val) { - const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; - return unsafe_yyjson_get_tag(val) == patt; -} - -yyjson_api_inline bool unsafe_yyjson_arr_is_flat(yyjson_val *val) { - size_t ofs = val->uni.ofs; - size_t len = (size_t)(val->tag >> YYJSON_TAG_BIT); - return len * sizeof(yyjson_val) + sizeof(yyjson_val) == ofs; -} - -yyjson_api_inline const char *unsafe_yyjson_get_raw(void *val) { - return ((yyjson_val *)val)->uni.str; -} - -yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) { - uint8_t tag = unsafe_yyjson_get_tag(val); - return (bool)((tag & YYJSON_SUBTYPE_MASK) >> YYJSON_TYPE_BIT); -} - -yyjson_api_inline uint64_t unsafe_yyjson_get_uint(void *val) { - return ((yyjson_val *)val)->uni.u64; -} - -yyjson_api_inline int64_t unsafe_yyjson_get_sint(void *val) { - return ((yyjson_val *)val)->uni.i64; -} - -yyjson_api_inline int unsafe_yyjson_get_int(void *val) { - return (int)((yyjson_val *)val)->uni.i64; -} - -yyjson_api_inline double unsafe_yyjson_get_real(void *val) { - return ((yyjson_val *)val)->uni.f64; -} - -yyjson_api_inline double unsafe_yyjson_get_num(void *val) { - uint8_t tag = unsafe_yyjson_get_tag(val); - if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL)) { - return ((yyjson_val *)val)->uni.f64; - } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT)) { - return (double)((yyjson_val *)val)->uni.i64; - } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT)) { -#if YYJSON_U64_TO_F64_NO_IMPL - uint64_t msb = ((uint64_t)1) << 63; - uint64_t num = ((yyjson_val *)val)->uni.u64; - if ((num & msb) == 0) { - return (double)(int64_t)num; - } else { - return ((double)(int64_t)((num >> 1) | (num & 1))) * (double)2.0; - } -#else - return (double)((yyjson_val *)val)->uni.u64; -#endif - } - return 0.0; -} - -yyjson_api_inline const char *unsafe_yyjson_get_str(void *val) { - return ((yyjson_val *)val)->uni.str; -} - -yyjson_api_inline size_t unsafe_yyjson_get_len(void *val) { - return (size_t)(((yyjson_val *)val)->tag >> YYJSON_TAG_BIT); -} - -yyjson_api_inline yyjson_val *unsafe_yyjson_get_first(yyjson_val *ctn) { - return ctn + 1; -} - -yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) { - bool is_ctn = unsafe_yyjson_is_ctn(val); - size_t ctn_ofs = val->uni.ofs; - size_t ofs = (is_ctn ? ctn_ofs : sizeof(yyjson_val)); - return (yyjson_val *)(void *)((uint8_t *)val + ofs); -} - -yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str, - size_t len) { - return unsafe_yyjson_get_len(val) == len && - memcmp(((yyjson_val *)val)->uni.str, str, len) == 0; -} - -yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) { - return unsafe_yyjson_equals_strn(val, str, strlen(str)); -} - -yyjson_api_inline void unsafe_yyjson_set_type(void *val, yyjson_type type, - yyjson_subtype subtype) { - uint8_t tag = (type | subtype); - uint64_t new_tag = ((yyjson_val *)val)->tag; - new_tag = (new_tag & (~(uint64_t)YYJSON_TAG_MASK)) | (uint64_t)tag; - ((yyjson_val *)val)->tag = new_tag; -} - -yyjson_api_inline void unsafe_yyjson_set_len(void *val, size_t len) { - uint64_t tag = ((yyjson_val *)val)->tag & YYJSON_TAG_MASK; - tag |= (uint64_t)len << YYJSON_TAG_BIT; - ((yyjson_val *)val)->tag = tag; -} - -yyjson_api_inline void unsafe_yyjson_inc_len(void *val) { - uint64_t tag = ((yyjson_val *)val)->tag; - tag += (uint64_t)(1 << YYJSON_TAG_BIT); - ((yyjson_val *)val)->tag = tag; -} - -yyjson_api_inline void unsafe_yyjson_set_raw(void *val, const char *raw, - size_t len) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_RAW, YYJSON_SUBTYPE_NONE); - unsafe_yyjson_set_len(val, len); - ((yyjson_val *)val)->uni.str = raw; -} - -yyjson_api_inline void unsafe_yyjson_set_null(void *val) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_NULL, YYJSON_SUBTYPE_NONE); - unsafe_yyjson_set_len(val, 0); -} - -yyjson_api_inline void unsafe_yyjson_set_bool(void *val, bool num) { - yyjson_subtype subtype = num ? YYJSON_SUBTYPE_TRUE : YYJSON_SUBTYPE_FALSE; - unsafe_yyjson_set_type(val, YYJSON_TYPE_BOOL, subtype); - unsafe_yyjson_set_len(val, 0); -} - -yyjson_api_inline void unsafe_yyjson_set_uint(void *val, uint64_t num) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT); - unsafe_yyjson_set_len(val, 0); - ((yyjson_val *)val)->uni.u64 = num; -} - -yyjson_api_inline void unsafe_yyjson_set_sint(void *val, int64_t num) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT); - unsafe_yyjson_set_len(val, 0); - ((yyjson_val *)val)->uni.i64 = num; -} - -yyjson_api_inline void unsafe_yyjson_set_real(void *val, double num) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL); - unsafe_yyjson_set_len(val, 0); - ((yyjson_val *)val)->uni.f64 = num; -} - -yyjson_api_inline void unsafe_yyjson_set_str(void *val, const char *str) { - size_t len = strlen(str); - bool noesc = unsafe_yyjson_is_str_noesc(str, len); - yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; - unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, sub); - unsafe_yyjson_set_len(val, len); - ((yyjson_val *)val)->uni.str = str; -} - -yyjson_api_inline void unsafe_yyjson_set_strn(void *val, const char *str, - size_t len) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, YYJSON_SUBTYPE_NONE); - unsafe_yyjson_set_len(val, len); - ((yyjson_val *)val)->uni.str = str; -} - -yyjson_api_inline void unsafe_yyjson_set_arr(void *val, size_t size) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_ARR, YYJSON_SUBTYPE_NONE); - unsafe_yyjson_set_len(val, size); -} - -yyjson_api_inline void unsafe_yyjson_set_obj(void *val, size_t size) { - unsafe_yyjson_set_type(val, YYJSON_TYPE_OBJ, YYJSON_SUBTYPE_NONE); - unsafe_yyjson_set_len(val, size); -} - - - -/*============================================================================== - * JSON Document API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) { - return doc ? doc->root : NULL; -} - -yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) { - return doc ? doc->dat_read : 0; -} - -yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { - return doc ? doc->val_read : 0; -} - -yyjson_api void yyjson_doc_free(yyjson_doc *doc) { - if (doc) { - yyjson_alc alc = doc->alc; - memset(&doc->alc, 0, sizeof(alc)); - if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); - alc.free(alc.ctx, doc); - } -} - - - -/*============================================================================== - * JSON Value Type API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_is_raw(yyjson_val *val) { - return val ? unsafe_yyjson_is_raw(val) : false; -} - -yyjson_api_inline bool yyjson_is_null(yyjson_val *val) { - return val ? unsafe_yyjson_is_null(val) : false; -} - -yyjson_api_inline bool yyjson_is_true(yyjson_val *val) { - return val ? unsafe_yyjson_is_true(val) : false; -} - -yyjson_api_inline bool yyjson_is_false(yyjson_val *val) { - return val ? unsafe_yyjson_is_false(val) : false; -} - -yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) { - return val ? unsafe_yyjson_is_bool(val) : false; -} - -yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) { - return val ? unsafe_yyjson_is_uint(val) : false; -} - -yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) { - return val ? unsafe_yyjson_is_sint(val) : false; -} - -yyjson_api_inline bool yyjson_is_int(yyjson_val *val) { - return val ? unsafe_yyjson_is_int(val) : false; -} - -yyjson_api_inline bool yyjson_is_real(yyjson_val *val) { - return val ? unsafe_yyjson_is_real(val) : false; -} - -yyjson_api_inline bool yyjson_is_num(yyjson_val *val) { - return val ? unsafe_yyjson_is_num(val) : false; -} - -yyjson_api_inline bool yyjson_is_str(yyjson_val *val) { - return val ? unsafe_yyjson_is_str(val) : false; -} - -yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) { - return val ? unsafe_yyjson_is_arr(val) : false; -} - -yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) { - return val ? unsafe_yyjson_is_obj(val) : false; -} - -yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { - return val ? unsafe_yyjson_is_ctn(val) : false; -} - - - -/*============================================================================== - * JSON Value Content API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { - return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE; -} - -yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) { - return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE; -} - -yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) { - return val ? unsafe_yyjson_get_tag(val) : 0; -} - -yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { - switch (yyjson_get_tag(val)) { - case YYJSON_TYPE_RAW | YYJSON_SUBTYPE_NONE: return "raw"; - case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE: return "null"; - case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE: return "string"; - case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC: return "string"; - case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE: return "array"; - case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE: return "object"; - case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE: return "true"; - case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE: return "false"; - case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: return "uint"; - case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: return "sint"; - case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: return "real"; - default: return "unknown"; - } -} - -yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val) { - return yyjson_is_raw(val) ? unsafe_yyjson_get_raw(val) : NULL; -} - -yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) { - return yyjson_is_bool(val) ? unsafe_yyjson_get_bool(val) : false; -} - -yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val) { - return yyjson_is_int(val) ? unsafe_yyjson_get_uint(val) : 0; -} - -yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val) { - return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0; -} - -yyjson_api_inline int yyjson_get_int(yyjson_val *val) { - return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0; -} - -yyjson_api_inline double yyjson_get_real(yyjson_val *val) { - return yyjson_is_real(val) ? unsafe_yyjson_get_real(val) : 0.0; -} - -yyjson_api_inline double yyjson_get_num(yyjson_val *val) { - return val ? unsafe_yyjson_get_num(val) : 0.0; -} - -yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) { - return yyjson_is_str(val) ? unsafe_yyjson_get_str(val) : NULL; -} - -yyjson_api_inline size_t yyjson_get_len(yyjson_val *val) { - return val ? unsafe_yyjson_get_len(val) : 0; -} - -yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { - if (yyjson_likely(val && str)) { - return unsafe_yyjson_is_str(val) && - unsafe_yyjson_equals_str(val, str); - } - return false; -} - -yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, - size_t len) { - if (yyjson_likely(val && str)) { - return unsafe_yyjson_is_str(val) && - unsafe_yyjson_equals_strn(val, str, len); - } - return false; -} - -yyjson_api bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); - -yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { - if (yyjson_unlikely(!lhs || !rhs)) return false; - return unsafe_yyjson_equals(lhs, rhs); -} - -yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, - const char *raw, size_t len) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_raw(val, raw, len); - return true; -} - -yyjson_api_inline bool yyjson_set_null(yyjson_val *val) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_null(val); - return true; -} - -yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_bool(val, num); - return true; -} - -yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_uint(val, num); - return true; -} - -yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_sint(val, num); - return true; -} - -yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_sint(val, (int64_t)num); - return true; -} - -yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_real(val, num); - return true; -} - -yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - if (yyjson_unlikely(!str)) return false; - unsafe_yyjson_set_str(val, str); - return true; -} - -yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, - const char *str, size_t len) { - if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - if (yyjson_unlikely(!str)) return false; - unsafe_yyjson_set_strn(val, str, len); - return true; -} - - - -/*============================================================================== - * JSON Array API (Implementation) - *============================================================================*/ - -yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) { - return yyjson_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; -} - -yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) { - if (yyjson_likely(yyjson_is_arr(arr))) { - if (yyjson_likely(unsafe_yyjson_get_len(arr) > idx)) { - yyjson_val *val = unsafe_yyjson_get_first(arr); - if (unsafe_yyjson_arr_is_flat(arr)) { - return val + idx; - } else { - while (idx-- > 0) val = unsafe_yyjson_get_next(val); - return val; - } - } - } - return NULL; -} - -yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { - if (yyjson_likely(yyjson_is_arr(arr))) { - if (yyjson_likely(unsafe_yyjson_get_len(arr) > 0)) { - return unsafe_yyjson_get_first(arr); - } - } - return NULL; -} - -yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { - if (yyjson_likely(yyjson_is_arr(arr))) { - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_likely(len > 0)) { - yyjson_val *val = unsafe_yyjson_get_first(arr); - if (unsafe_yyjson_arr_is_flat(arr)) { - return val + (len - 1); - } else { - while (len-- > 1) val = unsafe_yyjson_get_next(val); - return val; - } - } - } - return NULL; -} - - - -/*============================================================================== - * JSON Array Iterator API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, - yyjson_arr_iter *iter) { - if (yyjson_likely(yyjson_is_arr(arr) && iter)) { - iter->idx = 0; - iter->max = unsafe_yyjson_get_len(arr); - iter->cur = unsafe_yyjson_get_first(arr); - return true; - } - if (iter) memset(iter, 0, sizeof(yyjson_arr_iter)); - return false; -} - -yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr) { - yyjson_arr_iter iter; - yyjson_arr_iter_init(arr, &iter); - return iter; -} - -yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter) { - return iter ? iter->idx < iter->max : false; -} - -yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) { - yyjson_val *val; - if (iter && iter->idx < iter->max) { - val = iter->cur; - iter->cur = unsafe_yyjson_get_next(val); - iter->idx++; - return val; - } - return NULL; -} - - - -/*============================================================================== - * JSON Object API (Implementation) - *============================================================================*/ - -yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) { - return yyjson_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; -} - -yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, - const char *key) { - return yyjson_obj_getn(obj, key, key ? strlen(key) : 0); -} - -yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, - const char *_key, - size_t key_len) { - if (yyjson_likely(yyjson_is_obj(obj) && _key)) { - size_t len = unsafe_yyjson_get_len(obj); - yyjson_val *key = unsafe_yyjson_get_first(obj); - while (len-- > 0) { - if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key + 1; - key = unsafe_yyjson_get_next(key + 1); - } - } - return NULL; -} - - - -/*============================================================================== - * JSON Object Iterator API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, - yyjson_obj_iter *iter) { - if (yyjson_likely(yyjson_is_obj(obj) && iter)) { - iter->idx = 0; - iter->max = unsafe_yyjson_get_len(obj); - iter->cur = unsafe_yyjson_get_first(obj); - iter->obj = obj; - return true; - } - if (iter) memset(iter, 0, sizeof(yyjson_obj_iter)); - return false; -} - -yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj) { - yyjson_obj_iter iter; - yyjson_obj_iter_init(obj, &iter); - return iter; -} - -yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter) { - return iter ? iter->idx < iter->max : false; -} - -yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter) { - if (iter && iter->idx < iter->max) { - yyjson_val *key = iter->cur; - iter->idx++; - iter->cur = unsafe_yyjson_get_next(key + 1); - return key; - } - return NULL; -} - -yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key) { - return key ? key + 1 : NULL; -} - -yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, - const char *key) { - return yyjson_obj_iter_getn(iter, key, key ? strlen(key) : 0); -} - -yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, - const char *key, - size_t key_len) { - if (iter && key) { - size_t idx = iter->idx; - size_t max = iter->max; - yyjson_val *cur = iter->cur; - if (yyjson_unlikely(idx == max)) { - idx = 0; - cur = unsafe_yyjson_get_first(iter->obj); - } - while (idx++ < max) { - yyjson_val *next = unsafe_yyjson_get_next(cur + 1); - if (unsafe_yyjson_equals_strn(cur, key, key_len)) { - iter->idx = idx; - iter->cur = next; - return cur + 1; - } - cur = next; - if (idx == iter->max && iter->idx < iter->max) { - idx = 0; - max = iter->idx; - cur = unsafe_yyjson_get_first(iter->obj); - } - } - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Structure (Implementation) - *============================================================================*/ - -/** - Mutable JSON value, 24 bytes. - The 'tag' and 'uni' field is same as immutable value. - The 'next' field links all elements inside the container to be a cycle. - */ -struct yyjson_mut_val { - uint64_t tag; /**< type, subtype and length */ - yyjson_val_uni uni; /**< payload */ - yyjson_mut_val *next; /**< the next value in circular linked list */ -}; - -/** - A memory chunk in string memory pool. - */ -typedef struct yyjson_str_chunk { - struct yyjson_str_chunk *next; /* next chunk linked list */ - size_t chunk_size; /* chunk size in bytes */ - /* char str[]; flexible array member */ -} yyjson_str_chunk; - -/** - A memory pool to hold all strings in a mutable document. - */ -typedef struct yyjson_str_pool { - char *cur; /* cursor inside current chunk */ - char *end; /* the end of current chunk */ - size_t chunk_size; /* chunk size in bytes while creating new chunk */ - size_t chunk_size_max; /* maximum chunk size in bytes */ - yyjson_str_chunk *chunks; /* a linked list of chunks, nullable */ -} yyjson_str_pool; - -/** - A memory chunk in value memory pool. - `sizeof(yyjson_val_chunk)` should not larger than `sizeof(yyjson_mut_val)`. - */ -typedef struct yyjson_val_chunk { - struct yyjson_val_chunk *next; /* next chunk linked list */ - size_t chunk_size; /* chunk size in bytes */ - /* char pad[sizeof(yyjson_mut_val) - sizeof(yyjson_val_chunk)]; padding */ - /* yyjson_mut_val vals[]; flexible array member */ -} yyjson_val_chunk; - -/** - A memory pool to hold all values in a mutable document. - */ -typedef struct yyjson_val_pool { - yyjson_mut_val *cur; /* cursor inside current chunk */ - yyjson_mut_val *end; /* the end of current chunk */ - size_t chunk_size; /* chunk size in bytes while creating new chunk */ - size_t chunk_size_max; /* maximum chunk size in bytes */ - yyjson_val_chunk *chunks; /* a linked list of chunks, nullable */ -} yyjson_val_pool; - -struct yyjson_mut_doc { - yyjson_mut_val *root; /**< root value of the JSON document, nullable */ - yyjson_alc alc; /**< a valid allocator, nonnull */ - yyjson_str_pool str_pool; /**< string memory pool */ - yyjson_val_pool val_pool; /**< value memory pool */ -}; - -/* Ensures the capacity to at least equal to the specified byte length. */ -yyjson_api bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, - const yyjson_alc *alc, - size_t len); - -/* Ensures the capacity to at least equal to the specified value count. */ -yyjson_api bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, - const yyjson_alc *alc, - size_t count); - -/* Allocate memory for string. */ -yyjson_api_inline char *unsafe_yyjson_mut_str_alc(yyjson_mut_doc *doc, - size_t len) { - char *mem; - const yyjson_alc *alc = &doc->alc; - yyjson_str_pool *pool = &doc->str_pool; - if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) { - if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) { - return NULL; - } - } - mem = pool->cur; - pool->cur = mem + len + 1; - return mem; -} - -yyjson_api_inline char *unsafe_yyjson_mut_strncpy(yyjson_mut_doc *doc, - const char *str, size_t len) { - char *mem = unsafe_yyjson_mut_str_alc(doc, len); - if (yyjson_unlikely(!mem)) return NULL; - memcpy((void *)mem, (const void *)str, len); - mem[len] = '\0'; - return mem; -} - -yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc, - size_t count) { - yyjson_mut_val *val; - yyjson_alc *alc = &doc->alc; - yyjson_val_pool *pool = &doc->val_pool; - if (yyjson_unlikely((size_t)(pool->end - pool->cur) < count)) { - if (yyjson_unlikely(!unsafe_yyjson_val_pool_grow(pool, alc, count))) { - return NULL; - } - } - val = pool->cur; - pool->cur += count; - return val; -} - - - -/*============================================================================== - * Mutable JSON Document API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) { - return doc ? doc->root : NULL; -} - -yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, - yyjson_mut_val *root) { - if (doc) doc->root = root; -} - - - -/*============================================================================== - * Mutable JSON Value Type API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_raw(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_null(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_true(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_false(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_bool(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_uint(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_sint(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_int(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_real(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_num(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_str(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_arr(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_obj(val) : false; -} - -yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { - return val ? unsafe_yyjson_is_ctn(val) : false; -} - - - -/*============================================================================== - * Mutable JSON Value Content API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val) { - return yyjson_get_type((yyjson_val *)val); -} - -yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val) { - return yyjson_get_subtype((yyjson_val *)val); -} - -yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val) { - return yyjson_get_tag((yyjson_val *)val); -} - -yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val) { - return yyjson_get_type_desc((yyjson_val *)val); -} - -yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val) { - return yyjson_get_raw((yyjson_val *)val); -} - -yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val) { - return yyjson_get_bool((yyjson_val *)val); -} - -yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val) { - return yyjson_get_uint((yyjson_val *)val); -} - -yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val) { - return yyjson_get_sint((yyjson_val *)val); -} - -yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val) { - return yyjson_get_int((yyjson_val *)val); -} - -yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val) { - return yyjson_get_real((yyjson_val *)val); -} - -yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val) { - return yyjson_get_num((yyjson_val *)val); -} - -yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val) { - return yyjson_get_str((yyjson_val *)val); -} - -yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val) { - return yyjson_get_len((yyjson_val *)val); -} - -yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, - const char *str) { - return yyjson_equals_str((yyjson_val *)val, str); -} - -yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, - const char *str, size_t len) { - return yyjson_equals_strn((yyjson_val *)val, str, len); -} - -yyjson_api bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, - yyjson_mut_val *rhs); - -yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, - yyjson_mut_val *rhs) { - if (yyjson_unlikely(!lhs || !rhs)) return false; - return unsafe_yyjson_mut_equals(lhs, rhs); -} - -yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, - const char *raw, size_t len) { - if (yyjson_unlikely(!val || !raw)) return false; - unsafe_yyjson_set_raw(val, raw, len); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_null(val); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_bool(val, num); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_uint(val, num); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_sint(val, num); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_sint(val, (int64_t)num); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_real(val, num); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, - const char *str) { - if (yyjson_unlikely(!val || !str)) return false; - unsafe_yyjson_set_str(val, str); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, - const char *str, size_t len) { - if (yyjson_unlikely(!val || !str)) return false; - unsafe_yyjson_set_strn(val, str, len); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_arr(val, 0); - return true; -} - -yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val) { - if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_obj(val, 0); - return true; -} - - - -/*============================================================================== - * Mutable JSON Value Creation API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, - const char *str) { - if (yyjson_likely(str)) return yyjson_mut_rawn(doc, str, strlen(str)); - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, - const char *str, - size_t len) { - if (yyjson_likely(doc && str)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; - val->uni.str = str; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, - const char *str) { - if (yyjson_likely(str)) return yyjson_mut_rawncpy(doc, str, strlen(str)); - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, - const char *str, - size_t len) { - if (yyjson_likely(doc && str)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); - if (yyjson_likely(val && new_str)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; - val->uni.str = new_str; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, - bool _val) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - _val = !!_val; - val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3); - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, - uint64_t num) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = num; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, - int64_t num) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = num; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, - int64_t num) { - return yyjson_mut_sint(doc, num); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, - double num) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.f64 = num; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, - const char *str) { - if (yyjson_likely(doc && str)) { - size_t len = strlen(str); - bool noesc = unsafe_yyjson_is_str_noesc(str, len); - yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | - (uint64_t)(YYJSON_TYPE_STR | sub); - val->uni.str = str; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, - const char *str, - size_t len) { - if (yyjson_likely(doc && str)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = str; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, - const char *str) { - if (yyjson_likely(doc && str)) { - size_t len = strlen(str); - bool noesc = unsafe_yyjson_is_str_noesc(str, len); - yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); - if (yyjson_likely(val && new_str)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | - (uint64_t)(YYJSON_TYPE_STR | sub); - val->uni.str = new_str; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, - const char *str, - size_t len) { - if (yyjson_likely(doc && str)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); - if (yyjson_likely(val && new_str)) { - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = new_str; - return val; - } - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Array API (Implementation) - *============================================================================*/ - -yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) { - return yyjson_mut_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, - size_t idx) { - if (yyjson_likely(idx < yyjson_mut_arr_size(arr))) { - yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; - while (idx-- > 0) val = val->next; - return val->next; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( - yyjson_mut_val *arr) { - if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { - return ((yyjson_mut_val *)arr->uni.ptr)->next; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( - yyjson_mut_val *arr) { - if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { - return ((yyjson_mut_val *)arr->uni.ptr); - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Array Iterator API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, - yyjson_mut_arr_iter *iter) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && iter)) { - iter->idx = 0; - iter->max = unsafe_yyjson_get_len(arr); - iter->cur = iter->max ? (yyjson_mut_val *)arr->uni.ptr : NULL; - iter->pre = NULL; - iter->arr = arr; - return true; - } - if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter)); - return false; -} - -yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( - yyjson_mut_val *arr) { - yyjson_mut_arr_iter iter; - yyjson_mut_arr_iter_init(arr, &iter); - return iter; -} - -yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter) { - return iter ? iter->idx < iter->max : false; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( - yyjson_mut_arr_iter *iter) { - if (iter && iter->idx < iter->max) { - yyjson_mut_val *val = iter->cur; - iter->pre = val; - iter->cur = val->next; - iter->idx++; - return iter->cur; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( - yyjson_mut_arr_iter *iter) { - if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { - yyjson_mut_val *prev = iter->pre; - yyjson_mut_val *cur = iter->cur; - yyjson_mut_val *next = cur->next; - if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev; - iter->idx--; - iter->max--; - unsafe_yyjson_set_len(iter->arr, iter->max); - prev->next = next; - iter->cur = next; - return cur; - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Array Creation API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE; - return val; - } - } - return NULL; -} - -#define yyjson_mut_arr_with_func(func) \ - if (yyjson_likely(doc && ((0 < count && count < \ - (~(size_t)0) / sizeof(yyjson_mut_val) && vals) || count == 0))) { \ - yyjson_mut_val *arr = unsafe_yyjson_mut_val(doc, 1 + count); \ - if (yyjson_likely(arr)) { \ - arr->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; \ - if (count > 0) { \ - size_t i; \ - for (i = 0; i < count; i++) { \ - yyjson_mut_val *val = arr + i + 1; \ - func \ - val->next = val + 1; \ - } \ - arr[count].next = arr + 1; \ - arr->uni.ptr = arr + count; \ - } \ - return arr; \ - } \ - } \ - return NULL - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( - yyjson_mut_doc *doc, const bool *vals, size_t count) { - yyjson_mut_arr_with_func({ - bool _val = !!vals[i]; - val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3); - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( - yyjson_mut_doc *doc, const int64_t *vals, size_t count) { - return yyjson_mut_arr_with_sint64(doc, vals, count); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( - yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { - return yyjson_mut_arr_with_uint64(doc, vals, count); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( - yyjson_mut_doc *doc, const double *vals, size_t count) { - return yyjson_mut_arr_with_double(doc, vals, count); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( - yyjson_mut_doc *doc, const int8_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = (int64_t)vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( - yyjson_mut_doc *doc, const int16_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( - yyjson_mut_doc *doc, const int32_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( - yyjson_mut_doc *doc, const int64_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( - yyjson_mut_doc *doc, const uint8_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( - yyjson_mut_doc *doc, const uint16_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( - yyjson_mut_doc *doc, const uint32_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( - yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( - yyjson_mut_doc *doc, const float *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.f64 = (double)vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( - yyjson_mut_doc *doc, const double *vals, size_t count) { - yyjson_mut_arr_with_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.f64 = vals[i]; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( - yyjson_mut_doc *doc, const char **vals, size_t count) { - yyjson_mut_arr_with_func({ - uint64_t len = (uint64_t)strlen(vals[i]); - val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = vals[i]; - if (yyjson_unlikely(!val->uni.str)) return NULL; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( - yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { - if (yyjson_unlikely(count > 0 && !lens)) return NULL; - yyjson_mut_arr_with_func({ - val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = vals[i]; - if (yyjson_unlikely(!val->uni.str)) return NULL; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( - yyjson_mut_doc *doc, const char **vals, size_t count) { - size_t len; - const char *str; - yyjson_mut_arr_with_func({ - str = vals[i]; - if (!str) return NULL; - len = strlen(str); - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); - if (yyjson_unlikely(!val->uni.str)) return NULL; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( - yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { - size_t len; - const char *str; - if (yyjson_unlikely(count > 0 && !lens)) return NULL; - yyjson_mut_arr_with_func({ - str = vals[i]; - len = lens[i]; - val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); - if (yyjson_unlikely(!val->uni.str)) return NULL; - }); -} - -#undef yyjson_mut_arr_with_func - - - -/*============================================================================== - * Mutable JSON Array Modification API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, - yyjson_mut_val *val, size_t idx) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_likely(idx <= len)) { - unsafe_yyjson_set_len(arr, len + 1); - if (len == 0) { - val->next = val; - arr->uni.ptr = val; - } else { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - if (idx == len) { - prev->next = val; - val->next = next; - arr->uni.ptr = val; - } else { - while (idx-- > 0) { - prev = next; - next = next->next; - } - prev->next = val; - val->next = next; - } - } - return true; - } - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, - yyjson_mut_val *val) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { - size_t len = unsafe_yyjson_get_len(arr); - unsafe_yyjson_set_len(arr, len + 1); - if (len == 0) { - val->next = val; - } else { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - prev->next = val; - val->next = next; - } - arr->uni.ptr = val; - return true; - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, - yyjson_mut_val *val) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { - size_t len = unsafe_yyjson_get_len(arr); - unsafe_yyjson_set_len(arr, len + 1); - if (len == 0) { - val->next = val; - arr->uni.ptr = val; - } else { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - prev->next = val; - val->next = next; - } - return true; - } - return false; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, - size_t idx, - yyjson_mut_val *val) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_likely(idx < len)) { - if (yyjson_likely(len > 1)) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - while (idx-- > 0) { - prev = next; - next = next->next; - } - prev->next = val; - val->next = next->next; - if ((void *)next == arr->uni.ptr) arr->uni.ptr = val; - return next; - } else { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - val->next = val; - arr->uni.ptr = val; - return prev; - } - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, - size_t idx) { - if (yyjson_likely(yyjson_mut_is_arr(arr))) { - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_likely(idx < len)) { - unsafe_yyjson_set_len(arr, len - 1); - if (yyjson_likely(len > 1)) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - while (idx-- > 0) { - prev = next; - next = next->next; - } - prev->next = next->next; - if ((void *)next == arr->uni.ptr) arr->uni.ptr = prev; - return next; - } else { - return ((yyjson_mut_val *)arr->uni.ptr); - } - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( - yyjson_mut_val *arr) { - if (yyjson_likely(yyjson_mut_is_arr(arr))) { - size_t len = unsafe_yyjson_get_len(arr); - if (len > 1) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - prev->next = next->next; - unsafe_yyjson_set_len(arr, len - 1); - return next; - } else if (len == 1) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - unsafe_yyjson_set_len(arr, 0); - return prev; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( - yyjson_mut_val *arr) { - if (yyjson_likely(yyjson_mut_is_arr(arr))) { - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_likely(len > 1)) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - yyjson_mut_val *next = prev->next; - unsafe_yyjson_set_len(arr, len - 1); - while (--len > 0) prev = prev->next; - prev->next = next; - next = (yyjson_mut_val *)arr->uni.ptr; - arr->uni.ptr = prev; - return next; - } else if (len == 1) { - yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); - unsafe_yyjson_set_len(arr, 0); - return prev; - } - } - return NULL; -} - -yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, - size_t _idx, size_t _len) { - if (yyjson_likely(yyjson_mut_is_arr(arr))) { - yyjson_mut_val *prev, *next; - bool tail_removed; - size_t len = unsafe_yyjson_get_len(arr); - if (yyjson_unlikely(_idx + _len > len)) return false; - if (yyjson_unlikely(_len == 0)) return true; - unsafe_yyjson_set_len(arr, len - _len); - if (yyjson_unlikely(len == _len)) return true; - tail_removed = (_idx + _len == len); - prev = ((yyjson_mut_val *)arr->uni.ptr); - while (_idx-- > 0) prev = prev->next; - next = prev->next; - while (_len-- > 0) next = next->next; - prev->next = next; - if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev; - return true; - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr) { - if (yyjson_likely(yyjson_mut_is_arr(arr))) { - unsafe_yyjson_set_len(arr, 0); - return true; - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, - size_t idx) { - if (yyjson_likely(yyjson_mut_is_arr(arr) && - unsafe_yyjson_get_len(arr) > idx)) { - yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; - while (idx-- > 0) val = val->next; - arr->uni.ptr = (void *)val; - return true; - } - return false; -} - - - -/*============================================================================== - * Mutable JSON Array Modification Convenience API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, - yyjson_mut_val *val) { - return yyjson_mut_arr_append(arr, val); -} - -yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, - yyjson_mut_val *arr) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_null(doc); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, - yyjson_mut_val *arr) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_true(doc); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, - yyjson_mut_val *arr) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_false(doc); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - bool _val) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_bool(doc, _val); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - uint64_t num) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_uint(doc, num); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - int64_t num) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_sint(doc, num); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - int64_t num) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_sint(doc, num); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - double num) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_real(doc, num); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_str(doc, str); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str, size_t len) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_strn(doc, str, len); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_strcpy(doc, str); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, - yyjson_mut_val *arr, - const char *str, size_t len) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_strncpy(doc, str, len); - return yyjson_mut_arr_append(arr, val); - } - return false; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, - yyjson_mut_val *arr) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_arr(doc); - return yyjson_mut_arr_append(arr, val) ? val : NULL; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, - yyjson_mut_val *arr) { - if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { - yyjson_mut_val *val = yyjson_mut_obj(doc); - return yyjson_mut_arr_append(arr, val) ? val : NULL; - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Object API (Implementation) - *============================================================================*/ - -yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) { - return yyjson_mut_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, - const char *key) { - return yyjson_mut_obj_getn(obj, key, key ? strlen(key) : 0); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, - const char *_key, - size_t key_len) { - size_t len = yyjson_mut_obj_size(obj); - if (yyjson_likely(len && _key)) { - yyjson_mut_val *key = ((yyjson_mut_val *)obj->uni.ptr)->next->next; - while (len-- > 0) { - if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key->next; - key = key->next->next; - } - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Object Iterator API (Implementation) - *============================================================================*/ - -yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, - yyjson_mut_obj_iter *iter) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && iter)) { - iter->idx = 0; - iter->max = unsafe_yyjson_get_len(obj); - iter->cur = iter->max ? (yyjson_mut_val *)obj->uni.ptr : NULL; - iter->pre = NULL; - iter->obj = obj; - return true; - } - if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter)); - return false; -} - -yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( - yyjson_mut_val *obj) { - yyjson_mut_obj_iter iter; - yyjson_mut_obj_iter_init(obj, &iter); - return iter; -} - -yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter) { - return iter ? iter->idx < iter->max : false; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( - yyjson_mut_obj_iter *iter) { - if (iter && iter->idx < iter->max) { - yyjson_mut_val *key = iter->cur; - iter->pre = key; - iter->cur = key->next->next; - iter->idx++; - return iter->cur; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( - yyjson_mut_val *key) { - return key ? key->next : NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( - yyjson_mut_obj_iter *iter) { - if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { - yyjson_mut_val *prev = iter->pre; - yyjson_mut_val *cur = iter->cur; - yyjson_mut_val *next = cur->next->next; - if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev; - iter->idx--; - iter->max--; - unsafe_yyjson_set_len(iter->obj, iter->max); - prev->next->next = next; - iter->cur = prev; - return cur->next; - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( - yyjson_mut_obj_iter *iter, const char *key) { - return yyjson_mut_obj_iter_getn(iter, key, key ? strlen(key) : 0); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( - yyjson_mut_obj_iter *iter, const char *key, size_t key_len) { - if (iter && key) { - size_t idx = 0; - size_t max = iter->max; - yyjson_mut_val *pre, *cur = iter->cur; - while (idx++ < max) { - pre = cur; - cur = cur->next->next; - if (unsafe_yyjson_equals_strn(cur, key, key_len)) { - iter->idx += idx; - if (iter->idx > max) iter->idx -= max + 1; - iter->pre = pre; - iter->cur = cur; - return cur->next; - } - } - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Object Creation API (Implementation) - *============================================================================*/ - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) { - if (yyjson_likely(doc)) { - yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); - if (yyjson_likely(val)) { - val->tag = YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE; - return val; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, - const char **keys, - const char **vals, - size_t count) { - if (yyjson_likely(doc && ((count > 0 && keys && vals) || (count == 0)))) { - yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); - if (yyjson_likely(obj)) { - obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; - if (count > 0) { - size_t i; - for (i = 0; i < count; i++) { - yyjson_mut_val *key = obj + (i * 2 + 1); - yyjson_mut_val *val = obj + (i * 2 + 2); - uint64_t key_len = (uint64_t)strlen(keys[i]); - uint64_t val_len = (uint64_t)strlen(vals[i]); - key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - key->uni.str = keys[i]; - val->uni.str = vals[i]; - key->next = val; - val->next = val + 1; - } - obj[count * 2].next = obj + 1; - obj->uni.ptr = obj + (count * 2 - 1); - } - return obj; - } - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, - const char **pairs, - size_t count) { - if (yyjson_likely(doc && ((count > 0 && pairs) || (count == 0)))) { - yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); - if (yyjson_likely(obj)) { - obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; - if (count > 0) { - size_t i; - for (i = 0; i < count; i++) { - yyjson_mut_val *key = obj + (i * 2 + 1); - yyjson_mut_val *val = obj + (i * 2 + 2); - const char *key_str = pairs[i * 2 + 0]; - const char *val_str = pairs[i * 2 + 1]; - uint64_t key_len = (uint64_t)strlen(key_str); - uint64_t val_len = (uint64_t)strlen(val_str); - key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - key->uni.str = key_str; - val->uni.str = val_str; - key->next = val; - val->next = val + 1; - } - obj[count * 2].next = obj + 1; - obj->uni.ptr = obj + (count * 2 - 1); - } - return obj; - } - } - return NULL; -} - - - -/*============================================================================== - * Mutable JSON Object Modification API (Implementation) - *============================================================================*/ - -yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val, - size_t len) { - if (yyjson_likely(len)) { - yyjson_mut_val *prev_val = ((yyjson_mut_val *)obj->uni.ptr)->next; - yyjson_mut_val *next_key = prev_val->next; - prev_val->next = key; - val->next = next_key; - } else { - val->next = key; - } - key->next = val; - obj->uni.ptr = (void *)key; - unsafe_yyjson_set_len(obj, len + 1); -} - -yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_obj_remove( - yyjson_mut_val *obj, const char *key, size_t key_len) { - size_t obj_len = unsafe_yyjson_get_len(obj); - if (obj_len) { - yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; - yyjson_mut_val *cur_key = pre_key->next->next; - yyjson_mut_val *removed_item = NULL; - size_t i; - for (i = 0; i < obj_len; i++) { - if (unsafe_yyjson_equals_strn(cur_key, key, key_len)) { - if (!removed_item) removed_item = cur_key->next; - cur_key = cur_key->next->next; - pre_key->next->next = cur_key; - if (i + 1 == obj_len) obj->uni.ptr = pre_key; - i--; - obj_len--; - } else { - pre_key = cur_key; - cur_key = cur_key->next->next; - } - } - unsafe_yyjson_set_len(obj, obj_len); - return removed_item; - } else { - return NULL; - } -} - -yyjson_api_inline bool unsafe_yyjson_mut_obj_replace(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val) { - size_t key_len = unsafe_yyjson_get_len(key); - size_t obj_len = unsafe_yyjson_get_len(obj); - if (obj_len) { - yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; - yyjson_mut_val *cur_key = pre_key->next->next; - size_t i; - for (i = 0; i < obj_len; i++) { - if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { - cur_key->next->tag = val->tag; - cur_key->next->uni.u64 = val->uni.u64; - return true; - } else { - cur_key = cur_key->next->next; - } - } - } - return false; -} - -yyjson_api_inline void unsafe_yyjson_mut_obj_rotate(yyjson_mut_val *obj, - size_t idx) { - yyjson_mut_val *key = (yyjson_mut_val *)obj->uni.ptr; - while (idx-- > 0) key = key->next->next; - obj->uni.ptr = (void *)key; -} - -yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && - yyjson_mut_is_str(key) && val)) { - unsafe_yyjson_mut_obj_add(obj, key, val, unsafe_yyjson_get_len(obj)); - return true; - } - return false; -} - -yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val) { - bool replaced = false; - size_t key_len; - yyjson_mut_obj_iter iter; - yyjson_mut_val *cur_key; - if (yyjson_unlikely(!yyjson_mut_is_obj(obj) || - !yyjson_mut_is_str(key))) return false; - key_len = unsafe_yyjson_get_len(key); - yyjson_mut_obj_iter_init(obj, &iter); - while ((cur_key = yyjson_mut_obj_iter_next(&iter)) != 0) { - if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { - if (!replaced && val) { - replaced = true; - val->next = cur_key->next->next; - cur_key->next = val; - } else { - yyjson_mut_obj_iter_remove(&iter); - } - } - } - if (!replaced && val) unsafe_yyjson_mut_obj_add(obj, key, val, iter.max); - return true; -} - -yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val, - size_t idx) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && - yyjson_mut_is_str(key) && val)) { - size_t len = unsafe_yyjson_get_len(obj); - if (yyjson_likely(len >= idx)) { - if (len > idx) { - void *ptr = obj->uni.ptr; - unsafe_yyjson_mut_obj_rotate(obj, idx); - unsafe_yyjson_mut_obj_add(obj, key, val, len); - obj->uni.ptr = ptr; - } else { - unsafe_yyjson_mut_obj_add(obj, key, val, len); - } - return true; - } - } - return false; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, - yyjson_mut_val *key) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key))) { - return unsafe_yyjson_mut_obj_remove(obj, key->uni.str, - unsafe_yyjson_get_len(key)); - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( - yyjson_mut_val *obj, const char *key) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { - size_t key_len = strlen(key); - return unsafe_yyjson_mut_obj_remove(obj, key, key_len); - } - return NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( - yyjson_mut_val *obj, const char *key, size_t key_len) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { - return unsafe_yyjson_mut_obj_remove(obj, key, key_len); - } - return NULL; -} - -yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj) { - if (yyjson_likely(yyjson_mut_is_obj(obj))) { - unsafe_yyjson_set_len(obj, 0); - return true; - } - return false; -} - -yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, - yyjson_mut_val *key, - yyjson_mut_val *val) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && - yyjson_mut_is_str(key) && val)) { - return unsafe_yyjson_mut_obj_replace(obj, key, val); - } - return false; -} - -yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, - size_t idx) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && - unsafe_yyjson_get_len(obj) > idx)) { - unsafe_yyjson_mut_obj_rotate(obj, idx); - return true; - } - return false; -} - - - -/*============================================================================== - * Mutable JSON Object Modification Convenience API (Implementation) - *============================================================================*/ - -#define yyjson_mut_obj_add_func(func) \ - if (yyjson_likely(doc && yyjson_mut_is_obj(obj) && _key)) { \ - yyjson_mut_val *key = unsafe_yyjson_mut_val(doc, 2); \ - if (yyjson_likely(key)) { \ - size_t len = unsafe_yyjson_get_len(obj); \ - yyjson_mut_val *val = key + 1; \ - size_t key_len = strlen(_key); \ - bool noesc = unsafe_yyjson_is_str_noesc(_key, key_len); \ - key->tag = YYJSON_TYPE_STR; \ - key->tag |= noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; \ - key->tag |= (uint64_t)strlen(_key) << YYJSON_TAG_BIT; \ - key->uni.str = _key; \ - func \ - unsafe_yyjson_mut_obj_add(obj, key, val, len); \ - return true; \ - } \ - } \ - return false - -yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - bool _val) { - yyjson_mut_obj_add_func({ - _val = !!_val; - val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)(_val) << 3); - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - uint64_t _val) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; - val->uni.u64 = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - int64_t _val) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - int64_t _val) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; - val->uni.i64 = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - double _val) { - yyjson_mut_obj_add_func({ - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - val->uni.f64 = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - const char *_val) { - if (yyjson_unlikely(!_val)) return false; - yyjson_mut_obj_add_func({ - size_t val_len = strlen(_val); - bool val_noesc = unsafe_yyjson_is_str_noesc(_val, val_len); - val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->tag |= val_noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; - val->uni.str = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - const char *_val, - size_t _len) { - if (yyjson_unlikely(!_val)) return false; - yyjson_mut_obj_add_func({ - val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = _val; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - const char *_val) { - if (yyjson_unlikely(!_val)) return false; - yyjson_mut_obj_add_func({ - size_t _len = strlen(_val); - val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); - if (yyjson_unlikely(!val->uni.str)) return false; - val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - }); -} - -yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - const char *_val, - size_t _len) { - if (yyjson_unlikely(!_val)) return false; - yyjson_mut_obj_add_func({ - val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); - if (yyjson_unlikely(!val->uni.str)) return false; - val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key) { - yyjson_mut_val *key = yyjson_mut_str(doc, _key); - yyjson_mut_val *val = yyjson_mut_arr(doc); - return yyjson_mut_obj_add(obj, key, val) ? val : NULL; -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key) { - yyjson_mut_val *key = yyjson_mut_str(doc, _key); - yyjson_mut_val *val = yyjson_mut_obj(doc); - return yyjson_mut_obj_add(obj, key, val) ? val : NULL; -} - -yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *_key, - yyjson_mut_val *_val) { - if (yyjson_unlikely(!_val)) return false; - yyjson_mut_obj_add_func({ - val = _val; - }); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj, - const char *key) { - return yyjson_mut_obj_remove_strn(obj, key, key ? strlen(key) : 0); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( - yyjson_mut_val *obj, const char *_key, size_t _len) { - if (yyjson_likely(yyjson_mut_is_obj(obj) && _key)) { - yyjson_mut_val *key; - yyjson_mut_obj_iter iter; - yyjson_mut_val *val_removed = NULL; - yyjson_mut_obj_iter_init(obj, &iter); - while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) { - if (unsafe_yyjson_equals_strn(key, _key, _len)) { - if (!val_removed) val_removed = key->next; - yyjson_mut_obj_iter_remove(&iter); - } - } - return val_removed; - } - return NULL; -} - -yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - const char *new_key) { - if (!key || !new_key) return false; - return yyjson_mut_obj_rename_keyn(doc, obj, key, strlen(key), - new_key, strlen(new_key)); -} - -yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, - yyjson_mut_val *obj, - const char *key, - size_t len, - const char *new_key, - size_t new_len) { - char *cpy_key = NULL; - yyjson_mut_val *old_key; - yyjson_mut_obj_iter iter; - if (!doc || !obj || !key || !new_key) return false; - yyjson_mut_obj_iter_init(obj, &iter); - while ((old_key = yyjson_mut_obj_iter_next(&iter))) { - if (unsafe_yyjson_equals_strn((void *)old_key, key, len)) { - if (!cpy_key) { - cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len); - if (!cpy_key) return false; - } - yyjson_mut_set_strn(old_key, cpy_key, new_len); - } - } - return cpy_key != NULL; -} - - - -/*============================================================================== - * JSON Pointer API (Implementation) - *============================================================================*/ - -#define yyjson_ptr_set_err(_code, _msg) do { \ - if (err) { \ - err->code = YYJSON_PTR_ERR_##_code; \ - err->msg = _msg; \ - err->pos = 0; \ - } \ -} while(false) - -/* require: val != NULL, *ptr == '/', len > 0 */ -yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, - const char *ptr, size_t len, - yyjson_ptr_err *err); - -/* require: val != NULL, *ptr == '/', len > 0 */ -yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/* require: val/new_val/doc != NULL, *ptr == '/', len > 0 */ -yyjson_api bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, bool insert_new, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -/* require: val/err != NULL, *ptr == '/', len > 0 */ -yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); - -/* require: val/err != NULL, *ptr == '/', len > 0 */ -yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err); - -yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, - const char *ptr) { - if (yyjson_unlikely(!ptr)) return NULL; - return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, - const char *ptr, size_t len) { - return yyjson_doc_ptr_getx(doc, ptr, len, NULL); -} - -yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, - const char *ptr, size_t len, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (yyjson_unlikely(!doc || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - return doc->root; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_ptr_getx(doc->root, ptr, len, err); -} - -yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, - const char *ptr) { - if (yyjson_unlikely(!ptr)) return NULL; - return yyjson_ptr_getn(val, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, - const char *ptr, size_t len) { - return yyjson_ptr_getx(val, ptr, len, NULL); -} - -yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, - const char *ptr, size_t len, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (yyjson_unlikely(!val || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - return val; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_ptr_getx(val, ptr, len, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, - const char *ptr) { - if (!ptr) return NULL; - return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, - const char *ptr, - size_t len) { - return yyjson_mut_doc_ptr_getx(doc, ptr, len, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!doc || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - return doc->root; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_getx(doc->root, ptr, len, ctx, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, - const char *ptr) { - if (!ptr) return NULL; - return yyjson_mut_ptr_getn(val, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, - const char *ptr, - size_t len) { - return yyjson_mut_ptr_getx(val, ptr, len, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!val || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - return val; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, - const char *ptr, - yyjson_mut_val *new_val) { - if (yyjson_unlikely(!ptr)) return false; - return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, - const char *ptr, - size_t len, - yyjson_mut_val *new_val) { - return yyjson_mut_doc_ptr_addx(doc, ptr, len, new_val, true, NULL, NULL); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!doc || !ptr || !new_val)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return false; - } - if (yyjson_unlikely(len == 0)) { - if (doc->root) { - yyjson_ptr_set_err(SET_ROOT, "cannot set document's root"); - return false; - } else { - doc->root = new_val; - return true; - } - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return false; - } - if (yyjson_unlikely(!doc->root && !create_parent)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return false; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_mut_val *root = yyjson_mut_obj(doc); - if (yyjson_unlikely(!root)) { - yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); - return false; - } - if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, - create_parent, true, ctx, err)) { - doc->root = root; - return true; - } - return false; - } - return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, - create_parent, true, ctx, err); -} - -yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, - const char *ptr, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc) { - if (yyjson_unlikely(!ptr)) return false; - return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc); -} - -yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc) { - return yyjson_mut_ptr_addx(val, ptr, len, new_val, doc, true, NULL, NULL); -} - -yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return false; - } - if (yyjson_unlikely(len == 0)) { - yyjson_ptr_set_err(SET_ROOT, "cannot set root"); - return false; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return false; - } - return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, - doc, create_parent, true, ctx, err); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, - const char *ptr, - yyjson_mut_val *new_val) { - if (yyjson_unlikely(!ptr)) return false; - return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val) { - return yyjson_mut_doc_ptr_setx(doc, ptr, len, new_val, true, NULL, NULL); -} - -yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!doc || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return false; - } - if (yyjson_unlikely(len == 0)) { - if (ctx) ctx->old = doc->root; - doc->root = new_val; - return true; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return false; - } - if (!new_val) { - if (!doc->root) { - yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); - return false; - } - return !!unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); - } - if (yyjson_unlikely(!doc->root && !create_parent)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return false; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_mut_val *root = yyjson_mut_obj(doc); - if (yyjson_unlikely(!root)) { - yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); - return false; - } - if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, - create_parent, false, ctx, err)) { - doc->root = root; - return true; - } - return false; - } - return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, - create_parent, false, ctx, err); -} - -yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, - const char *ptr, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc) { - if (yyjson_unlikely(!ptr)) return false; - return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc); -} - -yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc) { - return yyjson_mut_ptr_setx(val, ptr, len, new_val, doc, true, NULL, NULL); -} - -yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, - const char *ptr, size_t len, - yyjson_mut_val *new_val, - yyjson_mut_doc *doc, - bool create_parent, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!val || !ptr || !doc)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return false; - } - if (yyjson_unlikely(len == 0)) { - yyjson_ptr_set_err(SET_ROOT, "cannot set root"); - return false; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return false; - } - if (!new_val) { - return !!unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); - } - return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, doc, - create_parent, false, ctx, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( - yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { - if (!ptr) return NULL; - return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( - yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { - return yyjson_mut_doc_ptr_replacex(doc, ptr, len, new_val, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( - yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!doc || !ptr || !new_val)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - yyjson_mut_val *root = doc->root; - if (yyjson_unlikely(!root)) { - yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); - return NULL; - } - if (ctx) ctx->old = root; - doc->root = new_val; - return root; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return NULL; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_replacex(doc->root, ptr, len, new_val, - ctx, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( - yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val) { - if (!ptr) return NULL; - return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val) { - return yyjson_mut_ptr_replacex(val, ptr, len, new_val, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!val || !ptr || !new_val)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - yyjson_ptr_set_err(SET_ROOT, "cannot set root"); - return NULL; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_replacex(val, ptr, len, new_val, ctx, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( - yyjson_mut_doc *doc, const char *ptr) { - if (!ptr) return NULL; - return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( - yyjson_mut_doc *doc, const char *ptr, size_t len) { - return yyjson_mut_doc_ptr_removex(doc, ptr, len, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( - yyjson_mut_doc *doc, const char *ptr, size_t len, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!doc || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(!doc->root)) { - yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - yyjson_mut_val *root = doc->root; - if (ctx) ctx->old = root; - doc->root = NULL; - return root; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, - const char *ptr) { - if (!ptr) return NULL; - return yyjson_mut_ptr_removen(val, ptr, strlen(ptr)); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, - const char *ptr, - size_t len) { - return yyjson_mut_ptr_removex(val, ptr, len, NULL, NULL); -} - -yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, - const char *ptr, - size_t len, - yyjson_ptr_ctx *ctx, - yyjson_ptr_err *err) { - yyjson_ptr_set_err(NONE, NULL); - if (ctx) memset(ctx, 0, sizeof(*ctx)); - - if (yyjson_unlikely(!val || !ptr)) { - yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); - return NULL; - } - if (yyjson_unlikely(len == 0)) { - yyjson_ptr_set_err(SET_ROOT, "cannot set root"); - return NULL; - } - if (yyjson_unlikely(*ptr != '/')) { - yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); - return NULL; - } - return unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); -} - -yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, - yyjson_mut_val *key, - yyjson_mut_val *val) { - yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; - if (!ctx || !ctx->ctn || !val) return false; - ctn = ctx->ctn; - - if (yyjson_mut_is_obj(ctn)) { - if (!key) return false; - key->next = val; - pre_key = ctx->pre; - if (unsafe_yyjson_get_len(ctn) == 0) { - val->next = key; - ctn->uni.ptr = key; - ctx->pre = key; - } else if (!pre_key) { - pre_key = (yyjson_mut_val *)ctn->uni.ptr; - pre_val = pre_key->next; - val->next = pre_val->next; - pre_val->next = key; - ctn->uni.ptr = key; - ctx->pre = pre_key; - } else { - cur_key = pre_key->next->next; - cur_val = cur_key->next; - val->next = cur_val->next; - cur_val->next = key; - if (ctn->uni.ptr == cur_key) ctn->uni.ptr = key; - ctx->pre = cur_key; - } - } else { - pre_val = ctx->pre; - if (unsafe_yyjson_get_len(ctn) == 0) { - val->next = val; - ctn->uni.ptr = val; - ctx->pre = val; - } else if (!pre_val) { - pre_val = (yyjson_mut_val *)ctn->uni.ptr; - val->next = pre_val->next; - pre_val->next = val; - ctn->uni.ptr = val; - ctx->pre = pre_val; - } else { - cur_val = pre_val->next; - val->next = cur_val->next; - cur_val->next = val; - if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; - ctx->pre = cur_val; - } - } - unsafe_yyjson_inc_len(ctn); - return true; -} - -yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, - yyjson_mut_val *val) { - yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val; - if (!ctx || !ctx->ctn || !ctx->pre || !val) return false; - ctn = ctx->ctn; - if (yyjson_mut_is_obj(ctn)) { - pre_key = ctx->pre; - pre_val = pre_key->next; - cur_key = pre_val->next; - cur_val = cur_key->next; - /* replace current value */ - cur_key->next = val; - val->next = cur_val->next; - ctx->old = cur_val; - } else { - pre_val = ctx->pre; - cur_val = pre_val->next; - /* replace current value */ - if (pre_val != cur_val) { - val->next = cur_val->next; - pre_val->next = val; - if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; - } else { - val->next = val; - ctn->uni.ptr = val; - ctx->pre = val; - } - ctx->old = cur_val; - } - return true; -} - -yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { - yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; - size_t len; - if (!ctx || !ctx->ctn || !ctx->pre) return false; - ctn = ctx->ctn; - if (yyjson_mut_is_obj(ctn)) { - pre_key = ctx->pre; - pre_val = pre_key->next; - cur_key = pre_val->next; - cur_val = cur_key->next; - /* remove current key-value */ - pre_val->next = cur_val->next; - if (ctn->uni.ptr == cur_key) ctn->uni.ptr = pre_key; - ctx->pre = NULL; - ctx->old = cur_val; - } else { - pre_val = ctx->pre; - cur_val = pre_val->next; - /* remove current key-value */ - pre_val->next = cur_val->next; - if (ctn->uni.ptr == cur_val) ctn->uni.ptr = pre_val; - ctx->pre = NULL; - ctx->old = cur_val; - } - len = unsafe_yyjson_get_len(ctn) - 1; - if (len == 0) ctn->uni.ptr = NULL; - unsafe_yyjson_set_len(ctn, len); - return true; -} - -#undef yyjson_ptr_set_err - - - -/*============================================================================== - * JSON Value at Pointer API (Implementation) - *============================================================================*/ - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is type bool. - Returns true if value at `ptr` exists and is the correct type, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_bool( - yyjson_val *root, const char *ptr, bool *value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && yyjson_is_bool(val)) { - *value = unsafe_yyjson_get_bool(val); - return true; - } else { - return false; - } -} - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer - that fits in `uint64_t`. Returns true if successful, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_uint( - yyjson_val *root, const char *ptr, uint64_t *value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && val) { - uint64_t ret = val->uni.u64; - if (unsafe_yyjson_is_uint(val) || - (unsafe_yyjson_is_sint(val) && !(ret >> 63))) { - *value = ret; - return true; - } - } - return false; -} - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer - that fits in `int64_t`. Returns true if successful, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_sint( - yyjson_val *root, const char *ptr, int64_t *value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && val) { - int64_t ret = val->uni.i64; - if (unsafe_yyjson_is_sint(val) || - (unsafe_yyjson_is_uint(val) && ret >= 0)) { - *value = ret; - return true; - } - } - return false; -} - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is type real. - Returns true if value at `ptr` exists and is the correct type, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_real( - yyjson_val *root, const char *ptr, double *value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && yyjson_is_real(val)) { - *value = unsafe_yyjson_get_real(val); - return true; - } else { - return false; - } -} - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint, - uint or real. - Returns true if value at `ptr` exists and is the correct type, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_num( - yyjson_val *root, const char *ptr, double *value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && yyjson_is_num(val)) { - *value = unsafe_yyjson_get_num(val); - return true; - } else { - return false; - } -} - -/** - Set provided `value` if the JSON Pointer (RFC 6901) exists and is type string. - Returns true if value at `ptr` exists and is the correct type, otherwise false. - */ -yyjson_api_inline bool yyjson_ptr_get_str( - yyjson_val *root, const char *ptr, const char **value) { - yyjson_val *val = yyjson_ptr_get(root, ptr); - if (value && yyjson_is_str(val)) { - *value = unsafe_yyjson_get_str(val); - return true; - } else { - return false; - } -} - - - -/*============================================================================== - * Deprecated - *============================================================================*/ - -/** @deprecated renamed to `yyjson_doc_ptr_get` */ -yyjson_deprecated("renamed to yyjson_doc_ptr_get") -yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc, - const char *ptr) { - return yyjson_doc_ptr_get(doc, ptr); -} - -/** @deprecated renamed to `yyjson_doc_ptr_getn` */ -yyjson_deprecated("renamed to yyjson_doc_ptr_getn") -yyjson_api_inline yyjson_val *yyjson_doc_get_pointern(yyjson_doc *doc, - const char *ptr, - size_t len) { - return yyjson_doc_ptr_getn(doc, ptr, len); -} - -/** @deprecated renamed to `yyjson_mut_doc_ptr_get` */ -yyjson_deprecated("renamed to yyjson_mut_doc_ptr_get") -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer( - yyjson_mut_doc *doc, const char *ptr) { - return yyjson_mut_doc_ptr_get(doc, ptr); -} - -/** @deprecated renamed to `yyjson_mut_doc_ptr_getn` */ -yyjson_deprecated("renamed to yyjson_mut_doc_ptr_getn") -yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointern( - yyjson_mut_doc *doc, const char *ptr, size_t len) { - return yyjson_mut_doc_ptr_getn(doc, ptr, len); -} - -/** @deprecated renamed to `yyjson_ptr_get` */ -yyjson_deprecated("renamed to yyjson_ptr_get") -yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val, - const char *ptr) { - return yyjson_ptr_get(val, ptr); -} - -/** @deprecated renamed to `yyjson_ptr_getn` */ -yyjson_deprecated("renamed to yyjson_ptr_getn") -yyjson_api_inline yyjson_val *yyjson_get_pointern(yyjson_val *val, - const char *ptr, - size_t len) { - return yyjson_ptr_getn(val, ptr, len); -} - -/** @deprecated renamed to `yyjson_mut_ptr_get` */ -yyjson_deprecated("renamed to yyjson_mut_ptr_get") -yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val, - const char *ptr) { - return yyjson_mut_ptr_get(val, ptr); -} - -/** @deprecated renamed to `yyjson_mut_ptr_getn` */ -yyjson_deprecated("renamed to yyjson_mut_ptr_getn") -yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointern(yyjson_mut_val *val, - const char *ptr, - size_t len) { - return yyjson_mut_ptr_getn(val, ptr, len); -} - -/** @deprecated renamed to `yyjson_mut_ptr_getn` */ -yyjson_deprecated("renamed to unsafe_yyjson_ptr_getn") -yyjson_api_inline yyjson_val *unsafe_yyjson_get_pointer(yyjson_val *val, - const char *ptr, - size_t len) { - yyjson_ptr_err err; - return unsafe_yyjson_ptr_getx(val, ptr, len, &err); -} - -/** @deprecated renamed to `unsafe_yyjson_mut_ptr_getx` */ -yyjson_deprecated("renamed to unsafe_yyjson_mut_ptr_getx") -yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( - yyjson_mut_val *val, const char *ptr, size_t len) { - yyjson_ptr_err err; - return unsafe_yyjson_mut_ptr_getx(val, ptr, len, NULL, &err); -} - - - -/*============================================================================== - * Compiler Hint End - *============================================================================*/ - -#if defined(__clang__) -# pragma clang diagnostic pop -#elif defined(__GNUC__) -# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -# pragma GCC diagnostic pop -# endif -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif /* warning suppress end */ - -#ifdef __cplusplus -} -#endif /* extern "C" end */ - -#endif /* YYJSON_H */ diff --git a/vendor/orjson/integration/client b/vendor/orjson/integration/client deleted file mode 100755 index f868476920..0000000000 --- a/vendor/orjson/integration/client +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python3 - -import asyncio -import sys -import time - -import httpx - -port = sys.argv[1] -url = f"http://127.0.0.1:{port}" - -timeout = httpx.Timeout(5.0) -client = httpx.AsyncClient(timeout=timeout) - -stop_time = time.time() + 5 - -TEST_MESSAGE = "http test running..." - - -async def main(): - sys.stdout.write(TEST_MESSAGE) - sys.stdout.flush() - count = 0 - while time.time() < stop_time: - res = await client.get(url) - count += 1 - sys.stdout.write(f"\r{TEST_MESSAGE} ok, {count} requests made\n") - - -loop = asyncio.new_event_loop() -asyncio.set_event_loop(loop) -asyncio.run(main()) -loop.close() diff --git a/vendor/orjson/integration/http b/vendor/orjson/integration/http deleted file mode 100755 index 574550093f..0000000000 --- a/vendor/orjson/integration/http +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -set -e - -_dir="$(dirname "${BASH_SOURCE[0]}")" - -PYTHONPATH=${_dir} gunicorn --preload --bind localhost:8001 --workers 2 "$@" wsgi:app diff --git a/vendor/orjson/integration/init b/vendor/orjson/integration/init deleted file mode 100755 index f00c11a36c..0000000000 --- a/vendor/orjson/integration/init +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import multiprocessing.pool -import sys - -import orjson - -NUM_PROC = 16 - -TEST_MESSAGE = "parallel import of orjson running..." - - -class Custom: - pass - - -def default(_): - return None - - -def func(_): - orjson.dumps(Custom(), option=orjson.OPT_SERIALIZE_NUMPY, default=default) - orjson.loads(b'{"a":1,"b":2,"c":3}') - - -def main(): - sys.stdout.write(TEST_MESSAGE) - sys.stdout.flush() - with multiprocessing.pool.ThreadPool(processes=NUM_PROC) as pool: - pool.map(func, (i for i in range(0, NUM_PROC))) - sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") - sys.stdout.flush() - - -if __name__ == "__main__": - main() diff --git a/vendor/orjson/integration/requirements.txt b/vendor/orjson/integration/requirements.txt deleted file mode 100644 index e5b290de29..0000000000 --- a/vendor/orjson/integration/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -flask;sys_platform!="win" -gunicorn;sys_platform!="win" -httpx==0.24.1;sys_platform!="win" diff --git a/vendor/orjson/integration/run b/vendor/orjson/integration/run deleted file mode 100755 index 477f707609..0000000000 --- a/vendor/orjson/integration/run +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -_dir="$(dirname "${BASH_SOURCE[0]}")" - -to_run="${@:-thread http init}" - -export PYTHONMALLOC="debug" - -if [[ $to_run == *"thread"* ]]; then - "${_dir}"/thread -fi - -if [[ $to_run == *"http"* ]]; then - "${_dir}"/http --daemon - sleep 2 - "${_dir}"/client 8001 - set +e - pkill -f 'wsgi:app' # pkill not present on all CI envs - set -e -fi - -if [[ $to_run == *"typestubs"* ]]; then - python "${_dir}"/typestubs.py - mypy "${_dir}"/typestubs.py -fi - -if [[ $to_run == *"init"* ]]; then - "${_dir}"/init -fi diff --git a/vendor/orjson/integration/thread b/vendor/orjson/integration/thread deleted file mode 100755 index be67a972bb..0000000000 --- a/vendor/orjson/integration/thread +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 - -import sys -import traceback -from concurrent.futures import ThreadPoolExecutor -from operator import itemgetter -from threading import get_ident - -import orjson - -DATA = sorted( - [ - { - "id": i, - "name": "90as90ji0123ioj2390as90as90", - "body": "哈哈89asu89as😊89as9as90jas-😋0apjzxiojzx89hq23n", - "score": 901290129.1, - "bool": True, - "int": 9832, - "none": None, - } - for i in range(10) - ], - key=itemgetter("id"), -) - - -STATUS = 0 - -TEST_MESSAGE = "thread test running..." - -sys.stdout.write(TEST_MESSAGE) -sys.stdout.flush() - - -def test_func(n): - try: - assert sorted(orjson.loads(orjson.dumps(DATA)), key=itemgetter("id")) == DATA - except Exception: - traceback.print_exc() - print("thread %s: %s dumps, loads ERROR" % (get_ident(), n)) - - -with ThreadPoolExecutor(max_workers=4) as executor: - executor.map(test_func, range(50000), chunksize=1000) - executor.shutdown(wait=True) - - -if STATUS == 0: - sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") -else: - sys.stdout.write(f"\r{TEST_MESSAGE} error\n") - - -sys.exit(STATUS) diff --git a/vendor/orjson/integration/typestubs.py b/vendor/orjson/integration/typestubs.py deleted file mode 100644 index 24f423feec..0000000000 --- a/vendor/orjson/integration/typestubs.py +++ /dev/null @@ -1,7 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - -orjson.JSONDecodeError(msg="the_msg", doc="the_doc", pos=1) - -orjson.dumps(orjson.Fragment(b"{}")) diff --git a/vendor/orjson/integration/wsgi.py b/vendor/orjson/integration/wsgi.py deleted file mode 100644 index b5d4d4c43f..0000000000 --- a/vendor/orjson/integration/wsgi.py +++ /dev/null @@ -1,29 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from datetime import datetime, timezone -from uuid import uuid4 - -from flask import Flask - -import orjson - -app = Flask(__name__) - -NOW = datetime.now(timezone.utc) - - -@app.route("/") -def root(): - data = { - "uuid": uuid4(), - "updated_at": NOW, - "data": [1, 2.2, None, True, False, orjson.Fragment(b"{}")], - } - payload = orjson.dumps( - data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_OMIT_MICROSECONDS - ) - return app.response_class( - response=payload, - status=200, - mimetype="application/json; charset=utf-8", - ) diff --git a/vendor/orjson/pyproject.toml b/vendor/orjson/pyproject.toml deleted file mode 100644 index f72f842ccc..0000000000 --- a/vendor/orjson/pyproject.toml +++ /dev/null @@ -1,65 +0,0 @@ -[project] -name = "orjson" -repository = "https://github.com/ijl/orjson" -requires-python = ">=3.8" -classifiers = [ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: Apache Software License", - "License :: OSI Approved :: MIT License", - "Operating System :: MacOS", - "Operating System :: Microsoft :: Windows", - "Operating System :: POSIX :: Linux", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: 3.13", - "Programming Language :: Python :: 3.14", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python", - "Programming Language :: Rust", - "Typing :: Typed", -] - -[project.urls] -Documentation = "https://github.com/ijl/orjson" -Changelog = "https://github.com/ijl/orjson/blob/master/CHANGELOG.md" - -[build-system] -build-backend = "maturin" -requires = ["maturin>=1,<2"] - -[tool.maturin] -python-source = "pysrc" -include = [ - { format = "sdist", path = ".cargo/*" }, - { format = "sdist", path = "build.rs" }, - { format = "sdist", path = "Cargo.lock" }, - { format = "sdist", path = "include/**/*" }, -] - -[tool.ruff] -line-length = 88 -target-version = "py38" - -[tool.ruff.lint] -select = [ - "I", -] -ignore = [ - "E501", # line too long - "F601", # Dictionary key literal ... repeated -] - -[tool.ruff.lint.isort] -known-first-party = ["orjson"] - -[tool.mypy] -python_version = "3.8" - -[[tool.mypy.overrides]] -module = ["dateutil", "pytz", "simplejson", "ujson"] -ignore_missing_imports = true diff --git a/vendor/orjson/pysrc/orjson/__init__.py b/vendor/orjson/pysrc/orjson/__init__.py deleted file mode 100644 index 892fd682bc..0000000000 --- a/vendor/orjson/pysrc/orjson/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -from .orjson import * -from .orjson import __version__ - -__all__ = ( - "__version__", - "dumps", - "Fragment", - "JSONDecodeError", - "JSONEncodeError", - "loads", - "OPT_APPEND_NEWLINE", - "OPT_INDENT_2", - "OPT_NAIVE_UTC", - "OPT_NON_STR_KEYS", - "OPT_OMIT_MICROSECONDS", - "OPT_PASSTHROUGH_DATACLASS", - "OPT_PASSTHROUGH_DATETIME", - "OPT_PASSTHROUGH_SUBCLASS", - "OPT_SERIALIZE_DATACLASS", - "OPT_SERIALIZE_NUMPY", - "OPT_SERIALIZE_UUID", - "OPT_SORT_KEYS", - "OPT_STRICT_INTEGER", - "OPT_UTC_Z", -) diff --git a/vendor/orjson/pysrc/orjson/__init__.pyi b/vendor/orjson/pysrc/orjson/__init__.pyi deleted file mode 100644 index d2b84db77c..0000000000 --- a/vendor/orjson/pysrc/orjson/__init__.pyi +++ /dev/null @@ -1,32 +0,0 @@ -import json -from typing import Any, Callable, Optional, Union - -__version__: str - -def dumps( - __obj: Any, - default: Optional[Callable[[Any], Any]] = ..., - option: Optional[int] = ..., -) -> bytes: ... -def loads(__obj: Union[bytes, bytearray, memoryview, str]) -> Any: ... - -class JSONDecodeError(json.JSONDecodeError): ... -class JSONEncodeError(TypeError): ... - -class Fragment(tuple): - contents: Union[bytes, str] - -OPT_APPEND_NEWLINE: int -OPT_INDENT_2: int -OPT_NAIVE_UTC: int -OPT_NON_STR_KEYS: int -OPT_OMIT_MICROSECONDS: int -OPT_PASSTHROUGH_DATACLASS: int -OPT_PASSTHROUGH_DATETIME: int -OPT_PASSTHROUGH_SUBCLASS: int -OPT_SERIALIZE_DATACLASS: int -OPT_SERIALIZE_NUMPY: int -OPT_SERIALIZE_UUID: int -OPT_SORT_KEYS: int -OPT_STRICT_INTEGER: int -OPT_UTC_Z: int diff --git a/vendor/orjson/pysrc/orjson/py.typed b/vendor/orjson/pysrc/orjson/py.typed deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/orjson/requirements.txt b/vendor/orjson/requirements.txt deleted file mode 100644 index 9981cc8c35..0000000000 --- a/vendor/orjson/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ --r bench/requirements.txt --r integration/requirements.txt --r test/requirements.txt -maturin -mypy==1.13.0 -ruff==0.7.1 diff --git a/vendor/orjson/script/develop b/vendor/orjson/script/develop deleted file mode 100755 index b92a22e91a..0000000000 --- a/vendor/orjson/script/develop +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh -e - -rm -f target/wheels/* - -export UNSAFE_PYO3_BUILD_FREE_THREADED=1 -export UNSAFE_PYO3_SKIP_VERSION_CHECK=1 - -export CC="${CC:-clang}" -export LD="${LD:-lld}" -export TARGET="${TARGET:-x86_64-unknown-linux-gnu}" -export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}" - -echo "CC: ${CC}, LD: ${LD}, LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}" - -export CFLAGS="-Os -fstrict-aliasing -fno-plt -flto=full -emit-llvm" -export LDFLAGS="-fuse-ld=${LD} -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow" -export RUSTFLAGS="-C linker=${CC} -C link-arg=-fuse-ld=${LD} -C linker-plugin-lto -C lto=fat -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=8" - -rm -f ${CARGO_TARGET_DIR}/wheels/*.whl - -maturin build --target="${TARGET}" "$@" - -uv pip install --link-mode=copy ${CARGO_TARGET_DIR}/wheels/*.whl diff --git a/vendor/orjson/script/generate-yyjson b/vendor/orjson/script/generate-yyjson deleted file mode 100755 index 826c4a8948..0000000000 --- a/vendor/orjson/script/generate-yyjson +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -_repo="$(dirname "$(dirname "${BASH_SOURCE[0]}")")" - -bindgen \ - "${_repo}/include/yyjson/yyjson.h" \ - --size_t-is-usize \ - --disable-header-comment \ - --no-derive-copy \ - --no-derive-debug \ - --no-doc-comments \ - --no-layout-tests \ - --allowlist-function=yyjson_alc_pool_init \ - --allowlist-function=yyjson_doc_free \ - --allowlist-function=yyjson_read_opts \ - --allowlist-type=yyjson_alc \ - --allowlist-type=yyjson_doc \ - --allowlist-type=yyjson_read_code \ - --allowlist-type=yyjson_read_err \ - --allowlist-type=yyjson_val \ - --allowlist-var=YYJSON_READ_NOFLAG \ - --allowlist-var=YYJSON_READ_SUCCESS \ - > "${_repo}/src/ffi/yyjson.rs" diff --git a/vendor/orjson/script/graph b/vendor/orjson/script/graph deleted file mode 100755 index 7cf47d0d31..0000000000 --- a/vendor/orjson/script/graph +++ /dev/null @@ -1,152 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import collections -import io -import math -import os - -import pandas as pd -import seaborn -from matplotlib import pyplot -from tabulate import tabulate - -import orjson - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - - -def aggregate(): - benchmarks_dir = os.path.join(".benchmarks", os.listdir(".benchmarks")[0]) - res = collections.defaultdict(dict) - for filename in os.listdir(benchmarks_dir): - with open(os.path.join(benchmarks_dir, filename), "r") as fileh: - data = orjson.loads(fileh.read()) - - for each in data["benchmarks"]: - res[each["group"]][each["extra_info"]["lib"]] = { - "data": [val * 1000 for val in each["stats"]["data"]], - "median": each["stats"]["median"] * 1000, - "ops": each["stats"]["ops"], - "correct": each["extra_info"]["correct"], - } - return res - - -def tab(obj): - buf = io.StringIO() - headers = ( - "Library", - "Median latency (milliseconds)", - "Operations per second", - "Relative (latency)", - ) - - seaborn.set(rc={"figure.facecolor": (0, 0, 0, 0)}) - seaborn.set_style("darkgrid") - - barplot_data = [] - for group, val in sorted(obj.items(), reverse=True): - buf.write("\n" + "#### " + group + "\n\n") - table = [] - for lib in LIBRARIES: - correct = val[lib]["correct"] - table.append( - [ - lib, - val[lib]["median"] if correct else None, - int(val[lib]["ops"]) if correct else None, - 0, - ] - ) - barplot_data.append( - { - "operation": "deserialization" - if "deserialization" in group - else "serialization", - "group": group.strip("serialization") - .strip("deserialization") - .strip(), - "library": lib, - "latency": val[lib]["median"], - "operations": int(val[lib]["ops"]) if correct else None, - } - ) - - orjson_baseline = table[0][1] - for each in table: - each[3] = ( - "%.1f" % (each[1] / orjson_baseline) - if isinstance(each[1], float) - else None - ) - if group.startswith("github"): - each[1] = "%.2f" % each[1] if isinstance(each[1], float) else None - else: - each[1] = "%.1f" % each[1] if isinstance(each[1], float) else None - - buf.write(tabulate(table, headers, tablefmt="github") + "\n") - - for operation in ("deserialization", "serialization"): - per_op_data = list( - (each for each in barplot_data if each["operation"] == operation) - ) - if not per_op_data: - continue - - max_y = 0 - - json_baseline = {} - for each in per_op_data: - if each["group"] == "witter.json": - each["group"] = "twitter.json" - if each["library"] == "json": - json_baseline[each["group"]] = each["operations"] - - for each in per_op_data: - relative = each["operations"] / json_baseline[each["group"]] - each["relative"] = relative - max_y = max(max_y, relative) - - p = pd.DataFrame.from_dict(per_op_data) - p.groupby("group") - - graph = seaborn.barplot( - p, - x="group", - y="relative", - orient="x", - hue="library", - errorbar="sd", - legend="brief", - ) - graph.set_xlabel("Document") - graph.set_ylabel("Operations/second relative to stdlib json") - - pyplot.title(operation) - - # ensure Y range - max_y = int(math.ceil(max_y)) - if max_y > 10 and max_y % 2 > 0: - max_y = max_y + 1 - pyplot.gca().set_yticks( - list( - {1, max_y}.union( - set(int(y) for y in pyplot.gca().get_yticks() if int(y) <= max_y) - ) - ) - ) - - # print Y as percent - pyplot.gca().set_yticklabels([f"{x}x" for x in pyplot.gca().get_yticks()]) - - # reference for stdlib - pyplot.axhline(y=1, color="#999", linestyle="dashed") - - pyplot.savefig(fname=f"doc/{operation}", dpi=300) - pyplot.close() - - print(buf.getvalue()) - - -tab(aggregate()) diff --git a/vendor/orjson/script/install-fedora b/vendor/orjson/script/install-fedora deleted file mode 100755 index 19a5a45536..0000000000 --- a/vendor/orjson/script/install-fedora +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -# export PYTHON=python3.11 -# export PYTHON_PACKAGE=python3.11 -# export RUST_TOOLCHAIN=nightly-2024-09-25 -# export TARGET=x86_64-unknown-linux-gnu -# export VENV=.venv -# export CARGO_TARGET_DIR=/tmp/orjson - -export VENV="${VENV:-.venv}" -export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}" - -rm /etc/yum.repos.d/fedora-cisco-openh264.repo || true - -dnf install --setopt=install_weak_deps=false -y rustup clang lld "${PYTHON_PACKAGE}" - -rustup-init --default-toolchain "${RUST_TOOLCHAIN}-${TARGET}" --profile minimal --component rust-src -y -source "${HOME}/.cargo/env" - -mkdir -p .cargo -cp ci/config.toml .cargo/config.toml - -cargo fetch --target="${TARGET}" & - -curl -LsSf https://astral.sh/uv/install.sh | sh -rm -rf "${VENV}" -uv venv --python "${PYTHON}" "${VENV}" -source "${VENV}/bin/activate" - -uv pip install --upgrade "maturin>=1,<2" -r test/requirements.txt -r integration/requirements.txt diff --git a/vendor/orjson/script/lint b/vendor/orjson/script/lint deleted file mode 100755 index 22f5742e71..0000000000 --- a/vendor/orjson/script/lint +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -to_lint="./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py script/pydataclass script/pymem -script/pysort script/pynumpy script/pynonstr script/pycorrectness script/graph integration/init -integration/wsgi.py integration/typestubs.py integration/thread" - -ruff check ${to_lint} --fix -ruff format ${to_lint} -mypy --ignore-missing-imports --check-untyped-defs ./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py diff --git a/vendor/orjson/script/profile b/vendor/orjson/script/profile deleted file mode 100755 index ea94fd4f41..0000000000 --- a/vendor/orjson/script/profile +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -e - -# usage: ./profile data/citm_catalog.json.xz loads - -perf record -g --delay 250 ./bench/run_func "$@" -perf report --percent-limit 0.1 diff --git a/vendor/orjson/script/pybench b/vendor/orjson/script/pybench deleted file mode 100755 index 2fd5d9ac7a..0000000000 --- a/vendor/orjson/script/pybench +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -pytest \ - --verbose \ - --benchmark-min-time=1 \ - --benchmark-max-time=5 \ - --benchmark-disable-gc \ - --benchmark-autosave \ - --benchmark-save-data \ - --random-order \ - "bench/benchmark_$1.py" diff --git a/vendor/orjson/script/pybench-empty b/vendor/orjson/script/pybench-empty deleted file mode 100755 index 1e53454da1..0000000000 --- a/vendor/orjson/script/pybench-empty +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -pytest \ - --verbose \ - --benchmark-min-time=1 \ - --benchmark-max-time=5 \ - --benchmark-disable-gc \ - --benchmark-autosave \ - --benchmark-save-data \ - --random-order \ - -k orjson \ - "bench/benchmark_empty.py" diff --git a/vendor/orjson/script/pycorrectness b/vendor/orjson/script/pycorrectness deleted file mode 100755 index 3e9c3f3654..0000000000 --- a/vendor/orjson/script/pycorrectness +++ /dev/null @@ -1,173 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import collections -import io -import json -import lzma -import os -from pathlib import Path - -import rapidjson -import simplejson -import ujson -from tabulate import tabulate - -import orjson - -dirname = os.path.join(os.path.dirname(__file__), "..", "data") - -LIBRARIES = ["orjson", "ujson", "rapidjson", "simplejson", "json"] - - -LIBRARY_FUNC_MAP = { - "orjson": orjson.loads, - "ujson": ujson.loads, - "rapidjson": rapidjson.loads, - "simplejson": simplejson.loads, - "json": json.loads, -} - - -def read_fixture_bytes(filename, subdir=None): - if subdir is None: - parts = (dirname, filename) - else: - parts = (dirname, subdir, filename) - path = Path(*parts) - if path.suffix == ".xz": - contents = lzma.decompress(path.read_bytes()) - else: - contents = path.read_bytes() - return contents - - -PARSING = { - filename: read_fixture_bytes(filename, "parsing") - for filename in os.listdir("data/parsing") -} - -JSONCHECKER = { - filename: read_fixture_bytes(filename, "jsonchecker") - for filename in os.listdir("data/jsonchecker") -} - - -RESULTS = collections.defaultdict(dict) - - -def test_passed(libname, fixture): - passed = [] - loads = LIBRARY_FUNC_MAP[libname] - try: - passed.append(loads(fixture) == orjson.loads(fixture)) - passed.append( - loads(fixture.decode("utf-8")) == orjson.loads(fixture.decode("utf-8")) - ) - except Exception: - passed.append(False) - return all(passed) - - -def test_failed(libname, fixture): - rejected_as_bytes = False - loads = LIBRARY_FUNC_MAP[libname] - try: - loads(fixture) - except Exception: - rejected_as_bytes = True - - rejected_as_str = False - try: - loads(fixture.decode("utf-8")) - except Exception: - rejected_as_str = True - return rejected_as_bytes and rejected_as_str - - -MISTAKEN_PASSES = {key: 0 for key in LIBRARIES} - -MISTAKEN_FAILS = {key: 0 for key in LIBRARIES} - -PASS_WHITELIST = ("fail01.json", "fail18.json") - - -def should_pass(filename): - return ( - filename.startswith("y_") - or filename.startswith("pass") - or filename in PASS_WHITELIST - ) - - -def should_fail(filename): - return ( - filename.startswith("n_") - or filename.startswith("i_string") - or filename.startswith("i_object") - or filename.startswith("fail") - ) and filename not in PASS_WHITELIST - - -for libname in LIBRARIES: - for fixture_set in (PARSING, JSONCHECKER): - for filename, fixture in fixture_set.items(): - if should_pass(filename): - res = test_passed(libname, fixture) - RESULTS[filename][libname] = res - if not res: - MISTAKEN_PASSES[libname] += 1 - - elif should_fail(filename): - res = test_failed(libname, fixture) - RESULTS[filename][libname] = res - if not res: - MISTAKEN_FAILS[libname] += 1 - elif filename.startswith("i_"): - continue - else: - raise NotImplementedError - -FILENAMES = sorted(list(PARSING.keys()) + list(JSONCHECKER.keys())) - - -tab_results = [] -for filename in FILENAMES: - entry = [ - filename, - ] - for libname in LIBRARIES: - try: - entry.append("ok" if RESULTS[filename][libname] else "fail") - except KeyError: - continue - tab_results.append(entry) - -buf = io.StringIO() -buf.write(tabulate(tab_results, ["Fixture"] + LIBRARIES, tablefmt="github")) -buf.write("\n") -print(buf.getvalue()) - -failure_results = [ - [libname, MISTAKEN_FAILS[libname], MISTAKEN_PASSES[libname]] - for libname in LIBRARIES -] - -buf = io.StringIO() -buf.write( - tabulate( - failure_results, - [ - "Library", - "Invalid JSON documents not rejected", - "Valid JSON documents not deserialized", - ], - tablefmt="github", - ) -) -buf.write("\n") -print(buf.getvalue()) - -num_results = len([each for each in tab_results if len(each) > 1]) - -print(f"{num_results} documents tested") diff --git a/vendor/orjson/script/pydataclass b/vendor/orjson/script/pydataclass deleted file mode 100755 index b43c95a455..0000000000 --- a/vendor/orjson/script/pydataclass +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import dataclasses -import io -import json -import os -from timeit import timeit -from typing import List - -import rapidjson -import simplejson -import ujson -from tabulate import tabulate - -import orjson - -os.sched_setaffinity(os.getpid(), {0, 1}) - - -@dataclasses.dataclass -class Member: - id: int - active: bool - - -@dataclasses.dataclass -class Object: - id: int - name: str - members: List[Member] - - -objects_as_dataclass = [ - Object(i, str(i) * 3, [Member(j, True) for j in range(0, 10)]) - for i in range(100000, 102000) -] - -objects_as_dict = [dataclasses.asdict(each) for each in objects_as_dataclass] - -output_in_kib = len(orjson.dumps(objects_as_dict)) / 1024 - -print(f"{output_in_kib:,.0f}KiB output (orjson)") - - -def default(__obj): - if dataclasses.is_dataclass(__obj): - return dataclasses.asdict(__obj) - - -headers = ("Library", "dict (ms)", "dataclass (ms)", "vs. orjson") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -ITERATIONS = 100 - - -def per_iter_latency(val): - if val is None: - return None - return (val * 1000) / ITERATIONS - - -table = [] -for lib_name in LIBRARIES: - if lib_name == "json": - as_dict = timeit( - lambda: json.dumps(objects_as_dict).encode("utf-8"), - number=ITERATIONS, - ) - as_dataclass = timeit( - lambda: json.dumps(objects_as_dataclass, default=default).encode("utf-8"), - number=ITERATIONS, - ) - elif lib_name == "simplejson": - as_dict = timeit( - lambda: simplejson.dumps(objects_as_dict).encode("utf-8"), - number=ITERATIONS, - ) - as_dataclass = timeit( - lambda: simplejson.dumps(objects_as_dataclass, default=default).encode( - "utf-8" - ), - number=ITERATIONS, - ) - elif lib_name == "ujson": - as_dict = timeit( - lambda: ujson.dumps(objects_as_dict).encode("utf-8"), - number=ITERATIONS, - ) - as_dataclass = None - elif lib_name == "rapidjson": - as_dict = timeit( - lambda: rapidjson.dumps(objects_as_dict).encode("utf-8"), - number=ITERATIONS, - ) - as_dataclass = timeit( - lambda: rapidjson.dumps(objects_as_dataclass, default=default).encode( - "utf-8" - ), - number=ITERATIONS, - ) - elif lib_name == "orjson": - as_dict = timeit(lambda: orjson.dumps(objects_as_dict), number=ITERATIONS) - as_dataclass = timeit( - lambda: orjson.dumps( - objects_as_dataclass, None, orjson.OPT_SERIALIZE_DATACLASS - ), - number=ITERATIONS, - ) - orjson_as_dataclass = per_iter_latency(as_dataclass) - else: - raise NotImplementedError - - as_dict = per_iter_latency(as_dict) - as_dataclass = per_iter_latency(as_dataclass) - - if lib_name == "orjson": - compared_to_orjson = 1 - elif as_dataclass: - compared_to_orjson = int(as_dataclass / orjson_as_dataclass) - else: - compared_to_orjson = None - - table.append( - ( - lib_name, - f"{as_dict:,.2f}" if as_dict else "", - f"{as_dataclass:,.2f}" if as_dataclass else "", - f"{compared_to_orjson:d}" if compared_to_orjson else "", - ) - ) - -buf = io.StringIO() -buf.write(tabulate(table, headers, tablefmt="github")) -buf.write("\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pyindent b/vendor/orjson/script/pyindent deleted file mode 100755 index 753c855f70..0000000000 --- a/vendor/orjson/script/pyindent +++ /dev/null @@ -1,137 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import io -import json -import lzma -import os -import sys -from pathlib import Path -from timeit import timeit - -import rapidjson -import simplejson -import ujson -from tabulate import tabulate - -import orjson - -os.sched_setaffinity(os.getpid(), {0, 1}) - - -dirname = os.path.join(os.path.dirname(__file__), "..", "data") - - -def read_fixture_obj(filename): - path = Path(dirname, filename) - if path.suffix == ".xz": - contents = lzma.decompress(path.read_bytes()) - else: - contents = path.read_bytes() - return orjson.loads(contents) - - -filename = sys.argv[1] if len(sys.argv) >= 1 else "" - -data = read_fixture_obj(f"{filename}.json.xz") - -headers = ("Library", "compact (ms)", "pretty (ms)", "vs. orjson") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -output_in_kib_compact = len(orjson.dumps(data)) / 1024 -output_in_kib_pretty = len(orjson.dumps(data, option=orjson.OPT_INDENT_2)) / 1024 - -# minimum 2s runtime for orjson compact -ITERATIONS = int(2 / (timeit(lambda: orjson.dumps(data), number=20) / 20)) - -print( - f"{output_in_kib_compact:,.0f}KiB compact, {output_in_kib_pretty:,.0f}KiB pretty, {ITERATIONS} iterations" -) - - -def per_iter_latency(val): - if val is None: - return None - return (val * 1000) / ITERATIONS - - -def test_correctness(serialized): - return orjson.loads(serialized) == data - - -table = [] -for lib_name in LIBRARIES: - print(f"{lib_name}...") - if lib_name == "json": - time_compact = timeit( - lambda: json.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_pretty = timeit( - lambda: json.dumps(data, indent=2).encode("utf-8"), - number=ITERATIONS, - ) - correct = test_correctness(json.dumps(data, indent=2).encode("utf-8")) - elif lib_name == "simplejson": - time_compact = timeit( - lambda: simplejson.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_pretty = timeit( - lambda: simplejson.dumps(data, indent=2).encode("utf-8"), - number=ITERATIONS, - ) - correct = test_correctness(simplejson.dumps(data, indent=2).encode("utf-8")) - elif lib_name == "ujson": - time_compact = timeit( - lambda: ujson.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_pretty = timeit( - lambda: ujson.dumps(data, indent=2).encode("utf-8"), - number=ITERATIONS, - ) - correct = test_correctness(ujson.dumps(data, indent=2).encode("utf-8")) - elif lib_name == "rapidjson": - time_compact = timeit(lambda: rapidjson.dumps(data), number=ITERATIONS) - time_pretty = timeit(lambda: rapidjson.dumps(data, indent=2), number=ITERATIONS) - correct = test_correctness(rapidjson.dumps(data, indent=2)) - elif lib_name == "orjson": - time_compact = timeit(lambda: orjson.dumps(data), number=ITERATIONS) - time_pretty = timeit( - lambda: orjson.dumps(data, None, orjson.OPT_INDENT_2), - number=ITERATIONS, - ) - correct = test_correctness(orjson.dumps(data, None, orjson.OPT_INDENT_2)) - orjson_time_pretty = per_iter_latency(time_pretty) - else: - raise NotImplementedError - - time_compact = per_iter_latency(time_compact) - if not correct: - time_pretty = None - else: - time_pretty = per_iter_latency(time_pretty) - - if lib_name == "orjson": - compared_to_orjson = 1 - elif time_pretty: - compared_to_orjson = time_pretty / orjson_time_pretty - else: - compared_to_orjson = None - - table.append( - ( - lib_name, - f"{time_compact:,.2f}" if time_compact else "", - f"{time_pretty:,.2f}" if time_pretty else "", - f"{compared_to_orjson:,.1f}" if compared_to_orjson else "", - ) - ) - -buf = io.StringIO() -buf.write(tabulate(table, headers, tablefmt="github")) -buf.write("\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pymem b/vendor/orjson/script/pymem deleted file mode 100755 index 6b97bb0d73..0000000000 --- a/vendor/orjson/script/pymem +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import io -import subprocess - -from tabulate import tabulate - -buf = io.StringIO() - -headers = ("Library", "import, read() RSS (MiB)", "loads() increase in RSS (MiB)") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -FIXTURES = ("canada.json", "citm_catalog.json", "github.json", "twitter.json") - -for fixture in sorted(FIXTURES, reverse=True): - table = [] - buf.write("\n" + "#### " + fixture + "\n\n") - for lib_name in LIBRARIES: - proc = subprocess.Popen( - ("bench/run_mem", f"data/{fixture}.xz", lib_name), stdout=subprocess.PIPE - ) - output = proc.stdout.readline().decode("utf-8").strip().split(",") - mem_base = int(output[0]) / 1024 / 1024 - mem_diff = int(output[1]) / 1024 / 1024 - correct = bool(int(output[2])) - if correct: - table.append((lib_name, f"{mem_base:,.1f}", f"{mem_diff:,.1f}")) - else: - table.append((lib_name, "", "")) - buf.write(tabulate(table, headers, tablefmt="github") + "\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pynonstr b/vendor/orjson/script/pynonstr deleted file mode 100755 index 9670443a76..0000000000 --- a/vendor/orjson/script/pynonstr +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import datetime -import io -import json -import os -import random -from time import mktime -from timeit import timeit - -import rapidjson -import simplejson -import ujson -from tabulate import tabulate - -import orjson - -os.sched_setaffinity(os.getpid(), {0, 1}) - -data_as_obj = [] -for year in range(1920, 2020): - start = datetime.date(year, 1, 1) - array = [ - (int(mktime((start + datetime.timedelta(days=i)).timetuple())), i + 1) - for i in range(0, 365) - ] - array.append(("other", 0)) - random.shuffle(array) - data_as_obj.append(dict(array)) - -data_as_str = orjson.loads(orjson.dumps(data_as_obj, option=orjson.OPT_NON_STR_KEYS)) - -headers = ("Library", "str keys (ms)", "int keys (ms)", "int keys sorted (ms)") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -ITERATIONS = 500 - - -output_in_kib = len(orjson.dumps(data_as_str)) / 1024 - -print(f"{output_in_kib:,.0f}KiB output (orjson)") - - -def per_iter_latency(val): - if val is None: - return None - return (val * 1000) / ITERATIONS - - -def test_correctness(serialized): - return orjson.loads(serialized) == data_as_str - - -table = [] -for lib_name in LIBRARIES: - print(f"{lib_name}...") - if lib_name == "json": - time_as_str = timeit( - lambda: json.dumps(data_as_str).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj = timeit( - lambda: json.dumps(data_as_obj).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj_sorted = ( - None # TypeError: '<' not supported between instances of 'str' and 'int' - ) - correct = False - elif lib_name == "simplejson": - time_as_str = timeit( - lambda: simplejson.dumps(data_as_str).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj = timeit( - lambda: simplejson.dumps(data_as_obj).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj_sorted = timeit( - lambda: simplejson.dumps(data_as_obj, sort_keys=True).encode("utf-8"), - number=ITERATIONS, - ) - correct = test_correctness( - simplejson.dumps(data_as_obj, sort_keys=True).encode("utf-8") - ) - elif lib_name == "ujson": - time_as_str = timeit( - lambda: ujson.dumps(data_as_str).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj = timeit( - lambda: ujson.dumps(data_as_obj).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj_sorted = None # segfault - correct = False - elif lib_name == "rapidjson": - time_as_str = timeit( - lambda: rapidjson.dumps(data_as_str).encode("utf-8"), - number=ITERATIONS, - ) - time_as_obj = None - time_as_obj_sorted = None - correct = False - elif lib_name == "orjson": - time_as_str = timeit( - lambda: orjson.dumps(data_as_str, None, orjson.OPT_NON_STR_KEYS), - number=ITERATIONS, - ) - time_as_obj = timeit( - lambda: orjson.dumps(data_as_obj, None, orjson.OPT_NON_STR_KEYS), - number=ITERATIONS, - ) - time_as_obj_sorted = timeit( - lambda: orjson.dumps( - data_as_obj, None, orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS - ), - number=ITERATIONS, - ) - correct = test_correctness( - orjson.dumps( - data_as_obj, None, orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS - ) - ) - else: - raise NotImplementedError - - time_as_str = per_iter_latency(time_as_str) - time_as_obj = per_iter_latency(time_as_obj) - if not correct: - time_as_obj_sorted = None - else: - time_as_obj_sorted = per_iter_latency(time_as_obj_sorted) - - table.append( - ( - lib_name, - f"{time_as_str:,.2f}" if time_as_str else "", - f"{time_as_obj:,.2f}" if time_as_obj else "", - f"{time_as_obj_sorted:,.2f}" if time_as_obj_sorted else "", - ) - ) - -buf = io.StringIO() -buf.write(tabulate(table, headers, tablefmt="github")) -buf.write("\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pynumpy b/vendor/orjson/script/pynumpy deleted file mode 100755 index 2ad6d982d8..0000000000 --- a/vendor/orjson/script/pynumpy +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import gc -import io -import json -import os -import sys -import time -from timeit import timeit - -import numpy -import psutil -import rapidjson -import simplejson -from memory_profiler import memory_usage -from tabulate import tabulate - -import orjson - -os.sched_setaffinity(os.getpid(), {0, 1}) - - -kind = sys.argv[1] if len(sys.argv) >= 1 else "" - - -if kind == "float16": - dtype = numpy.float16 - array = numpy.random.random(size=(50000, 100)).astype(dtype) -elif kind == "float32": - dtype = numpy.float32 - array = numpy.random.random(size=(50000, 100)).astype(dtype) -elif kind == "float64": - dtype = numpy.float64 - array = numpy.random.random(size=(50000, 100)) - assert array.dtype == numpy.float64 -elif kind == "bool": - dtype = numpy.bool_ - array = numpy.random.choice((True, False), size=(100000, 200)) -elif kind == "int8": - dtype = numpy.int8 - array = numpy.random.randint(((2**7) - 1), size=(100000, 100), dtype=dtype) -elif kind == "int16": - dtype = numpy.int16 - array = numpy.random.randint(((2**15) - 1), size=(100000, 100), dtype=dtype) -elif kind == "int32": - dtype = numpy.int32 - array = numpy.random.randint(((2**31) - 1), size=(100000, 100), dtype=dtype) -elif kind == "uint8": - dtype = numpy.uint8 - array = numpy.random.randint(((2**8) - 1), size=(100000, 100), dtype=dtype) -elif kind == "uint16": - dtype = numpy.uint16 - array = numpy.random.randint(((2**16) - 1), size=(100000, 100), dtype=dtype) -elif kind == "uint32": - dtype = numpy.uint32 - array = numpy.random.randint(((2**31) - 1), size=(100000, 100), dtype=dtype) -else: - print( - "usage: pynumpy (bool|int16|int32|float16|float32|float64|int8|uint8|uint16|uint32)" - ) - sys.exit(1) -proc = psutil.Process() - - -def default(__obj): - if isinstance(__obj, numpy.ndarray): - return __obj.tolist() - raise TypeError - - -headers = ("Library", "Latency (ms)", "RSS diff (MiB)", "vs. orjson") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -ITERATIONS = 10 - - -def orjson_dumps(): - return orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) - - -ujson_dumps = None - - -def rapidjson_dumps(): - return rapidjson.dumps(array, default=default).encode("utf-8") - - -def simplejson_dumps(): - return simplejson.dumps(array, default=default).encode("utf-8") - - -def json_dumps(): - return json.dumps(array, default=default).encode("utf-8") - - -output_in_mib = len(orjson_dumps()) / 1024 / 1024 - -print(f"{output_in_mib:,.1f}MiB {kind} output (orjson)") - -gc.collect() -mem_before = proc.memory_full_info().rss / 1024 / 1024 - - -def per_iter_latency(val): - if val is None: - return None - return (val * 1000) / ITERATIONS - - -def test_correctness(func): - return numpy.array_equal(array, numpy.array(orjson.loads(func()), dtype=dtype)) - - -table = [] -for lib_name in LIBRARIES: - gc.collect() - - print(f"{lib_name}...") - func = locals()[f"{lib_name}_dumps"] - if func is None: - total_latency = None - latency = None - mem = None - correct = False - else: - total_latency = timeit( - func, - number=ITERATIONS, - ) - latency = per_iter_latency(total_latency) - time.sleep(1) - mem = max(memory_usage((func,), interval=0.001, timeout=latency * 2)) - correct = test_correctness(func) - - if lib_name == "orjson": - compared_to_orjson = 1 - orjson_latency = latency - elif latency: - compared_to_orjson = latency / orjson_latency - else: - compared_to_orjson = None - - if not correct: - latency = None - mem = 0 - - mem_diff = mem - mem_before - - table.append( - ( - lib_name, - f"{latency:,.0f}" if latency else "", - f"{mem_diff:,.0f}" if mem else "", - f"{compared_to_orjson:,.1f}" if (latency and compared_to_orjson) else "", - ) - ) - -buf = io.StringIO() -buf.write(tabulate(table, headers, tablefmt="github")) -buf.write("\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pysort b/vendor/orjson/script/pysort deleted file mode 100755 index b1a4720452..0000000000 --- a/vendor/orjson/script/pysort +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import io -import json -import lzma -import os -from pathlib import Path -from timeit import timeit - -import rapidjson -import simplejson -import ujson -from tabulate import tabulate - -import orjson - -os.sched_setaffinity(os.getpid(), {0, 1}) - - -dirname = os.path.join(os.path.dirname(__file__), "..", "data") - - -def read_fixture_obj(filename): - path = Path(dirname, filename) - if path.suffix == ".xz": - contents = lzma.decompress(path.read_bytes()) - else: - contents = path.read_bytes() - return orjson.loads(contents) - - -data = read_fixture_obj("twitter.json.xz") - -headers = ("Library", "unsorted (ms)", "sorted (ms)", "vs. orjson") - -LIBRARIES = ("orjson", "ujson", "rapidjson", "simplejson", "json") - -ITERATIONS = 500 - - -def per_iter_latency(val): - if val is None: - return None - return (val * 1000) / ITERATIONS - - -table = [] -for lib_name in LIBRARIES: - if lib_name == "json": - time_unsorted = timeit( - lambda: json.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_sorted = timeit( - lambda: json.dumps(data, sort_keys=True).encode("utf-8"), - number=ITERATIONS, - ) - elif lib_name == "simplejson": - time_unsorted = timeit( - lambda: simplejson.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_sorted = timeit( - lambda: simplejson.dumps(data, sort_keys=True).encode("utf-8"), - number=ITERATIONS, - ) - elif lib_name == "ujson": - time_unsorted = timeit( - lambda: ujson.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_sorted = timeit( - lambda: ujson.dumps(data, sort_keys=True).encode("utf-8"), - number=ITERATIONS, - ) - elif lib_name == "rapidjson": - time_unsorted = timeit( - lambda: rapidjson.dumps(data).encode("utf-8"), - number=ITERATIONS, - ) - time_sorted = timeit( - lambda: rapidjson.dumps(data, sort_keys=True).encode("utf-8"), - number=ITERATIONS, - ) - elif lib_name == "orjson": - time_unsorted = timeit(lambda: orjson.dumps(data), number=ITERATIONS) - time_sorted = timeit( - lambda: orjson.dumps(data, None, orjson.OPT_SORT_KEYS), - number=ITERATIONS, - ) - orjson_time_sorted = per_iter_latency(time_sorted) - else: - raise NotImplementedError - - time_unsorted = per_iter_latency(time_unsorted) - time_sorted = per_iter_latency(time_sorted) - - if lib_name == "orjson": - compared_to_orjson = 1 - elif time_unsorted: - compared_to_orjson = time_sorted / orjson_time_sorted - else: - compared_to_orjson = None - - table.append( - ( - lib_name, - f"{time_unsorted:,.2f}" if time_unsorted else "", - f"{time_sorted:,.2f}" if time_sorted else "", - f"{compared_to_orjson:,.1f}" if compared_to_orjson else "", - ) - ) - -buf = io.StringIO() -buf.write(tabulate(table, headers, tablefmt="github")) -buf.write("\n") - -print(buf.getvalue()) diff --git a/vendor/orjson/script/pytest b/vendor/orjson/script/pytest deleted file mode 100755 index e8e7e51d10..0000000000 --- a/vendor/orjson/script/pytest +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -e - -PYTHONMALLOC="debug" pytest -s test diff --git a/vendor/orjson/script/valgrind b/vendor/orjson/script/valgrind deleted file mode 100755 index cbf2a3043f..0000000000 --- a/vendor/orjson/script/valgrind +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -valgrind pytest -v --ignore=test/test_memory.py test diff --git a/vendor/orjson/script/vendor-yyjson b/vendor/orjson/script/vendor-yyjson deleted file mode 100755 index c22c12e659..0000000000 --- a/vendor/orjson/script/vendor-yyjson +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash - -set -eou pipefail - -yyjson_version="b21c02904188add942d3c7cd4885422e4335f115" - -curl -Ls -o include/yyjson/yyjson.c "https://raw.githubusercontent.com/ibireme/yyjson/${yyjson_version}/src/yyjson.c" -curl -Ls -o include/yyjson/yyjson.h "https://raw.githubusercontent.com/ibireme/yyjson/${yyjson_version}/src/yyjson.h" - -sed -i 's/yyjson_api_inline void yyjson_doc_free/yyjson_api void yyjson_doc_free/g' include/yyjson/yyjson.h - -sed -i 's/(flg & (YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_BIGNUM_AS_RAW)) != 0/false/g' include/yyjson/yyjson.c -sed -i 's/if (pre)/if (false)/g' include/yyjson/yyjson.c - -sed -i 's/!false/true/g' include/yyjson/yyjson.c -sed -i 's/ && true//g' include/yyjson/yyjson.c -sed -i 's/true && //g' include/yyjson/yyjson.c -sed -i 's/unlikely(false)/false/g' include/yyjson/yyjson.c - -sed -i 's/YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC/YYJSON_TYPE_STR/g' include/yyjson/yyjson.c - -sed -i 's/unlikely(pos == src)/false/g' include/yyjson/yyjson.c - -sed -i 's/ yyjson_read_err dummy_err;//g' include/yyjson/yyjson.c -sed -i 's/ if (!err) err = &dummy_err;//g' include/yyjson/yyjson.c -sed -i 's/likely(!alc_ptr)/!alc_ptr/g' include/yyjson/yyjson.c - -sed -i 's/unlikely(read_flag_eq(flg, YYJSON_READ_##_flag))/false/g' include/yyjson/yyjson.c -sed -i 's/has_read_flag(ALLOW_INF_AND_NAN)/false/g' include/yyjson/yyjson.c -sed -i 's/has_read_flag(ALLOW_COMMENTS)/false/g' include/yyjson/yyjson.c -sed -i 's/has_read_flag(BIGNUM_AS_RAW)/false/g' include/yyjson/yyjson.c -sed -i 's/if (pre && \*pre)/if (false)/g' include/yyjson/yyjson.c -sed -i 's/(pre && !false)/(false)/g' include/yyjson/yyjson.c - -git apply include/yyjson-recursion-limit.patch -git apply include/yyjson-reduce-unused.patch diff --git a/vendor/orjson/src/deserialize/backend/json.rs b/vendor/orjson/src/deserialize/backend/json.rs deleted file mode 100644 index 4a962b0e95..0000000000 --- a/vendor/orjson/src/deserialize/backend/json.rs +++ /dev/null @@ -1,144 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::deserialize::pyobject::*; -use crate::deserialize::DeserializeError; -use crate::str::unicode_from_str; -use core::ptr::NonNull; -use serde::de::{self, DeserializeSeed, Deserializer, MapAccess, SeqAccess, Visitor}; -use smallvec::SmallVec; -use std::borrow::Cow; -use std::fmt; - -pub(crate) fn deserialize( - data: &'static str, -) -> Result, DeserializeError<'static>> { - let mut deserializer = serde_json::Deserializer::from_str(data); - let seed = JsonValue {}; - match seed.deserialize(&mut deserializer) { - Ok(obj) => { - deserializer.end().map_err(|e| { - DeserializeError::from_json(Cow::Owned(e.to_string()), e.line(), e.column(), data) - })?; - Ok(obj) - } - Err(e) => Err(DeserializeError::from_json( - Cow::Owned(e.to_string()), - e.line(), - e.column(), - data, - )), - } -} - -#[derive(Clone, Copy)] -struct JsonValue; - -impl<'de> DeserializeSeed<'de> for JsonValue { - type Value = NonNull; - - fn deserialize(self, deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_any(self) - } -} - -impl<'de> Visitor<'de> for JsonValue { - type Value = NonNull; - - fn expecting(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { - Ok(()) - } - - fn visit_unit(self) -> Result { - Ok(parse_none()) - } - - fn visit_bool(self, value: bool) -> Result - where - E: de::Error, - { - Ok(parse_bool(value)) - } - - fn visit_i64(self, value: i64) -> Result - where - E: de::Error, - { - Ok(parse_i64(value)) - } - - fn visit_u64(self, value: u64) -> Result - where - E: de::Error, - { - Ok(parse_u64(value)) - } - - fn visit_f64(self, value: f64) -> Result - where - E: de::Error, - { - Ok(parse_f64(value)) - } - - fn visit_borrowed_str(self, value: &str) -> Result - where - E: de::Error, - { - Ok(nonnull!(unicode_from_str(value))) - } - - fn visit_str(self, value: &str) -> Result - where - E: de::Error, - { - Ok(nonnull!(unicode_from_str(value))) - } - - fn visit_seq(self, mut seq: A) -> Result - where - A: SeqAccess<'de>, - { - match seq.next_element_seed(self) { - Ok(None) => Ok(nonnull!(ffi!(PyList_New(0)))), - Ok(Some(elem)) => { - let mut elements: SmallVec<[*mut pyo3_ffi::PyObject; 8]> = - SmallVec::with_capacity(8); - elements.push(elem.as_ptr()); - while let Some(elem) = seq.next_element_seed(self)? { - elements.push(elem.as_ptr()); - } - let ptr = ffi!(PyList_New(elements.len() as isize)); - for (i, &obj) in elements.iter().enumerate() { - ffi!(PyList_SET_ITEM(ptr, i as isize, obj)); - } - Ok(nonnull!(ptr)) - } - Err(err) => Result::Err(err), - } - } - - fn visit_map(self, mut map: A) -> Result - where - A: MapAccess<'de>, - { - let dict_ptr = ffi!(PyDict_New()); - while let Some(key) = map.next_key::>()? { - let pykey = get_unicode_key(&key); - let pyval = map.next_value_seed(self)?; - let _ = unsafe { - pyo3_ffi::_PyDict_SetItem_KnownHash( - dict_ptr, - pykey, - pyval.as_ptr(), - str_hash!(pykey), - ) - }; - reverse_pydict_incref!(pykey); - reverse_pydict_incref!(pyval.as_ptr()); - } - Ok(nonnull!(dict_ptr)) - } -} diff --git a/vendor/orjson/src/deserialize/backend/mod.rs b/vendor/orjson/src/deserialize/backend/mod.rs deleted file mode 100644 index d85ab1632b..0000000000 --- a/vendor/orjson/src/deserialize/backend/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -#[cfg(not(feature = "yyjson"))] -mod json; - -#[cfg(feature = "yyjson")] -mod yyjson; - -#[cfg(feature = "yyjson")] -pub(crate) use yyjson::deserialize; - -#[cfg(not(feature = "yyjson"))] -pub(crate) use json::deserialize; diff --git a/vendor/orjson/src/deserialize/backend/yyjson.rs b/vendor/orjson/src/deserialize/backend/yyjson.rs deleted file mode 100644 index c5538b4ba6..0000000000 --- a/vendor/orjson/src/deserialize/backend/yyjson.rs +++ /dev/null @@ -1,295 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::deserialize::pyobject::*; -use crate::deserialize::DeserializeError; -use crate::ffi::yyjson::*; -use crate::str::unicode_from_str; -use crate::typeref::{yyjson_init, YYJSON_ALLOC, YYJSON_BUFFER_SIZE}; -use core::ffi::c_char; -use core::ptr::{null, null_mut, NonNull}; -use std::borrow::Cow; - -const YYJSON_TAG_BIT: u8 = 8; - -const YYJSON_VAL_SIZE: usize = core::mem::size_of::(); - -const TAG_ARRAY: u8 = 0b00000110; -const TAG_DOUBLE: u8 = 0b00010100; -const TAG_FALSE: u8 = 0b00000011; -const TAG_INT64: u8 = 0b00001100; -const TAG_NULL: u8 = 0b00000010; -const TAG_OBJECT: u8 = 0b00000111; -const TAG_STRING: u8 = 0b00000101; -const TAG_TRUE: u8 = 0b00001011; -const TAG_UINT64: u8 = 0b00000100; - -macro_rules! is_yyjson_tag { - ($elem:expr, $tag:expr) => { - unsafe { (*$elem).tag as u8 == $tag } - }; -} - -fn yyjson_doc_get_root(doc: *mut yyjson_doc) -> *mut yyjson_val { - unsafe { (*doc).root } -} - -fn unsafe_yyjson_get_len(val: *mut yyjson_val) -> usize { - unsafe { ((*val).tag >> YYJSON_TAG_BIT) as usize } -} - -fn unsafe_yyjson_get_first(ctn: *mut yyjson_val) -> *mut yyjson_val { - unsafe { ctn.add(1) } -} - -fn yyjson_read_max_memory_usage(len: usize) -> usize { - (12 * len) + 256 -} - -fn unsafe_yyjson_is_ctn(val: *mut yyjson_val) -> bool { - unsafe { (*val).tag as u8 & 0b00000110 == 0b00000110 } -} - -fn unsafe_yyjson_get_next_container(val: *mut yyjson_val) -> *mut yyjson_val { - unsafe { ((val as *mut u8).add((*val).uni.ofs)) as *mut yyjson_val } -} - -fn unsafe_yyjson_get_next_non_container(val: *mut yyjson_val) -> *mut yyjson_val { - unsafe { ((val as *mut u8).add(YYJSON_VAL_SIZE)) as *mut yyjson_val } -} - -pub(crate) fn deserialize( - data: &'static str, -) -> Result, DeserializeError<'static>> { - let mut err = yyjson_read_err { - code: YYJSON_READ_SUCCESS, - msg: null(), - pos: 0, - }; - let doc = if yyjson_read_max_memory_usage(data.len()) < YYJSON_BUFFER_SIZE { - read_doc_with_buffer(data, &mut err) - } else { - read_doc_default(data, &mut err) - }; - if unlikely!(doc.is_null()) { - let msg: Cow = unsafe { core::ffi::CStr::from_ptr(err.msg).to_string_lossy() }; - Err(DeserializeError::from_yyjson(msg, err.pos as i64, data)) - } else { - let val = yyjson_doc_get_root(doc); - - if unlikely!(!unsafe_yyjson_is_ctn(val)) { - let pyval = match ElementType::from_tag(val) { - ElementType::String => parse_yy_string(val), - ElementType::Uint64 => parse_yy_u64(val), - ElementType::Int64 => parse_yy_i64(val), - ElementType::Double => parse_yy_f64(val), - ElementType::Null => parse_none(), - ElementType::True => parse_true(), - ElementType::False => parse_false(), - ElementType::Array => unreachable!(), - ElementType::Object => unreachable!(), - }; - unsafe { yyjson_doc_free(doc) }; - Ok(pyval) - } else if is_yyjson_tag!(val, TAG_ARRAY) { - let pyval = nonnull!(ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize))); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_array(pyval.as_ptr(), val); - } - unsafe { yyjson_doc_free(doc) }; - Ok(pyval) - } else { - let pyval = nonnull!(ffi!(_PyDict_NewPresized( - unsafe_yyjson_get_len(val) as isize - ))); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_object(pyval.as_ptr(), val); - } - unsafe { yyjson_doc_free(doc) }; - Ok(pyval) - } - } -} - -fn read_doc_default(data: &'static str, err: &mut yyjson_read_err) -> *mut yyjson_doc { - unsafe { yyjson_read_opts(data.as_ptr() as *mut c_char, data.len(), null_mut(), err) } -} - -fn read_doc_with_buffer(data: &'static str, err: &mut yyjson_read_err) -> *mut yyjson_doc { - unsafe { - yyjson_read_opts( - data.as_ptr() as *mut c_char, - data.len(), - &YYJSON_ALLOC.get_or_init(yyjson_init).alloc, - err, - ) - } -} - -enum ElementType { - String, - Uint64, - Int64, - Double, - Null, - True, - False, - Array, - Object, -} - -impl ElementType { - fn from_tag(elem: *mut yyjson_val) -> Self { - match unsafe { (*elem).tag as u8 } { - TAG_STRING => Self::String, - TAG_UINT64 => Self::Uint64, - TAG_INT64 => Self::Int64, - TAG_DOUBLE => Self::Double, - TAG_NULL => Self::Null, - TAG_TRUE => Self::True, - TAG_FALSE => Self::False, - TAG_ARRAY => Self::Array, - TAG_OBJECT => Self::Object, - _ => unreachable!(), - } - } -} - -#[inline(always)] -fn parse_yy_string(elem: *mut yyjson_val) -> NonNull { - nonnull!(unicode_from_str(str_from_slice!( - (*elem).uni.str_ as *const u8, - unsafe_yyjson_get_len(elem) - ))) -} - -#[inline(always)] -fn parse_yy_u64(elem: *mut yyjson_val) -> NonNull { - parse_u64(unsafe { (*elem).uni.u64_ }) -} - -#[inline(always)] -fn parse_yy_i64(elem: *mut yyjson_val) -> NonNull { - parse_i64(unsafe { (*elem).uni.i64_ }) -} - -#[inline(always)] -fn parse_yy_f64(elem: *mut yyjson_val) -> NonNull { - parse_f64(unsafe { (*elem).uni.f64_ }) -} - -macro_rules! append_to_list { - ($dptr:expr, $pyval:expr) => { - unsafe { - core::ptr::write($dptr, $pyval); - $dptr = $dptr.add(1); - } - }; -} - -#[inline(never)] -fn populate_yy_array(list: *mut pyo3_ffi::PyObject, elem: *mut yyjson_val) { - unsafe { - let len = unsafe_yyjson_get_len(elem); - assume!(len >= 1); - let mut next = unsafe_yyjson_get_first(elem); - let mut dptr = (*(list as *mut pyo3_ffi::PyListObject)).ob_item; - - for _ in 0..len { - let val = next; - if unlikely!(unsafe_yyjson_is_ctn(val)) { - next = unsafe_yyjson_get_next_container(val); - if is_yyjson_tag!(val, TAG_ARRAY) { - let pyval = ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize)); - append_to_list!(dptr, pyval); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_array(pyval, val); - } - } else { - let pyval = ffi!(_PyDict_NewPresized(unsafe_yyjson_get_len(val) as isize)); - append_to_list!(dptr, pyval); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_object(pyval, val); - } - } - } else { - next = unsafe_yyjson_get_next_non_container(val); - let pyval = match ElementType::from_tag(val) { - ElementType::String => parse_yy_string(val), - ElementType::Uint64 => parse_yy_u64(val), - ElementType::Int64 => parse_yy_i64(val), - ElementType::Double => parse_yy_f64(val), - ElementType::Null => parse_none(), - ElementType::True => parse_true(), - ElementType::False => parse_false(), - ElementType::Array => unreachable!(), - ElementType::Object => unreachable!(), - }; - append_to_list!(dptr, pyval.as_ptr()); - } - } - } -} - -macro_rules! add_to_dict { - ($dict:expr, $pykey:expr, $pyval:expr) => { - unsafe { pyo3_ffi::_PyDict_SetItem_KnownHash($dict, $pykey, $pyval, str_hash!($pykey)) } - }; -} - -#[inline(never)] -fn populate_yy_object(dict: *mut pyo3_ffi::PyObject, elem: *mut yyjson_val) { - unsafe { - let len = unsafe_yyjson_get_len(elem); - assume!(len >= 1); - let mut next_key = unsafe_yyjson_get_first(elem); - let mut next_val = next_key.add(1); - for _ in 0..len { - let val = next_val; - let pykey = { - let key_str = str_from_slice!( - (*next_key).uni.str_ as *const u8, - unsafe_yyjson_get_len(next_key) - ); - get_unicode_key(key_str) - }; - if unlikely!(unsafe_yyjson_is_ctn(val)) { - next_key = unsafe_yyjson_get_next_container(val); - next_val = next_key.add(1); - if is_yyjson_tag!(val, TAG_ARRAY) { - let pyval = ffi!(PyList_New(unsafe_yyjson_get_len(val) as isize)); - add_to_dict!(dict, pykey, pyval); - reverse_pydict_incref!(pykey); - reverse_pydict_incref!(pyval); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_array(pyval, val); - } - } else { - let pyval = ffi!(_PyDict_NewPresized(unsafe_yyjson_get_len(val) as isize)); - add_to_dict!(dict, pykey, pyval); - reverse_pydict_incref!(pykey); - reverse_pydict_incref!(pyval); - if unsafe_yyjson_get_len(val) > 0 { - populate_yy_object(pyval, val); - } - } - } else { - next_key = unsafe_yyjson_get_next_non_container(val); - next_val = next_key.add(1); - let pyval = match ElementType::from_tag(val) { - ElementType::String => parse_yy_string(val), - ElementType::Uint64 => parse_yy_u64(val), - ElementType::Int64 => parse_yy_i64(val), - ElementType::Double => parse_yy_f64(val), - ElementType::Null => parse_none(), - ElementType::True => parse_true(), - ElementType::False => parse_false(), - ElementType::Array => unreachable!(), - ElementType::Object => unreachable!(), - }; - add_to_dict!(dict, pykey, pyval.as_ptr()); - reverse_pydict_incref!(pykey); - reverse_pydict_incref!(pyval.as_ptr()); - } - } - } -} diff --git a/vendor/orjson/src/deserialize/cache.rs b/vendor/orjson/src/deserialize/cache.rs deleted file mode 100644 index f535789350..0000000000 --- a/vendor/orjson/src/deserialize/cache.rs +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use associative_cache::{AssociativeCache, Capacity2048, HashDirectMapped, RoundRobinReplacement}; -use core::ffi::c_void; -use once_cell::unsync::OnceCell; - -#[repr(transparent)] -pub struct CachedKey { - ptr: *mut c_void, -} - -unsafe impl Send for CachedKey {} -unsafe impl Sync for CachedKey {} - -impl CachedKey { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> CachedKey { - CachedKey { - ptr: ptr as *mut c_void, - } - } - pub fn get(&mut self) -> *mut pyo3_ffi::PyObject { - let ptr = self.ptr as *mut pyo3_ffi::PyObject; - debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1); - ffi!(Py_INCREF(ptr)); - ptr - } -} - -impl Drop for CachedKey { - fn drop(&mut self) { - ffi!(Py_DECREF(self.ptr as *mut pyo3_ffi::PyObject)); - } -} - -pub type KeyMap = - AssociativeCache; - -pub static mut KEY_MAP: OnceCell = OnceCell::new(); - -#[inline(always)] -pub fn cache_hash(key: &[u8]) -> u64 { - assume!(key.len() <= 64); - xxhash_rust::xxh3::xxh3_64(key) -} diff --git a/vendor/orjson/src/deserialize/deserializer.rs b/vendor/orjson/src/deserialize/deserializer.rs deleted file mode 100644 index b5a94f3a8a..0000000000 --- a/vendor/orjson/src/deserialize/deserializer.rs +++ /dev/null @@ -1,27 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::deserialize::utf8::read_input_to_buf; -use crate::deserialize::DeserializeError; -use crate::typeref::EMPTY_UNICODE; -use core::ptr::NonNull; - -pub fn deserialize( - ptr: *mut pyo3_ffi::PyObject, -) -> Result, DeserializeError<'static>> { - debug_assert!(ffi!(Py_REFCNT(ptr)) >= 1); - let buffer = read_input_to_buf(ptr)?; - - if unlikely!(buffer.len() == 2) { - if buffer == b"[]" { - return Ok(nonnull!(ffi!(PyList_New(0)))); - } else if buffer == b"{}" { - return Ok(nonnull!(ffi!(PyDict_New()))); - } else if buffer == b"\"\"" { - unsafe { return Ok(nonnull!(use_immortal!(EMPTY_UNICODE))) } - } - } - - let buffer_str = unsafe { std::str::from_utf8_unchecked(buffer) }; - - crate::deserialize::backend::deserialize(buffer_str) -} diff --git a/vendor/orjson/src/deserialize/error.rs b/vendor/orjson/src/deserialize/error.rs deleted file mode 100644 index 710b9c8e8c..0000000000 --- a/vendor/orjson/src/deserialize/error.rs +++ /dev/null @@ -1,106 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use std::borrow::Cow; - -pub struct DeserializeError<'a> { - pub message: Cow<'a, str>, - #[cfg(not(feature = "yyjson"))] - pub line: usize, // start at 1 - #[cfg(not(feature = "yyjson"))] - pub column: usize, // start at 1 - pub data: Option<&'a str>, - #[cfg(feature = "yyjson")] - pub pos: i64, -} - -impl<'a> DeserializeError<'a> { - #[cold] - pub fn invalid(message: Cow<'a, str>) -> Self { - DeserializeError { - message: message, - #[cfg(not(feature = "yyjson"))] - line: 0, - #[cfg(not(feature = "yyjson"))] - column: 0, - data: None, - #[cfg(feature = "yyjson")] - pos: 0, - } - } - - #[cold] - #[cfg(not(feature = "yyjson"))] - pub fn from_json(message: Cow<'a, str>, line: usize, column: usize, data: &'a str) -> Self { - DeserializeError { - message, - line, - column, - data: Some(data), - } - } - - #[cold] - #[cfg(feature = "yyjson")] - pub fn from_yyjson(message: Cow<'a, str>, pos: i64, data: &'a str) -> Self { - DeserializeError { - message: message, - data: Some(data), - pos: pos, - } - } - - /// Return position of the error in the deserialized data - #[cold] - #[cfg(feature = "yyjson")] - #[cfg_attr(feature = "optimize", optimize(size))] - pub fn pos(&self) -> i64 { - match self.data { - Some(as_str) => as_str[0..self.pos as usize].chars().count() as i64, - None => 0, - } - } - - /// Return position of the error in the deserialized data - #[cold] - #[cfg(not(feature = "yyjson"))] - #[cfg_attr(feature = "optimize", optimize(size))] - pub fn pos(&self) -> i64 { - if self.line == 0 || self.data.is_none() { - return 1; - } - - let val = self.data.unwrap() - .split('\n') - // take only the relevant lines - .take(self.line) - .enumerate() - .map(|(idx, s)| { - if idx == self.line - 1 { - // Last line: only characters until the column of the error are relevant. - // Note: Rust uses bytes whereas Python uses chars: we hence cannot - // directly use the `column` field - if self.column == 0 { return 0; } - - // Find a column we can safely slice on - let mut column = self.column - 1; - while column > 0 && !s.is_char_boundary(column) { - column -= 1; - } - - let chars_count = s[..column].chars().count(); - if chars_count == s.chars().count() - 1 { - chars_count + 1 - } else { - chars_count - } - } else { - // Other lines - s.chars().count() - } - }) - .sum::() - // add missed '\n' characters - + (self.line - 1); - val as i64 - } -} diff --git a/vendor/orjson/src/deserialize/mod.rs b/vendor/orjson/src/deserialize/mod.rs deleted file mode 100644 index 0f1484d533..0000000000 --- a/vendor/orjson/src/deserialize/mod.rs +++ /dev/null @@ -1,12 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -mod backend; -mod cache; -mod deserializer; -mod error; -mod pyobject; -mod utf8; - -pub use cache::{KeyMap, KEY_MAP}; -pub use deserializer::deserialize; -pub use error::DeserializeError; diff --git a/vendor/orjson/src/deserialize/pyobject.rs b/vendor/orjson/src/deserialize/pyobject.rs deleted file mode 100644 index 9ad9b09057..0000000000 --- a/vendor/orjson/src/deserialize/pyobject.rs +++ /dev/null @@ -1,71 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::deserialize::cache::*; -use crate::str::{hash_str, unicode_from_str}; -use crate::typeref::{FALSE, NONE, TRUE}; -use core::ptr::NonNull; - -#[inline(always)] -pub fn get_unicode_key(key_str: &str) -> *mut pyo3_ffi::PyObject { - if unlikely!(key_str.len() > 64) { - let pyob = unicode_from_str(key_str); - hash_str(pyob); - pyob - } else { - let hash = cache_hash(key_str.as_bytes()); - unsafe { - let entry = KEY_MAP - .get_mut() - .unwrap_or_else(|| unreachable!()) - .entry(&hash) - .or_insert_with( - || hash, - || { - let pyob = unicode_from_str(key_str); - hash_str(pyob); - CachedKey::new(pyob) - }, - ); - entry.get() - } - } -} - -#[allow(dead_code)] -#[inline(always)] -pub fn parse_bool(val: bool) -> NonNull { - if val { - parse_true() - } else { - parse_false() - } -} - -#[inline(always)] -pub fn parse_true() -> NonNull { - nonnull!(use_immortal!(TRUE)) -} - -#[inline(always)] -pub fn parse_false() -> NonNull { - nonnull!(use_immortal!(FALSE)) -} -#[inline(always)] -pub fn parse_i64(val: i64) -> NonNull { - nonnull!(ffi!(PyLong_FromLongLong(val))) -} - -#[inline(always)] -pub fn parse_u64(val: u64) -> NonNull { - nonnull!(ffi!(PyLong_FromUnsignedLongLong(val))) -} - -#[inline(always)] -pub fn parse_f64(val: f64) -> NonNull { - nonnull!(ffi!(PyFloat_FromDouble(val))) -} - -#[inline(always)] -pub fn parse_none() -> NonNull { - nonnull!(use_immortal!(NONE)) -} diff --git a/vendor/orjson/src/deserialize/utf8.rs b/vendor/orjson/src/deserialize/utf8.rs deleted file mode 100644 index 2d2842c6ea..0000000000 --- a/vendor/orjson/src/deserialize/utf8.rs +++ /dev/null @@ -1,92 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::deserialize::DeserializeError; -use crate::ffi::*; -use crate::str::unicode_to_str; -use crate::typeref::{BYTEARRAY_TYPE, BYTES_TYPE, MEMORYVIEW_TYPE, STR_TYPE}; -use crate::util::INVALID_STR; -use core::ffi::c_char; -use std::borrow::Cow; - -#[cfg(all(target_arch = "x86_64", not(target_feature = "avx2")))] -fn is_valid_utf8(buf: &[u8]) -> bool { - if std::is_x86_feature_detected!("avx2") { - unsafe { simdutf8::basic::imp::x86::avx2::validate_utf8(buf).is_ok() } - } else { - encoding_rs::Encoding::utf8_valid_up_to(buf) == buf.len() - } -} - -#[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] -fn is_valid_utf8(buf: &[u8]) -> bool { - simdutf8::basic::from_utf8(buf).is_ok() -} - -#[cfg(target_arch = "aarch64")] -fn is_valid_utf8(buf: &[u8]) -> bool { - simdutf8::basic::from_utf8(buf).is_ok() -} - -#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] -fn is_valid_utf8(buf: &[u8]) -> bool { - std::str::from_utf8(buf).is_ok() -} - -pub fn read_input_to_buf( - ptr: *mut pyo3_ffi::PyObject, -) -> Result<&'static [u8], DeserializeError<'static>> { - let obj_type_ptr = ob_type!(ptr); - let buffer: &[u8]; - if is_type!(obj_type_ptr, BYTES_TYPE) { - buffer = unsafe { - core::slice::from_raw_parts( - PyBytes_AS_STRING(ptr) as *const u8, - PyBytes_GET_SIZE(ptr) as usize, - ) - }; - if !is_valid_utf8(buffer) { - return Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))); - } - } else if is_type!(obj_type_ptr, STR_TYPE) { - let uni = unicode_to_str(ptr); - if unlikely!(uni.is_none()) { - return Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))); - } - let as_str = uni.unwrap(); - buffer = unsafe { core::slice::from_raw_parts(as_str.as_ptr(), as_str.len()) }; - } else if unlikely!(is_type!(obj_type_ptr, MEMORYVIEW_TYPE)) { - let membuf = unsafe { PyMemoryView_GET_BUFFER(ptr) }; - if unsafe { pyo3_ffi::PyBuffer_IsContiguous(membuf, b'C' as c_char) == 0 } { - return Err(DeserializeError::invalid(Cow::Borrowed( - "Input type memoryview must be a C contiguous buffer", - ))); - } - buffer = unsafe { - core::slice::from_raw_parts((*membuf).buf as *const u8, (*membuf).len as usize) - }; - if !is_valid_utf8(buffer) { - return Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))); - } - } else if unlikely!(is_type!(obj_type_ptr, BYTEARRAY_TYPE)) { - buffer = unsafe { - core::slice::from_raw_parts( - ffi!(PyByteArray_AsString(ptr)) as *const u8, - ffi!(PyByteArray_Size(ptr)) as usize, - ) - }; - if !is_valid_utf8(buffer) { - return Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))); - } - } else { - return Err(DeserializeError::invalid(Cow::Borrowed( - "Input must be bytes, bytearray, memoryview, or str", - ))); - } - if unlikely!(buffer.is_empty()) { - Err(DeserializeError::invalid(Cow::Borrowed( - "Input is a zero-length, empty document", - ))) - } else { - Ok(buffer) - } -} diff --git a/vendor/orjson/src/ffi/buffer.rs b/vendor/orjson/src/ffi/buffer.rs deleted file mode 100644 index ab0b800692..0000000000 --- a/vendor/orjson/src/ffi/buffer.rs +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::c_int; -use pyo3_ffi::*; - -#[repr(C)] -pub struct _PyManagedBufferObject { - pub ob_base: *mut pyo3_ffi::PyObject, - pub flags: c_int, - pub exports: Py_ssize_t, - pub master: *mut Py_buffer, -} - -#[repr(C)] -pub struct PyMemoryViewObject { - pub ob_base: PyVarObject, - pub mbuf: *mut _PyManagedBufferObject, - pub hash: Py_hash_t, - pub flags: c_int, - pub exports: Py_ssize_t, - pub view: Py_buffer, - pub weakreflist: *mut PyObject, - pub ob_array: [Py_ssize_t; 1], -} - -#[allow(non_snake_case)] -#[inline(always)] -pub unsafe fn PyMemoryView_GET_BUFFER(op: *mut PyObject) -> *const Py_buffer { - &(*op.cast::()).view -} diff --git a/vendor/orjson/src/ffi/bytes.rs b/vendor/orjson/src/ffi/bytes.rs deleted file mode 100644 index f59397ea29..0000000000 --- a/vendor/orjson/src/ffi/bytes.rs +++ /dev/null @@ -1,16 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::c_char; -use pyo3_ffi::{PyBytesObject, PyObject, PyVarObject, Py_ssize_t}; - -#[allow(non_snake_case)] -#[inline(always)] -pub unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char { - &(*op.cast::()).ob_sval as *const c_char -} - -#[allow(non_snake_case)] -#[inline(always)] -pub unsafe fn PyBytes_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { - (*op.cast::()).ob_size -} diff --git a/vendor/orjson/src/ffi/fragment.rs b/vendor/orjson/src/ffi/fragment.rs deleted file mode 100644 index 9cfd4d7f2a..0000000000 --- a/vendor/orjson/src/ffi/fragment.rs +++ /dev/null @@ -1,139 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::{c_char, c_ulong}; -use core::ptr::null_mut; -use pyo3_ffi::*; - -// https://docs.python.org/3/c-api/typeobj.html#typedef-examples - -#[repr(C)] -pub struct Fragment { - pub ob_refcnt: pyo3_ffi::Py_ssize_t, - pub ob_type: *mut pyo3_ffi::PyTypeObject, - pub contents: *mut pyo3_ffi::PyObject, -} - -#[cold] -#[inline(never)] -#[cfg_attr(feature = "optimize", optimize(size))] -fn raise_args_exception() -> *mut PyObject { - unsafe { - let msg = "orjson.Fragment() takes exactly 1 positional argument"; - let err_msg = - PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize); - PyErr_SetObject(PyExc_TypeError, err_msg); - Py_DECREF(err_msg); - }; - null_mut() -} - -#[no_mangle] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub unsafe extern "C" fn orjson_fragment_tp_new( - _subtype: *mut PyTypeObject, - args: *mut PyObject, - kwds: *mut PyObject, -) -> *mut PyObject { - if Py_SIZE(args) != 1 || !kwds.is_null() { - raise_args_exception(); - null_mut() - } else { - let contents = PyTuple_GET_ITEM(args, 0); - Py_INCREF(contents); - let obj = Box::new(Fragment { - ob_refcnt: 1, - ob_type: crate::typeref::FRAGMENT_TYPE, - contents: contents, - }); - Box::into_raw(obj) as *mut PyObject - } -} - -#[no_mangle] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub unsafe extern "C" fn orjson_fragment_dealloc(object: *mut PyObject) { - Py_DECREF((*(object as *mut Fragment)).contents); - std::alloc::dealloc(object as *mut u8, std::alloc::Layout::new::()); -} - -#[cfg(Py_3_10)] -const FRAGMENT_TP_FLAGS: c_ulong = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE; - -#[cfg(not(Py_3_10))] -const FRAGMENT_TP_FLAGS: c_ulong = Py_TPFLAGS_DEFAULT; - -#[no_mangle] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject { - let ob = Box::new(PyTypeObject { - ob_base: PyVarObject { - ob_base: PyObject { - #[cfg(Py_3_12)] - ob_refcnt: pyo3_ffi::PyObjectObRefcnt { ob_refcnt: 0 }, - #[cfg(not(Py_3_12))] - ob_refcnt: 0, - ob_type: core::ptr::addr_of_mut!(PyType_Type), - }, - ob_size: 0, - }, - tp_name: "orjson.Fragment\0".as_ptr() as *const c_char, - tp_basicsize: core::mem::size_of::() as isize, - tp_itemsize: 0, - tp_dealloc: Some(orjson_fragment_dealloc), - tp_init: None, - tp_new: Some(orjson_fragment_tp_new), - tp_flags: FRAGMENT_TP_FLAGS, - // ... - tp_bases: null_mut(), - tp_cache: null_mut(), - tp_del: None, - tp_finalize: None, - tp_free: None, - tp_is_gc: None, - tp_mro: null_mut(), - tp_subclasses: null_mut(), - tp_vectorcall: None, - tp_version_tag: 0, - tp_weaklist: null_mut(), - #[cfg(not(Py_3_9))] - tp_print: None, - tp_vectorcall_offset: 0, - tp_getattr: None, - tp_setattr: None, - tp_as_async: null_mut(), - tp_repr: None, - tp_as_number: null_mut(), - tp_as_sequence: null_mut(), - tp_as_mapping: null_mut(), - tp_hash: None, - tp_call: None, - tp_str: None, - tp_getattro: None, - tp_setattro: None, - tp_as_buffer: null_mut(), - tp_doc: core::ptr::null_mut(), - tp_traverse: None, - tp_clear: None, - tp_richcompare: None, - tp_weaklistoffset: 0, - tp_iter: None, - tp_iternext: None, - tp_methods: null_mut(), - tp_members: null_mut(), - tp_getset: null_mut(), - tp_base: null_mut(), - tp_dict: null_mut(), - tp_descr_get: None, - tp_descr_set: None, - tp_dictoffset: 0, - tp_alloc: None, - #[cfg(Py_3_12)] - tp_watched: 0, - }); - let ob_ptr = Box::into_raw(ob); - PyType_Ready(ob_ptr); - ob_ptr -} diff --git a/vendor/orjson/src/ffi/long.rs b/vendor/orjson/src/ffi/long.rs deleted file mode 100644 index 1ce944ed06..0000000000 --- a/vendor/orjson/src/ffi/long.rs +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -// longintrepr.h, _longobject, _PyLongValue - -#[allow(dead_code)] -#[cfg(Py_3_12)] -#[allow(non_upper_case_globals)] -const SIGN_MASK: usize = 3; - -#[cfg(all(Py_3_12, feature = "inline_int"))] -#[allow(non_upper_case_globals)] -const SIGN_ZERO: usize = 1; - -#[cfg(all(Py_3_12, feature = "inline_int"))] -#[allow(non_upper_case_globals)] -const NON_SIZE_BITS: usize = 3; - -#[cfg(Py_3_12)] -#[repr(C)] -pub struct _PyLongValue { - pub lv_tag: usize, - pub ob_digit: u32, -} - -#[cfg(Py_3_12)] -#[repr(C)] -pub struct PyLongObject { - pub ob_base: pyo3_ffi::PyObject, - pub long_value: _PyLongValue, -} - -#[allow(dead_code)] -#[cfg(not(Py_3_12))] -#[repr(C)] -pub struct PyLongObject { - pub ob_base: pyo3_ffi::PyVarObject, - pub ob_digit: u32, -} - -#[cfg(Py_3_12)] -#[inline(always)] -pub fn pylong_is_unsigned(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { (*(ptr as *mut PyLongObject)).long_value.lv_tag & SIGN_MASK == 0 } -} - -#[cfg(not(Py_3_12))] -#[inline(always)] -pub fn pylong_is_unsigned(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { (*(ptr as *mut pyo3_ffi::PyVarObject)).ob_size > 0 } -} - -#[cfg(all(Py_3_12, feature = "inline_int"))] -#[inline(always)] -pub fn pylong_fits_in_i32(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { (*(ptr as *mut PyLongObject)).long_value.lv_tag < (2 << NON_SIZE_BITS) } -} - -#[cfg(all(not(Py_3_12), feature = "inline_int"))] -#[inline(always)] -pub fn pylong_fits_in_i32(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { isize::abs((*(ptr as *mut pyo3_ffi::PyVarObject)).ob_size) == 1 } -} - -#[cfg(all(Py_3_12, feature = "inline_int"))] -#[inline(always)] -pub fn pylong_is_zero(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { (*(ptr as *mut PyLongObject)).long_value.lv_tag & SIGN_MASK == SIGN_ZERO } -} - -#[cfg(all(not(Py_3_12), feature = "inline_int"))] -#[inline(always)] -pub fn pylong_is_zero(ptr: *mut pyo3_ffi::PyObject) -> bool { - unsafe { (*(ptr as *mut pyo3_ffi::PyVarObject)).ob_size == 0 } -} - -#[cfg(all(Py_3_12, feature = "inline_int"))] -#[inline(always)] -pub fn pylong_get_inline_value(ptr: *mut pyo3_ffi::PyObject) -> i64 { - unsafe { - if pylong_is_unsigned(ptr) { - (*(ptr as *mut PyLongObject)).long_value.ob_digit as i64 - } else { - -1 * (*(ptr as *mut PyLongObject)).long_value.ob_digit as i64 - } - } -} - -#[cfg(all(not(Py_3_12), feature = "inline_int"))] -#[inline(always)] -pub fn pylong_get_inline_value(ptr: *mut pyo3_ffi::PyObject) -> i64 { - unsafe { - (*(ptr as *mut pyo3_ffi::PyVarObject)).ob_size as i64 - * (*(ptr as *mut PyLongObject)).ob_digit as i64 - } -} diff --git a/vendor/orjson/src/ffi/mod.rs b/vendor/orjson/src/ffi/mod.rs deleted file mode 100644 index 4f5489a9d1..0000000000 --- a/vendor/orjson/src/ffi/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -mod buffer; -mod bytes; -mod fragment; -mod long; -#[cfg(feature = "yyjson")] -pub mod yyjson; - -pub use buffer::*; -pub use bytes::*; -pub use fragment::{orjson_fragmenttype_new, Fragment}; -pub use long::pylong_is_unsigned; -#[cfg(feature = "inline_int")] -pub use long::{pylong_fits_in_i32, pylong_get_inline_value, pylong_is_zero}; diff --git a/vendor/orjson/src/ffi/yyjson.rs b/vendor/orjson/src/ffi/yyjson.rs deleted file mode 100644 index 73b958f5a1..0000000000 --- a/vendor/orjson/src/ffi/yyjson.rs +++ /dev/null @@ -1,68 +0,0 @@ -#[repr(C)] -pub struct yyjson_alc { - pub malloc: ::core::option::Option< - unsafe extern "C" fn( - ctx: *mut ::core::ffi::c_void, - size: usize, - ) -> *mut ::core::ffi::c_void, - >, - pub realloc: ::core::option::Option< - unsafe extern "C" fn( - ctx: *mut ::core::ffi::c_void, - ptr: *mut ::core::ffi::c_void, - size: usize, - ) -> *mut ::core::ffi::c_void, - >, - pub free: ::core::option::Option< - unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, ptr: *mut ::core::ffi::c_void), - >, - pub ctx: *mut ::core::ffi::c_void, -} -extern "C" { - pub fn yyjson_alc_pool_init( - alc: *mut yyjson_alc, - buf: *mut ::core::ffi::c_void, - size: usize, - ) -> bool; -} -pub type yyjson_read_code = u32; -pub const YYJSON_READ_SUCCESS: yyjson_read_code = 0; -#[repr(C)] -pub struct yyjson_read_err { - pub code: yyjson_read_code, - pub msg: *const ::core::ffi::c_char, - pub pos: usize, -} -extern "C" { - pub fn yyjson_read_opts( - dat: *mut ::core::ffi::c_char, - len: usize, - alc: *const yyjson_alc, - err: *mut yyjson_read_err, - ) -> *mut yyjson_doc; -} -extern "C" { - pub fn yyjson_doc_free(doc: *mut yyjson_doc); -} -#[repr(C)] -pub union yyjson_val_uni { - pub u64_: u64, - pub i64_: i64, - pub f64_: f64, - pub str_: *const ::core::ffi::c_char, - pub ptr: *mut ::core::ffi::c_void, - pub ofs: usize, -} -#[repr(C)] -pub struct yyjson_val { - pub tag: u64, - pub uni: yyjson_val_uni, -} -#[repr(C)] -pub struct yyjson_doc { - pub root: *mut yyjson_val, - pub alc: yyjson_alc, - pub dat_read: usize, - pub val_read: usize, - pub str_pool: *mut ::core::ffi::c_char, -} diff --git a/vendor/orjson/src/lib.rs b/vendor/orjson/src/lib.rs deleted file mode 100644 index 8627647f5f..0000000000 --- a/vendor/orjson/src/lib.rs +++ /dev/null @@ -1,391 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -#![cfg_attr(feature = "avx512", feature(stdarch_x86_avx512, avx512_target_feature))] -#![cfg_attr(feature = "intrinsics", feature(core_intrinsics))] -#![cfg_attr(feature = "optimize", feature(optimize_attribute))] -#![cfg_attr(feature = "unstable-simd", feature(portable_simd))] -#![allow(internal_features)] // core_intrinsics -#![allow(non_camel_case_types)] -#![allow(static_mut_refs)] -#![allow(unknown_lints)] // internal_features -#![allow(unused_unsafe)] -#![allow(clippy::missing_safety_doc)] -#![allow(clippy::redundant_field_names)] -#![allow(clippy::uninlined_format_args)] // MSRV 1.66 -#![allow(clippy::upper_case_acronyms)] -#![allow(clippy::zero_prefixed_literal)] - -#[cfg(feature = "unwind")] -extern crate unwinding; - -#[macro_use] -mod util; - -mod deserialize; -mod ffi; -mod opt; -mod serialize; -mod str; -mod typeref; - -pub use typeref::init_typerefs; -pub use serialize::{to_writer, WriteExt, PyObjectSerializer, SerializerState}; - -use core::ffi::{c_char, c_int, c_void}; -use pyo3_ffi::*; - -#[allow(unused_imports)] -use core::ptr::{null, null_mut, NonNull}; - -#[cfg(Py_3_13)] -macro_rules! add { - ($mptr:expr, $name:expr, $obj:expr) => { - PyModule_Add($mptr, $name.as_ptr() as *const c_char, $obj); - }; -} - -#[cfg(all(Py_3_10, not(Py_3_13)))] -macro_rules! add { - ($mptr:expr, $name:expr, $obj:expr) => { - PyModule_AddObjectRef($mptr, $name.as_ptr() as *const c_char, $obj); - }; -} - -#[cfg(not(Py_3_10))] -macro_rules! add { - ($mptr:expr, $name:expr, $obj:expr) => { - PyModule_AddObject($mptr, $name.as_ptr() as *const c_char, $obj); - }; -} - -macro_rules! opt { - ($mptr:expr, $name:expr, $opt:expr) => { - #[cfg(all(not(target_os = "windows"), target_pointer_width = "64"))] - PyModule_AddIntConstant($mptr, $name.as_ptr() as *const c_char, $opt as i64); - #[cfg(all(not(target_os = "windows"), target_pointer_width = "32"))] - PyModule_AddIntConstant($mptr, $name.as_ptr() as *const c_char, $opt as i32); - #[cfg(target_os = "windows")] - PyModule_AddIntConstant($mptr, $name.as_ptr() as *const c_char, $opt as i32); - }; -} - -#[allow(non_snake_case)] -#[no_mangle] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub unsafe extern "C" fn orjson_init_exec(mptr: *mut PyObject) -> c_int { - typeref::init_typerefs(); - { - let version = env!("CARGO_PKG_VERSION"); - let pyversion = - PyUnicode_FromStringAndSize(version.as_ptr() as *const c_char, version.len() as isize); - add!(mptr, "__version__\0", pyversion); - } - { - let dumps_doc = - "dumps(obj, /, default=None, option=None)\n--\n\nSerialize Python objects to JSON.\0"; - - let wrapped_dumps = PyMethodDef { - ml_name: "dumps\0".as_ptr() as *const c_char, - ml_meth: PyMethodDefPointer { - #[cfg(Py_3_10)] - PyCFunctionFastWithKeywords: dumps, - #[cfg(not(Py_3_10))] - _PyCFunctionFastWithKeywords: dumps, - }, - ml_flags: pyo3_ffi::METH_FASTCALL | METH_KEYWORDS, - ml_doc: dumps_doc.as_ptr() as *const c_char, - }; - - let func = PyCFunction_NewEx( - Box::into_raw(Box::new(wrapped_dumps)), - null_mut(), - PyUnicode_InternFromString("orjson\0".as_ptr() as *const c_char), - ); - add!(mptr, "dumps\0", func); - } - - { - let loads_doc = "loads(obj, /)\n--\n\nDeserialize JSON to Python objects.\0"; - - let wrapped_loads = PyMethodDef { - ml_name: "loads\0".as_ptr() as *const c_char, - ml_meth: PyMethodDefPointer { PyCFunction: loads }, - ml_flags: METH_O, - ml_doc: loads_doc.as_ptr() as *const c_char, - }; - let func = PyCFunction_NewEx( - Box::into_raw(Box::new(wrapped_loads)), - null_mut(), - PyUnicode_InternFromString("orjson\0".as_ptr() as *const c_char), - ); - add!(mptr, "loads\0", func); - } - - add!(mptr, "Fragment\0", typeref::FRAGMENT_TYPE as *mut PyObject); - - opt!(mptr, "OPT_APPEND_NEWLINE\0", opt::APPEND_NEWLINE); - opt!(mptr, "OPT_INDENT_2\0", opt::INDENT_2); - opt!(mptr, "OPT_NAIVE_UTC\0", opt::NAIVE_UTC); - opt!(mptr, "OPT_NON_STR_KEYS\0", opt::NON_STR_KEYS); - opt!(mptr, "OPT_OMIT_MICROSECONDS\0", opt::OMIT_MICROSECONDS); - opt!( - mptr, - "OPT_PASSTHROUGH_DATACLASS\0", - opt::PASSTHROUGH_DATACLASS - ); - opt!( - mptr, - "OPT_PASSTHROUGH_DATETIME\0", - opt::PASSTHROUGH_DATETIME - ); - opt!( - mptr, - "OPT_PASSTHROUGH_SUBCLASS\0", - opt::PASSTHROUGH_SUBCLASS - ); - opt!(mptr, "OPT_SERIALIZE_DATACLASS\0", opt::SERIALIZE_DATACLASS); - opt!(mptr, "OPT_SERIALIZE_NUMPY\0", opt::SERIALIZE_NUMPY); - opt!(mptr, "OPT_SERIALIZE_UUID\0", opt::SERIALIZE_UUID); - opt!(mptr, "OPT_SORT_KEYS\0", opt::SORT_KEYS); - opt!(mptr, "OPT_STRICT_INTEGER\0", opt::STRICT_INTEGER); - opt!(mptr, "OPT_UTC_Z\0", opt::UTC_Z); - - add!(mptr, "JSONDecodeError\0", typeref::JsonDecodeError); - add!(mptr, "JSONEncodeError\0", typeref::JsonEncodeError); - - 0 -} - -#[cfg(Py_3_13)] -#[allow(non_upper_case_globals)] -const Py_mod_gil: c_int = 4; -#[cfg(Py_3_13)] -#[allow(non_upper_case_globals, dead_code, fuzzy_provenance_casts)] -const Py_MOD_GIL_USED: *mut c_void = 0 as *mut c_void; -#[cfg(Py_3_13)] -#[allow(non_upper_case_globals, dead_code, fuzzy_provenance_casts)] -const Py_MOD_GIL_NOT_USED: *mut c_void = 1 as *mut c_void; - -#[cfg(not(Py_3_12))] -const PYMODULEDEF_LEN: usize = 2; -#[cfg(all(Py_3_12, not(Py_3_13)))] -const PYMODULEDEF_LEN: usize = 3; -#[cfg(Py_3_13)] -const PYMODULEDEF_LEN: usize = 4; - -#[allow(non_snake_case)] -#[no_mangle] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub unsafe extern "C" fn PyInit_orjson() -> *mut PyModuleDef { - let mod_slots: Box<[PyModuleDef_Slot; PYMODULEDEF_LEN]> = Box::new([ - PyModuleDef_Slot { - slot: Py_mod_exec, - value: orjson_init_exec as *mut c_void, - }, - #[cfg(Py_3_12)] - PyModuleDef_Slot { - slot: Py_mod_multiple_interpreters, - value: Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED, - }, - #[cfg(Py_3_13)] - PyModuleDef_Slot { - slot: Py_mod_gil, - value: Py_MOD_GIL_USED, - }, - PyModuleDef_Slot { - slot: 0, - value: null_mut(), - }, - ]); - - let init = Box::new(PyModuleDef { - m_base: PyModuleDef_HEAD_INIT, - m_name: "orjson\0".as_ptr() as *const c_char, - m_doc: null(), - m_size: 0, - m_methods: null_mut(), - m_slots: Box::into_raw(mod_slots) as *mut PyModuleDef_Slot, - m_traverse: None, - m_clear: None, - m_free: None, - }); - let init_ptr = Box::into_raw(init); - PyModuleDef_Init(init_ptr); - init_ptr -} - -#[cold] -#[inline(never)] -#[cfg_attr(feature = "optimize", optimize(size))] -fn raise_loads_exception(err: deserialize::DeserializeError) -> *mut PyObject { - let pos = err.pos(); - let msg = err.message; - let doc = match err.data { - Some(as_str) => unsafe { - PyUnicode_FromStringAndSize(as_str.as_ptr() as *const c_char, as_str.len() as isize) - }, - None => { - use_immortal!(crate::typeref::EMPTY_UNICODE) - } - }; - unsafe { - let err_msg = - PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize); - let args = PyTuple_New(3); - let pos = PyLong_FromLongLong(pos); - PyTuple_SET_ITEM(args, 0, err_msg); - PyTuple_SET_ITEM(args, 1, doc); - PyTuple_SET_ITEM(args, 2, pos); - PyErr_SetObject(typeref::JsonDecodeError, args); - debug_assert!(ffi!(Py_REFCNT(args)) <= 2); - Py_DECREF(args); - }; - null_mut() -} - -#[cold] -#[inline(never)] -#[cfg_attr(feature = "optimize", optimize(size))] -fn raise_dumps_exception_fixed(msg: &str) -> *mut PyObject { - unsafe { - let err_msg = - PyUnicode_FromStringAndSize(msg.as_ptr() as *const c_char, msg.len() as isize); - PyErr_SetObject(typeref::JsonEncodeError, err_msg); - debug_assert!(ffi!(Py_REFCNT(err_msg)) <= 2); - Py_DECREF(err_msg); - }; - null_mut() -} - -#[cold] -#[inline(never)] -#[cfg_attr(feature = "optimize", optimize(size))] -#[cfg(Py_3_12)] -fn raise_dumps_exception_dynamic(err: &str) -> *mut PyObject { - unsafe { - let cause_exc: *mut PyObject = PyErr_GetRaisedException(); - - let err_msg = - PyUnicode_FromStringAndSize(err.as_ptr() as *const c_char, err.len() as isize); - PyErr_SetObject(typeref::JsonEncodeError, err_msg); - debug_assert!(ffi!(Py_REFCNT(err_msg)) <= 2); - Py_DECREF(err_msg); - - if !cause_exc.is_null() { - let exc: *mut PyObject = PyErr_GetRaisedException(); - PyException_SetCause(exc, cause_exc); - PyErr_SetRaisedException(exc); - } - }; - null_mut() -} - -#[cold] -#[inline(never)] -#[cfg_attr(feature = "optimize", optimize(size))] -#[cfg(not(Py_3_12))] -fn raise_dumps_exception_dynamic(err: &str) -> *mut PyObject { - unsafe { - let mut cause_tp: *mut PyObject = null_mut(); - let mut cause_val: *mut PyObject = null_mut(); - let mut cause_traceback: *mut PyObject = null_mut(); - PyErr_Fetch(&mut cause_tp, &mut cause_val, &mut cause_traceback); - - let err_msg = - PyUnicode_FromStringAndSize(err.as_ptr() as *const c_char, err.len() as isize); - PyErr_SetObject(typeref::JsonEncodeError, err_msg); - debug_assert!(ffi!(Py_REFCNT(err_msg)) == 2); - Py_DECREF(err_msg); - let mut tp: *mut PyObject = null_mut(); - let mut val: *mut PyObject = null_mut(); - let mut traceback: *mut PyObject = null_mut(); - PyErr_Fetch(&mut tp, &mut val, &mut traceback); - PyErr_NormalizeException(&mut tp, &mut val, &mut traceback); - - if !cause_tp.is_null() { - PyErr_NormalizeException(&mut cause_tp, &mut cause_val, &mut cause_traceback); - PyException_SetCause(val, cause_val); - Py_DECREF(cause_tp); - } - if !cause_traceback.is_null() { - Py_DECREF(cause_traceback); - } - - PyErr_Restore(tp, val, traceback); - }; - null_mut() -} - -#[no_mangle] -pub unsafe extern "C" fn loads(_self: *mut PyObject, obj: *mut PyObject) -> *mut PyObject { - match crate::deserialize::deserialize(obj) { - Ok(val) => val.as_ptr(), - Err(err) => raise_loads_exception(err), - } -} - -#[no_mangle] -pub unsafe extern "C" fn dumps( - _self: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, - kwnames: *mut PyObject, -) -> *mut PyObject { - let mut default: Option> = None; - let mut optsptr: Option> = None; - - let num_args = PyVectorcall_NARGS(nargs as usize); - if unlikely!(num_args == 0) { - return raise_dumps_exception_fixed( - "dumps() missing 1 required positional argument: 'obj'", - ); - } - if num_args & 2 == 2 { - default = Some(NonNull::new_unchecked(*args.offset(1))); - } - if num_args & 3 == 3 { - optsptr = Some(NonNull::new_unchecked(*args.offset(2))); - } - if unlikely!(!kwnames.is_null()) { - for i in 0..=Py_SIZE(kwnames).saturating_sub(1) { - let arg = PyTuple_GET_ITEM(kwnames, i as Py_ssize_t); - if arg == typeref::DEFAULT { - if unlikely!(num_args & 2 == 2) { - return raise_dumps_exception_fixed( - "dumps() got multiple values for argument: 'default'", - ); - } - default = Some(NonNull::new_unchecked(*args.offset(num_args + i))); - } else if arg == typeref::OPTION { - if unlikely!(num_args & 3 == 3) { - return raise_dumps_exception_fixed( - "dumps() got multiple values for argument: 'option'", - ); - } - optsptr = Some(NonNull::new_unchecked(*args.offset(num_args + i))); - } else { - return raise_dumps_exception_fixed("dumps() got an unexpected keyword argument"); - } - } - } - - let mut optsbits: i32 = 0; - if unlikely!(optsptr.is_some()) { - let opts = optsptr.unwrap(); - if (*opts.as_ptr()).ob_type == typeref::INT_TYPE { - optsbits = PyLong_AsLong(optsptr.unwrap().as_ptr()) as i32; - if unlikely!(!(0..=opt::MAX_OPT).contains(&optsbits)) { - return raise_dumps_exception_fixed("Invalid opts"); - } - } else if unlikely!(opts.as_ptr() != typeref::NONE) { - return raise_dumps_exception_fixed("Invalid opts"); - } - } - - match crate::serialize::serialize(*args, default, optsbits as opt::Opt) { - Ok(val) => val.as_ptr(), - Err(err) => raise_dumps_exception_dynamic(err.as_str()), - } -} diff --git a/vendor/orjson/src/opt.rs b/vendor/orjson/src/opt.rs deleted file mode 100644 index 4ceff66bc5..0000000000 --- a/vendor/orjson/src/opt.rs +++ /dev/null @@ -1,40 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -pub type Opt = u32; - -pub const INDENT_2: Opt = 1; -pub const NAIVE_UTC: Opt = 1 << 1; -pub const NON_STR_KEYS: Opt = 1 << 2; -pub const OMIT_MICROSECONDS: Opt = 1 << 3; -pub const SERIALIZE_NUMPY: Opt = 1 << 4; -pub const SORT_KEYS: Opt = 1 << 5; -pub const STRICT_INTEGER: Opt = 1 << 6; -pub const UTC_Z: Opt = 1 << 7; -pub const PASSTHROUGH_SUBCLASS: Opt = 1 << 8; -pub const PASSTHROUGH_DATETIME: Opt = 1 << 9; -pub const APPEND_NEWLINE: Opt = 1 << 10; -pub const PASSTHROUGH_DATACLASS: Opt = 1 << 11; - -// deprecated -pub const SERIALIZE_DATACLASS: Opt = 0; -pub const SERIALIZE_UUID: Opt = 0; - -pub const SORT_OR_NON_STR_KEYS: Opt = SORT_KEYS | NON_STR_KEYS; - -pub const NOT_PASSTHROUGH: Opt = - !(PASSTHROUGH_DATETIME | PASSTHROUGH_DATACLASS | PASSTHROUGH_SUBCLASS); - -pub const MAX_OPT: i32 = (APPEND_NEWLINE - | INDENT_2 - | NAIVE_UTC - | NON_STR_KEYS - | OMIT_MICROSECONDS - | PASSTHROUGH_DATETIME - | PASSTHROUGH_DATACLASS - | PASSTHROUGH_SUBCLASS - | SERIALIZE_DATACLASS - | SERIALIZE_NUMPY - | SERIALIZE_UUID - | SORT_KEYS - | STRICT_INTEGER - | UTC_Z) as i32; diff --git a/vendor/orjson/src/serialize/buffer.rs b/vendor/orjson/src/serialize/buffer.rs deleted file mode 100644 index 894ee837b8..0000000000 --- a/vendor/orjson/src/serialize/buffer.rs +++ /dev/null @@ -1,72 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::mem::MaybeUninit; - -const BUFFER_LENGTH: usize = 64 - core::mem::size_of::(); - -/// For use to serialize fixed-size UUIDs and DateTime. -#[repr(align(64))] -pub struct SmallFixedBuffer { - idx: usize, - bytes: [MaybeUninit; BUFFER_LENGTH], -} - -impl SmallFixedBuffer { - #[inline] - pub fn new() -> Self { - Self { - idx: 0, - bytes: [MaybeUninit::::uninit(); BUFFER_LENGTH], - } - } - #[inline] - pub unsafe fn as_mut_slice(&mut self) -> &mut [u8] { - unsafe { - core::slice::from_raw_parts_mut( - (core::ptr::addr_of_mut!(self.bytes) as *mut u8).add(self.idx), - BUFFER_LENGTH - self.idx, - ) - } - } - - #[inline] - pub unsafe fn set_written(&mut self, len: usize) { - debug_assert!(self.idx + len < BUFFER_LENGTH); - self.idx += len; - } - - #[inline] - pub fn push(&mut self, value: u8) { - debug_assert!(self.idx + 1 < BUFFER_LENGTH); - unsafe { - core::ptr::write( - (core::ptr::addr_of_mut!(self.bytes) as *mut u8).add(self.idx), - value, - ); - self.idx += 1; - }; - } - - #[inline] - pub fn extend_from_slice(&mut self, slice: &[u8]) { - debug_assert!(self.idx + slice.len() < BUFFER_LENGTH); - unsafe { - core::ptr::copy_nonoverlapping( - slice.as_ptr(), - (core::ptr::addr_of_mut!(self.bytes) as *mut u8).add(self.idx), - slice.len(), - ); - self.idx += slice.len(); - } - } - - #[inline] - pub fn as_ptr(&self) -> *const u8 { - core::ptr::addr_of!(self.bytes) as *const u8 - } - - #[inline] - pub fn len(&self) -> usize { - self.idx - } -} diff --git a/vendor/orjson/src/serialize/error.rs b/vendor/orjson/src/serialize/error.rs deleted file mode 100644 index 744404bed4..0000000000 --- a/vendor/orjson/src/serialize/error.rs +++ /dev/null @@ -1,65 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::CStr; -use core::ptr::NonNull; - -pub enum SerializeError { - DatetimeLibraryUnsupported, - DefaultRecursionLimit, - Integer53Bits, - Integer64Bits, - InvalidStr, - InvalidFragment, - KeyMustBeStr, - RecursionLimit, - TimeHasTzinfo, - DictIntegerKey64Bit, - DictKeyInvalidType, - NumpyMalformed, - NumpyNotCContiguous, - NumpyNotNativeEndian, - NumpyUnsupportedDatatype, - UnsupportedType(NonNull), -} - -impl std::fmt::Display for SerializeError { - #[cold] - #[cfg_attr(feature = "optimize", optimize(size))] - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match *self { - SerializeError::DatetimeLibraryUnsupported => write!(f, "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil"), - SerializeError::DefaultRecursionLimit => { - write!(f, "default serializer exceeds recursion limit") - } - SerializeError::Integer53Bits => write!(f, "Integer exceeds 53-bit range"), - SerializeError::Integer64Bits => write!(f, "Integer exceeds 64-bit range"), - SerializeError::InvalidStr => write!(f, "{}", crate::util::INVALID_STR), - SerializeError::InvalidFragment => write!(f, "orjson.Fragment's content is not of type bytes or str"), - SerializeError::KeyMustBeStr => write!(f, "Dict key must be str"), - SerializeError::RecursionLimit => write!(f, "Recursion limit reached"), - SerializeError::TimeHasTzinfo => write!(f, "datetime.time must not have tzinfo set"), - SerializeError::DictIntegerKey64Bit => { - write!(f, "Dict integer key must be within 64-bit range") - } - SerializeError::DictKeyInvalidType => { - write!(f, "Dict key must a type serializable with OPT_NON_STR_KEYS") - } - SerializeError::NumpyMalformed => write!(f, "numpy array is malformed"), - SerializeError::NumpyNotCContiguous => write!( - f, - "numpy array is not C contiguous; use ndarray.tolist() in default" - ), - SerializeError::NumpyNotNativeEndian => write!( - f, - "numpy array is not native-endianness" - ), - SerializeError::NumpyUnsupportedDatatype => { - write!(f, "unsupported datatype in numpy array") - } - SerializeError::UnsupportedType(ptr) => { - let name = unsafe { CStr::from_ptr((*ob_type!(ptr.as_ptr())).tp_name).to_string_lossy() }; - write!(f, "Type is not JSON serializable: {}", name) - } - } - } -} diff --git a/vendor/orjson/src/serialize/mod.rs b/vendor/orjson/src/serialize/mod.rs deleted file mode 100644 index 21aa2fabd5..0000000000 --- a/vendor/orjson/src/serialize/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -mod buffer; -mod error; -mod obtype; -mod per_type; -mod serializer; -mod state; -mod writer; - -pub use serializer::{serialize, PyObjectSerializer}; -pub use state::SerializerState; -pub use writer::{to_writer, WriteExt}; diff --git a/vendor/orjson/src/serialize/obtype.rs b/vendor/orjson/src/serialize/obtype.rs deleted file mode 100644 index e526855e56..0000000000 --- a/vendor/orjson/src/serialize/obtype.rs +++ /dev/null @@ -1,108 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::{ - Opt, PASSTHROUGH_DATACLASS, PASSTHROUGH_DATETIME, PASSTHROUGH_SUBCLASS, SERIALIZE_NUMPY, -}; -use crate::serialize::per_type::{is_numpy_array, is_numpy_scalar}; -use crate::typeref::{ - BOOL_TYPE, DATACLASS_FIELDS_STR, DATETIME_TYPE, DATE_TYPE, DICT_TYPE, ENUM_TYPE, FLOAT_TYPE, - FRAGMENT_TYPE, INT_TYPE, LIST_TYPE, NONE_TYPE, STR_TYPE, TIME_TYPE, TUPLE_TYPE, UUID_TYPE, -}; - -#[repr(u32)] -pub enum ObType { - Str, - Int, - Bool, - None, - Float, - List, - Dict, - Datetime, - Date, - Time, - Tuple, - Uuid, - Dataclass, - NumpyScalar, - NumpyArray, - Enum, - StrSubclass, - Fragment, - Unknown, -} - -pub fn pyobject_to_obtype(obj: *mut pyo3_ffi::PyObject, opts: Opt) -> ObType { - let ob_type = ob_type!(obj); - if is_class_by_type!(ob_type, STR_TYPE) { - ObType::Str - } else if is_class_by_type!(ob_type, INT_TYPE) { - ObType::Int - } else if is_class_by_type!(ob_type, BOOL_TYPE) { - ObType::Bool - } else if is_class_by_type!(ob_type, NONE_TYPE) { - ObType::None - } else if is_class_by_type!(ob_type, FLOAT_TYPE) { - ObType::Float - } else if is_class_by_type!(ob_type, LIST_TYPE) { - ObType::List - } else if is_class_by_type!(ob_type, DICT_TYPE) { - ObType::Dict - } else if is_class_by_type!(ob_type, DATETIME_TYPE) && opt_disabled!(opts, PASSTHROUGH_DATETIME) - { - ObType::Datetime - } else { - pyobject_to_obtype_unlikely(ob_type, opts) - } -} - -#[cfg_attr(feature = "optimize", optimize(size))] -#[inline(never)] -pub fn pyobject_to_obtype_unlikely(ob_type: *mut pyo3_ffi::PyTypeObject, opts: Opt) -> ObType { - if is_class_by_type!(ob_type, UUID_TYPE) { - return ObType::Uuid; - } else if is_class_by_type!(ob_type, TUPLE_TYPE) { - return ObType::Tuple; - } else if is_class_by_type!(ob_type, FRAGMENT_TYPE) { - return ObType::Fragment; - } - - if opt_disabled!(opts, PASSTHROUGH_DATETIME) { - if is_class_by_type!(ob_type, DATE_TYPE) { - return ObType::Date; - } else if is_class_by_type!(ob_type, TIME_TYPE) { - return ObType::Time; - } - } - - if opt_disabled!(opts, PASSTHROUGH_SUBCLASS) { - if is_subclass_by_flag!(ob_type, Py_TPFLAGS_UNICODE_SUBCLASS) { - return ObType::StrSubclass; - } else if is_subclass_by_flag!(ob_type, Py_TPFLAGS_LONG_SUBCLASS) { - return ObType::Int; - } else if is_subclass_by_flag!(ob_type, Py_TPFLAGS_LIST_SUBCLASS) { - return ObType::List; - } else if is_subclass_by_flag!(ob_type, Py_TPFLAGS_DICT_SUBCLASS) { - return ObType::Dict; - } - } - - if is_subclass_by_type!(ob_type, ENUM_TYPE) { - return ObType::Enum; - } - - if opt_disabled!(opts, PASSTHROUGH_DATACLASS) && pydict_contains!(ob_type, DATACLASS_FIELDS_STR) - { - return ObType::Dataclass; - } - - if unlikely!(opt_enabled!(opts, SERIALIZE_NUMPY)) { - if is_numpy_scalar(ob_type) { - return ObType::NumpyScalar; - } else if is_numpy_array(ob_type) { - return ObType::NumpyArray; - } - } - - ObType::Unknown -} diff --git a/vendor/orjson/src/serialize/per_type/dataclass.rs b/vendor/orjson/src/serialize/per_type/dataclass.rs deleted file mode 100644 index a5dc707a5e..0000000000 --- a/vendor/orjson/src/serialize/per_type/dataclass.rs +++ /dev/null @@ -1,207 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::error::SerializeError; -use crate::serialize::per_type::dict::ZeroDictSerializer; -use crate::serialize::serializer::PyObjectSerializer; -use crate::serialize::state::SerializerState; -use crate::str::unicode_to_str; -use crate::typeref::{ - DATACLASS_FIELDS_STR, DICT_STR, FIELD_TYPE, FIELD_TYPE_STR, SLOTS_STR, STR_TYPE, -}; - -use serde::ser::{Serialize, SerializeMap, Serializer}; - -use core::ptr::NonNull; - -#[repr(transparent)] -pub struct DataclassGenericSerializer<'a> { - previous: &'a PyObjectSerializer, -} - -impl<'a> DataclassGenericSerializer<'a> { - pub fn new(previous: &'a PyObjectSerializer) -> Self { - Self { previous: previous } - } -} - -impl<'a> Serialize for DataclassGenericSerializer<'a> { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - if unlikely!(self.previous.state.recursion_limit()) { - err!(SerializeError::RecursionLimit) - } - let dict = ffi!(PyObject_GetAttr(self.previous.ptr, DICT_STR)); - let ob_type = ob_type!(self.previous.ptr); - if unlikely!(dict.is_null()) { - ffi!(PyErr_Clear()); - DataclassFallbackSerializer::new( - self.previous.ptr, - self.previous.state, - self.previous.default, - ) - .serialize(serializer) - } else if pydict_contains!(ob_type, SLOTS_STR) { - let ret = DataclassFallbackSerializer::new( - self.previous.ptr, - self.previous.state, - self.previous.default, - ) - .serialize(serializer); - ffi!(Py_DECREF(dict)); - ret - } else { - let ret = - DataclassFastSerializer::new(dict, self.previous.state, self.previous.default) - .serialize(serializer); - ffi!(Py_DECREF(dict)); - ret - } - } -} - -pub struct DataclassFastSerializer { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, -} - -impl DataclassFastSerializer { - pub fn new( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - DataclassFastSerializer { - ptr: ptr, - state: state.copy_for_recursive_call(), - default: default, - } - } -} - -impl Serialize for DataclassFastSerializer { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let len = ffi!(Py_SIZE(self.ptr)) as usize; - if unlikely!(len == 0) { - return ZeroDictSerializer::new().serialize(serializer); - } - let mut map = serializer.serialize_map(None).unwrap(); - - let mut pos = 0; - let mut next_key: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - let mut next_value: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - for _ in 0..ffi!(Py_SIZE(self.ptr)) as usize { - let key = next_key; - let value = next_value; - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - let key_as_str = { - let key_ob_type = ob_type!(key); - if unlikely!(!is_class_by_type!(key_ob_type, STR_TYPE)) { - err!(SerializeError::KeyMustBeStr) - } - let tmp = unicode_to_str(key); - if unlikely!(tmp.is_none()) { - err!(SerializeError::InvalidStr) - }; - tmp.unwrap() - }; - if unlikely!(key_as_str.as_bytes()[0] == b'_') { - continue; - } - let pyvalue = PyObjectSerializer::new(value, self.state, self.default); - map.serialize_key(key_as_str).unwrap(); - map.serialize_value(&pyvalue)?; - } - map.end() - } -} - -pub struct DataclassFallbackSerializer { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, -} - -impl DataclassFallbackSerializer { - pub fn new( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - DataclassFallbackSerializer { - ptr: ptr, - state: state.copy_for_recursive_call(), - default: default, - } - } -} - -impl Serialize for DataclassFallbackSerializer { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let fields = ffi!(PyObject_GetAttr(self.ptr, DATACLASS_FIELDS_STR)); - debug_assert!(ffi!(Py_REFCNT(fields)) >= 2); - ffi!(Py_DECREF(fields)); - let len = ffi!(Py_SIZE(fields)) as usize; - if unlikely!(len == 0) { - return ZeroDictSerializer::new().serialize(serializer); - } - let mut map = serializer.serialize_map(None).unwrap(); - - let mut pos = 0; - let mut next_key: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - let mut next_value: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - - pydict_next!(fields, &mut pos, &mut next_key, &mut next_value); - - for _ in 0..ffi!(Py_SIZE(fields)) as usize { - let attr = next_key; - let field = next_value; - - pydict_next!(fields, &mut pos, &mut next_key, &mut next_value); - - let field_type = ffi!(PyObject_GetAttr(field, FIELD_TYPE_STR)); - debug_assert!(ffi!(Py_REFCNT(field_type)) >= 2); - ffi!(Py_DECREF(field_type)); - if unsafe { field_type as *mut pyo3_ffi::PyTypeObject != FIELD_TYPE } { - continue; - } - - let key_as_str = { - let tmp = unicode_to_str(attr); - if unlikely!(tmp.is_none()) { - err!(SerializeError::InvalidStr) - }; - tmp.unwrap() - }; - if key_as_str.as_bytes()[0] == b'_' { - continue; - } - - let value = ffi!(PyObject_GetAttr(self.ptr, attr)); - debug_assert!(ffi!(Py_REFCNT(value)) >= 2); - ffi!(Py_DECREF(value)); - let pyvalue = PyObjectSerializer::new(value, self.state, self.default); - - map.serialize_key(key_as_str).unwrap(); - map.serialize_value(&pyvalue)? - } - map.end() - } -} diff --git a/vendor/orjson/src/serialize/per_type/datetime.rs b/vendor/orjson/src/serialize/per_type/datetime.rs deleted file mode 100644 index 634c40fcb4..0000000000 --- a/vendor/orjson/src/serialize/per_type/datetime.rs +++ /dev/null @@ -1,247 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::*; -use crate::serialize::buffer::SmallFixedBuffer; -use crate::serialize::error::SerializeError; -use crate::serialize::per_type::datetimelike::{DateTimeError, DateTimeLike, Offset}; -#[cfg(Py_3_9)] -use crate::typeref::ZONEINFO_TYPE; -use crate::typeref::{CONVERT_METHOD_STR, DST_STR, NORMALIZE_METHOD_STR, UTCOFFSET_METHOD_STR}; -use serde::ser::{Serialize, Serializer}; - -macro_rules! write_double_digit { - ($buf:ident, $value:ident) => { - if $value < 10 { - $buf.push(b'0'); - } - $buf.extend_from_slice(itoa::Buffer::new().format($value).as_bytes()); - }; -} - -macro_rules! write_microsecond { - ($buf:ident, $microsecond:ident) => { - if $microsecond != 0 { - let mut buf = itoa::Buffer::new(); - let formatted = buf.format($microsecond); - $buf.extend_from_slice( - &[b'.', b'0', b'0', b'0', b'0', b'0', b'0'][..(7 - formatted.len())], - ); - $buf.extend_from_slice(formatted.as_bytes()); - } - }; -} - -#[repr(transparent)] -pub struct Date { - ptr: *mut pyo3_ffi::PyObject, -} - -impl Date { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - Date { ptr: ptr } - } - - #[inline(never)] - pub fn write_buf(&self, buf: &mut SmallFixedBuffer) { - { - let year = ffi!(PyDateTime_GET_YEAR(self.ptr)); - let mut yearbuf = itoa::Buffer::new(); - let formatted = yearbuf.format(year); - if unlikely!(year < 1000) { - // date-fullyear = 4DIGIT - buf.extend_from_slice(&[b'0', b'0', b'0', b'0'][..(4 - formatted.len())]); - } - buf.extend_from_slice(formatted.as_bytes()); - } - buf.push(b'-'); - { - let month = ffi!(PyDateTime_GET_MONTH(self.ptr)) as u32; - write_double_digit!(buf, month); - } - buf.push(b'-'); - { - let day = ffi!(PyDateTime_GET_DAY(self.ptr)) as u32; - write_double_digit!(buf, day); - } - } -} -impl Serialize for Date { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut buf = SmallFixedBuffer::new(); - self.write_buf(&mut buf); - serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) - } -} - -pub enum TimeError { - HasTimezone, -} - -pub struct Time { - ptr: *mut pyo3_ffi::PyObject, - opts: Opt, -} - -impl Time { - pub fn new(ptr: *mut pyo3_ffi::PyObject, opts: Opt) -> Self { - Time { - ptr: ptr, - opts: opts, - } - } - - #[inline(never)] - pub fn write_buf(&self, buf: &mut SmallFixedBuffer) -> Result<(), TimeError> { - if unsafe { (*(self.ptr as *mut pyo3_ffi::PyDateTime_Time)).hastzinfo == 1 } { - return Err(TimeError::HasTimezone); - } - let hour = ffi!(PyDateTime_TIME_GET_HOUR(self.ptr)) as u8; - write_double_digit!(buf, hour); - buf.push(b':'); - let minute = ffi!(PyDateTime_TIME_GET_MINUTE(self.ptr)) as u8; - write_double_digit!(buf, minute); - buf.push(b':'); - let second = ffi!(PyDateTime_TIME_GET_SECOND(self.ptr)) as u8; - write_double_digit!(buf, second); - if opt_disabled!(self.opts, OMIT_MICROSECONDS) { - let microsecond = ffi!(PyDateTime_TIME_GET_MICROSECOND(self.ptr)) as u32; - write_microsecond!(buf, microsecond); - } - Ok(()) - } -} - -impl Serialize for Time { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut buf = SmallFixedBuffer::new(); - if self.write_buf(&mut buf).is_err() { - err!(SerializeError::DatetimeLibraryUnsupported) - }; - serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) - } -} - -pub struct DateTime { - ptr: *mut pyo3_ffi::PyObject, - opts: Opt, -} - -impl DateTime { - pub fn new(ptr: *mut pyo3_ffi::PyObject, opts: Opt) -> Self { - DateTime { - ptr: ptr, - opts: opts, - } - } -} - -macro_rules! pydatetime_get { - ($fn: ident, $pyfn: ident, $ty: ident) => { - fn $fn(&self) -> $ty { - ffi!($pyfn(self.ptr)) as $ty - } - }; -} - -impl DateTimeLike for DateTime { - pydatetime_get!(year, PyDateTime_GET_YEAR, i32); - pydatetime_get!(month, PyDateTime_GET_MONTH, u8); - pydatetime_get!(day, PyDateTime_GET_DAY, u8); - pydatetime_get!(hour, PyDateTime_DATE_GET_HOUR, u8); - pydatetime_get!(minute, PyDateTime_DATE_GET_MINUTE, u8); - pydatetime_get!(second, PyDateTime_DATE_GET_SECOND, u8); - pydatetime_get!(microsecond, PyDateTime_DATE_GET_MICROSECOND, u32); - - fn nanosecond(&self) -> u32 { - self.microsecond() * 1_000 - } - - fn has_tz(&self) -> bool { - unsafe { (*(self.ptr as *mut pyo3_ffi::PyDateTime_DateTime)).hastzinfo == 1 } - } - - fn slow_offset(&self) -> Result { - let tzinfo = ffi!(PyDateTime_DATE_GET_TZINFO(self.ptr)); - if ffi!(PyObject_HasAttr(tzinfo, CONVERT_METHOD_STR)) == 1 { - // pendulum - let py_offset = call_method!(self.ptr, UTCOFFSET_METHOD_STR); - let offset = Offset { - second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), - day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), - }; - ffi!(Py_DECREF(py_offset)); - Ok(offset) - } else if ffi!(PyObject_HasAttr(tzinfo, NORMALIZE_METHOD_STR)) == 1 { - // pytz - let method_ptr = call_method!(tzinfo, NORMALIZE_METHOD_STR, self.ptr); - let py_offset = call_method!(method_ptr, UTCOFFSET_METHOD_STR); - ffi!(Py_DECREF(method_ptr)); - let offset = Offset { - second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), - day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), - }; - ffi!(Py_DECREF(py_offset)); - Ok(offset) - } else if ffi!(PyObject_HasAttr(tzinfo, DST_STR)) == 1 { - // dateutil/arrow, datetime.timezone.utc - let py_offset = call_method!(tzinfo, UTCOFFSET_METHOD_STR, self.ptr); - let offset = Offset { - second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), - day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), - }; - ffi!(Py_DECREF(py_offset)); - Ok(offset) - } else { - Err(DateTimeError::LibraryUnsupported) - } - } - - #[cfg(Py_3_9)] - fn offset(&self) -> Result { - if !self.has_tz() { - Ok(Offset::default()) - } else { - let tzinfo = ffi!(PyDateTime_DATE_GET_TZINFO(self.ptr)); - if unsafe { ob_type!(tzinfo) == ZONEINFO_TYPE } { - // zoneinfo - let py_offset = call_method!(tzinfo, UTCOFFSET_METHOD_STR, self.ptr); - let offset = Offset { - second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), - day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), - }; - ffi!(Py_DECREF(py_offset)); - Ok(offset) - } else { - self.slow_offset() - } - } - } - - #[cfg(not(Py_3_9))] - fn offset(&self) -> Result { - if !self.has_tz() { - Ok(Offset::default()) - } else { - self.slow_offset() - } - } -} - -impl Serialize for DateTime { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut buf = SmallFixedBuffer::new(); - if self.write_buf(&mut buf, self.opts).is_err() { - err!(SerializeError::DatetimeLibraryUnsupported) - } - serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) - } -} diff --git a/vendor/orjson/src/serialize/per_type/datetimelike.rs b/vendor/orjson/src/serialize/per_type/datetimelike.rs deleted file mode 100644 index 6897a359ae..0000000000 --- a/vendor/orjson/src/serialize/per_type/datetimelike.rs +++ /dev/null @@ -1,147 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::*; - -use crate::serialize::buffer::SmallFixedBuffer; - -pub enum DateTimeError { - LibraryUnsupported, -} - -macro_rules! write_double_digit { - ($buf:ident, $value:expr) => { - if $value < 10 { - $buf.push(b'0'); - } - $buf.extend_from_slice(itoa::Buffer::new().format($value).as_bytes()); - }; -} - -macro_rules! write_triple_digit { - ($buf:ident, $value:expr) => { - if $value < 100 { - $buf.push(b'0'); - } - if $value < 10 { - $buf.push(b'0'); - } - $buf.extend_from_slice(itoa::Buffer::new().format($value).as_bytes()); - }; -} - -#[derive(Default)] -pub struct Offset { - pub day: i32, - pub second: i32, -} - -/// Trait providing a method to write a datetime-like object to a buffer in an RFC3339-compatible format. -/// -/// The provided `write_buf` method does not allocate, and is faster -/// than writing to a heap-allocated string. -pub trait DateTimeLike { - /// Returns the year component of the datetime. - fn year(&self) -> i32; - /// Returns the month component of the datetime. - fn month(&self) -> u8; - /// Returns the day component of the datetime. - fn day(&self) -> u8; - /// Returns the hour component of the datetime. - fn hour(&self) -> u8; - /// Returns the minute component of the datetime. - fn minute(&self) -> u8; - /// Returns the second component of the datetime. - fn second(&self) -> u8; - /// Returns the number of microseconds since the whole non-leap second. - fn microsecond(&self) -> u32; - /// Returns the number of nanoseconds since the whole non-leap second. - fn nanosecond(&self) -> u32; - - /// Is the object time-zone aware? - fn has_tz(&self) -> bool; - - //// python3.8 or below implementation of offset() - fn slow_offset(&self) -> Result; - - /// The offset of the timezone. - fn offset(&self) -> Result; - - /// Write `self` to a buffer in RFC3339 format, using `opts` to - /// customise if desired. - #[inline(never)] - fn write_buf(&self, buf: &mut SmallFixedBuffer, opts: Opt) -> Result<(), DateTimeError> { - { - let year = self.year(); - let mut yearbuf = itoa::Buffer::new(); - let formatted = yearbuf.format(year); - if unlikely!(year < 1000) { - // date-fullyear = 4DIGIT - buf.extend_from_slice(&[b'0', b'0', b'0', b'0'][..(4 - formatted.len())]); - } - buf.extend_from_slice(formatted.as_bytes()); - } - buf.push(b'-'); - write_double_digit!(buf, self.month()); - buf.push(b'-'); - write_double_digit!(buf, self.day()); - buf.push(b'T'); - write_double_digit!(buf, self.hour()); - buf.push(b':'); - write_double_digit!(buf, self.minute()); - buf.push(b':'); - write_double_digit!(buf, self.second()); - if opt_disabled!(opts, OMIT_MICROSECONDS) { - let microsecond = self.microsecond(); - if microsecond != 0 { - buf.push(b'.'); - write_triple_digit!(buf, microsecond / 1_000); - write_triple_digit!(buf, microsecond % 1_000); - // Don't support writing nanoseconds for now. - // If requested, something like the following should work, - // and `SmallFixedBuffer` needs at least length 35. - // let nanosecond = self.nanosecond(); - // if nanosecond % 1_000 != 0 { - // write_triple_digit!(buf, nanosecond % 1_000); - // } - } - } - if self.has_tz() || opt_enabled!(opts, NAIVE_UTC) { - let offset = self.offset()?; - let mut offset_second = offset.second; - if offset_second == 0 { - if opt_enabled!(opts, UTC_Z) { - buf.push(b'Z'); - } else { - buf.extend_from_slice(&[b'+', b'0', b'0', b':', b'0', b'0']); - } - } else { - // This branch is only really hit by the Python datetime implementation, - // since numpy datetimes are all converted to UTC. - if offset.day == -1 { - // datetime.timedelta(days=-1, seconds=68400) -> -05:00 - buf.push(b'-'); - offset_second = 86400 - offset_second; - } else { - // datetime.timedelta(seconds=37800) -> +10:30 - buf.push(b'+'); - } - let offset_minute = offset_second / 60; - let offset_hour = offset_minute / 60; - write_double_digit!(buf, offset_hour); - buf.push(b':'); - let mut offset_minute_print = offset_minute % 60; - // https://tools.ietf.org/html/rfc3339#section-5.8 - // "exactly 19 minutes and 32.13 seconds ahead of UTC" - // "closest representable UTC offset" - // "+20:00" - let offset_excess_second = - offset_second - (offset_minute_print * 60 + offset_hour * 3600); - if offset_excess_second >= 30 { - offset_minute_print += 1; - } - write_double_digit!(buf, offset_minute_print); - } - } - Ok(()) - } -} diff --git a/vendor/orjson/src/serialize/per_type/default.rs b/vendor/orjson/src/serialize/per_type/default.rs deleted file mode 100644 index 6c9e1d4775..0000000000 --- a/vendor/orjson/src/serialize/per_type/default.rs +++ /dev/null @@ -1,62 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::error::SerializeError; -use crate::serialize::serializer::PyObjectSerializer; - -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct DefaultSerializer<'a> { - previous: &'a PyObjectSerializer, -} - -impl<'a> DefaultSerializer<'a> { - pub fn new(previous: &'a PyObjectSerializer) -> Self { - Self { previous: previous } - } -} - -impl<'a> Serialize for DefaultSerializer<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match self.previous.default { - Some(callable) => { - if unlikely!(self.previous.state.default_calls_limit()) { - err!(SerializeError::DefaultRecursionLimit) - } - #[cfg(not(Py_3_10))] - let default_obj = ffi!(PyObject_CallFunctionObjArgs( - callable.as_ptr(), - self.previous.ptr, - core::ptr::null_mut() as *mut pyo3_ffi::PyObject - )); - #[cfg(Py_3_10)] - let default_obj = unsafe { - pyo3_ffi::PyObject_Vectorcall( - callable.as_ptr(), - core::ptr::addr_of!(self.previous.ptr), - pyo3_ffi::PyVectorcall_NARGS(1) as usize, - core::ptr::null_mut(), - ) - }; - if unlikely!(default_obj.is_null()) { - err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))) - } else { - let res = PyObjectSerializer::new( - default_obj, - self.previous.state.copy_for_default_call(), - self.previous.default, - ) - .serialize(serializer); - ffi!(Py_DECREF(default_obj)); - res - } - } - None => err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))), - } - } -} diff --git a/vendor/orjson/src/serialize/per_type/dict.rs b/vendor/orjson/src/serialize/per_type/dict.rs deleted file mode 100644 index b5e6cb3bd1..0000000000 --- a/vendor/orjson/src/serialize/per_type/dict.rs +++ /dev/null @@ -1,505 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::*; -use crate::serialize::buffer::SmallFixedBuffer; -use crate::serialize::error::SerializeError; -use crate::serialize::obtype::{pyobject_to_obtype, ObType}; -use crate::serialize::per_type::datetimelike::DateTimeLike; -use crate::serialize::per_type::{ - BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, EnumSerializer, - FloatSerializer, FragmentSerializer, IntSerializer, ListTupleSerializer, NoneSerializer, - NumpyScalar, NumpySerializer, StrSerializer, StrSubclassSerializer, Time, ZeroListSerializer, - UUID, -}; -use crate::serialize::serializer::PyObjectSerializer; -use crate::serialize::state::SerializerState; -use crate::str::{unicode_to_str, unicode_to_str_via_ffi}; -use crate::typeref::{STR_TYPE, TRUE, VALUE_STR}; -use compact_str::CompactString; -use core::ptr::NonNull; -use serde::ser::{Serialize, SerializeMap, Serializer}; -use smallvec::SmallVec; - -pub struct ZeroDictSerializer; - -impl ZeroDictSerializer { - pub const fn new() -> Self { - Self {} - } -} - -impl Serialize for ZeroDictSerializer { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_bytes(b"{}") - } -} - -pub struct DictGenericSerializer { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - #[allow(dead_code)] - default: Option>, -} - -impl DictGenericSerializer { - pub fn new( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - DictGenericSerializer { - ptr: ptr, - state: state.copy_for_recursive_call(), - default: default, - } - } -} - -impl Serialize for DictGenericSerializer { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - if unlikely!(self.state.recursion_limit()) { - err!(SerializeError::RecursionLimit) - } - - if unlikely!(ffi!(Py_SIZE(self.ptr)) == 0) { - ZeroDictSerializer::new().serialize(serializer) - } else if likely!(opt_disabled!(self.state.opts(), SORT_OR_NON_STR_KEYS)) { - unsafe { - core::mem::transmute::<&DictGenericSerializer, &Dict>(self).serialize(serializer) - } - } else if opt_enabled!(self.state.opts(), NON_STR_KEYS) { - unsafe { - core::mem::transmute::<&DictGenericSerializer, &DictNonStrKey>(self) - .serialize(serializer) - } - } else { - unsafe { - core::mem::transmute::<&DictGenericSerializer, &DictSortedKey>(self) - .serialize(serializer) - } - } - } -} - -macro_rules! impl_serialize_entry { - ($map:expr, $self:expr, $key:expr, $value:expr) => { - match pyobject_to_obtype($value, $self.state.opts()) { - ObType::Str => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&StrSerializer::new($value))?; - } - ObType::StrSubclass => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&StrSubclassSerializer::new($value))?; - } - ObType::Int => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&IntSerializer::new($value, $self.state.opts()))?; - } - ObType::None => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&NoneSerializer::new()).unwrap(); - } - ObType::Float => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&FloatSerializer::new($value))?; - } - ObType::Bool => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&BoolSerializer::new($value)).unwrap(); - } - ObType::Datetime => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&DateTime::new($value, $self.state.opts()))?; - } - ObType::Date => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&Date::new($value))?; - } - ObType::Time => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&Time::new($value, $self.state.opts()))?; - } - ObType::Uuid => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&UUID::new($value)).unwrap(); - } - ObType::Dict => { - let pyvalue = DictGenericSerializer::new($value, $self.state, $self.default); - $map.serialize_key($key).unwrap(); - $map.serialize_value(&pyvalue)?; - } - ObType::List => { - if ffi!(Py_SIZE($value)) == 0 { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&ZeroListSerializer::new()).unwrap(); - } else { - let pyvalue = - ListTupleSerializer::from_list($value, $self.state, $self.default); - $map.serialize_key($key).unwrap(); - $map.serialize_value(&pyvalue)?; - } - } - ObType::Tuple => { - if ffi!(Py_SIZE($value)) == 0 { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&ZeroListSerializer::new()).unwrap(); - } else { - let pyvalue = - ListTupleSerializer::from_tuple($value, $self.state, $self.default); - $map.serialize_key($key).unwrap(); - $map.serialize_value(&pyvalue)?; - } - } - ObType::Dataclass => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&DataclassGenericSerializer::new(&PyObjectSerializer::new( - $value, - $self.state, - $self.default, - )))?; - } - ObType::Enum => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&EnumSerializer::new(&PyObjectSerializer::new( - $value, - $self.state, - $self.default, - )))?; - } - ObType::NumpyArray => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&NumpySerializer::new(&PyObjectSerializer::new( - $value, - $self.state, - $self.default, - )))?; - } - ObType::NumpyScalar => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&NumpyScalar::new($value, $self.state.opts()))?; - } - ObType::Fragment => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&FragmentSerializer::new($value))?; - } - ObType::Unknown => { - $map.serialize_key($key).unwrap(); - $map.serialize_value(&DefaultSerializer::new(&PyObjectSerializer::new( - $value, - $self.state, - $self.default, - )))?; - } - } - }; -} - -pub struct Dict { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, -} - -impl Serialize for Dict { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut pos = 0; - let mut next_key: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - let mut next_value: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - let mut map = serializer.serialize_map(None).unwrap(); - - let len = ffi!(Py_SIZE(self.ptr)) as usize; - assume!(len > 0); - - for _ in 0..len { - let key = next_key; - let value = next_value; - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - // key - let key_as_str = { - let key_ob_type = ob_type!(key); - if unlikely!(!is_class_by_type!(key_ob_type, STR_TYPE)) { - err!(SerializeError::KeyMustBeStr) - } - let tmp = unicode_to_str(key); - if unlikely!(tmp.is_none()) { - err!(SerializeError::InvalidStr) - }; - tmp.unwrap() - }; - - // value - impl_serialize_entry!(map, self, key_as_str, value); - } - - map.end() - } -} - -pub struct DictSortedKey { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, -} - -impl Serialize for DictSortedKey { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut pos = 0; - let mut next_key: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - let mut next_value: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - let len = ffi!(Py_SIZE(self.ptr)) as usize; - assume!(len > 0); - - let mut items: SmallVec<[(&str, *mut pyo3_ffi::PyObject); 8]> = - SmallVec::with_capacity(len); - - for _ in 0..len as usize { - let key = next_key; - let value = next_value; - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - if unlikely!(unsafe { ob_type!(key) != STR_TYPE }) { - err!(SerializeError::KeyMustBeStr) - } - let data = unicode_to_str(key); - if unlikely!(data.is_none()) { - err!(SerializeError::InvalidStr) - } - items.push((data.unwrap(), value)); - } - - items.sort_unstable_by(|a, b| a.0.cmp(b.0)); - - let mut map = serializer.serialize_map(None).unwrap(); - for (key, val) in items.iter() { - let pyvalue = PyObjectSerializer::new(*val, self.state, self.default); - map.serialize_key(key).unwrap(); - map.serialize_value(&pyvalue)?; - } - map.end() - } -} - -#[inline(never)] -fn non_str_str(key: *mut pyo3_ffi::PyObject) -> Result { - // because of ObType::Enum - let uni = unicode_to_str(key); - if unlikely!(uni.is_none()) { - Err(SerializeError::InvalidStr) - } else { - Ok(CompactString::from(uni.unwrap())) - } -} - -#[cold] -#[inline(never)] -fn non_str_str_subclass(key: *mut pyo3_ffi::PyObject) -> Result { - let uni = unicode_to_str_via_ffi(key); - if unlikely!(uni.is_none()) { - Err(SerializeError::InvalidStr) - } else { - Ok(CompactString::from(uni.unwrap())) - } -} - -#[inline(never)] -fn non_str_date(key: *mut pyo3_ffi::PyObject) -> Result { - let mut buf = SmallFixedBuffer::new(); - Date::new(key).write_buf(&mut buf); - let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); - Ok(CompactString::from(key_as_str)) -} - -#[inline(never)] -fn non_str_datetime( - key: *mut pyo3_ffi::PyObject, - opts: crate::opt::Opt, -) -> Result { - let mut buf = SmallFixedBuffer::new(); - let dt = DateTime::new(key, opts); - if dt.write_buf(&mut buf, opts).is_err() { - return Err(SerializeError::DatetimeLibraryUnsupported); - } - let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); - Ok(CompactString::from(key_as_str)) -} - -#[cold] -#[inline(never)] -fn non_str_time( - key: *mut pyo3_ffi::PyObject, - opts: crate::opt::Opt, -) -> Result { - let mut buf = SmallFixedBuffer::new(); - let time = Time::new(key, opts); - if time.write_buf(&mut buf).is_err() { - return Err(SerializeError::TimeHasTzinfo); - } - let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); - Ok(CompactString::from(key_as_str)) -} - -#[inline(never)] -fn non_str_uuid(key: *mut pyo3_ffi::PyObject) -> Result { - let mut buf = SmallFixedBuffer::new(); - UUID::new(key).write_buf(&mut buf); - let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); - Ok(CompactString::from(key_as_str)) -} - -#[cold] -#[inline(never)] -fn non_str_float(key: *mut pyo3_ffi::PyObject) -> Result { - let val = ffi!(PyFloat_AS_DOUBLE(key)); - if !val.is_finite() { - Ok(CompactString::const_new("null")) - } else { - Ok(CompactString::from(ryu::Buffer::new().format_finite(val))) - } -} - -#[inline(never)] -fn non_str_int(key: *mut pyo3_ffi::PyObject) -> Result { - let ival = ffi!(PyLong_AsLongLong(key)); - if unlikely!(ival == -1 && !ffi!(PyErr_Occurred()).is_null()) { - ffi!(PyErr_Clear()); - let uval = ffi!(PyLong_AsUnsignedLongLong(key)); - if unlikely!(uval == u64::MAX && !ffi!(PyErr_Occurred()).is_null()) { - return Err(SerializeError::DictIntegerKey64Bit); - } - Ok(CompactString::from(itoa::Buffer::new().format(uval))) - } else { - Ok(CompactString::from(itoa::Buffer::new().format(ival))) - } -} - -#[inline(never)] -fn sort_non_str_dict_items(items: &mut SmallVec<[(CompactString, *mut pyo3_ffi::PyObject); 8]>) { - items.sort_unstable_by(|a, b| a.0.cmp(&b.0)); -} - -pub struct DictNonStrKey { - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, -} - -impl DictNonStrKey { - fn pyobject_to_string( - key: *mut pyo3_ffi::PyObject, - opts: crate::opt::Opt, - ) -> Result { - match pyobject_to_obtype(key, opts) { - ObType::None => Ok(CompactString::const_new("null")), - ObType::Bool => { - if unsafe { key == TRUE } { - Ok(CompactString::const_new("true")) - } else { - Ok(CompactString::const_new("false")) - } - } - ObType::Int => non_str_int(key), - ObType::Float => non_str_float(key), - ObType::Datetime => non_str_datetime(key, opts), - ObType::Date => non_str_date(key), - ObType::Time => non_str_time(key, opts), - ObType::Uuid => non_str_uuid(key), - ObType::Enum => { - let value = ffi!(PyObject_GetAttr(key, VALUE_STR)); - debug_assert!(ffi!(Py_REFCNT(value)) >= 2); - let ret = Self::pyobject_to_string(value, opts); - ffi!(Py_DECREF(value)); - ret - } - ObType::Str => non_str_str(key), - ObType::StrSubclass => non_str_str_subclass(key), - ObType::Tuple - | ObType::NumpyScalar - | ObType::NumpyArray - | ObType::Dict - | ObType::List - | ObType::Dataclass - | ObType::Fragment - | ObType::Unknown => Err(SerializeError::DictKeyInvalidType), - } - } -} - -impl Serialize for DictNonStrKey { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut pos = 0; - let mut next_key: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - let mut next_value: *mut pyo3_ffi::PyObject = core::ptr::null_mut(); - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - let opts = self.state.opts() & NOT_PASSTHROUGH; - - let len = ffi!(Py_SIZE(self.ptr)) as usize; - assume!(len > 0); - - let mut items: SmallVec<[(CompactString, *mut pyo3_ffi::PyObject); 8]> = - SmallVec::with_capacity(len); - - for _ in 0..len { - let key = next_key; - let value = next_value; - - pydict_next!(self.ptr, &mut pos, &mut next_key, &mut next_value); - - if is_type!(ob_type!(key), STR_TYPE) { - let uni = unicode_to_str(key); - if unlikely!(uni.is_none()) { - err!(SerializeError::InvalidStr) - } - items.push((CompactString::from(uni.unwrap()), value)); - } else { - match Self::pyobject_to_string(key, opts) { - Ok(key_as_str) => items.push((key_as_str, value)), - Err(err) => err!(err), - } - } - } - - if opt_enabled!(opts, SORT_KEYS) { - sort_non_str_dict_items(&mut items); - } - - let mut map = serializer.serialize_map(None).unwrap(); - for (key, val) in items.iter() { - let pyvalue = PyObjectSerializer::new(*val, self.state, self.default); - map.serialize_key(key).unwrap(); - map.serialize_value(&pyvalue)?; - } - map.end() - } -} diff --git a/vendor/orjson/src/serialize/per_type/float.rs b/vendor/orjson/src/serialize/per_type/float.rs deleted file mode 100644 index 68f1de9256..0000000000 --- a/vendor/orjson/src/serialize/per_type/float.rs +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct FloatSerializer { - ptr: *mut pyo3_ffi::PyObject, -} - -impl FloatSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - FloatSerializer { ptr: ptr } - } -} - -impl Serialize for FloatSerializer { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_f64(ffi!(PyFloat_AS_DOUBLE(self.ptr))) - } -} diff --git a/vendor/orjson/src/serialize/per_type/fragment.rs b/vendor/orjson/src/serialize/per_type/fragment.rs deleted file mode 100644 index c3b2e74c99..0000000000 --- a/vendor/orjson/src/serialize/per_type/fragment.rs +++ /dev/null @@ -1,50 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::ffi::{Fragment, PyBytes_AS_STRING, PyBytes_GET_SIZE}; -use crate::serialize::error::SerializeError; -use crate::str::unicode_to_str; -use crate::typeref::{BYTES_TYPE, STR_TYPE}; - -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct FragmentSerializer { - ptr: *mut pyo3_ffi::PyObject, -} - -impl FragmentSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - FragmentSerializer { ptr: ptr } - } -} - -impl Serialize for FragmentSerializer { - #[cold] - #[inline(never)] - #[cfg_attr(feature = "optimize", optimize(size))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let buffer: &[u8]; - unsafe { - let fragment: *mut Fragment = self.ptr as *mut Fragment; - let ob_type = ob_type!((*fragment).contents); - if ob_type == BYTES_TYPE { - buffer = core::slice::from_raw_parts( - PyBytes_AS_STRING((*fragment).contents) as *const u8, - PyBytes_GET_SIZE((*fragment).contents) as usize, - ); - } else if ob_type == STR_TYPE { - let uni = unicode_to_str((*fragment).contents); - if unlikely!(uni.is_none()) { - err!(SerializeError::InvalidStr) - } - buffer = uni.unwrap().as_bytes(); - } else { - err!(SerializeError::InvalidFragment) - } - } - serializer.serialize_bytes(buffer) - } -} diff --git a/vendor/orjson/src/serialize/per_type/int.rs b/vendor/orjson/src/serialize/per_type/int.rs deleted file mode 100644 index d94fc3c328..0000000000 --- a/vendor/orjson/src/serialize/per_type/int.rs +++ /dev/null @@ -1,112 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::{Opt, STRICT_INTEGER}; -use crate::serialize::error::SerializeError; -use serde::ser::{Serialize, Serializer}; - -// https://tools.ietf.org/html/rfc7159#section-6 -// "[-(2**53)+1, (2**53)-1]" -const STRICT_INT_MIN: i64 = -9007199254740991; -const STRICT_INT_MAX: i64 = 9007199254740991; - -pub struct IntSerializer { - ptr: *mut pyo3_ffi::PyObject, - opts: Opt, -} - -impl IntSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject, opts: Opt) -> Self { - IntSerializer { - ptr: ptr, - opts: opts, - } - } -} - -impl Serialize for IntSerializer { - #[inline(always)] - #[cfg(feature = "inline_int")] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - unsafe { - if crate::ffi::pylong_is_zero(self.ptr) { - return serializer.serialize_bytes(b"0"); - } - let is_signed = !crate::ffi::pylong_is_unsigned(self.ptr) as i32; - if crate::ffi::pylong_fits_in_i32(self.ptr) { - if is_signed == 0 { - serializer.serialize_u64(crate::ffi::pylong_get_inline_value(self.ptr) as u64) - } else { - serializer.serialize_i64(crate::ffi::pylong_get_inline_value(self.ptr) as i64) - } - } else { - let mut buffer: [u8; 8] = [0; 8]; - let ret = pyo3_ffi::_PyLong_AsByteArray( - self.ptr as *mut pyo3_ffi::PyLongObject, - buffer.as_mut_ptr() as *mut core::ffi::c_uchar, - 8, - 1, - is_signed, - ); - if unlikely!(ret == -1) { - ffi!(PyErr_Clear()); - err!(SerializeError::Integer64Bits) - } - if is_signed == 0 { - let val = core::mem::transmute::<[u8; 8], u64>(buffer); - if unlikely!(opt_enabled!(self.opts, STRICT_INTEGER)) - && val > STRICT_INT_MAX as u64 - { - err!(SerializeError::Integer53Bits) - } - serializer.serialize_u64(val) - } else { - let val = core::mem::transmute::<[u8; 8], i64>(buffer); - if unlikely!(opt_enabled!(self.opts, STRICT_INTEGER)) - && !(STRICT_INT_MIN..=STRICT_INT_MAX).contains(&val) - { - err!(SerializeError::Integer53Bits) - } - serializer.serialize_i64(val) - } - } - } - } - - #[inline(always)] - #[cfg(not(feature = "inline_int"))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - unsafe { - if crate::ffi::pylong_is_unsigned(self.ptr) { - let val = ffi!(PyLong_AsUnsignedLongLong(self.ptr)); - if unlikely!(val == u64::MAX) && !ffi!(PyErr_Occurred()).is_null() { - ffi!(PyErr_Clear()); - err!(SerializeError::Integer64Bits) - } else if unlikely!(opt_enabled!(self.opts, STRICT_INTEGER)) - && val > STRICT_INT_MAX as u64 - { - err!(SerializeError::Integer53Bits) - } else { - serializer.serialize_u64(val) - } - } else { - let val = ffi!(PyLong_AsLongLong(self.ptr)); - if unlikely!(val == -1) && !ffi!(PyErr_Occurred()).is_null() { - ffi!(PyErr_Clear()); - err!(SerializeError::Integer64Bits) - } else if unlikely!(opt_enabled!(self.opts, STRICT_INTEGER)) - && !(STRICT_INT_MIN..=STRICT_INT_MAX).contains(&val) - { - err!(SerializeError::Integer53Bits) - } else { - serializer.serialize_i64(val) - } - } - } - } -} diff --git a/vendor/orjson/src/serialize/per_type/list.rs b/vendor/orjson/src/serialize/per_type/list.rs deleted file mode 100644 index 3339cf300e..0000000000 --- a/vendor/orjson/src/serialize/per_type/list.rs +++ /dev/null @@ -1,184 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::error::SerializeError; -use crate::serialize::obtype::{pyobject_to_obtype, ObType}; -use crate::serialize::per_type::{ - BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, - DictGenericSerializer, EnumSerializer, FloatSerializer, FragmentSerializer, IntSerializer, - NoneSerializer, NumpyScalar, NumpySerializer, StrSerializer, StrSubclassSerializer, Time, UUID, -}; -use crate::serialize::serializer::PyObjectSerializer; -use crate::serialize::state::SerializerState; -use crate::typeref::*; - -use core::ptr::NonNull; -use serde::ser::{Serialize, SerializeSeq, Serializer}; - -pub struct ZeroListSerializer; - -impl ZeroListSerializer { - pub const fn new() -> Self { - Self {} - } -} - -impl Serialize for ZeroListSerializer { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_bytes(b"[]") - } -} - -pub struct ListTupleSerializer { - data_ptr: *const *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - len: usize, -} - -impl ListTupleSerializer { - pub fn from_list( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - debug_assert!( - is_type!(ob_type!(ptr), LIST_TYPE) - || is_subclass_by_flag!(ob_type!(ptr), Py_TPFLAGS_LIST_SUBCLASS) - ); - let data_ptr = unsafe { (*(ptr as *mut pyo3_ffi::PyListObject)).ob_item }; - let len = ffi!(Py_SIZE(ptr)) as usize; - Self { - data_ptr: data_ptr, - len: len, - state: state.copy_for_recursive_call(), - default: default, - } - } - - pub fn from_tuple( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - debug_assert!( - is_type!(ob_type!(ptr), TUPLE_TYPE) - || is_subclass_by_flag!(ob_type!(ptr), Py_TPFLAGS_TUPLE_SUBCLASS) - ); - let data_ptr = unsafe { (*(ptr as *mut pyo3_ffi::PyTupleObject)).ob_item.as_ptr() }; - let len = ffi!(Py_SIZE(ptr)) as usize; - Self { - data_ptr: data_ptr, - len: len, - state: state.copy_for_recursive_call(), - default: default, - } - } -} - -impl Serialize for ListTupleSerializer { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - if unlikely!(self.state.recursion_limit()) { - err!(SerializeError::RecursionLimit) - } - debug_assert!(self.len >= 1); - let mut seq = serializer.serialize_seq(None).unwrap(); - for idx in 0..self.len { - let value = unsafe { *((self.data_ptr).add(idx)) }; - match pyobject_to_obtype(value, self.state.opts()) { - ObType::Str => { - seq.serialize_element(&StrSerializer::new(value))?; - } - ObType::StrSubclass => { - seq.serialize_element(&StrSubclassSerializer::new(value))?; - } - ObType::Int => { - seq.serialize_element(&IntSerializer::new(value, self.state.opts()))?; - } - ObType::None => { - seq.serialize_element(&NoneSerializer::new()).unwrap(); - } - ObType::Float => { - seq.serialize_element(&FloatSerializer::new(value))?; - } - ObType::Bool => { - seq.serialize_element(&BoolSerializer::new(value)).unwrap(); - } - ObType::Datetime => { - seq.serialize_element(&DateTime::new(value, self.state.opts()))?; - } - ObType::Date => { - seq.serialize_element(&Date::new(value))?; - } - ObType::Time => { - seq.serialize_element(&Time::new(value, self.state.opts()))?; - } - ObType::Uuid => { - seq.serialize_element(&UUID::new(value)).unwrap(); - } - ObType::Dict => { - let pyvalue = DictGenericSerializer::new(value, self.state, self.default); - seq.serialize_element(&pyvalue)?; - } - ObType::List => { - if ffi!(Py_SIZE(value)) == 0 { - seq.serialize_element(&ZeroListSerializer::new()).unwrap(); - } else { - let pyvalue = - ListTupleSerializer::from_list(value, self.state, self.default); - seq.serialize_element(&pyvalue)?; - } - } - ObType::Tuple => { - if ffi!(Py_SIZE(value)) == 0 { - seq.serialize_element(&ZeroListSerializer::new()).unwrap(); - } else { - let pyvalue = - ListTupleSerializer::from_tuple(value, self.state, self.default); - seq.serialize_element(&pyvalue)?; - } - } - ObType::Dataclass => { - seq.serialize_element(&DataclassGenericSerializer::new( - &PyObjectSerializer::new(value, self.state, self.default), - ))?; - } - ObType::Enum => { - seq.serialize_element(&EnumSerializer::new(&PyObjectSerializer::new( - value, - self.state, - self.default, - )))?; - } - ObType::NumpyArray => { - seq.serialize_element(&NumpySerializer::new(&PyObjectSerializer::new( - value, - self.state, - self.default, - )))?; - } - ObType::NumpyScalar => { - seq.serialize_element(&NumpyScalar::new(value, self.state.opts()))?; - } - ObType::Fragment => { - seq.serialize_element(&FragmentSerializer::new(value))?; - } - ObType::Unknown => { - seq.serialize_element(&DefaultSerializer::new(&PyObjectSerializer::new( - value, - self.state, - self.default, - )))?; - } - } - } - seq.end() - } -} diff --git a/vendor/orjson/src/serialize/per_type/mod.rs b/vendor/orjson/src/serialize/per_type/mod.rs deleted file mode 100644 index 2390e266c9..0000000000 --- a/vendor/orjson/src/serialize/per_type/mod.rs +++ /dev/null @@ -1,34 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -mod dataclass; -mod datetime; -mod pybool; -#[macro_use] -mod datetimelike; -mod default; -mod dict; -mod float; -mod fragment; -mod int; -mod list; -mod none; -mod numpy; -mod pyenum; -mod unicode; -mod uuid; - -pub use dataclass::DataclassGenericSerializer; -pub use datetime::{Date, DateTime, Time}; -pub use datetimelike::{DateTimeError, DateTimeLike, Offset}; -pub use default::DefaultSerializer; -pub use dict::DictGenericSerializer; -pub use float::FloatSerializer; -pub use fragment::FragmentSerializer; -pub use int::IntSerializer; -pub use list::{ListTupleSerializer, ZeroListSerializer}; -pub use none::NoneSerializer; -pub use numpy::{is_numpy_array, is_numpy_scalar, NumpyScalar, NumpySerializer}; -pub use pybool::BoolSerializer; -pub use pyenum::EnumSerializer; -pub use unicode::{StrSerializer, StrSubclassSerializer}; -pub use uuid::UUID; diff --git a/vendor/orjson/src/serialize/per_type/none.rs b/vendor/orjson/src/serialize/per_type/none.rs deleted file mode 100644 index 1024082634..0000000000 --- a/vendor/orjson/src/serialize/per_type/none.rs +++ /dev/null @@ -1,21 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use serde::ser::{Serialize, Serializer}; - -pub struct NoneSerializer; - -impl NoneSerializer { - pub const fn new() -> Self { - Self {} - } -} - -impl Serialize for NoneSerializer { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_unit() - } -} diff --git a/vendor/orjson/src/serialize/per_type/numpy.rs b/vendor/orjson/src/serialize/per_type/numpy.rs deleted file mode 100644 index 0844292b13..0000000000 --- a/vendor/orjson/src/serialize/per_type/numpy.rs +++ /dev/null @@ -1,1406 +0,0 @@ -use crate::opt::*; - -use crate::serialize::buffer::SmallFixedBuffer; -use crate::serialize::error::SerializeError; -use crate::serialize::per_type::{ - DateTimeError, DateTimeLike, DefaultSerializer, Offset, ZeroListSerializer, -}; -use crate::serialize::serializer::PyObjectSerializer; -use crate::typeref::{load_numpy_types, ARRAY_STRUCT_STR, DESCR_STR, DTYPE_STR, NUMPY_TYPES}; -use core::ffi::{c_char, c_int, c_void}; -use jiff::civil::DateTime; -use jiff::Timestamp; -use pyo3_ffi::*; -use serde::ser::{self, Serialize, SerializeSeq, Serializer}; -use std::fmt; - -#[repr(transparent)] -pub struct NumpySerializer<'a> { - previous: &'a PyObjectSerializer, -} - -impl<'a> NumpySerializer<'a> { - pub fn new(previous: &'a PyObjectSerializer) -> Self { - Self { previous: previous } - } -} - -impl<'a> Serialize for NumpySerializer<'a> { - #[cold] - #[inline(never)] - #[cfg_attr(feature = "optimize", optimize(size))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match NumpyArray::new(self.previous.ptr, self.previous.state.opts()) { - Ok(val) => val.serialize(serializer), - Err(PyArrayError::Malformed) => err!(SerializeError::NumpyMalformed), - Err(PyArrayError::NotContiguous) | Err(PyArrayError::UnsupportedDataType) - if self.previous.default.is_some() => - { - DefaultSerializer::new(self.previous).serialize(serializer) - } - Err(PyArrayError::NotContiguous) => { - err!(SerializeError::NumpyNotCContiguous) - } - Err(PyArrayError::NotNativeEndian) => { - err!(SerializeError::NumpyNotNativeEndian) - } - Err(PyArrayError::UnsupportedDataType) => { - err!(SerializeError::NumpyUnsupportedDatatype) - } - } - } -} - -macro_rules! slice { - ($ptr:expr, $size:expr) => { - unsafe { core::slice::from_raw_parts($ptr, $size) } - }; -} - -#[cold] -pub fn is_numpy_scalar(ob_type: *mut PyTypeObject) -> bool { - let numpy_types = unsafe { NUMPY_TYPES.get_or_init(load_numpy_types) }; - if numpy_types.is_none() { - false - } else { - let scalar_types = unsafe { numpy_types.unwrap().as_ref() }; - ob_type == scalar_types.float64 - || ob_type == scalar_types.float32 - || ob_type == scalar_types.float16 - || ob_type == scalar_types.int64 - || ob_type == scalar_types.int16 - || ob_type == scalar_types.int32 - || ob_type == scalar_types.int8 - || ob_type == scalar_types.uint64 - || ob_type == scalar_types.uint32 - || ob_type == scalar_types.uint8 - || ob_type == scalar_types.uint16 - || ob_type == scalar_types.bool_ - || ob_type == scalar_types.datetime64 - } -} - -#[cold] -pub fn is_numpy_array(ob_type: *mut PyTypeObject) -> bool { - let numpy_types = unsafe { NUMPY_TYPES.get_or_init(load_numpy_types) }; - if numpy_types.is_none() { - false - } else { - let scalar_types = unsafe { numpy_types.unwrap().as_ref() }; - unsafe { ob_type == scalar_types.array } - } -} - -#[repr(C)] -pub struct PyCapsule { - pub ob_refcnt: Py_ssize_t, - pub ob_type: *mut PyTypeObject, - pub pointer: *mut c_void, - pub name: *const c_char, - pub context: *mut c_void, - pub destructor: *mut c_void, // should be typedef void (*PyCapsule_Destructor)(PyObject *); -} - -// https://docs.scipy.org/doc/numpy/reference/arrays.interface.html#c.__array_struct__ - -const NPY_ARRAY_C_CONTIGUOUS: c_int = 0x1; -const NPY_ARRAY_NOTSWAPPED: c_int = 0x200; - -#[repr(C)] -pub struct PyArrayInterface { - pub two: c_int, - pub nd: c_int, - pub typekind: c_char, - pub itemsize: c_int, - pub flags: c_int, - pub shape: *mut Py_intptr_t, - pub strides: *mut Py_intptr_t, - pub data: *mut c_void, - pub descr: *mut PyObject, -} - -#[derive(Clone, Copy)] -pub enum ItemType { - BOOL, - DATETIME64(NumpyDatetimeUnit), - F16, - F32, - F64, - I8, - I16, - I32, - I64, - U8, - U16, - U32, - U64, -} - -impl ItemType { - fn find(array: *mut PyArrayInterface, ptr: *mut PyObject) -> Option { - match unsafe { ((*array).typekind, (*array).itemsize) } { - (098, 1) => Some(ItemType::BOOL), - (077, 8) => { - let unit = NumpyDatetimeUnit::from_pyobject(ptr); - Some(ItemType::DATETIME64(unit)) - } - (102, 2) => Some(ItemType::F16), - (102, 4) => Some(ItemType::F32), - (102, 8) => Some(ItemType::F64), - (105, 1) => Some(ItemType::I8), - (105, 2) => Some(ItemType::I16), - (105, 4) => Some(ItemType::I32), - (105, 8) => Some(ItemType::I64), - (117, 1) => Some(ItemType::U8), - (117, 2) => Some(ItemType::U16), - (117, 4) => Some(ItemType::U32), - (117, 8) => Some(ItemType::U64), - _ => None, - } - } -} - -pub enum PyArrayError { - Malformed, - NotContiguous, - NotNativeEndian, - UnsupportedDataType, -} - -// >>> arr = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int32) -// >>> arr.ndim -// 3 -// >>> arr.shape -// (2, 2, 2) -// >>> arr.strides -// (16, 8, 4) -pub struct NumpyArray { - array: *mut PyArrayInterface, - position: Vec, - children: Vec, - depth: usize, - capsule: *mut PyCapsule, - kind: ItemType, - opts: Opt, -} - -impl NumpyArray { - #[cold] - #[inline(never)] - #[cfg_attr(feature = "optimize", optimize(size))] - pub fn new(ptr: *mut PyObject, opts: Opt) -> Result { - let capsule = ffi!(PyObject_GetAttr(ptr, ARRAY_STRUCT_STR)); - let array = unsafe { (*(capsule as *mut PyCapsule)).pointer as *mut PyArrayInterface }; - if unsafe { (*array).two != 2 } { - ffi!(Py_DECREF(capsule)); - Err(PyArrayError::Malformed) - } else if unsafe { (*array).flags } & NPY_ARRAY_C_CONTIGUOUS != NPY_ARRAY_C_CONTIGUOUS { - ffi!(Py_DECREF(capsule)); - Err(PyArrayError::NotContiguous) - } else if unsafe { (*array).flags } & NPY_ARRAY_NOTSWAPPED != NPY_ARRAY_NOTSWAPPED { - ffi!(Py_DECREF(capsule)); - Err(PyArrayError::NotNativeEndian) - } else { - let num_dimensions = unsafe { (*array).nd as usize }; - if num_dimensions == 0 { - ffi!(Py_DECREF(capsule)); - return Err(PyArrayError::UnsupportedDataType); - } - match ItemType::find(array, ptr) { - None => { - ffi!(Py_DECREF(capsule)); - Err(PyArrayError::UnsupportedDataType) - } - Some(kind) => { - let mut pyarray = NumpyArray { - array: array, - position: vec![0; num_dimensions], - children: Vec::with_capacity(num_dimensions), - depth: 0, - capsule: capsule as *mut PyCapsule, - kind: kind, - opts, - }; - if pyarray.dimensions() > 1 { - pyarray.build(); - } - Ok(pyarray) - } - } - } - } - - #[cfg_attr(feature = "optimize", optimize(size))] - fn child_from_parent(&self, position: Vec, num_children: usize) -> Self { - let mut arr = NumpyArray { - array: self.array, - position: position, - children: Vec::with_capacity(num_children), - depth: self.depth + 1, - capsule: self.capsule, - kind: self.kind, - opts: self.opts, - }; - arr.build(); - arr - } - - #[cfg_attr(feature = "optimize", optimize(size))] - fn build(&mut self) { - if self.depth < self.dimensions() - 1 { - for i in 0..self.shape()[self.depth] { - let mut position: Vec = self.position.to_vec(); - position[self.depth] = i; - let num_children: usize = if self.depth < self.dimensions() - 2 { - self.shape()[self.depth + 1] as usize - } else { - 0 - }; - self.children - .push(self.child_from_parent(position, num_children)) - } - } - } - - #[inline(always)] - fn data(&self) -> *const c_void { - let offset = self - .strides() - .iter() - .zip(self.position.iter().copied()) - .take(self.depth) - .map(|(a, b)| a * b) - .sum::(); - unsafe { (*self.array).data.offset(offset) } - } - - fn num_items(&self) -> usize { - self.shape()[self.shape().len() - 1] as usize - } - - fn dimensions(&self) -> usize { - unsafe { (*self.array).nd as usize } - } - - fn shape(&self) -> &[isize] { - slice!((*self.array).shape as *const isize, self.dimensions()) - } - - fn strides(&self) -> &[isize] { - slice!((*self.array).strides as *const isize, self.dimensions()) - } -} - -impl Drop for NumpyArray { - fn drop(&mut self) { - if self.depth == 0 { - ffi!(Py_DECREF(self.array as *mut pyo3_ffi::PyObject)); - ffi!(Py_DECREF(self.capsule as *mut pyo3_ffi::PyObject)); - } - } -} - -impl Serialize for NumpyArray { - #[cold] - #[inline(never)] - #[cfg_attr(feature = "optimize", optimize(size))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - if unlikely!(!(self.depth >= self.dimensions() || self.shape()[self.depth] != 0)) { - ZeroListSerializer::new().serialize(serializer) - } else if !self.children.is_empty() { - let mut seq = serializer.serialize_seq(None).unwrap(); - for child in &self.children { - seq.serialize_element(child).unwrap(); - } - seq.end() - } else { - match self.kind { - ItemType::F64 => { - NumpyF64Array::new(slice!(self.data() as *const f64, self.num_items())) - .serialize(serializer) - } - ItemType::F32 => { - NumpyF32Array::new(slice!(self.data() as *const f32, self.num_items())) - .serialize(serializer) - } - ItemType::F16 => { - NumpyF16Array::new(slice!(self.data() as *const u16, self.num_items())) - .serialize(serializer) - } - ItemType::U64 => { - NumpyU64Array::new(slice!(self.data() as *const u64, self.num_items())) - .serialize(serializer) - } - ItemType::U32 => { - NumpyU32Array::new(slice!(self.data() as *const u32, self.num_items())) - .serialize(serializer) - } - ItemType::U16 => { - NumpyU16Array::new(slice!(self.data() as *const u16, self.num_items())) - .serialize(serializer) - } - ItemType::U8 => { - NumpyU8Array::new(slice!(self.data() as *const u8, self.num_items())) - .serialize(serializer) - } - ItemType::I64 => { - NumpyI64Array::new(slice!(self.data() as *const i64, self.num_items())) - .serialize(serializer) - } - ItemType::I32 => { - NumpyI32Array::new(slice!(self.data() as *const i32, self.num_items())) - .serialize(serializer) - } - ItemType::I16 => { - NumpyI16Array::new(slice!(self.data() as *const i16, self.num_items())) - .serialize(serializer) - } - ItemType::I8 => { - NumpyI8Array::new(slice!(self.data() as *const i8, self.num_items())) - .serialize(serializer) - } - ItemType::BOOL => { - NumpyBoolArray::new(slice!(self.data() as *const u8, self.num_items())) - .serialize(serializer) - } - ItemType::DATETIME64(unit) => NumpyDatetime64Array::new( - slice!(self.data() as *const i64, self.num_items()), - unit, - self.opts, - ) - .serialize(serializer), - } - } - } -} - -#[repr(transparent)] -struct NumpyF64Array<'a> { - data: &'a [f64], -} - -impl<'a> NumpyF64Array<'a> { - fn new(data: &'a [f64]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyF64Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeF64 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeF64 { - obj: f64, -} - -impl Serialize for DataTypeF64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_f64(self.obj) - } -} - -#[repr(transparent)] -struct NumpyF32Array<'a> { - data: &'a [f32], -} - -impl<'a> NumpyF32Array<'a> { - fn new(data: &'a [f32]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyF32Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeF32 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -struct DataTypeF32 { - obj: f32, -} - -impl Serialize for DataTypeF32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_f32(self.obj) - } -} - -#[repr(transparent)] -struct NumpyF16Array<'a> { - data: &'a [u16], -} - -impl<'a> NumpyF16Array<'a> { - fn new(data: &'a [u16]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyF16Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeF16 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -struct DataTypeF16 { - obj: u16, -} - -impl Serialize for DataTypeF16 { - #[cold] - #[cfg_attr(feature = "optimize", optimize(size))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let as_f16 = half::f16::from_bits(self.obj); - serializer.serialize_f32(as_f16.to_f32()) - } -} - -#[repr(transparent)] -struct NumpyU64Array<'a> { - data: &'a [u64], -} - -impl<'a> NumpyU64Array<'a> { - fn new(data: &'a [u64]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyU64Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeU64 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeU64 { - obj: u64, -} - -impl Serialize for DataTypeU64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u64(self.obj) - } -} - -#[repr(transparent)] -struct NumpyU32Array<'a> { - data: &'a [u32], -} - -impl<'a> NumpyU32Array<'a> { - fn new(data: &'a [u32]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyU32Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeU32 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeU32 { - obj: u32, -} - -impl Serialize for DataTypeU32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.obj) - } -} - -#[repr(transparent)] -struct NumpyU16Array<'a> { - data: &'a [u16], -} - -impl<'a> NumpyU16Array<'a> { - fn new(data: &'a [u16]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyU16Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeU16 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeU16 { - obj: u16, -} - -impl Serialize for DataTypeU16 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.obj as u32) - } -} - -#[repr(transparent)] -struct NumpyI64Array<'a> { - data: &'a [i64], -} - -impl<'a> NumpyI64Array<'a> { - fn new(data: &'a [i64]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyI64Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeI64 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeI64 { - obj: i64, -} - -impl Serialize for DataTypeI64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i64(self.obj) - } -} - -#[repr(transparent)] -struct NumpyI32Array<'a> { - data: &'a [i32], -} - -impl<'a> NumpyI32Array<'a> { - fn new(data: &'a [i32]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyI32Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeI32 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeI32 { - obj: i32, -} - -impl Serialize for DataTypeI32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.obj) - } -} - -#[repr(transparent)] -struct NumpyI16Array<'a> { - data: &'a [i16], -} - -impl<'a> NumpyI16Array<'a> { - fn new(data: &'a [i16]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyI16Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeI16 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeI16 { - obj: i16, -} - -impl Serialize for DataTypeI16 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.obj as i32) - } -} - -#[repr(transparent)] -struct NumpyI8Array<'a> { - data: &'a [i8], -} - -impl<'a> NumpyI8Array<'a> { - fn new(data: &'a [i8]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyI8Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeI8 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeI8 { - obj: i8, -} - -impl Serialize for DataTypeI8 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.obj as i32) - } -} - -#[repr(transparent)] -struct NumpyU8Array<'a> { - data: &'a [u8], -} - -impl<'a> NumpyU8Array<'a> { - fn new(data: &'a [u8]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyU8Array<'a> { - #[cold] - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeU8 { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeU8 { - obj: u8, -} - -impl Serialize for DataTypeU8 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.obj as u32) - } -} - -#[repr(transparent)] -struct NumpyBoolArray<'a> { - data: &'a [u8], -} - -impl<'a> NumpyBoolArray<'a> { - fn new(data: &'a [u8]) -> Self { - Self { data } - } -} - -impl<'a> Serialize for NumpyBoolArray<'a> { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - seq.serialize_element(&DataTypeBool { obj: each }).unwrap(); - } - seq.end() - } -} - -#[repr(transparent)] -pub struct DataTypeBool { - obj: u8, -} - -impl Serialize for DataTypeBool { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_bool(self.obj == 1) - } -} - -pub struct NumpyScalar { - ptr: *mut pyo3_ffi::PyObject, - opts: Opt, -} - -impl NumpyScalar { - pub fn new(ptr: *mut PyObject, opts: Opt) -> Self { - NumpyScalar { ptr, opts } - } -} - -impl Serialize for NumpyScalar { - #[cold] - #[inline(never)] - #[cfg_attr(feature = "optimize", optimize(size))] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - unsafe { - let ob_type = ob_type!(self.ptr); - let scalar_types = - unsafe { NUMPY_TYPES.get_or_init(load_numpy_types).unwrap().as_ref() }; - if ob_type == scalar_types.float64 { - (*(self.ptr as *mut NumpyFloat64)).serialize(serializer) - } else if ob_type == scalar_types.float32 { - (*(self.ptr as *mut NumpyFloat32)).serialize(serializer) - } else if ob_type == scalar_types.float16 { - (*(self.ptr as *mut NumpyFloat16)).serialize(serializer) - } else if ob_type == scalar_types.int64 { - (*(self.ptr as *mut NumpyInt64)).serialize(serializer) - } else if ob_type == scalar_types.int32 { - (*(self.ptr as *mut NumpyInt32)).serialize(serializer) - } else if ob_type == scalar_types.int16 { - (*(self.ptr as *mut NumpyInt16)).serialize(serializer) - } else if ob_type == scalar_types.int8 { - (*(self.ptr as *mut NumpyInt8)).serialize(serializer) - } else if ob_type == scalar_types.uint64 { - (*(self.ptr as *mut NumpyUint64)).serialize(serializer) - } else if ob_type == scalar_types.uint32 { - (*(self.ptr as *mut NumpyUint32)).serialize(serializer) - } else if ob_type == scalar_types.uint16 { - (*(self.ptr as *mut NumpyUint16)).serialize(serializer) - } else if ob_type == scalar_types.uint8 { - (*(self.ptr as *mut NumpyUint8)).serialize(serializer) - } else if ob_type == scalar_types.bool_ { - (*(self.ptr as *mut NumpyBool)).serialize(serializer) - } else if ob_type == scalar_types.datetime64 { - let unit = NumpyDatetimeUnit::from_pyobject(self.ptr); - let obj = &*(self.ptr as *mut NumpyDatetime64); - let dt = unit - .datetime(obj.value, self.opts) - .map_err(NumpyDateTimeError::into_serde_err)?; - dt.serialize(serializer) - } else { - unreachable!() - } - } - } -} - -#[repr(C)] -pub struct NumpyInt8 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: i8, -} - -impl Serialize for NumpyInt8 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.value as i32) - } -} - -#[repr(C)] -pub struct NumpyInt16 { - pub ob_refcnt: Py_ssize_t, - pub ob_type: *mut PyTypeObject, - pub value: i16, -} - -impl Serialize for NumpyInt16 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.value as i32) - } -} - -#[repr(C)] -pub struct NumpyInt32 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: i32, -} - -impl Serialize for NumpyInt32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i32(self.value) - } -} - -#[repr(C)] -pub struct NumpyInt64 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: i64, -} - -impl Serialize for NumpyInt64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_i64(self.value) - } -} - -#[repr(C)] -pub struct NumpyUint8 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: u8, -} - -impl Serialize for NumpyUint8 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.value as u32) - } -} - -#[repr(C)] -pub struct NumpyUint16 { - pub ob_refcnt: Py_ssize_t, - pub ob_type: *mut PyTypeObject, - pub value: u16, -} - -impl Serialize for NumpyUint16 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.value as u32) - } -} - -#[repr(C)] -pub struct NumpyUint32 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: u32, -} - -impl Serialize for NumpyUint32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u32(self.value) - } -} - -#[repr(C)] -pub struct NumpyUint64 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: u64, -} - -impl Serialize for NumpyUint64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_u64(self.value) - } -} - -#[repr(C)] -pub struct NumpyFloat16 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: u16, -} - -impl Serialize for NumpyFloat16 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let as_f16 = half::f16::from_bits(self.value); - serializer.serialize_f32(as_f16.to_f32()) - } -} - -#[repr(C)] -pub struct NumpyFloat32 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: f32, -} - -impl Serialize for NumpyFloat32 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_f32(self.value) - } -} - -#[repr(C)] -pub struct NumpyFloat64 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: f64, -} - -impl Serialize for NumpyFloat64 { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_f64(self.value) - } -} - -#[repr(C)] -pub struct NumpyBool { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: bool, -} - -impl Serialize for NumpyBool { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_bool(self.value) - } -} - -/// This mimicks the units supported by numpy's datetime64 type. -/// -/// See -/// https://github.com/numpy/numpy/blob/fc8e3bbe419748ac5c6b7f3d0845e4bafa74644b/numpy/core/include/numpy/ndarraytypes.h#L268-L282. -#[derive(Clone, Copy)] -pub enum NumpyDatetimeUnit { - NaT, - Years, - Months, - Weeks, - Days, - Hours, - Minutes, - Seconds, - Milliseconds, - Microseconds, - Nanoseconds, - Picoseconds, - Femtoseconds, - Attoseconds, - Generic, -} - -impl fmt::Display for NumpyDatetimeUnit { - #[cold] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let unit = match self { - Self::NaT => "NaT", - Self::Years => "years", - Self::Months => "months", - Self::Weeks => "weeks", - Self::Days => "days", - Self::Hours => "hours", - Self::Minutes => "minutes", - Self::Seconds => "seconds", - Self::Milliseconds => "milliseconds", - Self::Microseconds => "microseconds", - Self::Nanoseconds => "nanoseconds", - Self::Picoseconds => "picoseconds", - Self::Femtoseconds => "femtoseconds", - Self::Attoseconds => "attoseconds", - Self::Generic => "generic", - }; - write!(f, "{}", unit) - } -} - -#[derive(Clone, Copy)] -enum NumpyDateTimeError { - UnsupportedUnit(NumpyDatetimeUnit), - Unrepresentable { unit: NumpyDatetimeUnit, val: i64 }, -} - -impl NumpyDateTimeError { - #[cold] - fn into_serde_err(self) -> T { - let err = match self { - Self::UnsupportedUnit(unit) => format!("unsupported numpy.datetime64 unit: {}", unit), - Self::Unrepresentable { unit, val } => { - format!("unrepresentable numpy.datetime64: {} {}", val, unit) - } - }; - ser::Error::custom(err) - } -} - -macro_rules! to_jiff_datetime { - ($timestamp:expr, $self:expr, $val:expr) => { - Ok( - ($timestamp.map_err(|_| NumpyDateTimeError::Unrepresentable { - unit: *$self, - val: $val, - })?) - .to_zoned(jiff::tz::TimeZone::UTC) - .datetime(), - ) - }; -} - -impl NumpyDatetimeUnit { - /// Create a `NumpyDatetimeUnit` from a pointer to a Python object holding a - /// numpy array. - /// - /// This function must only be called with pointers to numpy arrays. - /// - /// We need to look inside the `obj.dtype.descr` attribute of the Python - /// object rather than using the `descr` field of the `__array_struct__` - /// because that field isn't populated for datetime64 arrays; see - /// https://github.com/numpy/numpy/issues/5350. - #[cold] - #[cfg_attr(feature = "optimize", optimize(size))] - fn from_pyobject(ptr: *mut PyObject) -> Self { - let dtype = ffi!(PyObject_GetAttr(ptr, DTYPE_STR)); - let descr = ffi!(PyObject_GetAttr(dtype, DESCR_STR)); - let el0 = ffi!(PyList_GET_ITEM(descr, 0)); - let descr_str = ffi!(PyTuple_GET_ITEM(el0, 1)); - let uni = crate::str::unicode_to_str(descr_str).unwrap(); - if uni.len() < 5 { - return Self::NaT; - } - // unit descriptions are found at - // https://github.com/numpy/numpy/blob/b235f9e701e14ed6f6f6dcba885f7986a833743f/numpy/core/src/multiarray/datetime.c#L79-L96. - let ret = match &uni[4..uni.len() - 1] { - "Y" => Self::Years, - "M" => Self::Months, - "W" => Self::Weeks, - "D" => Self::Days, - "h" => Self::Hours, - "m" => Self::Minutes, - "s" => Self::Seconds, - "ms" => Self::Milliseconds, - "us" => Self::Microseconds, - "ns" => Self::Nanoseconds, - "ps" => Self::Picoseconds, - "fs" => Self::Femtoseconds, - "as" => Self::Attoseconds, - "generic" => Self::Generic, - _ => unreachable!(), - }; - ffi!(Py_DECREF(dtype)); - ffi!(Py_DECREF(descr)); - ret - } - - /// Return a `NumpyDatetime64Repr` for a value in array with this unit. - /// - /// Returns an `Err(NumpyDateTimeError)` if the value is invalid for this unit. - #[cold] - #[cfg_attr(feature = "optimize", optimize(size))] - fn datetime(&self, val: i64, opts: Opt) -> Result { - match self { - Self::Years => Ok(DateTime::new( - (val + 1970) - .try_into() - .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: *self, val })?, - 1, - 1, - 0, - 0, - 0, - 0, - ) - .unwrap()), - Self::Months => Ok(DateTime::new( - (val / 12 + 1970) - .try_into() - .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: *self, val })?, - (val % 12 + 1) - .try_into() - .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: *self, val })?, - 1, - 0, - 0, - 0, - 0, - ) - .unwrap()), - Self::Weeks => { - to_jiff_datetime!(Timestamp::from_second(val * 7 * 24 * 60 * 60), self, val) - } - Self::Days => to_jiff_datetime!(Timestamp::from_second(val * 24 * 60 * 60), self, val), - Self::Hours => to_jiff_datetime!(Timestamp::from_second(val * 60 * 60), self, val), - Self::Minutes => to_jiff_datetime!(Timestamp::from_second(val * 60), self, val), - Self::Seconds => to_jiff_datetime!(Timestamp::from_second(val), self, val), - Self::Milliseconds => to_jiff_datetime!(Timestamp::from_millisecond(val), self, val), - Self::Microseconds => to_jiff_datetime!(Timestamp::from_microsecond(val), self, val), - Self::Nanoseconds => { - to_jiff_datetime!(Timestamp::from_nanosecond(val as i128), self, val) - } - _ => Err(NumpyDateTimeError::UnsupportedUnit(*self)), - } - .map(|dt| NumpyDatetime64Repr { dt, opts }) - } -} - -struct NumpyDatetime64Array<'a> { - data: &'a [i64], - unit: NumpyDatetimeUnit, - opts: Opt, -} - -impl<'a> NumpyDatetime64Array<'a> { - fn new(data: &'a [i64], unit: NumpyDatetimeUnit, opts: Opt) -> Self { - Self { data, unit, opts } - } -} - -impl<'a> Serialize for NumpyDatetime64Array<'a> { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut seq = serializer.serialize_seq(None).unwrap(); - for &each in self.data.iter() { - let dt = self - .unit - .datetime(each, self.opts) - .map_err(NumpyDateTimeError::into_serde_err)?; - seq.serialize_element(&dt).unwrap(); - } - seq.end() - } -} - -#[repr(C)] -pub struct NumpyDatetime64 { - ob_refcnt: Py_ssize_t, - ob_type: *mut PyTypeObject, - value: i64, -} - -macro_rules! forward_inner { - ($meth: ident, $ty: ident) => { - fn $meth(&self) -> $ty { - self.dt.$meth() as $ty - } - }; -} - -struct NumpyDatetime64Repr { - dt: DateTime, - opts: Opt, -} - -impl DateTimeLike for NumpyDatetime64Repr { - forward_inner!(year, i32); - forward_inner!(month, u8); - forward_inner!(day, u8); - forward_inner!(hour, u8); - forward_inner!(minute, u8); - forward_inner!(second, u8); - - fn nanosecond(&self) -> u32 { - self.dt.subsec_nanosecond() as u32 - } - - fn microsecond(&self) -> u32 { - self.nanosecond() / 1_000 - } - - fn has_tz(&self) -> bool { - false - } - - fn slow_offset(&self) -> Result { - unreachable!() - } - - fn offset(&self) -> Result { - Ok(Offset::default()) - } -} - -impl Serialize for NumpyDatetime64Repr { - #[cold] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut buf = SmallFixedBuffer::new(); - let _ = self.write_buf(&mut buf, self.opts); - serializer.collect_str(str_from_slice!(buf.as_ptr(), buf.len())) - } -} diff --git a/vendor/orjson/src/serialize/per_type/pybool.rs b/vendor/orjson/src/serialize/per_type/pybool.rs deleted file mode 100644 index 6109693b73..0000000000 --- a/vendor/orjson/src/serialize/per_type/pybool.rs +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct BoolSerializer { - ptr: *mut pyo3_ffi::PyObject, -} - -impl BoolSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - BoolSerializer { ptr: ptr } - } -} - -impl Serialize for BoolSerializer { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_bool(unsafe { self.ptr == crate::typeref::TRUE }) - } -} diff --git a/vendor/orjson/src/serialize/per_type/pyenum.rs b/vendor/orjson/src/serialize/per_type/pyenum.rs deleted file mode 100644 index d84064d6f2..0000000000 --- a/vendor/orjson/src/serialize/per_type/pyenum.rs +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::serializer::PyObjectSerializer; -use crate::typeref::VALUE_STR; -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct EnumSerializer<'a> { - previous: &'a PyObjectSerializer, -} - -impl<'a> EnumSerializer<'a> { - pub fn new(previous: &'a PyObjectSerializer) -> Self { - Self { previous: previous } - } -} - -impl<'a> Serialize for EnumSerializer<'a> { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let value = ffi!(PyObject_GetAttr(self.previous.ptr, VALUE_STR)); - debug_assert!(ffi!(Py_REFCNT(value)) >= 2); - let ret = PyObjectSerializer::new(value, self.previous.state, self.previous.default) - .serialize(serializer); - ffi!(Py_DECREF(value)); - ret - } -} diff --git a/vendor/orjson/src/serialize/per_type/unicode.rs b/vendor/orjson/src/serialize/per_type/unicode.rs deleted file mode 100644 index ecc8ae6c59..0000000000 --- a/vendor/orjson/src/serialize/per_type/unicode.rs +++ /dev/null @@ -1,59 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::error::SerializeError; -use crate::str::{unicode_to_str, unicode_to_str_via_ffi}; - -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct StrSerializer { - ptr: *mut pyo3_ffi::PyObject, -} - -impl StrSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - StrSerializer { ptr: ptr } - } -} - -impl Serialize for StrSerializer { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let uni = { - let tmp = unicode_to_str(self.ptr); - if unlikely!(tmp.is_none()) { - err!(SerializeError::InvalidStr) - }; - tmp.unwrap() - }; - serializer.serialize_str(uni) - } -} - -#[repr(transparent)] -pub struct StrSubclassSerializer { - ptr: *mut pyo3_ffi::PyObject, -} - -impl StrSubclassSerializer { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - StrSubclassSerializer { ptr: ptr } - } -} - -impl Serialize for StrSubclassSerializer { - #[inline(never)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let uni = unicode_to_str_via_ffi(self.ptr); - if unlikely!(uni.is_none()) { - err!(SerializeError::InvalidStr) - } - serializer.serialize_str(uni.unwrap()) - } -} diff --git a/vendor/orjson/src/serialize/per_type/uuid.rs b/vendor/orjson/src/serialize/per_type/uuid.rs deleted file mode 100644 index f065fd2a69..0000000000 --- a/vendor/orjson/src/serialize/per_type/uuid.rs +++ /dev/null @@ -1,59 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::serialize::buffer::SmallFixedBuffer; -use crate::typeref::INT_ATTR_STR; -use core::ffi::c_uchar; -use serde::ser::{Serialize, Serializer}; - -#[repr(transparent)] -pub struct UUID { - ptr: *mut pyo3_ffi::PyObject, -} - -impl UUID { - pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self { - UUID { ptr: ptr } - } - - #[inline(never)] - pub fn write_buf(&self, buf: &mut SmallFixedBuffer) { - let value: u128; - { - // test_uuid_immutable, test_uuid_int - let py_int = ffi!(PyObject_GetAttr(self.ptr, INT_ATTR_STR)); - ffi!(Py_DECREF(py_int)); - let buffer: [c_uchar; 16] = [0; 16]; - unsafe { - // test_uuid_overflow - pyo3_ffi::_PyLong_AsByteArray( - py_int as *mut pyo3_ffi::PyLongObject, - buffer.as_ptr() as *mut c_uchar, - 16, - 1, // little_endian - 0, // is_signed - ) - }; - value = u128::from_le_bytes(buffer); - } - unsafe { - debug_assert!(buf.len() == 0); - let len = uuid::Uuid::from_u128(value) - .hyphenated() - .encode_lower(buf.as_mut_slice()) - .len(); - buf.set_written(len); - debug_assert!(buf.len() == len); - } - } -} -impl Serialize for UUID { - #[inline(always)] - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut buf = SmallFixedBuffer::new(); - self.write_buf(&mut buf); - serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) - } -} diff --git a/vendor/orjson/src/serialize/serializer.rs b/vendor/orjson/src/serialize/serializer.rs deleted file mode 100644 index 852d31e0b7..0000000000 --- a/vendor/orjson/src/serialize/serializer.rs +++ /dev/null @@ -1,108 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::{Opt, APPEND_NEWLINE, INDENT_2}; -use crate::serialize::obtype::{pyobject_to_obtype, ObType}; -use crate::serialize::per_type::{ - BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, - DictGenericSerializer, EnumSerializer, FloatSerializer, FragmentSerializer, IntSerializer, - ListTupleSerializer, NoneSerializer, NumpyScalar, NumpySerializer, StrSerializer, - StrSubclassSerializer, Time, ZeroListSerializer, UUID, -}; -use crate::serialize::state::SerializerState; -use crate::serialize::writer::{to_writer, to_writer_pretty, BytesWriter}; -use core::ptr::NonNull; -use serde::ser::{Serialize, Serializer}; -use std::io::Write; - -pub fn serialize( - ptr: *mut pyo3_ffi::PyObject, - default: Option>, - opts: Opt, -) -> Result, String> { - let mut buf = BytesWriter::default(); - let obj = PyObjectSerializer::new(ptr, SerializerState::new(opts), default); - let res = if opt_disabled!(opts, INDENT_2) { - to_writer(&mut buf, &obj) - } else { - to_writer_pretty(&mut buf, &obj) - }; - match res { - Ok(_) => { - if opt_enabled!(opts, APPEND_NEWLINE) { - let _ = buf.write(b"\n"); - } - Ok(buf.finish()) - } - Err(err) => { - ffi!(Py_DECREF(buf.bytes_ptr().as_ptr())); - Err(err.to_string()) - } - } -} - -pub struct PyObjectSerializer { - pub ptr: *mut pyo3_ffi::PyObject, - pub state: SerializerState, - pub default: Option>, -} - -impl PyObjectSerializer { - pub fn new( - ptr: *mut pyo3_ffi::PyObject, - state: SerializerState, - default: Option>, - ) -> Self { - PyObjectSerializer { - ptr: ptr, - state: state, - default: default, - } - } -} - -impl Serialize for PyObjectSerializer { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match pyobject_to_obtype(self.ptr, self.state.opts()) { - ObType::Str => StrSerializer::new(self.ptr).serialize(serializer), - ObType::StrSubclass => StrSubclassSerializer::new(self.ptr).serialize(serializer), - ObType::Int => IntSerializer::new(self.ptr, self.state.opts()).serialize(serializer), - ObType::None => NoneSerializer::new().serialize(serializer), - ObType::Float => FloatSerializer::new(self.ptr).serialize(serializer), - ObType::Bool => BoolSerializer::new(self.ptr).serialize(serializer), - ObType::Datetime => DateTime::new(self.ptr, self.state.opts()).serialize(serializer), - ObType::Date => Date::new(self.ptr).serialize(serializer), - ObType::Time => Time::new(self.ptr, self.state.opts()).serialize(serializer), - ObType::Uuid => UUID::new(self.ptr).serialize(serializer), - ObType::Dict => { - DictGenericSerializer::new(self.ptr, self.state, self.default).serialize(serializer) - } - ObType::List => { - if ffi!(Py_SIZE(self.ptr)) == 0 { - ZeroListSerializer::new().serialize(serializer) - } else { - ListTupleSerializer::from_list(self.ptr, self.state, self.default) - .serialize(serializer) - } - } - ObType::Tuple => { - if ffi!(Py_SIZE(self.ptr)) == 0 { - ZeroListSerializer::new().serialize(serializer) - } else { - ListTupleSerializer::from_tuple(self.ptr, self.state, self.default) - .serialize(serializer) - } - } - ObType::Dataclass => DataclassGenericSerializer::new(self).serialize(serializer), - ObType::Enum => EnumSerializer::new(self).serialize(serializer), - ObType::NumpyArray => NumpySerializer::new(self).serialize(serializer), - ObType::NumpyScalar => { - NumpyScalar::new(self.ptr, self.state.opts()).serialize(serializer) - } - ObType::Fragment => FragmentSerializer::new(self.ptr).serialize(serializer), - ObType::Unknown => DefaultSerializer::new(self).serialize(serializer), - } - } -} diff --git a/vendor/orjson/src/serialize/state.rs b/vendor/orjson/src/serialize/state.rs deleted file mode 100644 index 91b5b0aa1f..0000000000 --- a/vendor/orjson/src/serialize/state.rs +++ /dev/null @@ -1,59 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::opt::*; - -const RECURSION_SHIFT: usize = 24; -const RECURSION_MASK: u32 = 255 << RECURSION_SHIFT; - -const DEFAULT_SHIFT: usize = 16; -const DEFAULT_MASK: u32 = 255 << DEFAULT_SHIFT; - -#[repr(transparent)] -#[derive(Copy, Clone)] -pub struct SerializerState { - // recursion: u8, - // default_calls: u8, - // opts: u16, - state: u32, -} - -impl SerializerState { - #[inline(always)] - pub fn new(opts: Opt) -> Self { - debug_assert!(opts < u16::MAX as u32); - Self { state: opts } - } - - #[inline(always)] - pub fn opts(&self) -> u32 { - self.state - } - - #[inline(always)] - pub fn recursion_limit(&self) -> bool { - self.state & RECURSION_MASK == RECURSION_MASK - } - - #[inline(always)] - pub fn default_calls_limit(&self) -> bool { - self.state & DEFAULT_MASK == DEFAULT_MASK - } - - #[inline(always)] - pub fn copy_for_recursive_call(&self) -> Self { - let opt = self.state & !RECURSION_MASK; - let recursion = (((self.state & RECURSION_MASK) >> RECURSION_SHIFT) + 1) << RECURSION_SHIFT; - Self { - state: opt | recursion, - } - } - - #[inline(always)] - pub fn copy_for_default_call(&self) -> Self { - let opt = self.state & !DEFAULT_MASK; - let default_calls = (((self.state & DEFAULT_MASK) >> DEFAULT_SHIFT) + 1) << DEFAULT_SHIFT; - Self { - state: opt | default_calls, - } - } -} diff --git a/vendor/orjson/src/serialize/writer/byteswriter.rs b/vendor/orjson/src/serialize/writer/byteswriter.rs deleted file mode 100644 index dd121838d8..0000000000 --- a/vendor/orjson/src/serialize/writer/byteswriter.rs +++ /dev/null @@ -1,211 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::c_char; -use core::ptr::NonNull; -use pyo3_ffi::{ - PyBytesObject, PyBytes_FromStringAndSize, PyObject, PyVarObject, Py_ssize_t, _PyBytes_Resize, -}; -use std::io::Error; - -const BUFFER_LENGTH: usize = 1024; - -pub struct BytesWriter { - cap: usize, - len: usize, - bytes: *mut PyBytesObject, -} - -impl BytesWriter { - pub fn default() -> Self { - BytesWriter { - cap: BUFFER_LENGTH, - len: 0, - bytes: unsafe { - PyBytes_FromStringAndSize(core::ptr::null_mut(), BUFFER_LENGTH as isize) - as *mut PyBytesObject - }, - } - } - - pub fn bytes_ptr(&mut self) -> NonNull { - unsafe { NonNull::new_unchecked(self.bytes as *mut PyObject) } - } - - pub fn finish(&mut self) -> NonNull { - unsafe { - core::ptr::write(self.buffer_ptr(), 0); - (*self.bytes.cast::()).ob_size = self.len as Py_ssize_t; - self.resize(self.len); - self.bytes_ptr() - } - } - - fn buffer_ptr(&self) -> *mut u8 { - unsafe { - core::mem::transmute::<*mut [c_char; 1], *mut u8>(core::ptr::addr_of_mut!( - (*self.bytes).ob_sval - )) - .add(self.len) - } - } - - #[inline] - pub fn resize(&mut self, len: usize) { - self.cap = len; - unsafe { - #[allow(clippy::unnecessary_cast)] - _PyBytes_Resize( - core::ptr::addr_of_mut!(self.bytes) as *mut *mut PyBytesObject - as *mut *mut PyObject, - len as isize, - ); - } - } - - #[cold] - #[inline(never)] - fn grow(&mut self, len: usize) { - let mut cap = self.cap; - while len >= cap { - cap *= 2; - } - self.resize(cap); - } -} - -impl std::io::Write for BytesWriter { - fn write(&mut self, buf: &[u8]) -> Result { - let _ = self.write_all(buf); - Ok(buf.len()) - } - - fn write_all(&mut self, buf: &[u8]) -> Result<(), Error> { - let to_write = buf.len(); - let end_length = self.len + to_write; - if unlikely!(end_length >= self.cap) { - self.grow(end_length); - } - unsafe { - core::ptr::copy_nonoverlapping(buf.as_ptr(), self.buffer_ptr(), to_write); - }; - self.len = end_length; - Ok(()) - } - - fn flush(&mut self) -> Result<(), Error> { - Ok(()) - } -} - -// hack based on saethlin's research and patch in https://github.com/serde-rs/json/issues/766 -pub trait WriteExt: std::io::Write { - #[inline] - fn as_mut_buffer_ptr(&mut self) -> *mut u8 { - core::ptr::null_mut() - } - - #[inline] - fn reserve(&mut self, len: usize) { - let _ = len; - } - - #[inline] - fn has_capacity(&mut self, _len: usize) -> bool { - false - } - - #[inline] - fn set_written(&mut self, len: usize) { - let _ = len; - } - - #[inline] - fn write_str(&mut self, val: &str) -> Result<(), Error> { - let _ = val; - Ok(()) - } - - #[inline] - unsafe fn write_reserved_fragment(&mut self, val: &[u8]) -> Result<(), Error> { - let _ = val; - Ok(()) - } - - #[inline] - unsafe fn write_reserved_punctuation(&mut self, val: u8) -> Result<(), Error> { - let _ = val; - Ok(()) - } - - #[inline] - unsafe fn write_reserved_indent(&mut self, len: usize) -> Result<(), Error> { - let _ = len; - Ok(()) - } -} - -impl WriteExt for &mut BytesWriter { - #[inline(always)] - fn as_mut_buffer_ptr(&mut self) -> *mut u8 { - self.buffer_ptr() - } - - #[inline(always)] - fn reserve(&mut self, len: usize) { - let end_length = self.len + len; - if unlikely!(end_length >= self.cap) { - self.grow(end_length); - } - } - - #[inline] - fn has_capacity(&mut self, len: usize) -> bool { - self.len + len <= self.cap - } - - #[inline(always)] - fn set_written(&mut self, len: usize) { - self.len += len; - } - - fn write_str(&mut self, val: &str) -> Result<(), Error> { - let to_write = val.len(); - let end_length = self.len + to_write + 2; - if unlikely!(end_length >= self.cap) { - self.grow(end_length); - } - unsafe { - let ptr = self.buffer_ptr(); - core::ptr::write(ptr, b'"'); - core::ptr::copy_nonoverlapping(val.as_ptr(), ptr.add(1), to_write); - core::ptr::write(ptr.add(to_write + 1), b'"'); - }; - self.len = end_length; - Ok(()) - } - - unsafe fn write_reserved_fragment(&mut self, val: &[u8]) -> Result<(), Error> { - let to_write = val.len(); - unsafe { - core::ptr::copy_nonoverlapping(val.as_ptr(), self.buffer_ptr(), to_write); - }; - self.len += to_write; - Ok(()) - } - - #[inline(always)] - unsafe fn write_reserved_punctuation(&mut self, val: u8) -> Result<(), Error> { - unsafe { core::ptr::write(self.buffer_ptr(), val) }; - self.len += 1; - Ok(()) - } - - #[inline(always)] - unsafe fn write_reserved_indent(&mut self, len: usize) -> Result<(), Error> { - unsafe { - core::ptr::write_bytes(self.buffer_ptr(), b' ', len); - }; - self.len += len; - Ok(()) - } -} diff --git a/vendor/orjson/src/serialize/writer/formatter.rs b/vendor/orjson/src/serialize/writer/formatter.rs deleted file mode 100644 index 15971f3a65..0000000000 --- a/vendor/orjson/src/serialize/writer/formatter.rs +++ /dev/null @@ -1,430 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) -// This is an adaptation of `src/value/ser.rs` from serde-json. - -use crate::serialize::writer::WriteExt; -use std::io; - -macro_rules! debug_assert_has_capacity { - ($writer:expr) => { - debug_assert!($writer.has_capacity(4)) - }; -} - -pub trait Formatter { - #[inline] - fn write_null(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - writer.write_reserved_fragment(b"null") - } - } - - #[inline] - fn write_bool(&mut self, writer: &mut W, value: bool) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - let s = if value { - b"true" as &[u8] - } else { - b"false" as &[u8] - }; - reserve_minimum!(writer); - unsafe { writer.write_reserved_fragment(s) } - } - - fn write_i8(&mut self, _writer: &mut W, _value: i8) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - fn write_i16(&mut self, _writer: &mut W, _value: i16) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - #[inline] - fn write_i32(&mut self, writer: &mut W, value: i32) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = itoap::write_to_ptr(writer.as_mut_buffer_ptr(), value); - writer.set_written(len); - } - Ok(()) - } - - #[inline] - fn write_i64(&mut self, writer: &mut W, value: i64) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = itoap::write_to_ptr(writer.as_mut_buffer_ptr(), value); - writer.set_written(len); - } - Ok(()) - } - - fn write_i128(&mut self, _writer: &mut W, _value: i128) -> io::Result<()> - where - W: ?Sized + io::Write, - { - unreachable!(); - } - - fn write_u8(&mut self, _writer: &mut W, _value: u8) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - fn write_u16(&mut self, _writer: &mut W, _value: u16) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - #[inline] - fn write_u32(&mut self, writer: &mut W, value: u32) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = itoap::write_to_ptr(writer.as_mut_buffer_ptr(), value); - writer.set_written(len); - } - Ok(()) - } - - #[inline] - fn write_u64(&mut self, writer: &mut W, value: u64) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = itoap::write_to_ptr(writer.as_mut_buffer_ptr(), value); - writer.set_written(len); - } - Ok(()) - } - - fn write_u128(&mut self, _writer: &mut W, _value: u128) -> io::Result<()> - where - W: ?Sized + io::Write, - { - unreachable!(); - } - - #[inline] - fn write_f32(&mut self, writer: &mut W, value: f32) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = ryu::raw::format32(value, writer.as_mut_buffer_ptr()); - writer.set_written(len); - } - Ok(()) - } - - #[inline] - fn write_f64(&mut self, writer: &mut W, value: f64) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unsafe { - reserve_minimum!(writer); - let len = ryu::raw::format64(value, writer.as_mut_buffer_ptr()); - writer.set_written(len); - } - Ok(()) - } - - fn write_number_str(&mut self, _writer: &mut W, _value: &str) -> io::Result<()> - where - W: ?Sized + io::Write, - { - unreachable!(); - } - - #[inline] - fn begin_string(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - #[inline] - fn end_string(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - #[inline] - fn write_string_fragment(&mut self, _writer: &mut W, _fragment: &str) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - unreachable!(); - } - - #[inline] - fn begin_array(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - reserve_minimum!(writer); - unsafe { writer.write_reserved_punctuation(b'[').unwrap() }; - Ok(()) - } - - #[inline] - fn end_array(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - debug_assert_has_capacity!(writer); - unsafe { writer.write_reserved_punctuation(b']').unwrap() }; - Ok(()) - } - - #[inline] - fn begin_array_value(&mut self, writer: &mut W, first: bool) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - debug_assert_has_capacity!(writer); - if !first { - unsafe { writer.write_reserved_punctuation(b',').unwrap() } - } - Ok(()) - } - - #[inline] - fn end_array_value(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write, - { - Ok(()) - } - - #[inline] - fn begin_object(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - reserve_minimum!(writer); - unsafe { - writer.write_reserved_punctuation(b'{').unwrap(); - } - Ok(()) - } - - #[inline] - fn end_object(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - debug_assert_has_capacity!(writer); - unsafe { - writer.write_reserved_punctuation(b'}').unwrap(); - } - Ok(()) - } - - #[inline] - fn begin_object_key(&mut self, writer: &mut W, first: bool) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - debug_assert_has_capacity!(writer); - if !first { - unsafe { - writer.write_reserved_punctuation(b',').unwrap(); - } - } - Ok(()) - } - - #[inline] - fn end_object_key(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write, - { - Ok(()) - } - - #[inline] - fn begin_object_value(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - debug_assert_has_capacity!(writer); - unsafe { writer.write_reserved_punctuation(b':') } - } - - #[inline] - fn end_object_value(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write, - { - Ok(()) - } -} - -pub struct CompactFormatter; - -impl Formatter for CompactFormatter {} - -pub struct PrettyFormatter { - current_indent: usize, - has_value: bool, -} - -impl PrettyFormatter { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - PrettyFormatter { - current_indent: 0, - has_value: false, - } - } -} - -impl Formatter for PrettyFormatter { - #[inline] - fn begin_array(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - self.current_indent += 1; - self.has_value = false; - reserve_minimum!(writer); - unsafe { writer.write_reserved_punctuation(b'[') } - } - - #[inline] - fn end_array(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - self.current_indent -= 1; - let num_spaces = self.current_indent * 2; - reserve_pretty!(writer, num_spaces); - - unsafe { - if self.has_value { - writer.write_reserved_punctuation(b'\n')?; - writer.write_reserved_indent(num_spaces)?; - } - writer.write_reserved_punctuation(b']') - } - } - - #[inline] - fn begin_array_value(&mut self, writer: &mut W, first: bool) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - let num_spaces = self.current_indent * 2; - reserve_pretty!(writer, num_spaces); - - unsafe { - writer.write_reserved_fragment(if first { b"\n" } else { b",\n" })?; - writer.write_reserved_indent(num_spaces)?; - }; - Ok(()) - } - - #[inline] - fn end_array_value(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write, - { - self.has_value = true; - Ok(()) - } - - #[inline] - fn begin_object(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - self.current_indent += 1; - self.has_value = false; - - reserve_minimum!(writer); - unsafe { writer.write_reserved_punctuation(b'{') } - } - - #[inline] - fn end_object(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - self.current_indent -= 1; - let num_spaces = self.current_indent * 2; - reserve_pretty!(writer, num_spaces); - - unsafe { - if self.has_value { - writer.write_reserved_punctuation(b'\n')?; - writer.write_reserved_indent(num_spaces)?; - } - - writer.write_reserved_punctuation(b'}') - } - } - - #[inline] - fn begin_object_key(&mut self, writer: &mut W, first: bool) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - let num_spaces = self.current_indent * 2; - reserve_pretty!(writer, num_spaces); - unsafe { - writer.write_reserved_fragment(if first { b"\n" } else { b",\n" })?; - writer.write_reserved_indent(num_spaces)?; - } - Ok(()) - } - - #[inline] - fn begin_object_value(&mut self, writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write + WriteExt, - { - reserve_minimum!(writer); - unsafe { writer.write_reserved_fragment(b": ").unwrap() }; - Ok(()) - } - - #[inline] - fn end_object_value(&mut self, _writer: &mut W) -> io::Result<()> - where - W: ?Sized + io::Write, - { - self.has_value = true; - Ok(()) - } -} diff --git a/vendor/orjson/src/serialize/writer/json.rs b/vendor/orjson/src/serialize/writer/json.rs deleted file mode 100644 index cbb5fb338d..0000000000 --- a/vendor/orjson/src/serialize/writer/json.rs +++ /dev/null @@ -1,699 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) -// This is an adaptation of `src/value/ser.rs` from serde-json. - -use crate::serialize::writer::formatter::{CompactFormatter, Formatter, PrettyFormatter}; -use crate::serialize::writer::str::*; -use crate::serialize::writer::WriteExt; -use serde::ser::{self, Impossible, Serialize}; -use serde_json::error::{Error, Result}; -use std::io; - -pub struct Serializer { - writer: W, - formatter: F, -} - -impl Serializer -where - W: io::Write + WriteExt, -{ - #[inline] - pub fn new(writer: W) -> Self { - Serializer::with_formatter(writer, CompactFormatter) - } -} - -impl Serializer -where - W: io::Write + WriteExt, -{ - #[inline] - pub fn pretty(writer: W) -> Self { - Serializer::with_formatter(writer, PrettyFormatter::new()) - } -} - -impl Serializer -where - W: io::Write + WriteExt, - F: Formatter, -{ - #[inline] - pub fn with_formatter(writer: W, formatter: F) -> Self { - Serializer { writer, formatter } - } - - #[inline] - pub fn into_inner(self) -> W { - self.writer - } -} - -impl<'a, W, F> ser::Serializer for &'a mut Serializer -where - W: io::Write + WriteExt, - F: Formatter, -{ - type Ok = (); - type Error = Error; - - type SerializeSeq = Compound<'a, W, F>; - type SerializeTuple = Impossible<(), Error>; - type SerializeTupleStruct = Impossible<(), Error>; - type SerializeTupleVariant = Impossible<(), Error>; - type SerializeMap = Compound<'a, W, F>; - type SerializeStruct = Impossible<(), Error>; - type SerializeStructVariant = Impossible<(), Error>; - - #[inline] - fn serialize_bool(self, value: bool) -> Result<()> { - self.formatter - .write_bool(&mut self.writer, value) - .map_err(Error::io) - } - - fn serialize_i8(self, _value: i8) -> Result<()> { - unreachable!(); - } - - fn serialize_i16(self, _value: i16) -> Result<()> { - unreachable!(); - } - - #[inline] - fn serialize_i32(self, value: i32) -> Result<()> { - self.formatter - .write_i32(&mut self.writer, value) - .map_err(Error::io) - } - - #[inline] - fn serialize_i64(self, value: i64) -> Result<()> { - self.formatter - .write_i64(&mut self.writer, value) - .map_err(Error::io) - } - - fn serialize_i128(self, _value: i128) -> Result<()> { - unreachable!(); - } - - fn serialize_u8(self, _value: u8) -> Result<()> { - unreachable!(); - } - - fn serialize_u16(self, _value: u16) -> Result<()> { - unreachable!(); - } - - #[inline] - fn serialize_u32(self, value: u32) -> Result<()> { - self.formatter - .write_u32(&mut self.writer, value) - .map_err(Error::io) - } - - #[inline] - fn serialize_u64(self, value: u64) -> Result<()> { - self.formatter - .write_u64(&mut self.writer, value) - .map_err(Error::io) - } - - fn serialize_u128(self, _value: u128) -> Result<()> { - unreachable!(); - } - - #[inline] - fn serialize_f32(self, value: f32) -> Result<()> { - if unlikely!(value.is_infinite() || value.is_nan()) { - self.serialize_unit() - } else { - self.formatter - .write_f32(&mut self.writer, value) - .map_err(Error::io) - } - } - #[inline] - fn serialize_f64(self, value: f64) -> Result<()> { - if unlikely!(value.is_infinite() || value.is_nan()) { - self.serialize_unit() - } else { - self.formatter - .write_f64(&mut self.writer, value) - .map_err(Error::io) - } - } - - fn serialize_char(self, _value: char) -> Result<()> { - unreachable!(); - } - - #[inline(always)] - fn serialize_str(self, value: &str) -> Result<()> { - format_escaped_str(&mut self.writer, value); - Ok(()) - } - - #[inline(always)] - fn serialize_bytes(self, value: &[u8]) -> Result<()> { - self.writer.reserve(value.len() + 32); - unsafe { self.writer.write_reserved_fragment(value).unwrap() }; - Ok(()) - } - - #[inline] - fn serialize_unit(self) -> Result<()> { - self.formatter - .write_null(&mut self.writer) - .map_err(Error::io) - } - - #[inline(always)] - fn serialize_unit_struct(self, name: &'static str) -> Result<()> { - debug_assert!(name.len() <= 36); - reserve_minimum!(self.writer); - unsafe { - self.writer.write_reserved_punctuation(b'"').unwrap(); - self.writer - .write_reserved_fragment(name.as_bytes()) - .unwrap(); - self.writer.write_reserved_punctuation(b'"').unwrap(); - } - Ok(()) - } - - fn serialize_unit_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - ) -> Result<()> { - unreachable!(); - } - - fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - unreachable!(); - } - - fn serialize_newtype_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T, - ) -> Result<()> - where - T: ?Sized + Serialize, - { - unreachable!(); - } - - #[inline] - fn serialize_none(self) -> Result<()> { - self.serialize_unit() - } - - #[inline] - fn serialize_some(self, value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - value.serialize(self) - } - - #[inline(always)] - fn serialize_seq(self, _len: Option) -> Result { - self.formatter - .begin_array(&mut self.writer) - .map_err(Error::io)?; - Ok(Compound { - ser: self, - state: State::First, - }) - } - - fn serialize_tuple(self, _len: usize) -> Result { - unreachable!(); - } - - fn serialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } - - fn serialize_tuple_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } - - #[inline(always)] - fn serialize_map(self, _len: Option) -> Result { - self.formatter - .begin_object(&mut self.writer) - .map_err(Error::io)?; - Ok(Compound { - ser: self, - state: State::First, - }) - } - - fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { - unreachable!(); - } - - fn serialize_struct_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } -} - -#[derive(Eq, PartialEq)] -pub enum State { - First, - Rest, -} - -pub struct Compound<'a, W: 'a, F: 'a> { - ser: &'a mut Serializer, - state: State, -} - -impl<'a, W, F> ser::SerializeSeq for Compound<'a, W, F> -where - W: io::Write + WriteExt, - F: Formatter, -{ - type Ok = (); - type Error = Error; - - #[inline] - fn serialize_element(&mut self, value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - self.ser - .formatter - .begin_array_value(&mut self.ser.writer, self.state == State::First) - .unwrap(); - self.state = State::Rest; - value.serialize(&mut *self.ser)?; - self.ser - .formatter - .end_array_value(&mut self.ser.writer) - .map_err(Error::io) - .unwrap(); - Ok(()) - } - - #[inline] - fn end(self) -> Result<()> { - self.ser.formatter.end_array(&mut self.ser.writer).unwrap(); - Ok(()) - } -} - -impl<'a, W, F> ser::SerializeMap for Compound<'a, W, F> -where - W: io::Write + WriteExt, - F: Formatter, -{ - type Ok = (); - type Error = Error; - - fn serialize_entry(&mut self, _key: &K, _value: &V) -> Result<()> - where - K: ?Sized + Serialize, - V: ?Sized + Serialize, - { - unreachable!() - } - - #[inline] - fn serialize_key(&mut self, key: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - self.ser - .formatter - .begin_object_key(&mut self.ser.writer, self.state == State::First) - .unwrap(); - self.state = State::Rest; - - key.serialize(MapKeySerializer { ser: self.ser })?; - - self.ser - .formatter - .end_object_key(&mut self.ser.writer) - .unwrap(); - Ok(()) - } - - #[inline] - fn serialize_value(&mut self, value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - self.ser - .formatter - .begin_object_value(&mut self.ser.writer) - .unwrap(); - value.serialize(&mut *self.ser)?; - self.ser - .formatter - .end_object_value(&mut self.ser.writer) - .unwrap(); - Ok(()) - } - - #[inline] - fn end(self) -> Result<()> { - self.ser.formatter.end_object(&mut self.ser.writer).unwrap(); - Ok(()) - } -} - -#[repr(transparent)] -struct MapKeySerializer<'a, W: 'a, F: 'a> { - ser: &'a mut Serializer, -} - -impl<'a, W, F> ser::Serializer for MapKeySerializer<'a, W, F> -where - W: io::Write + WriteExt, - F: Formatter, -{ - type Ok = (); - type Error = Error; - type SerializeSeq = Impossible<(), Error>; - type SerializeTuple = Impossible<(), Error>; - type SerializeTupleStruct = Impossible<(), Error>; - type SerializeTupleVariant = Impossible<(), Error>; - type SerializeMap = Impossible<(), Error>; - type SerializeStruct = Impossible<(), Error>; - type SerializeStructVariant = Impossible<(), Error>; - - #[inline(always)] - fn serialize_str(self, value: &str) -> Result<()> { - self.ser.serialize_str(value) - } - - fn serialize_unit_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - ) -> Result<()> { - unreachable!(); - } - - fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - unreachable!(); - } - fn serialize_bool(self, _value: bool) -> Result<()> { - unreachable!(); - } - - fn serialize_i8(self, _value: i8) -> Result<()> { - unreachable!(); - } - - fn serialize_i16(self, _value: i16) -> Result<()> { - unreachable!(); - } - - fn serialize_i32(self, _value: i32) -> Result<()> { - unreachable!(); - } - - fn serialize_i64(self, _value: i64) -> Result<()> { - unreachable!(); - } - - fn serialize_i128(self, _value: i128) -> Result<()> { - unreachable!(); - } - - fn serialize_u8(self, _value: u8) -> Result<()> { - unreachable!(); - } - - fn serialize_u16(self, _value: u16) -> Result<()> { - unreachable!(); - } - - fn serialize_u32(self, _value: u32) -> Result<()> { - unreachable!(); - } - - fn serialize_u64(self, _value: u64) -> Result<()> { - unreachable!(); - } - - fn serialize_u128(self, _value: u128) -> Result<()> { - unreachable!(); - } - - fn serialize_f32(self, _value: f32) -> Result<()> { - unreachable!(); - } - - fn serialize_f64(self, _value: f64) -> Result<()> { - unreachable!(); - } - - fn serialize_char(self, _value: char) -> Result<()> { - unreachable!(); - } - - fn serialize_bytes(self, _value: &[u8]) -> Result<()> { - unreachable!(); - } - - fn serialize_unit(self) -> Result<()> { - unreachable!(); - } - - fn serialize_unit_struct(self, _name: &'static str) -> Result<()> { - unreachable!(); - } - - fn serialize_newtype_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _value: &T, - ) -> Result<()> - where - T: ?Sized + Serialize, - { - unreachable!(); - } - - fn serialize_none(self) -> Result<()> { - unreachable!(); - } - - fn serialize_some(self, _value: &T) -> Result<()> - where - T: ?Sized + Serialize, - { - unreachable!(); - } - - fn serialize_seq(self, _len: Option) -> Result { - unreachable!(); - } - - fn serialize_tuple(self, _len: usize) -> Result { - unreachable!(); - } - - fn serialize_tuple_struct( - self, - _name: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } - - fn serialize_tuple_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } - - fn serialize_map(self, _len: Option) -> Result { - unreachable!(); - } - - fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { - unreachable!(); - } - - fn serialize_struct_variant( - self, - _name: &'static str, - _variant_index: u32, - _variant: &'static str, - _len: usize, - ) -> Result { - unreachable!(); - } -} - -macro_rules! reserve_str { - ($writer:expr, $value:expr) => { - $writer.reserve($value.len() * 8 + 32); - }; -} - -#[cfg(all(feature = "unstable-simd", not(target_arch = "x86_64")))] -#[inline(always)] -fn format_escaped_str(writer: &mut W, value: &str) -where - W: ?Sized + io::Write + WriteExt, -{ - unsafe { - reserve_str!(writer, value); - - let written = format_escaped_str_impl_generic_128( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - - writer.set_written(written); - } -} - -#[cfg(all( - feature = "unstable-simd", - target_arch = "x86_64", - not(feature = "avx512") -))] -#[inline(always)] -fn format_escaped_str(writer: &mut W, value: &str) -where - W: ?Sized + io::Write + WriteExt, -{ - unsafe { - reserve_str!(writer, value); - - let written = format_escaped_str_impl_sse2_128( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - - writer.set_written(written); - } -} - -#[cfg(all(feature = "unstable-simd", target_arch = "x86_64", feature = "avx512"))] -#[inline(always)] -fn format_escaped_str(writer: &mut W, value: &str) -where - W: ?Sized + io::Write + WriteExt, -{ - unsafe { - reserve_str!(writer, value); - - if std::is_x86_feature_detected!("avx512vl") { - let written = format_escaped_str_impl_512vl( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - writer.set_written(written); - } else { - let written = format_escaped_str_impl_sse2_128( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - writer.set_written(written); - }; - } -} - -#[cfg(all(not(feature = "unstable-simd"), not(target_arch = "x86_64")))] -#[inline(always)] -fn format_escaped_str(writer: &mut W, value: &str) -where - W: ?Sized + io::Write + WriteExt, -{ - unsafe { - reserve_str!(writer, value); - - let written = format_escaped_str_scalar( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - writer.set_written(written); - } -} - -#[cfg(all(not(feature = "unstable-simd"), target_arch = "x86_64"))] -#[inline(always)] -fn format_escaped_str(writer: &mut W, value: &str) -where - W: ?Sized + io::Write + WriteExt, -{ - unsafe { - reserve_str!(writer, value); - - let written = format_escaped_str_impl_sse2_128( - writer.as_mut_buffer_ptr(), - value.as_bytes().as_ptr(), - value.len(), - ); - - writer.set_written(written); - } -} - -#[inline] -pub fn to_writer(writer: W, value: &T) -> Result<()> -where - W: io::Write + WriteExt, - T: ?Sized + Serialize, -{ - let mut ser = Serializer::new(writer); - value.serialize(&mut ser) -} - -#[inline] -pub fn to_writer_pretty(writer: W, value: &T) -> Result<()> -where - W: io::Write + WriteExt, - T: ?Sized + Serialize, -{ - let mut ser = Serializer::pretty(writer); - value.serialize(&mut ser) -} diff --git a/vendor/orjson/src/serialize/writer/mod.rs b/vendor/orjson/src/serialize/writer/mod.rs deleted file mode 100644 index 3e945b2c03..0000000000 --- a/vendor/orjson/src/serialize/writer/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -mod byteswriter; -mod formatter; -mod json; -mod str; - -pub use byteswriter::{BytesWriter, WriteExt}; -pub use json::{to_writer, to_writer_pretty}; diff --git a/vendor/orjson/src/serialize/writer/str/avx512.rs b/vendor/orjson/src/serialize/writer/str/avx512.rs deleted file mode 100644 index 1be47623a9..0000000000 --- a/vendor/orjson/src/serialize/writer/str/avx512.rs +++ /dev/null @@ -1,116 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::mem::transmute; - -use super::escape::QUOTE_TAB; - -use core::arch::x86_64::{ - __m256i, _mm256_cmpeq_epu8_mask, _mm256_cmplt_epu8_mask, _mm256_load_si256, _mm256_loadu_si256, - _mm256_maskz_loadu_epi8, _mm256_storeu_epi8, -}; - -#[repr(C, align(32))] -struct ConstArray { - pub data: [u8; 32], -} - -const BLASH: ConstArray = ConstArray { data: [b'\\'; 32] }; -const QUOTE: ConstArray = ConstArray { data: [b'"'; 32] }; -const X20: ConstArray = ConstArray { data: [32; 32] }; - -macro_rules! impl_format_simd_avx512vl { - ($dst:expr, $src:expr, $value_len:expr) => { - let mut nb: usize = $value_len; - - let blash = _mm256_load_si256(BLASH.data.as_ptr() as *const __m256i); - let quote = _mm256_load_si256(QUOTE.data.as_ptr() as *const __m256i); - let x20 = _mm256_load_si256(X20.data.as_ptr() as *const __m256i); - - unsafe { - while nb >= STRIDE { - let str_vec = _mm256_loadu_si256(transmute::<*const u8, *const __m256i>($src)); - - _mm256_storeu_epi8($dst as *mut i8, str_vec); - - let mask = _mm256_cmpeq_epu8_mask(str_vec, blash) - | _mm256_cmpeq_epu8_mask(str_vec, quote) - | _mm256_cmplt_epu8_mask(str_vec, x20); - - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask); - $src = $src.add(cn); - $dst = $dst.add(cn); - nb -= cn; - nb -= 1; - - let escape = QUOTE_TAB[*($src) as usize]; - $src = $src.add(1); - - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - } else { - nb -= STRIDE; - $dst = $dst.add(STRIDE); - $src = $src.add(STRIDE); - } - } - - if nb > 0 { - loop { - let remainder_mask = !(u32::MAX << nb); - let str_vec = _mm256_maskz_loadu_epi8(remainder_mask, $src as *const i8); - - _mm256_storeu_epi8($dst as *mut i8, str_vec); - - let mask = (_mm256_cmpeq_epu8_mask(str_vec, blash) - | _mm256_cmpeq_epu8_mask(str_vec, quote) - | _mm256_cmplt_epu8_mask(str_vec, x20)) - & remainder_mask; - - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask); - $src = $src.add(cn); - $dst = $dst.add(cn); - nb -= cn; - nb -= 1; - - let escape = QUOTE_TAB[*($src) as usize]; - $src = $src.add(1); - - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - } else { - $dst = $dst.add(nb); - break; - } - } - } - } - }; -} - -#[inline(never)] -#[cfg_attr( - feature = "avx512", - target_feature(enable = "avx512f,avx512bw,avx512vl,bmi2") -)] -pub unsafe fn format_escaped_str_impl_512vl( - odst: *mut u8, - value_ptr: *const u8, - value_len: usize, -) -> usize { - const STRIDE: usize = 32; - - let mut dst = odst; - let mut src = value_ptr; - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - impl_format_simd_avx512vl!(dst, src, value_len); - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - dst as usize - odst as usize -} diff --git a/vendor/orjson/src/serialize/writer/str/escape.rs b/vendor/orjson/src/serialize/writer/str/escape.rs deleted file mode 100644 index b1c8e0e81f..0000000000 --- a/vendor/orjson/src/serialize/writer/str/escape.rs +++ /dev/null @@ -1,118 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) -// the constants and SIMD approach are adapted from cloudwego's sonic-rs - -macro_rules! write_escape { - ($escape:expr, $dst:expr) => { - core::ptr::copy_nonoverlapping($escape.0.as_ptr(), $dst, 8); - }; -} - -pub const NEED_ESCAPED: [u8; 256] = [ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -]; - -pub const QUOTE_TAB: [([u8; 7], u8); 96] = [ - (*b"\\u0000\0", 6), - (*b"\\u0001\0", 6), - (*b"\\u0002\0", 6), - (*b"\\u0003\0", 6), - (*b"\\u0004\0", 6), - (*b"\\u0005\0", 6), - (*b"\\u0006\0", 6), - (*b"\\u0007\0", 6), - (*b"\\b\0\0\0\0\0", 2), - (*b"\\t\0\0\0\0\0", 2), - (*b"\\n\0\0\0\0\0", 2), - (*b"\\u000b\0", 6), - (*b"\\f\0\0\0\0\0", 2), - (*b"\\r\0\0\0\0\0", 2), - (*b"\\u000e\0", 6), - (*b"\\u000f\0", 6), - (*b"\\u0010\0", 6), - (*b"\\u0011\0", 6), - (*b"\\u0012\0", 6), - (*b"\\u0013\0", 6), - (*b"\\u0014\0", 6), - (*b"\\u0015\0", 6), - (*b"\\u0016\0", 6), - (*b"\\u0017\0", 6), - (*b"\\u0018\0", 6), - (*b"\\u0019\0", 6), - (*b"\\u001a\0", 6), - (*b"\\u001b\0", 6), - (*b"\\u001c\0", 6), - (*b"\\u001d\0", 6), - (*b"\\u001e\0", 6), - (*b"\\u001f\0", 6), - ([0; 7], 0), - ([0; 7], 0), - (*b"\\\"\0\0\0\0\0", 2), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), - (*b"\\\\\0\0\0\0\0", 2), - ([0; 7], 0), - ([0; 7], 0), - ([0; 7], 0), -]; diff --git a/vendor/orjson/src/serialize/writer/str/generic.rs b/vendor/orjson/src/serialize/writer/str/generic.rs deleted file mode 100644 index 0ca500f0f1..0000000000 --- a/vendor/orjson/src/serialize/writer/str/generic.rs +++ /dev/null @@ -1,109 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use super::escape::{NEED_ESCAPED, QUOTE_TAB}; -use core::simd::cmp::{SimdPartialEq, SimdPartialOrd}; - -macro_rules! impl_format_simd_generic_128 { - ($dst:expr, $src:expr, $value_len:expr) => { - let last_stride_src = $src.add($value_len).sub(STRIDE); - let mut nb: usize = $value_len; - - assume!($value_len >= STRIDE); - - const BLASH: StrVector = StrVector::from_array([b'\\'; STRIDE]); - const QUOTE: StrVector = StrVector::from_array([b'"'; STRIDE]); - const X20: StrVector = StrVector::from_array([32; STRIDE]); - - unsafe { - { - while nb >= STRIDE { - let v = StrVector::from_slice(core::slice::from_raw_parts($src, STRIDE)); - let mask = - (v.simd_eq(BLASH) | v.simd_eq(QUOTE) | v.simd_lt(X20)).to_bitmask() as u32; - v.copy_to_slice(core::slice::from_raw_parts_mut($dst, STRIDE)); - - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask) as usize; - nb -= cn; - $dst = $dst.add(cn); - $src = $src.add(cn); - nb -= 1; - let escape = QUOTE_TAB[*($src) as usize]; - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - $src = $src.add(1); - } else { - nb -= STRIDE; - $dst = $dst.add(STRIDE); - $src = $src.add(STRIDE); - } - } - } - - if nb > 0 { - let mut scratch: [u8; 32] = [b'a'; 32]; - let mut v = - StrVector::from_slice(core::slice::from_raw_parts(last_stride_src, STRIDE)); - v.copy_to_slice(core::slice::from_raw_parts_mut( - scratch.as_mut_ptr(), - STRIDE, - )); - - let mut scratch_ptr = scratch.as_mut_ptr().add(16 - nb); - v = StrVector::from_slice(core::slice::from_raw_parts(scratch_ptr, STRIDE)); - let mut mask = - (v.simd_eq(BLASH) | v.simd_eq(QUOTE) | v.simd_lt(X20)).to_bitmask() as u32; - - while nb > 0 { - v.copy_to_slice(core::slice::from_raw_parts_mut($dst, STRIDE)); - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask) as usize; - nb -= cn; - $dst = $dst.add(cn); - scratch_ptr = scratch_ptr.add(cn); - nb -= 1; - mask >>= cn + 1; - let escape = QUOTE_TAB[*(scratch_ptr) as usize]; - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - scratch_ptr = scratch_ptr.add(1); - v = StrVector::from_slice(core::slice::from_raw_parts(scratch_ptr, STRIDE)); - } else { - $dst = $dst.add(nb); - break; - } - } - } - } - }; -} - -#[allow(dead_code)] -#[inline(never)] -#[cfg_attr(target_arch = "x86_64", target_feature(enable = "sse2,bmi1"))] -#[cfg_attr(target_arch = "aarch64", target_feature(enable = "neon"))] -pub unsafe fn format_escaped_str_impl_generic_128( - odst: *mut u8, - value_ptr: *const u8, - value_len: usize, -) -> usize { - const STRIDE: usize = 16; - type StrVector = core::simd::u8x16; - - let mut dst = odst; - let mut src = value_ptr; - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - if value_len < STRIDE { - impl_format_scalar!(dst, src, value_len) - } else { - impl_format_simd_generic_128!(dst, src, value_len); - } - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - dst as usize - odst as usize -} diff --git a/vendor/orjson/src/serialize/writer/str/mod.rs b/vendor/orjson/src/serialize/writer/str/mod.rs deleted file mode 100644 index 6b18f06660..0000000000 --- a/vendor/orjson/src/serialize/writer/str/mod.rs +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#[macro_use] -mod escape; -#[macro_use] -mod scalar; - -#[cfg(target_arch = "x86_64")] -mod sse2; - -#[cfg(all(feature = "unstable-simd", target_arch = "x86_64", feature = "avx512"))] -mod avx512; - -#[cfg(feature = "unstable-simd")] -mod generic; - -#[cfg(all(not(feature = "unstable-simd"), not(target_arch = "x86_64")))] -pub use scalar::format_escaped_str_scalar; - -#[allow(unused_imports)] -#[cfg(feature = "unstable-simd")] -pub use generic::format_escaped_str_impl_generic_128; - -#[cfg(all(feature = "unstable-simd", target_arch = "x86_64", feature = "avx512"))] -pub use avx512::format_escaped_str_impl_512vl; - -#[allow(unused_imports)] -#[cfg(target_arch = "x86_64")] -pub use sse2::format_escaped_str_impl_sse2_128; diff --git a/vendor/orjson/src/serialize/writer/str/scalar.rs b/vendor/orjson/src/serialize/writer/str/scalar.rs deleted file mode 100644 index 2915080941..0000000000 --- a/vendor/orjson/src/serialize/writer/str/scalar.rs +++ /dev/null @@ -1,41 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -#[cfg(all(not(feature = "unstable-simd"), not(target_arch = "x86_64")))] -use super::escape::{NEED_ESCAPED, QUOTE_TAB}; - -macro_rules! impl_format_scalar { - ($dst:expr, $src:expr, $value_len:expr) => { - unsafe { - for _ in 0..$value_len { - core::ptr::write($dst, *($src)); - $src = $src.add(1); - $dst = $dst.add(1); - if unlikely!(NEED_ESCAPED[*($src.sub(1)) as usize] > 0) { - let escape = QUOTE_TAB[*($src.sub(1)) as usize]; - write_escape!(escape, $dst.sub(1)); - $dst = $dst.add(escape.1 as usize - 1); - } - } - } - }; -} - -#[cfg(all(not(feature = "unstable-simd"), not(target_arch = "x86_64")))] -pub unsafe fn format_escaped_str_scalar( - odst: *mut u8, - value_ptr: *const u8, - value_len: usize, -) -> usize { - let mut dst = odst; - let mut src = value_ptr; - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - impl_format_scalar!(dst, src, value_len); - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - dst as usize - odst as usize -} diff --git a/vendor/orjson/src/serialize/writer/str/sse2.rs b/vendor/orjson/src/serialize/writer/str/sse2.rs deleted file mode 100644 index 45114e0ca3..0000000000 --- a/vendor/orjson/src/serialize/writer/str/sse2.rs +++ /dev/null @@ -1,127 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -use super::escape::{NEED_ESCAPED, QUOTE_TAB}; - -use core::mem::transmute; - -use core::arch::x86_64::{ - __m128i, _mm_cmpeq_epi8, _mm_loadu_si128, _mm_movemask_epi8, _mm_or_si128, _mm_set1_epi8, - _mm_setzero_si128, _mm_storeu_si128, _mm_subs_epu8, -}; - -macro_rules! splat_mm128 { - ($val:expr) => { - _mm_set1_epi8(transmute::($val)) - }; -} - -macro_rules! impl_format_simd_sse2_128 { - ($dst:expr, $src:expr, $value_len:expr) => { - let last_stride_src = $src.add($value_len).sub(STRIDE); - let mut nb: usize = $value_len; - - assume!($value_len >= STRIDE); - - let blash = splat_mm128!(b'\\'); - let quote = splat_mm128!(b'"'); - let x20 = splat_mm128!(31); - let v0 = _mm_setzero_si128(); - - unsafe { - while nb >= STRIDE { - let str_vec = _mm_loadu_si128($src as *const __m128i); - - let mask = _mm_movemask_epi8(_mm_or_si128( - _mm_or_si128( - _mm_cmpeq_epi8(str_vec, blash), - _mm_cmpeq_epi8(str_vec, quote), - ), - _mm_cmpeq_epi8(_mm_subs_epu8(str_vec, x20), v0), - )) as u32; - - _mm_storeu_si128($dst as *mut __m128i, str_vec); - - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask) as usize; - nb -= cn; - $dst = $dst.add(cn); - $src = $src.add(cn); - nb -= 1; - let escape = QUOTE_TAB[*($src) as usize]; - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - $src = $src.add(1); - } else { - nb -= STRIDE; - $dst = $dst.add(STRIDE); - $src = $src.add(STRIDE); - } - } - - if nb > 0 { - let mut scratch: [u8; 32] = [b'a'; 32]; - let mut str_vec = _mm_loadu_si128(last_stride_src as *const __m128i); - _mm_storeu_si128(scratch.as_mut_ptr() as *mut __m128i, str_vec); - - let mut scratch_ptr = scratch.as_mut_ptr().add(16 - nb); - str_vec = _mm_loadu_si128(scratch_ptr as *const __m128i); - - let mut mask = _mm_movemask_epi8(_mm_or_si128( - _mm_or_si128( - _mm_cmpeq_epi8(str_vec, blash), - _mm_cmpeq_epi8(str_vec, quote), - ), - _mm_cmpeq_epi8(_mm_subs_epu8(str_vec, x20), v0), - )) as u32; - - while nb > 0 { - _mm_storeu_si128($dst as *mut __m128i, str_vec); - - if unlikely!(mask > 0) { - let cn = trailing_zeros!(mask) as usize; - nb -= cn; - $dst = $dst.add(cn); - scratch_ptr = scratch_ptr.add(cn); - nb -= 1; - mask >>= cn + 1; - let escape = QUOTE_TAB[*(scratch_ptr) as usize]; - write_escape!(escape, $dst); - $dst = $dst.add(escape.1 as usize); - scratch_ptr = scratch_ptr.add(1); - str_vec = _mm_loadu_si128(scratch_ptr as *const __m128i); - } else { - $dst = $dst.add(nb); - break; - } - } - } - } - }; -} - -#[allow(dead_code)] -#[inline(never)] -pub unsafe fn format_escaped_str_impl_sse2_128( - odst: *mut u8, - value_ptr: *const u8, - value_len: usize, -) -> usize { - const STRIDE: usize = 16; - - let mut dst = odst; - let mut src = value_ptr; - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - if value_len < STRIDE { - impl_format_scalar!(dst, src, value_len) - } else { - impl_format_simd_sse2_128!(dst, src, value_len); - } - - core::ptr::write(dst, b'"'); - dst = dst.add(1); - - dst as usize - odst as usize -} diff --git a/vendor/orjson/src/str/avx512.rs b/vendor/orjson/src/str/avx512.rs deleted file mode 100644 index 230a2de1dc..0000000000 --- a/vendor/orjson/src/str/avx512.rs +++ /dev/null @@ -1,102 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::str::pyunicode_new::*; - -use core::arch::x86_64::{ - __m256i, _mm256_and_si256, _mm256_cmpgt_epu8_mask, _mm256_cmpneq_epi8_mask, _mm256_loadu_si256, - _mm256_mask_cmpneq_epi8_mask, _mm256_maskz_loadu_epi8, _mm256_max_epu8, _mm256_set1_epi8, -}; - -macro_rules! u8_as_i8 { - ($val:expr) => { - core::mem::transmute::($val) - }; -} - -macro_rules! impl_kind_simd_avx512vl { - ($buf:expr) => { - unsafe { - const STRIDE: usize = 32; - - assume!($buf.len() > 0); - - let num_loops = $buf.len() / STRIDE; - let remainder = $buf.len() % STRIDE; - - let remainder_mask: u32 = !(u32::MAX << remainder); - let mut str_vec = - _mm256_maskz_loadu_epi8(remainder_mask, $buf.as_bytes().as_ptr() as *const i8); - let sptr = $buf.as_bytes().as_ptr().add(remainder); - - for i in 0..num_loops { - str_vec = _mm256_max_epu8( - str_vec, - _mm256_loadu_si256(sptr.add(STRIDE * i) as *const __m256i), - ); - } - - let vec_128 = _mm256_set1_epi8(u8_as_i8!(0b10000000)); - if _mm256_cmpgt_epu8_mask(str_vec, vec_128) == 0 { - pyunicode_ascii($buf.as_bytes().as_ptr(), $buf.len()) - } else { - let is_four = _mm256_cmpgt_epu8_mask(str_vec, _mm256_set1_epi8(u8_as_i8!(239))) > 0; - let is_not_latin = - _mm256_cmpgt_epu8_mask(str_vec, _mm256_set1_epi8(u8_as_i8!(195))) > 0; - let multibyte = _mm256_set1_epi8(u8_as_i8!(0b11000000)); - - let mut num_chars = popcnt!(_mm256_mask_cmpneq_epi8_mask( - remainder_mask, - _mm256_and_si256( - _mm256_maskz_loadu_epi8( - remainder_mask, - $buf.as_bytes().as_ptr() as *const i8 - ), - multibyte - ), - vec_128 - )); - - for i in 0..num_loops { - num_chars += popcnt!(_mm256_cmpneq_epi8_mask( - _mm256_and_si256( - _mm256_loadu_si256(sptr.add(STRIDE * i) as *const __m256i), - multibyte - ), - vec_128, - )) as usize; - } - - if is_four { - pyunicode_fourbyte($buf, num_chars) - } else if is_not_latin { - pyunicode_twobyte($buf, num_chars) - } else { - pyunicode_onebyte($buf, num_chars) - } - } - } - }; -} - -#[inline(never)] -#[cfg_attr( - feature = "avx512", - target_feature(enable = "avx512f,avx512bw,avx512vl,bmi2") -)] -pub unsafe fn create_str_impl_avx512vl(buf: &str) -> *mut pyo3_ffi::PyObject { - impl_kind_simd_avx512vl!(buf) -} - -#[inline(always)] -pub fn unicode_from_str(buf: &str) -> *mut pyo3_ffi::PyObject { - unsafe { - if unlikely!(buf.is_empty()) { - return use_immortal!(crate::typeref::EMPTY_UNICODE); - } - if std::is_x86_feature_detected!("avx512vl") { - create_str_impl_avx512vl(buf) - } else { - super::scalar::unicode_from_str(buf) - } - } -} diff --git a/vendor/orjson/src/str/ffi.rs b/vendor/orjson/src/str/ffi.rs deleted file mode 100644 index 21841a8800..0000000000 --- a/vendor/orjson/src/str/ffi.rs +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use core::ffi::c_void; -use pyo3_ffi::*; - -// see unicodeobject.h for documentation - -#[inline] -pub fn hash_str(op: *mut PyObject) -> Py_hash_t { - unsafe { - let data_ptr: *mut c_void = if (*op.cast::()).compact() == 1 - && (*op.cast::()).ascii() == 1 - { - (op as *mut PyASCIIObject).offset(1) as *mut c_void - } else { - (op as *mut PyCompactUnicodeObject).offset(1) as *mut c_void - }; - let num_bytes = - (*(op as *mut PyASCIIObject)).length * ((*(op as *mut PyASCIIObject)).kind()) as isize; - #[cfg(Py_3_14)] - let hash = pyo3_ffi::Py_HashBuffer(data_ptr, num_bytes); - #[cfg(not(Py_3_14))] - let hash = pyo3_ffi::_Py_HashBytes(data_ptr, num_bytes); - (*op.cast::()).hash = hash; - hash - } -} - -#[inline(never)] -pub fn unicode_to_str_via_ffi(op: *mut PyObject) -> Option<&'static str> { - let mut str_size: pyo3_ffi::Py_ssize_t = 0; - let ptr = ffi!(PyUnicode_AsUTF8AndSize(op, &mut str_size)) as *const u8; - if unlikely!(ptr.is_null()) { - None - } else { - Some(str_from_slice!(ptr, str_size as usize)) - } -} - -#[inline] -pub fn unicode_to_str(op: *mut PyObject) -> Option<&'static str> { - unsafe { - if unlikely!((*op.cast::()).compact() == 0) { - unicode_to_str_via_ffi(op) - } else if (*op.cast::()).ascii() == 1 { - let ptr = op.cast::().offset(1) as *const u8; - let len = (*op.cast::()).length as usize; - Some(str_from_slice!(ptr, len)) - } else if (*op.cast::()).utf8_length != 0 { - let ptr = (*op.cast::()).utf8 as *const u8; - let len = (*op.cast::()).utf8_length as usize; - Some(str_from_slice!(ptr, len)) - } else { - unicode_to_str_via_ffi(op) - } - } -} diff --git a/vendor/orjson/src/str/mod.rs b/vendor/orjson/src/str/mod.rs deleted file mode 100644 index 75149e9b09..0000000000 --- a/vendor/orjson/src/str/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -#[cfg(feature = "avx512")] -mod avx512; -mod ffi; -mod pyunicode_new; -mod scalar; - -#[cfg(not(feature = "avx512"))] -pub use scalar::unicode_from_str; - -#[cfg(feature = "avx512")] -pub use avx512::unicode_from_str; - -pub use ffi::*; diff --git a/vendor/orjson/src/str/pyunicode_new.rs b/vendor/orjson/src/str/pyunicode_new.rs deleted file mode 100644 index b549689138..0000000000 --- a/vendor/orjson/src/str/pyunicode_new.rs +++ /dev/null @@ -1,57 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use pyo3_ffi::{PyASCIIObject, PyCompactUnicodeObject}; - -#[inline(never)] -pub fn pyunicode_ascii(buf: *const u8, num_chars: usize) -> *mut pyo3_ffi::PyObject { - unsafe { - let ptr = ffi!(PyUnicode_New(num_chars as isize, 127)); - let data_ptr = ptr.cast::().offset(1) as *mut u8; - core::ptr::copy_nonoverlapping(buf, data_ptr, num_chars); - core::ptr::write(data_ptr.add(num_chars), 0); - ptr - } -} - -#[cold] -#[inline(never)] -pub fn pyunicode_onebyte(buf: &str, num_chars: usize) -> *mut pyo3_ffi::PyObject { - unsafe { - let ptr = ffi!(PyUnicode_New(num_chars as isize, 255)); - let mut data_ptr = ptr.cast::().offset(1) as *mut u8; - for each in buf.chars().fuse() { - core::ptr::write(data_ptr, each as u8); - data_ptr = data_ptr.offset(1); - } - core::ptr::write(data_ptr, 0); - ptr - } -} - -#[inline(never)] -pub fn pyunicode_twobyte(buf: &str, num_chars: usize) -> *mut pyo3_ffi::PyObject { - unsafe { - let ptr = ffi!(PyUnicode_New(num_chars as isize, 65535)); - let mut data_ptr = ptr.cast::().offset(1) as *mut u16; - for each in buf.chars().fuse() { - core::ptr::write(data_ptr, each as u16); - data_ptr = data_ptr.offset(1); - } - core::ptr::write(data_ptr, 0); - ptr - } -} - -#[inline(never)] -pub fn pyunicode_fourbyte(buf: &str, num_chars: usize) -> *mut pyo3_ffi::PyObject { - unsafe { - let ptr = ffi!(PyUnicode_New(num_chars as isize, 1114111)); - let mut data_ptr = ptr.cast::().offset(1) as *mut u32; - for each in buf.chars().fuse() { - core::ptr::write(data_ptr, each as u32); - data_ptr = data_ptr.offset(1); - } - core::ptr::write(data_ptr, 0); - ptr - } -} diff --git a/vendor/orjson/src/str/scalar.rs b/vendor/orjson/src/str/scalar.rs deleted file mode 100644 index 0aa84c7d8d..0000000000 --- a/vendor/orjson/src/str/scalar.rs +++ /dev/null @@ -1,45 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::str::pyunicode_new::*; -use crate::typeref::EMPTY_UNICODE; - -#[inline(always)] -pub fn str_impl_kind_scalar(buf: &str, num_chars: usize) -> *mut pyo3_ffi::PyObject { - unsafe { - let len = buf.len(); - assume!(len > 0); - - if unlikely!(*(buf.as_bytes().as_ptr()) > 239) { - return pyunicode_fourbyte(buf, num_chars); - } - - let sptr = buf.as_bytes().as_ptr(); - - let mut is_four = false; - let mut not_latin = false; - for i in 0..len { - is_four |= *sptr.add(i) > 239; - not_latin |= *sptr.add(i) > 195; - } - if is_four { - pyunicode_fourbyte(buf, num_chars) - } else if not_latin { - pyunicode_twobyte(buf, num_chars) - } else { - pyunicode_onebyte(buf, num_chars) - } - } -} - -#[inline(never)] -pub fn unicode_from_str(buf: &str) -> *mut pyo3_ffi::PyObject { - if unlikely!(buf.is_empty()) { - return use_immortal!(EMPTY_UNICODE); - } - let num_chars = bytecount::num_chars(buf.as_bytes()); - if buf.len() == num_chars { - pyunicode_ascii(buf.as_ptr(), num_chars) - } else { - str_impl_kind_scalar(buf, num_chars) - } -} diff --git a/vendor/orjson/src/typeref.rs b/vendor/orjson/src/typeref.rs deleted file mode 100644 index 0fe13f9f6b..0000000000 --- a/vendor/orjson/src/typeref.rs +++ /dev/null @@ -1,348 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -use crate::ffi::orjson_fragmenttype_new; -use core::ffi::c_char; -#[cfg(feature = "yyjson")] -use core::ffi::c_void; -#[cfg(feature = "yyjson")] -use core::mem::MaybeUninit; -use core::ptr::{null_mut, NonNull}; -use once_cell::race::{OnceBool, OnceBox}; -use pyo3_ffi::*; -#[cfg(feature = "yyjson")] -use std::cell::UnsafeCell; - -pub struct NumpyTypes { - pub array: *mut PyTypeObject, - pub float64: *mut PyTypeObject, - pub float32: *mut PyTypeObject, - pub float16: *mut PyTypeObject, - pub int64: *mut PyTypeObject, - pub int32: *mut PyTypeObject, - pub int16: *mut PyTypeObject, - pub int8: *mut PyTypeObject, - pub uint64: *mut PyTypeObject, - pub uint32: *mut PyTypeObject, - pub uint16: *mut PyTypeObject, - pub uint8: *mut PyTypeObject, - pub bool_: *mut PyTypeObject, - pub datetime64: *mut PyTypeObject, -} - -pub static mut DEFAULT: *mut PyObject = null_mut(); -pub static mut OPTION: *mut PyObject = null_mut(); - -pub static mut NONE: *mut PyObject = null_mut(); -pub static mut TRUE: *mut PyObject = null_mut(); -pub static mut FALSE: *mut PyObject = null_mut(); -pub static mut EMPTY_UNICODE: *mut PyObject = null_mut(); - -pub static mut BYTES_TYPE: *mut PyTypeObject = null_mut(); -pub static mut BYTEARRAY_TYPE: *mut PyTypeObject = null_mut(); -pub static mut MEMORYVIEW_TYPE: *mut PyTypeObject = null_mut(); -pub static mut STR_TYPE: *mut PyTypeObject = null_mut(); -pub static mut INT_TYPE: *mut PyTypeObject = null_mut(); -pub static mut BOOL_TYPE: *mut PyTypeObject = null_mut(); -pub static mut NONE_TYPE: *mut PyTypeObject = null_mut(); -pub static mut FLOAT_TYPE: *mut PyTypeObject = null_mut(); -pub static mut LIST_TYPE: *mut PyTypeObject = null_mut(); -pub static mut DICT_TYPE: *mut PyTypeObject = null_mut(); -pub static mut DATETIME_TYPE: *mut PyTypeObject = null_mut(); -pub static mut DATE_TYPE: *mut PyTypeObject = null_mut(); -pub static mut TIME_TYPE: *mut PyTypeObject = null_mut(); -pub static mut TUPLE_TYPE: *mut PyTypeObject = null_mut(); -pub static mut UUID_TYPE: *mut PyTypeObject = null_mut(); -pub static mut ENUM_TYPE: *mut PyTypeObject = null_mut(); -pub static mut FIELD_TYPE: *mut PyTypeObject = null_mut(); -pub static mut FRAGMENT_TYPE: *mut PyTypeObject = null_mut(); - -pub static mut NUMPY_TYPES: OnceBox>> = OnceBox::new(); - -#[cfg(Py_3_9)] -pub static mut ZONEINFO_TYPE: *mut PyTypeObject = null_mut(); - -pub static mut UTCOFFSET_METHOD_STR: *mut PyObject = null_mut(); -pub static mut NORMALIZE_METHOD_STR: *mut PyObject = null_mut(); -pub static mut CONVERT_METHOD_STR: *mut PyObject = null_mut(); -pub static mut DST_STR: *mut PyObject = null_mut(); - -pub static mut DICT_STR: *mut PyObject = null_mut(); -pub static mut DATACLASS_FIELDS_STR: *mut PyObject = null_mut(); -pub static mut SLOTS_STR: *mut PyObject = null_mut(); -pub static mut FIELD_TYPE_STR: *mut PyObject = null_mut(); -pub static mut ARRAY_STRUCT_STR: *mut PyObject = null_mut(); -pub static mut DTYPE_STR: *mut PyObject = null_mut(); -pub static mut DESCR_STR: *mut PyObject = null_mut(); -pub static mut VALUE_STR: *mut PyObject = null_mut(); -pub static mut INT_ATTR_STR: *mut PyObject = null_mut(); - -#[cfg(feature = "yyjson")] -pub const YYJSON_BUFFER_SIZE: usize = 1024 * 1024 * 8; - -#[cfg(feature = "yyjson")] -#[repr(align(64))] -struct YYJSONBuffer(UnsafeCell>); - -#[cfg(feature = "yyjson")] -pub struct YYJSONAlloc { - pub alloc: crate::ffi::yyjson::yyjson_alc, - _buffer: Box, -} - -#[cfg(feature = "yyjson")] -pub static mut YYJSON_ALLOC: OnceBox = OnceBox::new(); - -#[cfg(feature = "yyjson")] -pub fn yyjson_init() -> Box { - // Using unsafe to ensure allocation happens on the heap without going through the stack - // so we don't stack overflow in debug mode. Once rust-lang/rust#63291 is stable (Box::new_uninit) - // we can use that instead. - let layout = std::alloc::Layout::new::(); - let buffer = unsafe { Box::from_raw(std::alloc::alloc(layout).cast::()) }; - let mut alloc = crate::ffi::yyjson::yyjson_alc { - malloc: None, - realloc: None, - free: None, - ctx: null_mut(), - }; - unsafe { - crate::ffi::yyjson::yyjson_alc_pool_init( - &mut alloc, - buffer.0.get().cast::(), - YYJSON_BUFFER_SIZE, - ); - } - Box::new(YYJSONAlloc { - alloc, - _buffer: buffer, - }) -} - -#[allow(non_upper_case_globals)] -pub static mut JsonEncodeError: *mut PyObject = null_mut(); -#[allow(non_upper_case_globals)] -pub static mut JsonDecodeError: *mut PyObject = null_mut(); - -static INIT: OnceBool = OnceBool::new(); - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub fn init_typerefs() { - INIT.get_or_init(_init_typerefs_impl); -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -fn _init_typerefs_impl() -> bool { - unsafe { - debug_assert!(crate::opt::MAX_OPT < u16::MAX as i32); - - assert!(crate::deserialize::KEY_MAP - .set(crate::deserialize::KeyMap::default()) - .is_ok()); - FRAGMENT_TYPE = orjson_fragmenttype_new(); - PyDateTime_IMPORT(); - NONE = Py_None(); - TRUE = Py_True(); - FALSE = Py_False(); - EMPTY_UNICODE = PyUnicode_New(0, 255); - STR_TYPE = (*EMPTY_UNICODE).ob_type; - BYTES_TYPE = (*PyBytes_FromStringAndSize("".as_ptr() as *const c_char, 0)).ob_type; - - { - let bytearray = PyByteArray_FromStringAndSize("".as_ptr() as *const c_char, 0); - BYTEARRAY_TYPE = (*bytearray).ob_type; - - let memoryview = PyMemoryView_FromObject(bytearray); - MEMORYVIEW_TYPE = (*memoryview).ob_type; - Py_DECREF(memoryview); - Py_DECREF(bytearray); - } - - DICT_TYPE = (*PyDict_New()).ob_type; - LIST_TYPE = (*PyList_New(0)).ob_type; - TUPLE_TYPE = (*PyTuple_New(0)).ob_type; - NONE_TYPE = (*NONE).ob_type; - BOOL_TYPE = (*TRUE).ob_type; - INT_TYPE = (*PyLong_FromLongLong(0)).ob_type; - FLOAT_TYPE = (*PyFloat_FromDouble(0.0)).ob_type; - DATETIME_TYPE = look_up_datetime_type(); - DATE_TYPE = look_up_date_type(); - TIME_TYPE = look_up_time_type(); - UUID_TYPE = look_up_uuid_type(); - ENUM_TYPE = look_up_enum_type(); - FIELD_TYPE = look_up_field_type(); - - #[cfg(Py_3_9)] - { - ZONEINFO_TYPE = look_up_zoneinfo_type(); - } - - INT_ATTR_STR = PyUnicode_InternFromString("int\0".as_ptr() as *const c_char); - UTCOFFSET_METHOD_STR = PyUnicode_InternFromString("utcoffset\0".as_ptr() as *const c_char); - NORMALIZE_METHOD_STR = PyUnicode_InternFromString("normalize\0".as_ptr() as *const c_char); - CONVERT_METHOD_STR = PyUnicode_InternFromString("convert\0".as_ptr() as *const c_char); - DST_STR = PyUnicode_InternFromString("dst\0".as_ptr() as *const c_char); - DICT_STR = PyUnicode_InternFromString("__dict__\0".as_ptr() as *const c_char); - DATACLASS_FIELDS_STR = - PyUnicode_InternFromString("__dataclass_fields__\0".as_ptr() as *const c_char); - SLOTS_STR = PyUnicode_InternFromString("__slots__\0".as_ptr() as *const c_char); - FIELD_TYPE_STR = PyUnicode_InternFromString("_field_type\0".as_ptr() as *const c_char); - ARRAY_STRUCT_STR = - PyUnicode_InternFromString("__array_struct__\0".as_ptr() as *const c_char); - DTYPE_STR = PyUnicode_InternFromString("dtype\0".as_ptr() as *const c_char); - DESCR_STR = PyUnicode_InternFromString("descr\0".as_ptr() as *const c_char); - VALUE_STR = PyUnicode_InternFromString("value\0".as_ptr() as *const c_char); - DEFAULT = PyUnicode_InternFromString("default\0".as_ptr() as *const c_char); - OPTION = PyUnicode_InternFromString("option\0".as_ptr() as *const c_char); - JsonEncodeError = pyo3_ffi::PyExc_TypeError; - Py_INCREF(JsonEncodeError); - JsonDecodeError = look_up_json_exc(); - }; - true -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_json_exc() -> *mut PyObject { - let module = PyImport_ImportModule("json\0".as_ptr() as *const c_char); - let module_dict = PyObject_GenericGetDict(module, null_mut()); - let ptr = PyMapping_GetItemString(module_dict, "JSONDecodeError\0".as_ptr() as *const c_char); - let res = pyo3_ffi::PyErr_NewException( - "orjson.JSONDecodeError\0".as_ptr() as *const c_char, - ptr, - null_mut(), - ); - Py_DECREF(ptr); - Py_DECREF(module_dict); - Py_DECREF(module); - Py_INCREF(res); - res -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_numpy_type(numpy_module_dict: *mut PyObject, np_type: &str) -> *mut PyTypeObject { - let ptr = PyMapping_GetItemString(numpy_module_dict, np_type.as_ptr() as *const c_char); - Py_XDECREF(ptr); - ptr as *mut PyTypeObject -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -pub fn load_numpy_types() -> Box>> { - unsafe { - let numpy = PyImport_ImportModule("numpy\0".as_ptr() as *const c_char); - if numpy.is_null() { - PyErr_Clear(); - return Box::new(None); - } - let numpy_module_dict = PyObject_GenericGetDict(numpy, null_mut()); - let types = Box::new(NumpyTypes { - array: look_up_numpy_type(numpy_module_dict, "ndarray\0"), - float16: look_up_numpy_type(numpy_module_dict, "half\0"), - float32: look_up_numpy_type(numpy_module_dict, "float32\0"), - float64: look_up_numpy_type(numpy_module_dict, "float64\0"), - int8: look_up_numpy_type(numpy_module_dict, "int8\0"), - int16: look_up_numpy_type(numpy_module_dict, "int16\0"), - int32: look_up_numpy_type(numpy_module_dict, "int32\0"), - int64: look_up_numpy_type(numpy_module_dict, "int64\0"), - uint16: look_up_numpy_type(numpy_module_dict, "uint16\0"), - uint32: look_up_numpy_type(numpy_module_dict, "uint32\0"), - uint64: look_up_numpy_type(numpy_module_dict, "uint64\0"), - uint8: look_up_numpy_type(numpy_module_dict, "uint8\0"), - bool_: look_up_numpy_type(numpy_module_dict, "bool_\0"), - datetime64: look_up_numpy_type(numpy_module_dict, "datetime64\0"), - }); - Py_XDECREF(numpy_module_dict); - Py_XDECREF(numpy); - Box::new(Some(nonnull!(Box::::into_raw(types)))) - } -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_field_type() -> *mut PyTypeObject { - let module = PyImport_ImportModule("dataclasses\0".as_ptr() as *const c_char); - let module_dict = PyObject_GenericGetDict(module, null_mut()); - let ptr = PyMapping_GetItemString(module_dict, "_FIELD\0".as_ptr() as *const c_char) - as *mut PyTypeObject; - Py_DECREF(module_dict); - Py_DECREF(module); - ptr -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_enum_type() -> *mut PyTypeObject { - let module = PyImport_ImportModule("enum\0".as_ptr() as *const c_char); - let module_dict = PyObject_GenericGetDict(module, null_mut()); - let ptr = PyMapping_GetItemString(module_dict, "EnumMeta\0".as_ptr() as *const c_char) - as *mut PyTypeObject; - Py_DECREF(module_dict); - Py_DECREF(module); - ptr -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_uuid_type() -> *mut PyTypeObject { - let uuid_mod = PyImport_ImportModule("uuid\0".as_ptr() as *const c_char); - let uuid_mod_dict = PyObject_GenericGetDict(uuid_mod, null_mut()); - let uuid = PyMapping_GetItemString(uuid_mod_dict, "NAMESPACE_DNS\0".as_ptr() as *const c_char); - let ptr = (*uuid).ob_type; - Py_DECREF(uuid); - Py_DECREF(uuid_mod_dict); - Py_DECREF(uuid_mod); - ptr -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_datetime_type() -> *mut PyTypeObject { - let datetime = ((*PyDateTimeAPI()).DateTime_FromDateAndTime)( - 1970, - 1, - 1, - 0, - 0, - 0, - 0, - NONE, - (*(PyDateTimeAPI())).DateTimeType, - ); - let ptr = (*datetime).ob_type; - Py_DECREF(datetime); - ptr -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_date_type() -> *mut PyTypeObject { - let date = ((*PyDateTimeAPI()).Date_FromDate)(1, 1, 1, (*(PyDateTimeAPI())).DateType); - let ptr = (*date).ob_type; - Py_DECREF(date); - ptr -} - -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_time_type() -> *mut PyTypeObject { - let time = ((*PyDateTimeAPI()).Time_FromTime)(0, 0, 0, 0, NONE, (*(PyDateTimeAPI())).TimeType); - let ptr = (*time).ob_type; - Py_DECREF(time); - ptr -} - -#[cfg(Py_3_9)] -#[cold] -#[cfg_attr(feature = "optimize", optimize(size))] -unsafe fn look_up_zoneinfo_type() -> *mut PyTypeObject { - let module = PyImport_ImportModule("zoneinfo\0".as_ptr() as *const c_char); - let module_dict = PyObject_GenericGetDict(module, null_mut()); - let ptr = PyMapping_GetItemString(module_dict, "ZoneInfo\0".as_ptr() as *const c_char) - as *mut PyTypeObject; - Py_DECREF(module_dict); - Py_DECREF(module); - ptr -} diff --git a/vendor/orjson/src/util.rs b/vendor/orjson/src/util.rs deleted file mode 100644 index 8add0f750f..0000000000 --- a/vendor/orjson/src/util.rs +++ /dev/null @@ -1,307 +0,0 @@ -// SPDX-License-Identifier: (Apache-2.0 OR MIT) - -pub const INVALID_STR: &str = "str is not valid UTF-8: surrogates not allowed"; - -macro_rules! is_type { - ($obj_ptr:expr, $type_ptr:expr) => { - unsafe { $obj_ptr == $type_ptr } - }; -} - -macro_rules! ob_type { - ($obj:expr) => { - unsafe { (*$obj).ob_type } - }; -} - -macro_rules! is_class_by_type { - ($ob_type:expr, $type_ptr:ident) => { - unsafe { $ob_type == $type_ptr } - }; -} - -macro_rules! is_subclass_by_flag { - ($ob_type:expr, $flag:ident) => { - unsafe { (((*$ob_type).tp_flags & pyo3_ffi::$flag) != 0) } - }; -} - -macro_rules! is_subclass_by_type { - ($ob_type:expr, $type:ident) => { - unsafe { - (*($ob_type as *mut pyo3_ffi::PyTypeObject)) - .ob_base - .ob_base - .ob_type - == $type - } - }; -} - -macro_rules! err { - ($msg:expr) => { - return Err(serde::ser::Error::custom($msg)) - }; -} - -macro_rules! opt_enabled { - ($var:expr, $flag:expr) => { - $var & $flag != 0 - }; -} - -macro_rules! opt_disabled { - ($var:expr, $flag:expr) => { - $var & $flag == 0 - }; -} - -#[cfg(feature = "intrinsics")] -macro_rules! unlikely { - ($exp:expr) => { - core::intrinsics::unlikely($exp) - }; -} - -#[cfg(not(feature = "intrinsics"))] -macro_rules! unlikely { - ($exp:expr) => { - $exp - }; -} - -#[allow(unused_macros)] -#[cfg(feature = "intrinsics")] -macro_rules! likely { - ($exp:expr) => { - core::intrinsics::likely($exp) - }; -} - -#[allow(unused_macros)] -#[cfg(not(feature = "intrinsics"))] -macro_rules! likely { - ($exp:expr) => { - $exp - }; -} - -macro_rules! nonnull { - ($exp:expr) => { - unsafe { core::ptr::NonNull::new_unchecked($exp) } - }; -} - -macro_rules! str_from_slice { - ($ptr:expr, $size:expr) => { - unsafe { std::str::from_utf8_unchecked(core::slice::from_raw_parts($ptr, $size as usize)) } - }; -} - -#[cfg(Py_3_12)] -macro_rules! reverse_pydict_incref { - ($op:expr) => { - unsafe { - if pyo3_ffi::_Py_IsImmortal($op) == 0 { - debug_assert!(ffi!(Py_REFCNT($op)) >= 2); - (*$op).ob_refcnt.ob_refcnt -= 1; - } - } - }; -} - -#[cfg(not(Py_3_12))] -macro_rules! reverse_pydict_incref { - ($op:expr) => { - unsafe { - debug_assert!(ffi!(Py_REFCNT($op)) >= 2); - (*$op).ob_refcnt -= 1; - } - }; -} - -macro_rules! ffi { - ($fn:ident()) => { - unsafe { pyo3_ffi::$fn() } - }; - - ($fn:ident($obj1:expr)) => { - unsafe { pyo3_ffi::$fn($obj1) } - }; - - ($fn:ident($obj1:expr, $obj2:expr)) => { - unsafe { pyo3_ffi::$fn($obj1, $obj2) } - }; - - ($fn:ident($obj1:expr, $obj2:expr, $obj3:expr)) => { - unsafe { pyo3_ffi::$fn($obj1, $obj2, $obj3) } - }; - - ($fn:ident($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr)) => { - unsafe { pyo3_ffi::$fn($obj1, $obj2, $obj3, $obj4) } - }; -} - -#[cfg(Py_3_9)] -macro_rules! call_method { - ($obj1:expr, $obj2:expr) => { - unsafe { pyo3_ffi::PyObject_CallMethodNoArgs($obj1, $obj2) } - }; - ($obj1:expr, $obj2:expr, $obj3:expr) => { - unsafe { pyo3_ffi::PyObject_CallMethodOneArg($obj1, $obj2, $obj3) } - }; -} - -#[cfg(not(Py_3_9))] -macro_rules! call_method { - ($obj1:expr, $obj2:expr) => { - unsafe { - pyo3_ffi::PyObject_CallMethodObjArgs( - $obj1, - $obj2, - core::ptr::null_mut() as *mut pyo3_ffi::PyObject, - ) - } - }; - ($obj1:expr, $obj2:expr, $obj3:expr) => { - unsafe { - pyo3_ffi::PyObject_CallMethodObjArgs( - $obj1, - $obj2, - $obj3, - core::ptr::null_mut() as *mut pyo3_ffi::PyObject, - ) - } - }; -} - -macro_rules! str_hash { - ($op:expr) => { - unsafe { (*$op.cast::()).hash } - }; -} - -#[cfg(Py_3_13)] -macro_rules! pydict_contains { - ($obj1:expr, $obj2:expr) => { - unsafe { pyo3_ffi::PyDict_Contains(pyo3_ffi::PyType_GetDict($obj1), $obj2) == 1 } - }; -} - -#[cfg(all(Py_3_12, not(Py_3_13)))] -macro_rules! pydict_contains { - ($obj1:expr, $obj2:expr) => { - unsafe { - pyo3_ffi::_PyDict_Contains_KnownHash( - pyo3_ffi::PyType_GetDict($obj1), - $obj2, - (*$obj2.cast::()).hash, - ) == 1 - } - }; -} - -#[cfg(all(Py_3_10, not(Py_3_12)))] -macro_rules! pydict_contains { - ($obj1:expr, $obj2:expr) => { - unsafe { - pyo3_ffi::_PyDict_Contains_KnownHash( - (*$obj1).tp_dict, - $obj2, - (*$obj2.cast::()).hash, - ) == 1 - } - }; -} - -#[cfg(not(Py_3_10))] -macro_rules! pydict_contains { - ($obj1:expr, $obj2:expr) => { - unsafe { pyo3_ffi::PyDict_Contains((*$obj1).tp_dict, $obj2) == 1 } - }; -} - -#[cfg(Py_3_12)] -macro_rules! use_immortal { - ($op:expr) => { - unsafe { $op } - }; -} - -#[cfg(not(Py_3_12))] -macro_rules! use_immortal { - ($op:expr) => { - unsafe { - ffi!(Py_INCREF($op)); - $op - } - }; -} - -#[cfg(not(Py_3_13))] -macro_rules! pydict_next { - ($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr) => { - unsafe { pyo3_ffi::_PyDict_Next($obj1, $obj2, $obj3, $obj4, core::ptr::null_mut()) } - }; -} - -#[cfg(Py_3_13)] -macro_rules! pydict_next { - ($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr) => { - unsafe { pyo3_ffi::PyDict_Next($obj1, $obj2, $obj3, $obj4) } - }; -} - -macro_rules! reserve_minimum { - ($writer:expr) => { - $writer.reserve(64); - }; -} - -macro_rules! reserve_pretty { - ($writer:expr, $val:expr) => { - $writer.reserve($val + 16); - }; -} - -macro_rules! assume { - ($expr:expr) => { - debug_assert!($expr); - #[cfg(feature = "intrinsics")] - unsafe { - core::intrinsics::assume($expr); - }; - }; -} - -#[allow(unused_macros)] -#[cfg(feature = "intrinsics")] -macro_rules! trailing_zeros { - ($val:expr) => { - core::intrinsics::cttz_nonzero($val) as usize - }; -} - -#[allow(unused_macros)] -#[cfg(not(feature = "intrinsics"))] -macro_rules! trailing_zeros { - ($val:expr) => { - $val.trailing_zeros() as usize - }; -} - -#[allow(unused_macros)] -#[cfg(feature = "intrinsics")] -macro_rules! popcnt { - ($val:expr) => { - core::intrinsics::ctpop(core::mem::transmute::($val)) as usize - }; -} - -#[allow(unused_macros)] -#[cfg(not(feature = "intrinsics"))] -macro_rules! popcnt { - ($val:expr) => { - core::mem::transmute::($val).count_ones() as usize - }; -} diff --git a/vendor/orjson/test/__init__.py b/vendor/orjson/test/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/orjson/test/requirements.txt b/vendor/orjson/test/requirements.txt deleted file mode 100644 index 2c04102f75..0000000000 --- a/vendor/orjson/test/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -arrow -faker -numpy;(platform_machine=="x86_64" or (platform_machine=="aarch64" and sys_platform == "linux")) and python_version<"3.13" -pendulum;sys_platform=="linux" and platform_machine=="x86_64" and python_version<"3.12" -time-machine < 2.15;sys_platform=="linux" and platform_machine=="x86_64" and python_version<"3.12" -psutil;(sys_platform=="linux" or sys_platform == "macos") and platform_machine=="x86_64" and python_version<"3.13" -pytest -pytz -typing_extensions;python_version<"3.8" -xxhash==1.4.3;sys_platform=="linux" and platform_machine=="x86_64" and python_version<"3.9" # creates non-compact ASCII for test_str_ascii diff --git a/vendor/orjson/test/test_api.py b/vendor/orjson/test/test_api.py deleted file mode 100644 index 4be9f52cb1..0000000000 --- a/vendor/orjson/test/test_api.py +++ /dev/null @@ -1,286 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import datetime -import inspect -import json -import re - -import pytest - -import orjson - -SIMPLE_TYPES = (1, 1.0, -1, None, "str", True, False) - -LOADS_RECURSION_LIMIT = 1024 - - -def default(obj): - return str(obj) - - -class TestApi: - def test_loads_trailing(self): - """ - loads() handles trailing whitespace - """ - assert orjson.loads("{}\n\t ") == {} - - def test_loads_trailing_invalid(self): - """ - loads() handles trailing invalid - """ - pytest.raises(orjson.JSONDecodeError, orjson.loads, "{}\n\t a") - - def test_simple_json(self): - """ - dumps() equivalent to json on simple types - """ - for obj in SIMPLE_TYPES: - assert orjson.dumps(obj) == json.dumps(obj).encode("utf-8") - - def test_simple_round_trip(self): - """ - dumps(), loads() round trip on simple types - """ - for obj in SIMPLE_TYPES: - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_loads_type(self): - """ - loads() invalid type - """ - for val in (1, 3.14, [], {}, None): # type: ignore - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_loads_recursion_partial(self): - """ - loads() recursion limit partial - """ - pytest.raises(orjson.JSONDecodeError, orjson.loads, "[" * (1024 * 1024)) - - def test_loads_recursion_valid_limit_array(self): - """ - loads() recursion limit at limit array - """ - n = LOADS_RECURSION_LIMIT + 1 - value = b"[" * n + b"]" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_limit_object(self): - """ - loads() recursion limit at limit object - """ - n = LOADS_RECURSION_LIMIT - value = b'{"key":' * n + b'{"key":true}' + b"}" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_limit_mixed(self): - """ - loads() recursion limit at limit mixed - """ - n = LOADS_RECURSION_LIMIT - value = b'[{"key":' * n + b'{"key":true}' + b"}" * n + b"]" - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_excessive_array(self): - """ - loads() recursion limit excessively high value - """ - n = 10000000 - value = b"[" * n + b"]" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_limit_array_pretty(self): - """ - loads() recursion limit at limit array pretty - """ - n = LOADS_RECURSION_LIMIT + 1 - value = b"[\n " * n + b"]" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_limit_object_pretty(self): - """ - loads() recursion limit at limit object pretty - """ - n = LOADS_RECURSION_LIMIT - value = b'{\n "key":' * n + b'{"key":true}' + b"}" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_limit_mixed_pretty(self): - """ - loads() recursion limit at limit mixed pretty - """ - n = LOADS_RECURSION_LIMIT - value = b'[\n {"key":' * n + b'{"key":true}' + b"}" * n + b"]" - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_loads_recursion_valid_excessive_array_pretty(self): - """ - loads() recursion limit excessively high value pretty - """ - n = 10000000 - value = b"[\n " * n + b"]" * n - pytest.raises(orjson.JSONDecodeError, orjson.loads, value) - - def test_version(self): - """ - __version__ - """ - assert re.match(r"^\d+\.\d+(\.\d+)?$", orjson.__version__) - - def test_valueerror(self): - """ - orjson.JSONDecodeError is a subclass of ValueError - """ - pytest.raises(orjson.JSONDecodeError, orjson.loads, "{") - pytest.raises(ValueError, orjson.loads, "{") - - def test_optional_none(self): - """ - dumps() option, default None - """ - assert orjson.dumps([], option=None) == b"[]" - assert orjson.dumps([], default=None) == b"[]" - assert orjson.dumps([], option=None, default=None) == b"[]" - assert orjson.dumps([], None, None) == b"[]" - - def test_option_not_int(self): - """ - dumps() option not int or None - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(True, option=True) - - def test_option_invalid_int(self): - """ - dumps() option invalid 64-bit number - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(True, option=9223372036854775809) - - def test_option_range_low(self): - """ - dumps() option out of range low - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(True, option=-1) - - def test_option_range_high(self): - """ - dumps() option out of range high - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(True, option=1 << 12) - - def test_opts_multiple(self): - """ - dumps() multiple option - """ - assert ( - orjson.dumps( - [1, datetime.datetime(2000, 1, 1, 2, 3, 4)], - option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC, - ) - == b'[1,"2000-01-01T02:03:04+00:00"]' - ) - - def test_default_positional(self): - """ - dumps() positional arg - """ - with pytest.raises(TypeError): - orjson.dumps(__obj={}) # type: ignore - with pytest.raises(TypeError): - orjson.dumps(zxc={}) # type: ignore - - def test_default_unknown_kwarg(self): - """ - dumps() unknown kwarg - """ - with pytest.raises(TypeError): - orjson.dumps({}, zxc=default) # type: ignore - - def test_default_empty_kwarg(self): - """ - dumps() empty kwarg - """ - assert orjson.dumps(None, **{}) == b"null" - - def test_default_twice(self): - """ - dumps() default twice - """ - with pytest.raises(TypeError): - orjson.dumps({}, default, default=default) # type: ignore - - def test_option_twice(self): - """ - dumps() option twice - """ - with pytest.raises(TypeError): - orjson.dumps({}, None, orjson.OPT_NAIVE_UTC, option=orjson.OPT_NAIVE_UTC) # type: ignore - - def test_option_mixed(self): - """ - dumps() option one arg, one kwarg - """ - - class Custom: - def __str__(self): - return "zxc" - - assert ( - orjson.dumps( - [Custom(), datetime.datetime(2000, 1, 1, 2, 3, 4)], - default, - option=orjson.OPT_NAIVE_UTC, - ) - == b'["zxc","2000-01-01T02:03:04+00:00"]' - ) - - def test_dumps_signature(self): - """ - dumps() valid __text_signature__ - """ - assert ( - str(inspect.signature(orjson.dumps)) - == "(obj, /, default=None, option=None)" - ) - inspect.signature(orjson.dumps).bind("str") - inspect.signature(orjson.dumps).bind("str", default=default, option=1) - inspect.signature(orjson.dumps).bind("str", default=None, option=None) - - def test_loads_signature(self): - """ - loads() valid __text_signature__ - """ - assert str(inspect.signature(orjson.loads)), "(obj == /)" - inspect.signature(orjson.loads).bind("[]") - - def test_dumps_module_str(self): - """ - orjson.dumps.__module__ is a str - """ - assert orjson.dumps.__module__ == "orjson" - - def test_loads_module_str(self): - """ - orjson.loads.__module__ is a str - """ - assert orjson.loads.__module__ == "orjson" - - def test_bytes_buffer(self): - """ - dumps() trigger buffer growing where length is greater than growth - """ - a = "a" * 900 - b = "b" * 4096 - c = "c" * 4096 * 4096 - assert orjson.dumps([a, b, c]) == f'["{a}","{b}","{c}"]'.encode("utf-8") - - def test_bytes_null_terminated(self): - """ - dumps() PyBytesObject buffer is null-terminated - """ - # would raise ValueError: invalid literal for int() with base 10: b'1596728892' - int(orjson.dumps(1596728892)) diff --git a/vendor/orjson/test/test_append_newline.py b/vendor/orjson/test/test_append_newline.py deleted file mode 100644 index e636902619..0000000000 --- a/vendor/orjson/test/test_append_newline.py +++ /dev/null @@ -1,41 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - -from .util import read_fixture_obj - - -class TestAppendNewline: - def test_dumps_newline(self): - """ - dumps() OPT_APPEND_NEWLINE - """ - assert orjson.dumps([], option=orjson.OPT_APPEND_NEWLINE) == b"[]\n" - - def test_twitter_newline(self): - """ - loads(),dumps() twitter.json OPT_APPEND_NEWLINE - """ - val = read_fixture_obj("twitter.json.xz") - assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val - - def test_canada(self): - """ - loads(), dumps() canada.json OPT_APPEND_NEWLINE - """ - val = read_fixture_obj("canada.json.xz") - assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val - - def test_citm_catalog_newline(self): - """ - loads(), dumps() citm_catalog.json OPT_APPEND_NEWLINE - """ - val = read_fixture_obj("citm_catalog.json.xz") - assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val - - def test_github_newline(self): - """ - loads(), dumps() github.json OPT_APPEND_NEWLINE - """ - val = read_fixture_obj("github.json.xz") - assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val diff --git a/vendor/orjson/test/test_canonical.py b/vendor/orjson/test/test_canonical.py deleted file mode 100644 index 2153f6ff25..0000000000 --- a/vendor/orjson/test/test_canonical.py +++ /dev/null @@ -1,26 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - - -class TestCanonicalTests: - def test_dumps_ctrl_escape(self): - """ - dumps() ctrl characters - """ - assert orjson.dumps("text\u0003\r\n") == b'"text\\u0003\\r\\n"' - - def test_dumps_escape_quote_backslash(self): - """ - dumps() quote, backslash escape - """ - assert orjson.dumps(r'"\ test') == b'"\\"\\\\ test"' - - def test_dumps_escape_line_separator(self): - """ - dumps() U+2028, U+2029 escape - """ - assert ( - orjson.dumps({"spaces": "\u2028 \u2029"}) - == b'{"spaces":"\xe2\x80\xa8 \xe2\x80\xa9"}' - ) diff --git a/vendor/orjson/test/test_circular.py b/vendor/orjson/test/test_circular.py deleted file mode 100644 index 89ec6ae161..0000000000 --- a/vendor/orjson/test/test_circular.py +++ /dev/null @@ -1,70 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - - -class TestCircular: - def test_circular_dict(self): - """ - dumps() circular reference dict - """ - obj = {} # type: ignore - obj["obj"] = obj - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - def test_circular_dict_sort_keys(self): - """ - dumps() circular reference dict OPT_SORT_KEYS - """ - obj = {} # type: ignore - obj["obj"] = obj - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - - def test_circular_dict_non_str_keys(self): - """ - dumps() circular reference dict OPT_NON_STR_KEYS - """ - obj = {} # type: ignore - obj["obj"] = obj - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) - - def test_circular_list(self): - """ - dumps() circular reference list - """ - obj = [] # type: ignore - obj.append(obj) # type: ignore - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - def test_circular_nested(self): - """ - dumps() circular reference nested dict, list - """ - obj = {} # type: ignore - obj["list"] = [{"obj": obj}] - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - def test_circular_nested_sort_keys(self): - """ - dumps() circular reference nested dict, list OPT_SORT_KEYS - """ - obj = {} # type: ignore - obj["list"] = [{"obj": obj}] - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - - def test_circular_nested_non_str_keys(self): - """ - dumps() circular reference nested dict, list OPT_NON_STR_KEYS - """ - obj = {} # type: ignore - obj["list"] = [{"obj": obj}] - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) diff --git a/vendor/orjson/test/test_dataclass.py b/vendor/orjson/test/test_dataclass.py deleted file mode 100644 index f3b60fac71..0000000000 --- a/vendor/orjson/test/test_dataclass.py +++ /dev/null @@ -1,299 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import abc -import uuid -from dataclasses import InitVar, asdict, dataclass, field -from enum import Enum -from typing import ClassVar, Dict, Optional - -import pytest - -import orjson - - -class AnEnum(Enum): - ONE = 1 - TWO = 2 - - -@dataclass -class EmptyDataclass: - pass - - -@dataclass -class EmptyDataclassSlots: - __slots__ = () - - -@dataclass -class Dataclass1: - name: str - number: int - sub: Optional["Dataclass1"] - - -@dataclass -class Dataclass2: - name: Optional[str] = field(default="?") - - -@dataclass -class Dataclass3: - a: str - b: int - c: dict - d: bool - e: float - f: list - g: tuple - - -@dataclass -class Dataclass4: - a: str = field() - b: int = field(metadata={"unrelated": False}) - c: float = 1.1 - - -@dataclass -class Datasubclass(Dataclass1): - additional: bool - - -@dataclass -class Slotsdataclass: - __slots__ = ("a", "b", "_c", "d") - a: str - b: int - _c: str - d: InitVar[str] - cls_var: ClassVar[str] = "cls" - - -@dataclass -class Defaultdataclass: - a: uuid.UUID - b: AnEnum - - -@dataclass -class UnsortedDataclass: - c: int - b: int - a: int - d: Optional[Dict] - - -@dataclass -class InitDataclass: - a: InitVar[str] - b: InitVar[str] - cls_var: ClassVar[str] = "cls" - ab: str = "" - - def __post_init__(self, a: str, b: str): - self._other = 1 - self.ab = f"{a} {b}" - - -class AbstractBase(abc.ABC): - @abc.abstractmethod - def key(self): - raise NotImplementedError - - -@dataclass(frozen=True) -class ConcreteAbc(AbstractBase): - __slots__ = ("attr",) - - attr: float - - def key(self): - return "dkjf" - - -class TestDataclass: - def test_dataclass(self): - """ - dumps() dataclass - """ - obj = Dataclass1("a", 1, None) - assert orjson.dumps(obj) == b'{"name":"a","number":1,"sub":null}' - - def test_dataclass_recursive(self): - """ - dumps() dataclass recursive - """ - obj = Dataclass1("a", 1, Dataclass1("b", 2, None)) - assert ( - orjson.dumps(obj) - == b'{"name":"a","number":1,"sub":{"name":"b","number":2,"sub":null}}' - ) - - def test_dataclass_circular(self): - """ - dumps() dataclass circular - """ - obj1 = Dataclass1("a", 1, None) - obj2 = Dataclass1("b", 2, obj1) - obj1.sub = obj2 - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj1) - - def test_dataclass_empty(self): - """ - dumps() no attributes - """ - assert orjson.dumps(EmptyDataclass()) == b"{}" - - def test_dataclass_empty_slots(self): - """ - dumps() no attributes slots - """ - assert orjson.dumps(EmptyDataclassSlots()) == b"{}" - - def test_dataclass_default_arg(self): - """ - dumps() dataclass default arg - """ - obj = Dataclass2() - assert orjson.dumps(obj) == b'{"name":"?"}' - - def test_dataclass_types(self): - """ - dumps() dataclass types - """ - obj = Dataclass3("a", 1, {"a": "b"}, True, 1.1, [1, 2], (3, 4)) - assert ( - orjson.dumps(obj) - == b'{"a":"a","b":1,"c":{"a":"b"},"d":true,"e":1.1,"f":[1,2],"g":[3,4]}' - ) - - def test_dataclass_metadata(self): - """ - dumps() dataclass metadata - """ - obj = Dataclass4("a", 1, 2.1) - assert orjson.dumps(obj) == b'{"a":"a","b":1,"c":2.1}' - - def test_dataclass_classvar(self): - """ - dumps() dataclass class variable - """ - obj = Dataclass4("a", 1) - assert orjson.dumps(obj) == b'{"a":"a","b":1,"c":1.1}' - - def test_dataclass_subclass(self): - """ - dumps() dataclass subclass - """ - obj = Datasubclass("a", 1, None, False) - assert ( - orjson.dumps(obj) - == b'{"name":"a","number":1,"sub":null,"additional":false}' - ) - - def test_dataclass_slots(self): - """ - dumps() dataclass with __slots__ does not include under attributes, InitVar, or ClassVar - """ - obj = Slotsdataclass("a", 1, "c", "d") - assert "__dict__" not in dir(obj) - assert orjson.dumps(obj) == b'{"a":"a","b":1}' - - def test_dataclass_default(self): - """ - dumps() dataclass with default - """ - - def default(__obj): - if isinstance(__obj, uuid.UUID): - return str(__obj) - elif isinstance(__obj, Enum): - return __obj.value - - obj = Defaultdataclass( - uuid.UUID("808989c0-00d5-48a8-b5c4-c804bf9032f2"), AnEnum.ONE - ) - assert ( - orjson.dumps(obj, default=default) - == b'{"a":"808989c0-00d5-48a8-b5c4-c804bf9032f2","b":1}' - ) - - def test_dataclass_sort(self): - """ - OPT_SORT_KEYS has no effect on dataclasses - """ - obj = UnsortedDataclass(1, 2, 3, None) - assert ( - orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - == b'{"c":1,"b":2,"a":3,"d":null}' - ) - - def test_dataclass_sort_sub(self): - """ - dataclass fast path does not prevent OPT_SORT_KEYS from cascading - """ - obj = UnsortedDataclass(1, 2, 3, {"f": 2, "e": 1}) - assert ( - orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - == b'{"c":1,"b":2,"a":3,"d":{"e":1,"f":2}}' - ) - - def test_dataclass_under(self): - """ - dumps() does not include under attributes, InitVar, or ClassVar - """ - obj = InitDataclass("zxc", "vbn") - assert orjson.dumps(obj) == b'{"ab":"zxc vbn"}' - - def test_dataclass_option(self): - """ - dumps() accepts deprecated OPT_SERIALIZE_DATACLASS - """ - obj = Dataclass1("a", 1, None) - assert ( - orjson.dumps(obj, option=orjson.OPT_SERIALIZE_DATACLASS) - == b'{"name":"a","number":1,"sub":null}' - ) - - -class TestDataclassPassthrough: - def test_dataclass_passthrough_raise(self): - """ - dumps() dataclass passes to default with OPT_PASSTHROUGH_DATACLASS - """ - obj = Dataclass1("a", 1, None) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps( - InitDataclass("zxc", "vbn"), option=orjson.OPT_PASSTHROUGH_DATACLASS - ) - - def test_dataclass_passthrough_default(self): - """ - dumps() dataclass passes to default with OPT_PASSTHROUGH_DATACLASS - """ - obj = Dataclass1("a", 1, None) - assert ( - orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS, default=asdict) - == b'{"name":"a","number":1,"sub":null}' - ) - - def default(obj): - if isinstance(obj, Dataclass1): - return {"name": obj.name, "number": obj.number} - raise TypeError - - assert ( - orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS, default=default) - == b'{"name":"a","number":1}' - ) - - -class TestAbstractDataclass: - def test_dataclass_abc(self): - obj = ConcreteAbc(1.0) - assert orjson.dumps(obj) == b'{"attr":1.0}' diff --git a/vendor/orjson/test/test_datetime.py b/vendor/orjson/test/test_datetime.py deleted file mode 100644 index b40ffd3404..0000000000 --- a/vendor/orjson/test/test_datetime.py +++ /dev/null @@ -1,789 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import datetime - -import pytest - -import orjson - -try: - import zoneinfo - - _ = zoneinfo.ZoneInfo("Europe/Amsterdam") -except Exception: # ImportError,ZoneInfoNotFoundError - zoneinfo = None # type: ignore - -try: - import pytz -except ImportError: - pytz = None # type: ignore - -try: - import pendulum -except ImportError: - pendulum = None # type: ignore - -try: - from dateutil import tz -except ImportError: - tz = None # type: ignore - - -AMSTERDAM_1937_DATETIMES = ( - b'["1937-01-01T12:00:27.000087+00:20"]', # tzinfo<2022b and an example in RFC 3339 - b'["1937-01-01T12:00:27.000087+00:00"]', # tzinfo>=2022b -) - -AMSTERDAM_1937_DATETIMES_WITH_Z = ( - b'["1937-01-01T12:00:27.000087+00:20"]', - b'["1937-01-01T12:00:27.000087Z"]', -) - - -class TestDatetime: - def test_datetime_naive(self): - """ - datetime.datetime naive prints without offset - """ - assert ( - orjson.dumps([datetime.datetime(2000, 1, 1, 2, 3, 4, 123)]) - == b'["2000-01-01T02:03:04.000123"]' - ) - - def test_datetime_naive_utc(self): - """ - datetime.datetime naive with opt assumes UTC - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["2000-01-01T02:03:04.000123+00:00"]' - ) - - def test_datetime_min(self): - """ - datetime.datetime min range - """ - assert ( - orjson.dumps( - [datetime.datetime(datetime.MINYEAR, 1, 1, 0, 0, 0, 0)], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["0001-01-01T00:00:00+00:00"]' - ) - - def test_datetime_max(self): - """ - datetime.datetime max range - """ - assert ( - orjson.dumps( - [datetime.datetime(datetime.MAXYEAR, 12, 31, 23, 59, 50, 999999)], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["9999-12-31T23:59:50.999999+00:00"]' - ) - - def test_datetime_three_digits(self): - """ - datetime.datetime three digit year - """ - assert ( - orjson.dumps( - [datetime.datetime(312, 1, 1)], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["0312-01-01T00:00:00+00:00"]' - ) - - def test_datetime_two_digits(self): - """ - datetime.datetime two digit year - """ - assert ( - orjson.dumps( - [datetime.datetime(46, 1, 1)], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["0046-01-01T00:00:00+00:00"]' - ) - - @pytest.mark.skipif(tz is None, reason="dateutil optional") - def test_datetime_tz_assume(self): - """ - datetime.datetime tz with assume UTC uses tz - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 1, 1, 2, 3, 4, 0, tzinfo=tz.gettz("Asia/Shanghai") - ) - ], - option=orjson.OPT_NAIVE_UTC, - ) - == b'["2018-01-01T02:03:04+08:00"]' - ) - - def test_datetime_timezone_utc(self): - """ - datetime.datetime.utc - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 6, 1, 2, 3, 4, 0, tzinfo=datetime.timezone.utc - ) - ] - ) - == b'["2018-06-01T02:03:04+00:00"]' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_pytz_utc(self): - """ - pytz.UTC - """ - assert ( - orjson.dumps([datetime.datetime(2018, 6, 1, 2, 3, 4, 0, tzinfo=pytz.UTC)]) - == b'["2018-06-01T02:03:04+00:00"]' - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_zoneinfo_utc(self): - """ - zoneinfo.ZoneInfo("UTC") - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 6, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("UTC") - ) - ] - ) - == b'["2018-06-01T02:03:04+00:00"]' - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_zoneinfo_positive(self): - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 1, - 1, - 2, - 3, - 4, - 0, - tzinfo=zoneinfo.ZoneInfo("Asia/Shanghai"), - ) - ] - ) - == b'["2018-01-01T02:03:04+08:00"]' - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_zoneinfo_negative(self): - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 6, - 1, - 2, - 3, - 4, - 0, - tzinfo=zoneinfo.ZoneInfo("America/New_York"), - ) - ] - ) - == b'["2018-06-01T02:03:04-04:00"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_pendulum_utc(self): - """ - datetime.datetime UTC - """ - assert ( - orjson.dumps( - [datetime.datetime(2018, 6, 1, 2, 3, 4, 0, tzinfo=pendulum.UTC)] - ) - == b'["2018-06-01T02:03:04+00:00"]' - ) - - @pytest.mark.skipif(tz is None, reason="dateutil optional") - def test_datetime_arrow_positive(self): - """ - datetime.datetime positive UTC - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 1, 1, 2, 3, 4, 0, tzinfo=tz.gettz("Asia/Shanghai") - ) - ] - ) - == b'["2018-01-01T02:03:04+08:00"]' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_pytz_positive(self): - """ - datetime.datetime positive UTC - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 1, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("Asia/Shanghai") - ) - ] - ) - == b'["2018-01-01T02:03:04+08:00"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_pendulum_positive(self): - """ - datetime.datetime positive UTC - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 1, - 1, - 2, - 3, - 4, - 0, - tzinfo=pendulum.timezone("Asia/Shanghai"), # type: ignore - ) - ] - ) - == b'["2018-01-01T02:03:04+08:00"]' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_pytz_negative_dst(self): - """ - datetime.datetime negative UTC DST - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, 6, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("America/New_York") - ) - ] - ) - == b'["2018-06-01T02:03:04-04:00"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_pendulum_negative_dst(self): - """ - datetime.datetime negative UTC DST - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 6, - 1, - 2, - 3, - 4, - 0, - tzinfo=pendulum.timezone("America/New_York"), # type: ignore - ) - ] - ) - == b'["2018-06-01T02:03:04-04:00"]' - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_zoneinfo_negative_non_dst(self): - """ - datetime.datetime negative UTC non-DST - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=zoneinfo.ZoneInfo("America/New_York"), - ) - ] - ) - == b'["2018-12-01T02:03:04-05:00"]' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_pytz_negative_non_dst(self): - """ - datetime.datetime negative UTC non-DST - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=pytz.timezone("America/New_York"), - ) - ] - ) - == b'["2018-12-01T02:03:04-05:00"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_pendulum_negative_non_dst(self): - """ - datetime.datetime negative UTC non-DST - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=pendulum.timezone("America/New_York"), # type: ignore - ) - ] - ) - == b'["2018-12-01T02:03:04-05:00"]' - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_zoneinfo_partial_hour(self): - """ - datetime.datetime UTC offset partial hour - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=zoneinfo.ZoneInfo("Australia/Adelaide"), - ) - ] - ) - == b'["2018-12-01T02:03:04+10:30"]' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_pytz_partial_hour(self): - """ - datetime.datetime UTC offset partial hour - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=pytz.timezone("Australia/Adelaide"), - ) - ] - ) - == b'["2018-12-01T02:03:04+10:30"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_pendulum_partial_hour(self): - """ - datetime.datetime UTC offset partial hour - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2018, - 12, - 1, - 2, - 3, - 4, - 0, - tzinfo=pendulum.timezone("Australia/Adelaide"), # type: ignore - ) - ] - ) - == b'["2018-12-01T02:03:04+10:30"]' - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_partial_second_pendulum_supported(self): - """ - datetime.datetime UTC offset round seconds - - https://tools.ietf.org/html/rfc3339#section-5.8 - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 1937, - 1, - 1, - 12, - 0, - 27, - 87, - tzinfo=pendulum.timezone("Europe/Amsterdam"), # type: ignore - ) - ] - ) - in AMSTERDAM_1937_DATETIMES - ) - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_datetime_partial_second_zoneinfo(self): - """ - datetime.datetime UTC offset round seconds - - https://tools.ietf.org/html/rfc3339#section-5.8 - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 1937, - 1, - 1, - 12, - 0, - 27, - 87, - tzinfo=zoneinfo.ZoneInfo("Europe/Amsterdam"), - ) - ] - ) - in AMSTERDAM_1937_DATETIMES - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_datetime_partial_second_pytz(self): - """ - datetime.datetime UTC offset round seconds - - https://tools.ietf.org/html/rfc3339#section-5.8 - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 1937, - 1, - 1, - 12, - 0, - 27, - 87, - tzinfo=pytz.timezone("Europe/Amsterdam"), - ) - ] - ) - in AMSTERDAM_1937_DATETIMES - ) - - @pytest.mark.skipif(tz is None, reason="dateutil optional") - def test_datetime_partial_second_dateutil(self): - """ - datetime.datetime UTC offset round seconds - - https://tools.ietf.org/html/rfc3339#section-5.8 - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 1937, 1, 1, 12, 0, 27, 87, tzinfo=tz.gettz("Europe/Amsterdam") - ) - ] - ) - in AMSTERDAM_1937_DATETIMES - ) - - def test_datetime_microsecond_max(self): - """ - datetime.datetime microsecond max - """ - assert ( - orjson.dumps(datetime.datetime(2000, 1, 1, 0, 0, 0, 999999)) - == b'"2000-01-01T00:00:00.999999"' - ) - - def test_datetime_microsecond_min(self): - """ - datetime.datetime microsecond min - """ - assert ( - orjson.dumps(datetime.datetime(2000, 1, 1, 0, 0, 0, 1)) - == b'"2000-01-01T00:00:00.000001"' - ) - - def test_datetime_omit_microseconds(self): - """ - datetime.datetime OPT_OMIT_MICROSECONDS - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], - option=orjson.OPT_OMIT_MICROSECONDS, - ) - == b'["2000-01-01T02:03:04"]' - ) - - def test_datetime_omit_microseconds_naive(self): - """ - datetime.datetime naive OPT_OMIT_MICROSECONDS - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], - option=orjson.OPT_NAIVE_UTC | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'["2000-01-01T02:03:04+00:00"]' - ) - - def test_time_omit_microseconds(self): - """ - datetime.time OPT_OMIT_MICROSECONDS - """ - assert ( - orjson.dumps( - [datetime.time(2, 3, 4, 123)], option=orjson.OPT_OMIT_MICROSECONDS - ) - == b'["02:03:04"]' - ) - - def test_datetime_utc_z_naive_omit(self): - """ - datetime.datetime naive OPT_UTC_Z - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], - option=orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z - | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'["2000-01-01T02:03:04Z"]' - ) - - def test_datetime_utc_z_naive(self): - """ - datetime.datetime naive OPT_UTC_Z - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], - option=orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, - ) - == b'["2000-01-01T02:03:04.000123Z"]' - ) - - def test_datetime_utc_z_without_tz(self): - """ - datetime.datetime naive OPT_UTC_Z - """ - assert ( - orjson.dumps( - [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_UTC_Z - ) - == b'["2000-01-01T02:03:04.000123"]' - ) - - @pytest.mark.skipif(tz is None, reason="dateutil optional") - def test_datetime_utc_z_with_tz(self): - """ - datetime.datetime naive OPT_UTC_Z - """ - assert ( - orjson.dumps( - [ - datetime.datetime( - 2000, 1, 1, 0, 0, 0, 1, tzinfo=datetime.timezone.utc - ) - ], - option=orjson.OPT_UTC_Z, - ) - == b'["2000-01-01T00:00:00.000001Z"]' - ) - assert ( - orjson.dumps( - [ - datetime.datetime( - 1937, 1, 1, 12, 0, 27, 87, tzinfo=tz.gettz("Europe/Amsterdam") - ) - ], - option=orjson.OPT_UTC_Z, - ) - in AMSTERDAM_1937_DATETIMES_WITH_Z - ) - - @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") - def test_datetime_roundtrip(self): - """ - datetime.datetime parsed by pendulum - """ - obj = datetime.datetime(2000, 1, 1, 0, 0, 0, 1, tzinfo=datetime.timezone.utc) - serialized = orjson.dumps(obj).decode("utf-8").replace('"', "") - parsed = pendulum.parse(serialized) - for attr in ("year", "month", "day", "hour", "minute", "second", "microsecond"): - assert getattr(obj, attr) == getattr(parsed, attr) - - -class TestDate: - def test_date(self): - """ - datetime.date - """ - assert orjson.dumps([datetime.date(2000, 1, 13)]) == b'["2000-01-13"]' - - def test_date_min(self): - """ - datetime.date MINYEAR - """ - assert ( - orjson.dumps([datetime.date(datetime.MINYEAR, 1, 1)]) == b'["0001-01-01"]' - ) - - def test_date_max(self): - """ - datetime.date MAXYEAR - """ - assert ( - orjson.dumps([datetime.date(datetime.MAXYEAR, 12, 31)]) == b'["9999-12-31"]' - ) - - def test_date_three_digits(self): - """ - datetime.date three digit year - """ - assert ( - orjson.dumps( - [datetime.date(312, 1, 1)], - ) - == b'["0312-01-01"]' - ) - - def test_date_two_digits(self): - """ - datetime.date two digit year - """ - assert ( - orjson.dumps( - [datetime.date(46, 1, 1)], - ) - == b'["0046-01-01"]' - ) - - -class TestTime: - def test_time(self): - """ - datetime.time - """ - assert orjson.dumps([datetime.time(12, 15, 59, 111)]) == b'["12:15:59.000111"]' - assert orjson.dumps([datetime.time(12, 15, 59)]) == b'["12:15:59"]' - - @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") - def test_time_tz(self): - """ - datetime.time with tzinfo error - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps( - [ - datetime.time( - 12, 15, 59, 111, tzinfo=zoneinfo.ZoneInfo("Asia/Shanghai") - ) - ] - ) - - def test_time_microsecond_max(self): - """ - datetime.time microsecond max - """ - assert orjson.dumps(datetime.time(0, 0, 0, 999999)) == b'"00:00:00.999999"' - - def test_time_microsecond_min(self): - """ - datetime.time microsecond min - """ - assert orjson.dumps(datetime.time(0, 0, 0, 1)) == b'"00:00:00.000001"' - - -class TestDateclassPassthrough: - def test_passthrough_datetime(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps( - datetime.datetime(1970, 1, 1), option=orjson.OPT_PASSTHROUGH_DATETIME - ) - - def test_passthrough_date(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps( - datetime.date(1970, 1, 1), option=orjson.OPT_PASSTHROUGH_DATETIME - ) - - def test_passthrough_time(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps( - datetime.time(12, 0, 0), option=orjson.OPT_PASSTHROUGH_DATETIME - ) - - def test_passthrough_datetime_default(self): - def default(obj): - return obj.strftime("%a, %d %b %Y %H:%M:%S GMT") - - assert ( - orjson.dumps( - datetime.datetime(1970, 1, 1), - option=orjson.OPT_PASSTHROUGH_DATETIME, - default=default, - ) - == b'"Thu, 01 Jan 1970 00:00:00 GMT"' - ) diff --git a/vendor/orjson/test/test_default.py b/vendor/orjson/test/test_default.py deleted file mode 100644 index a8f2bcc12f..0000000000 --- a/vendor/orjson/test/test_default.py +++ /dev/null @@ -1,339 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import datetime -import sys -import uuid - -import pytest - -import orjson - -try: - import numpy -except ImportError: - numpy = None # type: ignore - - -class Custom: - def __init__(self): - self.name = uuid.uuid4().hex - - def __str__(self): - return f"{self.__class__.__name__}({self.name})" - - -class Recursive: - def __init__(self, cur): - self.cur = cur - - -def default_recursive(obj): - if obj.cur != 0: - obj.cur -= 1 - return obj - return obj.cur - - -def default_raises(obj): - raise TypeError - - -class TestType: - def test_default_not_callable(self): - """ - dumps() default not callable - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(Custom(), default=NotImplementedError) - - ran = False - try: - orjson.dumps(Custom(), default=NotImplementedError) - except Exception as err: - assert isinstance(err, orjson.JSONEncodeError) - assert str(err) == "default serializer exceeds recursion limit" - ran = True - assert ran - - def test_default_func(self): - """ - dumps() default function - """ - ref = Custom() - - def default(obj): - return str(obj) - - assert orjson.dumps(ref, default=default) == b'"%s"' % str(ref).encode("utf-8") - - def test_default_func_none(self): - """ - dumps() default function None ok - """ - assert orjson.dumps(Custom(), default=lambda x: None) == b"null" - - def test_default_func_empty(self): - """ - dumps() default function no explicit return - """ - ref = Custom() - - def default(obj): - if isinstance(obj, set): - return list(obj) - - assert orjson.dumps(ref, default=default) == b"null" - assert orjson.dumps({ref}, default=default) == b"[null]" - - def test_default_func_exc(self): - """ - dumps() default function raises exception - """ - - def default(obj): - raise NotImplementedError - - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(Custom(), default=default) - - ran = False - try: - orjson.dumps(Custom(), default=default) - except Exception as err: - assert isinstance(err, orjson.JSONEncodeError) - assert str(err) == "Type is not JSON serializable: Custom" - ran = True - assert ran - - def test_default_exception_type(self): - """ - dumps() TypeError in default() raises orjson.JSONEncodeError - """ - ref = Custom() - - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(ref, default=default_raises) - - def test_default_vectorcall_str(self): - """ - dumps() default function vectorcall str - """ - - class SubStr(str): - pass - - obj = SubStr("saasa") - ref = b'"%s"' % str(obj).encode("utf-8") - assert ( - orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_SUBCLASS, default=str) - == ref - ) - - def test_default_vectorcall_list(self): - """ - dumps() default function vectorcall list - """ - obj = {1, 2} - ref = b"[1,2]" - assert orjson.dumps(obj, default=list) == ref - - def test_default_func_nested_str(self): - """ - dumps() default function nested str - """ - ref = Custom() - - def default(obj): - return str(obj) - - assert orjson.dumps({"a": ref}, default=default) == b'{"a":"%s"}' % str( - ref - ).encode("utf-8") - - def test_default_func_list(self): - """ - dumps() default function nested list - """ - ref = Custom() - - def default(obj): - if isinstance(obj, Custom): - return [str(obj)] - - assert orjson.dumps({"a": ref}, default=default) == b'{"a":["%s"]}' % str( - ref - ).encode("utf-8") - - def test_default_func_nested_list(self): - """ - dumps() default function list - """ - ref = Custom() - - def default(obj): - return str(obj) - - assert orjson.dumps([ref] * 100, default=default) == b"[%s]" % b",".join( - b'"%s"' % str(ref).encode("utf-8") for _ in range(100) - ) - - def test_default_func_bytes(self): - """ - dumps() default function errors on non-str - """ - ref = Custom() - - def default(obj): - return bytes(obj) - - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(ref, default=default) - - ran = False - try: - orjson.dumps(ref, default=default) - except Exception as err: - assert isinstance(err, orjson.JSONEncodeError) - assert str(err) == "Type is not JSON serializable: Custom" - ran = True - assert ran - - def test_default_func_invalid_str(self): - """ - dumps() default function errors on invalid str - """ - ref = Custom() - - def default(obj): - return "\ud800" - - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(ref, default=default) - - def test_default_lambda_ok(self): - """ - dumps() default lambda - """ - ref = Custom() - assert orjson.dumps(ref, default=lambda x: str(x)) == b'"%s"' % str(ref).encode( - "utf-8" - ) - - def test_default_callable_ok(self): - """ - dumps() default callable - """ - - class CustomSerializer: - def __init__(self): - self._cache = {} - - def __call__(self, obj): - if obj not in self._cache: - self._cache[obj] = str(obj) - return self._cache[obj] - - ref_obj = Custom() - ref_bytes = b'"%s"' % str(ref_obj).encode("utf-8") - for obj in [ref_obj] * 100: - assert orjson.dumps(obj, default=CustomSerializer()) == ref_bytes - - def test_default_recursion(self): - """ - dumps() default recursion limit - """ - assert orjson.dumps(Recursive(254), default=default_recursive) == b"0" - - def test_default_recursion_reset(self): - """ - dumps() default recursion limit reset - """ - assert ( - orjson.dumps( - [Recursive(254), {"a": "b"}, Recursive(254), Recursive(254)], - default=default_recursive, - ) - == b'[0,{"a":"b"},0,0]' - ) - - def test_default_recursion_infinite(self): - """ - dumps() default infinite recursion - """ - ref = Custom() - - def default(obj): - return obj - - refcount = sys.getrefcount(ref) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(ref, default=default) - assert sys.getrefcount(ref) == refcount - - def test_reference_cleanup_default_custom_pass(self): - ref = Custom() - - def default(obj): - if isinstance(ref, Custom): - return str(ref) - raise TypeError - - refcount = sys.getrefcount(ref) - orjson.dumps(ref, default=default) - assert sys.getrefcount(ref) == refcount - - def test_reference_cleanup_default_custom_error(self): - """ - references to encoded objects are cleaned up - """ - ref = Custom() - - def default(obj): - raise TypeError - - refcount = sys.getrefcount(ref) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(ref, default=default) - assert sys.getrefcount(ref) == refcount - - def test_reference_cleanup_default_subclass(self): - ref = datetime.datetime(1970, 1, 1, 0, 0, 0) - - def default(obj): - if isinstance(ref, datetime.datetime): - return repr(ref) - raise TypeError - - refcount = sys.getrefcount(ref) - orjson.dumps(ref, option=orjson.OPT_PASSTHROUGH_DATETIME, default=default) - assert sys.getrefcount(ref) == refcount - - def test_reference_cleanup_default_subclass_lambda(self): - ref = uuid.uuid4() - - refcount = sys.getrefcount(ref) - orjson.dumps( - ref, option=orjson.OPT_PASSTHROUGH_DATETIME, default=lambda val: str(val) - ) - assert sys.getrefcount(ref) == refcount - - @pytest.mark.skipif(numpy is None, reason="numpy is not installed") - def test_default_numpy(self): - ref = numpy.array([""] * 100) - refcount = sys.getrefcount(ref) - orjson.dumps( - ref, option=orjson.OPT_SERIALIZE_NUMPY, default=lambda val: val.tolist() - ) - assert sys.getrefcount(ref) == refcount - - def test_default_set(self): - """ - dumps() default function with set - """ - - def default(obj): - if isinstance(obj, set): - return list(obj) - raise TypeError - - assert orjson.dumps({1, 2}, default=default) == b"[1,2]" diff --git a/vendor/orjson/test/test_dict.py b/vendor/orjson/test/test_dict.py deleted file mode 100644 index 82468f3154..0000000000 --- a/vendor/orjson/test/test_dict.py +++ /dev/null @@ -1,169 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - - -class TestDict: - def test_dict(self): - """ - dict - """ - obj = {"key": "value"} - ref = '{"key":"value"}' - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == obj - - def test_dict_duplicate_loads(self): - assert orjson.loads(b'{"1":true,"1":false}') == {"1": False} - - def test_dict_empty(self): - obj = [{"key": [{}] * 4096}] * 4096 # type:ignore - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_dict_large_dict(self): - """ - dict with >512 keys - """ - obj = {"key_%s" % idx: [{}, {"a": [{}, {}, {}]}, {}] for idx in range(513)} # type: ignore - assert len(obj) == 513 - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_dict_large_4096(self): - """ - dict with >4096 keys - """ - obj = {"key_%s" % idx: "value_%s" % idx for idx in range(4097)} - assert len(obj) == 4097 - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_dict_large_65536(self): - """ - dict with >65536 keys - """ - obj = {"key_%s" % idx: "value_%s" % idx for idx in range(65537)} - assert len(obj) == 65537 - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_dict_large_keys(self): - """ - dict with keys too large to cache - """ - obj = { - "keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey": "value" - } - ref = '{"keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey":"value"}' - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == obj - - def test_dict_unicode(self): - """ - dict unicode keys - """ - obj = {"🐈": "value"} - ref = b'{"\xf0\x9f\x90\x88":"value"}' - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - assert orjson.loads(ref)["🐈"] == "value" - - def test_dict_invalid_key_dumps(self): - """ - dict invalid key dumps() - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({1: "value"}) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({b"key": "value"}) - - def test_dict_invalid_key_loads(self): - """ - dict invalid key loads() - """ - with pytest.raises(orjson.JSONDecodeError): - orjson.loads('{1:"value"}') - with pytest.raises(orjson.JSONDecodeError): - orjson.loads('{{"a":true}:true}') - - def test_dict_similar_keys(self): - """ - loads() similar keys - - This was a regression in 3.4.2 caused by using - the implementation in wy instead of wyhash. - """ - assert orjson.loads( - '{"cf_status_firefox67": "---", "cf_status_firefox57": "verified"}' - ) == {"cf_status_firefox57": "verified", "cf_status_firefox67": "---"} - - def test_dict_pop_replace_first(self): - "Test pop and replace a first key in a dict with other keys." - data = {"id": "any", "other": "any"} - data.pop("id") - assert orjson.dumps(data) == b'{"other":"any"}' - data["id"] = "new" - assert orjson.dumps(data) == b'{"other":"any","id":"new"}' - - def test_dict_pop_replace_last(self): - "Test pop and replace a last key in a dict with other keys." - data = {"other": "any", "id": "any"} - data.pop("id") - assert orjson.dumps(data) == b'{"other":"any"}' - data["id"] = "new" - assert orjson.dumps(data) == b'{"other":"any","id":"new"}' - - def test_dict_pop(self): - "Test pop and replace a key in a dict with no other keys." - data = {"id": "any"} - data.pop("id") - assert orjson.dumps(data) == b"{}" - data["id"] = "new" - assert orjson.dumps(data) == b'{"id":"new"}' - - def test_in_place(self): - "Mutate dict in-place" - data = {"id": "any", "static": "msg"} - data["id"] = "new" - assert orjson.dumps(data) == b'{"id":"new","static":"msg"}' - - def test_dict_0xff(self): - "dk_size <= 0xff" - data = {str(idx): idx for idx in range(0, 0xFF)} - data.pop("112") - data["112"] = 1 - data["113"] = 2 - assert orjson.loads(orjson.dumps(data)) == data - - def test_dict_0xff_repeated(self): - "dk_size <= 0xff repeated" - for _ in range(0, 100): - data = {str(idx): idx for idx in range(0, 0xFF)} - data.pop("112") - data["112"] = 1 - data["113"] = 2 - assert orjson.loads(orjson.dumps(data)) == data - - def test_dict_0xffff(self): - "dk_size <= 0xffff" - data = {str(idx): idx for idx in range(0, 0xFFFF)} - data.pop("112") - data["112"] = 1 - data["113"] = 2 - assert orjson.loads(orjson.dumps(data)) == data - - def test_dict_0xffff_repeated(self): - "dk_size <= 0xffff repeated" - for _ in range(0, 100): - data = {str(idx): idx for idx in range(0, 0xFFFF)} - data.pop("112") - data["112"] = 1 - data["113"] = 2 - assert orjson.loads(orjson.dumps(data)) == data - - def test_dict_dict(self): - class C: - def __init__(self): - self.a = 0 - self.b = 1 - - assert orjson.dumps(C().__dict__) == b'{"a":0,"b":1}' diff --git a/vendor/orjson/test/test_enum.py b/vendor/orjson/test/test_enum.py deleted file mode 100644 index 1f94d9f0b7..0000000000 --- a/vendor/orjson/test/test_enum.py +++ /dev/null @@ -1,120 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import datetime -import enum - -import pytest - -import orjson - - -class StrEnum(str, enum.Enum): - AAA = "aaa" - - -class IntEnum(int, enum.Enum): - ONE = 1 - - -class IntEnumEnum(enum.IntEnum): - ONE = 1 - - -class IntFlagEnum(enum.IntFlag): - ONE = 1 - - -class FlagEnum(enum.Flag): - ONE = 1 - - -class AutoEnum(enum.auto): - A = "a" - - -class FloatEnum(float, enum.Enum): - ONE = 1.1 - - -class Custom: - def __init__(self, val): - self.val = val - - -def default(obj): - if isinstance(obj, Custom): - return obj.val - raise TypeError - - -class UnspecifiedEnum(enum.Enum): - A = "a" - B = 1 - C = FloatEnum.ONE - D = {"d": IntEnum.ONE} - E = Custom("c") - F = datetime.datetime(1970, 1, 1) - - -class TestEnum: - def test_cannot_subclass(self): - """ - enum.Enum cannot be subclassed - - obj->ob_type->ob_base will always be enum.EnumMeta - """ - with pytest.raises(TypeError): - - class Subclass(StrEnum): # type: ignore - B = "b" - - def test_arbitrary_enum(self): - assert orjson.dumps(UnspecifiedEnum.A) == b'"a"' - assert orjson.dumps(UnspecifiedEnum.B) == b"1" - assert orjson.dumps(UnspecifiedEnum.C) == b"1.1" - assert orjson.dumps(UnspecifiedEnum.D) == b'{"d":1}' - - def test_custom_enum(self): - assert orjson.dumps(UnspecifiedEnum.E, default=default) == b'"c"' - - def test_enum_options(self): - assert ( - orjson.dumps(UnspecifiedEnum.F, option=orjson.OPT_NAIVE_UTC) - == b'"1970-01-01T00:00:00+00:00"' - ) - - def test_int_enum(self): - assert orjson.dumps(IntEnum.ONE) == b"1" - - def test_intenum_enum(self): - assert orjson.dumps(IntEnumEnum.ONE) == b"1" - - def test_intflag_enum(self): - assert orjson.dumps(IntFlagEnum.ONE) == b"1" - - def test_flag_enum(self): - assert orjson.dumps(FlagEnum.ONE) == b"1" - - def test_auto_enum(self): - assert orjson.dumps(AutoEnum.A) == b'"a"' - - def test_float_enum(self): - assert orjson.dumps(FloatEnum.ONE) == b"1.1" - - def test_str_enum(self): - assert orjson.dumps(StrEnum.AAA) == b'"aaa"' - - def test_bool_enum(self): - with pytest.raises(TypeError): - - class BoolEnum(bool, enum.Enum): # type: ignore - TRUE = True - - def test_non_str_keys_enum(self): - assert ( - orjson.dumps({StrEnum.AAA: 1}, option=orjson.OPT_NON_STR_KEYS) - == b'{"aaa":1}' - ) - assert ( - orjson.dumps({IntEnum.ONE: 1}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":1}' - ) diff --git a/vendor/orjson/test/test_error.py b/vendor/orjson/test/test_error.py deleted file mode 100644 index 29e4c731bf..0000000000 --- a/vendor/orjson/test/test_error.py +++ /dev/null @@ -1,190 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import json - -import pytest - -import orjson - -from .util import read_fixture_str - -ASCII_TEST = b"""\ -{ - "a": "qwe", - "b": "qweqwe", - "c": "qweq", - "d: "qwe" -} -""" - -MULTILINE_EMOJI = """[ - "😊", - "a" -""" - - -class TestJsonDecodeError: - def _get_error_infos(self, json_decode_error_exc_info): - return { - k: v - for k, v in json_decode_error_exc_info.value.__dict__.items() - if k in ("pos", "lineno", "colno") - } - - def _test(self, data, expected_err_infos): - with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: - json.loads(data) - - with pytest.raises(json.decoder.JSONDecodeError) as orjson_exc_info: - orjson.loads(data) - - assert ( - self._get_error_infos(json_exc_info) - == self._get_error_infos(orjson_exc_info) - == expected_err_infos - ) - - def test_empty(self): - with pytest.raises(orjson.JSONDecodeError) as json_exc_info: - orjson.loads("") - assert str(json_exc_info.value).startswith( - "Input is a zero-length, empty document:" - ) - - def test_ascii(self): - self._test( - ASCII_TEST, - {"pos": 55, "lineno": 5, "colno": 8}, - ) - - def test_latin1(self): - self._test( - """["üýþÿ", "a" """, - {"pos": 13, "lineno": 1, "colno": 14}, - ) - - def test_two_byte_str(self): - self._test( - """["東京", "a" """, - {"pos": 11, "lineno": 1, "colno": 12}, - ) - - def test_two_byte_bytes(self): - self._test( - b'["\xe6\x9d\xb1\xe4\xba\xac", "a" ', - {"pos": 11, "lineno": 1, "colno": 12}, - ) - - def test_four_byte(self): - self._test( - MULTILINE_EMOJI, - {"pos": 19, "lineno": 4, "colno": 1}, - ) - - def test_tab(self): - data = read_fixture_str("fail26.json", "jsonchecker") - with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: - json.loads(data) - - assert self._get_error_infos(json_exc_info) == { - "pos": 5, - "lineno": 1, - "colno": 6, - } - - with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: - orjson.loads(data) - - assert self._get_error_infos(json_exc_info) == { - "pos": 6, - "lineno": 1, - "colno": 7, - } - - -class Custom: - pass - - -class CustomException(Exception): - pass - - -def default_typeerror(obj): - raise TypeError - - -def default_notimplementederror(obj): - raise NotImplementedError - - -def default_systemerror(obj): - raise SystemError - - -def default_importerror(obj): - import doesnotexist - - assert doesnotexist - - -CUSTOM_ERROR_MESSAGE = "zxc" - - -def default_customerror(obj): - raise CustomException(CUSTOM_ERROR_MESSAGE) - - -class TestJsonEncodeError: - def test_dumps_arg(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps() # type: ignore - assert exc_info.type == orjson.JSONEncodeError - assert ( - str(exc_info.value) - == "dumps() missing 1 required positional argument: 'obj'" - ) - assert exc_info.value.__cause__ is None - - def test_dumps_chain_none(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(Custom()) - assert exc_info.type == orjson.JSONEncodeError - assert str(exc_info.value) == "Type is not JSON serializable: Custom" - assert exc_info.value.__cause__ is None - - def test_dumps_chain_u64(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps([18446744073709551615, Custom()]) - assert exc_info.type == orjson.JSONEncodeError - assert exc_info.value.__cause__ is None - - def test_dumps_chain_default_typeerror(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(Custom(), default=default_typeerror) - assert exc_info.type == orjson.JSONEncodeError - assert isinstance(exc_info.value.__cause__, TypeError) - - def test_dumps_chain_default_systemerror(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(Custom(), default=default_systemerror) - assert exc_info.type == orjson.JSONEncodeError - assert isinstance(exc_info.value.__cause__, SystemError) - - def test_dumps_chain_default_importerror(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(Custom(), default=default_importerror) - assert exc_info.type == orjson.JSONEncodeError - assert isinstance(exc_info.value.__cause__, ImportError) - - def test_dumps_chain_default_customerror(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(Custom(), default=default_customerror) - assert exc_info.type == orjson.JSONEncodeError - assert isinstance(exc_info.value.__cause__, CustomException) - assert str(exc_info.value.__cause__) == CUSTOM_ERROR_MESSAGE - - def test_dumps_normalize_exception(self): - with pytest.raises(orjson.JSONEncodeError) as exc_info: - orjson.dumps(10**60) - assert exc_info.type == orjson.JSONEncodeError diff --git a/vendor/orjson/test/test_fake.py b/vendor/orjson/test/test_fake.py deleted file mode 100644 index bd404b9caf..0000000000 --- a/vendor/orjson/test/test_fake.py +++ /dev/null @@ -1,50 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import random - -import pytest - -import orjson - -try: - from faker import Faker -except ImportError: - Faker = None # type: ignore - -NUM_LOOPS = 10 -NUM_SHUFFLES = 10 -NUM_ENTRIES = 250 - -FAKER_LOCALES = [ - "ar_AA", - "fi_FI", - "fil_PH", - "he_IL", - "ja_JP", - "th_TH", - "tr_TR", - "uk_UA", - "vi_VN", -] - - -class TestFaker: - @pytest.mark.skipif(Faker is None, reason="faker not available") - def test_faker(self): - fake = Faker(FAKER_LOCALES) - profile_keys = list( - set(fake.profile().keys()) - {"birthdate", "current_location"} - ) - for _ in range(0, NUM_LOOPS): - data = [ - { - "person": fake.profile(profile_keys), - "emoji": fake.emoji(), - "text": fake.paragraphs(), - } - for _ in range(0, NUM_ENTRIES) - ] - for _ in range(0, NUM_SHUFFLES): - random.shuffle(data) - output = orjson.dumps(data) - assert orjson.loads(output) == data diff --git a/vendor/orjson/test/test_fixture.py b/vendor/orjson/test/test_fixture.py deleted file mode 100644 index 4121c6d020..0000000000 --- a/vendor/orjson/test/test_fixture.py +++ /dev/null @@ -1,53 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - -from .util import read_fixture_bytes, read_fixture_str - - -class TestFixture: - def test_twitter(self): - """ - loads(),dumps() twitter.json - """ - val = read_fixture_str("twitter.json.xz") - read = orjson.loads(val) - assert orjson.loads(orjson.dumps(read)) == read - - def test_canada(self): - """ - loads(), dumps() canada.json - """ - val = read_fixture_str("canada.json.xz") - read = orjson.loads(val) - assert orjson.loads(orjson.dumps(read)) == read - - def test_citm_catalog(self): - """ - loads(), dumps() citm_catalog.json - """ - val = read_fixture_str("citm_catalog.json.xz") - read = orjson.loads(val) - assert orjson.loads(orjson.dumps(read)) == read - - def test_github(self): - """ - loads(), dumps() github.json - """ - val = read_fixture_str("github.json.xz") - read = orjson.loads(val) - assert orjson.loads(orjson.dumps(read)) == read - - def test_blns(self): - """ - loads() blns.json JSONDecodeError - - https://github.com/minimaxir/big-list-of-naughty-strings - """ - val = read_fixture_bytes("blns.txt.xz") - for line in val.split(b"\n"): - if line and not line.startswith(b"#"): - with pytest.raises(orjson.JSONDecodeError): - _ = orjson.loads(b'"' + val + b'"') diff --git a/vendor/orjson/test/test_fragment.py b/vendor/orjson/test/test_fragment.py deleted file mode 100644 index 734cb06907..0000000000 --- a/vendor/orjson/test/test_fragment.py +++ /dev/null @@ -1,1059 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - -try: - import pandas -except ImportError: - pandas = None # type: ignore - -from .util import read_fixture_bytes - - -class TestFragment: - def test_fragment_fragment_eq(self): - assert orjson.Fragment(b"{}") != orjson.Fragment(b"{}") - - def test_fragment_fragment_not_mut(self): - fragment = orjson.Fragment(b"{}") - with pytest.raises(AttributeError): - fragment.contents = b"[]" - assert orjson.dumps(fragment) == b"{}" - - def test_fragment_repr(self): - assert repr(orjson.Fragment(b"{}")).startswith("?","hex":"ģ䕧覫췯ꯍ\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http://www.JSON.org/","comment":"// /* */":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\\"object with 1 member\\":[\\"array with 1 element\\"]}","quotes":"" \\" %22 0x22 034 "","/\\\\\\"쫾몾ꮘﳞ볚\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string"},0.5,98.6,99.44,1066,10.0,1.0,0.1,1.0,2.0,2.0,"rosebud"]'.encode() - - -class TestJsonChecker: - def _run_fail_json(self, filename, exc=orjson.JSONDecodeError): - data = read_fixture_str(filename, "jsonchecker") - pytest.raises(exc, orjson.loads, data) - - def _run_pass_json(self, filename, match=""): - data = read_fixture_str(filename, "jsonchecker") - assert orjson.dumps(orjson.loads(data)) == match - - def test_fail01(self): - """ - fail01.json - """ - self._run_pass_json( - "fail01.json", - b'"A JSON payload should be an object or array, not a string."', - ) - - def test_fail02(self): - """ - fail02.json - """ - self._run_fail_json("fail02.json", orjson.JSONDecodeError) # EOF - - def test_fail03(self): - """ - fail03.json - """ - self._run_fail_json("fail03.json") - - def test_fail04(self): - """ - fail04.json - """ - self._run_fail_json("fail04.json") - - def test_fail05(self): - """ - fail05.json - """ - self._run_fail_json("fail05.json") - - def test_fail06(self): - """ - fail06.json - """ - self._run_fail_json("fail06.json") - - def test_fail07(self): - """ - fail07.json - """ - self._run_fail_json("fail07.json") - - def test_fail08(self): - """ - fail08.json - """ - self._run_fail_json("fail08.json") - - def test_fail09(self): - """ - fail09.json - """ - self._run_fail_json("fail09.json") - - def test_fail10(self): - """ - fail10.json - """ - self._run_fail_json("fail10.json") - - def test_fail11(self): - """ - fail11.json - """ - self._run_fail_json("fail11.json") - - def test_fail12(self): - """ - fail12.json - """ - self._run_fail_json("fail12.json") - - def test_fail13(self): - """ - fail13.json - """ - self._run_fail_json("fail13.json") - - def test_fail14(self): - """ - fail14.json - """ - self._run_fail_json("fail14.json") - - def test_fail15(self): - """ - fail15.json - """ - self._run_fail_json("fail15.json") - - def test_fail16(self): - """ - fail16.json - """ - self._run_fail_json("fail16.json") - - def test_fail17(self): - """ - fail17.json - """ - self._run_fail_json("fail17.json") - - def test_fail18(self): - """ - fail18.json - """ - self._run_pass_json( - "fail18.json", b'[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' - ) - - def test_fail19(self): - """ - fail19.json - """ - self._run_fail_json("fail19.json") - - def test_fail20(self): - """ - fail20.json - """ - self._run_fail_json("fail20.json") - - def test_fail21(self): - """ - fail21.json - """ - self._run_fail_json("fail21.json") - - def test_fail22(self): - """ - fail22.json - """ - self._run_fail_json("fail22.json") - - def test_fail23(self): - """ - fail23.json - """ - self._run_fail_json("fail23.json") - - def test_fail24(self): - """ - fail24.json - """ - self._run_fail_json("fail24.json") - - def test_fail25(self): - """ - fail25.json - """ - self._run_fail_json("fail25.json") - - def test_fail26(self): - """ - fail26.json - """ - self._run_fail_json("fail26.json") - - def test_fail27(self): - """ - fail27.json - """ - self._run_fail_json("fail27.json") - - def test_fail28(self): - """ - fail28.json - """ - self._run_fail_json("fail28.json") - - def test_fail29(self): - """ - fail29.json - """ - self._run_fail_json("fail29.json") - - def test_fail30(self): - """ - fail30.json - """ - self._run_fail_json("fail30.json") - - def test_fail31(self): - """ - fail31.json - """ - self._run_fail_json("fail31.json") - - def test_fail32(self): - """ - fail32.json - """ - self._run_fail_json("fail32.json", orjson.JSONDecodeError) # EOF - - def test_fail33(self): - """ - fail33.json - """ - self._run_fail_json("fail33.json") - - def test_pass01(self): - """ - pass01.json - """ - self._run_pass_json("pass01.json", PATTERN_1) - - def test_pass02(self): - """ - pass02.json - """ - self._run_pass_json( - "pass02.json", b'[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]' - ) - - def test_pass03(self): - """ - pass03.json - """ - self._run_pass_json( - "pass03.json", - b'{"JSON Test Pattern pass3":{"The outermost value":"must be ' - b'an object or array.","In this test":"It is an object."}}', - ) diff --git a/vendor/orjson/test/test_memory.py b/vendor/orjson/test/test_memory.py deleted file mode 100644 index c4ccc82971..0000000000 --- a/vendor/orjson/test/test_memory.py +++ /dev/null @@ -1,294 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import dataclasses -import datetime -import gc -import random -from typing import List - -try: - import pytz -except ImportError: - pytz = None # type: ignore - -try: - import psutil -except ImportError: - psutil = None # type: ignore -import pytest - -import orjson - -try: - import numpy -except ImportError: - numpy = None # type: ignore - -try: - import pandas -except ImportError: - pandas = None # type: ignore - -FIXTURE = '{"a":[81891289, 8919812.190129012], "b": false, "c": null, "d": "東京"}' - - -def default(obj): - return str(obj) - - -@dataclasses.dataclass -class Member: - id: int - active: bool - - -@dataclasses.dataclass -class Object: - id: int - updated_at: datetime.datetime - name: str - members: List[Member] - - -DATACLASS_FIXTURE = [ - Object( - i, - datetime.datetime.now(datetime.timezone.utc) - + datetime.timedelta(seconds=random.randint(0, 10000)), - str(i) * 3, - [Member(j, True) for j in range(0, 10)], - ) - for i in range(100000, 101000) -] - -MAX_INCREASE = 4194304 # 4MiB - - -class Unsupported: - pass - - -class TestMemory: - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_loads(self): - """ - loads() memory leak - """ - proc = psutil.Process() - gc.collect() - val = orjson.loads(FIXTURE) - assert val - mem = proc.memory_info().rss - for _ in range(10000): - val = orjson.loads(FIXTURE) - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_loads_memoryview(self): - """ - loads() memory leak using memoryview - """ - proc = psutil.Process() - gc.collect() - fixture = FIXTURE.encode("utf-8") - val = orjson.loads(fixture) - assert val - mem = proc.memory_info().rss - for _ in range(10000): - val = orjson.loads(memoryview(fixture)) - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_dumps(self): - """ - dumps() memory leak - """ - proc = psutil.Process() - gc.collect() - fixture = orjson.loads(FIXTURE) - val = orjson.dumps(fixture) - assert val - mem = proc.memory_info().rss - for _ in range(10000): - val = orjson.dumps(fixture) - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_loads_exc(self): - """ - loads() memory leak exception without a GC pause - """ - proc = psutil.Process() - gc.disable() - mem = proc.memory_info().rss - n = 10000 - i = 0 - for _ in range(n): - try: - orjson.loads("") - except orjson.JSONDecodeError: - i += 1 - assert n == i - assert proc.memory_info().rss <= mem + MAX_INCREASE - gc.enable() - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_dumps_exc(self): - """ - dumps() memory leak exception without a GC pause - """ - proc = psutil.Process() - gc.disable() - data = Unsupported() - mem = proc.memory_info().rss - n = 10000 - i = 0 - for _ in range(n): - try: - orjson.dumps(data) - except orjson.JSONEncodeError: - i += 1 - assert n == i - assert proc.memory_info().rss <= mem + MAX_INCREASE - gc.enable() - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_dumps_default(self): - """ - dumps() default memory leak - """ - proc = psutil.Process() - gc.collect() - fixture = orjson.loads(FIXTURE) - - class Custom: - def __init__(self, name): - self.name = name - - def __str__(self): - return f"{self.__class__.__name__}({self.name})" - - fixture["custom"] = Custom("orjson") - val = orjson.dumps(fixture, default=default) - mem = proc.memory_info().rss - for _ in range(10000): - val = orjson.dumps(fixture, default=default) - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_dumps_dataclass(self): - """ - dumps() dataclass memory leak - """ - proc = psutil.Process() - gc.collect() - val = orjson.dumps(DATACLASS_FIXTURE) - assert val - mem = proc.memory_info().rss - for _ in range(100): - val = orjson.dumps(DATACLASS_FIXTURE) - assert val - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif( - psutil is None or pytz is None, - reason="psutil not installed", - ) - def test_memory_dumps_pytz_tzinfo(self): - """ - dumps() pytz tzinfo memory leak - """ - proc = psutil.Process() - gc.collect() - dt = datetime.datetime.now() - val = orjson.dumps(pytz.UTC.localize(dt)) - assert val - mem = proc.memory_info().rss - for _ in range(50000): - val = orjson.dumps(pytz.UTC.localize(dt)) - assert val - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_loads_keys(self): - """ - loads() memory leak with number of keys causing cache eviction - """ - proc = psutil.Process() - gc.collect() - fixture = {"key_%s" % idx: "value" for idx in range(1024)} - assert len(fixture) == 1024 - val = orjson.dumps(fixture) - loaded = orjson.loads(val) - assert loaded - mem = proc.memory_info().rss - for _ in range(100): - loaded = orjson.loads(val) - assert loaded - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - @pytest.mark.skipif(numpy is None, reason="numpy is not installed") - def test_memory_dumps_numpy(self): - """ - dumps() numpy memory leak - """ - proc = psutil.Process() - gc.collect() - fixture = numpy.random.rand(4, 4, 4) - val = orjson.dumps(fixture, option=orjson.OPT_SERIALIZE_NUMPY) - assert val - mem = proc.memory_info().rss - for _ in range(100): - val = orjson.dumps(fixture, option=orjson.OPT_SERIALIZE_NUMPY) - assert val - assert val - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - @pytest.mark.skipif(pandas is None, reason="pandas is not installed") - def test_memory_dumps_pandas(self): - """ - dumps() pandas memory leak - """ - proc = psutil.Process() - gc.collect() - numpy.random.rand(4, 4, 4) - df = pandas.Series(numpy.random.rand(4, 4, 4).tolist()) - val = df.map(orjson.dumps) - assert not val.empty - mem = proc.memory_info().rss - for _ in range(100): - val = df.map(orjson.dumps) - assert not val.empty - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE - - @pytest.mark.skipif(psutil is None, reason="psutil not installed") - def test_memory_dumps_fragment(self): - """ - dumps() Fragment memory leak - """ - proc = psutil.Process() - gc.collect() - orjson.dumps(orjson.Fragment(str(0))) - mem = proc.memory_info().rss - for i in range(10000): - orjson.dumps(orjson.Fragment(str(i))) - gc.collect() - assert proc.memory_info().rss <= mem + MAX_INCREASE diff --git a/vendor/orjson/test/test_non_str_keys.py b/vendor/orjson/test/test_non_str_keys.py deleted file mode 100644 index fad134866d..0000000000 --- a/vendor/orjson/test/test_non_str_keys.py +++ /dev/null @@ -1,306 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import dataclasses -import datetime -import uuid - -import pytest - -import orjson - -try: - import pytz -except ImportError: - pytz = None # type: ignore - -try: - import numpy -except ImportError: - numpy = None # type: ignore - - -class SubStr(str): - pass - - -class TestNonStrKeyTests: - def test_dict_keys_duplicate(self): - """ - OPT_NON_STR_KEYS serializes duplicate keys - """ - assert ( - orjson.dumps({"1": True, 1: False}, option=orjson.OPT_NON_STR_KEYS) - == b'{"1":true,"1":false}' - ) - - def test_dict_keys_int(self): - assert ( - orjson.dumps({1: True, 2: False}, option=orjson.OPT_NON_STR_KEYS) - == b'{"1":true,"2":false}' - ) - - def test_dict_keys_substr(self): - assert ( - orjson.dumps({SubStr("aaa"): True}, option=orjson.OPT_NON_STR_KEYS) - == b'{"aaa":true}' - ) - - def test_dict_keys_substr_passthrough(self): - """ - OPT_PASSTHROUGH_SUBCLASS does not affect OPT_NON_STR_KEYS - """ - assert ( - orjson.dumps( - {SubStr("aaa"): True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_SUBCLASS, - ) - == b'{"aaa":true}' - ) - - def test_dict_keys_substr_invalid(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({SubStr("\ud800"): True}, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_strict(self): - """ - OPT_NON_STR_KEYS does not respect OPT_STRICT_INTEGER - """ - assert ( - orjson.dumps( - {9223372036854775807: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"9223372036854775807":true}' - ) - - def test_dict_keys_int_range_valid_i64(self): - """ - OPT_NON_STR_KEYS has a i64 range for int, valid - """ - assert ( - orjson.dumps( - {9223372036854775807: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"9223372036854775807":true}' - ) - assert ( - orjson.dumps( - {-9223372036854775807: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"-9223372036854775807":true}' - ) - assert ( - orjson.dumps( - {9223372036854775809: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"9223372036854775809":true}' - ) - - def test_dict_keys_int_range_valid_u64(self): - """ - OPT_NON_STR_KEYS has a u64 range for int, valid - """ - assert ( - orjson.dumps( - {0: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"0":true}' - ) - assert ( - orjson.dumps( - {18446744073709551615: True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, - ) - == b'{"18446744073709551615":true}' - ) - - def test_dict_keys_int_range_invalid(self): - """ - OPT_NON_STR_KEYS has a range of i64::MIN to u64::MAX - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({-9223372036854775809: True}, option=orjson.OPT_NON_STR_KEYS) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({18446744073709551616: True}, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_float(self): - assert ( - orjson.dumps({1.1: True, 2.2: False}, option=orjson.OPT_NON_STR_KEYS) - == b'{"1.1":true,"2.2":false}' - ) - - def test_dict_keys_inf(self): - assert ( - orjson.dumps({float("Infinity"): True}, option=orjson.OPT_NON_STR_KEYS) - == b'{"null":true}' - ) - assert ( - orjson.dumps({float("-Infinity"): True}, option=orjson.OPT_NON_STR_KEYS) - == b'{"null":true}' - ) - - def test_dict_keys_nan(self): - assert ( - orjson.dumps({float("NaN"): True}, option=orjson.OPT_NON_STR_KEYS) - == b'{"null":true}' - ) - - def test_dict_keys_bool(self): - assert ( - orjson.dumps({True: True, False: False}, option=orjson.OPT_NON_STR_KEYS) - == b'{"true":true,"false":false}' - ) - - def test_dict_keys_datetime(self): - assert ( - orjson.dumps( - {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, - option=orjson.OPT_NON_STR_KEYS, - ) - == b'{"2000-01-01T02:03:04.000123":true}' - ) - - def test_dict_keys_datetime_opt(self): - assert ( - orjson.dumps( - {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, - option=orjson.OPT_NON_STR_KEYS - | orjson.OPT_OMIT_MICROSECONDS - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'{"2000-01-01T02:03:04Z":true}' - ) - - def test_dict_keys_datetime_passthrough(self): - """ - OPT_PASSTHROUGH_DATETIME does not affect OPT_NON_STR_KEYS - """ - assert ( - orjson.dumps( - {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_DATETIME, - ) - == b'{"2000-01-01T02:03:04.000123":true}' - ) - - def test_dict_keys_uuid(self): - """ - OPT_NON_STR_KEYS always serializes UUID as keys - """ - assert ( - orjson.dumps( - {uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"): True}, - option=orjson.OPT_NON_STR_KEYS, - ) - == b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":true}' - ) - - def test_dict_keys_date(self): - assert ( - orjson.dumps( - {datetime.date(1970, 1, 1): True}, option=orjson.OPT_NON_STR_KEYS - ) - == b'{"1970-01-01":true}' - ) - - def test_dict_keys_time(self): - assert ( - orjson.dumps( - {datetime.time(12, 15, 59, 111): True}, - option=orjson.OPT_NON_STR_KEYS, - ) - == b'{"12:15:59.000111":true}' - ) - - def test_dict_non_str_and_sort_keys(self): - assert ( - orjson.dumps( - { - "other": 1, - datetime.date(1970, 1, 5): 2, - datetime.date(1970, 1, 3): 3, - }, - option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS, - ) - == b'{"1970-01-03":3,"1970-01-05":2,"other":1}' - ) - - @pytest.mark.skipif(pytz is None, reason="pytz optional") - def test_dict_keys_time_err(self): - """ - OPT_NON_STR_KEYS propagates errors in types - """ - val = datetime.time(12, 15, 59, 111, tzinfo=pytz.timezone("Asia/Shanghai")) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_str(self): - assert ( - orjson.dumps({"1": True}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":true}' - ) - - def test_dict_keys_type(self): - class Obj: - a: str - - val = Obj() - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) - - @pytest.mark.skipif(numpy is None, reason="numpy is not installed") - def test_dict_keys_array(self): - with pytest.raises(TypeError): - {numpy.array([1, 2]): True} - - def test_dict_keys_dataclass(self): - @dataclasses.dataclass - class Dataclass: - a: str - - with pytest.raises(TypeError): - {Dataclass("a"): True} - - def test_dict_keys_dataclass_hash(self): - @dataclasses.dataclass - class Dataclass: - a: str - - def __hash__(self): - return 1 - - obj = {Dataclass("a"): True} - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_list(self): - with pytest.raises(TypeError): - {[]: True} - - def test_dict_keys_dict(self): - with pytest.raises(TypeError): - {{}: True} - - def test_dict_keys_tuple(self): - obj = {(): True} - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_unknown(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({frozenset(): True}, option=orjson.OPT_NON_STR_KEYS) - - def test_dict_keys_no_str_call(self): - class Obj: - a: str - - def __str__(self): - return "Obj" - - val = Obj() - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) diff --git a/vendor/orjson/test/test_numpy.py b/vendor/orjson/test/test_numpy.py deleted file mode 100644 index 637faf134e..0000000000 --- a/vendor/orjson/test/test_numpy.py +++ /dev/null @@ -1,1132 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - - -import sys - -import pytest - -import orjson - -try: - import numpy -except ImportError: - numpy = None # type: ignore - - -def numpy_default(obj): - if isinstance(obj, numpy.ndarray): - return obj.tolist() - raise TypeError - - -@pytest.mark.skipif(numpy is None, reason="numpy is not installed") -class TestNumpy: - def test_numpy_array_d1_uintp(self): - low = numpy.iinfo(numpy.uintp).min - high = numpy.iinfo(numpy.uintp).max - assert orjson.dumps( - numpy.array([low, high], numpy.uintp), - option=orjson.OPT_SERIALIZE_NUMPY, - ) == f"[{low},{high}]".encode("ascii") - - def test_numpy_array_d1_intp(self): - low = numpy.iinfo(numpy.intp).min - high = numpy.iinfo(numpy.intp).max - assert orjson.dumps( - numpy.array([low, high], numpy.intp), - option=orjson.OPT_SERIALIZE_NUMPY, - ) == f"[{low},{high}]".encode("ascii") - - def test_numpy_array_d1_i64(self): - assert ( - orjson.dumps( - numpy.array([-9223372036854775807, 9223372036854775807], numpy.int64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[-9223372036854775807,9223372036854775807]" - ) - - def test_numpy_array_d1_u64(self): - assert ( - orjson.dumps( - numpy.array([0, 18446744073709551615], numpy.uint64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[0,18446744073709551615]" - ) - - def test_numpy_array_d1_i8(self): - assert ( - orjson.dumps( - numpy.array([-128, 127], numpy.int8), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[-128,127]" - ) - - def test_numpy_array_d1_u8(self): - assert ( - orjson.dumps( - numpy.array([0, 255], numpy.uint8), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[0,255]" - ) - - def test_numpy_array_d1_i32(self): - assert ( - orjson.dumps( - numpy.array([-2147483647, 2147483647], numpy.int32), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[-2147483647,2147483647]" - ) - - def test_numpy_array_d1_i16(self): - assert ( - orjson.dumps( - numpy.array([-32768, 32767], numpy.int16), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[-32768,32767]" - ) - - def test_numpy_array_d1_u16(self): - assert ( - orjson.dumps( - numpy.array([0, 65535], numpy.uint16), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[0,65535]" - ) - - def test_numpy_array_d1_u32(self): - assert ( - orjson.dumps( - numpy.array([0, 4294967295], numpy.uint32), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[0,4294967295]" - ) - - def test_numpy_array_d1_f32(self): - assert ( - orjson.dumps( - numpy.array([1.0, 3.4028235e38], numpy.float32), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[1.0,3.4028235e38]" - ) - - def test_numpy_array_d1_f16(self): - assert ( - orjson.dumps( - numpy.array([-1.0, 0.0009765625, 1.0, 65504.0], numpy.float16), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[-1.0,0.0009765625,1.0,65504.0]" - ) - - def test_numpy_array_f16_roundtrip(self): - ref = [ - -1.0, - -2.0, - 0.000000059604645, - 0.000060975552, - 0.00006103515625, - 0.0009765625, - 0.33325195, - 0.99951172, - 1.0, - 1.00097656, - 65504.0, - ] - obj = numpy.array(ref, numpy.float16) # type: ignore - serialized = orjson.dumps( - obj, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - deserialized = numpy.array(orjson.loads(serialized), numpy.float16) # type: ignore - assert numpy.array_equal(obj, deserialized) - - def test_numpy_array_f16_edge(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.inf, - -numpy.inf, - numpy.nan, - -0.0, - 0.0, - numpy.pi, - ], - numpy.float16, - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[null,null,null,-0.0,0.0,3.140625]" - ) - - def test_numpy_array_f32_edge(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.inf, - -numpy.inf, - numpy.nan, - -0.0, - 0.0, - numpy.pi, - ], - numpy.float32, - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[null,null,null,-0.0,0.0,3.1415927]" - ) - - def test_numpy_array_f64_edge(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.inf, - -numpy.inf, - numpy.nan, - -0.0, - 0.0, - numpy.pi, - ], - numpy.float64, - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[null,null,null,-0.0,0.0,3.141592653589793]" - ) - - def test_numpy_array_d1_f64(self): - assert ( - orjson.dumps( - numpy.array([1.0, 1.7976931348623157e308], numpy.float64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[1.0,1.7976931348623157e308]" - ) - - def test_numpy_array_d1_bool(self): - assert ( - orjson.dumps( - numpy.array([True, False, False, True]), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[true,false,false,true]" - ) - - def test_numpy_array_d1_datetime64_years(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("1"), - numpy.datetime64("970"), - numpy.datetime64("1920"), - numpy.datetime64("1971"), - numpy.datetime64("2021"), - numpy.datetime64("2022"), - numpy.datetime64("2023"), - numpy.datetime64("9999"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["0001-01-01T00:00:00","0970-01-01T00:00:00","1920-01-01T00:00:00","1971-01-01T00:00:00","2021-01-01T00:00:00","2022-01-01T00:00:00","2023-01-01T00:00:00","9999-01-01T00:00:00"]' - ) - - def test_numpy_array_d1_datetime64_months(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01"), - numpy.datetime64("2022-01"), - numpy.datetime64("2023-01"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2022-01-01T00:00:00","2023-01-01T00:00:00"]' - ) - - def test_numpy_array_d1_datetime64_days(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01"), - numpy.datetime64("2021-01-01"), - numpy.datetime64("2021-01-01"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:00:00","2021-01-01T00:00:00"]' - ) - - def test_numpy_array_d1_datetime64_hours(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00"), - numpy.datetime64("2021-01-01T01"), - numpy.datetime64("2021-01-01T02"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T01:00:00","2021-01-01T02:00:00"]' - ) - - def test_numpy_array_d1_datetime64_minutes(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00"), - numpy.datetime64("2021-01-01T00:01"), - numpy.datetime64("2021-01-01T00:02"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:01:00","2021-01-01T00:02:00"]' - ) - - def test_numpy_array_d1_datetime64_seconds(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00:00"), - numpy.datetime64("2021-01-01T00:00:01"), - numpy.datetime64("2021-01-01T00:00:02"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:00:01","2021-01-01T00:00:02"]' - ) - - def test_numpy_array_d1_datetime64_milliseconds(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00:00"), - numpy.datetime64("2021-01-01T00:00:00.172"), - numpy.datetime64("2021-01-01T00:00:00.567"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567000"]' - ) - - def test_numpy_array_d1_datetime64_microseconds(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00:00"), - numpy.datetime64("2021-01-01T00:00:00.172"), - numpy.datetime64("2021-01-01T00:00:00.567891"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567891"]' - ) - - def test_numpy_array_d1_datetime64_nanoseconds(self): - assert ( - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00:00"), - numpy.datetime64("2021-01-01T00:00:00.172"), - numpy.datetime64("2021-01-01T00:00:00.567891234"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567891"]' - ) - - def test_numpy_array_d1_datetime64_picoseconds(self): - try: - orjson.dumps( - numpy.array( - [ - numpy.datetime64("2021-01-01T00:00:00"), - numpy.datetime64("2021-01-01T00:00:00.172"), - numpy.datetime64("2021-01-01T00:00:00.567891234567"), - ] - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - assert False - except TypeError as exc: - assert str(exc) == "unsupported numpy.datetime64 unit: picoseconds" - - def test_numpy_array_d2_i64(self): - assert ( - orjson.dumps( - numpy.array([[1, 2, 3], [4, 5, 6]], numpy.int64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[1,2,3],[4,5,6]]" - ) - - def test_numpy_array_d2_f64(self): - assert ( - orjson.dumps( - numpy.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], numpy.float64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[1.0,2.0,3.0],[4.0,5.0,6.0]]" - ) - - def test_numpy_array_d3_i8(self): - assert ( - orjson.dumps( - numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int8), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" - ) - - def test_numpy_array_d3_u8(self): - assert ( - orjson.dumps( - numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.uint8), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" - ) - - def test_numpy_array_d3_i32(self): - assert ( - orjson.dumps( - numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int32), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" - ) - - def test_numpy_array_d3_i64(self): - assert ( - orjson.dumps( - numpy.array([[[1, 2], [3, 4], [5, 6], [7, 8]]], numpy.int64), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1,2],[3,4],[5,6],[7,8]]]" - ) - - def test_numpy_array_d3_f64(self): - assert ( - orjson.dumps( - numpy.array( - [[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]], numpy.float64 - ), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]]" - ) - - def test_numpy_array_fortran(self): - array = numpy.array([[1, 2], [3, 4]], order="F") - assert array.flags["F_CONTIGUOUS"] is True - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) - assert orjson.dumps( - array, default=numpy_default, option=orjson.OPT_SERIALIZE_NUMPY - ) == orjson.dumps(array.tolist()) - - def test_numpy_array_non_contiguous_message(self): - array = numpy.array([[1, 2], [3, 4]], order="F") - assert array.flags["F_CONTIGUOUS"] is True - try: - orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) - assert False - except TypeError as exc: - assert ( - str(exc) - == "numpy array is not C contiguous; use ndarray.tolist() in default" - ) - - def test_numpy_array_unsupported_dtype(self): - array = numpy.array([[1, 2], [3, 4]], numpy.csingle) # type: ignore - with pytest.raises(orjson.JSONEncodeError) as cm: - orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) - assert "unsupported datatype in numpy array" in str(cm) - - def test_numpy_array_d1(self): - array = numpy.array([1]) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_d2(self): - array = numpy.array([[1]]) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_d3(self): - array = numpy.array([[[1]]]) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_d4(self): - array = numpy.array([[[[1]]]]) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_4_stride(self): - array = numpy.random.rand(4, 4, 4, 4) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_dimension_zero(self): - array = numpy.array(0) - assert array.ndim == 0 - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) - - array = numpy.empty((0, 4, 2)) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - array = numpy.empty((4, 0, 2)) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - array = numpy.empty((2, 4, 0)) - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_array_dimension_max(self): - array = numpy.random.rand( - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - ) - assert array.ndim == 32 - assert ( - orjson.loads( - orjson.dumps( - array, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - ) - == array.tolist() - ) - - def test_numpy_scalar_int8(self): - assert orjson.dumps(numpy.int8(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps(numpy.int8(127), option=orjson.OPT_SERIALIZE_NUMPY) == b"127" - ) - assert ( - orjson.dumps(numpy.int8(-128), option=orjson.OPT_SERIALIZE_NUMPY) == b"-128" - ) - - def test_numpy_scalar_int16(self): - assert orjson.dumps(numpy.int16(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps(numpy.int16(32767), option=orjson.OPT_SERIALIZE_NUMPY) - == b"32767" - ) - assert ( - orjson.dumps(numpy.int16(-32768), option=orjson.OPT_SERIALIZE_NUMPY) - == b"-32768" - ) - - def test_numpy_scalar_int32(self): - assert orjson.dumps(numpy.int32(1), option=orjson.OPT_SERIALIZE_NUMPY) == b"1" - assert ( - orjson.dumps(numpy.int32(2147483647), option=orjson.OPT_SERIALIZE_NUMPY) - == b"2147483647" - ) - assert ( - orjson.dumps(numpy.int32(-2147483648), option=orjson.OPT_SERIALIZE_NUMPY) - == b"-2147483648" - ) - - def test_numpy_scalar_int64(self): - assert ( - orjson.dumps( - numpy.int64(-9223372036854775808), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b"-9223372036854775808" - ) - assert ( - orjson.dumps( - numpy.int64(9223372036854775807), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b"9223372036854775807" - ) - - def test_numpy_scalar_uint8(self): - assert orjson.dumps(numpy.uint8(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps(numpy.uint8(255), option=orjson.OPT_SERIALIZE_NUMPY) == b"255" - ) - - def test_numpy_scalar_uint16(self): - assert orjson.dumps(numpy.uint16(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps(numpy.uint16(65535), option=orjson.OPT_SERIALIZE_NUMPY) - == b"65535" - ) - - def test_numpy_scalar_uint32(self): - assert orjson.dumps(numpy.uint32(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps(numpy.uint32(4294967295), option=orjson.OPT_SERIALIZE_NUMPY) - == b"4294967295" - ) - - def test_numpy_scalar_uint64(self): - assert orjson.dumps(numpy.uint64(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" - assert ( - orjson.dumps( - numpy.uint64(18446744073709551615), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b"18446744073709551615" - ) - - def test_numpy_scalar_float16(self): - assert ( - orjson.dumps(numpy.float16(1.0), option=orjson.OPT_SERIALIZE_NUMPY) - == b"1.0" - ) - - def test_numpy_scalar_float32(self): - assert ( - orjson.dumps(numpy.float32(1.0), option=orjson.OPT_SERIALIZE_NUMPY) - == b"1.0" - ) - - def test_numpy_scalar_float64(self): - assert ( - orjson.dumps(numpy.float64(123.123), option=orjson.OPT_SERIALIZE_NUMPY) - == b"123.123" - ) - - def test_numpy_bool(self): - assert ( - orjson.dumps( - {"a": numpy.bool_(True), "b": numpy.bool_(False)}, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'{"a":true,"b":false}' - ) - - def test_numpy_datetime_year(self): - assert ( - orjson.dumps(numpy.datetime64("2021"), option=orjson.OPT_SERIALIZE_NUMPY) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_month(self): - assert ( - orjson.dumps(numpy.datetime64("2021-01"), option=orjson.OPT_SERIALIZE_NUMPY) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_day(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01"), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_hour(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00"), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_minute(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00"), option=orjson.OPT_SERIALIZE_NUMPY - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_second(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00"), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_milli(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'"2021-01-01T00:00:00.172000"' - ) - - def test_numpy_datetime_micro(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576"), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'"2021-01-01T00:00:00.172576"' - ) - - def test_numpy_datetime_nano(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576789"), - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b'"2021-01-01T00:00:00.172576"' - ) - - def test_numpy_datetime_naive_utc_year(self): - assert ( - orjson.dumps( - numpy.datetime64("2021"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_month(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_day(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_hour(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_minute(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_second(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00+00:00"' - ) - - def test_numpy_datetime_naive_utc_milli(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00.172000+00:00"' - ) - - def test_numpy_datetime_naive_utc_micro(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00.172576+00:00"' - ) - - def test_numpy_datetime_naive_utc_nano(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576789"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, - ) - == b'"2021-01-01T00:00:00.172576+00:00"' - ) - - def test_numpy_datetime_naive_utc_utc_z_year(self): - assert ( - orjson.dumps( - numpy.datetime64("2021"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_month(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_day(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_hour(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_minute(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_second(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_milli(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00.172000Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_micro(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00.172576Z"' - ) - - def test_numpy_datetime_naive_utc_utc_z_nano(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576789"), - option=orjson.OPT_SERIALIZE_NUMPY - | orjson.OPT_NAIVE_UTC - | orjson.OPT_UTC_Z, - ) - == b'"2021-01-01T00:00:00.172576Z"' - ) - - def test_numpy_datetime_omit_microseconds_year(self): - assert ( - orjson.dumps( - numpy.datetime64("2021"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_month(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_day(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_hour(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_minute(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_second(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_milli(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_micro(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_omit_microseconds_nano(self): - assert ( - orjson.dumps( - numpy.datetime64("2021-01-01T00:00:00.172576789"), - option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, - ) - == b'"2021-01-01T00:00:00"' - ) - - def test_numpy_datetime_nat(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(numpy.datetime64("NaT"), option=orjson.OPT_SERIALIZE_NUMPY) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps([numpy.datetime64("NaT")], option=orjson.OPT_SERIALIZE_NUMPY) - - def test_numpy_repeated(self): - data = numpy.array([[[1, 2], [3, 4], [5, 6], [7, 8]]], numpy.int64) # type: ignore - for _ in range(0, 3): - assert ( - orjson.dumps( - data, - option=orjson.OPT_SERIALIZE_NUMPY, - ) - == b"[[[1,2],[3,4],[5,6],[7,8]]]" - ) - - -@pytest.mark.skipif(numpy is None, reason="numpy is not installed") -class TestNumpyEquivalence: - def _test(self, obj): - assert orjson.dumps(obj, option=orjson.OPT_SERIALIZE_NUMPY) == orjson.dumps( - obj.tolist() - ) - - def test_numpy_uint8(self): - self._test(numpy.array([0, 255], numpy.uint8)) - - def test_numpy_uint16(self): - self._test(numpy.array([0, 65535], numpy.uint16)) - - def test_numpy_uint32(self): - self._test(numpy.array([0, 4294967295], numpy.uint32)) - - def test_numpy_uint64(self): - self._test(numpy.array([0, 18446744073709551615], numpy.uint64)) - - def test_numpy_int8(self): - self._test(numpy.array([-128, 127], numpy.int8)) - - def test_numpy_int16(self): - self._test(numpy.array([-32768, 32767], numpy.int16)) - - def test_numpy_int32(self): - self._test(numpy.array([-2147483647, 2147483647], numpy.int32)) - - def test_numpy_int64(self): - self._test( - numpy.array([-9223372036854775807, 9223372036854775807], numpy.int64) - ) - - @pytest.mark.skip(reason="tolist() conversion results in 3.4028234663852886e38") - def test_numpy_float32(self): - self._test( - numpy.array( - [ - -340282346638528859811704183484516925440.0000000000000000, - 340282346638528859811704183484516925440.0000000000000000, - ], - numpy.float32, - ) - ) - self._test(numpy.array([-3.4028235e38, 3.4028235e38], numpy.float32)) - - def test_numpy_float64(self): - self._test( - numpy.array( - [-1.7976931348623157e308, 1.7976931348623157e308], numpy.float64 - ) - ) - - -@pytest.mark.skipif(numpy is None, reason="numpy is not installed") -class NumpyEndianness: - def test_numpy_array_dimension_zero(self): - wrong_endianness = ">" if sys.byteorder == "little" else "<" - array = numpy.array([0, 1, 0.4, 5.7], dtype=f"{wrong_endianness}f8") - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) diff --git a/vendor/orjson/test/test_parsing.py b/vendor/orjson/test/test_parsing.py deleted file mode 100644 index 8b25129cb8..0000000000 --- a/vendor/orjson/test/test_parsing.py +++ /dev/null @@ -1,1946 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - -from .util import read_fixture_bytes - - -class TestJSONTestSuiteParsing: - def _run_fail_json(self, filename, exc=orjson.JSONDecodeError): - data = read_fixture_bytes(filename, "parsing") - with pytest.raises(exc): - orjson.loads(data) - with pytest.raises(exc): - orjson.loads(bytearray(data)) - with pytest.raises(exc): - orjson.loads(memoryview(data)) - try: - decoded = data.decode("utf-8") - except UnicodeDecodeError: - pass - else: - with pytest.raises(exc): - orjson.loads(decoded) - - def _run_pass_json(self, filename, match=""): - data = read_fixture_bytes(filename, "parsing") - orjson.loads(data) - orjson.loads(bytearray(data)) - orjson.loads(memoryview(data)) - orjson.loads(data.decode("utf-8")) - - def test_y_array_arraysWithSpace(self): - """ - y_array_arraysWithSpaces.json - """ - self._run_pass_json("y_array_arraysWithSpaces.json") - - def test_y_array_empty_string(self): - """ - y_array_empty-string.json - """ - self._run_pass_json("y_array_empty-string.json") - - def test_y_array_empty(self): - """ - y_array_empty.json - """ - self._run_pass_json("y_array_empty.json") - - def test_y_array_ending_with_newline(self): - """ - y_array_ending_with_newline.json - """ - self._run_pass_json("y_array_ending_with_newline.json") - - def test_y_array_false(self): - """ - y_array_false.json - """ - self._run_pass_json("y_array_false.json") - - def test_y_array_heterogeneou(self): - """ - y_array_heterogeneous.json - """ - self._run_pass_json("y_array_heterogeneous.json") - - def test_y_array_null(self): - """ - y_array_null.json - """ - self._run_pass_json("y_array_null.json") - - def test_y_array_with_1_and_newline(self): - """ - y_array_with_1_and_newline.json - """ - self._run_pass_json("y_array_with_1_and_newline.json") - - def test_y_array_with_leading_space(self): - """ - y_array_with_leading_space.json - """ - self._run_pass_json("y_array_with_leading_space.json") - - def test_y_array_with_several_null(self): - """ - y_array_with_several_null.json - """ - self._run_pass_json("y_array_with_several_null.json") - - def test_y_array_with_trailing_space(self): - """ - y_array_with_trailing_space.json - """ - self._run_pass_json("y_array_with_trailing_space.json") - - def test_y_number(self): - """ - y_number.json - """ - self._run_pass_json("y_number.json") - - def test_y_number_0e_1(self): - """ - y_number_0e+1.json - """ - self._run_pass_json("y_number_0e+1.json") - - def test_y_number_0e1(self): - """ - y_number_0e1.json - """ - self._run_pass_json("y_number_0e1.json") - - def test_y_number_after_space(self): - """ - y_number_after_space.json - """ - self._run_pass_json("y_number_after_space.json") - - def test_y_number_double_close_to_zer(self): - """ - y_number_double_close_to_zero.json - """ - self._run_pass_json("y_number_double_close_to_zero.json") - - def test_y_number_int_with_exp(self): - """ - y_number_int_with_exp.json - """ - self._run_pass_json("y_number_int_with_exp.json") - - def test_y_number_minus_zer(self): - """ - y_number_minus_zero.json - """ - self._run_pass_json("y_number_minus_zero.json") - - def test_y_number_negative_int(self): - """ - y_number_negative_int.json - """ - self._run_pass_json("y_number_negative_int.json") - - def test_y_number_negative_one(self): - """ - y_number_negative_one.json - """ - self._run_pass_json("y_number_negative_one.json") - - def test_y_number_negative_zer(self): - """ - y_number_negative_zero.json - """ - self._run_pass_json("y_number_negative_zero.json") - - def test_y_number_real_capital_e(self): - """ - y_number_real_capital_e.json - """ - self._run_pass_json("y_number_real_capital_e.json") - - def test_y_number_real_capital_e_neg_exp(self): - """ - y_number_real_capital_e_neg_exp.json - """ - self._run_pass_json("y_number_real_capital_e_neg_exp.json") - - def test_y_number_real_capital_e_pos_exp(self): - """ - y_number_real_capital_e_pos_exp.json - """ - self._run_pass_json("y_number_real_capital_e_pos_exp.json") - - def test_y_number_real_exponent(self): - """ - y_number_real_exponent.json - """ - self._run_pass_json("y_number_real_exponent.json") - - def test_y_number_real_fraction_exponent(self): - """ - y_number_real_fraction_exponent.json - """ - self._run_pass_json("y_number_real_fraction_exponent.json") - - def test_y_number_real_neg_exp(self): - """ - y_number_real_neg_exp.json - """ - self._run_pass_json("y_number_real_neg_exp.json") - - def test_y_number_real_pos_exponent(self): - """ - y_number_real_pos_exponent.json - """ - self._run_pass_json("y_number_real_pos_exponent.json") - - def test_y_number_simple_int(self): - """ - y_number_simple_int.json - """ - self._run_pass_json("y_number_simple_int.json") - - def test_y_number_simple_real(self): - """ - y_number_simple_real.json - """ - self._run_pass_json("y_number_simple_real.json") - - def test_y_object(self): - """ - y_object.json - """ - self._run_pass_json("y_object.json") - - def test_y_object_basic(self): - """ - y_object_basic.json - """ - self._run_pass_json("y_object_basic.json") - - def test_y_object_duplicated_key(self): - """ - y_object_duplicated_key.json - """ - self._run_pass_json("y_object_duplicated_key.json") - - def test_y_object_duplicated_key_and_value(self): - """ - y_object_duplicated_key_and_value.json - """ - self._run_pass_json("y_object_duplicated_key_and_value.json") - - def test_y_object_empty(self): - """ - y_object_empty.json - """ - self._run_pass_json("y_object_empty.json") - - def test_y_object_empty_key(self): - """ - y_object_empty_key.json - """ - self._run_pass_json("y_object_empty_key.json") - - def test_y_object_escaped_null_in_key(self): - """ - y_object_escaped_null_in_key.json - """ - self._run_pass_json("y_object_escaped_null_in_key.json") - - def test_y_object_extreme_number(self): - """ - y_object_extreme_numbers.json - """ - self._run_pass_json("y_object_extreme_numbers.json") - - def test_y_object_long_string(self): - """ - y_object_long_strings.json - """ - self._run_pass_json("y_object_long_strings.json") - - def test_y_object_simple(self): - """ - y_object_simple.json - """ - self._run_pass_json("y_object_simple.json") - - def test_y_object_string_unicode(self): - """ - y_object_string_unicode.json - """ - self._run_pass_json("y_object_string_unicode.json") - - def test_y_object_with_newline(self): - """ - y_object_with_newlines.json - """ - self._run_pass_json("y_object_with_newlines.json") - - def test_y_string_1_2_3_bytes_UTF_8_sequence(self): - """ - y_string_1_2_3_bytes_UTF-8_sequences.json - """ - self._run_pass_json("y_string_1_2_3_bytes_UTF-8_sequences.json") - - def test_y_string_accepted_surrogate_pair(self): - """ - y_string_accepted_surrogate_pair.json - """ - self._run_pass_json("y_string_accepted_surrogate_pair.json") - - def test_y_string_accepted_surrogate_pairs(self): - """ - y_string_accepted_surrogate_pairs.json - """ - self._run_pass_json("y_string_accepted_surrogate_pairs.json") - - def test_y_string_allowed_escape(self): - """ - y_string_allowed_escapes.json - """ - self._run_pass_json("y_string_allowed_escapes.json") - - def test_y_string_backslash_and_u_escaped_zer(self): - """ - y_string_backslash_and_u_escaped_zero.json - """ - self._run_pass_json("y_string_backslash_and_u_escaped_zero.json") - - def test_y_string_backslash_doublequote(self): - """ - y_string_backslash_doublequotes.json - """ - self._run_pass_json("y_string_backslash_doublequotes.json") - - def test_y_string_comment(self): - """ - y_string_comments.json - """ - self._run_pass_json("y_string_comments.json") - - def test_y_string_double_escape_a(self): - """ - y_string_double_escape_a.json - """ - self._run_pass_json("y_string_double_escape_a.json") - - def test_y_string_double_escape_(self): - """ - y_string_double_escape_n.json - """ - self._run_pass_json("y_string_double_escape_n.json") - - def test_y_string_escaped_control_character(self): - """ - y_string_escaped_control_character.json - """ - self._run_pass_json("y_string_escaped_control_character.json") - - def test_y_string_escaped_noncharacter(self): - """ - y_string_escaped_noncharacter.json - """ - self._run_pass_json("y_string_escaped_noncharacter.json") - - def test_y_string_in_array(self): - """ - y_string_in_array.json - """ - self._run_pass_json("y_string_in_array.json") - - def test_y_string_in_array_with_leading_space(self): - """ - y_string_in_array_with_leading_space.json - """ - self._run_pass_json("y_string_in_array_with_leading_space.json") - - def test_y_string_last_surrogates_1_and_2(self): - """ - y_string_last_surrogates_1_and_2.json - """ - self._run_pass_json("y_string_last_surrogates_1_and_2.json") - - def test_y_string_nbsp_uescaped(self): - """ - y_string_nbsp_uescaped.json - """ - self._run_pass_json("y_string_nbsp_uescaped.json") - - def test_y_string_nonCharacterInUTF_8_U_10FFFF(self): - """ - y_string_nonCharacterInUTF-8_U+10FFFF.json - """ - self._run_pass_json("y_string_nonCharacterInUTF-8_U+10FFFF.json") - - def test_y_string_nonCharacterInUTF_8_U_FFFF(self): - """ - y_string_nonCharacterInUTF-8_U+FFFF.json - """ - self._run_pass_json("y_string_nonCharacterInUTF-8_U+FFFF.json") - - def test_y_string_null_escape(self): - """ - y_string_null_escape.json - """ - self._run_pass_json("y_string_null_escape.json") - - def test_y_string_one_byte_utf_8(self): - """ - y_string_one-byte-utf-8.json - """ - self._run_pass_json("y_string_one-byte-utf-8.json") - - def test_y_string_pi(self): - """ - y_string_pi.json - """ - self._run_pass_json("y_string_pi.json") - - def test_y_string_reservedCharacterInUTF_8_U_1BFFF(self): - """ - y_string_reservedCharacterInUTF-8_U+1BFFF.json - """ - self._run_pass_json("y_string_reservedCharacterInUTF-8_U+1BFFF.json") - - def test_y_string_simple_ascii(self): - """ - y_string_simple_ascii.json - """ - self._run_pass_json("y_string_simple_ascii.json") - - def test_y_string_space(self): - """ - y_string_space.json - """ - self._run_pass_json("y_string_space.json") - - def test_y_string_surrogates_U_1D11E_MUSICAL_SYMBOL_G_CLEF(self): - """ - y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json - """ - self._run_pass_json("y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json") - - def test_y_string_three_byte_utf_8(self): - """ - y_string_three-byte-utf-8.json - """ - self._run_pass_json("y_string_three-byte-utf-8.json") - - def test_y_string_two_byte_utf_8(self): - """ - y_string_two-byte-utf-8.json - """ - self._run_pass_json("y_string_two-byte-utf-8.json") - - def test_y_string_u_2028_line_sep(self): - """ - y_string_u+2028_line_sep.json - """ - self._run_pass_json("y_string_u+2028_line_sep.json") - - def test_y_string_u_2029_par_sep(self): - """ - y_string_u+2029_par_sep.json - """ - self._run_pass_json("y_string_u+2029_par_sep.json") - - def test_y_string_uEscape(self): - """ - y_string_uEscape.json - """ - self._run_pass_json("y_string_uEscape.json") - - def test_y_string_uescaped_newline(self): - """ - y_string_uescaped_newline.json - """ - self._run_pass_json("y_string_uescaped_newline.json") - - def test_y_string_unescaped_char_delete(self): - """ - y_string_unescaped_char_delete.json - """ - self._run_pass_json("y_string_unescaped_char_delete.json") - - def test_y_string_unicode(self): - """ - y_string_unicode.json - """ - self._run_pass_json("y_string_unicode.json") - - def test_y_string_unicodeEscapedBackslash(self): - """ - y_string_unicodeEscapedBackslash.json - """ - self._run_pass_json("y_string_unicodeEscapedBackslash.json") - - def test_y_string_unicode_2(self): - """ - y_string_unicode_2.json - """ - self._run_pass_json("y_string_unicode_2.json") - - def test_y_string_unicode_U_10FFFE_nonchar(self): - """ - y_string_unicode_U+10FFFE_nonchar.json - """ - self._run_pass_json("y_string_unicode_U+10FFFE_nonchar.json") - - def test_y_string_unicode_U_1FFFE_nonchar(self): - """ - y_string_unicode_U+1FFFE_nonchar.json - """ - self._run_pass_json("y_string_unicode_U+1FFFE_nonchar.json") - - def test_y_string_unicode_U_200B_ZERO_WIDTH_SPACE(self): - """ - y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json - """ - self._run_pass_json("y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json") - - def test_y_string_unicode_U_2064_invisible_plu(self): - """ - y_string_unicode_U+2064_invisible_plus.json - """ - self._run_pass_json("y_string_unicode_U+2064_invisible_plus.json") - - def test_y_string_unicode_U_FDD0_nonchar(self): - """ - y_string_unicode_U+FDD0_nonchar.json - """ - self._run_pass_json("y_string_unicode_U+FDD0_nonchar.json") - - def test_y_string_unicode_U_FFFE_nonchar(self): - """ - y_string_unicode_U+FFFE_nonchar.json - """ - self._run_pass_json("y_string_unicode_U+FFFE_nonchar.json") - - def test_y_string_unicode_escaped_double_quote(self): - """ - y_string_unicode_escaped_double_quote.json - """ - self._run_pass_json("y_string_unicode_escaped_double_quote.json") - - def test_y_string_utf8(self): - """ - y_string_utf8.json - """ - self._run_pass_json("y_string_utf8.json") - - def test_y_string_with_del_character(self): - """ - y_string_with_del_character.json - """ - self._run_pass_json("y_string_with_del_character.json") - - def test_y_structure_lonely_false(self): - """ - y_structure_lonely_false.json - """ - self._run_pass_json("y_structure_lonely_false.json") - - def test_y_structure_lonely_int(self): - """ - y_structure_lonely_int.json - """ - self._run_pass_json("y_structure_lonely_int.json") - - def test_y_structure_lonely_negative_real(self): - """ - y_structure_lonely_negative_real.json - """ - self._run_pass_json("y_structure_lonely_negative_real.json") - - def test_y_structure_lonely_null(self): - """ - y_structure_lonely_null.json - """ - self._run_pass_json("y_structure_lonely_null.json") - - def test_y_structure_lonely_string(self): - """ - y_structure_lonely_string.json - """ - self._run_pass_json("y_structure_lonely_string.json") - - def test_y_structure_lonely_true(self): - """ - y_structure_lonely_true.json - """ - self._run_pass_json("y_structure_lonely_true.json") - - def test_y_structure_string_empty(self): - """ - y_structure_string_empty.json - """ - self._run_pass_json("y_structure_string_empty.json") - - def test_y_structure_trailing_newline(self): - """ - y_structure_trailing_newline.json - """ - self._run_pass_json("y_structure_trailing_newline.json") - - def test_y_structure_true_in_array(self): - """ - y_structure_true_in_array.json - """ - self._run_pass_json("y_structure_true_in_array.json") - - def test_y_structure_whitespace_array(self): - """ - y_structure_whitespace_array.json - """ - self._run_pass_json("y_structure_whitespace_array.json") - - def test_n_array_1_true_without_comma(self): - """ - n_array_1_true_without_comma.json - """ - self._run_fail_json("n_array_1_true_without_comma.json") - - def test_n_array_a_invalid_utf8(self): - """ - n_array_a_invalid_utf8.json - """ - self._run_fail_json("n_array_a_invalid_utf8.json") - - def test_n_array_colon_instead_of_comma(self): - """ - n_array_colon_instead_of_comma.json - """ - self._run_fail_json("n_array_colon_instead_of_comma.json") - - def test_n_array_comma_after_close(self): - """ - n_array_comma_after_close.json - """ - self._run_fail_json("n_array_comma_after_close.json") - - def test_n_array_comma_and_number(self): - """ - n_array_comma_and_number.json - """ - self._run_fail_json("n_array_comma_and_number.json") - - def test_n_array_double_comma(self): - """ - n_array_double_comma.json - """ - self._run_fail_json("n_array_double_comma.json") - - def test_n_array_double_extra_comma(self): - """ - n_array_double_extra_comma.json - """ - self._run_fail_json("n_array_double_extra_comma.json") - - def test_n_array_extra_close(self): - """ - n_array_extra_close.json - """ - self._run_fail_json("n_array_extra_close.json") - - def test_n_array_extra_comma(self): - """ - n_array_extra_comma.json - """ - self._run_fail_json("n_array_extra_comma.json") - - def test_n_array_incomplete(self): - """ - n_array_incomplete.json - """ - self._run_fail_json("n_array_incomplete.json") - - def test_n_array_incomplete_invalid_value(self): - """ - n_array_incomplete_invalid_value.json - """ - self._run_fail_json("n_array_incomplete_invalid_value.json") - - def test_n_array_inner_array_no_comma(self): - """ - n_array_inner_array_no_comma.json - """ - self._run_fail_json("n_array_inner_array_no_comma.json") - - def test_n_array_invalid_utf8(self): - """ - n_array_invalid_utf8.json - """ - self._run_fail_json("n_array_invalid_utf8.json") - - def test_n_array_items_separated_by_semicol(self): - """ - n_array_items_separated_by_semicolon.json - """ - self._run_fail_json("n_array_items_separated_by_semicolon.json") - - def test_n_array_just_comma(self): - """ - n_array_just_comma.json - """ - self._run_fail_json("n_array_just_comma.json") - - def test_n_array_just_minu(self): - """ - n_array_just_minus.json - """ - self._run_fail_json("n_array_just_minus.json") - - def test_n_array_missing_value(self): - """ - n_array_missing_value.json - """ - self._run_fail_json("n_array_missing_value.json") - - def test_n_array_newlines_unclosed(self): - """ - n_array_newlines_unclosed.json - """ - self._run_fail_json("n_array_newlines_unclosed.json") - - def test_n_array_number_and_comma(self): - """ - n_array_number_and_comma.json - """ - self._run_fail_json("n_array_number_and_comma.json") - - def test_n_array_number_and_several_comma(self): - """ - n_array_number_and_several_commas.json - """ - self._run_fail_json("n_array_number_and_several_commas.json") - - def test_n_array_spaces_vertical_tab_formfeed(self): - """ - n_array_spaces_vertical_tab_formfeed.json - """ - self._run_fail_json("n_array_spaces_vertical_tab_formfeed.json") - - def test_n_array_star_inside(self): - """ - n_array_star_inside.json - """ - self._run_fail_json("n_array_star_inside.json") - - def test_n_array_unclosed(self): - """ - n_array_unclosed.json - """ - self._run_fail_json("n_array_unclosed.json") - - def test_n_array_unclosed_trailing_comma(self): - """ - n_array_unclosed_trailing_comma.json - """ - self._run_fail_json("n_array_unclosed_trailing_comma.json") - - def test_n_array_unclosed_with_new_line(self): - """ - n_array_unclosed_with_new_lines.json - """ - self._run_fail_json("n_array_unclosed_with_new_lines.json") - - def test_n_array_unclosed_with_object_inside(self): - """ - n_array_unclosed_with_object_inside.json - """ - self._run_fail_json("n_array_unclosed_with_object_inside.json") - - def test_n_incomplete_false(self): - """ - n_incomplete_false.json - """ - self._run_fail_json("n_incomplete_false.json") - - def test_n_incomplete_null(self): - """ - n_incomplete_null.json - """ - self._run_fail_json("n_incomplete_null.json") - - def test_n_incomplete_true(self): - """ - n_incomplete_true.json - """ - self._run_fail_json("n_incomplete_true.json") - - def test_n_multidigit_number_then_00(self): - """ - n_multidigit_number_then_00.json - """ - self._run_fail_json("n_multidigit_number_then_00.json") - - def test_n_number__(self): - """ - n_number_++.json - """ - self._run_fail_json("n_number_++.json") - - def test_n_number_1(self): - """ - n_number_+1.json - """ - self._run_fail_json("n_number_+1.json") - - def test_n_number_Inf(self): - """ - n_number_+Inf.json - """ - self._run_fail_json("n_number_+Inf.json") - - def test_n_number_01(self): - """ - n_number_-01.json - """ - self._run_fail_json("n_number_-01.json") - - def test_n_number_1_0(self): - """ - n_number_-1.0..json - """ - self._run_fail_json("n_number_-1.0..json") - - def test_n_number_2(self): - """ - n_number_-2..json - """ - self._run_fail_json("n_number_-2..json") - - def test_n_number_negative_NaN(self): - """ - n_number_-NaN.json - """ - self._run_fail_json("n_number_-NaN.json") - - def test_n_number_negative_1(self): - """ - n_number_.-1.json - """ - self._run_fail_json("n_number_.-1.json") - - def test_n_number_2e_3(self): - """ - n_number_.2e-3.json - """ - self._run_fail_json("n_number_.2e-3.json") - - def test_n_number_0_1_2(self): - """ - n_number_0.1.2.json - """ - self._run_fail_json("n_number_0.1.2.json") - - def test_n_number_0_3e_(self): - """ - n_number_0.3e+.json - """ - self._run_fail_json("n_number_0.3e+.json") - - def test_n_number_0_3e(self): - """ - n_number_0.3e.json - """ - self._run_fail_json("n_number_0.3e.json") - - def test_n_number_0_e1(self): - """ - n_number_0.e1.json - """ - self._run_fail_json("n_number_0.e1.json") - - def test_n_number_0_capital_E_(self): - """ - n_number_0_capital_E+.json - """ - self._run_fail_json("n_number_0_capital_E+.json") - - def test_n_number_0_capital_E(self): - """ - n_number_0_capital_E.json - """ - self._run_fail_json("n_number_0_capital_E.json") - - def test_n_number_0e_(self): - """ - n_number_0e+.json - """ - self._run_fail_json("n_number_0e+.json") - - def test_n_number_0e(self): - """ - n_number_0e.json - """ - self._run_fail_json("n_number_0e.json") - - def test_n_number_1_0e_(self): - """ - n_number_1.0e+.json - """ - self._run_fail_json("n_number_1.0e+.json") - - def test_n_number_1_0e_2(self): - """ - n_number_1.0e-.json - """ - self._run_fail_json("n_number_1.0e-.json") - - def test_n_number_1_0e(self): - """ - n_number_1.0e.json - """ - self._run_fail_json("n_number_1.0e.json") - - def test_n_number_1_000(self): - """ - n_number_1_000.json - """ - self._run_fail_json("n_number_1_000.json") - - def test_n_number_1eE2(self): - """ - n_number_1eE2.json - """ - self._run_fail_json("n_number_1eE2.json") - - def test_n_number_2_e_3(self): - """ - n_number_2.e+3.json - """ - self._run_fail_json("n_number_2.e+3.json") - - def test_n_number_2_e_3_2(self): - """ - n_number_2.e-3.json - """ - self._run_fail_json("n_number_2.e-3.json") - - def test_n_number_2_e3_3(self): - """ - n_number_2.e3.json - """ - self._run_fail_json("n_number_2.e3.json") - - def test_n_number_9_e_(self): - """ - n_number_9.e+.json - """ - self._run_fail_json("n_number_9.e+.json") - - def test_n_number_negative_Inf(self): - """ - n_number_Inf.json - """ - self._run_fail_json("n_number_Inf.json") - - def test_n_number_NaN(self): - """ - n_number_NaN.json - """ - self._run_fail_json("n_number_NaN.json") - - def test_n_number_U_FF11_fullwidth_digit_one(self): - """ - n_number_U+FF11_fullwidth_digit_one.json - """ - self._run_fail_json("n_number_U+FF11_fullwidth_digit_one.json") - - def test_n_number_expressi(self): - """ - n_number_expression.json - """ - self._run_fail_json("n_number_expression.json") - - def test_n_number_hex_1_digit(self): - """ - n_number_hex_1_digit.json - """ - self._run_fail_json("n_number_hex_1_digit.json") - - def test_n_number_hex_2_digit(self): - """ - n_number_hex_2_digits.json - """ - self._run_fail_json("n_number_hex_2_digits.json") - - def test_n_number_infinity(self): - """ - n_number_infinity.json - """ - self._run_fail_json("n_number_infinity.json") - - def test_n_number_invalid_(self): - """ - n_number_invalid+-.json - """ - self._run_fail_json("n_number_invalid+-.json") - - def test_n_number_invalid_negative_real(self): - """ - n_number_invalid-negative-real.json - """ - self._run_fail_json("n_number_invalid-negative-real.json") - - def test_n_number_invalid_utf_8_in_bigger_int(self): - """ - n_number_invalid-utf-8-in-bigger-int.json - """ - self._run_fail_json("n_number_invalid-utf-8-in-bigger-int.json") - - def test_n_number_invalid_utf_8_in_exponent(self): - """ - n_number_invalid-utf-8-in-exponent.json - """ - self._run_fail_json("n_number_invalid-utf-8-in-exponent.json") - - def test_n_number_invalid_utf_8_in_int(self): - """ - n_number_invalid-utf-8-in-int.json - """ - self._run_fail_json("n_number_invalid-utf-8-in-int.json") - - def test_n_number_minus_infinity(self): - """ - n_number_minus_infinity.json - """ - self._run_fail_json("n_number_minus_infinity.json") - - def test_n_number_minus_sign_with_trailing_garbage(self): - """ - n_number_minus_sign_with_trailing_garbage.json - """ - self._run_fail_json("n_number_minus_sign_with_trailing_garbage.json") - - def test_n_number_minus_space_1(self): - """ - n_number_minus_space_1.json - """ - self._run_fail_json("n_number_minus_space_1.json") - - def test_n_number_neg_int_starting_with_zer(self): - """ - n_number_neg_int_starting_with_zero.json - """ - self._run_fail_json("n_number_neg_int_starting_with_zero.json") - - def test_n_number_neg_real_without_int_part(self): - """ - n_number_neg_real_without_int_part.json - """ - self._run_fail_json("n_number_neg_real_without_int_part.json") - - def test_n_number_neg_with_garbage_at_end(self): - """ - n_number_neg_with_garbage_at_end.json - """ - self._run_fail_json("n_number_neg_with_garbage_at_end.json") - - def test_n_number_real_garbage_after_e(self): - """ - n_number_real_garbage_after_e.json - """ - self._run_fail_json("n_number_real_garbage_after_e.json") - - def test_n_number_real_with_invalid_utf8_after_e(self): - """ - n_number_real_with_invalid_utf8_after_e.json - """ - self._run_fail_json("n_number_real_with_invalid_utf8_after_e.json") - - def test_n_number_real_without_fractional_part(self): - """ - n_number_real_without_fractional_part.json - """ - self._run_fail_json("n_number_real_without_fractional_part.json") - - def test_n_number_starting_with_dot(self): - """ - n_number_starting_with_dot.json - """ - self._run_fail_json("n_number_starting_with_dot.json") - - def test_n_number_with_alpha(self): - """ - n_number_with_alpha.json - """ - self._run_fail_json("n_number_with_alpha.json") - - def test_n_number_with_alpha_char(self): - """ - n_number_with_alpha_char.json - """ - self._run_fail_json("n_number_with_alpha_char.json") - - def test_n_number_with_leading_zer(self): - """ - n_number_with_leading_zero.json - """ - self._run_fail_json("n_number_with_leading_zero.json") - - def test_n_object_bad_value(self): - """ - n_object_bad_value.json - """ - self._run_fail_json("n_object_bad_value.json") - - def test_n_object_bracket_key(self): - """ - n_object_bracket_key.json - """ - self._run_fail_json("n_object_bracket_key.json") - - def test_n_object_comma_instead_of_col(self): - """ - n_object_comma_instead_of_colon.json - """ - self._run_fail_json("n_object_comma_instead_of_colon.json") - - def test_n_object_double_col(self): - """ - n_object_double_colon.json - """ - self._run_fail_json("n_object_double_colon.json") - - def test_n_object_emoji(self): - """ - n_object_emoji.json - """ - self._run_fail_json("n_object_emoji.json") - - def test_n_object_garbage_at_end(self): - """ - n_object_garbage_at_end.json - """ - self._run_fail_json("n_object_garbage_at_end.json") - - def test_n_object_key_with_single_quote(self): - """ - n_object_key_with_single_quotes.json - """ - self._run_fail_json("n_object_key_with_single_quotes.json") - - def test_n_object_lone_continuation_byte_in_key_and_trailing_comma(self): - """ - n_object_lone_continuation_byte_in_key_and_trailing_comma.json - """ - self._run_fail_json( - "n_object_lone_continuation_byte_in_key_and_trailing_comma.json" - ) - - def test_n_object_missing_col(self): - """ - n_object_missing_colon.json - """ - self._run_fail_json("n_object_missing_colon.json") - - def test_n_object_missing_key(self): - """ - n_object_missing_key.json - """ - self._run_fail_json("n_object_missing_key.json") - - def test_n_object_missing_semicol(self): - """ - n_object_missing_semicolon.json - """ - self._run_fail_json("n_object_missing_semicolon.json") - - def test_n_object_missing_value(self): - """ - n_object_missing_value.json - """ - self._run_fail_json("n_object_missing_value.json") - - def test_n_object_no_col(self): - """ - n_object_no-colon.json - """ - self._run_fail_json("n_object_no-colon.json") - - def test_n_object_non_string_key(self): - """ - n_object_non_string_key.json - """ - self._run_fail_json("n_object_non_string_key.json") - - def test_n_object_non_string_key_but_huge_number_instead(self): - """ - n_object_non_string_key_but_huge_number_instead.json - """ - self._run_fail_json("n_object_non_string_key_but_huge_number_instead.json") - - def test_n_object_repeated_null_null(self): - """ - n_object_repeated_null_null.json - """ - self._run_fail_json("n_object_repeated_null_null.json") - - def test_n_object_several_trailing_comma(self): - """ - n_object_several_trailing_commas.json - """ - self._run_fail_json("n_object_several_trailing_commas.json") - - def test_n_object_single_quote(self): - """ - n_object_single_quote.json - """ - self._run_fail_json("n_object_single_quote.json") - - def test_n_object_trailing_comma(self): - """ - n_object_trailing_comma.json - """ - self._run_fail_json("n_object_trailing_comma.json") - - def test_n_object_trailing_comment(self): - """ - n_object_trailing_comment.json - """ - self._run_fail_json("n_object_trailing_comment.json") - - def test_n_object_trailing_comment_ope(self): - """ - n_object_trailing_comment_open.json - """ - self._run_fail_json("n_object_trailing_comment_open.json") - - def test_n_object_trailing_comment_slash_ope(self): - """ - n_object_trailing_comment_slash_open.json - """ - self._run_fail_json("n_object_trailing_comment_slash_open.json") - - def test_n_object_trailing_comment_slash_open_incomplete(self): - """ - n_object_trailing_comment_slash_open_incomplete.json - """ - self._run_fail_json("n_object_trailing_comment_slash_open_incomplete.json") - - def test_n_object_two_commas_in_a_row(self): - """ - n_object_two_commas_in_a_row.json - """ - self._run_fail_json("n_object_two_commas_in_a_row.json") - - def test_n_object_unquoted_key(self): - """ - n_object_unquoted_key.json - """ - self._run_fail_json("n_object_unquoted_key.json") - - def test_n_object_unterminated_value(self): - """ - n_object_unterminated-value.json - """ - self._run_fail_json("n_object_unterminated-value.json") - - def test_n_object_with_single_string(self): - """ - n_object_with_single_string.json - """ - self._run_fail_json("n_object_with_single_string.json") - - def test_n_object_with_trailing_garbage(self): - """ - n_object_with_trailing_garbage.json - """ - self._run_fail_json("n_object_with_trailing_garbage.json") - - def test_n_single_space(self): - """ - n_single_space.json - """ - self._run_fail_json("n_single_space.json") - - def test_n_string_1_surrogate_then_escape(self): - """ - n_string_1_surrogate_then_escape.json - """ - self._run_fail_json("n_string_1_surrogate_then_escape.json") - - def test_n_string_1_surrogate_then_escape_u(self): - """ - n_string_1_surrogate_then_escape_u.json - """ - self._run_fail_json("n_string_1_surrogate_then_escape_u.json") - - def test_n_string_1_surrogate_then_escape_u1(self): - """ - n_string_1_surrogate_then_escape_u1.json - """ - self._run_fail_json("n_string_1_surrogate_then_escape_u1.json") - - def test_n_string_1_surrogate_then_escape_u1x(self): - """ - n_string_1_surrogate_then_escape_u1x.json - """ - self._run_fail_json("n_string_1_surrogate_then_escape_u1x.json") - - def test_n_string_accentuated_char_no_quote(self): - """ - n_string_accentuated_char_no_quotes.json - """ - self._run_fail_json("n_string_accentuated_char_no_quotes.json") - - def test_n_string_backslash_00(self): - """ - n_string_backslash_00.json - """ - self._run_fail_json("n_string_backslash_00.json") - - def test_n_string_escape_x(self): - """ - n_string_escape_x.json - """ - self._run_fail_json("n_string_escape_x.json") - - def test_n_string_escaped_backslash_bad(self): - """ - n_string_escaped_backslash_bad.json - """ - self._run_fail_json("n_string_escaped_backslash_bad.json") - - def test_n_string_escaped_ctrl_char_tab(self): - """ - n_string_escaped_ctrl_char_tab.json - """ - self._run_fail_json("n_string_escaped_ctrl_char_tab.json") - - def test_n_string_escaped_emoji(self): - """ - n_string_escaped_emoji.json - """ - self._run_fail_json("n_string_escaped_emoji.json") - - def test_n_string_incomplete_escape(self): - """ - n_string_incomplete_escape.json - """ - self._run_fail_json("n_string_incomplete_escape.json") - - def test_n_string_incomplete_escaped_character(self): - """ - n_string_incomplete_escaped_character.json - """ - self._run_fail_json("n_string_incomplete_escaped_character.json") - - def test_n_string_incomplete_surrogate(self): - """ - n_string_incomplete_surrogate.json - """ - self._run_fail_json("n_string_incomplete_surrogate.json") - - def test_n_string_incomplete_surrogate_escape_invalid(self): - """ - n_string_incomplete_surrogate_escape_invalid.json - """ - self._run_fail_json("n_string_incomplete_surrogate_escape_invalid.json") - - def test_n_string_invalid_utf_8_in_escape(self): - """ - n_string_invalid-utf-8-in-escape.json - """ - self._run_fail_json("n_string_invalid-utf-8-in-escape.json") - - def test_n_string_invalid_backslash_esc(self): - """ - n_string_invalid_backslash_esc.json - """ - self._run_fail_json("n_string_invalid_backslash_esc.json") - - def test_n_string_invalid_unicode_escape(self): - """ - n_string_invalid_unicode_escape.json - """ - self._run_fail_json("n_string_invalid_unicode_escape.json") - - def test_n_string_invalid_utf8_after_escape(self): - """ - n_string_invalid_utf8_after_escape.json - """ - self._run_fail_json("n_string_invalid_utf8_after_escape.json") - - def test_n_string_leading_uescaped_thinspace(self): - """ - n_string_leading_uescaped_thinspace.json - """ - self._run_fail_json("n_string_leading_uescaped_thinspace.json") - - def test_n_string_no_quotes_with_bad_escape(self): - """ - n_string_no_quotes_with_bad_escape.json - """ - self._run_fail_json("n_string_no_quotes_with_bad_escape.json") - - def test_n_string_single_doublequote(self): - """ - n_string_single_doublequote.json - """ - self._run_fail_json("n_string_single_doublequote.json") - - def test_n_string_single_quote(self): - """ - n_string_single_quote.json - """ - self._run_fail_json("n_string_single_quote.json") - - def test_n_string_single_string_no_double_quote(self): - """ - n_string_single_string_no_double_quotes.json - """ - self._run_fail_json("n_string_single_string_no_double_quotes.json") - - def test_n_string_start_escape_unclosed(self): - """ - n_string_start_escape_unclosed.json - """ - self._run_fail_json("n_string_start_escape_unclosed.json") - - def test_n_string_unescaped_crtl_char(self): - """ - n_string_unescaped_crtl_char.json - """ - self._run_fail_json("n_string_unescaped_crtl_char.json") - - def test_n_string_unescaped_newline(self): - """ - n_string_unescaped_newline.json - """ - self._run_fail_json("n_string_unescaped_newline.json") - - def test_n_string_unescaped_tab(self): - """ - n_string_unescaped_tab.json - """ - self._run_fail_json("n_string_unescaped_tab.json") - - def test_n_string_unicode_CapitalU(self): - """ - n_string_unicode_CapitalU.json - """ - self._run_fail_json("n_string_unicode_CapitalU.json") - - def test_n_string_with_trailing_garbage(self): - """ - n_string_with_trailing_garbage.json - """ - self._run_fail_json("n_string_with_trailing_garbage.json") - - def test_n_structure_100000_opening_array(self): - """ - n_structure_100000_opening_arrays.json - """ - self._run_fail_json("n_structure_100000_opening_arrays.json.xz") - - def test_n_structure_U_2060_word_joined(self): - """ - n_structure_U+2060_word_joined.json - """ - self._run_fail_json("n_structure_U+2060_word_joined.json") - - def test_n_structure_UTF8_BOM_no_data(self): - """ - n_structure_UTF8_BOM_no_data.json - """ - self._run_fail_json("n_structure_UTF8_BOM_no_data.json") - - def test_n_structure_angle_bracket_(self): - """ - n_structure_angle_bracket_..json - """ - self._run_fail_json("n_structure_angle_bracket_..json") - - def test_n_structure_angle_bracket_null(self): - """ - n_structure_angle_bracket_null.json - """ - self._run_fail_json("n_structure_angle_bracket_null.json") - - def test_n_structure_array_trailing_garbage(self): - """ - n_structure_array_trailing_garbage.json - """ - self._run_fail_json("n_structure_array_trailing_garbage.json") - - def test_n_structure_array_with_extra_array_close(self): - """ - n_structure_array_with_extra_array_close.json - """ - self._run_fail_json("n_structure_array_with_extra_array_close.json") - - def test_n_structure_array_with_unclosed_string(self): - """ - n_structure_array_with_unclosed_string.json - """ - self._run_fail_json("n_structure_array_with_unclosed_string.json") - - def test_n_structure_ascii_unicode_identifier(self): - """ - n_structure_ascii-unicode-identifier.json - """ - self._run_fail_json("n_structure_ascii-unicode-identifier.json") - - def test_n_structure_capitalized_True(self): - """ - n_structure_capitalized_True.json - """ - self._run_fail_json("n_structure_capitalized_True.json") - - def test_n_structure_close_unopened_array(self): - """ - n_structure_close_unopened_array.json - """ - self._run_fail_json("n_structure_close_unopened_array.json") - - def test_n_structure_comma_instead_of_closing_brace(self): - """ - n_structure_comma_instead_of_closing_brace.json - """ - self._run_fail_json("n_structure_comma_instead_of_closing_brace.json") - - def test_n_structure_double_array(self): - """ - n_structure_double_array.json - """ - self._run_fail_json("n_structure_double_array.json") - - def test_n_structure_end_array(self): - """ - n_structure_end_array.json - """ - self._run_fail_json("n_structure_end_array.json") - - def test_n_structure_incomplete_UTF8_BOM(self): - """ - n_structure_incomplete_UTF8_BOM.json - """ - self._run_fail_json("n_structure_incomplete_UTF8_BOM.json") - - def test_n_structure_lone_invalid_utf_8(self): - """ - n_structure_lone-invalid-utf-8.json - """ - self._run_fail_json("n_structure_lone-invalid-utf-8.json") - - def test_n_structure_lone_open_bracket(self): - """ - n_structure_lone-open-bracket.json - """ - self._run_fail_json("n_structure_lone-open-bracket.json") - - def test_n_structure_no_data(self): - """ - n_structure_no_data.json - """ - self._run_fail_json("n_structure_no_data.json") - - def test_n_structure_null_byte_outside_string(self): - """ - n_structure_null-byte-outside-string.json - """ - self._run_fail_json("n_structure_null-byte-outside-string.json") - - def test_n_structure_number_with_trailing_garbage(self): - """ - n_structure_number_with_trailing_garbage.json - """ - self._run_fail_json("n_structure_number_with_trailing_garbage.json") - - def test_n_structure_object_followed_by_closing_object(self): - """ - n_structure_object_followed_by_closing_object.json - """ - self._run_fail_json("n_structure_object_followed_by_closing_object.json") - - def test_n_structure_object_unclosed_no_value(self): - """ - n_structure_object_unclosed_no_value.json - """ - self._run_fail_json("n_structure_object_unclosed_no_value.json") - - def test_n_structure_object_with_comment(self): - """ - n_structure_object_with_comment.json - """ - self._run_fail_json("n_structure_object_with_comment.json") - - def test_n_structure_object_with_trailing_garbage(self): - """ - n_structure_object_with_trailing_garbage.json - """ - self._run_fail_json("n_structure_object_with_trailing_garbage.json") - - def test_n_structure_open_array_apostrophe(self): - """ - n_structure_open_array_apostrophe.json - """ - self._run_fail_json("n_structure_open_array_apostrophe.json") - - def test_n_structure_open_array_comma(self): - """ - n_structure_open_array_comma.json - """ - self._run_fail_json("n_structure_open_array_comma.json") - - def test_n_structure_open_array_object(self): - """ - n_structure_open_array_object.json - """ - self._run_fail_json("n_structure_open_array_object.json.xz") - - def test_n_structure_open_array_open_object(self): - """ - n_structure_open_array_open_object.json - """ - self._run_fail_json("n_structure_open_array_open_object.json") - - def test_n_structure_open_array_open_string(self): - """ - n_structure_open_array_open_string.json - """ - self._run_fail_json("n_structure_open_array_open_string.json") - - def test_n_structure_open_array_string(self): - """ - n_structure_open_array_string.json - """ - self._run_fail_json("n_structure_open_array_string.json") - - def test_n_structure_open_object(self): - """ - n_structure_open_object.json - """ - self._run_fail_json("n_structure_open_object.json") - - def test_n_structure_open_object_close_array(self): - """ - n_structure_open_object_close_array.json - """ - self._run_fail_json("n_structure_open_object_close_array.json") - - def test_n_structure_open_object_comma(self): - """ - n_structure_open_object_comma.json - """ - self._run_fail_json("n_structure_open_object_comma.json") - - def test_n_structure_open_object_open_array(self): - """ - n_structure_open_object_open_array.json - """ - self._run_fail_json("n_structure_open_object_open_array.json") - - def test_n_structure_open_object_open_string(self): - """ - n_structure_open_object_open_string.json - """ - self._run_fail_json("n_structure_open_object_open_string.json") - - def test_n_structure_open_object_string_with_apostrophe(self): - """ - n_structure_open_object_string_with_apostrophes.json - """ - self._run_fail_json("n_structure_open_object_string_with_apostrophes.json") - - def test_n_structure_open_ope(self): - """ - n_structure_open_open.json - """ - self._run_fail_json("n_structure_open_open.json") - - def test_n_structure_single_eacute(self): - """ - n_structure_single_eacute.json - """ - self._run_fail_json("n_structure_single_eacute.json") - - def test_n_structure_single_star(self): - """ - n_structure_single_star.json - """ - self._run_fail_json("n_structure_single_star.json") - - def test_n_structure_trailing_(self): - """ - n_structure_trailing_#.json - """ - self._run_fail_json("n_structure_trailing_#.json") - - def test_n_structure_uescaped_LF_before_string(self): - """ - n_structure_uescaped_LF_before_string.json - """ - self._run_fail_json("n_structure_uescaped_LF_before_string.json") - - def test_n_structure_unclosed_array(self): - """ - n_structure_unclosed_array.json - """ - self._run_fail_json("n_structure_unclosed_array.json") - - def test_n_structure_unclosed_array_partial_null(self): - """ - n_structure_unclosed_array_partial_null.json - """ - self._run_fail_json("n_structure_unclosed_array_partial_null.json") - - def test_n_structure_unclosed_array_unfinished_false(self): - """ - n_structure_unclosed_array_unfinished_false.json - """ - self._run_fail_json("n_structure_unclosed_array_unfinished_false.json") - - def test_n_structure_unclosed_array_unfinished_true(self): - """ - n_structure_unclosed_array_unfinished_true.json - """ - self._run_fail_json("n_structure_unclosed_array_unfinished_true.json") - - def test_n_structure_unclosed_object(self): - """ - n_structure_unclosed_object.json - """ - self._run_fail_json("n_structure_unclosed_object.json") - - def test_n_structure_unicode_identifier(self): - """ - n_structure_unicode-identifier.json - """ - self._run_fail_json("n_structure_unicode-identifier.json") - - def test_n_structure_whitespace_U_2060_word_joiner(self): - """ - n_structure_whitespace_U+2060_word_joiner.json - """ - self._run_fail_json("n_structure_whitespace_U+2060_word_joiner.json") - - def test_n_structure_whitespace_formfeed(self): - """ - n_structure_whitespace_formfeed.json - """ - self._run_fail_json("n_structure_whitespace_formfeed.json") - - def test_i_number_double_huge_neg_exp(self): - """ - i_number_double_huge_neg_exp.json - """ - self._run_pass_json("i_number_double_huge_neg_exp.json") - - def test_i_number_huge_exp(self): - """ - i_number_huge_exp.json - """ - self._run_fail_json("i_number_huge_exp.json") - - def test_i_number_neg_int_huge_exp(self): - """ - i_number_neg_int_huge_exp.json - """ - self._run_fail_json("i_number_neg_int_huge_exp.json") - - def test_i_number_pos_double_huge_exp(self): - """ - i_number_pos_double_huge_exp.json - """ - self._run_fail_json("i_number_pos_double_huge_exp.json") - - def test_i_number_real_neg_overflow(self): - """ - i_number_real_neg_overflow.json - """ - self._run_fail_json("i_number_real_neg_overflow.json") - - def test_i_number_real_pos_overflow(self): - """ - i_number_real_pos_overflow.json - """ - self._run_fail_json("i_number_real_pos_overflow.json") - - def test_i_number_real_underflow(self): - """ - i_number_real_underflow.json - """ - self._run_pass_json("i_number_real_underflow.json") - - def test_i_number_too_big_neg_int(self): - """ - i_number_too_big_neg_int.json - """ - self._run_pass_json("i_number_too_big_neg_int.json") - - def test_i_number_too_big_pos_int(self): - """ - i_number_too_big_pos_int.json - """ - self._run_pass_json("i_number_too_big_pos_int.json") - - def test_i_number_very_big_negative_int(self): - """ - i_number_very_big_negative_int.json - """ - self._run_pass_json("i_number_very_big_negative_int.json") - - def test_i_object_key_lone_2nd_surrogate(self): - """ - i_object_key_lone_2nd_surrogate.json - """ - self._run_fail_json("i_object_key_lone_2nd_surrogate.json") - - def test_i_string_1st_surrogate_but_2nd_missing(self): - """ - i_string_1st_surrogate_but_2nd_missing.json - """ - self._run_fail_json("i_string_1st_surrogate_but_2nd_missing.json") - - def test_i_string_1st_valid_surrogate_2nd_invalid(self): - """ - i_string_1st_valid_surrogate_2nd_invalid.json - """ - self._run_fail_json("i_string_1st_valid_surrogate_2nd_invalid.json") - - def test_i_string_UTF_16LE_with_BOM(self): - """ - i_string_UTF-16LE_with_BOM.json - """ - self._run_fail_json("i_string_UTF-16LE_with_BOM.json") - - def test_i_string_UTF_8_invalid_sequence(self): - """ - i_string_UTF-8_invalid_sequence.json - """ - self._run_fail_json("i_string_UTF-8_invalid_sequence.json") - - def test_i_string_UTF8_surrogate_U_D800(self): - """ - i_string_UTF8_surrogate_U+D800.json - """ - self._run_fail_json("i_string_UTF8_surrogate_U+D800.json") - - def test_i_string_incomplete_surrogate_and_escape_valid(self): - """ - i_string_incomplete_surrogate_and_escape_valid.json - """ - self._run_fail_json("i_string_incomplete_surrogate_and_escape_valid.json") - - def test_i_string_incomplete_surrogate_pair(self): - """ - i_string_incomplete_surrogate_pair.json - """ - self._run_fail_json("i_string_incomplete_surrogate_pair.json") - - def test_i_string_incomplete_surrogates_escape_valid(self): - """ - i_string_incomplete_surrogates_escape_valid.json - """ - self._run_fail_json("i_string_incomplete_surrogates_escape_valid.json") - - def test_i_string_invalid_lonely_surrogate(self): - """ - i_string_invalid_lonely_surrogate.json - """ - self._run_fail_json("i_string_invalid_lonely_surrogate.json") - - def test_i_string_invalid_surrogate(self): - """ - i_string_invalid_surrogate.json - """ - self._run_fail_json("i_string_invalid_surrogate.json") - - def test_i_string_invalid_utf_8(self): - """ - i_string_invalid_utf-8.json - """ - self._run_fail_json("i_string_invalid_utf-8.json") - - def test_i_string_inverted_surrogates_U_1D11E(self): - """ - i_string_inverted_surrogates_U+1D11E.json - """ - self._run_fail_json("i_string_inverted_surrogates_U+1D11E.json") - - def test_i_string_iso_latin_1(self): - """ - i_string_iso_latin_1.json - """ - self._run_fail_json("i_string_iso_latin_1.json") - - def test_i_string_lone_second_surrogate(self): - """ - i_string_lone_second_surrogate.json - """ - self._run_fail_json("i_string_lone_second_surrogate.json") - - def test_i_string_lone_utf8_continuation_byte(self): - """ - i_string_lone_utf8_continuation_byte.json - """ - self._run_fail_json("i_string_lone_utf8_continuation_byte.json") - - def test_i_string_not_in_unicode_range(self): - """ - i_string_not_in_unicode_range.json - """ - self._run_fail_json("i_string_not_in_unicode_range.json") - - def test_i_string_overlong_sequence_2_byte(self): - """ - i_string_overlong_sequence_2_bytes.json - """ - self._run_fail_json("i_string_overlong_sequence_2_bytes.json") - - def test_i_string_overlong_sequence_6_byte(self): - """ - i_string_overlong_sequence_6_bytes.json - """ - self._run_fail_json("i_string_overlong_sequence_6_bytes.json") - - def test_i_string_overlong_sequence_6_bytes_null(self): - """ - i_string_overlong_sequence_6_bytes_null.json - """ - self._run_fail_json("i_string_overlong_sequence_6_bytes_null.json") - - def test_i_string_truncated_utf_8(self): - """ - i_string_truncated-utf-8.json - """ - self._run_fail_json("i_string_truncated-utf-8.json") - - def test_i_string_utf16BE_no_BOM(self): - """ - i_string_utf16BE_no_BOM.json - """ - self._run_fail_json("i_string_utf16BE_no_BOM.json") - - def test_i_string_utf16LE_no_BOM(self): - """ - i_string_utf16LE_no_BOM.json - """ - self._run_fail_json("i_string_utf16LE_no_BOM.json") - - def test_i_structure_500_nested_array(self): - """ - i_structure_500_nested_arrays.json - """ - try: - self._run_pass_json("i_structure_500_nested_arrays.json.xz") - except orjson.JSONDecodeError: - # fails on serde, passes on yyjson - pass - - def test_i_structure_UTF_8_BOM_empty_object(self): - """ - i_structure_UTF-8_BOM_empty_object.json - """ - self._run_fail_json("i_structure_UTF-8_BOM_empty_object.json") diff --git a/vendor/orjson/test/test_reentrant.py b/vendor/orjson/test/test_reentrant.py deleted file mode 100644 index 9863a6c118..0000000000 --- a/vendor/orjson/test/test_reentrant.py +++ /dev/null @@ -1,16 +0,0 @@ -import orjson - - -class C: - c: "C" - - def __del__(self): - orjson.loads('"' + "a" * 10000 + '"') - - -def test_reentrant(): - c = C() - c.c = c - del c - - orjson.loads("[" + "[]," * 1000 + "[]]") diff --git a/vendor/orjson/test/test_roundtrip.py b/vendor/orjson/test/test_roundtrip.py deleted file mode 100644 index 0acc286859..0000000000 --- a/vendor/orjson/test/test_roundtrip.py +++ /dev/null @@ -1,173 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - -from .util import read_fixture_str - - -class TestJsonChecker: - def _run_roundtrip_json(self, filename): - data = read_fixture_str(filename, "roundtrip") - assert orjson.dumps(orjson.loads(data)) == data.encode("utf-8") - - def test_roundtrip001(self): - """ - roundtrip001.json - """ - self._run_roundtrip_json("roundtrip01.json") - - def test_roundtrip002(self): - """ - roundtrip002.json - """ - self._run_roundtrip_json("roundtrip02.json") - - def test_roundtrip003(self): - """ - roundtrip003.json - """ - self._run_roundtrip_json("roundtrip03.json") - - def test_roundtrip004(self): - """ - roundtrip004.json - """ - self._run_roundtrip_json("roundtrip04.json") - - def test_roundtrip005(self): - """ - roundtrip005.json - """ - self._run_roundtrip_json("roundtrip05.json") - - def test_roundtrip006(self): - """ - roundtrip006.json - """ - self._run_roundtrip_json("roundtrip06.json") - - def test_roundtrip007(self): - """ - roundtrip007.json - """ - self._run_roundtrip_json("roundtrip07.json") - - def test_roundtrip008(self): - """ - roundtrip008.json - """ - self._run_roundtrip_json("roundtrip08.json") - - def test_roundtrip009(self): - """ - roundtrip009.json - """ - self._run_roundtrip_json("roundtrip09.json") - - def test_roundtrip010(self): - """ - roundtrip010.json - """ - self._run_roundtrip_json("roundtrip10.json") - - def test_roundtrip011(self): - """ - roundtrip011.json - """ - self._run_roundtrip_json("roundtrip11.json") - - def test_roundtrip012(self): - """ - roundtrip012.json - """ - self._run_roundtrip_json("roundtrip12.json") - - def test_roundtrip013(self): - """ - roundtrip013.json - """ - self._run_roundtrip_json("roundtrip13.json") - - def test_roundtrip014(self): - """ - roundtrip014.json - """ - self._run_roundtrip_json("roundtrip14.json") - - def test_roundtrip015(self): - """ - roundtrip015.json - """ - self._run_roundtrip_json("roundtrip15.json") - - def test_roundtrip016(self): - """ - roundtrip016.json - """ - self._run_roundtrip_json("roundtrip16.json") - - def test_roundtrip017(self): - """ - roundtrip017.json - """ - self._run_roundtrip_json("roundtrip17.json") - - def test_roundtrip018(self): - """ - roundtrip018.json - """ - self._run_roundtrip_json("roundtrip18.json") - - def test_roundtrip019(self): - """ - roundtrip019.json - """ - self._run_roundtrip_json("roundtrip19.json") - - def test_roundtrip020(self): - """ - roundtrip020.json - """ - self._run_roundtrip_json("roundtrip20.json") - - def test_roundtrip021(self): - """ - roundtrip021.json - """ - self._run_roundtrip_json("roundtrip21.json") - - def test_roundtrip022(self): - """ - roundtrip022.json - """ - self._run_roundtrip_json("roundtrip22.json") - - def test_roundtrip023(self): - """ - roundtrip023.json - """ - self._run_roundtrip_json("roundtrip23.json") - - def test_roundtrip024(self): - """ - roundtrip024.json - """ - self._run_roundtrip_json("roundtrip24.json") - - def test_roundtrip025(self): - """ - roundtrip025.json - """ - self._run_roundtrip_json("roundtrip25.json") - - def test_roundtrip026(self): - """ - roundtrip026.json - """ - self._run_roundtrip_json("roundtrip26.json") - - def test_roundtrip027(self): - """ - roundtrip027.json - """ - self._run_roundtrip_json("roundtrip27.json") diff --git a/vendor/orjson/test/test_sort_keys.py b/vendor/orjson/test/test_sort_keys.py deleted file mode 100644 index 2d79435022..0000000000 --- a/vendor/orjson/test/test_sort_keys.py +++ /dev/null @@ -1,50 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - -from .util import read_fixture_obj - - -class TestDictSortKeys: - # citm_catalog is already sorted - def test_twitter_sorted(self): - """ - twitter.json sorted - """ - obj = read_fixture_obj("twitter.json.xz") - assert list(obj.keys()) != sorted(list(obj.keys())) - serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - val = orjson.loads(serialized) - assert list(val.keys()) == sorted(list(val.keys())) - - def test_canada_sorted(self): - """ - canada.json sorted - """ - obj = read_fixture_obj("canada.json.xz") - assert list(obj.keys()) != sorted(list(obj.keys())) - serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - val = orjson.loads(serialized) - assert list(val.keys()) == sorted(list(val.keys())) - - def test_github_sorted(self): - """ - github.json sorted - """ - obj = read_fixture_obj("github.json.xz") - for each in obj: - assert list(each.keys()) != sorted(list(each.keys())) - serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - val = orjson.loads(serialized) - for each in val: - assert list(each.keys()) == sorted(list(each.keys())) - - def test_utf8_sorted(self): - """ - UTF-8 sorted - """ - obj = {"a": 1, "ä": 2, "A": 3} - assert list(obj.keys()) != sorted(list(obj.keys())) - serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) - val = orjson.loads(serialized) - assert list(val.keys()) == sorted(list(val.keys())) diff --git a/vendor/orjson/test/test_subclass.py b/vendor/orjson/test/test_subclass.py deleted file mode 100644 index 57842bb9a8..0000000000 --- a/vendor/orjson/test/test_subclass.py +++ /dev/null @@ -1,112 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import collections -import json - -import pytest - -import orjson - - -class SubStr(str): - pass - - -class SubInt(int): - pass - - -class SubDict(dict): - pass - - -class SubList(list): - pass - - -class SubFloat(float): - pass - - -class SubTuple(tuple): - pass - - -class TestSubclass: - def test_subclass_str(self): - assert orjson.dumps(SubStr("zxc")) == b'"zxc"' - - def test_subclass_str_invalid(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubStr("\ud800")) - - def test_subclass_int(self): - assert orjson.dumps(SubInt(1)) == b"1" - - def test_subclass_int_64(self): - for val in (9223372036854775807, -9223372036854775807): - assert orjson.dumps(SubInt(val)) == str(val).encode("utf-8") - - def test_subclass_int_53(self): - for val in (9007199254740992, -9007199254740992): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubInt(val), option=orjson.OPT_STRICT_INTEGER) - - def test_subclass_dict(self): - assert orjson.dumps(SubDict({"a": "b"})) == b'{"a":"b"}' - - def test_subclass_list(self): - assert orjson.dumps(SubList(["a", "b"])) == b'["a","b"]' - ref = [True] * 512 - assert orjson.loads(orjson.dumps(SubList(ref))) == ref - - def test_subclass_float(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubFloat(1.1)) - assert json.dumps(SubFloat(1.1)) == "1.1" - - def test_subclass_tuple(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubTuple((1, 2))) - assert json.dumps(SubTuple((1, 2))) == "[1, 2]" - - def test_namedtuple(self): - Point = collections.namedtuple("Point", ["x", "y"]) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(Point(1, 2)) - - def test_subclass_circular_dict(self): - obj = SubDict({}) - obj["obj"] = obj - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - def test_subclass_circular_list(self): - obj = SubList([]) - obj.append(obj) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - def test_subclass_circular_nested(self): - obj = SubDict({}) - obj["list"] = SubList([{"obj": obj}]) - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(obj) - - -class TestSubclassPassthrough: - def test_subclass_str(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubStr("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS) - - def test_subclass_int(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubInt(1), option=orjson.OPT_PASSTHROUGH_SUBCLASS) - - def test_subclass_dict(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubDict({"a": "b"}), option=orjson.OPT_PASSTHROUGH_SUBCLASS) - - def test_subclass_list(self): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(SubList(["a", "b"]), option=orjson.OPT_PASSTHROUGH_SUBCLASS) diff --git a/vendor/orjson/test/test_transform.py b/vendor/orjson/test/test_transform.py deleted file mode 100644 index caee07b284..0000000000 --- a/vendor/orjson/test/test_transform.py +++ /dev/null @@ -1,129 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import pytest - -import orjson - -from .util import read_fixture_bytes - - -def _read_file(filename): - return read_fixture_bytes(filename, "transform").strip(b"\n").strip(b"\r") - - -class TestJSONTestSuiteTransform: - def _pass_transform(self, filename, reference=None): - data = _read_file(filename) - assert orjson.dumps(orjson.loads(data)) == (reference or data) - - def _fail_transform(self, filename): - data = _read_file(filename) - with pytest.raises(orjson.JSONDecodeError): - orjson.loads(data) - - def test_number_1(self): - """ - number_1.0.json - """ - self._pass_transform("number_1.0.json") - - def test_number_1e6(self): - """ - number_1e6.json - """ - self._pass_transform("number_1e6.json", b"[1000000.0]") - - def test_number_1e_999(self): - """ - number_1e-999.json - """ - self._pass_transform("number_1e-999.json", b"[0.0]") - - def test_number_10000000000000000999(self): - """ - number_10000000000000000999.json - """ - # cannot serialize due to range - assert orjson.loads(_read_file("number_10000000000000000999.json")) == [ - 10000000000000000999 - ] - - def test_number_1000000000000000(self): - """ - number_1000000000000000.json - """ - self._pass_transform("number_1000000000000000.json") - - def test_object_key_nfc_nfd(self): - """ - object_key_nfc_nfd.json - """ - self._pass_transform("object_key_nfc_nfd.json") - - def test_object_key_nfd_nfc(self): - """ - object_key_nfd_nfc.json - """ - self._pass_transform("object_key_nfd_nfc.json") - - def test_object_same_key_different_values(self): - """ - object_same_key_different_values.json - """ - self._pass_transform("object_same_key_different_values.json", b'{"a":2}') - - def test_object_same_key_same_value(self): - """ - object_same_key_same_value.json - """ - self._pass_transform("object_same_key_same_value.json", b'{"a":1}') - - def test_object_same_key_unclear_values(self): - """ - object_same_key_unclear_values.json - """ - data = _read_file("object_same_key_unclear_values.json") - # varies by backend - assert data in (b'{"a":-0.0}', b'{"a":0, "a":-0}') - - def test_string_1_escaped_invalid_codepoint(self): - """ - string_1_escaped_invalid_codepoint.json - """ - self._fail_transform("string_1_escaped_invalid_codepoint.json") - - def test_string_1_invalid_codepoint(self): - """ - string_1_invalid_codepoint.json - """ - self._fail_transform("string_1_invalid_codepoint.json") - - def test_string_2_escaped_invalid_codepoints(self): - """ - string_2_escaped_invalid_codepoints.json - """ - self._fail_transform("string_2_escaped_invalid_codepoints.json") - - def test_string_2_invalid_codepoints(self): - """ - string_2_invalid_codepoints.json - """ - self._fail_transform("string_2_invalid_codepoints.json") - - def test_string_3_escaped_invalid_codepoints(self): - """ - string_3_escaped_invalid_codepoints.json - """ - self._fail_transform("string_3_escaped_invalid_codepoints.json") - - def test_string_3_invalid_codepoints(self): - """ - string_3_invalid_codepoints.json - """ - self._fail_transform("string_3_invalid_codepoints.json") - - def test_string_with_escaped_NULL(self): - """ - string_with_escaped_NULL.json - """ - self._pass_transform("string_with_escaped_NULL.json") diff --git a/vendor/orjson/test/test_type.py b/vendor/orjson/test/test_type.py deleted file mode 100644 index 81c8930745..0000000000 --- a/vendor/orjson/test/test_type.py +++ /dev/null @@ -1,550 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import io -import sys - -import pytest - -try: - import xxhash -except ImportError: - xxhash = None - -import orjson - - -class TestType: - def test_fragment(self): - """ - orjson.JSONDecodeError on fragments - """ - for val in ("n", "{", "[", "t"): - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_invalid(self): - """ - orjson.JSONDecodeError on invalid - """ - for val in ('{"age", 44}', "[31337,]", "[,31337]", "[]]", "[,]"): - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_str(self): - """ - str - """ - for obj, ref in (("blah", b'"blah"'), ("東京", b'"\xe6\x9d\xb1\xe4\xba\xac"')): - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - - def test_str_latin1(self): - """ - str latin1 - """ - assert orjson.loads(orjson.dumps("üýþÿ")) == "üýþÿ" - - def test_str_long(self): - """ - str long - """ - for obj in ("aaaa" * 1024, "üýþÿ" * 1024, "好" * 1024, "�" * 1024): - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_str_2mib(self): - ref = '🐈🐈🐈🐈🐈"üýa0s9999🐈🐈🐈🐈🐈9\0999\\9999' * 1024 * 50 - assert orjson.loads(orjson.dumps(ref)) == ref - - def test_str_very_long(self): - """ - str long enough to trigger overflow in bytecount - """ - for obj in ("aaaa" * 20000, "üýþÿ" * 20000, "好" * 20000, "�" * 20000): - assert orjson.loads(orjson.dumps(obj)) == obj - - def test_str_replacement(self): - """ - str roundtrip � - """ - assert orjson.dumps("�") == b'"\xef\xbf\xbd"' - assert orjson.loads(b'"\xef\xbf\xbd"') == "�" - - def test_str_trailing_4_byte(self): - ref = "うぞ〜😏🙌" - assert orjson.loads(orjson.dumps(ref)) == ref - - def test_str_ascii_control(self): - """ - worst case format_escaped_str_with_escapes() allocation - """ - ref = "\x01\x1f" * 1024 * 16 - assert orjson.loads(orjson.dumps(ref)) == ref - assert orjson.loads(orjson.dumps(ref, option=orjson.OPT_INDENT_2)) == ref - - def test_str_escape_quote_0(self): - assert orjson.dumps('"aaaaaaabb') == b'"\\"aaaaaaabb"' - - def test_str_escape_quote_1(self): - assert orjson.dumps('a"aaaaaabb') == b'"a\\"aaaaaabb"' - - def test_str_escape_quote_2(self): - assert orjson.dumps('aa"aaaaabb') == b'"aa\\"aaaaabb"' - - def test_str_escape_quote_3(self): - assert orjson.dumps('aaa"aaaabb') == b'"aaa\\"aaaabb"' - - def test_str_escape_quote_4(self): - assert orjson.dumps('aaaa"aaabb') == b'"aaaa\\"aaabb"' - - def test_str_escape_quote_5(self): - assert orjson.dumps('aaaaa"aabb') == b'"aaaaa\\"aabb"' - - def test_str_escape_quote_6(self): - assert orjson.dumps('aaaaaa"abb') == b'"aaaaaa\\"abb"' - - def test_str_escape_quote_7(self): - assert orjson.dumps('aaaaaaa"bb') == b'"aaaaaaa\\"bb"' - - def test_str_escape_quote_8(self): - assert orjson.dumps('aaaaaaaab"') == b'"aaaaaaaab\\""' - - def test_str_escape_quote_multi(self): - assert ( - orjson.dumps('aa"aaaaabbbbbbbbbbbbbbbbbbbb"bb') - == b'"aa\\"aaaaabbbbbbbbbbbbbbbbbbbb\\"bb"' - ) - - def test_str_escape_quote_buffer(self): - orjson.dumps(['"' * 4096] * 1024) - - def test_str_escape_backslash_0(self): - assert orjson.dumps("\\aaaaaaabb") == b'"\\\\aaaaaaabb"' - - def test_str_escape_backslash_1(self): - assert orjson.dumps("a\\aaaaaabb") == b'"a\\\\aaaaaabb"' - - def test_str_escape_backslash_2(self): - assert orjson.dumps("aa\\aaaaabb") == b'"aa\\\\aaaaabb"' - - def test_str_escape_backslash_3(self): - assert orjson.dumps("aaa\\aaaabb") == b'"aaa\\\\aaaabb"' - - def test_str_escape_backslash_4(self): - assert orjson.dumps("aaaa\\aaabb") == b'"aaaa\\\\aaabb"' - - def test_str_escape_backslash_5(self): - assert orjson.dumps("aaaaa\\aabb") == b'"aaaaa\\\\aabb"' - - def test_str_escape_backslash_6(self): - assert orjson.dumps("aaaaaa\\abb") == b'"aaaaaa\\\\abb"' - - def test_str_escape_backslash_7(self): - assert orjson.dumps("aaaaaaa\\bb") == b'"aaaaaaa\\\\bb"' - - def test_str_escape_backslash_8(self): - assert orjson.dumps("aaaaaaaab\\") == b'"aaaaaaaab\\\\"' - - def test_str_escape_backslash_multi(self): - assert ( - orjson.dumps("aa\\aaaaabbbbbbbbbbbbbbbbbbbb\\bb") - == b'"aa\\\\aaaaabbbbbbbbbbbbbbbbbbbb\\\\bb"' - ) - - def test_str_escape_backslash_buffer(self): - orjson.dumps(["\\" * 4096] * 1024) - - def test_str_escape_x32_0(self): - assert orjson.dumps("\taaaaaaabb") == b'"\\taaaaaaabb"' - - def test_str_escape_x32_1(self): - assert orjson.dumps("a\taaaaaabb") == b'"a\\taaaaaabb"' - - def test_str_escape_x32_2(self): - assert orjson.dumps("aa\taaaaabb") == b'"aa\\taaaaabb"' - - def test_str_escape_x32_3(self): - assert orjson.dumps("aaa\taaaabb") == b'"aaa\\taaaabb"' - - def test_str_escape_x32_4(self): - assert orjson.dumps("aaaa\taaabb") == b'"aaaa\\taaabb"' - - def test_str_escape_x32_5(self): - assert orjson.dumps("aaaaa\taabb") == b'"aaaaa\\taabb"' - - def test_str_escape_x32_6(self): - assert orjson.dumps("aaaaaa\tabb") == b'"aaaaaa\\tabb"' - - def test_str_escape_x32_7(self): - assert orjson.dumps("aaaaaaa\tbb") == b'"aaaaaaa\\tbb"' - - def test_str_escape_x32_8(self): - assert orjson.dumps("aaaaaaaab\t") == b'"aaaaaaaab\\t"' - - def test_str_escape_x32_multi(self): - assert ( - orjson.dumps("aa\taaaaabbbbbbbbbbbbbbbbbbbb\tbb") - == b'"aa\\taaaaabbbbbbbbbbbbbbbbbbbb\\tbb"' - ) - - def test_str_escape_x32_buffer(self): - orjson.dumps(["\t" * 4096] * 1024) - - def test_str_emoji(self): - ref = "®️" - assert orjson.loads(orjson.dumps(ref)) == ref - - def test_str_emoji_escape(self): - ref = '/"®️/"' - assert orjson.loads(orjson.dumps(ref)) == ref - - def test_very_long_list(self): - orjson.dumps([[]] * 1024 * 16) - - def test_very_long_list_pretty(self): - orjson.dumps([[]] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_dict(self): - orjson.dumps([{}] * 1024 * 16) - - def test_very_long_dict_pretty(self): - orjson.dumps([{}] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_str_empty(self): - orjson.dumps([""] * 1024 * 16) - - def test_very_long_str_empty_pretty(self): - orjson.dumps([""] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_str_not_empty(self): - orjson.dumps(["a"] * 1024 * 16) - - def test_very_long_str_not_empty_pretty(self): - orjson.dumps(["a"] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_bool(self): - orjson.dumps([True] * 1024 * 16) - - def test_very_long_bool_pretty(self): - orjson.dumps([True] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_int(self): - orjson.dumps([(2**64) - 1] * 1024 * 16) - - def test_very_long_int_pretty(self): - orjson.dumps([(2**64) - 1] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_very_long_float(self): - orjson.dumps([sys.float_info.max] * 1024 * 16) - - def test_very_long_float_pretty(self): - orjson.dumps([sys.float_info.max] * 1024 * 16, option=orjson.OPT_INDENT_2) - - def test_str_surrogates_loads(self): - """ - str unicode surrogates loads() - """ - pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\ud800"') - pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\ud83d\ude80"') - pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\udcff"') - pytest.raises( - orjson.JSONDecodeError, orjson.loads, b'"\xed\xa0\xbd\xed\xba\x80"' - ) # \ud83d\ude80 - - def test_str_surrogates_dumps(self): - """ - str unicode surrogates dumps() - """ - pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\ud800") - pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\ud83d\ude80") - pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\udcff") - pytest.raises(orjson.JSONEncodeError, orjson.dumps, {"\ud83d\ude80": None}) - pytest.raises( - orjson.JSONEncodeError, orjson.dumps, b"\xed\xa0\xbd\xed\xba\x80" - ) # \ud83d\ude80 - - @pytest.mark.skipif( - xxhash is None, reason="xxhash install broken on win, python3.9, Azure" - ) - def test_str_ascii(self): - """ - str is ASCII but not compact - """ - digest = xxhash.xxh32_hexdigest("12345") - for _ in range(2): - assert orjson.dumps(digest) == b'"b30d56b4"' - - def test_bytes_dumps(self): - """ - bytes dumps not supported - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps([b"a"]) - - def test_bytes_loads(self): - """ - bytes loads - """ - assert orjson.loads(b"[]") == [] - - def test_bytearray_loads(self): - """ - bytearray loads - """ - arr = bytearray() - arr.extend(b"[]") - assert orjson.loads(arr) == [] - - def test_memoryview_loads(self): - """ - memoryview loads - """ - arr = bytearray() - arr.extend(b"[]") - assert orjson.loads(memoryview(arr)) == [] - - def test_bytesio_loads(self): - """ - memoryview loads - """ - arr = io.BytesIO(b"[]") - assert orjson.loads(arr.getbuffer()) == [] - - def test_bool(self): - """ - bool - """ - for obj, ref in ((True, "true"), (False, "false")): - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == obj - - def test_bool_true_array(self): - """ - bool true array - """ - obj = [True] * 256 - ref = ("[" + ("true," * 255) + "true]").encode("utf-8") - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - - def test_bool_false_array(self): - """ - bool false array - """ - obj = [False] * 256 - ref = ("[" + ("false," * 255) + "false]").encode("utf-8") - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - - def test_none(self): - """ - null - """ - obj = None - ref = "null" - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == obj - - def test_int(self): - """ - int compact and non-compact - """ - obj = [-5000, -1000, -10, -5, -2, -1, 0, 1, 2, 5, 10, 1000, 50000] - ref = b"[-5000,-1000,-10,-5,-2,-1,0,1,2,5,10,1000,50000]" - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - - def test_null_array(self): - """ - null array - """ - obj = [None] * 256 - ref = ("[" + ("null," * 255) + "null]").encode("utf-8") - assert orjson.dumps(obj) == ref - assert orjson.loads(ref) == obj - - def test_nan_dumps(self): - """ - NaN serializes to null - """ - assert orjson.dumps(float("NaN")) == b"null" - - def test_nan_loads(self): - """ - NaN is not valid JSON - """ - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[NaN]") - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[nan]") - - def test_infinity_dumps(self): - """ - Infinity serializes to null - """ - assert orjson.dumps(float("Infinity")) == b"null" - - def test_infinity_loads(self): - """ - Infinity, -Infinity is not valid JSON - """ - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[infinity]") - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[Infinity]") - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[-Infinity]") - with pytest.raises(orjson.JSONDecodeError): - orjson.loads("[-infinity]") - - def test_int_53(self): - """ - int 53-bit - """ - for val in (9007199254740991, -9007199254740991): - assert orjson.loads(str(val)) == val - assert orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) == str( - val - ).encode("utf-8") - - def test_int_53_exc(self): - """ - int 53-bit exception on 64-bit - """ - for val in (9007199254740992, -9007199254740992): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) - - def test_int_53_exc_usize(self): - """ - int 53-bit exception on 64-bit usize - """ - for val in (9223372036854775808, 18446744073709551615): - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) - - def test_int_53_exc_128(self): - """ - int 53-bit exception on 128-bit - """ - val = 2**65 - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) - - def test_int_64(self): - """ - int 64-bit - """ - for val in (9223372036854775807, -9223372036854775807): - assert orjson.loads(str(val)) == val - assert orjson.dumps(val) == str(val).encode("utf-8") - - def test_uint_64(self): - """ - uint 64-bit - """ - for val in (0, 9223372036854775808, 18446744073709551615): - assert orjson.loads(str(val)) == val - assert orjson.dumps(val) == str(val).encode("utf-8") - - def test_int_128(self): - """ - int 128-bit - """ - for val in (18446744073709551616, -9223372036854775809): - pytest.raises(orjson.JSONEncodeError, orjson.dumps, val) - - def test_float(self): - """ - float - """ - assert -1.1234567893 == orjson.loads("-1.1234567893") - assert -1.234567893 == orjson.loads("-1.234567893") - assert -1.34567893 == orjson.loads("-1.34567893") - assert -1.4567893 == orjson.loads("-1.4567893") - assert -1.567893 == orjson.loads("-1.567893") - assert -1.67893 == orjson.loads("-1.67893") - assert -1.7893 == orjson.loads("-1.7893") - assert -1.893 == orjson.loads("-1.893") - assert -1.3 == orjson.loads("-1.3") - - assert 1.1234567893 == orjson.loads("1.1234567893") - assert 1.234567893 == orjson.loads("1.234567893") - assert 1.34567893 == orjson.loads("1.34567893") - assert 1.4567893 == orjson.loads("1.4567893") - assert 1.567893 == orjson.loads("1.567893") - assert 1.67893 == orjson.loads("1.67893") - assert 1.7893 == orjson.loads("1.7893") - assert 1.893 == orjson.loads("1.893") - assert 1.3 == orjson.loads("1.3") - - def test_float_precision_loads(self): - """ - float precision loads() - """ - assert orjson.loads("31.245270191439438") == 31.245270191439438 - assert orjson.loads("-31.245270191439438") == -31.245270191439438 - assert orjson.loads("121.48791951161945") == 121.48791951161945 - assert orjson.loads("-121.48791951161945") == -121.48791951161945 - assert orjson.loads("100.78399658203125") == 100.78399658203125 - assert orjson.loads("-100.78399658203125") == -100.78399658203125 - - def test_float_precision_dumps(self): - """ - float precision dumps() - """ - assert orjson.dumps(31.245270191439438) == b"31.245270191439438" - assert orjson.dumps(-31.245270191439438) == b"-31.245270191439438" - assert orjson.dumps(121.48791951161945) == b"121.48791951161945" - assert orjson.dumps(-121.48791951161945) == b"-121.48791951161945" - assert orjson.dumps(100.78399658203125) == b"100.78399658203125" - assert orjson.dumps(-100.78399658203125) == b"-100.78399658203125" - - def test_float_edge(self): - """ - float edge cases - """ - assert orjson.dumps(0.8701) == b"0.8701" - - assert orjson.loads("0.8701") == 0.8701 - assert ( - orjson.loads("0.0000000000000000000000000000000000000000000000000123e50") - == 1.23 - ) - assert orjson.loads("0.4e5") == 40000.0 - assert orjson.loads("0.00e-00") == 0.0 - assert orjson.loads("0.4e-001") == 0.04 - assert orjson.loads("0.123456789e-12") == 1.23456789e-13 - assert orjson.loads("1.234567890E+34") == 1.23456789e34 - assert orjson.loads("23456789012E66") == 2.3456789012e76 - - def test_float_notation(self): - """ - float notation - """ - for val in ("1.337E40", "1.337e+40", "1337e40", "1.337E-4"): - obj = orjson.loads(val) - assert obj == float(val) - assert orjson.dumps(val) == ('"%s"' % val).encode("utf-8") - - def test_list(self): - """ - list - """ - obj = ["a", "😊", True, {"b": 1.1}, 2] - ref = '["a","😊",true,{"b":1.1},2]' - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == obj - - def test_tuple(self): - """ - tuple - """ - obj = ("a", "😊", True, {"b": 1.1}, 2) - ref = '["a","😊",true,{"b":1.1},2]' - assert orjson.dumps(obj) == ref.encode("utf-8") - assert orjson.loads(ref) == list(obj) - - def test_object(self): - """ - object() dumps() - """ - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(object()) diff --git a/vendor/orjson/test/test_typeddict.py b/vendor/orjson/test/test_typeddict.py deleted file mode 100644 index 04f0224309..0000000000 --- a/vendor/orjson/test/test_typeddict.py +++ /dev/null @@ -1,22 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import orjson - -try: - from typing import TypedDict # type: ignore -except ImportError: - from typing_extensions import TypedDict - - -class TestTypedDict: - def test_typeddict(self): - """ - dumps() TypedDict - """ - - class TypedDict1(TypedDict): - a: str - b: int - - obj = TypedDict1(a="a", b=1) - assert orjson.dumps(obj) == b'{"a":"a","b":1}' diff --git a/vendor/orjson/test/test_ujson.py b/vendor/orjson/test/test_ujson.py deleted file mode 100644 index 4fdda0c7ab..0000000000 --- a/vendor/orjson/test/test_ujson.py +++ /dev/null @@ -1,346 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - - -import json - -import pytest - -import orjson - - -class TestUltraJSON: - def test_doubleLongIssue(self): - sut = {"a": -4342969734183514} - encoded = orjson.dumps(sut) - decoded = orjson.loads(encoded) - assert sut == decoded - encoded = orjson.dumps(sut) - decoded = orjson.loads(encoded) - assert sut == decoded - - def test_doubleLongDecimalIssue(self): - sut = {"a": -12345678901234.56789012} - encoded = orjson.dumps(sut) - decoded = orjson.loads(encoded) - assert sut == decoded - encoded = orjson.dumps(sut) - decoded = orjson.loads(encoded) - assert sut == decoded - - def test_encodeDecodeLongDecimal(self): - sut = {"a": -528656961.4399388} - encoded = orjson.dumps(sut) - orjson.loads(encoded) - - def test_decimalDecodeTest(self): - sut = {"a": 4.56} - encoded = orjson.dumps(sut) - decoded = orjson.loads(encoded) - pytest.approx(sut["a"], decoded["a"]) - - def test_encodeDictWithUnicodeKeys(self): - val = { - "key1": "value1", - "key1": "value1", - "key1": "value1", - "key1": "value1", - "key1": "value1", - "key1": "value1", - } - orjson.dumps(val) - - val = { - "بن": "value1", - "بن": "value1", - "بن": "value1", - "بن": "value1", - "بن": "value1", - "بن": "value1", - "بن": "value1", - } - orjson.dumps(val) - - def test_encodeArrayOfNestedArrays(self): - val = [[[[]]]] * 20 # type: ignore - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert val == orjson.loads(output) - - def test_encodeArrayOfDoubles(self): - val = [31337.31337, 31337.31337, 31337.31337, 31337.31337] * 10 - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert val == orjson.loads(output) - - def test_encodeStringConversion2(self): - val = "A string \\ / \b \f \n \r \t" - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == b'"A string \\\\ / \\b \\f \\n \\r \\t"' - assert val == orjson.loads(output) - - def test_decodeUnicodeConversion(self): - pass - - def test_encodeUnicodeConversion1(self): - val = "Räksmörgås اسامة بن محمد بن عوض بن لادن" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def test_encodeControlEscaping(self): - val = "\x19" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - assert val == dec - assert enc == orjson.dumps(val) - - def test_encodeUnicodeConversion2(self): - val = "\xe6\x97\xa5\xd1\x88" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def test_encodeUnicodeSurrogatePair(self): - val = "\xf0\x90\x8d\x86" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def test_encodeUnicode4BytesUTF8(self): - val = "\xf0\x91\x80\xb0TRAILINGNORMAL" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def test_encodeUnicode4BytesUTF8Highest(self): - val = "\xf3\xbf\xbf\xbfTRAILINGNORMAL" - enc = orjson.dumps(val) - dec = orjson.loads(enc) - - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def testEncodeUnicodeBMP(self): - s = "\U0001f42e\U0001f42e\U0001f42d\U0001f42d" # 🐮🐮🐭🐭 - orjson.dumps(s) - json.dumps(s) - - assert json.loads(json.dumps(s)) == s - assert orjson.loads(orjson.dumps(s)) == s - - def testEncodeSymbols(self): - s = "\u273f\u2661\u273f" # ✿♡✿ - encoded = orjson.dumps(s) - encoded_json = json.dumps(s) - - decoded = orjson.loads(encoded) - assert s == decoded - - encoded = orjson.dumps(s) - - # json outputs an unicode object - encoded_json = json.dumps(s, ensure_ascii=False) - assert encoded == encoded_json.encode("utf-8") - decoded = orjson.loads(encoded) - assert s == decoded - - def test_encodeArrayInArray(self): - val = [[[[]]]] # type: ignore - output = orjson.dumps(val) - - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeIntConversion(self): - val = 31337 - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeIntNegConversion(self): - val = -31337 - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeLongNegConversion(self): - val = -9223372036854775808 - output = orjson.dumps(val) - - orjson.loads(output) - orjson.loads(output) - - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeListConversion(self): - val = [1, 2, 3, 4] - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert val == orjson.loads(output) - - def test_encodeDictConversion(self): - val = {"k1": 1, "k2": 2, "k3": 3, "k4": 4} - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert val == orjson.loads(output) - assert val == orjson.loads(output) - - def test_encodeNoneConversion(self): - val = None - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeTrueConversion(self): - val = True - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeFalseConversion(self): - val = False - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - def test_encodeToUTF8(self): - val = b"\xe6\x97\xa5\xd1\x88".decode("utf-8") - enc = orjson.dumps(val) - dec = orjson.loads(enc) - assert enc == orjson.dumps(val) - assert dec == orjson.loads(enc) - - def test_decodeFromUnicode(self): - val = '{"obj": 31337}' - dec1 = orjson.loads(val) - dec2 = orjson.loads(str(val)) - assert dec1 == dec2 - - def test_decodeJibberish(self): - val = "fdsa sda v9sa fdsa" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenArrayStart(self): - val = "[" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenObjectStart(self): - val = "{" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenArrayEnd(self): - val = "]" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenObjectEnd(self): - val = "}" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeObjectDepthTooBig(self): - val = "{" * (1024 * 1024) - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeStringUnterminated(self): - val = '"TESTING' - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeStringUntermEscapeSequence(self): - val = '"TESTING\\"' - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeStringBadEscape(self): - val = '"TESTING\\"' - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeTrueBroken(self): - val = "tru" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeFalseBroken(self): - val = "fa" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeNullBroken(self): - val = "n" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenDictKeyTypeLeakTest(self): - val = '{{1337:""}}' - for _ in range(1000): - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenDictLeakTest(self): - val = '{{"key":"}' - for _ in range(1000): - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeBrokenListLeakTest(self): - val = "[[[true" - for _ in range(1000): - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeDictWithNoKey(self): - val = "{{{{31337}}}}" - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeDictWithNoColonOrValue(self): - val = '{{{{"key"}}}}' - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeDictWithNoValue(self): - val = '{{{{"key":}}}}' - pytest.raises(orjson.JSONDecodeError, orjson.loads, val) - - def test_decodeNumericIntPos(self): - val = "31337" - assert 31337 == orjson.loads(val) - - def test_decodeNumericIntNeg(self): - assert -31337 == orjson.loads("-31337") - - def test_encodeNullCharacter(self): - val = "31337 \x00 1337" - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - val = "\x00" - output = orjson.dumps(val) - assert val == orjson.loads(output) - assert output == orjson.dumps(val) - assert val == orjson.loads(output) - - assert b'" \\u0000\\r\\n "' == orjson.dumps(" \u0000\r\n ") - - def test_decodeNullCharacter(self): - val = '"31337 \\u0000 31337"' - assert orjson.loads(val) == json.loads(val) - - def test_decodeEscape(self): - base = "\u00e5".encode() - quote = b'"' - val = quote + base + quote - assert json.loads(val) == orjson.loads(val) - - def test_decodeBigEscape(self): - for _ in range(10): - base = "\u00e5".encode() - quote = b'"' - val = quote + (base * 1024 * 1024 * 2) + quote - assert json.loads(val) == orjson.loads(val) diff --git a/vendor/orjson/test/test_uuid.py b/vendor/orjson/test/test_uuid.py deleted file mode 100644 index d916ac4821..0000000000 --- a/vendor/orjson/test/test_uuid.py +++ /dev/null @@ -1,102 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import uuid - -import pytest - -import orjson - - -class TestUUID: - def test_uuid_immutable(self): - """ - UUID objects are immutable - """ - val = uuid.uuid4() - with pytest.raises(TypeError): - val.int = 1 # type: ignore - with pytest.raises(TypeError): - val.int = None # type: ignore - - def test_uuid_int(self): - """ - UUID.int is a 128-bit integer - """ - val = uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece") - assert isinstance(val.int, int) - assert val.int >= 2**64 - assert val.int < 2**128 - assert val.int, 151546616840194781678008611711208857294 - - def test_uuid_overflow(self): - """ - UUID.int can't trigger errors in _PyLong_AsByteArray - """ - with pytest.raises(ValueError): - uuid.UUID(int=2**128) - with pytest.raises(ValueError): - uuid.UUID(int=-1) - - def test_uuid_subclass(self): - """ - UUID subclasses are not serialized - """ - - class AUUID(uuid.UUID): - pass - - with pytest.raises(orjson.JSONEncodeError): - orjson.dumps(AUUID("{12345678-1234-5678-1234-567812345678}")) - - def test_serializes_withopt(self): - """ - dumps() accepts deprecated OPT_SERIALIZE_UUID - """ - assert ( - orjson.dumps( - uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"), - option=orjson.OPT_SERIALIZE_UUID, - ) - == b'"7202d115-7ff3-4c81-a7c1-2a1f067b1ece"' - ) - - def test_nil_uuid(self): - assert ( - orjson.dumps(uuid.UUID("00000000-0000-0000-0000-000000000000")) - == b'"00000000-0000-0000-0000-000000000000"' - ) - - def test_all_ways_to_create_uuid_behave_equivalently(self): - # Note that according to the docstring for the uuid.UUID class, all the - # forms below are equivalent -- they end up with the same value for - # `self.int`, which is all that really matters - uuids = [ - uuid.UUID("{12345678-1234-5678-1234-567812345678}"), - uuid.UUID("12345678123456781234567812345678"), - uuid.UUID("urn:uuid:12345678-1234-5678-1234-567812345678"), - uuid.UUID(bytes=b"\x12\x34\x56\x78" * 4), - uuid.UUID( - bytes_le=b"\x78\x56\x34\x12\x34\x12\x78\x56" - + b"\x12\x34\x56\x78\x12\x34\x56\x78" - ), - uuid.UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)), - uuid.UUID(int=0x12345678123456781234567812345678), - ] - result = orjson.dumps(uuids) - canonical_uuids = ['"%s"' % str(u) for u in uuids] - serialized = ("[%s]" % ",".join(canonical_uuids)).encode("utf8") - assert result == serialized - - def test_serializes_correctly_with_leading_zeroes(self): - instance = uuid.UUID(int=0x00345678123456781234567812345678) - assert orjson.dumps(instance) == ('"%s"' % str(instance)).encode("utf8") - - def test_all_uuid_creation_functions_create_serializable_uuids(self): - uuids = ( - uuid.uuid1(), - uuid.uuid3(uuid.NAMESPACE_DNS, "python.org"), - uuid.uuid4(), - uuid.uuid5(uuid.NAMESPACE_DNS, "python.org"), - ) - for val in uuids: - assert orjson.dumps(val) == f'"{val}"'.encode("utf-8") diff --git a/vendor/orjson/test/util.py b/vendor/orjson/test/util.py deleted file mode 100644 index bc00b93431..0000000000 --- a/vendor/orjson/test/util.py +++ /dev/null @@ -1,38 +0,0 @@ -# SPDX-License-Identifier: (Apache-2.0 OR MIT) - -import lzma -import os -from pathlib import Path -from typing import Any, Dict - -import orjson - -dirname = os.path.join(os.path.dirname(__file__), "../data") - -STR_CACHE: Dict[str, str] = {} - -OBJ_CACHE: Dict[str, Any] = {} - - -def read_fixture_bytes(filename, subdir=None): - if subdir is None: - path = Path(dirname, filename) - else: - path = Path(dirname, subdir, filename) - if path.suffix == ".xz": - contents = lzma.decompress(path.read_bytes()) - else: - contents = path.read_bytes() - return contents - - -def read_fixture_str(filename, subdir=None): - if filename not in STR_CACHE: - STR_CACHE[filename] = read_fixture_bytes(filename, subdir).decode("utf-8") - return STR_CACHE[filename] - - -def read_fixture_obj(filename): - if filename not in OBJ_CACHE: - OBJ_CACHE[filename] = orjson.loads(read_fixture_str(filename)) - return OBJ_CACHE[filename] diff --git a/vendor/pyo3-info.txt b/vendor/pyo3-info.txt deleted file mode 100644 index 5c64718361..0000000000 --- a/vendor/pyo3-info.txt +++ /dev/null @@ -1,35 +0,0 @@ -cloned from commit f2a8460559d8f58269f306679fc58c9d101f70cf (tag: v0.23.4) - -then apply diff: - -diff --git a/vendor/pyo3/pyo3-ffi/src/modsupport.rs b/vendor/pyo3/pyo3-ffi/src/modsupport.rs -index 4a18d30f..6da2795b 100644 ---- a/vendor/pyo3/pyo3-ffi/src/modsupport.rs -+++ b/vendor/pyo3/pyo3-ffi/src/modsupport.rs -@@ -36,6 +36,13 @@ extern "C" { - pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject; - // skipped Py_VaBuildValue - -+ #[cfg(Py_3_13)] -+ pub fn PyModule_Add( -+ module: *mut PyObject, -+ name: *const c_char, -+ value: *mut PyObject, -+ ) -> core::ffi::c_int; -+ - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyModule_AddObjectRef")] - pub fn PyModule_AddObjectRef( -diff --git a/vendor/pyo3/pyo3-ffi/src/object.rs b/vendor/pyo3/pyo3-ffi/src/object.rs -index 3f086ac1..51083a8e 100644 ---- a/vendor/pyo3/pyo3-ffi/src/object.rs -+++ b/vendor/pyo3/pyo3-ffi/src/object.rs -@@ -214,7 +214,7 @@ pub unsafe fn Py_SIZE(ob: *mut PyObject) -> Py_ssize_t { - - #[inline(always)] - #[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] --unsafe fn _Py_IsImmortal(op: *mut PyObject) -> c_int { -+pub unsafe fn _Py_IsImmortal(op: *mut PyObject) -> c_int { - #[cfg(target_pointer_width = "64")] - { - (((*op).ob_refcnt.ob_refcnt as crate::PY_INT32_T) < 0) as c_int diff --git a/vendor/pyo3/.github/ISSUE_TEMPLATE/blank_issue.md b/vendor/pyo3/.github/ISSUE_TEMPLATE/blank_issue.md deleted file mode 100644 index 039c2da5f7..0000000000 --- a/vendor/pyo3/.github/ISSUE_TEMPLATE/blank_issue.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -name: 📝 Blank Issue -about: Create a blank issue. ---- \ No newline at end of file diff --git a/vendor/pyo3/.github/ISSUE_TEMPLATE/bug_report.yml b/vendor/pyo3/.github/ISSUE_TEMPLATE/bug_report.yml deleted file mode 100644 index 40fb4d125e..0000000000 --- a/vendor/pyo3/.github/ISSUE_TEMPLATE/bug_report.yml +++ /dev/null @@ -1,81 +0,0 @@ -name: 🐛 Bug Report -description: Create a bug report -labels: [bug] -body: - - type: markdown - attributes: - value: | - Thank you for taking the time to fill out this bug report! - Please fill out the form below... - - type: textarea - id: description - attributes: - label: Bug Description - description: Please provide a clear and concise description of what the bug is. - placeholder: The bug is... - validations: - required: true - - type: textarea - id: reproduce - attributes: - label: Steps to Reproduce - description: Provide steps to reproduce this bug. - placeholder: | - 1. - 2. - 3. - validations: - required: true - - type: textarea - id: debug - attributes: - label: Backtrace - description: If your bug produces a backtrace, please include it here. - render: shell - - - type: input - id: os_version - attributes: - label: Your operating system and version - validations: - required: true - - type: input - id: py_version - attributes: - label: Your Python version (`python --version`) - placeholder: ex. Python 3.10.0 - validations: - required: true - - type: input - id: rust_version - attributes: - label: Your Rust version (`rustc --version`) - placeholder: ex. rustc 1.55.0 (c8dfcfe04 2021-09-06) - validations: - required: true - - type: input - id: pyo3_version - attributes: - label: Your PyO3 version - placeholder: ex. 0.14.0 - validations: - required: true - - type: textarea - id: install_method - attributes: - label: How did you install python? Did you use a virtualenv? - placeholder: | - apt - pyenv - pacman - brew - python.org installer - microsoft store - etc... - validations: - required: true - - type: textarea - id: additional-info - attributes: - label: Additional Info - description: Any additional info that you think might be useful or relevant to this bug \ No newline at end of file diff --git a/vendor/pyo3/.github/ISSUE_TEMPLATE/config.yml b/vendor/pyo3/.github/ISSUE_TEMPLATE/config.yml deleted file mode 100644 index 7919c91f0c..0000000000 --- a/vendor/pyo3/.github/ISSUE_TEMPLATE/config.yml +++ /dev/null @@ -1,11 +0,0 @@ -blank_issues_enabled: true -contact_links: - - name: ❓ Question - url: https://github.com/PyO3/pyo3/discussions - about: Ask and answer questions about PyO3 on Discussions - - name: 🔧 Troubleshooting - url: https://github.com/PyO3/pyo3/discussions - about: For troubleshooting help, see the Discussions - - name: 👋 Chat - url: https://discord.gg/33kcChzH7f - about: Engage with PyO3's users and developers on Discord diff --git a/vendor/pyo3/.github/ISSUE_TEMPLATE/feature_request.md b/vendor/pyo3/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index 3cac000bc4..0000000000 --- a/vendor/pyo3/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -name: 💡 Feature request -about: Suggest an idea for this project -labels: [enhancement] ---- - - \ No newline at end of file diff --git a/vendor/pyo3/.github/dependabot.yml b/vendor/pyo3/.github/dependabot.yml deleted file mode 100644 index f8a257e28f..0000000000 --- a/vendor/pyo3/.github/dependabot.yml +++ /dev/null @@ -1,32 +0,0 @@ -# To get started with Dependabot version updates, you'll need to specify which -# package ecosystems to update and where the package manifests are located. -# Please see the documentation for all configuration options: -# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates - -version: 2 -updates: - - package-ecosystem: "cargo" - directory: "/" - schedule: - interval: "weekly" - - - package-ecosystem: "cargo" - directory: "/pyo3-benches/" - schedule: - interval: "weekly" - - - package-ecosystem: "cargo" - directory: "/pyo3-ffi-check/" - schedule: - interval: "weekly" - - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "weekly" - labels: - # dependabot default labels - - "dependencies" - - "github-actions" - # additional labels - - "CI-skip-changelog" diff --git a/vendor/pyo3/.github/pull_request_template.md b/vendor/pyo3/.github/pull_request_template.md deleted file mode 100644 index 11375e966b..0000000000 --- a/vendor/pyo3/.github/pull_request_template.md +++ /dev/null @@ -1,13 +0,0 @@ -Thank you for contributing to PyO3! - -By submitting these contributions you agree for them to be dual-licensed under PyO3's [MIT OR Apache-2.0 license](https://github.com/PyO3/pyo3#license). - -Please consider adding the following to your pull request: - - an entry for this PR in newsfragments - see [https://pyo3.rs/main/contributing.html#documenting-changes] - - or start the PR title with `docs:` if this is a docs-only change to skip the check - - docs to all new functions and / or detail in the guide - - tests for all new or changed functions - -PyO3's CI pipeline will check your pull request, thus make sure you have checked the `Contributing.md` guidelines. To run most of its tests -locally, you can run ```nox```. See ```nox --list-sessions``` -for a list of supported actions. diff --git a/vendor/pyo3/.github/workflows/benches.yml b/vendor/pyo3/.github/workflows/benches.yml deleted file mode 100644 index 97b882dc85..0000000000 --- a/vendor/pyo3/.github/workflows/benches.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: benches - -on: - push: - branches: - - "main" - pull_request: - # `workflow_dispatch` allows CodSpeed to trigger backtest - # performance analysis in order to generate initial data. - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.event.pull_request.number || github.sha }}-benches - cancel-in-progress: true - -jobs: - benchmarks: - # No support for 24.04, see https://github.com/CodSpeedHQ/runner/issues/42 - runs-on: ubuntu-22.04 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - uses: dtolnay/rust-toolchain@stable - with: - components: rust-src - - - uses: Swatinem/rust-cache@v2 - with: - workspaces: | - . - pyo3-benches - continue-on-error: true - - - uses: taiki-e/install-action@v2 - with: - tool: cargo-codspeed - - - name: Install nox - run: pip install nox - - - name: Run the benchmarks - uses: CodSpeedHQ/action@v3 - with: - run: nox -s codspeed - token: ${{ secrets.CODSPEED_TOKEN }} diff --git a/vendor/pyo3/.github/workflows/build.yml b/vendor/pyo3/.github/workflows/build.yml deleted file mode 100644 index ed24957ad2..0000000000 --- a/vendor/pyo3/.github/workflows/build.yml +++ /dev/null @@ -1,156 +0,0 @@ -on: - workflow_call: - inputs: - os: - required: true - type: string - python-version: - required: true - type: string - python-architecture: - required: true - type: string - rust: - required: true - type: string - rust-target: - required: true - type: string - MSRV: - required: true - type: string - -jobs: - build: - continue-on-error: ${{ endsWith(inputs.python-version, '-dev') || contains(fromJSON('["3.7", "3.8"]'), inputs.python-version) || contains(fromJSON('["beta", "nightly"]'), inputs.rust) }} - runs-on: ${{ inputs.os }} - if: ${{ !(startsWith(inputs.python-version, 'graalpy') && startsWith(inputs.os, 'windows')) }} - steps: - - uses: actions/checkout@v4 - - - name: Set up Python ${{ inputs.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ inputs.python-version }} - architecture: ${{ inputs.python-architecture }} - check-latest: ${{ startsWith(inputs.python-version, 'pypy') }} # PyPy can have FFI changes within Python versions, which creates pain in CI - - - name: Install nox - run: python -m pip install --upgrade pip && pip install nox - - - name: Install Rust toolchain - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ inputs.rust }} - targets: ${{ inputs.rust-target }} - # needed to correctly format errors, see #1865 - components: rust-src - - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - - if: inputs.os == 'ubuntu-latest' - name: Prepare LD_LIBRARY_PATH (Ubuntu only) - run: echo LD_LIBRARY_PATH=${pythonLocation}/lib >> $GITHUB_ENV - - - if: inputs.rust == inputs.MSRV - name: Prepare MSRV package versions - run: nox -s set-msrv-package-versions - - - if: inputs.rust != 'stable' - name: Ignore changed error messages when using trybuild - run: echo "TRYBUILD=overwrite" >> "$GITHUB_ENV" - - - if: inputs.rust == 'nightly' - name: Prepare to test on nightly rust - run: echo "MAYBE_NIGHTLY=nightly" >> "$GITHUB_ENV" - - - name: Build docs - run: nox -s docs - - - name: Build (no features) - if: ${{ !startsWith(inputs.python-version, 'graalpy') }} - run: cargo build --lib --tests --no-default-features - - # --no-default-features when used with `cargo build/test -p` doesn't seem to work! - - name: Build pyo3-build-config (no features) - run: | - cd pyo3-build-config - cargo build --no-default-features - - # Run tests (except on PyPy, because no embedding API). - - if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }} - name: Test (no features) - run: cargo test --no-default-features --lib --tests - - # --no-default-features when used with `cargo build/test -p` doesn't seem to work! - - name: Test pyo3-build-config (no features) - run: | - cd pyo3-build-config - cargo test --no-default-features - - - name: Build (all additive features) - if: ${{ !startsWith(inputs.python-version, 'graalpy') }} - run: cargo build --lib --tests --no-default-features --features "multiple-pymethods full $MAYBE_NIGHTLY" - - - if: ${{ startsWith(inputs.python-version, 'pypy') }} - name: Build PyPy (abi3-py39) - run: cargo build --lib --tests --no-default-features --features "multiple-pymethods abi3-py39 full $MAYBE_NIGHTLY" - - # Run tests (except on PyPy, because no embedding API). - - if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }} - name: Test - run: cargo test --no-default-features --features "full $MAYBE_NIGHTLY" - - # Repeat, with multiple-pymethods feature enabled (it's not entirely additive) - - if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }} - name: Test - run: cargo test --no-default-features --features "multiple-pymethods full $MAYBE_NIGHTLY" - - # Run tests again, but in abi3 mode - - if: ${{ !startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy') }} - name: Test (abi3) - run: cargo test --no-default-features --features "multiple-pymethods abi3 full $MAYBE_NIGHTLY" - - # Run tests again, for abi3-py37 (the minimal Python version) - - if: ${{ (!startsWith(inputs.python-version, 'pypy') && !startsWith(inputs.python-version, 'graalpy')) && (inputs.python-version != '3.7') }} - name: Test (abi3-py37) - run: cargo test --no-default-features --features "multiple-pymethods abi3-py37 full $MAYBE_NIGHTLY" - - - name: Test proc-macro code - run: cargo test --manifest-path=pyo3-macros-backend/Cargo.toml - - - name: Test build config - run: cargo test --manifest-path=pyo3-build-config/Cargo.toml - - - name: Test python examples and tests - shell: bash - run: nox -s test-py - env: - CARGO_TARGET_DIR: ${{ github.workspace }}/target - - - uses: dorny/paths-filter@v3 - if: ${{ inputs.rust == 'stable' && !startsWith(inputs.python-version, 'graalpy') }} - id: ffi-changes - with: - base: ${{ github.event.pull_request.base.ref || github.event.merge_group.base_ref }} - ref: ${{ github.event.pull_request.head.ref || github.event.merge_group.head_ref }} - filters: | - changed: - - 'pyo3-ffi/**' - - 'pyo3-ffi-check/**' - - '.github/workflows/ci.yml' - - '.github/workflows/build.yml' - - - name: Run pyo3-ffi-check - # pypy 3.9 on windows is not PEP 3123 compliant, nor is graalpy - if: ${{ endsWith(inputs.python-version, '-dev') || (steps.ffi-changes.outputs.changed == 'true' && inputs.rust == 'stable' && !startsWith(inputs.python-version, 'graalpy') && !(inputs.python-version == 'pypy3.9' && contains(inputs.os, 'windows'))) }} - run: nox -s ffi-check - - env: - CARGO_TERM_VERBOSE: true - CARGO_BUILD_TARGET: ${{ inputs.rust-target }} - RUST_BACKTRACE: 1 - RUSTFLAGS: "-D warnings" - RUSTDOCFLAGS: "-D warnings" diff --git a/vendor/pyo3/.github/workflows/cache-cleanup.yml b/vendor/pyo3/.github/workflows/cache-cleanup.yml deleted file mode 100644 index 02e8a6ab3c..0000000000 --- a/vendor/pyo3/.github/workflows/cache-cleanup.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: CI Cache Cleanup -on: - pull_request_target: - types: - - closed - -jobs: - cleanup: - runs-on: ubuntu-latest - permissions: - actions: write - steps: - - name: Cleanup - run: | - gh extension install actions/gh-actions-cache - - echo "Fetching list of cache key" - cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 ) - - ## Setting this to not fail the workflow while deleting cache keys. - set +e - echo "Deleting caches..." - for cacheKey in $cacheKeysForPR - do - gh actions-cache delete -R $REPO -B $BRANCH --confirm -- $cacheKey - done - echo "Done" - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPO: ${{ github.repository }} - BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge diff --git a/vendor/pyo3/.github/workflows/changelog.yml b/vendor/pyo3/.github/workflows/changelog.yml deleted file mode 100644 index b4c3b9892f..0000000000 --- a/vendor/pyo3/.github/workflows/changelog.yml +++ /dev/null @@ -1,17 +0,0 @@ -name: changelog - -on: - pull_request: - types: [opened, synchronize, labeled, unlabeled, edited] - -jobs: - check: - name: Check changelog entry - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - run: python -m pip install --upgrade pip && pip install nox - - run: nox -s check-changelog diff --git a/vendor/pyo3/.github/workflows/ci.yml b/vendor/pyo3/.github/workflows/ci.yml deleted file mode 100644 index 76c5ab06f2..0000000000 --- a/vendor/pyo3/.github/workflows/ci.yml +++ /dev/null @@ -1,761 +0,0 @@ -name: CI - -on: - push: - branches: - - main - pull_request: - merge_group: - types: [checks_requested] - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }} - cancel-in-progress: true - -env: - CARGO_TERM_COLOR: always - -jobs: - fmt: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - run: python -m pip install --upgrade pip && pip install nox - - uses: dtolnay/rust-toolchain@stable - with: - components: rustfmt - - name: Check python formatting and lints (ruff) - run: nox -s ruff - - name: Check rust formatting (rustfmt) - run: nox -s rustfmt - - resolve: - runs-on: ubuntu-latest - outputs: - MSRV: ${{ steps.resolve-msrv.outputs.MSRV }} - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: resolve MSRV - id: resolve-msrv - run: echo MSRV=`python -c 'import tomllib; print(tomllib.load(open("Cargo.toml", "rb"))["package"]["rust-version"])'` >> $GITHUB_OUTPUT - - semver-checks: - if: github.ref != 'refs/heads/main' - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: obi1kenobi/cargo-semver-checks-action@v2 - - check-msrv: - needs: [fmt, resolve] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ needs.resolve.outputs.MSRV }} - components: rust-src - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - run: python -m pip install --upgrade pip && pip install nox - # This is a smoke test to confirm that CI will run on MSRV (including dev dependencies) - - name: Check with MSRV package versions - run: | - nox -s set-msrv-package-versions - nox -s check-all - - env: - CARGO_BUILD_TARGET: x86_64-unknown-linux-gnu - - clippy: - needs: [fmt] - runs-on: ${{ matrix.platform.os }} - strategy: - # If one platform fails, allow the rest to keep testing if `CI-no-fail-fast` label is present - fail-fast: ${{ !contains(github.event.pull_request.labels.*.name, 'CI-no-fail-fast') }} - matrix: - rust: [stable] - platform: - [ - { - os: "macos-latest", - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "powerpc64le-unknown-linux-gnu", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "s390x-unknown-linux-gnu", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "wasm32-wasip1", - }, - { - os: "windows-latest", - python-architecture: "x64", - rust-target: "x86_64-pc-windows-msvc", - }, - { - os: "windows-latest", - python-architecture: "x86", - rust-target: "i686-pc-windows-msvc", - }, - ] - include: - # Run beta clippy as a way to detect any incoming lints which may affect downstream users - - rust: beta - platform: - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - name: clippy/${{ matrix.platform.rust-target }}/${{ matrix.rust }} - continue-on-error: ${{ matrix.rust != 'stable' }} - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ matrix.rust }} - targets: ${{ matrix.platform.rust-target }} - components: clippy,rust-src - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - architecture: ${{ matrix.platform.python-architecture }} - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - run: python -m pip install --upgrade pip && pip install nox - - run: nox -s clippy-all - env: - CARGO_BUILD_TARGET: ${{ matrix.platform.rust-target }} - - build-pr: - if: ${{ !contains(github.event.pull_request.labels.*.name, 'CI-build-full') && github.event_name == 'pull_request' }} - name: python${{ matrix.python-version }}-${{ matrix.platform.python-architecture }} ${{ matrix.platform.os }} rust-${{ matrix.rust }} - needs: [fmt, resolve] - uses: ./.github/workflows/build.yml - with: - os: ${{ matrix.platform.os }} - python-version: ${{ matrix.python-version }} - python-architecture: ${{ matrix.platform.python-architecture }} - rust: ${{ matrix.rust }} - rust-target: ${{ matrix.platform.rust-target }} - MSRV: ${{ needs.resolve.outputs.MSRV }} - secrets: inherit - strategy: - # If one platform fails, allow the rest to keep testing if `CI-no-fail-fast` label is present - fail-fast: ${{ !contains(github.event.pull_request.labels.*.name, 'CI-no-fail-fast') }} - matrix: - rust: [stable] - python-version: ["3.12"] - platform: [ - { - os: "macos-latest", # first available arm macos runner - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - }, - { - os: "macos-13", # last available x86_64 macos runner - python-architecture: "x64", - rust-target: "x86_64-apple-darwin", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - }, - { - os: "windows-latest", - python-architecture: "x64", - rust-target: "x86_64-pc-windows-msvc", - }, - { - os: "windows-latest", - python-architecture: "x86", - rust-target: "i686-pc-windows-msvc", - }, - ] - include: - # Test nightly Rust on PRs so that PR authors have a chance to fix nightly - # failures, as nightly does not block merge. - - rust: nightly - python-version: "3.12" - platform: - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - build-full: - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - name: python${{ matrix.python-version }}-${{ matrix.platform.python-architecture }} ${{ matrix.platform.os }} rust-${{ matrix.rust }} - needs: [fmt, resolve] - uses: ./.github/workflows/build.yml - with: - os: ${{ matrix.platform.os }} - python-version: ${{ matrix.python-version }} - python-architecture: ${{ matrix.platform.python-architecture }} - rust: ${{ matrix.rust }} - rust-target: ${{ matrix.platform.rust-target }} - MSRV: ${{ needs.resolve.outputs.MSRV }} - secrets: inherit - strategy: - # If one platform fails, allow the rest to keep testing if `CI-no-fail-fast` label is present - fail-fast: ${{ !contains(github.event.pull_request.labels.*.name, 'CI-no-fail-fast') }} - matrix: - rust: [stable] - python-version: - [ - "3.7", - "3.8", - "3.9", - "3.10", - "3.11", - "3.12", - "3.13", - "pypy3.9", - "pypy3.10", - "graalpy24.0", - ] - platform: - [ - { - os: "macos-latest", - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - }, - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - }, - { - os: "windows-latest", - python-architecture: "x64", - rust-target: "x86_64-pc-windows-msvc", - }, - ] - include: - # Test minimal supported Rust version - - rust: ${{ needs.resolve.outputs.MSRV }} - python-version: "3.12" - platform: - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - - # Test the `nightly` feature - - rust: nightly - python-version: "3.12" - platform: - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - - # Run rust beta to help catch toolchain regressions - - rust: beta - python-version: "3.12" - platform: - { - os: "ubuntu-latest", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - - # Test 32-bit Windows and x64 macOS only with the latest Python version - - rust: stable - python-version: "3.12" - platform: - { - os: "windows-latest", - python-architecture: "x86", - rust-target: "i686-pc-windows-msvc", - } - - rust: stable - python-version: "3.12" - platform: - { - os: "macos-13", - python-architecture: "x64", - rust-target: "x86_64-apple-darwin", - } - # ubuntu-latest (24.04) no longer supports 3.7, so run on 22.04 - - rust: stable - python-version: "3.7" - platform: - { - os: "ubuntu-22.04", - python-architecture: "x64", - rust-target: "x86_64-unknown-linux-gnu", - } - - # arm64 macOS Python not available on GitHub Actions until 3.10 - # so backfill 3.7-3.9 with x64 macOS runners - - rust: stable - python-version: "3.7" - platform: - { - os: "macos-13", - python-architecture: "x64", - rust-target: "x86_64-apple-darwin", - } - - rust: stable - python-version: "3.8" - platform: - { - os: "macos-13", - python-architecture: "x64", - rust-target: "x86_64-apple-darwin", - } - - rust: stable - python-version: "3.9" - platform: - { - os: "macos-13", - python-architecture: "x64", - rust-target: "x86_64-apple-darwin", - } - - exclude: - # ubuntu-latest (24.04) no longer supports 3.7 - - python-version: "3.7" - platform: { os: "ubuntu-latest" } - # arm64 macOS Python not available on GitHub Actions until 3.10 - - rust: stable - python-version: "3.7" - platform: - { - os: "macos-latest", - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - } - - rust: stable - python-version: "3.8" - platform: - { - os: "macos-latest", - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - } - - rust: stable - python-version: "3.9" - platform: - { - os: "macos-latest", - python-architecture: "arm64", - rust-target: "aarch64-apple-darwin", - } - - valgrind: - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - # FIXME valgrind detects an issue with Python 3.12.5, needs investigation - # whether it's a PyO3 issue or upstream CPython. - python-version: "3.12.4" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@stable - - uses: taiki-e/install-action@valgrind - - run: python -m pip install --upgrade pip && pip install nox - - run: nox -s test-rust -- release skip-full - env: - CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: valgrind --leak-check=no --error-exitcode=1 - RUST_BACKTRACE: 1 - TRYBUILD: overwrite - - careful: - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@nightly - with: - components: rust-src - - uses: taiki-e/install-action@cargo-careful - - run: python -m pip install --upgrade pip && pip install nox - - run: nox -s test-rust -- careful skip-full - env: - RUST_BACKTRACE: 1 - TRYBUILD: overwrite - - docsrs: - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@nightly - with: - components: rust-src - - run: cargo rustdoc --lib --no-default-features --features full -Zunstable-options --config "build.rustdocflags=[\"--cfg\", \"docsrs\"]" - - coverage: - if: ${{ github.event_name != 'merge_group' }} - needs: [fmt] - name: coverage ${{ matrix.os }} - strategy: - matrix: - os: ["windows-latest", "macos-latest", "ubuntu-latest"] - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@stable - with: - components: llvm-tools-preview,rust-src - - name: Install cargo-llvm-cov - uses: taiki-e/install-action@cargo-llvm-cov - - run: python -m pip install --upgrade pip && pip install nox - - run: nox -s coverage - - uses: codecov/codecov-action@v5 - with: - files: coverage.json - name: ${{ matrix.os }} - token: ${{ secrets.CODECOV_TOKEN }} - - emscripten: - name: emscripten - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - # TODO bump emscripten builds to test on 3.12 - python-version: 3.11 - id: setup-python - - name: Install Rust toolchain - uses: dtolnay/rust-toolchain@stable - with: - targets: wasm32-unknown-emscripten - components: rust-src - - uses: actions/setup-node@v4 - with: - node-version: 18 - - run: python -m pip install --upgrade pip && pip install nox - - uses: actions/cache@v4 - id: cache - with: - path: | - .nox/emscripten - key: ${{ hashFiles('emscripten/*') }} - ${{ hashFiles('noxfile.py') }} - ${{ steps.setup-python.outputs.python-path }} - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - name: Build - if: steps.cache.outputs.cache-hit != 'true' - run: nox -s build-emscripten - - name: Test - run: nox -s test-emscripten - - test-debug: - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - needs: [fmt] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@stable - with: - components: rust-src - - name: Install python3 standalone debug build with nox - run: | - PBS_RELEASE="20241016" - PBS_PYTHON_VERSION="3.13.0" - PBS_ARCHIVE="cpython-${PBS_PYTHON_VERSION}+${PBS_RELEASE}-x86_64-unknown-linux-gnu-debug-full.tar.zst" - wget "https://github.com/indygreg/python-build-standalone/releases/download/${PBS_RELEASE}/${PBS_ARCHIVE}" - tar -I zstd -xf "${PBS_ARCHIVE}" - ls -l $(pwd)/python/install/bin - ls -l $(pwd)/python/install/lib - echo PATH=$(pwd)/python/install/bin:$PATH >> $GITHUB_ENV - echo LD_LIBRARY_PATH=$(pwd)/python/install/lib:$LD_LIBRARY_PATH >> $GITHUB_ENV - echo PYTHONHOME=$(pwd)/python/install >> $GITHUB_ENV - echo PYO3_PYTHON=$(pwd)/python/install/bin/python3 >> $GITHUB_ENV - - run: python3 -m sysconfig - - run: python3 -m pip install --upgrade pip && pip install nox - - run: | - PYO3_CONFIG_FILE=$(mktemp) - cat > $PYO3_CONFIG_FILE << EOF - implementation=CPython - version=3.13 - shared=true - abi3=false - lib_name=python3.13d - lib_dir=${{ github.workspace }}/python/install/lib - executable=${{ github.workspace }}/python/install/bin/python3 - pointer_width=64 - build_flags=Py_DEBUG,Py_REF_DEBUG - suppress_build_script_link_lines=false - EOF - echo PYO3_CONFIG_FILE=$PYO3_CONFIG_FILE >> $GITHUB_ENV - - run: python3 -m nox -s test - - test-free-threaded: - needs: [fmt] - name: Free threaded tests - ${{ matrix.os }} - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: ["ubuntu-latest", "macos-latest", "windows-latest"] - steps: - - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@stable - with: - components: rust-src - # TODO: replace with actions/setup-python when there is support - - uses: quansight-labs/setup-python@v5.3.1 - with: - python-version: "3.13t" - - name: Install cargo-llvm-cov - uses: taiki-e/install-action@cargo-llvm-cov - - run: python3 -m sysconfig - - run: python3 -m pip install --upgrade pip && pip install nox - - name: Prepare coverage environment - run: | - cargo llvm-cov clean --workspace --profraw-only - nox -s set-coverage-env - - run: nox -s ffi-check - - run: nox - - name: Generate coverage report - run: nox -s generate-coverage-report - - name: Upload coverage report - uses: codecov/codecov-action@v5 - with: - files: coverage.json - name: ${{ matrix.os }}-test-free-threaded - token: ${{ secrets.CODECOV_TOKEN }} - - test-version-limits: - needs: [fmt] - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@stable - - run: python3 -m pip install --upgrade pip && pip install nox - - run: python3 -m nox -s test-version-limits - - check-feature-powerset: - needs: [fmt, resolve] - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - runs-on: ubuntu-latest - name: check-feature-powerset ${{ matrix.rust }} - strategy: - # run on stable and MSRV to check that all combinations of features are expected to build fine on our supported - # range of compilers - matrix: - rust: ["stable"] - include: - - rust: ${{ needs.resolve.outputs.MSRV }} - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - save-if: ${{ github.event_name != 'merge_group' }} - - uses: dtolnay/rust-toolchain@master - with: - toolchain: stable - - uses: taiki-e/install-action@v2 - with: - tool: cargo-hack,cargo-minimal-versions - - run: python3 -m pip install --upgrade pip && pip install nox - - run: python3 -m nox -s check-feature-powerset -- ${{ matrix.rust != 'stable' && 'minimal-versions' || '' }} - - test-cross-compilation: - needs: [fmt] - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - runs-on: ${{ matrix.os }} - name: test-cross-compilation ${{ matrix.os }} -> ${{ matrix.target }} - strategy: - # If one platform fails, allow the rest to keep testing if `CI-no-fail-fast` label is present - fail-fast: ${{ !contains(github.event.pull_request.labels.*.name, 'CI-no-fail-fast') }} - matrix: - include: - # ubuntu "cross compile" to itself - - os: "ubuntu-latest" - target: "x86_64-unknown-linux-gnu" - flags: "-i python3.12" - manylinux: auto - # ubuntu x86_64 -> aarch64 - - os: "ubuntu-latest" - target: "aarch64-unknown-linux-gnu" - flags: "-i python3.12" - manylinux: auto - # ubuntu x86_64 -> windows x86_64 - - os: "ubuntu-latest" - target: "x86_64-pc-windows-gnu" - flags: "-i python3.12 --features generate-import-lib" - # macos x86_64 -> aarch64 - - os: "macos-13" # last x86_64 macos runners - target: "aarch64-apple-darwin" - # macos aarch64 -> x86_64 - - os: "macos-latest" - target: "x86_64-apple-darwin" - # windows x86_64 -> aarch64 - - os: "windows-latest" - target: "aarch64-pc-windows-msvc" - flags: "-i python3.12 --features generate-import-lib" - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - workspaces: examples/maturin-starter - save-if: ${{ github.event_name != 'merge_group' }} - key: ${{ matrix.target }} - - name: Setup cross-compiler - if: ${{ matrix.target == 'x86_64-pc-windows-gnu' }} - run: sudo apt-get install -y mingw-w64 llvm - - name: Compile version-specific library - uses: PyO3/maturin-action@v1 - with: - target: ${{ matrix.target }} - manylinux: ${{ matrix.manylinux }} - args: --release -m examples/maturin-starter/Cargo.toml ${{ matrix.flags }} - - name: Compile abi3 library - uses: PyO3/maturin-action@v1 - with: - target: ${{ matrix.target }} - manylinux: ${{ matrix.manylinux }} - args: --release -m examples/maturin-starter/Cargo.toml --features abi3 ${{ matrix.flags }} - - test-cross-compilation-windows: - needs: [fmt] - if: ${{ contains(github.event.pull_request.labels.*.name, 'CI-build-full') || github.event_name != 'pull_request' }} - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - uses: Swatinem/rust-cache@v2 - with: - workspaces: examples/maturin-starter - save-if: ${{ github.event_name != 'merge_group' }} - - uses: actions/cache/restore@v4 - with: - # https://github.com/PyO3/maturin/discussions/1953 - path: ~/.cache/cargo-xwin - key: cargo-xwin-cache - - name: Test cross compile to Windows - env: - XWIN_ARCH: x86_64 - run: | - set -ex - sudo apt-get install -y mingw-w64 llvm - rustup target add x86_64-pc-windows-gnu x86_64-pc-windows-msvc - pip install cargo-xwin - # abi3 - cargo build --manifest-path examples/maturin-starter/Cargo.toml --features abi3 --target x86_64-pc-windows-gnu - cargo xwin build --manifest-path examples/maturin-starter/Cargo.toml --features abi3 --target x86_64-pc-windows-msvc - # non-abi3 - export PYO3_CROSS_PYTHON_VERSION=3.12 - cargo build --manifest-path examples/maturin-starter/Cargo.toml --features generate-import-lib --target x86_64-pc-windows-gnu - cargo xwin build --manifest-path examples/maturin-starter/Cargo.toml --features generate-import-lib --target x86_64-pc-windows-msvc - - if: ${{ github.ref == 'refs/heads/main' }} - uses: actions/cache/save@v4 - with: - path: ~/.cache/cargo-xwin - key: cargo-xwin-cache - conclusion: - needs: - - fmt - - check-msrv - - clippy - - build-pr - - build-full - - valgrind - - careful - - docsrs - - coverage - - emscripten - - test-debug - - test-free-threaded - - test-version-limits - - check-feature-powerset - - test-cross-compilation - - test-cross-compilation-windows - if: always() - runs-on: ubuntu-latest - steps: - - name: Result - run: | - jq -C <<< "${needs}" - # Check if all needs were successful or skipped. - "$(jq -r 'all(.result as $result | (["success", "skipped"] | contains([$result])))' <<< "${needs}")" - env: - needs: ${{ toJson(needs) }} diff --git a/vendor/pyo3/.github/workflows/coverage-pr-base.yml b/vendor/pyo3/.github/workflows/coverage-pr-base.yml deleted file mode 100644 index 3311869763..0000000000 --- a/vendor/pyo3/.github/workflows/coverage-pr-base.yml +++ /dev/null @@ -1,42 +0,0 @@ -# This runs as a separate job because it needs to run on the `pull_request_target` event -# in order to access the CODECOV_TOKEN secret. -# -# This is safe because this doesn't run arbitrary code from PRs. - -name: Set Codecov PR base -on: - # See safety note / doc at the top of this file. - pull_request_target: - -jobs: - coverage-pr-base: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - name: Set PR base on codecov - run: | - # fetch the merge commit between the PR base and head - git fetch -u --progress --depth=1 origin "+$BASE_REF:$BASE_REF" "+$MERGE_REF:$MERGE_REF" - while [ -z "$(git merge-base "$BASE_REF" "$MERGE_REF")" ]; do - git fetch -u -q --deepen="10" origin "$BASE_REF" "$MERGE_REF"; - done - - MERGE_BASE=$(git merge-base "$BASE_REF" "$MERGE_REF") - echo "Merge base: $MERGE_BASE" - - # inform codecov about the merge base - pip install codecov-cli - codecovcli pr-base-picking \ - --base-sha $MERGE_BASE \ - --pr ${{ github.event.number }} \ - --slug PyO3/pyo3 \ - --token ${{ secrets.CODECOV_TOKEN }} \ - --service github - env: - # Don't put these in bash, because we don't want the expansion to - # risk code execution - BASE_REF: "refs/heads/${{ github.event.pull_request.base.ref }}" - MERGE_REF: "refs/pull/${{ github.event.pull_request.number }}/merge" diff --git a/vendor/pyo3/.github/workflows/gh-pages.yml b/vendor/pyo3/.github/workflows/gh-pages.yml deleted file mode 100644 index 1050b97f31..0000000000 --- a/vendor/pyo3/.github/workflows/gh-pages.yml +++ /dev/null @@ -1,56 +0,0 @@ -name: gh-pages - -on: - push: - branches: - - main - pull_request: - release: - types: [published] - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} - cancel-in-progress: true - -env: - CARGO_TERM_COLOR: always - -jobs: - guide-build: - runs-on: ubuntu-latest - outputs: - tag_name: ${{ steps.prepare_tag.outputs.tag_name }} - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - uses: dtolnay/rust-toolchain@nightly - - - name: Setup mdBook - uses: taiki-e/install-action@v2 - with: - tool: mdbook,lychee - - - name: Prepare tag - id: prepare_tag - run: | - TAG_NAME="${GITHUB_REF##*/}" - echo "::set-output name=tag_name::${TAG_NAME}" - - # This builds the book in target/guide/. - - name: Build the guide - run: | - python -m pip install --upgrade pip && pip install nox - nox -s check-guide - env: - PYO3_VERSION_TAG: ${{ steps.prepare_tag.outputs.tag_name }} - - - name: Deploy docs and the guide - if: ${{ github.event_name == 'release' }} - uses: peaceiris/actions-gh-pages@v4 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./target/guide/ - destination_dir: ${{ steps.prepare_tag.outputs.tag_name }} - full_commit_message: "Upload documentation for ${{ steps.prepare_tag.outputs.tag_name }}" diff --git a/vendor/pyo3/.gitignore b/vendor/pyo3/.gitignore deleted file mode 100644 index 3ad8328cd1..0000000000 --- a/vendor/pyo3/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -target -Cargo.lock -/doc -/gh-pages -build/ -*.py[co] -__pycache__/ -.cache -.pytest_cache/ -dist/ -.tox/ -.mypy_cache/ -.hypothesis/ -.eggs/ -venv* -guide/book/ -guide/src/LICENSE-APACHE -guide/src/LICENSE-MIT -*.so -*.out -*.egg-info -extensions/stamps/ -pip-wheel-metadata -valgrind-python.supp -*.pyd -lcov.info -coverage.json -netlify_build/ -.nox/ diff --git a/vendor/pyo3/.netlify/build.sh b/vendor/pyo3/.netlify/build.sh deleted file mode 100755 index a61180be59..0000000000 --- a/vendor/pyo3/.netlify/build.sh +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/env bash - -set -uex - -rustup update nightly -rustup default nightly - -PYO3_VERSION=$(cargo search pyo3 --limit 1 | head -1 | tr -s ' ' | cut -d ' ' -f 3 | tr -d '"') - -## Start from the existing gh-pages content. -## By serving it over netlify, we can have better UX for users because -## netlify can then redirect e.g. /v0.17.0 to /v0.17.0/ -## which leads to better loading of CSS assets. - -wget -qc https://github.com/PyO3/pyo3/archive/gh-pages.tar.gz -O - | tar -xz -mv pyo3-gh-pages netlify_build - -## Configure netlify _redirects file - -# Add redirect for each documented version -set +x # these loops get very spammy and fill the deploy log - -for d in netlify_build/v*; do - version="${d/netlify_build\/v/}" - echo "/v$version/doc/* https://docs.rs/pyo3/$version/:splat" >> netlify_build/_redirects - if [ $version != $PYO3_VERSION ]; then - # for old versions, mark the files in the latest version as the canonical URL - for file in $(find $d -type f); do - file_path="${file/$d\//}" - # remove index.html and/or .html suffix to match the page URL on the - # final netlfiy site - url_path="$file_path" - if [[ $file_path == index.html ]]; then - url_path="" - elif [[ $file_path == *.html ]]; then - url_path="${file_path%.html}" - fi - echo "/v$version/$url_path" >> netlify_build/_headers - if test -f "netlify_build/v$PYO3_VERSION/$file_path"; then - echo " Link: ; rel=\"canonical\"" >> netlify_build/_headers - else - # this file doesn't exist in the latest guide, don't index it - echo " X-Robots-Tag: noindex" >> netlify_build/_headers - fi - done - fi -done - -# Add latest redirect -echo "/latest/* /v${PYO3_VERSION}/:splat 302" >> netlify_build/_redirects - -# some backwards compatbiility redirects -echo "/latest/building_and_distribution/* /latest/building-and-distribution/:splat 302" >> netlify_build/_redirects -echo "/latest/building-and-distribution/multiple_python_versions/* /latest/building-and-distribution/multiple-python-versions:splat 302" >> netlify_build/_redirects -echo "/latest/function/error_handling/* /latest/function/error-handling/:splat 302" >> netlify_build/_redirects -echo "/latest/getting_started/* /latest/getting-started/:splat 302" >> netlify_build/_redirects -echo "/latest/python_from_rust/* /latest/python-from-rust/:splat 302" >> netlify_build/_redirects -echo "/latest/python_typing_hints/* /latest/python-typing-hints/:splat 302" >> netlify_build/_redirects -echo "/latest/trait_bounds/* /latest/trait-bounds/:splat 302" >> netlify_build/_redirects - -## Add landing page redirect -if [ "${CONTEXT}" == "deploy-preview" ]; then - echo "/ /main/" >> netlify_build/_redirects -else - echo "/ /v${PYO3_VERSION}/ 302" >> netlify_build/_redirects -fi - -set -x -## Generate towncrier release notes - -pip install towncrier -towncrier build --yes --version Unreleased --date TBC - -## Build guide - -# Install latest mdbook. Netlify will cache the cargo bin dir, so this will -# only build mdbook if needed. -MDBOOK_VERSION=$(cargo search mdbook --limit 1 | head -1 | tr -s ' ' | cut -d ' ' -f 3 | tr -d '"') -INSTALLED_MDBOOK_VERSION=$(mdbook --version || echo "none") -if [ "${INSTALLED_MDBOOK_VERSION}" != "mdbook v${MDBOOK_VERSION}" ]; then - cargo install mdbook@${MDBOOK_VERSION} --force -fi - -# Install latest mdbook-linkcheck. Netlify will cache the cargo bin dir, so this will -# only build mdbook-linkcheck if needed. -MDBOOK_LINKCHECK_VERSION=$(cargo search mdbook-linkcheck --limit 1 | head -1 | tr -s ' ' | cut -d ' ' -f 3 | tr -d '"') -INSTALLED_MDBOOK_LINKCHECK_VERSION=$(mdbook-linkcheck --version || echo "none") -if [ "${INSTALLED_MDBOOK_LINKCHECK_VERSION}" != "mdbook v${MDBOOK_LINKCHECK_VERSION}" ]; then - cargo install mdbook-linkcheck@${MDBOOK_LINKCHECK_VERSION} --force -fi - -pip install nox -nox -s build-guide -mv target/guide/ netlify_build/main/ - -## Build public docs - -nox -s docs -mv target/doc netlify_build/main/doc/ - -echo "" > netlify_build/main/doc/index.html - -## Build internal docs - -nox -s docs -- nightly internal - -mkdir -p netlify_build/internal -mv target/doc netlify_build/internal/ - -ls -l netlify_build/ diff --git a/vendor/pyo3/.netlify/internal_banner.html b/vendor/pyo3/.netlify/internal_banner.html deleted file mode 100644 index 6f1024e44e..0000000000 --- a/vendor/pyo3/.netlify/internal_banner.html +++ /dev/null @@ -1,47 +0,0 @@ - - diff --git a/vendor/pyo3/.towncrier.template.md b/vendor/pyo3/.towncrier.template.md deleted file mode 100644 index 056a06d9a1..0000000000 --- a/vendor/pyo3/.towncrier.template.md +++ /dev/null @@ -1,14 +0,0 @@ -{% for section_text, section in sections.items() %}{%- if section %}{{section_text}}{% endif -%} -{% if section %} -{% for category in ['packaging', 'added', 'changed', 'removed', 'fixed' ] if category in section %} -### {{ definitions[category]['name'] }} - -{% if definitions[category]['showcontent'] %} -{% for text, pull_requests in section[category].items() %} -- {{ text }} {{ pull_requests|join(', ') }} -{% endfor %} -{% else %} -- {{ section[category]['']|join(', ') }} -{% endif %} - -{% endfor %}{% else %}No significant changes.{% endif %}{% endfor %} diff --git a/vendor/pyo3/Architecture.md b/vendor/pyo3/Architecture.md deleted file mode 100644 index be9d2b53d8..0000000000 --- a/vendor/pyo3/Architecture.md +++ /dev/null @@ -1,198 +0,0 @@ - - - - -# PyO3: Architecture - -This document roughly describes the high-level architecture of PyO3. -If you want to become familiar with the codebase you are in the right place! - -## Overview - -PyO3 provides a bridge between Rust and Python, based on the [Python/C API]. -Thus, PyO3 has low-level bindings of these API as its core. -On top of that, we have higher-level bindings to operate Python objects safely. -Also, to define Python classes and functions in Rust code, we have `trait PyClass` and a set of -protocol traits (e.g., `PyIterProtocol`) for supporting object protocols (i.e., `__dunder__` methods). -Since implementing `PyClass` requires lots of boilerplate, we have a proc-macro `#[pyclass]`. - -To summarize, there are six main parts to the PyO3 codebase. - -1. [Low-level bindings of Python/C API.](#1-low-level-bindings-of-python-capi) - - [`pyo3-ffi`] and [`src/ffi`] -2. [Bindings to Python objects.](#2-bindings-to-python-objects) - - [`src/instance.rs`] and [`src/types`] -3. [`PyClass` and related functionalities.](#3-pyclass-and-related-functionalities) - - [`src/pycell.rs`], [`src/pyclass.rs`], and more -4. [Procedural macros to simplify usage for users.](#4-procedural-macros-to-simplify-usage-for-users) - - [`src/impl_`], [`pyo3-macros`] and [`pyo3-macros-backend`] -5. [`build.rs` and `pyo3-build-config`](#5-buildrs-and-pyo3-build-config) - - [`build.rs`](https://github.com/PyO3/pyo3/tree/main/build.rs) - - [`pyo3-build-config`] - -## 1. Low-level bindings of Python/C API - -[`pyo3-ffi`] contains wrappers of the [Python/C API]. This is currently done by hand rather than -automated tooling because: - - it gives us best control about how to adapt C conventions to Rust, and - - there are many Python interpreter versions we support in a single set of files. - -We aim to provide straight-forward Rust wrappers resembling the file structure of [`cpython/Include`](https://github.com/python/cpython/tree/3.13/Include). - -We are continuously updating the module to match the latest CPython version which PyO3 supports (i.e. as of time of writing Python 3.13). The tracking issue is [#1289](https://github.com/PyO3/pyo3/issues/1289), and contribution is welcome. - -In the [`pyo3-ffi`] crate, there is lots of conditional compilation such as `#[cfg(Py_LIMITED_API)]`, -`#[cfg(Py_3_7)]`, and `#[cfg(PyPy)]`. -`Py_LIMITED_API` corresponds to `#define Py_LIMITED_API` macro in Python/C API. -With `Py_LIMITED_API`, we can build a Python-version-agnostic binary called an -[abi3 wheel](https://pyo3.rs/latest/building-and-distribution.html#py_limited_apiabi3). -`Py_3_7` means that the API is available from Python >= 3.7. -There are also `Py_3_8`, `Py_3_9`, and so on. -`PyPy` means that the API definition is for PyPy. -Those flags are set in [`build.rs`](#6-buildrs-and-pyo3-build-config). - -## 2. Bindings to Python objects - -[`src/types`] contains bindings to [built-in types](https://docs.python.org/3/library/stdtypes.html) -of Python, such as `dict` and `list`. -For historical reasons, Python's `object` is called `PyAny` in PyO3 and located in [`src/types/any.rs`]. - -Currently, `PyAny` is a straightforward wrapper of `ffi::PyObject`, defined as: - -```rust -#[repr(transparent)] -pub struct PyAny(UnsafeCell); -``` - -Concrete Python objects are implemented by wrapping `PyAny`, e.g.,: - -```rust -#[repr(transparent)] -pub struct PyDict(PyAny); -``` - -These types are not intended to be accessed directly, and instead are used through the `Py` and `Bound` smart pointers. - -We have some macros in [`src/types/mod.rs`] which make it easier to implement APIs for concrete Python types. - -## 3. `PyClass` and related functionalities - -[`src/pycell.rs`], [`src/pyclass.rs`], and [`src/type_object.rs`] contain types and -traits to make `#[pyclass]` work. -Also, [`src/pyclass_init.rs`] and [`src/impl_/pyclass.rs`] have related functionalities. - -To realize object-oriented programming in C, all Python objects have `ob_base: PyObject` as their -first field in their structure definition. Thanks to this guarantee, casting `*mut A` to `*mut PyObject` -is valid if `A` is a Python object. - -To ensure this guarantee, we have a wrapper struct `PyCell` in [`src/pycell.rs`] which is roughly: - -```rust -#[repr(C)] -pub struct PyCell { - ob_base: crate::ffi::PyObject, - inner: T, -} -``` - -Thus, when copying a Rust struct to a Python object, we first allocate `PyCell` on the Python heap and then -move `T` into it. -Also, `PyCell` provides [RefCell](https://doc.rust-lang.org/std/cell/struct.RefCell.html)-like methods -to ensure Rust's borrow rules. -See [the documentation](https://docs.rs/pyo3/latest/pyo3/pycell/struct.PyCell.html) for more. - -`PyCell` requires that `T` implements `PyClass`. -This trait is somewhat complex and derives many traits, but the most important one is `PyTypeInfo` -in [`src/type_object.rs`]. -`PyTypeInfo` is also implemented for built-in types. -In Python, all objects have their types, and types are also objects of `type`. -For example, you can see `type({})` shows `dict` and `type(type({}))` shows `type` in Python REPL. -`T: PyTypeInfo` implies that `T` has a corresponding type object. - -### Protocol methods - -Python has some built-in special methods called dunder methods, such as `__iter__`. -They are called "slots" in the [abstract objects layer](https://docs.python.org/3/c-api/abstract.html) in -Python/C API. -We provide a way to implement those protocols similarly, by recognizing special -names in `#[pymethods]`, with a few new ones for slots that can not be -implemented in Python, such as GC support. - -## 4. Procedural macros to simplify usage for users. - -[`pyo3-macros`] provides five proc-macro APIs: `pymodule`, `pyfunction`, `pyclass`, -`pymethods`, and `#[derive(FromPyObject)]`. -[`pyo3-macros-backend`] has the actual implementations of these APIs. -[`src/impl_`] contains `#[doc(hidden)]` functionality used in code generated by these proc-macros, -such as parsing function arguments. - -## 5. `build.rs` and `pyo3-build-config` - -PyO3 supports a wide range of OSes, interpreters and use cases. The correct environment must be -detected at build time in order to set up relevant conditional compilation correctly. This logic -is captured in the [`pyo3-build-config`] crate, which is a `build-dependency` of `pyo3` and -`pyo3-macros`, and can also be used by downstream users in the same way. - -In [`pyo3-build-config`]'s `build.rs` the build environment is detected and inlined into the crate -as a "config file". This works in all cases except for cross-compiling, where it is necessary to -capture this from the `pyo3` `build.rs` to get some extra environment variables that Cargo doesn't -set for build dependencies. - -The `pyo3` `build.rs` also runs some safety checks such as ensuring the Python version detected is -actually supported. - -Some of the functionality of `pyo3-build-config`: -- Find the interpreter for build and detect the Python version. - - We have to set some version flags like `#[cfg(Py_3_7)]`. - - If the interpreter is PyPy, we set `#[cfg(PyPy)`. - - If the `PYO3_CONFIG_FILE` environment variable is set then that file's contents will be used - instead of any detected configuration. - - If the `PYO3_NO_PYTHON` environment variable is set then the interpreter detection is bypassed - entirely and only abi3 extensions can be built. -- Check if we are building a Python extension. - - If we are building an extension (e.g., Python library installable by `pip`), - we don't link `libpython`. - Currently we use the `extension-module` feature for this purpose. This may change in the future. - See [#1123](https://github.com/PyO3/pyo3/pull/1123). -- Cross-compiling configuration - - If `TARGET` architecture and `HOST` architecture differ, we can find cross compile information - from environment variables (`PYO3_CROSS_LIB_DIR`, `PYO3_CROSS_PYTHON_VERSION` and - `PYO3_CROSS_PYTHON_IMPLEMENTATION`) or system files. - When cross compiling extension modules it is often possible to make it work without any - additional user input. - - When an experimental feature `generate-import-lib` is enabled, the `pyo3-ffi` build script can - generate `python3.dll` import libraries for Windows targets automatically via an external - [`python3-dll-a`] crate. This enables the users to cross compile Python extensions for Windows without - having to install any Windows Python libraries. - - - -[python/c api]: https://docs.python.org/3/c-api/ -[`python3-dll-a`]: https://docs.rs/python3-dll-a/latest/python3_dll_a/ - - - -[`pyo3-macros`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros -[`pyo3-macros-backend`]: https://github.com/PyO3/pyo3/tree/main/pyo3-macros-backend -[`pyo3-build-config`]: https://github.com/PyO3/pyo3/tree/main/pyo3-build-config -[`pyo3-ffi`]: https://github.com/PyO3/pyo3/tree/main/pyo3-ffi - - - -[`src/class`]: https://github.com/PyO3/pyo3/tree/main/src/class -[`src/ffi`]: https://github.com/PyO3/pyo3/tree/main/src/ffi -[`src/types`]: https://github.com/PyO3/pyo3/tree/main/src/types - - - -[`src/impl_`]: https://github.com/PyO3/pyo3/blob/main/src/impl_ -[`src/instance.rs`]: https://github.com/PyO3/pyo3/tree/main/src/instance.rs -[`src/pycell.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pycell.rs -[`src/pyclass.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass.rs -[`src/pyclass_init.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass_init.rs -[`src/pyclass_slot.rs`]: https://github.com/PyO3/pyo3/tree/main/src/pyclass_slot.rs -[`src/type_object.rs`]: https://github.com/PyO3/pyo3/tree/main/src/type_object.rs -[`src/class/methods.rs`]: https://github.com/PyO3/pyo3/tree/main/src/class/methods.rs -[`src/class/impl_.rs`]: https://github.com/PyO3/pyo3/tree/main/src/class/impl_.rs -[`src/types/any.rs`]: https://github.com/PyO3/pyo3/tree/main/src/types/any.rs -[`src/types/mod.rs`]: https://github.com/PyO3/pyo3/tree/main/src/types/mod.rs diff --git a/vendor/pyo3/CHANGELOG.md b/vendor/pyo3/CHANGELOG.md deleted file mode 100644 index dfcf20e6fa..0000000000 --- a/vendor/pyo3/CHANGELOG.md +++ /dev/null @@ -1,2144 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. For help with updating to new -PyO3 versions, please see the [migration guide](https://pyo3.rs/latest/migration.html). - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -To see unreleased changes, please see the [CHANGELOG on the main branch guide](https://pyo3.rs/main/changelog.html). - - - -## [0.23.4] - 2025-01-10 - -### Added - -- Add `PyList::locked_for_each`, which uses a critical section to lock the list on the free-threaded build. [#4789](https://github.com/PyO3/pyo3/pull/4789) -- Add `pyo3_build_config::add_python_framework_link_args` build script API to set rpath when using macOS system Python. [#4833](https://github.com/PyO3/pyo3/pull/4833) - -### Changed - -- Use `datetime.fold` to distinguish ambiguous datetimes when converting to and from `chrono::DateTime` (rather than erroring). [#4791](https://github.com/PyO3/pyo3/pull/4791) -- Optimize PyList iteration on the free-threaded build. [#4789](https://github.com/PyO3/pyo3/pull/4789) - -### Fixed - -- Fix unnecessary internal `py.allow_threads` GIL-switch when attempting to access contents of a `PyErr` which originated from Python (could lead to unintended deadlocks). [#4766](https://github.com/PyO3/pyo3/pull/4766) -- Fix thread-unsafe access of dict internals in `BoundDictIterator` on the free-threaded build. [#4788](https://github.com/PyO3/pyo3/pull/4788) -* Fix unnecessary critical sections in `BoundDictIterator` on the free-threaded build. [#4788](https://github.com/PyO3/pyo3/pull/4788) -- Fix time-of-check to time-of-use issues with list iteration on the free-threaded build. [#4789](https://github.com/PyO3/pyo3/pull/4789) -- Fix `chrono::DateTime` to-Python conversion when `Tz` is `chrono_tz::Tz`. [#4790](https://github.com/PyO3/pyo3/pull/4790) -- Fix `#[pyclass]` not being able to be named `Probe`. [#4794](https://github.com/PyO3/pyo3/pull/4794) -- Fix not treating cross-compilation from x64 to aarch64 on Windows as a cross-compile. [#4800](https://github.com/PyO3/pyo3/pull/4800) -- Fix missing struct fields on GraalPy when subclassing builtin classes. [#4802](https://github.com/PyO3/pyo3/pull/4802) -- Fix generating import lib for PyPy when `abi3` feature is enabled. [#4806](https://github.com/PyO3/pyo3/pull/4806) -- Fix generating import lib for python3.13t when `abi3` feature is enabled. [#4808](https://github.com/PyO3/pyo3/pull/4808) -- Fix compile failure for raw identifiers like `r#box` in `derive(FromPyObject)`. [#4814](https://github.com/PyO3/pyo3/pull/4814) -- Fix compile failure for `#[pyclass]` enum variants with more than 12 fields. [#4832](https://github.com/PyO3/pyo3/pull/4832) - - -## [0.23.3] - 2024-12-03 - -### Packaging - -- Bump optional `python3-dll-a` dependency to 0.2.11. [#4749](https://github.com/PyO3/pyo3/pull/4749) - -### Fixed - -- Fix unresolved symbol link failures on Windows when compiling for Python 3.13t with `abi3` features enabled. [#4733](https://github.com/PyO3/pyo3/pull/4733) -- Fix unresolved symbol link failures on Windows when compiling for Python 3.13t using the `generate-import-lib` feature. [#4749](https://github.com/PyO3/pyo3/pull/4749) -- Fix compile-time regression in PyO3 0.23.0 where changing `PYO3_CONFIG_FILE` would not reconfigure PyO3 for the new interpreter. [#4758](https://github.com/PyO3/pyo3/pull/4758) - - -## [0.23.2] - 2024-11-25 - -### Added - -- Add `IntoPyObjectExt` trait. [#4708](https://github.com/PyO3/pyo3/pull/4708) - -### Fixed - -- Fix compile failures when building for free-threaded Python when the `abi3` or `abi3-pyxx` features are enabled. [#4719](https://github.com/PyO3/pyo3/pull/4719) -- Fix `ambiguous_associated_items` lint error in `#[pyclass]` and `#[derive(IntoPyObject)]` macros. [#4725](https://github.com/PyO3/pyo3/pull/4725) - - -## [0.23.1] - 2024-11-16 - -Re-release of 0.23.0 with fixes to docs.rs build. - -## [0.23.0] - 2024-11-15 - -### Packaging - -- Drop support for PyPy 3.7 and 3.8. [#4582](https://github.com/PyO3/pyo3/pull/4582) -- Extend range of supported versions of `hashbrown` optional dependency to include version 0.15. [#4604](https://github.com/PyO3/pyo3/pull/4604) -- Bump minimum version of `eyre` optional dependency to 0.6.8. [#4617](https://github.com/PyO3/pyo3/pull/4617) -- Bump minimum version of `hashbrown` optional dependency to 0.14.5. [#4617](https://github.com/PyO3/pyo3/pull/4617) -- Bump minimum version of `indexmap` optional dependency to 2.5.0. [#4617](https://github.com/PyO3/pyo3/pull/4617) -- Bump minimum version of `num-complex` optional dependency to 0.4.6. [#4617](https://github.com/PyO3/pyo3/pull/4617) -- Bump minimum version of `chrono-tz` optional dependency to 0.10. [#4617](https://github.com/PyO3/pyo3/pull/4617) -- Support free-threaded Python 3.13t. [#4588](https://github.com/PyO3/pyo3/pull/4588) - -### Added - -- Add `IntoPyObject` (fallible) conversion trait to convert from Rust to Python values. [#4060](https://github.com/PyO3/pyo3/pull/4060) -- Add `#[pyclass(str="")]` option to generate `__str__` based on a `Display` implementation or format string. [#4233](https://github.com/PyO3/pyo3/pull/4233) -- Implement `PartialEq` for `Bound<'py, PyInt>` with `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128` and `isize`. [#4317](https://github.com/PyO3/pyo3/pull/4317) -- Implement `PartialEq` and `PartialEq` for `Bound<'py, PyFloat>`. [#4348](https://github.com/PyO3/pyo3/pull/4348) -- Add `as_super` and `into_super` methods for `Bound`. [#4351](https://github.com/PyO3/pyo3/pull/4351) -- Add FFI definitions `PyCFunctionFast` and `PyCFunctionFastWithKeywords` [#4415](https://github.com/PyO3/pyo3/pull/4415) -- Add FFI definitions for `PyMutex` on Python 3.13 and newer. [#4421](https://github.com/PyO3/pyo3/pull/4421) -- Add `PyDict::locked_for_each` to iterate efficiently on freethreaded Python. [#4439](https://github.com/PyO3/pyo3/pull/4439) -- Add FFI definitions `PyObject_GetOptionalAttr`, `PyObject_GetOptionalAttrString`, `PyObject_HasAttrWithError`, `PyObject_HasAttrStringWithError`, `Py_CONSTANT_*` constants, `Py_GetConstant`, `Py_GetConstantBorrowed`, and `PyType_GetModuleByDef` on Python 3.13 and newer. [#4447](https://github.com/PyO3/pyo3/pull/4447) -- Add FFI definitions for the Python critical section API available on Python 3.13 and newer. [#4477](https://github.com/PyO3/pyo3/pull/4477) -- Add derive macro for `IntoPyObject`. [#4495](https://github.com/PyO3/pyo3/pull/4495) -- Add `Borrowed::as_ptr`. [#4520](https://github.com/PyO3/pyo3/pull/4520) -- Add FFI definition for `PyImport_AddModuleRef`. [#4529](https://github.com/PyO3/pyo3/pull/4529) -- Add `PyAnyMethods::try_iter`. [#4553](https://github.com/PyO3/pyo3/pull/4553) -- Add `pyo3::sync::with_critical_section`, a wrapper around the Python Critical Section API added in Python 3.13. [#4587](https://github.com/PyO3/pyo3/pull/4587) -- Add `#[pymodule(gil_used = false)]` option to declare that a module supports the free-threaded build. [#4588](https://github.com/PyO3/pyo3/pull/4588) -- Add `PyModule::gil_used` method to declare that a module supports the free-threaded build. [#4588](https://github.com/PyO3/pyo3/pull/4588) -- Add FFI definition `PyDateTime_CAPSULE_NAME`. [#4634](https://github.com/PyO3/pyo3/pull/4634) -- Add `PyMappingProxy` type to represent the `mappingproxy` Python class. [#4644](https://github.com/PyO3/pyo3/pull/4644) -- Add FFI definitions `PyList_Extend` and `PyList_Clear`. [#4667](https://github.com/PyO3/pyo3/pull/4667) -- Add derive macro for `IntoPyObjectRef`. [#4674](https://github.com/PyO3/pyo3/pull/4674) -- Add `pyo3::sync::OnceExt` and `pyo3::sync::OnceLockExt` traits. [#4676](https://github.com/PyO3/pyo3/pull/4676) - -### Changed - -- Prefer `IntoPyObject` over `IntoPy>>` for `#[pyfunction]` and `#[pymethods]` return types. [#4060](https://github.com/PyO3/pyo3/pull/4060) -- Report multiple errors from `#[pyclass]` and `#[pyo3(..)]` attributes. [#4243](https://github.com/PyO3/pyo3/pull/4243) -- Nested declarative `#[pymodule]` are automatically treated as submodules (no `PyInit_` entrypoint is created). [#4308](https://github.com/PyO3/pyo3/pull/4308) -- Deprecate `PyAnyMethods::is_ellipsis` (`Py::is_ellipsis` was deprecated in PyO3 0.20). [#4322](https://github.com/PyO3/pyo3/pull/4322) -- Deprecate `PyLong` in favor of `PyInt`. [#4347](https://github.com/PyO3/pyo3/pull/4347) -- Rename `IntoPyDict::into_py_dict_bound` to `IntoPyDict::into_py_dict`. [#4388](https://github.com/PyO3/pyo3/pull/4388) -- `PyModule::from_code` now expects `&CStr` as arguments instead of `&str`. [#4404](https://github.com/PyO3/pyo3/pull/4404) -- Use "fastcall" Python calling convention for `#[pyfunction]`s when compiling on abi3 for Python 3.10 and up. [#4415](https://github.com/PyO3/pyo3/pull/4415) -- Remove `Copy` and `Clone` from `PyObject` struct FFI definition. [#4434](https://github.com/PyO3/pyo3/pull/4434) -- `Python::eval` and `Python::run` now take a `&CStr` instead of `&str`. [#4435](https://github.com/PyO3/pyo3/pull/4435) -- Deprecate `IPowModulo`, `PyClassAttributeDef`, `PyGetterDef`, `PyMethodDef`, `PyMethodDefType`, and `PySetterDef` from PyO3's public API. [#4441](https://github.com/PyO3/pyo3/pull/4441) -- `IntoPyObject` impls for `Vec`, `&[u8]`, `[u8; N]`, `Cow<[u8]>` and `SmallVec<[u8; N]>` now convert into Python `bytes` rather than a `list` of integers. [#4442](https://github.com/PyO3/pyo3/pull/4442) -- Emit a compile-time error when attempting to subclass a class that doesn't allow subclassing. [#4453](https://github.com/PyO3/pyo3/pull/4453) -- `IntoPyDict::into_py_dict` is now fallible due to `IntoPyObject` migration. [#4493](https://github.com/PyO3/pyo3/pull/4493) -- The `abi3` feature will now override config files provided via `PYO3_BUILD_CONFIG`. [#4497](https://github.com/PyO3/pyo3/pull/4497) -- Disable the `GILProtected` struct on free-threaded Python. [#4504](https://github.com/PyO3/pyo3/pull/4504) -- Updated FFI definitions for functions and struct fields that have been deprecated or removed from CPython. [#4534](https://github.com/PyO3/pyo3/pull/4534) -- Disable `PyListMethods::get_item_unchecked` on free-threaded Python. [#4539](https://github.com/PyO3/pyo3/pull/4539) -- Add `GILOnceCell::import`. [#4542](https://github.com/PyO3/pyo3/pull/4542) -- Deprecate `PyAnyMethods::iter` in favour of `PyAnyMethods::try_iter`. [#4553](https://github.com/PyO3/pyo3/pull/4553) -- The `#[pyclass]` macro now requires a types to be `Sync`. (Except for `#[pyclass(unsendable)]` types). [#4566](https://github.com/PyO3/pyo3/pull/4566) -- `PyList::new` and `PyTuple::new` are now fallible due to `IntoPyObject` migration. [#4580](https://github.com/PyO3/pyo3/pull/4580) -- `PyErr::matches` is now fallible due to `IntoPyObject` migration. [#4595](https://github.com/PyO3/pyo3/pull/4595) -- Deprecate `ToPyObject` in favour of `IntoPyObject` [#4595](https://github.com/PyO3/pyo3/pull/4595) -- Deprecate `PyWeakrefMethods::get_option`. [#4597](https://github.com/PyO3/pyo3/pull/4597) -- Seal `PyWeakrefMethods` trait. [#4598](https://github.com/PyO3/pyo3/pull/4598) -- Remove `PyNativeTypeInitializer` and `PyObjectInit` from the PyO3 public API. [#4611](https://github.com/PyO3/pyo3/pull/4611) -- Deprecate `IntoPy` in favor of `IntoPyObject` [#4618](https://github.com/PyO3/pyo3/pull/4618) -- Eagerly normalize exceptions in `PyErr::take()` and `PyErr::fetch()` on Python 3.11 and older. [#4655](https://github.com/PyO3/pyo3/pull/4655) -- Move `IntoPy::type_output` to `IntoPyObject::type_output`. [#4657](https://github.com/PyO3/pyo3/pull/4657) -- Change return type of `PyMapping::keys`, `PyMapping::values` and `PyMapping::items` to `Bound<'py, PyList>` instead of `Bound<'py, PySequence>`. [#4661](https://github.com/PyO3/pyo3/pull/4661) -- Complex enums now allow field types that either implement `IntoPyObject` by reference or by value together with `Clone`. This makes `Py` available as field type. [#4694](https://github.com/PyO3/pyo3/pull/4694) - - -### Removed - -- Remove all functionality deprecated in PyO3 0.20. [#4322](https://github.com/PyO3/pyo3/pull/4322) -- Remove all functionality deprecated in PyO3 0.21. [#4323](https://github.com/PyO3/pyo3/pull/4323) -- Deprecate `PyUnicode` in favour of `PyString`. [#4370](https://github.com/PyO3/pyo3/pull/4370) -- Remove deprecated `gil-refs` feature. [#4378](https://github.com/PyO3/pyo3/pull/4378) -- Remove private FFI definitions `_Py_IMMORTAL_REFCNT`, `_Py_IsImmortal`, `_Py_TPFLAGS_STATIC_BUILTIN`, `_Py_Dealloc`, `_Py_IncRef`, `_Py_DecRef`. [#4447](https://github.com/PyO3/pyo3/pull/4447) -- Remove private FFI definitions `_Py_c_sum`, `_Py_c_diff`, `_Py_c_neg`, `_Py_c_prod`, `_Py_c_quot`, `_Py_c_pow`, `_Py_c_abs`. [#4521](https://github.com/PyO3/pyo3/pull/4521) -- Remove `_borrowed` methods of `PyWeakRef` and `PyWeakRefProxy`. [#4528](https://github.com/PyO3/pyo3/pull/4528) -- Removed private FFI definition `_PyErr_ChainExceptions`. [#4534](https://github.com/PyO3/pyo3/pull/4534) - -### Fixed - -- Fix invalid library search path `lib_dir` when cross-compiling. [#4389](https://github.com/PyO3/pyo3/pull/4389) -- Fix FFI definition `Py_Is` for PyPy on 3.10 to call the function defined by PyPy. [#4447](https://github.com/PyO3/pyo3/pull/4447) -- Fix compile failure when using `#[cfg]` attributes for simple enum variants. [#4509](https://github.com/PyO3/pyo3/pull/4509) -- Fix compiler warning for `non_snake_case` method names inside `#[pymethods]` generated code. [#4567](https://github.com/PyO3/pyo3/pull/4567) -- Fix compile error with `#[derive(FromPyObject)]` generic struct with trait bounds. [#4645](https://github.com/PyO3/pyo3/pull/4645) -- Fix compile error for `#[classmethod]` and `#[staticmethod]` on magic methods. [#4654](https://github.com/PyO3/pyo3/pull/4654) -- Fix compile warning for `unsafe_op_in_unsafe_fn` in generated macro code. [#4674](https://github.com/PyO3/pyo3/pull/4674) -- Fix incorrect deprecation warning for `#[pyclass] enum`s with custom `__eq__` implementation. [#4692](https://github.com/PyO3/pyo3/pull/4692) -- Fix `non_upper_case_globals` lint firing for generated `__match_args__` on complex enums. [#4705](https://github.com/PyO3/pyo3/pull/4705) - -## [0.22.5] - 2024-10-15 - -### Fixed - -- Fix regression in 0.22.4 of naming collision in `__clear__` slot and `clear` method generated code. [#4619](https://github.com/PyO3/pyo3/pull/4619) - - -## [0.22.4] - 2024-10-12 - -### Added - -- Add FFI definition `PyWeakref_GetRef` and `compat::PyWeakref_GetRef`. [#4528](https://github.com/PyO3/pyo3/pull/4528) - -### Changed - -- Deprecate `_borrowed` methods on `PyWeakRef` and `PyWeakrefProxy` (just use the owning forms). [#4590](https://github.com/PyO3/pyo3/pull/4590) - -### Fixed - -- Revert removal of private FFI function `_PyLong_NumBits` on Python 3.13 and later. [#4450](https://github.com/PyO3/pyo3/pull/4450) -- Fix `__traverse__` functions for base classes not being called by subclasses created with `#[pyclass(extends = ...)]`. [#4563](https://github.com/PyO3/pyo3/pull/4563) -- Fix regression in 0.22.3 failing compiles under `#![forbid(unsafe_code)]`. [#4574](https://github.com/PyO3/pyo3/pull/4574) -- Fix `create_exception` macro triggering lint and compile errors due to interaction with `gil-refs` feature. [#4589](https://github.com/PyO3/pyo3/pull/4589) -- Workaround possible use-after-free in `_borrowed` methods on `PyWeakRef` and `PyWeakrefProxy` by leaking their contents. [#4590](https://github.com/PyO3/pyo3/pull/4590) -- Fix crash calling `PyType_GetSlot` on static types before Python 3.10. [#4599](https://github.com/PyO3/pyo3/pull/4599) - - -## [0.22.3] - 2024-09-15 - -### Added - -- Add `pyo3::ffi::compat` namespace with compatibility shims for C API functions added in recent versions of Python. -- Add FFI definition `PyDict_GetItemRef` on Python 3.13 and newer, and `compat::PyDict_GetItemRef` for all versions. [#4355](https://github.com/PyO3/pyo3/pull/4355) -- Add FFI definition `PyList_GetItemRef` on Python 3.13 and newer, and `pyo3_ffi::compat::PyList_GetItemRef` for all versions. [#4410](https://github.com/PyO3/pyo3/pull/4410) -- Add FFI definitions `compat::Py_NewRef` and `compat::Py_XNewRef`. [#4445](https://github.com/PyO3/pyo3/pull/4445) -- Add FFI definitions `compat::PyObject_CallNoArgs` and `compat::PyObject_CallMethodNoArgs`. [#4461](https://github.com/PyO3/pyo3/pull/4461) -- Add `GilOnceCell>::clone_ref`. [#4511](https://github.com/PyO3/pyo3/pull/4511) - -### Changed - -- Improve error messages for `#[pyfunction]` defined inside `#[pymethods]`. [#4349](https://github.com/PyO3/pyo3/pull/4349) -- Improve performance of calls to Python by using the vectorcall calling convention where possible. [#4456](https://github.com/PyO3/pyo3/pull/4456) -- Mention the type name in the exception message when trying to instantiate a class with no constructor defined. [#4481](https://github.com/PyO3/pyo3/pull/4481) - -### Removed - -- Remove private FFI definition `_Py_PackageContext`. [#4420](https://github.com/PyO3/pyo3/pull/4420) - -### Fixed - -- Fix compile failure in declarative `#[pymodule]` under presence of `#![no_implicit_prelude]`. [#4328](https://github.com/PyO3/pyo3/pull/4328) -- Fix use of borrowed reference in `PyDict::get_item` (unsafe in free-threaded Python). [#4355](https://github.com/PyO3/pyo3/pull/4355) -- Fix `#[pyclass(eq)]` macro hygiene issues for structs and enums. [#4359](https://github.com/PyO3/pyo3/pull/4359) -- Fix hygiene/span issues of `#[pyfunction]` and `#[pymethods]` generated code which affected expansion in `macro_rules` context. [#4382](https://github.com/PyO3/pyo3/pull/4382) -- Fix `unsafe_code` lint error in `#[pyclass]` generated code. [#4396](https://github.com/PyO3/pyo3/pull/4396) -- Fix async functions returning a tuple only returning the first element to Python. [#4407](https://github.com/PyO3/pyo3/pull/4407) -- Fix use of borrowed reference in `PyList::get_item` (unsafe in free-threaded Python). [#4410](https://github.com/PyO3/pyo3/pull/4410) -- Correct FFI definition `PyArg_ParseTupleAndKeywords` to take `*const *const c_char` instead of `*mut *mut c_char` on Python 3.13 and up. [#4420](https://github.com/PyO3/pyo3/pull/4420) -- Fix a soundness bug with `PyClassInitializer`: panic if adding subclass to existing instance via `PyClassInitializer::from(Py).add_subclass(SubClass)`. [#4454](https://github.com/PyO3/pyo3/pull/4454) -- Fix illegal reference counting op inside implementation of `__traverse__` handlers. [#4479](https://github.com/PyO3/pyo3/pull/4479) - -## [0.22.2] - 2024-07-17 - -### Packaging - -- Require opt-in to freethreaded Python using the `UNSAFE_PYO3_BUILD_FREE_THREADED=1` environment variable (it is not yet supported by PyO3). [#4327](https://github.com/PyO3/pyo3/pull/4327) - -### Changed - -- Use FFI function calls for reference counting on all abi3 versions. [#4324](https://github.com/PyO3/pyo3/pull/4324) -- `#[pymodule(...)]` now directly accepts all relevant `#[pyo3(...)]` options. [#4330](https://github.com/PyO3/pyo3/pull/4330) - -### Fixed - -- Fix compile failure in declarative `#[pymodule]` under presence of `#![no_implicit_prelude]`. [#4328](https://github.com/PyO3/pyo3/pull/4328) -- Fix compile failure due to c-string literals on Rust < 1.79. [#4353](https://github.com/PyO3/pyo3/pull/4353) - -## [0.22.1] - 2024-07-06 - -### Added - -- Add `#[pyo3(submodule)]` option for declarative `#[pymodule]`s. [#4301](https://github.com/PyO3/pyo3/pull/4301) -- Implement `PartialEq` for `Bound<'py, PyBool>`. [#4305](https://github.com/PyO3/pyo3/pull/4305) - -### Fixed - -- Return `NotImplemented` instead of raising `TypeError` from generated equality method when comparing different types. [#4287](https://github.com/PyO3/pyo3/pull/4287) -- Handle full-path `#[pyo3::prelude::pymodule]` and similar for `#[pyclass]` and `#[pyfunction]` in declarative modules. [#4288](https://github.com/PyO3/pyo3/pull/4288) -- Fix 128-bit int regression on big-endian platforms with Python <3.13. [#4291](https://github.com/PyO3/pyo3/pull/4291) -- Stop generating code that will never be covered with declarative modules. [#4297](https://github.com/PyO3/pyo3/pull/4297) -- Fix invalid deprecation warning for trailing optional on `#[setter]` function. [#4304](https://github.com/PyO3/pyo3/pull/4304) - -## [0.22.0] - 2024-06-24 - -### Packaging - -- Update `heck` dependency to 0.5. [#3966](https://github.com/PyO3/pyo3/pull/3966) -- Extend range of supported versions of `chrono-tz` optional dependency to include version 0.10. [#4061](https://github.com/PyO3/pyo3/pull/4061) -- Update MSRV to 1.63. [#4129](https://github.com/PyO3/pyo3/pull/4129) -- Add optional `num-rational` feature to add conversions with Python's `fractions.Fraction`. [#4148](https://github.com/PyO3/pyo3/pull/4148) -- Support Python 3.13. [#4184](https://github.com/PyO3/pyo3/pull/4184) - -### Added - -- Add `PyWeakref`, `PyWeakrefReference` and `PyWeakrefProxy`. [#3835](https://github.com/PyO3/pyo3/pull/3835) -- Support `#[pyclass]` on enums that have tuple variants. [#4072](https://github.com/PyO3/pyo3/pull/4072) -- Add support for scientific notation in `Decimal` conversion. [#4079](https://github.com/PyO3/pyo3/pull/4079) -- Add `pyo3_disable_reference_pool` conditional compilation flag to avoid the overhead of the global reference pool at the cost of known limitations as explained in the performance section of the guide. [#4095](https://github.com/PyO3/pyo3/pull/4095) -- Add `#[pyo3(constructor = (...))]` to customize the generated constructors for complex enum variants. [#4158](https://github.com/PyO3/pyo3/pull/4158) -- Add `PyType::module`, which always matches Python `__module__`. [#4196](https://github.com/PyO3/pyo3/pull/4196) -- Add `PyType::fully_qualified_name` which matches the "fully qualified name" defined in [PEP 737](https://peps.python.org/pep-0737). [#4196](https://github.com/PyO3/pyo3/pull/4196) -- Add `PyTypeMethods::mro` and `PyTypeMethods::bases`. [#4197](https://github.com/PyO3/pyo3/pull/4197) -- Add `#[pyclass(ord)]` to implement ordering based on `PartialOrd`. [#4202](https://github.com/PyO3/pyo3/pull/4202) -- Implement `ToPyObject` and `IntoPy` for `PyBackedStr` and `PyBackedBytes`. [#4205](https://github.com/PyO3/pyo3/pull/4205) -- Add `#[pyclass(hash)]` option to implement `__hash__` in terms of the `Hash` implementation [#4206](https://github.com/PyO3/pyo3/pull/4206) -- Add `#[pyclass(eq)]` option to generate `__eq__` based on `PartialEq`, and `#[pyclass(eq_int)]` for simple enums to implement equality based on their discriminants. [#4210](https://github.com/PyO3/pyo3/pull/4210) -- Implement `From>` for `PyClassInitializer`. [#4214](https://github.com/PyO3/pyo3/pull/4214) -- Add `as_super` methods to `PyRef` and `PyRefMut` for accesing the base class by reference. [#4219](https://github.com/PyO3/pyo3/pull/4219) -- Implement `PartialEq` for `Bound<'py, PyString>`. [#4245](https://github.com/PyO3/pyo3/pull/4245) -- Implement `PyModuleMethods::filename` on PyPy. [#4249](https://github.com/PyO3/pyo3/pull/4249) -- Implement `PartialEq<[u8]>` for `Bound<'py, PyBytes>`. [#4250](https://github.com/PyO3/pyo3/pull/4250) -- Add `pyo3_ffi::c_str` macro to create `&'static CStr` on Rust versions which don't have 1.77's `c""` literals. [#4255](https://github.com/PyO3/pyo3/pull/4255) -- Support `bool` conversion with `numpy` 2.0's `numpy.bool` type [#4258](https://github.com/PyO3/pyo3/pull/4258) -- Add `PyAnyMethods::{bitnot, matmul, floor_div, rem, divmod}`. [#4264](https://github.com/PyO3/pyo3/pull/4264) - -### Changed - -- Change the type of `PySliceIndices::slicelength` and the `length` parameter of `PySlice::indices()`. [#3761](https://github.com/PyO3/pyo3/pull/3761) -- Deprecate implicit default for trailing optional arguments [#4078](https://github.com/PyO3/pyo3/pull/4078) -- `Clone`ing pointers into the Python heap has been moved behind the `py-clone` feature, as it must panic without the GIL being held as a soundness fix. [#4095](https://github.com/PyO3/pyo3/pull/4095) -- Add `#[track_caller]` to all `Py`, `Bound<'py, T>` and `Borrowed<'a, 'py, T>` methods which can panic. [#4098](https://github.com/PyO3/pyo3/pull/4098) -- Change `PyAnyMethods::dir` to be fallible and return `PyResult>` (and similar for `PyAny::dir`). [#4100](https://github.com/PyO3/pyo3/pull/4100) -- The global reference pool (to track pending reference count decrements) is now initialized lazily to avoid the overhead of taking a mutex upon function entry when the functionality is not actually used. [#4178](https://github.com/PyO3/pyo3/pull/4178) -- Emit error messages when using `weakref` or `dict` when compiling for `abi3` for Python older than 3.9. [#4194](https://github.com/PyO3/pyo3/pull/4194) -- Change `PyType::name` to always match Python `__name__`. [#4196](https://github.com/PyO3/pyo3/pull/4196) -- Remove CPython internal ffi call for complex number including: add, sub, mul, div, neg, abs, pow. Added PyAnyMethods::{abs, pos, neg} [#4201](https://github.com/PyO3/pyo3/pull/4201) -- Deprecate implicit integer comparision for simple enums in favor of `#[pyclass(eq_int)]`. [#4210](https://github.com/PyO3/pyo3/pull/4210) -- Set the `module=` attribute of declarative modules' child `#[pymodule]`s and `#[pyclass]`es. [#4213](https://github.com/PyO3/pyo3/pull/4213) -- Set the `module` option for complex enum variants from the value set on the complex enum `module`. [#4228](https://github.com/PyO3/pyo3/pull/4228) -- Respect the Python "limited API" when building for the `abi3` feature on PyPy or GraalPy. [#4237](https://github.com/PyO3/pyo3/pull/4237) -- Optimize code generated by `#[pyo3(get)]` on `#[pyclass]` fields. [#4254](https://github.com/PyO3/pyo3/pull/4254) -- `PyCFunction::new`, `PyCFunction::new_with_keywords` and `PyCFunction::new_closure` now take `&'static CStr` name and doc arguments (previously was `&'static str`). [#4255](https://github.com/PyO3/pyo3/pull/4255) -- The `experimental-declarative-modules` feature is now stabilized and available by default. [#4257](https://github.com/PyO3/pyo3/pull/4257) - -### Fixed - -- Fix panic when `PYO3_CROSS_LIB_DIR` is set to a missing path. [#4043](https://github.com/PyO3/pyo3/pull/4043) -- Fix a compile error when exporting an exception created with `create_exception!` living in a different Rust module using the `declarative-module` feature. [#4086](https://github.com/PyO3/pyo3/pull/4086) -- Fix FFI definitions of `PY_VECTORCALL_ARGUMENTS_OFFSET` and `PyVectorcall_NARGS` to fix a false-positive assertion. [#4104](https://github.com/PyO3/pyo3/pull/4104) -- Disable `PyUnicode_DATA` on PyPy: not exposed by PyPy. [#4116](https://github.com/PyO3/pyo3/pull/4116) -- Correctly handle `#[pyo3(from_py_with = ...)]` attribute on dunder (`__magic__`) method arguments instead of silently ignoring it. [#4117](https://github.com/PyO3/pyo3/pull/4117) -- Fix a compile error when declaring a standalone function or class method with a Python name that is a Rust keyword. [#4226](https://github.com/PyO3/pyo3/pull/4226) -- Fix declarative modules discarding doc comments on the `mod` node. [#4236](https://github.com/PyO3/pyo3/pull/4236) -- Fix `__dict__` attribute missing for `#[pyclass(dict)]` instances when building for `abi3` on Python 3.9. [#4251](https://github.com/PyO3/pyo3/pull/4251) - -## [0.21.2] - 2024-04-16 - -### Changed - -- Deprecate the `PySet::empty()` gil-ref constructor. [#4082](https://github.com/PyO3/pyo3/pull/4082) - -### Fixed - -- Fix compile error for `async fn` in `#[pymethods]` with a `&self` receiver and more than one additional argument. [#4035](https://github.com/PyO3/pyo3/pull/4035) -- Improve error message for wrong receiver type in `__traverse__`. [#4045](https://github.com/PyO3/pyo3/pull/4045) -- Fix compile error when exporting a `#[pyclass]` living in a different Rust module using the `experimental-declarative-modules` feature. [#4054](https://github.com/PyO3/pyo3/pull/4054) -- Fix `missing_docs` lint triggering on documented `#[pymodule]` functions. [#4067](https://github.com/PyO3/pyo3/pull/4067) -- Fix undefined symbol errors for extension modules on AIX (by linking `libpython`). [#4073](https://github.com/PyO3/pyo3/pull/4073) - -## [0.21.1] - 2024-04-01 - -### Added - -- Implement `Send` and `Sync` for `PyBackedStr` and `PyBackedBytes`. [#4007](https://github.com/PyO3/pyo3/pull/4007) -- Implement `Clone`, `Debug`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash` implementation for `PyBackedBytes` and `PyBackedStr`, and `Display` for `PyBackedStr`. [#4020](https://github.com/PyO3/pyo3/pull/4020) -- Add `import_exception_bound!` macro to import exception types without generating GIL Ref functionality for them. [#4027](https://github.com/PyO3/pyo3/pull/4027) - -### Changed - -- Emit deprecation warning for uses of GIL Refs as `#[setter]` function arguments. [#3998](https://github.com/PyO3/pyo3/pull/3998) -- Add `#[inline]` hints on many `Bound` and `Borrowed` methods. [#4024](https://github.com/PyO3/pyo3/pull/4024) - -### Fixed - -- Handle `#[pyo3(from_py_with = "")]` in `#[setter]` methods [#3995](https://github.com/PyO3/pyo3/pull/3995) -- Allow extraction of `&Bound` in `#[setter]` methods. [#3998](https://github.com/PyO3/pyo3/pull/3998) -- Fix some uncovered code blocks emitted by `#[pymodule]`, `#[pyfunction]` and `#[pyclass]` macros. [#4009](https://github.com/PyO3/pyo3/pull/4009) -- Fix typo in the panic message when a class referenced in `pyo3::import_exception!` does not exist. [#4012](https://github.com/PyO3/pyo3/pull/4012) -- Fix compile error when using an async `#[pymethod]` with a receiver and additional arguments. [#4015](https://github.com/PyO3/pyo3/pull/4015) - - -## [0.21.0] - 2024-03-25 - -### Added - -- Add support for GraalPy (24.0 and up). [#3247](https://github.com/PyO3/pyo3/pull/3247) -- Add `PyMemoryView` type. [#3514](https://github.com/PyO3/pyo3/pull/3514) -- Allow `async fn` in for `#[pyfunction]` and `#[pymethods]`, with the `experimental-async` feature. [#3540](https://github.com/PyO3/pyo3/pull/3540) [#3588](https://github.com/PyO3/pyo3/pull/3588) [#3599](https://github.com/PyO3/pyo3/pull/3599) [#3931](https://github.com/PyO3/pyo3/pull/3931) -- Implement `PyTypeInfo` for `PyEllipsis`, `PyNone` and `PyNotImplemented`. [#3577](https://github.com/PyO3/pyo3/pull/3577) -- Support `#[pyclass]` on enums that have non-unit variants. [#3582](https://github.com/PyO3/pyo3/pull/3582) -- Support `chrono` feature with `abi3` feature. [#3664](https://github.com/PyO3/pyo3/pull/3664) -- `FromPyObject`, `IntoPy` and `ToPyObject` are implemented on `std::duration::Duration` [#3670](https://github.com/PyO3/pyo3/pull/3670) -- Add `PyString::to_cow`. Add `Py::to_str`, `Py::to_cow`, and `Py::to_string_lossy`, as ways to access Python string data safely beyond the GIL lifetime. [#3677](https://github.com/PyO3/pyo3/pull/3677) -- Add `Bound` and `Borrowed` smart pointers as a new API for accessing Python objects. [#3686](https://github.com/PyO3/pyo3/pull/3686) -- Add `PyNativeType::as_borrowed` to convert "GIL refs" to the new `Bound` smart pointer. [#3692](https://github.com/PyO3/pyo3/pull/3692) -- Add `FromPyObject::extract_bound` method, to migrate `FromPyObject` implementations to the Bound API. [#3706](https://github.com/PyO3/pyo3/pull/3706) -- Add `gil-refs` feature to allow continued use of the deprecated GIL Refs APIs. [#3707](https://github.com/PyO3/pyo3/pull/3707) -- Add methods to `PyAnyMethods` for binary operators (`add`, `sub`, etc.) [#3712](https://github.com/PyO3/pyo3/pull/3712) -- Add `chrono-tz` feature allowing conversion between `chrono_tz::Tz` and `zoneinfo.ZoneInfo` [#3730](https://github.com/PyO3/pyo3/pull/3730) -- Add FFI definition `PyType_GetModuleByDef`. [#3734](https://github.com/PyO3/pyo3/pull/3734) -- Conversion between `std::time::SystemTime` and `datetime.datetime` [#3736](https://github.com/PyO3/pyo3/pull/3736) -- Add `Py::as_any` and `Py::into_any`. [#3785](https://github.com/PyO3/pyo3/pull/3785) -- Add `PyStringMethods::encode_utf8`. [#3801](https://github.com/PyO3/pyo3/pull/3801) -- Add `PyBackedStr` and `PyBackedBytes`, as alternatives to `&str` and `&bytes` where a Python object owns the data. [#3802](https://github.com/PyO3/pyo3/pull/3802) [#3991](https://github.com/PyO3/pyo3/pull/3991) -- Allow `#[pymodule]` macro on Rust `mod` blocks, with the `experimental-declarative-modules` feature. [#3815](https://github.com/PyO3/pyo3/pull/3815) -- Implement `ExactSizeIterator` for `set` and `frozenset` iterators on `abi3` feature. [#3849](https://github.com/PyO3/pyo3/pull/3849) -- Add `Py::drop_ref` to explicitly drop a `Py`` and immediately decrease the Python reference count if the GIL is already held. [#3871](https://github.com/PyO3/pyo3/pull/3871) -- Allow `#[pymodule]` macro on single argument functions that take `&Bound<'_, PyModule>`. [#3905](https://github.com/PyO3/pyo3/pull/3905) -- Implement `FromPyObject` for `Cow`. [#3928](https://github.com/PyO3/pyo3/pull/3928) -- Implement `Default` for `GILOnceCell`. [#3971](https://github.com/PyO3/pyo3/pull/3971) -- Add `PyDictMethods::into_mapping`, `PyListMethods::into_sequence` and `PyTupleMethods::into_sequence`. [#3982](https://github.com/PyO3/pyo3/pull/3982) - -### Changed - -- `PyDict::from_sequence` now takes a single argument of type `&PyAny` (previously took two arguments `Python` and `PyObject`). [#3532](https://github.com/PyO3/pyo3/pull/3532) -- Deprecate `Py::is_ellipsis` and `PyAny::is_ellipsis` in favour of `any.is(py.Ellipsis())`. [#3577](https://github.com/PyO3/pyo3/pull/3577) -- Split some `PyTypeInfo` functionality into new traits `HasPyGilRef` and `PyTypeCheck`. [#3600](https://github.com/PyO3/pyo3/pull/3600) -- Deprecate `PyTryFrom` and `PyTryInto` traits in favor of `any.downcast()` via the `PyTypeCheck` and `PyTypeInfo` traits. [#3601](https://github.com/PyO3/pyo3/pull/3601) -- Allow async methods to accept `&self`/`&mut self` [#3609](https://github.com/PyO3/pyo3/pull/3609) -- `FromPyObject` for set types now also accept `frozenset` objects as input. [#3632](https://github.com/PyO3/pyo3/pull/3632) -- `FromPyObject` for `bool` now also accepts NumPy's `bool_` as input. [#3638](https://github.com/PyO3/pyo3/pull/3638) -- Add `AsRefSource` associated type to `PyNativeType`. [#3653](https://github.com/PyO3/pyo3/pull/3653) -- Rename `.is_true` to `.is_truthy` on `PyAny` and `Py` to clarify that the test is not based on identity with or equality to the True singleton. [#3657](https://github.com/PyO3/pyo3/pull/3657) -- `PyType::name` is now `PyType::qualname` whereas `PyType::name` efficiently accesses the full name which includes the module name. [#3660](https://github.com/PyO3/pyo3/pull/3660) -- The `Iter(A)NextOutput` types are now deprecated and `__(a)next__` can directly return anything which can be converted into Python objects, i.e. awaitables do not need to be wrapped into `IterANextOutput` or `Option` any more. `Option` can still be used as well and returning `None` will trigger the fast path for `__next__`, stopping iteration without having to raise a `StopIteration` exception. [#3661](https://github.com/PyO3/pyo3/pull/3661) -- Implement `FromPyObject` on `chrono::DateTime` for all `Tz`, not just `FixedOffset` and `Utc`. [#3663](https://github.com/PyO3/pyo3/pull/3663) -- Add lifetime parameter to `PyTzInfoAccess` trait. For the deprecated gil-ref API, the trait is now implemented for `&'py PyTime` and `&'py PyDateTime` instead of `PyTime` and `PyDate`. [#3679](https://github.com/PyO3/pyo3/pull/3679) -- Calls to `__traverse__` become no-ops for unsendable pyclasses if on the wrong thread, thereby avoiding hard aborts at the cost of potential leakage. [#3689](https://github.com/PyO3/pyo3/pull/3689) -- Include `PyNativeType` in `pyo3::prelude`. [#3692](https://github.com/PyO3/pyo3/pull/3692) -- Improve performance of `extract::` (and other integer types) by avoiding call to `__index__()` converting the value to an integer for 3.10+. Gives performance improvement of around 30% for successful extraction. [#3742](https://github.com/PyO3/pyo3/pull/3742) -- Relax bound of `FromPyObject` for `Py` to just `T: PyTypeCheck`. [#3776](https://github.com/PyO3/pyo3/pull/3776) -- `PySet` and `PyFrozenSet` iterators now always iterate the equivalent of `iter(set)`. (A "fast path" with no noticeable performance benefit was removed.) [#3849](https://github.com/PyO3/pyo3/pull/3849) -- Move implementations of `FromPyObject` for `&str`, `Cow`, `&[u8]` and `Cow<[u8]>` onto a temporary trait `FromPyObjectBound` when `gil-refs` feature is deactivated. [#3928](https://github.com/PyO3/pyo3/pull/3928) -- Deprecate `GILPool`, `Python::with_pool`, and `Python::new_pool`. [#3947](https://github.com/PyO3/pyo3/pull/3947) - -### Removed - -- Remove all functionality deprecated in PyO3 0.19. [#3603](https://github.com/PyO3/pyo3/pull/3603) - -### Fixed - -- Match PyPy 7.3.14 in removing PyPy-only symbol `Py_MAX_NDIMS` in favour of `PyBUF_MAX_NDIM`. [#3757](https://github.com/PyO3/pyo3/pull/3757) -- Fix segmentation fault using `datetime` types when an invalid `datetime` module is on sys.path. [#3818](https://github.com/PyO3/pyo3/pull/3818) -- Fix `non_local_definitions` lint warning triggered by many PyO3 macros. [#3901](https://github.com/PyO3/pyo3/pull/3901) -- Disable `PyCode` and `PyCode_Type` on PyPy: `PyCode_Type` is not exposed by PyPy. [#3934](https://github.com/PyO3/pyo3/pull/3934) - -## [0.21.0-beta.0] - 2024-03-10 - -Prerelease of PyO3 0.21. See [the GitHub diff](https://github.com/pyo3/pyo3/compare/v0.21.0-beta.0...v0.21.0) for what changed between 0.21.0-beta.0 and the final release. - -## [0.20.3] - 2024-02-23 - -### Packaging - -- Add `portable-atomic` dependency. [#3619](https://github.com/PyO3/pyo3/pull/3619) -- Check maximum version of Python at build time and for versions not yet supported require opt-in to the `abi3` stable ABI by the environment variable `PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1`. [#3821](https://github.com/PyO3/pyo3/pull/3821) - -### Fixed - -- Use `portable-atomic` to support platforms without 64-bit atomics. [#3619](https://github.com/PyO3/pyo3/pull/3619) -- Fix compilation failure with `either` feature enabled without `experimental-inspect` enabled. [#3834](https://github.com/PyO3/pyo3/pull/3834) - -## [0.20.2] - 2024-01-04 - -### Packaging - -- Pin `pyo3` and `pyo3-ffi` dependencies on `pyo3-build-config` to require the same patch version, i.e. `pyo3` 0.20.2 requires _exactly_ `pyo3-build-config` 0.20.2. [#3721](https://github.com/PyO3/pyo3/pull/3721) - -### Fixed - -- Fix compile failure when building `pyo3` 0.20.0 with latest `pyo3-build-config` 0.20.X. [#3724](https://github.com/PyO3/pyo3/pull/3724) -- Fix docs.rs build. [#3722](https://github.com/PyO3/pyo3/pull/3722) - -## [0.20.1] - 2023-12-30 - -### Added - -- Add optional `either` feature to add conversions for `either::Either` sum type. [#3456](https://github.com/PyO3/pyo3/pull/3456) -- Add optional `smallvec` feature to add conversions for `smallvec::SmallVec`. [#3507](https://github.com/PyO3/pyo3/pull/3507) -- Add `take` and `into_inner` methods to `GILOnceCell` [#3556](https://github.com/PyO3/pyo3/pull/3556) -- `#[classmethod]` methods can now also receive `Py` as their first argument. [#3587](https://github.com/PyO3/pyo3/pull/3587) -- `#[pyfunction(pass_module)]` can now also receive `Py` as their first argument. [#3587](https://github.com/PyO3/pyo3/pull/3587) -- Add `traverse` method to `GILProtected`. [#3616](https://github.com/PyO3/pyo3/pull/3616) -- Added `abi3-py312` feature [#3687](https://github.com/PyO3/pyo3/pull/3687) - -### Fixed - -- Fix minimum version specification for optional `chrono` dependency. [#3512](https://github.com/PyO3/pyo3/pull/3512) -- Silenced new `clippy::unnecessary_fallible_conversions` warning when using a `Py` `self` receiver. [#3564](https://github.com/PyO3/pyo3/pull/3564) - - -## [0.20.0] - 2023-10-11 - -### Packaging - -- Dual-license PyO3 under either the Apache 2.0 OR the MIT license. This makes the project GPLv2 compatible. [#3108](https://github.com/PyO3/pyo3/pull/3108) -- Update MSRV to Rust 1.56. [#3208](https://github.com/PyO3/pyo3/pull/3208) -- Bump `indoc` dependency to 2.0 and `unindent` dependency to 0.2. [#3237](https://github.com/PyO3/pyo3/pull/3237) -- Bump `syn` dependency to 2.0. [#3239](https://github.com/PyO3/pyo3/pull/3239) -- Drop support for debug builds of Python 3.7. [#3387](https://github.com/PyO3/pyo3/pull/3387) -- Bump `chrono` optional dependency to require 0.4.25 or newer. [#3427](https://github.com/PyO3/pyo3/pull/3427) -- Support Python 3.12. [#3488](https://github.com/PyO3/pyo3/pull/3488) - -### Added - -- Support `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__` and `__ge__` in `#[pymethods]`. [#3203](https://github.com/PyO3/pyo3/pull/3203) -- Add FFI definition `Py_GETENV`. [#3336](https://github.com/PyO3/pyo3/pull/3336) -- Add `as_ptr` and `into_ptr` inherent methods for `Py`, `PyAny`, `PyRef`, and `PyRefMut`. [#3359](https://github.com/PyO3/pyo3/pull/3359) -- Implement `DoubleEndedIterator` for `PyTupleIterator` and `PyListIterator`. [#3366](https://github.com/PyO3/pyo3/pull/3366) -- Add `#[pyclass(rename_all = "...")]` option: this allows renaming all getters and setters of a struct, or all variants of an enum. Available renaming rules are: `"camelCase"`, `"kebab-case"`, `"lowercase"`, `"PascalCase"`, `"SCREAMING-KEBAB-CASE"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"UPPERCASE"`. [#3384](https://github.com/PyO3/pyo3/pull/3384) -- Add FFI definitions `PyObject_GC_IsTracked` and `PyObject_GC_IsFinalized` on Python 3.9 and up (PyPy 3.10 and up). [#3403](https://github.com/PyO3/pyo3/pull/3403) -- Add types for `None`, `Ellipsis`, and `NotImplemented`. [#3408](https://github.com/PyO3/pyo3/pull/3408) -- Add FFI definitions for the `Py_mod_multiple_interpreters` constant and its possible values. [#3494](https://github.com/PyO3/pyo3/pull/3494) -- Add FFI definitions for `PyInterpreterConfig` struct, its constants and `Py_NewInterpreterFromConfig`. [#3502](https://github.com/PyO3/pyo3/pull/3502) - -### Changed - -- Change `PySet::discard` to return `PyResult` (previously returned nothing). [#3281](https://github.com/PyO3/pyo3/pull/3281) -- Optimize implmentation of `IntoPy` for Rust tuples to Python tuples. [#3321](https://github.com/PyO3/pyo3/pull/3321) -- Change `PyDict::get_item` to no longer suppress arbitrary exceptions (the return type is now `PyResult>` instead of `Option<&PyAny>`), and deprecate `PyDict::get_item_with_error`. [#3330](https://github.com/PyO3/pyo3/pull/3330) -- Deprecate FFI definitions which are deprecated in Python 3.12. [#3336](https://github.com/PyO3/pyo3/pull/3336) -- `AsPyPointer` is now an `unsafe trait`. [#3358](https://github.com/PyO3/pyo3/pull/3358) -- Accept all `os.PathLike` values in implementation of `FromPyObject` for `PathBuf`. [#3374](https://github.com/PyO3/pyo3/pull/3374) -- Add `__builtins__` to globals in `py.run()` and `py.eval()` if they're missing. [#3378](https://github.com/PyO3/pyo3/pull/3378) -- Optimize implementation of `FromPyObject` for `BigInt` and `BigUint`. [#3379](https://github.com/PyO3/pyo3/pull/3379) -- `PyIterator::from_object` and `PyByteArray::from` now take a single argument of type `&PyAny` (previously took two arguments `Python` and `AsPyPointer`). [#3389](https://github.com/PyO3/pyo3/pull/3389) -- Replace `AsPyPointer` with `AsRef` as a bound in the blanket implementation of `From<&T> for PyObject`. [#3391](https://github.com/PyO3/pyo3/pull/3391) -- Replace blanket `impl IntoPy for &T where T: AsPyPointer` with implementations of `impl IntoPy` for `&PyAny`, `&T where T: AsRef`, and `&Py`. [#3393](https://github.com/PyO3/pyo3/pull/3393) -- Preserve `std::io::Error` kind in implementation of `From` for `PyErr` [#3396](https://github.com/PyO3/pyo3/pull/3396) -- Try to select a relevant `ErrorKind` in implementation of `From` for `OSError` subclass. [#3397](https://github.com/PyO3/pyo3/pull/3397) -- Retrieve the original `PyErr` in implementation of `From` for `PyErr` if the `std::io::Error` has been built using a Python exception (previously would create a new exception wrapping the `std::io::Error`). [#3402](https://github.com/PyO3/pyo3/pull/3402) -- `#[pymodule]` will now return the same module object on repeated import by the same Python interpreter, on Python 3.9 and up. [#3446](https://github.com/PyO3/pyo3/pull/3446) -- Truncate leap-seconds and warn when converting `chrono` types to Python `datetime` types (`datetime` cannot represent leap-seconds). [#3458](https://github.com/PyO3/pyo3/pull/3458) -- `Err` returned from `#[pyfunction]` will now have a non-None `__context__` if called from inside a `catch` block. [#3455](https://github.com/PyO3/pyo3/pull/3455) -- Deprecate undocumented `#[__new__]` form of `#[new]` attribute. [#3505](https://github.com/PyO3/pyo3/pull/3505) - -### Removed - -- Remove all functionality deprecated in PyO3 0.18, including `#[args]` attribute for `#[pymethods]`. [#3232](https://github.com/PyO3/pyo3/pull/3232) -- Remove `IntoPyPointer` trait in favour of `into_ptr` inherent methods. [#3385](https://github.com/PyO3/pyo3/pull/3385) - -### Fixed - -- Handle exceptions properly in `PySet::discard`. [#3281](https://github.com/PyO3/pyo3/pull/3281) -- The `PyTupleIterator` type returned by `PyTuple::iter` is now public and hence can be named by downstream crates. [#3366](https://github.com/PyO3/pyo3/pull/3366) -- Linking of `PyOS_FSPath` on PyPy. [#3374](https://github.com/PyO3/pyo3/pull/3374) -- Fix memory leak in `PyTypeBuilder::build`. [#3401](https://github.com/PyO3/pyo3/pull/3401) -- Disable removed FFI definitions `_Py_GetAllocatedBlocks`, `_PyObject_GC_Malloc`, and `_PyObject_GC_Calloc` on Python 3.11 and up. [#3403](https://github.com/PyO3/pyo3/pull/3403) -- Fix `ResourceWarning` and crashes related to GC when running with debug builds of CPython. [#3404](https://github.com/PyO3/pyo3/pull/3404) -- Some-wrapping of `Option` default arguments will no longer re-wrap `Some(T)` or expressions evaluating to `None`. [#3461](https://github.com/PyO3/pyo3/pull/3461) -- Fix `IterNextOutput::Return` not returning a value on PyPy. [#3471](https://github.com/PyO3/pyo3/pull/3471) -- Emit compile errors instead of ignoring macro invocations inside `#[pymethods]` blocks. [#3491](https://github.com/PyO3/pyo3/pull/3491) -- Emit error on invalid arguments to `#[new]`, `#[classmethod]`, `#[staticmethod]`, and `#[classattr]`. [#3484](https://github.com/PyO3/pyo3/pull/3484) -- Disable `PyMarshal_WriteObjectToString` from `PyMarshal_ReadObjectFromString` with the `abi3` feature. [#3490](https://github.com/PyO3/pyo3/pull/3490) -- Fix FFI definitions for `_PyFrameEvalFunction` on Python 3.11 and up (it now receives a `_PyInterpreterFrame` opaque struct). [#3500](https://github.com/PyO3/pyo3/pull/3500) - - -## [0.19.2] - 2023-08-01 - -### Added - -- Add FFI definitions `PyState_AddModule`, `PyState_RemoveModule` and `PyState_FindModule` for PyPy 3.9 and up. [#3295](https://github.com/PyO3/pyo3/pull/3295) -- Add FFI definitions `_PyObject_CallFunction_SizeT` and `_PyObject_CallMethod_SizeT`. [#3297](https://github.com/PyO3/pyo3/pull/3297) -- Add a "performance" section to the guide collecting performance-related tricks and problems. [#3304](https://github.com/PyO3/pyo3/pull/3304) -- Add `PyErr::Display` for all Python versions, and FFI symbol `PyErr_DisplayException` for Python 3.12. [#3334](https://github.com/PyO3/pyo3/pull/3334) -- Add FFI definition `PyType_GetDict()` for Python 3.12. [#3339](https://github.com/PyO3/pyo3/pull/3339) -- Add `PyAny::downcast_exact`. [#3346](https://github.com/PyO3/pyo3/pull/3346) -- Add `PySlice::full()` to construct a full slice (`::`). [#3353](https://github.com/PyO3/pyo3/pull/3353) - -### Changed - -- Update `PyErr` for 3.12 betas to avoid deprecated ffi methods. [#3306](https://github.com/PyO3/pyo3/pull/3306) -- Update FFI definitions of `object.h` for Python 3.12.0b4. [#3335](https://github.com/PyO3/pyo3/pull/3335) -- Update `pyo3::ffi` struct definitions to be compatible with 3.12.0b4. [#3342](https://github.com/PyO3/pyo3/pull/3342) -- Optimize conversion of `float` to `f64` (and `PyFloat::value`) on non-abi3 builds. [#3345](https://github.com/PyO3/pyo3/pull/3345) - -### Fixed - -- Fix timezone conversion bug for FixedOffset datetimes that were being incorrectly converted to and from UTC. [#3269](https://github.com/PyO3/pyo3/pull/3269) -- Fix `SystemError` raised in `PyUnicodeDecodeError_Create` on PyPy 3.10. [#3297](https://github.com/PyO3/pyo3/pull/3297) -- Correct FFI definition `Py_EnterRecursiveCall` to return `c_int` (was incorrectly returning `()`). [#3300](https://github.com/PyO3/pyo3/pull/3300) -- Fix case where `PyErr::matches` and `PyErr::is_instance` returned results inconsistent with `PyErr::get_type`. [#3313](https://github.com/PyO3/pyo3/pull/3313) -- Fix loss of panic message in `PanicException` when unwinding after the exception was "normalized". [#3326](https://github.com/PyO3/pyo3/pull/3326) -- Fix `PyErr::from_value` and `PyErr::into_value` losing traceback on conversion. [#3328](https://github.com/PyO3/pyo3/pull/3328) -- Fix reference counting of immortal objects on Python 3.12.0b4. [#3335](https://github.com/PyO3/pyo3/pull/3335) - - -## [0.19.1] - 2023-07-03 - -### Packaging - -- Extend range of supported versions of `hashbrown` optional dependency to include version 0.14 [#3258](https://github.com/PyO3/pyo3/pull/3258) -- Extend range of supported versions of `indexmap` optional dependency to include version 2. [#3277](https://github.com/PyO3/pyo3/pull/3277) -- Support PyPy 3.10. [#3289](https://github.com/PyO3/pyo3/pull/3289) - -### Added - -- Add `pyo3::types::PyFrozenSetBuilder` to allow building a `PyFrozenSet` item by item. [#3156](https://github.com/PyO3/pyo3/pull/3156) -- Add support for converting to and from Python's `ipaddress.IPv4Address`/`ipaddress.IPv6Address` and `std::net::IpAddr`. [#3197](https://github.com/PyO3/pyo3/pull/3197) -- Add support for `num-bigint` feature in combination with `abi3`. [#3198](https://github.com/PyO3/pyo3/pull/3198) -- Add `PyErr_GetRaisedException()`, `PyErr_SetRaisedException()` to FFI definitions for Python 3.12 and later. [#3248](https://github.com/PyO3/pyo3/pull/3248) -- Add `Python::with_pool` which is a safer but more limited alternative to `Python::new_pool`. [#3263](https://github.com/PyO3/pyo3/pull/3263) -- Add `PyDict::get_item_with_error` on PyPy. [#3270](https://github.com/PyO3/pyo3/pull/3270) -- Allow `#[new]` methods may to return `Py` in order to return existing instances. [#3287](https://github.com/PyO3/pyo3/pull/3287) - -### Fixed - -- Fix conversion of classes implementing `__complex__` to `Complex` when using `abi3` or PyPy. [#3185](https://github.com/PyO3/pyo3/pull/3185) -- Stop suppressing unrelated exceptions in `PyAny::hasattr`. [#3271](https://github.com/PyO3/pyo3/pull/3271) -- Fix memory leak when creating `PySet` or `PyFrozenSet` or returning types converted into these internally, e.g. `HashSet` or `BTreeSet`. [#3286](https://github.com/PyO3/pyo3/pull/3286) - - -## [0.19.0] - 2023-05-31 - -### Packaging - -- Correct dependency on syn to version 1.0.85 instead of the incorrect version 1.0.56. [#3152](https://github.com/PyO3/pyo3/pull/3152) - -### Added - -- Accept `text_signature` option (and automatically generate signature) for `#[new]` in `#[pymethods]`. [#2980](https://github.com/PyO3/pyo3/pull/2980) -- Add support for converting to and from Python's `decimal.Decimal` and `rust_decimal::Decimal`. [#3016](https://github.com/PyO3/pyo3/pull/3016) -- Add `#[pyo3(from_item_all)]` when deriving `FromPyObject` to specify `get_item` as getter for all fields. [#3120](https://github.com/PyO3/pyo3/pull/3120) -- Add `pyo3::exceptions::PyBaseExceptionGroup` for Python 3.11, and corresponding FFI definition `PyExc_BaseExceptionGroup`. [#3141](https://github.com/PyO3/pyo3/pull/3141) -- Accept `#[new]` with `#[classmethod]` to create a constructor which receives a (subtype's) class/`PyType` as its first argument. [#3157](https://github.com/PyO3/pyo3/pull/3157) -- Add `PyClass::get` and `Py::get` for GIL-indepedent access to classes with `#[pyclass(frozen)]`. [#3158](https://github.com/PyO3/pyo3/pull/3158) -- Add `PyAny::is_exact_instance` and `PyAny::is_exact_instance_of`. [#3161](https://github.com/PyO3/pyo3/pull/3161) - -### Changed - -- `PyAny::is_instance_of::(obj)` is now equivalent to `T::is_type_of(obj)`, and now returns `bool` instead of `PyResult`. [#2881](https://github.com/PyO3/pyo3/pull/2881) -- Deprecate `text_signature` option on `#[pyclass]` structs. [#2980](https://github.com/PyO3/pyo3/pull/2980) -- No longer wrap `anyhow::Error`/`eyre::Report` containing a basic `PyErr` without a chain in a `PyRuntimeError`. [#3004](https://github.com/PyO3/pyo3/pull/3004) -- - Change `#[getter]` and `#[setter]` to use a common call "trampoline" to slightly reduce generated code size and compile times. [#3029](https://github.com/PyO3/pyo3/pull/3029) -- Improve default values for str, numbers and bool in automatically-generated `text_signature`. [#3050](https://github.com/PyO3/pyo3/pull/3050) -- Improve default value for `None` in automatically-generated `text_signature`. [#3066](https://github.com/PyO3/pyo3/pull/3066) -- Rename `PySequence::list` and `PySequence::tuple` to `PySequence::to_list` and `PySequence::to_tuple`. (The old names continue to exist as deprecated forms.) [#3111](https://github.com/PyO3/pyo3/pull/3111) -- Extend the lifetime of the GIL token returned by `PyRef::py` and `PyRefMut::py` to match the underlying borrow. [#3131](https://github.com/PyO3/pyo3/pull/3131) -- Safe access to the GIL, for example via `Python::with_gil`, is now locked inside of implementations of the `__traverse__` slot. [#3168](https://github.com/PyO3/pyo3/pull/3168) - -### Removed - -- Remove all functionality deprecated in PyO3 0.17, most prominently `Python::acquire_gil` is replaced by `Python::with_gil`. [#2981](https://github.com/PyO3/pyo3/pull/2981) - -### Fixed - -- Correct FFI definitions `PyGetSetDef`, `PyMemberDef`, `PyStructSequence_Field` and `PyStructSequence_Desc` to have `*const c_char` members for `name` and `doc` (not `*mut c_char`). [#3036](https://github.com/PyO3/pyo3/pull/3036) -- Fix panic on `fmt::Display`, instead return `""` string and report error via `sys.unraisablehook()` [#3062](https://github.com/PyO3/pyo3/pull/3062) -- Fix a compile error of "temporary value dropped while borrowed" when `#[pyfunction]`s take references into `#[pyclass]`es [#3142](https://github.com/PyO3/pyo3/pull/3142) -- Fix crashes caused by PyO3 applying deferred reference count updates when entering a `__traverse__` implementation. [#3168](https://github.com/PyO3/pyo3/pull/3168) -- Forbid running the `Drop` implementations of unsendable classes on other threads. [#3176](https://github.com/PyO3/pyo3/pull/3176) -- Fix a compile error when `#[pymethods]` items come from somewhere else (for example, as a macro argument) and a custom receiver like `Py` is used. [#3178](https://github.com/PyO3/pyo3/pull/3178) - - -## [0.18.3] - 2023-04-13 - -### Added - -- Add `GILProtected` to mediate concurrent access to a value using Python's global interpreter lock (GIL). [#2975](https://github.com/PyO3/pyo3/pull/2975) -- Support `PyASCIIObject` / `PyUnicode` and associated methods on big-endian architectures. [#3015](https://github.com/PyO3/pyo3/pull/3015) -- Add FFI definition `_PyDict_Contains_KnownHash()` for CPython 3.10 and up. [#3088](https://github.com/PyO3/pyo3/pull/3088) - -### Fixed - -- Fix compile error for `#[pymethods]` and `#[pyfunction]` called "output". [#3022](https://github.com/PyO3/pyo3/pull/3022) -- Fix compile error in generated code for magic methods implemented as a `#[staticmethod]`. [#3055](https://github.com/PyO3/pyo3/pull/3055) -- Fix `is_instance` for `PyDateTime` (would incorrectly check for a `PyDate`). [#3071](https://github.com/PyO3/pyo3/pull/3071) -- Fix upstream deprecation of `PyUnicode_InternImmortal` since Python 3.10. [#3071](https://github.com/PyO3/pyo3/pull/3087) - - -## [0.18.2] - 2023-03-24 - -### Packaging - -- Disable default features of `chrono` to avoid depending on `time` v0.1.x. [#2939](https://github.com/PyO3/pyo3/pull/2939) - -### Added - -- Implement `IntoPy`, `ToPyObject` and `FromPyObject` for `Cow<[u8]>` to efficiently handle both `bytes` and `bytearray` objects. [#2899](https://github.com/PyO3/pyo3/pull/2899) -- Implement `IntoPy`, `ToPyObject` and `FromPyObject` for `Cell`. [#3014](https://github.com/PyO3/pyo3/pull/3014) -- Add `PyList::to_tuple()`, as a convenient and efficient conversion from lists to tuples. [#3042](https://github.com/PyO3/pyo3/pull/3042) -- Add `PyTuple::to_list()`, as a convenient and efficient conversion from tuples to lists. [#3044](https://github.com/PyO3/pyo3/pull/3044) - -### Changed - -- Optimize `PySequence` conversion for `list` and `tuple` inputs. [#2944](https://github.com/PyO3/pyo3/pull/2944) -- Improve exception raised when creating `#[pyclass]` type object fails during module import. [#2947](https://github.com/PyO3/pyo3/pull/2947) -- Optimize `PyMapping` conversion for `dict` inputs. [#2954](https://github.com/PyO3/pyo3/pull/2954) -- Allow `create_exception!` to take a `dotted.module` to place the exception in a submodule. [#2979](https://github.com/PyO3/pyo3/pull/2979) - -### Fixed - -- Fix a reference counting race condition affecting `PyObject`s cloned in `allow_threads` blocks. [#2952](https://github.com/PyO3/pyo3/pull/2952) -- Fix `clippy::redundant_closure` lint on default arguments in `#[pyo3(signature = (...))]` annotations. [#2990](https://github.com/PyO3/pyo3/pull/2990) -- Fix `non_snake_case` lint on generated code in `#[pyfunction]` macro. [#2993](https://github.com/PyO3/pyo3/pull/2993) -- Fix some FFI definitions for the upcoming PyPy 3.10 release. [#3031](https://github.com/PyO3/pyo3/pull/3031) - - -## [0.18.1] - 2023-02-07 - -### Added - -- Add `PyErr::write_unraisable()`. [#2889](https://github.com/PyO3/pyo3/pull/2889) -- Add `Python::Ellipsis()` and `PyAny::is_ellipsis()` methods. [#2911](https://github.com/PyO3/pyo3/pull/2911) -- Add `PyDict::update()` and `PyDict::update_if_missing()` methods. [#2912](https://github.com/PyO3/pyo3/pull/2912) - -### Changed - -- FFI definition `PyIter_Check` on CPython 3.7 is now implemented as `hasattr(type(obj), "__next__")`, which works correctly on all platforms and adds support for `abi3`. [#2914](https://github.com/PyO3/pyo3/pull/2914) -- Warn about unknown config keys in `PYO3_CONFIG_FILE` instead of denying. [#2926](https://github.com/PyO3/pyo3/pull/2926) - -### Fixed - -- Send errors returned by `__releasebuffer__` to `sys.unraisablehook` rather than causing `SystemError`. [#2886](https://github.com/PyO3/pyo3/pull/2886) -- Fix downcast to `PyIterator` succeeding for Python classes which did not implement `__next__`. [#2914](https://github.com/PyO3/pyo3/pull/2914) -- Fix segfault in `__traverse__` when visiting `None` fields of `Option`. [#2921](https://github.com/PyO3/pyo3/pull/2921) -- Fix `#[pymethods(crate = "...")]` option being ignored. [#2923](https://github.com/PyO3/pyo3/pull/2923) -- Link against `pythonXY_d.dll` for debug Python builds on Windows. [#2937](https://github.com/PyO3/pyo3/pull/2937) - - -## [0.18.0] - 2023-01-17 - -### Packaging - -- Relax `indexmap` optional depecency to allow `>= 1.6, < 2`. [#2849](https://github.com/PyO3/pyo3/pull/2849) -- Relax `hashbrown` optional dependency to allow `>= 0.9, < 0.14`. [#2875](https://github.com/PyO3/pyo3/pull/2875) -- Update `memoffset` dependency to 0.8. [#2875](https://github.com/PyO3/pyo3/pull/2875) - -### Added - -- Add `GILOnceCell::get_or_try_init` for fallible `GILOnceCell` initialization. [#2398](https://github.com/PyO3/pyo3/pull/2398) -- Add experimental feature `experimental-inspect` with `type_input()` and `type_output()` helpers to get the Python type of any Python-compatible object. [#2490](https://github.com/PyO3/pyo3/pull/2490) [#2882](https://github.com/PyO3/pyo3/pull/2882) -- The `#[pyclass]` macro can now take `get_all` and `set_all` to create getters and setters for every field. [#2692](https://github.com/PyO3/pyo3/pull/2692) -- Add `#[pyo3(signature = (...))]` option for `#[pyfunction]` and `#[pymethods]`. [#2702](https://github.com/PyO3/pyo3/pull/2702) -- `pyo3-build-config`: rebuild when `PYO3_ENVIRONMENT_SIGNATURE` environment variable value changes. [#2727](https://github.com/PyO3/pyo3/pull/2727) -- Add conversions between non-zero int types in `std::num` and Python `int`. [#2730](https://github.com/PyO3/pyo3/pull/2730) -- Add `Py::downcast()` as a companion to `PyAny::downcast()`, as well as `downcast_unchecked()` for both types. [#2734](https://github.com/PyO3/pyo3/pull/2734) -- Add types for all built-in `Warning` classes as well as `PyErr::warn_explicit`. [#2742](https://github.com/PyO3/pyo3/pull/2742) -- Add `abi3-py311` feature. [#2776](https://github.com/PyO3/pyo3/pull/2776) -- Add FFI definition `_PyErr_ChainExceptions()` for CPython. [#2788](https://github.com/PyO3/pyo3/pull/2788) -- Add FFI definitions `PyVectorcall_NARGS` and `PY_VECTORCALL_ARGUMENTS_OFFSET` for PyPy 3.8 and up. [#2811](https://github.com/PyO3/pyo3/pull/2811) -- Add `PyList::get_item_unchecked` for PyPy. [#2827](https://github.com/PyO3/pyo3/pull/2827) - -### Changed - -- PyO3's macros now emit a much nicer error message if function return values don't implement the required trait(s). [#2664](https://github.com/PyO3/pyo3/pull/2664) -- Use a TypeError, rather than a ValueError, when refusing to treat a str as a Vec. [#2685](https://github.com/PyO3/pyo3/pull/2685) -- Change `PyCFunction::new_closure` to take `name` and `doc` arguments. [#2686](https://github.com/PyO3/pyo3/pull/2686) -- `PyType::is_subclass`, `PyErr::is_instance` and `PyAny::is_instance` now take `&PyAny` instead of `&PyType` arguments, so that they work with objects that pretend to be types using `__subclasscheck__` and `__instancecheck__`. [#2695](https://github.com/PyO3/pyo3/pull/2695) -- Deprecate `#[args]` attribute and passing "args" specification directly to `#[pyfunction]` in favor of the new `#[pyo3(signature = (...))]` option. [#2702](https://github.com/PyO3/pyo3/pull/2702) -- Deprecate required arguments after `Option` arguments to `#[pyfunction]` and `#[pymethods]` without also using `#[pyo3(signature)]` to specify whether the arguments should be required or have defaults. [#2703](https://github.com/PyO3/pyo3/pull/2703) -- Change `#[pyfunction]` and `#[pymethods]` to use a common call "trampoline" to slightly reduce generated code size and compile times. [#2705](https://github.com/PyO3/pyo3/pull/2705) -- `PyAny::cast_as()` and `Py::cast_as()` are now deprecated in favor of `PyAny::downcast()` and the new `Py::downcast()`. [#2734](https://github.com/PyO3/pyo3/pull/2734) -- Relax lifetime bounds on `PyAny::downcast()`. [#2734](https://github.com/PyO3/pyo3/pull/2734) -- Automatically generate `__text_signature__` for all Python functions created using `#[pyfunction]` and `#[pymethods]`. [#2784](https://github.com/PyO3/pyo3/pull/2784) -- Accept any iterator in `PySet::new` and `PyFrozenSet::new`. [#2795](https://github.com/PyO3/pyo3/pull/2795) -- Mixing `#[cfg(...)]` and `#[pyo3(...)]` attributes on `#[pyclass]` struct fields will now work. [#2796](https://github.com/PyO3/pyo3/pull/2796) -- Re-enable `PyFunction` on when building for abi3 or PyPy. [#2838](https://github.com/PyO3/pyo3/pull/2838) -- Improve `derive(FromPyObject)` to use `intern!` when applicable for `#[pyo3(item)]`. [#2879](https://github.com/PyO3/pyo3/pull/2879) - -### Removed - -- Remove the deprecated `pyproto` feature, `#[pyproto]` macro, and all accompanying APIs. [#2587](https://github.com/PyO3/pyo3/pull/2587) -- Remove all functionality deprecated in PyO3 0.16. [#2843](https://github.com/PyO3/pyo3/pull/2843) - -### Fixed - -- Disable `PyModule::filename` on PyPy. [#2715](https://github.com/PyO3/pyo3/pull/2715) -- `PyCodeObject` is now once again defined with fields on Python 3.7. [#2726](https://github.com/PyO3/pyo3/pull/2726) -- Raise a `TypeError` if `#[new]` pymethods with no arguments receive arguments when called from Python. [#2749](https://github.com/PyO3/pyo3/pull/2749) -- Use the `NOARGS` argument calling convention for methods that have a single `py: Python` argument (as a performance optimization). [#2760](https://github.com/PyO3/pyo3/pull/2760) -- Fix truncation of `isize` values to `c_long` in `PySlice::new`. [#2769](https://github.com/PyO3/pyo3/pull/2769) -- Fix soundness issue with FFI definition `PyUnicodeDecodeError_Create` on PyPy leading to indeterminate behavior (typically a `TypeError`). [#2772](https://github.com/PyO3/pyo3/pull/2772) -- Allow functions taking `**kwargs` to accept keyword arguments which share a name with a positional-only argument (as permitted by PEP 570). [#2800](https://github.com/PyO3/pyo3/pull/2800) -- Fix unresolved symbol for `PyObject_Vectorcall` on PyPy 3.9 and up. [#2811](https://github.com/PyO3/pyo3/pull/2811) -- Fix memory leak in `PyCFunction::new_closure`. [#2842](https://github.com/PyO3/pyo3/pull/2842) - - -## [0.17.3] - 2022-11-01 - -### Packaging - -- Support Python 3.11. (Previous versions of PyO3 0.17 have been tested against Python 3.11 release candidates and are expected to be compatible, this is the first version tested against Python 3.11.0.) [#2708](https://github.com/PyO3/pyo3/pull/2708) - -### Added - -- Implemented `ExactSizeIterator` for `PyListIterator`, `PyDictIterator`, `PySetIterator` and `PyFrozenSetIterator`. [#2676](https://github.com/PyO3/pyo3/pull/2676) - -### Fixed - -- Fix regression of `impl FromPyObject for [T; N]` no longer accepting types passing `PySequence_Check`, e.g. NumPy arrays, since version 0.17.0. This the same fix that was applied `impl FromPyObject for Vec` in version 0.17.1 extended to fixed-size arrays. [#2675](https://github.com/PyO3/pyo3/pull/2675) -- Fix UB in `FunctionDescription::extract_arguments_fastcall` due to creating slices from a null pointer. [#2687](https://github.com/PyO3/pyo3/pull/2687) - - -## [0.17.2] - 2022-10-04 - -### Packaging - -- Added optional `chrono` feature to convert `chrono` types into types in the `datetime` module. [#2612](https://github.com/PyO3/pyo3/pull/2612) - -### Added - -- Add support for `num-bigint` feature on `PyPy`. [#2626](https://github.com/PyO3/pyo3/pull/2626) - -### Fixed - -- Correctly implement `__richcmp__` for enums, fixing `__ne__` returning always returning `True`. [#2622](https://github.com/PyO3/pyo3/pull/2622) -- Fix compile error since 0.17.0 with `Option<&SomePyClass>` argument with a default. [#2630](https://github.com/PyO3/pyo3/pull/2630) -- Fix regression of `impl FromPyObject for Vec` no longer accepting types passing `PySequence_Check`, e.g. NumPy arrays, since 0.17.0. [#2631](https://github.com/PyO3/pyo3/pull/2631) - -## [0.17.1] - 2022-08-28 - -### Fixed - -- Fix visibility of `PyDictItems`, `PyDictKeys`, and `PyDictValues` types added in PyO3 0.17.0. -- Fix compile failure when using `#[pyo3(from_py_with = "...")]` attribute on an argument of type `Option`. [#2592](https://github.com/PyO3/pyo3/pull/2592) -- Fix clippy `redundant-closure` lint on `**kwargs` arguments for `#[pyfunction]` and `#[pymethods]`. [#2595](https://github.com/PyO3/pyo3/pull/2595) - -## [0.17.0] - 2022-08-23 - -### Packaging - -- Update inventory dependency to `0.3` (the `multiple-pymethods` feature now requires Rust 1.62 for correctness). [#2492](https://github.com/PyO3/pyo3/pull/2492) - -### Added - -- Add `timezone_utc`. [#1588](https://github.com/PyO3/pyo3/pull/1588) -- Implement `ToPyObject` for `[T; N]`. [#2313](https://github.com/PyO3/pyo3/pull/2313) -- Add `PyDictKeys`, `PyDictValues` and `PyDictItems` Rust types. [#2358](https://github.com/PyO3/pyo3/pull/2358) -- Add `append_to_inittab`. [#2377](https://github.com/PyO3/pyo3/pull/2377) -- Add FFI definition `PyFrame_GetCode`. [#2406](https://github.com/PyO3/pyo3/pull/2406) -- Add `PyCode` and `PyFrame` high level objects. [#2408](https://github.com/PyO3/pyo3/pull/2408) -- Add FFI definitions `Py_fstring_input`, `sendfunc`, and `_PyErr_StackItem`. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Add `PyDateTime::new_with_fold`, `PyTime::new_with_fold`, `PyTime::get_fold`, and `PyDateTime::get_fold` for PyPy. [#2428](https://github.com/PyO3/pyo3/pull/2428) -- Add `#[pyclass(frozen)]`. [#2448](https://github.com/PyO3/pyo3/pull/2448) -- Accept `#[pyo3(name)]` on enum variants. [#2457](https://github.com/PyO3/pyo3/pull/2457) -- Add `CompareOp::matches` to implement `__richcmp__` as the result of a Rust `std::cmp::Ordering` comparison. [#2460](https://github.com/PyO3/pyo3/pull/2460) -- Add `PySuper` type. [#2486](https://github.com/PyO3/pyo3/pull/2486) -- Support PyPy on Windows with the `generate-import-lib` feature. [#2506](https://github.com/PyO3/pyo3/pull/2506) -- Add FFI definitions `Py_EnterRecursiveCall` and `Py_LeaveRecursiveCall`. [#2511](https://github.com/PyO3/pyo3/pull/2511) -- Add `PyDict::get_item_with_error`. [#2536](https://github.com/PyO3/pyo3/pull/2536) -- Add `#[pyclass(sequence)]` option. [#2567](https://github.com/PyO3/pyo3/pull/2567) - -### Changed - -- Change datetime constructors taking a `tzinfo` to take `Option<&PyTzInfo>` instead of `Option<&PyObject>`: `PyDateTime::new`, `PyDateTime::new_with_fold`, `PyTime::new`, and `PyTime::new_with_fold`. [#1588](https://github.com/PyO3/pyo3/pull/1588) -- Move `PyTypeObject::type_object` method to the `PyTypeInfo` trait, and deprecate the `PyTypeObject` trait. [#2287](https://github.com/PyO3/pyo3/pull/2287) -- Methods of `Py` and `PyAny` now accept `impl IntoPy>` rather than just `&str` to allow use of the `intern!` macro. [#2312](https://github.com/PyO3/pyo3/pull/2312) -- Change the deprecated `pyproto` feature to be opt-in instead of opt-out. [#2322](https://github.com/PyO3/pyo3/pull/2322) -- Emit better error messages when `#[pyfunction]` return types do not implement `IntoPy`. [#2326](https://github.com/PyO3/pyo3/pull/2326) -- Require `T: IntoPy` for `impl IntoPy for [T; N]` instead of `T: ToPyObject`. [#2326](https://github.com/PyO3/pyo3/pull/2326) -- Deprecate the `ToBorrowedObject` trait. [#2333](https://github.com/PyO3/pyo3/pull/2333) -- Iterators over `PySet` and `PyDict` will now panic if the underlying collection is mutated during the iteration. [#2380](https://github.com/PyO3/pyo3/pull/2380) -- Iterators over `PySet` and `PyDict` will now panic if the underlying collection is mutated during the iteration. [#2380](https://github.com/PyO3/pyo3/pull/2380) -- Allow `#[classattr]` methods to be fallible. [#2385](https://github.com/PyO3/pyo3/pull/2385) -- Prevent multiple `#[pymethods]` with the same name for a single `#[pyclass]`. [#2399](https://github.com/PyO3/pyo3/pull/2399) -- Fixup `lib_name` when using `PYO3_CONFIG_FILE`. [#2404](https://github.com/PyO3/pyo3/pull/2404) -- Add a message to the `ValueError` raised by the `#[derive(FromPyObject)]` implementation for a tuple struct. [#2414](https://github.com/PyO3/pyo3/pull/2414) -- Allow `#[classattr]` methods to take `Python` argument. [#2456](https://github.com/PyO3/pyo3/pull/2456) -- Rework `PyCapsule` type to resolve soundness issues: [#2485](https://github.com/PyO3/pyo3/pull/2485) - - `PyCapsule::new` and `PyCapsule::new_with_destructor` now take `name: Option` instead of `&CStr`. - - The destructor `F` in `PyCapsule::new_with_destructor` must now be `Send`. - - `PyCapsule::get_context` deprecated in favor of `PyCapsule::context` which doesn't take a `py: Python<'_>` argument. - - `PyCapsule::set_context` no longer takes a `py: Python<'_>` argument. - - `PyCapsule::name` now returns `PyResult>` instead of `&CStr`. -- `FromPyObject::extract` for `Vec` no longer accepts Python `str` inputs. [#2500](https://github.com/PyO3/pyo3/pull/2500) -- Ensure each `#[pymodule]` is only initialized once. [#2523](https://github.com/PyO3/pyo3/pull/2523) -- `pyo3_build_config::add_extension_module_link_args` now also emits linker arguments for `wasm32-unknown-emscripten`. [#2538](https://github.com/PyO3/pyo3/pull/2538) -- Type checks for `PySequence` and `PyMapping` now require inputs to inherit from (or register with) `collections.abc.Sequence` and `collections.abc.Mapping` respectively. [#2477](https://github.com/PyO3/pyo3/pull/2477) -- Disable `PyFunction` on when building for abi3 or PyPy. [#2542](https://github.com/PyO3/pyo3/pull/2542) -- Deprecate `Python::acquire_gil`. [#2549](https://github.com/PyO3/pyo3/pull/2549) - -### Removed - -- Remove all functionality deprecated in PyO3 0.15. [#2283](https://github.com/PyO3/pyo3/pull/2283) -- Make the `Dict`, `WeakRef` and `BaseNativeType` members of the `PyClass` private implementation details. [#2572](https://github.com/PyO3/pyo3/pull/2572) - -### Fixed - -- Enable incorrectly disabled FFI definition `PyThreadState_DeleteCurrent`. [#2357](https://github.com/PyO3/pyo3/pull/2357) -- Fix `wrap_pymodule` interactions with name resolution rules: it no longer "sees through" glob imports of `use submodule::*` when `submodule::submodule` is a `#[pymodule]`. [#2363](https://github.com/PyO3/pyo3/pull/2363) -- Correct FFI definition `PyEval_EvalCodeEx` to take `*const *mut PyObject` array arguments instead of `*mut *mut PyObject`. [#2368](https://github.com/PyO3/pyo3/pull/2368) -- Fix "raw-ident" structs (e.g. `#[pyclass] struct r#RawName`) incorrectly having `r#` at the start of the class name created in Python. [#2395](https://github.com/PyO3/pyo3/pull/2395) -- Correct FFI definition `Py_tracefunc` to be `unsafe extern "C" fn` (was previously safe). [#2407](https://github.com/PyO3/pyo3/pull/2407) -- Fix compile failure with `#[pyo3(from_py_with = "...")]` annotations on a field in a `#[derive(FromPyObject)]` struct. [#2414](https://github.com/PyO3/pyo3/pull/2414) -- Fix FFI definitions `_PyDateTime_BaseTime` and `_PyDateTime_BaseDateTime` lacking leading underscores in their names. [#2421](https://github.com/PyO3/pyo3/pull/2421) -- Remove FFI definition `PyArena` on Python 3.10 and up. [#2421](https://github.com/PyO3/pyo3/pull/2421) -- Fix FFI definition `PyCompilerFlags` missing member `cf_feature_version` on Python 3.8 and up. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Fix FFI definition `PyAsyncMethods` missing member `am_send` on Python 3.10 and up. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Fix FFI definition `PyGenObject` having multiple incorrect members on various Python versions. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Fix FFI definition `PySyntaxErrorObject` missing members `end_lineno` and `end_offset` on Python 3.10 and up. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Fix FFI definition `PyHeapTypeObject` missing member `ht_module` on Python 3.9 and up. [#2423](https://github.com/PyO3/pyo3/pull/2423) -- Fix FFI definition `PyFrameObject` having multiple incorrect members on various Python versions. [#2424](https://github.com/PyO3/pyo3/pull/2424) [#2434](https://github.com/PyO3/pyo3/pull/2434) -- Fix FFI definition `PyTypeObject` missing deprecated field `tp_print` on Python 3.8. [#2428](https://github.com/PyO3/pyo3/pull/2428) -- Fix FFI definitions `PyDateTime_CAPI`. `PyDateTime_Date`, `PyASCIIObject`, `PyBaseExceptionObject`, `PyListObject`, and `PyTypeObject` on PyPy. [#2428](https://github.com/PyO3/pyo3/pull/2428) -- Fix FFI definition `_inittab` field `initfunc` typo'd as `initfun`. [#2431](https://github.com/PyO3/pyo3/pull/2431) -- Fix FFI definitions `_PyDateTime_BaseTime` and `_PyDateTime_BaseDateTime` incorrectly having `fold` member. [#2432](https://github.com/PyO3/pyo3/pull/2432) -- Fix FFI definitions `PyTypeObject`. `PyHeapTypeObject`, and `PyCFunctionObject` having incorrect members on PyPy 3.9. [#2433](https://github.com/PyO3/pyo3/pull/2433) -- Fix FFI definition `PyGetSetDef` to have `*const c_char` for `doc` member (not `*mut c_char`). [#2439](https://github.com/PyO3/pyo3/pull/2439) -- Fix `#[pyo3(from_py_with = "...")]` being ignored for 1-element tuple structs and transparent structs. [#2440](https://github.com/PyO3/pyo3/pull/2440) -- Use `memoffset` to avoid UB when computing `PyCell` layout. [#2450](https://github.com/PyO3/pyo3/pull/2450) -- Fix incorrect enum names being returned by the generated `repr` for enums renamed by `#[pyclass(name = "...")]` [#2457](https://github.com/PyO3/pyo3/pull/2457) -- Fix `PyObject_CallNoArgs` incorrectly being available when building for abi3 on Python 3.9. [#2476](https://github.com/PyO3/pyo3/pull/2476) -- Fix several clippy warnings generated by `#[pyfunction]` arguments. [#2503](https://github.com/PyO3/pyo3/pull/2503) - -## [0.16.6] - 2022-08-23 - -### Changed - -- Fix soundness issues with `PyCapsule` type with select workarounds. Users are encourage to upgrade to PyO3 0.17 at their earliest convenience which contains API breakages which fix the issues in a long-term fashion. [#2522](https://github.com/PyO3/pyo3/pull/2522) - - `PyCapsule::new` and `PyCapsule::new_with_destructor` now take ownership of a copy of the `name` to resolve a possible use-after-free. - - `PyCapsule::name` now returns an empty `CStr` instead of dereferencing a null pointer if the capsule has no name. - - The destructor `F` in `PyCapsule::new_with_destructor` will never be called if the capsule is deleted from a thread other than the one which the capsule was created in (a warning will be emitted). -- Panics during drop of panic payload caught by PyO3 will now abort. [#2544](https://github.com/PyO3/pyo3/pull/2544) - -## [0.16.5] - 2022-05-15 - -### Added - -- Add an experimental `generate-import-lib` feature to support auto-generating non-abi3 python import libraries for Windows targets. [#2364](https://github.com/PyO3/pyo3/pull/2364) -- Add FFI definition `Py_ExitStatusException`. [#2374](https://github.com/PyO3/pyo3/pull/2374) - -### Changed - -- Deprecate experimental `generate-abi3-import-lib` feature in favor of the new `generate-import-lib` feature. [#2364](https://github.com/PyO3/pyo3/pull/2364) - -### Fixed - -- Added missing `warn_default_encoding` field to `PyConfig` on 3.10+. The previously missing field could result in incorrect behavior or crashes. [#2370](https://github.com/PyO3/pyo3/pull/2370) -- Fixed order of `pathconfig_warnings` and `program_name` fields of `PyConfig` on 3.10+. Previously, the order of the fields was swapped and this could lead to incorrect behavior or crashes. [#2370](https://github.com/PyO3/pyo3/pull/2370) - -## [0.16.4] - 2022-04-14 - -### Added - -- Add `PyTzInfoAccess` trait for safe access to time zone information. [#2263](https://github.com/PyO3/pyo3/pull/2263) -- Add an experimental `generate-abi3-import-lib` feature to auto-generate `python3.dll` import libraries for Windows. [#2282](https://github.com/PyO3/pyo3/pull/2282) -- Add FFI definitions for `PyDateTime_BaseTime` and `PyDateTime_BaseDateTime`. [#2294](https://github.com/PyO3/pyo3/pull/2294) - -### Changed - -- Improved performance of failing calls to `FromPyObject::extract` which is common when functions accept multiple distinct types. [#2279](https://github.com/PyO3/pyo3/pull/2279) -- Default to "m" ABI tag when choosing `libpython` link name for CPython 3.7 on Unix. [#2288](https://github.com/PyO3/pyo3/pull/2288) -- Allow to compile "abi3" extensions without a working build host Python interpreter. [#2293](https://github.com/PyO3/pyo3/pull/2293) - -### Fixed - -- Crates depending on PyO3 can collect code coverage via LLVM instrumentation using stable Rust. [#2286](https://github.com/PyO3/pyo3/pull/2286) -- Fix segfault when calling FFI methods `PyDateTime_DATE_GET_TZINFO` or `PyDateTime_TIME_GET_TZINFO` on `datetime` or `time` without a tzinfo. [#2289](https://github.com/PyO3/pyo3/pull/2289) -- Fix directory names starting with the letter `n` breaking serialization of the interpreter configuration on Windows since PyO3 0.16.3. [#2299](https://github.com/PyO3/pyo3/pull/2299) - -## [0.16.3] - 2022-04-05 - -### Packaging - -- Extend `parking_lot` dependency supported versions to include 0.12. [#2239](https://github.com/PyO3/pyo3/pull/2239) - -### Added - -- Add methods to `pyo3_build_config::InterpreterConfig` to run Python scripts using the configured executable. [#2092](https://github.com/PyO3/pyo3/pull/2092) -- Add `as_bytes` method to `Py`. [#2235](https://github.com/PyO3/pyo3/pull/2235) -- Add FFI definitions for `PyType_FromModuleAndSpec`, `PyType_GetModule`, `PyType_GetModuleState` and `PyModule_AddType`. [#2250](https://github.com/PyO3/pyo3/pull/2250) -- Add `pyo3_build_config::cross_compiling_from_to` as a helper to detect when PyO3 is cross-compiling. [#2253](https://github.com/PyO3/pyo3/pull/2253) -- Add `#[pyclass(mapping)]` option to leave sequence slots empty in container implementations. [#2265](https://github.com/PyO3/pyo3/pull/2265) -- Add `PyString::intern` to enable usage of the Python's built-in string interning. [#2268](https://github.com/PyO3/pyo3/pull/2268) -- Add `intern!` macro which can be used to amortize the cost of creating Python strings by storing them inside a `GILOnceCell`. [#2269](https://github.com/PyO3/pyo3/pull/2269) -- Add `PYO3_CROSS_PYTHON_IMPLEMENTATION` environment variable for selecting the default cross Python implementation. [#2272](https://github.com/PyO3/pyo3/pull/2272) - -### Changed - -- Allow `#[pyo3(crate = "...", text_signature = "...")]` options to be used directly in `#[pyclass(crate = "...", text_signature = "...")]`. [#2234](https://github.com/PyO3/pyo3/pull/2234) -- Make `PYO3_CROSS_LIB_DIR` environment variable optional when cross compiling. [#2241](https://github.com/PyO3/pyo3/pull/2241) -- Mark `METH_FASTCALL` calling convention as limited API on Python 3.10. [#2250](https://github.com/PyO3/pyo3/pull/2250) -- Deprecate `pyo3_build_config::cross_compiling` in favor of `pyo3_build_config::cross_compiling_from_to`. [#2253](https://github.com/PyO3/pyo3/pull/2253) - -### Fixed - -- Fix `abi3-py310` feature: use Python 3.10 ABI when available instead of silently falling back to the 3.9 ABI. [#2242](https://github.com/PyO3/pyo3/pull/2242) -- Use shared linking mode when cross compiling against a [Framework bundle](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html) for macOS. [#2233](https://github.com/PyO3/pyo3/pull/2233) -- Fix panic during compilation when `PYO3_CROSS_LIB_DIR` is set for some host/target combinations. [#2232](https://github.com/PyO3/pyo3/pull/2232) -- Correct dependency version for `syn` to require minimal patch version 1.0.56. [#2240](https://github.com/PyO3/pyo3/pull/2240) - -## [0.16.2] - 2022-03-15 - -### Packaging - -- Warn when modules are imported on PyPy 3.7 versions older than PyPy 7.3.8, as they are known to have binary compatibility issues. [#2217](https://github.com/PyO3/pyo3/pull/2217) -- Ensure build script of `pyo3-ffi` runs before that of `pyo3` to fix cross compilation. [#2224](https://github.com/PyO3/pyo3/pull/2224) - -## [0.16.1] - 2022-03-05 - -### Packaging - -- Extend `hashbrown` optional dependency supported versions to include 0.12. [#2197](https://github.com/PyO3/pyo3/pull/2197) - -### Fixed - -- Fix incorrect platform detection for Windows in `pyo3-build-config`. [#2198](https://github.com/PyO3/pyo3/pull/2198) -- Fix regression from 0.16 preventing cross compiling to aarch64 macOS. [#2201](https://github.com/PyO3/pyo3/pull/2201) - -## [0.16.0] - 2022-02-27 - -### Packaging - -- Update MSRV to Rust 1.48. [#2004](https://github.com/PyO3/pyo3/pull/2004) -- Update `indoc` optional dependency to 1.0. [#2004](https://github.com/PyO3/pyo3/pull/2004) -- Drop support for Python 3.6, remove `abi3-py36` feature. [#2006](https://github.com/PyO3/pyo3/pull/2006) -- `pyo3-build-config` no longer enables the `resolve-config` feature by default. [#2008](https://github.com/PyO3/pyo3/pull/2008) -- Update `inventory` optional dependency to 0.2. [#2019](https://github.com/PyO3/pyo3/pull/2019) -- Drop `paste` dependency. [#2081](https://github.com/PyO3/pyo3/pull/2081) -- The bindings found in `pyo3::ffi` are now a re-export of a separate `pyo3-ffi` crate. [#2126](https://github.com/PyO3/pyo3/pull/2126) -- Support PyPy 3.9. [#2143](https://github.com/PyO3/pyo3/pull/2143) - -### Added - -- Add `PyCapsule` type exposing the [Capsule API](https://docs.python.org/3/c-api/capsule.html#capsules). [#1980](https://github.com/PyO3/pyo3/pull/1980) -- Add `pyo3_build_config::Sysconfigdata` and supporting APIs. [#1996](https://github.com/PyO3/pyo3/pull/1996) -- Add `Py::setattr` method. [#2009](https://github.com/PyO3/pyo3/pull/2009) -- Add `#[pyo3(crate = "some::path")]` option to all attribute macros (except the deprecated `#[pyproto]`). [#2022](https://github.com/PyO3/pyo3/pull/2022) -- Enable `create_exception!` macro to take an optional docstring. [#2027](https://github.com/PyO3/pyo3/pull/2027) -- Enable `#[pyclass]` for fieldless (aka C-like) enums. [#2034](https://github.com/PyO3/pyo3/pull/2034) -- Add buffer magic methods `__getbuffer__` and `__releasebuffer__` to `#[pymethods]`. [#2067](https://github.com/PyO3/pyo3/pull/2067) -- Add support for paths in `wrap_pyfunction` and `wrap_pymodule`. [#2081](https://github.com/PyO3/pyo3/pull/2081) -- Enable `wrap_pyfunction!` to wrap a `#[pyfunction]` implemented in a different Rust module or crate. [#2091](https://github.com/PyO3/pyo3/pull/2091) -- Add `PyAny::contains` method (`in` operator for `PyAny`). [#2115](https://github.com/PyO3/pyo3/pull/2115) -- Add `PyMapping::contains` method (`in` operator for `PyMapping`). [#2133](https://github.com/PyO3/pyo3/pull/2133) -- Add garbage collection magic magic methods `__traverse__` and `__clear__` to `#[pymethods]`. [#2159](https://github.com/PyO3/pyo3/pull/2159) -- Add support for `from_py_with` on struct tuples and enums to override the default from-Python conversion. [#2181](https://github.com/PyO3/pyo3/pull/2181) -- Add `eq`, `ne`, `lt`, `le`, `gt`, `ge` methods to `PyAny` that wrap `rich_compare`. [#2175](https://github.com/PyO3/pyo3/pull/2175) -- Add `Py::is` and `PyAny::is` methods to check for object identity. [#2183](https://github.com/PyO3/pyo3/pull/2183) -- Add support for the `__getattribute__` magic method. [#2187](https://github.com/PyO3/pyo3/pull/2187) - -### Changed - -- `PyType::is_subclass`, `PyErr::is_instance` and `PyAny::is_instance` now operate run-time type object instead of a type known at compile-time. The old behavior is still available as `PyType::is_subclass_of`, `PyErr::is_instance_of` and `PyAny::is_instance_of`. [#1985](https://github.com/PyO3/pyo3/pull/1985) -- Rename some methods on `PyErr` (the old names are just marked deprecated for now): [#2026](https://github.com/PyO3/pyo3/pull/2026) - - `pytype` -> `get_type` - - `pvalue` -> `value` (and deprecate equivalent `instance`) - - `ptraceback` -> `traceback` - - `from_instance` -> `from_value` - - `into_instance` -> `into_value` -- `PyErr::new_type` now takes an optional docstring and now returns `PyResult>` rather than a `ffi::PyTypeObject` pointer. [#2027](https://github.com/PyO3/pyo3/pull/2027) -- Deprecate `PyType::is_instance`; it is inconsistent with other `is_instance` methods in PyO3. Instead of `typ.is_instance(obj)`, use `obj.is_instance(typ)`. [#2031](https://github.com/PyO3/pyo3/pull/2031) -- `__getitem__`, `__setitem__` and `__delitem__` in `#[pymethods]` now implement both a Python mapping and sequence by default. [#2065](https://github.com/PyO3/pyo3/pull/2065) -- Improve performance and error messages for `#[derive(FromPyObject)]` for enums. [#2068](https://github.com/PyO3/pyo3/pull/2068) -- Reduce generated LLVM code size (to improve compile times) for: - - internal `handle_panic` helper [#2074](https://github.com/PyO3/pyo3/pull/2074) [#2158](https://github.com/PyO3/pyo3/pull/2158) - - `#[pyfunction]` and `#[pymethods]` argument extraction [#2075](https://github.com/PyO3/pyo3/pull/2075) [#2085](https://github.com/PyO3/pyo3/pull/2085) - - `#[pyclass]` type object creation [#2076](https://github.com/PyO3/pyo3/pull/2076) [#2081](https://github.com/PyO3/pyo3/pull/2081) [#2157](https://github.com/PyO3/pyo3/pull/2157) -- Respect Rust privacy rules for items wrapped with `wrap_pyfunction` and `wrap_pymodule`. [#2081](https://github.com/PyO3/pyo3/pull/2081) -- Add modulo argument to `__ipow__` magic method. [#2083](https://github.com/PyO3/pyo3/pull/2083) -- Fix FFI definition for `_PyCFunctionFast`. [#2126](https://github.com/PyO3/pyo3/pull/2126) -- `PyDateTimeAPI` and `PyDateTime_TimeZone_UTC` are now unsafe functions instead of statics. [#2126](https://github.com/PyO3/pyo3/pull/2126) -- `PyDateTimeAPI` does not implicitly call `PyDateTime_IMPORT` anymore to reflect the original Python API more closely. Before the first call to `PyDateTime_IMPORT` a null pointer is returned. Therefore before calling any of the following FFI functions `PyDateTime_IMPORT` must be called to avoid undefined behavior: [#2126](https://github.com/PyO3/pyo3/pull/2126) - - `PyDateTime_TimeZone_UTC` - - `PyDate_Check` - - `PyDate_CheckExact` - - `PyDateTime_Check` - - `PyDateTime_CheckExact` - - `PyTime_Check` - - `PyTime_CheckExact` - - `PyDelta_Check` - - `PyDelta_CheckExact` - - `PyTZInfo_Check` - - `PyTZInfo_CheckExact` - - `PyDateTime_FromTimestamp` - - `PyDate_FromTimestamp` -- Deprecate the `gc` option for `pyclass` (e.g. `#[pyclass(gc)]`). Just implement a `__traverse__` `#[pymethod]`. [#2159](https://github.com/PyO3/pyo3/pull/2159) -- The `ml_meth` field of `PyMethodDef` is now represented by the `PyMethodDefPointer` union. [2166](https://github.com/PyO3/pyo3/pull/2166) -- Deprecate the `#[pyproto]` traits. [#2173](https://github.com/PyO3/pyo3/pull/2173) - -### Removed - -- Remove all functionality deprecated in PyO3 0.14. [#2007](https://github.com/PyO3/pyo3/pull/2007) -- Remove `Default` impl for `PyMethodDef`. [#2166](https://github.com/PyO3/pyo3/pull/2166) -- Remove `PartialEq` impl for `Py` and `PyAny` (use the new `is` instead). [#2183](https://github.com/PyO3/pyo3/pull/2183) - -### Fixed - -- Fix undefined symbol for `PyObject_HasAttr` on PyPy. [#2025](https://github.com/PyO3/pyo3/pull/2025) -- Fix memory leak in `PyErr::into_value`. [#2026](https://github.com/PyO3/pyo3/pull/2026) -- Fix clippy warning `needless-option-as-deref` in code generated by `#[pyfunction]` and `#[pymethods]`. [#2040](https://github.com/PyO3/pyo3/pull/2040) -- Fix undefined behavior in `PySlice::indices`. [#2061](https://github.com/PyO3/pyo3/pull/2061) -- Fix the `wrap_pymodule!` macro using the wrong name for a `#[pymodule]` with a `#[pyo3(name = "..")]` attribute. [#2081](https://github.com/PyO3/pyo3/pull/2081) -- Fix magic methods in `#[pymethods]` accepting implementations with the wrong number of arguments. [#2083](https://github.com/PyO3/pyo3/pull/2083) -- Fix panic in `#[pyfunction]` generated code when a required argument following an `Option` was not provided. [#2093](https://github.com/PyO3/pyo3/pull/2093) -- Fixed undefined behavior caused by incorrect `ExactSizeIterator` implementations. [#2124](https://github.com/PyO3/pyo3/pull/2124) -- Fix missing FFI definition `PyCMethod_New` on Python 3.9 and up. [#2143](https://github.com/PyO3/pyo3/pull/2143) -- Add missing FFI definitions `_PyLong_NumBits` and `_PyLong_AsByteArray` on PyPy. [#2146](https://github.com/PyO3/pyo3/pull/2146) -- Fix memory leak in implementation of `AsPyPointer` for `Option`. [#2160](https://github.com/PyO3/pyo3/pull/2160) -- Fix FFI definition of `_PyLong_NumBits` to return `size_t` instead of `c_int`. [#2161](https://github.com/PyO3/pyo3/pull/2161) -- Fix `TypeError` thrown when argument parsing failed missing the originating causes. [2177](https://github.com/PyO3/pyo3/pull/2178) - -## [0.15.2] - 2022-04-14 - -### Packaging - -- Backport of PyPy 3.9 support from PyO3 0.16. [#2262](https://github.com/PyO3/pyo3/pull/2262) - -## [0.15.1] - 2021-11-19 - -### Added - -- Add implementations for `Py::as_ref` and `Py::into_ref` for `Py`, `Py` and `Py`. [#1682](https://github.com/PyO3/pyo3/pull/1682) -- Add `PyTraceback` type to represent and format Python tracebacks. [#1977](https://github.com/PyO3/pyo3/pull/1977) - -### Changed - -- `#[classattr]` constants with a known magic method name (which is lowercase) no longer trigger lint warnings expecting constants to be uppercase. [#1969](https://github.com/PyO3/pyo3/pull/1969) - -### Fixed - -- Fix creating `#[classattr]` by functions with the name of a known magic method. [#1969](https://github.com/PyO3/pyo3/pull/1969) -- Fix use of `catch_unwind` in `allow_threads` which can cause fatal crashes. [#1989](https://github.com/PyO3/pyo3/pull/1989) -- Fix build failure on PyPy when abi3 features are activated. [#1991](https://github.com/PyO3/pyo3/pull/1991) -- Fix mingw platform detection. [#1993](https://github.com/PyO3/pyo3/pull/1993) -- Fix panic in `__get__` implementation when accessing descriptor on type object. [#1997](https://github.com/PyO3/pyo3/pull/1997) - -## [0.15.0] - 2021-11-03 - -### Packaging - -- `pyo3`'s `Cargo.toml` now advertises `links = "python"` to inform Cargo that it links against *libpython*. [#1819](https://github.com/PyO3/pyo3/pull/1819) -- Added optional `anyhow` feature to convert `anyhow::Error` into `PyErr`. [#1822](https://github.com/PyO3/pyo3/pull/1822) -- Support Python 3.10. [#1889](https://github.com/PyO3/pyo3/pull/1889) -- Added optional `eyre` feature to convert `eyre::Report` into `PyErr`. [#1893](https://github.com/PyO3/pyo3/pull/1893) -- Support PyPy 3.8. [#1948](https://github.com/PyO3/pyo3/pull/1948) - -### Added - -- Add `PyList::get_item_unchecked` and `PyTuple::get_item_unchecked` to get items without bounds checks. [#1733](https://github.com/PyO3/pyo3/pull/1733) -- Support `#[doc = include_str!(...)]` attributes on Rust 1.54 and up. [#1746](https://github.com/PyO3/pyo3/issues/1746) -- Add `PyAny::py` as a convenience for `PyNativeType::py`. [#1751](https://github.com/PyO3/pyo3/pull/1751) -- Add implementation of `std::ops::Index` for `PyList`, `PyTuple` and `PySequence`. [#1825](https://github.com/PyO3/pyo3/pull/1825) -- Add range indexing implementations of `std::ops::Index` for `PyList`, `PyTuple` and `PySequence`. [#1829](https://github.com/PyO3/pyo3/pull/1829) -- Add `PyMapping` type to represent the Python mapping protocol. [#1844](https://github.com/PyO3/pyo3/pull/1844) -- Add commonly-used sequence methods to `PyList` and `PyTuple`. [#1849](https://github.com/PyO3/pyo3/pull/1849) -- Add `as_sequence` methods to `PyList` and `PyTuple`. [#1860](https://github.com/PyO3/pyo3/pull/1860) -- Add support for magic methods in `#[pymethods]`, intended as a replacement for `#[pyproto]`. [#1864](https://github.com/PyO3/pyo3/pull/1864) -- Add `abi3-py310` feature. [#1889](https://github.com/PyO3/pyo3/pull/1889) -- Add `PyCFunction::new_closure` to create a Python function from a Rust closure. [#1901](https://github.com/PyO3/pyo3/pull/1901) -- Add support for positional-only arguments in `#[pyfunction]`. [#1925](https://github.com/PyO3/pyo3/pull/1925) -- Add `PyErr::take` to attempt to fetch a Python exception if present. [#1957](https://github.com/PyO3/pyo3/pull/1957) - -### Changed - -- `PyList`, `PyTuple` and `PySequence`'s APIs now accepts only `usize` indices instead of `isize`. - [#1733](https://github.com/PyO3/pyo3/pull/1733), [#1802](https://github.com/PyO3/pyo3/pull/1802), - [#1803](https://github.com/PyO3/pyo3/pull/1803) -- `PyList::get_item` and `PyTuple::get_item` now return `PyResult<&PyAny>` instead of panicking. [#1733](https://github.com/PyO3/pyo3/pull/1733) -- `PySequence::in_place_repeat` and `PySequence::in_place_concat` now return `PyResult<&PySequence>` instead of `PyResult<()>`, which is needed in case of immutable sequences such as tuples. [#1803](https://github.com/PyO3/pyo3/pull/1803) -- `PySequence::get_slice` now returns `PyResult<&PySequence>` instead of `PyResult<&PyAny>`. [#1829](https://github.com/PyO3/pyo3/pull/1829) -- Deprecate `PyTuple::split_from`. [#1804](https://github.com/PyO3/pyo3/pull/1804) -- Deprecate `PyTuple::slice`, new method `PyTuple::get_slice` added with `usize` indices. [#1828](https://github.com/PyO3/pyo3/pull/1828) -- Deprecate FFI definitions `PyParser_SimpleParseStringFlags`, `PyParser_SimpleParseStringFlagsFilename`, `PyParser_SimpleParseFileFlags` when building for Python 3.9. [#1830](https://github.com/PyO3/pyo3/pull/1830) -- Mark FFI definitions removed in Python 3.10 `PyParser_ASTFromString`, `PyParser_ASTFromStringObject`, `PyParser_ASTFromFile`, `PyParser_ASTFromFileObject`, `PyParser_SimpleParseStringFlags`, `PyParser_SimpleParseStringFlagsFilename`, `PyParser_SimpleParseFileFlags`, `PyParser_SimpleParseString`, `PyParser_SimpleParseFile`, `Py_SymtableString`, and `Py_SymtableStringObject`. [#1830](https://github.com/PyO3/pyo3/pull/1830) -- `#[pymethods]` now handles magic methods similarly to `#[pyproto]`. In the future, `#[pyproto]` may be deprecated. [#1864](https://github.com/PyO3/pyo3/pull/1864) -- Deprecate FFI definitions `PySys_AddWarnOption`, `PySys_AddWarnOptionUnicode` and `PySys_HasWarnOptions`. [#1887](https://github.com/PyO3/pyo3/pull/1887) -- Deprecate `#[call]` attribute in favor of using `fn __call__`. [#1929](https://github.com/PyO3/pyo3/pull/1929) -- Fix missing FFI definition `_PyImport_FindExtensionObject` on Python 3.10. [#1942](https://github.com/PyO3/pyo3/pull/1942) -- Change `PyErr::fetch` to panic in debug mode if no exception is present. [#1957](https://github.com/PyO3/pyo3/pull/1957) - -### Fixed - -- Fix building with a conda environment on Windows. [#1873](https://github.com/PyO3/pyo3/pull/1873) -- Fix panic on Python 3.6 when calling `Python::with_gil` with Python initialized but threading not initialized. [#1874](https://github.com/PyO3/pyo3/pull/1874) -- Fix incorrect linking to version-specific DLL instead of `python3.dll` when cross-compiling to Windows with `abi3`. [#1880](https://github.com/PyO3/pyo3/pull/1880) -- Fix FFI definition for `PyTuple_ClearFreeList` incorrectly being present for Python 3.9 and up. [#1887](https://github.com/PyO3/pyo3/pull/1887) -- Fix panic in generated `#[derive(FromPyObject)]` for enums. [#1888](https://github.com/PyO3/pyo3/pull/1888) -- Fix cross-compiling to Python 3.7 builds with the "m" abi flag. [#1908](https://github.com/PyO3/pyo3/pull/1908) -- Fix `__mod__` magic method fallback to `__rmod__`. [#1934](https://github.com/PyO3/pyo3/pull/1934). -- Fix missing FFI definition `_PyImport_FindExtensionObject` on Python 3.10. [#1942](https://github.com/PyO3/pyo3/pull/1942) - -## [0.14.5] - 2021-09-05 - -### Added - -- Make `pyo3_build_config::InterpreterConfig` and subfields public. [#1848](https://github.com/PyO3/pyo3/pull/1848) -- Add `resolve-config` feature to the `pyo3-build-config` to control whether its build script does anything. [#1856](https://github.com/PyO3/pyo3/pull/1856) - -### Fixed - -- Fix 0.14.4 compile regression on `s390x-unknown-linux-gnu` target. [#1850](https://github.com/PyO3/pyo3/pull/1850) - -## [0.14.4] - 2021-08-29 - -### Changed - -- Mark `PyString::data` as `unsafe` and disable it and some supporting PyUnicode FFI APIs (which depend on a C bitfield) on big-endian targets. [#1834](https://github.com/PyO3/pyo3/pull/1834) - -## [0.14.3] - 2021-08-22 - -### Added - -- Add `PyString::data` to access the raw bytes stored in a Python string. [#1794](https://github.com/PyO3/pyo3/pull/1794) - -### Fixed - -- Raise `AttributeError` to avoid panic when calling `del` on a `#[setter]` defined class property. [#1779](https://github.com/PyO3/pyo3/pull/1779) -- Restrict FFI definitions `PyGILState_Check` and `Py_tracefunc` to the unlimited API. [#1787](https://github.com/PyO3/pyo3/pull/1787) -- Add missing `_type` field to `PyStatus` struct definition. [#1791](https://github.com/PyO3/pyo3/pull/1791) -- Reduce lower bound `num-complex` optional dependency to support interop with `rust-numpy` and `ndarray` when building with the MSRV of 1.41 [#1799](https://github.com/PyO3/pyo3/pull/1799) -- Fix memory leak in `Python::run_code`. [#1806](https://github.com/PyO3/pyo3/pull/1806) -- Fix memory leak in `PyModule::from_code`. [#1810](https://github.com/PyO3/pyo3/pull/1810) -- Remove use of `pyo3::` in `pyo3::types::datetime` which broke builds using `-Z avoid-dev-deps` [#1811](https://github.com/PyO3/pyo3/pull/1811) - -## [0.14.2] - 2021-08-09 - -### Added - -- Add `indexmap` feature to add `ToPyObject`, `IntoPy` and `FromPyObject` implementations for `indexmap::IndexMap`. [#1728](https://github.com/PyO3/pyo3/pull/1728) -- Add `pyo3_build_config::add_extension_module_link_args` to use in build scripts to set linker arguments (for macOS). [#1755](https://github.com/PyO3/pyo3/pull/1755) -- Add `Python::with_gil_unchecked` unsafe variation of `Python::with_gil` to allow obtaining a `Python` in scenarios where `Python::with_gil` would fail. [#1769](https://github.com/PyO3/pyo3/pull/1769) - -### Changed - -- `PyErr::new` no longer acquires the Python GIL internally. [#1724](https://github.com/PyO3/pyo3/pull/1724) -- Reverted PyO3 0.14.0's use of `cargo:rustc-cdylib-link-arg` in its build script, as Cargo unintentionally allowed crates to pass linker args to downstream crates in this way. Projects supporting macOS may need to restore `.cargo/config.toml` files. [#1755](https://github.com/PyO3/pyo3/pull/1755) - -### Fixed - -- Fix regression in 0.14.0 rejecting usage of `#[doc(hidden)]` on structs and functions annotated with PyO3 macros. [#1722](https://github.com/PyO3/pyo3/pull/1722) -- Fix regression in 0.14.0 leading to incorrect code coverage being computed for `#[pyfunction]`s. [#1726](https://github.com/PyO3/pyo3/pull/1726) -- Fix incorrect FFI definition of `Py_Buffer` on PyPy. [#1737](https://github.com/PyO3/pyo3/pull/1737) -- Fix incorrect calculation of `dictoffset` on 32-bit Windows. [#1475](https://github.com/PyO3/pyo3/pull/1475) -- Fix regression in 0.13.2 leading to linking to incorrect Python library on Windows "gnu" targets. [#1759](https://github.com/PyO3/pyo3/pull/1759) -- Fix compiler warning: deny trailing semicolons in expression macro. [#1762](https://github.com/PyO3/pyo3/pull/1762) -- Fix incorrect FFI definition of `Py_DecodeLocale`. The 2nd argument is now `*mut Py_ssize_t` instead of `Py_ssize_t`. [#1766](https://github.com/PyO3/pyo3/pull/1766) - -## [0.14.1] - 2021-07-04 - -### Added - -- Implement `IntoPy` for `&PathBuf` and `&OsString`. [#1712](https://github.com/PyO3/pyo3/pull/1712) - -### Fixed - -- Fix crashes on PyPy due to incorrect definitions of `PyList_SET_ITEM`. [#1713](https://github.com/PyO3/pyo3/pull/1713) - -## [0.14.0] - 2021-07-03 - -### Packaging - -- Update `num-bigint` optional dependency to 0.4. [#1481](https://github.com/PyO3/pyo3/pull/1481) -- Update `num-complex` optional dependency to 0.4. [#1482](https://github.com/PyO3/pyo3/pull/1482) -- Extend `hashbrown` optional dependency supported versions to include 0.11. [#1496](https://github.com/PyO3/pyo3/pull/1496) -- Support PyPy 3.7. [#1538](https://github.com/PyO3/pyo3/pull/1538) - -### Added - -- Extend conversions for `[T; N]` to all `N` using const generics (on Rust 1.51 and up). [#1128](https://github.com/PyO3/pyo3/pull/1128) -- Add conversions between `OsStr`/ `OsString` and Python strings. [#1379](https://github.com/PyO3/pyo3/pull/1379) -- Add conversions between `Path`/ `PathBuf` and Python strings (and `pathlib.Path` objects). [#1379](https://github.com/PyO3/pyo3/pull/1379) [#1654](https://github.com/PyO3/pyo3/pull/1654) -- Add a new set of `#[pyo3(...)]` attributes to control various PyO3 macro functionality: - - `#[pyo3(from_py_with = "...")]` function arguments and struct fields to override the default from-Python conversion. [#1411](https://github.com/PyO3/pyo3/pull/1411) - - `#[pyo3(name = "...")]` for setting Python names. [#1567](https://github.com/PyO3/pyo3/pull/1567) - - `#[pyo3(text_signature = "...")]` for setting text signature. [#1658](https://github.com/PyO3/pyo3/pull/1658) -- Add FFI definition `PyCFunction_CheckExact` for Python 3.9 and later. [#1425](https://github.com/PyO3/pyo3/pull/1425) -- Add FFI definition `Py_IS_TYPE`. [#1429](https://github.com/PyO3/pyo3/pull/1429) -- Add FFI definition `_Py_InitializeMain`. [#1473](https://github.com/PyO3/pyo3/pull/1473) -- Add FFI definitions from `cpython/import.h`.[#1475](https://github.com/PyO3/pyo3/pull/1475) -- Add tuple and unit struct support for `#[pyclass]` macro. [#1504](https://github.com/PyO3/pyo3/pull/1504) -- Add FFI definition `PyDateTime_TimeZone_UTC`. [#1572](https://github.com/PyO3/pyo3/pull/1572) -- Add support for `#[pyclass(extends=Exception)]`. [#1591](https://github.com/PyO3/pyo3/pull/1591) -- Add `PyErr::cause` and `PyErr::set_cause`. [#1679](https://github.com/PyO3/pyo3/pull/1679) -- Add FFI definitions from `cpython/pystate.h`. [#1687](https://github.com/PyO3/pyo3/pull/1687/) -- Add `wrap_pyfunction!` macro to `pyo3::prelude`. [#1695](https://github.com/PyO3/pyo3/pull/1695) - -### Changed - -- Allow only one `#[pymethods]` block per `#[pyclass]` by default, to remove the dependency on `inventory`. Add a `multiple-pymethods` feature to opt-in the original behavior and dependency on `inventory`. [#1457](https://github.com/PyO3/pyo3/pull/1457) -- Change `PyTimeAccess::get_fold` to return a `bool` instead of a `u8`. [#1397](https://github.com/PyO3/pyo3/pull/1397) -- Deprecate FFI definition `PyCFunction_Call` for Python 3.9 and up. [#1425](https://github.com/PyO3/pyo3/pull/1425) -- Deprecate FFI definition `PyModule_GetFilename`. [#1425](https://github.com/PyO3/pyo3/pull/1425) -- The `auto-initialize` feature is no longer enabled by default. [#1443](https://github.com/PyO3/pyo3/pull/1443) -- Change `PyCFunction::new` and `PyCFunction::new_with_keywords` to take `&'static str` arguments rather than implicitly copying (and leaking) them. [#1450](https://github.com/PyO3/pyo3/pull/1450) -- Deprecate `PyModule::call`, `PyModule::call0`, `PyModule::call1` and `PyModule::get`. [#1492](https://github.com/PyO3/pyo3/pull/1492) -- Add length information to `PyBufferError`s raised from `PyBuffer::copy_to_slice` and `PyBuffer::copy_from_slice`. [#1534](https://github.com/PyO3/pyo3/pull/1534) -- Automatically set `-undefined` and `dynamic_lookup` linker arguments on macOS with the `extension-module` feature. [#1539](https://github.com/PyO3/pyo3/pull/1539) -- Deprecate `#[pyproto]` methods which are easier to implement as `#[pymethods]`: [#1560](https://github.com/PyO3/pyo3/pull/1560) - - `PyBasicProtocol::__bytes__` and `PyBasicProtocol::__format__` - - `PyContextProtocol::__enter__` and `PyContextProtocol::__exit__` - - `PyDescrProtocol::__delete__` and `PyDescrProtocol::__set_name__` - - `PyMappingProtocol::__reversed__` - - `PyNumberProtocol::__complex__` and `PyNumberProtocol::__round__` - - `PyAsyncProtocol::__aenter__` and `PyAsyncProtocol::__aexit__` -- Deprecate several attributes in favor of the new `#[pyo3(...)]` options: - - `#[name = "..."]`, replaced by `#[pyo3(name = "...")]` [#1567](https://github.com/PyO3/pyo3/pull/1567) - - `#[pyfn(m, "name")]`, replaced by `#[pyfn(m)] #[pyo3(name = "...")]`. [#1610](https://github.com/PyO3/pyo3/pull/1610) - - `#[pymodule(name)]`, replaced by `#[pymodule] #[pyo3(name = "...")]` [#1650](https://github.com/PyO3/pyo3/pull/1650) - - `#[text_signature = "..."]`, replaced by `#[pyo3(text_signature = "...")]`. [#1658](https://github.com/PyO3/pyo3/pull/1658) -- Reduce LLVM line counts to improve compilation times. [#1604](https://github.com/PyO3/pyo3/pull/1604) -- No longer call `PyEval_InitThreads` in `#[pymodule]` init code. [#1630](https://github.com/PyO3/pyo3/pull/1630) -- Use `METH_FASTCALL` argument passing convention, when possible, to improve `#[pyfunction]` and method performance. - [#1619](https://github.com/PyO3/pyo3/pull/1619), [#1660](https://github.com/PyO3/pyo3/pull/1660) -- Filter sysconfigdata candidates by architecture when cross-compiling. [#1626](https://github.com/PyO3/pyo3/pull/1626) - -### Removed - -- Remove deprecated exception names `BaseException` etc. [#1426](https://github.com/PyO3/pyo3/pull/1426) -- Remove deprecated methods `Python::is_instance`, `Python::is_subclass`, `Python::release`, `Python::xdecref`, and `Py::from_owned_ptr_or_panic`. [#1426](https://github.com/PyO3/pyo3/pull/1426) -- Remove many FFI definitions which never existed in the Python C-API: - - (previously deprecated) `PyGetSetDef_INIT`, `PyGetSetDef_DICT`, `PyCoro_Check`, `PyCoroWrapper_Check`, and `PyAsyncGen_Check` [#1426](https://github.com/PyO3/pyo3/pull/1426) - - `PyMethodDef_INIT` [#1426](https://github.com/PyO3/pyo3/pull/1426) - - `PyTypeObject_INIT` [#1429](https://github.com/PyO3/pyo3/pull/1429) - - `PyObject_Check`, `PySuper_Check`, and `FreeFunc` [#1438](https://github.com/PyO3/pyo3/pull/1438) - - `PyModuleDef_INIT` [#1630](https://github.com/PyO3/pyo3/pull/1630) -- Remove pyclass implementation details from `PyTypeInfo`: - - `Type`, `DESCRIPTION`, and `FLAGS` [#1456](https://github.com/PyO3/pyo3/pull/1456) - - `BaseType`, `BaseLayout`, `Layout`, `Initializer` [#1596](https://github.com/PyO3/pyo3/pull/1596) -- Remove `PYO3_CROSS_INCLUDE_DIR` environment variable and the associated C header parsing functionality. [#1521](https://github.com/PyO3/pyo3/pull/1521) -- Remove `raw_pycfunction!` macro. [#1619](https://github.com/PyO3/pyo3/pull/1619) -- Remove `PyClassAlloc` trait. [#1657](https://github.com/PyO3/pyo3/pull/1657) -- Remove `PyList::get_parked_item`. [#1664](https://github.com/PyO3/pyo3/pull/1664) - -### Fixed - -- Remove FFI definition `PyCFunction_ClearFreeList` for Python 3.9 and later. [#1425](https://github.com/PyO3/pyo3/pull/1425) -- `PYO3_CROSS_LIB_DIR` environment variable no long required when compiling for x86-64 Python from macOS arm64 and reverse. [#1428](https://github.com/PyO3/pyo3/pull/1428) -- Fix FFI definition `_PyEval_RequestCodeExtraIndex`, which took an argument of the wrong type. [#1429](https://github.com/PyO3/pyo3/pull/1429) -- Fix FFI definition `PyIndex_Check` missing with the `abi3` feature. [#1436](https://github.com/PyO3/pyo3/pull/1436) -- Fix incorrect `TypeError` raised when keyword-only argument passed along with a positional argument in `*args`. [#1440](https://github.com/PyO3/pyo3/pull/1440) -- Fix inability to use a named lifetime for `&PyTuple` of `*args` in `#[pyfunction]`. [#1440](https://github.com/PyO3/pyo3/pull/1440) -- Fix use of Python argument for `#[pymethods]` inside macro expansions. [#1505](https://github.com/PyO3/pyo3/pull/1505) -- No longer include `__doc__` in `__all__` generated for `#[pymodule]`. [#1509](https://github.com/PyO3/pyo3/pull/1509) -- Always use cross-compiling configuration if any of the `PYO3_CROSS` family of environment variables are set. [#1514](https://github.com/PyO3/pyo3/pull/1514) -- Support `EnvironmentError`, `IOError`, and `WindowsError` on PyPy. [#1533](https://github.com/PyO3/pyo3/pull/1533) -- Fix unnecessary rebuilds when cycling between `cargo check` and `cargo clippy` in a Python virtualenv. [#1557](https://github.com/PyO3/pyo3/pull/1557) -- Fix segfault when dereferencing `ffi::PyDateTimeAPI` without the GIL. [#1563](https://github.com/PyO3/pyo3/pull/1563) -- Fix memory leak in `FromPyObject` implementations for `u128` and `i128`. [#1638](https://github.com/PyO3/pyo3/pull/1638) -- Fix `#[pyclass(extends=PyDict)]` leaking the dict contents on drop. [#1657](https://github.com/PyO3/pyo3/pull/1657) -- Fix segfault when calling `PyList::get_item` with negative indices. [#1668](https://github.com/PyO3/pyo3/pull/1668) -- Fix FFI definitions of `PyEval_SetProfile`/`PyEval_SetTrace` to take `Option` parameters. [#1692](https://github.com/PyO3/pyo3/pull/1692) -- Fix `ToPyObject` impl for `HashSet` to accept non-default hashers. [#1702](https://github.com/PyO3/pyo3/pull/1702) - -## [0.13.2] - 2021-02-12 - -### Packaging - -- Lower minimum supported Rust version to 1.41. [#1421](https://github.com/PyO3/pyo3/pull/1421) - -### Added - -- Add unsafe API `with_embedded_python_interpreter` to initialize a Python interpreter, execute a closure, and finalize the interpreter. [#1355](https://github.com/PyO3/pyo3/pull/1355) -- Add `serde` feature which provides implementations of `Serialize` and `Deserialize` for `Py`. [#1366](https://github.com/PyO3/pyo3/pull/1366) -- Add FFI definition `_PyCFunctionFastWithKeywords` on Python 3.7 and up. [#1384](https://github.com/PyO3/pyo3/pull/1384) -- Add `PyDateTime::new_with_fold` method. [#1398](https://github.com/PyO3/pyo3/pull/1398) -- Add `size_hint` impls for `{PyDict,PyList,PySet,PyTuple}Iterator`s. [#1699](https://github.com/PyO3/pyo3/pull/1699) - -### Changed - -- `prepare_freethreaded_python` will no longer register an `atexit` handler to call `Py_Finalize`. This resolves a number of issues with incompatible C extensions causing crashes at finalization. [#1355](https://github.com/PyO3/pyo3/pull/1355) -- Mark `PyLayout::py_init`, `PyClassDict::clear_dict`, and `opt_to_pyobj` safe, as they do not perform any unsafe operations. [#1404](https://github.com/PyO3/pyo3/pull/1404) - -### Fixed - -- Fix support for using `r#raw_idents` as argument names in pyfunctions. [#1383](https://github.com/PyO3/pyo3/pull/1383) -- Fix typo in FFI definition for `PyFunction_GetCode` (was incorrectly `PyFunction_Code`). [#1387](https://github.com/PyO3/pyo3/pull/1387) -- Fix FFI definitions `PyMarshal_WriteObjectToString` and `PyMarshal_ReadObjectFromString` as available in limited API. [#1387](https://github.com/PyO3/pyo3/pull/1387) -- Fix FFI definitions `PyListObject` and those from `funcobject.h` as requiring non-limited API. [#1387](https://github.com/PyO3/pyo3/pull/1387) -- Fix unqualified `Result` usage in `pyobject_native_type_base`. [#1402](https://github.com/PyO3/pyo3/pull/1402) -- Fix build on systems where the default Python encoding is not UTF-8. [#1405](https://github.com/PyO3/pyo3/pull/1405) -- Fix build on mingw / MSYS2. [#1423](https://github.com/PyO3/pyo3/pull/1423) - -## [0.13.1] - 2021-01-10 - -### Added - -- Add support for `#[pyclass(dict)]` and `#[pyclass(weakref)]` with the `abi3` feature on Python 3.9 and up. [#1342](https://github.com/PyO3/pyo3/pull/1342) -- Add FFI definitions `PyOS_BeforeFork`, `PyOS_AfterFork_Parent`, `PyOS_AfterFork_Child` for Python 3.7 and up. [#1348](https://github.com/PyO3/pyo3/pull/1348) -- Add an `auto-initialize` feature to control whether PyO3 should automatically initialize an embedded Python interpreter. For compatibility this feature is enabled by default in PyO3 0.13.1, but is planned to become opt-in from PyO3 0.14.0. [#1347](https://github.com/PyO3/pyo3/pull/1347) -- Add support for cross-compiling to Windows without needing `PYO3_CROSS_INCLUDE_DIR`. [#1350](https://github.com/PyO3/pyo3/pull/1350) - -### Deprecated - -- Deprecate FFI definitions `PyEval_CallObjectWithKeywords`, `PyEval_CallObject`, `PyEval_CallFunction`, `PyEval_CallMethod` when building for Python 3.9. [#1338](https://github.com/PyO3/pyo3/pull/1338) -- Deprecate FFI definitions `PyGetSetDef_DICT` and `PyGetSetDef_INIT` which have never been in the Python API. [#1341](https://github.com/PyO3/pyo3/pull/1341) -- Deprecate FFI definitions `PyGen_NeedsFinalizing`, `PyImport_Cleanup` (removed in 3.9), and `PyOS_InitInterrupts` (3.10). [#1348](https://github.com/PyO3/pyo3/pull/1348) -- Deprecate FFI definition `PyOS_AfterFork` for Python 3.7 and up. [#1348](https://github.com/PyO3/pyo3/pull/1348) -- Deprecate FFI definitions `PyCoro_Check`, `PyAsyncGen_Check`, and `PyCoroWrapper_Check`, which have never been in the Python API (for the first two, it is possible to use `PyCoro_CheckExact` and `PyAsyncGen_CheckExact` instead; these are the actual functions provided by the Python API). [#1348](https://github.com/PyO3/pyo3/pull/1348) -- Deprecate FFI definitions for `PyUnicode_FromUnicode`, `PyUnicode_AsUnicode` and `PyUnicode_AsUnicodeAndSize`, which will be removed from 3.12 and up due to [PEP 623](https://www.python.org/dev/peps/pep-0623/). [#1370](https://github.com/PyO3/pyo3/pull/1370) - -### Removed - -- Remove FFI definition `PyFrame_ClearFreeList` when building for Python 3.9. [#1341](https://github.com/PyO3/pyo3/pull/1341) -- Remove FFI definition `_PyDict_Contains` when building for Python 3.10. [#1341](https://github.com/PyO3/pyo3/pull/1341) -- Remove FFI definitions `PyGen_NeedsFinalizing` and `PyImport_Cleanup` (for 3.9 and up), and `PyOS_InitInterrupts` (3.10). [#1348](https://github.com/PyO3/pyo3/pull/1348) - -### Fixed - -- Stop including `Py_TRACE_REFS` config setting automatically if `Py_DEBUG` is set on Python 3.8 and up. [#1334](https://github.com/PyO3/pyo3/pull/1334) -- Remove `#[deny(warnings)]` attribute (and instead refuse warnings only in CI). [#1340](https://github.com/PyO3/pyo3/pull/1340) -- Fix deprecation warning for missing `__module__` with `#[pyclass]`. [#1343](https://github.com/PyO3/pyo3/pull/1343) -- Correct return type of `PyFrozenSet::empty` to `&PyFrozenSet` (was incorrectly `&PySet`). [#1351](https://github.com/PyO3/pyo3/pull/1351) -- Fix missing `Py_INCREF` on heap type objects on Python versions before 3.8. [#1365](https://github.com/PyO3/pyo3/pull/1365) - -## [0.13.0] - 2020-12-22 - -### Packaging - -- Drop support for Python 3.5 (as it is now end-of-life). [#1250](https://github.com/PyO3/pyo3/pull/1250) -- Bump minimum supported Rust version to 1.45. [#1272](https://github.com/PyO3/pyo3/pull/1272) -- Bump indoc dependency to 1.0. [#1272](https://github.com/PyO3/pyo3/pull/1272) -- Bump paste dependency to 1.0. [#1272](https://github.com/PyO3/pyo3/pull/1272) -- Rename internal crates `pyo3cls` and `pyo3-derive-backend` to `pyo3-macros` and `pyo3-macros-backend` respectively. [#1317](https://github.com/PyO3/pyo3/pull/1317) - -### Added - -- Add support for building for CPython limited API. Opting-in to the limited API enables a single extension wheel built with PyO3 to be installable on multiple Python versions. This required a few minor changes to runtime behavior of of PyO3 `#[pyclass]` types. See the migration guide for full details. [#1152](https://github.com/PyO3/pyo3/pull/1152) - - Add feature flags `abi3-py36`, `abi3-py37`, `abi3-py38` etc. to set the minimum Python version when using the limited API. [#1263](https://github.com/PyO3/pyo3/pull/1263) -- Add argument names to `TypeError` messages generated by pymethod wrappers. [#1212](https://github.com/PyO3/pyo3/pull/1212) -- Add FFI definitions for PEP 587 "Python Initialization Configuration". [#1247](https://github.com/PyO3/pyo3/pull/1247) -- Add FFI definitions for `PyEval_SetProfile` and `PyEval_SetTrace`. [#1255](https://github.com/PyO3/pyo3/pull/1255) -- Add FFI definitions for context.h functions (`PyContext_New`, etc). [#1259](https://github.com/PyO3/pyo3/pull/1259) -- Add `PyAny::is_instance` method. [#1276](https://github.com/PyO3/pyo3/pull/1276) -- Add support for conversion between `char` and `PyString`. [#1282](https://github.com/PyO3/pyo3/pull/1282) -- Add FFI definitions for `PyBuffer_SizeFromFormat`, `PyObject_LengthHint`, `PyObject_CallNoArgs`, `PyObject_CallOneArg`, `PyObject_CallMethodNoArgs`, `PyObject_CallMethodOneArg`, `PyObject_VectorcallDict`, and `PyObject_VectorcallMethod`. [#1287](https://github.com/PyO3/pyo3/pull/1287) -- Add conversions between `u128`/`i128` and `PyLong` for PyPy. [#1310](https://github.com/PyO3/pyo3/pull/1310) -- Add `Python::version` and `Python::version_info` to get the running interpreter version. [#1322](https://github.com/PyO3/pyo3/pull/1322) -- Add conversions for tuples of length 10, 11, and 12. [#1454](https://github.com/PyO3/pyo3/pull/1454) - -### Changed - -- Change return type of `PyType::name` from `Cow` to `PyResult<&str>`. [#1152](https://github.com/PyO3/pyo3/pull/1152) -- `#[pyclass(subclass)]` is now required for subclassing from Rust (was previously just required for subclassing from Python). [#1152](https://github.com/PyO3/pyo3/pull/1152) -- Change `PyIterator` to be consistent with other native types: it is now used as `&PyIterator` instead of `PyIterator<'a>`. [#1176](https://github.com/PyO3/pyo3/pull/1176) -- Change formatting of `PyDowncastError` messages to be closer to Python's builtin error messages. [#1212](https://github.com/PyO3/pyo3/pull/1212) -- Change `Debug` and `Display` impls for `PyException` to be consistent with `PyAny`. [#1275](https://github.com/PyO3/pyo3/pull/1275) -- Change `Debug` impl of `PyErr` to output more helpful information (acquiring the GIL if necessary). [#1275](https://github.com/PyO3/pyo3/pull/1275) -- Rename `PyTypeInfo::is_instance` and `PyTypeInfo::is_exact_instance` to `PyTypeInfo::is_type_of` and `PyTypeInfo::is_exact_type_of`. [#1278](https://github.com/PyO3/pyo3/pull/1278) -- Optimize `PyAny::call0`, `Py::call0` and `PyAny::call_method0` and `Py::call_method0` on Python 3.9 and up. [#1287](https://github.com/PyO3/pyo3/pull/1285) -- Require double-quotes for pyclass name argument e.g `#[pyclass(name = "MyClass")]`. [#1303](https://github.com/PyO3/pyo3/pull/1303) - -### Deprecated - -- Deprecate `Python::is_instance`, `Python::is_subclass`, `Python::release`, and `Python::xdecref`. [#1292](https://github.com/PyO3/pyo3/pull/1292) - -### Removed - -- Remove deprecated ffi definitions `PyUnicode_AsUnicodeCopy`, `PyUnicode_GetMax`, `_Py_CheckRecursionLimit`, `PyObject_AsCharBuffer`, `PyObject_AsReadBuffer`, `PyObject_CheckReadBuffer` and `PyObject_AsWriteBuffer`, which will be removed in Python 3.10. [#1217](https://github.com/PyO3/pyo3/pull/1217) -- Remove unused `python3` feature. [#1235](https://github.com/PyO3/pyo3/pull/1235) - -### Fixed - -- Fix missing field in `PyCodeObject` struct (`co_posonlyargcount`) - caused invalid access to other fields in Python >3.7. [#1260](https://github.com/PyO3/pyo3/pull/1260) -- Fix building for `x86_64-unknown-linux-musl` target from `x86_64-unknown-linux-gnu` host. [#1267](https://github.com/PyO3/pyo3/pull/1267) -- Fix `#[text_signature]` interacting badly with rust `r#raw_identifiers`. [#1286](https://github.com/PyO3/pyo3/pull/1286) -- Fix FFI definitions for `PyObject_Vectorcall` and `PyVectorcall_Call`. [#1287](https://github.com/PyO3/pyo3/pull/1285) -- Fix building with Anaconda python inside a virtualenv. [#1290](https://github.com/PyO3/pyo3/pull/1290) -- Fix definition of opaque FFI types. [#1312](https://github.com/PyO3/pyo3/pull/1312) -- Fix using custom error type in pyclass `#[new]` methods. [#1319](https://github.com/PyO3/pyo3/pull/1319) - -## [0.12.4] - 2020-11-28 - -### Fixed - -- Fix reference count bug in implementation of `From>` for `PyObject`, a regression introduced in PyO3 0.12. [#1297](https://github.com/PyO3/pyo3/pull/1297) - -## [0.12.3] - 2020-10-12 - -### Fixed - -- Fix support for Rust versions 1.39 to 1.44, broken by an incorrect internal update to paste 1.0 which was done in PyO3 0.12.2. [#1234](https://github.com/PyO3/pyo3/pull/1234) - -## [0.12.2] - 2020-10-12 - -### Added - -- Add support for keyword-only arguments without default values in `#[pyfunction]`. [#1209](https://github.com/PyO3/pyo3/pull/1209) -- Add `Python::check_signals` as a safe a wrapper for `PyErr_CheckSignals`. [#1214](https://github.com/PyO3/pyo3/pull/1214) - -### Fixed - -- Fix invalid document for protocol methods. [#1169](https://github.com/PyO3/pyo3/pull/1169) -- Hide docs of PyO3 private implementation details in `pyo3::class::methods`. [#1169](https://github.com/PyO3/pyo3/pull/1169) -- Fix unnecessary rebuild on PATH changes when the python interpreter is provided by PYO3_PYTHON. [#1231](https://github.com/PyO3/pyo3/pull/1231) - -## [0.12.1] - 2020-09-16 - -### Fixed - -- Fix building for a 32-bit Python on 64-bit Windows with a 64-bit Rust toolchain. [#1179](https://github.com/PyO3/pyo3/pull/1179) -- Fix building on platforms where `c_char` is `u8`. [#1182](https://github.com/PyO3/pyo3/pull/1182) - -## [0.12.0] - 2020-09-12 - -### Added - -- Add FFI definitions `Py_FinalizeEx`, `PyOS_getsig`, and `PyOS_setsig`. [#1021](https://github.com/PyO3/pyo3/pull/1021) -- Add `PyString::to_str` for accessing `PyString` as `&str`. [#1023](https://github.com/PyO3/pyo3/pull/1023) -- Add `Python::with_gil` for executing a closure with the Python GIL. [#1037](https://github.com/PyO3/pyo3/pull/1037) -- Add type information to failures in `PyAny::downcast`. [#1050](https://github.com/PyO3/pyo3/pull/1050) -- Implement `Debug` for `PyIterator`. [#1051](https://github.com/PyO3/pyo3/pull/1051) -- Add `PyBytes::new_with` and `PyByteArray::new_with` for initialising `bytes` and `bytearray` objects using a closure. [#1074](https://github.com/PyO3/pyo3/pull/1074) -- Add `#[derive(FromPyObject)]` macro for enums and structs. [#1065](https://github.com/PyO3/pyo3/pull/1065) -- Add `Py::as_ref` and `Py::into_ref` for converting `Py` to `&T`. [#1098](https://github.com/PyO3/pyo3/pull/1098) -- Add ability to return `Result` types other than `PyResult` from `#[pyfunction]`, `#[pymethod]` and `#[pyproto]` functions. [#1106](https://github.com/PyO3/pyo3/pull/1118). -- Implement `ToPyObject`, `IntoPy`, and `FromPyObject` for [hashbrown](https://crates.io/crates/hashbrown)'s `HashMap` and `HashSet` types (requires the `hashbrown` feature). [#1114](https://github.com/PyO3/pyo3/pull/1114) -- Add `#[pyfunction(pass_module)]` and `#[pyfn(pass_module)]` to pass the module object as the first function argument. [#1143](https://github.com/PyO3/pyo3/pull/1143) -- Add `PyModule::add_function` and `PyModule::add_submodule` as typed alternatives to `PyModule::add_wrapped`. [#1143](https://github.com/PyO3/pyo3/pull/1143) -- Add native `PyCFunction` and `PyFunction` types. [#1163](https://github.com/PyO3/pyo3/pull/1163) - -### Changed - -- Rework exception types: [#1024](https://github.com/PyO3/pyo3/pull/1024) [#1115](https://github.com/PyO3/pyo3/pull/1115) - - Rename exception types from e.g. `RuntimeError` to `PyRuntimeError`. The old names continue to exist but are deprecated. - - Exception objects are now accessible as `&T` or `Py`, just like other Python-native types. - - Rename `PyException::py_err` to `PyException::new_err`. - - Rename `PyUnicodeDecodeErr::new_err` to `PyUnicodeDecodeErr::new`. - - Remove `PyStopIteration::stop_iteration`. -- Require `T: Send` for the return value `T` of `Python::allow_threads`. [#1036](https://github.com/PyO3/pyo3/pull/1036) -- Rename `PYTHON_SYS_EXECUTABLE` to `PYO3_PYTHON`. The old name will continue to work (undocumented) but will be removed in a future release. [#1039](https://github.com/PyO3/pyo3/pull/1039) -- Remove `unsafe` from signature of `PyType::as_type_ptr`. [#1047](https://github.com/PyO3/pyo3/pull/1047) -- Change return type of `PyIterator::from_object` to `PyResult` (was `Result`). [#1051](https://github.com/PyO3/pyo3/pull/1051) -- `IntoPy` is no longer implied by `FromPy`. [#1063](https://github.com/PyO3/pyo3/pull/1063) -- Change `PyObject` to be a type alias for `Py`. [#1063](https://github.com/PyO3/pyo3/pull/1063) -- Rework `PyErr` to be compatible with the `std::error::Error` trait: [#1067](https://github.com/PyO3/pyo3/pull/1067) [#1115](https://github.com/PyO3/pyo3/pull/1115) - - Implement `Display`, `Error`, `Send` and `Sync` for `PyErr` and `PyErrArguments`. - - Add `PyErr::instance` for accessing `PyErr` as `&PyBaseException`. - - `PyErr`'s fields are now an implementation detail. The equivalent values can be accessed with `PyErr::ptype`, `PyErr::pvalue` and `PyErr::ptraceback`. - - Change receiver of `PyErr::print` and `PyErr::print_and_set_sys_last_vars` to `&self` (was `self`). - - Remove `PyErrValue`, `PyErr::from_value`, `PyErr::into_normalized`, and `PyErr::normalize`. - - Remove `PyException::into`. - - Remove `Into>` for `PyErr` and `PyException`. -- Change methods generated by `#[pyproto]` to return `NotImplemented` if Python should try a reversed operation. #[1072](https://github.com/PyO3/pyo3/pull/1072) -- Change argument to `PyModule::add` to `impl IntoPy` (was `impl ToPyObject`). #[1124](https://github.com/PyO3/pyo3/pull/1124) - -### Removed - -- Remove many exception and `PyErr` APIs; see the "changed" section above. [#1024](https://github.com/PyO3/pyo3/pull/1024) [#1067](https://github.com/PyO3/pyo3/pull/1067) [#1115](https://github.com/PyO3/pyo3/pull/1115) -- Remove `PyString::to_string` (use new `PyString::to_str`). [#1023](https://github.com/PyO3/pyo3/pull/1023) -- Remove `PyString::as_bytes`. [#1023](https://github.com/PyO3/pyo3/pull/1023) -- Remove `Python::register_any`. [#1023](https://github.com/PyO3/pyo3/pull/1023) -- Remove `GILGuard::acquire` from the public API. Use `Python::acquire_gil` or `Python::with_gil`. [#1036](https://github.com/PyO3/pyo3/pull/1036) -- Remove the `FromPy` trait. [#1063](https://github.com/PyO3/pyo3/pull/1063) -- Remove the `AsPyRef` trait. [#1098](https://github.com/PyO3/pyo3/pull/1098) - -### Fixed - -- Correct FFI definitions `Py_SetProgramName` and `Py_SetPythonHome` to take `*const` arguments (was `*mut`). [#1021](https://github.com/PyO3/pyo3/pull/1021) -- Fix `FromPyObject` for `num_bigint::BigInt` for Python objects with an `__index__` method. [#1027](https://github.com/PyO3/pyo3/pull/1027) -- Correct FFI definition `_PyLong_AsByteArray` to take `*mut c_uchar` argument (was `*const c_uchar`). [#1029](https://github.com/PyO3/pyo3/pull/1029) -- Fix segfault with `#[pyclass(dict, unsendable)]`. [#1058](https://github.com/PyO3/pyo3/pull/1058) [#1059](https://github.com/PyO3/pyo3/pull/1059) -- Fix using `&Self` as an argument type for functions in a `#[pymethods]` block. [#1071](https://github.com/PyO3/pyo3/pull/1071) -- Fix best-effort build against PyPy 3.6. [#1092](https://github.com/PyO3/pyo3/pull/1092) -- Fix many cases of lifetime elision in `#[pyproto]` implementations. [#1093](https://github.com/PyO3/pyo3/pull/1093) -- Fix detection of Python build configuration when cross-compiling. [#1095](https://github.com/PyO3/pyo3/pull/1095) -- Always link against libpython on android with the `extension-module` feature. [#1095](https://github.com/PyO3/pyo3/pull/1095) -- Fix the `+` operator not trying `__radd__` when both `__add__` and `__radd__` are defined in `PyNumberProtocol` (and similar for all other reversible operators). [#1107](https://github.com/PyO3/pyo3/pull/1107) -- Fix building with Anaconda python. [#1175](https://github.com/PyO3/pyo3/pull/1175) - -## [0.11.1] - 2020-06-30 - -### Added - -- `#[pyclass(unsendable)]`. [#1009](https://github.com/PyO3/pyo3/pull/1009) - -### Changed - -- Update `parking_lot` dependency to `0.11`. [#1010](https://github.com/PyO3/pyo3/pull/1010) - -## [0.11.0] - 2020-06-28 - -### Added - -- Support stable versions of Rust (>=1.39). [#969](https://github.com/PyO3/pyo3/pull/969) -- Add FFI definition `PyObject_AsFileDescriptor`. [#938](https://github.com/PyO3/pyo3/pull/938) -- Add `PyByteArray::data`, `PyByteArray::as_bytes`, and `PyByteArray::as_bytes_mut`. [#967](https://github.com/PyO3/pyo3/pull/967) -- Add `GILOnceCell` to use in situations where `lazy_static` or `once_cell` can deadlock. [#975](https://github.com/PyO3/pyo3/pull/975) -- Add `Py::borrow`, `Py::borrow_mut`, `Py::try_borrow`, and `Py::try_borrow_mut` for accessing `#[pyclass]` values. [#976](https://github.com/PyO3/pyo3/pull/976) -- Add `IterNextOutput` and `IterANextOutput` for returning from `__next__` / `__anext__`. [#997](https://github.com/PyO3/pyo3/pull/997) - -### Changed - -- Simplify internals of `#[pyo3(get)]` attribute. (Remove the hidden API `GetPropertyValue`.) [#934](https://github.com/PyO3/pyo3/pull/934) -- Call `Py_Finalize` at exit to flush buffers, etc. [#943](https://github.com/PyO3/pyo3/pull/943) -- Add type parameter to PyBuffer. #[951](https://github.com/PyO3/pyo3/pull/951) -- Require `Send` bound for `#[pyclass]`. [#966](https://github.com/PyO3/pyo3/pull/966) -- Add `Python` argument to most methods on `PyObject` and `Py` to ensure GIL safety. [#970](https://github.com/PyO3/pyo3/pull/970) -- Change signature of `PyTypeObject::type_object` - now takes `Python` argument and returns `&PyType`. [#970](https://github.com/PyO3/pyo3/pull/970) -- Change return type of `PyTuple::slice` and `PyTuple::split_from` from `Py` to `&PyTuple`. [#970](https://github.com/PyO3/pyo3/pull/970) -- Change return type of `PyTuple::as_slice` to `&[&PyAny]`. [#971](https://github.com/PyO3/pyo3/pull/971) -- Rename `PyTypeInfo::type_object` to `type_object_raw`, and add `Python` argument. [#975](https://github.com/PyO3/pyo3/pull/975) -- Update `num-complex` optional dependendency from `0.2` to `0.3`. [#977](https://github.com/PyO3/pyo3/pull/977) -- Update `num-bigint` optional dependendency from `0.2` to `0.3`. [#978](https://github.com/PyO3/pyo3/pull/978) -- `#[pyproto]` is re-implemented without specialization. [#961](https://github.com/PyO3/pyo3/pull/961) -- `PyClassAlloc::alloc` is renamed to `PyClassAlloc::new`. [#990](https://github.com/PyO3/pyo3/pull/990) -- `#[pyproto]` methods can now have return value `T` or `PyResult` (previously only `PyResult` was supported). [#996](https://github.com/PyO3/pyo3/pull/996) -- `#[pyproto]` methods can now skip annotating the return type if it is `()`. [#998](https://github.com/PyO3/pyo3/pull/998) - -### Removed - -- Remove `ManagedPyRef` (unused, and needs specialization) [#930](https://github.com/PyO3/pyo3/pull/930) - -### Fixed - -- Fix passing explicit `None` to `Option` argument `#[pyfunction]` with a default value. [#936](https://github.com/PyO3/pyo3/pull/936) -- Fix `PyClass.__new__`'s not respecting subclasses when inherited by a Python class. [#990](https://github.com/PyO3/pyo3/pull/990) -- Fix returning `Option` from `#[pyproto]` methods. [#996](https://github.com/PyO3/pyo3/pull/996) -- Fix accepting `PyRef` and `PyRefMut` to `#[getter]` and `#[setter]` methods. [#999](https://github.com/PyO3/pyo3/pull/999) - -## [0.10.1] - 2020-05-14 - -### Fixed - -- Fix deadlock in `Python::acquire_gil` after dropping a `PyObject` or `Py`. [#924](https://github.com/PyO3/pyo3/pull/924) - -## [0.10.0] - 2020-05-13 - -### Added - -- Add FFI definition `_PyDict_NewPresized`. [#849](https://github.com/PyO3/pyo3/pull/849) -- Implement `IntoPy` for `HashSet` and `BTreeSet`. [#864](https://github.com/PyO3/pyo3/pull/864) -- Add `PyAny::dir` method. [#886](https://github.com/PyO3/pyo3/pull/886) -- Gate macros behind a `macros` feature (enabled by default). [#897](https://github.com/PyO3/pyo3/pull/897) -- Add ability to define class attributes using `#[classattr]` on functions in `#[pymethods]`. [#905](https://github.com/PyO3/pyo3/pull/905) -- Implement `Clone` for `PyObject` and `Py`. [#908](https://github.com/PyO3/pyo3/pull/908) -- Implement `Deref` for all builtin types. (`PyList`, `PyTuple`, `PyDict` etc.) [#911](https://github.com/PyO3/pyo3/pull/911) -- Implement `Deref` for `PyCell`. [#911](https://github.com/PyO3/pyo3/pull/911) -- Add `#[classattr]` support for associated constants in `#[pymethods]`. [#914](https://github.com/PyO3/pyo3/pull/914) - -### Changed - -- Panics will now be raised as a Python `PanicException`. [#797](https://github.com/PyO3/pyo3/pull/797) -- Change `PyObject` and `Py` reference counts to decrement immediately upon drop when the GIL is held. [#851](https://github.com/PyO3/pyo3/pull/851) -- Allow `PyIterProtocol` methods to use either `PyRef` or `PyRefMut` as the receiver type. [#856](https://github.com/PyO3/pyo3/pull/856) -- Change the implementation of `FromPyObject` for `Py` to apply to a wider range of `T`, including all `T: PyClass`. [#880](https://github.com/PyO3/pyo3/pull/880) -- Move all methods from the `ObjectProtocol` trait to the `PyAny` struct. [#911](https://github.com/PyO3/pyo3/pull/911) -- Remove need for `#![feature(specialization)]` in crates depending on PyO3. [#917](https://github.com/PyO3/pyo3/pull/917) - -### Removed - -- Remove `PyMethodsProtocol` trait. [#889](https://github.com/PyO3/pyo3/pull/889) -- Remove `num-traits` dependency. [#895](https://github.com/PyO3/pyo3/pull/895) -- Remove `ObjectProtocol` trait. [#911](https://github.com/PyO3/pyo3/pull/911) -- Remove `PyAny::None`. Users should use `Python::None` instead. [#911](https://github.com/PyO3/pyo3/pull/911) -- Remove all `*ProtocolImpl` traits. [#917](https://github.com/PyO3/pyo3/pull/917) - -### Fixed - -- Fix support for `__radd__` and other `__r*__` methods as implementations for Python mathematical operators. [#839](https://github.com/PyO3/pyo3/pull/839) -- Fix panics during garbage collection when traversing objects that were already mutably borrowed. [#855](https://github.com/PyO3/pyo3/pull/855) -- Prevent `&'static` references to Python objects as arguments to `#[pyfunction]` and `#[pymethods]`. [#869](https://github.com/PyO3/pyo3/pull/869) -- Fix lifetime safety bug with `AsPyRef::as_ref`. [#876](https://github.com/PyO3/pyo3/pull/876) -- Fix `#[pyo3(get)]` attribute on `Py` fields. [#880](https://github.com/PyO3/pyo3/pull/880) -- Fix segmentation faults caused by functions such as `PyList::get_item` returning borrowed objects when it was not safe to do so. [#890](https://github.com/PyO3/pyo3/pull/890) -- Fix segmentation faults caused by nested `Python::acquire_gil` calls creating dangling references. [#893](https://github.com/PyO3/pyo3/pull/893) -- Fix segmentatation faults when a panic occurs during a call to `Python::allow_threads`. [#912](https://github.com/PyO3/pyo3/pull/912) - -## [0.9.2] - 2020-04-09 - -### Added - -- `FromPyObject` implementations for `HashSet` and `BTreeSet`. [#842](https://github.com/PyO3/pyo3/pull/842) - -### Fixed - -- Correctly detect 32bit architecture. [#830](https://github.com/PyO3/pyo3/pull/830) - -## [0.9.1] - 2020-03-23 - -### Fixed - -- Error messages for `#[pyclass]`. [#826](https://github.com/PyO3/pyo3/pull/826) -- `FromPyObject` implementation for `PySequence`. [#827](https://github.com/PyO3/pyo3/pull/827) - -## [0.9.0] - 2020-03-19 - -### Added - -- `PyCell`, which has RefCell-like features. [#770](https://github.com/PyO3/pyo3/pull/770) -- `PyClass`, `PyLayout`, `PyClassInitializer`. [#683](https://github.com/PyO3/pyo3/pull/683) -- Implemented `IntoIterator` for `PySet` and `PyFrozenSet`. [#716](https://github.com/PyO3/pyo3/pull/716) -- `FromPyObject` is now automatically implemented for `T: Clone` pyclasses. [#730](https://github.com/PyO3/pyo3/pull/730) -- `#[pyo3(get)]` and `#[pyo3(set)]` will now use the Rust doc-comment from the field for the Python property. [#755](https://github.com/PyO3/pyo3/pull/755) -- `#[setter]` functions may now take an argument of `Pyo3::Python`. [#760](https://github.com/PyO3/pyo3/pull/760) -- `PyTypeInfo::BaseLayout` and `PyClass::BaseNativeType`. [#770](https://github.com/PyO3/pyo3/pull/770) -- `PyDowncastImpl`. [#770](https://github.com/PyO3/pyo3/pull/770) -- Implement `FromPyObject` and `IntoPy` traits for arrays (up to 32). [#778](https://github.com/PyO3/pyo3/pull/778) -- `migration.md` and `types.md` in the guide. [#795](https://github.com/PyO3/pyo3/pull/795), #[802](https://github.com/PyO3/pyo3/pull/802) -- `ffi::{_PyBytes_Resize, _PyDict_Next, _PyDict_Contains, _PyDict_GetDictPtr}`. #[820](https://github.com/PyO3/pyo3/pull/820) - -### Changed - -- `#[new]` does not take `PyRawObject` and can return `Self`. [#683](https://github.com/PyO3/pyo3/pull/683) -- The blanket implementations for `FromPyObject` for `&T` and `&mut T` are no longer specializable. Implement `PyTryFrom` for your type to control the behavior of `FromPyObject::extract` for your types. [#713](https://github.com/PyO3/pyo3/pull/713) -- The implementation for `IntoPy for T` where `U: FromPy` is no longer specializable. Control the behavior of this via the implementation of `FromPy`. [#713](https://github.com/PyO3/pyo3/pull/713) -- Use `parking_lot::Mutex` instead of `spin::Mutex`. [#734](https://github.com/PyO3/pyo3/pull/734) -- Bumped minimum Rust version to `1.42.0-nightly 2020-01-21`. [#761](https://github.com/PyO3/pyo3/pull/761) -- `PyRef` and `PyRefMut` are renewed for `PyCell`. [#770](https://github.com/PyO3/pyo3/pull/770) -- Some new FFI functions for Python 3.8. [#784](https://github.com/PyO3/pyo3/pull/784) -- `PyAny` is now on the top level module and prelude. [#816](https://github.com/PyO3/pyo3/pull/816) - -### Removed - -- `PyRawObject`. [#683](https://github.com/PyO3/pyo3/pull/683) -- `PyNoArgsFunction`. [#741](https://github.com/PyO3/pyo3/pull/741) -- `initialize_type`. To set the module name for a `#[pyclass]`, use the `module` argument to the macro. #[751](https://github.com/PyO3/pyo3/pull/751) -- `AsPyRef::as_mut/with/with_mut/into_py/into_mut_py`. [#770](https://github.com/PyO3/pyo3/pull/770) -- `PyTryFrom::try_from_mut/try_from_mut_exact/try_from_mut_unchecked`. [#770](https://github.com/PyO3/pyo3/pull/770) -- `Python::mut_from_owned_ptr/mut_from_borrowed_ptr`. [#770](https://github.com/PyO3/pyo3/pull/770) -- `ObjectProtocol::get_base/get_mut_base`. [#770](https://github.com/PyO3/pyo3/pull/770) - -### Fixed - -- Fixed unsoundness of subclassing. [#683](https://github.com/PyO3/pyo3/pull/683). -- Clear error indicator when the exception is handled on the Rust side. [#719](https://github.com/PyO3/pyo3/pull/719) -- Usage of raw identifiers with `#[pyo3(set)]`. [#745](https://github.com/PyO3/pyo3/pull/745) -- Usage of `PyObject` with `#[pyo3(get)]`. [#760](https://github.com/PyO3/pyo3/pull/760) -- `#[pymethods]` used in conjunction with `#[cfg]`. #[769](https://github.com/PyO3/pyo3/pull/769) -- `"*"` in a `#[pyfunction()]` argument list incorrectly accepting any number of positional arguments (use `args = "*"` when this behavior is desired). #[792](https://github.com/PyO3/pyo3/pull/792) -- `PyModule::dict`. #[809](https://github.com/PyO3/pyo3/pull/809) -- Fix the case where `DESCRIPTION` is not null-terminated. #[822](https://github.com/PyO3/pyo3/pull/822) - -## [0.8.5] - 2020-01-05 - -### Added - -- Implemented `FromPyObject` for `HashMap` and `BTreeMap` -- Support for `#[name = "foo"]` attribute for `#[pyfunction]` and in `#[pymethods]`. [#692](https://github.com/PyO3/pyo3/pull/692) - -## [0.8.4] - 2019-12-14 - -### Added - -- Support for `#[text_signature]` attribute. [#675](https://github.com/PyO3/pyo3/pull/675) - -## [0.8.3] - 2019-11-23 - -### Removed - -- `#[init]` is removed. [#658](https://github.com/PyO3/pyo3/pull/658) - -### Fixed - -- Now all `&Py~` types have `!Send` bound. [#655](https://github.com/PyO3/pyo3/pull/655) -- Fix a compile error raised by the stabilization of `!` type. [#672](https://github.com/PyO3/pyo3/issues/672). - -## [0.8.2] - 2019-10-27 - -### Added - -- FFI compatibility for PEP 590 Vectorcall. [#641](https://github.com/PyO3/pyo3/pull/641) - -### Fixed - -- Fix PySequenceProtocol::set_item. [#624](https://github.com/PyO3/pyo3/pull/624) -- Fix a corner case of BigInt::FromPyObject. [#630](https://github.com/PyO3/pyo3/pull/630) -- Fix index errors in parameter conversion. [#631](https://github.com/PyO3/pyo3/pull/631) -- Fix handling of invalid utf-8 sequences in `PyString::as_bytes`. [#639](https://github.com/PyO3/pyo3/pull/639) - and `PyString::to_string_lossy` [#642](https://github.com/PyO3/pyo3/pull/642). -- Remove `__contains__` and `__iter__` from PyMappingProtocol. [#644](https://github.com/PyO3/pyo3/pull/644) -- Fix proc-macro definition of PySetAttrProtocol. [#645](https://github.com/PyO3/pyo3/pull/645) - -## [0.8.1] - 2019-10-08 - -### Added - -- Conversion between [num-bigint](https://github.com/rust-num/num-bigint) and Python int. [#608](https://github.com/PyO3/pyo3/pull/608) - -### Fixed - -- Make sure the right Python interpreter is used in OSX builds. [#604](https://github.com/PyO3/pyo3/pull/604) -- Patch specialization being broken by Rust 1.40. [#614](https://github.com/PyO3/pyo3/issues/614) -- Fix a segfault around PyErr. [#597](https://github.com/PyO3/pyo3/pull/597) - -## [0.8.0] - 2019-09-16 - -### Added - -- `module` argument to `pyclass` macro. [#499](https://github.com/PyO3/pyo3/pull/499) -- `py_run!` macro [#512](https://github.com/PyO3/pyo3/pull/512) -- Use existing fields and methods before calling custom **getattr**. [#505](https://github.com/PyO3/pyo3/pull/505) -- `PyBytes` can now be indexed just like `Vec` -- Implement `IntoPy` for `PyRef` and `PyRefMut`. - -### Changed - -- Implementing the Using the `gc` parameter for `pyclass` (e.g. `#[pyclass(gc)]`) without implementing the `class::PyGCProtocol` trait is now a compile-time error. Failing to implement this trait could lead to segfaults. [#532](https://github.com/PyO3/pyo3/pull/532) -- `PyByteArray::data` has been replaced with `PyDataArray::to_vec` because returning a `&[u8]` is unsound. (See [this comment](https://github.com/PyO3/pyo3/issues/373#issuecomment-512332696) for a great write-up for why that was unsound) -- Replace `mashup` with `paste`. -- `GILPool` gained a `Python` marker to prevent it from being misused to release Python objects without the GIL held. - -### Removed - -- `IntoPyObject` was replaced with `IntoPy` -- `#[pyclass(subclass)]` is hidden a `unsound-subclass` feature because it's causing segmentation faults. - -### Fixed - -- More readable error message for generics in pyclass [#503](https://github.com/PyO3/pyo3/pull/503) - -## [0.7.0] - 2019-05-26 - -### Added - -- PyPy support by omerbenamram in [#393](https://github.com/PyO3/pyo3/pull/393) -- Have `PyModule` generate an index of its members (`__all__` list). -- Allow `slf: PyRef` for pyclass(#419) -- Allow to use lifetime specifiers in `pymethods` -- Add `marshal` module. [#460](https://github.com/PyO3/pyo3/pull/460) - -### Changed - -- `Python::run` returns `PyResult<()>` instead of `PyResult<&PyAny>`. -- Methods decorated with `#[getter]` and `#[setter]` can now omit wrapping the - result type in `PyResult` if they don't raise exceptions. - -### Fixed - -- `type_object::PyTypeObject` has been marked unsafe because breaking the contract `type_object::PyTypeObject::init_type` can lead to UB. -- Fixed automatic derive of `PySequenceProtocol` implementation in [#423](https://github.com/PyO3/pyo3/pull/423). -- Capitalization & better wording to README.md. -- Docstrings of properties is now properly set using the doc of the `#[getter]` method. -- Fixed issues with `pymethods` crashing on doc comments containing double quotes. -- `PySet::new` and `PyFrozenSet::new` now return `PyResult<&Py[Frozen]Set>`; exceptions are raised if - the items are not hashable. -- Fixed building using `venv` on Windows. -- `PyTuple::new` now returns `&PyTuple` instead of `Py`. -- Fixed several issues with argument parsing; notable, the `*args` and `**kwargs` - tuple/dict now doesn't contain arguments that are otherwise assigned to parameters. - -## [0.6.0] - 2019-03-28 - -### Regressions - -- Currently, [#341](https://github.com/PyO3/pyo3/issues/341) causes `cargo test` to fail with weird linking errors when the `extension-module` feature is activated. For now you can work around this by making the `extension-module` feature optional and running the tests with `cargo test --no-default-features`: - -```toml -[dependencies.pyo3] -version = "0.6.0" - -[features] -extension-module = ["pyo3/extension-module"] -default = ["extension-module"] -``` - -### Added - -- Added a `wrap_pymodule!` macro similar to the existing `wrap_pyfunction!` macro. Only available on python 3 -- Added support for cross compiling (e.g. to arm v7) by mtp401 in [#327](https://github.com/PyO3/pyo3/pull/327). See the "Cross Compiling" section in the "Building and Distribution" chapter of the guide for more details. -- The `PyRef` and `PyRefMut` types, which allow to differentiate between an instance of a rust struct on the rust heap and an instance that is embedded inside a python object. By kngwyu in [#335](https://github.com/PyO3/pyo3/pull/335) -- Added `FromPy` and `IntoPy` which are equivalent to `From` and `Into` except that they require a gil token. -- Added `ManagedPyRef`, which should eventually replace `ToBorrowedObject`. - -### Changed - -- Renamed `PyObjectRef` to `PyAny` in #388 -- Renamed `add_function` to `add_wrapped` as it now also supports modules. -- Renamed `#[pymodinit]` to `#[pymodule]` -- `py.init(|| value)` becomes `Py::new(value)` -- `py.init_ref(|| value)` becomes `PyRef::new(value)` -- `py.init_mut(|| value)` becomes `PyRefMut::new(value)`. -- `PyRawObject::init` is now infallible, e.g. it returns `()` instead of `PyResult<()>`. -- Renamed `py_exception!` to `create_exception!` and refactored the error macros. -- Renamed `wrap_function!` to `wrap_pyfunction!` -- Renamed `#[prop(get, set)]` to `#[pyo3(get, set)]` -- `#[pyfunction]` now supports the same arguments as `#[pyfn()]` -- Some macros now emit proper spanned errors instead of panics. -- Migrated to the 2018 edition -- `crate::types::exceptions` moved to `crate::exceptions` -- Replace `IntoPyTuple` with `IntoPy>`. -- `IntoPyPointer` and `ToPyPointer` moved into the crate root. -- `class::CompareOp` moved into `class::basic::CompareOp` -- PyTypeObject is now a direct subtrait PyTypeCreate, removing the old cyclical implementation in [#350](https://github.com/PyO3/pyo3/pull/350) -- Add `PyList::{sort, reverse}` by chr1sj0nes in [#357](https://github.com/PyO3/pyo3/pull/357) and [#358](https://github.com/PyO3/pyo3/pull/358) -- Renamed the `typeob` module to `type_object` - -### Removed - -- `PyToken` was removed due to unsoundness (See [#94](https://github.com/PyO3/pyo3/issues/94)). -- Removed the unnecessary type parameter from `PyObjectAlloc` -- `NoArgs`. Just use an empty tuple -- `PyObjectWithGIL`. `PyNativeType` is sufficient now that PyToken is removed. - -### Fixed - -- A soudness hole where every instances of a `#[pyclass]` struct was considered to be part of a python object, even though you can create instances that are not part of the python heap. This was fixed through `PyRef` and `PyRefMut`. -- Fix kwargs support in [#328](https://github.com/PyO3/pyo3/pull/328). -- Add full support for `__dict__` in [#403](https://github.com/PyO3/pyo3/pull/403). - -## [0.5.3] - 2019-01-04 - -### Fixed - -- Fix memory leak in ArrayList by kngwyu [#316](https://github.com/PyO3/pyo3/pull/316) - -## [0.5.2] - 2018-11-25 - -### Fixed - -- Fix undeterministic segfaults when creating many objects by kngwyu in [#281](https://github.com/PyO3/pyo3/pull/281) - -## [0.5.1] - 2018-11-24 - -Yanked - -## [0.5.0] - 2018-11-11 - -### Added - -- `#[pyclass]` objects can now be returned from rust functions -- `PyComplex` by kngwyu in [#226](https://github.com/PyO3/pyo3/pull/226) -- `PyDict::from_sequence`, equivalent to `dict([(key, val), ...])` -- Bindings for the `datetime` standard library types: `PyDate`, `PyTime`, `PyDateTime`, `PyTzInfo`, `PyDelta` with associated `ffi` types, by pganssle [#200](https://github.com/PyO3/pyo3/pull/200). -- `PyString`, `PyUnicode`, and `PyBytes` now have an `as_bytes` method that returns `&[u8]`. -- `PyObjectProtocol::get_type_ptr` by ijl in [#242](https://github.com/PyO3/pyo3/pull/242) - -### Changed - -- Removes the types from the root module and the prelude. They now live in `pyo3::types` instead. -- All exceptions are constructed with `py_err` instead of `new`, as they return `PyErr` and not `Self`. -- `as_mut` and friends take and `&mut self` instead of `&self` -- `ObjectProtocol::call` now takes an `Option<&PyDict>` for the kwargs instead of an `IntoPyDictPointer`. -- `IntoPyDictPointer` was replace by `IntoPyDict` which doesn't convert `PyDict` itself anymore and returns a `PyDict` instead of `*mut PyObject`. -- `PyTuple::new` now takes an `IntoIterator` instead of a slice -- Updated to syn 0.15 -- Splitted `PyTypeObject` into `PyTypeObject` without the create method and `PyTypeCreate` with requires `PyObjectAlloc + PyTypeInfo + Sized`. -- Ran `cargo edition --fix` which prefixed path with `crate::` for rust 2018 -- Renamed `async` to `pyasync` as async will be a keyword in the 2018 edition. -- Starting to use `NonNull<*mut PyObject>` for Py and PyObject by ijl [#260](https://github.com/PyO3/pyo3/pull/260) - -### Removed - -- Removed most entries from the prelude. The new prelude is small and clear. -- Slowly removing specialization uses -- `PyString`, `PyUnicode`, and `PyBytes` no longer have a `data` method - (replaced by `as_bytes`) and `PyStringData` has been removed. -- The pyobject_extract macro - -### Fixed - -- Added an explanation that the GIL can temporarily be released even while holding a GILGuard. -- Lots of clippy errors -- Fix segfault on calling an unknown method on a PyObject -- Work around a [bug](https://github.com/rust-lang/rust/issues/55380) in the rust compiler by kngwyu [#252](https://github.com/PyO3/pyo3/pull/252) -- Fixed a segfault with subclassing pyo3 create classes and using `__class__` by kngwyu [#263](https://github.com/PyO3/pyo3/pull/263) - -## [0.4.1] - 2018-08-20 - -### Changed - -- PyTryFrom's error is always to `PyDowncastError` - -### Fixed - -- Fixed compilation on nightly since `use_extern_macros` was stabilized - -### Removed - -- The pyobject_downcast macro - -## [0.4.0] - 2018-07-30 - -### Changed - -- Merged both examples into one -- Rustfmt all the things :heavy_check_mark: -- Switched to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - -### Removed - -- Conversions from tuples to PyDict due to [rust-lang/rust#52050](https://github.com/rust-lang/rust/issues/52050) - -## [0.3.2] - 2018-07-22 - -### Changed - -- Replaced `concat_idents` with mashup - -## [0.3.1] - 2018-07-18 - -### Fixed - -- Fixed scoping bug in pyobject_native_type that would break rust-numpy - -## [0.3.0] - 2018-07-18 - -### Added - -- A few internal macros became part of the public api ([#155](https://github.com/PyO3/pyo3/pull/155), [#186](https://github.com/PyO3/pyo3/pull/186)) -- Always clone in getters. This allows using the get-annotation on all Clone-Types - -### Changed - -- Upgraded to syn 0.14 which means much better error messages :tada: -- 128 bit integer support by [kngwyu](https://github.com/kngwyu) ([#137](https://github.com/PyO3/pyo3/pull/173)) -- `proc_macro` has been stabilized on nightly ([rust-lang/rust#52081](https://github.com/rust-lang/rust/pull/52081)). This means that we can remove the `proc_macro` feature, but now we need the `use_extern_macros` from the 2018 edition instead. -- All proc macro are now prefixed with `py` and live in the prelude. This means you can use `#[pyclass]`, `#[pymethods]`, `#[pyproto]`, `#[pyfunction]` and `#[pymodinit]` directly, at least after a `use pyo3::prelude::*`. They were also moved into a module called `proc_macro`. You shouldn't use `#[pyo3::proc_macro::pyclass]` or other longer paths in attributes because `proc_macro_path_invoc` isn't going to be stabilized soon. -- Renamed the `base` option in the `pyclass` macro to `extends`. -- `#[pymodinit]` uses the function name as module name, unless the name is overrriden with `#[pymodinit(name)]` -- The guide is now properly versioned. - -## [0.2.7] - 2018-05-18 - -### Fixed - -- Fix nightly breakage with proc_macro_path - -## [0.2.6] - 2018-04-03 - -### Fixed - -- Fix compatibility with TryFrom trait #137 - -## [0.2.5] - 2018-02-21 - -### Added - -- CPython 3.7 support - -### Fixed - -- Embedded CPython 3.7b1 crashes on initialization #110 -- Generated extension functions are weakly typed #108 -- call_method\* crashes when the method does not exist #113 -- Allow importing exceptions from nested modules #116 - -## [0.2.4] - 2018-01-19 - -### Added - -- Allow to get mutable ref from PyObject #106 -- Drop `RefFromPyObject` trait -- Add Python::register_any method - -### Fixed - -- Fix impl `FromPyObject` for `Py` -- Mark method that work with raw pointers as unsafe #95 - -## [0.2.3] - 11-27-2017 - -### Changed - -- Rustup to 1.23.0-nightly 2017-11-07 - -### Fixed - -- Proper `c_char` usage #93 - -### Removed - -- Remove use of now unneeded 'AsciiExt' trait - -## [0.2.2] - 09-26-2017 - -### Changed - -- Rustup to 1.22.0-nightly 2017-09-30 - -## [0.2.1] - 09-26-2017 - -### Fixed - -- Fix rustc const_fn nightly breakage - -## [0.2.0] - 08-12-2017 - -### Added - -- Added inheritance support #15 -- Added weakref support #56 -- Added subclass support #64 -- Added `self.__dict__` supoort #68 -- Added `pyo3::prelude` module #70 -- Better `Iterator` support for PyTuple, PyList, PyDict #75 -- Introduce IntoPyDictPointer similar to IntoPyTuple #69 - -### Changed - -- Allow to add gc support without implementing PyGCProtocol #57 -- Refactor `PyErr` implementation. Drop `py` parameter from constructor. - -## [0.1.0] - 07-23-2017 - -### Added - -- Initial release - -[Unreleased]: https://github.com/pyo3/pyo3/compare/v0.23.4...HEAD -[0.23.4]: https://github.com/pyo3/pyo3/compare/v0.23.3...v0.23.4 -[0.23.3]: https://github.com/pyo3/pyo3/compare/v0.23.2...v0.23.3 -[0.23.2]: https://github.com/pyo3/pyo3/compare/v0.23.1...v0.23.2 -[0.23.1]: https://github.com/pyo3/pyo3/compare/v0.23.0...v0.23.1 -[0.23.0]: https://github.com/pyo3/pyo3/compare/v0.22.5...v0.23.0 -[0.22.5]: https://github.com/pyo3/pyo3/compare/v0.22.4...v0.22.5 -[0.22.4]: https://github.com/pyo3/pyo3/compare/v0.22.3...v0.22.4 -[0.22.3]: https://github.com/pyo3/pyo3/compare/v0.22.2...v0.22.3 -[0.22.2]: https://github.com/pyo3/pyo3/compare/v0.22.1...v0.22.2 -[0.22.1]: https://github.com/pyo3/pyo3/compare/v0.22.0...v0.22.1 -[0.22.0]: https://github.com/pyo3/pyo3/compare/v0.21.2...v0.22.0 -[0.21.2]: https://github.com/pyo3/pyo3/compare/v0.21.1...v0.21.2 -[0.21.1]: https://github.com/pyo3/pyo3/compare/v0.21.0...v0.21.1 -[0.21.0]: https://github.com/pyo3/pyo3/compare/v0.20.3...v0.21.0 -[0.21.0-beta.0]: https://github.com/pyo3/pyo3/compare/v0.20.3...v0.21.0-beta.0 -[0.20.3]: https://github.com/pyo3/pyo3/compare/v0.20.2...v0.20.3 -[0.20.2]: https://github.com/pyo3/pyo3/compare/v0.20.1...v0.20.2 -[0.20.1]: https://github.com/pyo3/pyo3/compare/v0.20.0...v0.20.1 -[0.20.0]: https://github.com/pyo3/pyo3/compare/v0.19.2...v0.20.0 -[0.19.2]: https://github.com/pyo3/pyo3/compare/v0.19.1...v0.19.2 -[0.19.1]: https://github.com/pyo3/pyo3/compare/v0.19.0...v0.19.1 -[0.19.0]: https://github.com/pyo3/pyo3/compare/v0.18.3...v0.19.0 -[0.18.3]: https://github.com/pyo3/pyo3/compare/v0.18.2...v0.18.3 -[0.18.2]: https://github.com/pyo3/pyo3/compare/v0.18.1...v0.18.2 -[0.18.1]: https://github.com/pyo3/pyo3/compare/v0.18.0...v0.18.1 -[0.18.0]: https://github.com/pyo3/pyo3/compare/v0.17.3...v0.18.0 -[0.17.3]: https://github.com/pyo3/pyo3/compare/v0.17.2...v0.17.3 -[0.17.2]: https://github.com/pyo3/pyo3/compare/v0.17.1...v0.17.2 -[0.17.1]: https://github.com/pyo3/pyo3/compare/v0.17.0...v0.17.1 -[0.17.0]: https://github.com/pyo3/pyo3/compare/v0.16.6...v0.17.0 -[0.16.6]: https://github.com/pyo3/pyo3/compare/v0.16.5...v0.16.6 -[0.16.5]: https://github.com/pyo3/pyo3/compare/v0.16.4...v0.16.5 -[0.16.4]: https://github.com/pyo3/pyo3/compare/v0.16.3...v0.16.4 -[0.16.3]: https://github.com/pyo3/pyo3/compare/v0.16.2...v0.16.3 -[0.16.2]: https://github.com/pyo3/pyo3/compare/v0.16.1...v0.16.2 -[0.16.1]: https://github.com/pyo3/pyo3/compare/v0.16.0...v0.16.1 -[0.16.0]: https://github.com/pyo3/pyo3/compare/v0.15.1...v0.16.0 -[0.15.2]: https://github.com/pyo3/pyo3/compare/v0.15.1...v0.15.2 -[0.15.1]: https://github.com/pyo3/pyo3/compare/v0.15.0...v0.15.1 -[0.15.0]: https://github.com/pyo3/pyo3/compare/v0.14.5...v0.15.0 -[0.14.5]: https://github.com/pyo3/pyo3/compare/v0.14.4...v0.14.5 -[0.14.4]: https://github.com/pyo3/pyo3/compare/v0.14.3...v0.14.4 -[0.14.3]: https://github.com/pyo3/pyo3/compare/v0.14.2...v0.14.3 -[0.14.2]: https://github.com/pyo3/pyo3/compare/v0.14.1...v0.14.2 -[0.14.1]: https://github.com/pyo3/pyo3/compare/v0.14.0...v0.14.1 -[0.14.0]: https://github.com/pyo3/pyo3/compare/v0.13.2...v0.14.0 -[0.13.2]: https://github.com/pyo3/pyo3/compare/v0.13.1...v0.13.2 -[0.13.1]: https://github.com/pyo3/pyo3/compare/v0.13.0...v0.13.1 -[0.13.0]: https://github.com/pyo3/pyo3/compare/v0.12.4...v0.13.0 -[0.12.4]: https://github.com/pyo3/pyo3/compare/v0.12.3...v0.12.4 -[0.12.3]: https://github.com/pyo3/pyo3/compare/v0.12.2...v0.12.3 -[0.12.2]: https://github.com/pyo3/pyo3/compare/v0.12.1...v0.12.2 -[0.12.1]: https://github.com/pyo3/pyo3/compare/v0.12.0...v0.12.1 -[0.12.0]: https://github.com/pyo3/pyo3/compare/v0.11.1...v0.12.0 -[0.11.1]: https://github.com/pyo3/pyo3/compare/v0.11.0...v0.11.1 -[0.11.0]: https://github.com/pyo3/pyo3/compare/v0.10.1...v0.11.0 -[0.10.1]: https://github.com/pyo3/pyo3/compare/v0.10.0...v0.10.1 -[0.10.0]: https://github.com/pyo3/pyo3/compare/v0.9.2...v0.10.0 -[0.9.2]: https://github.com/pyo3/pyo3/compare/v0.9.1...v0.9.2 -[0.9.1]: https://github.com/pyo3/pyo3/compare/v0.9.0...v0.9.1 -[0.9.0]: https://github.com/pyo3/pyo3/compare/v0.8.5...v0.9.0 -[0.8.5]: https://github.com/pyo3/pyo3/compare/v0.8.4...v0.8.5 -[0.8.4]: https://github.com/pyo3/pyo3/compare/v0.8.3...v0.8.4 -[0.8.3]: https://github.com/pyo3/pyo3/compare/v0.8.2...v0.8.3 -[0.8.2]: https://github.com/pyo3/pyo3/compare/v0.8.1...v0.8.2 -[0.8.1]: https://github.com/pyo3/pyo3/compare/v0.8.0...v0.8.1 -[0.8.0]: https://github.com/pyo3/pyo3/compare/v0.7.0...v0.8.0 -[0.7.0]: https://github.com/pyo3/pyo3/compare/v0.6.0...v0.7.0 -[0.6.0]: https://github.com/pyo3/pyo3/compare/v0.5.3...v0.6.0 -[0.5.3]: https://github.com/pyo3/pyo3/compare/v0.5.2...v0.5.3 -[0.5.2]: https://github.com/pyo3/pyo3/compare/v0.5.1...v0.5.2 -[0.5.1]: https://github.com/pyo3/pyo3/compare/v0.5.0...v0.5.1 -[0.5.0]: https://github.com/pyo3/pyo3/compare/v0.4.1...v0.5.0 -[0.4.1]: https://github.com/pyo3/pyo3/compare/v0.4.0...v0.4.1 -[0.4.0]: https://github.com/pyo3/pyo3/compare/v0.3.2...v0.4.0 -[0.3.2]: https://github.com/pyo3/pyo3/compare/v0.3.1...v0.3.2 -[0.3.1]: https://github.com/pyo3/pyo3/compare/v0.3.0...v0.3.1 -[0.3.0]: https://github.com/pyo3/pyo3/compare/v0.2.7...v0.3.0 -[0.2.7]: https://github.com/pyo3/pyo3/compare/v0.2.6...v0.2.7 -[0.2.6]: https://github.com/pyo3/pyo3/compare/v0.2.5...v0.2.6 -[0.2.5]: https://github.com/pyo3/pyo3/compare/v0.2.4...v0.2.5 -[0.2.4]: https://github.com/pyo3/pyo3/compare/v0.2.3...v0.2.4 -[0.2.3]: https://github.com/pyo3/pyo3/compare/v0.2.2...v0.2.3 -[0.2.2]: https://github.com/pyo3/pyo3/compare/v0.2.1...v0.2.2 -[0.2.1]: https://github.com/pyo3/pyo3/compare/v0.2.0...v0.2.1 -[0.2.0]: https://github.com/pyo3/pyo3/compare/v0.1.0...v0.2.0 -[0.1.0]: https://github.com/PyO3/pyo3/tree/0.1.0 diff --git a/vendor/pyo3/CITATION.cff b/vendor/pyo3/CITATION.cff deleted file mode 100644 index dfba070166..0000000000 --- a/vendor/pyo3/CITATION.cff +++ /dev/null @@ -1,12 +0,0 @@ -cff-version: 1.2.0 -title: PyO3 -message: >- - If you use this software as part of a publication and wish to cite - it, please use the metadata from this file. -type: software -authors: - - name: PyO3 Project and Contributors - website: https://github.com/PyO3 -license: - - Apache-2.0 - - MIT diff --git a/vendor/pyo3/Cargo.toml b/vendor/pyo3/Cargo.toml deleted file mode 100644 index 22c18228c4..0000000000 --- a/vendor/pyo3/Cargo.toml +++ /dev/null @@ -1,180 +0,0 @@ -[package] -name = "pyo3" -version = "0.23.4" -description = "Bindings to Python interpreter" -authors = ["PyO3 Project and Contributors "] -readme = "README.md" -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -documentation = "https://docs.rs/crate/pyo3/" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -exclude = ["/.gitignore", ".cargo/config", "/codecov.yml", "/Makefile", "/pyproject.toml", "/noxfile.py", "/.github", "/tests/test_compile_error.rs", "/tests/ui"] -edition = "2021" -rust-version = "1.63" - -[dependencies] -cfg-if = "1.0" -libc = "0.2.62" -memoffset = "0.9" -once_cell = "1.13" - -# ffi bindings to the python interpreter, split into a separate crate so they can be used independently -pyo3-ffi = { path = "pyo3-ffi", version = "=0.23.4" } - -# support crates for macros feature -pyo3-macros = { path = "pyo3-macros", version = "=0.23.4", optional = true } -indoc = { version = "2.0.1", optional = true } -unindent = { version = "0.2.1", optional = true } - -# support crate for multiple-pymethods feature -inventory = { version = "0.3.0", optional = true } - -# crate integrations that can be added using the eponymous features -anyhow = { version = "1.0.1", optional = true } -chrono = { version = "0.4.25", default-features = false, optional = true } -chrono-tz = { version = ">= 0.10, < 0.11", default-features = false, optional = true } -either = { version = "1.9", optional = true } -eyre = { version = ">= 0.6.8, < 0.7", optional = true } -hashbrown = { version = ">= 0.14.5, < 0.16", optional = true } -indexmap = { version = ">= 2.5.0, < 3", optional = true } -num-bigint = { version = "0.4.2", optional = true } -num-complex = { version = ">= 0.4.6, < 0.5", optional = true } -num-rational = {version = "0.4.1", optional = true } -rust_decimal = { version = "1.15", default-features = false, optional = true } -serde = { version = "1.0", optional = true } -smallvec = { version = "1.0", optional = true } - -[target.'cfg(not(target_has_atomic = "64"))'.dependencies] -portable-atomic = "1.0" - -[dev-dependencies] -assert_approx_eq = "1.1.0" -chrono = "0.4.25" -chrono-tz = ">= 0.10, < 0.11" -# Required for "and $N others" normalization -trybuild = ">=1.0.70" -proptest = { version = "1.0", default-features = false, features = ["std"] } -send_wrapper = "0.6" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0.61" -rayon = "1.6.1" -futures = "0.3.28" -tempfile = "3.12.0" -static_assertions = "1.1.0" -uuid = {version = "1.10.0", features = ["v4"] } - -[build-dependencies] -pyo3-build-config = { path = "pyo3-build-config", version = "=0.23.4", features = ["resolve-config"] } - -[features] -default = ["macros"] - -# Enables support for `async fn` for `#[pyfunction]` and `#[pymethods]`. -experimental-async = ["macros", "pyo3-macros/experimental-async"] - -# Enables pyo3::inspect module and additional type information on FromPyObject -# and IntoPy traits -experimental-inspect = [] - -# Enables macros: #[pyclass], #[pymodule], #[pyfunction] etc. -macros = ["pyo3-macros", "indoc", "unindent"] - -# Enables multiple #[pymethods] per #[pyclass] -multiple-pymethods = ["inventory", "pyo3-macros/multiple-pymethods"] - -# Use this feature when building an extension module. -# It tells the linker to keep the python symbols unresolved, -# so that the module can also be used with statically linked python interpreters. -extension-module = ["pyo3-ffi/extension-module"] - -# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more. -abi3 = ["pyo3-build-config/abi3", "pyo3-ffi/abi3"] - -# With abi3, we can manually set the minimum Python version. -abi3-py37 = ["abi3-py38", "pyo3-build-config/abi3-py37", "pyo3-ffi/abi3-py37"] -abi3-py38 = ["abi3-py39", "pyo3-build-config/abi3-py38", "pyo3-ffi/abi3-py38"] -abi3-py39 = ["abi3-py310", "pyo3-build-config/abi3-py39", "pyo3-ffi/abi3-py39"] -abi3-py310 = ["abi3-py311", "pyo3-build-config/abi3-py310", "pyo3-ffi/abi3-py310"] -abi3-py311 = ["abi3-py312", "pyo3-build-config/abi3-py311", "pyo3-ffi/abi3-py311"] -abi3-py312 = ["abi3", "pyo3-build-config/abi3-py312", "pyo3-ffi/abi3-py312"] - -# Automatically generates `python3.dll` import libraries for Windows targets. -generate-import-lib = ["pyo3-ffi/generate-import-lib"] - -# Changes `Python::with_gil` to automatically initialize the Python interpreter if needed. -auto-initialize = [] - -# Enables `Clone`ing references to Python objects `Py` which panics if the GIL is not held. -py-clone = [] - -# Optimizes PyObject to Vec conversion and so on. -nightly = [] - -# Activates all additional features -# This is mostly intended for testing purposes - activating *all* of these isn't particularly useful. -full = [ - "macros", - # "multiple-pymethods", # Not supported by wasm - "anyhow", - "chrono", - "chrono-tz", - "either", - "experimental-async", - "experimental-inspect", - "eyre", - "hashbrown", - "indexmap", - "num-bigint", - "num-complex", - "num-rational", - "py-clone", - "rust_decimal", - "serde", - "smallvec", -] - -[workspace] -members = [ - "pyo3-ffi", - "pyo3-build-config", - "pyo3-macros", - "pyo3-macros-backend", - "pytests", - "examples", -] - -[package.metadata.docs.rs] -no-default-features = true -features = ["full"] -rustdoc-args = ["--cfg", "docsrs"] - -[workspace.lints.clippy] -checked_conversions = "warn" -dbg_macro = "warn" -explicit_into_iter_loop = "warn" -explicit_iter_loop = "warn" -filter_map_next = "warn" -flat_map_option = "warn" -let_unit_value = "warn" -manual_assert = "warn" -manual_ok_or = "warn" -todo = "warn" -unnecessary_wraps = "warn" -useless_transmute = "warn" -used_underscore_binding = "warn" - -[workspace.lints.rust] -elided_lifetimes_in_paths = "warn" -invalid_doc_attributes = "warn" -rust_2018_idioms = { level = "warn", priority = -1 } -rust_2021_prelude_collisions = "warn" -unused_lifetimes = "warn" - -[workspace.lints.rustdoc] -broken_intra_doc_links = "warn" -bare_urls = "warn" - -[lints] -workspace = true diff --git a/vendor/pyo3/Code-of-Conduct.md b/vendor/pyo3/Code-of-Conduct.md deleted file mode 100644 index c06c62548b..0000000000 --- a/vendor/pyo3/Code-of-Conduct.md +++ /dev/null @@ -1,77 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, sex characteristics, gender identity and expression, -level of experience, education, socio-economic status, nationality, personal -appearance, race, religion, or sexual identity and orientation. - -## Our Standards - -Examples of behavior that contributes to creating a positive environment -include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or - advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -## Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. - -## Scope - -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team. All -complaints will be reviewed and investigated and will result in a response that -is deemed necessary and appropriate to the circumstances. The project team is -obligated to maintain confidentiality with regard to the reporter of an incident. -Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, -available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html - -[homepage]: https://www.contributor-covenant.org - -For answers to common questions about this code of conduct, see -https://www.contributor-covenant.org/faq - diff --git a/vendor/pyo3/Contributing.md b/vendor/pyo3/Contributing.md deleted file mode 100644 index 76af08325f..0000000000 --- a/vendor/pyo3/Contributing.md +++ /dev/null @@ -1,239 +0,0 @@ -# Contributing - -Thank you for your interest in contributing to PyO3! All are welcome - please consider reading our [Code of Conduct](https://github.com/PyO3/pyo3/blob/main/Code-of-Conduct.md) to keep our community positive and inclusive. - -If you are searching for ideas how to contribute, proceed to the ["Getting started contributing"](#getting-started-contributing) section. If you have found a specific issue to contribute to and need information about the development process, you may find the section ["Writing pull requests"](#writing-pull-requests) helpful. - -If you want to become familiar with the codebase, see -[Architecture.md](https://github.com/PyO3/pyo3/blob/main/Architecture.md). - -## Getting started contributing - -Please join in with any part of PyO3 which interests you. We use GitHub issues to record all bugs and ideas. Feel free to request an issue to be assigned to you if you want to work on it. - -You can browse the API of the non-public parts of PyO3 [here](https://pyo3.netlify.app/internal/doc/pyo3/index.html). - -The following sections also contain specific ideas on where to start contributing to PyO3. - -## Setting up a development environment - -To work and develop PyO3, you need Python & Rust installed on your system. -* We encourage the use of [rustup](https://rustup.rs/) to be able to select and choose specific toolchains based on the project. -* [Pyenv](https://github.com/pyenv/pyenv) is also highly recommended for being able to choose a specific Python version. -* [virtualenv](https://virtualenv.pypa.io/en/latest/) can also be used with or without Pyenv to use specific installed Python versions. -* [`nox`][nox] is used to automate many of our CI tasks. - -### Help users identify bugs - -The [PyO3 Discord server](https://discord.gg/33kcChzH7f) is very active with users who are new to PyO3, and often completely new to Rust. Helping them debug is a great way to get experience with the PyO3 codebase. - -Helping others often reveals bugs, documentation weaknesses, and missing APIs. It's a good idea to open GitHub issues for these immediately so the resolution can be designed and implemented! - -### Implement issues ready for development - -Issues where the solution is clear and work is not in progress use the [needs-implementer](https://github.com/PyO3/pyo3/issues?q=is%3Aissue+is%3Aopen+label%3Aneeds-implementer) label. - -Don't be afraid if the solution is not clear to you! The core PyO3 contributors will be happy to mentor you through any questions you have to help you write the solution. - -### Help write great docs - -PyO3 has a user guide (using mdbook) as well as the usual Rust API docs. The aim is for both of these to be detailed, easy to understand, and up-to-date. Pull requests are always welcome to fix typos, change wording, add examples, etc. - -There are some specific areas of focus where help is currently needed for the documentation: - -- Issues requesting documentation improvements are tracked with the [documentation](https://github.com/PyO3/pyo3/issues?q=is%3Aissue+is%3Aopen+label%3Adocumentation) label. -- Not all APIs had docs or examples when they were made. The goal is to have documentation on all PyO3 APIs ([#306](https://github.com/PyO3/pyo3/issues/306)). If you see an API lacking a doc, please write one and open a PR! - -To build the docs (including all features), install [`nox`][nox] and then run - -```shell -nox -s docs -- open -``` - -#### Doctests - -We use lots of code blocks in our docs. Run `cargo test --doc` when making changes to check that -the doctests still work, or `cargo test` to run all the Rust tests including doctests. See -https://doc.rust-lang.org/rustdoc/documentation-tests.html for a guide on doctests. - -#### Building the guide - -You can preview the user guide by building it locally with `mdbook`. - -First, install [`mdbook`][mdbook] and [`nox`][nox]. Then, run - -```shell -nox -s build-guide -- --open -``` - -To check all links in the guide are valid, also install [`lychee`][lychee] and use the `check-guide` session instead: - -```shell -nox -s check-guide -``` - -### Help design the next PyO3 - -Issues which don't yet have a clear solution use the [needs-design](https://github.com/PyO3/pyo3/issues?q=is%3Aissue+is%3Aopen+label%3Aneeds-design) label. - -If any of these issues interest you, please join in with the conversation on the issue! All opinions are valued, and if you're interested in going further with e.g. draft PRs to experiment with API designs, even better! - -### Review pull requests - -Everybody is welcome to submit comments on open PRs. Please help ensure new PyO3 APIs are safe, performant, tidy, and easy to use! - -## Writing pull requests - -Here are a few things to note when you are writing PRs. - -### Testing and Continuous Integration - -The PyO3 repo uses GitHub Actions. PRs are blocked from merging if CI is not successful. Formatting, linting and tests are checked for all Rust and Python code. In addition, all warnings in Rust code are disallowed (using `RUSTFLAGS="-D warnings"`). - -Tests run with all supported Python versions with the latest stable Rust compiler, as well as for Python 3.9 with the minimum supported Rust version. - -If you are adding a new feature, you should add it to the `full` feature in our *Cargo.toml** so that it is tested in CI. - -You can run these checks yourself with `nox`. Use `nox -l` to list the full set of subcommands you can run. - -#### Linting Python code -`nox -s ruff` - -#### Linting Rust code -`nox -s rustfmt` - -#### Semver checks -`cargo semver-checks check-release` - -#### Clippy -`nox -s clippy-all` - -#### Tests -`nox -s test` or `cargo test` for Rust tests only, `nox -f pytests/noxfile.py -s test` for Python tests only - -#### Check all conditional compilation -`nox -s check-feature-powerset` - -#### UI Tests - -PyO3 uses [`trybuild`](https://github.com/dtolnay/trybuild) to develop UI tests to capture error messages from the Rust compiler for some of the macro functionality. - -Because there are several feature combinations for these UI tests, when updating them all (e.g. for a new Rust compiler version) it may be helpful to use the `update-ui-tests` nox session: - -```bash -nox -s update-ui-tests -``` - -### Documenting changes - -We use [towncrier](https://towncrier.readthedocs.io/en/stable/index.html) to generate a CHANGELOG for each release. - -To include your changes in the release notes, you should create one (or more) news items in the `newsfragments` directory. Valid news items should be saved as `..md` where `` is the pull request number and `` is one of the following: -- `packaging` - for dependency changes and Python / Rust version compatibility changes -- `added` - for new features -- `changed` - for features which already existed but have been altered or deprecated -- `removed` - for features which have been removed -- `fixed` - for "changed" features which were classed as a bugfix - -Docs-only PRs do not need news items; start your PR title with `docs:` to skip the check. - -### Style guide - -#### Generic code - -PyO3 has a lot of generic APIs to increase usability. These can come at the cost of generic code bloat. Where reasonable, try to implement a concrete sub-portion of generic functions. There are two forms of this: - -- If the concrete sub-portion doesn't benefit from re-use by other functions, name it `inner` and keep it as a local to the function. -- If the concrete sub-portion is re-used by other functions, preferably name it `_foo` and place it directly below `foo` in the source code (where `foo` is the original generic function). - -#### FFI calls - -PyO3 makes a lot of FFI calls to Python's C API using raw pointers. Where possible try to avoid using pointers-to-temporaries in expressions: - -```rust -// dangerous -pyo3::ffi::Something(name.to_object(py).as_ptr()); - -// because the following refactoring is a use-after-free error: -let name = name.to_object(py).as_ptr(); -pyo3::ffi::Something(name) -``` - -Instead, prefer to bind the safe owned `PyObject` wrapper before passing to ffi functions: - -```rust -let name: PyObject = name.to_object(py); -pyo3::ffi::Something(name.as_ptr()) -// name will automatically be freed when it falls out of scope -``` - -## Python and Rust version support policy - -PyO3 aims to keep sufficient compatibility to make packaging Python extensions built with PyO3 feasible on most common package managers. - -To keep package maintainers' lives simpler, PyO3 will commit, wherever possible, to only adjust minimum supported Rust and Python versions at the same time. This bump will only come in an `0.x` release, roughly once per year, after the oldest supported Python version reaches its end-of-life. (Check https://endoflife.date/python for a clear timetable on these.) - -Below are guidelines on what compatibility all PRs are expected to deliver for each language. - -### Python - -PyO3 supports all officially supported Python versions, as well as the latest PyPy3 release. All of these versions are tested in CI. - -### Rust - -PyO3 aims to make use of up-to-date Rust language features to keep the implementation as efficient as possible. - -The minimum Rust version supported will be decided when the release which bumps Python and Rust versions is made. At the time, the minimum Rust version will be set no higher than the lowest Rust version shipped in the current Debian, RHEL and Alpine Linux distributions. - -CI tests both the most recent stable Rust version and the minimum supported Rust version. Because of Rust's stability guarantees this is sufficient to confirm support for all Rust versions in between. - -## Benchmarking - -PyO3 has two sets of benchmarks for evaluating some aspects of its performance. The benchmark suite is currently very small - please open PRs with new benchmarks if you're interested in helping to expand it! - -First, there are Rust-based benchmarks located in the `pyo3-benches` subdirectory. You can run these benchmarks with: - - nox -s bench - -Second, there is a Python-based benchmark contained in the `pytests` subdirectory. You can read more about it [here](https://github.com/PyO3/pyo3/tree/main/pytests). - -## Code coverage - -You can view what code is and isn't covered by PyO3's tests. We aim to have 100% coverage - please check coverage and add tests if you notice a lack of coverage! - -- First, ensure the llvm-cov cargo plugin is installed. You may need to run the plugin through cargo once before using it with `nox`. -```shell -cargo install cargo-llvm-cov -cargo llvm-cov -``` -- Then, generate an `lcov.info` file with -```shell -nox -s coverage -- lcov -``` -You can install an IDE plugin to view the coverage. For example, if you use VSCode: -- Add the [coverage-gutters](https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters) plugin. -- Add these settings to VSCode's `settings.json`: -```json -{ - "coverage-gutters.coverageFileNames": [ - "lcov.info", - "cov.xml", - "coverage.xml", - ], - "coverage-gutters.showLineCoverage": true -} -``` -- You should now be able to see green highlights for code that is tested, and red highlights for code that is not tested. - -## Sponsor this project - -At the moment there is no official organisation that accepts sponsorship on PyO3's behalf. If you're seeking to provide significant funding to the PyO3 ecosystem, please reach out to us on [GitHub](https://github.com/PyO3/pyo3/issues/new) or [Discord](https://discord.gg/33kcChzH7f) and we can discuss. - -In the meanwhile, some of our maintainers have personal GitHub sponsorship pages and would be grateful for your support: - -- [davidhewitt](https://github.com/sponsors/davidhewitt) -- [messense](https://github.com/sponsors/messense) - -[mdbook]: https://rust-lang.github.io/mdBook/cli/index.html -[lychee]: https://github.com/lycheeverse/lychee -[nox]: https://github.com/theacodes/nox diff --git a/vendor/pyo3/LICENSE-APACHE b/vendor/pyo3/LICENSE-APACHE deleted file mode 100644 index 72207b851d..0000000000 --- a/vendor/pyo3/LICENSE-APACHE +++ /dev/null @@ -1,178 +0,0 @@ -Copyright (c) 2017-present PyO3 Project and Contributors. https://github.com/PyO3 - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS diff --git a/vendor/pyo3/LICENSE-MIT b/vendor/pyo3/LICENSE-MIT deleted file mode 100644 index cd0ad009a8..0000000000 --- a/vendor/pyo3/LICENSE-MIT +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2023-present PyO3 Project and Contributors. https://github.com/PyO3 - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/vendor/pyo3/README.md b/vendor/pyo3/README.md deleted file mode 100644 index ece3524816..0000000000 --- a/vendor/pyo3/README.md +++ /dev/null @@ -1,278 +0,0 @@ -# PyO3 - -[![actions status](https://img.shields.io/github/actions/workflow/status/PyO3/pyo3/ci.yml?branch=main&logo=github&style=)](https://github.com/PyO3/pyo3/actions) -[![benchmark](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/PyO3/pyo3) -[![codecov](https://img.shields.io/codecov/c/gh/PyO3/pyo3?logo=codecov)](https://codecov.io/gh/PyO3/pyo3) -[![crates.io](https://img.shields.io/crates/v/pyo3?logo=rust)](https://crates.io/crates/pyo3) -[![minimum rustc 1.63](https://img.shields.io/badge/rustc-1.63+-blue?logo=rust)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) -[![discord server](https://img.shields.io/discord/1209263839632424990?logo=discord)](https://discord.gg/33kcChzH7f) -[![contributing notes](https://img.shields.io/badge/contribute-on%20github-Green?logo=github)](https://github.com/PyO3/pyo3/blob/main/Contributing.md) - -[Rust](https://www.rust-lang.org/) bindings for [Python](https://www.python.org/), including tools for creating native Python extension modules. Running and interacting with Python code from a Rust binary is also supported. - -- User Guide: [stable](https://pyo3.rs) | [main](https://pyo3.rs/main) - -- API Documentation: [stable](https://docs.rs/pyo3/) | [main](https://pyo3.rs/main/doc) - -## Usage - -Requires Rust 1.63 or greater. - -PyO3 supports the following Python distributions: - - CPython 3.7 or greater - - PyPy 7.3 (Python 3.9+) - - GraalPy 24.0 or greater (Python 3.10+) - -You can use PyO3 to write a native Python module in Rust, or to embed Python in a Rust binary. The following sections explain each of these in turn. - -### Using Rust from Python - -PyO3 can be used to generate a native Python module. The easiest way to try this out for the first time is to use [`maturin`](https://github.com/PyO3/maturin). `maturin` is a tool for building and publishing Rust-based Python packages with minimal configuration. The following steps install `maturin`, use it to generate and build a new Python package, and then launch Python to import and execute a function from the package. - -First, follow the commands below to create a new directory containing a new Python `virtualenv`, and install `maturin` into the virtualenv using Python's package manager, `pip`: - -```bash -# (replace string_sum with the desired package name) -$ mkdir string_sum -$ cd string_sum -$ python -m venv .env -$ source .env/bin/activate -$ pip install maturin -``` - -Still inside this `string_sum` directory, now run `maturin init`. This will generate the new package source. When given the choice of bindings to use, select pyo3 bindings: - -```bash -$ maturin init -✔ 🤷 What kind of bindings to use? · pyo3 - ✨ Done! New project created string_sum -``` - -The most important files generated by this command are `Cargo.toml` and `lib.rs`, which will look roughly like the following: - -**`Cargo.toml`** - -```toml -[package] -name = "string_sum" -version = "0.1.0" -edition = "2021" - -[lib] -# The name of the native library. This is the name which will be used in Python to import the -# library (i.e. `import string_sum`). If you change this, you must also change the name of the -# `#[pymodule]` in `src/lib.rs`. -name = "string_sum" -# "cdylib" is necessary to produce a shared library for Python to import from. -# -# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able -# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: -# crate-type = ["cdylib", "rlib"] -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "0.23.4", features = ["extension-module"] } -``` - -**`src/lib.rs`** - -```rust -use pyo3::prelude::*; - -/// Formats the sum of two numbers as string. -#[pyfunction] -fn sum_as_string(a: usize, b: usize) -> PyResult { - Ok((a + b).to_string()) -} - -/// A Python module implemented in Rust. The name of this function must match -/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to -/// import the module. -#[pymodule] -fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; - Ok(()) -} -``` - -Finally, run `maturin develop`. This will build the package and install it into the Python virtualenv previously created and activated. The package is then ready to be used from `python`: - -```bash -$ maturin develop -# lots of progress output as maturin runs the compilation... -$ python ->>> import string_sum ->>> string_sum.sum_as_string(5, 20) -'25' -``` - -To make changes to the package, just edit the Rust source code and then re-run `maturin develop` to recompile. - -To run this all as a single copy-and-paste, use the bash script below (replace `string_sum` in the first command with the desired package name): - -```bash -mkdir string_sum && cd "$_" -python -m venv .env -source .env/bin/activate -pip install maturin -maturin init --bindings pyo3 -maturin develop -``` - -If you want to be able to run `cargo test` or use this project in a Cargo workspace and are running into linker issues, there are some workarounds in [the FAQ](https://pyo3.rs/latest/faq.html#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror). - -As well as with `maturin`, it is possible to build using [`setuptools-rust`](https://github.com/PyO3/setuptools-rust) or [manually](https://pyo3.rs/latest/building-and-distribution.html#manual-builds). Both offer more flexibility than `maturin` but require more configuration to get started. - -### Using Python from Rust - -To embed Python into a Rust binary, you need to ensure that your Python installation contains a shared library. The following steps demonstrate how to ensure this (for Ubuntu), and then give some example code which runs an embedded Python interpreter. - -To install the Python shared library on Ubuntu: - -```bash -sudo apt install python3-dev -``` - -To install the Python shared library on RPM based distributions (e.g. Fedora, Red Hat, SuSE), install the `python3-devel` package. - - -Start a new project with `cargo new` and add `pyo3` to the `Cargo.toml` like this: - -```toml -[dependencies.pyo3] -version = "0.23.4" -features = ["auto-initialize"] -``` - -Example program displaying the value of `sys.version` and the current user name: - -```rust -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; -use pyo3::ffi::c_str; - -fn main() -> PyResult<()> { - Python::with_gil(|py| { - let sys = py.import("sys")?; - let version: String = sys.getattr("version")?.extract()?; - - let locals = [("os", py.import("os")?)].into_py_dict(py)?; - let code = c_str!("os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'"); - let user: String = py.eval(code, None, Some(&locals))?.extract()?; - - println!("Hello {}, I'm Python {}", user, version); - Ok(()) - }) -} -``` - -The guide has [a section](https://pyo3.rs/latest/python-from-rust.html) with lots of examples -about this topic. - -## Tools and libraries - -- [maturin](https://github.com/PyO3/maturin) _Build and publish crates with pyo3, rust-cpython or cffi bindings as well as rust binaries as python packages_ -- [setuptools-rust](https://github.com/PyO3/setuptools-rust) _Setuptools plugin for Rust support_. -- [pyo3-built](https://github.com/PyO3/pyo3-built) _Simple macro to expose metadata obtained with the [`built`](https://crates.io/crates/built) crate as a [`PyDict`](https://docs.rs/pyo3/*/pyo3/types/struct.PyDict.html)_ -- [rust-numpy](https://github.com/PyO3/rust-numpy) _Rust binding of NumPy C-API_ -- [dict-derive](https://github.com/gperinazzo/dict-derive) _Derive FromPyObject to automatically transform Python dicts into Rust structs_ -- [pyo3-log](https://github.com/vorner/pyo3-log) _Bridge from Rust to Python logging_ -- [pythonize](https://github.com/davidhewitt/pythonize) _Serde serializer for converting Rust objects to JSON-compatible Python objects_ -- [pyo3-asyncio](https://github.com/awestlake87/pyo3-asyncio) _Utilities for working with Python's Asyncio library and async functions_ -- [rustimport](https://github.com/mityax/rustimport) _Directly import Rust files or crates from Python, without manual compilation step. Provides pyo3 integration by default and generates pyo3 binding code automatically._ -- [pyo3-arrow](https://crates.io/crates/pyo3-arrow) _Lightweight [Apache Arrow](https://arrow.apache.org/) integration for pyo3._ - -## Examples - -- [autopy](https://github.com/autopilot-rs/autopy) _A simple, cross-platform GUI automation library for Python and Rust._ - - Contains an example of building wheels on TravisCI and appveyor using [cibuildwheel](https://github.com/pypa/cibuildwheel) -- [ballista-python](https://github.com/apache/arrow-ballista-python) _A Python library that binds to Apache Arrow distributed query engine Ballista._ -- [bed-reader](https://github.com/fastlmm/bed-reader) _Read and write the PLINK BED format, simply and efficiently._ - - Shows Rayon/ndarray::parallel (including capturing errors, controlling thread num), Python types to Rust generics, Github Actions -- [cellular_raza](https://cellular-raza.com) _A cellular agent-based simulation framework for building complex models from a clean slate._ -- [connector-x](https://github.com/sfu-db/connector-x) _Fastest library to load data from DB to DataFrames in Rust and Python._ -- [cryptography](https://github.com/pyca/cryptography/tree/main/src/rust) _Python cryptography library with some functionality in Rust._ -- [css-inline](https://github.com/Stranger6667/css-inline/tree/master/bindings/python) _CSS inlining for Python implemented in Rust._ -- [datafusion-python](https://github.com/apache/arrow-datafusion-python) _A Python library that binds to Apache Arrow in-memory query engine DataFusion._ -- [deltalake-python](https://github.com/delta-io/delta-rs/tree/main/python) _Native Delta Lake Python binding based on delta-rs with Pandas integration._ -- [fastbloom](https://github.com/yankun1992/fastbloom) _A fast [bloom filter](https://github.com/yankun1992/fastbloom#BloomFilter) | [counting bloom filter](https://github.com/yankun1992/fastbloom#countingbloomfilter) implemented by Rust for Rust and Python!_ -- [fastuuid](https://github.com/thedrow/fastuuid/) _Python bindings to Rust's UUID library._ -- [feos](https://github.com/feos-org/feos) _Lightning fast thermodynamic modeling in Rust with fully developed Python interface._ -- [forust](https://github.com/jinlow/forust) _A lightweight gradient boosted decision tree library written in Rust._ -- [granian](https://github.com/emmett-framework/granian) _A Rust HTTP server for Python applications._ -- [greptimedb](https://github.com/GreptimeTeam/greptimedb/tree/main/src/script) _Support [Python scripting](https://docs.greptime.com/user-guide/python-scripts/overview) in the database_ -- [haem](https://github.com/BooleanCat/haem) _A Python library for working on Bioinformatics problems._ -- [html2text-rs](https://github.com/deedy5/html2text_rs) _Python library for converting HTML to markup or plain text._ -- [html-py-ever](https://github.com/PyO3/setuptools-rust/tree/main/examples/html-py-ever) _Using [html5ever](https://github.com/servo/html5ever) through [kuchiki](https://github.com/kuchiki-rs/kuchiki) to speed up html parsing and css-selecting._ -- [hyperjson](https://github.com/mre/hyperjson) _A hyper-fast Python module for reading/writing JSON data using Rust's serde-json._ -- [inline-python](https://github.com/fusion-engineering/inline-python) _Inline Python code directly in your Rust code._ -- [johnnycanencrypt](https://github.com/kushaldas/johnnycanencrypt) OpenPGP library with Yubikey support. -- [jsonschema](https://github.com/Stranger6667/jsonschema/tree/master/crates/jsonschema-py) _A high-performance JSON Schema validator for Python._ -- [mocpy](https://github.com/cds-astro/mocpy) _Astronomical Python library offering data structures for describing any arbitrary coverage regions on the unit sphere._ -- [opendal](https://github.com/apache/opendal/tree/main/bindings/python) _A data access layer that allows users to easily and efficiently retrieve data from various storage services in a unified way._ -- [orjson](https://github.com/ijl/orjson) _Fast Python JSON library._ -- [ormsgpack](https://github.com/aviramha/ormsgpack) _Fast Python msgpack library._ -- [point-process](https://github.com/ManifoldFR/point-process-rust/tree/master/pylib) _High level API for pointprocesses as a Python library._ -- [polaroid](https://github.com/daggy1234/polaroid) _Hyper Fast and safe image manipulation library for Python written in Rust._ -- [polars](https://github.com/pola-rs/polars) _Fast multi-threaded DataFrame library in Rust | Python | Node.js._ -- [pydantic-core](https://github.com/pydantic/pydantic-core) _Core validation logic for pydantic written in Rust._ -- [pyheck](https://github.com/kevinheavey/pyheck) _Fast case conversion library, built by wrapping [heck](https://github.com/withoutboats/heck)._ - - Quite easy to follow as there's not much code. -- [pyre](https://github.com/Project-Dream-Weaver/pyre-http) _Fast Python HTTP server written in Rust._ -- [primp](https://github.com/deedy5/primp) _The fastest python HTTP client that can impersonate web browsers by mimicking their headers and TLS/JA3/JA4/HTTP2 fingerprints._ -- [rateslib](https://github.com/attack68/rateslib) _A fixed income library for Python using Rust extensions._ -- [ril-py](https://github.com/Cryptex-github/ril-py) _A performant and high-level image processing library for Python written in Rust._ -- [river](https://github.com/online-ml/river) _Online machine learning in python, the computationally heavy statistics algorithms are implemented in Rust._ -- [robyn](https://github.com/sparckles/Robyn) A Super Fast Async Python Web Framework with a Rust runtime. -- [rust-python-coverage](https://github.com/cjermain/rust-python-coverage) _Example PyO3 project with automated test coverage for Rust and Python._ -- [sail](https://github.com/lakehq/sail) _Unifying stream, batch, and AI workloads with Apache Spark compatibility._ -- [tiktoken](https://github.com/openai/tiktoken) _A fast BPE tokeniser for use with OpenAI's models._ -- [tokenizers](https://github.com/huggingface/tokenizers/tree/main/bindings/python) _Python bindings to the Hugging Face tokenizers (NLP) written in Rust._ -- [tzfpy](http://github.com/ringsaturn/tzfpy) _A fast package to convert longitude/latitude to timezone name._ -- [utiles](https://github.com/jessekrubin/utiles) _Fast Python web-map tile utilities_ -- [wasmer-python](https://github.com/wasmerio/wasmer-python) _Python library to run WebAssembly binaries._ - -## Articles and other media - -- [(Video) PyO3: From Python to Rust and Back Again](https://www.youtube.com/watch?v=UmL_CA-v3O8) - Jul 3, 2024 -- [Parsing Python ASTs 20x Faster with Rust](https://www.gauge.sh/blog/parsing-python-asts-20x-faster-with-rust) - Jun 17, 2024 -- [(Video) How Python Harnesses Rust through PyO3](https://www.youtube.com/watch?v=UkZ_m3Wj2hA) - May 18, 2024 -- [(Video) Combining Rust and Python: The Best of Both Worlds?](https://www.youtube.com/watch?v=lyG6AKzu4ew) - Mar 1, 2024 -- [(Video) Extending Python with Rust using PyO3](https://www.youtube.com/watch?v=T45ZEmSR1-s) - Dec 16, 2023 -- [A Week of PyO3 + rust-numpy (How to Speed Up Your Data Pipeline X Times)](https://terencezl.github.io/blog/2023/06/06/a-week-of-pyo3-rust-numpy/) - Jun 6, 2023 -- [(Podcast) PyO3 with David Hewitt](https://rustacean-station.org/episode/david-hewitt/) - May 19, 2023 -- [Making Python 100x faster with less than 100 lines of Rust](https://ohadravid.github.io/posts/2023-03-rusty-python/) - Mar 28, 2023 -- [How Pydantic V2 leverages Rust's Superpowers](https://fosdem.org/2023/schedule/event/rust_how_pydantic_v2_leverages_rusts_superpowers/) - Feb 4, 2023 -- [How we extended the River stats module with Rust using PyO3](https://boring-guy.sh/posts/river-rust/) - Dec 23, 2022 -- [Nine Rules for Writing Python Extensions in Rust](https://towardsdatascience.com/nine-rules-for-writing-python-extensions-in-rust-d35ea3a4ec29?sk=f8d808d5f414154fdb811e4137011437) - Dec 31, 2021 -- [Calling Rust from Python using PyO3](https://saidvandeklundert.net/learn/2021-11-18-calling-rust-from-python-using-pyo3/) - Nov 18, 2021 -- [davidhewitt's 2021 talk at Rust Manchester meetup](https://www.youtube.com/watch?v=-XyWG_klSAw&t=320s) - Aug 19, 2021 -- [Incrementally porting a small Python project to Rust](https://blog.waleedkhan.name/port-python-to-rust/) - Apr 29, 2021 -- [Vortexa - Integrating Rust into Python](https://www.vortexa.com/insight/integrating-rust-into-python) - Apr 12, 2021 -- [Writing and publishing a Python module in Rust](https://blog.yossarian.net/2020/08/02/Writing-and-publishing-a-python-module-in-rust) - Aug 2, 2020 - -## Contributing - -Everyone is welcomed to contribute to PyO3! There are many ways to support the project, such as: - -- help PyO3 users with issues on GitHub and [Discord](https://discord.gg/33kcChzH7f) -- improve documentation -- write features and bugfixes -- publish blogs and examples of how to use PyO3 - -Our [contributing notes](https://github.com/PyO3/pyo3/blob/main/Contributing.md) and [architecture guide](https://github.com/PyO3/pyo3/blob/main/Architecture.md) have more resources if you wish to volunteer time for PyO3 and are searching where to start. - -If you don't have time to contribute yourself but still wish to support the project's future success, some of our maintainers have GitHub sponsorship pages: - -- [davidhewitt](https://github.com/sponsors/davidhewitt) -- [messense](https://github.com/sponsors/messense) - -## License - -PyO3 is licensed under the [Apache-2.0 license](LICENSE-APACHE) or the [MIT license](LICENSE-MIT), at your option. - -Python is licensed under the [Python License](https://docs.python.org/3/license.html). - -Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in PyO3 by you, as defined in the Apache License, shall be dual-licensed as above, without any additional terms or conditions. - - Deploys by Netlify diff --git a/vendor/pyo3/Releasing.md b/vendor/pyo3/Releasing.md deleted file mode 100644 index 545783c598..0000000000 --- a/vendor/pyo3/Releasing.md +++ /dev/null @@ -1,57 +0,0 @@ -# Releasing - -This is notes for the current process of releasing a new PyO3 version. Replace `` in all instructions below with the new version. - -## 1. Prepare the release commit - -Follow the process below to update all required pieces to bump the version. All these changes are done in a single commit because it makes it clear to git readers what happened to bump the version. It also makes it easy to cherry-pick the version bump onto the `main` branch when tidying up branch history at the end of the release process. - -1. Replace all instances of the PyO3 current version and the with the new version to be released. Places to check: - - `Cargo.toml` for all PyO3 crates in the repository. - - Examples in `README.md` - - PyO3 version embedded into documentation like the README. - - `pre-script.rhai` templates for the examples. - - `[towncrier]` section in `pyproject.toml`. - - Some of the above locations may already have the new version with a `-dev` suffix, which needs to be removed. - - **Make sure not to modify the CHANGELOG during this step!** - -2. Run `towncrier build` to generate the CHANGELOG. The version used by `towncrier` should automatically be correct because of the update to `pyproject.toml` in step 1. - -3. Manually edit the CHANGELOG for final notes. Steps to do: - - Adjust wording of any release lines to make them clearer for users / fix typos. - - Add a new link at the bottom for the new version, and update the `Unreleased` link. - -4. Create the commit containing all the above changes, with a message of `release: `. Push to `release-` branch on the main PyO3 repository, where `` depends on whether this is a major or minor release: - - for O.X.0 minor releases, just use `0.X`, e.g. `release-0.17`. This will become the maintenance branch after release. - - for 0.X.Y patch releases, use the full `0.X.Y`, e.g. `release-0.17.1`. This will be deleted after merge. - -## 2. Create the release PR and draft release notes - -Open a PR for the branch, and confirm that it passes CI. For `0.X.0` minor releases, the PR should be merging into `main`, for `0.X.Y` patch releases, the PR should be merging the `release-0.X` maintenance branch. - -On https://github.com/PyO3/pyo3/releases, click "Draft a new release". The tag will be a new tag of `v` (note preceding `v`) and target should be the `release-` branch you just pushed. - -Write release notes which match the style of previous releases. You can get the list of contributors by running `nox -s contributors -- v release-` to get contributors from the previous version tag through to the branch tip you just pushed. (This uses the GitHub API, so you'll need to push the branch first.) - -Save as a draft and wait for now. - -## 3. Leave for a cooling off period - -Wait a couple of days in case anyone wants to hold up the release to add bugfixes etc. - -## 4. Put live - -To put live: -- 1. run `nox -s publish` to put live on crates.io -- 2. publish the release on Github -- 3. merge the release PR - -## 5. Tidy the main branch - -If the release PR targeted a branch other than main, you will need to cherry-pick the version bumps, CHANGELOG modifications and removal of towncrier `newsfragments` and open another PR to land these on main. - -## 6. Delete the release branch (patch releases only) - -For 0.X.Y patch releases, the release branch is no longer needed, so it should be deleted. diff --git a/vendor/pyo3/assets/script.py b/vendor/pyo3/assets/script.py deleted file mode 100644 index 897bd3ec09..0000000000 --- a/vendor/pyo3/assets/script.py +++ /dev/null @@ -1,5 +0,0 @@ -# Used in PyModule examples. - - -class Blah: - pass diff --git a/vendor/pyo3/branding/favicon/pyo3_16x16.png b/vendor/pyo3/branding/favicon/pyo3_16x16.png deleted file mode 100644 index 0d2d77eb15..0000000000 Binary files a/vendor/pyo3/branding/favicon/pyo3_16x16.png and /dev/null differ diff --git a/vendor/pyo3/branding/favicon/pyo3_32x32.png b/vendor/pyo3/branding/favicon/pyo3_32x32.png deleted file mode 100644 index ff1f97ae26..0000000000 Binary files a/vendor/pyo3/branding/favicon/pyo3_32x32.png and /dev/null differ diff --git a/vendor/pyo3/branding/pyo3logo.png b/vendor/pyo3/branding/pyo3logo.png deleted file mode 100644 index 06cad61734..0000000000 Binary files a/vendor/pyo3/branding/pyo3logo.png and /dev/null differ diff --git a/vendor/pyo3/branding/pyo3logo.svg b/vendor/pyo3/branding/pyo3logo.svg deleted file mode 100644 index 0315c63e56..0000000000 --- a/vendor/pyo3/branding/pyo3logo.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/vendor/pyo3/branding/pyotr.png b/vendor/pyo3/branding/pyotr.png deleted file mode 100644 index 75ab9bb34b..0000000000 Binary files a/vendor/pyo3/branding/pyotr.png and /dev/null differ diff --git a/vendor/pyo3/branding/pyotr.svg b/vendor/pyo3/branding/pyotr.svg deleted file mode 100644 index 52d478e67c..0000000000 --- a/vendor/pyo3/branding/pyotr.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/vendor/pyo3/build.rs b/vendor/pyo3/build.rs deleted file mode 100644 index 68a658bf28..0000000000 --- a/vendor/pyo3/build.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::env; - -use pyo3_build_config::pyo3_build_script_impl::{cargo_env_var, errors::Result}; -use pyo3_build_config::{ - add_python_framework_link_args, bail, print_feature_cfgs, InterpreterConfig, -}; - -fn ensure_auto_initialize_ok(interpreter_config: &InterpreterConfig) -> Result<()> { - if cargo_env_var("CARGO_FEATURE_AUTO_INITIALIZE").is_some() && !interpreter_config.shared { - bail!( - "The `auto-initialize` feature is enabled, but your python installation only supports \ - embedding the Python interpreter statically. If you are attempting to run tests, or a \ - binary which is okay to link dynamically, install a Python distribution which ships \ - with the Python shared library.\n\ - \n\ - Embedding the Python interpreter statically does not yet have first-class support in \ - PyO3. If you are sure you intend to do this, disable the `auto-initialize` feature.\n\ - \n\ - For more information, see \ - https://pyo3.rs/v{pyo3_version}/\ - building-and-distribution.html#embedding-python-in-rust", - pyo3_version = env::var("CARGO_PKG_VERSION").unwrap() - ); - } - Ok(()) -} - -/// Prepares the PyO3 crate for compilation. -/// -/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX -/// for users. -/// -/// Emits the cargo configuration based on this config as well as a few checks of the Rust compiler -/// version to enable features which aren't supported on MSRV. -fn configure_pyo3() -> Result<()> { - let interpreter_config = pyo3_build_config::get(); - - ensure_auto_initialize_ok(interpreter_config)?; - - for cfg in interpreter_config.build_script_outputs() { - println!("{}", cfg) - } - - // Emit cfgs like `invalid_from_utf8_lint` - print_feature_cfgs(); - - // Make `cargo test` etc work on macOS with Xcode bundled Python - add_python_framework_link_args(); - - Ok(()) -} - -fn main() { - pyo3_build_config::print_expected_cfgs(); - if let Err(e) = configure_pyo3() { - eprintln!("error: {}", e.report()); - std::process::exit(1) - } -} diff --git a/vendor/pyo3/codecov.yml b/vendor/pyo3/codecov.yml deleted file mode 100644 index d30f0ff47d..0000000000 --- a/vendor/pyo3/codecov.yml +++ /dev/null @@ -1,14 +0,0 @@ -comment: off - -coverage: - status: - project: - default: - target: auto - # Allow a tiny drop of overall project coverage in PR to reduce spurious failures. - threshold: 0.25% - -ignore: - - tests/ - - pytests/ - - src/test_hygiene/*.rs diff --git a/vendor/pyo3/emscripten/.gitignore b/vendor/pyo3/emscripten/.gitignore deleted file mode 100644 index a8ee928439..0000000000 --- a/vendor/pyo3/emscripten/.gitignore +++ /dev/null @@ -1 +0,0 @@ -pybuilddir.txt diff --git a/vendor/pyo3/emscripten/Makefile b/vendor/pyo3/emscripten/Makefile deleted file mode 100644 index 54094382c1..0000000000 --- a/vendor/pyo3/emscripten/Makefile +++ /dev/null @@ -1,87 +0,0 @@ -CURDIR=$(abspath .) - -# These three are passed in from nox. -BUILDROOT ?= $(CURDIR)/builddir -PYMAJORMINORMICRO ?= 3.11.0 - -EMSCRIPTEN_VERSION=3.1.68 - -export EMSDKDIR = $(BUILDROOT)/emsdk - -PLATFORM=wasm32_emscripten -SYSCONFIGDATA_NAME=_sysconfigdata__$(PLATFORM) - -# BASH_ENV tells bash to source emsdk_env.sh on startup. -export BASH_ENV := $(CURDIR)/env.sh -# Use bash to run each command so that env.sh will be used. -SHELL := /bin/bash - - -# Set version variables. -version_tuple := $(subst ., ,$(PYMAJORMINORMICRO:v%=%)) -PYMAJOR=$(word 1,$(version_tuple)) -PYMINOR=$(word 2,$(version_tuple)) -PYMICRO=$(word 3,$(version_tuple)) -PYVERSION=$(PYMAJORMINORMICRO) -PYMAJORMINOR=$(PYMAJOR).$(PYMINOR) - - -PYTHONURL=https://www.python.org/ftp/python/$(PYMAJORMINORMICRO)/Python-$(PYVERSION).tgz -PYTHONTARBALL=$(BUILDROOT)/downloads/Python-$(PYVERSION).tgz -PYTHONBUILD=$(BUILDROOT)/build/Python-$(PYVERSION) - -PYTHONLIBDIR=$(BUILDROOT)/install/Python-$(PYVERSION)/lib - -all: $(PYTHONLIBDIR)/libpython$(PYMAJORMINOR).a - -$(BUILDROOT)/.exists: - mkdir -p $(BUILDROOT) - touch $@ - - -# Install emscripten -$(EMSDKDIR): $(CURDIR)/emscripten_patches/* $(BUILDROOT)/.exists - git clone https://github.com/emscripten-core/emsdk.git --depth 1 --branch $(EMSCRIPTEN_VERSION) $(EMSDKDIR) - $(EMSDKDIR)/emsdk install $(EMSCRIPTEN_VERSION) - cd $(EMSDKDIR)/upstream/emscripten && cat $(CURDIR)/emscripten_patches/* | patch -p1 - $(EMSDKDIR)/emsdk activate $(EMSCRIPTEN_VERSION) - - -$(PYTHONTARBALL): - [ -d $(BUILDROOT)/downloads ] || mkdir -p $(BUILDROOT)/downloads - wget -q -O $@ $(PYTHONURL) - -$(PYTHONBUILD)/.patched: $(PYTHONTARBALL) - [ -d $(PYTHONBUILD) ] || ( \ - mkdir -p $(dir $(PYTHONBUILD));\ - tar -C $(dir $(PYTHONBUILD)) -xf $(PYTHONTARBALL) \ - ) - touch $@ - -$(PYTHONBUILD)/Makefile: $(PYTHONBUILD)/.patched $(BUILDROOT)/emsdk - cd $(PYTHONBUILD) && \ - CONFIG_SITE=Tools/wasm/config.site-wasm32-emscripten \ - emconfigure ./configure -C \ - --host=wasm32-unknown-emscripten \ - --build=$(shell $(PYTHONBUILD)/config.guess) \ - --with-emscripten-target=browser \ - --enable-wasm-dynamic-linking \ - --with-build-python=python3.11 - -$(PYTHONLIBDIR)/libpython$(PYMAJORMINOR).a : $(PYTHONBUILD)/Makefile - cd $(PYTHONBUILD) && \ - emmake make -j3 libpython$(PYMAJORMINOR).a - - # Generate sysconfigdata - _PYTHON_SYSCONFIGDATA_NAME=$(SYSCONFIGDATA_NAME) _PYTHON_PROJECT_BASE=$(PYTHONBUILD) python3.11 -m sysconfig --generate-posix-vars - cp `cat pybuilddir.txt`/$(SYSCONFIGDATA_NAME).py $(PYTHONBUILD)/Lib - - mkdir -p $(PYTHONLIBDIR) - # Copy libexpat.a, libmpdec.a, and libpython3.11.a - # In noxfile, we explicitly link libexpat and libmpdec via RUSTFLAGS - find $(PYTHONBUILD) -name '*.a' -exec cp {} $(PYTHONLIBDIR) \; - # Install Python stdlib - cp -r $(PYTHONBUILD)/Lib $(PYTHONLIBDIR)/python$(PYMAJORMINOR) - -clean: - rm -rf $(BUILDROOT) diff --git a/vendor/pyo3/emscripten/emscripten_patches/0001-Add-_gxx_personality_v0-stub-to-library.js.patch b/vendor/pyo3/emscripten/emscripten_patches/0001-Add-_gxx_personality_v0-stub-to-library.js.patch deleted file mode 100644 index bd0af28a03..0000000000 --- a/vendor/pyo3/emscripten/emscripten_patches/0001-Add-_gxx_personality_v0-stub-to-library.js.patch +++ /dev/null @@ -1,28 +0,0 @@ -From 4b56f37c3dc9185a235a8314086c4d7a6239b2f8 Mon Sep 17 00:00:00 2001 -From: Hood Chatham -Date: Sat, 4 Jun 2022 19:19:47 -0700 -Subject: [PATCH] Add _gxx_personality_v0 stub to library.js - -Mitigation for an incompatibility between Rust and Emscripten: -https://github.com/rust-lang/rust/issues/85821 -https://github.com/emscripten-core/emscripten/issues/17128 ---- - src/library.js | 2 ++ - 1 file changed, 2 insertions(+) - -diff --git a/src/library.js b/src/library.js -index e7bb4c38e..7d01744df 100644 ---- a/src/library.js -+++ b/src/library.js -@@ -403,6 +403,8 @@ mergeInto(LibraryManager.library, { - abort('Assertion failed: ' + UTF8ToString(condition) + ', at: ' + [filename ? UTF8ToString(filename) : 'unknown filename', line, func ? UTF8ToString(func) : 'unknown function']); - }, - -+ __gxx_personality_v0: function() {}, -+ - // ========================================================================== - // time.h - // ========================================================================== --- -2.25.1 - diff --git a/vendor/pyo3/emscripten/env.sh b/vendor/pyo3/emscripten/env.sh deleted file mode 100644 index 87b7b55138..0000000000 --- a/vendor/pyo3/emscripten/env.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -# Activate emsdk environment. emsdk_env.sh writes a lot to stderr so we suppress -# the output. This also prevents it from complaining when emscripten isn't yet -# installed. -source "$EMSDKDIR/emsdk_env.sh" 2> /dev/null || true diff --git a/vendor/pyo3/emscripten/runner.py b/vendor/pyo3/emscripten/runner.py deleted file mode 100755 index 95eaa8d4f3..0000000000 --- a/vendor/pyo3/emscripten/runner.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/local/bin/python -import pathlib -import sys -import subprocess - -p = pathlib.Path(sys.argv[1]) - -sys.exit(subprocess.call(["node", p.name], cwd=p.parent)) diff --git a/vendor/pyo3/examples/Cargo.toml b/vendor/pyo3/examples/Cargo.toml deleted file mode 100644 index f6c77eb609..0000000000 --- a/vendor/pyo3/examples/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "pyo3-examples" -version = "0.0.0" -publish = false -edition = "2021" -rust-version = "1.63" - -[dev-dependencies] -pyo3 = { path = "..", features = ["auto-initialize", "extension-module"] } - -[[example]] -name = "decorator" -path = "decorator/src/lib.rs" -crate-type = ["cdylib"] -doc-scrape-examples = true diff --git a/vendor/pyo3/examples/README.md b/vendor/pyo3/examples/README.md deleted file mode 100644 index 3c7cc30139..0000000000 --- a/vendor/pyo3/examples/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# PyO3 Examples - -These example crates are a collection of toy extension modules built with PyO3. They are all tested using `nox` in PyO3's CI. - -Below is a brief description of each of these: - -| Example | Description | -| ------- | ----------- | -| `decorator` | A project showcasing the example from the [Emulating callable objects](https://pyo3.rs/latest/class/call.html) chapter of the guide. | -| `maturin-starter` | A template project which is configured to use [`maturin`](https://github.com/PyO3/maturin) for development. | -| `setuptools-rust-starter` | A template project which is configured to use [`setuptools_rust`](https://github.com/PyO3/setuptools-rust/) for development. | -| `plugin` | Illustrates how to use Python as a scripting language within a Rust application | - -Note that there are also other examples in the `pyo3-ffi/examples` -directory that illustrate how to create rust extensions using raw FFI calls into -the CPython C API instead of using PyO3's abstractions. - -## Creating new projects from these examples - -To copy an example, use [`cargo-generate`](https://crates.io/crates/cargo-generate). Follow the commands below, replacing `` with the example to start from: - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/ -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/decorator/.template/Cargo.toml b/vendor/pyo3/examples/decorator/.template/Cargo.toml deleted file mode 100644 index 95c3f0f14a..0000000000 --- a/vendor/pyo3/examples/decorator/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "decorator" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/examples/decorator/.template/pre-script.rhai b/vendor/pyo3/examples/decorator/.template/pre-script.rhai deleted file mode 100644 index a4e89a0d05..0000000000 --- a/vendor/pyo3/examples/decorator/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.23.4"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/decorator/.template/pyproject.toml b/vendor/pyo3/examples/decorator/.template/pyproject.toml deleted file mode 100644 index d27a1be983..0000000000 --- a/vendor/pyo3/examples/decorator/.template/pyproject.toml +++ /dev/null @@ -1,10 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/decorator/Cargo.toml b/vendor/pyo3/examples/decorator/Cargo.toml deleted file mode 100644 index 3456302a9f..0000000000 --- a/vendor/pyo3/examples/decorator/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "decorator" -version = "0.1.0" -edition = "2021" - -[lib] -name = "decorator" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { path = "../../", features = ["extension-module"] } - -[workspace] diff --git a/vendor/pyo3/examples/decorator/MANIFEST.in b/vendor/pyo3/examples/decorator/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/examples/decorator/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/examples/decorator/README.md b/vendor/pyo3/examples/decorator/README.md deleted file mode 100644 index ccfb784d22..0000000000 --- a/vendor/pyo3/examples/decorator/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# decorator - -A project showcasing the example from the [Emulating callable objects](https://pyo3.rs/latest/class/call.html) chapter of the guide. - -## Building and Testing - -To build this package, first install `maturin`: - -```shell -pip install maturin -``` - -To build and test use `maturin develop`: - -```shell -pip install -r requirements-dev.txt -maturin develop -pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/decorator -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/decorator/cargo-generate.toml b/vendor/pyo3/examples/decorator/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/decorator/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/decorator/noxfile.py b/vendor/pyo3/examples/decorator/noxfile.py deleted file mode 100644 index 23d967cc69..0000000000 --- a/vendor/pyo3/examples/decorator/noxfile.py +++ /dev/null @@ -1,8 +0,0 @@ -import nox - - -@nox.session -def python(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/examples/decorator/pyproject.toml b/vendor/pyo3/examples/decorator/pyproject.toml deleted file mode 100644 index 88280a6b65..0000000000 --- a/vendor/pyo3/examples/decorator/pyproject.toml +++ /dev/null @@ -1,19 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "decorator" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/decorator/src/lib.rs b/vendor/pyo3/examples/decorator/src/lib.rs deleted file mode 100644 index 4c5471c994..0000000000 --- a/vendor/pyo3/examples/decorator/src/lib.rs +++ /dev/null @@ -1,64 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; -use std::sync::atomic::{AtomicU64, Ordering}; - -/// A function decorator that keeps track how often it is called. -/// -/// It otherwise doesn't do anything special. -#[pyclass(name = "Counter")] -pub struct PyCounter { - // Keeps track of how many calls have gone through. - // - // See the discussion at the end for why `AtomicU64` is used. - count: AtomicU64, - - // This is the actual function being wrapped. - wraps: Py, -} - -#[pymethods] -impl PyCounter { - // Note that we don't validate whether `wraps` is actually callable. - // - // While we could use `PyAny::is_callable` for that, it has some flaws: - // 1. It doesn't guarantee the object can actually be called successfully - // 2. We still need to handle any exceptions that the function might raise - #[new] - fn __new__(wraps: Py) -> Self { - PyCounter { - count: AtomicU64::new(0), - wraps, - } - } - - #[getter] - fn count(&self) -> u64 { - self.count.load(Ordering::Relaxed) - } - - #[pyo3(signature = (*args, **kwargs))] - fn __call__( - &self, - py: Python<'_>, - args: &Bound<'_, PyTuple>, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult> { - let new_count = self.count.fetch_add(1, Ordering::Relaxed); - let name = self.wraps.getattr(py, "__name__")?; - - println!("{} has been called {} time(s).", name, new_count); - - // After doing something, we finally forward the call to the wrapped function - let ret = self.wraps.call(py, args, kwargs)?; - - // We could do something with the return value of - // the function before returning it - Ok(ret) - } -} - -#[pymodule] -pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/examples/decorator/tests/example.py b/vendor/pyo3/examples/decorator/tests/example.py deleted file mode 100644 index 7be4b33e97..0000000000 --- a/vendor/pyo3/examples/decorator/tests/example.py +++ /dev/null @@ -1,14 +0,0 @@ -from decorator import Counter - - -@Counter -def say_hello(): - print("hello") - - -say_hello() -say_hello() -say_hello() -say_hello() - -assert say_hello.count == 4 diff --git a/vendor/pyo3/examples/decorator/tests/test_.py b/vendor/pyo3/examples/decorator/tests/test_.py deleted file mode 100644 index d97bf27dda..0000000000 --- a/vendor/pyo3/examples/decorator/tests/test_.py +++ /dev/null @@ -1,51 +0,0 @@ -from decorator import Counter - - -def test_no_args(): - @Counter - def say_hello(): - print("hello") - - say_hello() - say_hello() - say_hello() - say_hello() - - assert say_hello.count == 4 - - -def test_arg(): - @Counter - def say_hello(name): - print(f"hello {name}") - - say_hello("a") - say_hello("b") - say_hello("c") - say_hello("d") - - assert say_hello.count == 4 - - -def test_default_arg(): - @Counter - def say_hello(name="default"): - print(f"hello {name}") - - say_hello("a") - say_hello() - say_hello("c") - say_hello() - - assert say_hello.count == 4 - - -# https://github.com/PyO3/pyo3/discussions/2598 -def test_discussion_2598(): - @Counter - def say_hello(): - if say_hello.count < 2: - print("hello from decorator") - - say_hello() - say_hello() diff --git a/vendor/pyo3/examples/getitem/.template/Cargo.toml b/vendor/pyo3/examples/getitem/.template/Cargo.toml deleted file mode 100644 index 47dbb7b0ed..0000000000 --- a/vendor/pyo3/examples/getitem/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "getitem" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/examples/getitem/.template/pre-script.rhai b/vendor/pyo3/examples/getitem/.template/pre-script.rhai deleted file mode 100644 index 0368bb1f43..0000000000 --- a/vendor/pyo3/examples/getitem/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.19.0"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/getitem/.template/pyproject.toml b/vendor/pyo3/examples/getitem/.template/pyproject.toml deleted file mode 100644 index d27a1be983..0000000000 --- a/vendor/pyo3/examples/getitem/.template/pyproject.toml +++ /dev/null @@ -1,10 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/getitem/Cargo.toml b/vendor/pyo3/examples/getitem/Cargo.toml deleted file mode 100644 index 17020b9bd0..0000000000 --- a/vendor/pyo3/examples/getitem/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "getitem" -version = "0.1.0" -edition = "2021" - -[lib] -name = "getitem" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { path = "../../", features = ["extension-module"] } - -[workspace] diff --git a/vendor/pyo3/examples/getitem/MANIFEST.in b/vendor/pyo3/examples/getitem/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/examples/getitem/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/examples/getitem/README.md b/vendor/pyo3/examples/getitem/README.md deleted file mode 100644 index fc06fc1bc6..0000000000 --- a/vendor/pyo3/examples/getitem/README.md +++ /dev/null @@ -1,46 +0,0 @@ -# getitem - -A project showcasing how to create a `__getitem__` override that also showcases how to deal with multiple incoming types - -## Relevant Documentation - -Some of the relevant documentation links for this example: - -* Converting Slices to Indices: https://docs.rs/pyo3/latest/pyo3/types/struct.PySlice.html#method.indices -* GetItem Docs: https://pyo3.rs/latest/class/protocols.html?highlight=__getitem__#mapping--sequence-types -* Extract: https://pyo3.rs/latest/conversions/traits.html?highlight=extract#extract-and-the-frompyobject-trait -* Downcast and getattr: https://pyo3.rs/v0.19.0/types.html?highlight=getattr#pyany - - -## Building and Testing - -To build this package, first install `maturin`: - -```shell -pip install maturin -``` - -To build and test use `maturin develop`: - -```shell -pip install -r requirements-dev.txt -maturin develop -pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/decorator -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/getitem/cargo-generate.toml b/vendor/pyo3/examples/getitem/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/getitem/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/getitem/noxfile.py b/vendor/pyo3/examples/getitem/noxfile.py deleted file mode 100644 index 23d967cc69..0000000000 --- a/vendor/pyo3/examples/getitem/noxfile.py +++ /dev/null @@ -1,8 +0,0 @@ -import nox - - -@nox.session -def python(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/examples/getitem/pyproject.toml b/vendor/pyo3/examples/getitem/pyproject.toml deleted file mode 100644 index 6e05570eac..0000000000 --- a/vendor/pyo3/examples/getitem/pyproject.toml +++ /dev/null @@ -1,19 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "getitem" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/getitem/src/lib.rs b/vendor/pyo3/examples/getitem/src/lib.rs deleted file mode 100644 index ba850a06b8..0000000000 --- a/vendor/pyo3/examples/getitem/src/lib.rs +++ /dev/null @@ -1,83 +0,0 @@ -// This is a very fake example of how to check __getitem__ parameter and handle appropriately -use pyo3::exceptions::PyTypeError; -use pyo3::prelude::*; -use pyo3::types::PySlice; - -#[derive(FromPyObject)] -enum IntOrSlice<'py> { - Int(i32), - Slice(Bound<'py, PySlice>), -} - -#[pyclass] -struct ExampleContainer { - // represent the maximum length our container is pretending to be - max_length: i32, -} - -#[pymethods] -impl ExampleContainer { - #[new] - fn new() -> Self { - ExampleContainer { max_length: 100 } - } - - fn __getitem__(&self, key: &Bound<'_, PyAny>) -> PyResult { - if let Ok(position) = key.extract::() { - return Ok(position); - } else if let Ok(slice) = key.downcast::() { - // METHOD 1 - the use PySliceIndices to help with bounds checking and for cases when only start or end are provided - // in this case the start/stop/step all filled in to give valid values based on the max_length given - let index = slice.indices(self.max_length as isize).unwrap(); - let _delta = index.stop - index.start; - - // METHOD 2 - Do the getattr manually really only needed if you have some special cases for stop/_step not being present - // convert to indices and this will help you deal with stop being the max length - let start: i32 = slice.getattr("start")?.extract()?; - // This particular example assumes stop is present, but note that if not present, this will cause us to return due to the - // extract failing. Not needing custom code to deal with this is a good reason to use the Indices method. - let stop: i32 = slice.getattr("stop")?.extract()?; - // example of grabbing step since it is not always present - let _step: i32 = match slice.getattr("step")?.extract() { - // if no value found assume step is 1 - Ok(v) => v, - Err(_) => 1 as i32, - }; - - // Use something like this if you don't support negative stepping and want to give users - // leeway on how they provide their ordering - let (start, stop) = if start > stop { - (stop, start) - } else { - (start, stop) - }; - let delta = stop - start; - - return Ok(delta); - } else { - return Err(PyTypeError::new_err("Unsupported type")); - } - } - fn __setitem__(&self, idx: IntOrSlice, value: u32) -> PyResult<()> { - match idx { - IntOrSlice::Slice(slice) => { - let index = slice.indices(self.max_length as isize).unwrap(); - println!( - "Got a slice! {}-{}, step: {}, value: {}", - index.start, index.stop, index.step, value - ); - } - IntOrSlice::Int(index) => { - println!("Got an index! {} : value: {}", index, value); - } - } - Ok(()) - } -} - -#[pymodule(name = "getitem")] -fn example(m: &Bound<'_, PyModule>) -> PyResult<()> { - // ? -https://github.com/PyO3/maturin/issues/475 - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/examples/getitem/tests/test_getitem.py b/vendor/pyo3/examples/getitem/tests/test_getitem.py deleted file mode 100644 index 7f2d9a6277..0000000000 --- a/vendor/pyo3/examples/getitem/tests/test_getitem.py +++ /dev/null @@ -1,18 +0,0 @@ -import getitem -import pytest - - -def test_simple(): - container = getitem.ExampleContainer() - assert container[3] == 3 - assert container[4] == 4 - assert container[-1] == -1 - assert container[5:3] == 2 - assert container[3:5] == 2 - # test setitem, but this just displays, no return to check - container[3:5] = 2 - container[2] = 2 - # and note we will get an error on this one since we didn't - # add strings - with pytest.raises(TypeError): - container["foo"] = 2 diff --git a/vendor/pyo3/examples/maturin-starter/.template/Cargo.toml b/vendor/pyo3/examples/maturin-starter/.template/Cargo.toml deleted file mode 100644 index e67e124051..0000000000 --- a/vendor/pyo3/examples/maturin-starter/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "maturin_starter" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/examples/maturin-starter/.template/pre-script.rhai b/vendor/pyo3/examples/maturin-starter/.template/pre-script.rhai deleted file mode 100644 index a4e89a0d05..0000000000 --- a/vendor/pyo3/examples/maturin-starter/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.23.4"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/maturin-starter/.template/pyproject.toml b/vendor/pyo3/examples/maturin-starter/.template/pyproject.toml deleted file mode 100644 index d27a1be983..0000000000 --- a/vendor/pyo3/examples/maturin-starter/.template/pyproject.toml +++ /dev/null @@ -1,10 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/maturin-starter/Cargo.toml b/vendor/pyo3/examples/maturin-starter/Cargo.toml deleted file mode 100644 index 257908a4bb..0000000000 --- a/vendor/pyo3/examples/maturin-starter/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "maturin-starter" -version = "0.1.0" -edition = "2021" - -[lib] -name = "maturin_starter" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { path = "../../", features = ["extension-module"] } - -[features] -abi3 = ["pyo3/abi3-py37", "generate-import-lib"] -generate-import-lib = ["pyo3/generate-import-lib"] - -[workspace] diff --git a/vendor/pyo3/examples/maturin-starter/MANIFEST.in b/vendor/pyo3/examples/maturin-starter/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/examples/maturin-starter/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/examples/maturin-starter/README.md b/vendor/pyo3/examples/maturin-starter/README.md deleted file mode 100644 index da1ee9e4a4..0000000000 --- a/vendor/pyo3/examples/maturin-starter/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# maturin-starter - -An example of a basic Python extension module built using PyO3 and [`maturin`](https://github.com/PyO3/maturin). - -## Building and Testing - -To build this package, first install `maturin`: - -```shell -pip install maturin -``` - -To build and test use `maturin develop`: - -```shell -pip install -r requirements-dev.txt -maturin develop && pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/maturin-starter -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/maturin-starter/cargo-generate.toml b/vendor/pyo3/examples/maturin-starter/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/maturin-starter/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/maturin-starter/maturin_starter/__init__.py b/vendor/pyo3/examples/maturin-starter/maturin_starter/__init__.py deleted file mode 100644 index d0525f1930..0000000000 --- a/vendor/pyo3/examples/maturin-starter/maturin_starter/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# import the contents of the Rust library into the Python extension -from .maturin_starter import * -from .maturin_starter import __all__ - -# optional: include the documentation from the Rust module -from .maturin_starter import __doc__ # noqa: F401 - -__all__ = __all__ + ["PythonClass"] - - -class PythonClass: - def __init__(self, value: int) -> None: - self.value = value diff --git a/vendor/pyo3/examples/maturin-starter/noxfile.py b/vendor/pyo3/examples/maturin-starter/noxfile.py deleted file mode 100644 index 12ed90e5d1..0000000000 --- a/vendor/pyo3/examples/maturin-starter/noxfile.py +++ /dev/null @@ -1,8 +0,0 @@ -import nox - - -@nox.session -def python(session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/examples/maturin-starter/pyproject.toml b/vendor/pyo3/examples/maturin-starter/pyproject.toml deleted file mode 100644 index 52ecba15ed..0000000000 --- a/vendor/pyo3/examples/maturin-starter/pyproject.toml +++ /dev/null @@ -1,19 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "maturin-starter" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/maturin-starter/src/lib.rs b/vendor/pyo3/examples/maturin-starter/src/lib.rs deleted file mode 100644 index 4c2a30d3a5..0000000000 --- a/vendor/pyo3/examples/maturin-starter/src/lib.rs +++ /dev/null @@ -1,35 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyDict; -use pyo3::wrap_pymodule; - -mod submodule; - -#[pyclass] -struct ExampleClass { - #[pyo3(get, set)] - value: i32, -} - -#[pymethods] -impl ExampleClass { - #[new] - pub fn new(value: i32) -> Self { - ExampleClass { value } - } -} - -/// An example module implemented in Rust using PyO3. -#[pymodule] -fn maturin_starter(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_wrapped(wrap_pymodule!(submodule::submodule))?; - - // Inserting to sys.modules allows importing submodules nicely from Python - // e.g. from maturin_starter.submodule import SubmoduleClass - - let sys = PyModule::import(py, "sys")?; - let sys_modules: Bound<'_, PyDict> = sys.getattr("modules")?.downcast_into()?; - sys_modules.set_item("maturin_starter.submodule", m.getattr("submodule")?)?; - - Ok(()) -} diff --git a/vendor/pyo3/examples/maturin-starter/src/submodule.rs b/vendor/pyo3/examples/maturin-starter/src/submodule.rs deleted file mode 100644 index f3eb174100..0000000000 --- a/vendor/pyo3/examples/maturin-starter/src/submodule.rs +++ /dev/null @@ -1,22 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct SubmoduleClass {} - -#[pymethods] -impl SubmoduleClass { - #[new] - pub fn __new__() -> Self { - SubmoduleClass {} - } - - pub fn greeting(&self) -> &'static str { - "Hello, world!" - } -} - -#[pymodule] -pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/examples/maturin-starter/tests/test_maturin_starter.py b/vendor/pyo3/examples/maturin-starter/tests/test_maturin_starter.py deleted file mode 100644 index 1f3e8d9f7f..0000000000 --- a/vendor/pyo3/examples/maturin-starter/tests/test_maturin_starter.py +++ /dev/null @@ -1,19 +0,0 @@ -from maturin_starter import ExampleClass, PythonClass - - -def test_python_class() -> None: - py_class = PythonClass(value=10) - assert py_class.value == 10 - - -def test_example_class() -> None: - example = ExampleClass(value=11) - assert example.value == 11 - - -def test_doc() -> None: - import maturin_starter - - assert ( - maturin_starter.__doc__ == "An example module implemented in Rust using PyO3." - ) diff --git a/vendor/pyo3/examples/maturin-starter/tests/test_submodule.py b/vendor/pyo3/examples/maturin-starter/tests/test_submodule.py deleted file mode 100644 index 5cf2f57c2c..0000000000 --- a/vendor/pyo3/examples/maturin-starter/tests/test_submodule.py +++ /dev/null @@ -1,6 +0,0 @@ -from maturin_starter.submodule import SubmoduleClass - - -def test_submodule_class() -> None: - submodule_class = SubmoduleClass() - assert submodule_class.greeting() == "Hello, world!" diff --git a/vendor/pyo3/examples/plugin/.template/Cargo.toml b/vendor/pyo3/examples/plugin/.template/Cargo.toml deleted file mode 100644 index e2017a20b8..0000000000 --- a/vendor/pyo3/examples/plugin/.template/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[dependencies] -pyo3 = "{{PYO3_VERSION}}" -plugin_api = { path = "plugin_api" } diff --git a/vendor/pyo3/examples/plugin/.template/plugin_api/Cargo.toml b/vendor/pyo3/examples/plugin/.template/plugin_api/Cargo.toml deleted file mode 100644 index dfbd855aeb..0000000000 --- a/vendor/pyo3/examples/plugin/.template/plugin_api/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "plugin_api" -version = "0.1.0" -description = "Plugin API example" -edition = "2021" - -[lib] -name = "plugin_api" -crate-type = ["cdylib", "rlib"] - -[dependencies] -#!!! Important - DO NOT ENABLE extension-module FEATURE HERE!!! -pyo3 = "{{PYO3_VERSION}}" - -[features] -# instead extension-module feature for pyo3 is enabled conditionally when we want to build a standalone extension module to test our plugins without "main" program -extension-module = ["pyo3/extension-module"] diff --git a/vendor/pyo3/examples/plugin/.template/pre-script.rhai b/vendor/pyo3/examples/plugin/.template/pre-script.rhai deleted file mode 100644 index 3a1392912c..0000000000 --- a/vendor/pyo3/examples/plugin/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.23.4"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/plugin_api/Cargo.toml", "plugin_api/Cargo.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/plugin/Cargo.toml b/vendor/pyo3/examples/plugin/Cargo.toml deleted file mode 100644 index 08127b5003..0000000000 --- a/vendor/pyo3/examples/plugin/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "plugin_example" -version = "0.1.0" -edition = "2021" - - -[dependencies] -pyo3={path="../../", features=["macros"]} -plugin_api={path="plugin_api"} - - -[workspace] diff --git a/vendor/pyo3/examples/plugin/README.md b/vendor/pyo3/examples/plugin/README.md deleted file mode 100644 index 17f2cf740b..0000000000 --- a/vendor/pyo3/examples/plugin/README.md +++ /dev/null @@ -1,48 +0,0 @@ -# plugin - -An example of a Rust app that uses Python for a plugin. A Python extension module built using PyO3 and [`maturin`](https://github.com/PyO3/maturin) is used to provide -interface types that can be used to exchange data between Rust and Python. This also deals with how to separately test and load python modules. - -# Building and Testing -## Host application -To run the app itself, you only need to run - -```shell -cargo run -``` -It will build the app, as well as the plugin API, then run the app, load the plugin and show it working. - -## Plugin API testing - -The plugin API is in a separate crate `plugin_api`, so you can test it separately from the main app. - -To build the API only package, first install `maturin`: - -```shell -pip install maturin -``` - -When building the plugin, simply using `maturin develop` will fail to produce a viable extension module due to the features arrangement of PyO3. -Instead, one needs to enable the optional feature as follows: - -```shell -cd plugin_api -maturin build --features "extension-module" -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/plugin -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/plugin/cargo-generate.toml b/vendor/pyo3/examples/plugin/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/plugin/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/plugin/plugin_api/Cargo.toml b/vendor/pyo3/examples/plugin/plugin_api/Cargo.toml deleted file mode 100644 index 870ad76a37..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "plugin_api" -version = "0.1.0" -description = "Plugin API example" -edition = "2021" - -[lib] -name = "plugin_api" -crate-type = ["cdylib", "rlib"] - -[dependencies] -#!!! Important - DO NOT ENABLE extension-module FEATURE HERE!!! -pyo3 = { path = "../../../" } - -[features] -# instead extension-module feature for pyo3 is enabled conditionally when we want to build a standalone extension module to test our plugins without "main" program -extension-module = ["pyo3/extension-module"] diff --git a/vendor/pyo3/examples/plugin/plugin_api/noxfile.py b/vendor/pyo3/examples/plugin/plugin_api/noxfile.py deleted file mode 100644 index 12ed90e5d1..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/noxfile.py +++ /dev/null @@ -1,8 +0,0 @@ -import nox - - -@nox.session -def python(session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/examples/plugin/plugin_api/pyproject.toml b/vendor/pyo3/examples/plugin/plugin_api/pyproject.toml deleted file mode 100644 index 82f331d957..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/pyproject.toml +++ /dev/null @@ -1,15 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "plugin_api" -requires-python = ">=3.7" -classifiers = [ - "Programming Language :: Rust", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", -] - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/examples/plugin/plugin_api/src/lib.rs b/vendor/pyo3/examples/plugin/plugin_api/src/lib.rs deleted file mode 100644 index 580c85a8c8..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/src/lib.rs +++ /dev/null @@ -1,32 +0,0 @@ -use pyo3::prelude::*; - -///this is our Gadget that python plugin code can create, and rust app can then access natively. -#[pyclass] -pub struct Gadget { - #[pyo3(get, set)] - pub prop: usize, - //this field will only be accessible to rust code - pub rustonly: Vec, -} - -#[pymethods] -impl Gadget { - #[new] - fn new() -> Self { - Gadget { - prop: 777, - rustonly: Vec::new(), - } - } - - fn push(&mut self, v: usize) { - self.rustonly.push(v); - } -} - -/// A Python module for plugin interface types -#[pymodule] -pub fn plugin_api(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/examples/plugin/plugin_api/tests/test_Gadget.py b/vendor/pyo3/examples/plugin/plugin_api/tests/test_Gadget.py deleted file mode 100644 index f1175f2789..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/tests/test_Gadget.py +++ /dev/null @@ -1,22 +0,0 @@ -import pytest - - -@pytest.fixture -def gadget(): - import plugin_api as pa - - g = pa.Gadget() - return g - - -def test_creation(gadget): - pass - - -def test_property(gadget): - gadget.prop = 42 - assert gadget.prop == 42 - - -def test_push(gadget): - gadget.push(42) diff --git a/vendor/pyo3/examples/plugin/plugin_api/tests/test_import.py b/vendor/pyo3/examples/plugin/plugin_api/tests/test_import.py deleted file mode 100644 index 2a72892056..0000000000 --- a/vendor/pyo3/examples/plugin/plugin_api/tests/test_import.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_import(): - import plugin_api # noqa: F401 diff --git a/vendor/pyo3/examples/plugin/python_plugin/gadget_init_plugin.py b/vendor/pyo3/examples/plugin/python_plugin/gadget_init_plugin.py deleted file mode 100644 index 2eeba6fa7b..0000000000 --- a/vendor/pyo3/examples/plugin/python_plugin/gadget_init_plugin.py +++ /dev/null @@ -1,12 +0,0 @@ -import plugin_api -import rng - - -def start(): - """create an instance of Gadget, configure it and return to Rust""" - g = plugin_api.Gadget() - g.push(1) - g.push(2) - g.push(3) - g.prop = rng.get_random_number() - return g diff --git a/vendor/pyo3/examples/plugin/python_plugin/rng.py b/vendor/pyo3/examples/plugin/python_plugin/rng.py deleted file mode 100644 index 042e5e4b7d..0000000000 --- a/vendor/pyo3/examples/plugin/python_plugin/rng.py +++ /dev/null @@ -1,3 +0,0 @@ -def get_random_number(): - # verified by the roll of a fair die to be random - return 4 diff --git a/vendor/pyo3/examples/plugin/src/main.rs b/vendor/pyo3/examples/plugin/src/main.rs deleted file mode 100644 index 59442549e6..0000000000 --- a/vendor/pyo3/examples/plugin/src/main.rs +++ /dev/null @@ -1,44 +0,0 @@ -use plugin_api::plugin_api as pylib_module; -use pyo3::prelude::*; -use pyo3::types::PyList; -use std::path::Path; - -fn main() -> Result<(), Box> { - //"export" our API module to the python runtime - pyo3::append_to_inittab!(pylib_module); - //spawn runtime - pyo3::prepare_freethreaded_python(); - //import path for python - let path = Path::new("./python_plugin/"); - //do useful work - Python::with_gil(|py| { - //add the current directory to import path of Python (do not use this in production!) - let syspath: Bound = py.import("sys")?.getattr("path")?.extract()?; - syspath.insert(0, &path)?; - println!("Import path is: {:?}", syspath); - - // Now we can load our python_plugin/gadget_init_plugin.py file. - // It can in turn import other stuff as it deems appropriate - let plugin = PyModule::import(py, "gadget_init_plugin")?; - // and call start function there, which will return a python reference to Gadget. - // Gadget here is a "pyclass" object reference - let gadget = plugin.getattr("start")?.call0()?; - - //now we extract (i.e. mutably borrow) the rust struct from python object - { - //this scope will have mutable access to the gadget instance, which will be dropped on - //scope exit so Python can access it again. - let mut gadget_rs: PyRefMut<'_, plugin_api::Gadget> = gadget.extract()?; - // we can now modify it as if it was a native rust struct - gadget_rs.prop = 42; - //which includes access to rust-only fields that are not visible to python - println!("rust-only vec contains {:?}", gadget_rs.rustonly); - gadget_rs.rustonly.clear(); - } - - //any modifications we make to rust object are reflected on Python object as well - let res: usize = gadget.getattr("prop")?.extract()?; - println!("{res}"); - Ok(()) - }) -} diff --git a/vendor/pyo3/examples/setuptools-rust-starter/.template/Cargo.toml b/vendor/pyo3/examples/setuptools-rust-starter/.template/Cargo.toml deleted file mode 100644 index d0e43066ac..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "setuptools_rust_starter" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/examples/setuptools-rust-starter/.template/pre-script.rhai b/vendor/pyo3/examples/setuptools-rust-starter/.template/pre-script.rhai deleted file mode 100644 index 6b93bf9e6d..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.23.4"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/setup.cfg", "setup.cfg"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/setuptools-rust-starter/.template/setup.cfg b/vendor/pyo3/examples/setuptools-rust-starter/.template/setup.cfg deleted file mode 100644 index 90f44aba81..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/.template/setup.cfg +++ /dev/null @@ -1,9 +0,0 @@ -[metadata] -name = {{project-name}} -version = 0.1.0 -packages = - setuptools_rust_starter - -[options] -include_package_data = True -zip_safe = False diff --git a/vendor/pyo3/examples/setuptools-rust-starter/Cargo.toml b/vendor/pyo3/examples/setuptools-rust-starter/Cargo.toml deleted file mode 100644 index 5777cbbcd7..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "setuptools-rust-starter" -version = "0.1.0" -edition = "2021" - -[lib] -name = "setuptools_rust_starter" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { path = "../../", features = ["extension-module"] } - -[workspace] diff --git a/vendor/pyo3/examples/setuptools-rust-starter/MANIFEST.in b/vendor/pyo3/examples/setuptools-rust-starter/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/examples/setuptools-rust-starter/README.md b/vendor/pyo3/examples/setuptools-rust-starter/README.md deleted file mode 100644 index 86f1d46031..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# setuptools-rust-starter - -An example of a basic Python extension module built using PyO3 and [`setuptools_rust`](https://github.com/PyO3/setuptools-rust). - -## Building and Testing - -To build this package, first install `setuptools_rust`: - -```shell -pip install setuptools_rust -``` - -To build and test use `python setup.py develop`: - -```shell -pip install -r requirements-dev.txt -python setup.py develop && pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/setuptools-rust-starter -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/setuptools-rust-starter/cargo-generate.toml b/vendor/pyo3/examples/setuptools-rust-starter/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/setuptools-rust-starter/noxfile.py b/vendor/pyo3/examples/setuptools-rust-starter/noxfile.py deleted file mode 100644 index 9edab96254..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/noxfile.py +++ /dev/null @@ -1,10 +0,0 @@ -import nox - - -@nox.session -def python(session: nox.Session): - session.install("-rrequirements-dev.txt") - session.run_always( - "pip", "install", "-e", ".", "--no-build-isolation", env={"BUILD_DEBUG": "1"} - ) - session.run("pytest") diff --git a/vendor/pyo3/examples/setuptools-rust-starter/pyproject.toml b/vendor/pyo3/examples/setuptools-rust-starter/pyproject.toml deleted file mode 100644 index d82653c170..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/pyproject.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build-system] -requires = ["setuptools>=41.0.0", "wheel", "setuptools_rust>=1.0.0"] diff --git a/vendor/pyo3/examples/setuptools-rust-starter/requirements-dev.txt b/vendor/pyo3/examples/setuptools-rust-starter/requirements-dev.txt deleted file mode 100644 index 3235670811..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/requirements-dev.txt +++ /dev/null @@ -1,4 +0,0 @@ -pytest>=3.5.0 -setuptools_rust~=1.0.0 -pip>=21.3 -wheel diff --git a/vendor/pyo3/examples/setuptools-rust-starter/setup.cfg b/vendor/pyo3/examples/setuptools-rust-starter/setup.cfg deleted file mode 100644 index 95caeb757e..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/setup.cfg +++ /dev/null @@ -1,17 +0,0 @@ -[metadata] -name = setuptools-rust-starter -version = 0.1.0 -classifiers = - License :: OSI Approved :: MIT License - Development Status :: 3 - Alpha - Intended Audience :: Developers - Programming Language :: Python - Programming Language :: Rust - Operating System :: POSIX - Operating System :: MacOS :: MacOS X - -[options] -packages = - setuptools_rust_starter -include_package_data = True -zip_safe = False diff --git a/vendor/pyo3/examples/setuptools-rust-starter/setup.py b/vendor/pyo3/examples/setuptools-rust-starter/setup.py deleted file mode 100644 index 85d9e21d3a..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/setup.py +++ /dev/null @@ -1,13 +0,0 @@ -import os - -from setuptools import setup -from setuptools_rust import RustExtension - -setup( - rust_extensions=[ - RustExtension( - "setuptools_rust_starter._setuptools_rust_starter", - debug=os.environ.get("BUILD_DEBUG") == "1", - ) - ], -) diff --git a/vendor/pyo3/examples/setuptools-rust-starter/setuptools_rust_starter/__init__.py b/vendor/pyo3/examples/setuptools-rust-starter/setuptools_rust_starter/__init__.py deleted file mode 100644 index ecca1d15e5..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/setuptools_rust_starter/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# import the contents of the Rust library into the Python extension -from ._setuptools_rust_starter import * -from ._setuptools_rust_starter import __all__ - -# optional: include the documentation from the Rust module -from ._setuptools_rust_starter import __doc__ # noqa: F401 - -__all__ = __all__ + ["PythonClass"] - - -class PythonClass: - def __init__(self, value: int) -> None: - self.value = value diff --git a/vendor/pyo3/examples/setuptools-rust-starter/src/lib.rs b/vendor/pyo3/examples/setuptools-rust-starter/src/lib.rs deleted file mode 100644 index a26623bc04..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/src/lib.rs +++ /dev/null @@ -1,35 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyDict; -use pyo3::wrap_pymodule; - -mod submodule; - -#[pyclass] -struct ExampleClass { - #[pyo3(get, set)] - value: i32, -} - -#[pymethods] -impl ExampleClass { - #[new] - pub fn new(value: i32) -> Self { - ExampleClass { value } - } -} - -/// An example module implemented in Rust using PyO3. -#[pymodule] -fn _setuptools_rust_starter(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_wrapped(wrap_pymodule!(submodule::submodule))?; - - // Inserting to sys.modules allows importing submodules nicely from Python - // e.g. from setuptools_rust_starter.submodule import SubmoduleClass - - let sys = PyModule::import(py, "sys")?; - let sys_modules: Bound<'_, PyDict> = sys.getattr("modules")?.downcast_into()?; - sys_modules.set_item("setuptools_rust_starter.submodule", m.getattr("submodule")?)?; - - Ok(()) -} diff --git a/vendor/pyo3/examples/setuptools-rust-starter/src/submodule.rs b/vendor/pyo3/examples/setuptools-rust-starter/src/submodule.rs deleted file mode 100644 index f3eb174100..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/src/submodule.rs +++ /dev/null @@ -1,22 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct SubmoduleClass {} - -#[pymethods] -impl SubmoduleClass { - #[new] - pub fn __new__() -> Self { - SubmoduleClass {} - } - - pub fn greeting(&self) -> &'static str { - "Hello, world!" - } -} - -#[pymodule] -pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/examples/setuptools-rust-starter/tests/test_setuptools_rust_starter.py b/vendor/pyo3/examples/setuptools-rust-starter/tests/test_setuptools_rust_starter.py deleted file mode 100644 index 07ca6c5e80..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/tests/test_setuptools_rust_starter.py +++ /dev/null @@ -1,20 +0,0 @@ -from setuptools_rust_starter import ExampleClass, PythonClass - - -def test_python_class() -> None: - py_class = PythonClass(value=10) - assert py_class.value == 10 - - -def test_example_class() -> None: - example = ExampleClass(value=11) - assert example.value == 11 - - -def test_doc() -> None: - import setuptools_rust_starter - - assert ( - setuptools_rust_starter.__doc__ - == "An example module implemented in Rust using PyO3." - ) diff --git a/vendor/pyo3/examples/setuptools-rust-starter/tests/test_submodule.py b/vendor/pyo3/examples/setuptools-rust-starter/tests/test_submodule.py deleted file mode 100644 index 55bb922e8b..0000000000 --- a/vendor/pyo3/examples/setuptools-rust-starter/tests/test_submodule.py +++ /dev/null @@ -1,6 +0,0 @@ -from setuptools_rust_starter.submodule import SubmoduleClass - - -def test_submodule_class() -> None: - submodule_class = SubmoduleClass() - assert submodule_class.greeting() == "Hello, world!" diff --git a/vendor/pyo3/examples/word-count/.template/Cargo.toml b/vendor/pyo3/examples/word-count/.template/Cargo.toml deleted file mode 100644 index 5d6bffafcb..0000000000 --- a/vendor/pyo3/examples/word-count/.template/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "word_count" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } -rayon = "1.0.2" diff --git a/vendor/pyo3/examples/word-count/.template/pre-script.rhai b/vendor/pyo3/examples/word-count/.template/pre-script.rhai deleted file mode 100644 index a4e89a0d05..0000000000 --- a/vendor/pyo3/examples/word-count/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.23.4"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/examples/word-count/.template/pyproject.toml b/vendor/pyo3/examples/word-count/.template/pyproject.toml deleted file mode 100644 index 8aa3c9553b..0000000000 --- a/vendor/pyo3/examples/word-count/.template/pyproject.toml +++ /dev/null @@ -1,13 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" - -[project.optional-dependencies] -dev = ["pytest"] - -[tool.pytest.ini_options] -addopts = "--benchmark-disable" diff --git a/vendor/pyo3/examples/word-count/Cargo.toml b/vendor/pyo3/examples/word-count/Cargo.toml deleted file mode 100644 index cfb3444d5f..0000000000 --- a/vendor/pyo3/examples/word-count/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "word-count" -version = "0.1.0" -edition = "2021" - -[lib] -name = "word_count" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { path = "../..", features = ["extension-module"] } -rayon = "1.0.2" - -[workspace] diff --git a/vendor/pyo3/examples/word-count/MANIFEST.in b/vendor/pyo3/examples/word-count/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/examples/word-count/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/examples/word-count/README.md b/vendor/pyo3/examples/word-count/README.md deleted file mode 100644 index 6005fc5341..0000000000 --- a/vendor/pyo3/examples/word-count/README.md +++ /dev/null @@ -1,46 +0,0 @@ -# word-count - -Demonstrates searching for a file in plain python, with rust singlethreaded and with rust multithreaded. - -## Build - -```shell -pip install . -``` - -## Usage - -```python -from word_count import search_py, search, search_sequential - -search_py("foo bar", "foo") -search("foo bar", "foo") -search_sequential("foo bar", "foo") -``` - -## Testing - -To test install nox globally and run - -```shell -nox -``` - -## Benchmark - -To test install nox globally and run - -```shell -nox -s bench -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/word-count -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/examples/word-count/cargo-generate.toml b/vendor/pyo3/examples/word-count/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/examples/word-count/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/examples/word-count/noxfile.py b/vendor/pyo3/examples/word-count/noxfile.py deleted file mode 100644 index d64f210f3e..0000000000 --- a/vendor/pyo3/examples/word-count/noxfile.py +++ /dev/null @@ -1,17 +0,0 @@ -import nox - -nox.options.sessions = ["test"] - - -@nox.session -def test(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") - - -@nox.session -def bench(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest", "--benchmark-enable") diff --git a/vendor/pyo3/examples/word-count/pyproject.toml b/vendor/pyo3/examples/word-count/pyproject.toml deleted file mode 100644 index e5703294af..0000000000 --- a/vendor/pyo3/examples/word-count/pyproject.toml +++ /dev/null @@ -1,22 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "word-count" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = ["pytest", "pytest-benchmark"] - -[tool.pytest.ini_options] -addopts = "--benchmark-disable" diff --git a/vendor/pyo3/examples/word-count/src/lib.rs b/vendor/pyo3/examples/word-count/src/lib.rs deleted file mode 100644 index 5bc73df97a..0000000000 --- a/vendor/pyo3/examples/word-count/src/lib.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::prelude::*; -use rayon::prelude::*; - -/// Searches for the word, parallelized by rayon -#[pyfunction] -fn search(contents: &str, needle: &str) -> usize { - contents - .par_lines() - .map(|line| count_line(line, needle)) - .sum() -} - -/// Searches for a word in a classic sequential fashion -#[pyfunction] -fn search_sequential(contents: &str, needle: &str) -> usize { - contents.lines().map(|line| count_line(line, needle)).sum() -} - -#[pyfunction] -fn search_sequential_allow_threads(py: Python<'_>, contents: &str, needle: &str) -> usize { - py.allow_threads(|| search_sequential(contents, needle)) -} - -/// Count the occurrences of needle in line, case insensitive -fn count_line(line: &str, needle: &str) -> usize { - let mut total = 0; - for word in line.split(' ') { - if word == needle { - total += 1; - } - } - total -} - -#[pymodule] -fn word_count(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(search, m)?)?; - m.add_function(wrap_pyfunction!(search_sequential, m)?)?; - m.add_function(wrap_pyfunction!(search_sequential_allow_threads, m)?)?; - - Ok(()) -} diff --git a/vendor/pyo3/examples/word-count/tests/test_word_count.py b/vendor/pyo3/examples/word-count/tests/test_word_count.py deleted file mode 100644 index 5991e4ae1d..0000000000 --- a/vendor/pyo3/examples/word-count/tests/test_word_count.py +++ /dev/null @@ -1,67 +0,0 @@ -from concurrent.futures import ThreadPoolExecutor - -import pytest -import word_count - - -@pytest.fixture(scope="session") -def contents() -> str: - text = """ -The Zen of Python, by Tim Peters - -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! -""" - return text * 1000 - - -def test_word_count_rust_parallel(benchmark, contents): - count = benchmark(word_count.search, contents, "is") - assert count == 10000 - - -def test_word_count_rust_sequential(benchmark, contents): - count = benchmark(word_count.search_sequential, contents, "is") - assert count == 10000 - - -def test_word_count_python_sequential(benchmark, contents): - count = benchmark(word_count.search_py, contents, "is") - assert count == 10000 - - -def run_rust_sequential_twice( - executor: ThreadPoolExecutor, contents: str, needle: str -) -> int: - future_1 = executor.submit( - word_count.search_sequential_allow_threads, contents, needle - ) - future_2 = executor.submit( - word_count.search_sequential_allow_threads, contents, needle - ) - result_1 = future_1.result() - result_2 = future_2.result() - return result_1 + result_2 - - -def test_word_count_rust_sequential_twice_with_threads(benchmark, contents): - executor = ThreadPoolExecutor(max_workers=2) - count = benchmark(run_rust_sequential_twice, executor, contents, "is") - assert count == 20000 diff --git a/vendor/pyo3/examples/word-count/word_count/__init__.py b/vendor/pyo3/examples/word-count/word_count/__init__.py deleted file mode 100644 index 8ce7a17547..0000000000 --- a/vendor/pyo3/examples/word-count/word_count/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -from .word_count import search, search_sequential, search_sequential_allow_threads - -__all__ = [ - "search_py", - "search", - "search_sequential", - "search_sequential_allow_threads", -] - - -def search_py(contents: str, needle: str) -> int: - total = 0 - for line in contents.splitlines(): - for word in line.split(" "): - if word == needle: - total += 1 - return total diff --git a/vendor/pyo3/guide/book.toml b/vendor/pyo3/guide/book.toml deleted file mode 100644 index bccc350609..0000000000 --- a/vendor/pyo3/guide/book.toml +++ /dev/null @@ -1,12 +0,0 @@ -[book] -title = "PyO3 user guide" -description = "PyO3 user guide" -author = "PyO3 Project and Contributors" - -[preprocessor.pyo3_version] -command = "python3 guide/pyo3_version.py" - -[output.html] -git-repository-url = "https://github.com/PyO3/pyo3/tree/main/guide" -edit-url-template = "https://github.com/PyO3/pyo3/edit/main/guide/{path}" -playground.runnable = false diff --git a/vendor/pyo3/guide/pyclass-parameters.md b/vendor/pyo3/guide/pyclass-parameters.md deleted file mode 100644 index 7ebca2ec82..0000000000 --- a/vendor/pyo3/guide/pyclass-parameters.md +++ /dev/null @@ -1,49 +0,0 @@ -`#[pyclass]` can be used with the following parameters: - -| Parameter | Description | -| :- | :- | -| `constructor` | This is currently only allowed on [variants of complex enums][params-constructor]. It allows customization of the generated class constructor for each variant. It uses the same syntax and supports the same options as the `signature` attribute of functions and methods. | -| `crate = "some::path"` | Path to import the `pyo3` crate, if it's not accessible at `::pyo3`. | -| `dict` | Gives instances of this class an empty `__dict__` to store custom attributes. | -| `eq` | Implements `__eq__` using the `PartialEq` implementation of the underlying Rust datatype. | -| `eq_int` | Implements `__eq__` using `__int__` for simple enums. | -| `extends = BaseType` | Use a custom baseclass. Defaults to [`PyAny`][params-1] | -| `freelist = N` | Implements a [free list][params-2] of size N. This can improve performance for types that are often created and deleted in quick succession. Profile your code to see whether `freelist` is right for you. | -| `frozen` | Declares that your pyclass is immutable. It removes the borrow checker overhead when retrieving a shared reference to the Rust struct, but disables the ability to get a mutable reference. | -| `get_all` | Generates getters for all fields of the pyclass. | -| `hash` | Implements `__hash__` using the `Hash` implementation of the underlying Rust datatype. | -| `mapping` | Inform PyO3 that this class is a [`Mapping`][params-mapping], and so leave its implementation of sequence C-API slots empty. | -| `module = "module_name"` | Python code will see the class as being defined in this module. Defaults to `builtins`. | -| `name = "python_name"` | Sets the name that Python sees this class as. Defaults to the name of the Rust struct. | -| `ord` | Implements `__lt__`, `__gt__`, `__le__`, & `__ge__` using the `PartialOrd` implementation of the underlying Rust datatype. *Requires `eq`* | -| `rename_all = "renaming_rule"` | Applies renaming rules to every getters and setters of a struct, or every variants of an enum. Possible values are: "camelCase", "kebab-case", "lowercase", "PascalCase", "SCREAMING-KEBAB-CASE", "SCREAMING_SNAKE_CASE", "snake_case", "UPPERCASE". | -| `sequence` | Inform PyO3 that this class is a [`Sequence`][params-sequence], and so leave its C-API mapping length slot empty. | -| `set_all` | Generates setters for all fields of the pyclass. | -| `str` | Implements `__str__` using the `Display` implementation of the underlying Rust datatype or by passing an optional format string `str=""`. *Note: The optional format string is only allowed for structs. `name` and `rename_all` are incompatible with the optional format string. Additional details can be found in the discussion on this [PR](https://github.com/PyO3/pyo3/pull/4233).* | -| `subclass` | Allows other Python classes and `#[pyclass]` to inherit from this class. Enums cannot be subclassed. | -| `unsendable` | Required if your struct is not [`Send`][params-3]. Rather than using `unsendable`, consider implementing your struct in a thread-safe way by e.g. substituting [`Rc`][params-4] with [`Arc`][params-5]. By using `unsendable`, your class will panic when accessed by another thread. Also note the Python's GC is multi-threaded and while unsendable classes will not be traversed on foreign threads to avoid UB, this can lead to memory leaks. | -| `weakref` | Allows this class to be [weakly referenceable][params-6]. | - -All of these parameters can either be passed directly on the `#[pyclass(...)]` annotation, or as one or -more accompanying `#[pyo3(...)]` annotations, e.g.: - -```rust,ignore -// Argument supplied directly to the `#[pyclass]` annotation. -#[pyclass(name = "SomeName", subclass)] -struct MyClass {} - -// Argument supplied as a separate annotation. -#[pyclass] -#[pyo3(name = "SomeName", subclass)] -struct MyClass {} -``` - -[params-1]: https://docs.rs/pyo3/latest/pyo3/types/struct.PyAny.html -[params-2]: https://en.wikipedia.org/wiki/Free_list -[params-3]: https://doc.rust-lang.org/std/marker/trait.Send.html -[params-4]: https://doc.rust-lang.org/std/rc/struct.Rc.html -[params-5]: https://doc.rust-lang.org/std/sync/struct.Arc.html -[params-6]: https://docs.python.org/3/library/weakref.html -[params-constructor]: https://pyo3.rs/latest/class.html#complex-enums -[params-mapping]: https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types -[params-sequence]: https://pyo3.rs/latest/class/protocols.html#mapping--sequence-types diff --git a/vendor/pyo3/guide/pyo3_version.py b/vendor/pyo3/guide/pyo3_version.py deleted file mode 100644 index 860915ba66..0000000000 --- a/vendor/pyo3/guide/pyo3_version.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Simple mdbook preprocessor to inject pyo3 version into the guide. - -It will replace: - - {{#PYO3_VERSION_TAG}} with the contents of the PYO3_VERSION_TAG environment var - - {{#PYO3_DOCS_URL}} with the location of docs (e.g. 'https://docs.rs/pyo3/0.13.2') - - {{#PYO3_CRATE_VERSION}} with a relevant toml snippet (e.g. 'version = "0.13.2"') - - -Tested against mdbook 0.4.10. -""" - -import json -import os -import sys - -# Set PYO3_VERSION in CI to build the correct version into links -PYO3_VERSION_TAG = os.environ.get("PYO3_VERSION_TAG", "main") - -if PYO3_VERSION_TAG == "main": - PYO3_DOCS_URL = "https://pyo3.rs/main/doc" - PYO3_DOCS_VERSION = "latest" - PYO3_CRATE_VERSION = 'git = "https://github.com/pyo3/pyo3"' -else: - # v0.13.2 -> 0.13.2 - version = PYO3_VERSION_TAG.lstrip("v") - PYO3_DOCS_URL = f"https://docs.rs/pyo3/{version}" - PYO3_DOCS_VERSION = version - PYO3_CRATE_VERSION = f'version = "{version}"' - - -def replace_section_content(section): - if not isinstance(section, dict) or "Chapter" not in section: - return - - # Replace raw and url-encoded forms - section["Chapter"]["content"] = ( - section["Chapter"]["content"] - .replace("{{#PYO3_VERSION_TAG}}", PYO3_VERSION_TAG) - .replace("{{#PYO3_DOCS_URL}}", PYO3_DOCS_URL) - .replace("{{#PYO3_DOCS_VERSION}}", PYO3_DOCS_VERSION) - .replace("{{#PYO3_CRATE_VERSION}}", PYO3_CRATE_VERSION) - ) - - for sub_item in section["Chapter"]["sub_items"]: - replace_section_content(sub_item) - - -for line in sys.stdin: - if line: - [context, book] = json.loads(line) - for section in book["sections"]: - replace_section_content(section) - json.dump(book, fp=sys.stdout) diff --git a/vendor/pyo3/guide/src/SUMMARY.md b/vendor/pyo3/guide/src/SUMMARY.md deleted file mode 100644 index f8cc899d75..0000000000 --- a/vendor/pyo3/guide/src/SUMMARY.md +++ /dev/null @@ -1,50 +0,0 @@ -# Summary - -[Introduction](index.md) - ---- - -- [Getting started](getting-started.md) -- [Using Rust from Python](rust-from-python.md) - - [Python modules](module.md) - - [Python functions](function.md) - - [Function signatures](function/signature.md) - - [Error handling](function/error-handling.md) - - [Python classes](class.md) - - [Class customizations](class/protocols.md) - - [Basic object customization](class/object.md) - - [Emulating numeric types](class/numeric.md) - - [Emulating callable objects](class/call.md) - - [Thread safety](class/thread-safety.md) -- [Calling Python from Rust](python-from-rust.md) - - [Python object types](types.md) - - [Python exceptions](exception.md) - - [Calling Python functions](python-from-rust/function-calls.md) - - [Executing existing Python code](python-from-rust/calling-existing-code.md) -- [Type conversions](conversions.md) - - [Mapping of Rust types to Python types](conversions/tables.md) - - [Conversion traits](conversions/traits.md) -- [Using `async` and `await`](async-await.md) -- [Parallelism](parallelism.md) -- [Supporting Free-Threaded Python](free-threading.md) -- [Debugging](debugging.md) -- [Features reference](features.md) -- [Performance](performance.md) -- [Advanced topics](advanced.md) -- [Building and distribution](building-and-distribution.md) - - [Supporting multiple Python versions](building-and-distribution/multiple-python-versions.md) -- [Useful crates](ecosystem.md) - - [Logging](ecosystem/logging.md) - - [Using `async` and `await`](ecosystem/async-await.md) -- [FAQ and troubleshooting](faq.md) - ---- - -[Appendix A: Migration guide](migration.md) -[Appendix B: Trait bounds](trait-bounds.md) -[Appendix C: Python typing hints](python-typing-hints.md) -[CHANGELOG](changelog.md) - ---- - -[Contributing](contributing.md) diff --git a/vendor/pyo3/guide/src/advanced.md b/vendor/pyo3/guide/src/advanced.md deleted file mode 100644 index 61dc66382f..0000000000 --- a/vendor/pyo3/guide/src/advanced.md +++ /dev/null @@ -1,7 +0,0 @@ -# Advanced topics - -## FFI - -PyO3 exposes much of Python's C API through the `ffi` module. - -The C API is naturally unsafe and requires you to manage reference counts, errors and specific invariants yourself. Please refer to the [C API Reference Manual](https://docs.python.org/3/c-api/) and [The Rustonomicon](https://doc.rust-lang.org/nightly/nomicon/ffi.html) before using any function from that API. diff --git a/vendor/pyo3/guide/src/async-await.md b/vendor/pyo3/guide/src/async-await.md deleted file mode 100644 index 2757418180..0000000000 --- a/vendor/pyo3/guide/src/async-await.md +++ /dev/null @@ -1,103 +0,0 @@ -# Using `async` and `await` - -*This feature is still in active development. See [the related issue](https://github.com/PyO3/pyo3/issues/1632).* - -`#[pyfunction]` and `#[pymethods]` attributes also support `async fn`. - -```rust -# #![allow(dead_code)] -# #[cfg(feature = "experimental-async")] { -use std::{thread, time::Duration}; -use futures::channel::oneshot; -use pyo3::prelude::*; - -#[pyfunction] -#[pyo3(signature=(seconds, result=None))] -async fn sleep(seconds: f64, result: Option) -> Option { - let (tx, rx) = oneshot::channel(); - thread::spawn(move || { - thread::sleep(Duration::from_secs_f64(seconds)); - tx.send(()).unwrap(); - }); - rx.await.unwrap(); - result -} -# } -``` - -*Python awaitables instantiated with this method can only be awaited in *asyncio* context. Other Python async runtime may be supported in the future.* - -## `Send + 'static` constraint - -Resulting future of an `async fn` decorated by `#[pyfunction]` must be `Send + 'static` to be embedded in a Python object. - -As a consequence, `async fn` parameters and return types must also be `Send + 'static`, so it is not possible to have a signature like `async fn does_not_compile<'py>(arg: Bound<'py, PyAny>) -> Bound<'py, PyAny>`. - -However, there is an exception for method receivers, so async methods can accept `&self`/`&mut self`. Note that this means that the class instance is borrowed for as long as the returned future is not completed, even across yield points and while waiting for I/O operations to complete. Hence, other methods cannot obtain exclusive borrows while the future is still being polled. This is the same as how async methods in Rust generally work but it is more problematic for Rust code interfacing with Python code due to pervasive shared mutability. This strongly suggests to prefer shared borrows `&self` over exclusive ones `&mut self` to avoid racy borrow check failures at runtime. - -## Implicit GIL holding - -Even if it is not possible to pass a `py: Python<'py>` parameter to `async fn`, the GIL is still held during the execution of the future – it's also the case for regular `fn` without `Python<'py>`/`Bound<'py, PyAny>` parameter, yet the GIL is held. - -It is still possible to get a `Python` marker using [`Python::with_gil`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.with_gil); because `with_gil` is reentrant and optimized, the cost will be negligible. - -## Release the GIL across `.await` - -There is currently no simple way to release the GIL when awaiting a future, *but solutions are currently in development*. - -Here is the advised workaround for now: - -```rust,ignore -use std::{ - future::Future, - pin::{Pin, pin}, - task::{Context, Poll}, -}; -use pyo3::prelude::*; - -struct AllowThreads(F); - -impl Future for AllowThreads -where - F: Future + Unpin + Send, - F::Output: Send, -{ - type Output = F::Output; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let waker = cx.waker(); - Python::with_gil(|gil| { - gil.allow_threads(|| pin!(&mut self.0).poll(&mut Context::from_waker(waker))) - }) - } -} -``` - -## Cancellation - -Cancellation on the Python side can be caught using [`CancelHandle`]({{#PYO3_DOCS_URL}}/pyo3/coroutine/struct.CancelHandle.html) type, by annotating a function parameter with `#[pyo3(cancel_handle)]`. - -```rust -# #![allow(dead_code)] -# #[cfg(feature = "experimental-async")] { -use futures::FutureExt; -use pyo3::prelude::*; -use pyo3::coroutine::CancelHandle; - -#[pyfunction] -async fn cancellable(#[pyo3(cancel_handle)] mut cancel: CancelHandle) { - futures::select! { - /* _ = ... => println!("done"), */ - _ = cancel.cancelled().fuse() => println!("cancelled"), - } -} -# } -``` - -## The `Coroutine` type - -To make a Rust future awaitable in Python, PyO3 defines a [`Coroutine`]({{#PYO3_DOCS_URL}}/pyo3/coroutine/struct.Coroutine.html) type, which implements the Python [coroutine protocol](https://docs.python.org/3/library/collections.abc.html#collections.abc.Coroutine). - -Each `coroutine.send` call is translated to a `Future::poll` call. If a [`CancelHandle`]({{#PYO3_DOCS_URL}}/pyo3/coroutine/struct.CancelHandle.html) parameter is declared, the exception passed to `coroutine.throw` call is stored in it and can be retrieved with [`CancelHandle::cancelled`]({{#PYO3_DOCS_URL}}/pyo3/coroutine/struct.CancelHandle.html#method.cancelled); otherwise, it cancels the Rust future, and the exception is reraised; - -*The type does not yet have a public constructor until the design is finalized.* diff --git a/vendor/pyo3/guide/src/building-and-distribution.md b/vendor/pyo3/guide/src/building-and-distribution.md deleted file mode 100644 index d3474fedaf..0000000000 --- a/vendor/pyo3/guide/src/building-and-distribution.md +++ /dev/null @@ -1,353 +0,0 @@ -# Building and distribution - -This chapter of the guide goes into detail on how to build and distribute projects using PyO3. The way to achieve this is very different depending on whether the project is a Python module implemented in Rust, or a Rust binary embedding Python. For both types of project there are also common problems such as the Python version to build for and the [linker](https://en.wikipedia.org/wiki/Linker_(computing)) arguments to use. - -The material in this chapter is intended for users who have already read the PyO3 [README](./index.md). It covers in turn the choices that can be made for Python modules and for Rust binaries. There is also a section at the end about cross-compiling projects using PyO3. - -There is an additional sub-chapter dedicated to [supporting multiple Python versions](./building-and-distribution/multiple-python-versions.md). - -## Configuring the Python version - -PyO3 uses a build script (backed by the [`pyo3-build-config`] crate) to determine the Python version and set the correct linker arguments. By default it will attempt to use the following in order: - - Any active Python virtualenv. - - The `python` executable (if it's a Python 3 interpreter). - - The `python3` executable. - -You can override the Python interpreter by setting the `PYO3_PYTHON` environment variable, e.g. `PYO3_PYTHON=python3.7`, `PYO3_PYTHON=/usr/bin/python3.9`, or even a PyPy interpreter `PYO3_PYTHON=pypy3`. - -Once the Python interpreter is located, `pyo3-build-config` executes it to query the information in the `sysconfig` module which is needed to configure the rest of the compilation. - -To validate the configuration which PyO3 will use, you can run a compilation with the environment variable `PYO3_PRINT_CONFIG=1` set. An example output of doing this is shown below: - -```console -$ PYO3_PRINT_CONFIG=1 cargo build - Compiling pyo3 v0.14.1 (/home/david/dev/pyo3) -error: failed to run custom build command for `pyo3 v0.14.1 (/home/david/dev/pyo3)` - -Caused by: - process didn't exit successfully: `/home/david/dev/pyo3/target/debug/build/pyo3-7a8cf4fe22e959b7/build-script-build` (exit status: 101) - --- stdout - cargo:rerun-if-env-changed=PYO3_CROSS - cargo:rerun-if-env-changed=PYO3_CROSS_LIB_DIR - cargo:rerun-if-env-changed=PYO3_CROSS_PYTHON_VERSION - cargo:rerun-if-env-changed=PYO3_PRINT_CONFIG - - -- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile -- - implementation=CPython - version=3.8 - shared=true - abi3=false - lib_name=python3.8 - lib_dir=/usr/lib - executable=/usr/bin/python - pointer_width=64 - build_flags= - suppress_build_script_link_lines=false -``` - -The `PYO3_ENVIRONMENT_SIGNATURE` environment variable can be used to trigger rebuilds when its value changes, it has no other effect. - -### Advanced: config files - -If you save the above output config from `PYO3_PRINT_CONFIG` to a file, it is possible to manually override the contents and feed it back into PyO3 using the `PYO3_CONFIG_FILE` env var. - -If your build environment is unusual enough that PyO3's regular configuration detection doesn't work, using a config file like this will give you the flexibility to make PyO3 work for you. To see the full set of options supported, see the documentation for the [`InterpreterConfig` struct](https://docs.rs/pyo3-build-config/{{#PYO3_DOCS_VERSION}}/pyo3_build_config/struct.InterpreterConfig.html). - -## Building Python extension modules - -Python extension modules need to be compiled differently depending on the OS (and architecture) that they are being compiled for. As well as multiple OSes (and architectures), there are also many different Python versions which are actively supported. Packages uploaded to [PyPI](https://pypi.org/) usually want to upload prebuilt "wheels" covering many OS/arch/version combinations so that users on all these different platforms don't have to compile the package themselves. Package vendors can opt-in to the "abi3" limited Python API which allows their wheels to be used on multiple Python versions, reducing the number of wheels they need to compile, but restricts the functionality they can use. - -There are many ways to go about this: it is possible to use `cargo` to build the extension module (along with some manual work, which varies with OS). The PyO3 ecosystem has two packaging tools, [`maturin`] and [`setuptools-rust`], which abstract over the OS difference and also support building wheels for PyPI upload. - -PyO3 has some Cargo features to configure projects for building Python extension modules: - - The `extension-module` feature, which must be enabled when building Python extension modules. - - The `abi3` feature and its version-specific `abi3-pyXY` companions, which are used to opt-in to the limited Python API in order to support multiple Python versions in a single wheel. - -This section describes each of these packaging tools before describing how to build manually without them. It then proceeds with an explanation of the `extension-module` feature. Finally, there is a section describing PyO3's `abi3` features. - -### Packaging tools - -The PyO3 ecosystem has two main choices to abstract the process of developing Python extension modules: -- [`maturin`] is a command-line tool to build, package and upload Python modules. It makes opinionated choices about project layout meaning it needs very little configuration. This makes it a great choice for users who are building a Python extension from scratch and don't need flexibility. -- [`setuptools-rust`] is an add-on for `setuptools` which adds extra keyword arguments to the `setup.py` configuration file. It requires more configuration than `maturin`, however this gives additional flexibility for users adding Rust to an existing Python package that can't satisfy `maturin`'s constraints. - -Consult each project's documentation for full details on how to get started using them and how to upload wheels to PyPI. It should be noted that while `maturin` is able to build [manylinux](https://github.com/pypa/manylinux)-compliant wheels out-of-the-box, `setuptools-rust` requires a bit more effort, [relying on Docker](https://setuptools-rust.readthedocs.io/en/latest/building_wheels.html) for this purpose. - -There are also [`maturin-starter`] and [`setuptools-rust-starter`] examples in the PyO3 repository. - -### Manual builds - -To build a PyO3-based Python extension manually, start by running `cargo build` as normal in a library project which uses PyO3's `extension-module` feature and has the [`cdylib` crate type](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-crate-type-field). - -Once built, symlink (or copy) and rename the shared library from Cargo's `target/` directory to your desired output directory: -- on macOS, rename `libyour_module.dylib` to `your_module.so`. -- on Windows, rename `libyour_module.dll` to `your_module.pyd`. -- on Linux, rename `libyour_module.so` to `your_module.so`. - -You can then open a Python shell in the output directory and you'll be able to run `import your_module`. - -If you're packaging your library for redistribution, you should indicated the Python interpreter your library is compiled for by including the [platform tag](#platform-tags) in its name. This prevents incompatible interpreters from trying to import your library. If you're compiling for PyPy you *must* include the platform tag, or PyPy will ignore the module. - -#### Bazel builds - -To use PyO3 with bazel one needs to manually configure PyO3, PyO3-ffi and PyO3-macros. In particular, one needs to make sure that it is compiled with the right python flags for the version you intend to use. -For example see: - -1. [github.com/abrisco/rules_pyo3](https://github.com/abrisco/rules_pyo3) -- General rules for building extension modules. -2. [github.com/OliverFM/pytorch_with_gazelle](https://github.com/OliverFM/pytorch_with_gazelle) -- for a minimal example of a repo that can use PyO3, PyTorch and Gazelle to generate python Build files. -3. [github.com/TheButlah/rules_pyo3](https://github.com/TheButlah/rules_pyo3) -- is somewhat dated. - -#### Platform tags - -Rather than using just the `.so` or `.pyd` extension suggested above (depending on OS), you can prefix the shared library extension with a platform tag to indicate the interpreter it is compatible with. You can query your interpreter's platform tag from the `sysconfig` module. Some example outputs of this are seen below: - -```bash -# CPython 3.10 on macOS -.cpython-310-darwin.so - -# PyPy 7.3 (Python 3.9) on Linux -$ python -c 'import sysconfig; print(sysconfig.get_config_var("EXT_SUFFIX"))' -.pypy39-pp73-x86_64-linux-gnu.so -``` - -So, for example, a valid module library name on CPython 3.10 for macOS is `your_module.cpython-310-darwin.so`, and its equivalent when compiled for PyPy 7.3 on Linux would be `your_module.pypy38-pp73-x86_64-linux-gnu.so`. - -See [PEP 3149](https://peps.python.org/pep-3149/) for more background on platform tags. - -#### macOS - -On macOS, because the `extension-module` feature disables linking to `libpython` ([see the next section](#the-extension-module-feature)), some additional linker arguments need to be set. `maturin` and `setuptools-rust` both pass these arguments for PyO3 automatically, but projects using manual builds will need to set these directly in order to support macOS. - -The easiest way to set the correct linker arguments is to add a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) with the following content: - -```rust,ignore -fn main() { - pyo3_build_config::add_extension_module_link_args(); -} -``` - -Remember to also add `pyo3-build-config` to the `build-dependencies` section in `Cargo.toml`. - -An alternative to using `pyo3-build-config` is add the following to a cargo configuration file (e.g. `.cargo/config.toml`): - -```toml -[target.x86_64-apple-darwin] -rustflags = [ - "-C", "link-arg=-undefined", - "-C", "link-arg=dynamic_lookup", -] - -[target.aarch64-apple-darwin] -rustflags = [ - "-C", "link-arg=-undefined", - "-C", "link-arg=dynamic_lookup", -] -``` - -Using the MacOS system python3 (`/usr/bin/python3`, as opposed to python installed via homebrew, pyenv, nix, etc.) may result in runtime errors such as `Library not loaded: @rpath/Python3.framework/Versions/3.8/Python3`. - -The easiest way to set the correct linker arguments is to add a `build.rs` with the following content: - -```rust,ignore -fn main() { - pyo3_build_config::add_python_framework_link_args(); -} -``` - -Alternatively it can be resolved with another addition to `.cargo/config.toml`: - -```toml -[build] -rustflags = [ - "-C", "link-args=-Wl,-rpath,/Library/Developer/CommandLineTools/Library/Frameworks", -] -``` - -For more discussion on and workarounds for MacOS linking problems [see this issue](https://github.com/PyO3/pyo3/issues/1800#issuecomment-906786649). - -Finally, don't forget that on MacOS the `extension-module` feature will cause `cargo test` to fail without the `--no-default-features` flag (see [the FAQ](https://pyo3.rs/main/faq.html#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror)). - -### The `extension-module` feature - -PyO3's `extension-module` feature is used to disable [linking](https://en.wikipedia.org/wiki/Linker_(computing)) to `libpython` on Unix targets. - -This is necessary because by default PyO3 links to `libpython`. This makes binaries, tests, and examples "just work". However, Python extensions on Unix must not link to libpython for [manylinux](https://www.python.org/dev/peps/pep-0513/) compliance. - -The downside of not linking to `libpython` is that binaries, tests, and examples (which usually embed Python) will fail to build. If you have an extension module as well as other outputs in a single project, you need to use optional Cargo features to disable the `extension-module` when you're not building the extension module. See [the FAQ](faq.md#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror) for an example workaround. - -### `Py_LIMITED_API`/`abi3` - -By default, Python extension modules can only be used with the same Python version they were compiled against. For example, an extension module built for Python 3.5 can't be imported in Python 3.8. [PEP 384](https://www.python.org/dev/peps/pep-0384/) introduced the idea of the limited Python API, which would have a stable ABI enabling extension modules built with it to be used against multiple Python versions. This is also known as `abi3`. - -The advantage of building extension modules using the limited Python API is that package vendors only need to build and distribute a single copy (for each OS / architecture), and users can install it on all Python versions from the [minimum version](#minimum-python-version-for-abi3) and up. The downside of this is that PyO3 can't use optimizations which rely on being compiled against a known exact Python version. It's up to you to decide whether this matters for your extension module. It's also possible to design your extension module such that you can distribute `abi3` wheels but allow users compiling from source to benefit from additional optimizations - see the [support for multiple python versions](./building-and-distribution/multiple-python-versions.md) section of this guide, in particular the `#[cfg(Py_LIMITED_API)]` flag. - -There are three steps involved in making use of `abi3` when building Python packages as wheels: - -1. Enable the `abi3` feature in `pyo3`. This ensures `pyo3` only calls Python C-API functions which are part of the stable API, and on Windows also ensures that the project links against the correct shared object (no special behavior is required on other platforms): - -```toml -[dependencies] -pyo3 = { {{#PYO3_CRATE_VERSION}}, features = ["abi3"] } -``` - -2. Ensure that the built shared objects are correctly marked as `abi3`. This is accomplished by telling your build system that you're using the limited API. [`maturin`] >= 0.9.0 and [`setuptools-rust`] >= 0.11.4 support `abi3` wheels. -See the [corresponding](https://github.com/PyO3/maturin/pull/353) [PRs](https://github.com/PyO3/setuptools-rust/pull/82) for more. - -3. Ensure that the `.whl` is correctly marked as `abi3`. For projects using `setuptools`, this is accomplished by passing `--py-limited-api=cp3x` (where `x` is the minimum Python version supported by the wheel, e.g. `--py-limited-api=cp35` for Python 3.5) to `setup.py bdist_wheel`. - -#### Minimum Python version for `abi3` - -Because a single `abi3` wheel can be used with many different Python versions, PyO3 has feature flags `abi3-py37`, `abi3-py38`, `abi3-py39` etc. to set the minimum required Python version for your `abi3` wheel. -For example, if you set the `abi3-py37` feature, your extension wheel can be used on all Python 3 versions from Python 3.7 and up. `maturin` and `setuptools-rust` will give the wheel a name like `my-extension-1.0-cp37-abi3-manylinux2020_x86_64.whl`. - -As your extension module may be run with multiple different Python versions you may occasionally find you need to check the Python version at runtime to customize behavior. See [the relevant section of this guide](./building-and-distribution/multiple-python-versions.md#checking-the-python-version-at-runtime) on supporting multiple Python versions at runtime. - -PyO3 is only able to link your extension module to abi3 version up to and including your host Python version. E.g., if you set `abi3-py38` and try to compile the crate with a host of Python 3.7, the build will fail. - -> Note: If you set more that one of these `abi3` version feature flags the lowest version always wins. For example, with both `abi3-py37` and `abi3-py38` set, PyO3 would build a wheel which supports Python 3.7 and up. - -#### Building `abi3` extensions without a Python interpreter - -As an advanced feature, you can build PyO3 wheel without calling Python interpreter with the environment variable `PYO3_NO_PYTHON` set. -Also, if the build host Python interpreter is not found or is too old or otherwise unusable, -PyO3 will still attempt to compile `abi3` extension modules after displaying a warning message. -On Unix-like systems this works unconditionally; on Windows you must also set the `RUSTFLAGS` environment variable -to contain `-L native=/path/to/python/libs` so that the linker can find `python3.lib`. - -If the `python3.dll` import library is not available, an experimental `generate-import-lib` crate -feature may be enabled, and the required library will be created and used by PyO3 automatically. - -*Note*: MSVC targets require LLVM binutils (`llvm-dlltool`) to be available in `PATH` for -the automatic import library generation feature to work. - -#### Missing features - -Due to limitations in the Python API, there are a few `pyo3` features that do -not work when compiling for `abi3`. These are: - -- `#[pyo3(text_signature = "...")]` does not work on classes until Python 3.10 or greater. -- The `dict` and `weakref` options on classes are not supported until Python 3.9 or greater. -- The buffer API is not supported until Python 3.11 or greater. -- Optimizations which rely on knowledge of the exact Python version compiled against. - -## Embedding Python in Rust - -If you want to embed the Python interpreter inside a Rust program, there are two modes in which this can be done: dynamically and statically. We'll cover each of these modes in the following sections. Each of them affect how you must distribute your program. Instead of learning how to do this yourself, you might want to consider using a project like [PyOxidizer] to ship your application and all of its dependencies in a single file. - -PyO3 automatically switches between the two linking modes depending on whether the Python distribution you have configured PyO3 to use ([see above](#configuring-the-python-version)) contains a shared library or a static library. The static library is most often seen in Python distributions compiled from source without the `--enable-shared` configuration option. - -### Dynamically embedding the Python interpreter - -Embedding the Python interpreter dynamically is much easier than doing so statically. This is done by linking your program against a Python shared library (such as `libpython.3.9.so` on UNIX, or `python39.dll` on Windows). The implementation of the Python interpreter resides inside the shared library. This means that when the OS runs your Rust program it also needs to be able to find the Python shared library. - -This mode of embedding works well for Rust tests which need access to the Python interpreter. It is also great for Rust software which is installed inside a Python virtualenv, because the virtualenv sets up appropriate environment variables to locate the correct Python shared library. - -For distributing your program to non-technical users, you will have to consider including the Python shared library in your distribution as well as setting up wrapper scripts to set the right environment variables (such as `LD_LIBRARY_PATH` on UNIX, or `PATH` on Windows). - -Note that PyPy cannot be embedded in Rust (or any other software). Support for this is tracked on the [PyPy issue tracker](https://github.com/pypy/pypy/issues/3836). - -### Statically embedding the Python interpreter - -Embedding the Python interpreter statically means including the contents of a Python static library directly inside your Rust binary. This means that to distribute your program you only need to ship your binary file: it contains the Python interpreter inside the binary! - -On Windows static linking is almost never done, so Python distributions don't usually include a static library. The information below applies only to UNIX. - -The Python static library is usually called `libpython.a`. - -Static linking has a lot of complications, listed below. For these reasons PyO3 does not yet have first-class support for this embedding mode. See [issue 416 on PyO3's GitHub](https://github.com/PyO3/pyo3/issues/416) for more information and to discuss any issues you encounter. - -The [`auto-initialize`](features.md#auto-initialize) feature is deliberately disabled when embedding the interpreter statically because this is often unintentionally done by new users to PyO3 running test programs. Trying out PyO3 is much easier using dynamic embedding. - -The known complications are: - - To import compiled extension modules (such as other Rust extension modules, or those written in C), your binary must have the correct linker flags set during compilation to export the original contents of `libpython.a` so that extensions can use them (e.g. `-Wl,--export-dynamic`). - - The C compiler and flags which were used to create `libpython.a` must be compatible with your Rust compiler and flags, else you will experience compilation failures. - - Significantly different compiler versions may see errors like this: - - ```text - lto1: fatal error: bytecode stream in file 'rust-numpy/target/release/deps/libpyo3-6a7fb2ed970dbf26.rlib' generated with LTO version 6.0 instead of the expected 6.2 - ``` - - Mismatching flags may lead to errors like this: - - ```text - /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libpython3.9.a(zlibmodule.o): relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIE - ``` - -If you encounter these or other complications when linking the interpreter statically, discuss them on [issue 416 on PyO3's GitHub](https://github.com/PyO3/pyo3/issues/416). It is hoped that eventually that discussion will contain enough information and solutions that PyO3 can offer first-class support for static embedding. - -### Import your module when embedding the Python interpreter - -When you run your Rust binary with an embedded interpreter, any `#[pymodule]` created modules won't be accessible to import unless added to a table called `PyImport_Inittab` before the embedded interpreter is initialized. This will cause Python statements in your embedded interpreter such as `import your_new_module` to fail. You can call the macro [`append_to_inittab`]({{#PYO3_DOCS_URL}}/pyo3/macro.append_to_inittab.html) with your module before initializing the Python interpreter to add the module function into that table. (The Python interpreter will be initialized by calling `prepare_freethreaded_python`, `with_embedded_python_interpreter`, or `Python::with_gil` with the [`auto-initialize`](features.md#auto-initialize) feature enabled.) - -## Cross Compiling - -Thanks to Rust's great cross-compilation support, cross-compiling using PyO3 is relatively straightforward. To get started, you'll need a few pieces of software: - -* A toolchain for your target. -* The appropriate options in your Cargo `.config` for the platform you're targeting and the toolchain you are using. -* A Python interpreter that's already been compiled for your target (optional when building "abi3" extension modules). -* A Python interpreter that is built for your host and available through the `PATH` or setting the [`PYO3_PYTHON`](#configuring-the-python-version) variable (optional when building "abi3" extension modules). - -After you've obtained the above, you can build a cross-compiled PyO3 module by using Cargo's `--target` flag. PyO3's build script will detect that you are attempting a cross-compile based on your host machine and the desired target. - -When cross-compiling, PyO3's build script cannot execute the target Python interpreter to query the configuration, so there are a few additional environment variables you may need to set: - -* `PYO3_CROSS`: If present this variable forces PyO3 to configure as a cross-compilation. -* `PYO3_CROSS_LIB_DIR`: This variable can be set to the directory containing the target's libpython DSO and the associated `_sysconfigdata*.py` file for Unix-like targets, or the Python DLL import libraries for the Windows target. This variable is only needed when the output binary must link to libpython explicitly (e.g. when targeting Windows and Android or embedding a Python interpreter), or when it is absolutely required to get the interpreter configuration from `_sysconfigdata*.py`. -* `PYO3_CROSS_PYTHON_VERSION`: Major and minor version (e.g. 3.9) of the target Python installation. This variable is only needed if PyO3 cannot determine the version to target from `abi3-py3*` features, or if `PYO3_CROSS_LIB_DIR` is not set, or if there are multiple versions of Python present in `PYO3_CROSS_LIB_DIR`. -* `PYO3_CROSS_PYTHON_IMPLEMENTATION`: Python implementation name ("CPython" or "PyPy") of the target Python installation. CPython is assumed by default when this variable is not set, unless `PYO3_CROSS_LIB_DIR` is set for a Unix-like target and PyO3 can get the interpreter configuration from `_sysconfigdata*.py`. - -An experimental `pyo3` crate feature `generate-import-lib` enables the user to cross-compile -extension modules for Windows targets without setting the `PYO3_CROSS_LIB_DIR` environment -variable or providing any Windows Python library files. It uses an external [`python3-dll-a`] crate -to generate import libraries for the Python DLL for MinGW-w64 and MSVC compile targets. -`python3-dll-a` uses the binutils `dlltool` program to generate DLL import libraries for MinGW-w64 targets. -It is possible to override the default `dlltool` command name for the cross target -by setting `PYO3_MINGW_DLLTOOL` environment variable. -*Note*: MSVC targets require LLVM binutils or MSVC build tools to be available on the host system. -More specifically, `python3-dll-a` requires `llvm-dlltool` or `lib.exe` executable to be present in `PATH` when -targeting `*-pc-windows-msvc`. The Zig compiler executable can be used in place of `llvm-dlltool` when the `ZIG_COMMAND` -environment variable is set to the installed Zig program name (`"zig"` or `"python -m ziglang"`). - -An example might look like the following (assuming your target's sysroot is at `/home/pyo3/cross/sysroot` and that your target is `armv7`): - -```sh -export PYO3_CROSS_LIB_DIR="/home/pyo3/cross/sysroot/usr/lib" - -cargo build --target armv7-unknown-linux-gnueabihf -``` - -If there are multiple python versions at the cross lib directory and you cannot set a more precise location to include both the `libpython` DSO and `_sysconfigdata*.py` files, you can set the required version: -```sh -export PYO3_CROSS_PYTHON_VERSION=3.8 -export PYO3_CROSS_LIB_DIR="/home/pyo3/cross/sysroot/usr/lib" - -cargo build --target armv7-unknown-linux-gnueabihf -``` - -Or another example with the same sys root but building for Windows: -```sh -export PYO3_CROSS_PYTHON_VERSION=3.9 -export PYO3_CROSS_LIB_DIR="/home/pyo3/cross/sysroot/usr/lib" - -cargo build --target x86_64-pc-windows-gnu -``` - -Any of the `abi3-py3*` features can be enabled instead of setting `PYO3_CROSS_PYTHON_VERSION` in the above examples. - -`PYO3_CROSS_LIB_DIR` can often be omitted when cross compiling extension modules for Unix and macOS targets, -or when cross compiling extension modules for Windows and the experimental `generate-import-lib` -crate feature is enabled. - -The following resources may also be useful for cross-compiling: - - [github.com/japaric/rust-cross](https://github.com/japaric/rust-cross) is a primer on cross compiling Rust. - - [github.com/rust-embedded/cross](https://github.com/rust-embedded/cross) uses Docker to make Rust cross-compilation easier. - -[`pyo3-build-config`]: https://github.com/PyO3/pyo3/tree/main/pyo3-build-config -[`maturin-starter`]: https://github.com/PyO3/pyo3/tree/main/examples/maturin-starter -[`setuptools-rust-starter`]: https://github.com/PyO3/pyo3/tree/main/examples/setuptools-rust-starter -[`maturin`]: https://github.com/PyO3/maturin -[`setuptools-rust`]: https://github.com/PyO3/setuptools-rust -[PyOxidizer]: https://github.com/indygreg/PyOxidizer -[`python3-dll-a`]: https://docs.rs/python3-dll-a/latest/python3_dll_a/ diff --git a/vendor/pyo3/guide/src/building-and-distribution/multiple-python-versions.md b/vendor/pyo3/guide/src/building-and-distribution/multiple-python-versions.md deleted file mode 100644 index a7879941d0..0000000000 --- a/vendor/pyo3/guide/src/building-and-distribution/multiple-python-versions.md +++ /dev/null @@ -1,108 +0,0 @@ -# Supporting multiple Python versions - -PyO3 supports all actively-supported Python 3 and PyPy versions. As much as possible, this is done internally to PyO3 so that your crate's code does not need to adapt to the differences between each version. However, as Python features grow and change between versions, PyO3 cannot offer a completely identical API for every Python version. This may require you to add conditional compilation to your crate or runtime checks for the Python version. - -This section of the guide first introduces the `pyo3-build-config` crate, which you can use as a `build-dependency` to add additional `#[cfg]` flags which allow you to support multiple Python versions at compile-time. - -Second, we'll show how to check the Python version at runtime. This can be useful when building for multiple versions with the `abi3` feature, where the Python API compiled against is not always the same as the one in use. - -## Conditional compilation for different Python versions - -The `pyo3-build-config` exposes multiple [`#[cfg]` flags](https://doc.rust-lang.org/rust-by-example/attribute/cfg.html) which can be used to conditionally compile code for a given Python version. PyO3 itself depends on this crate, so by using it you can be sure that you are configured correctly for the Python version PyO3 is building against. - -This allows us to write code like the following - -```rust,ignore -#[cfg(Py_3_7)] -fn function_only_supported_on_python_3_7_and_up() {} - -#[cfg(not(Py_3_8))] -fn function_only_supported_before_python_3_8() {} - -#[cfg(not(Py_LIMITED_API))] -fn function_incompatible_with_abi3_feature() {} -``` - -The following sections first show how to add these `#[cfg]` flags to your build process, and then cover some common patterns flags in a little more detail. - -To see a full reference of all the `#[cfg]` flags provided, see the [`pyo3-build-cfg` docs](https://docs.rs/pyo3-build-config). - -### Using `pyo3-build-config` - -You can use the `#[cfg]` flags in just two steps: - -1. Add `pyo3-build-config` with the [`resolve-config`](../features.md#resolve-config) feature enabled to your crate's build dependencies in `Cargo.toml`: - - ```toml - [build-dependencies] - pyo3-build-config = { {{#PYO3_CRATE_VERSION}}, features = ["resolve-config"] } - ``` - -2. Add a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) file to your crate with the following contents: - - ```rust,ignore - fn main() { - // If you have an existing build.rs file, just add this line to it. - pyo3_build_config::use_pyo3_cfgs(); - } - ``` - -After these steps you are ready to annotate your code! - -### Common usages of `pyo3-build-cfg` flags - -The `#[cfg]` flags added by `pyo3-build-cfg` can be combined with all of Rust's logic in the `#[cfg]` attribute to create very precise conditional code generation. The following are some common patterns implemented using these flags: - -```text -#[cfg(Py_3_7)] -``` - -This `#[cfg]` marks code that will only be present on Python 3.7 and upwards. There are similar options `Py_3_8`, `Py_3_9`, `Py_3_10` and so on for each minor version. - -```text -#[cfg(not(Py_3_7))] -``` - -This `#[cfg]` marks code that will only be present on Python versions before (but not including) Python 3.7. - -```text -#[cfg(not(Py_LIMITED_API))] -``` - -This `#[cfg]` marks code that is only available when building for the unlimited Python API (i.e. PyO3's `abi3` feature is not enabled). This might be useful if you want to ship your extension module as an `abi3` wheel and also allow users to compile it from source to make use of optimizations only possible with the unlimited API. - -```text -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -``` - -This `#[cfg]` marks code which is available when running Python 3.9 or newer, or when using the unlimited API with an older Python version. Patterns like this are commonly seen on Python APIs which were added to the limited Python API in a specific minor version. - -```text -#[cfg(PyPy)] -``` - -This `#[cfg]` marks code which is running on PyPy. - -## Checking the Python version at runtime - -When building with PyO3's `abi3` feature, your extension module will be compiled against a specific [minimum version](../building-and-distribution.md#minimum-python-version-for-abi3) of Python, but may be running on newer Python versions. - -For example with PyO3's `abi3-py38` feature, your extension will be compiled as if it were for Python 3.8. If you were using `pyo3-build-config`, `#[cfg(Py_3_8)]` would be present. Your user could freely install and run your abi3 extension on Python 3.9. - -There's no way to detect your user doing that at compile time, so instead you need to fall back to runtime checks. - -PyO3 provides the APIs [`Python::version()`] and [`Python::version_info()`] to query the running Python version. This allows you to do the following, for example: - - -```rust -use pyo3::Python; - -Python::with_gil(|py| { - // PyO3 supports Python 3.7 and up. - assert!(py.version_info() >= (3, 7)); - assert!(py.version_info() >= (3, 7, 0)); -}); -``` - -[`Python::version()`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.version -[`Python::version_info()`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.version_info diff --git a/vendor/pyo3/guide/src/changelog.md b/vendor/pyo3/guide/src/changelog.md deleted file mode 100644 index c16cb03d37..0000000000 --- a/vendor/pyo3/guide/src/changelog.md +++ /dev/null @@ -1 +0,0 @@ -{{#include ../../CHANGELOG.md}} diff --git a/vendor/pyo3/guide/src/class.md b/vendor/pyo3/guide/src/class.md deleted file mode 100644 index 7229330a36..0000000000 --- a/vendor/pyo3/guide/src/class.md +++ /dev/null @@ -1,1480 +0,0 @@ -# Python classes - -PyO3 exposes a group of attributes powered by Rust's proc macro system for defining Python classes as Rust structs. - -The main attribute is `#[pyclass]`, which is placed upon a Rust `struct` or `enum` to generate a Python type for it. They will usually also have *one* `#[pymethods]`-annotated `impl` block for the struct, which is used to define Python methods and constants for the generated Python type. (If the [`multiple-pymethods`] feature is enabled, each `#[pyclass]` is allowed to have multiple `#[pymethods]` blocks.) `#[pymethods]` may also have implementations for Python magic methods such as `__str__`. - -This chapter will discuss the functionality and configuration these attributes offer. Below is a list of links to the relevant section of this chapter for each: - -- [`#[pyclass]`](#defining-a-new-class) - - [`#[pyo3(get, set)]`](#object-properties-using-pyo3get-set) -- [`#[pymethods]`](#instance-methods) - - [`#[new]`](#constructor) - - [`#[getter]`](#object-properties-using-getter-and-setter) - - [`#[setter]`](#object-properties-using-getter-and-setter) - - [`#[staticmethod]`](#static-methods) - - [`#[classmethod]`](#class-methods) - - [`#[classattr]`](#class-attributes) - - [`#[args]`](#method-arguments) -- [Magic methods and slots](class/protocols.md) -- [Classes as function arguments](#classes-as-function-arguments) - -## Defining a new class - -To define a custom Python class, add the `#[pyclass]` attribute to a Rust struct or enum. -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; - -#[pyclass] -struct MyClass { - inner: i32, -} - -// A "tuple" struct -#[pyclass] -struct Number(i32); - -// PyO3 supports unit-only enums (which contain only unit variants) -// These simple enums behave similarly to Python's enumerations (enum.Enum) -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum MyEnum { - Variant, - OtherVariant = 30, // PyO3 supports custom discriminants. -} - -// PyO3 supports custom discriminants in unit-only enums -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum HttpResponse { - Ok = 200, - NotFound = 404, - Teapot = 418, - // ... -} - -// PyO3 also supports enums with Struct and Tuple variants -// These complex enums have sligtly different behavior from the simple enums above -// They are meant to work with instance checks and match statement patterns -// The variants can be mixed and matched -// Struct variants have named fields while tuple enums generate generic names for fields in order _0, _1, _2, ... -// Apart from this both types are functionally identical -#[pyclass] -enum Shape { - Circle { radius: f64 }, - Rectangle { width: f64, height: f64 }, - RegularPolygon(u32, f64), - Nothing(), -} -``` - -The above example generates implementations for [`PyTypeInfo`] and [`PyClass`] for `MyClass`, `Number`, `MyEnum`, `HttpResponse`, and `Shape`. To see these generated implementations, refer to the [implementation details](#implementation-details) at the end of this chapter. - -### Restrictions - -To integrate Rust types with Python, PyO3 needs to place some restrictions on the types which can be annotated with `#[pyclass]`. In particular, they must have no lifetime parameters, no generic parameters, and must be thread-safe. The reason for each of these is explained below. - -#### No lifetime parameters - -Rust lifetimes are used by the Rust compiler to reason about a program's memory safety. They are a compile-time only concept; there is no way to access Rust lifetimes at runtime from a dynamic language like Python. - -As soon as Rust data is exposed to Python, there is no guarantee that the Rust compiler can make on how long the data will live. Python is a reference-counted language and those references can be held for an arbitrarily long time which is untraceable by the Rust compiler. The only possible way to express this correctly is to require that any `#[pyclass]` does not borrow data for any lifetime shorter than the `'static` lifetime, i.e. the `#[pyclass]` cannot have any lifetime parameters. - -When you need to share ownership of data between Python and Rust, instead of using borrowed references with lifetimes consider using reference-counted smart pointers such as [`Arc`] or [`Py`]. - -#### No generic parameters - -A Rust `struct Foo` with a generic parameter `T` generates new compiled implementations each time it is used with a different concrete type for `T`. These new implementations are generated by the compiler at each usage site. This is incompatible with wrapping `Foo` in Python, where there needs to be a single compiled implementation of `Foo` which is integrated with the Python interpreter. - -Currently, the best alternative is to write a macro which expands to a new `#[pyclass]` for each instantiation you want: - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; - -struct GenericClass { - data: T, -} - -macro_rules! create_interface { - ($name: ident, $type: ident) => { - #[pyclass] - pub struct $name { - inner: GenericClass<$type>, - } - #[pymethods] - impl $name { - #[new] - pub fn new(data: $type) -> Self { - Self { - inner: GenericClass { data: data }, - } - } - } - }; -} - -create_interface!(IntClass, i64); -create_interface!(FloatClass, String); -``` - -#### Must be thread-safe - -Python objects are freely shared between threads by the Python interpreter. This means that: -- Python objects may be created and destroyed by different Python threads; therefore `#[pyclass]` objects must be `Send`. -- Python objects may be accessed by multiple Python threads simultaneously; therefore `#[pyclass]` objects must be `Sync`. - -For now, don't worry about these requirements; simple classes will already be thread-safe. There is a [detailed discussion on thread-safety](./class/thread-safety.md) later in the guide. - -## Constructor - -By default, it is not possible to create an instance of a custom class from Python code. -To declare a constructor, you need to define a method and annotate it with the `#[new]` -attribute. Only Python's `__new__` method can be specified, `__init__` is not available. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - #[new] - fn new(value: i32) -> Self { - Number(value) - } -} -``` - -Alternatively, if your `new` method may fail you can return `PyResult`. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::exceptions::PyValueError; -# #[pyclass] -# struct Nonzero(i32); -# -#[pymethods] -impl Nonzero { - #[new] - fn py_new(value: i32) -> PyResult { - if value == 0 { - Err(PyValueError::new_err("cannot be zero")) - } else { - Ok(Nonzero(value)) - } - } -} -``` - -If you want to return an existing object (for example, because your `new` -method caches the values it returns), `new` can return `pyo3::Py`. - -As you can see, the Rust method name is not important here; this way you can -still, use `new()` for a Rust-level constructor. - -If no method marked with `#[new]` is declared, object instances can only be -created from Rust, but not from Python. - -For arguments, see the [`Method arguments`](#method-arguments) section below. - -## Adding the class to a module - -The next step is to create the module initializer and add our class to it: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# #[pyclass] -# struct Number(i32); -# -#[pymodule] -fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} -``` - -## Bound and interior mutability - -Often is useful to turn a `#[pyclass]` type `T` into a Python object and access it from Rust code. The [`Py`] and [`Bound<'py, T>`] smart pointers are the ways to represent a Python object in PyO3's API. More detail can be found about them [in the Python objects](./types.md#pyo3s-smart-pointers) section of the guide. - -Most Python objects do not offer exclusive (`&mut`) access (see the [section on Python's memory model](./python-from-rust.md#pythons-memory-model)). However, Rust structs wrapped as Python objects (called `pyclass` types) often *do* need `&mut` access. Due to the GIL, PyO3 *can* guarantee exclusive access to them. - -The Rust borrow checker cannot reason about `&mut` references once an object's ownership has been passed to the Python interpreter. This means that borrow checking is done at runtime using with a scheme very similar to `std::cell::RefCell`. This is known as [interior mutability](https://doc.rust-lang.org/book/ch15-05-interior-mutability.html). - -Users who are familiar with `RefCell` can use `Py` and `Bound<'py, T>` just like `RefCell`. - -For users who are not very familiar with `RefCell`, here is a reminder of Rust's rules of borrowing: -- At any given time, you can have either (but not both of) one mutable reference or any number of immutable references. -- References can never outlast the data they refer to. - -`Py` and `Bound<'py, T>`, like `RefCell`, ensure these borrowing rules by tracking references at runtime. - -```rust -# use pyo3::prelude::*; -#[pyclass] -struct MyClass { - #[pyo3(get)] - num: i32, -} -Python::with_gil(|py| { - let obj = Bound::new(py, MyClass { num: 3 }).unwrap(); - { - let obj_ref = obj.borrow(); // Get PyRef - assert_eq!(obj_ref.num, 3); - // You cannot get PyRefMut unless all PyRefs are dropped - assert!(obj.try_borrow_mut().is_err()); - } - { - let mut obj_mut = obj.borrow_mut(); // Get PyRefMut - obj_mut.num = 5; - // You cannot get any other refs until the PyRefMut is dropped - assert!(obj.try_borrow().is_err()); - assert!(obj.try_borrow_mut().is_err()); - } - - // You can convert `Bound` to a Python object - pyo3::py_run!(py, obj, "assert obj.num == 5"); -}); -``` - -A `Bound<'py, T>` is restricted to the GIL lifetime `'py`. To make the object longer lived (for example, to store it in a struct on the -Rust side), use `Py`. `Py` needs a `Python<'_>` token to allow access: - -```rust -# use pyo3::prelude::*; -#[pyclass] -struct MyClass { - num: i32, -} - -fn return_myclass() -> Py { - Python::with_gil(|py| Py::new(py, MyClass { num: 1 }).unwrap()) -} - -let obj = return_myclass(); - -Python::with_gil(move |py| { - let bound = obj.bind(py); // Py::bind returns &Bound<'py, MyClass> - let obj_ref = bound.borrow(); // Get PyRef - assert_eq!(obj_ref.num, 1); -}); -``` - -### frozen classes: Opting out of interior mutability - -As detailed above, runtime borrow checking is currently enabled by default. But a class can opt of out it by declaring itself `frozen`. It can still use interior mutability via standard Rust types like `RefCell` or `Mutex`, but it is not bound to the implementation provided by PyO3 and can choose the most appropriate strategy on field-by-field basis. - -Classes which are `frozen` and also `Sync`, e.g. they do use `Mutex` but not `RefCell`, can be accessed without needing the Python GIL via the `Bound::get` and `Py::get` methods: - -```rust -use std::sync::atomic::{AtomicUsize, Ordering}; -# use pyo3::prelude::*; - -#[pyclass(frozen)] -struct FrozenCounter { - value: AtomicUsize, -} - -let py_counter: Py = Python::with_gil(|py| { - let counter = FrozenCounter { - value: AtomicUsize::new(0), - }; - - Py::new(py, counter).unwrap() -}); - -py_counter.get().value.fetch_add(1, Ordering::Relaxed); - -Python::with_gil(move |_py| drop(py_counter)); -``` - -Frozen classes are likely to become the default thereby guiding the PyO3 ecosystem towards a more deliberate application of interior mutability. Eventually, this should enable further optimizations of PyO3's internals and avoid downstream code paying the cost of interior mutability when it is not actually required. - -## Customizing the class - -{{#include ../pyclass-parameters.md}} - -These parameters are covered in various sections of this guide. - -### Return type - -Generally, `#[new]` methods have to return `T: Into>` or -`PyResult where T: Into>`. - -For constructors that may fail, you should wrap the return type in a PyResult as well. -Consult the table below to determine which type your constructor should return: - -| | **Cannot fail** | **May fail** | -|-----------------------------|---------------------------|-----------------------------------| -|**No inheritance** | `T` | `PyResult` | -|**Inheritance(T Inherits U)**| `(T, U)` | `PyResult<(T, U)>` | -|**Inheritance(General Case)**| [`PyClassInitializer`] | `PyResult>` | - -## Inheritance - -By default, `object`, i.e. `PyAny` is used as the base class. To override this default, -use the `extends` parameter for `pyclass` with the full path to the base class. -Currently, only classes defined in Rust and builtins provided by PyO3 can be inherited -from; inheriting from other classes defined in Python is not yet supported -([#991](https://github.com/PyO3/pyo3/issues/991)). - - -For convenience, `(T, U)` implements `Into>` where `U` is the -base class of `T`. -But for a more deeply nested inheritance, you have to return `PyClassInitializer` -explicitly. - -To get a parent class from a child, use [`PyRef`] instead of `&self` for methods, -or [`PyRefMut`] instead of `&mut self`. -Then you can access a parent class by `self_.as_super()` as `&PyRef`, -or by `self_.into_super()` as `PyRef` (and similar for the `PyRefMut` -case). For convenience, `self_.as_ref()` can also be used to get `&Self::BaseClass` -directly; however, this approach does not let you access base classes higher in the -inheritance hierarchy, for which you would need to chain multiple `as_super` or -`into_super` calls. - -```rust -# use pyo3::prelude::*; - -#[pyclass(subclass)] -struct BaseClass { - val1: usize, -} - -#[pymethods] -impl BaseClass { - #[new] - fn new() -> Self { - BaseClass { val1: 10 } - } - - pub fn method1(&self) -> PyResult { - Ok(self.val1) - } -} - -#[pyclass(extends=BaseClass, subclass)] -struct SubClass { - val2: usize, -} - -#[pymethods] -impl SubClass { - #[new] - fn new() -> (Self, BaseClass) { - (SubClass { val2: 15 }, BaseClass::new()) - } - - fn method2(self_: PyRef<'_, Self>) -> PyResult { - let super_ = self_.as_super(); // Get &PyRef - super_.method1().map(|x| x * self_.val2) - } -} - -#[pyclass(extends=SubClass)] -struct SubSubClass { - val3: usize, -} - -#[pymethods] -impl SubSubClass { - #[new] - fn new() -> PyClassInitializer { - PyClassInitializer::from(SubClass::new()).add_subclass(SubSubClass { val3: 20 }) - } - - fn method3(self_: PyRef<'_, Self>) -> PyResult { - let base = self_.as_super().as_super(); // Get &PyRef<'_, BaseClass> - base.method1().map(|x| x * self_.val3) - } - - fn method4(self_: PyRef<'_, Self>) -> PyResult { - let v = self_.val3; - let super_ = self_.into_super(); // Get PyRef<'_, SubClass> - SubClass::method2(super_).map(|x| x * v) - } - - fn get_values(self_: PyRef<'_, Self>) -> (usize, usize, usize) { - let val1 = self_.as_super().as_super().val1; - let val2 = self_.as_super().val2; - (val1, val2, self_.val3) - } - - fn double_values(mut self_: PyRefMut<'_, Self>) { - self_.as_super().as_super().val1 *= 2; - self_.as_super().val2 *= 2; - self_.val3 *= 2; - } - - #[staticmethod] - fn factory_method(py: Python<'_>, val: usize) -> PyResult { - let base = PyClassInitializer::from(BaseClass::new()); - let sub = base.add_subclass(SubClass { val2: val }); - if val % 2 == 0 { - Ok(Py::new(py, sub)?.into_any()) - } else { - let sub_sub = sub.add_subclass(SubSubClass { val3: val }); - Ok(Py::new(py, sub_sub)?.into_any()) - } - } -} -# Python::with_gil(|py| { -# let subsub = pyo3::Py::new(py, SubSubClass::new()).unwrap(); -# pyo3::py_run!(py, subsub, "assert subsub.method1() == 10"); -# pyo3::py_run!(py, subsub, "assert subsub.method2() == 150"); -# pyo3::py_run!(py, subsub, "assert subsub.method3() == 200"); -# pyo3::py_run!(py, subsub, "assert subsub.method4() == 3000"); -# pyo3::py_run!(py, subsub, "assert subsub.get_values() == (10, 15, 20)"); -# pyo3::py_run!(py, subsub, "assert subsub.double_values() == None"); -# pyo3::py_run!(py, subsub, "assert subsub.get_values() == (20, 30, 40)"); -# let subsub = SubSubClass::factory_method(py, 2).unwrap(); -# let subsubsub = SubSubClass::factory_method(py, 3).unwrap(); -# let cls = py.get_type::(); -# pyo3::py_run!(py, subsub cls, "assert not isinstance(subsub, cls)"); -# pyo3::py_run!(py, subsubsub cls, "assert isinstance(subsubsub, cls)"); -# }); -``` - -You can inherit native types such as `PyDict`, if they implement -[`PySizedLayout`]({{#PYO3_DOCS_URL}}/pyo3/type_object/trait.PySizedLayout.html). -This is not supported when building for the Python limited API (aka the `abi3` feature of PyO3). - -To convert between the Rust type and its native base class, you can take -`slf` as a Python object. To access the Rust fields use `slf.borrow()` or -`slf.borrow_mut()`, and to access the base class use `slf.downcast::()`. - -```rust -# #[cfg(not(Py_LIMITED_API))] { -# use pyo3::prelude::*; -use pyo3::types::PyDict; -use std::collections::HashMap; - -#[pyclass(extends=PyDict)] -#[derive(Default)] -struct DictWithCounter { - counter: HashMap, -} - -#[pymethods] -impl DictWithCounter { - #[new] - fn new() -> Self { - Self::default() - } - - fn set(slf: &Bound<'_, Self>, key: String, value: Bound<'_, PyAny>) -> PyResult<()> { - slf.borrow_mut().counter.entry(key.clone()).or_insert(0); - let dict = slf.downcast::()?; - dict.set_item(key, value) - } -} -# Python::with_gil(|py| { -# let cnt = pyo3::Py::new(py, DictWithCounter::new()).unwrap(); -# pyo3::py_run!(py, cnt, "cnt.set('abc', 10); assert cnt['abc'] == 10") -# }); -# } -``` - -If `SubClass` does not provide a base class initialization, the compilation fails. -```rust,compile_fail -# use pyo3::prelude::*; - -#[pyclass] -struct BaseClass { - val1: usize, -} - -#[pyclass(extends=BaseClass)] -struct SubClass { - val2: usize, -} - -#[pymethods] -impl SubClass { - #[new] - fn new() -> Self { - SubClass { val2: 15 } - } -} -``` - -The `__new__` constructor of a native base class is called implicitly when -creating a new instance from Python. Be sure to accept arguments in the -`#[new]` method that you want the base class to get, even if they are not used -in that `fn`: - -```rust -# #[allow(dead_code)] -# #[cfg(not(Py_LIMITED_API))] { -# use pyo3::prelude::*; -use pyo3::types::PyDict; - -#[pyclass(extends=PyDict)] -struct MyDict { - private: i32, -} - -#[pymethods] -impl MyDict { - #[new] - #[pyo3(signature = (*args, **kwargs))] - fn new(args: &Bound<'_, PyAny>, kwargs: Option<&Bound<'_, PyAny>>) -> Self { - Self { private: 0 } - } - - // some custom methods that use `private` here... -} -# Python::with_gil(|py| { -# let cls = py.get_type::(); -# pyo3::py_run!(py, cls, "cls(a=1, b=2)") -# }); -# } -``` - -Here, the `args` and `kwargs` allow creating instances of the subclass passing -initial items, such as `MyDict(item_sequence)` or `MyDict(a=1, b=2)`. - -## Object properties - -PyO3 supports two ways to add properties to your `#[pyclass]`: -- For simple struct fields with no side effects, a `#[pyo3(get, set)]` attribute can be added directly to the field definition in the `#[pyclass]`. -- For properties which require computation you can define `#[getter]` and `#[setter]` functions in the [`#[pymethods]`](#instance-methods) block. - -We'll cover each of these in the following sections. - -### Object properties using `#[pyo3(get, set)]` - -For simple cases where a member variable is just read and written with no side effects, you can declare getters and setters in your `#[pyclass]` field definition using the `pyo3` attribute, like in the example below: - -```rust -# use pyo3::prelude::*; -# #[allow(dead_code)] -#[pyclass] -struct MyClass { - #[pyo3(get, set)] - num: i32, -} -``` - -The above would make the `num` field available for reading and writing as a `self.num` Python property. To expose the property with a different name to the field, specify this alongside the rest of the options, e.g. `#[pyo3(get, set, name = "custom_name")]`. - -Properties can be readonly or writeonly by using just `#[pyo3(get)]` or `#[pyo3(set)]` respectively. - -To use these annotations, your field type must implement some conversion traits: -- For `get` the field type must implement both `IntoPy` and `Clone`. -- For `set` the field type must implement `FromPyObject`. - -For example, implementations of those traits are provided for the `Cell` type, if the inner type also implements the trait. This means you can use `#[pyo3(get, set)]` on fields wrapped in a `Cell`. - -### Object properties using `#[getter]` and `#[setter]` - -For cases which don't satisfy the `#[pyo3(get, set)]` trait requirements, or need side effects, descriptor methods can be defined in a `#[pymethods]` `impl` block. - -This is done using the `#[getter]` and `#[setter]` attributes, like in the example below: - -```rust -# use pyo3::prelude::*; -#[pyclass] -struct MyClass { - num: i32, -} - -#[pymethods] -impl MyClass { - #[getter] - fn num(&self) -> PyResult { - Ok(self.num) - } -} -``` - -A getter or setter's function name is used as the property name by default. There are several -ways how to override the name. - -If a function name starts with `get_` or `set_` for getter or setter respectively, -the descriptor name becomes the function name with this prefix removed. This is also useful in case of -Rust keywords like `type` -([raw identifiers](https://doc.rust-lang.org/edition-guide/rust-2018/module-system/raw-identifiers.html) -can be used since Rust 2018). - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass { -# num: i32, -# } -#[pymethods] -impl MyClass { - #[getter] - fn get_num(&self) -> PyResult { - Ok(self.num) - } - - #[setter] - fn set_num(&mut self, value: i32) -> PyResult<()> { - self.num = value; - Ok(()) - } -} -``` - -In this case, a property `num` is defined and available from Python code as `self.num`. - -Both the `#[getter]` and `#[setter]` attributes accept one parameter. -If this parameter is specified, it is used as the property name, i.e. - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass { -# num: i32, -# } -#[pymethods] -impl MyClass { - #[getter(number)] - fn num(&self) -> PyResult { - Ok(self.num) - } - - #[setter(number)] - fn set_num(&mut self, value: i32) -> PyResult<()> { - self.num = value; - Ok(()) - } -} -``` - -In this case, the property `number` is defined and available from Python code as `self.number`. - -Attributes defined by `#[setter]` or `#[pyo3(set)]` will always raise `AttributeError` on `del` -operations. Support for defining custom `del` behavior is tracked in -[#1778](https://github.com/PyO3/pyo3/issues/1778). - -## Instance methods - -To define a Python compatible method, an `impl` block for your struct has to be annotated with the -`#[pymethods]` attribute. PyO3 generates Python compatible wrappers for all functions in this -block with some variations, like descriptors, class method static methods, etc. - -Since Rust allows any number of `impl` blocks, you can easily split methods -between those accessible to Python (and Rust) and those accessible only to Rust. However to have multiple -`#[pymethods]`-annotated `impl` blocks for the same struct you must enable the [`multiple-pymethods`] feature of PyO3. - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass { -# num: i32, -# } -#[pymethods] -impl MyClass { - fn method1(&self) -> PyResult { - Ok(10) - } - - fn set_method(&mut self, value: i32) -> PyResult<()> { - self.num = value; - Ok(()) - } -} -``` - -Calls to these methods are protected by the GIL, so both `&self` and `&mut self` can be used. -The return type must be `PyResult` or `T` for some `T` that implements `IntoPy`; -the latter is allowed if the method cannot raise Python exceptions. - -A `Python` parameter can be specified as part of method signature, in this case the `py` argument -gets injected by the method wrapper, e.g. - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass { -# #[allow(dead_code)] -# num: i32, -# } -#[pymethods] -impl MyClass { - fn method2(&self, py: Python<'_>) -> PyResult { - Ok(10) - } -} -``` - -From the Python perspective, the `method2` in this example does not accept any arguments. - -## Class methods - -To create a class method for a custom class, the method needs to be annotated -with the `#[classmethod]` attribute. -This is the equivalent of the Python decorator `@classmethod`. - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyType; -# #[pyclass] -# struct MyClass { -# #[allow(dead_code)] -# num: i32, -# } -#[pymethods] -impl MyClass { - #[classmethod] - fn cls_method(cls: &Bound<'_, PyType>) -> PyResult { - Ok(10) - } -} -``` - -Declares a class method callable from Python. - -* The first parameter is the type object of the class on which the method is called. - This may be the type object of a derived class. -* The first parameter implicitly has type `&Bound<'_, PyType>`. -* For details on `parameter-list`, see the documentation of `Method arguments` section. -* The return type must be `PyResult` or `T` for some `T` that implements `IntoPy`. - -### Constructors which accept a class argument - -To create a constructor which takes a positional class argument, you can combine the `#[classmethod]` and `#[new]` modifiers: -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyType; -# #[pyclass] -# struct BaseClass(PyObject); -# -#[pymethods] -impl BaseClass { - #[new] - #[classmethod] - fn py_new(cls: &Bound<'_, PyType>) -> PyResult { - // Get an abstract attribute (presumably) declared on a subclass of this class. - let subclass_attr: Bound<'_, PyAny> = cls.getattr("a_class_attr")?; - Ok(Self(subclass_attr.unbind())) - } -} -``` - -## Static methods - -To create a static method for a custom class, the method needs to be annotated with the -`#[staticmethod]` attribute. The return type must be `T` or `PyResult` for some `T` that implements -`IntoPy`. - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass { -# #[allow(dead_code)] -# num: i32, -# } -#[pymethods] -impl MyClass { - #[staticmethod] - fn static_method(param1: i32, param2: &str) -> PyResult { - Ok(10) - } -} -``` - -## Class attributes - -To create a class attribute (also called [class variable][classattr]), a method without -any arguments can be annotated with the `#[classattr]` attribute. - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass {} -#[pymethods] -impl MyClass { - #[classattr] - fn my_attribute() -> String { - "hello".to_string() - } -} - -Python::with_gil(|py| { - let my_class = py.get_type::(); - pyo3::py_run!(py, my_class, "assert my_class.my_attribute == 'hello'") -}); -``` - -> Note: if the method has a `Result` return type and returns an `Err`, PyO3 will panic during -class creation. - -If the class attribute is defined with `const` code only, one can also annotate associated -constants: - -```rust -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass {} -#[pymethods] -impl MyClass { - #[classattr] - const MY_CONST_ATTRIBUTE: &'static str = "foobar"; -} -``` - -## Classes as function arguments - -Free functions defined using `#[pyfunction]` interact with classes through the same mechanisms as the self parameters of instance methods, i.e. they can take GIL-bound references, GIL-bound reference wrappers or GIL-indepedent references: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyclass] -struct MyClass { - my_field: i32, -} - -// Take a reference when the underlying `Bound` is irrelevant. -#[pyfunction] -fn increment_field(my_class: &mut MyClass) { - my_class.my_field += 1; -} - -// Take a reference wrapper when borrowing should be automatic, -// but interaction with the underlying `Bound` is desired. -#[pyfunction] -fn print_field(my_class: PyRef<'_, MyClass>) { - println!("{}", my_class.my_field); -} - -// Take a reference to the underlying Bound -// when borrowing needs to be managed manually. -#[pyfunction] -fn increment_then_print_field(my_class: &Bound<'_, MyClass>) { - my_class.borrow_mut().my_field += 1; - - println!("{}", my_class.borrow().my_field); -} - -// Take a GIL-indepedent reference when you want to store the reference elsewhere. -#[pyfunction] -fn print_refcnt(my_class: Py, py: Python<'_>) { - println!("{}", my_class.get_refcnt(py)); -} -``` - -Classes can also be passed by value if they can be cloned, i.e. they automatically implement `FromPyObject` if they implement `Clone`, e.g. via `#[derive(Clone)]`: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyclass] -#[derive(Clone)] -struct MyClass { - my_field: Box, -} - -#[pyfunction] -fn dissamble_clone(my_class: MyClass) { - let MyClass { mut my_field } = my_class; - *my_field += 1; -} -``` - -Note that `#[derive(FromPyObject)]` on a class is usually not useful as it tries to construct a new Rust value by filling in the fields by looking up attributes of any given Python value. - -## Method arguments - -Similar to `#[pyfunction]`, the `#[pyo3(signature = (...))]` attribute can be used to specify the way that `#[pymethods]` accept arguments. Consult the documentation for [`function signatures`](./function/signature.md) to see the parameters this attribute accepts. - -The following example defines a class `MyClass` with a method `method`. This method has a signature that sets default values for `num` and `name`, and indicates that `py_args` should collect all extra positional arguments and `py_kwargs` all extra keyword arguments: - -```rust -# use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; -# -# #[pyclass] -# struct MyClass { -# num: i32, -# } -#[pymethods] -impl MyClass { - #[new] - #[pyo3(signature = (num=-1))] - fn new(num: i32) -> Self { - MyClass { num } - } - - #[pyo3(signature = (num=10, *py_args, name="Hello", **py_kwargs))] - fn method( - &mut self, - num: i32, - py_args: &Bound<'_, PyTuple>, - name: &str, - py_kwargs: Option<&Bound<'_, PyDict>>, - ) -> String { - let num_before = self.num; - self.num = num; - format!( - "num={} (was previously={}), py_args={:?}, name={}, py_kwargs={:?} ", - num, num_before, py_args, name, py_kwargs, - ) - } -} -``` - -In Python, this might be used like: - -```python ->>> import mymodule ->>> mc = mymodule.MyClass() ->>> print(mc.method(44, False, "World", 666, x=44, y=55)) -py_args=('World', 666), py_kwargs=Some({'x': 44, 'y': 55}), name=Hello, num=44, num_before=-1 ->>> print(mc.method(num=-1, name="World")) -py_args=(), py_kwargs=None, name=World, num=-1, num_before=44 -``` - -The [`#[pyo3(text_signature = "...")`](./function/signature.md#overriding-the-generated-signature) option for `#[pyfunction]` also works for `#[pymethods]`. - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; -use pyo3::types::PyType; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[new] - #[pyo3(text_signature = "(c, d)")] - fn new(c: i32, d: &str) -> Self { - Self {} - } - // the self argument should be written $self - #[pyo3(text_signature = "($self, e, f)")] - fn my_method(&self, e: i32, f: i32) -> i32 { - e + f - } - // similarly for classmethod arguments, use $cls - #[classmethod] - #[pyo3(text_signature = "($cls, e, f)")] - fn my_class_method(cls: &Bound<'_, PyType>, e: i32, f: i32) -> i32 { - e + f - } - #[staticmethod] - #[pyo3(text_signature = "(e, f)")] - fn my_static_method(e: i32, f: i32) -> i32 { - e + f - } -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?; -# let module = PyModule::new(py, "my_module")?; -# module.add_class::()?; -# let class = module.getattr("MyClass")?; -# -# if cfg!(not(Py_LIMITED_API)) || py.version_info() >= (3, 10) { -# let doc: String = class.getattr("__doc__")?.extract()?; -# assert_eq!(doc, ""); -# -# let sig: String = inspect -# .call1((&class,))? -# .call_method0("__str__")? -# .extract()?; -# assert_eq!(sig, "(c, d)"); -# } else { -# let doc: String = class.getattr("__doc__")?.extract()?; -# assert_eq!(doc, ""); -# -# inspect.call1((&class,)).expect_err("`text_signature` on classes is not compatible with compilation in `abi3` mode until Python 3.10 or greater"); -# } -# -# { -# let method = class.getattr("my_method")?; -# -# assert!(method.getattr("__doc__")?.is_none()); -# -# let sig: String = inspect -# .call1((method,))? -# .call_method0("__str__")? -# .extract()?; -# assert_eq!(sig, "(self, /, e, f)"); -# } -# -# { -# let method = class.getattr("my_class_method")?; -# -# assert!(method.getattr("__doc__")?.is_none()); -# -# let sig: String = inspect -# .call1((method,))? -# .call_method0("__str__")? -# .extract()?; -# assert_eq!(sig, "(e, f)"); // inspect.signature skips the $cls arg -# } -# -# { -# let method = class.getattr("my_static_method")?; -# -# assert!(method.getattr("__doc__")?.is_none()); -# -# let sig: String = inspect -# .call1((method,))? -# .call_method0("__str__")? -# .extract()?; -# assert_eq!(sig, "(e, f)"); -# } -# -# Ok(()) -# }) -# } -``` - -Note that `text_signature` on `#[new]` is not compatible with compilation in -`abi3` mode until Python 3.10 or greater. - -### Method receivers and lifetime elision - -PyO3 supports writing instance methods using the normal method receivers for shared `&self` and unique `&mut self` references. This interacts with [lifetime elision][lifetime-elision] insofar as the lifetime of a such a receiver is assigned to all elided output lifetime parameters. - -This is a good default for general Rust code where return values are more likely to borrow from the receiver than from the other arguments, if they contain any lifetimes at all. However, when returning bound references `Bound<'py, T>` in PyO3-based code, the GIL lifetime `'py` should usually be derived from a GIL token `py: Python<'py>` passed as an argument instead of the receiver. - -Specifically, signatures like - -```rust,ignore -fn frobnicate(&self, py: Python) -> Bound; -``` - -will not work as they are inferred as - -```rust,ignore -fn frobnicate<'a, 'py>(&'a self, py: Python<'py>) -> Bound<'a, Foo>; -``` - -instead of the intended - -```rust,ignore -fn frobnicate<'a, 'py>(&'a self, py: Python<'py>) -> Bound<'py, Foo>; -``` - -and should usually be written as - -```rust,ignore -fn frobnicate<'py>(&self, py: Python<'py>) -> Bound<'py, Foo>; -``` - -The same problem does not exist for `#[pyfunction]`s as the special case for receiver lifetimes does not apply and indeed a signature like - -```rust,ignore -fn frobnicate(bar: &Bar, py: Python) -> Bound; -``` - -will yield compiler error [E0106 "missing lifetime specifier"][compiler-error-e0106]. - -## `#[pyclass]` enums - -Enum support in PyO3 comes in two flavors, depending on what kind of variants the enum has: simple and complex. - -### Simple enums - -A simple enum (a.k.a. C-like enum) has only unit variants. - -PyO3 adds a class attribute for each variant, so you can access them in Python without defining `#[new]`. PyO3 also provides default implementations of `__richcmp__` and `__int__`, so they can be compared using `==`: - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum MyEnum { - Variant, - OtherVariant, -} - -Python::with_gil(|py| { - let x = Py::new(py, MyEnum::Variant).unwrap(); - let y = Py::new(py, MyEnum::OtherVariant).unwrap(); - let cls = py.get_type::(); - pyo3::py_run!(py, x y cls, r#" - assert x == cls.Variant - assert y == cls.OtherVariant - assert x != y - "#) -}) -``` - -You can also convert your simple enums into `int`: - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum MyEnum { - Variant, - OtherVariant = 10, -} - -Python::with_gil(|py| { - let cls = py.get_type::(); - let x = MyEnum::Variant as i32; // The exact value is assigned by the compiler. - pyo3::py_run!(py, cls x, r#" - assert int(cls.Variant) == x - assert int(cls.OtherVariant) == 10 - "#) -}) -``` - -PyO3 also provides `__repr__` for enums: - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum MyEnum{ - Variant, - OtherVariant, -} - -Python::with_gil(|py| { - let cls = py.get_type::(); - let x = Py::new(py, MyEnum::Variant).unwrap(); - pyo3::py_run!(py, cls x, r#" - assert repr(x) == 'MyEnum.Variant' - assert repr(cls.OtherVariant) == 'MyEnum.OtherVariant' - "#) -}) -``` - -All methods defined by PyO3 can be overridden. For example here's how you override `__repr__`: - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum MyEnum { - Answer = 42, -} - -#[pymethods] -impl MyEnum { - fn __repr__(&self) -> &'static str { - "42" - } -} - -Python::with_gil(|py| { - let cls = py.get_type::(); - pyo3::py_run!(py, cls, "assert repr(cls.Answer) == '42'") -}) -``` - -Enums and their variants can also be renamed using `#[pyo3(name)]`. - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, eq_int, name = "RenamedEnum")] -#[derive(PartialEq)] -enum MyEnum { - #[pyo3(name = "UPPERCASE")] - Variant, -} - -Python::with_gil(|py| { - let x = Py::new(py, MyEnum::Variant).unwrap(); - let cls = py.get_type::(); - pyo3::py_run!(py, x cls, r#" - assert repr(x) == 'RenamedEnum.UPPERCASE' - assert x == cls.UPPERCASE - "#) -}) -``` - -Ordering of enum variants is optionally added using `#[pyo3(ord)]`. -*Note: Implementation of the `PartialOrd` trait is required when passing the `ord` argument. If not implemented, a compile time error is raised.* - -```rust -# use pyo3::prelude::*; -#[pyclass(eq, ord)] -#[derive(PartialEq, PartialOrd)] -enum MyEnum{ - A, - B, - C, -} - -Python::with_gil(|py| { - let cls = py.get_type::(); - let a = Py::new(py, MyEnum::A).unwrap(); - let b = Py::new(py, MyEnum::B).unwrap(); - let c = Py::new(py, MyEnum::C).unwrap(); - pyo3::py_run!(py, cls a b c, r#" - assert (a < b) == True - assert (c <= b) == False - assert (c > a) == True - "#) -}) -``` - -You may not use enums as a base class or let enums inherit from other classes. - -```rust,compile_fail -# use pyo3::prelude::*; -#[pyclass(subclass)] -enum BadBase { - Var1, -} -``` - -```rust,compile_fail -# use pyo3::prelude::*; - -#[pyclass(subclass)] -struct Base; - -#[pyclass(extends=Base)] -enum BadSubclass { - Var1, -} -``` - -`#[pyclass]` enums are currently not interoperable with `IntEnum` in Python. - -### Complex enums - -An enum is complex if it has any non-unit (struct or tuple) variants. - -PyO3 supports only struct and tuple variants in a complex enum. Unit variants aren't supported at present (the recommendation is to use an empty tuple enum instead). - -PyO3 adds a class attribute for each variant, which may be used to construct values and in match patterns. PyO3 also provides getter methods for all fields of each variant. - -```rust -# use pyo3::prelude::*; -#[pyclass] -enum Shape { - Circle { radius: f64 }, - Rectangle { width: f64, height: f64 }, - RegularPolygon(u32, f64), - Nothing { }, -} - -# #[cfg(Py_3_10)] -Python::with_gil(|py| { - let circle = Shape::Circle { radius: 10.0 }.into_pyobject(py)?; - let square = Shape::RegularPolygon(4, 10.0).into_pyobject(py)?; - let cls = py.get_type::(); - pyo3::py_run!(py, circle square cls, r#" - assert isinstance(circle, cls) - assert isinstance(circle, cls.Circle) - assert circle.radius == 10.0 - - assert isinstance(square, cls) - assert isinstance(square, cls.RegularPolygon) - assert square[0] == 4 # Gets _0 field - assert square[1] == 10.0 # Gets _1 field - - def count_vertices(cls, shape): - match shape: - case cls.Circle(): - return 0 - case cls.Rectangle(): - return 4 - case cls.RegularPolygon(n): - return n - case cls.Nothing(): - return 0 - - assert count_vertices(cls, circle) == 0 - assert count_vertices(cls, square) == 4 - "#); -# Ok::<_, PyErr>(()) -}) -# .unwrap(); -``` - -WARNING: `Py::new` and `.into_pyobject` are currently inconsistent. Note how the constructed value is _not_ an instance of the specific variant. For this reason, constructing values is only recommended using `.into_pyobject`. - -```rust -# use pyo3::prelude::*; -#[pyclass] -enum MyEnum { - Variant { i: i32 }, -} - -Python::with_gil(|py| { - let x = Py::new(py, MyEnum::Variant { i: 42 }).unwrap(); - let cls = py.get_type::(); - pyo3::py_run!(py, x cls, r#" - assert isinstance(x, cls) - assert not isinstance(x, cls.Variant) - "#) -}) -``` - -The constructor of each generated class can be customized using the `#[pyo3(constructor = (...))]` attribute. This uses the same syntax as the [`#[pyo3(signature = (...))]`](function/signature.md) -attribute on function and methods and supports the same options. To apply this attribute simply place it on top of a variant in a `#[pyclass]` complex enum as shown below: - -```rust -# use pyo3::prelude::*; -#[pyclass] -enum Shape { - #[pyo3(constructor = (radius=1.0))] - Circle { radius: f64 }, - #[pyo3(constructor = (*, width, height))] - Rectangle { width: f64, height: f64 }, - #[pyo3(constructor = (side_count, radius=1.0))] - RegularPolygon { side_count: u32, radius: f64 }, - Nothing { }, -} - -# #[cfg(Py_3_10)] -Python::with_gil(|py| { - let cls = py.get_type::(); - pyo3::py_run!(py, cls, r#" - circle = cls.Circle() - assert isinstance(circle, cls) - assert isinstance(circle, cls.Circle) - assert circle.radius == 1.0 - - square = cls.Rectangle(width = 1, height = 1) - assert isinstance(square, cls) - assert isinstance(square, cls.Rectangle) - assert square.width == 1 - assert square.height == 1 - - hexagon = cls.RegularPolygon(6) - assert isinstance(hexagon, cls) - assert isinstance(hexagon, cls.RegularPolygon) - assert hexagon.side_count == 6 - assert hexagon.radius == 1 - "#) -}) -``` - -## Implementation details - -The `#[pyclass]` macros rely on a lot of conditional code generation: each `#[pyclass]` can optionally have a `#[pymethods]` block. - -To support this flexibility the `#[pyclass]` macro expands to a blob of boilerplate code which sets up the structure for ["dtolnay specialization"](https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md). This implementation pattern enables the Rust compiler to use `#[pymethods]` implementations when they are present, and fall back to default (empty) definitions when they are not. - -This simple technique works for the case when there is zero or one implementations. To support multiple `#[pymethods]` for a `#[pyclass]` (in the [`multiple-pymethods`] feature), a registry mechanism provided by the [`inventory`](https://github.com/dtolnay/inventory) crate is used instead. This collects `impl`s at library load time, but isn't supported on all platforms. See [inventory: how it works](https://github.com/dtolnay/inventory#how-it-works) for more details. - -The `#[pyclass]` macro expands to roughly the code seen below. The `PyClassImplCollector` is the type used internally by PyO3 for dtolnay specialization: - -```rust -# #[cfg(not(feature = "multiple-pymethods"))] { -# use pyo3::prelude::*; -// Note: the implementation differs slightly with the `multiple-pymethods` feature enabled. -# #[allow(dead_code)] -struct MyClass { - # #[allow(dead_code)] - num: i32, -} - -impl pyo3::types::DerefToPyAny for MyClass {} - -unsafe impl pyo3::type_object::PyTypeInfo for MyClass { - const NAME: &'static str = "MyClass"; - const MODULE: ::std::option::Option<&'static str> = ::std::option::Option::None; - #[inline] - fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject { - ::lazy_type_object() - .get_or_init(py) - .as_type_ptr() - } -} - -impl pyo3::PyClass for MyClass { - type Frozen = pyo3::pyclass::boolean_struct::False; -} - -impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a MyClass -{ - type Holder = ::std::option::Option>; - - #[inline] - fn extract(obj: &'a pyo3::Bound<'py, PyAny>, holder: &'a mut Self::Holder) -> pyo3::PyResult { - pyo3::impl_::extract_argument::extract_pyclass_ref(obj, holder) - } -} - -impl<'a, 'py> pyo3::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a mut MyClass -{ - type Holder = ::std::option::Option>; - - #[inline] - fn extract(obj: &'a pyo3::Bound<'py, PyAny>, holder: &'a mut Self::Holder) -> pyo3::PyResult { - pyo3::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder) - } -} - -#[allow(deprecated)] -impl pyo3::IntoPy for MyClass { - fn into_py(self, py: pyo3::Python<'_>) -> pyo3::PyObject { - pyo3::IntoPy::into_py(pyo3::Py::new(py, self).unwrap(), py) - } -} - -impl pyo3::impl_::pyclass::PyClassImpl for MyClass { - const IS_BASETYPE: bool = false; - const IS_SUBCLASS: bool = false; - const IS_MAPPING: bool = false; - const IS_SEQUENCE: bool = false; - type BaseType = PyAny; - type ThreadChecker = pyo3::impl_::pyclass::SendablePyClass; - type PyClassMutability = <::PyClassMutability as pyo3::impl_::pycell::PyClassMutability>::MutableChild; - type Dict = pyo3::impl_::pyclass::PyClassDummySlot; - type WeakRef = pyo3::impl_::pyclass::PyClassDummySlot; - type BaseNativeType = pyo3::PyAny; - - fn items_iter() -> pyo3::impl_::pyclass::PyClassItemsIter { - use pyo3::impl_::pyclass::*; - let collector = PyClassImplCollector::::new(); - static INTRINSIC_ITEMS: PyClassItems = PyClassItems { slots: &[], methods: &[] }; - PyClassItemsIter::new(&INTRINSIC_ITEMS, collector.py_methods()) - } - - fn lazy_type_object() -> &'static pyo3::impl_::pyclass::LazyTypeObject { - use pyo3::impl_::pyclass::LazyTypeObject; - static TYPE_OBJECT: LazyTypeObject = LazyTypeObject::new(); - &TYPE_OBJECT - } - - fn doc(py: Python<'_>) -> pyo3::PyResult<&'static ::std::ffi::CStr> { - use pyo3::impl_::pyclass::*; - static DOC: pyo3::sync::GILOnceCell<::std::borrow::Cow<'static, ::std::ffi::CStr>> = pyo3::sync::GILOnceCell::new(); - DOC.get_or_try_init(py, || { - let collector = PyClassImplCollector::::new(); - build_pyclass_doc(::NAME, pyo3::ffi::c_str!(""), collector.new_text_signature()) - }).map(::std::ops::Deref::deref) - } -} - -# Python::with_gil(|py| { -# let cls = py.get_type::(); -# pyo3::py_run!(py, cls, "assert cls.__name__ == 'MyClass'") -# }); -# } -``` - - -[`PyTypeInfo`]: {{#PYO3_DOCS_URL}}/pyo3/type_object/trait.PyTypeInfo.html - -[`Py`]: {{#PYO3_DOCS_URL}}/pyo3/struct.Py.html -[`Bound<'_, T>`]: {{#PYO3_DOCS_URL}}/pyo3/struct.Bound.html -[`PyClass`]: {{#PYO3_DOCS_URL}}/pyo3/pyclass/trait.PyClass.html -[`PyRef`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRef.html -[`PyRefMut`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRefMut.html -[`PyClassInitializer`]: {{#PYO3_DOCS_URL}}/pyo3/pyclass_init/struct.PyClassInitializer.html - -[`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html -[`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html - -[classattr]: https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables - -[`multiple-pymethods`]: features.md#multiple-pymethods - -[lifetime-elision]: https://doc.rust-lang.org/reference/lifetime-elision.html -[compiler-error-e0106]: https://doc.rust-lang.org/error_codes/E0106.html diff --git a/vendor/pyo3/guide/src/class/call.md b/vendor/pyo3/guide/src/class/call.md deleted file mode 100644 index 5242a4f41a..0000000000 --- a/vendor/pyo3/guide/src/class/call.md +++ /dev/null @@ -1,122 +0,0 @@ -# Emulating callable objects - -Classes can be callable if they have a `#[pymethod]` named `__call__`. -This allows instances of a class to behave similar to functions. - -This method's signature must look like `__call__(, ...) -> object` - here, - any argument list can be defined as for normal pymethods - -### Example: Implementing a call counter - -The following pyclass is a basic decorator - its constructor takes a Python object -as argument and calls that object when called. An equivalent Python implementation -is linked at the end. - -An example crate containing this pyclass can be found [here](https://github.com/PyO3/pyo3/tree/main/examples/decorator) - -```rust,ignore -{{#include ../../../examples/decorator/src/lib.rs}} -``` - -Python code: - -```python -{{#include ../../../examples/decorator/tests/example.py}} -``` - -Output: - -```text -say_hello has been called 1 time(s). -hello -say_hello has been called 2 time(s). -hello -say_hello has been called 3 time(s). -hello -say_hello has been called 4 time(s). -hello -``` - -### Pure Python implementation - -A Python implementation of this looks similar to the Rust version: - -```python -class Counter: - def __init__(self, wraps): - self.count = 0 - self.wraps = wraps - - def __call__(self, *args, **kwargs): - self.count += 1 - print(f"{self.wraps.__name__} has been called {self.count} time(s)") - self.wraps(*args, **kwargs) -``` - -Note that it can also be implemented as a higher order function: - -```python -def Counter(wraps): - count = 0 - def call(*args, **kwargs): - nonlocal count - count += 1 - print(f"{wraps.__name__} has been called {count} time(s)") - return wraps(*args, **kwargs) - return call -``` - -### What is the `AtomicU64` for? - -A [previous implementation] used a normal `u64`, which meant it required a `&mut self` receiver to update the count: - -```rust,ignore -#[pyo3(signature = (*args, **kwargs))] -fn __call__( - &mut self, - py: Python<'_>, - args: &Bound<'_, PyTuple>, - kwargs: Option<&Bound<'_, PyDict>>, -) -> PyResult> { - self.count += 1; - let name = self.wraps.getattr(py, "__name__")?; - - println!("{} has been called {} time(s).", name, self.count); - - // After doing something, we finally forward the call to the wrapped function - let ret = self.wraps.call(py, args, kwargs)?; - - // We could do something with the return value of - // the function before returning it - Ok(ret) -} -``` - -The problem with this is that the `&mut self` receiver means PyO3 has to borrow it exclusively, - and hold this borrow across the`self.wraps.call(py, args, kwargs)` call. This call returns control to the user's Python code - which is free to call arbitrary things, *including* the decorated function. If that happens PyO3 is unable to create a second unique borrow and will be forced to raise an exception. - -As a result, something innocent like this will raise an exception: - -```py -@Counter -def say_hello(): - if say_hello.count < 2: - print(f"hello from decorator") - -say_hello() -# RuntimeError: Already borrowed -``` - -The implementation in this chapter fixes that by never borrowing exclusively; all the methods take `&self` as receivers, of which multiple may exist simultaneously. This requires a shared counter and the most straightforward way to implement thread-safe interior mutability (e.g. the type does not need to accept `&mut self` to modify the "interior" state) for a `u64` is to use [`AtomicU64`], so that's what is used here. - -This shows the dangers of running arbitrary Python code - note that "running arbitrary Python code" can be far more subtle than the example above: -- Python's asynchronous executor may park the current thread in the middle of Python code, even in Python code that *you* control, and let other Python code run. -- Dropping arbitrary Python objects may invoke destructors defined in Python (`__del__` methods). -- Calling Python's C-api (most PyO3 apis call C-api functions internally) may raise exceptions, which may allow Python code in signal handlers to run. -- On the free-threaded build, users might use Python's `threading` module to work with your types simultaneously from multiple OS threads. - -This is especially important if you are writing unsafe code; Python code must never be able to cause undefined behavior. You must ensure that your Rust code is in a consistent state before doing any of the above things. - -[previous implementation]: https://github.com/PyO3/pyo3/discussions/2598 "Thread Safe Decorator · Discussion #2598 · PyO3/pyo3" -[`AtomicU64`]: https://doc.rust-lang.org/std/sync/atomic/struct.AtomicU64.html "AtomicU64 in std::sync::atomic - Rust" diff --git a/vendor/pyo3/guide/src/class/numeric.md b/vendor/pyo3/guide/src/class/numeric.md deleted file mode 100644 index a441eba4e1..0000000000 --- a/vendor/pyo3/guide/src/class/numeric.md +++ /dev/null @@ -1,445 +0,0 @@ -# Emulating numeric types - -At this point we have a `Number` class that we can't actually do any math on! - -Before proceeding, we should think about how we want to handle overflows. There are three obvious solutions: -- We can have infinite precision just like Python's `int`. However that would be quite boring - we'd - be reinventing the wheel. -- We can raise exceptions whenever `Number` overflows, but that makes the API painful to use. -- We can wrap around the boundary of `i32`. This is the approach we'll take here. To do that we'll just forward to `i32`'s - `wrapping_*` methods. - -### Fixing our constructor - -Let's address the first overflow, in `Number`'s constructor: - -```python -from my_module import Number - -n = Number(1 << 1337) -``` - -```text -Traceback (most recent call last): - File "example.py", line 3, in - n = Number(1 << 1337) -OverflowError: Python int too large to convert to C long -``` - -Instead of relying on the default [`FromPyObject`] extraction to parse arguments, we can specify our -own extraction function, using the `#[pyo3(from_py_with = "...")]` attribute. Unfortunately PyO3 -doesn't provide a way to wrap Python integers out of the box, but we can do a Python call to mask it -and cast it to an `i32`. - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; - -fn wrap(obj: &Bound<'_, PyAny>) -> PyResult { - let val = obj.call_method1("__and__", (0xFFFFFFFF_u32,))?; - let val: u32 = val.extract()?; - // 👇 This intentionally overflows! - Ok(val as i32) -} -``` -We also add documentation, via `///` comments, which are visible to Python users. - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; - -fn wrap(obj: &Bound<'_, PyAny>) -> PyResult { - let val = obj.call_method1("__and__", (0xFFFFFFFF_u32,))?; - let val: u32 = val.extract()?; - Ok(val as i32) -} - -/// Did you ever hear the tragedy of Darth Signed The Overfloweth? I thought not. -/// It's not a story C would tell you. It's a Rust legend. -#[pyclass(module = "my_module")] -struct Number(i32); - -#[pymethods] -impl Number { - #[new] - fn new(#[pyo3(from_py_with = "wrap")] value: i32) -> Self { - Self(value) - } -} -``` - - -With that out of the way, let's implement some operators: -```rust -use pyo3::exceptions::{PyZeroDivisionError, PyValueError}; - -# use pyo3::prelude::*; -# -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __add__(&self, other: &Self) -> Self { - Self(self.0.wrapping_add(other.0)) - } - - fn __sub__(&self, other: &Self) -> Self { - Self(self.0.wrapping_sub(other.0)) - } - - fn __mul__(&self, other: &Self) -> Self { - Self(self.0.wrapping_mul(other.0)) - } - - fn __truediv__(&self, other: &Self) -> PyResult { - match self.0.checked_div(other.0) { - Some(i) => Ok(Self(i)), - None => Err(PyZeroDivisionError::new_err("division by zero")), - } - } - - fn __floordiv__(&self, other: &Self) -> PyResult { - match self.0.checked_div(other.0) { - Some(i) => Ok(Self(i)), - None => Err(PyZeroDivisionError::new_err("division by zero")), - } - } - - fn __rshift__(&self, other: &Self) -> PyResult { - match other.0.try_into() { - Ok(rhs) => Ok(Self(self.0.wrapping_shr(rhs))), - Err(_) => Err(PyValueError::new_err("negative shift count")), - } - } - - fn __lshift__(&self, other: &Self) -> PyResult { - match other.0.try_into() { - Ok(rhs) => Ok(Self(self.0.wrapping_shl(rhs))), - Err(_) => Err(PyValueError::new_err("negative shift count")), - } - } -} -``` - -### Unary arithmetic operations - -```rust -# use pyo3::prelude::*; -# -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __pos__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __neg__(&self) -> Self { - Self(-self.0) - } - - fn __abs__(&self) -> Self { - Self(self.0.abs()) - } - - fn __invert__(&self) -> Self { - Self(!self.0) - } -} -``` - -### Support for the `complex()`, `int()` and `float()` built-in functions. - -```rust -# use pyo3::prelude::*; -# -# #[pyclass] -# struct Number(i32); -# -use pyo3::types::PyComplex; - -#[pymethods] -impl Number { - fn __int__(&self) -> i32 { - self.0 - } - - fn __float__(&self) -> f64 { - self.0 as f64 - } - - fn __complex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyComplex> { - PyComplex::from_doubles(py, self.0 as f64, 0.0) - } -} -``` - -We do not implement the in-place operations like `__iadd__` because we do not wish to mutate `Number`. -Similarly we're not interested in supporting operations with different types, so we do not implement - the reflected operations like `__radd__` either. - -Now Python can use our `Number` class: - -```python -from my_module import Number - -def hash_djb2(s: str): - ''' - A version of Daniel J. Bernstein's djb2 string hashing algorithm - Like many hashing algorithms, it relies on integer wrapping. - ''' - - n = Number(0) - five = Number(5) - - for x in s: - n = Number(ord(x)) + ((n << five) - n) - return n - -assert hash_djb2('l50_50') == Number(-1152549421) -``` - -### Final code - -```rust -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; - -use pyo3::exceptions::{PyValueError, PyZeroDivisionError}; -use pyo3::prelude::*; -use pyo3::class::basic::CompareOp; -use pyo3::types::{PyComplex, PyString}; - -fn wrap(obj: &Bound<'_, PyAny>) -> PyResult { - let val = obj.call_method1("__and__", (0xFFFFFFFF_u32,))?; - let val: u32 = val.extract()?; - Ok(val as i32) -} -/// Did you ever hear the tragedy of Darth Signed The Overfloweth? I thought not. -/// It's not a story C would tell you. It's a Rust legend. -#[pyclass(module = "my_module")] -struct Number(i32); - -#[pymethods] -impl Number { - #[new] - fn new(#[pyo3(from_py_with = "wrap")] value: i32) -> Self { - Self(value) - } - - fn __repr__(slf: &Bound<'_, Self>) -> PyResult { - // Get the class name dynamically in case `Number` is subclassed - let class_name: Bound<'_, PyString> = slf.get_type().qualname()?; - Ok(format!("{}({})", class_name, slf.borrow().0)) - } - - fn __str__(&self) -> String { - self.0.to_string() - } - - fn __hash__(&self) -> u64 { - let mut hasher = DefaultHasher::new(); - self.0.hash(&mut hasher); - hasher.finish() - } - - fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { - match op { - CompareOp::Lt => Ok(self.0 < other.0), - CompareOp::Le => Ok(self.0 <= other.0), - CompareOp::Eq => Ok(self.0 == other.0), - CompareOp::Ne => Ok(self.0 != other.0), - CompareOp::Gt => Ok(self.0 > other.0), - CompareOp::Ge => Ok(self.0 >= other.0), - } - } - - fn __bool__(&self) -> bool { - self.0 != 0 - } - - fn __add__(&self, other: &Self) -> Self { - Self(self.0.wrapping_add(other.0)) - } - - fn __sub__(&self, other: &Self) -> Self { - Self(self.0.wrapping_sub(other.0)) - } - - fn __mul__(&self, other: &Self) -> Self { - Self(self.0.wrapping_mul(other.0)) - } - - fn __truediv__(&self, other: &Self) -> PyResult { - match self.0.checked_div(other.0) { - Some(i) => Ok(Self(i)), - None => Err(PyZeroDivisionError::new_err("division by zero")), - } - } - - fn __floordiv__(&self, other: &Self) -> PyResult { - match self.0.checked_div(other.0) { - Some(i) => Ok(Self(i)), - None => Err(PyZeroDivisionError::new_err("division by zero")), - } - } - - fn __rshift__(&self, other: &Self) -> PyResult { - match other.0.try_into() { - Ok(rhs) => Ok(Self(self.0.wrapping_shr(rhs))), - Err(_) => Err(PyValueError::new_err("negative shift count")), - } - } - - fn __lshift__(&self, other: &Self) -> PyResult { - match other.0.try_into() { - Ok(rhs) => Ok(Self(self.0.wrapping_shl(rhs))), - Err(_) => Err(PyValueError::new_err("negative shift count")), - } - } - - fn __xor__(&self, other: &Self) -> Self { - Self(self.0 ^ other.0) - } - - fn __or__(&self, other: &Self) -> Self { - Self(self.0 | other.0) - } - - fn __and__(&self, other: &Self) -> Self { - Self(self.0 & other.0) - } - - fn __int__(&self) -> i32 { - self.0 - } - - fn __float__(&self) -> f64 { - self.0 as f64 - } - - fn __complex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyComplex> { - PyComplex::from_doubles(py, self.0 as f64, 0.0) - } -} - -#[pymodule] -fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} -# const SCRIPT: &'static std::ffi::CStr = pyo3::ffi::c_str!(r#" -# def hash_djb2(s: str): -# n = Number(0) -# five = Number(5) -# -# for x in s: -# n = Number(ord(x)) + ((n << five) - n) -# return n -# -# assert hash_djb2('l50_50') == Number(-1152549421) -# assert hash_djb2('logo') == Number(3327403) -# assert hash_djb2('horizon') == Number(1097468315) -# -# -# assert Number(2) + Number(2) == Number(4) -# assert Number(2) + Number(2) != Number(5) -# -# assert Number(13) - Number(7) == Number(6) -# assert Number(13) - Number(-7) == Number(20) -# -# assert Number(13) / Number(7) == Number(1) -# assert Number(13) // Number(7) == Number(1) -# -# assert Number(13) * Number(7) == Number(13*7) -# -# assert Number(13) > Number(7) -# assert Number(13) < Number(20) -# assert Number(13) == Number(13) -# assert Number(13) >= Number(7) -# assert Number(13) <= Number(20) -# assert Number(13) == Number(13) -# -# -# assert (True if Number(1) else False) -# assert (False if Number(0) else True) -# -# -# assert int(Number(13)) == 13 -# assert float(Number(13)) == 13 -# assert Number.__doc__ == "Did you ever hear the tragedy of Darth Signed The Overfloweth? I thought not.\nIt's not a story C would tell you. It's a Rust legend." -# assert Number(12345234523452) == Number(1498514748) -# try: -# import inspect -# assert inspect.signature(Number).__str__() == '(value)' -# except ValueError: -# # Not supported with `abi3` before Python 3.10 -# pass -# assert Number(1337).__str__() == '1337' -# assert Number(1337).__repr__() == 'Number(1337)' -"#); - -# -# use pyo3::PyTypeInfo; -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let globals = PyModule::import(py, "__main__")?.dict(); -# globals.set_item("Number", Number::type_object(py))?; -# -# py.run(SCRIPT, Some(&globals), None)?; -# Ok(()) -# }) -# } -``` - -## Appendix: Writing some unsafe code - -At the beginning of this chapter we said that PyO3 doesn't provide a way to wrap Python integers out -of the box but that's a half truth. There's not a PyO3 API for it, but there's a Python C API -function that does: - -```c -unsigned long PyLong_AsUnsignedLongMask(PyObject *obj) -``` - -We can call this function from Rust by using [`pyo3::ffi::PyLong_AsUnsignedLongMask`]. This is an *unsafe* -function, which means we have to use an unsafe block to call it and take responsibility for upholding -the contracts of this function. Let's review those contracts: -- The GIL must be held. If it's not, calling this function causes a data race. -- The pointer must be valid, i.e. it must be properly aligned and point to a valid Python object. - -Let's create that helper function. The signature has to be `fn(&Bound<'_, PyAny>) -> PyResult`. -- `&Bound<'_, PyAny>` represents a checked borrowed reference, so the pointer derived from it is valid (and not null). -- Whenever we have borrowed references to Python objects in scope, it is guaranteed that the GIL is held. This reference is also where we can get a [`Python`] token to use in our call to [`PyErr::take`]. - -```rust -# #![allow(dead_code)] -use std::os::raw::c_ulong; -use pyo3::prelude::*; -use pyo3::ffi; - -fn wrap(obj: &Bound<'_, PyAny>) -> Result { - let py: Python<'_> = obj.py(); - - unsafe { - let ptr = obj.as_ptr(); - - let ret: c_ulong = ffi::PyLong_AsUnsignedLongMask(ptr); - if ret == c_ulong::MAX { - if let Some(err) = PyErr::take(py) { - return Err(err); - } - } - - Ok(ret as i32) - } -} -``` - -[`PyErr::take`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/struct.PyErr.html#method.take -[`Python`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html -[`FromPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.FromPyObject.html -[`pyo3::ffi::PyLong_AsUnsignedLongMask`]: {{#PYO3_DOCS_URL}}/pyo3/ffi/fn.PyLong_AsUnsignedLongMask.html diff --git a/vendor/pyo3/guide/src/class/object.md b/vendor/pyo3/guide/src/class/object.md deleted file mode 100644 index 07f445aac6..0000000000 --- a/vendor/pyo3/guide/src/class/object.md +++ /dev/null @@ -1,393 +0,0 @@ -# Basic object customization - -Recall the `Number` class from the previous chapter: - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; - -#[pyclass] -struct Number(i32); - -#[pymethods] -impl Number { - #[new] - fn new(value: i32) -> Self { - Self(value) - } -} - -#[pymodule] -fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} -``` - -At this point Python code can import the module, access the class and create class instances - but -nothing else. - -```python -from my_module import Number - -n = Number(5) -print(n) -``` - -```text - -``` - -### String representations - -It can't even print an user-readable representation of itself! We can fix that by defining the -`__repr__` and `__str__` methods inside a `#[pymethods]` block. We do this by accessing the value -contained inside `Number`. - -```rust -# use pyo3::prelude::*; -# -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - // For `__repr__` we want to return a string that Python code could use to recreate - // the `Number`, like `Number(5)` for example. - fn __repr__(&self) -> String { - // We use the `format!` macro to create a string. Its first argument is a - // format string, followed by any number of parameters which replace the - // `{}`'s in the format string. - // - // 👇 Tuple field access in Rust uses a dot - format!("Number({})", self.0) - } - // `__str__` is generally used to create an "informal" representation, so we - // just forward to `i32`'s `ToString` trait implementation to print a bare number. - fn __str__(&self) -> String { - self.0.to_string() - } -} -``` - -To automatically generate the `__str__` implementation using a `Display` trait implementation, pass the `str` argument to `pyclass`. - -```rust -# use std::fmt::{Display, Formatter}; -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass(str)] -# struct Coordinate { - x: i32, - y: i32, - z: i32, -} - -impl Display for Coordinate { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "({}, {}, {})", self.x, self.y, self.z) - } -} -``` - -For convenience, a shorthand format string can be passed to `str` as `str=""` for **structs only**. It expands and is passed into the `format!` macro in the following ways: - -* `"{x}"` -> `"{}", self.x` -* `"{0}"` -> `"{}", self.0` -* `"{x:?}"` -> `"{:?}", self.x` - -*Note: Depending upon the format string you use, this may require implementation of the `Display` or `Debug` traits for the given Rust types.* -*Note: the pyclass args `name` and `rename_all` are incompatible with the shorthand format string and will raise a compile time error.* - -```rust -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass(str="({x}, {y}, {z})")] -# struct Coordinate { - x: i32, - y: i32, - z: i32, -} -``` - -#### Accessing the class name - -In the `__repr__`, we used a hard-coded class name. This is sometimes not ideal, -because if the class is subclassed in Python, we would like the repr to reflect -the subclass name. This is typically done in Python code by accessing -`self.__class__.__name__`. In order to be able to access the Python type information -*and* the Rust struct, we need to use a `Bound` as the `self` argument. - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyString; -# -# #[allow(dead_code)] -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __repr__(slf: &Bound<'_, Self>) -> PyResult { - // This is the equivalent of `self.__class__.__name__` in Python. - let class_name: Bound<'_, PyString> = slf.get_type().qualname()?; - // To access fields of the Rust struct, we need to borrow the `PyCell`. - Ok(format!("{}({})", class_name, slf.borrow().0)) - } -} -``` - -### Hashing - - -Let's also implement hashing. We'll just hash the `i32`. For that we need a [`Hasher`]. The one -provided by `std` is [`DefaultHasher`], which uses the [SipHash] algorithm. - -```rust -use std::collections::hash_map::DefaultHasher; - -// Required to call the `.hash` and `.finish` methods, which are defined on traits. -use std::hash::{Hash, Hasher}; - -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __hash__(&self) -> u64 { - let mut hasher = DefaultHasher::new(); - self.0.hash(&mut hasher); - hasher.finish() - } -} -``` -To implement `__hash__` using the Rust [`Hash`] trait implementation, the `hash` option can be used. -This option is only available for `frozen` classes to prevent accidental hash changes from mutating the object. If you need -an `__hash__` implementation for a mutable class, use the manual method from above. This option also requires `eq`: According to the -[Python docs](https://docs.python.org/3/reference/datamodel.html#object.__hash__) "If a class does not define an `__eq__()` -method it should not define a `__hash__()` operation either" -```rust -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -#[pyclass(frozen, eq, hash)] -#[derive(PartialEq, Hash)] -struct Number(i32); -``` - - -> **Note**: When implementing `__hash__` and comparisons, it is important that the following property holds: -> -> ```text -> k1 == k2 -> hash(k1) == hash(k2) -> ``` -> -> In other words, if two keys are equal, their hashes must also be equal. In addition you must take -> care that your classes' hash doesn't change during its lifetime. In this tutorial we do that by not -> letting Python code change our `Number` class. In other words, it is immutable. -> -> By default, all `#[pyclass]` types have a default hash implementation from Python. -> Types which should not be hashable can override this by setting `__hash__` to None. -> This is the same mechanism as for a pure-Python class. This is done like so: -> -> ```rust -> # use pyo3::prelude::*; -> #[pyclass] -> struct NotHashable {} -> -> #[pymethods] -> impl NotHashable { -> #[classattr] -> const __hash__: Option> = None; -> } -> ``` - -### Comparisons - -PyO3 supports the usual magic comparison methods available in Python such as `__eq__`, `__lt__` -and so on. It is also possible to support all six operations at once with `__richcmp__`. -This method will be called with a value of `CompareOp` depending on the operation. - -```rust -use pyo3::class::basic::CompareOp; - -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { - match op { - CompareOp::Lt => Ok(self.0 < other.0), - CompareOp::Le => Ok(self.0 <= other.0), - CompareOp::Eq => Ok(self.0 == other.0), - CompareOp::Ne => Ok(self.0 != other.0), - CompareOp::Gt => Ok(self.0 > other.0), - CompareOp::Ge => Ok(self.0 >= other.0), - } - } -} -``` - -If you obtain the result by comparing two Rust values, as in this example, you -can take a shortcut using `CompareOp::matches`: - -```rust -use pyo3::class::basic::CompareOp; - -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { - op.matches(self.0.cmp(&other.0)) - } -} -``` - -It checks that the `std::cmp::Ordering` obtained from Rust's `Ord` matches -the given `CompareOp`. - -Alternatively, you can implement just equality using `__eq__`: - - -```rust -# use pyo3::prelude::*; -# -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let x = &Bound::new(py, Number(4))?; -# let y = &Bound::new(py, Number(4))?; -# assert!(x.eq(y)?); -# assert!(!x.ne(y)?); -# Ok(()) -# }) -# } -``` - -To implement `__eq__` using the Rust [`PartialEq`] trait implementation, the `eq` option can be used. - -```rust -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -#[pyclass(eq)] -#[derive(PartialEq)] -struct Number(i32); -``` - -To implement `__lt__`, `__le__`, `__gt__`, & `__ge__` using the Rust `PartialOrd` trait implementation, the `ord` option can be used. *Note: Requires `eq`.* - -```rust -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -#[pyclass(eq, ord)] -#[derive(PartialEq, PartialOrd)] -struct Number(i32); -``` - -### Truthyness - -We'll consider `Number` to be `True` if it is nonzero: - -```rust -# use pyo3::prelude::*; -# -# #[allow(dead_code)] -# #[pyclass] -# struct Number(i32); -# -#[pymethods] -impl Number { - fn __bool__(&self) -> bool { - self.0 != 0 - } -} -``` - -### Final code - -```rust -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; - -use pyo3::prelude::*; -use pyo3::class::basic::CompareOp; -use pyo3::types::PyString; - -#[pyclass] -struct Number(i32); - -#[pymethods] -impl Number { - #[new] - fn new(value: i32) -> Self { - Self(value) - } - - fn __repr__(slf: &Bound<'_, Self>) -> PyResult { - let class_name: Bound<'_, PyString> = slf.get_type().qualname()?; - Ok(format!("{}({})", class_name, slf.borrow().0)) - } - - fn __str__(&self) -> String { - self.0.to_string() - } - - fn __hash__(&self) -> u64 { - let mut hasher = DefaultHasher::new(); - self.0.hash(&mut hasher); - hasher.finish() - } - - fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult { - match op { - CompareOp::Lt => Ok(self.0 < other.0), - CompareOp::Le => Ok(self.0 <= other.0), - CompareOp::Eq => Ok(self.0 == other.0), - CompareOp::Ne => Ok(self.0 != other.0), - CompareOp::Gt => Ok(self.0 > other.0), - CompareOp::Ge => Ok(self.0 >= other.0), - } - } - - fn __bool__(&self) -> bool { - self.0 != 0 - } -} - -#[pymodule] -fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} -``` - -[`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html -[`Hasher`]: https://doc.rust-lang.org/std/hash/trait.Hasher.html -[`DefaultHasher`]: https://doc.rust-lang.org/std/collections/hash_map/struct.DefaultHasher.html -[SipHash]: https://en.wikipedia.org/wiki/SipHash -[`PartialEq`]: https://doc.rust-lang.org/stable/std/cmp/trait.PartialEq.html diff --git a/vendor/pyo3/guide/src/class/protocols.md b/vendor/pyo3/guide/src/class/protocols.md deleted file mode 100644 index 8a361a1442..0000000000 --- a/vendor/pyo3/guide/src/class/protocols.md +++ /dev/null @@ -1,466 +0,0 @@ -# Class customizations - -Python's object model defines several protocols for different object behavior, such as the sequence, mapping, and number protocols. Python classes support these protocols by implementing "magic" methods, such as `__str__` or `__repr__`. Because of the double-underscores surrounding their name, these are also known as "dunder" methods. - -PyO3 makes it possible for every magic method to be implemented in `#[pymethods]` just as they would be done in a regular Python class, with a few notable differences: -- `__new__` and `__init__` are replaced by the [`#[new]` attribute](../class.md#constructor). -- `__del__` is not yet supported, but may be in the future. -- `__buffer__` and `__release_buffer__` are currently not supported and instead PyO3 supports [`__getbuffer__` and `__releasebuffer__`](#buffer-objects) methods (these predate [PEP 688](https://peps.python.org/pep-0688/#python-level-buffer-protocol)), again this may change in the future. -- PyO3 adds [`__traverse__` and `__clear__`](#garbage-collector-integration) methods for controlling garbage collection. -- The Python C-API which PyO3 is implemented upon requires many magic methods to have a specific function signature in C and be placed into special "slots" on the class type object. This limits the allowed argument and return types for these methods. They are listed in detail in the section below. - -If a magic method is not on the list above (for example `__init_subclass__`), then it should just work in PyO3. If this is not the case, please file a bug report. - -## Magic Methods handled by PyO3 - -If a function name in `#[pymethods]` is a magic method which is known to need special handling, it will be automatically placed into the correct slot in the Python type object. The function name is taken from the usual rules for naming `#[pymethods]`: the `#[pyo3(name = "...")]` attribute is used if present, otherwise the Rust function name is used. - -The magic methods handled by PyO3 are very similar to the standard Python ones on [this page](https://docs.python.org/3/reference/datamodel.html#special-method-names) - in particular they are the subset which have slots as [defined here](https://docs.python.org/3/c-api/typeobj.html). - -When PyO3 handles a magic method, a couple of changes apply compared to other `#[pymethods]`: - - The Rust function signature is restricted to match the magic method. - - The `#[pyo3(signature = (...)]` and `#[pyo3(text_signature = "...")]` attributes are not allowed. - -The following sections list all magic methods for which PyO3 implements the necessary special handling. The -given signatures should be interpreted as follows: - - All methods take a receiver as first argument, shown as ``. It can be - `&self`, `&mut self` or a `Bound` reference like `self_: PyRef<'_, Self>` and - `self_: PyRefMut<'_, Self>`, as described [here](../class.md#inheritance). - - An optional `Python<'py>` argument is always allowed as the first argument. - - Return values can be optionally wrapped in `PyResult`. - - `object` means that any type is allowed that can be extracted from a Python - object (if argument) or converted to a Python object (if return value). - - Other types must match what's given, e.g. `pyo3::basic::CompareOp` for - `__richcmp__`'s second argument. - - For the comparison and arithmetic methods, extraction errors are not - propagated as exceptions, but lead to a return of `NotImplemented`. - - For some magic methods, the return values are not restricted by PyO3, but - checked by the Python interpreter. For example, `__str__` needs to return a - string object. This is indicated by `object (Python type)`. - -### Basic object customization - - - `__str__() -> object (str)` - - `__repr__() -> object (str)` - - - `__hash__() -> isize` - - Objects that compare equal must have the same hash value. Any type up to 64 bits may be returned instead of `isize`, PyO3 will convert to an isize automatically (wrapping unsigned types like `u64` and `usize`). -
- Disabling Python's default hash - By default, all `#[pyclass]` types have a default hash implementation from Python. Types which should not be hashable can override this by setting `__hash__` to `None`. This is the same mechanism as for a pure-Python class. This is done like so: - - ```rust - # use pyo3::prelude::*; - # - #[pyclass] - struct NotHashable {} - - #[pymethods] - impl NotHashable { - #[classattr] - const __hash__: Option = None; - } - ``` -
- - - `__lt__(, object) -> object` - - `__le__(, object) -> object` - - `__eq__(, object) -> object` - - `__ne__(, object) -> object` - - `__gt__(, object) -> object` - - `__ge__(, object) -> object` - - The implementations of Python's "rich comparison" operators `<`, `<=`, `==`, `!=`, `>` and `>=` respectively. - - _Note that implementing any of these methods will cause Python not to generate a default `__hash__` implementation, so consider also implementing `__hash__`._ -
- Return type - The return type will normally be `bool` or `PyResult`, however any Python object can be returned. -
- - - `__richcmp__(, object, pyo3::basic::CompareOp) -> object` - - Implements Python comparison operations (`==`, `!=`, `<`, `<=`, `>`, and `>=`) in a single method. - The `CompareOp` argument indicates the comparison operation being performed. You can use - [`CompareOp::matches`] to adapt a Rust `std::cmp::Ordering` result to the requested comparison. - - _This method cannot be implemented in combination with any of `__lt__`, `__le__`, `__eq__`, `__ne__`, `__gt__`, or `__ge__`._ - - _Note that implementing `__richcmp__` will cause Python not to generate a default `__hash__` implementation, so consider implementing `__hash__` when implementing `__richcmp__`._ -
- Return type - The return type will normally be `PyResult`, but any Python object can be returned. - - If you want to leave some operations unimplemented, you can return `py.NotImplemented()` - for some of the operations: - - ```rust - use pyo3::class::basic::CompareOp; - use pyo3::types::PyNotImplemented; - - # use pyo3::prelude::*; - # use pyo3::BoundObject; - # - # #[pyclass] - # struct Number(i32); - # - #[pymethods] - impl Number { - fn __richcmp__<'py>(&self, other: &Self, op: CompareOp, py: Python<'py>) -> PyResult> { - match op { - CompareOp::Eq => Ok((self.0 == other.0).into_pyobject(py)?.into_any()), - CompareOp::Ne => Ok((self.0 != other.0).into_pyobject(py)?.into_any()), - _ => Ok(PyNotImplemented::get(py).into_any()), - } - } - } - ``` - - If the second argument `object` is not of the type specified in the - signature, the generated code will automatically `return NotImplemented`. -
- - - `__getattr__(, object) -> object` - - `__getattribute__(, object) -> object` -
- Differences between `__getattr__` and `__getattribute__` - As in Python, `__getattr__` is only called if the attribute is not found - by normal attribute lookup. `__getattribute__`, on the other hand, is - called for *every* attribute access. If it wants to access existing - attributes on `self`, it needs to be very careful not to introduce - infinite recursion, and use `baseclass.__getattribute__()`. -
- - - `__setattr__(, value: object) -> ()` - - `__delattr__(, object) -> ()` - - Overrides attribute access. - - - `__bool__() -> bool` - - Determines the "truthyness" of an object. - - - `__call__(, ...) -> object` - here, any argument list can be defined - as for normal `pymethods` - -### Iterable objects - -Iterators can be defined using these methods: - - - `__iter__() -> object` - - `__next__() -> Option or IterNextOutput` ([see details](#returning-a-value-from-iteration)) - -Returning `None` from `__next__` indicates that that there are no further items. - -Example: - -```rust -use pyo3::prelude::*; - -use std::sync::Mutex; - -#[pyclass] -struct MyIterator { - iter: Mutex + Send>>, -} - -#[pymethods] -impl MyIterator { - fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - fn __next__(slf: PyRefMut<'_, Self>) -> Option { - slf.iter.lock().unwrap().next() - } -} -``` - -In many cases you'll have a distinction between the type being iterated over -(i.e. the *iterable*) and the iterator it provides. In this case, the iterable -only needs to implement `__iter__()` while the iterator must implement both -`__iter__()` and `__next__()`. For example: - -```rust -# use pyo3::prelude::*; - -#[pyclass] -struct Iter { - inner: std::vec::IntoIter, -} - -#[pymethods] -impl Iter { - fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __next__(mut slf: PyRefMut<'_, Self>) -> Option { - slf.inner.next() - } -} - -#[pyclass] -struct Container { - iter: Vec, -} - -#[pymethods] -impl Container { - fn __iter__(slf: PyRef<'_, Self>) -> PyResult> { - let iter = Iter { - inner: slf.iter.clone().into_iter(), - }; - Py::new(slf.py(), iter) - } -} - -# Python::with_gil(|py| { -# let container = Container { iter: vec![1, 2, 3, 4] }; -# let inst = pyo3::Py::new(py, container).unwrap(); -# pyo3::py_run!(py, inst, "assert list(inst) == [1, 2, 3, 4]"); -# pyo3::py_run!(py, inst, "assert list(iter(iter(inst))) == [1, 2, 3, 4]"); -# }); -``` - -For more details on Python's iteration protocols, check out [the "Iterator Types" section of the library -documentation](https://docs.python.org/library/stdtypes.html#iterator-types). - -#### Returning a value from iteration - -This guide has so far shown how to use `Option` to implement yielding values -during iteration. In Python a generator can also return a value. This is done by -raising a `StopIteration` exception. To express this in Rust, return `PyResult::Err` -with a `PyStopIteration` as the error. - -### Awaitable objects - - - `__await__() -> object` - - `__aiter__() -> object` - - `__anext__() -> Option` - -### Mapping & Sequence types - -The magic methods in this section can be used to implement Python container types. They are two main categories of container in Python: "mappings" such as `dict`, with arbitrary keys, and "sequences" such as `list` and `tuple`, with integer keys. - -The Python C-API which PyO3 is built upon has separate "slots" for sequences and mappings. When writing a `class` in pure Python, there is no such distinction in the implementation - a `__getitem__` implementation will fill the slots for both the mapping and sequence forms, for example. - -By default PyO3 reproduces the Python behaviour of filling both mapping and sequence slots. This makes sense for the "simple" case which matches Python, and also for sequences, where the mapping slot is used anyway to implement slice indexing. - -Mapping types usually will not want the sequence slots filled. Having them filled will lead to outcomes which may be unwanted, such as: -- The mapping type will successfully cast to [`PySequence`]. This may lead to consumers of the type handling it incorrectly. -- Python provides a default implementation of `__iter__` for sequences, which calls `__getitem__` with consecutive positive integers starting from 0 until an `IndexError` is returned. Unless the mapping only contains consecutive positive integer keys, this `__iter__` implementation will likely not be the intended behavior. - -Use the `#[pyclass(mapping)]` annotation to instruct PyO3 to only fill the mapping slots, leaving the sequence ones empty. This will apply to `__getitem__`, `__setitem__`, and `__delitem__`. - -Use the `#[pyclass(sequence)]` annotation to instruct PyO3 to fill the `sq_length` slot instead of the `mp_length` slot for `__len__`. This will help libraries such as `numpy` recognise the class as a sequence, however will also cause CPython to automatically add the sequence length to any negative indices before passing them to `__getitem__`. (`__getitem__`, `__setitem__` and `__delitem__` mapping slots are still used for sequences, for slice operations.) - - - `__len__() -> usize` - - Implements the built-in function `len()`. - - - `__contains__(, object) -> bool` - - Implements membership test operators. - Should return true if `item` is in `self`, false otherwise. - For objects that don’t define `__contains__()`, the membership test simply - traverses the sequence until it finds a match. - -
- Disabling Python's default contains - - By default, all `#[pyclass]` types with an `__iter__` method support a - default implementation of the `in` operator. Types which do not want this - can override this by setting `__contains__` to `None`. This is the same - mechanism as for a pure-Python class. This is done like so: - - ```rust - # use pyo3::prelude::*; - # - #[pyclass] - struct NoContains {} - - #[pymethods] - impl NoContains { - #[classattr] - const __contains__: Option = None; - } - ``` -
- - - `__getitem__(, object) -> object` - - Implements retrieval of the `self[a]` element. - - *Note:* Negative integer indexes are not handled specially by PyO3. - However, for classes with `#[pyclass(sequence)]`, when a negative index is - accessed via `PySequence::get_item`, the underlying C API already adjusts - the index to be positive. - - - `__setitem__(, object, object) -> ()` - - Implements assignment to the `self[a]` element. - Should only be implemented if elements can be replaced. - - Same behavior regarding negative indices as for `__getitem__`. - - - `__delitem__(, object) -> ()` - - Implements deletion of the `self[a]` element. - Should only be implemented if elements can be deleted. - - Same behavior regarding negative indices as for `__getitem__`. - - * `fn __concat__(&self, other: impl FromPyObject) -> PyResult` - - Concatenates two sequences. - Used by the `+` operator, after trying the numeric addition via - the `__add__` and `__radd__` methods. - - * `fn __repeat__(&self, count: isize) -> PyResult` - - Repeats the sequence `count` times. - Used by the `*` operator, after trying the numeric multiplication via - the `__mul__` and `__rmul__` methods. - - * `fn __inplace_concat__(&self, other: impl FromPyObject) -> PyResult` - - Concatenates two sequences. - Used by the `+=` operator, after trying the numeric addition via - the `__iadd__` method. - - * `fn __inplace_repeat__(&self, count: isize) -> PyResult` - - Concatenates two sequences. - Used by the `*=` operator, after trying the numeric multiplication via - the `__imul__` method. - -### Descriptors - - - `__get__(, object, object) -> object` - - `__set__(, object, object) -> ()` - - `__delete__(, object) -> ()` - -### Numeric types - -Binary arithmetic operations (`+`, `-`, `*`, `@`, `/`, `//`, `%`, `divmod()`, -`pow()` and `**`, `<<`, `>>`, `&`, `^`, and `|`) and their reflected versions: - -(If the `object` is not of the type specified in the signature, the generated code -will automatically `return NotImplemented`.) - - - `__add__(, object) -> object` - - `__radd__(, object) -> object` - - `__sub__(, object) -> object` - - `__rsub__(, object) -> object` - - `__mul__(, object) -> object` - - `__rmul__(, object) -> object` - - `__matmul__(, object) -> object` - - `__rmatmul__(, object) -> object` - - `__floordiv__(, object) -> object` - - `__rfloordiv__(, object) -> object` - - `__truediv__(, object) -> object` - - `__rtruediv__(, object) -> object` - - `__divmod__(, object) -> object` - - `__rdivmod__(, object) -> object` - - `__mod__(, object) -> object` - - `__rmod__(, object) -> object` - - `__lshift__(, object) -> object` - - `__rlshift__(, object) -> object` - - `__rshift__(, object) -> object` - - `__rrshift__(, object) -> object` - - `__and__(, object) -> object` - - `__rand__(, object) -> object` - - `__xor__(, object) -> object` - - `__rxor__(, object) -> object` - - `__or__(, object) -> object` - - `__ror__(, object) -> object` - - `__pow__(, object, object) -> object` - - `__rpow__(, object, object) -> object` - -In-place assignment operations (`+=`, `-=`, `*=`, `@=`, `/=`, `//=`, `%=`, -`**=`, `<<=`, `>>=`, `&=`, `^=`, `|=`): - - - `__iadd__(, object) -> ()` - - `__isub__(, object) -> ()` - - `__imul__(, object) -> ()` - - `__imatmul__(, object) -> ()` - - `__itruediv__(, object) -> ()` - - `__ifloordiv__(, object) -> ()` - - `__imod__(, object) -> ()` - - `__ipow__(, object, object) -> ()` - - `__ilshift__(, object) -> ()` - - `__irshift__(, object) -> ()` - - `__iand__(, object) -> ()` - - `__ixor__(, object) -> ()` - - `__ior__(, object) -> ()` - -Unary operations (`-`, `+`, `abs()` and `~`): - - - `__pos__() -> object` - - `__neg__() -> object` - - `__abs__() -> object` - - `__invert__() -> object` - -Coercions: - - - `__index__() -> object (int)` - - `__int__() -> object (int)` - - `__float__() -> object (float)` - -### Buffer objects - - - `__getbuffer__(, *mut ffi::Py_buffer, flags) -> ()` - - `__releasebuffer__(, *mut ffi::Py_buffer) -> ()` - Errors returned from `__releasebuffer__` will be sent to `sys.unraiseablehook`. It is strongly advised to never return an error from `__releasebuffer__`, and if it really is necessary, to make best effort to perform any required freeing operations before returning. `__releasebuffer__` will not be called a second time; anything not freed will be leaked. - -### Garbage Collector Integration - -If your type owns references to other Python objects, you will need to integrate -with Python's garbage collector so that the GC is aware of those references. To -do this, implement the two methods `__traverse__` and `__clear__`. These -correspond to the slots `tp_traverse` and `tp_clear` in the Python C API. -`__traverse__` must call `visit.call()` for each reference to another Python -object. `__clear__` must clear out any mutable references to other Python -objects (thus breaking reference cycles). Immutable references do not have to be -cleared, as every cycle must contain at least one mutable reference. - - - `__traverse__(, pyo3::class::gc::PyVisit<'_>) -> Result<(), pyo3::class::gc::PyTraverseError>` - - `__clear__() -> ()` - -Example: - -```rust -use pyo3::prelude::*; -use pyo3::PyTraverseError; -use pyo3::gc::PyVisit; - -#[pyclass] -struct ClassWithGCSupport { - obj: Option, -} - -#[pymethods] -impl ClassWithGCSupport { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - if let Some(obj) = &self.obj { - visit.call(obj)? - } - Ok(()) - } - - fn __clear__(&mut self) { - // Clear reference, this decrements ref counter. - self.obj = None; - } -} -``` - -Usually, an implementation of `__traverse__` should do nothing but calls to `visit.call`. -Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, -i.e. `Python::with_gil` will panic. - -> Note: these methods are part of the C API, PyPy does not necessarily honor them. If you are building for PyPy you should measure memory consumption to make sure you do not have runaway memory growth. See [this issue on the PyPy bug tracker](https://github.com/pypy/pypy/issues/3848). - -[`PySequence`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PySequence.html -[`CompareOp::matches`]: {{#PYO3_DOCS_URL}}/pyo3/pyclass/enum.CompareOp.html#method.matches diff --git a/vendor/pyo3/guide/src/class/thread-safety.md b/vendor/pyo3/guide/src/class/thread-safety.md deleted file mode 100644 index 841ee6ae2d..0000000000 --- a/vendor/pyo3/guide/src/class/thread-safety.md +++ /dev/null @@ -1,108 +0,0 @@ -# `#[pyclass]` thread safety - -Python objects are freely shared between threads by the Python interpreter. This means that: -- there is no control which thread might eventually drop the `#[pyclass]` object, meaning `Send` is required. -- multiple threads can potentially be reading the `#[pyclass]` data simultaneously, meaning `Sync` is required. - -This section of the guide discusses various data structures which can be used to make types satisfy these requirements. - -In special cases where it is known that your Python application is never going to use threads (this is rare!), these thread-safety requirements can be opted-out with [`#[pyclass(unsendable)]`](../class.md#customizing-the-class), at the cost of making concurrent access to the Rust data be runtime errors. This is only for very specific use cases; it is almost always better to make proper thread-safe types. - -## Making `#[pyclass]` types thread-safe - -The general challenge with thread-safety is to make sure that two threads cannot produce a data race, i.e. unsynchronized writes to the same data at the same time. A data race produces an unpredictable result and is forbidden by Rust. - -By default, `#[pyclass]` employs an ["interior mutability" pattern](../class.md#bound-and-interior-mutability) to allow for either multiple `&T` references or a single exclusive `&mut T` reference to access the data. This allows for simple `#[pyclass]` types to be thread-safe automatically, at the cost of runtime checking for concurrent access. Errors will be raised if the usage overlaps. - -For example, the below simple class is thread-safe: - -```rust -# use pyo3::prelude::*; - -#[pyclass] -struct MyClass { - x: i32, - y: i32, -} - -#[pymethods] -impl MyClass { - fn get_x(&self) -> i32 { - self.x - } - - fn set_y(&mut self, value: i32) { - self.y = value; - } -} -``` - -In the above example, if calls to `get_x` and `set_y` overlap (from two different threads) then at least one of those threads will experience a runtime error indicating that the data was "already borrowed". - -To avoid these errors, you can take control of the interior mutability yourself in one of the following ways. - -### Using atomic data structures - -To remove the possibility of having overlapping `&self` and `&mut self` references produce runtime errors, consider using `#[pyclass(frozen)]` and use [atomic data structures](https://doc.rust-lang.org/std/sync/atomic/) to control modifications directly. - -For example, a thread-safe version of the above `MyClass` using atomic integers would be as follows: - -```rust -# use pyo3::prelude::*; -use std::sync::atomic::{AtomicI32, Ordering}; - -#[pyclass(frozen)] -struct MyClass { - x: AtomicI32, - y: AtomicI32, -} - -#[pymethods] -impl MyClass { - fn get_x(&self) -> i32 { - self.x.load(Ordering::Relaxed) - } - - fn set_y(&self, value: i32) { - self.y.store(value, Ordering::Relaxed) - } -} -``` - -### Using locks - -An alternative to atomic data structures is to use [locks](https://doc.rust-lang.org/std/sync/struct.Mutex.html) to make threads wait for access to shared data. - -For example, a thread-safe version of the above `MyClass` using locks would be as follows: - -```rust -# use pyo3::prelude::*; -use std::sync::Mutex; - -struct MyClassInner { - x: i32, - y: i32, -} - -#[pyclass(frozen)] -struct MyClass { - inner: Mutex -} - -#[pymethods] -impl MyClass { - fn get_x(&self) -> i32 { - self.inner.lock().expect("lock not poisoned").x - } - - fn set_y(&self, value: i32) { - self.inner.lock().expect("lock not poisoned").y = value; - } -} -``` - -### Wrapping unsynchronized data - -In some cases, the data structures stored within a `#[pyclass]` may themselves not be thread-safe. Rust will therefore not implement `Send` and `Sync` on the `#[pyclass]` type. - -To achieve thread-safety, a manual `Send` and `Sync` implementation is required which is `unsafe` and should only be done following careful review of the soundness of the implementation. Doing this for PyO3 types is no different than for any other Rust code, [the Rustonomicon](https://doc.rust-lang.org/nomicon/send-and-sync.html) has a great discussion on this. diff --git a/vendor/pyo3/guide/src/contributing.md b/vendor/pyo3/guide/src/contributing.md deleted file mode 100644 index aa08bca898..0000000000 --- a/vendor/pyo3/guide/src/contributing.md +++ /dev/null @@ -1 +0,0 @@ -{{#include ../../Contributing.md}} diff --git a/vendor/pyo3/guide/src/conversions.md b/vendor/pyo3/guide/src/conversions.md deleted file mode 100644 index 991c206104..0000000000 --- a/vendor/pyo3/guide/src/conversions.md +++ /dev/null @@ -1,3 +0,0 @@ -# Type conversions - -In this portion of the guide we'll talk about the mapping of Python types to Rust types offered by PyO3, as well as the traits available to perform conversions between them. diff --git a/vendor/pyo3/guide/src/conversions/tables.md b/vendor/pyo3/guide/src/conversions/tables.md deleted file mode 100644 index 531e48b8f7..0000000000 --- a/vendor/pyo3/guide/src/conversions/tables.md +++ /dev/null @@ -1,118 +0,0 @@ -## Mapping of Rust types to Python types - -When writing functions callable from Python (such as a `#[pyfunction]` or in a `#[pymethods]` block), the trait `FromPyObject` is required for function arguments, and `IntoPy` is required for function return values. - -Consult the tables in the following section to find the Rust types provided by PyO3 which implement these traits. - -### Argument Types - -When accepting a function argument, it is possible to either use Rust library types or PyO3's Python-native types. (See the next section for discussion on when to use each.) - -The table below contains the Python type and the corresponding function argument types that will accept them: - -| Python | Rust | Rust (Python-native) | -| ------------- |:-------------------------------:|:--------------------:| -| `object` | - | `PyAny` | -| `str` | `String`, `Cow`, `&str`, `char`, `OsString`, `PathBuf`, `Path` | `PyString` | -| `bytes` | `Vec`, `&[u8]`, `Cow<[u8]>` | `PyBytes` | -| `bool` | `bool` | `PyBool` | -| `int` | `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`, `num_bigint::BigInt`[^1], `num_bigint::BigUint`[^1] | `PyInt` | -| `float` | `f32`, `f64` | `PyFloat` | -| `complex` | `num_complex::Complex`[^2] | `PyComplex` | -| `fractions.Fraction`| `num_rational::Ratio`[^8] | - | -| `list[T]` | `Vec` | `PyList` | -| `dict[K, V]` | `HashMap`, `BTreeMap`, `hashbrown::HashMap`[^3], `indexmap::IndexMap`[^4] | `PyDict` | -| `tuple[T, U]` | `(T, U)`, `Vec` | `PyTuple` | -| `set[T]` | `HashSet`, `BTreeSet`, `hashbrown::HashSet`[^3] | `PySet` | -| `frozenset[T]` | `HashSet`, `BTreeSet`, `hashbrown::HashSet`[^3] | `PyFrozenSet` | -| `bytearray` | `Vec`, `Cow<[u8]>` | `PyByteArray` | -| `slice` | - | `PySlice` | -| `type` | - | `PyType` | -| `module` | - | `PyModule` | -| `collections.abc.Buffer` | - | `PyBuffer` | -| `datetime.datetime` | `SystemTime`, `chrono::DateTime`[^5], `chrono::NaiveDateTime`[^5] | `PyDateTime` | -| `datetime.date` | `chrono::NaiveDate`[^5] | `PyDate` | -| `datetime.time` | `chrono::NaiveTime`[^5] | `PyTime` | -| `datetime.tzinfo` | `chrono::FixedOffset`[^5], `chrono::Utc`[^5], `chrono_tz::TimeZone`[^6] | `PyTzInfo` | -| `datetime.timedelta` | `Duration`, `chrono::Duration`[^5] | `PyDelta` | -| `decimal.Decimal` | `rust_decimal::Decimal`[^7] | - | -| `ipaddress.IPv4Address` | `std::net::IpAddr`, `std::net::IpV4Addr` | - | -| `ipaddress.IPv6Address` | `std::net::IpAddr`, `std::net::IpV6Addr` | - | -| `os.PathLike ` | `PathBuf`, `Path` | `PyString` | -| `pathlib.Path` | `PathBuf`, `Path` | `PyString` | -| `typing.Optional[T]` | `Option` | - | -| `typing.Sequence[T]` | `Vec` | `PySequence` | -| `typing.Mapping[K, V]` | `HashMap`, `BTreeMap`, `hashbrown::HashMap`[^3], `indexmap::IndexMap`[^4] | `&PyMapping` | -| `typing.Iterator[Any]` | - | `PyIterator` | -| `typing.Union[...]` | See [`#[derive(FromPyObject)]`](traits.md#deriving-frompyobject-for-enums) | - | - -It is also worth remembering the following special types: - -| What | Description | -| ---------------- | ------------------------------------- | -| `Python<'py>` | A GIL token, used to pass to PyO3 constructors to prove ownership of the GIL. | -| `Bound<'py, T>` | A Python object connected to the GIL lifetime. This provides access to most of PyO3's APIs. | -| `Py` | A Python object isolated from the GIL lifetime. This can be sent to other threads. | -| `PyObject` | An alias for `Py` | -| `PyRef` | A `#[pyclass]` borrowed immutably. | -| `PyRefMut` | A `#[pyclass]` borrowed mutably. | - -For more detail on accepting `#[pyclass]` values as function arguments, see [the section of this guide on Python Classes](../class.md). - -#### Using Rust library types vs Python-native types - -Using Rust library types as function arguments will incur a conversion cost compared to using the Python-native types. Using the Python-native types is almost zero-cost (they just require a type check similar to the Python builtin function `isinstance()`). - -However, once that conversion cost has been paid, the Rust standard library types offer a number of benefits: -- You can write functionality in native-speed Rust code (free of Python's runtime costs). -- You get better interoperability with the rest of the Rust ecosystem. -- You can use `Python::allow_threads` to release the Python GIL and let other Python threads make progress while your Rust code is executing. -- You also benefit from stricter type checking. For example you can specify `Vec`, which will only accept a Python `list` containing integers. The Python-native equivalent, `&PyList`, would accept a Python `list` containing Python objects of any type. - -For most PyO3 usage the conversion cost is worth paying to get these benefits. As always, if you're not sure it's worth it in your case, benchmark it! - -### Returning Rust values to Python - -When returning values from functions callable from Python, [PyO3's smart pointers](../types.md#pyo3s-smart-pointers) (`Py`, `Bound<'py, T>`, and `Borrowed<'a, 'py, T>`) can be used with zero cost. - -Because `Bound<'py, T>` and `Borrowed<'a, 'py, T>` have lifetime parameters, the Rust compiler may ask for lifetime annotations to be added to your function. See the [section of the guide dedicated to this](../types.md#function-argument-lifetimes). - -If your function is fallible, it should return `PyResult` or `Result` where `E` implements `From for PyErr`. This will raise a `Python` exception if the `Err` variant is returned. - -Finally, the following Rust types are also able to convert to Python as return values: - -| Rust type | Resulting Python Type | -| ------------- |:-------------------------------:| -| `String` | `str` | -| `&str` | `str` | -| `bool` | `bool` | -| Any integer type (`i32`, `u32`, `usize`, etc) | `int` | -| `f32`, `f64` | `float` | -| `Option` | `Optional[T]` | -| `(T, U)` | `Tuple[T, U]` | -| `Vec` | `List[T]` | -| `Cow<[u8]>` | `bytes` | -| `HashMap` | `Dict[K, V]` | -| `BTreeMap` | `Dict[K, V]` | -| `HashSet` | `Set[T]` | -| `BTreeSet` | `Set[T]` | -| `Py` | `T` | -| `Bound` | `T` | -| `PyRef` | `T` | -| `PyRefMut` | `T` | - -[^1]: Requires the `num-bigint` optional feature. - -[^2]: Requires the `num-complex` optional feature. - -[^3]: Requires the `hashbrown` optional feature. - -[^4]: Requires the `indexmap` optional feature. - -[^5]: Requires the `chrono` optional feature. - -[^6]: Requires the `chrono-tz` optional feature. - -[^7]: Requires the `rust_decimal` optional feature. - -[^8]: Requires the `num-rational` optional feature. diff --git a/vendor/pyo3/guide/src/conversions/traits.md b/vendor/pyo3/guide/src/conversions/traits.md deleted file mode 100644 index c4e8f14866..0000000000 --- a/vendor/pyo3/guide/src/conversions/traits.md +++ /dev/null @@ -1,725 +0,0 @@ -## Conversion traits - -PyO3 provides some handy traits to convert between Python types and Rust types. - -### `.extract()` and the `FromPyObject` trait - -The easiest way to convert a Python object to a Rust value is using -`.extract()`. It returns a `PyResult` with a type error if the conversion -fails, so usually you will use something like - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyList; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let list = PyList::new(py, b"foo")?; -let v: Vec = list.extract()?; -# assert_eq!(&v, &[102, 111, 111]); -# Ok(()) -# }) -# } -``` - -This method is available for many Python object types, and can produce a wide -variety of Rust types, which you can check out in the implementor list of -[`FromPyObject`]. - -[`FromPyObject`] is also implemented for your own Rust types wrapped as Python -objects (see [the chapter about classes](../class.md)). There, in order to both be -able to operate on mutable references *and* satisfy Rust's rules of non-aliasing -mutable references, you have to extract the PyO3 reference wrappers [`PyRef`] -and [`PyRefMut`]. They work like the reference wrappers of -`std::cell::RefCell` and ensure (at runtime) that Rust borrows are allowed. - -#### Deriving [`FromPyObject`] - -[`FromPyObject`] can be automatically derived for many kinds of structs and enums -if the member types themselves implement `FromPyObject`. This even includes members -with a generic type `T: FromPyObject`. Derivation for empty enums, enum variants and -structs is not supported. - -#### Deriving [`FromPyObject`] for structs - -The derivation generates code that will attempt to access the attribute `my_string` on -the Python object, i.e. `obj.getattr("my_string")`, and call `extract()` on the attribute. - -```rust -use pyo3::prelude::*; -use pyo3_ffi::c_str; - -#[derive(FromPyObject)] -struct RustyStruct { - my_string: String, -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let module = PyModule::from_code( -# py, -# c_str!("class Foo: -# def __init__(self): -# self.my_string = 'test'"), -# c_str!(""), -# c_str!(""), -# )?; -# -# let class = module.getattr("Foo")?; -# let instance = class.call0()?; -# let rustystruct: RustyStruct = instance.extract()?; -# assert_eq!(rustystruct.my_string, "test"); -# Ok(()) -# }) -# } -``` - -By setting the `#[pyo3(item)]` attribute on the field, PyO3 will attempt to extract the value by calling the `get_item` method on the Python object. - -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -struct RustyStruct { - #[pyo3(item)] - my_string: String, -} -# -# use pyo3::types::PyDict; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let dict = PyDict::new(py); -# dict.set_item("my_string", "test")?; -# -# let rustystruct: RustyStruct = dict.extract()?; -# assert_eq!(rustystruct.my_string, "test"); -# Ok(()) -# }) -# } -``` - -The argument passed to `getattr` and `get_item` can also be configured: - -```rust -use pyo3::prelude::*; -use pyo3_ffi::c_str; - -#[derive(FromPyObject)] -struct RustyStruct { - #[pyo3(item("key"))] - string_in_mapping: String, - #[pyo3(attribute("name"))] - string_attr: String, -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let module = PyModule::from_code( -# py, -# c_str!("class Foo(dict): -# def __init__(self): -# self.name = 'test' -# self['key'] = 'test2'"), -# c_str!(""), -# c_str!(""), -# )?; -# -# let class = module.getattr("Foo")?; -# let instance = class.call0()?; -# let rustystruct: RustyStruct = instance.extract()?; -# assert_eq!(rustystruct.string_attr, "test"); -# assert_eq!(rustystruct.string_in_mapping, "test2"); -# -# Ok(()) -# }) -# } -``` - -This tries to extract `string_attr` from the attribute `name` and `string_in_mapping` -from a mapping with the key `"key"`. The arguments for `attribute` are restricted to -non-empty string literals while `item` can take any valid literal that implements -`ToBorrowedObject`. - -You can use `#[pyo3(from_item_all)]` on a struct to extract every field with `get_item` method. -In this case, you can't use `#[pyo3(attribute)]` or barely use `#[pyo3(item)]` on any field. -However, using `#[pyo3(item("key"))]` to specify the key for a field is still allowed. - -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -struct RustyStruct { - foo: String, - bar: String, - #[pyo3(item("foobar"))] - baz: String, -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let py_dict = py.eval(pyo3::ffi::c_str!("{'foo': 'foo', 'bar': 'bar', 'foobar': 'foobar'}"), None, None)?; -# let rustystruct: RustyStruct = py_dict.extract()?; -# assert_eq!(rustystruct.foo, "foo"); -# assert_eq!(rustystruct.bar, "bar"); -# assert_eq!(rustystruct.baz, "foobar"); -# -# Ok(()) -# }) -# } -``` - -#### Deriving [`FromPyObject`] for tuple structs - -Tuple structs are also supported but do not allow customizing the extraction. The input is -always assumed to be a Python tuple with the same length as the Rust type, the `n`th field -is extracted from the `n`th item in the Python tuple. - -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -struct RustyTuple(String, String); - -# use pyo3::types::PyTuple; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let tuple = PyTuple::new(py, vec!["test", "test2"])?; -# -# let rustytuple: RustyTuple = tuple.extract()?; -# assert_eq!(rustytuple.0, "test"); -# assert_eq!(rustytuple.1, "test2"); -# -# Ok(()) -# }) -# } -``` - -Tuple structs with a single field are treated as wrapper types which are described in the -following section. To override this behaviour and ensure that the input is in fact a tuple, -specify the struct as -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -struct RustyTuple((String,)); - -# use pyo3::types::PyTuple; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let tuple = PyTuple::new(py, vec!["test"])?; -# -# let rustytuple: RustyTuple = tuple.extract()?; -# assert_eq!((rustytuple.0).0, "test"); -# -# Ok(()) -# }) -# } -``` - -#### Deriving [`FromPyObject`] for wrapper types - -The `pyo3(transparent)` attribute can be used on structs with exactly one field. This results -in extracting directly from the input object, i.e. `obj.extract()`, rather than trying to access -an item or attribute. This behaviour is enabled per default for newtype structs and tuple-variants -with a single field. - -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -struct RustyTransparentTupleStruct(String); - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct RustyTransparentStruct { - inner: String, -} - -# use pyo3::types::PyString; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# let s = PyString::new(py, "test"); -# -# let tup: RustyTransparentTupleStruct = s.extract()?; -# assert_eq!(tup.0, "test"); -# -# let stru: RustyTransparentStruct = s.extract()?; -# assert_eq!(stru.inner, "test"); -# -# Ok(()) -# }) -# } -``` - -#### Deriving [`FromPyObject`] for enums - -The `FromPyObject` derivation for enums generates code that tries to extract the variants in the -order of the fields. As soon as a variant can be extracted successfully, that variant is returned. -This makes it possible to extract Python union types like `str | int`. - -The same customizations and restrictions described for struct derivations apply to enum variants, -i.e. a tuple variant assumes that the input is a Python tuple, and a struct variant defaults to -extracting fields as attributes but can be configured in the same manner. The `transparent` -attribute can be applied to single-field-variants. - -```rust -use pyo3::prelude::*; -use pyo3_ffi::c_str; - -#[derive(FromPyObject)] -# #[derive(Debug)] -enum RustyEnum<'py> { - Int(usize), // input is a positive int - String(String), // input is a string - IntTuple(usize, usize), // input is a 2-tuple with positive ints - StringIntTuple(String, usize), // input is a 2-tuple with String and int - Coordinates3d { - // needs to be in front of 2d - x: usize, - y: usize, - z: usize, - }, - Coordinates2d { - // only gets checked if the input did not have `z` - #[pyo3(attribute("x"))] - a: usize, - #[pyo3(attribute("y"))] - b: usize, - }, - #[pyo3(transparent)] - CatchAll(Bound<'py, PyAny>), // This extraction never fails -} -# -# use pyo3::types::{PyBytes, PyString}; -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# { -# let thing = 42_u8.into_pyobject(py)?; -# let rust_thing: RustyEnum<'_> = thing.extract()?; -# -# assert_eq!( -# 42, -# match rust_thing { -# RustyEnum::Int(i) => i, -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# { -# let thing = PyString::new(py, "text"); -# let rust_thing: RustyEnum<'_> = thing.extract()?; -# -# assert_eq!( -# "text", -# match rust_thing { -# RustyEnum::String(i) => i, -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# { -# let thing = (32_u8, 73_u8).into_pyobject(py)?; -# let rust_thing: RustyEnum<'_> = thing.extract()?; -# -# assert_eq!( -# (32, 73), -# match rust_thing { -# RustyEnum::IntTuple(i, j) => (i, j), -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# { -# let thing = ("foo", 73_u8).into_pyobject(py)?; -# let rust_thing: RustyEnum<'_> = thing.extract()?; -# -# assert_eq!( -# (String::from("foo"), 73), -# match rust_thing { -# RustyEnum::StringIntTuple(i, j) => (i, j), -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# { -# let module = PyModule::from_code( -# py, -# c_str!("class Foo(dict): -# def __init__(self): -# self.x = 0 -# self.y = 1 -# self.z = 2"), -# c_str!(""), -# c_str!(""), -# )?; -# -# let class = module.getattr("Foo")?; -# let instance = class.call0()?; -# let rust_thing: RustyEnum<'_> = instance.extract()?; -# -# assert_eq!( -# (0, 1, 2), -# match rust_thing { -# RustyEnum::Coordinates3d { x, y, z } => (x, y, z), -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# -# { -# let module = PyModule::from_code( -# py, -# c_str!("class Foo(dict): -# def __init__(self): -# self.x = 3 -# self.y = 4"), -# c_str!(""), -# c_str!(""), -# )?; -# -# let class = module.getattr("Foo")?; -# let instance = class.call0()?; -# let rust_thing: RustyEnum<'_> = instance.extract()?; -# -# assert_eq!( -# (3, 4), -# match rust_thing { -# RustyEnum::Coordinates2d { a, b } => (a, b), -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# -# { -# let thing = PyBytes::new(py, b"text"); -# let rust_thing: RustyEnum<'_> = thing.extract()?; -# -# assert_eq!( -# b"text", -# match rust_thing { -# RustyEnum::CatchAll(ref i) => i.downcast::()?.as_bytes(), -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# Ok(()) -# }) -# } -``` - -If none of the enum variants match, a `PyTypeError` containing the names of the -tested variants is returned. The names reported in the error message can be customized -through the `#[pyo3(annotation = "name")]` attribute, e.g. to use conventional Python type -names: - -```rust -use pyo3::prelude::*; - -#[derive(FromPyObject)] -# #[derive(Debug)] -enum RustyEnum { - #[pyo3(transparent, annotation = "str")] - String(String), - #[pyo3(transparent, annotation = "int")] - Int(isize), -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| -> PyResult<()> { -# { -# let thing = 42_u8.into_pyobject(py)?; -# let rust_thing: RustyEnum = thing.extract()?; -# -# assert_eq!( -# 42, -# match rust_thing { -# RustyEnum::Int(i) => i, -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# -# { -# let thing = "foo".into_pyobject(py)?; -# let rust_thing: RustyEnum = thing.extract()?; -# -# assert_eq!( -# "foo", -# match rust_thing { -# RustyEnum::String(i) => i, -# other => unreachable!("Error extracting: {:?}", other), -# } -# ); -# } -# -# { -# let thing = b"foo".into_pyobject(py)?; -# let error = thing.extract::().unwrap_err(); -# assert!(error.is_instance_of::(py)); -# } -# -# Ok(()) -# }) -# } -``` - -If the input is neither a string nor an integer, the error message will be: -`"'' cannot be converted to 'str | int'"`. - -#### `#[derive(FromPyObject)]` Container Attributes -- `pyo3(transparent)` - - extract the field directly from the object as `obj.extract()` instead of `get_item()` or - `getattr()` - - Newtype structs and tuple-variants are treated as transparent per default. - - only supported for single-field structs and enum variants -- `pyo3(annotation = "name")` - - changes the name of the failed variant in the generated error message in case of failure. - - e.g. `pyo3("int")` reports the variant's type as `int`. - - only supported for enum variants - -#### `#[derive(FromPyObject)]` Field Attributes -- `pyo3(attribute)`, `pyo3(attribute("name"))` - - retrieve the field from an attribute, possibly with a custom name specified as an argument - - argument must be a string-literal. -- `pyo3(item)`, `pyo3(item("key"))` - - retrieve the field from a mapping, possibly with the custom key specified as an argument. - - can be any literal that implements `ToBorrowedObject` -- `pyo3(from_py_with = "...")` - - apply a custom function to convert the field from Python the desired Rust type. - - the argument must be the name of the function as a string. - - the function signature must be `fn(&Bound) -> PyResult` where `T` is the Rust type of the argument. - -### `IntoPyObject` -The ['IntoPyObject'] trait defines the to-python conversion for a Rust type. All types in PyO3 implement this trait, -as does a `#[pyclass]` which doesn't use `extends`. - -This trait defines a single method, `into_pyobject()`, which returns a [`Result`] with `Ok` and `Err` types depending on the input value. For convenience, there is a companion [`IntoPyObjectExt`] trait which adds methods such as `into_py_any()` which converts the `Ok` and `Err` types to commonly used types (in the case of `into_py_any()`, `Py` and `PyErr` respectively). - -Occasionally you may choose to implement this for custom types which are mapped to Python types -_without_ having a unique python type. - -#### derive macro - -`IntoPyObject` can be implemented using our derive macro. Both `struct`s and `enum`s are supported. - -`struct`s will turn into a `PyDict` using the field names as keys, tuple `struct`s will turn convert -into `PyTuple` with the fields in declaration order. -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use std::collections::HashMap; -# use std::hash::Hash; - -// structs convert into `PyDict` with field names as keys -#[derive(IntoPyObject)] -struct Struct { - count: usize, - obj: Py, -} - -// tuple structs convert into `PyTuple` -// lifetimes and generics are supported, the impl will be bounded by -// `K: IntoPyObject, V: IntoPyObject` -#[derive(IntoPyObject)] -struct Tuple<'a, K: Hash + Eq, V>(&'a str, HashMap); -``` - -For structs with a single field (newtype pattern) the `#[pyo3(transparent)]` option can be used to -forward the implementation to the inner type. - - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; - -// newtype tuple structs are implicitly `transparent` -#[derive(IntoPyObject)] -struct TransparentTuple(PyObject); - -#[derive(IntoPyObject)] -#[pyo3(transparent)] -struct TransparentStruct<'py> { - inner: Bound<'py, PyAny>, // `'py` lifetime will be used as the Python lifetime -} -``` - -For `enum`s each variant is converted according to the rules for `struct`s above. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use std::collections::HashMap; -# use std::hash::Hash; - -#[derive(IntoPyObject)] -enum Enum<'a, 'py, K: Hash + Eq, V> { // enums are supported and convert using the same - TransparentTuple(PyObject), // rules on the variants as the structs above - #[pyo3(transparent)] - TransparentStruct { inner: Bound<'py, PyAny> }, - Tuple(&'a str, HashMap), - Struct { count: usize, obj: Py } -} -``` - -Additionally `IntoPyObject` can be derived for a reference to a struct or enum using the -`IntoPyObjectRef` derive macro. All the same rules from above apply as well. - -#### manual implementation - -If the derive macro is not suitable for your use case, `IntoPyObject` can be implemented manually as -demonstrated below. - -```rust -# use pyo3::prelude::*; -# #[allow(dead_code)] -struct MyPyObjectWrapper(PyObject); - -impl<'py> IntoPyObject<'py> for MyPyObjectWrapper { - type Target = PyAny; // the Python type - type Output = Bound<'py, Self::Target>; // in most cases this will be `Bound` - type Error = std::convert::Infallible; // the conversion error type, has to be convertable to `PyErr` - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.0.into_bound(py)) - } -} - -// equivalent to former `ToPyObject` implementations -impl<'a, 'py> IntoPyObject<'py> for &'a MyPyObjectWrapper { - type Target = PyAny; - type Output = Borrowed<'a, 'py, Self::Target>; // `Borrowed` can be used to optimized reference counting - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.0.bind_borrowed(py)) - } -} -``` - -#### `BoundObject` for conversions that may be `Bound` or `Borrowed` - -`IntoPyObject::into_py_object` returns either `Bound` or `Borrowed` depending on the implementation for a concrete type. For example, the `IntoPyObject` implementation for `u32` produces a `Bound<'py, PyInt>` and the `bool` implementation produces a `Borrowed<'py, 'py, PyBool>`: - -```rust -use pyo3::prelude::*; -use pyo3::IntoPyObject; -use pyo3::types::{PyBool, PyInt}; - -let ints: Vec = vec![1, 2, 3, 4]; -let bools = vec![true, false, false, true]; - -Python::with_gil(|py| { - let ints_as_pyint: Vec> = ints - .iter() - .map(|x| Ok(x.into_pyobject(py)?)) - .collect::>() - .unwrap(); - - let bools_as_pybool: Vec> = bools - .iter() - .map(|x| Ok(x.into_pyobject(py)?)) - .collect::>() - .unwrap(); -}); -``` - -In this example if we wanted to combine `ints_as_pyints` and `bools_as_pybool` into a single `Vec>` to return from the `with_gil` closure, we would have to manually convert the concrete types for the smart pointers and the python types. - -Instead, we can write a function that generically converts vectors of either integers or bools into a vector of `Py` using the [`BoundObject`] trait: - -```rust -# use pyo3::prelude::*; -# use pyo3::BoundObject; -# use pyo3::IntoPyObject; - -# let bools = vec![true, false, false, true]; -# let ints = vec![1, 2, 3, 4]; - -fn convert_to_vec_of_pyobj<'py, T>(py: Python<'py>, the_vec: Vec) -> PyResult>> -where - T: IntoPyObject<'py> + Copy -{ - the_vec.iter() - .map(|x| { - Ok( - // Note: the below is equivalent to `x.into_py_any()` - // from the `IntoPyObjectExt` trait - x.into_pyobject(py) - .map_err(Into::into)? - .into_any() - .unbind() - ) - }) - .collect() -} - -let vec_of_pyobjs: Vec> = Python::with_gil(|py| { - let mut bools_as_pyany = convert_to_vec_of_pyobj(py, bools).unwrap(); - let mut ints_as_pyany = convert_to_vec_of_pyobj(py, ints).unwrap(); - let mut result: Vec> = vec![]; - result.append(&mut bools_as_pyany); - result.append(&mut ints_as_pyany); - result -}); -``` - -In the example above we used `BoundObject::into_any` and `BoundObject::unbind` to manipulate the python types and smart pointers into the result type we wanted to produce from the function. - -### `IntoPy` - -
- -⚠️ Warning: API update in progress 🛠️ - -PyO3 0.23 has introduced `IntoPyObject` as the new trait for to-python conversions. While `#[pymethods]` and `#[pyfunction]` contain a compatibility layer to allow `IntoPy` as a return type, all Python API have been migrated to use `IntoPyObject`. To migrate implement `IntoPyObject` for your type. -
- - -This trait defines the to-python conversion for a Rust type. It is usually implemented as -`IntoPy`, which is the trait needed for returning a value from `#[pyfunction]` and -`#[pymethods]`. - -All types in PyO3 implement this trait, as does a `#[pyclass]` which doesn't use `extends`. - -Occasionally you may choose to implement this for custom types which are mapped to Python types -_without_ having a unique python type. - -```rust -use pyo3::prelude::*; -# #[allow(dead_code)] -struct MyPyObjectWrapper(PyObject); - -#[allow(deprecated)] -impl IntoPy for MyPyObjectWrapper { - fn into_py(self, py: Python<'_>) -> PyObject { - self.0 - } -} -``` - -### The `ToPyObject` trait - -
- -⚠️ Warning: API update in progress 🛠️ - -PyO3 0.23 has introduced `IntoPyObject` as the new trait for to-python conversions. To migrate -implement `IntoPyObject` on a referece of your type (`impl<'py> IntoPyObject<'py> for &Type { ... }`). -
- -[`ToPyObject`] is a conversion trait that allows various objects to be -converted into [`PyObject`]. `IntoPy` serves the -same purpose, except that it consumes `self`. - -[`IntoPy`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.IntoPy.html -[`FromPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.FromPyObject.html -[`ToPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.ToPyObject.html -[`IntoPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.IntoPyObject.html -[`IntoPyObjectExt`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.IntoPyObjectExt.html -[`PyObject`]: {{#PYO3_DOCS_URL}}/pyo3/type.PyObject.html - -[`PyRef`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRef.html -[`PyRefMut`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRefMut.html -[`BoundObject`]: {{#PYO3_DOCS_URL}}/pyo3/instance/trait.BoundObject.html - -[`Result`]: https://doc.rust-lang.org/stable/std/result/enum.Result.html diff --git a/vendor/pyo3/guide/src/debugging.md b/vendor/pyo3/guide/src/debugging.md deleted file mode 100644 index 00c22631c3..0000000000 --- a/vendor/pyo3/guide/src/debugging.md +++ /dev/null @@ -1,47 +0,0 @@ -# Debugging - -## Macros - -PyO3's attributes (`#[pyclass]`, `#[pymodule]`, etc.) are [procedural macros](https://doc.rust-lang.org/reference/procedural-macros.html), which means that they rewrite the source of the annotated item. You can view the generated source with the following command, which also expands a few other things: - -```bash -cargo rustc --profile=check -- -Z unstable-options --pretty=expanded > expanded.rs; rustfmt expanded.rs -``` - -(You might need to install [rustfmt](https://github.com/rust-lang-nursery/rustfmt) if you don't already have it.) - -You can also debug classic `!`-macros by adding `-Z trace-macros`: - -```bash -cargo rustc --profile=check -- -Z unstable-options --pretty=expanded -Z trace-macros > expanded.rs; rustfmt expanded.rs -``` - -Note that those commands require using the nightly build of rust and may occasionally have bugs. See [cargo expand](https://github.com/dtolnay/cargo-expand) for a more elaborate and stable version of those commands. - -## Running with Valgrind - -Valgrind is a tool to detect memory management bugs such as memory leaks. - -You first need to install a debug build of Python, otherwise Valgrind won't produce usable results. In Ubuntu there's e.g. a `python3-dbg` package. - -Activate an environment with the debug interpreter and recompile. If you're on Linux, use `ldd` with the name of your binary and check that you're linking e.g. `libpython3.7d.so.1.0` instead of `libpython3.7.so.1.0`. - -[Download the suppressions file for CPython](https://raw.githubusercontent.com/python/cpython/master/Misc/valgrind-python.supp). - -Run Valgrind with `valgrind --suppressions=valgrind-python.supp ./my-command --with-options` - -## Getting a stacktrace - -The best start to investigate a crash such as an segmentation fault is a backtrace. You can set `RUST_BACKTRACE=1` as an environment variable to get the stack trace on a `panic!`. Alternatively you can use a debugger such as `gdb` to explore the issue. Rust provides a wrapper, `rust-gdb`, which has pretty-printers for inspecting Rust variables. Since PyO3 uses `cdylib` for Python shared objects, it does not receive the pretty-print debug hooks in `rust-gdb` ([rust-lang/rust#96365](https://github.com/rust-lang/rust/issues/96365)). The mentioned issue contains a workaround for enabling pretty-printers in this case. - - * Link against a debug build of python as described in the previous chapter - * Run `rust-gdb ` - * Set a breakpoint (`b`) on `rust_panic` if you are investigating a `panic!` - * Enter `r` to run - * After the crash occurred, enter `bt` or `bt full` to print the stacktrace - - Often it is helpful to run a small piece of Python code to exercise a section of Rust. - - ```console - rust-gdb --args python -c "import my_package; my_package.sum_to_string(1, 2)" - ``` diff --git a/vendor/pyo3/guide/src/ecosystem.md b/vendor/pyo3/guide/src/ecosystem.md deleted file mode 100644 index 6c0297e032..0000000000 --- a/vendor/pyo3/guide/src/ecosystem.md +++ /dev/null @@ -1,5 +0,0 @@ -# The PyO3 ecosystem - -This portion of the guide is dedicated to crates which are external to the main PyO3 project and provide additional functionality you might find useful. - -Because these projects evolve independently of the PyO3 repository the content of these articles may fall out of date over time; please file issues on the PyO3 GitHub to alert maintainers when this is the case. diff --git a/vendor/pyo3/guide/src/ecosystem/async-await.md b/vendor/pyo3/guide/src/ecosystem/async-await.md deleted file mode 100644 index 0128efbc8a..0000000000 --- a/vendor/pyo3/guide/src/ecosystem/async-await.md +++ /dev/null @@ -1,555 +0,0 @@ -# Using `async` and `await` - -*`async`/`await` support is currently being integrated in PyO3. See the [dedicated documentation](../async-await.md)* - -If you are working with a Python library that makes use of async functions or wish to provide -Python bindings for an async Rust library, [`pyo3-asyncio`](https://github.com/awestlake87/pyo3-asyncio) -likely has the tools you need. It provides conversions between async functions in both Python and -Rust and was designed with first-class support for popular Rust runtimes such as -[`tokio`](https://tokio.rs/) and [`async-std`](https://async.rs/). In addition, all async Python -code runs on the default `asyncio` event loop, so `pyo3-asyncio` should work just fine with existing -Python libraries. - -In the following sections, we'll give a general overview of `pyo3-asyncio` explaining how to call -async Python functions with PyO3, how to call async Rust functions from Python, and how to configure -your codebase to manage the runtimes of both. - -## Quickstart - -Here are some examples to get you started right away! A more detailed breakdown -of the concepts in these examples can be found in the following sections. - -### Rust Applications -Here we initialize the runtime, import Python's `asyncio` library and run the given future to completion using Python's default `EventLoop` and `async-std`. Inside the future, we convert `asyncio` sleep into a Rust future and await it. - - -```toml -# Cargo.toml dependencies -[dependencies] -pyo3 = { version = "0.14" } -pyo3-asyncio = { version = "0.14", features = ["attributes", "async-std-runtime"] } -async-std = "1.9" -``` - -```rust -//! main.rs - -use pyo3::prelude::*; - -#[pyo3_asyncio::async_std::main] -async fn main() -> PyResult<()> { - let fut = Python::with_gil(|py| { - let asyncio = py.import("asyncio")?; - // convert asyncio.sleep into a Rust Future - pyo3_asyncio::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) - })?; - - fut.await?; - - Ok(()) -} -``` - -The same application can be written to use `tokio` instead using the `#[pyo3_asyncio::tokio::main]` -attribute. - -```toml -# Cargo.toml dependencies -[dependencies] -pyo3 = { version = "0.14" } -pyo3-asyncio = { version = "0.14", features = ["attributes", "tokio-runtime"] } -tokio = "1.4" -``` - -```rust -//! main.rs - -use pyo3::prelude::*; - -#[pyo3_asyncio::tokio::main] -async fn main() -> PyResult<()> { - let fut = Python::with_gil(|py| { - let asyncio = py.import("asyncio")?; - // convert asyncio.sleep into a Rust Future - pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) - })?; - - fut.await?; - - Ok(()) -} -``` - -More details on the usage of this library can be found in the [API docs](https://awestlake87.github.io/pyo3-asyncio/master/doc) and the primer below. - -### PyO3 Native Rust Modules - -PyO3 Asyncio can also be used to write native modules with async functions. - -Add the `[lib]` section to `Cargo.toml` to make your library a `cdylib` that Python can import. -```toml -[lib] -name = "my_async_module" -crate-type = ["cdylib"] -``` - -Make your project depend on `pyo3` with the `extension-module` feature enabled and select your -`pyo3-asyncio` runtime: - -For `async-std`: -```toml -[dependencies] -pyo3 = { version = "0.14", features = ["extension-module"] } -pyo3-asyncio = { version = "0.14", features = ["async-std-runtime"] } -async-std = "1.9" -``` - -For `tokio`: -```toml -[dependencies] -pyo3 = { version = "0.14", features = ["extension-module"] } -pyo3-asyncio = { version = "0.14", features = ["tokio-runtime"] } -tokio = "1.4" -``` - -Export an async function that makes use of `async-std`: - -```rust -//! lib.rs - -use pyo3::{prelude::*, wrap_pyfunction}; - -#[pyfunction] -fn rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>> { - pyo3_asyncio::async_std::future_into_py(py, async { - async_std::task::sleep(std::time::Duration::from_secs(1)).await; - Ok(Python::with_gil(|py| py.None())) - }) -} - -#[pymodule] -fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(rust_sleep, m)?) -} -``` - -If you want to use `tokio` instead, here's what your module should look like: - -```rust -//! lib.rs - -use pyo3::{prelude::*, wrap_pyfunction}; - -#[pyfunction] -fn rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>>> { - pyo3_asyncio::tokio::future_into_py(py, async { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - Ok(Python::with_gil(|py| py.None())) - }) -} - -#[pymodule] -fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(rust_sleep, m)?) -} -``` - -You can build your module with maturin (see the [Using Rust in Python](https://pyo3.rs/main/#using-rust-from-python) section in the PyO3 guide for setup instructions). After that you should be able to run the Python REPL to try it out. - -```bash -maturin develop && python3 -🔗 Found pyo3 bindings -🐍 Found CPython 3.8 at python3 - Finished dev [unoptimized + debuginfo] target(s) in 0.04s -Python 3.8.5 (default, Jan 27 2021, 15:41:15) -[GCC 9.3.0] on linux -Type "help", "copyright", "credits" or "license" for more information. ->>> import asyncio ->>> ->>> from my_async_module import rust_sleep ->>> ->>> async def main(): ->>> await rust_sleep() ->>> ->>> # should sleep for 1s ->>> asyncio.run(main()) ->>> -``` - -## Awaiting an Async Python Function in Rust - -Let's take a look at a dead simple async Python function: - -```python -# Sleep for 1 second -async def py_sleep(): - await asyncio.sleep(1) -``` - -**Async functions in Python are simply functions that return a `coroutine` object**. For our purposes, -we really don't need to know much about these `coroutine` objects. The key factor here is that calling -an `async` function is _just like calling a regular function_, the only difference is that we have -to do something special with the object that it returns. - -Normally in Python, that something special is the `await` keyword, but in order to await this -coroutine in Rust, we first need to convert it into Rust's version of a `coroutine`: a `Future`. -That's where `pyo3-asyncio` comes in. -[`pyo3_asyncio::async_std::into_future`](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/async_std/fn.into_future.html) -performs this conversion for us. - -The following example uses `into_future` to call the `py_sleep` function shown above and then await the -coroutine object returned from the call: - -```rust -use pyo3::prelude::*; - -#[pyo3_asyncio::tokio::main] -async fn main() -> PyResult<()> { - let future = Python::with_gil(|py| -> PyResult<_> { - // import the module containing the py_sleep function - let example = py.import("example")?; - - // calling the py_sleep method like a normal function - // returns a coroutine - let coroutine = example.call_method0("py_sleep")?; - - // convert the coroutine into a Rust future using the - // tokio runtime - pyo3_asyncio::tokio::into_future(coroutine) - })?; - - // await the future - future.await?; - - Ok(()) -} -``` - -Alternatively, the below example shows how to write a `#[pyfunction]` which uses `into_future` to receive and await -a coroutine argument: - -```rust -#[pyfunction] -fn await_coro(coro: &Bound<'_, PyAny>>) -> PyResult<()> { - // convert the coroutine into a Rust future using the - // async_std runtime - let f = pyo3_asyncio::async_std::into_future(coro)?; - - pyo3_asyncio::async_std::run_until_complete(coro.py(), async move { - // await the future - f.await?; - Ok(()) - }) -} -``` - -This could be called from Python as: - -```python -import asyncio - -async def py_sleep(): - asyncio.sleep(1) - -await_coro(py_sleep()) -``` - -If you wanted to pass a callable function to the `#[pyfunction]` instead, (i.e. the last line becomes `await_coro(py_sleep))`, then the above example needs to be tweaked to first call the callable to get the coroutine: - -```rust -#[pyfunction] -fn await_coro(callable: &Bound<'_, PyAny>>) -> PyResult<()> { - // get the coroutine by calling the callable - let coro = callable.call0()?; - - // convert the coroutine into a Rust future using the - // async_std runtime - let f = pyo3_asyncio::async_std::into_future(coro)?; - - pyo3_asyncio::async_std::run_until_complete(coro.py(), async move { - // await the future - f.await?; - Ok(()) - }) -} -``` - -This can be particularly helpful where you need to repeatedly create and await a coroutine. Trying to await the same coroutine multiple times will raise an error: - -```python -RuntimeError: cannot reuse already awaited coroutine -``` - -> If you're interested in learning more about `coroutines` and `awaitables` in general, check out the -> [Python 3 `asyncio` docs](https://docs.python.org/3/library/asyncio-task.html) for more information. - -## Awaiting a Rust Future in Python - -Here we have the same async function as before written in Rust using the -[`async-std`](https://async.rs/) runtime: - -```rust -/// Sleep for 1 second -async fn rust_sleep() { - async_std::task::sleep(std::time::Duration::from_secs(1)).await; -} -``` - -Similar to Python, Rust's async functions also return a special object called a -`Future`: - -```rust -let future = rust_sleep(); -``` - -We can convert this `Future` object into Python to make it `awaitable`. This tells Python that you -can use the `await` keyword with it. In order to do this, we'll call -[`pyo3_asyncio::async_std::future_into_py`](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/async_std/fn.future_into_py.html): - -```rust -use pyo3::prelude::*; - -async fn rust_sleep() { - async_std::task::sleep(std::time::Duration::from_secs(1)).await; -} - -#[pyfunction] -fn call_rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>>> { - pyo3_asyncio::async_std::future_into_py(py, async move { - rust_sleep().await; - Ok(Python::with_gil(|py| py.None())) - }) -} -``` - -In Python, we can call this pyo3 function just like any other async function: - -```python -from example import call_rust_sleep - -async def rust_sleep(): - await call_rust_sleep() -``` - -## Managing Event Loops - -Python's event loop requires some special treatment, especially regarding the main thread. Some of -Python's `asyncio` features, like proper signal handling, require control over the main thread, which -doesn't always play well with Rust. - -Luckily, Rust's event loops are pretty flexible and don't _need_ control over the main thread, so in -`pyo3-asyncio`, we decided the best way to handle Rust/Python interop was to just surrender the main -thread to Python and run Rust's event loops in the background. Unfortunately, since most event loop -implementations _prefer_ control over the main thread, this can still make some things awkward. - -### PyO3 Asyncio Initialization - -Because Python needs to control the main thread, we can't use the convenient proc macros from Rust -runtimes to handle the `main` function or `#[test]` functions. Instead, the initialization for PyO3 has to be done from the `main` function and the main -thread must block on [`pyo3_asyncio::async_std::run_until_complete`](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/async_std/fn.run_until_complete.html). - -Because we have to block on one of those functions, we can't use [`#[async_std::main]`](https://docs.rs/async-std/latest/async_std/attr.main.html) or [`#[tokio::main]`](https://docs.rs/tokio/1.1.0/tokio/attr.main.html) -since it's not a good idea to make long blocking calls during an async function. - -> Internally, these `#[main]` proc macros are expanded to something like this: -> ```rust -> fn main() { -> // your async main fn -> async fn _main_impl() { /* ... */ } -> Runtime::new().block_on(_main_impl()); -> } -> ``` -> Making a long blocking call inside the `Future` that's being driven by `block_on` prevents that -> thread from doing anything else and can spell trouble for some runtimes (also this will actually -> deadlock a single-threaded runtime!). Many runtimes have some sort of `spawn_blocking` mechanism -> that can avoid this problem, but again that's not something we can use here since we need it to -> block on the _main_ thread. - -For this reason, `pyo3-asyncio` provides its own set of proc macros to provide you with this -initialization. These macros are intended to mirror the initialization of `async-std` and `tokio` -while also satisfying the Python runtime's needs. - -Here's a full example of PyO3 initialization with the `async-std` runtime: -```rust -use pyo3::prelude::*; - -#[pyo3_asyncio::async_std::main] -async fn main() -> PyResult<()> { - // PyO3 is initialized - Ready to go - - let fut = Python::with_gil(|py| -> PyResult<_> { - let asyncio = py.import("asyncio")?; - - // convert asyncio.sleep into a Rust Future - pyo3_asyncio::async_std::into_future( - asyncio.call_method1("sleep", (1.into_py(py),))? - ) - })?; - - fut.await?; - - Ok(()) -} -``` - -### A Note About `asyncio.run` - -In Python 3.7+, the recommended way to run a top-level coroutine with `asyncio` -is with `asyncio.run`. In `v0.13` we recommended against using this function due to initialization issues, but in `v0.14` it's perfectly valid to use this function... with a caveat. - -Since our Rust <--> Python conversions require a reference to the Python event loop, this poses a problem. Imagine we have a PyO3 Asyncio module that defines -a `rust_sleep` function like in previous examples. You might rightfully assume that you can call pass this directly into `asyncio.run` like this: - -```python -import asyncio - -from my_async_module import rust_sleep - -asyncio.run(rust_sleep()) -``` - -You might be surprised to find out that this throws an error: -```bash -Traceback (most recent call last): - File "example.py", line 5, in - asyncio.run(rust_sleep()) -RuntimeError: no running event loop -``` - -What's happening here is that we are calling `rust_sleep` _before_ the future is -actually running on the event loop created by `asyncio.run`. This is counter-intuitive, but expected behaviour, and unfortunately there doesn't seem to be a good way of solving this problem within PyO3 Asyncio itself. - -However, we can make this example work with a simple workaround: - -```python -import asyncio - -from my_async_module import rust_sleep - -# Calling main will just construct the coroutine that later calls rust_sleep. -# - This ensures that rust_sleep will be called when the event loop is running, -# not before. -async def main(): - await rust_sleep() - -# Run the main() coroutine at the top-level instead -asyncio.run(main()) -``` - -### Non-standard Python Event Loops - -Python allows you to use alternatives to the default `asyncio` event loop. One -popular alternative is `uvloop`. In `v0.13` using non-standard event loops was -a bit of an ordeal, but in `v0.14` it's trivial. - -#### Using `uvloop` in a PyO3 Asyncio Native Extensions - -```toml -# Cargo.toml - -[lib] -name = "my_async_module" -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { version = "0.14", features = ["extension-module"] } -pyo3-asyncio = { version = "0.14", features = ["tokio-runtime"] } -async-std = "1.9" -tokio = "1.4" -``` - -```rust -//! lib.rs - -use pyo3::{prelude::*, wrap_pyfunction}; - -#[pyfunction] -fn rust_sleep(py: Python<'_>) -> PyResult<&Bound<'_, PyAny>>> { - pyo3_asyncio::tokio::future_into_py(py, async { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - Ok(Python::with_gil(|py| py.None())) - }) -} - -#[pymodule] -fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; - - Ok(()) -} -``` - -```bash -$ maturin develop && python3 -🔗 Found pyo3 bindings -🐍 Found CPython 3.8 at python3 - Finished dev [unoptimized + debuginfo] target(s) in 0.04s -Python 3.8.8 (default, Apr 13 2021, 19:58:26) -[GCC 7.3.0] :: Anaconda, Inc. on linux -Type "help", "copyright", "credits" or "license" for more information. ->>> import asyncio ->>> import uvloop ->>> ->>> import my_async_module ->>> ->>> uvloop.install() ->>> ->>> async def main(): -... await my_async_module.rust_sleep() -... ->>> asyncio.run(main()) ->>> -``` - -#### Using `uvloop` in Rust Applications - -Using `uvloop` in Rust applications is a bit trickier, but it's still possible -with relatively few modifications. - -Unfortunately, we can't make use of the `#[pyo3_asyncio::::main]` attribute with non-standard event loops. This is because the `#[pyo3_asyncio::::main]` proc macro has to interact with the Python -event loop before we can install the `uvloop` policy. - -```toml -[dependencies] -async-std = "1.9" -pyo3 = "0.14" -pyo3-asyncio = { version = "0.14", features = ["async-std-runtime"] } -``` - -```rust -//! main.rs - -use pyo3::{prelude::*, types::PyType}; - -fn main() -> PyResult<()> { - pyo3::prepare_freethreaded_python(); - - Python::with_gil(|py| { - let uvloop = py.import("uvloop")?; - uvloop.call_method0("install")?; - - // store a reference for the assertion - let uvloop = PyObject::from(uvloop); - - pyo3_asyncio::async_std::run(py, async move { - // verify that we are on a uvloop.Loop - Python::with_gil(|py| -> PyResult<()> { - assert!(pyo3_asyncio::async_std::get_current_loop(py)?.is_instance( - uvloop - .as_ref(py) - .getattr("Loop")? - )?); - Ok(()) - })?; - - async_std::task::sleep(std::time::Duration::from_secs(1)).await; - - Ok(()) - }) - }) -} -``` - -## Additional Information -- Managing event loop references can be tricky with pyo3-asyncio. See [Event Loop References](https://docs.rs/pyo3-asyncio/#event-loop-references) in the API docs to get a better intuition for how event loop references are managed in this library. -- Testing pyo3-asyncio libraries and applications requires a custom test harness since Python requires control over the main thread. You can find a testing guide in the [API docs for the `testing` module](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/testing) diff --git a/vendor/pyo3/guide/src/ecosystem/logging.md b/vendor/pyo3/guide/src/ecosystem/logging.md deleted file mode 100644 index da95c4a7cd..0000000000 --- a/vendor/pyo3/guide/src/ecosystem/logging.md +++ /dev/null @@ -1,98 +0,0 @@ -# Logging - -It is desirable if both the Python and Rust parts of the application end up -logging using the same configuration into the same place. - -This section of the guide briefly discusses how to connect the two languages' -logging ecosystems together. The recommended way for Python extension modules is -to configure Rust's logger to send log messages to Python using the `pyo3-log` -crate. For users who want to do the opposite and send Python log messages to -Rust, see the note at the end of this guide. - -## Using `pyo3-log` to send Rust log messages to Python - -The [pyo3-log] crate allows sending the messages from the Rust side to Python's -[logging] system. This is mostly suitable for writing native extensions for -Python programs. - -Use [`pyo3_log::init`][init] to install the logger in its default configuration. -It's also possible to tweak its configuration (mostly to tune its performance). - -```rust -use log::info; -use pyo3::prelude::*; - -#[pyfunction] -fn log_something() { - // This will use the logger installed in `my_module` to send the `info` - // message to the Python logging facilities. - info!("Something!"); -} - -#[pymodule] -fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - // A good place to install the Rust -> Python logger. - pyo3_log::init(); - - m.add_function(wrap_pyfunction!(log_something, m)?)?; - Ok(()) -} -``` - -Then it is up to the Python side to actually output the messages somewhere. - -```python -import logging -import my_module - -FORMAT = '%(levelname)s %(name)s %(asctime)-15s %(filename)s:%(lineno)d %(message)s' -logging.basicConfig(format=FORMAT) -logging.getLogger().setLevel(logging.INFO) -my_module.log_something() -``` - -It is important to initialize the Python loggers first, before calling any Rust -functions that may log. This limitation can be worked around if it is not -possible to satisfy, read the documentation about [caching]. - -## The Python to Rust direction - -To have python logs be handled by Rust, one need only register a rust function to handle logs emitted from the core python logging module. - -This has been implemented within the [pyo3-pylogger] crate. - -```rust -use log::{info, warn}; -use pyo3::prelude::*; - -fn main() -> PyResult<()> { - // register the host handler with python logger, providing a logger target - // set the name here to something appropriate for your application - pyo3_pylogger::register("example_application_py_logger"); - - // initialize up a logger - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init(); - - // Log some messages from Rust. - info!("Just some normal information!"); - warn!("Something spooky happened!"); - - // Log some messages from Python - Python::with_gil(|py| { - py.run( - " -import logging -logging.error('Something bad happened') -", - None, - None, - ) - }) -} -``` - -[logging]: https://docs.python.org/3/library/logging.html -[pyo3-log]: https://crates.io/crates/pyo3-log -[init]: https://docs.rs/pyo3-log/*/pyo3_log/fn.init.html -[caching]: https://docs.rs/pyo3-log/*/pyo3_log/#performance-filtering-and-caching -[pyo3-pylogger]: https://crates.io/crates/pyo3-pylogger diff --git a/vendor/pyo3/guide/src/exception.md b/vendor/pyo3/guide/src/exception.md deleted file mode 100644 index fd427a34fb..0000000000 --- a/vendor/pyo3/guide/src/exception.md +++ /dev/null @@ -1,138 +0,0 @@ -# Python exceptions - -## Defining a new exception - -Use the [`create_exception!`] macro: - -```rust -use pyo3::create_exception; - -create_exception!(module, MyError, pyo3::exceptions::PyException); -``` - -* `module` is the name of the containing module. -* `MyError` is the name of the new exception type. - -For example: - -```rust -use pyo3::prelude::*; -use pyo3::create_exception; -use pyo3::types::IntoPyDict; -use pyo3::exceptions::PyException; - -create_exception!(mymodule, CustomError, PyException); - -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - let ctx = [("CustomError", py.get_type::())].into_py_dict(py)?; - pyo3::py_run!( - py, - *ctx, - "assert str(CustomError) == \"\"" - ); - pyo3::py_run!(py, *ctx, "assert CustomError('oops').args == ('oops',)"); -# Ok(()) -}) -# } -``` - -When using PyO3 to create an extension module, you can add the new exception to -the module like this, so that it is importable from Python: - -```rust -use pyo3::prelude::*; -use pyo3::exceptions::PyException; - -pyo3::create_exception!(mymodule, CustomError, PyException); - -#[pymodule] -fn mymodule(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - // ... other elements added to module ... - m.add("CustomError", py.get_type::())?; - - Ok(()) -} -``` - -## Raising an exception - -As described in the [function error handling](./function/error-handling.md) chapter, to raise an exception from a `#[pyfunction]` or `#[pymethods]`, return an `Err(PyErr)`. PyO3 will automatically raise this exception for you when returning the result to Python. - -You can also manually write and fetch errors in the Python interpreter's global state: - -```rust -use pyo3::{Python, PyErr}; -use pyo3::exceptions::PyTypeError; - -Python::with_gil(|py| { - PyTypeError::new_err("Error").restore(py); - assert!(PyErr::occurred(py)); - drop(PyErr::fetch(py)); -}); -``` - -## Checking exception types - -Python has an [`isinstance`](https://docs.python.org/3/library/functions.html#isinstance) method to check an object's type. -In PyO3 every object has the [`PyAny::is_instance`] and [`PyAny::is_instance_of`] methods which do the same thing. - -```rust -use pyo3::prelude::*; -use pyo3::types::{PyBool, PyList}; - -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - assert!(PyBool::new(py, true).is_instance_of::()); - let list = PyList::new(py, &[1, 2, 3, 4])?; - assert!(!list.is_instance_of::()); - assert!(list.is_instance_of::()); -# Ok(()) -}) -# } -``` - -To check the type of an exception, you can similarly do: - -```rust -# use pyo3::exceptions::PyTypeError; -# use pyo3::prelude::*; -# Python::with_gil(|py| { -# let err = PyTypeError::new_err(()); -err.is_instance_of::(py); -# }); -``` - -## Using exceptions defined in Python code - -It is possible to use an exception defined in Python code as a native Rust type. -The `import_exception!` macro allows importing a specific exception class and defines a Rust type -for that exception. - -```rust -#![allow(dead_code)] -use pyo3::prelude::*; - -mod io { - pyo3::import_exception!(io, UnsupportedOperation); -} - -fn tell(file: &Bound<'_, PyAny>) -> PyResult { - match file.call_method0("tell") { - Err(_) => Err(io::UnsupportedOperation::new_err("not supported: tell")), - Ok(x) => x.extract::(), - } -} -``` - -[`pyo3::exceptions`]({{#PYO3_DOCS_URL}}/pyo3/exceptions/index.html) -defines exceptions for several standard library modules. - -[`create_exception!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.create_exception.html -[`import_exception!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.import_exception.html - -[`PyErr`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html -[`PyResult`]: {{#PYO3_DOCS_URL}}/pyo3/type.PyResult.html -[`PyErr::from_value`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html#method.from_value -[`PyAny::is_instance`]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.is_instance -[`PyAny::is_instance_of`]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.is_instance_of diff --git a/vendor/pyo3/guide/src/faq.md b/vendor/pyo3/guide/src/faq.md deleted file mode 100644 index 83089cf395..0000000000 --- a/vendor/pyo3/guide/src/faq.md +++ /dev/null @@ -1,207 +0,0 @@ -# Frequently Asked Questions and troubleshooting - -Sorry that you're having trouble using PyO3. If you can't find the answer to your problem in the list below, you can also reach out for help on [GitHub Discussions](https://github.com/PyO3/pyo3/discussions) and on [Discord](https://discord.gg/33kcChzH7f). - -## I'm experiencing deadlocks using PyO3 with `std::sync::OnceLock`, `std::sync::LazyLock`, `lazy_static`, and `once_cell`! - -`OnceLock`, `LazyLock`, and their thirdparty predecessors use blocking to ensure only one thread ever initializes them. Because the Python GIL is an additional lock this can lead to deadlocks in the following way: - -1. A thread (thread A) which has acquired the Python GIL starts initialization of a `OnceLock` value. -2. The initialization code calls some Python API which temporarily releases the GIL e.g. `Python::import`. -3. Another thread (thread B) acquires the Python GIL and attempts to access the same `OnceLock` value. -4. Thread B is blocked, because it waits for `OnceLock`'s initialization to lock to release. -5. Thread A is blocked, because it waits to re-acquire the GIL which thread B still holds. -6. Deadlock. - -PyO3 provides a struct [`GILOnceCell`] which implements a single-initialization API based on these types that relies on the GIL for locking. If the GIL is released or there is no GIL, then this type allows the initialization function to race but ensures that the data is only ever initialized once. If you need to ensure that the initialization function is called once and only once, you can make use of the [`OnceExt`] and [`OnceLockExt`] extension traits that enable using the standard library types for this purpose but provide new methods for these types that avoid the risk of deadlocking with the Python GIL. This means they can be used in place of other choices when you are experiencing the deadlock described above. See the documentation for [`GILOnceCell`] and [`OnceExt`] for further details and an example how to use them. - -[`GILOnceCell`]: {{#PYO3_DOCS_URL}}/pyo3/sync/struct.GILOnceCell.html -[`OnceExt`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceExt.html -[`OnceLockExt`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceLockExt.html - -## I can't run `cargo test`; or I can't build in a Cargo workspace: I'm having linker issues like "Symbol not found" or "Undefined reference to _PyExc_SystemError"! - -Currently, [#340](https://github.com/PyO3/pyo3/issues/340) causes `cargo test` to fail with linking errors when the `extension-module` feature is activated. Linking errors can also happen when building in a cargo workspace where a different crate also uses PyO3 (see [#2521](https://github.com/PyO3/pyo3/issues/2521)). For now, there are three ways we can work around these issues. - -1. Make the `extension-module` feature optional. Build with `maturin develop --features "extension-module"` - -```toml -[dependencies.pyo3] -{{#PYO3_CRATE_VERSION}} - -[features] -extension-module = ["pyo3/extension-module"] -``` - -2. Make the `extension-module` feature optional and default. Run tests with `cargo test --no-default-features`: - -```toml -[dependencies.pyo3] -{{#PYO3_CRATE_VERSION}} - -[features] -extension-module = ["pyo3/extension-module"] -default = ["extension-module"] -``` - -3. If you are using a [`pyproject.toml`](https://maturin.rs/metadata.html) file to control maturin settings, add the following section: - -```toml -[tool.maturin] -features = ["pyo3/extension-module"] -# Or for maturin 0.12: -# cargo-extra-args = ["--features", "pyo3/extension-module"] -``` - -## I can't run `cargo test`: my crate cannot be found for tests in `tests/` directory! - -The Rust book suggests to [put integration tests inside a `tests/` directory](https://doc.rust-lang.org/book/ch11-03-test-organization.html#integration-tests). - -For a PyO3 `extension-module` project where the `crate-type` is set to `"cdylib"` in your `Cargo.toml`, -the compiler won't be able to find your crate and will display errors such as `E0432` or `E0463`: - -```text -error[E0432]: unresolved import `my_crate` - --> tests/test_my_crate.rs:1:5 - | -1 | use my_crate; - | ^^^^^^^^^^^^ no external crate `my_crate` -``` - -The best solution is to make your crate types include both `rlib` and `cdylib`: - -```toml -# Cargo.toml -[lib] -crate-type = ["cdylib", "rlib"] -``` - -## Ctrl-C doesn't do anything while my Rust code is executing! - -This is because Ctrl-C raises a SIGINT signal, which is handled by the calling Python process by simply setting a flag to action upon later. This flag isn't checked while Rust code called from Python is executing, only once control returns to the Python interpreter. - -You can give the Python interpreter a chance to process the signal properly by calling `Python::check_signals`. It's good practice to call this function regularly if you have a long-running Rust function so that your users can cancel it. - -## `#[pyo3(get)]` clones my field! - -You may have a nested struct similar to this: - -```rust -# use pyo3::prelude::*; -#[pyclass] -#[derive(Clone)] -struct Inner {/* fields omitted */} - -#[pyclass] -struct Outer { - #[pyo3(get)] - inner: Inner, -} - -#[pymethods] -impl Outer { - #[new] - fn __new__() -> Self { - Self { inner: Inner {} } - } -} -``` - -When Python code accesses `Outer`'s field, PyO3 will return a new object on every access (note that their addresses are different): - -```python -outer = Outer() - -a = outer.inner -b = outer.inner - -assert a is b, f"a: {a}\nb: {b}" -``` - -```text -AssertionError: a: -b: -``` - -This can be especially confusing if the field is mutable, as getting the field and then mutating it won't persist - you'll just get a fresh clone of the original on the next access. Unfortunately Python and Rust don't agree about ownership - if PyO3 gave out references to (possibly) temporary Rust objects to Python code, Python code could then keep that reference alive indefinitely. Therefore returning Rust objects requires cloning. - -If you don't want that cloning to happen, a workaround is to allocate the field on the Python heap and store a reference to that, by using [`Py<...>`]({{#PYO3_DOCS_URL}}/pyo3/struct.Py.html): -```rust -# use pyo3::prelude::*; -#[pyclass] -struct Inner {/* fields omitted */} - -#[pyclass] -struct Outer { - inner: Py, -} - -#[pymethods] -impl Outer { - #[new] - fn __new__(py: Python<'_>) -> PyResult { - Ok(Self { - inner: Py::new(py, Inner {})?, - }) - } - - #[getter] - fn inner(&self, py: Python<'_>) -> Py { - self.inner.clone_ref(py) - } -} -``` -This time `a` and `b` *are* the same object: -```python -outer = Outer() - -a = outer.inner -b = outer.inner - -assert a is b, f"a: {a}\nb: {b}" -print(f"a: {a}\nb: {b}") -``` - -```text -a: -b: -``` -The downside to this approach is that any Rust code working on the `Outer` struct now has to acquire the GIL to do anything with its field. - -## I want to use the `pyo3` crate re-exported from dependency but the proc-macros fail! - -All PyO3 proc-macros (`#[pyclass]`, `#[pyfunction]`, `#[derive(FromPyObject)]` -and so on) expect the `pyo3` crate to be available under that name in your crate -root, which is the normal situation when `pyo3` is a direct dependency of your -crate. - -However, when the dependency is renamed, or your crate only indirectly depends -on `pyo3`, you need to let the macro code know where to find the crate. This is -done with the `crate` attribute: - -```rust -# use pyo3::prelude::*; -# pub extern crate pyo3; -# mod reexported { pub use ::pyo3; } -# #[allow(dead_code)] -#[pyclass] -#[pyo3(crate = "reexported::pyo3")] -struct MyClass; -``` - -## I'm trying to call Python from Rust but I get `STATUS_DLL_NOT_FOUND` or `STATUS_ENTRYPOINT_NOT_FOUND`! - -This happens on Windows when linking to the python DLL fails or the wrong one is linked. The Python DLL on Windows will usually be called something like: -- `python3X.dll` for Python 3.X, e.g. `python310.dll` for Python 3.10 -- `python3.dll` when using PyO3's `abi3` feature - -The DLL needs to be locatable using the [Windows DLL search order](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-unpackaged-apps). Some ways to achieve this are: -- Put the Python DLL in the same folder as your build artifacts -- Add the directory containing the Python DLL to your `PATH` environment variable, for example `C:\Users\\AppData\Local\Programs\Python\Python310` -- If this happens when you are *distributing* your program, consider using [PyOxidizer](https://github.com/indygreg/PyOxidizer) to package it with your binary. - -If the wrong DLL is linked it is possible that this happened because another program added itself and its own Python DLLs to `PATH`. Rearrange your `PATH` variables to give the correct DLL priority. - -> **Note**: Changes to `PATH` (or any other environment variable) are not visible to existing shells. Restart it for changes to take effect. - -For advanced troubleshooting, [Dependency Walker](https://www.dependencywalker.com/) can be used to diagnose linking errors. diff --git a/vendor/pyo3/guide/src/features.md b/vendor/pyo3/guide/src/features.md deleted file mode 100644 index d801e2dd1e..0000000000 --- a/vendor/pyo3/guide/src/features.md +++ /dev/null @@ -1,201 +0,0 @@ -# Features reference - -PyO3 provides a number of Cargo features to customize functionality. This chapter of the guide provides detail on each of them. - -By default, only the `macros` feature is enabled. - -## Features for extension module authors - -### `extension-module` - -This feature is required when building a Python extension module using PyO3. - -It tells PyO3's build script to skip linking against `libpython.so` on Unix platforms, where this must not be done. - -See the [building and distribution](building-and-distribution.md#the-extension-module-feature) section for further detail. - -### `abi3` - -This feature is used when building Python extension modules to create wheels which are compatible with multiple Python versions. - -It restricts PyO3's API to a subset of the full Python API which is guaranteed by [PEP 384](https://www.python.org/dev/peps/pep-0384/) to be forwards-compatible with future Python versions. - -See the [building and distribution](building-and-distribution.md#py_limited_apiabi3) section for further detail. - -### The `abi3-pyXY` features - -(`abi3-py37`, `abi3-py38`, `abi3-py39`, `abi3-py310` and `abi3-py311`) - -These features are extensions of the `abi3` feature to specify the exact minimum Python version which the multiple-version-wheel will support. - -See the [building and distribution](building-and-distribution.md#minimum-python-version-for-abi3) section for further detail. - -### `generate-import-lib` - -This experimental feature is used to generate import libraries for Python DLL -for MinGW-w64 and MSVC (cross-)compile targets. - -Enabling it allows to (cross-)compile extension modules to any Windows targets -without having to install the Windows Python distribution files for the target. - -See the [building and distribution](building-and-distribution.md#building-abi3-extensions-without-a-python-interpreter) -section for further detail. - -## Features for embedding Python in Rust - -### `auto-initialize` - -This feature changes [`Python::with_gil`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.with_gil) to automatically initialize a Python interpreter (by calling [`prepare_freethreaded_python`]({{#PYO3_DOCS_URL}}/pyo3/fn.prepare_freethreaded_python.html)) if needed. - -If you do not enable this feature, you should call `pyo3::prepare_freethreaded_python()` before attempting to call any other Python APIs. - -## Advanced Features - -### `experimental-async` - -This feature adds support for `async fn` in `#[pyfunction]` and `#[pymethods]`. - -The feature has some unfinished refinements and performance improvements. To help finish this off, see [issue #1632](https://github.com/PyO3/pyo3/issues/1632) and its associated draft PRs. - -### `experimental-inspect` - -This feature adds the `pyo3::inspect` module, as well as `IntoPy::type_output` and `FromPyObject::type_input` APIs to produce Python type "annotations" for Rust types. - -This is a first step towards adding first-class support for generating type annotations automatically in PyO3, however work is needed to finish this off. All feedback and offers of help welcome on [issue #2454](https://github.com/PyO3/pyo3/issues/2454). - -### `gil-refs` - -This feature is a backwards-compatibility feature to allow continued use of the "GIL Refs" APIs deprecated in PyO3 0.21. These APIs have performance drawbacks and soundness edge cases which the newer `Bound` smart pointer and accompanying APIs resolve. - -This feature and the APIs it enables is expected to be removed in a future PyO3 version. - -### `py-clone` - -This feature was introduced to ease migration. It was found that delayed reference counts cannot be made sound and hence `Clon`ing an instance of `Py` must panic without the GIL being held. To avoid migrations introducing new panics without warning, the `Clone` implementation itself is now gated behind this feature. - -### `pyo3_disable_reference_pool` - -This is a performance-oriented conditional compilation flag, e.g. [set via `$RUSTFLAGS`][set-configuration-options], which disabled the global reference pool and the assocaited overhead for the crossing the Python-Rust boundary. However, if enabled, `Drop`ping an instance of `Py` without the GIL being held will abort the process. - -### `macros` - -This feature enables a dependency on the `pyo3-macros` crate, which provides the procedural macros portion of PyO3's API: - -- `#[pymodule]` -- `#[pyfunction]` -- `#[pyclass]` -- `#[pymethods]` -- `#[derive(FromPyObject)]` - -It also provides the `py_run!` macro. - -These macros require a number of dependencies which may not be needed by users who just need PyO3 for Python FFI. Disabling this feature enables faster builds for those users, as these dependencies will not be built if this feature is disabled. - -> This feature is enabled by default. To disable it, set `default-features = false` for the `pyo3` entry in your Cargo.toml. - -### `multiple-pymethods` - -This feature enables each `#[pyclass]` to have more than one `#[pymethods]` block. - -Most users should only need a single `#[pymethods]` per `#[pyclass]`. In addition, not all platforms (e.g. Wasm) are supported by `inventory`, which is used in the implementation of the feature. For this reason this feature is not enabled by default, meaning fewer dependencies and faster compilation for the majority of users. - -See [the `#[pyclass]` implementation details](class.md#implementation-details) for more information. - -### `nightly` - -The `nightly` feature needs the nightly Rust compiler. This allows PyO3 to use the `auto_traits` and `negative_impls` features to fix the `Python::allow_threads` function. - -### `resolve-config` - -The `resolve-config` feature of the `pyo3-build-config` crate controls whether that crate's -build script automatically resolves a Python interpreter / build configuration. This feature is primarily useful when building PyO3 -itself. By default this feature is not enabled, meaning you can freely use `pyo3-build-config` as a standalone library to read or write PyO3 build configuration files or resolve metadata about a Python interpreter. - -## Optional Dependencies - -These features enable conversions between Python types and types from other Rust crates, enabling easy access to the rest of the Rust ecosystem. - -### `anyhow` - -Adds a dependency on [anyhow](https://docs.rs/anyhow). Enables a conversion from [anyhow](https://docs.rs/anyhow)’s [`Error`](https://docs.rs/anyhow/latest/anyhow/struct.Error.html) type to [`PyErr`]({{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html), for easy error handling. - -### `chrono` - -Adds a dependency on [chrono](https://docs.rs/chrono). Enables a conversion from [chrono](https://docs.rs/chrono)'s types to python: -- [TimeDelta](https://docs.rs/chrono/latest/chrono/struct.TimeDelta.html) -> [`PyDelta`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyDelta.html) -- [FixedOffset](https://docs.rs/chrono/latest/chrono/offset/struct.FixedOffset.html) -> [`PyDelta`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyDelta.html) -- [Utc](https://docs.rs/chrono/latest/chrono/offset/struct.Utc.html) -> [`PyTzInfo`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyTzInfo.html) -- [NaiveDate](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html) -> [`PyDate`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyDate.html) -- [NaiveTime](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html) -> [`PyTime`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyTime.html) -- [DateTime](https://docs.rs/chrono/latest/chrono/struct.DateTime.html) -> [`PyDateTime`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyDateTime.html) - -### `chrono-tz` - -Adds a dependency on [chrono-tz](https://docs.rs/chrono-tz). -Enables conversion from and to [`Tz`](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html). -It requires at least Python 3.9. - -### `either` - -Adds a dependency on [either](https://docs.rs/either). Enables a conversions into [either](https://docs.rs/either)’s [`Either`](https://docs.rs/either/latest/either/enum.Either.html) type. - -### `eyre` - -Adds a dependency on [eyre](https://docs.rs/eyre). Enables a conversion from [eyre](https://docs.rs/eyre)’s [`Report`](https://docs.rs/eyre/latest/eyre/struct.Report.html) type to [`PyErr`]({{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html), for easy error handling. - -### `hashbrown` - -Adds a dependency on [hashbrown](https://docs.rs/hashbrown) and enables conversions into its [`HashMap`](https://docs.rs/hashbrown/latest/hashbrown/struct.HashMap.html) and [`HashSet`](https://docs.rs/hashbrown/latest/hashbrown/struct.HashSet.html) types. - -### `indexmap` - -Adds a dependency on [indexmap](https://docs.rs/indexmap) and enables conversions into its [`IndexMap`](https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html) type. - -### `num-bigint` - -Adds a dependency on [num-bigint](https://docs.rs/num-bigint) and enables conversions into its [`BigInt`](https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html) and [`BigUint`](https://docs.rs/num-bigint/latest/num_bigint/struct.BigUint.html) types. - -### `num-complex` - -Adds a dependency on [num-complex](https://docs.rs/num-complex) and enables conversions into its [`Complex`](https://docs.rs/num-complex/latest/num_complex/struct.Complex.html) type. - -### `num-rational` - -Adds a dependency on [num-rational](https://docs.rs/num-rational) and enables conversions into its [`Ratio`](https://docs.rs/num-rational/latest/num_rational/struct.Ratio.html) type. - -### `rust_decimal` - -Adds a dependency on [rust_decimal](https://docs.rs/rust_decimal) and enables conversions into its [`Decimal`](https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html) type. - -### `serde` - -Enables (de)serialization of `Py` objects via [serde](https://serde.rs/). -This allows to use [`#[derive(Serialize, Deserialize)`](https://serde.rs/derive.html) on structs that hold references to `#[pyclass]` instances - -```rust -# #[cfg(feature = "serde")] -# #[allow(dead_code)] -# mod serde_only { -# use pyo3::prelude::*; -# use serde::{Deserialize, Serialize}; - -#[pyclass] -#[derive(Serialize, Deserialize)] -struct Permission { - name: String, -} - -#[pyclass] -#[derive(Serialize, Deserialize)] -struct User { - username: String, - permissions: Vec>, -} -# } -``` - -### `smallvec` - -Adds a dependency on [smallvec](https://docs.rs/smallvec) and enables conversions into its [`SmallVec`](https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html) type. - -[set-configuration-options]: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options diff --git a/vendor/pyo3/guide/src/free-threading.md b/vendor/pyo3/guide/src/free-threading.md deleted file mode 100644 index 8ee9a2e100..0000000000 --- a/vendor/pyo3/guide/src/free-threading.md +++ /dev/null @@ -1,415 +0,0 @@ -# Supporting Free-Threaded CPython - -CPython 3.13 introduces an experimental "free-threaded" build of CPython that -does not rely on the [global interpreter -lock](https://docs.python.org/3/glossary.html#term-global-interpreter-lock) -(often referred to as the GIL) for thread safety. As of version 0.23, PyO3 also -has preliminary support for building Rust extensions for the free-threaded -Python build and support for calling into free-threaded Python from Rust. - -If you want more background on free-threaded Python in general, see the [what's -new](https://docs.python.org/3.13/whatsnew/3.13.html#whatsnew313-free-threaded-cpython) -entry in the CPython docs, the [HOWTO -guide](https://docs.python.org/3.13/howto/free-threading-extensions.html#freethreading-extensions-howto) -for porting C extensions, and [PEP 703](https://peps.python.org/pep-0703/), -which provides the technical background for the free-threading implementation in -CPython. - -In the GIL-enabled build, the global interpreter lock serializes access to the -Python runtime. The GIL is therefore a fundamental limitation to parallel -scaling of multithreaded Python workflows, due to [Amdahl's -law](https://en.wikipedia.org/wiki/Amdahl%27s_law), because any time spent -executing a parallel processing task on only one execution context fundamentally -cannot be sped up using parallelism. - -The free-threaded build removes this limit on multithreaded Python scaling. This -means it's much more straightforward to achieve parallelism using the Python -[`threading`] module. If you -have ever needed to use -[`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html) to -achieve a parallel speedup for some Python code, free-threading will likely -allow the use of Python threads instead for the same workflow. - -PyO3's support for free-threaded Python will enable authoring native Python -extensions that are thread-safe by construction, with much stronger safety -guarantees than C extensions. Our goal is to enable ["fearless -concurrency"](https://doc.rust-lang.org/book/ch16-00-concurrency.html) in the -native Python runtime by building on the Rust [`Send` and -`Sync`](https://doc.rust-lang.org/nomicon/send-and-sync.html) traits. - -This document provides advice for porting Rust code using PyO3 to run under -free-threaded Python. - -## Supporting free-threaded Python with PyO3 - -Many simple uses of PyO3, like exposing bindings for a "pure" Rust function -with no side-effects or defining an immutable Python class, will likely work -"out of the box" on the free-threaded build. All that will be necessary is to -annotate Python modules declared by rust code in your project to declare that -they support free-threaded Python, for example by declaring the module with -`#[pymodule(gil_used = false)]`. - -More complicated `#[pyclass]` types may need to deal with thread-safety directly; there is [a dedicated section of the guide](./class/thread-safety.md) to discuss this. - -At a low-level, annotating a module sets the `Py_MOD_GIL` slot on modules -defined by an extension to `Py_MOD_GIL_NOT_USED`, which allows the interpreter -to see at runtime that the author of the extension thinks the extension is -thread-safe. You should only do this if you know that your extension is -thread-safe. Because of Rust's guarantees, this is already true for many -extensions, however see below for more discussion about how to evaluate the -thread safety of existing Rust extensions and how to think about the PyO3 API -using a Python runtime with no GIL. - -If you do not explicitly mark that modules are thread-safe, the Python -interpreter will re-enable the GIL at runtime while importing your module and -print a `RuntimeWarning` with a message containing the name of the module -causing it to re-enable the GIL. You can force the GIL to remain disabled by -setting the `PYTHON_GIL=0` as an environment variable or passing `-Xgil=0` when -starting Python (`0` means the GIL is turned off). - -If you are sure that all data structures exposed in a `PyModule` are -thread-safe, then pass `gil_used = false` as a parameter to the -`pymodule` procedural macro declaring the module or call -`PyModule::gil_used` on a `PyModule` instance. For example: - -```rust -use pyo3::prelude::*; - -/// This module supports free-threaded Python -#[pymodule(gil_used = false)] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - // add members to the module that you know are thread-safe - Ok(()) -} -``` - -Or for a module that is set up without using the `pymodule` macro: - -```rust -use pyo3::prelude::*; - -# #[allow(dead_code)] -fn register_child_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { - let child_module = PyModule::new(parent_module.py(), "child_module")?; - child_module.gil_used(false)?; - parent_module.add_submodule(&child_module) -} - -``` - -For now you must explicitly opt in to free-threading support by annotating -modules defined in your extension. In a future version of `PyO3`, we plan to -make `gil_used = false` the default. - -See the -[`string-sum`](https://github.com/PyO3/pyo3/tree/main/pyo3-ffi/examples/string-sum) -example for how to declare free-threaded support using raw FFI calls for modules -using single-phase initialization and the -[`sequential`](https://github.com/PyO3/pyo3/tree/main/pyo3-ffi/examples/sequential) -example for modules using multi-phase initialization. - -If you would like to use conditional compilation to trigger different code paths -under the free-threaded build, you can use the `Py_GIL_DISABLED` attribute once -you have configured your crate to generate the necessary build configuration -data. See [the guide -section](./building-and-distribution/multiple-python-versions.md) for more -details about supporting multiple different Python versions, including the -free-threaded build. - - -## Special considerations for the free-threaded build - -The free-threaded interpreter does not have a GIL, and this can make interacting -with the PyO3 API confusing, since the API was originally designed around strong -assumptions about the GIL providing locking. Additionally, since the GIL -provided locking for operations on Python objects, many existing extensions that -provide mutable data structures relied on the GIL to make interior mutability -thread-safe. - -Working with PyO3 under the free-threaded interpreter therefore requires some -additional care and mental overhead compared with a GIL-enabled interpreter. We -discuss how to handle this below. - -### Many symbols exposed by PyO3 have `GIL` in the name - -We are aware that there are some naming issues in the PyO3 API that make it -awkward to think about a runtime environment where there is no GIL. We plan to -change the names of these types to de-emphasize the role of the GIL in future -versions of PyO3, but for now you should remember that the use of the term `GIL` -in functions and types like [`Python::with_gil`] and [`GILOnceCell`] is -historical. - -Instead, you should think about whether or not a Rust thread is attached to a -Python interpreter runtime. Calling into the CPython C API is only legal when an -OS thread is explicitly attached to the interpreter runtime. In the GIL-enabled -build, this happens when the GIL is acquired. In the free-threaded build there -is no GIL, but the same C macros that release or acquire the GIL in the -GIL-enabled build instead ask the interpreter to attach the thread to the Python -runtime, and there can be many threads simultaneously attached. See [PEP -703](https://peps.python.org/pep-0703/#thread-states) for more background about -how threads can be attached and detached from the interpreter runtime, in a -manner analagous to releasing and acquiring the GIL in the GIL-enabled build. - -In the GIL-enabled build, PyO3 uses the [`Python<'py>`] type and the `'py` -lifetime to signify that the global interpreter lock is held. In the -freethreaded build, holding a `'py` lifetime means only that the thread is -currently attached to the Python interpreter -- other threads can be -simultaneously interacting with the interpreter. - -You still need to obtain a `'py` lifetime is to interact with Python -objects or call into the CPython C API. If you are not yet attached to the -Python runtime, you can register a thread using the [`Python::with_gil`] -function. Threads created via the Python [`threading`] module do not not need to -do this, and pyo3 will handle setting up the [`Python<'py>`] token when CPython -calls into your extension. - -### Global synchronization events can cause hangs and deadlocks - -The free-threaded build triggers global synchronization events in the following -situations: - -* During garbage collection in order to get a globally consistent view of - reference counts and references between objects -* In Python 3.13, when the first background thread is started in - order to mark certain objects as immortal -* When either `sys.settrace` or `sys.setprofile` are called in order to - instrument running code objects and threads -* Before `os.fork()` is called. - -This is a non-exhaustive list and there may be other situations in future Python -versions that can trigger global synchronization events. - -This means that you should detach from the interpreter runtime using -[`Python::allow_threads`] in exactly the same situations as you should detach -from the runtime in the GIL-enabled build: when doing long-running tasks that do -not require the CPython runtime or when doing any task that needs to re-attach -to the runtime (see the [guide -section](parallelism.md#sharing-python-objects-between-rust-threads) that -covers this). In the former case, you would observe a hang on threads that are -waiting on the long-running task to complete, and in the latter case you would -see a deadlock while a thread tries to attach after the runtime triggers a -global synchronization event, but the spawning thread prevents the -synchronization event from completing. - -### Exceptions and panics for multithreaded access of mutable `pyclass` instances - -Data attached to `pyclass` instances is protected from concurrent access by a -`RefCell`-like pattern of runtime borrow checking. Like a `RefCell`, PyO3 will -raise exceptions (or in some cases panic) to enforce exclusive access for -mutable borrows. It was always possible to generate panics like this in PyO3 in -code that releases the GIL with [`Python::allow_threads`] or calling a python -method accepting `&self` from a `&mut self` (see [the docs on interior -mutability](./class.md#bound-and-interior-mutability),) but now in free-threaded -Python there are more opportunities to trigger these panics from Python because -there is no GIL to lock concurrent access to mutably borrowed data from Python. - -The most straightforward way to trigger this problem is to use the Python -[`threading`] module to simultaneously call a rust function that mutably borrows a -[`pyclass`]({{#PYO3_DOCS_URL}}/pyo3/attr.pyclass.html) in multiple threads. For -example, consider the following implementation: - -```rust -# use pyo3::prelude::*; -# fn main() { -#[pyclass] -#[derive(Default)] -struct ThreadIter { - count: usize, -} - -#[pymethods] -impl ThreadIter { - #[new] - pub fn new() -> Self { - Default::default() - } - - fn __next__(&mut self, py: Python<'_>) -> usize { - self.count += 1; - self.count - } -} -# } -``` - -And then if we do something like this in Python: - -```python -import concurrent.futures -from my_module import ThreadIter - -i = ThreadIter() - -def increment(): - next(i) - -with concurrent.futures.ThreadPoolExecutor(max_workers=16) as tpe: - futures = [tpe.submit(increment) for _ in range(100)] - [f.result() for f in futures] -``` - -We will see an exception: - -```text -Traceback (most recent call last) - File "example.py", line 5, in - next(i) -RuntimeError: Already borrowed -``` - -We plan to allow user-selectable semantics for mutable pyclass definitions in -PyO3 0.24, allowing some form of opt-in locking to emulate the GIL if that is -needed. For now you should explicitly add locking, possibly using conditional -compilation or using the critical section API, to avoid creating deadlocks with -the GIL. - -### Cannot build extensions using the limited API - -The free-threaded build uses a completely new ABI and there is not yet an -equivalent to the limited API for the free-threaded ABI. That means if your -crate depends on PyO3 using the `abi3` feature or an an `abi3-pyxx` feature, -PyO3 will print a warning and ignore that setting when building extensions using -the free-threaded interpreter. - -This means that if your package makes use of the ABI forward compatibility -provided by the limited API to upload only one wheel for each release of your -package, you will need to update your release procedure to also upload a -version-specific free-threaded wheel. - -See [the guide section](./building-and-distribution/multiple-python-versions.md) -for more details about supporting multiple different Python versions, including -the free-threaded build. - -### Thread-safe single initialization - -Until version 0.23, PyO3 provided only [`GILOnceCell`] to enable deadlock-free -single initialization of data in contexts that might execute arbitrary Python -code. While we have updated [`GILOnceCell`] to avoid thread safety issues -triggered only under the free-threaded build, the design of [`GILOnceCell`] is -inherently thread-unsafe, in a manner that can be problematic even in the -GIL-enabled build. - -If, for example, the function executed by [`GILOnceCell`] releases the GIL or -calls code that releases the GIL, then it is possible for multiple threads to -race to initialize the cell. While the cell will only ever be intialized -once, it can be problematic in some contexts that [`GILOnceCell`] does not block -like the standard library [`OnceLock`]. - -In cases where the initialization function must run exactly once, you can bring -the [`OnceExt`] or [`OnceLockExt`] traits into scope. The [`OnceExt`] trait adds -[`OnceExt::call_once_py_attached`] and [`OnceExt::call_once_force_py_attached`] -functions to the api of `std::sync::Once`, enabling use of [`Once`] in contexts -where the GIL is held. Similarly, [`OnceLockExt`] adds -[`OnceLockExt::get_or_init_py_attached`]. These functions are analogous to -[`Once::call_once`], [`Once::call_once_force`], and [`OnceLock::get_or_init`] except -they accept a [`Python<'py>`] token in addition to an `FnOnce`. All of these -functions release the GIL and re-acquire it before executing the function, -avoiding deadlocks with the GIL that are possible without using the PyO3 -extension traits. Here is an example of how to use [`OnceExt`] to -enable single-initialization of a runtime cache holding a `Py`. - -```rust -# fn main() { -# use pyo3::prelude::*; -use std::sync::Once; -use pyo3::sync::OnceExt; -use pyo3::types::PyDict; - -struct RuntimeCache { - once: Once, - cache: Option> -} - -let mut cache = RuntimeCache { - once: Once::new(), - cache: None -}; - -Python::with_gil(|py| { - // guaranteed to be called once and only once - cache.once.call_once_py_attached(py, || { - cache.cache = Some(PyDict::new(py).unbind()); - }); -}); -# } -``` - -### `GILProtected` is not exposed - -[`GILProtected`] is a PyO3 type that allows mutable access to static data by -leveraging the GIL to lock concurrent access from other threads. In -free-threaded Python there is no GIL, so you will need to replace this type with -some other form of locking. In many cases, a type from -[`std::sync::atomic`](https://doc.rust-lang.org/std/sync/atomic/) or a -[`std::sync::Mutex`](https://doc.rust-lang.org/std/sync/struct.Mutex.html) will -be sufficient. - -Before: - -```rust -# fn main() { -# #[cfg(not(Py_GIL_DISABLED))] { -# use pyo3::prelude::*; -use pyo3::sync::GILProtected; -use pyo3::types::{PyDict, PyNone}; -use std::cell::RefCell; - -static OBJECTS: GILProtected>>> = - GILProtected::new(RefCell::new(Vec::new())); - -Python::with_gil(|py| { - // stand-in for something that executes arbitrary Python code - let d = PyDict::new(py); - d.set_item(PyNone::get(py), PyNone::get(py)).unwrap(); - OBJECTS.get(py).borrow_mut().push(d.unbind()); -}); -# }} -``` - -After: - -```rust -# use pyo3::prelude::*; -# fn main() { -use pyo3::types::{PyDict, PyNone}; -use std::sync::Mutex; - -static OBJECTS: Mutex>> = Mutex::new(Vec::new()); - -Python::with_gil(|py| { - // stand-in for something that executes arbitrary Python code - let d = PyDict::new(py); - d.set_item(PyNone::get(py), PyNone::get(py)).unwrap(); - // as with any `Mutex` usage, lock the mutex for as little time as possible - // in this case, we do it just while pushing into the `Vec` - OBJECTS.lock().unwrap().push(d.unbind()); -}); -# } -``` - -If you are executing arbitrary Python code while holding the lock, then you will -need to use conditional compilation to use [`GILProtected`] on GIL-enabled Python -builds and mutexes otherwise. If your use of [`GILProtected`] does not guard the -execution of arbitrary Python code or use of the CPython C API, then conditional -compilation is likely unnecessary since [`GILProtected`] was not needed in the -first place and instead Rust mutexes or atomics should be preferred. Python 3.13 -introduces [`PyMutex`](https://docs.python.org/3/c-api/init.html#c.PyMutex), -which releases the GIL while the waiting for the lock, so that is another option -if you only need to support newer Python versions. - -[`GILOnceCell`]: {{#PYO3_DOCS_URL}}/pyo3/sync/struct.GILOnceCell.html -[`GILProtected`]: https://docs.rs/pyo3/0.22/pyo3/sync/struct.GILProtected.html -[`Once`]: https://doc.rust-lang.org/stable/std/sync/struct.Once.html -[`Once::call_once`]: https://doc.rust-lang.org/stable/std/sync/struct.Once.html#tymethod.call_once -[`Once::call_once_force`]: https://doc.rust-lang.org/stable/std/sync/struct.Once.html#tymethod.call_once_force -[`OnceExt`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceExt.html -[`OnceExt::call_once_py_attached`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceExt.html#tymethod.call_once_py_attached -[`OnceExt::call_once_force_py_attached`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceExt.html#tymethod.call_once_force_py_attached -[`OnceLockExt`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceLockExt.html -[`OnceLockExt::get_or_init_py_attached`]: {{#PYO3_DOCS_URL}}/pyo3/sync/trait.OnceLockExt.html#tymethod.get_or_init_py_attached -[`OnceLock`]: https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html -[`OnceLock::get_or_init`]: https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html#tymethod.get_or_init -[`Python::allow_threads`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.allow_threads -[`Python::with_gil`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.with_gil -[`Python<'py>`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html -[`threading`]: https://docs.python.org/3/library/threading.html diff --git a/vendor/pyo3/guide/src/function-calls.md b/vendor/pyo3/guide/src/function-calls.md deleted file mode 100644 index e5c9c1ed9b..0000000000 --- a/vendor/pyo3/guide/src/function-calls.md +++ /dev/null @@ -1 +0,0 @@ -# Calling Python functions diff --git a/vendor/pyo3/guide/src/function.md b/vendor/pyo3/guide/src/function.md deleted file mode 100644 index fd215a1550..0000000000 --- a/vendor/pyo3/guide/src/function.md +++ /dev/null @@ -1,208 +0,0 @@ -# Python functions - -The `#[pyfunction]` attribute is used to define a Python function from a Rust function. Once defined, the function needs to be added to a [module](./module.md) using the `wrap_pyfunction!` macro. - -The following example defines a function called `double` in a Python module called `my_extension`: - -```rust -use pyo3::prelude::*; - -#[pyfunction] -fn double(x: usize) -> usize { - x * 2 -} - -#[pymodule] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?) -} -``` - -This chapter of the guide explains full usage of the `#[pyfunction]` attribute. In this first section, the following topics are covered: - -- [Function options](#function-options) - - [`#[pyo3(name = "...")]`](#name) - - [`#[pyo3(signature = (...))]`](#signature) - - [`#[pyo3(text_signature = "...")]`](#text_signature) - - [`#[pyo3(pass_module)]`](#pass_module) -- [Per-argument options](#per-argument-options) -- [Advanced function patterns](#advanced-function-patterns) -- [`#[pyfn]` shorthand](#pyfn-shorthand) - -There are also additional sections on the following topics: - -- [Function Signatures](./function/signature.md) - -## Function options - -The `#[pyo3]` attribute can be used to modify properties of the generated Python function. It can take any combination of the following options: - - - `#[pyo3(name = "...")]` - - Overrides the name exposed to Python. - - In the following example, the Rust function `no_args_py` will be added to the Python module - `module_with_functions` as the Python function `no_args`: - - ```rust - use pyo3::prelude::*; - - #[pyfunction] - #[pyo3(name = "no_args")] - fn no_args_py() -> usize { - 42 - } - - #[pymodule] - fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(no_args_py, m)?) - } - - # Python::with_gil(|py| { - # let m = pyo3::wrap_pymodule!(module_with_functions)(py); - # assert!(m.getattr(py, "no_args").is_ok()); - # assert!(m.getattr(py, "no_args_py").is_err()); - # }); - ``` - - - `#[pyo3(signature = (...))]` - - Defines the function signature in Python. See [Function Signatures](./function/signature.md). - - - `#[pyo3(text_signature = "...")]` - - Overrides the PyO3-generated function signature visible in Python tooling (such as via [`inspect.signature`]). See the [corresponding topic in the Function Signatures subchapter](./function/signature.md#making-the-function-signature-available-to-python). - - - `#[pyo3(pass_module)]` - - Set this option to make PyO3 pass the containing module as the first argument to the function. It is then possible to use the module in the function body. The first argument **must** be of type `&Bound<'_, PyModule>`, `Bound<'_, PyModule>`, or `Py`. - - The following example creates a function `pyfunction_with_module` which returns the containing module's name (i.e. `module_with_fn`): - - ```rust - use pyo3::prelude::*; - use pyo3::types::PyString; - - #[pyfunction] - #[pyo3(pass_module)] - fn pyfunction_with_module<'py>( - module: &Bound<'py, PyModule>, - ) -> PyResult> { - module.name() - } - - #[pymodule] - fn module_with_fn(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?) - } - ``` - -## Per-argument options - -The `#[pyo3]` attribute can be used on individual arguments to modify properties of them in the generated function. It can take any combination of the following options: - - - `#[pyo3(from_py_with = "...")]` - - Set this on an option to specify a custom function to convert the function argument from Python to the desired Rust type, instead of using the default `FromPyObject` extraction. The function signature must be `fn(&Bound<'_, PyAny>) -> PyResult` where `T` is the Rust type of the argument. - - The following example uses `from_py_with` to convert the input Python object to its length: - - ```rust - use pyo3::prelude::*; - - fn get_length(obj: &Bound<'_, PyAny>) -> PyResult { - obj.len() - } - - #[pyfunction] - fn object_length(#[pyo3(from_py_with = "get_length")] argument: usize) -> usize { - argument - } - - # Python::with_gil(|py| { - # let f = pyo3::wrap_pyfunction!(object_length)(py).unwrap(); - # assert_eq!(f.call1((vec![1, 2, 3],)).unwrap().extract::().unwrap(), 3); - # }); - ``` - -## Advanced function patterns - -### Calling Python functions in Rust - -You can pass Python `def`'d functions and built-in functions to Rust functions [`PyFunction`] -corresponds to regular Python functions while [`PyCFunction`] describes built-ins such as -`repr()`. - -You can also use [`Bound<'_, PyAny>::is_callable`] to check if you have a callable object. `is_callable` -will return `true` for functions (including lambdas), methods and objects with a `__call__` method. -You can call the object with [`Bound<'_, PyAny>::call`] with the args as first parameter and the kwargs -(or `None`) as second parameter. There are also [`Bound<'_, PyAny>::call0`] with no args and -[`Bound<'_, PyAny>::call1`] with only positional args. - -### Calling Rust functions in Python - -The ways to convert a Rust function into a Python object vary depending on the function: - -- Named functions, e.g. `fn foo()`: add `#[pyfunction]` and then use [`wrap_pyfunction!`] to get the corresponding [`PyCFunction`]. -- Anonymous functions (or closures), e.g. `foo: fn()` either: - - use a `#[pyclass]` struct which stores the function as a field and implement `__call__` to call the stored function. - - use `PyCFunction::new_closure` to create an object directly from the function. - -[`Bound<'_, PyAny>::is_callable`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyAnyMethods.html#tymethod.is_callable -[`Bound<'_, PyAny>::call`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyAnyMethods.html#tymethod.call -[`Bound<'_, PyAny>::call0`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyAnyMethods.html#tymethod.call0 -[`Bound<'_, PyAny>::call1`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyAnyMethods.html#tymethod.call1 -[`wrap_pyfunction!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.wrap_pyfunction.html -[`PyFunction`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyFunction.html -[`PyCFunction`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyCFunction.html - -### Accessing the FFI functions - -In order to make Rust functions callable from Python, PyO3 generates an `extern "C"` -function whose exact signature depends on the Rust signature. (PyO3 chooses the optimal -Python argument passing convention.) It then embeds the call to the Rust function inside this -FFI-wrapper function. This wrapper handles extraction of the regular arguments and the keyword -arguments from the input `PyObject`s. - -The `wrap_pyfunction` macro can be used to directly get a `Bound` given a -`#[pyfunction]` and a `Bound`: `wrap_pyfunction!(rust_fun, module)`. - -## `#[pyfn]` shorthand - -There is a shorthand to `#[pyfunction]` and `wrap_pymodule!`: the function can be placed inside the module definition and -annotated with `#[pyfn]`. To simplify PyO3, it is expected that `#[pyfn]` may be removed in a future release (See [#694](https://github.com/PyO3/pyo3/issues/694)). - -An example of `#[pyfn]` is below: - -```rust -use pyo3::prelude::*; - -#[pymodule] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - #[pyfn(m)] - fn double(x: usize) -> usize { - x * 2 - } - - Ok(()) -} -``` - -`#[pyfn(m)]` is just syntactic sugar for `#[pyfunction]`, and takes all the same options -documented in the rest of this chapter. The code above is expanded to the following: - -```rust -use pyo3::prelude::*; - -#[pymodule] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - #[pyfunction] - fn double(x: usize) -> usize { - x * 2 - } - - m.add_function(wrap_pyfunction!(double, m)?) -} -``` - -[`inspect.signature`]: https://docs.python.org/3/library/inspect.html#inspect.signature diff --git a/vendor/pyo3/guide/src/function/error-handling.md b/vendor/pyo3/guide/src/function/error-handling.md deleted file mode 100644 index 0d7fae7976..0000000000 --- a/vendor/pyo3/guide/src/function/error-handling.md +++ /dev/null @@ -1,239 +0,0 @@ -# Error handling - -This chapter contains a little background of error handling in Rust and how PyO3 integrates this with Python exceptions. - -This covers enough detail to create a `#[pyfunction]` which raises Python exceptions from errors originating in Rust. - -There is a later section of the guide on [Python exceptions](../exception.md) which covers exception types in more detail. - -## Representing Python exceptions - -Rust code uses the generic [`Result`] enum to propagate errors. The error type `E` is chosen by the code author to describe the possible errors which can happen. - -PyO3 has the [`PyErr`] type which represents a Python exception. If a PyO3 API could result in a Python exception being raised, the return type of that `API` will be [`PyResult`], which is an alias for the type `Result`. - -In summary: -- When Python exceptions are raised and caught by PyO3, the exception will be stored in the `Err` variant of the `PyResult`. -- Passing Python exceptions through Rust code then uses all the "normal" techniques such as the `?` operator, with `PyErr` as the error type. -- Finally, when a `PyResult` crosses from Rust back to Python via PyO3, if the result is an `Err` variant the contained exception will be raised. - -(There are many great tutorials on Rust error handling and the `?` operator, so this guide will not go into detail on Rust-specific topics.) - -## Raising an exception from a function - -As indicated in the previous section, when a `PyResult` containing an `Err` crosses from Rust to Python, PyO3 will raise the exception contained within. - -Accordingly, to raise an exception from a `#[pyfunction]`, change the return type `T` to `PyResult`. When the function returns an `Err` it will raise a Python exception. (Other `Result` types can be used as long as the error `E` has a `From` conversion for `PyErr`, see [custom Rust error types](#custom-rust-error-types) below.) - -This also works for functions in `#[pymethods]`. - -For example, the following `check_positive` function raises a `ValueError` when the input is negative: - -```rust -use pyo3::exceptions::PyValueError; -use pyo3::prelude::*; - -#[pyfunction] -fn check_positive(x: i32) -> PyResult<()> { - if x < 0 { - Err(PyValueError::new_err("x is negative")) - } else { - Ok(()) - } -} -# -# fn main(){ -# Python::with_gil(|py|{ -# let fun = pyo3::wrap_pyfunction!(check_positive, py).unwrap(); -# fun.call1((-1,)).unwrap_err(); -# fun.call1((1,)).unwrap(); -# }); -# } -``` - -All built-in Python exception types are defined in the [`pyo3::exceptions`] module. They have a `new_err` constructor to directly build a `PyErr`, as seen in the example above. - -## Custom Rust error types - -PyO3 will automatically convert a `Result` returned by a `#[pyfunction]` into a `PyResult` as long as there is an implementation of `std::from::From for PyErr`. Many error types in the Rust standard library have a [`From`] conversion defined in this way. - -If the type `E` you are handling is defined in a third-party crate, see the section on [foreign rust error types](#foreign-rust-error-types) below for ways to work with this error. - -The following example makes use of the implementation of `From for PyErr` to raise exceptions encountered when parsing strings as integers: - -```rust -# use pyo3::prelude::*; -use std::num::ParseIntError; - -#[pyfunction] -fn parse_int(x: &str) -> Result { - x.parse() -} - -# fn main() { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(parse_int, py).unwrap(); -# let value: usize = fun.call1(("5",)).unwrap().extract().unwrap(); -# assert_eq!(value, 5); -# }); -# } -``` - -When passed a string which doesn't contain a floating-point number, the exception raised will look like the below: - -```python ->>> parse_int("bar") -Traceback (most recent call last): - File "", line 1, in -ValueError: invalid digit found in string -``` - -As a more complete example, the following snippet defines a Rust error named `CustomIOError`. It then defines a `From for PyErr`, which returns a `PyErr` representing Python's `OSError`. -Therefore, it can use this error in the result of a `#[pyfunction]` directly, relying on the conversion if it has to be propagated into a Python exception. - -```rust -use pyo3::exceptions::PyOSError; -use pyo3::prelude::*; -use std::fmt; - -#[derive(Debug)] -struct CustomIOError; - -impl std::error::Error for CustomIOError {} - -impl fmt::Display for CustomIOError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Oh no!") - } -} - -impl std::convert::From for PyErr { - fn from(err: CustomIOError) -> PyErr { - PyOSError::new_err(err.to_string()) - } -} - -pub struct Connection {/* ... */} - -fn bind(addr: String) -> Result { - if &addr == "0.0.0.0" { - Err(CustomIOError) - } else { - Ok(Connection{ /* ... */}) - } -} - -#[pyfunction] -fn connect(s: String) -> Result<(), CustomIOError> { - bind(s)?; - // etc. - Ok(()) -} - -fn main() { - Python::with_gil(|py| { - let fun = pyo3::wrap_pyfunction!(connect, py).unwrap(); - let err = fun.call1(("0.0.0.0",)).unwrap_err(); - assert!(err.is_instance_of::(py)); - }); -} -``` - -If lazy construction of the Python exception instance is desired, the -[`PyErrArguments`]({{#PYO3_DOCS_URL}}/pyo3/trait.PyErrArguments.html) -trait can be implemented instead of `From`. In that case, actual exception argument creation is delayed -until the `PyErr` is needed. - -A final note is that any errors `E` which have a `From` conversion can be used with the `?` -("try") operator with them. An alternative implementation of the above `parse_int` which instead returns `PyResult` is below: - -```rust -use pyo3::prelude::*; - -fn parse_int(s: String) -> PyResult { - let x = s.parse()?; - Ok(x) -} -# -# use pyo3::exceptions::PyValueError; -# -# fn main() { -# Python::with_gil(|py| { -# assert_eq!(parse_int(String::from("1")).unwrap(), 1); -# assert_eq!(parse_int(String::from("1337")).unwrap(), 1337); -# -# assert!(parse_int(String::from("-1")) -# .unwrap_err() -# .is_instance_of::(py)); -# assert!(parse_int(String::from("foo")) -# .unwrap_err() -# .is_instance_of::(py)); -# assert!(parse_int(String::from("13.37")) -# .unwrap_err() -# .is_instance_of::(py)); -# }) -# } -``` - -## Foreign Rust error types - -The Rust compiler will not permit implementation of traits for types outside of the crate where the type is defined. (This is known as the "orphan rule".) - -Given a type `OtherError` which is defined in third-party code, there are two main strategies available to integrate it with PyO3: - -- Create a newtype wrapper, e.g. `MyOtherError`. Then implement `From for PyErr` (or `PyErrArguments`), as well as `From` for `MyOtherError`. -- Use Rust's Result combinators such as `map_err` to write code freely to convert `OtherError` into whatever is needed. This requires boilerplate at every usage however gives unlimited flexibility. - -To detail the newtype strategy a little further, the key trick is to return `Result` from the `#[pyfunction]`. This means that PyO3 will make use of `From for PyErr` to create Python exceptions while the `#[pyfunction]` implementation can use `?` to convert `OtherError` to `MyOtherError` automatically. - -The following example demonstrates this for some imaginary third-party crate `some_crate` with a function `get_x` returning `Result`: - -```rust -# mod some_crate { -# pub struct OtherError(()); -# impl OtherError { -# pub fn message(&self) -> &'static str { "some error occurred" } -# } -# pub fn get_x() -> Result { Ok(5) } -# } - -use pyo3::prelude::*; -use pyo3::exceptions::PyValueError; -use some_crate::{OtherError, get_x}; - -struct MyOtherError(OtherError); - -impl From for PyErr { - fn from(error: MyOtherError) -> Self { - PyValueError::new_err(error.0.message()) - } -} - -impl From for MyOtherError { - fn from(other: OtherError) -> Self { - Self(other) - } -} - -#[pyfunction] -fn wrapped_get_x() -> Result { - // get_x is a function returning Result - let x: i32 = get_x()?; - Ok(x) -} - -# fn main() { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(wrapped_get_x, py).unwrap(); -# let value: usize = fun.call0().unwrap().extract().unwrap(); -# assert_eq!(value, 5); -# }); -# } -``` - - -[`From`]: https://doc.rust-lang.org/stable/std/convert/trait.From.html -[`Result`]: https://doc.rust-lang.org/stable/std/result/enum.Result.html -[`PyResult`]: {{#PYO3_DOCS_URL}}/pyo3/prelude/type.PyResult.html -[`PyErr`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html -[`pyo3::exceptions`]: {{#PYO3_DOCS_URL}}/pyo3/exceptions/index.html diff --git a/vendor/pyo3/guide/src/function/signature.md b/vendor/pyo3/guide/src/function/signature.md deleted file mode 100644 index 8ebe74456a..0000000000 --- a/vendor/pyo3/guide/src/function/signature.md +++ /dev/null @@ -1,328 +0,0 @@ -# Function signatures - -The `#[pyfunction]` attribute also accepts parameters to control how the generated Python function accepts arguments. Just like in Python, arguments can be positional-only, keyword-only, or accept either. `*args` lists and `**kwargs` dicts can also be accepted. These parameters also work for `#[pymethods]` which will be introduced in the [Python Classes](../class.md) section of the guide. - -Like Python, by default PyO3 accepts all arguments as either positional or keyword arguments. Most arguments are required by default, except for trailing `Option<_>` arguments, which are [implicitly given a default of `None`](#trailing-optional-arguments). This behaviour can be configured by the `#[pyo3(signature = (...))]` option which allows writing a signature in Python syntax. - -This section of the guide goes into detail about use of the `#[pyo3(signature = (...))]` option and its related option `#[pyo3(text_signature = "...")]` - -## Using `#[pyo3(signature = (...))]` - -For example, below is a function that accepts arbitrary keyword arguments (`**kwargs` in Python syntax) and returns the number that was passed: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyDict; - -#[pyfunction] -#[pyo3(signature = (**kwds))] -fn num_kwds(kwds: Option<&Bound<'_, PyDict>>) -> usize { - kwds.map_or(0, |dict| dict.len()) -} - -#[pymodule] -fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(num_kwds, m)?) -} -``` - -Just like in Python, the following constructs can be part of the signature:: - - * `/`: positional-only arguments separator, each parameter defined before `/` is a positional-only parameter. - * `*`: var arguments separator, each parameter defined after `*` is a keyword-only parameter. - * `*args`: "args" is var args. Type of the `args` parameter has to be `&Bound<'_, PyTuple>`. - * `**kwargs`: "kwargs" receives keyword arguments. The type of the `kwargs` parameter has to be `Option<&Bound<'_, PyDict>>`. - * `arg=Value`: arguments with default value. - If the `arg` argument is defined after var arguments, it is treated as a keyword-only argument. - Note that `Value` has to be valid rust code, PyO3 just inserts it into the generated - code unmodified. - -Example: -```rust -# use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; -# -# #[pyclass] -# struct MyClass { -# num: i32, -# } -#[pymethods] -impl MyClass { - #[new] - #[pyo3(signature = (num=-1))] - fn new(num: i32) -> Self { - MyClass { num } - } - - #[pyo3(signature = (num=10, *py_args, name="Hello", **py_kwargs))] - fn method( - &mut self, - num: i32, - py_args: &Bound<'_, PyTuple>, - name: &str, - py_kwargs: Option<&Bound<'_, PyDict>>, - ) -> String { - let num_before = self.num; - self.num = num; - format!( - "num={} (was previously={}), py_args={:?}, name={}, py_kwargs={:?} ", - num, num_before, py_args, name, py_kwargs, - ) - } - - fn make_change(&mut self, num: i32) -> PyResult { - self.num = num; - Ok(format!("num={}", self.num)) - } -} -``` - -Arguments of type `Python` must not be part of the signature: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyfunction] -#[pyo3(signature = (lambda))] -pub fn simple_python_bound_function(py: Python<'_>, lambda: PyObject) -> PyResult<()> { - Ok(()) -} -``` - -N.B. the position of the `/` and `*` arguments (if included) control the system of handling positional and keyword arguments. In Python: -```python -import mymodule - -mc = mymodule.MyClass() -print(mc.method(44, False, "World", 666, x=44, y=55)) -print(mc.method(num=-1, name="World")) -print(mc.make_change(44, False)) -``` -Produces output: -```text -py_args=('World', 666), py_kwargs=Some({'x': 44, 'y': 55}), name=Hello, num=44 -py_args=(), py_kwargs=None, name=World, num=-1 -num=44 -num=-1 -``` - -> Note: to use keywords like `struct` as a function argument, use "raw identifier" syntax `r#struct` in both the signature and the function definition: -> -> ```rust -> # #![allow(dead_code)] -> # use pyo3::prelude::*; -> #[pyfunction(signature = (r#struct = "foo"))] -> fn function_with_keyword(r#struct: &str) { -> # let _ = r#struct; -> /* ... */ -> } -> ``` - -## Trailing optional arguments - -
- -⚠️ Warning: This behaviour is being phased out 🛠️ - -The special casing of trailing optional arguments is deprecated. In a future `pyo3` version, arguments of type `Option<..>` will share the same behaviour as other arguments, they are required unless a default is set using `#[pyo3(signature = (...))]`. - -This is done to better align the Python and Rust definition of such functions and make it more intuitive to rewrite them from Python in Rust. Specifically `def some_fn(a: int, b: Optional[int]): ...` will not automatically default `b` to `none`, but requires an explicit default if desired, where as in current `pyo3` it is handled the other way around. - -During the migration window a `#[pyo3(signature = (...))]` will be required to silence the deprecation warning. After support for trailing optional arguments is fully removed, the signature attribute can be removed if all arguments should be required. -
- - -As a convenience, functions without a `#[pyo3(signature = (...))]` option will treat trailing `Option` arguments as having a default of `None`. In the example below, PyO3 will create `increment` with a signature of `increment(x, amount=None)`. - -```rust -#![allow(deprecated)] -use pyo3::prelude::*; - -/// Returns a copy of `x` increased by `amount`. -/// -/// If `amount` is unspecified or `None`, equivalent to `x + 1`. -#[pyfunction] -fn increment(x: u64, amount: Option) -> u64 { - x + amount.unwrap_or(1) -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(increment, py)?; -# -# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?; -# let sig: String = inspect -# .call1((fun,))? -# .call_method0("__str__")? -# .extract()?; -# -# #[cfg(Py_3_8)] // on 3.7 the signature doesn't render b, upstream bug? -# assert_eq!(sig, "(x, amount=None)"); -# -# Ok(()) -# }) -# } -``` - -To make trailing `Option` arguments required, but still accept `None`, add a `#[pyo3(signature = (...))]` annotation. For the example above, this would be `#[pyo3(signature = (x, amount))]`: - -```rust -# use pyo3::prelude::*; -#[pyfunction] -#[pyo3(signature = (x, amount))] -fn increment(x: u64, amount: Option) -> u64 { - x + amount.unwrap_or(1) -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(increment, py)?; -# -# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?; -# let sig: String = inspect -# .call1((fun,))? -# .call_method0("__str__")? -# .extract()?; -# -# #[cfg(Py_3_8)] // on 3.7 the signature doesn't render b, upstream bug? -# assert_eq!(sig, "(x, amount)"); -# -# Ok(()) -# }) -# } -``` - -To help avoid confusion, PyO3 requires `#[pyo3(signature = (...))]` when an `Option` argument is surrounded by arguments which aren't `Option`. - -## Making the function signature available to Python - -The function signature is exposed to Python via the `__text_signature__` attribute. PyO3 automatically generates this for every `#[pyfunction]` and all `#[pymethods]` directly from the Rust function, taking into account any override done with the `#[pyo3(signature = (...))]` option. - -This automatic generation can only display the value of default arguments for strings, integers, boolean types, and `None`. Any other default arguments will be displayed as `...`. (`.pyi` type stub files commonly also use `...` for default arguments in the same way.) - -In cases where the automatically-generated signature needs adjusting, it can [be overridden](#overriding-the-generated-signature) using the `#[pyo3(text_signature)]` option.) - -The example below creates a function `add` which accepts two positional-only arguments `a` and `b`, where `b` has a default value of zero. - -```rust -use pyo3::prelude::*; - -/// This function adds two unsigned 64-bit integers. -#[pyfunction] -#[pyo3(signature = (a, b=0, /))] -fn add(a: u64, b: u64) -> u64 { - a + b -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(add, py)?; -# -# let doc: String = fun.getattr("__doc__")?.extract()?; -# assert_eq!(doc, "This function adds two unsigned 64-bit integers."); -# -# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?; -# let sig: String = inspect -# .call1((fun,))? -# .call_method0("__str__")? -# .extract()?; -# -# #[cfg(Py_3_8)] // on 3.7 the signature doesn't render b, upstream bug? -# assert_eq!(sig, "(a, b=0, /)"); -# -# Ok(()) -# }) -# } -``` - -The following IPython output demonstrates how this generated signature will be seen from Python tooling: - -```text ->>> pyo3_test.add.__text_signature__ -'(a, b=..., /)' ->>> pyo3_test.add? -Signature: pyo3_test.add(a, b=0, /) -Docstring: This function adds two unsigned 64-bit integers. -Type: builtin_function_or_method -``` - -### Overriding the generated signature - -The `#[pyo3(text_signature = "()")]` attribute can be used to override the default generated signature. - -In the snippet below, the text signature attribute is used to include the default value of `0` for the argument `b`, instead of the automatically-generated default value of `...`: - -```rust -use pyo3::prelude::*; - -/// This function adds two unsigned 64-bit integers. -#[pyfunction] -#[pyo3(signature = (a, b=0, /), text_signature = "(a, b=0, /)")] -fn add(a: u64, b: u64) -> u64 { - a + b -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(add, py)?; -# -# let doc: String = fun.getattr("__doc__")?.extract()?; -# assert_eq!(doc, "This function adds two unsigned 64-bit integers."); -# -# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?; -# let sig: String = inspect -# .call1((fun,))? -# .call_method0("__str__")? -# .extract()?; -# assert_eq!(sig, "(a, b=0, /)"); -# -# Ok(()) -# }) -# } -``` - -PyO3 will include the contents of the annotation unmodified as the `__text_signature__`. Below shows how IPython will now present this (see the default value of 0 for b): - -```text ->>> pyo3_test.add.__text_signature__ -'(a, b=0, /)' ->>> pyo3_test.add? -Signature: pyo3_test.add(a, b=0, /) -Docstring: This function adds two unsigned 64-bit integers. -Type: builtin_function_or_method -``` - -If no signature is wanted at all, `#[pyo3(text_signature = None)]` will disable the built-in signature. The snippet below demonstrates use of this: - -```rust -use pyo3::prelude::*; - -/// This function adds two unsigned 64-bit integers. -#[pyfunction] -#[pyo3(signature = (a, b=0, /), text_signature = None)] -fn add(a: u64, b: u64) -> u64 { - a + b -} -# -# fn main() -> PyResult<()> { -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(add, py)?; -# -# let doc: String = fun.getattr("__doc__")?.extract()?; -# assert_eq!(doc, "This function adds two unsigned 64-bit integers."); -# assert!(fun.getattr("__text_signature__")?.is_none()); -# -# Ok(()) -# }) -# } -``` - -Now the function's `__text_signature__` will be set to `None`, and IPython will not display any signature in the help: - -```text ->>> pyo3_test.add.__text_signature__ == None -True ->>> pyo3_test.add? -Docstring: This function adds two unsigned 64-bit integers. -Type: builtin_function_or_method -``` diff --git a/vendor/pyo3/guide/src/getting-started.md b/vendor/pyo3/guide/src/getting-started.md deleted file mode 100644 index e2cc040bbd..0000000000 --- a/vendor/pyo3/guide/src/getting-started.md +++ /dev/null @@ -1,181 +0,0 @@ -# Installation - -To get started using PyO3 you will need three things: a Rust toolchain, a Python environment, and a way to build. We'll cover each of these below. - -> If you'd like to chat to the PyO3 maintainers and other PyO3 users, consider joining the [PyO3 Discord server](https://discord.gg/33kcChzH7f). We're keen to hear about your experience getting started, so we can make PyO3 as accessible as possible for everyone! - -## Rust - -First, make sure you have Rust installed on your system. If you haven't already done so, try following the instructions [here](https://www.rust-lang.org/tools/install). PyO3 runs on both the `stable` and `nightly` versions so you can choose whichever one fits you best. The minimum required Rust version is 1.63. - -If you can run `rustc --version` and the version is new enough you're good to go! - -## Python - -To use PyO3, you need at least Python 3.7. While you can simply use the default Python interpreter on your system, it is recommended to use a virtual environment. - -## Virtualenvs - -While you can use any virtualenv manager you like, we recommend the use of `pyenv` in particular if you want to develop or test for multiple different Python versions, so that is what the examples in this book will use. The installation instructions for `pyenv` can be found [here](https://github.com/pyenv/pyenv#getting-pyenv). (Note: To get the `pyenv activate` and `pyenv virtualenv` commands, you will also need to install the [`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv) plugin. The [pyenv installer](https://github.com/pyenv/pyenv-installer#installation--update--uninstallation) will install both together.) - -It can be useful to keep the sources used when installing using `pyenv` so that future debugging can see the original source files. This can be done by passing the `--keep` flag as part of the `pyenv install` command. - -For example: - -```bash -pyenv install 3.12 --keep -``` - -### Building - -There are a number of build and Python package management systems such as [`setuptools-rust`](https://github.com/PyO3/setuptools-rust) or [manually](./building-and-distribution.md#manual-builds). We recommend the use of `maturin`, which you can install [here](https://maturin.rs/installation.html). It is developed to work with PyO3 and provides the most "batteries included" experience, especially if you are aiming to publish to PyPI. `maturin` is just a Python package, so you can add it in the same way you already install Python packages. - -System Python: -```bash -pip install maturin --user -``` - -pipx: -```bash -pipx install maturin -``` - -pyenv: -```bash -pyenv activate pyo3 -pip install maturin -``` - -poetry: -```bash -poetry add -G dev maturin -``` - -After installation, you can run `maturin --version` to check that you have correctly installed it. - -# Starting a new project - -First you should create the folder and virtual environment that are going to contain your new project. Here we will use the recommended `pyenv`: - -```bash -mkdir pyo3-example -cd pyo3-example -pyenv virtualenv pyo3 -pyenv local pyo3 -``` - -After this, you should install your build manager. In this example, we will use `maturin`. After you've activated your virtualenv, add `maturin` to it: - -```bash -pip install maturin -``` - -Now you can initialize the new project: - -```bash -maturin init -``` - -If `maturin` is already installed, you can create a new project using that directly as well: - -```bash -maturin new -b pyo3 pyo3-example -cd pyo3-example -pyenv virtualenv pyo3 -pyenv local pyo3 -``` - -# Adding to an existing project - -Sadly, `maturin` cannot currently be run in existing projects, so if you want to use Python in an existing project you basically have two options: - -1. Create a new project as above and move your existing code into that project -2. Manually edit your project configuration as necessary - -If you opt for the second option, here are the things you need to pay attention to: - -## Cargo.toml - -Make sure that the Rust crate you want to be able to access from Python is compiled into a library. You can have a binary output as well, but the code you want to access from Python has to be in the library part. Also, make sure that the crate type is `cdylib` and add PyO3 as a dependency as so: - - -```toml -# If you already have [package] information in `Cargo.toml`, you can ignore -# this section! -[package] -# `name` here is name of the package. -name = "pyo3_start" -# these are good defaults: -version = "0.1.0" -edition = "2021" - -[lib] -# The name of the native library. This is the name which will be used in Python to import the -# library (i.e. `import string_sum`). If you change this, you must also change the name of the -# `#[pymodule]` in `src/lib.rs`. -name = "pyo3_example" - -# "cdylib" is necessary to produce a shared library for Python to import from. -crate-type = ["cdylib"] - -[dependencies] -pyo3 = { {{#PYO3_CRATE_VERSION}}, features = ["extension-module"] } -``` - -## pyproject.toml - -You should also create a `pyproject.toml` with the following contents: - -```toml -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "pyo3_example" -requires-python = ">=3.7" -classifiers = [ - "Programming Language :: Rust", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", -] -``` - -## Running code - -After this you can setup Rust code to be available in Python as below; for example, you can place this code in `src/lib.rs`: - -```rust -use pyo3::prelude::*; - -/// Formats the sum of two numbers as string. -#[pyfunction] -fn sum_as_string(a: usize, b: usize) -> PyResult { - Ok((a + b).to_string()) -} - -/// A Python module implemented in Rust. The name of this function must match -/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to -/// import the module. -#[pymodule] -fn pyo3_example(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(sum_as_string, m)?) -} -``` - -Now you can run `maturin develop` to prepare the Python package, after which you can use it like so: - -```bash -$ maturin develop -# lots of progress output as maturin runs the compilation... -$ python ->>> import pyo3_example ->>> pyo3_example.sum_as_string(5, 20) -'25' -``` - -For more instructions on how to use Python code from Rust, see the [Python from Rust](python-from-rust.md) page. - -## Maturin Import Hook - -In development, any changes in the code would require running `maturin develop` before testing. To streamline the development process, you may want to install [Maturin Import Hook](https://github.com/PyO3/maturin-import-hook) which will run `maturin develop` automatically when the library with code changes is being imported. diff --git a/vendor/pyo3/guide/src/index.md b/vendor/pyo3/guide/src/index.md deleted file mode 100644 index 4e85d25d52..0000000000 --- a/vendor/pyo3/guide/src/index.md +++ /dev/null @@ -1,15 +0,0 @@ -# The PyO3 user guide - -Welcome to the PyO3 user guide! This book is a companion to [PyO3's API docs](https://docs.rs/pyo3). It contains examples and documentation to explain all of PyO3's use cases in detail. - -The rough order of material in this user guide is as follows: - 1. Getting started - 2. Wrapping Rust code for use from Python - 3. How to use Python code from Rust - 4. Remaining topics which go into advanced concepts in detail - -Please choose from the chapters on the left to jump to individual topics, or continue below to start with PyO3's README. - -
- -{{#include ../../README.md}} diff --git a/vendor/pyo3/guide/src/migration.md b/vendor/pyo3/guide/src/migration.md deleted file mode 100644 index 8a8f3694f6..0000000000 --- a/vendor/pyo3/guide/src/migration.md +++ /dev/null @@ -1,2159 +0,0 @@ -# Migrating from older PyO3 versions - -This guide can help you upgrade code through breaking changes from one PyO3 version to the next. -For a detailed list of all changes, see the [CHANGELOG](changelog.md). - -## from 0.22.* to 0.23 -
-Click to expand - -PyO3 0.23 is a significant rework of PyO3's internals for two major improvements: - - Support of Python 3.13's new freethreaded build (aka "3.13t") - - Rework of to-Python conversions with a new `IntoPyObject` trait. - -These changes are both substantial and reasonable efforts have been made to allow as much code as possible to continue to work as-is despite the changes. The impacts are likely to be seen in three places when upgrading: - - PyO3's data structures [are now thread-safe](#free-threaded-python-support) instead of reliant on the GIL for synchronization. In particular, `#[pyclass]` types are [now required to be `Sync`](./class/thread-safety.md). - - The [`IntoPyObject` trait](#new-intopyobject-trait-unifies-to-python-conversions) may need to be implemented for types in your codebase. In most cases this can simply be done with [`#[derive(IntoPyObject)]`](#intopyobject-and-intopyobjectref-derive-macros). There will be many deprecation warnings from the replacement of `IntoPy` and `ToPyObject` traits. - - There will be many deprecation warnings from the [final removal of the `gil-refs` feature](#gil-refs-feature-removed), which opened up API space for a cleanup and simplification to PyO3's "Bound" API. - -The sections below discuss the rationale and details of each change in more depth. -
- -### Free-threaded Python Support -
-Click to expand - -PyO3 0.23 introduces initial support for the new free-threaded build of -CPython 3.13, aka "3.13t". - -Because this build allows multiple Python threads to operate simultaneously on underlying Rust data, the `#[pyclass]` macro now requires that types it operates on implement `Sync`. - -Aside from the change to `#[pyclass]`, most features of PyO3 work unchanged, as the changes have been to the internal data structures to make them thread-safe. An example of this is the `GILOnceCell` type, which used the GIL to synchronize single-initialization. It now uses internal locks to guarantee that only one write ever succeeds, however it allows for multiple racing runs of the initialization closure. It may be preferable to instead use `std::sync::OnceLock` in combination with the `pyo3::sync::OnceLockExt` trait which adds `OnceLock::get_or_init_py_attached` for single-initialization where the initialization closure is guaranteed only ever to run once and without deadlocking with the GIL. - -Future PyO3 versions will likely add more traits and data structures to make working with free-threaded Python easier. - -Some features are unaccessible on the free-threaded build: - - The `GILProtected` type, which relied on the GIL to expose synchronized access to inner contents - - `PyList::get_item_unchecked`, which cannot soundly be used due to races between time-of-check and time-of-use - -If you make use of these features then you will need to account for the -unavailability of the API in the free-threaded build. One way to handle it is via conditional compilation -- extensions can use `pyo3-build-config` to get access to a `#[cfg(Py_GIL_DISABLED)]` guard. - -See [the guide section on free-threaded Python](free-threading.md) for more details about supporting free-threaded Python in your PyO3 extensions. -
- -### New `IntoPyObject` trait unifies to-Python conversions -
-Click to expand - -PyO3 0.23 introduces a new `IntoPyObject` trait to convert Rust types into Python objects which replaces both `IntoPy` and `ToPyObject`. -Notable features of this new trait include: -- conversions can now return an error -- it is designed to work efficiently for both `T` owned types and `&T` references -- compared to `IntoPy` the generic `T` moved into an associated type, so - - there is now only one way to convert a given type - - the output type is stronger typed and may return any Python type instead of just `PyAny` -- byte collections are specialized to convert into `PyBytes` now, see [below](#to-python-conversions-changed-for-byte-collections-vecu8-u8-n-and-smallvecu8-n) -- `()` (unit) is now only specialized in return position of `#[pyfunction]` and `#[pymethods]` to return `None`, in normal usage it converts into an empty `PyTuple` - -All PyO3 provided types as well as `#[pyclass]`es already implement `IntoPyObject`. Other types will -need to adapt an implementation of `IntoPyObject` to stay compatible with the Python APIs. In many cases -the new [`#[derive(IntoPyObject)]`](#intopyobject-and-intopyobjectref-derive-macros) macro can be used instead of -[manual implementations](#intopyobject-manual-implementation). - -Since `IntoPyObject::into_pyobject` may return either a `Bound` or `Borrowed`, you may find the [`BoundObject`](conversions/traits.md#boundobject-for-conversions-that-may-be-bound-or-borrowed) trait to be useful to write code that generically handles either type of smart pointer. - -Together with the introduction of `IntoPyObject` the old conversion traits `ToPyObject` and `IntoPy` -are deprecated and will be removed in a future PyO3 version. - -#### `IntoPyObject` and `IntoPyObjectRef` derive macros - -To implement the new trait you may use the new `IntoPyObject` and `IntoPyObjectRef` derive macros as below. - -```rust -# use pyo3::prelude::*; -#[derive(IntoPyObject, IntoPyObjectRef)] -struct Struct { - count: usize, - obj: Py, -} -``` - -The `IntoPyObjectRef` derive macro derives implementations for references (e.g. for `&Struct` in the example above), which is a replacement for the `ToPyObject` trait. - -#### `IntoPyObject` manual implementation - -Before: -```rust,ignore -# use pyo3::prelude::*; -# #[allow(dead_code)] -struct MyPyObjectWrapper(PyObject); - -impl IntoPy for MyPyObjectWrapper { - fn into_py(self, py: Python<'_>) -> PyObject { - self.0 - } -} - -impl ToPyObject for MyPyObjectWrapper { - fn to_object(&self, py: Python<'_>) -> PyObject { - self.0.clone_ref(py) - } -} -``` - -After: -```rust -# use pyo3::prelude::*; -# #[allow(dead_code)] -# struct MyPyObjectWrapper(PyObject); - -impl<'py> IntoPyObject<'py> for MyPyObjectWrapper { - type Target = PyAny; // the Python type - type Output = Bound<'py, Self::Target>; // in most cases this will be `Bound` - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.0.into_bound(py)) - } -} - -// `ToPyObject` implementations should be converted to implementations on reference types -impl<'a, 'py> IntoPyObject<'py> for &'a MyPyObjectWrapper { - type Target = PyAny; - type Output = Borrowed<'a, 'py, Self::Target>; // `Borrowed` can be used to optimized reference counting - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.0.bind_borrowed(py)) - } -} -``` -
- -### To-Python conversions changed for byte collections (`Vec`, `[u8; N]` and `SmallVec<[u8; N]>`). -
-Click to expand - -With the introduction of the `IntoPyObject` trait, PyO3's macros now prefer `IntoPyObject` implementations over `IntoPy` when producing Python values. This applies to `#[pyfunction]` and `#[pymethods]` return values and also fields accessed via `#[pyo3(get)]`. - -This change has an effect on functions and methods returning _byte_ collections like -- `Vec` -- `[u8; N]` -- `SmallVec<[u8; N]>` - -In their new `IntoPyObject` implementation these will now turn into `PyBytes` rather than a -`PyList`. All other `T`s are unaffected and still convert into a `PyList`. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyfunction] -fn foo() -> Vec { // would previously turn into a `PyList`, now `PyBytes` - vec![0, 1, 2, 3] -} - -#[pyfunction] -fn bar() -> Vec { // unaffected, returns `PyList` - vec![0, 1, 2, 3] -} -``` - -If this conversion is _not_ desired, consider building a list manually using `PyList::new`. - -The following types were previously _only_ implemented for `u8` and now allow other `T`s turn into `PyList`: -- `&[T]` -- `Cow<[T]>` - -This is purely additional and should just extend the possible return types. - -
- -### `gil-refs` feature removed -
-Click to expand - -PyO3 0.23 completes the removal of the "GIL Refs" API in favour of the new "Bound" API introduced in PyO3 0.21. - -With the removal of the old API, many "Bound" API functions which had been introduced with `_bound` suffixes no longer need the suffixes as these names have been freed up. For example, `PyTuple::new_bound` is now just `PyTuple::new` (the existing name remains but is deprecated). - -Before: - -```rust -# #![allow(deprecated)] -# use pyo3::prelude::*; -# use pyo3::types::PyTuple; -# fn main() { -# Python::with_gil(|py| { -// For example, for PyTuple. Many such APIs have been changed. -let tup = PyTuple::new_bound(py, [1, 2, 3]); -# }) -# } -``` - -After: - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyTuple; -# fn main() { -# Python::with_gil(|py| { -// For example, for PyTuple. Many such APIs have been changed. -let tup = PyTuple::new(py, [1, 2, 3]); -# }) -# } -``` - -#### `IntoPyDict` trait adjusted for removal of `gil-refs` - -As part of this API simplification, the `IntoPyDict` trait has had a small breaking change: `IntoPyDict::into_py_dict_bound` method has been renamed to `IntoPyDict::into_py_dict`. It is also now fallible as part of the `IntoPyObject` trait addition. - -If you implemented `IntoPyDict` for your type, you should implement `into_py_dict` instead of `into_py_dict_bound`. The old name is still available for calling but deprecated. - -Before: - -```rust,ignore -# use pyo3::prelude::*; -# use pyo3::types::{PyDict, IntoPyDict}; -# use std::collections::HashMap; - -struct MyMap(HashMap); - -impl IntoPyDict for MyMap -where - K: ToPyObject, - V: ToPyObject, -{ - fn into_py_dict_bound(self, py: Python<'_>) -> Bound<'_, PyDict> { - let dict = PyDict::new_bound(py); - for (key, value) in self.0 { - dict.set_item(key, value) - .expect("Failed to set_item on dict"); - } - dict - } -} -``` - -After: - -```rust -# use pyo3::prelude::*; -# use pyo3::types::{PyDict, IntoPyDict}; -# use std::collections::HashMap; - -# #[allow(dead_code)] -struct MyMap(HashMap); - -impl<'py, K, V> IntoPyDict<'py> for MyMap -where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>, -{ - fn into_py_dict(self, py: Python<'py>) -> PyResult> { - let dict = PyDict::new(py); - for (key, value) in self.0 { - dict.set_item(key, value)?; - } - Ok(dict) - } -} -``` -
- -## from 0.21.* to 0.22 - -### Deprecation of `gil-refs` feature continues -
-Click to expand - -Following the introduction of the "Bound" API in PyO3 0.21 and the planned removal of the "GIL Refs" API, all functionality related to GIL Refs is now gated behind the `gil-refs` feature and emits a deprecation warning on use. - -See the 0.21 migration entry for help upgrading. -
- -### Deprecation of implicit default for trailing optional arguments -
-Click to expand - -With `pyo3` 0.22 the implicit `None` default for trailing `Option` type argument is deprecated. To migrate, place a `#[pyo3(signature = (...))]` attribute on affected functions or methods and specify the desired behavior. -The migration warning specifies the corresponding signature to keep the current behavior. With 0.23 the signature will be required for any function containing `Option` type parameters to prevent accidental -and unnoticed changes in behavior. With 0.24 this restriction will be lifted again and `Option` type arguments will be treated as any other argument _without_ special handling. - -Before: - -```rust -# #![allow(deprecated, dead_code)] -# use pyo3::prelude::*; -#[pyfunction] -fn increment(x: u64, amount: Option) -> u64 { - x + amount.unwrap_or(1) -} -``` - -After: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyfunction] -#[pyo3(signature = (x, amount=None))] -fn increment(x: u64, amount: Option) -> u64 { - x + amount.unwrap_or(1) -} -``` -
- -### `Py::clone` is now gated behind the `py-clone` feature -
-Click to expand -If you rely on `impl Clone for Py` to fulfil trait requirements imposed by existing Rust code written without PyO3-based code in mind, the newly introduced feature `py-clone` must be enabled. - -However, take care to note that the behaviour is different from previous versions. If `Clone` was called without the GIL being held, we tried to delay the application of these reference count increments until PyO3-based code would re-acquire it. This turned out to be impossible to implement in a sound manner and hence was removed. Now, if `Clone` is called without the GIL being held, we panic instead for which calling code might not be prepared. - -It is advised to migrate off the `py-clone` feature. The simplest way to remove dependency on `impl Clone for Py` is to wrap `Py` as `Arc>` and use cloning of the arc. - -Related to this, we also added a `pyo3_disable_reference_pool` conditional compilation flag which removes the infrastructure necessary to apply delayed reference count decrements implied by `impl Drop for Py`. They do not appear to be a soundness hazard as they should lead to memory leaks in the worst case. However, the global synchronization adds significant overhead to cross the Python-Rust boundary. Enabling this feature will remove these costs and make the `Drop` implementation abort the process if called without the GIL being held instead. -
- -### Require explicit opt-in for comparison for simple enums -
-Click to expand - -With `pyo3` 0.22 the new `#[pyo3(eq)]` options allows automatic implementation of Python equality using Rust's `PartialEq`. Previously simple enums automatically implemented equality in terms of their discriminants. To make PyO3 more consistent, this automatic equality implementation is deprecated in favour of having opt-ins for all `#[pyclass]` types. Similarly, simple enums supported comparison with integers, which is not covered by Rust's `PartialEq` derive, so has been split out into the `#[pyo3(eq_int)]` attribute. - -To migrate, place a `#[pyo3(eq, eq_int)]` attribute on simple enum classes. - -Before: - -```rust -# #![allow(deprecated, dead_code)] -# use pyo3::prelude::*; -#[pyclass] -enum SimpleEnum { - VariantA, - VariantB = 42, -} -``` - -After: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum SimpleEnum { - VariantA, - VariantB = 42, -} -``` -
- -### `PyType::name` reworked to better match Python `__name__` -
-Click to expand - -This function previously would try to read directly from Python type objects' C API field (`tp_name`), in which case it -would return a `Cow::Borrowed`. However the contents of `tp_name` don't have well-defined semantics. - -Instead `PyType::name()` now returns the equivalent of Python `__name__` and returns `PyResult>`. - -The closest equivalent to PyO3 0.21's version of `PyType::name()` has been introduced as a new function `PyType::fully_qualified_name()`, -which is equivalent to `__module__` and `__qualname__` joined as `module.qualname`. - -Before: - -```rust,ignore -# #![allow(deprecated, dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::{PyBool}; -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - let bool_type = py.get_type_bound::(); - let name = bool_type.name()?.into_owned(); - println!("Hello, {}", name); - - let mut name_upper = bool_type.name()?; - name_upper.to_mut().make_ascii_uppercase(); - println!("Hello, {}", name_upper); - - Ok(()) -}) -# } -``` - -After: - -```rust,ignore -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::{PyBool}; -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - let bool_type = py.get_type_bound::(); - let name = bool_type.name()?; - println!("Hello, {}", name); - - // (if the full dotted path was desired, switch from `name()` to `fully_qualified_name()`) - let mut name_upper = bool_type.fully_qualified_name()?.to_string(); - name_upper.make_ascii_uppercase(); - println!("Hello, {}", name_upper); - - Ok(()) -}) -# } -``` -
- - - -## from 0.20.* to 0.21 -
-Click to expand - -PyO3 0.21 introduces a new `Bound<'py, T>` smart pointer which replaces the existing "GIL Refs" API to interact with Python objects. For example, in PyO3 0.20 the reference `&'py PyAny` would be used to interact with Python objects. In PyO3 0.21 the updated type is `Bound<'py, PyAny>`. Making this change moves Rust ownership semantics out of PyO3's internals and into user code. This change fixes [a known soundness edge case of interaction with gevent](https://github.com/PyO3/pyo3/issues/3668) as well as improves CPU and [memory performance](https://github.com/PyO3/pyo3/issues/1056). For a full history of discussion see https://github.com/PyO3/pyo3/issues/3382. - -The "GIL Ref" `&'py PyAny` and similar types such as `&'py PyDict` continue to be available as a deprecated API. Due to the advantages of the new API it is advised that all users make the effort to upgrade as soon as possible. - -In addition to the major API type overhaul, PyO3 has needed to make a few small breaking adjustments to other APIs to close correctness and soundness gaps. - -The recommended steps to update to PyO3 0.21 is as follows: - 1. Enable the `gil-refs` feature to silence deprecations related to the API change - 2. Fix all other PyO3 0.21 migration steps - 3. Disable the `gil-refs` feature and migrate off the deprecated APIs - -The following sections are laid out in this order. -
- -### Enable the `gil-refs` feature -
-Click to expand - -To make the transition for the PyO3 ecosystem away from the GIL Refs API as smooth as possible, in PyO3 0.21 no APIs consuming or producing GIL Refs have been altered. Instead, variants using `Bound` smart pointers have been introduced, for example `PyTuple::new_bound` which returns `Bound` is the replacement form of `PyTuple::new`. The GIL Ref APIs have been deprecated, but to make migration easier it is possible to disable these deprecation warnings by enabling the `gil-refs` feature. - -> The one single exception where an existing API was changed in-place is the `pyo3::intern!` macro. Almost all uses of this macro did not need to update code to account it changing to return `&Bound` immediately, and adding an `intern_bound!` replacement was perceived as adding more work for users. - -It is recommended that users do this as a first step of updating to PyO3 0.21 so that the deprecation warnings do not get in the way of resolving the rest of the migration steps. - -Before: - -```toml -# Cargo.toml -[dependencies] -pyo3 = "0.20" -``` - -After: - -```toml -# Cargo.toml -[dependencies] -pyo3 = { version = "0.21", features = ["gil-refs"] } -``` -
- -### `PyTypeInfo` and `PyTryFrom` have been adjusted -
-Click to expand - -The `PyTryFrom` trait has aged poorly, its `try_from` method now conflicts with `TryFrom::try_from` in the 2021 edition prelude. A lot of its functionality was also duplicated with `PyTypeInfo`. - -To tighten up the PyO3 traits as part of the deprecation of the GIL Refs API the `PyTypeInfo` trait has had a simpler companion `PyTypeCheck`. The methods `PyAny::downcast` and `PyAny::downcast_exact` no longer use `PyTryFrom` as a bound, instead using `PyTypeCheck` and `PyTypeInfo` respectively. - -To migrate, switch all type casts to use `obj.downcast()` instead of `try_from(obj)` (and similar for `downcast_exact`). - -Before: - -```rust,ignore -# #![allow(deprecated)] -# use pyo3::prelude::*; -# use pyo3::types::{PyInt, PyList}; -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - let list = PyList::new(py, 0..5); - let b = ::try_from(list.get_item(0).unwrap())?; - Ok(()) -}) -# } -``` - -After: - -```rust,ignore -# use pyo3::prelude::*; -# use pyo3::types::{PyInt, PyList}; -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - // Note that PyList::new is deprecated for PyList::new_bound as part of the GIL Refs API removal, - // see the section below on migration to Bound. - #[allow(deprecated)] - let list = PyList::new(py, 0..5); - let b = list.get_item(0).unwrap().downcast::()?; - Ok(()) -}) -# } -``` -
- -### `Iter(A)NextOutput` are deprecated -
-Click to expand - -The `__next__` and `__anext__` magic methods can now return any type convertible into Python objects directly just like all other `#[pymethods]`. The `IterNextOutput` used by `__next__` and `IterANextOutput` used by `__anext__` are subsequently deprecated. Most importantly, this change allows returning an awaitable from `__anext__` without non-sensically wrapping it into `Yield` or `Some`. Only the return types `Option` and `Result, E>` are still handled in a special manner where `Some(val)` yields `val` and `None` stops iteration. - -Starting with an implementation of a Python iterator using `IterNextOutput`, e.g. - -```rust,ignore -use pyo3::prelude::*; -use pyo3::iter::IterNextOutput; - -#[pyclass] -struct PyClassIter { - count: usize, -} - -#[pymethods] -impl PyClassIter { - fn __next__(&mut self) -> IterNextOutput { - if self.count < 5 { - self.count += 1; - IterNextOutput::Yield(self.count) - } else { - IterNextOutput::Return("done") - } - } -} -``` - -If returning `"done"` via `StopIteration` is not really required, this should be written as - -```rust -use pyo3::prelude::*; - -#[pyclass] -struct PyClassIter { - count: usize, -} - -#[pymethods] -impl PyClassIter { - fn __next__(&mut self) -> Option { - if self.count < 5 { - self.count += 1; - Some(self.count) - } else { - None - } - } -} -``` - -This form also has additional benefits: It has already worked in previous PyO3 versions, it matches the signature of Rust's [`Iterator` trait](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html) and it allows using a fast path in CPython which completely avoids the cost of raising a `StopIteration` exception. Note that using [`Option::transpose`](https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose) and the `Result, E>` variant, this form can also be used to wrap fallible iterators. - -Alternatively, the implementation can also be done as it would in Python itself, i.e. by "raising" a `StopIteration` exception - -```rust -use pyo3::prelude::*; -use pyo3::exceptions::PyStopIteration; - -#[pyclass] -struct PyClassIter { - count: usize, -} - -#[pymethods] -impl PyClassIter { - fn __next__(&mut self) -> PyResult { - if self.count < 5 { - self.count += 1; - Ok(self.count) - } else { - Err(PyStopIteration::new_err("done")) - } - } -} -``` - -Finally, an asynchronous iterator can directly return an awaitable without confusing wrapping - -```rust -use pyo3::prelude::*; - -#[pyclass] -struct PyClassAwaitable { - number: usize, -} - -#[pymethods] -impl PyClassAwaitable { - fn __next__(&self) -> usize { - self.number - } - - fn __await__(slf: Py) -> Py { - slf - } -} - -#[pyclass] -struct PyClassAsyncIter { - number: usize, -} - -#[pymethods] -impl PyClassAsyncIter { - fn __anext__(&mut self) -> PyClassAwaitable { - self.number += 1; - PyClassAwaitable { - number: self.number, - } - } - - fn __aiter__(slf: Py) -> Py { - slf - } -} -``` -
- -### `PyType::name` has been renamed to `PyType::qualname` -
-Click to expand - -`PyType::name` has been renamed to `PyType::qualname` to indicate that it does indeed return the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name), matching the `__qualname__` attribute. The newly added `PyType::name` yields the full name including the module name now which corresponds to `__module__.__name__` on the level of attributes. -
- -### `PyCell` has been deprecated -
-Click to expand - -Interactions with Python objects implemented in Rust no longer need to go though `PyCell`. Instead iteractions with Python object now consistently go through `Bound` or `Py` independently of whether `T` is native Python object or a `#[pyclass]` implemented in Rust. Use `Bound::new` or `Py::new` respectively to create and `Bound::borrow(_mut)` / `Py::borrow(_mut)` to borrow the Rust object. -
- -### Migrating from the GIL Refs API to `Bound` -
-Click to expand - -To minimise breakage of code using the GIL Refs API, the `Bound` smart pointer has been introduced by adding complements to all functions which accept or return GIL Refs. This allows code to migrate by replacing the deprecated APIs with the new ones. - -To identify what to migrate, temporarily switch off the `gil-refs` feature to see deprecation warnings on [almost](#cases-where-pyo3-cannot-emit-gil-ref-deprecation-warnings) all uses of APIs accepting and producing GIL Refs . Over one or more PRs it should be possible to follow the deprecation hints to update code. Depending on your development environment, switching off the `gil-refs` feature may introduce [some very targeted breakages](#deactivating-the-gil-refs-feature), so you may need to fixup those first. - -For example, the following APIs have gained updated variants: -- `PyList::new`, `PyTyple::new` and similar constructors have replacements `PyList::new_bound`, `PyTuple::new_bound` etc. -- `FromPyObject::extract` has a new `FromPyObject::extract_bound` (see the section below) -- The `PyTypeInfo` trait has had new `_bound` methods added to accept / return `Bound`. - -Because the new `Bound` API brings ownership out of the PyO3 framework and into user code, there are a few places where user code is expected to need to adjust while switching to the new API: -- Code will need to add the occasional `&` to borrow the new smart pointer as `&Bound` to pass these types around (or use `.clone()` at the very small cost of increasing the Python reference count) -- `Bound` and `Bound` cannot support indexing with `list[0]`, you should use `list.get_item(0)` instead. -- `Bound::iter_borrowed` is slightly more efficient than `Bound::iter`. The default iteration of `Bound` cannot return borrowed references because Rust does not (yet) have "lending iterators". Similarly `Bound::get_borrowed_item` is more efficient than `Bound::get_item` for the same reason. -- `&Bound` does not implement `FromPyObject` (although it might be possible to do this in the future once the GIL Refs API is completely removed). Use `bound_any.downcast::()` instead of `bound_any.extract::<&Bound>()`. -- `Bound::to_str` now borrows from the `Bound` rather than from the `'py` lifetime, so code will need to store the smart pointer as a value in some cases where previously `&PyString` was just used as a temporary. (There are some more details relating to this in [the section below](#deactivating-the-gil-refs-feature).) -- `.extract::<&str>()` now borrows from the source Python object. The simplest way to update is to change to `.extract::()`, which retains ownership of the Python reference. See more information [in the section on deactivating the `gil-refs` feature](#deactivating-the-gil-refs-feature). - -To convert between `&PyAny` and `&Bound` use the `as_borrowed()` method: - -```rust,ignore -let gil_ref: &PyAny = ...; -let bound: &Bound = &gil_ref.as_borrowed(); -``` - -To convert between `Py` and `Bound` use the `bind()` / `into_bound()` methods, and `as_unbound()` / `unbind()` to go back from `Bound` to `Py`. - -```rust,ignore -let obj: Py = ...; -let bound: &Bound<'py, PyList> = obj.bind(py); -let bound: Bound<'py, PyList> = obj.into_bound(py); - -let obj: &Py = bound.as_unbound(); -let obj: Py = bound.unbind(); -``` - -
- -⚠️ Warning: dangling pointer trap 💣 - -> Because of the ownership changes, code which uses `.as_ptr()` to convert `&PyAny` and other GIL Refs to a `*mut pyo3_ffi::PyObject` should take care to avoid creating dangling pointers now that `Bound` carries ownership. -> -> For example, the following pattern with `Option<&PyAny>` can easily create a dangling pointer when migrating to the `Bound` smart pointer: -> -> ```rust,ignore -> let opt: Option<&PyAny> = ...; -> let p: *mut ffi::PyObject = opt.map_or(std::ptr::null_mut(), |any| any.as_ptr()); -> ``` -> -> The correct way to migrate this code is to use `.as_ref()` to avoid dropping the `Bound` in the `map_or` closure: -> -> ```rust,ignore -> let opt: Option> = ...; -> let p: *mut ffi::PyObject = opt.as_ref().map_or(std::ptr::null_mut(), Bound::as_ptr); -> ``` -
- -#### Migrating `FromPyObject` implementations - -`FromPyObject` has had a new method `extract_bound` which takes `&Bound<'py, PyAny>` as an argument instead of `&PyAny`. Both `extract` and `extract_bound` have been given default implementations in terms of the other, to avoid breaking code immediately on update to 0.21. - -All implementations of `FromPyObject` should be switched from `extract` to `extract_bound`. - -Before: - -```rust,ignore -impl<'py> FromPyObject<'py> for MyType { - fn extract(obj: &'py PyAny) -> PyResult { - /* ... */ - } -} -``` - -After: - -```rust,ignore -impl<'py> FromPyObject<'py> for MyType { - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - /* ... */ - } -} -``` - -The expectation is that in 0.22 `extract_bound` will have the default implementation removed and in 0.23 `extract` will be removed. - -#### Cases where PyO3 cannot emit GIL Ref deprecation warnings - -Despite a large amount of deprecations warnings produced by PyO3 to aid with the transition from GIL Refs to the Bound API, there are a few cases where PyO3 cannot automatically warn on uses of GIL Refs. It is worth checking for these cases manually after the deprecation warnings have all been addressed: - -- Individual implementations of the `FromPyObject` trait cannot be deprecated, so PyO3 cannot warn about uses of code patterns like `.extract<&PyAny>()` which produce a GIL Ref. -- GIL Refs in `#[pyfunction]` arguments emit a warning, but if the GIL Ref is wrapped inside another container such as `Vec<&PyAny>` then PyO3 cannot warn against this. -- The `wrap_pyfunction!(function)(py)` deferred argument form of the `wrap_pyfunction` macro taking `py: Python<'py>` produces a GIL Ref, and due to limitations in type inference PyO3 cannot warn against this specific case. - -
- -### Deactivating the `gil-refs` feature -
-Click to expand - -As a final step of migration, deactivating the `gil-refs` feature will set up code for best performance and is intended to set up a forward-compatible API for PyO3 0.22. - -At this point code that needed to manage GIL Ref memory can safely remove uses of `GILPool` (which are constructed by calls to `Python::new_pool` and `Python::with_pool`). Deprecation warnings will highlight these cases. - -There is just one case of code that changes upon disabling these features: `FromPyObject` trait implementations for types that borrow directly from the input data cannot be implemented by PyO3 without GIL Refs (while the GIL Refs API is in the process of being removed). The main types affected are `&str`, `Cow<'_, str>`, `&[u8]`, `Cow<'_, u8>`. - -To make PyO3's core functionality continue to work while the GIL Refs API is in the process of being removed, disabling the `gil-refs` feature moves the implementations of `FromPyObject` for `&str`, `Cow<'_, str>`, `&[u8]`, `Cow<'_, u8>` to a new temporary trait `FromPyObjectBound`. This trait is the expected future form of `FromPyObject` and has an additional lifetime `'a` to enable these types to borrow data from Python objects. - -PyO3 0.21 has introduced the [`PyBackedStr`]({{#PYO3_DOCS_URL}}/pyo3/pybacked/struct.PyBackedStr.html) and [`PyBackedBytes`]({{#PYO3_DOCS_URL}}/pyo3/pybacked/struct.PyBackedBytes.html) types to help with this case. The easiest way to avoid lifetime challenges from extracting `&str` is to use these. For more complex types like `Vec<&str>`, is now impossible to extract directly from a Python object and `Vec` is the recommended upgrade path. - -A key thing to note here is because extracting to these types now ties them to the input lifetime, some extremely common patterns may need to be split into multiple Rust lines. For example, the following snippet of calling `.extract::<&str>()` directly on the result of `.getattr()` needs to be adjusted when deactivating the `gil-refs` feature. - -Before: - -```rust,ignore -# #[cfg(feature = "gil-refs")] { -# use pyo3::prelude::*; -# use pyo3::types::{PyList, PyType}; -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -#[allow(deprecated)] // GIL Ref API -let obj: &'py PyType = py.get_type::(); -let name: &'py str = obj.getattr("__name__")?.extract()?; -assert_eq!(name, "list"); -# Ok(()) -# } -# Python::with_gil(example).unwrap(); -# } -``` - -After: - -```rust,ignore -# #[cfg(any(not(Py_LIMITED_API), Py_3_10))] { -# use pyo3::prelude::*; -# use pyo3::types::{PyList, PyType}; -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -let obj: Bound<'py, PyType> = py.get_type_bound::(); -let name_obj: Bound<'py, PyAny> = obj.getattr("__name__")?; -// the lifetime of the data is no longer `'py` but the much shorter -// lifetime of the `name_obj` smart pointer above -let name: &'_ str = name_obj.extract()?; -assert_eq!(name, "list"); -# Ok(()) -# } -# Python::with_gil(example).unwrap(); -# } -``` - -To avoid needing to worry about lifetimes at all, it is also possible to use the new `PyBackedStr` type, which stores a reference to the Python `str` without a lifetime attachment. In particular, `PyBackedStr` helps for `abi3` builds for Python older than 3.10. Due to limitations in the `abi3` CPython API for those older versions, PyO3 cannot offer a `FromPyObjectBound` implementation for `&str` on those versions. The easiest way to migrate for older `abi3` builds is to replace any cases of `.extract::<&str>()` with `.extract::()`. Alternatively, use `.extract::>()`, `.extract::()` to copy the data into Rust. - -The following example uses the same snippet as those just above, but this time the final extracted type is `PyBackedStr`: - -```rust,ignore -# use pyo3::prelude::*; -# use pyo3::types::{PyList, PyType}; -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -use pyo3::pybacked::PyBackedStr; -let obj: Bound<'py, PyType> = py.get_type_bound::(); -let name: PyBackedStr = obj.getattr("__name__")?.extract()?; -assert_eq!(&*name, "list"); -# Ok(()) -# } -# Python::with_gil(example).unwrap(); -``` -
- -## from 0.19.* to 0.20 - -### Drop support for older technologies -
-Click to expand - -PyO3 0.20 has increased minimum Rust version to 1.56. This enables use of newer language features and simplifies maintenance of the project. -
- -### `PyDict::get_item` now returns a `Result` -
-Click to expand - -`PyDict::get_item` in PyO3 0.19 and older was implemented using a Python API which would suppress all exceptions and return `None` in those cases. This included errors in `__hash__` and `__eq__` implementations of the key being looked up. - -Newer recommendations by the Python core developers advise against using these APIs which suppress exceptions, instead allowing exceptions to bubble upwards. `PyDict::get_item_with_error` already implemented this recommended behavior, so that API has been renamed to `PyDict::get_item`. - - -Before: - -```rust,ignore -use pyo3::prelude::*; -use pyo3::exceptions::PyTypeError; -use pyo3::types::{PyDict, IntoPyDict}; - -# fn main() { -# let _ = -Python::with_gil(|py| { - let dict: &PyDict = [("a", 1)].into_py_dict(py); - // `a` is in the dictionary, with value 1 - assert!(dict.get_item("a").map_or(Ok(false), |x| x.eq(1))?); - // `b` is not in the dictionary - assert!(dict.get_item("b").is_none()); - // `dict` is not hashable, so this fails with a `TypeError` - assert!(dict - .get_item_with_error(dict) - .unwrap_err() - .is_instance_of::(py)); -}); -# } -``` - -After: - -```rust,ignore -use pyo3::prelude::*; -use pyo3::exceptions::PyTypeError; -use pyo3::types::{PyDict, IntoPyDict}; - -# fn main() { -# let _ = -Python::with_gil(|py| -> PyResult<()> { - let dict: &PyDict = [("a", 1)].into_py_dict(py); - // `a` is in the dictionary, with value 1 - assert!(dict.get_item("a")?.map_or(Ok(false), |x| x.eq(1))?); - // `b` is not in the dictionary - assert!(dict.get_item("b")?.is_none()); - // `dict` is not hashable, so this fails with a `TypeError` - assert!(dict - .get_item(dict) - .unwrap_err() - .is_instance_of::(py)); - - Ok(()) -}); -# } -``` -
- -### Required arguments are no longer accepted after optional arguments -
-Click to expand - -[Trailing `Option` arguments](./function/signature.md#trailing-optional-arguments) have an automatic default of `None`. To avoid unwanted changes when modifying function signatures, in PyO3 0.18 it was deprecated to have a required argument after an `Option` argument without using `#[pyo3(signature = (...))]` to specify the intended defaults. In PyO3 0.20, this becomes a hard error. - -Before: - -```rust,ignore -#[pyfunction] -fn x_or_y(x: Option, y: u64) -> u64 { - x.unwrap_or(y) -} -``` - -After: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; - -#[pyfunction] -#[pyo3(signature = (x, y))] // both x and y have no defaults and are required -fn x_or_y(x: Option, y: u64) -> u64 { - x.unwrap_or(y) -} -``` -
- -### Remove deprecated function forms -
-Click to expand - -In PyO3 0.18 the `#[args]` attribute for `#[pymethods]`, and directly specifying the function signature in `#[pyfunction]`, was deprecated. This functionality has been removed in PyO3 0.20. - -Before: - -```rust,ignore -#[pyfunction] -#[pyo3(a, b = "0", "/")] -fn add(a: u64, b: u64) -> u64 { - a + b -} -``` - -After: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; - -#[pyfunction] -#[pyo3(signature = (a, b=0, /))] -fn add(a: u64, b: u64) -> u64 { - a + b -} -``` - -
- -### `IntoPyPointer` trait removed -
-Click to expand - -The trait `IntoPyPointer`, which provided the `into_ptr` method on many types, has been removed. `into_ptr` is now available as an inherent method on all types that previously implemented this trait. -
- -### `AsPyPointer` now `unsafe` trait -
-Click to expand - -The trait `AsPyPointer` is now `unsafe trait`, meaning any external implementation of it must be marked as `unsafe impl`, and ensure that they uphold the invariant of returning valid pointers. -
- -## from 0.18.* to 0.19 - -### Access to `Python` inside `__traverse__` implementations are now forbidden -
-Click to expand - -During `__traverse__` implementations for Python's Garbage Collection it is forbidden to do anything other than visit the members of the `#[pyclass]` being traversed. This means making Python function calls or other API calls are forbidden. - -Previous versions of PyO3 would allow access to `Python` (e.g. via `Python::with_gil`), which could cause the Python interpreter to crash or otherwise confuse the garbage collection algorithm. - -Attempts to acquire the GIL will now panic. See [#3165](https://github.com/PyO3/pyo3/issues/3165) for more detail. - -```rust,ignore -# use pyo3::prelude::*; - -#[pyclass] -struct SomeClass {} - -impl SomeClass { - fn __traverse__(&self, pyo3::class::gc::PyVisit<'_>) -> Result<(), pyo3::class::gc::PyTraverseError>` { - Python::with_gil(|| { /*...*/ }) // ERROR: this will panic - } -} -``` -
- -### Smarter `anyhow::Error` / `eyre::Report` conversion when inner error is "simple" `PyErr` -
-Click to expand - -When converting from `anyhow::Error` or `eyre::Report` to `PyErr`, if the inner error is a "simple" `PyErr` (with no source error), then the inner error will be used directly as the `PyErr` instead of wrapping it in a new `PyRuntimeError` with the original information converted into a string. - -```rust,ignore -# #[cfg(feature = "anyhow")] -# #[allow(dead_code)] -# mod anyhow_only { -# use pyo3::prelude::*; -# use pyo3::exceptions::PyValueError; -#[pyfunction] -fn raise_err() -> anyhow::Result<()> { - Err(PyValueError::new_err("original error message").into()) -} - -fn main() { - Python::with_gil(|py| { - let rs_func = wrap_pyfunction!(raise_err, py).unwrap(); - pyo3::py_run!( - py, - rs_func, - r" - try: - rs_func() - except Exception as e: - print(repr(e)) - " - ); - }) -} -# } -``` - -Before, the above code would have printed `RuntimeError('ValueError: original error message')`, which might be confusing. - -After, the same code will print `ValueError: original error message`, which is more straightforward. - -However, if the `anyhow::Error` or `eyre::Report` has a source, then the original exception will still be wrapped in a `PyRuntimeError`. -
- -### The deprecated `Python::acquire_gil` was removed and `Python::with_gil` must be used instead -
-Click to expand - -While the API provided by [`Python::acquire_gil`](https://docs.rs/pyo3/0.18.3/pyo3/marker/struct.Python.html#method.acquire_gil) seems convenient, it is somewhat brittle as the design of the GIL token [`Python`](https://docs.rs/pyo3/0.18.3/pyo3/marker/struct.Python.html) relies on proper nesting and panics if not used correctly, e.g. - -```rust,ignore -# #![allow(dead_code, deprecated)] -# use pyo3::prelude::*; - -#[pyclass] -struct SomeClass {} - -struct ObjectAndGuard { - object: Py, - guard: GILGuard, -} - -impl ObjectAndGuard { - fn new() -> Self { - let guard = Python::acquire_gil(); - let object = Py::new(guard.python(), SomeClass {}).unwrap(); - - Self { object, guard } - } -} - -let first = ObjectAndGuard::new(); -let second = ObjectAndGuard::new(); -// Panics because the guard within `second` is still alive. -drop(first); -drop(second); -``` - -The replacement is [`Python::with_gil`](https://docs.rs/pyo3/0.18.3/pyo3/marker/struct.Python.html#method.with_gil) which is more cumbersome but enforces the proper nesting by design, e.g. - -```rust,ignore -# #![allow(dead_code)] -# use pyo3::prelude::*; - -#[pyclass] -struct SomeClass {} - -struct Object { - object: Py, -} - -impl Object { - fn new(py: Python<'_>) -> Self { - let object = Py::new(py, SomeClass {}).unwrap(); - - Self { object } - } -} - -// It either forces us to release the GIL before aquiring it again. -let first = Python::with_gil(|py| Object::new(py)); -let second = Python::with_gil(|py| Object::new(py)); -drop(first); -drop(second); - -// Or it ensures releasing the inner lock before the outer one. -Python::with_gil(|py| { - let first = Object::new(py); - let second = Python::with_gil(|py| Object::new(py)); - drop(first); - drop(second); -}); -``` - -Furthermore, `Python::acquire_gil` provides ownership of a `GILGuard` which can be freely stored and passed around. This is usually not helpful as it may keep the lock held for a long time thereby blocking progress in other parts of the program. Due to the generative lifetime attached to the GIL token supplied by `Python::with_gil`, the problem is avoided as the GIL token can only be passed down the call chain. Often, this issue can also be avoided entirely as any GIL-bound reference `&'py PyAny` implies access to a GIL token `Python<'py>` via the [`PyAny::py`](https://docs.rs/pyo3/0.22.5/pyo3/types/struct.PyAny.html#method.py) method. -
- -## from 0.17.* to 0.18 - -### Required arguments after `Option<_>` arguments will no longer be automatically inferred -
-Click to expand - -In `#[pyfunction]` and `#[pymethods]`, if a "required" function input such as `i32` came after an `Option<_>` input, then the `Option<_>` would be implicitly treated as required. (All trailing `Option<_>` arguments were treated as optional with a default value of `None`). - -Starting with PyO3 0.18, this is deprecated and a future PyO3 version will require a [`#[pyo3(signature = (...))]` option](./function/signature.md) to explicitly declare the programmer's intention. - -Before, x in the below example would be required to be passed from Python code: - -```rust,compile_fail -# #![allow(dead_code)] -# use pyo3::prelude::*; - -#[pyfunction] -fn required_argument_after_option(x: Option, y: i32) {} -``` - -After, specify the intended Python signature explicitly: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; - -// If x really was intended to be required -#[pyfunction(signature = (x, y))] -fn required_argument_after_option_a(x: Option, y: i32) {} - -// If x was intended to be optional, y needs a default too -#[pyfunction(signature = (x=None, y=0))] -fn required_argument_after_option_b(x: Option, y: i32) {} -``` -
- -### `__text_signature__` is now automatically generated for `#[pyfunction]` and `#[pymethods]` -
-Click to expand - -The [`#[pyo3(text_signature = "...")]` option](./function/signature.md#making-the-function-signature-available-to-python) was previously the only supported way to set the `__text_signature__` attribute on generated Python functions. - -PyO3 is now able to automatically populate `__text_signature__` for all functions automatically based on their Rust signature (or the [new `#[pyo3(signature = (...))]` option](./function/signature.md)). These automatically-generated `__text_signature__` values will currently only render `...` for all default values. Many `#[pyo3(text_signature = "...")]` options can be removed from functions when updating to PyO3 0.18, however in cases with default values a manual implementation may still be preferred for now. - -As examples: - -```rust -# use pyo3::prelude::*; - -// The `text_signature` option here is no longer necessary, as PyO3 will automatically -// generate exactly the same value. -#[pyfunction(text_signature = "(a, b, c)")] -fn simple_function(a: i32, b: i32, c: i32) {} - -// The `text_signature` still provides value here as of PyO3 0.18, because the automatically -// generated signature would be "(a, b=..., c=...)". -#[pyfunction(signature = (a, b = 1, c = 2), text_signature = "(a, b=1, c=2)")] -fn function_with_defaults(a: i32, b: i32, c: i32) {} - -# fn main() { -# Python::with_gil(|py| { -# let simple = wrap_pyfunction!(simple_function, py).unwrap(); -# assert_eq!(simple.getattr("__text_signature__").unwrap().to_string(), "(a, b, c)"); -# let defaulted = wrap_pyfunction!(function_with_defaults, py).unwrap(); -# assert_eq!(defaulted.getattr("__text_signature__").unwrap().to_string(), "(a, b=1, c=2)"); -# }) -# } -``` -
- -## from 0.16.* to 0.17 - -### Type checks have been changed for `PyMapping` and `PySequence` types -
-Click to expand - -Previously the type checks for `PyMapping` and `PySequence` (implemented in `PyTryFrom`) -used the Python C-API functions `PyMapping_Check` and `PySequence_Check`. -Unfortunately these functions are not sufficient for distinguishing such types, -leading to inconsistent behavior (see -[pyo3/pyo3#2072](https://github.com/PyO3/pyo3/issues/2072)). - -PyO3 0.17 changes these downcast checks to explicitly test if the type is a -subclass of the corresponding abstract base class `collections.abc.Mapping` or -`collections.abc.Sequence`. Note this requires calling into Python, which may -incur a performance penalty over the previous method. If this performance -penalty is a problem, you may be able to perform your own checks and use -`try_from_unchecked` (unsafe). - -Another side-effect is that a pyclass defined in Rust with PyO3 will need to -be _registered_ with the corresponding Python abstract base class for -downcasting to succeed. `PySequence::register` and `PyMapping:register` have -been added to make it easy to do this from Rust code. These are equivalent to -calling `collections.abc.Mapping.register(MappingPyClass)` or -`collections.abc.Sequence.register(SequencePyClass)` from Python. - -For example, for a mapping class defined in Rust: -```rust,compile_fail -use pyo3::prelude::*; -use std::collections::HashMap; - -#[pyclass(mapping)] -struct Mapping { - index: HashMap, -} - -#[pymethods] -impl Mapping { - #[new] - fn new(elements: Option<&PyList>) -> PyResult { - // ... - // truncated implementation of this mapping pyclass - basically a wrapper around a HashMap -} -``` - -You must register the class with `collections.abc.Mapping` before the downcast will work: -```rust,compile_fail -let m = Py::new(py, Mapping { index }).unwrap(); -assert!(m.as_ref(py).downcast::().is_err()); -PyMapping::register::(py).unwrap(); -assert!(m.as_ref(py).downcast::().is_ok()); -``` - -Note that this requirement may go away in the future when a pyclass is able to inherit from the abstract base class directly (see [pyo3/pyo3#991](https://github.com/PyO3/pyo3/issues/991)). -
- -### The `multiple-pymethods` feature now requires Rust 1.62 -
-Click to expand - -Due to limitations in the `inventory` crate which the `multiple-pymethods` feature depends on, this feature now -requires Rust 1.62. For more information see [dtolnay/inventory#32](https://github.com/dtolnay/inventory/issues/32). -
- -### Added `impl IntoPy> for &str` -
-Click to expand - -This may cause inference errors. - -Before: -```rust,compile_fail -# use pyo3::prelude::*; -# -# fn main() { -Python::with_gil(|py| { - // Cannot infer either `Py` or `Py` - let _test = "test".into_py(py); -}); -# } -``` - -After, some type annotations may be necessary: - -```rust -# #![allow(deprecated)] -# use pyo3::prelude::*; -# -# fn main() { -Python::with_gil(|py| { - let _test: Py = "test".into_py(py); -}); -# } -``` -
- -### The `pyproto` feature is now disabled by default -
-Click to expand - -In preparation for removing the deprecated `#[pyproto]` attribute macro in a future PyO3 version, it is now gated behind an opt-in feature flag. This also gives a slight saving to compile times for code which does not use the deprecated macro. -
- -### `PyTypeObject` trait has been deprecated -
-Click to expand - -The `PyTypeObject` trait already was near-useless; almost all functionality was already on the `PyTypeInfo` trait, which `PyTypeObject` had a blanket implementation based upon. In PyO3 0.17 the final method, `PyTypeObject::type_object` was moved to `PyTypeInfo::type_object`. - -To migrate, update trait bounds and imports from `PyTypeObject` to `PyTypeInfo`. - -Before: - -```rust,ignore -use pyo3::Python; -use pyo3::type_object::PyTypeObject; -use pyo3::types::PyType; - -fn get_type_object(py: Python<'_>) -> &PyType { - T::type_object(py) -} -``` - -After - -```rust,ignore -use pyo3::{Python, PyTypeInfo}; -use pyo3::types::PyType; - -fn get_type_object(py: Python<'_>) -> &PyType { - T::type_object(py) -} - -# Python::with_gil(|py| { get_type_object::(py); }); -``` -
- -### `impl IntoPy for [T; N]` now requires `T: IntoPy` rather than `T: ToPyObject` -
-Click to expand - -If this leads to errors, simply implement `IntoPy`. Because pyclasses already implement `IntoPy`, you probably don't need to worry about this. -
- -### Each `#[pymodule]` can now only be initialized once per process -
-Click to expand - -To make PyO3 modules sound in the presence of Python sub-interpreters, for now it has been necessary to explicitly disable the ability to initialize a `#[pymodule]` more than once in the same process. Attempting to do this will now raise an `ImportError`. -
- -## from 0.15.* to 0.16 - -### Drop support for older technologies -
-Click to expand - -PyO3 0.16 has increased minimum Rust version to 1.48 and minimum Python version to 3.7. This enables use of newer language features (enabling some of the other additions in 0.16) and simplifies maintenance of the project. -
- -### `#[pyproto]` has been deprecated -
-Click to expand - -In PyO3 0.15, the `#[pymethods]` attribute macro gained support for implementing "magic methods" such as `__str__` (aka "dunder" methods). This implementation was not quite finalized at the time, with a few edge cases to be decided upon. The existing `#[pyproto]` attribute macro was left untouched, because it covered these edge cases. - -In PyO3 0.16, the `#[pymethods]` implementation has been completed and is now the preferred way to implement magic methods. To allow the PyO3 project to move forward, `#[pyproto]` has been deprecated (with expected removal in PyO3 0.18). - -Migration from `#[pyproto]` to `#[pymethods]` is straightforward; copying the existing methods directly from the `#[pyproto]` trait implementation is all that is needed in most cases. - -Before: - -```rust,compile_fail -use pyo3::prelude::*; -use pyo3::class::{PyObjectProtocol, PyIterProtocol}; -use pyo3::types::PyString; - -#[pyclass] -struct MyClass {} - -#[pyproto] -impl PyObjectProtocol for MyClass { - fn __str__(&self) -> &'static [u8] { - b"hello, world" - } -} - -#[pyproto] -impl PyIterProtocol for MyClass { - fn __iter__(slf: PyRef) -> PyResult<&PyAny> { - PyString::new(slf.py(), "hello, world").iter() - } -} -``` - -After - -```rust,compile_fail -use pyo3::prelude::*; -use pyo3::types::PyString; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - fn __str__(&self) -> &'static [u8] { - b"hello, world" - } - - fn __iter__(slf: PyRef) -> PyResult<&PyAny> { - PyString::new(slf.py(), "hello, world").iter() - } -} -``` -
- -### Removed `PartialEq` for object wrappers -
-Click to expand - -The Python object wrappers `Py` and `PyAny` had implementations of `PartialEq` -so that `object_a == object_b` would compare the Python objects for pointer -equality, which corresponds to the `is` operator, not the `==` operator in -Python. This has been removed in favor of a new method: use -`object_a.is(object_b)`. This also has the advantage of not requiring the same -wrapper type for `object_a` and `object_b`; you can now directly compare a -`Py` with a `&PyAny` without having to convert. - -To check for Python object equality (the Python `==` operator), use the new -method `eq()`. -
- -### Container magic methods now match Python behavior -
-Click to expand - -In PyO3 0.15, `__getitem__`, `__setitem__` and `__delitem__` in `#[pymethods]` would generate only the _mapping_ implementation for a `#[pyclass]`. To match the Python behavior, these methods now generate both the _mapping_ **and** _sequence_ implementations. - -This means that classes implementing these `#[pymethods]` will now also be treated as sequences, same as a Python `class` would be. Small differences in behavior may result: - - PyO3 will allow instances of these classes to be cast to `PySequence` as well as `PyMapping`. - - Python will provide a default implementation of `__iter__` (if the class did not have one) which repeatedly calls `__getitem__` with integers (starting at 0) until an `IndexError` is raised. - -To explain this in detail, consider the following Python class: - -```python -class ExampleContainer: - - def __len__(self): - return 5 - - def __getitem__(self, idx: int) -> int: - if idx < 0 or idx > 5: - raise IndexError() - return idx -``` - -This class implements a Python [sequence](https://docs.python.org/3/glossary.html#term-sequence). - -The `__len__` and `__getitem__` methods are also used to implement a Python [mapping](https://docs.python.org/3/glossary.html#term-mapping). In the Python C-API, these methods are not shared: the sequence `__len__` and `__getitem__` are defined by the `sq_length` and `sq_item` slots, and the mapping equivalents are `mp_length` and `mp_subscript`. There are similar distinctions for `__setitem__` and `__delitem__`. - -Because there is no such distinction from Python, implementing these methods will fill the mapping and sequence slots simultaneously. A Python class with `__len__` implemented, for example, will have both the `sq_length` and `mp_length` slots filled. - -The PyO3 behavior in 0.16 has been changed to be closer to this Python behavior by default. -
- -### `wrap_pymodule!` and `wrap_pyfunction!` now respect privacy correctly -
-Click to expand - -Prior to PyO3 0.16 the `wrap_pymodule!` and `wrap_pyfunction!` macros could use modules and functions whose defining `fn` was not reachable according Rust privacy rules. - -For example, the following code was legal before 0.16, but in 0.16 is rejected because the `wrap_pymodule!` macro cannot access the `private_submodule` function: - -```rust,compile_fail -mod foo { - use pyo3::prelude::*; - - #[pymodule] - fn private_submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> { - Ok(()) - } -} - -use pyo3::prelude::*; -use foo::*; - -#[pymodule] -fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { - m.add_wrapped(wrap_pymodule!(private_submodule))?; - Ok(()) -} -``` - -To fix it, make the private submodule visible, e.g. with `pub` or `pub(crate)`. - -```rust,ignore -mod foo { - use pyo3::prelude::*; - - #[pymodule] - pub(crate) fn private_submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> { - Ok(()) - } -} - -use pyo3::prelude::*; -use pyo3::wrap_pymodule; -use foo::*; - -#[pymodule] -fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { - m.add_wrapped(wrap_pymodule!(private_submodule))?; - Ok(()) -} -``` -
- -## from 0.14.* to 0.15 - -### Changes in sequence indexing -
-Click to expand - -For all types that take sequence indices (`PyList`, `PyTuple` and `PySequence`), -the API has been made consistent to only take `usize` indices, for consistency -with Rust's indexing conventions. Negative indices, which were only -sporadically supported even in APIs that took `isize`, now aren't supported -anywhere. - -Further, the `get_item` methods now always return a `PyResult` instead of -panicking on invalid indices. The `Index` trait has been implemented instead, -and provides the same panic behavior as on Rust vectors. - -Note that *slice* indices (accepted by `PySequence::get_slice` and other) still -inherit the Python behavior of clamping the indices to the actual length, and -not panicking/returning an error on out of range indices. - -An additional advantage of using Rust's indexing conventions for these types is -that these types can now also support Rust's indexing operators as part of a -consistent API: - -```rust,ignore -#![allow(deprecated)] -use pyo3::{Python, types::PyList}; - -Python::with_gil(|py| { - let list = PyList::new(py, &[1, 2, 3]); - assert_eq!(list[0..2].to_string(), "[1, 2]"); -}); -``` -
- -## from 0.13.* to 0.14 - -### `auto-initialize` feature is now opt-in -
-Click to expand - -For projects embedding Python in Rust, PyO3 no longer automatically initializes a Python interpreter on the first call to `Python::with_gil` (or `Python::acquire_gil`) unless the [`auto-initialize` feature](features.md#auto-initialize) is enabled. -
- -### New `multiple-pymethods` feature -
-Click to expand - -`#[pymethods]` have been reworked with a simpler default implementation which removes the dependency on the `inventory` crate. This reduces dependencies and compile times for the majority of users. - -The limitation of the new default implementation is that it cannot support multiple `#[pymethods]` blocks for the same `#[pyclass]`. If you need this functionality, you must enable the `multiple-pymethods` feature which will switch `#[pymethods]` to the inventory-based implementation. -
- -### Deprecated `#[pyproto]` methods -
-Click to expand - -Some protocol (aka `__dunder__`) methods such as `__bytes__` and `__format__` have been possible to implement two ways in PyO3 for some time: via a `#[pyproto]` (e.g. `PyObjectProtocol` for the methods listed here), or by writing them directly in `#[pymethods]`. This is only true for a handful of the `#[pyproto]` methods (for technical reasons to do with the way PyO3 currently interacts with the Python C-API). - -In the interest of having only one way to do things, the `#[pyproto]` forms of these methods have been deprecated. - -To migrate just move the affected methods from a `#[pyproto]` to a `#[pymethods]` block. - -Before: - -```rust,compile_fail -use pyo3::prelude::*; -use pyo3::class::basic::PyObjectProtocol; - -#[pyclass] -struct MyClass {} - -#[pyproto] -impl PyObjectProtocol for MyClass { - fn __bytes__(&self) -> &'static [u8] { - b"hello, world" - } -} -``` - -After: - -```rust -use pyo3::prelude::*; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - fn __bytes__(&self) -> &'static [u8] { - b"hello, world" - } -} -``` -
- -## from 0.12.* to 0.13 - -### Minimum Rust version increased to Rust 1.45 -
-Click to expand - -PyO3 `0.13` makes use of new Rust language features stabilized between Rust 1.40 and Rust 1.45. If you are using a Rust compiler older than Rust 1.45, you will need to update your toolchain to be able to continue using PyO3. -
- -### Runtime changes to support the CPython limited API -
-Click to expand - -In PyO3 `0.13` support was added for compiling against the CPython limited API. This had a number of implications for _all_ PyO3 users, described here. - -The largest of these is that all types created from PyO3 are what CPython calls "heap" types. The specific implications of this are: - -- If you wish to subclass one of these types _from Rust_ you must mark it `#[pyclass(subclass)]`, as you would if you wished to allow subclassing it from Python code. -- Type objects are now mutable - Python code can set attributes on them. -- `__module__` on types without `#[pyclass(module="mymodule")]` no longer returns `builtins`, it now raises `AttributeError`. -
- -## from 0.11.* to 0.12 - -### `PyErr` has been reworked -
-Click to expand - -In PyO3 `0.12` the `PyErr` type has been re-implemented to be significantly more compatible with -the standard Rust error handling ecosystem. Specifically `PyErr` now implements -`Error + Send + Sync`, which are the standard traits used for error types. - -While this has necessitated the removal of a number of APIs, the resulting `PyErr` type should now -be much more easier to work with. The following sections list the changes in detail and how to -migrate to the new APIs. -
- -#### `PyErr::new` and `PyErr::from_type` now require `Send + Sync` for their argument -
-Click to expand - -For most uses no change will be needed. If you are trying to construct `PyErr` from a value that is -not `Send + Sync`, you will need to first create the Python object and then use -`PyErr::from_instance`. - -Similarly, any types which implemented `PyErrArguments` will now need to be `Send + Sync`. -
- -#### `PyErr`'s contents are now private -
-Click to expand - -It is no longer possible to access the fields `.ptype`, `.pvalue` and `.ptraceback` of a `PyErr`. -You should instead now use the new methods `PyErr::ptype`, `PyErr::pvalue` and `PyErr::ptraceback`. -
- -#### `PyErrValue` and `PyErr::from_value` have been removed -
-Click to expand - -As these were part the internals of `PyErr` which have been reworked, these APIs no longer exist. - -If you used this API, it is recommended to use `PyException::new_err` (see [the section on -Exception types](#exception-types-have-been-reworked)). -
- -#### `Into>` for `PyErr` has been removed -
-Click to expand - -This implementation was redundant. Just construct the `Result::Err` variant directly. - -Before: -```rust,compile_fail -let result: PyResult<()> = PyErr::new::("error message").into(); -``` - -After (also using the new reworked exception types; see the following section): -```rust -# use pyo3::{PyResult, exceptions::PyTypeError}; -let result: PyResult<()> = Err(PyTypeError::new_err("error message")); -``` -
- -### Exception types have been reworked -
-Click to expand - -Previously exception types were zero-sized marker types purely used to construct `PyErr`. In PyO3 -0.12, these types have been replaced with full definitions and are usable in the same way as `PyAny`, `PyDict` etc. This -makes it possible to interact with Python exception objects. - -The new types also have names starting with the "Py" prefix. For example, before: - -```rust,ignore -let err: PyErr = TypeError::py_err("error message"); -``` - -After: - -```rust,ignore -# use pyo3::{PyErr, PyResult, Python, type_object::PyTypeObject}; -# use pyo3::exceptions::{PyBaseException, PyTypeError}; -# Python::with_gil(|py| -> PyResult<()> { -let err: PyErr = PyTypeError::new_err("error message"); - -// Uses Display for PyErr, new for PyO3 0.12 -assert_eq!(err.to_string(), "TypeError: error message"); - -// Now possible to interact with exception instances, new for PyO3 0.12 -let instance: &PyBaseException = err.instance(py); -assert_eq!( - instance.getattr("__class__")?, - PyTypeError::type_object(py).as_ref() -); -# Ok(()) -# }).unwrap(); -``` -
- -### `FromPy` has been removed -
-Click to expand - -To simplify the PyO3 conversion traits, the `FromPy` trait has been removed. Previously there were -two ways to define the to-Python conversion for a type: -`FromPy for PyObject` and `IntoPy for T`. - -Now there is only one way to define the conversion, `IntoPy`, so downstream crates may need to -adjust accordingly. - -Before: -```rust,compile_fail -# use pyo3::prelude::*; -struct MyPyObjectWrapper(PyObject); - -impl FromPy for PyObject { - fn from_py(other: MyPyObjectWrapper, _py: Python<'_>) -> Self { - other.0 - } -} -``` - -After -```rust -# use pyo3::prelude::*; -# #[allow(dead_code)] -struct MyPyObjectWrapper(PyObject); - -# #[allow(deprecated)] -impl IntoPy for MyPyObjectWrapper { - fn into_py(self, _py: Python<'_>) -> PyObject { - self.0 - } -} -``` - -Similarly, code which was using the `FromPy` trait can be trivially rewritten to use `IntoPy`. - -Before: -```rust,compile_fail -# use pyo3::prelude::*; -# Python::with_gil(|py| { -let obj = PyObject::from_py(1.234, py); -# }) -``` - -After: -```rust -# #![allow(deprecated)] -# use pyo3::prelude::*; -# Python::with_gil(|py| { -let obj: PyObject = 1.234.into_py(py); -# }) -``` -
- -### `PyObject` is now a type alias of `Py` -
-Click to expand - -This should change very little from a usage perspective. If you implemented traits for both -`PyObject` and `Py`, you may find you can just remove the `PyObject` implementation. -
- -### `AsPyRef` has been removed -
-Click to expand - -As `PyObject` has been changed to be just a type alias, the only remaining implementor of `AsPyRef` -was `Py`. This removed the need for a trait, so the `AsPyRef::as_ref` method has been moved to -`Py::as_ref`. - -This should require no code changes except removing `use pyo3::AsPyRef` for code which did not use -`pyo3::prelude::*`. - -Before: -```rust,ignore -use pyo3::{AsPyRef, Py, types::PyList}; -# pyo3::Python::with_gil(|py| { -let list_py: Py = PyList::empty(py).into(); -let list_ref: &PyList = list_py.as_ref(py); -# }) -``` - -After: -```rust,ignore -use pyo3::{Py, types::PyList}; -# pyo3::Python::with_gil(|py| { -let list_py: Py = PyList::empty(py).into(); -let list_ref: &PyList = list_py.as_ref(py); -# }) -``` -
- -## from 0.10.* to 0.11 - -### Stable Rust -
-Click to expand - -PyO3 now supports the stable Rust toolchain. The minimum required version is 1.39.0. -
- -### `#[pyclass]` structs must now be `Send` or `unsendable` -
-Click to expand - -Because `#[pyclass]` structs can be sent between threads by the Python interpreter, they must implement -`Send` or declared as `unsendable` (by `#[pyclass(unsendable)]`). -Note that `unsendable` is added in PyO3 `0.11.1` and `Send` is always required in PyO3 `0.11.0`. - -This may "break" some code which previously was accepted, even though it could be unsound. -There can be two fixes: - -1. If you think that your `#[pyclass]` actually must be `Send`able, then let's implement `Send`. - A common, safer way is using thread-safe types. E.g., `Arc` instead of `Rc`, `Mutex` instead of - `RefCell`, and `Box` instead of `Box`. - - Before: - ```rust,compile_fail - use pyo3::prelude::*; - use std::rc::Rc; - use std::cell::RefCell; - - #[pyclass] - struct NotThreadSafe { - shared_bools: Rc>>, - closure: Box, - } - ``` - - After: - ```rust,ignore - # #![allow(dead_code)] - use pyo3::prelude::*; - use std::sync::{Arc, Mutex}; - - #[pyclass] - struct ThreadSafe { - shared_bools: Arc>>, - closure: Box, - } - ``` - - In situations where you cannot change your `#[pyclass]` to automatically implement `Send` - (e.g., when it contains a raw pointer), you can use `unsafe impl Send`. - In such cases, care should be taken to ensure the struct is actually thread safe. - See [the Rustonomicon](https://doc.rust-lang.org/nomicon/send-and-sync.html) for more. - -2. If you think that your `#[pyclass]` should not be accessed by another thread, you can use - `unsendable` flag. A class marked with `unsendable` panics when accessed by another thread, - making it thread-safe to expose an unsendable object to the Python interpreter. - - Before: - ```rust,compile_fail - use pyo3::prelude::*; - - #[pyclass] - struct Unsendable { - pointers: Vec<*mut std::os::raw::c_char>, - } - ``` - - After: - ```rust - # #![allow(dead_code)] - use pyo3::prelude::*; - - #[pyclass(unsendable)] - struct Unsendable { - pointers: Vec<*mut std::os::raw::c_char>, - } - ``` -
- -### All `PyObject` and `Py` methods now take `Python` as an argument -
-Click to expand - -Previously, a few methods such as `Object::get_refcnt` did not take `Python` as an argument (to -ensure that the Python GIL was held by the current thread). Technically, this was not sound. -To migrate, just pass a `py` argument to any calls to these methods. - -Before: -```rust,compile_fail -# pyo3::Python::with_gil(|py| { -py.None().get_refcnt(); -# }) -``` - -After: -```rust -# pyo3::Python::with_gil(|py| { -py.None().get_refcnt(py); -# }) -``` -
- -## from 0.9.* to 0.10 - -### `ObjectProtocol` is removed -
-Click to expand - -All methods are moved to [`PyAny`]. -And since now all native types (e.g., `PyList`) implements `Deref`, -all you need to do is remove `ObjectProtocol` from your code. -Or if you use `ObjectProtocol` by `use pyo3::prelude::*`, you have to do nothing. - -Before: -```rust,compile_fail,ignore -use pyo3::ObjectProtocol; - -# pyo3::Python::with_gil(|py| { -let obj = py.eval("lambda: 'Hi :)'", None, None).unwrap(); -let hi: &pyo3::types::PyString = obj.call0().unwrap().downcast().unwrap(); -assert_eq!(hi.len().unwrap(), 5); -# }) -``` - -After: -```rust,ignore -# pyo3::Python::with_gil(|py| { -let obj = py.eval("lambda: 'Hi :)'", None, None).unwrap(); -let hi: &pyo3::types::PyString = obj.call0().unwrap().downcast().unwrap(); -assert_eq!(hi.len().unwrap(), 5); -# }) -``` -
- -### No `#![feature(specialization)]` in user code -
-Click to expand - -While PyO3 itself still requires specialization and nightly Rust, -now you don't have to use `#![feature(specialization)]` in your crate. -
- -## from 0.8.* to 0.9 - -### `#[new]` interface -
-Click to expand - -[`PyRawObject`](https://docs.rs/pyo3/0.8.5/pyo3/type_object/struct.PyRawObject.html) -is now removed and our syntax for constructors has changed. - -Before: -```rust,compile_fail -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[new] - fn new(obj: &PyRawObject) { - obj.init(MyClass {}) - } -} -``` - -After: -```rust -# use pyo3::prelude::*; -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[new] - fn new() -> Self { - MyClass {} - } -} -``` - -Basically you can return `Self` or `Result` directly. -For more, see [the constructor section](class.md#constructor) of this guide. -
- -### PyCell -
-Click to expand - -PyO3 0.9 introduces `PyCell`, which is a [`RefCell`]-like object wrapper -for ensuring Rust's rules regarding aliasing of references are upheld. -For more detail, see the -[Rust Book's section on Rust's rules of references](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#the-rules-of-references) - -For `#[pymethods]` or `#[pyfunction]`s, your existing code should continue to work without any change. -Python exceptions will automatically be raised when your functions are used in a way which breaks Rust's -rules of references. - -Here is an example. -```rust -# use pyo3::prelude::*; - -#[pyclass] -struct Names { - names: Vec, -} - -#[pymethods] -impl Names { - #[new] - fn new() -> Self { - Names { names: vec![] } - } - fn merge(&mut self, other: &mut Names) { - self.names.append(&mut other.names) - } -} -# Python::with_gil(|py| { -# let names = Py::new(py, Names::new()).unwrap(); -# pyo3::py_run!(py, names, r" -# try: -# names.merge(names) -# assert False, 'Unreachable' -# except RuntimeError as e: -# assert str(e) == 'Already borrowed' -# "); -# }) -``` -`Names` has a `merge` method, which takes `&mut self` and another argument of type `&mut Self`. -Given this `#[pyclass]`, calling `names.merge(names)` in Python raises -a [`PyBorrowMutError`] exception, since it requires two mutable borrows of `names`. - -However, for `#[pyproto]` and some functions, you need to manually fix the code. - -#### Object creation -In 0.8 object creation was done with `PyRef::new` and `PyRefMut::new`. -In 0.9 these have both been removed. -To upgrade code, please use -`PyCell::new` instead. -If you need [`PyRef`] or [`PyRefMut`], just call `.borrow()` or `.borrow_mut()` -on the newly-created `PyCell`. - -Before: -```rust,compile_fail -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass {} -# Python::with_gil(|py| { -let obj_ref = PyRef::new(py, MyClass {}).unwrap(); -# }) -``` - -After: -```rust,ignore -# use pyo3::prelude::*; -# #[pyclass] -# struct MyClass {} -# Python::with_gil(|py| { -let obj = PyCell::new(py, MyClass {}).unwrap(); -let obj_ref = obj.borrow(); -# }) -``` - -#### Object extraction -For `PyClass` types `T`, `&T` and `&mut T` no longer have [`FromPyObject`] implementations. -Instead you should extract `PyRef` or `PyRefMut`, respectively. -If `T` implements `Clone`, you can extract `T` itself. -In addition, you can also extract `&PyCell`, though you rarely need it. - -Before: -```compile_fail -let obj: &PyAny = create_obj(); -let obj_ref: &MyClass = obj.extract().unwrap(); -let obj_ref_mut: &mut MyClass = obj.extract().unwrap(); -``` - -After: -```rust,ignore -# use pyo3::prelude::*; -# use pyo3::types::IntoPyDict; -# #[pyclass] #[derive(Clone)] struct MyClass {} -# #[pymethods] impl MyClass { #[new]fn new() -> Self { MyClass {} }} -# Python::with_gil(|py| { -# let typeobj = py.get_type::(); -# let d = [("c", typeobj)].into_py_dict(py); -# let create_obj = || py.eval("c()", None, Some(d)).unwrap(); -let obj: &PyAny = create_obj(); -let obj_cell: &PyCell = obj.extract().unwrap(); -let obj_cloned: MyClass = obj.extract().unwrap(); // extracted by cloning the object -{ - let obj_ref: PyRef<'_, MyClass> = obj.extract().unwrap(); - // we need to drop obj_ref before we can extract a PyRefMut due to Rust's rules of references -} -let obj_ref_mut: PyRefMut<'_, MyClass> = obj.extract().unwrap(); -# }) -``` - - -#### `#[pyproto]` -Most of the arguments to methods in `#[pyproto]` impls require a -[`FromPyObject`] implementation. -So if your protocol methods take `&T` or `&mut T` (where `T: PyClass`), -please use [`PyRef`] or [`PyRefMut`] instead. - -Before: -```rust,compile_fail -# use pyo3::prelude::*; -# use pyo3::class::PySequenceProtocol; -#[pyclass] -struct ByteSequence { - elements: Vec, -} -#[pyproto] -impl PySequenceProtocol for ByteSequence { - fn __concat__(&self, other: &Self) -> PyResult { - let mut elements = self.elements.clone(); - elements.extend_from_slice(&other.elements); - Ok(Self { elements }) - } -} -``` - -After: -```rust,compile_fail -# use pyo3::prelude::*; -# use pyo3::class::PySequenceProtocol; -#[pyclass] -struct ByteSequence { - elements: Vec, -} -#[pyproto] -impl PySequenceProtocol for ByteSequence { - fn __concat__(&self, other: PyRef<'p, Self>) -> PyResult { - let mut elements = self.elements.clone(); - elements.extend_from_slice(&other.elements); - Ok(Self { elements }) - } -} -``` -
- - - -[`FromPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.FromPyObject.html -[`PyAny`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyAny.html -[`PyBorrowMutError`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyBorrowMutError.html -[`PyRef`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRef.html -[`PyRefMut`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRef.html - -[`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html diff --git a/vendor/pyo3/guide/src/module.md b/vendor/pyo3/guide/src/module.md deleted file mode 100644 index 1e274c7c95..0000000000 --- a/vendor/pyo3/guide/src/module.md +++ /dev/null @@ -1,181 +0,0 @@ -# Python modules - -You can create a module using `#[pymodule]`: - -```rust -use pyo3::prelude::*; - -#[pyfunction] -fn double(x: usize) -> usize { - x * 2 -} - -/// This module is implemented in Rust. -#[pymodule] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?) -} -``` - -The `#[pymodule]` procedural macro takes care of exporting the initialization function of your -module to Python. - -The module's name defaults to the name of the Rust function. You can override the module name by -using `#[pyo3(name = "custom_name")]`: - -```rust -use pyo3::prelude::*; - -#[pyfunction] -fn double(x: usize) -> usize { - x * 2 -} - -#[pymodule(name = "custom_name")] -fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?) -} -``` - -The name of the module must match the name of the `.so` or `.pyd` -file. Otherwise, you will get an import error in Python with the following message: -`ImportError: dynamic module does not define module export function (PyInit_name_of_your_module)` - -To import the module, either: - - copy the shared library as described in [Manual builds](building-and-distribution.md#manual-builds), or - - use a tool, e.g. `maturin develop` with [maturin](https://github.com/PyO3/maturin) or -`python setup.py develop` with [setuptools-rust](https://github.com/PyO3/setuptools-rust). - -## Documentation - -The [Rust doc comments](https://doc.rust-lang.org/stable/book/ch03-04-comments.html) of the module -initialization function will be applied automatically as the Python docstring of your module. - -For example, building off of the above code, this will print `This module is implemented in Rust.`: - -```python -import my_extension - -print(my_extension.__doc__) -``` - -## Python submodules - -You can create a module hierarchy within a single extension module by using -[`Bound<'_, PyModule>::add_submodule()`]({{#PYO3_DOCS_URL}}/pyo3/prelude/trait.PyModuleMethods.html#tymethod.add_submodule). -For example, you could define the modules `parent_module` and `parent_module.child_module`. - -```rust -use pyo3::prelude::*; - -#[pymodule] -fn parent_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - register_child_module(m)?; - Ok(()) -} - -fn register_child_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()> { - let child_module = PyModule::new(parent_module.py(), "child_module")?; - child_module.add_function(wrap_pyfunction!(func, &child_module)?)?; - parent_module.add_submodule(&child_module) -} - -#[pyfunction] -fn func() -> String { - "func".to_string() -} - -# Python::with_gil(|py| { -# use pyo3::wrap_pymodule; -# use pyo3::types::IntoPyDict; -# use pyo3::ffi::c_str; -# let parent_module = wrap_pymodule!(parent_module)(py); -# let ctx = [("parent_module", parent_module)].into_py_dict(py).unwrap(); -# -# py.run(c_str!("assert parent_module.child_module.func() == 'func'"), None, Some(&ctx)).unwrap(); -# }) -``` - -Note that this does not define a package, so this won’t allow Python code to directly import -submodules by using `from parent_module import child_module`. For more information, see -[#759](https://github.com/PyO3/pyo3/issues/759) and -[#1517](https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021). - -It is not necessary to add `#[pymodule]` on nested modules, which is only required on the top-level module. - -## Declarative modules - -Another syntax based on Rust inline modules is also available to declare modules. - -For example: -```rust -# mod declarative_module_test { -use pyo3::prelude::*; - -#[pyfunction] -fn double(x: usize) -> usize { - x * 2 -} - -#[pymodule] -mod my_extension { - use super::*; - - #[pymodule_export] - use super::double; // Exports the double function as part of the module - - #[pyfunction] // This will be part of the module - fn triple(x: usize) -> usize { - x * 3 - } - - #[pyclass] // This will be part of the module - struct Unit; - - #[pymodule] - mod submodule { - // This is a submodule - } - - #[pymodule_init] - fn init(m: &Bound<'_, PyModule>) -> PyResult<()> { - // Arbitrary code to run at the module initialization - m.add("double2", m.getattr("double")?) - } -} -# } -``` - -The `#[pymodule]` macro automatically sets the `module` attribute of the `#[pyclass]` macros declared inside of it with its name. -For nested modules, the name of the parent module is automatically added. -In the following example, the `Unit` class will have for `module` `my_extension.submodule` because it is properly nested -but the `Ext` class will have for `module` the default `builtins` because it not nested. - -```rust -# mod declarative_module_module_attr_test { -use pyo3::prelude::*; - -#[pyclass] -struct Ext; - -#[pymodule] -mod my_extension { - use super::*; - - #[pymodule_export] - use super::Ext; - - #[pymodule] - mod submodule { - use super::*; - // This is a submodule - - #[pyclass] // This will be part of the module - struct Unit; - } -} -# } -``` -It is possible to customize the `module` value for a `#[pymodule]` with the `#[pyo3(module = "MY_MODULE")]` option. - -You can provide the `submodule` argument to `pymodule()` for modules that are not top-level modules -- it is automatically set for modules nested inside of a `#[pymodule]`. diff --git a/vendor/pyo3/guide/src/parallelism.md b/vendor/pyo3/guide/src/parallelism.md deleted file mode 100644 index 64ff1c8c9c..0000000000 --- a/vendor/pyo3/guide/src/parallelism.md +++ /dev/null @@ -1,177 +0,0 @@ -# Parallelism - -CPython has the infamous [Global Interpreter Lock](https://docs.python.org/3/glossary.html#term-global-interpreter-lock) (GIL), which prevents several threads from executing Python bytecode in parallel. This makes threading in Python a bad fit for [CPU-bound](https://en.wikipedia.org/wiki/CPU-bound) tasks and often forces developers to accept the overhead of multiprocessing. There is an experimental "free-threaded" version of CPython 3.13 that does not have a GIL, see the PyO3 docs on [free-threaded Python](./free-threading.md) for more information about that. - -In PyO3 parallelism can be easily achieved in Rust-only code. Let's take a look at our [word-count](https://github.com/PyO3/pyo3/blob/main/examples/word-count/src/lib.rs) example, where we have a `search` function that utilizes the [rayon](https://github.com/rayon-rs/rayon) crate to count words in parallel. -```rust,no_run -# #![allow(dead_code)] -use pyo3::prelude::*; - -// These traits let us use `par_lines` and `map`. -use rayon::str::ParallelString; -use rayon::iter::ParallelIterator; - -/// Count the occurrences of needle in line, case insensitive -fn count_line(line: &str, needle: &str) -> usize { - let mut total = 0; - for word in line.split(' ') { - if word == needle { - total += 1; - } - } - total -} - -#[pyfunction] -fn search(contents: &str, needle: &str) -> usize { - contents - .par_lines() - .map(|line| count_line(line, needle)) - .sum() -} -``` - -But let's assume you have a long running Rust function which you would like to execute several times in parallel. For the sake of example let's take a sequential version of the word count: -```rust,no_run -# #![allow(dead_code)] -# fn count_line(line: &str, needle: &str) -> usize { -# let mut total = 0; -# for word in line.split(' ') { -# if word == needle { -# total += 1; -# } -# } -# total -# } -# -fn search_sequential(contents: &str, needle: &str) -> usize { - contents.lines().map(|line| count_line(line, needle)).sum() -} -``` - -To enable parallel execution of this function, the [`Python::allow_threads`] method can be used to temporarily release the GIL, thus allowing other Python threads to run. We then have a function exposed to the Python runtime which calls `search_sequential` inside a closure passed to [`Python::allow_threads`] to enable true parallelism: -```rust,no_run -# #![allow(dead_code)] -# use pyo3::prelude::*; -# -# fn count_line(line: &str, needle: &str) -> usize { -# let mut total = 0; -# for word in line.split(' ') { -# if word == needle { -# total += 1; -# } -# } -# total -# } -# -# fn search_sequential(contents: &str, needle: &str) -> usize { -# contents.lines().map(|line| count_line(line, needle)).sum() -# } -#[pyfunction] -fn search_sequential_allow_threads(py: Python<'_>, contents: &str, needle: &str) -> usize { - py.allow_threads(|| search_sequential(contents, needle)) -} -``` - -Now Python threads can use more than one CPU core, resolving the limitation which usually makes multi-threading in Python only good for IO-bound tasks: -```Python -from concurrent.futures import ThreadPoolExecutor -from word_count import search_sequential_allow_threads - -executor = ThreadPoolExecutor(max_workers=2) - -future_1 = executor.submit( - word_count.search_sequential_allow_threads, contents, needle -) -future_2 = executor.submit( - word_count.search_sequential_allow_threads, contents, needle -) -result_1 = future_1.result() -result_2 = future_2.result() -``` - -## Benchmark - -Let's benchmark the `word-count` example to verify that we really did unlock parallelism with PyO3. - -We are using `pytest-benchmark` to benchmark four word count functions: - -1. Pure Python version -2. Rust parallel version -3. Rust sequential version -4. Rust sequential version executed twice with two Python threads - -The benchmark script can be found [here](https://github.com/PyO3/pyo3/blob/main/examples/word-count/tests/test_word_count.py), and we can run `nox` in the `word-count` folder to benchmark these functions. - -While the results of the benchmark of course depend on your machine, the relative results should be similar to this (mid 2020): -```text --------------------------------------------------------------------------------------------------- benchmark: 4 tests ------------------------------------------------------------------------------------------------- -Name (time in ms) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -test_word_count_rust_parallel 1.7315 (1.0) 4.6495 (1.0) 1.9972 (1.0) 0.4299 (1.0) 1.8142 (1.0) 0.2049 (1.0) 40;46 500.6943 (1.0) 375 1 -test_word_count_rust_sequential 7.3348 (4.24) 10.3556 (2.23) 8.0035 (4.01) 0.7785 (1.81) 7.5597 (4.17) 0.8641 (4.22) 26;5 124.9457 (0.25) 121 1 -test_word_count_rust_sequential_twice_with_threads 7.9839 (4.61) 10.3065 (2.22) 8.4511 (4.23) 0.4709 (1.10) 8.2457 (4.55) 0.3927 (1.92) 17;17 118.3274 (0.24) 114 1 -test_word_count_python_sequential 27.3985 (15.82) 45.4527 (9.78) 28.9604 (14.50) 4.1449 (9.64) 27.5781 (15.20) 0.4638 (2.26) 3;5 34.5299 (0.07) 35 1 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -``` - -You can see that the Python threaded version is not much slower than the Rust sequential version, which means compared to an execution on a single CPU core the speed has doubled. - -## Sharing Python objects between Rust threads - -In the example above we made a Python interface to a low-level rust function, -and then leveraged the python `threading` module to run the low-level function -in parallel. It is also possible to spawn threads in Rust that acquire the GIL -and operate on Python objects. However, care must be taken to avoid writing code -that deadlocks with the GIL in these cases. - -* Note: This example is meant to illustrate how to drop and re-acquire the GIL - to avoid creating deadlocks. Unless the spawned threads subsequently - release the GIL or you are using the free-threaded build of CPython, you - will not see any speedups due to multi-threaded parallelism using `rayon` - to parallelize code that acquires and holds the GIL for the entire - execution of the spawned thread. - -In the example below, we share a `Vec` of User ID objects defined using the -`pyclass` macro and spawn threads to process the collection of data into a `Vec` -of booleans based on a predicate using a rayon parallel iterator: - -```rust,no_run -use pyo3::prelude::*; - -// These traits let us use int_par_iter and map -use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; - -#[pyclass] -struct UserID { - id: i64, -} - -let allowed_ids: Vec = Python::with_gil(|outer_py| { - let instances: Vec> = (0..10).map(|x| Py::new(outer_py, UserID { id: x }).unwrap()).collect(); - outer_py.allow_threads(|| { - instances.par_iter().map(|instance| { - Python::with_gil(|inner_py| { - instance.borrow(inner_py).id > 5 - }) - }).collect() - }) -}); -assert!(allowed_ids.into_iter().filter(|b| *b).count() == 4); -``` - -It's important to note that there is an `outer_py` GIL lifetime token as well as -an `inner_py` token. Sharing GIL lifetime tokens between threads is not allowed -and threads must individually acquire the GIL to access data wrapped by a python -object. - -It's also important to see that this example uses [`Python::allow_threads`] to -wrap the code that spawns OS threads via `rayon`. If this example didn't use -`allow_threads`, a rayon worker thread would block on acquiring the GIL while a -thread that owns the GIL spins forever waiting for the result of the rayon -thread. Calling `allow_threads` allows the GIL to be released in the thread -collecting the results from the worker threads. You should always call -`allow_threads` in situations that spawn worker threads, but especially so in -cases where worker threads need to acquire the GIL, to prevent deadlocks. - -[`Python::allow_threads`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.allow_threads diff --git a/vendor/pyo3/guide/src/performance.md b/vendor/pyo3/guide/src/performance.md deleted file mode 100644 index fb2288dd56..0000000000 --- a/vendor/pyo3/guide/src/performance.md +++ /dev/null @@ -1,142 +0,0 @@ -# Performance - -To achieve the best possible performance, it is useful to be aware of several tricks and sharp edges concerning PyO3's API. - -## `extract` versus `downcast` - -Pythonic API implemented using PyO3 are often polymorphic, i.e. they will accept `&Bound<'_, PyAny>` and try to turn this into multiple more concrete types to which the requested operation is applied. This often leads to chains of calls to `extract`, e.g. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::{exceptions::PyTypeError, types::PyList}; - -fn frobnicate_list<'py>(list: &Bound<'_, PyList>) -> PyResult> { - todo!() -} - -fn frobnicate_vec<'py>(vec: Vec>) -> PyResult> { - todo!() -} - -#[pyfunction] -fn frobnicate<'py>(value: &Bound<'py, PyAny>) -> PyResult> { - if let Ok(list) = value.extract::>() { - frobnicate_list(&list) - } else if let Ok(vec) = value.extract::>>() { - frobnicate_vec(vec) - } else { - Err(PyTypeError::new_err("Cannot frobnicate that type.")) - } -} -``` - -This suboptimal as the `FromPyObject` trait requires `extract` to have a `Result` return type. For native types like `PyList`, it faster to use `downcast` (which `extract` calls internally) when the error value is ignored. This avoids the costly conversion of a `PyDowncastError` to a `PyErr` required to fulfil the `FromPyObject` contract, i.e. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::{exceptions::PyTypeError, types::PyList}; -# fn frobnicate_list<'py>(list: &Bound<'_, PyList>) -> PyResult> { todo!() } -# fn frobnicate_vec<'py>(vec: Vec>) -> PyResult> { todo!() } -# -#[pyfunction] -fn frobnicate<'py>(value: &Bound<'py, PyAny>) -> PyResult> { - // Use `downcast` instead of `extract` as turning `PyDowncastError` into `PyErr` is quite costly. - if let Ok(list) = value.downcast::() { - frobnicate_list(list) - } else if let Ok(vec) = value.extract::>>() { - frobnicate_vec(vec) - } else { - Err(PyTypeError::new_err("Cannot frobnicate that type.")) - } -} -``` - -## Access to Bound implies access to GIL token - -Calling `Python::with_gil` is effectively a no-op when the GIL is already held, but checking that this is the case still has a cost. If an existing GIL token can not be accessed, for example when implementing a pre-existing trait, but a GIL-bound reference is available, this cost can be avoided by exploiting that access to GIL-bound reference gives zero-cost access to a GIL token via `Bound::py`. - -For example, instead of writing - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyList; - -struct Foo(Py); - -struct FooBound<'py>(Bound<'py, PyList>); - -impl PartialEq for FooBound<'_> { - fn eq(&self, other: &Foo) -> bool { - Python::with_gil(|py| { - let len = other.0.bind(py).len(); - self.0.len() == len - }) - } -} -``` - -use the more efficient - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyList; -# struct Foo(Py); -# struct FooBound<'py>(Bound<'py, PyList>); -# -impl PartialEq for FooBound<'_> { - fn eq(&self, other: &Foo) -> bool { - // Access to `&Bound<'py, PyAny>` implies access to `Python<'py>`. - let py = self.0.py(); - let len = other.0.bind(py).len(); - self.0.len() == len - } -} -``` - -## Disable the global reference pool - -PyO3 uses global mutable state to keep track of deferred reference count updates implied by `impl Drop for Py` being called without the GIL being held. The necessary synchronization to obtain and apply these reference count updates when PyO3-based code next acquires the GIL is somewhat expensive and can become a significant part of the cost of crossing the Python-Rust boundary. - -This functionality can be avoided by setting the `pyo3_disable_reference_pool` conditional compilation flag. This removes the global reference pool and the associated costs completely. However, it does _not_ remove the `Drop` implementation for `Py` which is necessary to interoperate with existing Rust code written without PyO3-based code in mind. To stay compatible with the wider Rust ecosystem in these cases, we keep the implementation but abort when `Drop` is called without the GIL being held. If `pyo3_leak_on_drop_without_reference_pool` is additionally enabled, objects dropped without the GIL being held will be leaked instead which is always sound but might have determinal effects like resource exhaustion in the long term. - -This limitation is important to keep in mind when this setting is used, especially when embedding Python code into a Rust application as it is quite easy to accidentally drop a `Py` (or types containing it like `PyErr`, `PyBackedStr` or `PyBackedBytes`) returned from `Python::with_gil` without making sure to re-acquire the GIL beforehand. For example, the following code - -```rust,ignore -# use pyo3::prelude::*; -# use pyo3::types::PyList; -let numbers: Py = Python::with_gil(|py| PyList::empty(py).unbind()); - -Python::with_gil(|py| { - numbers.bind(py).append(23).unwrap(); -}); - -Python::with_gil(|py| { - numbers.bind(py).append(42).unwrap(); -}); -``` - -will abort if the list not explicitly disposed via - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyList; -let numbers: Py = Python::with_gil(|py| PyList::empty(py).unbind()); - -Python::with_gil(|py| { - numbers.bind(py).append(23).unwrap(); -}); - -Python::with_gil(|py| { - numbers.bind(py).append(42).unwrap(); -}); - -Python::with_gil(move |py| { - drop(numbers); -}); -``` - -[conditional-compilation]: https://doc.rust-lang.org/reference/conditional-compilation.html diff --git a/vendor/pyo3/guide/src/python-from-rust.md b/vendor/pyo3/guide/src/python-from-rust.md deleted file mode 100644 index ebb7fa1f4d..0000000000 --- a/vendor/pyo3/guide/src/python-from-rust.md +++ /dev/null @@ -1,50 +0,0 @@ -# Calling Python in Rust code - -This chapter of the guide documents some ways to interact with Python code from Rust. - -Below is an introduction to the `'py` lifetime and some general remarks about how PyO3's API reasons about Python code. - -The subchapters also cover the following topics: - - Python object types available in PyO3's API - - How to work with Python exceptions - - How to call Python functions - - How to execute existing Python code - -## The `'py` lifetime - -To safely interact with the Python interpreter a Rust thread must have a corresponding Python thread state and hold the [Global Interpreter Lock (GIL)](#the-global-interpreter-lock). PyO3 has a `Python<'py>` token that is used to prove that these conditions -are met. Its lifetime `'py` is a central part of PyO3's API. - -The `Python<'py>` token serves three purposes: - -* It provides global APIs for the Python interpreter, such as [`py.eval_bound()`][eval] and [`py.import_bound()`][import]. -* It can be passed to functions that require a proof of holding the GIL, such as [`Py::clone_ref`][clone_ref]. -* Its lifetime `'py` is used to bind many of PyO3's types to the Python interpreter, such as [`Bound<'py, T>`][Bound]. - -PyO3's types that are bound to the `'py` lifetime, for example `Bound<'py, T>`, all contain a `Python<'py>` token. This means they have full access to the Python interpreter and offer a complete API for interacting with Python objects. - -Consult [PyO3's API documentation][obtaining-py] to learn how to acquire one of these tokens. - -### The Global Interpreter Lock - -Concurrent programming in Python is aided by the Global Interpreter Lock (GIL), which ensures that only one Python thread can use the Python interpreter and its API at the same time. This allows it to be used to synchronize code. See the [`pyo3::sync`] module for synchronization tools PyO3 offers that are based on the GIL's guarantees. - -Non-Python operations (system calls and native Rust code) can unlock the GIL. See [the section on parallelism](parallelism.md) for how to do that using PyO3's API. - -## Python's memory model - -Python's memory model differs from Rust's memory model in two key ways: -- There is no concept of ownership; all Python objects are shared and usually implemented via reference counting -- There is no concept of exclusive (`&mut`) references; any reference can mutate a Python object - -PyO3's API reflects this by providing [smart pointer][smart-pointers] types, `Py`, `Bound<'py, T>`, and (the very rarely used) `Borrowed<'a, 'py, T>`. These smart pointers all use Python reference counting. See the [subchapter on types](./types.md) for more detail on these types. - -Because of the lack of exclusive `&mut` references, PyO3's APIs for Python objects, for example [`PyListMethods::append`], use shared references. This is safe because Python objects have internal mechanisms to prevent data races (as of time of writing, the Python GIL). - -[smart-pointers]: https://doc.rust-lang.org/book/ch15-00-smart-pointers.html -[obtaining-py]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#obtaining-a-python-token -[`pyo3::sync`]: {{#PYO3_DOCS_URL}}/pyo3/sync/index.html -[eval]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.eval -[import]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.import_bound -[clone_ref]: {{#PYO3_DOCS_URL}}/pyo3/prelude/struct.Py.html#method.clone_ref -[Bound]: {{#PYO3_DOCS_URL}}/pyo3/struct.Bound.html diff --git a/vendor/pyo3/guide/src/python-from-rust/calling-existing-code.md b/vendor/pyo3/guide/src/python-from-rust/calling-existing-code.md deleted file mode 100644 index 4eba33f11c..0000000000 --- a/vendor/pyo3/guide/src/python-from-rust/calling-existing-code.md +++ /dev/null @@ -1,408 +0,0 @@ -# Executing existing Python code - -If you already have some existing Python code that you need to execute from Rust, the following FAQs can help you select the right PyO3 functionality for your situation: - -## Want to access Python APIs? Then use `PyModule::import`. - -[`PyModule::import`] can be used to get handle to a Python module from Rust. You can use this to import and use any Python -module available in your environment. - -```rust -use pyo3::prelude::*; - -fn main() -> PyResult<()> { - Python::with_gil(|py| { - let builtins = PyModule::import(py, "builtins")?; - let total: i32 = builtins - .getattr("sum")? - .call1((vec![1, 2, 3],))? - .extract()?; - assert_eq!(total, 6); - Ok(()) - }) -} -``` - -[`PyModule::import`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.import - -## Want to run just an expression? Then use `eval`. - -[`Python::eval`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.eval) is -a method to execute a [Python expression](https://docs.python.org/3/reference/expressions.html) -and return the evaluated value as a `Bound<'py, PyAny>` object. - -```rust -use pyo3::prelude::*; -use pyo3::ffi::c_str; - -# fn main() -> Result<(), ()> { -Python::with_gil(|py| { - let result = py - .eval(c_str!("[i * 10 for i in range(5)]"), None, None) - .map_err(|e| { - e.print_and_set_sys_last_vars(py); - })?; - let res: Vec = result.extract().unwrap(); - assert_eq!(res, vec![0, 10, 20, 30, 40]); - Ok(()) -}) -# } -``` - -## Want to run statements? Then use `run`. - -[`Python::run`] is a method to execute one or more -[Python statements](https://docs.python.org/3/reference/simple_stmts.html). -This method returns nothing (like any Python statement), but you can get -access to manipulated objects via the `locals` dict. - -You can also use the [`py_run!`] macro, which is a shorthand for [`Python::run`]. -Since [`py_run!`] panics on exceptions, we recommend you use this macro only for -quickly testing your Python extensions. - -[`Python::run`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.run - -```rust -use pyo3::prelude::*; -use pyo3::py_run; - -# fn main() { -#[pyclass] -struct UserData { - id: u32, - name: String, -} - -#[pymethods] -impl UserData { - fn as_tuple(&self) -> (u32, String) { - (self.id, self.name.clone()) - } - - fn __repr__(&self) -> PyResult { - Ok(format!("User {}(id: {})", self.name, self.id)) - } -} - -Python::with_gil(|py| { - let userdata = UserData { - id: 34, - name: "Yu".to_string(), - }; - let userdata = Py::new(py, userdata).unwrap(); - let userdata_as_tuple = (34, "Yu"); - py_run!(py, userdata userdata_as_tuple, r#" -assert repr(userdata) == "User Yu(id: 34)" -assert userdata.as_tuple() == userdata_as_tuple - "#); -}) -# } -``` - -## You have a Python file or code snippet? Then use `PyModule::from_code`. - -[`PyModule::from_code`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.from_code) -can be used to generate a Python module which can then be used just as if it was imported with -`PyModule::import`. - -**Warning**: This will compile and execute code. **Never** pass untrusted code -to this function! - -```rust -use pyo3::{prelude::*, types::IntoPyDict}; -use pyo3_ffi::c_str; - -# fn main() -> PyResult<()> { -Python::with_gil(|py| { - let activators = PyModule::from_code( - py, - c_str!(r#" -def relu(x): - """see https://en.wikipedia.org/wiki/Rectifier_(neural_networks)""" - return max(0.0, x) - -def leaky_relu(x, slope=0.01): - return x if x >= 0 else x * slope - "#), - c_str!("activators.py"), - c_str!("activators"), - )?; - - let relu_result: f64 = activators.getattr("relu")?.call1((-1.0,))?.extract()?; - assert_eq!(relu_result, 0.0); - - let kwargs = [("slope", 0.2)].into_py_dict(py)?; - let lrelu_result: f64 = activators - .getattr("leaky_relu")? - .call((-1.0,), Some(&kwargs))? - .extract()?; - assert_eq!(lrelu_result, -0.2); -# Ok(()) -}) -# } -``` - -## Want to embed Python in Rust with additional modules? - -Python maintains the `sys.modules` dict as a cache of all imported modules. -An import in Python will first attempt to lookup the module from this dict, -and if not present will use various strategies to attempt to locate and load -the module. - -The [`append_to_inittab`]({{#PYO3_DOCS_URL}}/pyo3/macro.append_to_inittab.html) -macro can be used to add additional `#[pymodule]` modules to an embedded -Python interpreter. The macro **must** be invoked _before_ initializing Python. - -As an example, the below adds the module `foo` to the embedded interpreter: - -```rust -use pyo3::prelude::*; -use pyo3::ffi::c_str; - -#[pyfunction] -fn add_one(x: i64) -> i64 { - x + 1 -} - -#[pymodule] -fn foo(foo_module: &Bound<'_, PyModule>) -> PyResult<()> { - foo_module.add_function(wrap_pyfunction!(add_one, foo_module)?)?; - Ok(()) -} - -fn main() -> PyResult<()> { - pyo3::append_to_inittab!(foo); - Python::with_gil(|py| Python::run(py, c_str!("import foo; foo.add_one(6)"), None, None)) -} -``` - -If `append_to_inittab` cannot be used due to constraints in the program, -an alternative is to create a module using [`PyModule::new`] -and insert it manually into `sys.modules`: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyDict; -use pyo3::ffi::c_str; - -#[pyfunction] -pub fn add_one(x: i64) -> i64 { - x + 1 -} - -fn main() -> PyResult<()> { - Python::with_gil(|py| { - // Create new module - let foo_module = PyModule::new(py, "foo")?; - foo_module.add_function(wrap_pyfunction!(add_one, &foo_module)?)?; - - // Import and get sys.modules - let sys = PyModule::import(py, "sys")?; - let py_modules: Bound<'_, PyDict> = sys.getattr("modules")?.downcast_into()?; - - // Insert foo into sys.modules - py_modules.set_item("foo", foo_module)?; - - // Now we can import + run our python code - Python::run(py, c_str!("import foo; foo.add_one(6)"), None, None) - }) -} -``` - -## Include multiple Python files - -You can include a file at compile time by using -[`std::include_str`](https://doc.rust-lang.org/std/macro.include_str.html) macro. - -Or you can load a file at runtime by using -[`std::fs::read_to_string`](https://doc.rust-lang.org/std/fs/fn.read_to_string.html) function. - -Many Python files can be included and loaded as modules. If one file depends on -another you must preserve correct order while declaring `PyModule`. - -Example directory structure: -```text -. -├── Cargo.lock -├── Cargo.toml -├── python_app -│ ├── app.py -│ └── utils -│ └── foo.py -└── src - └── main.rs -``` - -`python_app/app.py`: -```python -from utils.foo import bar - - -def run(): - return bar() -``` - -`python_app/utils/foo.py`: -```python -def bar(): - return "baz" -``` - -The example below shows: -* how to include content of `app.py` and `utils/foo.py` into your rust binary -* how to call function `run()` (declared in `app.py`) that needs function - imported from `utils/foo.py` - -`src/main.rs`: -```rust,ignore -use pyo3::prelude::*; -use pyo3_ffi::c_str; - -fn main() -> PyResult<()> { - let py_foo = c_str!(include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/python_app/utils/foo.py" - ))); - let py_app = c_str!(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/python_app/app.py"))); - let from_python = Python::with_gil(|py| -> PyResult> { - PyModule::from_code(py, py_foo, c_str!("utils.foo"), c_str!("utils.foo"))?; - let app: Py = PyModule::from_code(py, py_app, c_str!(""), c_str!(""))? - .getattr("run")? - .into(); - app.call0(py) - }); - - println!("py: {}", from_python?); - Ok(()) -} -``` - -The example below shows: -* how to load content of `app.py` at runtime so that it sees its dependencies - automatically -* how to call function `run()` (declared in `app.py`) that needs function - imported from `utils/foo.py` - -It is recommended to use absolute paths because then your binary can be run -from anywhere as long as your `app.py` is in the expected directory (in this example -that directory is `/usr/share/python_app`). - -`src/main.rs`: -```rust,no_run -use pyo3::prelude::*; -use pyo3::types::PyList; -use pyo3_ffi::c_str; -use std::fs; -use std::path::Path; -use std::ffi::CString; - -fn main() -> PyResult<()> { - let path = Path::new("/usr/share/python_app"); - let py_app = CString::new(fs::read_to_string(path.join("app.py"))?)?; - let from_python = Python::with_gil(|py| -> PyResult> { - let syspath = py - .import("sys")? - .getattr("path")? - .downcast_into::()?; - syspath.insert(0, path)?; - let app: Py = PyModule::from_code(py, py_app.as_c_str(), c_str!(""), c_str!(""))? - .getattr("run")? - .into(); - app.call0(py) - }); - - println!("py: {}", from_python?); - Ok(()) -} -``` - - -[`Python::run`]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.run -[`py_run!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.py_run.html - -## Need to use a context manager from Rust? - -Use context managers by directly invoking `__enter__` and `__exit__`. - -```rust -use pyo3::prelude::*; -use pyo3::ffi::c_str; - -fn main() { - Python::with_gil(|py| { - let custom_manager = PyModule::from_code( - py, - c_str!(r#" -class House(object): - def __init__(self, address): - self.address = address - def __enter__(self): - print(f"Welcome to {self.address}!") - def __exit__(self, type, value, traceback): - if type: - print(f"Sorry you had {type} trouble at {self.address}") - else: - print(f"Thank you for visiting {self.address}, come again soon!") - - "#), - c_str!("house.py"), - c_str!("house"), - ) - .unwrap(); - - let house_class = custom_manager.getattr("House").unwrap(); - let house = house_class.call1(("123 Main Street",)).unwrap(); - - house.call_method0("__enter__").unwrap(); - - let result = py.eval(c_str!("undefined_variable + 1"), None, None); - - // If the eval threw an exception we'll pass it through to the context manager. - // Otherwise, __exit__ is called with empty arguments (Python "None"). - match result { - Ok(_) => { - let none = py.None(); - house - .call_method1("__exit__", (&none, &none, &none)) - .unwrap(); - } - Err(e) => { - house - .call_method1( - "__exit__", - ( - e.get_type(py), - e.value(py), - e.traceback(py), - ), - ) - .unwrap(); - } - } - }) -} -``` - -## Handling system signals/interrupts (Ctrl-C) - -The best way to handle system signals when running Rust code is to periodically call `Python::check_signals` to handle any signals captured by Python's signal handler. See also [the FAQ entry](../faq.md#ctrl-c-doesnt-do-anything-while-my-rust-code-is-executing). - -Alternatively, set Python's `signal` module to take the default action for a signal: - -```rust -use pyo3::prelude::*; - -# fn main() -> PyResult<()> { -Python::with_gil(|py| -> PyResult<()> { - let signal = py.import("signal")?; - // Set SIGINT to have the default action - signal - .getattr("signal")? - .call1((signal.getattr("SIGINT")?, signal.getattr("SIG_DFL")?))?; - Ok(()) -}) -# } -``` - - -[`PyModule::new`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.new diff --git a/vendor/pyo3/guide/src/python-from-rust/function-calls.md b/vendor/pyo3/guide/src/python-from-rust/function-calls.md deleted file mode 100644 index 2e5cf3c589..0000000000 --- a/vendor/pyo3/guide/src/python-from-rust/function-calls.md +++ /dev/null @@ -1,108 +0,0 @@ -# Calling Python functions - -The `Bound<'py, T>` smart pointer (such as `Bound<'py, PyAny>`, `Bound<'py, PyList>`, or `Bound<'py, MyClass>`) can be used to call Python functions. - -PyO3 offers two APIs to make function calls: - -* [`call`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call) - call any callable Python object. -* [`call_method`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call_method) - call a method on the Python object. - -Both of these APIs take `args` and `kwargs` arguments (for positional and keyword arguments respectively). There are variants for less complex calls: - -* [`call1`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call1) and [`call_method1`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call_method1) to call only with positional `args`. -* [`call0`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call0) and [`call_method0`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call_method0) to call with no arguments. - -For convenience the [`Py`](../types.md#pyt-and-pyobject) smart pointer also exposes these same six API methods, but needs a `Python` token as an additional first argument to prove the GIL is held. - -The example below calls a Python function behind a `PyObject` (aka `Py`) reference: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyTuple; -use pyo3_ffi::c_str; - -fn main() -> PyResult<()> { - let arg1 = "arg1"; - let arg2 = "arg2"; - let arg3 = "arg3"; - - Python::with_gil(|py| { - let fun: Py = PyModule::from_code( - py, - c_str!("def example(*args, **kwargs): - if args != (): - print('called with args', args) - if kwargs != {}: - print('called with kwargs', kwargs) - if args == () and kwargs == {}: - print('called with no arguments')"), - c_str!(""), - c_str!(""), - )? - .getattr("example")? - .into(); - - // call object without any arguments - fun.call0(py)?; - - // pass object with Rust tuple of positional arguments - let args = (arg1, arg2, arg3); - fun.call1(py, args)?; - - // call object with Python tuple of positional arguments - let args = PyTuple::new(py, &[arg1, arg2, arg3])?; - fun.call1(py, args)?; - Ok(()) - }) -} -``` - -## Creating keyword arguments - -For the `call` and `call_method` APIs, `kwargs` are `Option<&Bound<'py, PyDict>>`, so can either be `None` or `Some(&dict)`. You can use the [`IntoPyDict`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.IntoPyDict.html) trait to convert other dict-like containers, e.g. `HashMap` or `BTreeMap`, as well as tuples with up to 10 elements and `Vec`s where each element is a two-element tuple. - -```rust -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; -use std::collections::HashMap; -use pyo3_ffi::c_str; - -fn main() -> PyResult<()> { - let key1 = "key1"; - let val1 = 1; - let key2 = "key2"; - let val2 = 2; - - Python::with_gil(|py| { - let fun: Py = PyModule::from_code( - py, - c_str!("def example(*args, **kwargs): - if args != (): - print('called with args', args) - if kwargs != {}: - print('called with kwargs', kwargs) - if args == () and kwargs == {}: - print('called with no arguments')"), - c_str!(""), - c_str!(""), - )? - .getattr("example")? - .into(); - - // call object with PyDict - let kwargs = [(key1, val1)].into_py_dict(py)?; - fun.call(py, (), Some(&kwargs))?; - - // pass arguments as Vec - let kwargs = vec![(key1, val1), (key2, val2)]; - fun.call(py, (), Some(&kwargs.into_py_dict(py)?))?; - - // pass arguments as HashMap - let mut kwargs = HashMap::<&str, i32>::new(); - kwargs.insert(key1, 1); - fun.call(py, (), Some(&kwargs.into_py_dict(py)?))?; - - Ok(()) - }) -} -``` \ No newline at end of file diff --git a/vendor/pyo3/guide/src/python-typing-hints.md b/vendor/pyo3/guide/src/python-typing-hints.md deleted file mode 100644 index 43a15a21a6..0000000000 --- a/vendor/pyo3/guide/src/python-typing-hints.md +++ /dev/null @@ -1,167 +0,0 @@ -# Typing and IDE hints for your Python package - -PyO3 provides an easy to use interface to code native Python libraries in Rust. The accompanying Maturin allows you to build and publish them as a package. Yet, for a better user experience, Python libraries should provide typing hints and documentation for all public entities, so that IDEs can show them during development and type analyzing tools such as `mypy` can use them to properly verify the code. - -Currently the best solution for the problem is to manually maintain `*.pyi` files and ship them along with the package. - -There is a sketch of a roadmap towards completing [the `experimental-inspect` feature](./features.md#experimental-inspect) which may eventually lead to automatic type annotations generated by PyO3. This needs more testing and implementation, please see [issue #2454](https://github.com/PyO3/pyo3/issues/2454). - -## Introduction to `pyi` files - -`pyi` files (an abbreviation for `Python Interface`) are called "stub files" in most of the documentation related to them. A very good definition of what it is can be found in [old MyPy documentation](https://github.com/python/mypy/wiki/Creating-Stubs-For-Python-Modules): - -> A stubs file only contains a description of the public interface of the module without any implementations. - -There is also [extensive documentation on type stubs on the official Python typing documentation](https://typing.readthedocs.io/en/latest/source/stubs.html). - -Most Python developers probably already encountered them when trying to use their IDE's "Go to Definition" function on any builtin type. For example, the definitions of a few standard exceptions look like this: - -```python -class BaseException(object): - args: Tuple[Any, ...] - __cause__: BaseException | None - __context__: BaseException | None - __suppress_context__: bool - __traceback__: TracebackType | None - def __init__(self, *args: object) -> None: ... - def __str__(self) -> str: ... - def __repr__(self) -> str: ... - def with_traceback(self: _TBE, tb: TracebackType | None) -> _TBE: ... - -class SystemExit(BaseException): - code: int - -class Exception(BaseException): ... - -class StopIteration(Exception): - value: Any -``` - -As we can see, those are not full definitions containing implementation, but just a description of the interface. It is usually all that the user of the library needs. - -### What do the PEPs say? - -At the time of writing this documentation, the `pyi` files are referenced in three PEPs. - -[PEP8 - Style Guide for Python Code - #Function Annotations](https://www.python.org/dev/peps/pep-0008/#function-annotations) (last point) recommends all third party library creators to provide stub files as the source of knowledge about the package for type checker tools. - -> (...) it is expected that users of third party library packages may want to run type checkers over those packages. For this purpose [PEP 484](https://www.python.org/dev/peps/pep-0484) recommends the use of stub files: .pyi files that are read by the type checker in preference of the corresponding .py files. (...) - -[PEP484 - Type Hints - #Stub Files](https://www.python.org/dev/peps/pep-0484/#stub-files) defines stub files as follows. - -> Stub files are files containing type hints that are only for use by the type checker, not at runtime. - -It contains a specification for them (highly recommended reading, since it contains at least one thing that is not used in normal Python code) and also some general information about where to store the stub files. - -[PEP561 - Distributing and Packaging Type Information](https://www.python.org/dev/peps/pep-0561/) describes in detail how to build packages that will enable type checking. In particular it contains information about how the stub files must be distributed in order for type checkers to use them. - -## How to do it? - -[PEP561](https://www.python.org/dev/peps/pep-0561/) recognizes three ways of distributing type information: - -* `inline` - the typing is placed directly in source (`py`) files; -* `separate package with stub files` - the typing is placed in `pyi` files distributed in their own, separate package; -* `in-package stub files` - the typing is placed in `pyi` files distributed in the same package as source files. - -The first way is tricky with PyO3 since we do not have `py` files. When it has been investigated and necessary changes are implemented, this document will be updated. - -The second way is easy to do, and the whole work can be fully separated from the main library code. The example repo for the package with stub files can be found in [PEP561 references section](https://www.python.org/dev/peps/pep-0561/#references): [Stub package repository](https://github.com/ethanhs/stub-package) - -The third way is described below. - -### Including `pyi` files in your PyO3/Maturin build package - -When source files are in the same package as stub files, they should be placed next to each other. We need a way to do that with Maturin. Also, in order to mark our package as typing-enabled we need to add an empty file named `py.typed` to the package. - -#### If you do not have other Python files - -If you do not need to add any other Python files apart from `pyi` to the package, Maturin provides a way to do most of the work for you. As documented in the [Maturin Guide](https://github.com/PyO3/maturin/#mixed-rustpython-projects), the only thing you need to do is to create a stub file for your module named `.pyi` in your project root and Maturin will do the rest. - -```text -my-rust-project/ -├── Cargo.toml -├── my_project.pyi # <<< add type stubs for Rust functions in the my_project module here -├── pyproject.toml -└── src - └── lib.rs -``` - -For an example `pyi` file see the [`my_project.pyi` content](#my_projectpyi-content) section. - -#### If you need other Python files - -If you need to add other Python files apart from `pyi` to the package, you can do it also, but that requires some more work. Maturin provides an easy way to add files to a package ([documentation](https://github.com/PyO3/maturin/blob/0dee40510083c03607834c821eea76964140a126/Readme.md#mixed-rustpython-projects)). You just need to create a folder with the name of your module next to the `Cargo.toml` file (for customization see documentation linked above). - -The folder structure would be: - -```text -my-project -├── Cargo.toml -├── my_project -│ ├── __init__.py -│ ├── my_project.pyi -│ ├── other_python_file.py -│ └── py.typed -├── pyproject.toml -├── Readme.md -└── src - └── lib.rs -``` - -Let's go a little bit more into detail regarding the files inside the package folder. - -##### `__init__.py` content - -As we now specify our own package content, we have to provide the `__init__.py` file, so the folder is treated as a package and we can import things from it. We can always use the same content that Maturin creates for us if we do not specify a Python source folder. For PyO3 bindings it would be: - -```python -from .my_project import * -``` - -That way everything that is exposed by our native module can be imported directly from the package. - -##### `py.typed` requirement - -As stated in [PEP561](https://www.python.org/dev/peps/pep-0561/): -> Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing. This marker applies recursively: if a top-level package includes it, all its sub-packages MUST support type checking as well. - -If we do not include that file, some IDEs might still use our `pyi` files to show hints, but the type checkers might not. MyPy will raise an error in this situation: - -```text -error: Skipping analyzing "my_project": found module but no type hints or library stubs -``` - -The file is just a marker file, so it should be empty. - -##### `my_project.pyi` content - -Our module stub file. This document does not aim at describing how to write them, since you can find a lot of documentation on it, starting from the already quoted [PEP484](https://www.python.org/dev/peps/pep-0484/#stub-files). - -The example can look like this: - -```python -class Car: - """ - A class representing a car. - - :param body_type: the name of body type, e.g. hatchback, sedan - :param horsepower: power of the engine in horsepower - """ - def __init__(self, body_type: str, horsepower: int) -> None: ... - - @classmethod - def from_unique_name(cls, name: str) -> 'Car': - """ - Creates a Car based on unique name - - :param name: model name of a car to be created - :return: a Car instance with default data - """ - - def best_color(self) -> str: - """ - Gets the best color for the car. - - :return: the name of the color our great algorithm thinks is the best for this car - """ -``` diff --git a/vendor/pyo3/guide/src/rust-from-python.md b/vendor/pyo3/guide/src/rust-from-python.md deleted file mode 100644 index 3b525d399d..0000000000 --- a/vendor/pyo3/guide/src/rust-from-python.md +++ /dev/null @@ -1,13 +0,0 @@ -# Using Rust from Python - -This chapter of the guide is dedicated to explaining how to wrap Rust code into Python objects. - -PyO3 uses Rust's "procedural macros" to provide a powerful yet simple API to denote what Rust code should map into Python objects. - -PyO3 can create three types of Python objects: - -- Python modules, via the `#[pymodule]` macro -- Python functions, via the `#[pyfunction]` macro -- Python classes, via the `#[pyclass]` macro (plus `#[pymethods]` to define methods for those classes) - -The following subchapters go through each of these in turn. diff --git a/vendor/pyo3/guide/src/trait-bounds.md b/vendor/pyo3/guide/src/trait-bounds.md deleted file mode 100644 index e1b8e82f1d..0000000000 --- a/vendor/pyo3/guide/src/trait-bounds.md +++ /dev/null @@ -1,555 +0,0 @@ -# Using in Python a Rust function with trait bounds - -PyO3 allows for easy conversion from Rust to Python for certain functions and classes (see the [conversion table](conversions/tables.md)). -However, it is not always straightforward to convert Rust code that requires a given trait implementation as an argument. - -This tutorial explains how to convert a Rust function that takes a trait as argument for use in Python with classes implementing the same methods as the trait. - -Why is this useful? - -### Pros -- Make your Rust code available to Python users -- Code complex algorithms in Rust with the help of the borrow checker - -### Cons -- Not as fast as native Rust (type conversion has to be performed and one part of the code runs in Python) -- You need to adapt your code to expose it - -## Example - -Let's work with the following basic example of an implementation of a optimization solver operating on a given model. - -Let's say we have a function `solve` that operates on a model and mutates its state. -The argument of the function can be any model that implements the `Model` trait : - -```rust -# #![allow(dead_code)] -pub trait Model { - fn set_variables(&mut self, inputs: &Vec); - fn compute(&mut self); - fn get_results(&self) -> Vec; -} - -pub fn solve(model: &mut T) { - println!("Magic solver that mutates the model into a resolved state"); -} -``` -Let's assume we have the following constraints: -- We cannot change that code as it runs on many Rust models. -- We also have many Python models that cannot be solved as this solver is not available in that language. -Rewriting it in Python would be cumbersome and error-prone, as everything is already available in Rust. - -How could we expose this solver to Python thanks to PyO3 ? - -## Implementation of the trait bounds for the Python class - -If a Python class implements the same three methods as the `Model` trait, it seems logical it could be adapted to use the solver. -However, it is not possible to pass a `PyObject` to it as it does not implement the Rust trait (even if the Python model has the required methods). - -In order to implement the trait, we must write a wrapper around the calls in Rust to the Python model. -The method signatures must be the same as the trait, keeping in mind that the Rust trait cannot be changed for the purpose of making the code available in Python. - -The Python model we want to expose is the following one, which already contains all the required methods: - -```python -class Model: - def set_variables(self, inputs): - self.inputs = inputs - def compute(self): - self.results = [elt**2 - 3 for elt in self.inputs] - def get_results(self): - return self.results -``` - -The following wrapper will call the Python model from Rust, using a struct to hold the model as a `PyAny` object: - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; -use pyo3::types::PyList; - -# pub trait Model { -# fn set_variables(&mut self, inputs: &Vec); -# fn compute(&mut self); -# fn get_results(&self) -> Vec; -# } - -struct UserModel { - model: Py, -} - -impl Model for UserModel { - fn set_variables(&mut self, var: &Vec) { - println!("Rust calling Python to set the variables"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("set_variables", (PyList::new(py, var).unwrap(),), None) - .unwrap(); - }) - } - - fn get_results(&self) -> Vec { - println!("Rust calling Python to get the results"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("get_results", (), None) - .unwrap() - .extract() - .unwrap() - }) - } - - fn compute(&mut self) { - println!("Rust calling Python to perform the computation"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("compute", (), None) - .unwrap(); - }) - } -} -``` - -Now that this bit is implemented, let's expose the model wrapper to Python. -Let's add the PyO3 annotations and add a constructor: - -```rust -# #![allow(dead_code)] -# pub trait Model { -# fn set_variables(&mut self, inputs: &Vec); -# fn compute(&mut self); -# fn get_results(&self) -> Vec; -# } -# use pyo3::prelude::*; - -#[pyclass] -struct UserModel { - model: Py, -} - -#[pymodule] -fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} - -#[pymethods] -impl UserModel { - #[new] - pub fn new(model: Py) -> Self { - UserModel { model } - } -} -``` - -Now we add the PyO3 annotations to the trait implementation: - -```rust,ignore -#[pymethods] -impl Model for UserModel { - // the previous trait implementation -} -``` - -However, the previous code will not compile. The compilation error is the following one: -`error: #[pymethods] cannot be used on trait impl blocks` - -That's a bummer! -However, we can write a second wrapper around these functions to call them directly. -This wrapper will also perform the type conversions between Python and Rust. - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyList; -# -# pub trait Model { -# fn set_variables(&mut self, inputs: &Vec); -# fn compute(&mut self); -# fn get_results(&self) -> Vec; -# } -# -# #[pyclass] -# struct UserModel { -# model: Py, -# } -# -# impl Model for UserModel { -# fn set_variables(&mut self, var: &Vec) { -# println!("Rust calling Python to set the variables"); -# Python::with_gil(|py| { -# self.model.bind(py) -# .call_method("set_variables", (PyList::new(py, var).unwrap(),), None) -# .unwrap(); -# }) -# } -# -# fn get_results(&self) -> Vec { -# println!("Rust calling Python to get the results"); -# Python::with_gil(|py| { -# self.model -# .bind(py) -# .call_method("get_results", (), None) -# .unwrap() -# .extract() -# .unwrap() -# }) -# } -# -# fn compute(&mut self) { -# println!("Rust calling Python to perform the computation"); -# Python::with_gil(|py| { -# self.model -# .bind(py) -# .call_method("compute", (), None) -# .unwrap(); -# }) -# -# } -# } - -#[pymethods] -impl UserModel { - pub fn set_variables(&mut self, var: Vec) { - println!("Set variables from Python calling Rust"); - Model::set_variables(self, &var) - } - - pub fn get_results(&mut self) -> Vec { - println!("Get results from Python calling Rust"); - Model::get_results(self) - } - - pub fn compute(&mut self) { - println!("Compute from Python calling Rust"); - Model::compute(self) - } -} -``` -This wrapper handles the type conversion between the PyO3 requirements and the trait. -In order to meet PyO3 requirements, this wrapper must: -- return an object of type `PyResult` -- use only values, not references in the method signatures - -Let's run the file python file: - -```python -class Model: - def set_variables(self, inputs): - self.inputs = inputs - def compute(self): - self.results = [elt**2 - 3 for elt in self.inputs] - def get_results(self): - return self.results - -if __name__=="__main__": - import trait_exposure - - myModel = Model() - my_rust_model = trait_exposure.UserModel(myModel) - my_rust_model.set_variables([2.0]) - print("Print value from Python: ", myModel.inputs) - my_rust_model.compute() - print("Print value from Python through Rust: ", my_rust_model.get_results()) - print("Print value directly from Python: ", myModel.get_results()) -``` - -This outputs: - -```block -Set variables from Python calling Rust -Set variables from Rust calling Python -Print value from Python: [2.0] -Compute from Python calling Rust -Compute from Rust calling Python -Get results from Python calling Rust -Get results from Rust calling Python -Print value from Python through Rust: [1.0] -Print value directly from Python: [1.0] -``` - -We have now successfully exposed a Rust model that implements the `Model` trait to Python! - -We will now expose the `solve` function, but before, let's talk about types errors. - -## Type errors in Python - -What happens if you have type errors when using Python and how can you improve the error messages? - - -### Wrong types in Python function arguments - -Let's assume in the first case that you will use in your Python file `my_rust_model.set_variables(2.0)` instead of `my_rust_model.set_variables([2.0])`. - -The Rust signature expects a vector, which corresponds to a list in Python. -What happens if instead of a vector, we pass a single value ? - -At the execution of Python, we get : - -```block -File "main.py", line 15, in - my_rust_model.set_variables(2) -TypeError -``` - -It is a type error and Python points to it, so it's easy to identify and solve. - -### Wrong types in Python method signatures - -Let's assume now that the return type of one of the methods of our Model class is wrong, for example the `get_results` method that is expected to return a `Vec` in Rust, a list in Python. - -```python -class Model: - def set_variables(self, inputs): - self.inputs = inputs - def compute(self): - self.results = [elt**2 -3 for elt in self.inputs] - def get_results(self): - return self.results[0] - #return self.results <-- this is the expected output -``` - -This call results in the following panic: - -```block -pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: PyErr { type: Py(0x10dcf79f0, PhantomData) } -``` - -This error code is not helpful for a Python user that does not know anything about Rust, or someone that does not know PyO3 was used to interface the Rust code. - -However, as we are responsible for making the Rust code available to Python, we can do something about it. - -The issue is that we called `unwrap` anywhere we could, and therefore any panic from PyO3 will be directly forwarded to the end user. - -Let's modify the code performing the type conversion to give a helpful error message to the Python user: - -We used in our `get_results` method the following call that performs the type conversion: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyList; -# -# pub trait Model { -# fn set_variables(&mut self, inputs: &Vec); -# fn compute(&mut self); -# fn get_results(&self) -> Vec; -# } -# -# #[pyclass] -# struct UserModel { -# model: Py, -# } - -impl Model for UserModel { - fn get_results(&self) -> Vec { - println!("Rust calling Python to get the results"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("get_results", (), None) - .unwrap() - .extract() - .unwrap() - }) - } -# fn set_variables(&mut self, var: &Vec) { -# println!("Rust calling Python to set the variables"); -# Python::with_gil(|py| { -# self.model.bind(py) -# .call_method("set_variables", (PyList::new(py, var).unwrap(),), None) -# .unwrap(); -# }) -# } -# -# fn compute(&mut self) { -# println!("Rust calling Python to perform the computation"); -# Python::with_gil(|py| { -# self.model -# .bind(py) -# .call_method("compute", (), None) -# .unwrap(); -# }) -# } -} -``` - -Let's break it down in order to perform better error handling: - -```rust -# #![allow(dead_code)] -# use pyo3::prelude::*; -# use pyo3::types::PyList; -# -# pub trait Model { -# fn set_variables(&mut self, inputs: &Vec); -# fn compute(&mut self); -# fn get_results(&self) -> Vec; -# } -# -# #[pyclass] -# struct UserModel { -# model: Py, -# } - -impl Model for UserModel { - fn get_results(&self) -> Vec { - println!("Get results from Rust calling Python"); - Python::with_gil(|py| { - let py_result: Bound<'_, PyAny> = self - .model - .bind(py) - .call_method("get_results", (), None) - .unwrap(); - - if py_result.get_type().name().unwrap() != "list" { - panic!( - "Expected a list for the get_results() method signature, got {}", - py_result.get_type().name().unwrap() - ); - } - py_result.extract() - }) - .unwrap() - } -# fn set_variables(&mut self, var: &Vec) { -# println!("Rust calling Python to set the variables"); -# Python::with_gil(|py| { -# let py_model = self.model.bind(py) -# .call_method("set_variables", (PyList::new(py, var).unwrap(),), None) -# .unwrap(); -# }) -# } -# -# fn compute(&mut self) { -# println!("Rust calling Python to perform the computation"); -# Python::with_gil(|py| { -# self.model -# .bind(py) -# .call_method("compute", (), None) -# .unwrap(); -# }) -# } -} -``` - -By doing so, you catch the result of the Python computation and check its type in order to be able to deliver a better error message before performing the unwrapping. - -Of course, it does not cover all the possible wrong outputs: -the user could return a list of strings instead of a list of floats. -In this case, a runtime panic would still occur due to PyO3, but with an error message much more difficult to decipher for non-rust user. - -It is up to the developer exposing the rust code to decide how much effort to invest into Python type error handling and improved error messages. - -## The final code - -Now let's expose the `solve()` function to make it available from Python. - -It is not possible to directly expose the `solve` function to Python, as the type conversion cannot be performed. -It requires an object implementing the `Model` trait as input. - -However, the `UserModel` already implements this trait. -Because of this, we can write a function wrapper that takes the `UserModel`--which has already been exposed to Python--as an argument in order to call the core function `solve`. - -It is also required to make the struct public. - -```rust -# #![allow(dead_code)] -use pyo3::prelude::*; -use pyo3::types::PyList; - -pub trait Model { - fn set_variables(&mut self, var: &Vec); - fn get_results(&self) -> Vec; - fn compute(&mut self); -} - -pub fn solve(model: &mut T) { - println!("Magic solver that mutates the model into a resolved state"); -} - -#[pyfunction] -#[pyo3(name = "solve")] -pub fn solve_wrapper(model: &mut UserModel) { - solve(model); -} - -#[pyclass] -pub struct UserModel { - model: Py, -} - -#[pymodule] -fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?; - Ok(()) -} - -#[pymethods] -impl UserModel { - #[new] - pub fn new(model: Py) -> Self { - UserModel { model } - } - - pub fn set_variables(&mut self, var: Vec) { - println!("Set variables from Python calling Rust"); - Model::set_variables(self, &var) - } - - pub fn get_results(&mut self) -> Vec { - println!("Get results from Python calling Rust"); - Model::get_results(self) - } - - pub fn compute(&mut self) { - Model::compute(self) - } -} - -impl Model for UserModel { - fn set_variables(&mut self, var: &Vec) { - println!("Rust calling Python to set the variables"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("set_variables", (PyList::new(py, var).unwrap(),), None) - .unwrap(); - }) - } - - fn get_results(&self) -> Vec { - println!("Get results from Rust calling Python"); - Python::with_gil(|py| { - let py_result: Bound<'_, PyAny> = self - .model - .bind(py) - .call_method("get_results", (), None) - .unwrap(); - - if py_result.get_type().name().unwrap() != "list" { - panic!( - "Expected a list for the get_results() method signature, got {}", - py_result.get_type().name().unwrap() - ); - } - py_result.extract() - }) - .unwrap() - } - - fn compute(&mut self) { - println!("Rust calling Python to perform the computation"); - Python::with_gil(|py| { - self.model - .bind(py) - .call_method("compute", (), None) - .unwrap(); - }) - } -} -``` diff --git a/vendor/pyo3/guide/src/types.md b/vendor/pyo3/guide/src/types.md deleted file mode 100644 index 06559d49d1..0000000000 --- a/vendor/pyo3/guide/src/types.md +++ /dev/null @@ -1,331 +0,0 @@ -# Python object types - -PyO3 offers two main sets of types to interact with Python objects. This section of the guide expands into detail about these types and how to choose which to use. - -The first set of types are the [smart pointers][smart-pointers] which all Python objects are wrapped in. These are `Py`, `Bound<'py, T>`, and `Borrowed<'a, 'py, T>`. The [first section below](#pyo3s-smart-pointers) expands on each of these in detail and why there are three of them. - -The second set of types are types which fill in the generic parameter `T` of the smart pointers. The most common is `PyAny`, which represents any Python object (similar to Python's `typing.Any`). There are also concrete types for many Python built-in types, such as `PyList`, `PyDict`, and `PyTuple`. User defined `#[pyclass]` types also fit this category. The [second section below](#concrete-python-types) expands on how to use these types. - -## PyO3's smart pointers - -PyO3's API offers three generic smart pointers: `Py`, `Bound<'py, T>` and `Borrowed<'a, 'py, T>`. For each of these the type parameter `T` will be filled by a [concrete Python type](#concrete-python-types). For example, a Python list object can be represented by `Py`, `Bound<'py, PyList>`, and `Borrowed<'a, 'py, PyList>`. - -These smart pointers behave differently due to their lifetime parameters. `Py` has no lifetime parameters, `Bound<'py, T>` has [the `'py` lifetime](./python-from-rust.md#the-py-lifetime) as a parameter, and `Borrowed<'a, 'py, T>` has the `'py` lifetime plus an additional lifetime `'a` to denote the lifetime it is borrowing data for. (You can read more about these lifetimes in the subsections below). - -Python objects are reference counted, like [`std::sync::Arc`](https://doc.rust-lang.org/stable/std/sync/struct.Arc.html). A major reason for these smart pointers is to bring Python's reference counting to a Rust API. - -The recommendation of when to use each of these smart pointers is as follows: - -- Use `Bound<'py, T>` for as much as possible, as it offers the most efficient and complete API. -- Use `Py` mostly just for storage inside Rust `struct`s which do not want to or can't add a lifetime parameter for `Bound<'py, T>`. -- `Borrowed<'a, 'py, T>` is almost never used. It is occasionally present at the boundary between Rust and the Python interpreter, for example when borrowing data from Python tuples (which is safe because they are immutable). - -The sections below also explain these smart pointers in a little more detail. - -### `Py` (and `PyObject`) - -[`Py`][Py] is the foundational smart pointer in PyO3's API. The type parameter `T` denotes the type of the Python object. Very frequently this is `PyAny`, meaning any Python object. This is so common that `Py` has a type alias `PyObject`. - -Because `Py` is not bound to [the `'py` lifetime](./python-from-rust.md#the-py-lifetime), it is the type to use when storing a Python object inside a Rust `struct` or `enum` which do not want to have a lifetime parameter. In particular, [`#[pyclass]`][pyclass] types are not permitted to have a lifetime, so `Py` is the correct type to store Python objects inside them. - -The lack of binding to the `'py` lifetime also carries drawbacks: - - Almost all methods on `Py` require a `Python<'py>` token as the first argument - - Other functionality, such as [`Drop`][Drop], needs to check at runtime for attachment to the Python GIL, at a small performance cost - -Because of the drawbacks `Bound<'py, T>` is preferred for many of PyO3's APIs. In particular, `Bound<'py, T>` is better for function arguments. - -To convert a `Py` into a `Bound<'py, T>`, the `Py::bind` and `Py::into_bound` methods are available. `Bound<'py, T>` can be converted back into `Py` using [`Bound::unbind`]. - -### `Bound<'py, T>` - -[`Bound<'py, T>`][Bound] is the counterpart to `Py` which is also bound to the `'py` lifetime. It can be thought of as equivalent to the Rust tuple `(Python<'py>, Py)`. - -By having the binding to the `'py` lifetime, `Bound<'py, T>` can offer the complete PyO3 API at maximum efficiency. This means that `Bound<'py, T>` should usually be used whenever carrying this lifetime is acceptable, and `Py` otherwise. - -`Bound<'py, T>` engages in Python reference counting. This means that `Bound<'py, T>` owns a Python object. Rust code which just wants to borrow a Python object should use a shared reference `&Bound<'py, T>`. Just like `std::sync::Arc`, using `.clone()` and `drop()` will cheaply increment and decrement the reference count of the object (just in this case, the reference counting is implemented by the Python interpreter itself). - -To give an example of how `Bound<'py, T>` is PyO3's primary API type, consider the following Python code: - -```python -def example(): - x = list() # create a Python list - x.append(1) # append the integer 1 to it - y = x # create a second reference to the list - del x # delete the original reference -``` - -Using PyO3's API, and in particular `Bound<'py, PyList>`, this code translates into the following Rust code: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyList; - -fn example<'py>(py: Python<'py>) -> PyResult<()> { - let x: Bound<'py, PyList> = PyList::empty(py); - x.append(1)?; - let y: Bound<'py, PyList> = x.clone(); // y is a new reference to the same list - drop(x); // release the original reference x - Ok(()) -} -# Python::with_gil(example).unwrap(); -``` - -Or, without the type annotations: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyList; - -fn example(py: Python<'_>) -> PyResult<()> { - let x = PyList::empty(py); - x.append(1)?; - let y = x.clone(); - drop(x); - Ok(()) -} -# Python::with_gil(example).unwrap(); -``` - -#### Function argument lifetimes - -Because the `'py` lifetime often appears in many function arguments as part of the `Bound<'py, T>` smart pointer, the Rust compiler will often require annotations of input and output lifetimes. This occurs when the function output has at least one lifetime, and there is more than one lifetime present on the inputs. - -To demonstrate, consider this function which takes accepts Python objects and applies the [Python `+` operation][PyAnyMethods::add] to them: - -```rust,compile_fail -# use pyo3::prelude::*; -fn add(left: &'_ Bound<'_, PyAny>, right: &'_ Bound<'_, PyAny>) -> PyResult> { - left.add(right) -} -``` - -Because the Python `+` operation might raise an exception, this function returns `PyResult>`. It doesn't need ownership of the inputs, so it takes `&Bound<'_, PyAny>` shared references. To demonstrate the point, all lifetimes have used the wildcard `'_` to allow the Rust compiler to attempt to infer them. Because there are four input lifetimes (two lifetimes of the shared references, and two `'py` lifetimes unnamed inside the `Bound<'_, PyAny>` pointers), the compiler cannot reason about which must be connected to the output. - -The correct way to solve this is to add the `'py` lifetime as a parameter for the function, and name all the `'py` lifetimes inside the `Bound<'py, PyAny>` smart pointers. For the shared references, it's also fine to reduce `&'_` to just `&`. The working end result is below: - -```rust -# use pyo3::prelude::*; -fn add<'py>( - left: &Bound<'py, PyAny>, - right: &Bound<'py, PyAny>, -) -> PyResult> { - left.add(right) -} -# Python::with_gil(|py| { -# let s = pyo3::types::PyString::new(py, "s"); -# assert!(add(&s, &s).unwrap().eq("ss").unwrap()); -# }) -``` - -If naming the `'py` lifetime adds unwanted complexity to the function signature, it is also acceptable to return `PyObject` (aka `Py`), which has no lifetime. The cost is instead paid by a slight increase in implementation complexity, as seen by the introduction of a call to [`Bound::unbind`]: - -```rust -# use pyo3::prelude::*; -fn add(left: &Bound<'_, PyAny>, right: &Bound<'_, PyAny>) -> PyResult { - let output: Bound<'_, PyAny> = left.add(right)?; - Ok(output.unbind()) -} -# Python::with_gil(|py| { -# let s = pyo3::types::PyString::new(py, "s"); -# assert!(add(&s, &s).unwrap().bind(py).eq("ss").unwrap()); -# }) -``` - -### `Borrowed<'a, 'py, T>` - -[`Borrowed<'a, 'py, T>`][Borrowed] is an advanced type used just occasionally at the edge of interaction with the Python interpreter. It can be thought of as analogous to the shared reference `&'a Bound<'py, T>`. The difference is that `Borrowed<'a, 'py, T>` is just a smart pointer rather than a reference-to-a-smart-pointer, which is a helpful reduction in indirection in specific interactions with the Python interpreter. - -`Borrowed<'a, 'py, T>` dereferences to `Bound<'py, T>`, so all methods on `Bound<'py, T>` are available on `Borrowed<'a, 'py, T>`. - -An example where `Borrowed<'a, 'py, T>` is used is in [`PyTupleMethods::get_borrowed_item`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyTupleMethods.html#tymethod.get_item): - -```rust -use pyo3::prelude::*; -use pyo3::types::PyTuple; - -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -// Create a new tuple with the elements (0, 1, 2) -let t = PyTuple::new(py, [0, 1, 2])?; -for i in 0..=2 { - let entry: Borrowed<'_, 'py, PyAny> = t.get_borrowed_item(i)?; - // `PyAnyMethods::extract` is available on `Borrowed` - // via the dereference to `Bound` - let value: usize = entry.extract()?; - assert_eq!(i, value); -} -# Ok(()) -# } -# Python::with_gil(example).unwrap(); -``` - -### Casting between smart pointer types - -To convert between `Py` and `Bound<'py, T>` use the `bind()` / `into_bound()` methods. Use the `as_unbound()` / `unbind()` methods to go back from `Bound<'py, T>` to `Py`. - -```rust,ignore -let obj: Py = ...; -let bound: &Bound<'py, PyAny> = obj.bind(py); -let bound: Bound<'py, PyAny> = obj.into_bound(py); - -let obj: &Py = bound.as_unbound(); -let obj: Py = bound.unbind(); -``` - -To convert between `Bound<'py, T>` and `Borrowed<'a, 'py, T>` use the `as_borrowed()` method. `Borrowed<'a, 'py, T>` has a deref coercion to `Bound<'py, T>`. Use the `to_owned()` method to increment the Python reference count and to create a new `Bound<'py, T>` from the `Borrowed<'a, 'py, T>`. - -```rust,ignore -let bound: Bound<'py, PyAny> = ...; -let borrowed: Borrowed<'_, 'py, PyAny> = bound.as_borrowed(); - -// deref coercion -let bound: &Bound<'py, PyAny> = &borrowed; - -// create a new Bound by increase the Python reference count -let bound: Bound<'py, PyAny> = borrowed.to_owned(); -``` - -To convert between `Py` and `Borrowed<'a, 'py, T>` use the `bind_borrowed()` method. Use either `as_unbound()` or `.to_owned().unbind()` to go back to `Py` from `Borrowed<'a, 'py, T>`, via `Bound<'py, T>`. - -```rust,ignore -let obj: Py = ...; -let borrowed: Borrowed<'_, 'py, PyAny> = bound.as_borrowed(); - -// via deref coercion to Bound and then using Bound::as_unbound -let obj: &Py = borrowed.as_unbound(); - -// via a new Bound by increasing the Python reference count, and unbind it -let obj: Py = borrowed.to_owned().unbind(). -``` - -## Concrete Python types - -In all of `Py`, `Bound<'py, T>`, and `Borrowed<'a, 'py, T>`, the type parameter `T` denotes the type of the Python object referred to by the smart pointer. - -This parameter `T` can be filled by: - - [`PyAny`][PyAny], which represents any Python object, - - Native Python types such as `PyList`, `PyTuple`, and `PyDict`, and - - [`#[pyclass]`][pyclass] types defined from Rust - -The following subsections covers some further detail about how to work with these types: -- the APIs that are available for these concrete types, -- how to cast `Bound<'py, T>` to a specific concrete type, and -- how to get Rust data out of a `Bound<'py, T>`. - -### Using APIs for concrete Python types - -Each concrete Python type such as `PyAny`, `PyTuple` and `PyDict` exposes its API on the corresponding bound smart pointer `Bound<'py, PyAny>`, `Bound<'py, PyTuple>` and `Bound<'py, PyDict>`. - -Each type's API is exposed as a trait: [`PyAnyMethods`], [`PyTupleMethods`], [`PyDictMethods`], and so on for all concrete types. Using traits rather than associated methods on the `Bound` smart pointer is done for a couple of reasons: -- Clarity of documentation: each trait gets its own documentation page in the PyO3 API docs. If all methods were on the `Bound` smart pointer directly, the vast majority of PyO3's API would be on a single, extremely long, documentation page. -- Consistency: downstream code implementing Rust APIs for existing Python types can also follow this pattern of using a trait. Downstream code would not be allowed to add new associated methods directly on the `Bound` type. -- Future design: it is hoped that a future Rust with [arbitrary self types](https://github.com/rust-lang/rust/issues/44874) will remove the need for these traits in favour of placing the methods directly on `PyAny`, `PyTuple`, `PyDict`, and so on. - -These traits are all included in the `pyo3::prelude` module, so with the glob import `use pyo3::prelude::*` the full PyO3 API is made available to downstream code. - -The following function accesses the first item in the input Python list, using the `.get_item()` method from the `PyListMethods` trait: - -```rust -use pyo3::prelude::*; -use pyo3::types::PyList; - -fn get_first_item<'py>(list: &Bound<'py, PyList>) -> PyResult> { - list.get_item(0) -} -# Python::with_gil(|py| { -# let l = PyList::new(py, ["hello world"]).unwrap(); -# assert!(get_first_item(&l).unwrap().eq("hello world").unwrap()); -# }) -``` - -### Casting between Python object types - -To cast `Bound<'py, T>` smart pointers to some other type, use the [`.downcast()`][PyAnyMethods::downcast] family of functions. This converts `&Bound<'py, T>` to a different `&Bound<'py, U>`, without transferring ownership. There is also [`.downcast_into()`][PyAnyMethods::downcast_into] to convert `Bound<'py, T>` to `Bound<'py, U>` with transfer of ownership. These methods are available for all types `T` which implement the [`PyTypeCheck`] trait. - -Casting to `Bound<'py, PyAny>` can be done with `.as_any()` or `.into_any()`. - -For example, the following snippet shows how to cast `Bound<'py, PyAny>` to `Bound<'py, PyTuple>`: - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyTuple; -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -// create a new Python `tuple`, and use `.into_any()` to erase the type -let obj: Bound<'py, PyAny> = PyTuple::empty(py).into_any(); - -// use `.downcast()` to cast to `PyTuple` without transferring ownership -let _: &Bound<'py, PyTuple> = obj.downcast()?; - -// use `.downcast_into()` to cast to `PyTuple` with transfer of ownership -let _: Bound<'py, PyTuple> = obj.downcast_into()?; -# Ok(()) -# } -# Python::with_gil(example).unwrap() -``` - -Custom [`#[pyclass]`][pyclass] types implement [`PyTypeCheck`], so `.downcast()` also works for these types. The snippet below is the same as the snippet above casting instead to a custom type `MyClass`: - -```rust -use pyo3::prelude::*; - -#[pyclass] -struct MyClass {} - -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -// create a new Python `tuple`, and use `.into_any()` to erase the type -let obj: Bound<'py, PyAny> = Bound::new(py, MyClass {})?.into_any(); - -// use `.downcast()` to cast to `MyClass` without transferring ownership -let _: &Bound<'py, MyClass> = obj.downcast()?; - -// use `.downcast_into()` to cast to `MyClass` with transfer of ownership -let _: Bound<'py, MyClass> = obj.downcast_into()?; -# Ok(()) -# } -# Python::with_gil(example).unwrap() -``` - -### Extracting Rust data from Python objects - -To extract Rust data from Python objects, use [`.extract()`][PyAnyMethods::extract] instead of `.downcast()`. This method is available for all types which implement the [`FromPyObject`] trait. - -For example, the following snippet extracts a Rust tuple of integers from a Python tuple: - -```rust -# use pyo3::prelude::*; -# use pyo3::types::PyTuple; -# fn example<'py>(py: Python<'py>) -> PyResult<()> { -// create a new Python `tuple`, and use `.into_any()` to erase the type -let obj: Bound<'py, PyAny> = PyTuple::new(py, [1, 2, 3])?.into_any(); - -// extracting the Python `tuple` to a rust `(i32, i32, i32)` tuple -let (x, y, z) = obj.extract::<(i32, i32, i32)>()?; -assert_eq!((x, y, z), (1, 2, 3)); -# Ok(()) -# } -# Python::with_gil(example).unwrap() -``` - -To avoid copying data, [`#[pyclass]`][pyclass] types can directly reference Rust data stored within the Python objects without needing to `.extract()`. See the [corresponding documentation in the class section of the guide](./class.md#bound-and-interior-mutability) -for more detail. - -[Bound]: {{#PYO3_DOCS_URL}}/pyo3/struct.Bound.html -[`Bound::unbind`]: {{#PYO3_DOCS_URL}}/pyo3/struct.Bound.html#method.unbind -[Py]: {{#PYO3_DOCS_URL}}/pyo3/struct.Py.html -[PyAnyMethods::add]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.add -[PyAnyMethods::extract]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.extract -[PyAnyMethods::downcast]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.downcast -[PyAnyMethods::downcast_into]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.downcast_into -[`PyTypeCheck`]: {{#PYO3_DOCS_URL}}/pyo3/type_object/trait.PyTypeCheck.html -[`PyAnyMethods`]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html -[`PyDictMethods`]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyDictMethods.html -[`PyTupleMethods`]: {{#PYO3_DOCS_URL}}/pyo3/types/trait.PyTupleMethods.html -[pyclass]: class.md -[Borrowed]: {{#PYO3_DOCS_URL}}/pyo3/struct.Borrowed.html -[Drop]: https://doc.rust-lang.org/std/ops/trait.Drop.html -[eval]: {{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.eval -[clone_ref]: {{#PYO3_DOCS_URL}}/pyo3/struct.Py.html#method.clone_ref -[pyo3::types]: {{#PYO3_DOCS_URL}}/pyo3/types/index.html -[PyAny]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyAny.html -[PyList_append]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyList.html#method.append -[RefCell]: https://doc.rust-lang.org/std/cell/struct.RefCell.html -[smart-pointers]: https://doc.rust-lang.org/book/ch15-00-smart-pointers.html diff --git a/vendor/pyo3/netlify.toml b/vendor/pyo3/netlify.toml deleted file mode 100644 index b0e8bd83eb..0000000000 --- a/vendor/pyo3/netlify.toml +++ /dev/null @@ -1,6 +0,0 @@ -[build] -publish = "netlify_build/" -command = ".netlify/build.sh" - -[build.environment] -PYTHON_VERSION = "3.8" diff --git a/vendor/pyo3/newsfragments/.gitignore b/vendor/pyo3/newsfragments/.gitignore deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/pyo3/noxfile.py b/vendor/pyo3/noxfile.py deleted file mode 100644 index ed7759f5f9..0000000000 --- a/vendor/pyo3/noxfile.py +++ /dev/null @@ -1,978 +0,0 @@ -from contextlib import contextmanager -import json -import os -import re -import shutil -import subprocess -import sys -import sysconfig -import tempfile -from functools import lru_cache -from glob import glob -from pathlib import Path -from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Tuple - -import nox -import nox.command - -try: - import tomllib as toml -except ImportError: - try: - import toml - except ImportError: - toml = None - -nox.options.sessions = ["test", "clippy", "rustfmt", "ruff", "docs"] - - -PYO3_DIR = Path(__file__).parent -PYO3_TARGET = Path(os.environ.get("CARGO_TARGET_DIR", PYO3_DIR / "target")).absolute() -PYO3_GUIDE_SRC = PYO3_DIR / "guide" / "src" -PYO3_GUIDE_TARGET = PYO3_TARGET / "guide" -PYO3_DOCS_TARGET = PYO3_TARGET / "doc" -PY_VERSIONS = ("3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13") -PYPY_VERSIONS = ("3.9", "3.10") -FREE_THREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) - - -@nox.session(venv_backend="none") -def test(session: nox.Session) -> None: - test_rust(session) - test_py(session) - - -@nox.session(name="test-rust", venv_backend="none") -def test_rust(session: nox.Session): - _run_cargo_test(session, package="pyo3-build-config") - _run_cargo_test(session, package="pyo3-macros-backend") - _run_cargo_test(session, package="pyo3-macros") - _run_cargo_test(session, package="pyo3-ffi") - - _run_cargo_test(session) - # the free-threaded build ignores abi3, so we skip abi3 - # tests to avoid unnecessarily running the tests twice - if not FREE_THREADED_BUILD: - _run_cargo_test(session, features="abi3") - if "skip-full" not in session.posargs: - _run_cargo_test(session, features="full") - if not FREE_THREADED_BUILD: - _run_cargo_test(session, features="abi3 full") - - -@nox.session(name="test-py", venv_backend="none") -def test_py(session: nox.Session) -> None: - _run(session, "nox", "-f", "pytests/noxfile.py", external=True) - for example in glob("examples/*/noxfile.py"): - _run(session, "nox", "-f", example, external=True) - for example in glob("pyo3-ffi/examples/*/noxfile.py"): - _run(session, "nox", "-f", example, external=True) - - -@nox.session(venv_backend="none") -def coverage(session: nox.Session) -> None: - session.env.update(_get_coverage_env()) - _run_cargo(session, "llvm-cov", "clean", "--workspace") - test(session) - generate_coverage_report(session) - - -@nox.session(name="set-coverage-env", venv_backend="none") -def set_coverage_env(session: nox.Session) -> None: - """For use in GitHub Actions to set coverage environment variables.""" - with open(os.environ["GITHUB_ENV"], "a") as env_file: - for k, v in _get_coverage_env().items(): - print(f"{k}={v}", file=env_file) - - -@nox.session(name="generate-coverage-report", venv_backend="none") -def generate_coverage_report(session: nox.Session) -> None: - cov_format = "codecov" - output_file = "coverage.json" - - if "lcov" in session.posargs: - cov_format = "lcov" - output_file = "lcov.info" - - _run_cargo( - session, - "llvm-cov", - "--package=pyo3", - "--package=pyo3-build-config", - "--package=pyo3-macros-backend", - "--package=pyo3-macros", - "--package=pyo3-ffi", - "report", - f"--{cov_format}", - "--output-path", - output_file, - ) - - -@nox.session(venv_backend="none") -def rustfmt(session: nox.Session): - _run_cargo(session, "fmt", "--all", "--check") - _run_cargo(session, "fmt", _FFI_CHECK, "--all", "--check") - - -@nox.session(name="ruff") -def ruff(session: nox.Session): - session.install("ruff") - _run(session, "ruff", "format", ".", "--check") - _run(session, "ruff", "check", ".") - - -@nox.session(name="clippy", venv_backend="none") -def clippy(session: nox.Session) -> bool: - if not _clippy(session) and _clippy_additional_workspaces(session): - session.error("one or more jobs failed") - - -def _clippy(session: nox.Session, *, env: Dict[str, str] = None) -> bool: - success = True - env = env or os.environ - for feature_set in _get_feature_sets(): - try: - _run_cargo( - session, - "clippy", - *feature_set, - "--all-targets", - "--workspace", - "--", - "--deny=warnings", - env=env, - ) - except nox.command.CommandFailed: - success = False - return success - - -def _clippy_additional_workspaces(session: nox.Session) -> bool: - # pyo3-benches and pyo3-ffi-check are in isolated workspaces so that their - # dependencies do not interact with MSRV - - success = True - try: - _run_cargo(session, "clippy", _BENCHES) - except Exception: - success = False - - # Run pyo3-ffi-check only on when not cross-compiling, because it needs to - # have Python headers to feed to bindgen which gets messy when cross-compiling. - target = os.environ.get("CARGO_BUILD_TARGET") - if target is None or _get_rust_default_target() == target: - try: - _build_docs_for_ffi_check(session) - _run_cargo(session, "clippy", _FFI_CHECK, "--workspace", "--all-targets") - except Exception: - success = False - return success - - -@nox.session(venv_backend="none") -def bench(session: nox.Session) -> bool: - _run_cargo(session, "bench", _BENCHES, *session.posargs) - - -@nox.session() -def codspeed(session: nox.Session) -> bool: - # rust benchmarks - os.chdir(PYO3_DIR / "pyo3-benches") - _run_cargo(session, "codspeed", "build") - _run_cargo(session, "codspeed", "run") - # python benchmarks - os.chdir(PYO3_DIR / "pytests") - session.install(".[dev]", "pytest-codspeed") - _run(session, "pytest", "--codspeed", external=True) - - -@nox.session(name="clippy-all", venv_backend="none") -def clippy_all(session: nox.Session) -> None: - success = True - - def _clippy_with_config(env: Dict[str, str]) -> None: - nonlocal success - success &= _clippy(session, env=env) - - _for_all_version_configs(session, _clippy_with_config) - success &= _clippy_additional_workspaces(session) - - if not success: - session.error("one or more jobs failed") - - -@nox.session(name="check-all", venv_backend="none") -def check_all(session: nox.Session) -> None: - success = True - - def _check(env: Dict[str, str]) -> None: - nonlocal success - for feature_set in _get_feature_sets(): - try: - _run_cargo( - session, - "check", - *feature_set, - "--all-targets", - "--workspace", - env=env, - ) - except Exception: - success = False - - _for_all_version_configs(session, _check) - - if not success: - session.error("one or more jobs failed") - - -@nox.session(venv_backend="none") -def publish(session: nox.Session) -> None: - _run_cargo_publish(session, package="pyo3-build-config") - _run_cargo_publish(session, package="pyo3-macros-backend") - _run_cargo_publish(session, package="pyo3-macros") - _run_cargo_publish(session, package="pyo3-ffi") - _run_cargo_publish(session, package="pyo3") - - -@nox.session(venv_backend="none") -def contributors(session: nox.Session) -> None: - import requests - - if len(session.posargs) < 1: - raise Exception("base commit positional argument missing") - - base = session.posargs[0] - page = 1 - - head = "HEAD" - if len(session.posargs) == 2: - head = session.posargs[1] - - if len(session.posargs) > 2: - raise Exception("too many arguments") - - authors = set() - - while True: - resp = requests.get( - f"https://api.github.com/repos/PyO3/pyo3/compare/{base}...{head}", - params={"page": page, "per_page": 100}, - ) - - body = resp.json() - - if resp.status_code != 200: - raise Exception( - f"failed to retrieve commits: {resp.status_code} {body['message']}" - ) - - for commit in body["commits"]: - try: - authors.add(commit["author"]["login"]) - except Exception: - continue - - if "next" in resp.links: - page += 1 - else: - break - - authors = sorted(list(authors), key=lambda author: author.lower()) - - for author in authors: - print(f"@{author}") - - -class EmscriptenInfo: - def __init__(self): - self.emscripten_dir = PYO3_DIR / "emscripten" - self.builddir = PYO3_DIR / ".nox/emscripten" - self.builddir.mkdir(exist_ok=True, parents=True) - - self.pyversion = sys.version.split()[0] - self.pymajor, self.pyminor, self.pymicro = self.pyversion.split(".") - self.pymicro, self.pydev = re.match( - "([0-9]*)([^0-9].*)?", self.pymicro - ).groups() - if self.pydev is None: - self.pydev = "" - - self.pymajorminor = f"{self.pymajor}.{self.pyminor}" - self.pymajorminormicro = f"{self.pymajorminor}.{self.pymicro}" - - -@nox.session(name="build-emscripten", venv_backend="none") -def build_emscripten(session: nox.Session): - info = EmscriptenInfo() - _run( - session, - "make", - "-C", - str(info.emscripten_dir), - f"PYTHON={sys.executable}", - f"BUILDROOT={info.builddir}", - f"PYMAJORMINORMICRO={info.pymajorminormicro}", - f"PYPRERELEASE={info.pydev}", - external=True, - ) - - -@nox.session(name="test-emscripten", venv_backend="none") -def test_emscripten(session: nox.Session): - info = EmscriptenInfo() - - libdir = info.builddir / f"install/Python-{info.pyversion}/lib" - pythonlibdir = libdir / f"python{info.pymajorminor}" - - target = "wasm32-unknown-emscripten" - - session.env["CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER"] = "python " + str( - info.emscripten_dir / "runner.py" - ) - session.env["RUSTFLAGS"] = " ".join( - [ - f"-L native={libdir}", - "-C link-arg=--preload-file", - f"-C link-arg={pythonlibdir}@/lib/python{info.pymajorminor}", - f"-C link-arg=-lpython{info.pymajorminor}", - "-C link-arg=-lexpat", - "-C link-arg=-lmpdec", - "-C link-arg=-lsqlite3", - "-C link-arg=-lz", - "-C link-arg=-lbz2", - "-C link-arg=-sALLOW_MEMORY_GROWTH=1", - ] - ) - session.env["CARGO_BUILD_TARGET"] = target - session.env["PYO3_CROSS_LIB_DIR"] = pythonlibdir - _run(session, "rustup", "target", "add", target, "--toolchain", "stable") - _run( - session, - "bash", - "-c", - f"source {info.builddir / 'emsdk/emsdk_env.sh'} && cargo test", - ) - - -@nox.session(venv_backend="none") -def docs(session: nox.Session) -> None: - rustdoc_flags = ["-Dwarnings"] - toolchain_flags = [] - cargo_flags = [] - - if "open" in session.posargs: - cargo_flags.append("--open") - - if "nightly" in session.posargs: - rustdoc_flags.append("--cfg docsrs") - toolchain_flags.append("+nightly") - cargo_flags.extend(["-Z", "unstable-options", "-Z", "rustdoc-scrape-examples"]) - - if "nightly" in session.posargs and "internal" in session.posargs: - rustdoc_flags.append("--Z unstable-options") - rustdoc_flags.append("--document-hidden-items") - rustdoc_flags.extend(("--html-after-content", ".netlify/internal_banner.html")) - cargo_flags.append("--document-private-items") - else: - cargo_flags.extend(["--exclude=pyo3-macros", "--exclude=pyo3-macros-backend"]) - - rustdoc_flags.append(session.env.get("RUSTDOCFLAGS", "")) - session.env["RUSTDOCFLAGS"] = " ".join(rustdoc_flags) - - shutil.rmtree(PYO3_DOCS_TARGET, ignore_errors=True) - _run_cargo( - session, - *toolchain_flags, - "doc", - "--lib", - "--no-default-features", - "--features=full", - "--no-deps", - "--workspace", - *cargo_flags, - ) - - -@nox.session(name="build-guide", venv_backend="none") -def build_guide(session: nox.Session): - shutil.rmtree(PYO3_GUIDE_TARGET, ignore_errors=True) - _run(session, "mdbook", "build", "-d", PYO3_GUIDE_TARGET, "guide", *session.posargs) - for license in ("LICENSE-APACHE", "LICENSE-MIT"): - target_file = PYO3_GUIDE_TARGET / license - target_file.unlink(missing_ok=True) - shutil.copy(PYO3_DIR / license, target_file) - - -@nox.session(name="check-guide", venv_backend="none") -def check_guide(session: nox.Session): - # reuse other sessions, but with default args - posargs = [*session.posargs] - del session.posargs[:] - build_guide(session) - docs(session) - session.posargs.extend(posargs) - - if toml is None: - session.error("requires Python 3.11 or `toml` to be installed") - pyo3_version = toml.loads((PYO3_DIR / "Cargo.toml").read_text())["package"][ - "version" - ] - - remaps = { - f"file://{PYO3_GUIDE_SRC}/([^/]*/)*?%7B%7B#PYO3_DOCS_URL}}}}": f"file://{PYO3_DOCS_TARGET}", - f"https://pyo3.rs/v{pyo3_version}": f"file://{PYO3_GUIDE_TARGET}", - "https://pyo3.rs/main/": f"file://{PYO3_GUIDE_TARGET}/", - "https://pyo3.rs/latest/": f"file://{PYO3_GUIDE_TARGET}/", - "%7B%7B#PYO3_DOCS_VERSION}}": "latest", - } - remap_args = [] - for key, value in remaps.items(): - remap_args.extend(("--remap", f"{key} {value}")) - # check all links in the guide - _run( - session, - "lychee", - "--include-fragments", - str(PYO3_GUIDE_SRC), - *remap_args, - *session.posargs, - ) - # check external links in the docs - # (intra-doc links are checked by rustdoc) - _run( - session, - "lychee", - str(PYO3_DOCS_TARGET), - *remap_args, - f"--exclude=file://{PYO3_DOCS_TARGET}", - "--exclude=http://www.adobe.com/", - *session.posargs, - ) - - -@nox.session(name="format-guide", venv_backend="none") -def format_guide(session: nox.Session): - fence_line = "//! ```\n" - - for path in Path("guide").glob("**/*.md"): - session.log("Working on %s", path) - lines = iter(path.read_text().splitlines(True)) - new_lines = [] - - for line in lines: - new_lines.append(line) - if not re.search("```rust(,.*)?$", line): - continue - - # Found a code block fence, gobble up its lines and write to temp. file - prefix = line[: line.index("```")] - with tempfile.NamedTemporaryFile("w", delete=False) as file: - tempname = file.name - file.write(fence_line) - for line in lines: - if line == prefix + "```\n": - break - file.write(("//! " + line[len(prefix) :]).rstrip() + "\n") - file.write(fence_line) - - # Format it (needs nightly rustfmt for `format_code_in_doc_comments`) - _run( - session, - "rustfmt", - "+nightly", - "--config", - "format_code_in_doc_comments=true", - "--config", - "reorder_imports=false", - tempname, - ) - - # Re-read the formatted file, add its lines, and delete it - with open(tempname, "r") as file: - for line in file: - if line == fence_line: - continue - new_lines.append((prefix + line[4:]).rstrip() + "\n") - os.unlink(tempname) - - new_lines.append(prefix + "```\n") - - path.write_text("".join(new_lines)) - - -@nox.session(name="address-sanitizer", venv_backend="none") -def address_sanitizer(session: nox.Session): - _run_cargo( - session, - "+nightly", - "test", - "--release", - "-Zbuild-std", - f"--target={_get_rust_default_target()}", - "--", - "--test-threads=1", - env={ - "RUSTFLAGS": "-Zsanitizer=address", - "RUSTDOCFLAGS": "-Zsanitizer=address", - "ASAN_OPTIONS": "detect_leaks=0", - }, - ) - - -_IGNORE_CHANGELOG_PR_CATEGORIES = ( - "release", - "docs", -) - - -@nox.session(name="check-changelog") -def check_changelog(session: nox.Session): - if not _is_github_actions(): - session.error("Can only check changelog on github actions") - - event_path = os.environ["GITHUB_EVENT_PATH"] - - with open(event_path) as event_file: - event = json.load(event_file) - - for category in _IGNORE_CHANGELOG_PR_CATEGORIES: - if event["pull_request"]["title"].startswith(f"{category}:"): - session.skip(f"PR title starts with {category}") - - for label in event["pull_request"]["labels"]: - if label["name"] == "CI-skip-changelog": - session.skip("CI-skip-changelog label applied") - - issue_number = event["pull_request"]["number"] - - newsfragments = PYO3_DIR / "newsfragments" - - fragments = tuple( - filter( - Path.exists, - ( - newsfragments / f"{issue_number}.{change_type}.md" - for change_type in ("packaging", "added", "changed", "removed", "fixed") - ), - ) - ) - - if not fragments: - session.error( - "Changelog entry not found, please add one (or more) to the `newsfragments` directory.\n" - "Alternatively, start the PR title with `docs:` if this PR is a docs-only PR.\n" - "See https://github.com/PyO3/pyo3/blob/main/Contributing.md#documenting-changes for more information." - ) - - print("Found newsfragments:") - for fragment in fragments: - print(fragment.name) - - -@nox.session(name="set-msrv-package-versions", venv_backend="none") -def set_msrv_package_versions(session: nox.Session): - from collections import defaultdict - - if toml is None: - session.error("requires Python 3.11 or `toml` to be installed") - - projects = ( - None, - "examples/decorator", - "examples/maturin-starter", - "examples/setuptools-rust-starter", - "examples/word-count", - ) - min_pkg_versions = { - "regex": "1.9.6", - "proptest": "1.2.0", - "trybuild": "1.0.89", - "eyre": "0.6.8", - "allocator-api2": "0.2.10", - "indexmap": "2.5.0", # to be compatible with hashbrown 0.14 - "hashbrown": "0.14.5", # https://github.com/rust-lang/hashbrown/issues/574 - } - - # run cargo update first to ensure that everything is at highest - # possible version, so that this matches what CI will resolve to. - for project in projects: - if project is None: - _run_cargo(session, "update") - else: - _run_cargo(session, "update", f"--manifest-path={project}/Cargo.toml") - - for project in projects: - lock_file = Path(project or "") / "Cargo.lock" - - def load_pkg_versions(): - cargo_lock = toml.loads(lock_file.read_text()) - # Cargo allows to depends on multiple versions of the same package - pkg_versions = defaultdict(list) - for pkg in cargo_lock["package"]: - name = pkg["name"] - if name not in min_pkg_versions: - continue - pkg_versions[name].append(pkg["version"]) - return pkg_versions - - pkg_versions = load_pkg_versions() - for pkg_name, min_version in min_pkg_versions.items(): - versions = pkg_versions.get(pkg_name, []) - for version in versions: - if version != min_version: - pkg_id = pkg_name + ":" + version - _run_cargo_set_package_version( - session, pkg_id, min_version, project=project - ) - # assume `_run_cargo_set_package_version` has changed something - # and re-read `Cargo.lock` - pkg_versions = load_pkg_versions() - - # As a smoke test, cargo metadata solves all dependencies, so - # will break if any crates rely on cargo features not - # supported on MSRV - for project in projects: - if project is None: - _run_cargo(session, "metadata", silent=True) - else: - _run_cargo( - session, - "metadata", - f"--manifest-path={project}/Cargo.toml", - silent=True, - ) - - -@nox.session(name="ffi-check") -def ffi_check(session: nox.Session): - _build_docs_for_ffi_check(session) - _run_cargo(session, "run", _FFI_CHECK) - - -@nox.session(name="test-version-limits") -def test_version_limits(session: nox.Session): - env = os.environ.copy() - with _config_file() as config_file: - env["PYO3_CONFIG_FILE"] = config_file.name - - assert "3.6" not in PY_VERSIONS - config_file.set("CPython", "3.6") - _run_cargo(session, "check", env=env, expect_error=True) - - assert "3.14" not in PY_VERSIONS - config_file.set("CPython", "3.14") - _run_cargo(session, "check", env=env, expect_error=True) - - # 3.14 CPython should build with forward compatibility - env["PYO3_USE_ABI3_FORWARD_COMPATIBILITY"] = "1" - _run_cargo(session, "check", env=env) - - assert "3.8" not in PYPY_VERSIONS - config_file.set("PyPy", "3.8") - _run_cargo(session, "check", env=env, expect_error=True) - - assert "3.11" not in PYPY_VERSIONS - config_file.set("PyPy", "3.11") - _run_cargo(session, "check", env=env, expect_error=True) - - -@nox.session(name="check-feature-powerset", venv_backend="none") -def check_feature_powerset(session: nox.Session): - if toml is None: - session.error("requires Python 3.11 or `toml` to be installed") - - cargo_toml = toml.loads((PYO3_DIR / "Cargo.toml").read_text()) - - EXCLUDED_FROM_FULL = { - "nightly", - "extension-module", - "full", - "default", - "auto-initialize", - "generate-import-lib", - "multiple-pymethods", # Because it's not supported on wasm - } - - features = cargo_toml["features"] - - full_feature = set(features["full"]) - abi3_features = {feature for feature in features if feature.startswith("abi3")} - abi3_version_features = abi3_features - {"abi3"} - - expected_full_feature = features.keys() - EXCLUDED_FROM_FULL - abi3_features - - uncovered_features = expected_full_feature - full_feature - if uncovered_features: - session.error( - f"some features missing from `full` meta feature: {uncovered_features}" - ) - - experimental_features = { - feature for feature in features if feature.startswith("experimental-") - } - full_without_experimental = full_feature - experimental_features - - if len(experimental_features) >= 2: - # justification: we always assume that feature within these groups are - # mutually exclusive to simplify CI - features_to_group = [ - full_without_experimental, - experimental_features, - ] - elif len(experimental_features) == 1: - # no need to make an experimental features group - features_to_group = [full_without_experimental] - else: - session.error("no experimental features exist; please simplify the noxfile") - - features_to_skip = [ - *(EXCLUDED_FROM_FULL), - *abi3_version_features, - ] - - # deny warnings - env = os.environ.copy() - rust_flags = env.get("RUSTFLAGS", "") - env["RUSTFLAGS"] = f"{rust_flags} -Dwarnings" - - subcommand = "hack" - if "minimal-versions" in session.posargs: - subcommand = "minimal-versions" - - comma_join = ",".join - _run_cargo( - session, - subcommand, - "--feature-powerset", - '--optional-deps=""', - f'--skip="{comma_join(features_to_skip)}"', - *(f"--group-features={comma_join(group)}" for group in features_to_group), - "check", - "--all-targets", - env=env, - ) - - -@nox.session(name="update-ui-tests", venv_backend="none") -def update_ui_tests(session: nox.Session): - env = os.environ.copy() - env["TRYBUILD"] = "overwrite" - command = ["test", "--test", "test_compile_error"] - _run_cargo(session, *command, env=env) - _run_cargo(session, *command, "--features=full", env=env) - _run_cargo(session, *command, "--features=abi3,full", env=env) - - -def _build_docs_for_ffi_check(session: nox.Session) -> None: - # pyo3-ffi-check needs to scrape docs of pyo3-ffi - env = os.environ.copy() - env["PYO3_PYTHON"] = sys.executable - _run_cargo(session, "doc", _FFI_CHECK, "-p", "pyo3-ffi", "--no-deps", env=env) - - -@lru_cache() -def _get_rust_info() -> Tuple[str, ...]: - output = _get_output("rustc", "-vV") - - return tuple(output.splitlines()) - - -def _get_rust_version() -> Tuple[int, int, int, List[str]]: - for line in _get_rust_info(): - if line.startswith(_RELEASE_LINE_START): - version = line[len(_RELEASE_LINE_START) :].strip() - # e.g. 1.67.0-beta.2 - (version_number, *extra) = version.split("-", maxsplit=1) - return (*map(int, version_number.split(".")), extra) - - -def _get_rust_default_target() -> str: - for line in _get_rust_info(): - if line.startswith(_HOST_LINE_START): - return line[len(_HOST_LINE_START) :].strip() - - -@lru_cache() -def _get_feature_sets() -> Tuple[Tuple[str, ...], ...]: - """Returns feature sets to use for clippy job""" - cargo_target = os.getenv("CARGO_BUILD_TARGET", "") - if "wasm32-wasip1" not in cargo_target: - # multiple-pymethods not supported on wasm - return ( - ("--no-default-features",), - ( - "--no-default-features", - "--features=abi3", - ), - ("--features=full multiple-pymethods",), - ("--features=abi3 full multiple-pymethods",), - ) - else: - return ( - ("--no-default-features",), - ( - "--no-default-features", - "--features=abi3", - ), - ("--features=full",), - ("--features=abi3 full",), - ) - - -_RELEASE_LINE_START = "release: " -_HOST_LINE_START = "host: " - - -def _get_coverage_env() -> Dict[str, str]: - env = {} - output = _get_output("cargo", "llvm-cov", "show-env") - - for line in output.strip().splitlines(): - (key, value) = line.split("=", maxsplit=1) - # Strip single or double quotes from the variable value - # - quote used by llvm-cov differs between Windows and Linux - if value and value[0] in ("'", '"'): - value = value[1:-1] - env[key] = value - - # Ensure that examples/ and pytests/ all build to the correct target directory to collect - # coverage artifacts. - env["CARGO_TARGET_DIR"] = env["CARGO_LLVM_COV_TARGET_DIR"] - - return env - - -def _run(session: nox.Session, *args: str, **kwargs: Any) -> None: - """Wrapper for _run(session, which creates nice groups on GitHub Actions.""" - is_github_actions = _is_github_actions() - failed = False - if is_github_actions: - # Insert ::group:: at the start of nox's command line output - print("::group::", end="", flush=True, file=sys.stderr) - try: - session.run(*args, **kwargs) - except nox.command.CommandFailed: - failed = True - raise - finally: - if is_github_actions: - print("::endgroup::", file=sys.stderr) - # Defer the error message until after the group to make them easier - # to find in the log - if failed: - command = " ".join(args) - print(f"::error::`{command}` failed", file=sys.stderr) - - -def _run_cargo( - session: nox.Session, *args: str, expect_error: bool = False, **kwargs: Any -) -> None: - if expect_error: - if "success_codes" in kwargs: - raise ValueError("expect_error overrides success_codes") - kwargs["success_codes"] = [101] - _run(session, "cargo", *args, **kwargs, external=True) - - -def _run_cargo_test( - session: nox.Session, - *, - package: Optional[str] = None, - features: Optional[str] = None, -) -> None: - command = ["cargo"] - if "careful" in session.posargs: - # do explicit setup so failures in setup can be seen - _run_cargo(session, "careful", "setup") - command.append("careful") - command.extend(("test", "--no-fail-fast")) - if "release" in session.posargs: - command.append("--release") - if package: - command.append(f"--package={package}") - if features: - command.append(f"--features={features}") - - _run(session, *command, external=True) - - -def _run_cargo_publish(session: nox.Session, *, package: str) -> None: - _run_cargo(session, "publish", f"--package={package}") - - -def _run_cargo_set_package_version( - session: nox.Session, - pkg_id: str, - version: str, - *, - project: Optional[str] = None, -) -> None: - command = ["cargo", "update", "-p", pkg_id, "--precise", version, "--workspace"] - if project: - command.append(f"--manifest-path={project}/Cargo.toml") - _run(session, *command, external=True) - - -def _get_output(*args: str) -> str: - return subprocess.run(args, capture_output=True, text=True, check=True).stdout - - -def _for_all_version_configs( - session: nox.Session, job: Callable[[Dict[str, str]], None] -) -> None: - env = os.environ.copy() - with _config_file() as config_file: - env["PYO3_CONFIG_FILE"] = config_file.name - - def _job_with_config(implementation, version): - session.log(f"{implementation} {version}") - config_file.set(implementation, version) - job(env) - - for version in PY_VERSIONS: - _job_with_config("CPython", version) - - for version in PYPY_VERSIONS: - _job_with_config("PyPy", version) - - -class _ConfigFile: - def __init__(self, config_file) -> None: - self._config_file = config_file - - def set( - self, implementation: str, version: str, build_flags: Iterable[str] = () - ) -> None: - """Set the contents of this config file to the given implementation and version.""" - self._config_file.seek(0) - self._config_file.truncate(0) - self._config_file.write( - f"""\ -implementation={implementation} -version={version} -build_flags={",".join(build_flags)} -suppress_build_script_link_lines=true -""" - ) - self._config_file.flush() - - @property - def name(self) -> str: - return self._config_file.name - - -@contextmanager -def _config_file() -> Iterator[_ConfigFile]: - """Creates a temporary config file which can be repeatedly set to different values.""" - with tempfile.NamedTemporaryFile("r+") as config: - yield _ConfigFile(config) - - -def _is_github_actions() -> bool: - return "GITHUB_ACTIONS" in os.environ - - -_BENCHES = "--manifest-path=pyo3-benches/Cargo.toml" -_FFI_CHECK = "--manifest-path=pyo3-ffi-check/Cargo.toml" diff --git a/vendor/pyo3/pyo3-benches/Cargo.toml b/vendor/pyo3/pyo3-benches/Cargo.toml deleted file mode 100644 index 24ec9b5d76..0000000000 --- a/vendor/pyo3/pyo3-benches/Cargo.toml +++ /dev/null @@ -1,90 +0,0 @@ -[package] -name = "pyo3-benches" -version = "0.1.0" -description = "In-tree benchmarks for the PyO3 project" -authors = ["PyO3 Project and Contributors "] -edition = "2021" -publish = false - -[dependencies] -pyo3 = { path = "../", features = ["auto-initialize", "full"] } - -[build-dependencies] -pyo3-build-config = { path = "../pyo3-build-config" } - -[dev-dependencies] -codspeed-criterion-compat = "2.3" -criterion = "0.5.1" -num-bigint = "0.4.3" -rust_decimal = { version = "1.0.0", default-features = false } -hashbrown = "0.15" - -[[bench]] -name = "bench_any" -harness = false - -[[bench]] -name = "bench_call" -harness = false - -[[bench]] -name = "bench_comparisons" -harness = false - -[[bench]] -name = "bench_err" -harness = false - -[[bench]] -name = "bench_decimal" -harness = false - -[[bench]] -name = "bench_dict" -harness = false - -[[bench]] -name = "bench_frompyobject" -harness = false - -[[bench]] -name = "bench_gil" -harness = false - -[[bench]] -name = "bench_intopyobject" -harness = false - -[[bench]] -name = "bench_list" -harness = false - -[[bench]] -name = "bench_pyclass" -harness = false - -[[bench]] -name = "bench_pyobject" -harness = false - -[[bench]] -name = "bench_set" -harness = false - -[[bench]] -name = "bench_tuple" -harness = false - -[[bench]] -name = "bench_intern" -harness = false - -[[bench]] -name = "bench_extract" -harness = false - -[[bench]] -name = "bench_bigint" -harness = false - -[workspace] diff --git a/vendor/pyo3/pyo3-benches/benches/bench_any.rs b/vendor/pyo3/pyo3-benches/benches/bench_any.rs deleted file mode 100644 index 4ed1449387..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_any.rs +++ /dev/null @@ -1,94 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::{ - prelude::*, - types::{ - PyBool, PyByteArray, PyBytes, PyDict, PyFloat, PyFrozenSet, PyInt, PyList, PyMapping, - PySequence, PySet, PyString, PyTuple, - }, -}; - -#[derive(PartialEq, Eq, Debug)] -enum ObjectType { - None, - Bool, - ByteArray, - Bytes, - Dict, - Float, - FrozenSet, - Int, - List, - Set, - Str, - Tuple, - Sequence, - Mapping, - Unknown, -} - -fn find_object_type(obj: &Bound<'_, PyAny>) -> ObjectType { - if obj.is_none() { - ObjectType::None - } else if obj.is_instance_of::() { - ObjectType::Bool - } else if obj.is_instance_of::() { - ObjectType::ByteArray - } else if obj.is_instance_of::() { - ObjectType::Bytes - } else if obj.is_instance_of::() { - ObjectType::Dict - } else if obj.is_instance_of::() { - ObjectType::Float - } else if obj.is_instance_of::() { - ObjectType::FrozenSet - } else if obj.is_instance_of::() { - ObjectType::Int - } else if obj.is_instance_of::() { - ObjectType::List - } else if obj.is_instance_of::() { - ObjectType::Set - } else if obj.is_instance_of::() { - ObjectType::Str - } else if obj.is_instance_of::() { - ObjectType::Tuple - } else if obj.downcast::().is_ok() { - ObjectType::Sequence - } else if obj.downcast::().is_ok() { - ObjectType::Mapping - } else { - ObjectType::Unknown - } -} - -fn bench_identify_object_type(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let obj = py.eval(c"object()", None, None).unwrap(); - - b.iter(|| find_object_type(&obj)); - - assert_eq!(find_object_type(&obj), ObjectType::Unknown); - }); -} - -fn bench_collect_generic_iterator(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let collection = py.eval(c"list(range(1 << 20))", None, None).unwrap(); - - b.iter(|| { - collection - .try_iter() - .unwrap() - .collect::>>() - .unwrap() - }); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("identify_object_type", bench_identify_object_type); - c.bench_function("collect_generic_iterator", bench_collect_generic_iterator); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_bigint.rs b/vendor/pyo3/pyo3-benches/benches/bench_bigint.rs deleted file mode 100644 index d2c78f0ad4..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_bigint.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; -use num_bigint::BigInt; - -use pyo3::prelude::*; -use pyo3::types::PyDict; - -fn extract_bigint_extract_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).extract::() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -fn extract_bigint_small(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = py.eval(c"-42", None, None).unwrap(); - - bench.iter_with_large_drop(|| black_box(&int).extract::().unwrap()); - }); -} - -fn extract_bigint_big_negative(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = py.eval(c"-10**300", None, None).unwrap(); - - bench.iter_with_large_drop(|| black_box(&int).extract::().unwrap()); - }); -} - -fn extract_bigint_big_positive(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = py.eval(c"10**300", None, None).unwrap(); - - bench.iter_with_large_drop(|| black_box(&int).extract::().unwrap()); - }); -} - -fn extract_bigint_huge_negative(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = py.eval(c"-10**3000", None, None).unwrap(); - - bench.iter_with_large_drop(|| black_box(&int).extract::().unwrap()); - }); -} - -fn extract_bigint_huge_positive(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = py.eval(c"10**3000", None, None).unwrap(); - - bench.iter_with_large_drop(|| black_box(&int).extract::().unwrap()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("extract_bigint_extract_fail", extract_bigint_extract_fail); - c.bench_function("extract_bigint_small", extract_bigint_small); - c.bench_function("extract_bigint_big_negative", extract_bigint_big_negative); - c.bench_function("extract_bigint_big_positive", extract_bigint_big_positive); - c.bench_function("extract_bigint_huge_negative", extract_bigint_huge_negative); - c.bench_function("extract_bigint_huge_positive", extract_bigint_huge_positive); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_call.rs b/vendor/pyo3/pyo3-benches/benches/bench_call.rs deleted file mode 100644 index b6e090a7db..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_call.rs +++ /dev/null @@ -1,200 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::ffi::c_str; -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; - -macro_rules! test_module { - ($py:ident, $code:literal) => { - PyModule::from_code($py, c_str!($code), c_str!(file!()), c_str!("test_module")) - .expect("module creation failed") - }; -} - -fn bench_call_0(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!(py, "def foo(): pass"); - - let foo_module = &module.getattr("foo").unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module).call0().unwrap(); - } - }); - }) -} - -fn bench_call_1(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!(py, "def foo(a, b, c): pass"); - - let foo_module = &module.getattr("foo").unwrap(); - let args = ( - 1.into_pyobject(py).unwrap(), - "s".into_pyobject(py).unwrap(), - 1.23.into_pyobject(py).unwrap(), - ); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module).call1(args.clone()).unwrap(); - } - }); - }) -} - -fn bench_call(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!(py, "def foo(a, b, c, d, e): pass"); - - let foo_module = &module.getattr("foo").unwrap(); - let args = ( - 1.into_pyobject(py).unwrap(), - "s".into_pyobject(py).unwrap(), - 1.23.into_pyobject(py).unwrap(), - ); - let kwargs = [("d", 1), ("e", 42)].into_py_dict(py).unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module) - .call(args.clone(), Some(&kwargs)) - .unwrap(); - } - }); - }) -} - -fn bench_call_one_arg(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!(py, "def foo(a): pass"); - - let foo_module = &module.getattr("foo").unwrap(); - let arg = 1i32.into_pyobject(py).unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module).call1((arg.clone(),)).unwrap(); - } - }); - }) -} - -fn bench_call_method_0(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!( - py, - " -class Foo: - def foo(self): - pass -" - ); - - let foo_module = &module.getattr("Foo").unwrap().call0().unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module).call_method0("foo").unwrap(); - } - }); - }) -} - -fn bench_call_method_1(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!( - py, - " -class Foo: - def foo(self, a, b, c): - pass -" - ); - - let foo_module = &module.getattr("Foo").unwrap().call0().unwrap(); - let args = ( - 1.into_pyobject(py).unwrap(), - "s".into_pyobject(py).unwrap(), - 1.23.into_pyobject(py).unwrap(), - ); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module) - .call_method1("foo", args.clone()) - .unwrap(); - } - }); - }) -} - -fn bench_call_method(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!( - py, - " -class Foo: - def foo(self, a, b, c, d, e): - pass -" - ); - - let foo_module = &module.getattr("Foo").unwrap().call0().unwrap(); - let args = ( - 1.into_pyobject(py).unwrap(), - "s".into_pyobject(py).unwrap(), - 1.23.into_pyobject(py).unwrap(), - ); - let kwargs = [("d", 1), ("e", 42)].into_py_dict(py).unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module) - .call_method("foo", args.clone(), Some(&kwargs)) - .unwrap(); - } - }); - }) -} - -fn bench_call_method_one_arg(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let module = test_module!( - py, - " -class Foo: - def foo(self, a): - pass -" - ); - - let foo_module = &module.getattr("Foo").unwrap().call0().unwrap(); - let arg = 1i32.into_pyobject(py).unwrap(); - - b.iter(|| { - for _ in 0..1000 { - black_box(foo_module) - .call_method1("foo", (arg.clone(),)) - .unwrap(); - } - }); - }) -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("call_0", bench_call_0); - c.bench_function("call_1", bench_call_1); - c.bench_function("call", bench_call); - c.bench_function("call_one_arg", bench_call_one_arg); - c.bench_function("call_method_0", bench_call_method_0); - c.bench_function("call_method_1", bench_call_method_1); - c.bench_function("call_method", bench_call_method); - c.bench_function("call_method_one_arg", bench_call_method_one_arg); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_comparisons.rs b/vendor/pyo3/pyo3-benches/benches/bench_comparisons.rs deleted file mode 100644 index fbd473f06c..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_comparisons.rs +++ /dev/null @@ -1,70 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::{prelude::*, pyclass::CompareOp, Python}; - -#[pyclass] -struct OrderedDunderMethods(i64); - -#[pymethods] -impl OrderedDunderMethods { - fn __lt__(&self, other: &Self) -> bool { - self.0 < other.0 - } - - fn __le__(&self, other: &Self) -> bool { - self.0 <= other.0 - } - - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } - - fn __ne__(&self, other: &Self) -> bool { - self.0 != other.0 - } - - fn __gt__(&self, other: &Self) -> bool { - self.0 > other.0 - } - - fn __ge__(&self, other: &Self) -> bool { - self.0 >= other.0 - } -} - -#[pyclass] -#[derive(PartialEq, Eq, PartialOrd, Ord)] -struct OrderedRichcmp(i64); - -#[pymethods] -impl OrderedRichcmp { - fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { - op.matches(self.cmp(other)) - } -} - -fn bench_ordered_dunder_methods(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let obj1 = &Bound::new(py, OrderedDunderMethods(0)).unwrap().into_any(); - let obj2 = &Bound::new(py, OrderedDunderMethods(1)).unwrap().into_any(); - - b.iter(|| obj2.gt(obj1).unwrap()); - }); -} - -fn bench_ordered_richcmp(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let obj1 = &Bound::new(py, OrderedRichcmp(0)).unwrap().into_any(); - let obj2 = &Bound::new(py, OrderedRichcmp(1)).unwrap().into_any(); - - b.iter(|| obj2.gt(obj1).unwrap()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("ordered_dunder_methods", bench_ordered_dunder_methods); - c.bench_function("ordered_richcmp", bench_ordered_richcmp); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_decimal.rs b/vendor/pyo3/pyo3-benches/benches/bench_decimal.rs deleted file mode 100644 index 65fb47e23f..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_decimal.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; -use rust_decimal::Decimal; - -use pyo3::prelude::*; -use pyo3::types::PyDict; - -fn decimal_via_extract(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - cr#" -import decimal -py_dec = decimal.Decimal("0.0") -"#, - None, - Some(&locals), - ) - .unwrap(); - let py_dec = locals.get_item("py_dec").unwrap().unwrap(); - - b.iter(|| black_box(&py_dec).extract::().unwrap()); - }) -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("decimal_via_extract", decimal_via_extract); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_dict.rs b/vendor/pyo3/pyo3-benches/benches/bench_dict.rs deleted file mode 100644 index 6a92cf21c5..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_dict.rs +++ /dev/null @@ -1,114 +0,0 @@ -use std::collections::{BTreeMap, HashMap}; -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::types::IntoPyDict; -use pyo3::{prelude::*, types::PyMapping}; - -fn iter_dict(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let dict = (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - let mut sum = 0; - b.iter(|| { - for (k, _v) in &dict { - let i: u64 = k.extract().unwrap(); - sum += i; - } - }); - }) -} - -fn dict_new(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - b.iter_with_large_drop(|| { - (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap() - }); - }); -} - -fn dict_get_item(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let dict = (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - sum += dict - .get_item(i) - .unwrap() - .unwrap() - .extract::() - .unwrap(); - } - }); - }); -} - -fn extract_hashmap(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let dict = (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - b.iter(|| HashMap::::extract_bound(&dict)); - }); -} - -fn extract_btreemap(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let dict = (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - b.iter(|| BTreeMap::::extract_bound(&dict)); - }); -} - -fn extract_hashbrown_map(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let dict = (0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - b.iter(|| hashbrown::HashMap::::extract_bound(&dict)); - }); -} - -fn mapping_from_dict(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let dict = &(0..LEN as u64) - .map(|i| (i, i * 2)) - .into_py_dict(py) - .unwrap(); - b.iter(|| black_box(dict).downcast::().unwrap()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("iter_dict", iter_dict); - c.bench_function("dict_new", dict_new); - c.bench_function("dict_get_item", dict_get_item); - c.bench_function("extract_hashmap", extract_hashmap); - c.bench_function("extract_btreemap", extract_btreemap); - c.bench_function("mapping_from_dict", mapping_from_dict); - c.bench_function("extract_hashbrown_map", extract_hashbrown_map); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_err.rs b/vendor/pyo3/pyo3-benches/benches/bench_err.rs deleted file mode 100644 index 998ed6975b..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_err.rs +++ /dev/null @@ -1,24 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::{exceptions::PyValueError, prelude::*}; - -fn err_new_restore_and_fetch(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - b.iter(|| { - PyValueError::new_err("some exception message").restore(py); - PyErr::fetch(py) - }) - }) -} - -fn err_new_without_gil(b: &mut Bencher<'_>) { - b.iter(|| PyValueError::new_err("some exception message")) -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("err_new_restore_and_fetch", err_new_restore_and_fetch); - c.bench_function("err_new_without_gil", err_new_without_gil); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_extract.rs b/vendor/pyo3/pyo3-benches/benches/bench_extract.rs deleted file mode 100644 index 2062ccba7b..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_extract.rs +++ /dev/null @@ -1,157 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::{ - prelude::*, - types::{PyDict, PyFloat, PyInt, PyString}, -}; - -fn extract_str_extract_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let s = PyString::new(py, "Hello, World!").into_any(); - - bench.iter(|| black_box(&s).extract::<&str>().unwrap()); - }); -} - -fn extract_str_extract_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).extract::<&str>() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -#[cfg(Py_3_10)] -fn extract_str_downcast_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let s = PyString::new(py, "Hello, World!").into_any(); - - bench.iter(|| { - let py_str = black_box(&s).downcast::().unwrap(); - py_str.to_str().unwrap() - }); - }); -} - -fn extract_str_downcast_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).downcast::() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -fn extract_int_extract_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = 123i32.into_pyobject(py).unwrap(); - - bench.iter(|| black_box(&int).extract::().unwrap()); - }); -} - -fn extract_int_extract_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).extract::() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -fn extract_int_downcast_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let int = 123i32.into_pyobject(py).unwrap(); - - bench.iter(|| { - let py_int = black_box(&int).downcast::().unwrap(); - py_int.extract::().unwrap() - }); - }); -} - -fn extract_int_downcast_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).downcast::() { - Ok(v) => panic!("should err {}", v), - Err(e) => black_box(e), - }); - }); -} - -fn extract_float_extract_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let float = 23.42f64.into_pyobject(py).unwrap(); - - bench.iter(|| black_box(&float).extract::().unwrap()); - }); -} - -fn extract_float_extract_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).extract::() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -fn extract_float_downcast_success(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let float = 23.42f64.into_pyobject(py).unwrap(); - - bench.iter(|| { - let py_float = black_box(&float).downcast::().unwrap(); - py_float.value() - }); - }); -} - -fn extract_float_downcast_fail(bench: &mut Bencher<'_>) { - Python::with_gil(|py| { - let d = PyDict::new(py).into_any(); - - bench.iter(|| match black_box(&d).downcast::() { - Ok(v) => panic!("should err {}", v), - Err(e) => e, - }); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("extract_str_extract_success", extract_str_extract_success); - c.bench_function("extract_str_extract_fail", extract_str_extract_fail); - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - c.bench_function("extract_str_downcast_success", extract_str_downcast_success); - c.bench_function("extract_str_downcast_fail", extract_str_downcast_fail); - c.bench_function("extract_int_extract_success", extract_int_extract_success); - c.bench_function("extract_int_extract_fail", extract_int_extract_fail); - c.bench_function("extract_int_downcast_success", extract_int_downcast_success); - c.bench_function("extract_int_downcast_fail", extract_int_downcast_fail); - c.bench_function( - "extract_float_extract_success", - extract_float_extract_success, - ); - c.bench_function("extract_float_extract_fail", extract_float_extract_fail); - c.bench_function( - "extract_float_downcast_success", - extract_float_downcast_success, - ); - c.bench_function("extract_float_downcast_fail", extract_float_downcast_fail); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_frompyobject.rs b/vendor/pyo3/pyo3-benches/benches/bench_frompyobject.rs deleted file mode 100644 index 6411a391f9..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_frompyobject.rs +++ /dev/null @@ -1,89 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::{ - prelude::*, - types::{PyList, PyString}, -}; - -#[derive(FromPyObject)] -#[allow(dead_code)] -enum ManyTypes { - Int(i32), - Bytes(Vec), - String(String), -} - -fn enum_from_pyobject(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyString::new(py, "hello world").into_any(); - - b.iter(|| black_box(&any).extract::().unwrap()); - }) -} - -fn list_via_downcast(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyList::empty(py).into_any(); - - b.iter(|| black_box(&any).downcast::().unwrap()); - }) -} - -fn list_via_extract(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyList::empty(py).into_any(); - - b.iter(|| black_box(&any).extract::>().unwrap()); - }) -} - -fn not_a_list_via_downcast(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyString::new(py, "foobar").into_any(); - - b.iter(|| black_box(&any).downcast::().unwrap_err()); - }) -} - -fn not_a_list_via_extract(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyString::new(py, "foobar").into_any(); - - b.iter(|| black_box(&any).extract::>().unwrap_err()); - }) -} - -#[derive(FromPyObject)] -enum ListOrNotList<'a> { - List(Bound<'a, PyList>), - NotList(Bound<'a, PyAny>), -} - -fn not_a_list_via_extract_enum(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let any = PyString::new(py, "foobar").into_any(); - - b.iter(|| match black_box(&any).extract::>() { - Ok(ListOrNotList::List(_list)) => panic!(), - Ok(ListOrNotList::NotList(any)) => any, - Err(_) => panic!(), - }); - }) -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("enum_from_pyobject", enum_from_pyobject); - - c.bench_function("list_via_downcast", list_via_downcast); - - c.bench_function("list_via_extract", list_via_extract); - - c.bench_function("not_a_list_via_downcast", not_a_list_via_downcast); - c.bench_function("not_a_list_via_extract", not_a_list_via_extract); - c.bench_function("not_a_list_via_extract_enum", not_a_list_via_extract_enum); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_gil.rs b/vendor/pyo3/pyo3-benches/benches/bench_gil.rs deleted file mode 100644 index cede8836f3..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_gil.rs +++ /dev/null @@ -1,22 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::prelude::*; - -fn bench_clean_acquire_gil(b: &mut Bencher<'_>) { - // Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead. - b.iter(|| Python::with_gil(|_| {})); -} - -fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) { - let obj = Python::with_gil(|py| py.None()); - // Drop the returned clone of the object so that the reference pool has work to do. - b.iter(|| Python::with_gil(|py| obj.clone_ref(py))); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("clean_acquire_gil", bench_clean_acquire_gil); - c.bench_function("dirty_acquire_gil", bench_dirty_acquire_gil); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_intern.rs b/vendor/pyo3/pyo3-benches/benches/bench_intern.rs deleted file mode 100644 index 1b7dc07370..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_intern.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::prelude::*; - -use pyo3::intern; - -fn getattr_direct(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let sys = &py.import("sys").unwrap(); - - b.iter(|| black_box(sys).getattr("version").unwrap()); - }); -} - -fn getattr_intern(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let sys = &py.import("sys").unwrap(); - - b.iter(|| black_box(sys).getattr(intern!(py, "version")).unwrap()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("getattr_direct", getattr_direct); - c.bench_function("getattr_intern", getattr_intern); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_intopyobject.rs b/vendor/pyo3/pyo3-benches/benches/bench_intopyobject.rs deleted file mode 100644 index 42af893cd8..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_intopyobject.rs +++ /dev/null @@ -1,95 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::conversion::IntoPyObject; -use pyo3::prelude::*; -use pyo3::types::PyBytes; - -fn bench_bytes_new(b: &mut Bencher<'_>, data: &[u8]) { - Python::with_gil(|py| { - b.iter_with_large_drop(|| PyBytes::new(py, black_box(data))); - }); -} - -fn bytes_new_small(b: &mut Bencher<'_>) { - bench_bytes_new(b, &[]); -} - -fn bytes_new_medium(b: &mut Bencher<'_>) { - let data = (0..u8::MAX).collect::>(); - bench_bytes_new(b, &data); -} - -fn bytes_new_large(b: &mut Bencher<'_>) { - let data = vec![10u8; 100_000]; - bench_bytes_new(b, &data); -} - -fn bench_bytes_into_pyobject(b: &mut Bencher<'_>, data: &[u8]) { - Python::with_gil(|py| { - b.iter_with_large_drop(|| black_box(data).into_pyobject(py)); - }); -} - -fn byte_slice_into_pyobject_small(b: &mut Bencher<'_>) { - bench_bytes_into_pyobject(b, &[]); -} - -fn byte_slice_into_pyobject_medium(b: &mut Bencher<'_>) { - let data = (0..u8::MAX).collect::>(); - bench_bytes_into_pyobject(b, &data); -} - -fn byte_slice_into_pyobject_large(b: &mut Bencher<'_>) { - let data = vec![10u8; 100_000]; - bench_bytes_into_pyobject(b, &data); -} - -#[allow(deprecated)] -fn byte_slice_into_py(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let data = (0..u8::MAX).collect::>(); - let bytes = data.as_slice(); - b.iter_with_large_drop(|| black_box(bytes).into_py(py)); - }); -} - -fn vec_into_pyobject(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let bytes = (0..u8::MAX).collect::>(); - b.iter_with_large_drop(|| black_box(&bytes).clone().into_pyobject(py)); - }); -} - -#[allow(deprecated)] -fn vec_into_py(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - let bytes = (0..u8::MAX).collect::>(); - b.iter_with_large_drop(|| black_box(&bytes).clone().into_py(py)); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("bytes_new_small", bytes_new_small); - c.bench_function("bytes_new_medium", bytes_new_medium); - c.bench_function("bytes_new_large", bytes_new_large); - c.bench_function( - "byte_slice_into_pyobject_small", - byte_slice_into_pyobject_small, - ); - c.bench_function( - "byte_slice_into_pyobject_medium", - byte_slice_into_pyobject_medium, - ); - c.bench_function( - "byte_slice_into_pyobject_large", - byte_slice_into_pyobject_large, - ); - c.bench_function("byte_slice_into_py", byte_slice_into_py); - c.bench_function("vec_into_pyobject", vec_into_pyobject); - c.bench_function("vec_into_py", vec_into_py); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_list.rs b/vendor/pyo3/pyo3-benches/benches/bench_list.rs deleted file mode 100644 index cc790db37b..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_list.rs +++ /dev/null @@ -1,76 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::prelude::*; -use pyo3::types::{PyList, PySequence}; - -fn iter_list(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let list = PyList::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for x in &list { - let i: u64 = x.extract().unwrap(); - sum += i; - } - }); - }); -} - -fn list_new(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - b.iter_with_large_drop(|| PyList::new(py, 0..LEN)); - }); -} - -fn list_get_item(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let list = PyList::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - sum += list.get_item(i).unwrap().extract::().unwrap(); - } - }); - }); -} - -#[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] -fn list_get_item_unchecked(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let list = PyList::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - unsafe { - sum += list.get_item_unchecked(i).extract::().unwrap(); - } - } - }); - }); -} - -fn sequence_from_list(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let list = &PyList::new(py, 0..LEN).unwrap(); - b.iter(|| black_box(list).downcast::().unwrap()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("iter_list", iter_list); - c.bench_function("list_new", list_new); - c.bench_function("list_get_item", list_get_item); - #[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] - c.bench_function("list_get_item_unchecked", list_get_item_unchecked); - c.bench_function("sequence_from_list", sequence_from_list); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_pyclass.rs b/vendor/pyo3/pyo3-benches/benches/bench_pyclass.rs deleted file mode 100644 index b917a4acc0..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_pyclass.rs +++ /dev/null @@ -1,46 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; -use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*}; - -/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle. -#[pyclass] -struct MyClass { - #[pyo3(get, set)] - elements: Vec, -} - -#[pymethods] -impl MyClass { - #[new] - fn new(elements: Vec) -> Self { - Self { elements } - } - - fn __call__(&mut self, new_element: i32) -> usize { - self.elements.push(new_element); - self.elements.len() - } - - /// A basic __str__ implementation. - fn __str__(&self) -> &'static str { - "MyClass" - } -} - -pub fn first_time_init(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - b.iter(|| { - // This is using an undocumented internal PyO3 API to measure pyclass performance; please - // don't use this in your own code! - let ty = LazyTypeObject::::new(); - ty.get_or_init(py); - }); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("first_time_init", first_time_init); -} - -criterion_group!(benches, criterion_benchmark); - -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_pyobject.rs b/vendor/pyo3/pyo3-benches/benches/bench_pyobject.rs deleted file mode 100644 index c731216c79..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_pyobject.rs +++ /dev/null @@ -1,116 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion}; - -use std::sync::{ - atomic::{AtomicUsize, Ordering}, - mpsc::channel, - Arc, Barrier, -}; -use std::thread::spawn; -use std::time::{Duration, Instant}; - -use pyo3::prelude::*; - -fn drop_many_objects(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - b.iter(|| { - for _ in 0..1000 { - drop(py.None()); - } - }); - }); -} - -fn drop_many_objects_without_gil(b: &mut Bencher<'_>) { - b.iter_batched( - || Python::with_gil(|py| (0..1000).map(|_| py.None()).collect::>()), - |objs| { - drop(objs); - - Python::with_gil(|_py| ()); - }, - BatchSize::SmallInput, - ); -} - -fn drop_many_objects_multiple_threads(b: &mut Bencher<'_>) { - const THREADS: usize = 5; - - let barrier = Arc::new(Barrier::new(1 + THREADS)); - - let done = Arc::new(AtomicUsize::new(0)); - - let sender = (0..THREADS) - .map(|_| { - let (sender, receiver) = channel(); - - let barrier = barrier.clone(); - - let done = done.clone(); - - spawn(move || { - for objs in receiver { - barrier.wait(); - - drop(objs); - - done.fetch_add(1, Ordering::AcqRel); - } - }); - - sender - }) - .collect::>(); - - b.iter_custom(|iters| { - let mut duration = Duration::ZERO; - - let mut last_done = done.load(Ordering::Acquire); - - for _ in 0..iters { - for sender in &sender { - let objs = Python::with_gil(|py| { - (0..1000 / THREADS) - .map(|_| py.None()) - .collect::>() - }); - - sender.send(objs).unwrap(); - } - - barrier.wait(); - - let start = Instant::now(); - - loop { - Python::with_gil(|_py| ()); - - let done = done.load(Ordering::Acquire); - if done - last_done == THREADS { - last_done = done; - break; - } - } - - Python::with_gil(|_py| ()); - - duration += start.elapsed(); - } - - duration - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("drop_many_objects", drop_many_objects); - c.bench_function( - "drop_many_objects_without_gil", - drop_many_objects_without_gil, - ); - c.bench_function( - "drop_many_objects_multiple_threads", - drop_many_objects_multiple_threads, - ); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_set.rs b/vendor/pyo3/pyo3-benches/benches/bench_set.rs deleted file mode 100644 index 2d468740ea..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_set.rs +++ /dev/null @@ -1,67 +0,0 @@ -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::types::PySet; -use pyo3::{prelude::*, IntoPyObjectExt}; -use std::{ - collections::{BTreeSet, HashSet}, - hint::black_box, -}; - -fn set_new(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - // Create Python objects up-front, so that the benchmark doesn't need to include - // the cost of allocating LEN Python integers - let elements: Vec = (0..LEN).map(|i| i.into_py_any(py).unwrap()).collect(); - b.iter_with_large_drop(|| PySet::new(py, &elements).unwrap()); - }); -} - -fn iter_set(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let set = PySet::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for x in &set { - let i: u64 = x.extract().unwrap(); - sum += i; - } - }); - }); -} - -fn extract_hashset(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let any = PySet::new(py, 0..LEN).unwrap().into_any(); - b.iter_with_large_drop(|| black_box(&any).extract::>()); - }); -} - -fn extract_btreeset(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let any = PySet::new(py, 0..LEN).unwrap().into_any(); - b.iter_with_large_drop(|| black_box(&any).extract::>()); - }); -} - -fn extract_hashbrown_set(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let any = PySet::new(py, 0..LEN).unwrap().into_any(); - b.iter_with_large_drop(|| black_box(&any).extract::>()); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("set_new", set_new); - c.bench_function("iter_set", iter_set); - c.bench_function("extract_hashset", extract_hashset); - c.bench_function("extract_btreeset", extract_btreeset); - c.bench_function("extract_hashbrown_set", extract_hashbrown_set); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/benches/bench_tuple.rs b/vendor/pyo3/pyo3-benches/benches/bench_tuple.rs deleted file mode 100644 index 72146c80b5..0000000000 --- a/vendor/pyo3/pyo3-benches/benches/bench_tuple.rs +++ /dev/null @@ -1,155 +0,0 @@ -use std::hint::black_box; - -use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion}; - -use pyo3::prelude::*; -use pyo3::types::{PyList, PySequence, PyTuple}; - -fn iter_tuple(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 100_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for x in tuple.iter_borrowed() { - let i: u64 = x.extract().unwrap(); - sum += i; - } - }); - }); -} - -fn tuple_new(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - b.iter_with_large_drop(|| PyTuple::new(py, 0..LEN).unwrap()); - }); -} - -fn tuple_get_item(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - sum += tuple.get_item(i).unwrap().extract::().unwrap(); - } - }); - }); -} - -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -fn tuple_get_item_unchecked(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - unsafe { - sum += tuple.get_item_unchecked(i).extract::().unwrap(); - } - } - }); - }); -} - -fn tuple_get_borrowed_item(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - sum += tuple - .get_borrowed_item(i) - .unwrap() - .extract::() - .unwrap(); - } - }); - }); -} - -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -fn tuple_get_borrowed_item_unchecked(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - let mut sum = 0; - b.iter(|| { - for i in 0..LEN { - unsafe { - sum += tuple - .get_borrowed_item_unchecked(i) - .extract::() - .unwrap(); - } - } - }); - }); -} - -fn sequence_from_tuple(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap().into_any(); - b.iter(|| black_box(&tuple).downcast::().unwrap()); - }); -} - -fn tuple_new_list(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - b.iter_with_large_drop(|| PyList::new(py, tuple.iter_borrowed())); - }); -} - -fn tuple_to_list(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - const LEN: usize = 50_000; - let tuple = PyTuple::new(py, 0..LEN).unwrap(); - b.iter_with_large_drop(|| tuple.to_list()); - }); -} - -#[allow(deprecated)] -fn tuple_into_py(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - b.iter(|| -> PyObject { (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).into_py(py) }); - }); -} - -fn tuple_into_pyobject(b: &mut Bencher<'_>) { - Python::with_gil(|py| { - b.iter(|| { - (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) - .into_pyobject(py) - .unwrap() - }); - }); -} - -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("iter_tuple", iter_tuple); - c.bench_function("tuple_new", tuple_new); - c.bench_function("tuple_get_item", tuple_get_item); - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - c.bench_function("tuple_get_item_unchecked", tuple_get_item_unchecked); - c.bench_function("tuple_get_borrowed_item", tuple_get_borrowed_item); - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - c.bench_function( - "tuple_get_borrowed_item_unchecked", - tuple_get_borrowed_item_unchecked, - ); - c.bench_function("sequence_from_tuple", sequence_from_tuple); - c.bench_function("tuple_new_list", tuple_new_list); - c.bench_function("tuple_to_list", tuple_to_list); - c.bench_function("tuple_into_py", tuple_into_py); - c.bench_function("tuple_into_pyobject", tuple_into_pyobject); -} - -criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); diff --git a/vendor/pyo3/pyo3-benches/build.rs b/vendor/pyo3/pyo3-benches/build.rs deleted file mode 100644 index 0475124bb4..0000000000 --- a/vendor/pyo3/pyo3-benches/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - pyo3_build_config::use_pyo3_cfgs(); -} diff --git a/vendor/pyo3/pyo3-build-config/Cargo.toml b/vendor/pyo3/pyo3-build-config/Cargo.toml deleted file mode 100644 index cee4c3ad52..0000000000 --- a/vendor/pyo3/pyo3-build-config/Cargo.toml +++ /dev/null @@ -1,43 +0,0 @@ -[package] -name = "pyo3-build-config" -version = "0.23.4" -description = "Build configuration for the PyO3 ecosystem" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" -rust-version = "1.63" - -[dependencies] -once_cell = "1" -python3-dll-a = { version = "0.2.12", optional = true } -target-lexicon = "0.12.14" - -[build-dependencies] -python3-dll-a = { version = "0.2.12", optional = true } -target-lexicon = "0.12.14" - -[features] -default = [] - -# Attempt to resolve a Python interpreter config for building in the build -# script. If this feature isn't enabled, the build script no-ops. -resolve-config = [] - -# This feature is enabled by pyo3 when building an extension module. -extension-module = [] - -# These features are enabled by pyo3 when building Stable ABI extension modules. -abi3 = [] -abi3-py37 = ["abi3-py38"] -abi3-py38 = ["abi3-py39"] -abi3-py39 = ["abi3-py310"] -abi3-py310 = ["abi3-py311"] -abi3-py311 = ["abi3-py312"] -abi3-py312 = ["abi3"] - -[package.metadata.docs.rs] -features = ["resolve-config"] diff --git a/vendor/pyo3/pyo3-build-config/LICENSE-APACHE b/vendor/pyo3/pyo3-build-config/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/pyo3/pyo3-build-config/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/pyo3/pyo3-build-config/LICENSE-MIT b/vendor/pyo3/pyo3-build-config/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/pyo3/pyo3-build-config/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/pyo3/pyo3-build-config/build.rs b/vendor/pyo3/pyo3-build-config/build.rs deleted file mode 100644 index a6e767edcf..0000000000 --- a/vendor/pyo3/pyo3-build-config/build.rs +++ /dev/null @@ -1,58 +0,0 @@ -// Import some modules from this crate inline to generate the build config. -// Allow dead code because not all code in the modules is used in this build script. - -#[path = "src/impl_.rs"] -#[allow(dead_code)] -mod impl_; - -#[path = "src/errors.rs"] -#[allow(dead_code)] -mod errors; - -use std::{env, path::Path}; - -use errors::{Context, Result}; -use impl_::{make_interpreter_config, InterpreterConfig}; - -fn configure(interpreter_config: Option, name: &str) -> Result { - let target = Path::new(&env::var_os("OUT_DIR").unwrap()).join(name); - if let Some(config) = interpreter_config { - config - .to_writer(&mut std::fs::File::create(&target).with_context(|| { - format!("failed to write config file at {}", target.display()) - })?)?; - Ok(true) - } else { - std::fs::File::create(&target) - .with_context(|| format!("failed to create new file at {}", target.display()))?; - Ok(false) - } -} - -fn generate_build_configs() -> Result<()> { - // If PYO3_CONFIG_FILE is set, copy it into the crate. - let configured = configure( - InterpreterConfig::from_pyo3_config_file_env().transpose()?, - "pyo3-build-config-file.txt", - )?; - - if configured { - // Don't bother trying to find an interpreter on the host system - // if the user-provided config file is present. - configure(None, "pyo3-build-config.txt")?; - } else { - configure(Some(make_interpreter_config()?), "pyo3-build-config.txt")?; - } - Ok(()) -} - -fn main() { - if std::env::var("CARGO_FEATURE_RESOLVE_CONFIG").is_ok() { - if let Err(e) = generate_build_configs() { - eprintln!("error: {}", e.report()); - std::process::exit(1) - } - } else { - eprintln!("resolve-config feature not enabled; build script in no-op mode"); - } -} diff --git a/vendor/pyo3/pyo3-build-config/src/errors.rs b/vendor/pyo3/pyo3-build-config/src/errors.rs deleted file mode 100644 index 87c59a998b..0000000000 --- a/vendor/pyo3/pyo3-build-config/src/errors.rs +++ /dev/null @@ -1,153 +0,0 @@ -/// A simple macro for returning an error. Resembles anyhow::bail. -#[macro_export] -#[doc(hidden)] -macro_rules! bail { - ($($args: tt)+) => { return Err(format!($($args)+).into()) }; -} - -/// A simple macro for checking a condition. Resembles anyhow::ensure. -#[macro_export] -#[doc(hidden)] -macro_rules! ensure { - ($condition:expr, $($args: tt)+) => { if !($condition) { bail!($($args)+) } }; -} - -/// Show warning. -#[macro_export] -#[doc(hidden)] -macro_rules! warn { - ($($args: tt)+) => { - println!("{}", $crate::format_warn!($($args)+)) - }; -} - -/// Format warning into string. -#[macro_export] -#[doc(hidden)] -macro_rules! format_warn { - ($($args: tt)+) => { - format!("cargo:warning={}", format_args!($($args)+)) - }; -} - -/// A simple error implementation which allows chaining of errors, inspired somewhat by anyhow. -#[derive(Debug)] -pub struct Error { - value: String, - source: Option>, -} - -/// Error report inspired by -/// -pub struct ErrorReport<'a>(&'a Error); - -impl Error { - pub fn report(&self) -> ErrorReport<'_> { - ErrorReport(self) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.value) - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.source.as_deref() - } -} - -impl std::fmt::Display for ErrorReport<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use std::error::Error; - self.0.fmt(f)?; - let mut source = self.0.source(); - if source.is_some() { - writeln!(f, "\ncaused by:")?; - let mut index = 0; - while let Some(some_source) = source { - writeln!(f, " - {}: {}", index, some_source)?; - source = some_source.source(); - index += 1; - } - } - Ok(()) - } -} - -impl From for Error { - fn from(value: String) -> Self { - Self { - value, - source: None, - } - } -} - -impl From<&'_ str> for Error { - fn from(value: &str) -> Self { - value.to_string().into() - } -} - -impl From for Error { - fn from(value: std::convert::Infallible) -> Self { - match value {} - } -} - -pub type Result = std::result::Result; - -pub trait Context { - fn context(self, message: impl Into) -> Result; - fn with_context(self, message: impl FnOnce() -> String) -> Result; -} - -impl Context for Result -where - E: std::error::Error + 'static, -{ - fn context(self, message: impl Into) -> Result { - self.map_err(|error| Error { - value: message.into(), - source: Some(Box::new(error)), - }) - } - - fn with_context(self, message: impl FnOnce() -> String) -> Result { - self.map_err(|error| Error { - value: message(), - source: Some(Box::new(error)), - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn error_report() { - let error: Result<()> = Err(Error::from("there was an internal error")) - .with_context(|| format!("failed to do {}", "something difficult")) - .context("things went wrong"); - - assert_eq!( - error - .unwrap_err() - .report() - .to_string() - .split('\n') - .collect::>(), - vec![ - "things went wrong", - "caused by:", - " - 0: failed to do something difficult", - " - 1: there was an internal error", - "" - ] - ); - } -} diff --git a/vendor/pyo3/pyo3-build-config/src/impl_.rs b/vendor/pyo3/pyo3-build-config/src/impl_.rs deleted file mode 100644 index 1de37da010..0000000000 --- a/vendor/pyo3/pyo3-build-config/src/impl_.rs +++ /dev/null @@ -1,3204 +0,0 @@ -//! Main implementation module included in both the `pyo3-build-config` library crate -//! and its build script. - -// Optional python3.dll import library generator for Windows -#[cfg(feature = "python3-dll-a")] -#[path = "import_lib.rs"] -mod import_lib; - -#[cfg(test)] -use std::cell::RefCell; -use std::{ - collections::{HashMap, HashSet}, - env, - ffi::{OsStr, OsString}, - fmt::Display, - fs::{self, DirEntry}, - io::{BufRead, BufReader, Read, Write}, - path::{Path, PathBuf}, - process::{Command, Stdio}, - str::{self, FromStr}, -}; - -pub use target_lexicon::Triple; - -use target_lexicon::{Architecture, Environment, OperatingSystem}; - -use crate::{ - bail, ensure, - errors::{Context, Error, Result}, - warn, -}; - -/// Minimum Python version PyO3 supports. -pub(crate) const MINIMUM_SUPPORTED_VERSION: PythonVersion = PythonVersion { major: 3, minor: 7 }; - -/// GraalPy may implement the same CPython version over multiple releases. -const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion { - major: 24, - minor: 0, -}; - -/// Maximum Python version that can be used as minimum required Python version with abi3. -pub(crate) const ABI3_MAX_MINOR: u8 = 12; - -#[cfg(test)] -thread_local! { - static READ_ENV_VARS: RefCell> = const { RefCell::new(Vec::new()) }; -} - -/// Gets an environment variable owned by cargo. -/// -/// Environment variables set by cargo are expected to be valid UTF8. -pub fn cargo_env_var(var: &str) -> Option { - env::var_os(var).map(|os_string| os_string.to_str().unwrap().into()) -} - -/// Gets an external environment variable, and registers the build script to rerun if -/// the variable changes. -pub fn env_var(var: &str) -> Option { - if cfg!(feature = "resolve-config") { - println!("cargo:rerun-if-env-changed={}", var); - } - #[cfg(test)] - { - READ_ENV_VARS.with(|env_vars| { - env_vars.borrow_mut().push(var.to_owned()); - }); - } - env::var_os(var) -} - -/// Gets the compilation target triple from environment variables set by Cargo. -/// -/// Must be called from a crate build script. -pub fn target_triple_from_env() -> Triple { - env::var("TARGET") - .expect("target_triple_from_env() must be called from a build script") - .parse() - .expect("Unrecognized TARGET environment variable value") -} - -/// Configuration needed by PyO3 to build for the correct Python implementation. -/// -/// Usually this is queried directly from the Python interpreter, or overridden using the -/// `PYO3_CONFIG_FILE` environment variable. -/// -/// When the `PYO3_NO_PYTHON` variable is set, or during cross compile situations, then alternative -/// strategies are used to populate this type. -#[cfg_attr(test, derive(Debug, PartialEq, Eq))] -pub struct InterpreterConfig { - /// The Python implementation flavor. - /// - /// Serialized to `implementation`. - pub implementation: PythonImplementation, - - /// Python `X.Y` version. e.g. `3.9`. - /// - /// Serialized to `version`. - pub version: PythonVersion, - - /// Whether link library is shared. - /// - /// Serialized to `shared`. - pub shared: bool, - - /// Whether linking against the stable/limited Python 3 API. - /// - /// Serialized to `abi3`. - pub abi3: bool, - - /// The name of the link library defining Python. - /// - /// This effectively controls the `cargo:rustc-link-lib=` value to - /// control how libpython is linked. Values should not contain the `lib` - /// prefix. - /// - /// Serialized to `lib_name`. - pub lib_name: Option, - - /// The directory containing the Python library to link against. - /// - /// The effectively controls the `cargo:rustc-link-search=native=` value - /// to add an additional library search path for the linker. - /// - /// Serialized to `lib_dir`. - pub lib_dir: Option, - - /// Path of host `python` executable. - /// - /// This is a valid executable capable of running on the host/building machine. - /// For configurations derived by invoking a Python interpreter, it was the - /// executable invoked. - /// - /// Serialized to `executable`. - pub executable: Option, - - /// Width in bits of pointers on the target machine. - /// - /// Serialized to `pointer_width`. - pub pointer_width: Option, - - /// Additional relevant Python build flags / configuration settings. - /// - /// Serialized to `build_flags`. - pub build_flags: BuildFlags, - - /// Whether to suppress emitting of `cargo:rustc-link-*` lines from the build script. - /// - /// Typically, `pyo3`'s build script will emit `cargo:rustc-link-lib=` and - /// `cargo:rustc-link-search=` lines derived from other fields in this struct. In - /// advanced building configurations, the default logic to derive these lines may not - /// be sufficient. This field can be set to `Some(true)` to suppress the emission - /// of these lines. - /// - /// If suppression is enabled, `extra_build_script_lines` should contain equivalent - /// functionality or else a build failure is likely. - pub suppress_build_script_link_lines: bool, - - /// Additional lines to `println!()` from Cargo build scripts. - /// - /// This field can be populated to enable the `pyo3` crate to emit additional lines from its - /// its Cargo build script. - /// - /// This crate doesn't populate this field itself. Rather, it is intended to be used with - /// externally provided config files to give them significant control over how the crate - /// is build/configured. - /// - /// Serialized to multiple `extra_build_script_line` values. - pub extra_build_script_lines: Vec, - /// macOS Python3.framework requires special rpath handling - pub python_framework_prefix: Option, -} - -impl InterpreterConfig { - #[doc(hidden)] - pub fn build_script_outputs(&self) -> Vec { - // This should have been checked during pyo3-build-config build time. - assert!(self.version >= MINIMUM_SUPPORTED_VERSION); - - let mut out = vec![]; - - for i in MINIMUM_SUPPORTED_VERSION.minor..=self.version.minor { - out.push(format!("cargo:rustc-cfg=Py_3_{}", i)); - } - - match self.implementation { - PythonImplementation::CPython => {} - PythonImplementation::PyPy => out.push("cargo:rustc-cfg=PyPy".to_owned()), - PythonImplementation::GraalPy => out.push("cargo:rustc-cfg=GraalPy".to_owned()), - } - - // If Py_GIL_DISABLED is set, do not build with limited API support - if self.abi3 && !self.is_free_threaded() { - out.push("cargo:rustc-cfg=Py_LIMITED_API".to_owned()); - } - - for flag in &self.build_flags.0 { - match flag { - BuildFlag::Py_GIL_DISABLED => { - out.push("cargo:rustc-cfg=Py_GIL_DISABLED".to_owned()) - } - flag => out.push(format!("cargo:rustc-cfg=py_sys_config=\"{}\"", flag)), - } - } - - out - } - - #[doc(hidden)] - pub fn from_interpreter(interpreter: impl AsRef) -> Result { - const SCRIPT: &str = r#" -# Allow the script to run on Python 2, so that nicer error can be printed later. -from __future__ import print_function - -import os.path -import platform -import struct -import sys -from sysconfig import get_config_var, get_platform - -PYPY = platform.python_implementation() == "PyPy" -GRAALPY = platform.python_implementation() == "GraalVM" - -if GRAALPY: - graalpy_ver = map(int, __graalpython__.get_graalvm_version().split('.')); - print("graalpy_major", next(graalpy_ver)) - print("graalpy_minor", next(graalpy_ver)) - -# sys.base_prefix is missing on Python versions older than 3.3; this allows the script to continue -# so that the version mismatch can be reported in a nicer way later. -base_prefix = getattr(sys, "base_prefix", None) - -if base_prefix: - # Anaconda based python distributions have a static python executable, but include - # the shared library. Use the shared library for embedding to avoid rust trying to - # LTO the static library (and failing with newer gcc's, because it is old). - ANACONDA = os.path.exists(os.path.join(base_prefix, "conda-meta")) -else: - ANACONDA = False - -def print_if_set(varname, value): - if value is not None: - print(varname, value) - -# Windows always uses shared linking -WINDOWS = platform.system() == "Windows" - -# macOS framework packages use shared linking -FRAMEWORK = bool(get_config_var("PYTHONFRAMEWORK")) -FRAMEWORK_PREFIX = get_config_var("PYTHONFRAMEWORKPREFIX") - -# unix-style shared library enabled -SHARED = bool(get_config_var("Py_ENABLE_SHARED")) - -print("implementation", platform.python_implementation()) -print("version_major", sys.version_info[0]) -print("version_minor", sys.version_info[1]) -print("shared", PYPY or GRAALPY or ANACONDA or WINDOWS or FRAMEWORK or SHARED) -print("python_framework_prefix", FRAMEWORK_PREFIX) -print_if_set("ld_version", get_config_var("LDVERSION")) -print_if_set("libdir", get_config_var("LIBDIR")) -print_if_set("base_prefix", base_prefix) -print("executable", sys.executable) -print("calcsize_pointer", struct.calcsize("P")) -print("mingw", get_platform().startswith("mingw")) -print("ext_suffix", get_config_var("EXT_SUFFIX")) -print("gil_disabled", get_config_var("Py_GIL_DISABLED")) -"#; - let output = run_python_script(interpreter.as_ref(), SCRIPT)?; - let map: HashMap = parse_script_output(&output); - - ensure!( - !map.is_empty(), - "broken Python interpreter: {}", - interpreter.as_ref().display() - ); - - if let Some(value) = map.get("graalpy_major") { - let graalpy_version = PythonVersion { - major: value - .parse() - .context("failed to parse GraalPy major version")?, - minor: map["graalpy_minor"] - .parse() - .context("failed to parse GraalPy minor version")?, - }; - ensure!( - graalpy_version >= MINIMUM_SUPPORTED_VERSION_GRAALPY, - "At least GraalPy version {} needed, got {}", - MINIMUM_SUPPORTED_VERSION_GRAALPY, - graalpy_version - ); - }; - - let shared = map["shared"].as_str() == "True"; - let python_framework_prefix = map.get("python_framework_prefix").cloned(); - - let version = PythonVersion { - major: map["version_major"] - .parse() - .context("failed to parse major version")?, - minor: map["version_minor"] - .parse() - .context("failed to parse minor version")?, - }; - - let abi3 = is_abi3(); - - let implementation = map["implementation"].parse()?; - - let gil_disabled = match map["gil_disabled"].as_str() { - "1" => true, - "0" => false, - "None" => false, - _ => panic!("Unknown Py_GIL_DISABLED value"), - }; - - let lib_name = if cfg!(windows) { - default_lib_name_windows( - version, - implementation, - abi3, - map["mingw"].as_str() == "True", - // This is the best heuristic currently available to detect debug build - // on Windows from sysconfig - e.g. ext_suffix may be - // `_d.cp312-win_amd64.pyd` for 3.12 debug build - map["ext_suffix"].starts_with("_d."), - gil_disabled, - )? - } else { - default_lib_name_unix( - version, - implementation, - map.get("ld_version").map(String::as_str), - gil_disabled, - )? - }; - - let lib_dir = if cfg!(windows) { - map.get("base_prefix") - .map(|base_prefix| format!("{}\\libs", base_prefix)) - } else { - map.get("libdir").cloned() - }; - - // The reason we don't use platform.architecture() here is that it's not - // reliable on macOS. See https://stackoverflow.com/a/1405971/823869. - // Similarly, sys.maxsize is not reliable on Windows. See - // https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os/1405971#comment6209952_1405971 - // and https://stackoverflow.com/a/3411134/823869. - let calcsize_pointer: u32 = map["calcsize_pointer"] - .parse() - .context("failed to parse calcsize_pointer")?; - - Ok(InterpreterConfig { - version, - implementation, - shared, - abi3, - lib_name: Some(lib_name), - lib_dir, - executable: map.get("executable").cloned(), - pointer_width: Some(calcsize_pointer * 8), - build_flags: BuildFlags::from_interpreter(interpreter)?, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix, - }) - } - - /// Generate from parsed sysconfigdata file - /// - /// Use [`parse_sysconfigdata`] to generate a hash map of configuration values which may be - /// used to build an [`InterpreterConfig`]. - pub fn from_sysconfigdata(sysconfigdata: &Sysconfigdata) -> Result { - macro_rules! get_key { - ($sysconfigdata:expr, $key:literal) => { - $sysconfigdata - .get_value($key) - .ok_or(concat!($key, " not found in sysconfigdata file")) - }; - } - - macro_rules! parse_key { - ($sysconfigdata:expr, $key:literal) => { - get_key!($sysconfigdata, $key)? - .parse() - .context(concat!("could not parse value of ", $key)) - }; - } - - let soabi = get_key!(sysconfigdata, "SOABI")?; - let implementation = PythonImplementation::from_soabi(soabi)?; - let version = parse_key!(sysconfigdata, "VERSION")?; - let shared = match sysconfigdata.get_value("Py_ENABLE_SHARED") { - Some("1") | Some("true") | Some("True") => true, - Some("0") | Some("false") | Some("False") => false, - _ => bail!("expected a bool (1/true/True or 0/false/False) for Py_ENABLE_SHARED"), - }; - // macOS framework packages use shared linking (PYTHONFRAMEWORK is the framework name, hence the empty check) - let framework = match sysconfigdata.get_value("PYTHONFRAMEWORK") { - Some(s) => !s.is_empty(), - _ => false, - }; - let python_framework_prefix = sysconfigdata - .get_value("PYTHONFRAMEWORKPREFIX") - .map(str::to_string); - let lib_dir = get_key!(sysconfigdata, "LIBDIR").ok().map(str::to_string); - let gil_disabled = match sysconfigdata.get_value("Py_GIL_DISABLED") { - Some(value) => value == "1", - None => false, - }; - let lib_name = Some(default_lib_name_unix( - version, - implementation, - sysconfigdata.get_value("LDVERSION"), - gil_disabled, - )?); - let pointer_width = parse_key!(sysconfigdata, "SIZEOF_VOID_P") - .map(|bytes_width: u32| bytes_width * 8) - .ok(); - let build_flags = BuildFlags::from_sysconfigdata(sysconfigdata); - - Ok(InterpreterConfig { - implementation, - version, - shared: shared || framework, - abi3: is_abi3(), - lib_dir, - lib_name, - executable: None, - pointer_width, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix, - }) - } - - /// Import an externally-provided config file. - /// - /// The `abi3` features, if set, may apply an `abi3` constraint to the Python version. - #[allow(dead_code)] // only used in build.rs - pub(super) fn from_pyo3_config_file_env() -> Option> { - env_var("PYO3_CONFIG_FILE").map(|path| { - let path = Path::new(&path); - println!("cargo:rerun-if-changed={}", path.display()); - // Absolute path is necessary because this build script is run with a cwd different to the - // original `cargo build` instruction. - ensure!( - path.is_absolute(), - "PYO3_CONFIG_FILE must be an absolute path" - ); - - let mut config = InterpreterConfig::from_path(path) - .context("failed to parse contents of PYO3_CONFIG_FILE")?; - // If the abi3 feature is enabled, the minimum Python version is constrained by the abi3 - // feature. - // - // TODO: abi3 is a property of the build mode, not the interpreter. Should this be - // removed from `InterpreterConfig`? - config.abi3 |= is_abi3(); - config.fixup_for_abi3_version(get_abi3_version())?; - - Ok(config) - }) - } - - #[doc(hidden)] - pub fn from_path(path: impl AsRef) -> Result { - let path = path.as_ref(); - let config_file = std::fs::File::open(path) - .with_context(|| format!("failed to open PyO3 config file at {}", path.display()))?; - let reader = std::io::BufReader::new(config_file); - InterpreterConfig::from_reader(reader) - } - - #[doc(hidden)] - pub fn from_cargo_dep_env() -> Option> { - cargo_env_var("DEP_PYTHON_PYO3_CONFIG") - .map(|buf| InterpreterConfig::from_reader(&*unescape(&buf))) - } - - #[doc(hidden)] - pub fn from_reader(reader: impl Read) -> Result { - let reader = BufReader::new(reader); - let lines = reader.lines(); - - macro_rules! parse_value { - ($variable:ident, $value:ident) => { - $variable = Some($value.trim().parse().context(format!( - concat!( - "failed to parse ", - stringify!($variable), - " from config value '{}'" - ), - $value - ))?) - }; - } - - let mut implementation = None; - let mut version = None; - let mut shared = None; - let mut abi3 = None; - let mut lib_name = None; - let mut lib_dir = None; - let mut executable = None; - let mut pointer_width = None; - let mut build_flags: Option = None; - let mut suppress_build_script_link_lines = None; - let mut extra_build_script_lines = vec![]; - let mut python_framework_prefix = None; - - for (i, line) in lines.enumerate() { - let line = line.context("failed to read line from config")?; - let mut split = line.splitn(2, '='); - let (key, value) = ( - split - .next() - .expect("first splitn value should always be present"), - split - .next() - .ok_or_else(|| format!("expected key=value pair on line {}", i + 1))?, - ); - match key { - "implementation" => parse_value!(implementation, value), - "version" => parse_value!(version, value), - "shared" => parse_value!(shared, value), - "abi3" => parse_value!(abi3, value), - "lib_name" => parse_value!(lib_name, value), - "lib_dir" => parse_value!(lib_dir, value), - "executable" => parse_value!(executable, value), - "pointer_width" => parse_value!(pointer_width, value), - "build_flags" => parse_value!(build_flags, value), - "suppress_build_script_link_lines" => { - parse_value!(suppress_build_script_link_lines, value) - } - "extra_build_script_line" => { - extra_build_script_lines.push(value.to_string()); - } - "python_framework_prefix" => parse_value!(python_framework_prefix, value), - unknown => warn!("unknown config key `{}`", unknown), - } - } - - let version = version.ok_or("missing value for version")?; - let implementation = implementation.unwrap_or(PythonImplementation::CPython); - let abi3 = abi3.unwrap_or(false); - let build_flags = build_flags.unwrap_or_default(); - let gil_disabled = build_flags.0.contains(&BuildFlag::Py_GIL_DISABLED); - // Fixup lib_name if it's not set - let lib_name = lib_name.or_else(|| { - if let Ok(Ok(target)) = env::var("TARGET").map(|target| target.parse::()) { - default_lib_name_for_target(version, implementation, abi3, gil_disabled, &target) - } else { - None - } - }); - - Ok(InterpreterConfig { - implementation, - version, - shared: shared.unwrap_or(true), - abi3, - lib_name, - lib_dir, - executable, - pointer_width, - build_flags, - suppress_build_script_link_lines: suppress_build_script_link_lines.unwrap_or(false), - extra_build_script_lines, - python_framework_prefix, - }) - } - - #[cfg(feature = "python3-dll-a")] - #[allow(clippy::unnecessary_wraps)] - pub fn generate_import_libs(&mut self) -> Result<()> { - // Auto generate python3.dll import libraries for Windows targets. - if self.lib_dir.is_none() { - let target = target_triple_from_env(); - let py_version = if self.implementation == PythonImplementation::CPython - && self.abi3 - && !self.is_free_threaded() - { - None - } else { - Some(self.version) - }; - let abiflags = if self.is_free_threaded() { - Some("t") - } else { - None - }; - self.lib_dir = import_lib::generate_import_lib( - &target, - self.implementation, - py_version, - abiflags, - )?; - } - Ok(()) - } - - #[cfg(not(feature = "python3-dll-a"))] - #[allow(clippy::unnecessary_wraps)] - pub fn generate_import_libs(&mut self) -> Result<()> { - Ok(()) - } - - #[doc(hidden)] - /// Serialize the `InterpreterConfig` and print it to the environment for Cargo to pass along - /// to dependent packages during build time. - /// - /// NB: writing to the cargo environment requires the - /// [`links`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key) - /// manifest key to be set. In this case that means this is called by the `pyo3-ffi` crate and - /// available for dependent package build scripts in `DEP_PYTHON_PYO3_CONFIG`. See - /// documentation for the - /// [`DEP__`](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts) - /// environment variable. - pub fn to_cargo_dep_env(&self) -> Result<()> { - let mut buf = Vec::new(); - self.to_writer(&mut buf)?; - // escape newlines in env var - println!("cargo:PYO3_CONFIG={}", escape(&buf)); - Ok(()) - } - - #[doc(hidden)] - pub fn to_writer(&self, mut writer: impl Write) -> Result<()> { - macro_rules! write_line { - ($value:ident) => { - writeln!(writer, "{}={}", stringify!($value), self.$value).context(concat!( - "failed to write ", - stringify!($value), - " to config" - )) - }; - } - - macro_rules! write_option_line { - ($value:ident) => { - if let Some(value) = &self.$value { - writeln!(writer, "{}={}", stringify!($value), value).context(concat!( - "failed to write ", - stringify!($value), - " to config" - )) - } else { - Ok(()) - } - }; - } - - write_line!(implementation)?; - write_line!(version)?; - write_line!(shared)?; - write_line!(abi3)?; - write_option_line!(lib_name)?; - write_option_line!(lib_dir)?; - write_option_line!(executable)?; - write_option_line!(pointer_width)?; - write_line!(build_flags)?; - write_option_line!(python_framework_prefix)?; - write_line!(suppress_build_script_link_lines)?; - for line in &self.extra_build_script_lines { - writeln!(writer, "extra_build_script_line={}", line) - .context("failed to write extra_build_script_line")?; - } - Ok(()) - } - - /// Run a python script using the [`InterpreterConfig::executable`]. - /// - /// # Panics - /// - /// This function will panic if the [`executable`](InterpreterConfig::executable) is `None`. - pub fn run_python_script(&self, script: &str) -> Result { - run_python_script_with_envs( - Path::new(self.executable.as_ref().expect("no interpreter executable")), - script, - std::iter::empty::<(&str, &str)>(), - ) - } - - /// Run a python script using the [`InterpreterConfig::executable`] with additional - /// environment variables (e.g. PYTHONPATH) set. - /// - /// # Panics - /// - /// This function will panic if the [`executable`](InterpreterConfig::executable) is `None`. - pub fn run_python_script_with_envs(&self, script: &str, envs: I) -> Result - where - I: IntoIterator, - K: AsRef, - V: AsRef, - { - run_python_script_with_envs( - Path::new(self.executable.as_ref().expect("no interpreter executable")), - script, - envs, - ) - } - - pub fn is_free_threaded(&self) -> bool { - self.build_flags.0.contains(&BuildFlag::Py_GIL_DISABLED) - } - - /// Updates configured ABI to build for to the requested abi3 version - /// This is a no-op for platforms where abi3 is not supported - fn fixup_for_abi3_version(&mut self, abi3_version: Option) -> Result<()> { - // PyPy, GraalPy, and the free-threaded build don't support abi3; don't adjust the version - if self.implementation.is_pypy() - || self.implementation.is_graalpy() - || self.is_free_threaded() - { - return Ok(()); - } - - if let Some(version) = abi3_version { - ensure!( - version <= self.version, - "cannot set a minimum Python version {} higher than the interpreter version {} \ - (the minimum Python version is implied by the abi3-py3{} feature)", - version, - self.version, - version.minor, - ); - - self.version = version; - } - - Ok(()) - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub struct PythonVersion { - pub major: u8, - pub minor: u8, -} - -impl PythonVersion { - pub const PY313: Self = PythonVersion { - major: 3, - minor: 13, - }; - const PY310: Self = PythonVersion { - major: 3, - minor: 10, - }; - const PY37: Self = PythonVersion { major: 3, minor: 7 }; -} - -impl Display for PythonVersion { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}.{}", self.major, self.minor) - } -} - -impl FromStr for PythonVersion { - type Err = crate::errors::Error; - - fn from_str(value: &str) -> Result { - let mut split = value.splitn(2, '.'); - let (major, minor) = ( - split - .next() - .expect("first splitn value should always be present"), - split.next().ok_or("expected major.minor version")?, - ); - Ok(Self { - major: major.parse().context("failed to parse major version")?, - minor: minor.parse().context("failed to parse minor version")?, - }) - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum PythonImplementation { - CPython, - PyPy, - GraalPy, -} - -impl PythonImplementation { - #[doc(hidden)] - pub fn is_pypy(self) -> bool { - self == PythonImplementation::PyPy - } - - #[doc(hidden)] - pub fn is_graalpy(self) -> bool { - self == PythonImplementation::GraalPy - } - - #[doc(hidden)] - pub fn from_soabi(soabi: &str) -> Result { - if soabi.starts_with("pypy") { - Ok(PythonImplementation::PyPy) - } else if soabi.starts_with("cpython") { - Ok(PythonImplementation::CPython) - } else if soabi.starts_with("graalpy") { - Ok(PythonImplementation::GraalPy) - } else { - bail!("unsupported Python interpreter"); - } - } -} - -impl Display for PythonImplementation { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - PythonImplementation::CPython => write!(f, "CPython"), - PythonImplementation::PyPy => write!(f, "PyPy"), - PythonImplementation::GraalPy => write!(f, "GraalVM"), - } - } -} - -impl FromStr for PythonImplementation { - type Err = Error; - fn from_str(s: &str) -> Result { - match s { - "CPython" => Ok(PythonImplementation::CPython), - "PyPy" => Ok(PythonImplementation::PyPy), - "GraalVM" => Ok(PythonImplementation::GraalPy), - _ => bail!("unknown interpreter: {}", s), - } - } -} - -/// Checks if we should look for a Python interpreter installation -/// to get the target interpreter configuration. -/// -/// Returns `false` if `PYO3_NO_PYTHON` environment variable is set. -fn have_python_interpreter() -> bool { - env_var("PYO3_NO_PYTHON").is_none() -} - -/// Checks if `abi3` or any of the `abi3-py3*` features is enabled for the PyO3 crate. -/// -/// Must be called from a PyO3 crate build script. -fn is_abi3() -> bool { - cargo_env_var("CARGO_FEATURE_ABI3").is_some() - || env_var("PYO3_USE_ABI3_FORWARD_COMPATIBILITY").map_or(false, |os_str| os_str == "1") -} - -/// Gets the minimum supported Python version from PyO3 `abi3-py*` features. -/// -/// Must be called from a PyO3 crate build script. -pub fn get_abi3_version() -> Option { - let minor_version = (MINIMUM_SUPPORTED_VERSION.minor..=ABI3_MAX_MINOR) - .find(|i| cargo_env_var(&format!("CARGO_FEATURE_ABI3_PY3{}", i)).is_some()); - minor_version.map(|minor| PythonVersion { major: 3, minor }) -} - -/// Checks if the `extension-module` feature is enabled for the PyO3 crate. -/// -/// Must be called from a PyO3 crate build script. -pub fn is_extension_module() -> bool { - cargo_env_var("CARGO_FEATURE_EXTENSION_MODULE").is_some() -} - -/// Checks if we need to link to `libpython` for the current build target. -/// -/// Must be called from a PyO3 crate build script. -pub fn is_linking_libpython() -> bool { - is_linking_libpython_for_target(&target_triple_from_env()) -} - -/// Checks if we need to link to `libpython` for the target. -/// -/// Must be called from a PyO3 crate build script. -fn is_linking_libpython_for_target(target: &Triple) -> bool { - target.operating_system == OperatingSystem::Windows - // See https://github.com/PyO3/pyo3/issues/4068#issuecomment-2051159852 - || target.operating_system == OperatingSystem::Aix - || target.environment == Environment::Android - || target.environment == Environment::Androideabi - || !is_extension_module() -} - -/// Checks if we need to discover the Python library directory -/// to link the extension module binary. -/// -/// Must be called from a PyO3 crate build script. -fn require_libdir_for_target(target: &Triple) -> bool { - let is_generating_libpython = cfg!(feature = "python3-dll-a") - && target.operating_system == OperatingSystem::Windows - && is_abi3(); - - is_linking_libpython_for_target(target) && !is_generating_libpython -} - -/// Configuration needed by PyO3 to cross-compile for a target platform. -/// -/// Usually this is collected from the environment (i.e. `PYO3_CROSS_*` and `CARGO_CFG_TARGET_*`) -/// when a cross-compilation configuration is detected. -#[derive(Debug, PartialEq, Eq)] -pub struct CrossCompileConfig { - /// The directory containing the Python library to link against. - pub lib_dir: Option, - - /// The version of the Python library to link against. - version: Option, - - /// The target Python implementation hint (CPython, PyPy, GraalPy, ...) - implementation: Option, - - /// The compile target triple (e.g. aarch64-unknown-linux-gnu) - target: Triple, - - /// Python ABI flags, used to detect free-threaded Python builds. - abiflags: Option, -} - -impl CrossCompileConfig { - /// Creates a new cross compile config struct from PyO3 environment variables - /// and the build environment when cross compilation mode is detected. - /// - /// Returns `None` when not cross compiling. - fn try_from_env_vars_host_target( - env_vars: CrossCompileEnvVars, - host: &Triple, - target: &Triple, - ) -> Result> { - if env_vars.any() || Self::is_cross_compiling_from_to(host, target) { - let lib_dir = env_vars.lib_dir_path()?; - let (version, abiflags) = env_vars.parse_version()?; - let implementation = env_vars.parse_implementation()?; - let target = target.clone(); - - Ok(Some(CrossCompileConfig { - lib_dir, - version, - implementation, - target, - abiflags, - })) - } else { - Ok(None) - } - } - - /// Checks if compiling on `host` for `target` required "real" cross compilation. - /// - /// Returns `false` if the target Python interpreter can run on the host. - fn is_cross_compiling_from_to(host: &Triple, target: &Triple) -> bool { - // Not cross-compiling if arch-vendor-os is all the same - // e.g. x86_64-unknown-linux-musl on x86_64-unknown-linux-gnu host - // x86_64-pc-windows-gnu on x86_64-pc-windows-msvc host - let mut compatible = host.architecture == target.architecture - && host.vendor == target.vendor - && host.operating_system == target.operating_system; - - // Not cross-compiling to compile for 32-bit Python from windows 64-bit - compatible |= target.operating_system == OperatingSystem::Windows - && host.operating_system == OperatingSystem::Windows - && matches!(target.architecture, Architecture::X86_32(_)) - && host.architecture == Architecture::X86_64; - - // Not cross-compiling to compile for x86-64 Python from macOS arm64 and vice versa - compatible |= target.operating_system == OperatingSystem::Darwin - && host.operating_system == OperatingSystem::Darwin; - - !compatible - } - - /// Converts `lib_dir` member field to an UTF-8 string. - /// - /// The conversion can not fail because `PYO3_CROSS_LIB_DIR` variable - /// is ensured contain a valid UTF-8 string. - fn lib_dir_string(&self) -> Option { - self.lib_dir - .as_ref() - .map(|s| s.to_str().unwrap().to_owned()) - } -} - -/// PyO3-specific cross compile environment variable values -struct CrossCompileEnvVars { - /// `PYO3_CROSS` - pyo3_cross: Option, - /// `PYO3_CROSS_LIB_DIR` - pyo3_cross_lib_dir: Option, - /// `PYO3_CROSS_PYTHON_VERSION` - pyo3_cross_python_version: Option, - /// `PYO3_CROSS_PYTHON_IMPLEMENTATION` - pyo3_cross_python_implementation: Option, -} - -impl CrossCompileEnvVars { - /// Grabs the PyO3 cross-compile variables from the environment. - /// - /// Registers the build script to rerun if any of the variables changes. - fn from_env() -> Self { - CrossCompileEnvVars { - pyo3_cross: env_var("PYO3_CROSS"), - pyo3_cross_lib_dir: env_var("PYO3_CROSS_LIB_DIR"), - pyo3_cross_python_version: env_var("PYO3_CROSS_PYTHON_VERSION"), - pyo3_cross_python_implementation: env_var("PYO3_CROSS_PYTHON_IMPLEMENTATION"), - } - } - - /// Checks if any of the variables is set. - fn any(&self) -> bool { - self.pyo3_cross.is_some() - || self.pyo3_cross_lib_dir.is_some() - || self.pyo3_cross_python_version.is_some() - || self.pyo3_cross_python_implementation.is_some() - } - - /// Parses `PYO3_CROSS_PYTHON_VERSION` environment variable value - /// into `PythonVersion` and ABI flags. - fn parse_version(&self) -> Result<(Option, Option)> { - match self.pyo3_cross_python_version.as_ref() { - Some(os_string) => { - let utf8_str = os_string - .to_str() - .ok_or("PYO3_CROSS_PYTHON_VERSION is not valid a UTF-8 string")?; - let (utf8_str, abiflags) = if let Some(version) = utf8_str.strip_suffix('t') { - (version, Some("t".to_string())) - } else { - (utf8_str, None) - }; - let version = utf8_str - .parse() - .context("failed to parse PYO3_CROSS_PYTHON_VERSION")?; - Ok((Some(version), abiflags)) - } - None => Ok((None, None)), - } - } - - /// Parses `PYO3_CROSS_PYTHON_IMPLEMENTATION` environment variable value - /// into `PythonImplementation`. - fn parse_implementation(&self) -> Result> { - let implementation = self - .pyo3_cross_python_implementation - .as_ref() - .map(|os_string| { - let utf8_str = os_string - .to_str() - .ok_or("PYO3_CROSS_PYTHON_IMPLEMENTATION is not valid a UTF-8 string")?; - utf8_str - .parse() - .context("failed to parse PYO3_CROSS_PYTHON_IMPLEMENTATION") - }) - .transpose()?; - - Ok(implementation) - } - - /// Converts the stored `PYO3_CROSS_LIB_DIR` variable value (if any) - /// into a `PathBuf` instance. - /// - /// Ensures that the path is a valid UTF-8 string. - fn lib_dir_path(&self) -> Result> { - let lib_dir = self.pyo3_cross_lib_dir.as_ref().map(PathBuf::from); - - if let Some(dir) = lib_dir.as_ref() { - ensure!( - dir.to_str().is_some(), - "PYO3_CROSS_LIB_DIR variable value is not a valid UTF-8 string" - ); - } - - Ok(lib_dir) - } -} - -/// Detect whether we are cross compiling and return an assembled CrossCompileConfig if so. -/// -/// This function relies on PyO3 cross-compiling environment variables: -/// -/// * `PYO3_CROSS`: If present, forces PyO3 to configure as a cross-compilation. -/// * `PYO3_CROSS_LIB_DIR`: If present, must be set to the directory containing -/// the target's libpython DSO and the associated `_sysconfigdata*.py` file for -/// Unix-like targets, or the Python DLL import libraries for the Windows target. -/// * `PYO3_CROSS_PYTHON_VERSION`: Major and minor version (e.g. 3.9) of the target Python -/// installation. This variable is only needed if PyO3 cannnot determine the version to target -/// from `abi3-py3*` features, or if there are multiple versions of Python present in -/// `PYO3_CROSS_LIB_DIR`. -/// -/// See the [PyO3 User Guide](https://pyo3.rs/) for more info on cross-compiling. -pub fn cross_compiling_from_to( - host: &Triple, - target: &Triple, -) -> Result> { - let env_vars = CrossCompileEnvVars::from_env(); - CrossCompileConfig::try_from_env_vars_host_target(env_vars, host, target) -} - -/// Detect whether we are cross compiling from Cargo and `PYO3_CROSS_*` environment -/// variables and return an assembled `CrossCompileConfig` if so. -/// -/// This must be called from PyO3's build script, because it relies on environment -/// variables such as `CARGO_CFG_TARGET_OS` which aren't available at any other time. -pub fn cross_compiling_from_cargo_env() -> Result> { - let env_vars = CrossCompileEnvVars::from_env(); - let host = Triple::host(); - let target = target_triple_from_env(); - - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) -} - -#[allow(non_camel_case_types)] -#[derive(Debug, Clone, Hash, PartialEq, Eq)] -pub enum BuildFlag { - Py_DEBUG, - Py_REF_DEBUG, - Py_TRACE_REFS, - Py_GIL_DISABLED, - COUNT_ALLOCS, - Other(String), -} - -impl Display for BuildFlag { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - BuildFlag::Other(flag) => write!(f, "{}", flag), - _ => write!(f, "{:?}", self), - } - } -} - -impl FromStr for BuildFlag { - type Err = std::convert::Infallible; - fn from_str(s: &str) -> Result { - match s { - "Py_DEBUG" => Ok(BuildFlag::Py_DEBUG), - "Py_REF_DEBUG" => Ok(BuildFlag::Py_REF_DEBUG), - "Py_TRACE_REFS" => Ok(BuildFlag::Py_TRACE_REFS), - "Py_GIL_DISABLED" => Ok(BuildFlag::Py_GIL_DISABLED), - "COUNT_ALLOCS" => Ok(BuildFlag::COUNT_ALLOCS), - other => Ok(BuildFlag::Other(other.to_owned())), - } - } -} - -/// A list of python interpreter compile-time preprocessor defines. -/// -/// PyO3 will pick these up and pass to rustc via `--cfg=py_sys_config={varname}`; -/// this allows using them conditional cfg attributes in the .rs files, so -/// -/// ```rust -/// #[cfg(py_sys_config="{varname}")] -/// # struct Foo; -/// ``` -/// -/// is the equivalent of `#ifdef {varname}` in C. -/// -/// see Misc/SpecialBuilds.txt in the python source for what these mean. -#[cfg_attr(test, derive(Debug, PartialEq, Eq))] -#[derive(Clone, Default)] -pub struct BuildFlags(pub HashSet); - -impl BuildFlags { - const ALL: [BuildFlag; 5] = [ - BuildFlag::Py_DEBUG, - BuildFlag::Py_REF_DEBUG, - BuildFlag::Py_TRACE_REFS, - BuildFlag::Py_GIL_DISABLED, - BuildFlag::COUNT_ALLOCS, - ]; - - pub fn new() -> Self { - BuildFlags(HashSet::new()) - } - - fn from_sysconfigdata(config_map: &Sysconfigdata) -> Self { - Self( - BuildFlags::ALL - .iter() - .filter(|flag| config_map.get_value(flag.to_string()) == Some("1")) - .cloned() - .collect(), - ) - .fixup() - } - - /// Examine python's compile flags to pass to cfg by launching - /// the interpreter and printing variables of interest from - /// sysconfig.get_config_vars. - fn from_interpreter(interpreter: impl AsRef) -> Result { - // sysconfig is missing all the flags on windows for Python 3.12 and - // older, so we can't actually query the interpreter directly for its - // build flags on those versions. - if cfg!(windows) { - let script = String::from("import sys;print(sys.version_info < (3, 13))"); - let stdout = run_python_script(interpreter.as_ref(), &script)?; - if stdout.trim_end() == "True" { - return Ok(Self::new()); - } - } - - let mut script = String::from("import sysconfig\n"); - script.push_str("config = sysconfig.get_config_vars()\n"); - - for k in &BuildFlags::ALL { - use std::fmt::Write; - writeln!(&mut script, "print(config.get('{}', '0'))", k).unwrap(); - } - - let stdout = run_python_script(interpreter.as_ref(), &script)?; - let split_stdout: Vec<&str> = stdout.trim_end().lines().collect(); - ensure!( - split_stdout.len() == BuildFlags::ALL.len(), - "Python stdout len didn't return expected number of lines: {}", - split_stdout.len() - ); - let flags = BuildFlags::ALL - .iter() - .zip(split_stdout) - .filter(|(_, flag_value)| *flag_value == "1") - .map(|(flag, _)| flag.clone()) - .collect(); - - Ok(Self(flags).fixup()) - } - - fn fixup(mut self) -> Self { - if self.0.contains(&BuildFlag::Py_DEBUG) { - self.0.insert(BuildFlag::Py_REF_DEBUG); - } - - self - } -} - -impl Display for BuildFlags { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut first = true; - for flag in &self.0 { - if first { - first = false; - } else { - write!(f, ",")?; - } - write!(f, "{}", flag)?; - } - Ok(()) - } -} - -impl FromStr for BuildFlags { - type Err = std::convert::Infallible; - - fn from_str(value: &str) -> Result { - let mut flags = HashSet::new(); - for flag in value.split_terminator(',') { - flags.insert(flag.parse().unwrap()); - } - Ok(BuildFlags(flags)) - } -} - -fn parse_script_output(output: &str) -> HashMap { - output - .lines() - .filter_map(|line| { - let mut i = line.splitn(2, ' '); - Some((i.next()?.into(), i.next()?.into())) - }) - .collect() -} - -/// Parsed data from Python sysconfigdata file -/// -/// A hash map of all values from a sysconfigdata file. -pub struct Sysconfigdata(HashMap); - -impl Sysconfigdata { - pub fn get_value>(&self, k: S) -> Option<&str> { - self.0.get(k.as_ref()).map(String::as_str) - } - - #[allow(dead_code)] - fn new() -> Self { - Sysconfigdata(HashMap::new()) - } - - #[allow(dead_code)] - fn insert>(&mut self, k: S, v: S) { - self.0.insert(k.into(), v.into()); - } -} - -/// Parse sysconfigdata file -/// -/// The sysconfigdata is simply a dictionary containing all the build time variables used for the -/// python executable and library. This function necessitates a python interpreter on the host -/// machine to work. Here it is read into a `Sysconfigdata` (hash map), which can be turned into an -/// [`InterpreterConfig`] using -/// [`from_sysconfigdata`](InterpreterConfig::from_sysconfigdata). -pub fn parse_sysconfigdata(sysconfigdata_path: impl AsRef) -> Result { - let sysconfigdata_path = sysconfigdata_path.as_ref(); - let mut script = fs::read_to_string(sysconfigdata_path).with_context(|| { - format!( - "failed to read config from {}", - sysconfigdata_path.display() - ) - })?; - script += r#" -for key, val in build_time_vars.items(): - print(key, val) -"#; - - let output = run_python_script(&find_interpreter()?, &script)?; - - Ok(Sysconfigdata(parse_script_output(&output))) -} - -fn starts_with(entry: &DirEntry, pat: &str) -> bool { - let name = entry.file_name(); - name.to_string_lossy().starts_with(pat) -} -fn ends_with(entry: &DirEntry, pat: &str) -> bool { - let name = entry.file_name(); - name.to_string_lossy().ends_with(pat) -} - -/// Finds the sysconfigdata file when the target Python library directory is set. -/// -/// Returns `None` if the library directory is not available, and a runtime error -/// when no or multiple sysconfigdata files are found. -fn find_sysconfigdata(cross: &CrossCompileConfig) -> Result> { - let mut sysconfig_paths = find_all_sysconfigdata(cross)?; - if sysconfig_paths.is_empty() { - if let Some(lib_dir) = cross.lib_dir.as_ref() { - bail!("Could not find _sysconfigdata*.py in {}", lib_dir.display()); - } else { - // Continue with the default configuration when PYO3_CROSS_LIB_DIR is not set. - return Ok(None); - } - } else if sysconfig_paths.len() > 1 { - let mut error_msg = String::from( - "Detected multiple possible Python versions. Please set either the \ - PYO3_CROSS_PYTHON_VERSION variable to the wanted version or the \ - _PYTHON_SYSCONFIGDATA_NAME variable to the wanted sysconfigdata file name.\n\n\ - sysconfigdata files found:", - ); - for path in sysconfig_paths { - use std::fmt::Write; - write!(&mut error_msg, "\n\t{}", path.display()).unwrap(); - } - bail!("{}\n", error_msg); - } - - Ok(Some(sysconfig_paths.remove(0))) -} - -/// Finds `_sysconfigdata*.py` files for detected Python interpreters. -/// -/// From the python source for `_sysconfigdata*.py` is always going to be located at -/// `build/lib.{PLATFORM}-{PY_MINOR_VERSION}` when built from source. The [exact line][1] is defined as: -/// -/// ```py -/// pybuilddir = 'build/lib.%s-%s' % (get_platform(), sys.version_info[:2]) -/// ``` -/// -/// Where get_platform returns a kebab-case formatted string containing the os, the architecture and -/// possibly the os' kernel version (not the case on linux). However, when installed using a package -/// manager, the `_sysconfigdata*.py` file is installed in the `${PREFIX}/lib/python3.Y/` directory. -/// The `_sysconfigdata*.py` is generally in a sub-directory of the location of `libpython3.Y.so`. -/// So we must find the file in the following possible locations: -/// -/// ```sh -/// # distribution from package manager, (lib_dir may or may not include lib/) -/// ${INSTALL_PREFIX}/lib/python3.Y/_sysconfigdata*.py -/// ${INSTALL_PREFIX}/lib/libpython3.Y.so -/// ${INSTALL_PREFIX}/lib/python3.Y/config-3.Y-${HOST_TRIPLE}/libpython3.Y.so -/// -/// # Built from source from host -/// ${CROSS_COMPILED_LOCATION}/build/lib.linux-x86_64-Y/_sysconfigdata*.py -/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so -/// -/// # if cross compiled, kernel release is only present on certain OS targets. -/// ${CROSS_COMPILED_LOCATION}/build/lib.{OS}(-{OS-KERNEL-RELEASE})?-{ARCH}-Y/_sysconfigdata*.py -/// ${CROSS_COMPILED_LOCATION}/libpython3.Y.so -/// -/// # PyPy includes a similar file since v73 -/// ${INSTALL_PREFIX}/lib/pypy3.Y/_sysconfigdata.py -/// ${INSTALL_PREFIX}/lib_pypy/_sysconfigdata.py -/// ``` -/// -/// [1]: https://github.com/python/cpython/blob/3.5/Lib/sysconfig.py#L389 -/// -/// Returns an empty vector when the target Python library directory -/// is not set via `PYO3_CROSS_LIB_DIR`. -pub fn find_all_sysconfigdata(cross: &CrossCompileConfig) -> Result> { - let sysconfig_paths = if let Some(lib_dir) = cross.lib_dir.as_ref() { - search_lib_dir(lib_dir, cross).with_context(|| { - format!( - "failed to search the lib dir at 'PYO3_CROSS_LIB_DIR={}'", - lib_dir.display() - ) - })? - } else { - return Ok(Vec::new()); - }; - - let sysconfig_name = env_var("_PYTHON_SYSCONFIGDATA_NAME"); - let mut sysconfig_paths = sysconfig_paths - .iter() - .filter_map(|p| { - let canonical = fs::canonicalize(p).ok(); - match &sysconfig_name { - Some(_) => canonical.filter(|p| p.file_stem() == sysconfig_name.as_deref()), - None => canonical, - } - }) - .collect::>(); - - sysconfig_paths.sort(); - sysconfig_paths.dedup(); - - Ok(sysconfig_paths) -} - -fn is_pypy_lib_dir(path: &str, v: &Option) -> bool { - let pypy_version_pat = if let Some(v) = v { - format!("pypy{}", v) - } else { - "pypy3.".into() - }; - path == "lib_pypy" || path.starts_with(&pypy_version_pat) -} - -fn is_graalpy_lib_dir(path: &str, v: &Option) -> bool { - let graalpy_version_pat = if let Some(v) = v { - format!("graalpy{}", v) - } else { - "graalpy2".into() - }; - path == "lib_graalpython" || path.starts_with(&graalpy_version_pat) -} - -fn is_cpython_lib_dir(path: &str, v: &Option) -> bool { - let cpython_version_pat = if let Some(v) = v { - format!("python{}", v) - } else { - "python3.".into() - }; - path.starts_with(&cpython_version_pat) -} - -/// recursive search for _sysconfigdata, returns all possibilities of sysconfigdata paths -fn search_lib_dir(path: impl AsRef, cross: &CrossCompileConfig) -> Result> { - let mut sysconfig_paths = vec![]; - for f in fs::read_dir(path.as_ref()).with_context(|| { - format!( - "failed to list the entries in '{}'", - path.as_ref().display() - ) - })? { - sysconfig_paths.extend(match &f { - // Python 3.7+ sysconfigdata with platform specifics - Ok(f) if starts_with(f, "_sysconfigdata_") && ends_with(f, "py") => vec![f.path()], - Ok(f) if f.metadata().map_or(false, |metadata| metadata.is_dir()) => { - let file_name = f.file_name(); - let file_name = file_name.to_string_lossy(); - if file_name == "build" || file_name == "lib" { - search_lib_dir(f.path(), cross)? - } else if file_name.starts_with("lib.") { - // check if right target os - if !file_name.contains(&cross.target.operating_system.to_string()) { - continue; - } - // Check if right arch - if !file_name.contains(&cross.target.architecture.to_string()) { - continue; - } - search_lib_dir(f.path(), cross)? - } else if is_cpython_lib_dir(&file_name, &cross.version) - || is_pypy_lib_dir(&file_name, &cross.version) - || is_graalpy_lib_dir(&file_name, &cross.version) - { - search_lib_dir(f.path(), cross)? - } else { - continue; - } - } - _ => continue, - }); - } - // If we got more than one file, only take those that contain the arch name. - // For ubuntu 20.04 with host architecture x86_64 and a foreign architecture of armhf - // this reduces the number of candidates to 1: - // - // $ find /usr/lib/python3.8/ -name '_sysconfigdata*.py' -not -lname '*' - // /usr/lib/python3.8/_sysconfigdata__x86_64-linux-gnu.py - // /usr/lib/python3.8/_sysconfigdata__arm-linux-gnueabihf.py - if sysconfig_paths.len() > 1 { - let temp = sysconfig_paths - .iter() - .filter(|p| { - p.to_string_lossy() - .contains(&cross.target.architecture.to_string()) - }) - .cloned() - .collect::>(); - if !temp.is_empty() { - sysconfig_paths = temp; - } - } - - Ok(sysconfig_paths) -} - -/// Find cross compilation information from sysconfigdata file -/// -/// first find sysconfigdata file which follows the pattern [`_sysconfigdata_{abi}_{platform}_{multiarch}`][1] -/// -/// [1]: https://github.com/python/cpython/blob/3.8/Lib/sysconfig.py#L348 -/// -/// Returns `None` when the target Python library directory is not set. -fn cross_compile_from_sysconfigdata( - cross_compile_config: &CrossCompileConfig, -) -> Result> { - if let Some(path) = find_sysconfigdata(cross_compile_config)? { - let data = parse_sysconfigdata(path)?; - let mut config = InterpreterConfig::from_sysconfigdata(&data)?; - if let Some(cross_lib_dir) = cross_compile_config.lib_dir_string() { - config.lib_dir = Some(cross_lib_dir) - } - - Ok(Some(config)) - } else { - Ok(None) - } -} - -/// Generates "default" cross compilation information for the target. -/// -/// This should work for most CPython extension modules when targeting -/// Windows, macOS and Linux. -/// -/// Must be called from a PyO3 crate build script. -#[allow(unused_mut)] -fn default_cross_compile(cross_compile_config: &CrossCompileConfig) -> Result { - let version = cross_compile_config - .version - .or_else(get_abi3_version) - .ok_or_else(|| - format!( - "PYO3_CROSS_PYTHON_VERSION or an abi3-py3* feature must be specified \ - when cross-compiling and PYO3_CROSS_LIB_DIR is not set.\n\ - = help: see the PyO3 user guide for more information: https://pyo3.rs/v{}/building-and-distribution.html#cross-compiling", - env!("CARGO_PKG_VERSION") - ) - )?; - - let abi3 = is_abi3(); - let implementation = cross_compile_config - .implementation - .unwrap_or(PythonImplementation::CPython); - let gil_disabled = cross_compile_config.abiflags.as_deref() == Some("t"); - - let lib_name = default_lib_name_for_target( - version, - implementation, - abi3, - gil_disabled, - &cross_compile_config.target, - ); - - let mut lib_dir = cross_compile_config.lib_dir_string(); - - // Auto generate python3.dll import libraries for Windows targets. - #[cfg(feature = "python3-dll-a")] - if lib_dir.is_none() { - let py_version = if implementation == PythonImplementation::CPython && abi3 && !gil_disabled - { - None - } else { - Some(version) - }; - lib_dir = self::import_lib::generate_import_lib( - &cross_compile_config.target, - cross_compile_config - .implementation - .unwrap_or(PythonImplementation::CPython), - py_version, - None, - )?; - } - - Ok(InterpreterConfig { - implementation, - version, - shared: true, - abi3, - lib_name, - lib_dir, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }) -} - -/// Generates "default" interpreter configuration when compiling "abi3" extensions -/// without a working Python interpreter. -/// -/// `version` specifies the minimum supported Stable ABI CPython version. -/// -/// This should work for most CPython extension modules when compiling on -/// Windows, macOS and Linux. -/// -/// Must be called from a PyO3 crate build script. -fn default_abi3_config(host: &Triple, version: PythonVersion) -> Result { - // FIXME: PyPy & GraalPy do not support the Stable ABI. - let implementation = PythonImplementation::CPython; - let abi3 = true; - - let lib_name = if host.operating_system == OperatingSystem::Windows { - Some(default_lib_name_windows( - version, - implementation, - abi3, - false, - false, - false, - )?) - } else { - None - }; - - Ok(InterpreterConfig { - implementation, - version, - shared: true, - abi3, - lib_name, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }) -} - -/// Detects the cross compilation target interpreter configuration from all -/// available sources (PyO3 environment variables, Python sysconfigdata, etc.). -/// -/// Returns the "default" target interpreter configuration for Windows and -/// when no target Python interpreter is found. -/// -/// Must be called from a PyO3 crate build script. -fn load_cross_compile_config( - cross_compile_config: CrossCompileConfig, -) -> Result { - let windows = cross_compile_config.target.operating_system == OperatingSystem::Windows; - - let config = if windows || !have_python_interpreter() { - // Load the defaults for Windows even when `PYO3_CROSS_LIB_DIR` is set - // since it has no sysconfigdata files in it. - // Also, do not try to look for sysconfigdata when `PYO3_NO_PYTHON` variable is set. - default_cross_compile(&cross_compile_config)? - } else if let Some(config) = cross_compile_from_sysconfigdata(&cross_compile_config)? { - // Try to find and parse sysconfigdata files on other targets. - config - } else { - // Fall back to the defaults when nothing else can be done. - default_cross_compile(&cross_compile_config)? - }; - - if config.lib_name.is_some() && config.lib_dir.is_none() { - warn!( - "The output binary will link to libpython, \ - but PYO3_CROSS_LIB_DIR environment variable is not set. \ - Ensure that the target Python library directory is \ - in the rustc native library search path." - ); - } - - Ok(config) -} - -// These contains only the limited ABI symbols. -const WINDOWS_ABI3_LIB_NAME: &str = "python3"; -const WINDOWS_ABI3_DEBUG_LIB_NAME: &str = "python3_d"; - -fn default_lib_name_for_target( - version: PythonVersion, - implementation: PythonImplementation, - abi3: bool, - gil_disabled: bool, - target: &Triple, -) -> Option { - if target.operating_system == OperatingSystem::Windows { - Some( - default_lib_name_windows(version, implementation, abi3, false, false, gil_disabled) - .unwrap(), - ) - } else if is_linking_libpython_for_target(target) { - Some(default_lib_name_unix(version, implementation, None, gil_disabled).unwrap()) - } else { - None - } -} - -fn default_lib_name_windows( - version: PythonVersion, - implementation: PythonImplementation, - abi3: bool, - mingw: bool, - debug: bool, - gil_disabled: bool, -) -> Result { - if debug && version < PythonVersion::PY310 { - // CPython bug: linking against python3_d.dll raises error - // https://github.com/python/cpython/issues/101614 - Ok(format!("python{}{}_d", version.major, version.minor)) - } else if abi3 && !(gil_disabled || implementation.is_pypy() || implementation.is_graalpy()) { - if debug { - Ok(WINDOWS_ABI3_DEBUG_LIB_NAME.to_owned()) - } else { - Ok(WINDOWS_ABI3_LIB_NAME.to_owned()) - } - } else if mingw { - ensure!( - !gil_disabled, - "MinGW free-threaded builds are not currently tested or supported" - ); - // https://packages.msys2.org/base/mingw-w64-python - Ok(format!("python{}.{}", version.major, version.minor)) - } else if gil_disabled { - ensure!(version >= PythonVersion::PY313, "Cannot compile C extensions for the free-threaded build on Python versions earlier than 3.13, found {}.{}", version.major, version.minor); - if debug { - Ok(format!("python{}{}t_d", version.major, version.minor)) - } else { - Ok(format!("python{}{}t", version.major, version.minor)) - } - } else if debug { - Ok(format!("python{}{}_d", version.major, version.minor)) - } else { - Ok(format!("python{}{}", version.major, version.minor)) - } -} - -fn default_lib_name_unix( - version: PythonVersion, - implementation: PythonImplementation, - ld_version: Option<&str>, - gil_disabled: bool, -) -> Result { - match implementation { - PythonImplementation::CPython => match ld_version { - Some(ld_version) => Ok(format!("python{}", ld_version)), - None => { - if version > PythonVersion::PY37 { - // PEP 3149 ABI version tags are finally gone - if gil_disabled { - ensure!(version >= PythonVersion::PY313, "Cannot compile C extensions for the free-threaded build on Python versions earlier than 3.13, found {}.{}", version.major, version.minor); - Ok(format!("python{}.{}t", version.major, version.minor)) - } else { - Ok(format!("python{}.{}", version.major, version.minor)) - } - } else { - // Work around https://bugs.python.org/issue36707 - Ok(format!("python{}.{}m", version.major, version.minor)) - } - } - }, - PythonImplementation::PyPy => match ld_version { - Some(ld_version) => Ok(format!("pypy{}-c", ld_version)), - None => Ok(format!("pypy{}.{}-c", version.major, version.minor)), - }, - - PythonImplementation::GraalPy => Ok("python-native".to_string()), - } -} - -/// Run a python script using the specified interpreter binary. -fn run_python_script(interpreter: &Path, script: &str) -> Result { - run_python_script_with_envs(interpreter, script, std::iter::empty::<(&str, &str)>()) -} - -/// Run a python script using the specified interpreter binary with additional environment -/// variables (e.g. PYTHONPATH) set. -fn run_python_script_with_envs(interpreter: &Path, script: &str, envs: I) -> Result -where - I: IntoIterator, - K: AsRef, - V: AsRef, -{ - let out = Command::new(interpreter) - .env("PYTHONIOENCODING", "utf-8") - .envs(envs) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::inherit()) - .spawn() - .and_then(|mut child| { - child - .stdin - .as_mut() - .expect("piped stdin") - .write_all(script.as_bytes())?; - child.wait_with_output() - }); - - match out { - Err(err) => bail!( - "failed to run the Python interpreter at {}: {}", - interpreter.display(), - err - ), - Ok(ok) if !ok.status.success() => bail!("Python script failed"), - Ok(ok) => Ok(String::from_utf8(ok.stdout) - .context("failed to parse Python script output as utf-8")?), - } -} - -fn venv_interpreter(virtual_env: &OsStr, windows: bool) -> PathBuf { - if windows { - Path::new(virtual_env).join("Scripts").join("python.exe") - } else { - Path::new(virtual_env).join("bin").join("python") - } -} - -fn conda_env_interpreter(conda_prefix: &OsStr, windows: bool) -> PathBuf { - if windows { - Path::new(conda_prefix).join("python.exe") - } else { - Path::new(conda_prefix).join("bin").join("python") - } -} - -fn get_env_interpreter() -> Option { - match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) { - // Use cfg rather than CARGO_CFG_TARGET_OS because this affects where files are located on the - // build host - (Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))), - (None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))), - (Some(_), Some(_)) => { - warn!( - "Both VIRTUAL_ENV and CONDA_PREFIX are set. PyO3 will ignore both of these for \ - locating the Python interpreter until you unset one of them." - ); - None - } - (None, None) => None, - } -} - -/// Attempts to locate a python interpreter. -/// -/// Locations are checked in the order listed: -/// 1. If `PYO3_PYTHON` is set, this interpreter is used. -/// 2. If in a virtualenv, that environment's interpreter is used. -/// 3. `python`, if this is functional a Python 3.x interpreter -/// 4. `python3`, as above -pub fn find_interpreter() -> Result { - // Trigger rebuilds when `PYO3_ENVIRONMENT_SIGNATURE` env var value changes - // See https://github.com/PyO3/pyo3/issues/2724 - println!("cargo:rerun-if-env-changed=PYO3_ENVIRONMENT_SIGNATURE"); - - if let Some(exe) = env_var("PYO3_PYTHON") { - Ok(exe.into()) - } else if let Some(env_interpreter) = get_env_interpreter() { - Ok(env_interpreter) - } else { - println!("cargo:rerun-if-env-changed=PATH"); - ["python", "python3"] - .iter() - .find(|bin| { - if let Ok(out) = Command::new(bin).arg("--version").output() { - // begin with `Python 3.X.X :: additional info` - out.stdout.starts_with(b"Python 3") - || out.stderr.starts_with(b"Python 3") - || out.stdout.starts_with(b"GraalPy 3") - } else { - false - } - }) - .map(PathBuf::from) - .ok_or_else(|| "no Python 3.x interpreter found".into()) - } -} - -/// Locates and extracts the build host Python interpreter configuration. -/// -/// Lowers the configured Python version to `abi3_version` if required. -fn get_host_interpreter(abi3_version: Option) -> Result { - let interpreter_path = find_interpreter()?; - - let mut interpreter_config = InterpreterConfig::from_interpreter(interpreter_path)?; - interpreter_config.fixup_for_abi3_version(abi3_version)?; - - Ok(interpreter_config) -} - -/// Generates an interpreter config suitable for cross-compilation. -/// -/// This must be called from PyO3's build script, because it relies on environment variables such as -/// CARGO_CFG_TARGET_OS which aren't available at any other time. -pub fn make_cross_compile_config() -> Result> { - let interpreter_config = if let Some(cross_config) = cross_compiling_from_cargo_env()? { - let mut interpreter_config = load_cross_compile_config(cross_config)?; - interpreter_config.fixup_for_abi3_version(get_abi3_version())?; - Some(interpreter_config) - } else { - None - }; - - Ok(interpreter_config) -} - -/// Generates an interpreter config which will be hard-coded into the pyo3-build-config crate. -/// Only used by `pyo3-build-config` build script. -#[allow(dead_code, unused_mut)] -pub fn make_interpreter_config() -> Result { - let host = Triple::host(); - let abi3_version = get_abi3_version(); - - // See if we can safely skip the Python interpreter configuration detection. - // Unix "abi3" extension modules can usually be built without any interpreter. - let need_interpreter = abi3_version.is_none() || require_libdir_for_target(&host); - - if have_python_interpreter() { - match get_host_interpreter(abi3_version) { - Ok(interpreter_config) => return Ok(interpreter_config), - // Bail if the interpreter configuration is required to build. - Err(e) if need_interpreter => return Err(e), - _ => { - // Fall back to the "abi3" defaults just as if `PYO3_NO_PYTHON` - // environment variable was set. - warn!("Compiling without a working Python interpreter."); - } - } - } else { - ensure!( - abi3_version.is_some(), - "An abi3-py3* feature must be specified when compiling without a Python interpreter." - ); - }; - - let mut interpreter_config = default_abi3_config(&host, abi3_version.unwrap())?; - - // Auto generate python3.dll import libraries for Windows targets. - #[cfg(feature = "python3-dll-a")] - { - let gil_disabled = interpreter_config - .build_flags - .0 - .contains(&BuildFlag::Py_GIL_DISABLED); - let py_version = if interpreter_config.implementation == PythonImplementation::CPython - && interpreter_config.abi3 - && !gil_disabled - { - None - } else { - Some(interpreter_config.version) - }; - interpreter_config.lib_dir = self::import_lib::generate_import_lib( - &host, - interpreter_config.implementation, - py_version, - None, - )?; - } - - Ok(interpreter_config) -} - -fn escape(bytes: &[u8]) -> String { - let mut escaped = String::with_capacity(2 * bytes.len()); - - for byte in bytes { - const LUT: &[u8; 16] = b"0123456789abcdef"; - - escaped.push(LUT[(byte >> 4) as usize] as char); - escaped.push(LUT[(byte & 0x0F) as usize] as char); - } - - escaped -} - -fn unescape(escaped: &str) -> Vec { - assert!(escaped.len() % 2 == 0, "invalid hex encoding"); - - let mut bytes = Vec::with_capacity(escaped.len() / 2); - - for chunk in escaped.as_bytes().chunks_exact(2) { - fn unhex(hex: u8) -> u8 { - match hex { - b'a'..=b'f' => hex - b'a' + 10, - b'0'..=b'9' => hex - b'0', - _ => panic!("invalid hex encoding"), - } - } - - bytes.push((unhex(chunk[0]) << 4) | unhex(chunk[1])); - } - - bytes -} - -#[cfg(test)] -mod tests { - use target_lexicon::triple; - - use super::*; - - #[test] - fn test_config_file_roundtrip() { - let config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: Some(32), - executable: Some("executable".into()), - implementation: PythonImplementation::CPython, - lib_name: Some("lib_name".into()), - lib_dir: Some("lib_dir".into()), - shared: true, - version: MINIMUM_SUPPORTED_VERSION, - suppress_build_script_link_lines: true, - extra_build_script_lines: vec!["cargo:test1".to_string(), "cargo:test2".to_string()], - python_framework_prefix: None, - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - - // And some different options, for variety - - let config = InterpreterConfig { - abi3: false, - build_flags: { - let mut flags = HashSet::new(); - flags.insert(BuildFlag::Py_DEBUG); - flags.insert(BuildFlag::Other(String::from("Py_SOME_FLAG"))); - BuildFlags(flags) - }, - pointer_width: None, - executable: None, - implementation: PythonImplementation::PyPy, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { - major: 3, - minor: 10, - }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - } - - #[test] - fn test_config_file_roundtrip_with_escaping() { - let config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: Some(32), - executable: Some("executable".into()), - implementation: PythonImplementation::CPython, - lib_name: Some("lib_name".into()), - lib_dir: Some("lib_dir\\n".into()), - shared: true, - version: MINIMUM_SUPPORTED_VERSION, - suppress_build_script_link_lines: true, - extra_build_script_lines: vec!["cargo:test1".to_string(), "cargo:test2".to_string()], - python_framework_prefix: None, - }; - let mut buf: Vec = Vec::new(); - config.to_writer(&mut buf).unwrap(); - - let buf = unescape(&escape(&buf)); - - assert_eq!(config, InterpreterConfig::from_reader(&*buf).unwrap()); - } - - #[test] - fn test_config_file_defaults() { - // Only version is required - assert_eq!( - InterpreterConfig::from_reader("version=3.7".as_bytes()).unwrap(), - InterpreterConfig { - version: PythonVersion { major: 3, minor: 7 }, - implementation: PythonImplementation::CPython, - shared: true, - abi3: false, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ) - } - - #[test] - fn test_config_file_unknown_keys() { - // ext_suffix is unknown to pyo3-build-config, but it shouldn't error - assert_eq!( - InterpreterConfig::from_reader("version=3.7\next_suffix=.python37.so".as_bytes()) - .unwrap(), - InterpreterConfig { - version: PythonVersion { major: 3, minor: 7 }, - implementation: PythonImplementation::CPython, - shared: true, - abi3: false, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ) - } - - #[test] - fn build_flags_default() { - assert_eq!(BuildFlags::default(), BuildFlags::new()); - } - - #[test] - fn build_flags_from_sysconfigdata() { - let mut sysconfigdata = Sysconfigdata::new(); - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - HashSet::new() - ); - - for flag in &BuildFlags::ALL { - sysconfigdata.insert(flag.to_string(), "0".into()); - } - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - HashSet::new() - ); - - let mut expected_flags = HashSet::new(); - for flag in &BuildFlags::ALL { - sysconfigdata.insert(flag.to_string(), "1".into()); - expected_flags.insert(flag.clone()); - } - - assert_eq!( - BuildFlags::from_sysconfigdata(&sysconfigdata).0, - expected_flags - ); - } - - #[test] - fn build_flags_fixup() { - let mut build_flags = BuildFlags::new(); - - build_flags = build_flags.fixup(); - assert!(build_flags.0.is_empty()); - - build_flags.0.insert(BuildFlag::Py_DEBUG); - - build_flags = build_flags.fixup(); - - // Py_DEBUG implies Py_REF_DEBUG - assert!(build_flags.0.contains(&BuildFlag::Py_REF_DEBUG)); - } - - #[test] - fn parse_script_output() { - let output = "foo bar\nbar foobar\n\n"; - let map = super::parse_script_output(output); - assert_eq!(map.len(), 2); - assert_eq!(map["foo"], "bar"); - assert_eq!(map["bar"], "foobar"); - } - - #[test] - fn config_from_interpreter() { - // Smoke test to just see whether this works - // - // PyO3's CI is dependent on Python being installed, so this should be reliable. - assert!(make_interpreter_config().is_ok()) - } - - #[test] - fn config_from_empty_sysconfigdata() { - let sysconfigdata = Sysconfigdata::new(); - assert!(InterpreterConfig::from_sysconfigdata(&sysconfigdata).is_err()); - } - - #[test] - fn config_from_sysconfigdata() { - let mut sysconfigdata = Sysconfigdata::new(); - // these are the minimal values required such that InterpreterConfig::from_sysconfigdata - // does not error - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - sysconfigdata.insert("Py_ENABLE_SHARED", "1"); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: true, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn config_from_sysconfigdata_framework() { - let mut sysconfigdata = Sysconfigdata::new(); - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - // PYTHONFRAMEWORK should override Py_ENABLE_SHARED - sysconfigdata.insert("Py_ENABLE_SHARED", "0"); - sysconfigdata.insert("PYTHONFRAMEWORK", "Python"); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: true, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - - sysconfigdata = Sysconfigdata::new(); - sysconfigdata.insert("SOABI", "cpython-37m-x86_64-linux-gnu"); - sysconfigdata.insert("VERSION", "3.7"); - // An empty PYTHONFRAMEWORK means it is not a framework - sysconfigdata.insert("Py_ENABLE_SHARED", "0"); - sysconfigdata.insert("PYTHONFRAMEWORK", ""); - sysconfigdata.insert("LIBDIR", "/usr/lib"); - sysconfigdata.insert("LDVERSION", "3.7m"); - sysconfigdata.insert("SIZEOF_VOID_P", "8"); - assert_eq!( - InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(), - InterpreterConfig { - abi3: false, - build_flags: BuildFlags::from_sysconfigdata(&sysconfigdata), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: Some("/usr/lib".into()), - lib_name: Some("python3.7m".into()), - shared: false, - version: PythonVersion::PY37, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn windows_hardcoded_abi3_compile() { - let host = triple!("x86_64-pc-windows-msvc"); - let min_version = "3.7".parse().unwrap(); - - assert_eq!( - default_abi3_config(&host, min_version).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: true, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn unix_hardcoded_abi3_compile() { - let host = triple!("x86_64-unknown-linux-gnu"); - let min_version = "3.9".parse().unwrap(); - - assert_eq!( - default_abi3_config(&host, min_version).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: true, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn windows_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("C:\\some\\path".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.7".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("i686-pc-windows-msvc"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: false, - lib_name: Some("python37".into()), - lib_dir: Some("C:\\some\\path".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn mingw_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("/usr/lib/mingw".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.8".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("i686-pc-windows-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 8 }, - shared: true, - abi3: false, - lib_name: Some("python38".into()), - lib_dir: Some("/usr/lib/mingw".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn unix_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: Some("/usr/arm64/lib".into()), - pyo3_cross_python_implementation: None, - pyo3_cross_python_version: Some("3.9".into()), - }; - - let host = triple!("x86_64-unknown-linux-gnu"); - let target = triple!("aarch64-unknown-linux-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &host, &target) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: false, - lib_name: Some("python3.9".into()), - lib_dir: Some("/usr/arm64/lib".into()), - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn pypy_hardcoded_cross_compile() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_implementation: Some("PyPy".into()), - pyo3_cross_python_version: Some("3.10".into()), - }; - - let triple = triple!("x86_64-unknown-linux-gnu"); - let cross_config = - CrossCompileConfig::try_from_env_vars_host_target(env_vars, &triple, &triple) - .unwrap() - .unwrap(); - - assert_eq!( - default_cross_compile(&cross_config).unwrap(), - InterpreterConfig { - implementation: PythonImplementation::PyPy, - version: PythonVersion { - major: 3, - minor: 10 - }, - shared: true, - abi3: false, - lib_name: Some("pypy3.10-c".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ); - } - - #[test] - fn default_lib_name_windows() { - use PythonImplementation::*; - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - false, - false, - false, - ) - .unwrap(), - "python39", - ); - assert!(super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - false, - false, - true, - ) - .is_err()); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - false, - false, - false, - ) - .unwrap(), - "python3", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - true, - false, - false, - ) - .unwrap(), - "python3.9", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - true, - false, - false, - ) - .unwrap(), - "python3", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - PyPy, - true, - false, - false, - false, - ) - .unwrap(), - "python39", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - false, - false, - true, - false, - ) - .unwrap(), - "python39_d", - ); - // abi3 debug builds on windows use version-specific lib on 3.9 and older - // to workaround https://github.com/python/cpython/issues/101614 - assert_eq!( - super::default_lib_name_windows( - PythonVersion { major: 3, minor: 9 }, - CPython, - true, - false, - true, - false, - ) - .unwrap(), - "python39_d", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 10 - }, - CPython, - true, - false, - true, - false, - ) - .unwrap(), - "python3_d", - ); - // Python versions older than 3.13 don't support gil_disabled - assert!(super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 12, - }, - CPython, - false, - false, - false, - true, - ) - .is_err()); - // mingw and free-threading are incompatible (until someone adds support) - assert!(super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 12, - }, - CPython, - false, - true, - false, - true, - ) - .is_err()); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 13 - }, - CPython, - false, - false, - false, - true, - ) - .unwrap(), - "python313t", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 13 - }, - CPython, - true, // abi3 true should not affect the free-threaded lib name - false, - false, - true, - ) - .unwrap(), - "python313t", - ); - assert_eq!( - super::default_lib_name_windows( - PythonVersion { - major: 3, - minor: 13 - }, - CPython, - false, - false, - true, - true, - ) - .unwrap(), - "python313t_d", - ); - } - - #[test] - fn default_lib_name_unix() { - use PythonImplementation::*; - // Defaults to python3.7m for CPython 3.7 - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 7 }, - CPython, - None, - false - ) - .unwrap(), - "python3.7m", - ); - // Defaults to pythonX.Y for CPython 3.8+ - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 8 }, - CPython, - None, - false - ) - .unwrap(), - "python3.8", - ); - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 9 }, - CPython, - None, - false - ) - .unwrap(), - "python3.9", - ); - // Can use ldversion to override for CPython - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 9 }, - CPython, - Some("3.7md"), - false - ) - .unwrap(), - "python3.7md", - ); - - // PyPy 3.9 includes ldversion - assert_eq!( - super::default_lib_name_unix(PythonVersion { major: 3, minor: 9 }, PyPy, None, false) - .unwrap(), - "pypy3.9-c", - ); - - assert_eq!( - super::default_lib_name_unix( - PythonVersion { major: 3, minor: 9 }, - PyPy, - Some("3.9d"), - false - ) - .unwrap(), - "pypy3.9d-c", - ); - - // free-threading adds a t suffix - assert_eq!( - super::default_lib_name_unix( - PythonVersion { - major: 3, - minor: 13 - }, - CPython, - None, - true - ) - .unwrap(), - "python3.13t", - ); - // 3.12 and older are incompatible with gil_disabled - assert!(super::default_lib_name_unix( - PythonVersion { - major: 3, - minor: 12, - }, - CPython, - None, - true, - ) - .is_err()); - } - - #[test] - fn parse_cross_python_version() { - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: Some("3.9".into()), - pyo3_cross_python_implementation: None, - }; - - assert_eq!( - env_vars.parse_version().unwrap(), - (Some(PythonVersion { major: 3, minor: 9 }), None), - ); - - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: None, - pyo3_cross_python_implementation: None, - }; - - assert_eq!(env_vars.parse_version().unwrap(), (None, None)); - - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: Some("3.13t".into()), - pyo3_cross_python_implementation: None, - }; - - assert_eq!( - env_vars.parse_version().unwrap(), - ( - Some(PythonVersion { - major: 3, - minor: 13 - }), - Some("t".into()) - ), - ); - - let env_vars = CrossCompileEnvVars { - pyo3_cross: None, - pyo3_cross_lib_dir: None, - pyo3_cross_python_version: Some("100".into()), - pyo3_cross_python_implementation: None, - }; - - assert!(env_vars.parse_version().is_err()); - } - - #[test] - fn interpreter_version_reduced_to_abi3() { - let mut config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::default(), - pointer_width: None, - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { major: 3, minor: 7 }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - - config - .fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 7 })) - .unwrap(); - assert_eq!(config.version, PythonVersion { major: 3, minor: 7 }); - } - - #[test] - fn abi3_version_cannot_be_higher_than_interpreter() { - let mut config = InterpreterConfig { - abi3: true, - build_flags: BuildFlags::new(), - pointer_width: None, - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: None, - lib_name: None, - shared: true, - version: PythonVersion { major: 3, minor: 7 }, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - - assert!(config - .fixup_for_abi3_version(Some(PythonVersion { major: 3, minor: 8 })) - .unwrap_err() - .to_string() - .contains( - "cannot set a minimum Python version 3.8 higher than the interpreter version 3.7" - )); - } - - #[test] - #[cfg(all( - target_os = "linux", - target_arch = "x86_64", - feature = "resolve-config" - ))] - fn parse_sysconfigdata() { - // A best effort attempt to get test coverage for the sysconfigdata parsing. - // Might not complete successfully depending on host installation; that's ok as long as - // CI demonstrates this path is covered! - - let interpreter_config = crate::get(); - - let lib_dir = match &interpreter_config.lib_dir { - Some(lib_dir) => Path::new(lib_dir), - // Don't know where to search for sysconfigdata; never mind. - None => return, - }; - - let cross = CrossCompileConfig { - lib_dir: Some(lib_dir.into()), - version: Some(interpreter_config.version), - implementation: Some(interpreter_config.implementation), - target: triple!("x86_64-unknown-linux-gnu"), - abiflags: if interpreter_config.is_free_threaded() { - Some("t".into()) - } else { - None - }, - }; - - let sysconfigdata_path = match find_sysconfigdata(&cross) { - Ok(Some(path)) => path, - // Couldn't find a matching sysconfigdata; never mind! - _ => return, - }; - let sysconfigdata = super::parse_sysconfigdata(sysconfigdata_path).unwrap(); - let parsed_config = InterpreterConfig::from_sysconfigdata(&sysconfigdata).unwrap(); - - assert_eq!( - parsed_config, - InterpreterConfig { - abi3: false, - build_flags: BuildFlags(interpreter_config.build_flags.0.clone()), - pointer_width: Some(64), - executable: None, - implementation: PythonImplementation::CPython, - lib_dir: interpreter_config.lib_dir.to_owned(), - lib_name: interpreter_config.lib_name.to_owned(), - shared: true, - version: interpreter_config.version, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - } - ) - } - - #[test] - fn test_venv_interpreter() { - let base = OsStr::new("base"); - assert_eq!( - venv_interpreter(base, true), - PathBuf::from_iter(&["base", "Scripts", "python.exe"]) - ); - assert_eq!( - venv_interpreter(base, false), - PathBuf::from_iter(&["base", "bin", "python"]) - ); - } - - #[test] - fn test_conda_env_interpreter() { - let base = OsStr::new("base"); - assert_eq!( - conda_env_interpreter(base, true), - PathBuf::from_iter(&["base", "python.exe"]) - ); - assert_eq!( - conda_env_interpreter(base, false), - PathBuf::from_iter(&["base", "bin", "python"]) - ); - } - - #[test] - fn test_not_cross_compiling_from_to() { - assert!(cross_compiling_from_to( - &triple!("x86_64-unknown-linux-gnu"), - &triple!("x86_64-unknown-linux-gnu"), - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-apple-darwin"), - &triple!("x86_64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("aarch64-apple-darwin"), - &triple!("x86_64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-apple-darwin"), - &triple!("aarch64-apple-darwin") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-pc-windows-msvc"), - &triple!("i686-pc-windows-msvc") - ) - .unwrap() - .is_none()); - - assert!(cross_compiling_from_to( - &triple!("x86_64-unknown-linux-gnu"), - &triple!("x86_64-unknown-linux-musl") - ) - .unwrap() - .is_none()); - } - - #[test] - fn test_is_cross_compiling_from_to() { - assert!(cross_compiling_from_to( - &triple!("x86_64-pc-windows-msvc"), - &triple!("aarch64-pc-windows-msvc") - ) - .unwrap() - .is_some()); - } - - #[test] - fn test_run_python_script() { - // as above, this should be okay in CI where Python is presumed installed - let interpreter = make_interpreter_config() - .expect("could not get InterpreterConfig from installed interpreter"); - let out = interpreter - .run_python_script("print(2 + 2)") - .expect("failed to run Python script"); - assert_eq!(out.trim_end(), "4"); - } - - #[test] - fn test_run_python_script_with_envs() { - // as above, this should be okay in CI where Python is presumed installed - let interpreter = make_interpreter_config() - .expect("could not get InterpreterConfig from installed interpreter"); - let out = interpreter - .run_python_script_with_envs( - "import os; print(os.getenv('PYO3_TEST'))", - vec![("PYO3_TEST", "42")], - ) - .expect("failed to run Python script"); - assert_eq!(out.trim_end(), "42"); - } - - #[test] - fn test_build_script_outputs_base() { - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - ] - ); - - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::PyPy, - ..interpreter_config - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=PyPy".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_abi3() { - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 9 }, - shared: true, - abi3: true, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=Py_LIMITED_API".to_owned(), - ] - ); - - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::PyPy, - ..interpreter_config - }; - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=PyPy".to_owned(), - "cargo:rustc-cfg=Py_LIMITED_API".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_gil_disabled() { - let mut build_flags = BuildFlags::default(); - build_flags.0.insert(BuildFlag::Py_GIL_DISABLED); - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { - major: 3, - minor: 13, - }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=Py_3_8".to_owned(), - "cargo:rustc-cfg=Py_3_9".to_owned(), - "cargo:rustc-cfg=Py_3_10".to_owned(), - "cargo:rustc-cfg=Py_3_11".to_owned(), - "cargo:rustc-cfg=Py_3_12".to_owned(), - "cargo:rustc-cfg=Py_3_13".to_owned(), - "cargo:rustc-cfg=Py_GIL_DISABLED".to_owned(), - ] - ); - } - - #[test] - fn test_build_script_outputs_debug() { - let mut build_flags = BuildFlags::default(); - build_flags.0.insert(BuildFlag::Py_DEBUG); - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { major: 3, minor: 7 }, - shared: true, - abi3: false, - lib_name: Some("python3".into()), - lib_dir: None, - executable: None, - pointer_width: None, - build_flags, - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: None, - }; - - assert_eq!( - interpreter_config.build_script_outputs(), - [ - "cargo:rustc-cfg=Py_3_7".to_owned(), - "cargo:rustc-cfg=py_sys_config=\"Py_DEBUG\"".to_owned(), - ] - ); - } - - #[test] - fn test_find_sysconfigdata_in_invalid_lib_dir() { - let e = find_all_sysconfigdata(&CrossCompileConfig { - lib_dir: Some(PathBuf::from("/abc/123/not/a/real/path")), - version: None, - implementation: None, - target: triple!("x86_64-unknown-linux-gnu"), - abiflags: None, - }) - .unwrap_err(); - - // actual error message is platform-dependent, so just check the context we add - assert!(e.report().to_string().starts_with( - "failed to search the lib dir at 'PYO3_CROSS_LIB_DIR=/abc/123/not/a/real/path'\n\ - caused by:\n \ - - 0: failed to list the entries in '/abc/123/not/a/real/path'\n \ - - 1: \ - " - )); - } - - #[test] - fn test_from_pyo3_config_file_env_rebuild() { - READ_ENV_VARS.with(|vars| vars.borrow_mut().clear()); - let _ = InterpreterConfig::from_pyo3_config_file_env(); - // it's possible that other env vars were also read, hence just checking for contains - READ_ENV_VARS.with(|vars| assert!(vars.borrow().contains(&"PYO3_CONFIG_FILE".to_string()))); - } -} diff --git a/vendor/pyo3/pyo3-build-config/src/import_lib.rs b/vendor/pyo3/pyo3-build-config/src/import_lib.rs deleted file mode 100644 index ee934441f7..0000000000 --- a/vendor/pyo3/pyo3-build-config/src/import_lib.rs +++ /dev/null @@ -1,64 +0,0 @@ -//! Optional `python3.dll` import library generator for Windows - -use std::env; -use std::path::PathBuf; - -use python3_dll_a::ImportLibraryGenerator; -use target_lexicon::{Architecture, OperatingSystem, Triple}; - -use super::{PythonImplementation, PythonVersion}; -use crate::errors::{Context, Error, Result}; - -/// Generates the `python3.dll` or `pythonXY.dll` import library for Windows targets. -/// -/// Places the generated import library into the build script output directory -/// and returns the full library directory path. -/// -/// Does nothing if the target OS is not Windows. -pub(super) fn generate_import_lib( - target: &Triple, - py_impl: PythonImplementation, - py_version: Option, - abiflags: Option<&str>, -) -> Result> { - if target.operating_system != OperatingSystem::Windows { - return Ok(None); - } - - let out_dir = - env::var_os("OUT_DIR").expect("generate_import_lib() must be called from a build script"); - - // Put the newly created import library into the build script output directory. - let mut out_lib_dir = PathBuf::from(out_dir); - out_lib_dir.push("lib"); - - // Convert `Architecture` enum to rustc `target_arch` option format. - let arch = match target.architecture { - // i686, i586, etc. - Architecture::X86_32(_) => "x86".to_string(), - other => other.to_string(), - }; - - let env = target.environment.to_string(); - let implementation = match py_impl { - PythonImplementation::CPython => python3_dll_a::PythonImplementation::CPython, - PythonImplementation::PyPy => python3_dll_a::PythonImplementation::PyPy, - PythonImplementation::GraalPy => { - return Err(Error::from("No support for GraalPy on Windows")) - } - }; - - ImportLibraryGenerator::new(&arch, &env) - .version(py_version.map(|v| (v.major, v.minor))) - .implementation(implementation) - .abiflags(abiflags) - .generate(&out_lib_dir) - .context("failed to generate python3.dll import library")?; - - let out_lib_dir_string = out_lib_dir - .to_str() - .ok_or("build directory is not a valid UTF-8 string")? - .to_owned(); - - Ok(Some(out_lib_dir_string)) -} diff --git a/vendor/pyo3/pyo3-build-config/src/lib.rs b/vendor/pyo3/pyo3-build-config/src/lib.rs deleted file mode 100644 index db7f332284..0000000000 --- a/vendor/pyo3/pyo3-build-config/src/lib.rs +++ /dev/null @@ -1,387 +0,0 @@ -//! Configuration used by PyO3 for conditional support of varying Python versions. -//! -//! This crate exposes functionality to be called from build scripts to simplify building crates -//! which depend on PyO3. -//! -//! It used internally by the PyO3 crate's build script to apply the same configuration. - -#![warn(elided_lifetimes_in_paths, unused_lifetimes)] - -mod errors; -mod impl_; - -#[cfg(feature = "resolve-config")] -use std::{ - io::Cursor, - path::{Path, PathBuf}, -}; - -use std::{env, process::Command, str::FromStr}; - -use once_cell::sync::OnceCell; - -pub use impl_::{ - cross_compiling_from_to, find_all_sysconfigdata, parse_sysconfigdata, BuildFlag, BuildFlags, - CrossCompileConfig, InterpreterConfig, PythonImplementation, PythonVersion, Triple, -}; -use target_lexicon::OperatingSystem; - -/// Adds all the [`#[cfg]` flags](index.html) to the current compilation. -/// -/// This should be called from a build script. -/// -/// The full list of attributes added are the following: -/// -/// | Flag | Description | -/// | ---- | ----------- | -/// | `#[cfg(Py_3_7)]`, `#[cfg(Py_3_8)]`, `#[cfg(Py_3_9)]`, `#[cfg(Py_3_10)]` | These attributes mark code only for a given Python version and up. For example, `#[cfg(Py_3_7)]` marks code which can run on Python 3.7 **and newer**. | -/// | `#[cfg(Py_LIMITED_API)]` | This marks code which is run when compiling with PyO3's `abi3` feature enabled. | -/// | `#[cfg(PyPy)]` | This marks code which is run when compiling for PyPy. | -/// | `#[cfg(GraalPy)]` | This marks code which is run when compiling for GraalPy. | -/// -/// For examples of how to use these attributes, -#[doc = concat!("[see PyO3's guide](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/building-and-distribution/multiple_python_versions.html)")] -/// . -#[cfg(feature = "resolve-config")] -pub fn use_pyo3_cfgs() { - print_expected_cfgs(); - for cargo_command in get().build_script_outputs() { - println!("{}", cargo_command) - } -} - -/// Adds linker arguments suitable for PyO3's `extension-module` feature. -/// -/// This should be called from a build script. -/// -/// The following link flags are added: -/// - macOS: `-undefined dynamic_lookup` -/// - wasm32-unknown-emscripten: `-sSIDE_MODULE=2 -sWASM_BIGINT` -/// -/// All other platforms currently are no-ops, however this may change as necessary -/// in future. -pub fn add_extension_module_link_args() { - _add_extension_module_link_args(&impl_::target_triple_from_env(), std::io::stdout()) -} - -fn _add_extension_module_link_args(triple: &Triple, mut writer: impl std::io::Write) { - if triple.operating_system == OperatingSystem::Darwin { - writeln!(writer, "cargo:rustc-cdylib-link-arg=-undefined").unwrap(); - writeln!(writer, "cargo:rustc-cdylib-link-arg=dynamic_lookup").unwrap(); - } else if triple == &Triple::from_str("wasm32-unknown-emscripten").unwrap() { - writeln!(writer, "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2").unwrap(); - writeln!(writer, "cargo:rustc-cdylib-link-arg=-sWASM_BIGINT").unwrap(); - } -} - -/// Adds linker arguments suitable for linking against the Python framework on macOS. -/// -/// This should be called from a build script. -/// -/// The following link flags are added: -/// - macOS: `-Wl,-rpath,` -/// -/// All other platforms currently are no-ops. -#[cfg(feature = "resolve-config")] -pub fn add_python_framework_link_args() { - let interpreter_config = pyo3_build_script_impl::resolve_interpreter_config().unwrap(); - _add_python_framework_link_args( - &interpreter_config, - &impl_::target_triple_from_env(), - impl_::is_linking_libpython(), - std::io::stdout(), - ) -} - -#[cfg(feature = "resolve-config")] -fn _add_python_framework_link_args( - interpreter_config: &InterpreterConfig, - triple: &Triple, - link_libpython: bool, - mut writer: impl std::io::Write, -) { - if matches!(triple.operating_system, OperatingSystem::Darwin) && link_libpython { - if let Some(framework_prefix) = interpreter_config.python_framework_prefix.as_ref() { - writeln!( - writer, - "cargo:rustc-link-arg=-Wl,-rpath,{}", - framework_prefix - ) - .unwrap(); - } - } -} - -/// Loads the configuration determined from the build environment. -/// -/// Because this will never change in a given compilation run, this is cached in a `once_cell`. -#[cfg(feature = "resolve-config")] -pub fn get() -> &'static InterpreterConfig { - static CONFIG: OnceCell = OnceCell::new(); - CONFIG.get_or_init(|| { - // Check if we are in a build script and cross compiling to a different target. - let cross_compile_config_path = resolve_cross_compile_config_path(); - let cross_compiling = cross_compile_config_path - .as_ref() - .map(|path| path.exists()) - .unwrap_or(false); - - // CONFIG_FILE is generated in build.rs, so it's content can vary - #[allow(unknown_lints, clippy::const_is_empty)] - if let Some(interpreter_config) = InterpreterConfig::from_cargo_dep_env() { - interpreter_config - } else if !CONFIG_FILE.is_empty() { - InterpreterConfig::from_reader(Cursor::new(CONFIG_FILE)) - } else if cross_compiling { - InterpreterConfig::from_path(cross_compile_config_path.as_ref().unwrap()) - } else { - InterpreterConfig::from_reader(Cursor::new(HOST_CONFIG)) - } - .expect("failed to parse PyO3 config") - }) -} - -/// Build configuration provided by `PYO3_CONFIG_FILE`. May be empty if env var not set. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -const CONFIG_FILE: &str = include_str!(concat!(env!("OUT_DIR"), "/pyo3-build-config-file.txt")); - -/// Build configuration discovered by `pyo3-build-config` build script. Not aware of -/// cross-compilation settings. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -const HOST_CONFIG: &str = include_str!(concat!(env!("OUT_DIR"), "/pyo3-build-config.txt")); - -/// Returns the path where PyO3's build.rs writes its cross compile configuration. -/// -/// The config file will be named `$OUT_DIR//pyo3-build-config.txt`. -/// -/// Must be called from a build script, returns `None` if not. -#[doc(hidden)] -#[cfg(feature = "resolve-config")] -fn resolve_cross_compile_config_path() -> Option { - env::var_os("TARGET").map(|target| { - let mut path = PathBuf::from(env!("OUT_DIR")); - path.push(Path::new(&target)); - path.push("pyo3-build-config.txt"); - path - }) -} - -/// Use certain features if we detect the compiler being used supports them. -/// -/// Features may be removed or added as MSRV gets bumped or new features become available, -/// so this function is unstable. -#[doc(hidden)] -pub fn print_feature_cfgs() { - let rustc_minor_version = rustc_minor_version().unwrap_or(0); - - if rustc_minor_version >= 70 { - println!("cargo:rustc-cfg=rustc_has_once_lock"); - } - - // invalid_from_utf8 lint was added in Rust 1.74 - if rustc_minor_version >= 74 { - println!("cargo:rustc-cfg=invalid_from_utf8_lint"); - } - - if rustc_minor_version >= 79 { - println!("cargo:rustc-cfg=c_str_lit"); - } - - // Actually this is available on 1.78, but we should avoid - // https://github.com/rust-lang/rust/issues/124651 just in case - if rustc_minor_version >= 79 { - println!("cargo:rustc-cfg=diagnostic_namespace"); - } - - if rustc_minor_version >= 85 { - println!("cargo:rustc-cfg=fn_ptr_eq"); - } -} - -/// Registers `pyo3`s config names as reachable cfg expressions -/// -/// - -/// - -#[doc(hidden)] -pub fn print_expected_cfgs() { - if rustc_minor_version().map_or(false, |version| version < 80) { - // rustc 1.80.0 stabilized `rustc-check-cfg` feature, don't emit before - return; - } - - println!("cargo:rustc-check-cfg=cfg(Py_LIMITED_API)"); - println!("cargo:rustc-check-cfg=cfg(Py_GIL_DISABLED)"); - println!("cargo:rustc-check-cfg=cfg(PyPy)"); - println!("cargo:rustc-check-cfg=cfg(GraalPy)"); - println!("cargo:rustc-check-cfg=cfg(py_sys_config, values(\"Py_DEBUG\", \"Py_REF_DEBUG\", \"Py_TRACE_REFS\", \"COUNT_ALLOCS\"))"); - println!("cargo:rustc-check-cfg=cfg(invalid_from_utf8_lint)"); - println!("cargo:rustc-check-cfg=cfg(pyo3_disable_reference_pool)"); - println!("cargo:rustc-check-cfg=cfg(pyo3_leak_on_drop_without_reference_pool)"); - println!("cargo:rustc-check-cfg=cfg(diagnostic_namespace)"); - println!("cargo:rustc-check-cfg=cfg(c_str_lit)"); - println!("cargo:rustc-check-cfg=cfg(rustc_has_once_lock)"); - println!("cargo:rustc-check-cfg=cfg(fn_ptr_eq)"); - - // allow `Py_3_*` cfgs from the minimum supported version up to the - // maximum minor version (+1 for development for the next) - for i in impl_::MINIMUM_SUPPORTED_VERSION.minor..=impl_::ABI3_MAX_MINOR + 1 { - println!("cargo:rustc-check-cfg=cfg(Py_3_{i})"); - } -} - -/// Private exports used in PyO3's build.rs -/// -/// Please don't use these - they could change at any time. -#[doc(hidden)] -pub mod pyo3_build_script_impl { - #[cfg(feature = "resolve-config")] - use crate::errors::{Context, Result}; - - #[cfg(feature = "resolve-config")] - use super::*; - - pub mod errors { - pub use crate::errors::*; - } - pub use crate::impl_::{ - cargo_env_var, env_var, is_linking_libpython, make_cross_compile_config, InterpreterConfig, - PythonVersion, - }; - - /// Gets the configuration for use from PyO3's build script. - /// - /// Differs from .get() above only in the cross-compile case, where PyO3's build script is - /// required to generate a new config (as it's the first build script which has access to the - /// correct value for CARGO_CFG_TARGET_OS). - #[cfg(feature = "resolve-config")] - pub fn resolve_interpreter_config() -> Result { - // CONFIG_FILE is generated in build.rs, so it's content can vary - #[allow(unknown_lints, clippy::const_is_empty)] - if !CONFIG_FILE.is_empty() { - let mut interperter_config = InterpreterConfig::from_reader(Cursor::new(CONFIG_FILE))?; - interperter_config.generate_import_libs()?; - Ok(interperter_config) - } else if let Some(interpreter_config) = make_cross_compile_config()? { - // This is a cross compile and need to write the config file. - let path = resolve_cross_compile_config_path() - .expect("resolve_interpreter_config() must be called from a build script"); - let parent_dir = path.parent().ok_or_else(|| { - format!( - "failed to resolve parent directory of config file {}", - path.display() - ) - })?; - std::fs::create_dir_all(parent_dir).with_context(|| { - format!( - "failed to create config file directory {}", - parent_dir.display() - ) - })?; - interpreter_config.to_writer(&mut std::fs::File::create(&path).with_context( - || format!("failed to create config file at {}", path.display()), - )?)?; - Ok(interpreter_config) - } else { - InterpreterConfig::from_reader(Cursor::new(HOST_CONFIG)) - } - } -} - -fn rustc_minor_version() -> Option { - static RUSTC_MINOR_VERSION: OnceCell> = OnceCell::new(); - *RUSTC_MINOR_VERSION.get_or_init(|| { - let rustc = env::var_os("RUSTC")?; - let output = Command::new(rustc).arg("--version").output().ok()?; - let version = core::str::from_utf8(&output.stdout).ok()?; - let mut pieces = version.split('.'); - if pieces.next() != Some("rustc 1") { - return None; - } - pieces.next()?.parse().ok() - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn extension_module_link_args() { - let mut buf = Vec::new(); - - // Does nothing on non-mac - _add_extension_module_link_args( - &Triple::from_str("x86_64-pc-windows-msvc").unwrap(), - &mut buf, - ); - assert_eq!(buf, Vec::new()); - - _add_extension_module_link_args( - &Triple::from_str("x86_64-apple-darwin").unwrap(), - &mut buf, - ); - assert_eq!( - std::str::from_utf8(&buf).unwrap(), - "cargo:rustc-cdylib-link-arg=-undefined\n\ - cargo:rustc-cdylib-link-arg=dynamic_lookup\n" - ); - - buf.clear(); - _add_extension_module_link_args( - &Triple::from_str("wasm32-unknown-emscripten").unwrap(), - &mut buf, - ); - assert_eq!( - std::str::from_utf8(&buf).unwrap(), - "cargo:rustc-cdylib-link-arg=-sSIDE_MODULE=2\n\ - cargo:rustc-cdylib-link-arg=-sWASM_BIGINT\n" - ); - } - - #[cfg(feature = "resolve-config")] - #[test] - fn python_framework_link_args() { - let mut buf = Vec::new(); - - let interpreter_config = InterpreterConfig { - implementation: PythonImplementation::CPython, - version: PythonVersion { - major: 3, - minor: 13, - }, - shared: true, - abi3: false, - lib_name: None, - lib_dir: None, - executable: None, - pointer_width: None, - build_flags: BuildFlags::default(), - suppress_build_script_link_lines: false, - extra_build_script_lines: vec![], - python_framework_prefix: Some( - "/Applications/Xcode.app/Contents/Developer/Library/Frameworks".to_string(), - ), - }; - // Does nothing on non-mac - _add_python_framework_link_args( - &interpreter_config, - &Triple::from_str("x86_64-pc-windows-msvc").unwrap(), - true, - &mut buf, - ); - assert_eq!(buf, Vec::new()); - - _add_python_framework_link_args( - &interpreter_config, - &Triple::from_str("x86_64-apple-darwin").unwrap(), - true, - &mut buf, - ); - assert_eq!( - std::str::from_utf8(&buf).unwrap(), - "cargo:rustc-link-arg=-Wl,-rpath,/Applications/Xcode.app/Contents/Developer/Library/Frameworks\n" - ); - } -} diff --git a/vendor/pyo3/pyo3-ffi-check/Cargo.toml b/vendor/pyo3/pyo3-ffi-check/Cargo.toml deleted file mode 100644 index 776add0c91..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "pyo3-ffi-check" -version = "0.1.0" -edition = "2021" -publish = false - -[dependencies] -pyo3-ffi-check-macro = { path = "./macro" } -memoffset = "0.9.0" - -[dependencies.pyo3-ffi] -path = "../pyo3-ffi" -features = ["extension-module"] # A lazy way of skipping linking in most cases (as we don't use any runtime symbols) - -[build-dependencies] -bindgen = "0.66.1" -pyo3-build-config = { path = "../pyo3-build-config" } - -[workspace] -members = [ - "macro" -] diff --git a/vendor/pyo3/pyo3-ffi-check/README.md b/vendor/pyo3/pyo3-ffi-check/README.md deleted file mode 100644 index ced60dceb6..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# pyo3-ffi-check - -This is a simple program which compares ffi definitions from `pyo3-ffi` against those produced by `bindgen`. - -If any differ in size, these are printed to stdout and a the process will exit nonzero. - -The main purpose of this program is to be run as part of PyO3's continuous integration pipeline to catch possible errors in PyO3's ffi definitions. diff --git a/vendor/pyo3/pyo3-ffi-check/build.rs b/vendor/pyo3/pyo3-ffi-check/build.rs deleted file mode 100644 index 072e8c072b..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/build.rs +++ /dev/null @@ -1,50 +0,0 @@ -use std::env; -use std::path::PathBuf; - -fn main() { - let config = pyo3_build_config::get(); - let python_include_dir = config - .run_python_script( - "import sysconfig; print(sysconfig.get_config_var('INCLUDEPY'), end='');", - ) - .expect("failed to get lib dir"); - let gil_disabled_on_windows = config - .run_python_script( - "import sysconfig; import platform; print(sysconfig.get_config_var('Py_GIL_DISABLED') == 1 and platform.system() == 'Windows');", - ) - .expect("failed to get Py_GIL_DISABLED").trim_end() == "True"; - - let clang_args = if gil_disabled_on_windows { - vec![ - format!("-I{python_include_dir}"), - "-DPy_GIL_DISABLED".to_string(), - ] - } else { - vec![format!("-I{python_include_dir}")] - }; - - println!("cargo:rerun-if-changed=wrapper.h"); - - let bindings = bindgen::Builder::default() - .header("wrapper.h") - .clang_args(clang_args) - .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - // blocklist some values which apparently have conflicting definitions on unix - .blocklist_item("FP_NORMAL") - .blocklist_item("FP_SUBNORMAL") - .blocklist_item("FP_NAN") - .blocklist_item("FP_INFINITE") - .blocklist_item("FP_INT_UPWARD") - .blocklist_item("FP_INT_DOWNWARD") - .blocklist_item("FP_INT_TOWARDZERO") - .blocklist_item("FP_INT_TONEARESTFROMZERO") - .blocklist_item("FP_INT_TONEAREST") - .blocklist_item("FP_ZERO") - .generate() - .expect("Unable to generate bindings"); - - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); -} diff --git a/vendor/pyo3/pyo3-ffi-check/macro/Cargo.toml b/vendor/pyo3/pyo3-ffi-check/macro/Cargo.toml deleted file mode 100644 index dfefcd2c1e..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/macro/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "pyo3-ffi-check-macro" -version = "0.1.0" -edition = "2021" -publish = false - -[lib] -proc-macro = true - -[dependencies] -glob = "0.3" -quote = "1" -proc-macro2 = "1.0.60" -scraper = "0.17" -pyo3-build-config = { path = "../../pyo3-build-config" } diff --git a/vendor/pyo3/pyo3-ffi-check/macro/src/lib.rs b/vendor/pyo3/pyo3-ffi-check/macro/src/lib.rs deleted file mode 100644 index e3d442c370..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/macro/src/lib.rs +++ /dev/null @@ -1,167 +0,0 @@ -use std::{env, fs, path::PathBuf}; - -use proc_macro2::{Ident, Span, TokenStream, TokenTree}; -use pyo3_build_config::PythonVersion; -use quote::quote; - -const PY_3_12: PythonVersion = PythonVersion { - major: 3, - minor: 12, -}; - -/// Macro which expands to multiple macro calls, one per pyo3-ffi struct. -#[proc_macro] -pub fn for_all_structs(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let input: TokenStream = input.into(); - let mut input = input.into_iter(); - - let macro_name = match input.next() { - Some(TokenTree::Ident(i)) => i, - _ => { - return quote!(compile_error!( - "for_all_structs!() takes only a single ident as input" - )) - .into() - } - }; - - if input.next().is_some() { - return quote!(compile_error!( - "for_all_structs!() takes only a single ident as input" - )) - .into(); - } - - let doc_dir = get_doc_dir(); - let structs_glob = format!("{}/doc/pyo3_ffi/struct.*.html", doc_dir.display()); - - let mut output = TokenStream::new(); - - for entry in glob::glob(&structs_glob).expect("Failed to read glob pattern") { - let entry = entry - .unwrap() - .file_name() - .unwrap() - .to_string_lossy() - .into_owned(); - let struct_name = entry - .strip_prefix("struct.") - .unwrap() - .strip_suffix(".html") - .unwrap(); - let struct_ident = Ident::new(struct_name, Span::call_site()); - output.extend(quote!(#macro_name!(#struct_ident);)); - } - - if output.is_empty() { - quote!(compile_error!(concat!( - "No files found at `", - #structs_glob, - "`, try running `cargo doc -p pyo3-ffi` first." - ))) - } else { - output - } - .into() -} - -fn get_doc_dir() -> PathBuf { - let path = PathBuf::from(env::var_os("OUT_DIR").unwrap()); - path.parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .to_owned() -} - -/// Macro which expands to multiple macro calls, one per field in a pyo3-ffi -/// struct. -#[proc_macro] -pub fn for_all_fields(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - let input: TokenStream = input.into(); - let mut input = input.into_iter(); - - let struct_name = match input.next() { - Some(TokenTree::Ident(i)) => i, - _ => { - return quote!(compile_error!( - "for_all_fields!() takes exactly two idents as input" - )) - .into() - } - }; - - match input.next() { - Some(TokenTree::Punct(punct)) if punct.as_char() == ',' => (), - _ => { - return quote!(compile_error!( - "for_all_fields!() takes exactly two idents as input" - )) - .into() - } - }; - - let macro_name = match input.next() { - Some(TokenTree::Ident(i)) => i, - _ => { - return quote!(compile_error!( - "for_all_fields!() takes exactly two idents as input" - )) - .into() - } - }; - - if input.next().is_some() { - return quote!(compile_error!( - "for_all_fields!() takes exactly two idents as input" - )) - .into(); - } - - let doc_dir = get_doc_dir(); - let struct_file = fs::read_to_string(format!( - "{}/doc/pyo3_ffi/struct.{}.html", - doc_dir.display(), - struct_name - )) - .unwrap(); - - let html = scraper::Html::parse_document(&struct_file); - let selector = scraper::Selector::parse("span.structfield").unwrap(); - - let mut output = TokenStream::new(); - - for el in html.select(&selector) { - let field_name = el - .value() - .id() - .unwrap() - .strip_prefix("structfield.") - .unwrap(); - - let field_ident = Ident::new(field_name, Span::call_site()); - - let bindgen_field_ident = if (pyo3_build_config::get().version >= PY_3_12) - && struct_name == "PyObject" - && field_name == "ob_refcnt" - { - // PyObject since 3.12 implements ob_refcnt as a union; bindgen creates - // an anonymous name for the field - Ident::new("__bindgen_anon_1", Span::call_site()) - } else if struct_name == "PyMemberDef" && field_name == "type_code" { - // the field name in the C API is `type`, but that's a keyword in Rust - // so PyO3 picked type_code, bindgen picked type_ - Ident::new("type_", Span::call_site()) - } else { - field_ident.clone() - }; - - output.extend(quote!(#macro_name!(#struct_name, #field_ident, #bindgen_field_ident);)); - } - - output.into() -} diff --git a/vendor/pyo3/pyo3-ffi-check/src/main.rs b/vendor/pyo3/pyo3-ffi-check/src/main.rs deleted file mode 100644 index 0407a2ffa3..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/src/main.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::{ffi::CStr, process::exit}; - -fn main() { - println!( - "comparing pyo3-ffi against headers generated for {}", - CStr::from_bytes_with_nul(bindings::PY_VERSION) - .unwrap() - .to_string_lossy() - ); - - let mut failed = false; - - macro_rules! check_struct { - ($name:ident) => {{ - let pyo3_ffi_size = std::mem::size_of::(); - let bindgen_size = std::mem::size_of::(); - - let pyo3_ffi_align = std::mem::align_of::(); - let bindgen_align = std::mem::align_of::(); - - // Check if sizes differ, but ignore zero-sized types (probably "opaque" in pyo3-ffi) - if pyo3_ffi_size == 0 { - println!( - "warning: ignoring zero-sized pyo3_ffi type {}", - stringify!($name), - ); - } else if pyo3_ffi_size != bindgen_size { - failed = true; - println!( - "error: size of {} differs between pyo3_ffi ({}) and bindgen ({})", - stringify!($name), - pyo3_ffi_size, - bindgen_size - ); - } else if pyo3_ffi_align != bindgen_align { - failed = true; - println!( - "error: alignment of {} differs between pyo3_ffi ({}) and bindgen ({})", - stringify!($name), - pyo3_ffi_align, - bindgen_align - ); - } - - pyo3_ffi_check_macro::for_all_fields!($name, check_field); - }}; - } - - macro_rules! check_field { - ($struct_name:ident, $field:ident, $bindgen_field:ident) => {{ - // some struct fields are deprecated but still present in the ABI - #[allow(clippy::used_underscore_binding, deprecated)] - let pyo3_ffi_offset = memoffset::offset_of!(pyo3_ffi::$struct_name, $field); - #[allow(clippy::used_underscore_binding)] - let bindgen_offset = memoffset::offset_of!(bindings::$struct_name, $bindgen_field); - - if pyo3_ffi_offset != bindgen_offset { - failed = true; - println!( - "error: field offset of {}.{} differs between pyo3_ffi ({}) and bindgen ({})", - stringify!($struct_name), - stringify!($field), - pyo3_ffi_offset, - bindgen_offset - ); - } - }}; - } - - pyo3_ffi_check_macro::for_all_structs!(check_struct); - - if failed { - exit(1); - } else { - exit(0); - } -} - -#[allow( - non_snake_case, - non_camel_case_types, - non_upper_case_globals, - dead_code, - improper_ctypes, - clippy::all, - // clippy fails with lots of errors if this is not set specifically - clippy::used_underscore_binding -)] -mod bindings { - include!(concat!(env!("OUT_DIR"), "/bindings.rs")); -} diff --git a/vendor/pyo3/pyo3-ffi-check/wrapper.h b/vendor/pyo3/pyo3-ffi-check/wrapper.h deleted file mode 100644 index b91582e107..0000000000 --- a/vendor/pyo3/pyo3-ffi-check/wrapper.h +++ /dev/null @@ -1,4 +0,0 @@ -#include "Python.h" -#include "datetime.h" -#include "frameobject.h" -#include "structmember.h" diff --git a/vendor/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS b/vendor/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS deleted file mode 100644 index 8b20727dec..0000000000 --- a/vendor/pyo3/pyo3-ffi/ACKNOWLEDGEMENTS +++ /dev/null @@ -1,6 +0,0 @@ -This is a Rust reimplementation of the CPython public header files as necessary -for binary compatibility, with additional metadata to support PyPy. - -For original implementations please see: - - https://github.com/python/cpython - - https://github.com/pypy/pypy diff --git a/vendor/pyo3/pyo3-ffi/Cargo.toml b/vendor/pyo3/pyo3-ffi/Cargo.toml deleted file mode 100644 index fe8b3b2551..0000000000 --- a/vendor/pyo3/pyo3-ffi/Cargo.toml +++ /dev/null @@ -1,48 +0,0 @@ -[package] -name = "pyo3-ffi" -version = "0.23.4" -description = "Python-API bindings for the PyO3 ecosystem" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" -links = "python" -rust-version = "1.63" - -[dependencies] -libc = "0.2.62" - -[features] - -default = [] - -# Use this feature when building an extension module. -# It tells the linker to keep the python symbols unresolved, -# so that the module can also be used with statically linked python interpreters. -extension-module = ["pyo3-build-config/extension-module"] - -# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more. -abi3 = ["pyo3-build-config/abi3"] - -# With abi3, we can manually set the minimum Python version. -abi3-py37 = ["abi3-py38", "pyo3-build-config/abi3-py37"] -abi3-py38 = ["abi3-py39", "pyo3-build-config/abi3-py38"] -abi3-py39 = ["abi3-py310", "pyo3-build-config/abi3-py39"] -abi3-py310 = ["abi3-py311", "pyo3-build-config/abi3-py310"] -abi3-py311 = ["abi3-py312", "pyo3-build-config/abi3-py311"] -abi3-py312 = ["abi3", "pyo3-build-config/abi3-py312"] - -# Automatically generates `python3.dll` import libraries for Windows targets. -generate-import-lib = ["pyo3-build-config/python3-dll-a"] - -[dev-dependencies] -paste = "1" - -[build-dependencies] -pyo3-build-config = { path = "../pyo3-build-config", version = "=0.23.4", features = ["resolve-config"] } - -[lints] -workspace = true diff --git a/vendor/pyo3/pyo3-ffi/LICENSE-APACHE b/vendor/pyo3/pyo3-ffi/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/pyo3/pyo3-ffi/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/pyo3/pyo3-ffi/LICENSE-MIT b/vendor/pyo3/pyo3-ffi/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/pyo3/pyo3-ffi/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/pyo3/pyo3-ffi/README.md b/vendor/pyo3/pyo3-ffi/README.md deleted file mode 100644 index d220949a87..0000000000 --- a/vendor/pyo3/pyo3-ffi/README.md +++ /dev/null @@ -1,243 +0,0 @@ -# pyo3-ffi - -This crate provides [Rust](https://www.rust-lang.org/) FFI declarations for Python 3. -It supports both the stable and the unstable component of the ABI through the use of cfg flags. -Python Versions 3.7+ are supported. -It is meant for advanced users only - regular PyO3 users shouldn't -need to interact with this crate at all. - -The contents of this crate are not documented here, as it would entail -basically copying the documentation from CPython. Consult the [Python/C API Reference -Manual][capi] for up-to-date documentation. - -# Minimum supported Rust and Python versions - -Requires Rust 1.63 or greater. - -`pyo3-ffi` supports the following Python distributions: - - CPython 3.7 or greater - - PyPy 7.3 (Python 3.9+) - - GraalPy 24.0 or greater (Python 3.10+) - -# Example: Building Python Native modules - -PyO3 can be used to generate a native Python module. The easiest way to try this out for the -first time is to use [`maturin`]. `maturin` is a tool for building and publishing Rust-based -Python packages with minimal configuration. The following steps set up some files for an example -Python module, install `maturin`, and then show how to build and import the Python module. - -First, create a new folder (let's call it `string_sum`) containing the following two files: - -**`Cargo.toml`** - -```toml -[lib] -name = "string_sum" -# "cdylib" is necessary to produce a shared library for Python to import from. -# -# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able -# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: -# crate-type = ["cdylib", "rlib"] -crate-type = ["cdylib"] - -[dependencies.pyo3-ffi] -version = "0.23.4" -features = ["extension-module"] - -[build-dependencies] -# This is only necessary if you need to configure your build based on -# the Python version or the compile-time configuration for the interpreter. -pyo3_build_config = "0.23.4" -``` - -If you need to use conditional compilation based on Python version or how -Python was compiled, you need to add `pyo3-build-config` as a -`build-dependency` in your `Cargo.toml` as in the example above and either -create a new `build.rs` file or modify an existing one so that -`pyo3_build_config::use_pyo3_cfgs()` gets called at build time: - -**`build.rs`** - -```rust,ignore -fn main() { - pyo3_build_config::use_pyo3_cfgs() -} -``` - -**`src/lib.rs`** -```rust -use std::os::raw::{c_char, c_long}; -use std::ptr; - -use pyo3_ffi::*; - -static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT, - m_name: c_str!("string_sum").as_ptr(), - m_doc: c_str!("A Python module written in Rust.").as_ptr(), - m_size: 0, - m_methods: unsafe { METHODS as *const [PyMethodDef] as *mut PyMethodDef }, - m_slots: std::ptr::null_mut(), - m_traverse: None, - m_clear: None, - m_free: None, -}; - -static mut METHODS: &[PyMethodDef] = &[ - PyMethodDef { - ml_name: c_str!("sum_as_string").as_ptr(), - ml_meth: PyMethodDefPointer { - PyCFunctionFast: sum_as_string, - }, - ml_flags: METH_FASTCALL, - ml_doc: c_str!("returns the sum of two integers as a string").as_ptr(), - }, - // A zeroed PyMethodDef to mark the end of the array. - PyMethodDef::zeroed(), -]; - -// The module initialization function, which must be named `PyInit_`. -#[allow(non_snake_case)] -#[no_mangle] -pub unsafe extern "C" fn PyInit_string_sum() -> *mut PyObject { - let module = PyModule_Create(ptr::addr_of_mut!(MODULE_DEF)); - if module.is_null() { - return module; - } - #[cfg(Py_GIL_DISABLED)] - { - if PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) < 0 { - Py_DECREF(module); - return std::ptr::null_mut(); - } - } - module -} - -/// A helper to parse function arguments -/// If we used PyO3's proc macros they'd handle all of this boilerplate for us :) -unsafe fn parse_arg_as_i32(obj: *mut PyObject, n_arg: usize) -> Option { - if PyLong_Check(obj) == 0 { - let msg = format!( - "sum_as_string expected an int for positional argument {}\0", - n_arg - ); - PyErr_SetString(PyExc_TypeError, msg.as_ptr().cast::()); - return None; - } - - // Let's keep the behaviour consistent on platforms where `c_long` is bigger than 32 bits. - // In particular, it is an i32 on Windows but i64 on most Linux systems - let mut overflow = 0; - let i_long: c_long = PyLong_AsLongAndOverflow(obj, &mut overflow); - - #[allow(irrefutable_let_patterns)] // some platforms have c_long equal to i32 - if overflow != 0 { - raise_overflowerror(obj); - None - } else if let Ok(i) = i_long.try_into() { - Some(i) - } else { - raise_overflowerror(obj); - None - } -} - -unsafe fn raise_overflowerror(obj: *mut PyObject) { - let obj_repr = PyObject_Str(obj); - if !obj_repr.is_null() { - let mut size = 0; - let p = PyUnicode_AsUTF8AndSize(obj_repr, &mut size); - if !p.is_null() { - let s = std::str::from_utf8_unchecked(std::slice::from_raw_parts( - p.cast::(), - size as usize, - )); - let msg = format!("cannot fit {} in 32 bits\0", s); - - PyErr_SetString(PyExc_OverflowError, msg.as_ptr().cast::()); - } - Py_DECREF(obj_repr); - } -} - -pub unsafe extern "C" fn sum_as_string( - _self: *mut PyObject, - args: *mut *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - if nargs != 2 { - PyErr_SetString( - PyExc_TypeError, - c_str!("sum_as_string expected 2 positional arguments").as_ptr(), - ); - return std::ptr::null_mut(); - } - - let (first, second) = (*args, *args.add(1)); - - let first = match parse_arg_as_i32(first, 1) { - Some(x) => x, - None => return std::ptr::null_mut(), - }; - let second = match parse_arg_as_i32(second, 2) { - Some(x) => x, - None => return std::ptr::null_mut(), - }; - - match first.checked_add(second) { - Some(sum) => { - let string = sum.to_string(); - PyUnicode_FromStringAndSize(string.as_ptr().cast::(), string.len() as isize) - } - None => { - PyErr_SetString( - PyExc_OverflowError, - c_str!("arguments too large to add").as_ptr(), - ); - std::ptr::null_mut() - } - } -} -``` - -With those two files in place, now `maturin` needs to be installed. This can be done using -Python's package manager `pip`. First, load up a new Python `virtualenv`, and install `maturin` -into it: -```bash -$ cd string_sum -$ python -m venv .env -$ source .env/bin/activate -$ pip install maturin -``` - -Now build and execute the module: -```bash -$ maturin develop -# lots of progress output as maturin runs the compilation... -$ python ->>> import string_sum ->>> string_sum.sum_as_string(5, 20) -'25' -``` - -As well as with `maturin`, it is possible to build using [setuptools-rust] or -[manually][manual_builds]. Both offer more flexibility than `maturin` but require further -configuration. - - -While most projects use the safe wrapper provided by PyO3, -you can take a look at the [`orjson`] library as an example on how to use `pyo3-ffi` directly. -For those well versed in C and Rust the [tutorials] from the CPython documentation -can be easily converted to rust as well. - -[tutorials]: https://docs.python.org/3/extending/ -[`orjson`]: https://github.com/ijl/orjson -[capi]: https://docs.python.org/3/c-api/index.html -[`maturin`]: https://github.com/PyO3/maturin "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" -[`pyo3-build-config`]: https://docs.rs/pyo3-build-config -[feature flags]: https://doc.rust-lang.org/cargo/reference/features.html "Features - The Cargo Book" -[manual_builds]: https://pyo3.rs/latest/building-and-distribution.html#manual-builds "Manual builds - Building and Distribution - PyO3 user guide" -[setuptools-rust]: https://github.com/PyO3/setuptools-rust "Setuptools plugin for Rust extensions" -[PEP 384]: https://www.python.org/dev/peps/pep-0384 "PEP 384 -- Defining a Stable ABI" -[Features chapter of the guide]: https://pyo3.rs/latest/features.html#features-reference "Features Reference - PyO3 user guide" diff --git a/vendor/pyo3/pyo3-ffi/build.rs b/vendor/pyo3/pyo3-ffi/build.rs deleted file mode 100644 index ea023de75f..0000000000 --- a/vendor/pyo3/pyo3-ffi/build.rs +++ /dev/null @@ -1,239 +0,0 @@ -use pyo3_build_config::{ - bail, ensure, print_feature_cfgs, - pyo3_build_script_impl::{ - cargo_env_var, env_var, errors::Result, is_linking_libpython, resolve_interpreter_config, - InterpreterConfig, PythonVersion, - }, - warn, PythonImplementation, -}; - -/// Minimum Python version PyO3 supports. -struct SupportedVersions { - min: PythonVersion, - max: PythonVersion, -} - -const SUPPORTED_VERSIONS_CPYTHON: SupportedVersions = SupportedVersions { - min: PythonVersion { major: 3, minor: 7 }, - max: PythonVersion { - major: 3, - minor: 13, - }, -}; - -const SUPPORTED_VERSIONS_PYPY: SupportedVersions = SupportedVersions { - min: PythonVersion { major: 3, minor: 9 }, - max: PythonVersion { - major: 3, - minor: 10, - }, -}; - -const SUPPORTED_VERSIONS_GRAALPY: SupportedVersions = SupportedVersions { - min: PythonVersion { - major: 3, - minor: 10, - }, - max: PythonVersion { - major: 3, - minor: 11, - }, -}; - -fn ensure_python_version(interpreter_config: &InterpreterConfig) -> Result<()> { - // This is an undocumented env var which is only really intended to be used in CI / for testing - // and development. - if std::env::var("UNSAFE_PYO3_SKIP_VERSION_CHECK").as_deref() == Ok("1") { - return Ok(()); - } - - match interpreter_config.implementation { - PythonImplementation::CPython => { - let versions = SUPPORTED_VERSIONS_CPYTHON; - ensure!( - interpreter_config.version >= versions.min, - "the configured Python interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - if interpreter_config.version > versions.max { - ensure!(!interpreter_config.is_free_threaded(), - "The configured Python interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}\n\ - = help: The free-threaded build of CPython does not support the limited API so this check cannot be suppressed.", - interpreter_config.version, versions.max, std::env::var("CARGO_PKG_VERSION").unwrap() - ); - ensure!(env_var("PYO3_USE_ABI3_FORWARD_COMPATIBILITY").map_or(false, |os_str| os_str == "1"), - "the configured Python interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}\n\ - = help: set PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1 to suppress this check and build anyway using the stable ABI", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap(), - ); - } - } - PythonImplementation::PyPy => { - let versions = SUPPORTED_VERSIONS_PYPY; - ensure!( - interpreter_config.version >= versions.min, - "the configured PyPy interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - // PyO3 does not support abi3, so we cannot offer forward compatibility - ensure!( - interpreter_config.version <= versions.max, - "the configured PyPy interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap() - ); - } - PythonImplementation::GraalPy => { - let versions = SUPPORTED_VERSIONS_GRAALPY; - ensure!( - interpreter_config.version >= versions.min, - "the configured GraalPy interpreter version ({}) is lower than PyO3's minimum supported version ({})", - interpreter_config.version, - versions.min, - ); - // GraalPy does not support abi3, so we cannot offer forward compatibility - ensure!( - interpreter_config.version <= versions.max, - "the configured GraalPy interpreter version ({}) is newer than PyO3's maximum supported version ({})\n\ - = help: please check if an updated version of PyO3 is available. Current version: {}", - interpreter_config.version, - versions.max, - std::env::var("CARGO_PKG_VERSION").unwrap() - ); - } - } - - if interpreter_config.abi3 { - match interpreter_config.implementation { - PythonImplementation::CPython => { - if interpreter_config.is_free_threaded() { - warn!( - "The free-threaded build of CPython does not yet support abi3 so the build artifacts will be version-specific." - ) - } - } - PythonImplementation::PyPy => warn!( - "PyPy does not yet support abi3 so the build artifacts will be version-specific. \ - See https://github.com/pypy/pypy/issues/3397 for more information." - ), - PythonImplementation::GraalPy => warn!( - "GraalPy does not support abi3 so the build artifacts will be version-specific." - ), - } - } - - Ok(()) -} - -fn ensure_target_pointer_width(interpreter_config: &InterpreterConfig) -> Result<()> { - if let Some(pointer_width) = interpreter_config.pointer_width { - // Try to check whether the target architecture matches the python library - let rust_target = match cargo_env_var("CARGO_CFG_TARGET_POINTER_WIDTH") - .unwrap() - .as_str() - { - "64" => 64, - "32" => 32, - x => bail!("unexpected Rust target pointer width: {}", x), - }; - - ensure!( - rust_target == pointer_width, - "your Rust target architecture ({}-bit) does not match your python interpreter ({}-bit)", - rust_target, - pointer_width - ); - } - Ok(()) -} - -fn emit_link_config(interpreter_config: &InterpreterConfig) -> Result<()> { - let target_os = cargo_env_var("CARGO_CFG_TARGET_OS").unwrap(); - - println!( - "cargo:rustc-link-lib={link_model}{alias}{lib_name}", - link_model = if interpreter_config.shared { - "" - } else { - "static=" - }, - alias = if target_os == "windows" { - "pythonXY:" - } else { - "" - }, - lib_name = interpreter_config.lib_name.as_ref().ok_or( - "attempted to link to Python shared library but config does not contain lib_name" - )?, - ); - - if let Some(lib_dir) = &interpreter_config.lib_dir { - println!("cargo:rustc-link-search=native={}", lib_dir); - } - - Ok(()) -} - -/// Prepares the PyO3 crate for compilation. -/// -/// This loads the config from pyo3-build-config and then makes some additional checks to improve UX -/// for users. -/// -/// Emits the cargo configuration based on this config as well as a few checks of the Rust compiler -/// version to enable features which aren't supported on MSRV. -fn configure_pyo3() -> Result<()> { - let interpreter_config = resolve_interpreter_config()?; - - if env_var("PYO3_PRINT_CONFIG").map_or(false, |os_str| os_str == "1") { - print_config_and_exit(&interpreter_config); - } - - ensure_python_version(&interpreter_config)?; - ensure_target_pointer_width(&interpreter_config)?; - - // Serialize the whole interpreter config into DEP_PYTHON_PYO3_CONFIG env var. - interpreter_config.to_cargo_dep_env()?; - - if is_linking_libpython() && !interpreter_config.suppress_build_script_link_lines { - emit_link_config(&interpreter_config)?; - } - - for cfg in interpreter_config.build_script_outputs() { - println!("{}", cfg) - } - - // Extra lines come last, to support last write wins. - for line in &interpreter_config.extra_build_script_lines { - println!("{}", line); - } - - // Emit cfgs like `invalid_from_utf8_lint` - print_feature_cfgs(); - - Ok(()) -} - -fn print_config_and_exit(config: &InterpreterConfig) { - println!("\n-- PYO3_PRINT_CONFIG=1 is set, printing configuration and halting compile --"); - config - .to_writer(std::io::stdout()) - .expect("failed to print config to stdout"); - println!("\nnote: unset the PYO3_PRINT_CONFIG environment variable and retry to compile with the above config"); - std::process::exit(101); -} - -fn main() { - pyo3_build_config::print_expected_cfgs(); - if let Err(e) = configure_pyo3() { - eprintln!("error: {}", e.report()); - std::process::exit(1) - } -} diff --git a/vendor/pyo3/pyo3-ffi/examples/README.md b/vendor/pyo3/pyo3-ffi/examples/README.md deleted file mode 100644 index f02ae4ba6b..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# `pyo3-ffi` Examples - -These example crates are a collection of toy extension modules built with -`pyo3-ffi`. They are all tested using `nox` in PyO3's CI. - -Below is a brief description of each of these: - -| Example | Description | -| `word-count` | Illustrates how to use pyo3-ffi to write a static rust extension | -| `sequential` | Illustrates how to use pyo3-ffi to write subinterpreter-safe modules using multi-phase module initialization | - -## Creating new projects from these examples - -To copy an example, use [`cargo-generate`](https://crates.io/crates/cargo-generate). Follow the commands below, replacing `` with the example to start from: - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/ -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/Cargo.toml b/vendor/pyo3/pyo3-ffi/examples/sequential/.template/Cargo.toml deleted file mode 100644 index cd0ece4291..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "sequential" -crate-type = ["cdylib", "lib"] - -[dependencies] -pyo3-ffi = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pre-script.rhai b/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pre-script.rhai deleted file mode 100644 index d3341677b1..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.19.2"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pyproject.toml b/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pyproject.toml deleted file mode 100644 index 537fdacc66..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/.template/pyproject.toml +++ /dev/null @@ -1,7 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/Cargo.toml b/vendor/pyo3/pyo3-ffi/examples/sequential/Cargo.toml deleted file mode 100644 index 288eb1ba32..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "sequential" -version = "0.1.0" -edition = "2021" - -[lib] -name = "sequential" -crate-type = ["cdylib", "lib"] - -[dependencies] -pyo3-ffi = { path = "../../", features = ["extension-module"] } - -[build-dependencies] -pyo3-build-config = { path = "../../../pyo3-build-config" } - -[workspace] diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/MANIFEST.in b/vendor/pyo3/pyo3-ffi/examples/sequential/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/README.md b/vendor/pyo3/pyo3-ffi/examples/sequential/README.md deleted file mode 100644 index e3c0078241..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# sequential - -A project built using only `pyo3_ffi`, without any of PyO3's safe api. It can be executed by subinterpreters that have their own GIL. - -## Building and Testing - -To build this package, first install `maturin`: - -```shell -pip install maturin -``` - -To build and test use `maturin develop`: - -```shell -pip install -r requirements-dev.txt -maturin develop -pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/sequential -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/build.rs b/vendor/pyo3/pyo3-ffi/examples/sequential/build.rs deleted file mode 100644 index 0475124bb4..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - pyo3_build_config::use_pyo3_cfgs(); -} diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/cargo-generate.toml b/vendor/pyo3/pyo3-ffi/examples/sequential/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/noxfile.py b/vendor/pyo3/pyo3-ffi/examples/sequential/noxfile.py deleted file mode 100644 index b6e4bfe58b..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/noxfile.py +++ /dev/null @@ -1,11 +0,0 @@ -import sys -import nox - - -@nox.session -def python(session): - if sys.version_info < (3, 12): - session.skip("Python 3.12+ is required") - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/pyproject.toml b/vendor/pyo3/pyo3-ffi/examples/sequential/pyproject.toml deleted file mode 100644 index ee3bfa986d..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/pyproject.toml +++ /dev/null @@ -1,20 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "sequential" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] -requires-python = ">=3.12" - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/src/id.rs b/vendor/pyo3/pyo3-ffi/examples/sequential/src/id.rs deleted file mode 100644 index fa72bb091c..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/src/id.rs +++ /dev/null @@ -1,136 +0,0 @@ -use core::sync::atomic::{AtomicU64, Ordering}; -use core::{mem, ptr}; -use std::ffi::CString; -use std::os::raw::{c_char, c_int, c_uint, c_ulonglong, c_void}; - -use pyo3_ffi::*; - -#[repr(C)] -pub struct PyId { - _ob_base: PyObject, - id: Id, -} - -static COUNT: AtomicU64 = AtomicU64::new(0); - -#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] -pub struct Id(u64); - -impl Id { - fn new() -> Self { - Id(COUNT.fetch_add(1, Ordering::Relaxed)) - } -} - -unsafe extern "C" fn id_new( - subtype: *mut PyTypeObject, - args: *mut PyObject, - kwds: *mut PyObject, -) -> *mut PyObject { - if PyTuple_Size(args) != 0 || !kwds.is_null() { - // We use pyo3-ffi's `c_str!` macro to create null-terminated literals because - // Rust's string literals are not null-terminated - // On Rust 1.77 or newer you can use `c"text"` instead. - PyErr_SetString(PyExc_TypeError, c_str!("Id() takes no arguments").as_ptr()); - return ptr::null_mut(); - } - - let f: allocfunc = (*subtype).tp_alloc.unwrap_or(PyType_GenericAlloc); - let slf = f(subtype, 0); - - if slf.is_null() { - return ptr::null_mut(); - } else { - let id = Id::new(); - let slf = slf.cast::(); - ptr::addr_of_mut!((*slf).id).write(id); - } - - slf -} - -unsafe extern "C" fn id_repr(slf: *mut PyObject) -> *mut PyObject { - let slf = slf.cast::(); - let id = (*slf).id.0; - let string = format!("Id({})", id); - PyUnicode_FromStringAndSize(string.as_ptr().cast::(), string.len() as Py_ssize_t) -} - -unsafe extern "C" fn id_int(slf: *mut PyObject) -> *mut PyObject { - let slf = slf.cast::(); - let id = (*slf).id.0; - PyLong_FromUnsignedLongLong(id as c_ulonglong) -} - -unsafe extern "C" fn id_richcompare( - slf: *mut PyObject, - other: *mut PyObject, - op: c_int, -) -> *mut PyObject { - let pytype = Py_TYPE(slf); // guaranteed to be `sequential.Id` - if Py_TYPE(other) != pytype { - return Py_NewRef(Py_NotImplemented()); - } - let slf = (*slf.cast::()).id; - let other = (*other.cast::()).id; - - let cmp = match op { - pyo3_ffi::Py_LT => slf < other, - pyo3_ffi::Py_LE => slf <= other, - pyo3_ffi::Py_EQ => slf == other, - pyo3_ffi::Py_NE => slf != other, - pyo3_ffi::Py_GT => slf > other, - pyo3_ffi::Py_GE => slf >= other, - unrecognized => { - let msg = CString::new(&*format!( - "unrecognized richcompare opcode {}", - unrecognized - )) - .unwrap(); - PyErr_SetString(PyExc_SystemError, msg.as_ptr()); - return ptr::null_mut(); - } - }; - - if cmp { - Py_NewRef(Py_True()) - } else { - Py_NewRef(Py_False()) - } -} - -static mut SLOTS: &[PyType_Slot] = &[ - PyType_Slot { - slot: Py_tp_new, - pfunc: id_new as *mut c_void, - }, - PyType_Slot { - slot: Py_tp_doc, - pfunc: c_str!("An id that is increased every time an instance is created").as_ptr() - as *mut c_void, - }, - PyType_Slot { - slot: Py_tp_repr, - pfunc: id_repr as *mut c_void, - }, - PyType_Slot { - slot: Py_nb_int, - pfunc: id_int as *mut c_void, - }, - PyType_Slot { - slot: Py_tp_richcompare, - pfunc: id_richcompare as *mut c_void, - }, - PyType_Slot { - slot: 0, - pfunc: ptr::null_mut(), - }, -]; - -pub static mut ID_SPEC: PyType_Spec = PyType_Spec { - name: c_str!("sequential.Id").as_ptr(), - basicsize: mem::size_of::() as c_int, - itemsize: 0, - flags: (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE) as c_uint, - slots: unsafe { SLOTS as *const [PyType_Slot] as *mut PyType_Slot }, -}; diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/src/lib.rs b/vendor/pyo3/pyo3-ffi/examples/sequential/src/lib.rs deleted file mode 100644 index 7e77064de4..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/src/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::ptr; - -use pyo3_ffi::*; - -mod id; -mod module; -use crate::module::MODULE_DEF; - -// The module initialization function, which must be named `PyInit_`. -#[allow(non_snake_case)] -#[no_mangle] -pub unsafe extern "C" fn PyInit_sequential() -> *mut PyObject { - PyModuleDef_Init(ptr::addr_of_mut!(MODULE_DEF)) -} diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/src/module.rs b/vendor/pyo3/pyo3-ffi/examples/sequential/src/module.rs deleted file mode 100644 index baa7c66f20..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/src/module.rs +++ /dev/null @@ -1,85 +0,0 @@ -use core::{mem, ptr}; -use pyo3_ffi::*; -use std::os::raw::{c_int, c_void}; - -pub static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT, - m_name: c_str!("sequential").as_ptr(), - m_doc: c_str!("A library for generating sequential ids, written in Rust.").as_ptr(), - m_size: mem::size_of::() as Py_ssize_t, - m_methods: std::ptr::null_mut(), - m_slots: unsafe { SEQUENTIAL_SLOTS as *const [PyModuleDef_Slot] as *mut PyModuleDef_Slot }, - m_traverse: Some(sequential_traverse), - m_clear: Some(sequential_clear), - m_free: Some(sequential_free), -}; - -static mut SEQUENTIAL_SLOTS: &[PyModuleDef_Slot] = &[ - PyModuleDef_Slot { - slot: Py_mod_exec, - value: sequential_exec as *mut c_void, - }, - PyModuleDef_Slot { - slot: Py_mod_multiple_interpreters, - value: Py_MOD_PER_INTERPRETER_GIL_SUPPORTED, - }, - #[cfg(Py_GIL_DISABLED)] - PyModuleDef_Slot { - slot: Py_mod_gil, - value: Py_MOD_GIL_NOT_USED, - }, - PyModuleDef_Slot { - slot: 0, - value: ptr::null_mut(), - }, -]; - -unsafe extern "C" fn sequential_exec(module: *mut PyObject) -> c_int { - let state: *mut sequential_state = PyModule_GetState(module).cast(); - - let id_type = PyType_FromModuleAndSpec( - module, - ptr::addr_of_mut!(crate::id::ID_SPEC), - ptr::null_mut(), - ); - if id_type.is_null() { - PyErr_SetString( - PyExc_SystemError, - c_str!("cannot locate type object").as_ptr(), - ); - return -1; - } - (*state).id_type = id_type.cast::(); - - PyModule_AddObjectRef(module, c_str!("Id").as_ptr(), id_type) -} - -unsafe extern "C" fn sequential_traverse( - module: *mut PyObject, - visit: visitproc, - arg: *mut c_void, -) -> c_int { - let state: *mut sequential_state = PyModule_GetState(module.cast()).cast(); - let id_type: *mut PyObject = (*state).id_type.cast(); - - if id_type.is_null() { - 0 - } else { - (visit)(id_type, arg) - } -} - -unsafe extern "C" fn sequential_clear(module: *mut PyObject) -> c_int { - let state: *mut sequential_state = PyModule_GetState(module.cast()).cast(); - Py_CLEAR(ptr::addr_of_mut!((*state).id_type).cast()); - 0 -} - -unsafe extern "C" fn sequential_free(module: *mut c_void) { - sequential_clear(module.cast()); -} - -#[repr(C)] -struct sequential_state { - id_type: *mut PyTypeObject, -} diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test.rs b/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test.rs deleted file mode 100644 index f2a08433ce..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test.rs +++ /dev/null @@ -1,147 +0,0 @@ -use core::ffi::{c_char, CStr}; -use core::ptr; -use std::thread; - -use pyo3_ffi::*; -use sequential::PyInit_sequential; - -static COMMAND: &'static str = c_str!( - " -from sequential import Id - -s = sum(int(Id()) for _ in range(12)) -" -); - -// Newtype to be able to pass it to another thread. -struct State(*mut PyThreadState); -unsafe impl Sync for State {} -unsafe impl Send for State {} - -#[test] -fn lets_go_fast() -> Result<(), String> { - unsafe { - let ret = PyImport_AppendInittab(c_str!("sequential").as_ptr(), Some(PyInit_sequential)); - if ret == -1 { - return Err("could not add module to inittab".into()); - } - - Py_Initialize(); - - let main_state = PyThreadState_Swap(ptr::null_mut()); - - const NULL: State = State(ptr::null_mut()); - let mut subs = [NULL; 12]; - - let config = PyInterpreterConfig { - use_main_obmalloc: 0, - allow_fork: 0, - allow_exec: 0, - allow_threads: 1, - allow_daemon_threads: 0, - check_multi_interp_extensions: 1, - gil: PyInterpreterConfig_OWN_GIL, - }; - - for State(state) in &mut subs { - let status = Py_NewInterpreterFromConfig(state, &config); - if PyStatus_IsError(status) == 1 { - let msg = if status.err_msg.is_null() { - "no error message".into() - } else { - CStr::from_ptr(status.err_msg).to_string_lossy() - }; - PyThreadState_Swap(main_state); - Py_FinalizeEx(); - return Err(format!("could not create new subinterpreter: {msg}")); - } - } - - PyThreadState_Swap(main_state); - - let main_state = PyEval_SaveThread(); // a PyInterpreterConfig with shared gil would deadlock otherwise - - let ints: Vec<_> = thread::scope(move |s| { - let mut handles = vec![]; - - for state in subs { - let handle = s.spawn(move || { - let state = state; - PyEval_RestoreThread(state.0); - - let ret = run_code(); - - Py_EndInterpreter(state.0); - ret - }); - - handles.push(handle); - } - - handles.into_iter().map(|h| h.join().unwrap()).collect() - }); - - PyEval_RestoreThread(main_state); - - let ret = Py_FinalizeEx(); - if ret == -1 { - return Err("could not finalize interpreter".into()); - } - - let mut sum: u64 = 0; - for i in ints { - let i = i?; - sum += i; - } - - assert_eq!(sum, (0..).take(12 * 12).sum()); - } - - Ok(()) -} - -unsafe fn fetch() -> String { - let err = PyErr_GetRaisedException(); - let err_repr = PyObject_Str(err); - if !err_repr.is_null() { - let mut size = 0; - let p = PyUnicode_AsUTF8AndSize(err_repr, &mut size); - if !p.is_null() { - let s = std::str::from_utf8_unchecked(std::slice::from_raw_parts( - p.cast::(), - size as usize, - )); - let s = String::from(s); - Py_DECREF(err_repr); - return s; - } - } - String::from("could not get error") -} - -fn run_code() -> Result { - unsafe { - let code_obj = - Py_CompileString(COMMAND.as_ptr(), c_str!("program").as_ptr(), Py_file_input); - if code_obj.is_null() { - return Err(fetch()); - } - let globals = PyDict_New(); - let res_ptr = PyEval_EvalCode(code_obj, globals, ptr::null_mut()); - Py_DECREF(code_obj); - if res_ptr.is_null() { - return Err(fetch()); - } else { - Py_DECREF(res_ptr); - } - let sum = PyDict_GetItemString(globals, c_str!("s").as_ptr()); /* borrowed reference */ - if sum.is_null() { - Py_DECREF(globals); - return Err("globals did not have `s`".into()); - } - let int = PyLong_AsUnsignedLongLong(sum) as u64; - - Py_DECREF(globals); - Ok(int) - } -} diff --git a/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test_.py b/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test_.py deleted file mode 100644 index a97522731c..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/sequential/tests/test_.py +++ /dev/null @@ -1,21 +0,0 @@ -import pytest -from sequential import Id - - -def test_make_some(): - for x in range(12): - i = Id() - assert x == int(i) - - -def test_args(): - with pytest.raises(TypeError, match="Id\\(\\) takes no arguments"): - Id(3, 4) - - -def test_cmp(): - a = Id() - b = Id() - assert a <= b - assert a < b - assert a == a diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/Cargo.toml b/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/Cargo.toml deleted file mode 100644 index 40afa2504c..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -authors = ["{{authors}}"] -name = "{{project-name}}" -version = "0.1.0" -edition = "2021" - -[lib] -name = "string_sum" -crate-type = ["cdylib"] - -[dependencies] -pyo3-ffi = { version = "{{PYO3_VERSION}}", features = ["extension-module"] } diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pre-script.rhai b/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pre-script.rhai deleted file mode 100644 index d3341677b1..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pre-script.rhai +++ /dev/null @@ -1,4 +0,0 @@ -variable::set("PYO3_VERSION", "0.19.2"); -file::rename(".template/Cargo.toml", "Cargo.toml"); -file::rename(".template/pyproject.toml", "pyproject.toml"); -file::delete(".template"); diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pyproject.toml b/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pyproject.toml deleted file mode 100644 index d27a1be983..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/.template/pyproject.toml +++ /dev/null @@ -1,10 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "{{project-name}}" -version = "0.1.0" - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/Cargo.toml b/vendor/pyo3/pyo3-ffi/examples/string-sum/Cargo.toml deleted file mode 100644 index 3c9893b3e8..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "string_sum" -version = "0.1.0" -edition = "2021" - -[lib] -name = "string_sum" -crate-type = ["cdylib"] - -[dependencies] -pyo3-ffi = { path = "../../", features = ["extension-module"] } - -[build-dependencies] -pyo3-build-config = { path = "../../../pyo3-build-config" } - -[workspace] diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/MANIFEST.in b/vendor/pyo3/pyo3-ffi/examples/string-sum/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/README.md b/vendor/pyo3/pyo3-ffi/examples/string-sum/README.md deleted file mode 100644 index 552d902096..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# string_sum - -A project built using only `pyo3_ffi`, without any of PyO3's safe api. - -## Building and Testing - -To build this package, first install `maturin`: - -```shell -pip install maturin -``` - -To build and test use `maturin develop`: - -```shell -pip install -r requirements-dev.txt -maturin develop -pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Copying this example - -Use [`cargo-generate`](https://crates.io/crates/cargo-generate): - -```bash -$ cargo install cargo-generate -$ cargo generate --git https://github.com/PyO3/pyo3 examples/string_sum -``` - -(`cargo generate` will take a little while to clone the PyO3 repo first; be patient when waiting for the command to run.) diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/build.rs b/vendor/pyo3/pyo3-ffi/examples/string-sum/build.rs deleted file mode 100644 index 0475124bb4..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - pyo3_build_config::use_pyo3_cfgs(); -} diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/cargo-generate.toml b/vendor/pyo3/pyo3-ffi/examples/string-sum/cargo-generate.toml deleted file mode 100644 index d750c4de7a..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/cargo-generate.toml +++ /dev/null @@ -1,5 +0,0 @@ -[template] -ignore = [".nox"] - -[hooks] -pre = [".template/pre-script.rhai"] diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/noxfile.py b/vendor/pyo3/pyo3-ffi/examples/string-sum/noxfile.py deleted file mode 100644 index 23d967cc69..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/noxfile.py +++ /dev/null @@ -1,8 +0,0 @@ -import nox - - -@nox.session -def python(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.install(".[dev]") - session.run("pytest") diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/pyproject.toml b/vendor/pyo3/pyo3-ffi/examples/string-sum/pyproject.toml deleted file mode 100644 index 4433178e3b..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/pyproject.toml +++ /dev/null @@ -1,19 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[project] -name = "string_sum" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = ["pytest"] diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/src/lib.rs b/vendor/pyo3/pyo3-ffi/examples/string-sum/src/lib.rs deleted file mode 100644 index 7af80bc08d..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/src/lib.rs +++ /dev/null @@ -1,133 +0,0 @@ -use std::os::raw::{c_char, c_long}; -use std::ptr; - -use pyo3_ffi::*; - -static mut MODULE_DEF: PyModuleDef = PyModuleDef { - m_base: PyModuleDef_HEAD_INIT, - m_name: c_str!("string_sum").as_ptr(), - m_doc: c_str!("A Python module written in Rust.").as_ptr(), - m_size: 0, - m_methods: unsafe { METHODS as *const [PyMethodDef] as *mut PyMethodDef }, - m_slots: std::ptr::null_mut(), - m_traverse: None, - m_clear: None, - m_free: None, -}; - -static mut METHODS: &[PyMethodDef] = &[ - PyMethodDef { - ml_name: c_str!("sum_as_string").as_ptr(), - ml_meth: PyMethodDefPointer { - PyCFunctionFast: sum_as_string, - }, - ml_flags: METH_FASTCALL, - ml_doc: c_str!("returns the sum of two integers as a string").as_ptr(), - }, - // A zeroed PyMethodDef to mark the end of the array. - PyMethodDef::zeroed(), -]; - -// The module initialization function, which must be named `PyInit_`. -#[allow(non_snake_case)] -#[no_mangle] -pub unsafe extern "C" fn PyInit_string_sum() -> *mut PyObject { - let module = PyModule_Create(ptr::addr_of_mut!(MODULE_DEF)); - if module.is_null() { - return module; - } - #[cfg(Py_GIL_DISABLED)] - { - if PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) < 0 { - Py_DECREF(module); - return std::ptr::null_mut(); - } - } - module -} - -/// A helper to parse function arguments -/// If we used PyO3's proc macros they'd handle all of this boilerplate for us :) -unsafe fn parse_arg_as_i32(obj: *mut PyObject, n_arg: usize) -> Option { - if PyLong_Check(obj) == 0 { - let msg = format!( - "sum_as_string expected an int for positional argument {}\0", - n_arg - ); - PyErr_SetString(PyExc_TypeError, msg.as_ptr().cast::()); - return None; - } - - // Let's keep the behaviour consistent on platforms where `c_long` is bigger than 32 bits. - // In particular, it is an i32 on Windows but i64 on most Linux systems - let mut overflow = 0; - let i_long: c_long = PyLong_AsLongAndOverflow(obj, &mut overflow); - - #[allow(irrefutable_let_patterns)] // some platforms have c_long equal to i32 - if overflow != 0 { - raise_overflowerror(obj); - None - } else if let Ok(i) = i_long.try_into() { - Some(i) - } else { - raise_overflowerror(obj); - None - } -} - -unsafe fn raise_overflowerror(obj: *mut PyObject) { - let obj_repr = PyObject_Str(obj); - if !obj_repr.is_null() { - let mut size = 0; - let p = PyUnicode_AsUTF8AndSize(obj_repr, &mut size); - if !p.is_null() { - let s = std::str::from_utf8_unchecked(std::slice::from_raw_parts( - p.cast::(), - size as usize, - )); - let msg = format!("cannot fit {} in 32 bits\0", s); - - PyErr_SetString(PyExc_OverflowError, msg.as_ptr().cast::()); - } - Py_DECREF(obj_repr); - } -} - -pub unsafe extern "C" fn sum_as_string( - _self: *mut PyObject, - args: *mut *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - if nargs != 2 { - PyErr_SetString( - PyExc_TypeError, - c_str!("sum_as_string expected 2 positional arguments").as_ptr(), - ); - return std::ptr::null_mut(); - } - - let (first, second) = (*args, *args.add(1)); - - let first = match parse_arg_as_i32(first, 1) { - Some(x) => x, - None => return std::ptr::null_mut(), - }; - let second = match parse_arg_as_i32(second, 2) { - Some(x) => x, - None => return std::ptr::null_mut(), - }; - - match first.checked_add(second) { - Some(sum) => { - let string = sum.to_string(); - PyUnicode_FromStringAndSize(string.as_ptr().cast::(), string.len() as isize) - } - None => { - PyErr_SetString( - PyExc_OverflowError, - c_str!("arguments too large to add").as_ptr(), - ); - std::ptr::null_mut() - } - } -} diff --git a/vendor/pyo3/pyo3-ffi/examples/string-sum/tests/test_.py b/vendor/pyo3/pyo3-ffi/examples/string-sum/tests/test_.py deleted file mode 100644 index a3e88a6344..0000000000 --- a/vendor/pyo3/pyo3-ffi/examples/string-sum/tests/test_.py +++ /dev/null @@ -1,41 +0,0 @@ -import pytest -from string_sum import sum_as_string - - -def test_sum(): - a, b = 12, 42 - - added = sum_as_string(a, b) - assert added == "54" - - -def test_err1(): - a, b = "abc", 42 - - with pytest.raises( - TypeError, match="sum_as_string expected an int for positional argument 1" - ): - sum_as_string(a, b) - - -def test_err2(): - a, b = 0, {} - - with pytest.raises( - TypeError, match="sum_as_string expected an int for positional argument 2" - ): - sum_as_string(a, b) - - -def test_overflow1(): - a, b = 0, 1 << 43 - - with pytest.raises(OverflowError, match="cannot fit 8796093022208 in 32 bits"): - sum_as_string(a, b) - - -def test_overflow2(): - a, b = 1 << 30, 1 << 30 - - with pytest.raises(OverflowError, match="arguments too large to add"): - sum_as_string(a, b) diff --git a/vendor/pyo3/pyo3-ffi/src/abstract_.rs b/vendor/pyo3/pyo3-ffi/src/abstract_.rs deleted file mode 100644 index ce6c9b9473..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/abstract_.rs +++ /dev/null @@ -1,332 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -#[inline] -#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h -pub unsafe fn PyObject_DelAttrString(o: *mut PyObject, attr_name: *const c_char) -> c_int { - PyObject_SetAttrString(o, attr_name, std::ptr::null_mut()) -} - -#[inline] -#[cfg(all(not(Py_3_13), not(PyPy)))] // CPython exposed as a function in 3.13, in object.h -pub unsafe fn PyObject_DelAttr(o: *mut PyObject, attr_name: *mut PyObject) -> c_int { - PyObject_SetAttr(o, attr_name, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(all( - not(PyPy), - any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10 - ))] - #[cfg_attr(PyPy, link_name = "PyPyObject_CallNoArgs")] - pub fn PyObject_CallNoArgs(func: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Call")] - pub fn PyObject_Call( - callable_object: *mut PyObject, - args: *mut PyObject, - kw: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallObject")] - pub fn PyObject_CallObject( - callable_object: *mut PyObject, - args: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunction")] - pub fn PyObject_CallFunction( - callable_object: *mut PyObject, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethod")] - pub fn PyObject_CallMethod( - o: *mut PyObject, - method: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallFunction_SizeT")] - pub fn _PyObject_CallFunction_SizeT( - callable_object: *mut PyObject, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallMethod_SizeT")] - pub fn _PyObject_CallMethod_SizeT( - o: *mut PyObject, - method: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunctionObjArgs")] - pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethodObjArgs")] - pub fn PyObject_CallMethodObjArgs( - o: *mut PyObject, - method: *mut PyObject, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Type")] - pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Size")] - pub fn PyObject_Size(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -pub unsafe fn PyObject_Length(o: *mut PyObject) -> Py_ssize_t { - PyObject_Size(o) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_GetItem")] - pub fn PyObject_GetItem(o: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetItem")] - pub fn PyObject_SetItem(o: *mut PyObject, key: *mut PyObject, v: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_DelItemString")] - pub fn PyObject_DelItemString(o: *mut PyObject, key: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_DelItem")] - pub fn PyObject_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_Format")] - pub fn PyObject_Format(obj: *mut PyObject, format_spec: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetIter")] - pub fn PyObject_GetIter(arg1: *mut PyObject) -> *mut PyObject; -} - -// Before 3.8 PyIter_Check was defined in CPython as a macro, -// but the implementation of that in PyO3 did not work, see -// https://github.com/PyO3/pyo3/pull/2914 -// -// This is a slow implementation which should function equivalently. -#[cfg(not(any(Py_3_8, PyPy)))] -#[inline] -pub unsafe fn PyIter_Check(o: *mut PyObject) -> c_int { - crate::PyObject_HasAttrString(crate::Py_TYPE(o).cast(), c_str!("__next__").as_ptr()) -} - -extern "C" { - #[cfg(any(Py_3_8, PyPy))] - #[cfg_attr(PyPy, link_name = "PyPyIter_Check")] - pub fn PyIter_Check(obj: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyIter_Next")] - pub fn PyIter_Next(arg1: *mut PyObject) -> *mut PyObject; - #[cfg(all(not(PyPy), Py_3_10))] - #[cfg_attr(PyPy, link_name = "PyPyIter_Send")] - pub fn PyIter_Send(iter: *mut PyObject, arg: *mut PyObject, presult: *mut *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPyNumber_Check")] - pub fn PyNumber_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Add")] - pub fn PyNumber_Add(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Subtract")] - pub fn PyNumber_Subtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Multiply")] - pub fn PyNumber_Multiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_MatrixMultiply")] - pub fn PyNumber_MatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_FloorDivide")] - pub fn PyNumber_FloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_TrueDivide")] - pub fn PyNumber_TrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Remainder")] - pub fn PyNumber_Remainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Divmod")] - pub fn PyNumber_Divmod(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Power")] - pub fn PyNumber_Power(o1: *mut PyObject, o2: *mut PyObject, o3: *mut PyObject) - -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Negative")] - pub fn PyNumber_Negative(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Positive")] - pub fn PyNumber_Positive(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Absolute")] - pub fn PyNumber_Absolute(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Invert")] - pub fn PyNumber_Invert(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Lshift")] - pub fn PyNumber_Lshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Rshift")] - pub fn PyNumber_Rshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_And")] - pub fn PyNumber_And(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Xor")] - pub fn PyNumber_Xor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Or")] - pub fn PyNumber_Or(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; -} - -// Defined as this macro in Python limited API, but relies on -// non-limited PyTypeObject. Don't expose this since it cannot be used. -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -#[inline] -pub unsafe fn PyIndex_Check(o: *mut PyObject) -> c_int { - let tp_as_number = (*Py_TYPE(o)).tp_as_number; - (!tp_as_number.is_null() && (*tp_as_number).nb_index.is_some()) as c_int -} - -extern "C" { - #[cfg(any(all(Py_3_8, Py_LIMITED_API), PyPy))] - #[link_name = "PyPyIndex_Check"] - pub fn PyIndex_Check(o: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyNumber_Index")] - pub fn PyNumber_Index(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_AsSsize_t")] - pub fn PyNumber_AsSsize_t(o: *mut PyObject, exc: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Long")] - pub fn PyNumber_Long(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_Float")] - pub fn PyNumber_Float(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAdd")] - pub fn PyNumber_InPlaceAdd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceSubtract")] - pub fn PyNumber_InPlaceSubtract(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMultiply")] - pub fn PyNumber_InPlaceMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceMatrixMultiply")] - pub fn PyNumber_InPlaceMatrixMultiply(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceFloorDivide")] - pub fn PyNumber_InPlaceFloorDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceTrueDivide")] - pub fn PyNumber_InPlaceTrueDivide(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRemainder")] - pub fn PyNumber_InPlaceRemainder(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlacePower")] - pub fn PyNumber_InPlacePower( - o1: *mut PyObject, - o2: *mut PyObject, - o3: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceLshift")] - pub fn PyNumber_InPlaceLshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceRshift")] - pub fn PyNumber_InPlaceRshift(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceAnd")] - pub fn PyNumber_InPlaceAnd(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceXor")] - pub fn PyNumber_InPlaceXor(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyNumber_InPlaceOr")] - pub fn PyNumber_InPlaceOr(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - pub fn PyNumber_ToBase(n: *mut PyObject, base: c_int) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPySequence_Check")] - pub fn PySequence_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_Size")] - pub fn PySequence_Size(o: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPySequence_Length"] - pub fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PySequence_Length(o: *mut PyObject) -> Py_ssize_t { - PySequence_Size(o) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySequence_Concat")] - pub fn PySequence_Concat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_Repeat")] - pub fn PySequence_Repeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_GetItem")] - pub fn PySequence_GetItem(o: *mut PyObject, i: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_GetSlice")] - pub fn PySequence_GetSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_SetItem")] - pub fn PySequence_SetItem(o: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_DelItem")] - pub fn PySequence_DelItem(o: *mut PyObject, i: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_SetSlice")] - pub fn PySequence_SetSlice( - o: *mut PyObject, - i1: Py_ssize_t, - i2: Py_ssize_t, - v: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_DelSlice")] - pub fn PySequence_DelSlice(o: *mut PyObject, i1: Py_ssize_t, i2: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySequence_Tuple")] - pub fn PySequence_Tuple(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_List")] - pub fn PySequence_List(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_Fast")] - pub fn PySequence_Fast(o: *mut PyObject, m: *const c_char) -> *mut PyObject; - // skipped PySequence_Fast_GET_SIZE - // skipped PySequence_Fast_GET_ITEM - // skipped PySequence_Fast_GET_ITEMS - pub fn PySequence_Count(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPySequence_Contains")] - pub fn PySequence_Contains(seq: *mut PyObject, ob: *mut PyObject) -> c_int; -} - -#[inline] -pub unsafe fn PySequence_In(o: *mut PyObject, value: *mut PyObject) -> c_int { - PySequence_Contains(o, value) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySequence_Index")] - pub fn PySequence_Index(o: *mut PyObject, value: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceConcat")] - pub fn PySequence_InPlaceConcat(o1: *mut PyObject, o2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySequence_InPlaceRepeat")] - pub fn PySequence_InPlaceRepeat(o: *mut PyObject, count: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Check")] - pub fn PyMapping_Check(o: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Size")] - pub fn PyMapping_Size(o: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPyMapping_Length"] - pub fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyMapping_Length(o: *mut PyObject) -> Py_ssize_t { - PyMapping_Size(o) -} - -#[inline] -pub unsafe fn PyMapping_DelItemString(o: *mut PyObject, key: *mut c_char) -> c_int { - PyObject_DelItemString(o, key) -} - -#[inline] -pub unsafe fn PyMapping_DelItem(o: *mut PyObject, key: *mut PyObject) -> c_int { - PyObject_DelItem(o, key) -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKeyString")] - pub fn PyMapping_HasKeyString(o: *mut PyObject, key: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_HasKey")] - pub fn PyMapping_HasKey(o: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Keys")] - pub fn PyMapping_Keys(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Values")] - pub fn PyMapping_Values(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_Items")] - pub fn PyMapping_Items(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_GetItemString")] - pub fn PyMapping_GetItemString(o: *mut PyObject, key: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMapping_SetItemString")] - pub fn PyMapping_SetItemString( - o: *mut PyObject, - key: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsInstance")] - pub fn PyObject_IsInstance(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsSubclass")] - pub fn PyObject_IsSubclass(object: *mut PyObject, typeorclass: *mut PyObject) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/bltinmodule.rs b/vendor/pyo3/pyo3-ffi/src/bltinmodule.rs deleted file mode 100644 index cd5be0439a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/bltinmodule.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::object::PyTypeObject; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyFilter_Type: PyTypeObject; - pub static mut PyMap_Type: PyTypeObject; - pub static mut PyZip_Type: PyTypeObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/boolobject.rs b/vendor/pyo3/pyo3-ffi/src/boolobject.rs deleted file mode 100644 index eec9da707a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/boolobject.rs +++ /dev/null @@ -1,60 +0,0 @@ -#[cfg(not(GraalPy))] -use crate::longobject::PyLongObject; -use crate::object::*; -use std::os::raw::{c_int, c_long}; -use std::ptr::addr_of_mut; - -#[inline] -pub unsafe fn PyBool_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyBool_Type)) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_FalseStruct")] - static mut _Py_FalseStruct: PyLongObject; - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_TrueStruct")] - static mut _Py_TrueStruct: PyLongObject; - - #[cfg(GraalPy)] - static mut _Py_FalseStructReference: *mut PyObject; - #[cfg(GraalPy)] - static mut _Py_TrueStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_False() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_FalseStruct) as *mut PyObject; - #[cfg(GraalPy)] - return _Py_FalseStructReference; -} - -#[inline] -pub unsafe fn Py_True() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_TrueStruct) as *mut PyObject; - #[cfg(GraalPy)] - return _Py_TrueStructReference; -} - -#[inline] -pub unsafe fn Py_IsTrue(x: *mut PyObject) -> c_int { - Py_Is(x, Py_True()) -} - -#[inline] -pub unsafe fn Py_IsFalse(x: *mut PyObject) -> c_int { - Py_Is(x, Py_False()) -} - -// skipped Py_RETURN_TRUE -// skipped Py_RETURN_FALSE - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBool_FromLong")] - pub fn PyBool_FromLong(arg1: c_long) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/bytearrayobject.rs b/vendor/pyo3/pyo3-ffi/src/bytearrayobject.rs deleted file mode 100644 index 24a97bcc31..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/bytearrayobject.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -#[repr(C)] -pub struct PyByteArrayObject { - pub ob_base: PyVarObject, - pub ob_alloc: Py_ssize_t, - pub ob_bytes: *mut c_char, - pub ob_start: *mut c_char, - #[cfg(Py_3_9)] - pub ob_exports: Py_ssize_t, - #[cfg(not(Py_3_9))] - pub ob_exports: c_int, -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -opaque_struct!(PyByteArrayObject); - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Type")] - pub static mut PyByteArray_Type: PyTypeObject; - - pub static mut PyByteArrayIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyByteArray_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyByteArray_Type)) -} - -#[inline] -pub unsafe fn PyByteArray_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyByteArray_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyByteArray_FromObject")] - pub fn PyByteArray_FromObject(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Concat")] - pub fn PyByteArray_Concat(a: *mut PyObject, b: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_FromStringAndSize")] - pub fn PyByteArray_FromStringAndSize(string: *const c_char, len: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Size")] - pub fn PyByteArray_Size(bytearray: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_AsString")] - pub fn PyByteArray_AsString(bytearray: *mut PyObject) -> *mut c_char; - #[cfg_attr(PyPy, link_name = "PyPyByteArray_Resize")] - pub fn PyByteArray_Resize(bytearray: *mut PyObject, len: Py_ssize_t) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/bytesobject.rs b/vendor/pyo3/pyo3-ffi/src/bytesobject.rs deleted file mode 100644 index 0fd327b6fc..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/bytesobject.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBytes_Type")] - pub static mut PyBytes_Type: PyTypeObject; - pub static mut PyBytesIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyBytes_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS) -} - -#[inline] -pub unsafe fn PyBytes_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyBytes_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromStringAndSize")] - pub fn PyBytes_FromStringAndSize(arg1: *const c_char, arg2: Py_ssize_t) -> *mut PyObject; - pub fn PyBytes_FromString(arg1: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromObject")] - pub fn PyBytes_FromObject(arg1: *mut PyObject) -> *mut PyObject; - // skipped PyBytes_FromFormatV - //#[cfg_attr(PyPy, link_name = "PyPyBytes_FromFormatV")] - //pub fn PyBytes_FromFormatV(arg1: *const c_char, arg2: va_list) - // -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_FromFormat")] - pub fn PyBytes_FromFormat(arg1: *const c_char, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_Size")] - pub fn PyBytes_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBytes_AsString")] - pub fn PyBytes_AsString(arg1: *mut PyObject) -> *mut c_char; - pub fn PyBytes_Repr(arg1: *mut PyObject, arg2: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_Concat")] - pub fn PyBytes_Concat(arg1: *mut *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyBytes_ConcatAndDel")] - pub fn PyBytes_ConcatAndDel(arg1: *mut *mut PyObject, arg2: *mut PyObject); - pub fn PyBytes_DecodeEscape( - arg1: *const c_char, - arg2: Py_ssize_t, - arg3: *const c_char, - arg4: Py_ssize_t, - arg5: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyBytes_AsStringAndSize")] - pub fn PyBytes_AsStringAndSize( - obj: *mut PyObject, - s: *mut *mut c_char, - len: *mut Py_ssize_t, - ) -> c_int; -} - -// skipped F_LJUST -// skipped F_SIGN -// skipped F_BLANK -// skipped F_ALT -// skipped F_ZERO diff --git a/vendor/pyo3/pyo3-ffi/src/ceval.rs b/vendor/pyo3/pyo3-ffi/src/ceval.rs deleted file mode 100644 index d1839a108c..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/ceval.rs +++ /dev/null @@ -1,139 +0,0 @@ -use crate::object::PyObject; -use crate::pystate::PyThreadState; -use std::os::raw::{c_char, c_int, c_void}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyEval_EvalCode")] - pub fn PyEval_EvalCode( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - - pub fn PyEval_EvalCodeEx( - co: *mut PyObject, - globals: *mut PyObject, - locals: *mut PyObject, - args: *const *mut PyObject, - argc: c_int, - kwds: *const *mut PyObject, - kwdc: c_int, - defs: *const *mut PyObject, - defc: c_int, - kwdefs: *mut PyObject, - closure: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallObjectWithKeywords")] - pub fn PyEval_CallObjectWithKeywords( - func: *mut PyObject, - obj: *mut PyObject, - kwargs: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(not(Py_3_13))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyEval_CallObject(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { - #[allow(deprecated)] - PyEval_CallObjectWithKeywords(func, arg, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallFunction")] - pub fn PyEval_CallFunction(obj: *mut PyObject, format: *const c_char, ...) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - #[cfg_attr(PyPy, link_name = "PyPyEval_CallMethod")] - pub fn PyEval_CallMethod( - obj: *mut PyObject, - methodname: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetBuiltins")] - pub fn PyEval_GetBuiltins() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetGlobals")] - pub fn PyEval_GetGlobals() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_GetLocals")] - pub fn PyEval_GetLocals() -> *mut PyObject; - pub fn PyEval_GetFrame() -> *mut crate::PyFrameObject; - #[cfg_attr(PyPy, link_name = "PyPy_AddPendingCall")] - pub fn Py_AddPendingCall( - func: Option c_int>, - arg: *mut c_void, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPy_MakePendingCalls")] - pub fn Py_MakePendingCalls() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPy_SetRecursionLimit")] - pub fn Py_SetRecursionLimit(arg1: c_int); - #[cfg_attr(PyPy, link_name = "PyPy_GetRecursionLimit")] - pub fn Py_GetRecursionLimit() -> c_int; - fn _Py_CheckRecursiveCall(_where: *mut c_char) -> c_int; -} - -extern "C" { - #[cfg(Py_3_9)] - #[cfg_attr(PyPy, link_name = "PyPy_EnterRecursiveCall")] - pub fn Py_EnterRecursiveCall(arg1: *const c_char) -> c_int; - #[cfg(Py_3_9)] - #[cfg_attr(PyPy, link_name = "PyPy_LeaveRecursiveCall")] - pub fn Py_LeaveRecursiveCall(); -} - -extern "C" { - pub fn PyEval_GetFuncName(arg1: *mut PyObject) -> *const c_char; - pub fn PyEval_GetFuncDesc(arg1: *mut PyObject) -> *const c_char; - pub fn PyEval_GetCallStats(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyEval_EvalFrame(arg1: *mut crate::PyFrameObject) -> *mut PyObject; - pub fn PyEval_EvalFrameEx(f: *mut crate::PyFrameObject, exc: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyEval_SaveThread")] - pub fn PyEval_SaveThread() -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyEval_RestoreThread")] - pub fn PyEval_RestoreThread(arg1: *mut PyThreadState); -} - -extern "C" { - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "PyPyEval_ThreadsInitialized")] - #[cfg_attr( - Py_3_9, - deprecated( - note = "Deprecated in Python 3.9, this function always returns true in Python 3.7 or newer." - ) - )] - pub fn PyEval_ThreadsInitialized() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyEval_InitThreads")] - #[cfg_attr( - Py_3_9, - deprecated( - note = "Deprecated in Python 3.9, this function does nothing in Python 3.7 or newer." - ) - )] - pub fn PyEval_InitThreads(); - pub fn PyEval_AcquireLock(); - pub fn PyEval_ReleaseLock(); - #[cfg_attr(PyPy, link_name = "PyPyEval_AcquireThread")] - pub fn PyEval_AcquireThread(tstate: *mut PyThreadState); - #[cfg_attr(PyPy, link_name = "PyPyEval_ReleaseThread")] - pub fn PyEval_ReleaseThread(tstate: *mut PyThreadState); - #[cfg(not(Py_3_8))] - pub fn PyEval_ReInitThreads(); -} - -// skipped Py_BEGIN_ALLOW_THREADS -// skipped Py_BLOCK_THREADS -// skipped Py_UNBLOCK_THREADS -// skipped Py_END_ALLOW_THREADS -// skipped FVC_MASK -// skipped FVC_NONE -// skipped FVC_STR -// skipped FVC_REPR -// skipped FVC_ASCII -// skipped FVS_MASK -// skipped FVS_HAVE_SPEC diff --git a/vendor/pyo3/pyo3-ffi/src/code.rs b/vendor/pyo3/pyo3-ffi/src/code.rs deleted file mode 100644 index d28f68cded..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/code.rs +++ /dev/null @@ -1,4 +0,0 @@ -// This header doesn't exist in CPython, but Include/cpython/code.h does. We add -// this here so that PyCodeObject has a definition under the limited API. - -opaque_struct!(PyCodeObject); diff --git a/vendor/pyo3/pyo3-ffi/src/codecs.rs b/vendor/pyo3/pyo3-ffi/src/codecs.rs deleted file mode 100644 index 2fd214cbbf..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/codecs.rs +++ /dev/null @@ -1,58 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn PyCodec_Register(search_function: *mut PyObject) -> c_int; - #[cfg(Py_3_10)] - #[cfg(not(PyPy))] - pub fn PyCodec_Unregister(search_function: *mut PyObject) -> c_int; - // skipped non-limited _PyCodec_Lookup from Include/codecs.h - // skipped non-limited _PyCodec_Forget from Include/codecs.h - pub fn PyCodec_KnownEncoding(encoding: *const c_char) -> c_int; - pub fn PyCodec_Encode( - object: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_Decode( - object: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - // skipped non-limited _PyCodec_LookupTextEncoding from Include/codecs.h - // skipped non-limited _PyCodec_EncodeText from Include/codecs.h - // skipped non-limited _PyCodec_DecodeText from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalDecoder from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalEncoder from Include/codecs.h - pub fn PyCodec_Encoder(encoding: *const c_char) -> *mut PyObject; - pub fn PyCodec_Decoder(encoding: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCodec_IncrementalEncoder")] - pub fn PyCodec_IncrementalEncoder( - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCodec_IncrementalDecoder")] - pub fn PyCodec_IncrementalDecoder( - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_StreamReader( - encoding: *const c_char, - stream: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_StreamWriter( - encoding: *const c_char, - stream: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyCodec_RegisterError(name: *const c_char, error: *mut PyObject) -> c_int; - pub fn PyCodec_LookupError(name: *const c_char) -> *mut PyObject; - pub fn PyCodec_StrictErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_IgnoreErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_ReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_XMLCharRefReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - pub fn PyCodec_BackslashReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - // skipped non-limited PyCodec_NameReplaceErrors from Include/codecs.h - // skipped non-limited Py_hexdigits from Include/codecs.h -} diff --git a/vendor/pyo3/pyo3-ffi/src/compat/mod.rs b/vendor/pyo3/pyo3-ffi/src/compat/mod.rs deleted file mode 100644 index 11f2912848..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/compat/mod.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! C API Compatibility Shims -//! -//! Some CPython C API functions added in recent versions of Python are -//! inherently safer to use than older C API constructs. This module -//! exposes functions available on all Python versions that wrap the -//! old C API on old Python versions and wrap the function directly -//! on newer Python versions. - -// Unless otherwise noted, the compatibility shims are adapted from -// the pythoncapi-compat project: https://github.com/python/pythoncapi-compat - -/// Internal helper macro which defines compatibility shims for C API functions, deferring to a -/// re-export when that's available. -macro_rules! compat_function { - ( - originally_defined_for($cfg:meta); - - $(#[$attrs:meta])* - pub unsafe fn $name:ident($($arg_names:ident: $arg_types:ty),* $(,)?) -> $ret:ty $body:block - ) => { - // Define as a standalone function under docsrs cfg so that this shows as a unique function in the docs, - // not a re-export (the re-export has the wrong visibility) - #[cfg(any(docsrs, not($cfg)))] - #[cfg_attr(docsrs, doc(cfg(all())))] - $(#[$attrs])* - pub unsafe fn $name( - $($arg_names: $arg_types,)* - ) -> $ret $body - - #[cfg(all($cfg, not(docsrs)))] - pub use $crate::$name; - - #[cfg(test)] - paste::paste! { - // Test that the compat function does not overlap with the original function. If the - // cfgs line up, then the the two glob imports will resolve to the same item via the - // re-export. If the cfgs mismatch, then the use of $name will be ambiguous in cases - // where the function is defined twice, and the test will fail to compile. - #[allow(unused_imports)] - mod [] { - use $crate::*; - use $crate::compat::*; - - #[test] - fn test_export() { - let _ = $name; - } - } - } - }; -} - -mod py_3_10; -mod py_3_13; -mod py_3_9; - -pub use self::py_3_10::*; -pub use self::py_3_13::*; -pub use self::py_3_9::*; diff --git a/vendor/pyo3/pyo3-ffi/src/compat/py_3_10.rs b/vendor/pyo3/pyo3-ffi/src/compat/py_3_10.rs deleted file mode 100644 index c6e8c2cb5c..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/compat/py_3_10.rs +++ /dev/null @@ -1,19 +0,0 @@ -compat_function!( - originally_defined_for(Py_3_10); - - #[inline] - pub unsafe fn Py_NewRef(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::Py_INCREF(obj); - obj - } -); - -compat_function!( - originally_defined_for(Py_3_10); - - #[inline] - pub unsafe fn Py_XNewRef(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::Py_XINCREF(obj); - obj - } -); diff --git a/vendor/pyo3/pyo3-ffi/src/compat/py_3_13.rs b/vendor/pyo3/pyo3-ffi/src/compat/py_3_13.rs deleted file mode 100644 index 59289cb76a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/compat/py_3_13.rs +++ /dev/null @@ -1,106 +0,0 @@ -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyDict_GetItemRef( - dp: *mut crate::PyObject, - key: *mut crate::PyObject, - result: *mut *mut crate::PyObject, - ) -> std::os::raw::c_int { - use crate::{compat::Py_NewRef, PyDict_GetItemWithError, PyErr_Occurred}; - - let item = PyDict_GetItemWithError(dp, key); - if !item.is_null() { - *result = Py_NewRef(item); - return 1; // found - } - *result = std::ptr::null_mut(); - if PyErr_Occurred().is_null() { - return 0; // not found - } - -1 - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyList_GetItemRef( - arg1: *mut crate::PyObject, - arg2: crate::Py_ssize_t, - ) -> *mut crate::PyObject { - use crate::{PyList_GetItem, Py_XINCREF}; - - let item = PyList_GetItem(arg1, arg2); - Py_XINCREF(item); - item - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyImport_AddModuleRef( - name: *const std::os::raw::c_char, - ) -> *mut crate::PyObject { - use crate::{compat::Py_XNewRef, PyImport_AddModule}; - - Py_XNewRef(PyImport_AddModule(name)) - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyWeakref_GetRef( - reference: *mut crate::PyObject, - pobj: *mut *mut crate::PyObject, - ) -> std::os::raw::c_int { - use crate::{ - compat::Py_NewRef, PyErr_SetString, PyExc_TypeError, PyWeakref_Check, - PyWeakref_GetObject, Py_None, - }; - - if !reference.is_null() && PyWeakref_Check(reference) == 0 { - *pobj = std::ptr::null_mut(); - PyErr_SetString(PyExc_TypeError, c_str!("expected a weakref").as_ptr()); - return -1; - } - let obj = PyWeakref_GetObject(reference); - if obj.is_null() { - // SystemError if reference is NULL - *pobj = std::ptr::null_mut(); - return -1; - } - if obj == Py_None() { - *pobj = std::ptr::null_mut(); - return 0; - } - *pobj = Py_NewRef(obj); - 1 - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyList_Extend( - list: *mut crate::PyObject, - iterable: *mut crate::PyObject, - ) -> std::os::raw::c_int { - crate::PyList_SetSlice(list, crate::PY_SSIZE_T_MAX, crate::PY_SSIZE_T_MAX, iterable) - } -); - -compat_function!( - originally_defined_for(Py_3_13); - - #[inline] - pub unsafe fn PyList_Clear(list: *mut crate::PyObject) -> std::os::raw::c_int { - crate::PyList_SetSlice(list, 0, crate::PY_SSIZE_T_MAX, std::ptr::null_mut()) - } -); diff --git a/vendor/pyo3/pyo3-ffi/src/compat/py_3_9.rs b/vendor/pyo3/pyo3-ffi/src/compat/py_3_9.rs deleted file mode 100644 index 285f2b2ae7..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/compat/py_3_9.rs +++ /dev/null @@ -1,21 +0,0 @@ -compat_function!( - originally_defined_for(all( - not(PyPy), - not(GraalPy), - any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10 - )); - - #[inline] - pub unsafe fn PyObject_CallNoArgs(obj: *mut crate::PyObject) -> *mut crate::PyObject { - crate::PyObject_CallObject(obj, std::ptr::null_mut()) - } -); - -compat_function!( - originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy)))); - - #[inline] - pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject { - crate::PyObject_CallMethodObjArgs(obj, name, std::ptr::null_mut::()) - } -); diff --git a/vendor/pyo3/pyo3-ffi/src/compile.rs b/vendor/pyo3/pyo3-ffi/src/compile.rs deleted file mode 100644 index 189a1a8b8a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/compile.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::os::raw::c_int; - -pub const Py_single_input: c_int = 256; -pub const Py_file_input: c_int = 257; -pub const Py_eval_input: c_int = 258; -#[cfg(Py_3_8)] -pub const Py_func_type_input: c_int = 345; - -#[cfg(Py_3_9)] -pub const Py_fstring_input: c_int = 800; diff --git a/vendor/pyo3/pyo3-ffi/src/complexobject.rs b/vendor/pyo3/pyo3-ffi/src/complexobject.rs deleted file mode 100644 index 283bacf6e8..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/complexobject.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_double, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyComplex_Type")] - pub static mut PyComplex_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyComplex_Type)) -} - -#[inline] -pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int { - Py_IS_TYPE(op, addr_of_mut!(PyComplex_Type)) -} - -extern "C" { - // skipped non-limited PyComplex_FromCComplex - #[cfg_attr(PyPy, link_name = "PyPyComplex_FromDoubles")] - pub fn PyComplex_FromDoubles(real: c_double, imag: c_double) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyComplex_RealAsDouble")] - pub fn PyComplex_RealAsDouble(op: *mut PyObject) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyComplex_ImagAsDouble")] - pub fn PyComplex_ImagAsDouble(op: *mut PyObject) -> c_double; -} diff --git a/vendor/pyo3/pyo3-ffi/src/context.rs b/vendor/pyo3/pyo3-ffi/src/context.rs deleted file mode 100644 index 210d6e21f0..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/context.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::object::{PyObject, PyTypeObject, Py_TYPE}; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -extern "C" { - pub static mut PyContext_Type: PyTypeObject; - // skipped non-limited opaque PyContext - pub static mut PyContextVar_Type: PyTypeObject; - // skipped non-limited opaque PyContextVar - pub static mut PyContextToken_Type: PyTypeObject; - // skipped non-limited opaque PyContextToken -} - -#[inline] -pub unsafe fn PyContext_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContext_Type)) as c_int -} - -#[inline] -pub unsafe fn PyContextVar_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContextVar_Type)) as c_int -} - -#[inline] -pub unsafe fn PyContextToken_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyContextToken_Type)) as c_int -} - -extern "C" { - pub fn PyContext_New() -> *mut PyObject; - pub fn PyContext_Copy(ctx: *mut PyObject) -> *mut PyObject; - pub fn PyContext_CopyCurrent() -> *mut PyObject; - - pub fn PyContext_Enter(ctx: *mut PyObject) -> c_int; - pub fn PyContext_Exit(ctx: *mut PyObject) -> c_int; - - pub fn PyContextVar_New(name: *const c_char, def: *mut PyObject) -> *mut PyObject; - pub fn PyContextVar_Get( - var: *mut PyObject, - default_value: *mut PyObject, - value: *mut *mut PyObject, - ) -> c_int; - pub fn PyContextVar_Set(var: *mut PyObject, value: *mut PyObject) -> *mut PyObject; - pub fn PyContextVar_Reset(var: *mut PyObject, token: *mut PyObject) -> c_int; - // skipped non-limited _PyContext_NewHamtForTests -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/abstract_.rs b/vendor/pyo3/pyo3-ffi/src/cpython/abstract_.rs deleted file mode 100644 index 477ad02b74..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/abstract_.rs +++ /dev/null @@ -1,334 +0,0 @@ -use crate::{PyObject, Py_ssize_t}; -#[cfg(not(all(Py_3_11, GraalPy)))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -#[cfg(not(Py_3_11))] -use crate::Py_buffer; - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -use crate::{ - vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check, - PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL, -}; -#[cfg(Py_3_8)] -use libc::size_t; - -extern "C" { - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _PyStack_AsDict(values: *const *mut PyObject, kwnames: *mut PyObject) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -const _PY_FASTCALL_SMALL_STACK: size_t = 5; - -extern "C" { - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _Py_CheckFunctionResult( - tstate: *mut PyThreadState, - callable: *mut PyObject, - result: *mut PyObject, - where_: *const c_char, - ) -> *mut PyObject; - - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _PyObject_MakeTpCall( - tstate: *mut PyThreadState, - callable: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, - keywords: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(Py_3_8)] -pub const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = - 1 << (8 * std::mem::size_of::() as size_t - 1); - -#[cfg(Py_3_8)] -#[inline(always)] -pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t { - let n = n & !PY_VECTORCALL_ARGUMENTS_OFFSET; - n.try_into().expect("cannot fail due to mask") -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option { - assert!(!callable.is_null()); - let tp = crate::Py_TYPE(callable); - if PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL) == 0 { - return None; - } - assert!(PyCallable_Check(callable) > 0); - let offset = (*tp).tp_vectorcall_offset; - assert!(offset > 0); - let ptr = callable.cast::().offset(offset).cast(); - *ptr -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_VectorcallTstate( - tstate: *mut PyThreadState, - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, -) -> *mut PyObject { - assert!(kwnames.is_null() || PyTuple_Check(kwnames) > 0); - assert!(!args.is_null() || PyVectorcall_NARGS(nargsf) == 0); - - match PyVectorcall_Function(callable) { - None => { - let nargs = PyVectorcall_NARGS(nargsf); - _PyObject_MakeTpCall(tstate, callable, args, nargs, kwnames) - } - Some(func) => { - let res = func(callable, args, nargsf, kwnames); - _Py_CheckFunctionResult(tstate, callable, res, std::ptr::null_mut()) - } - } -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_Vectorcall( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, -) -> *mut PyObject { - _PyObject_VectorcallTstate(PyThreadState_GET(), callable, args, nargsf, kwnames) -} - -extern "C" { - #[cfg(all(PyPy, Py_3_8))] - #[cfg_attr(not(Py_3_9), link_name = "_PyPyObject_Vectorcall")] - #[cfg_attr(Py_3_9, link_name = "PyPyObject_Vectorcall")] - pub fn PyObject_Vectorcall( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_8)] - #[cfg_attr( - all(not(any(PyPy, GraalPy)), not(Py_3_9)), - link_name = "_PyObject_VectorcallDict" - )] - #[cfg_attr(all(PyPy, not(Py_3_9)), link_name = "_PyPyObject_VectorcallDict")] - #[cfg_attr(all(PyPy, Py_3_9), link_name = "PyPyObject_VectorcallDict")] - pub fn PyObject_VectorcallDict( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwdict: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_8)] - #[cfg_attr(not(any(Py_3_9, PyPy)), link_name = "_PyVectorcall_Call")] - #[cfg_attr(PyPy, link_name = "PyPyVectorcall_Call")] - pub fn PyVectorcall_Call( - callable: *mut PyObject, - tuple: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCallTstate( - tstate: *mut PyThreadState, - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_VectorcallTstate(tstate, func, args, nargs as size_t, std::ptr::null_mut()) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCall( - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject { - _PyObject_VectorcallTstate( - PyThreadState_GET(), - func, - std::ptr::null_mut(), - 0, - std::ptr::null_mut(), - ) -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "_PyPyObject_CallNoArg"] - pub fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { - assert!(!arg.is_null()); - let args_array = [std::ptr::null_mut(), arg]; - let args = args_array.as_ptr().offset(1); // For PY_VECTORCALL_ARGUMENTS_OFFSET - let tstate = PyThreadState_GET(); - let nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - _PyObject_VectorcallTstate(tstate, func, args, nargsf, std::ptr::null_mut()) -} - -extern "C" { - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] - pub fn PyObject_VectorcallMethod( - name: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallMethodNoArgs( - self_: *mut PyObject, - name: *mut PyObject, -) -> *mut PyObject { - PyObject_VectorcallMethod( - name, - &self_, - 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, - std::ptr::null_mut(), - ) -} - -#[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn PyObject_CallMethodOneArg( - self_: *mut PyObject, - name: *mut PyObject, - arg: *mut PyObject, -) -> *mut PyObject { - let args = [self_, arg]; - assert!(!arg.is_null()); - PyObject_VectorcallMethod( - name, - args.as_ptr(), - 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, - std::ptr::null_mut(), - ) -} - -// skipped _PyObject_VectorcallMethodId -// skipped _PyObject_CallMethodIdNoArgs -// skipped _PyObject_CallMethodIdOneArg - -// skipped _PyObject_HasLen - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_LengthHint")] - pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t; - - #[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11 - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] - pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int; -} - -#[cfg(not(any(Py_3_9, PyPy)))] -#[inline] -pub unsafe fn PyObject_CheckBuffer(o: *mut PyObject) -> c_int { - let tp_as_buffer = (*crate::Py_TYPE(o)).tp_as_buffer; - (!tp_as_buffer.is_null() && (*tp_as_buffer).bf_getbuffer.is_some()) as c_int -} - -#[cfg(not(Py_3_11))] // moved to src/buffer.rs from 3.11 -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")] - pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")] - pub fn PyBuffer_GetPointer( - view: *mut Py_buffer, - indices: *mut Py_ssize_t, - ) -> *mut std::os::raw::c_void; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")] - pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")] - pub fn PyBuffer_ToContiguous( - buf: *mut std::os::raw::c_void, - view: *mut Py_buffer, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")] - pub fn PyBuffer_FromContiguous( - view: *mut Py_buffer, - buf: *mut std::os::raw::c_void, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")] - pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int; - pub fn PyBuffer_FillContiguousStrides( - ndims: c_int, - shape: *mut Py_ssize_t, - strides: *mut Py_ssize_t, - itemsize: c_int, - fort: c_char, - ); - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")] - pub fn PyBuffer_FillInfo( - view: *mut Py_buffer, - o: *mut PyObject, - buf: *mut std::os::raw::c_void, - len: Py_ssize_t, - readonly: c_int, - flags: c_int, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")] - pub fn PyBuffer_Release(view: *mut Py_buffer); -} - -// PyIter_Check defined in ffi/abstract_.rs -// PyIndex_Check defined in ffi/abstract_.rs -// Not defined here because this file is not compiled under the -// limited API, but the macros need to be defined for 3.6, 3.7 which -// predate the limited API changes. - -// skipped PySequence_ITEM - -pub const PY_ITERSEARCH_COUNT: c_int = 1; -pub const PY_ITERSEARCH_INDEX: c_int = 2; -pub const PY_ITERSEARCH_CONTAINS: c_int = 3; - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PySequence_IterSearch( - seq: *mut PyObject, - obj: *mut PyObject, - operation: c_int, - ) -> Py_ssize_t; -} - -// skipped _PyObject_RealIsInstance -// skipped _PyObject_RealIsSubclass - -// skipped _PySequence_BytesToCharpArray - -// skipped _Py_FreeCharPArray - -// skipped _Py_add_one_to_index_F -// skipped _Py_add_one_to_index_C - -// skipped _Py_convert_optional_to_ssize_t - -// skipped _PyNumber_Index(*mut PyObject o) diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/bytesobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/bytesobject.rs deleted file mode 100644 index 306702de25..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/bytesobject.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::object::*; -use crate::Py_ssize_t; -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -#[repr(C)] -pub struct PyBytesObject { - pub ob_base: PyVarObject, - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated in Python 3.11 and will be removed in a future version.") - )] - pub ob_shash: crate::Py_hash_t, - pub ob_sval: [c_char; 1], -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -opaque_struct!(PyBytesObject); - -extern "C" { - #[cfg_attr(PyPy, link_name = "_PyPyBytes_Resize")] - pub fn _PyBytes_Resize(bytes: *mut *mut PyObject, newsize: Py_ssize_t) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/ceval.rs b/vendor/pyo3/pyo3-ffi/src/cpython/ceval.rs deleted file mode 100644 index 6df10627d2..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/ceval.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::cpython::pystate::Py_tracefunc; -use crate::object::{freefunc, PyObject}; -use std::os::raw::c_int; - -extern "C" { - // skipped non-limited _PyEval_CallTracing - - #[cfg(not(Py_3_11))] - pub fn _PyEval_EvalFrameDefault(arg1: *mut crate::PyFrameObject, exc: c_int) -> *mut PyObject; - - #[cfg(Py_3_11)] - pub fn _PyEval_EvalFrameDefault( - tstate: *mut crate::PyThreadState, - frame: *mut crate::_PyInterpreterFrame, - exc: c_int, - ) -> *mut crate::PyObject; - - pub fn _PyEval_RequestCodeExtraIndex(func: freefunc) -> c_int; - pub fn PyEval_SetProfile(trace_func: Option, arg1: *mut PyObject); - pub fn PyEval_SetTrace(trace_func: Option, arg1: *mut PyObject); -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/code.rs b/vendor/pyo3/pyo3-ffi/src/cpython/code.rs deleted file mode 100644 index 230096ca37..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/code.rs +++ /dev/null @@ -1,336 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; - -#[allow(unused_imports)] -use std::os::raw::{c_char, c_int, c_short, c_uchar, c_void}; -#[cfg(not(any(PyPy, GraalPy)))] -use std::ptr::addr_of_mut; - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy)), not(Py_3_11)))] -opaque_struct!(_PyOpcache); - -#[cfg(Py_3_12)] -pub const _PY_MONITORING_LOCAL_EVENTS: usize = 10; -#[cfg(Py_3_12)] -pub const _PY_MONITORING_UNGROUPED_EVENTS: usize = 15; -#[cfg(Py_3_12)] -pub const _PY_MONITORING_EVENTS: usize = 17; - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _Py_LocalMonitors { - pub tools: [u8; if cfg!(Py_3_13) { - _PY_MONITORING_LOCAL_EVENTS - } else { - _PY_MONITORING_UNGROUPED_EVENTS - }], -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _Py_GlobalMonitors { - pub tools: [u8; _PY_MONITORING_UNGROUPED_EVENTS], -} - -// skipped _Py_CODEUNIT - -// skipped _Py_OPCODE -// skipped _Py_OPARG - -// skipped _py_make_codeunit - -// skipped _py_set_opcode - -// skipped _Py_MAKE_CODEUNIT -// skipped _Py_SET_OPCODE - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoCached { - pub _co_code: *mut PyObject, - pub _co_varnames: *mut PyObject, - pub _co_cellvars: *mut PyObject, - pub _co_freevars: *mut PyObject, -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoLineInstrumentationData { - pub original_opcode: u8, - pub line_delta: i8, -} - -#[cfg(Py_3_12)] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCoMonitoringData { - pub local_monitors: _Py_LocalMonitors, - pub active_monitors: _Py_LocalMonitors, - pub tools: *mut u8, - pub lines: *mut _PyCoLineInstrumentationData, - pub line_tools: *mut u8, - pub per_instruction_opcodes: *mut u8, - pub per_instruction_tools: *mut u8, -} - -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_7)))] -opaque_struct!(PyCodeObject); - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_7, not(Py_3_8)))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_argcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_nlocals: c_int, - pub co_stacksize: c_int, - pub co_flags: c_int, - pub co_firstlineno: c_int, - pub co_code: *mut PyObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_varnames: *mut PyObject, - pub co_freevars: *mut PyObject, - pub co_cellvars: *mut PyObject, - pub co_cell2arg: *mut Py_ssize_t, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - pub co_lnotab: *mut PyObject, - pub co_zombieframe: *mut c_void, - pub co_weakreflist: *mut PyObject, - pub co_extra: *mut c_void, -} - -#[cfg(Py_3_13)] -opaque_struct!(_PyExecutorArray); - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_8, not(Py_3_11)))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_argcount: c_int, - pub co_posonlyargcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_nlocals: c_int, - pub co_stacksize: c_int, - pub co_flags: c_int, - pub co_firstlineno: c_int, - pub co_code: *mut PyObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_varnames: *mut PyObject, - pub co_freevars: *mut PyObject, - pub co_cellvars: *mut PyObject, - pub co_cell2arg: *mut Py_ssize_t, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - #[cfg(not(Py_3_10))] - pub co_lnotab: *mut PyObject, - #[cfg(Py_3_10)] - pub co_linetable: *mut PyObject, - pub co_zombieframe: *mut c_void, - pub co_weakreflist: *mut PyObject, - pub co_extra: *mut c_void, - pub co_opcache_map: *mut c_uchar, - pub co_opcache: *mut _PyOpcache, - pub co_opcache_flag: c_int, - pub co_opcache_size: c_uchar, -} - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_11))] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyVarObject, - pub co_consts: *mut PyObject, - pub co_names: *mut PyObject, - pub co_exceptiontable: *mut PyObject, - pub co_flags: c_int, - #[cfg(not(Py_3_12))] - pub co_warmup: c_int, - - pub co_argcount: c_int, - pub co_posonlyargcount: c_int, - pub co_kwonlyargcount: c_int, - pub co_stacksize: c_int, - pub co_firstlineno: c_int, - - pub co_nlocalsplus: c_int, - #[cfg(Py_3_12)] - pub co_framesize: c_int, - pub co_nlocals: c_int, - #[cfg(not(Py_3_12))] - pub co_nplaincellvars: c_int, - pub co_ncellvars: c_int, - pub co_nfreevars: c_int, - #[cfg(Py_3_12)] - pub co_version: u32, - - pub co_localsplusnames: *mut PyObject, - pub co_localspluskinds: *mut PyObject, - pub co_filename: *mut PyObject, - pub co_name: *mut PyObject, - pub co_qualname: *mut PyObject, - pub co_linetable: *mut PyObject, - pub co_weakreflist: *mut PyObject, - #[cfg(not(Py_3_12))] - pub _co_code: *mut PyObject, - #[cfg(not(Py_3_12))] - pub _co_linearray: *mut c_char, - #[cfg(Py_3_13)] - pub co_executors: *mut _PyExecutorArray, - #[cfg(Py_3_12)] - pub _co_cached: *mut _PyCoCached, - #[cfg(Py_3_12)] - pub _co_instrumentation_version: u64, - #[cfg(Py_3_12)] - pub _co_monitoring: *mut _PyCoMonitoringData, - pub _co_firsttraceable: c_int, - pub co_extra: *mut c_void, - pub co_code_adaptive: [c_char; 1], -} - -#[cfg(PyPy)] -#[repr(C)] -pub struct PyCodeObject { - pub ob_base: PyObject, - pub co_name: *mut PyObject, - pub co_filename: *mut PyObject, - pub co_argcount: c_int, - pub co_flags: c_int, -} - -/* Masks for co_flags */ -pub const CO_OPTIMIZED: c_int = 0x0001; -pub const CO_NEWLOCALS: c_int = 0x0002; -pub const CO_VARARGS: c_int = 0x0004; -pub const CO_VARKEYWORDS: c_int = 0x0008; -pub const CO_NESTED: c_int = 0x0010; -pub const CO_GENERATOR: c_int = 0x0020; -/* The CO_NOFREE flag is set if there are no free or cell variables. - This information is redundant, but it allows a single flag test - to determine whether there is any extra work to be done when the - call frame it setup. -*/ -pub const CO_NOFREE: c_int = 0x0040; -/* The CO_COROUTINE flag is set for coroutine functions (defined with -``async def`` keywords) */ -pub const CO_COROUTINE: c_int = 0x0080; -pub const CO_ITERABLE_COROUTINE: c_int = 0x0100; -pub const CO_ASYNC_GENERATOR: c_int = 0x0200; - -pub const CO_FUTURE_DIVISION: c_int = 0x2000; -pub const CO_FUTURE_ABSOLUTE_IMPORT: c_int = 0x4000; /* do absolute imports by default */ -pub const CO_FUTURE_WITH_STATEMENT: c_int = 0x8000; -pub const CO_FUTURE_PRINT_FUNCTION: c_int = 0x1_0000; -pub const CO_FUTURE_UNICODE_LITERALS: c_int = 0x2_0000; - -pub const CO_FUTURE_BARRY_AS_BDFL: c_int = 0x4_0000; -pub const CO_FUTURE_GENERATOR_STOP: c_int = 0x8_0000; -// skipped CO_FUTURE_ANNOTATIONS -// skipped CO_CELL_NOT_AN_ARG - -pub const CO_MAXBLOCKS: usize = 20; - -#[cfg(not(any(PyPy, GraalPy)))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCode_Type: PyTypeObject; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCode_Type)) as c_int -} - -#[inline] -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10, not(Py_3_11)))] -pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t { - crate::PyTuple_GET_SIZE((*op).co_freevars) -} - -#[inline] -#[cfg(all(not(Py_3_10), Py_3_11, not(any(PyPy, GraalPy))))] -pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> c_int { - (*op).co_nfreevars -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyCode_Check"] - pub fn PyCode_Check(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyCode_GetNumFree"] - pub fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t; -} - -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyCode_New")] - pub fn PyCode_New( - argcount: c_int, - kwonlyargcount: c_int, - nlocals: c_int, - stacksize: c_int, - flags: c_int, - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - varnames: *mut PyObject, - freevars: *mut PyObject, - cellvars: *mut PyObject, - filename: *mut PyObject, - name: *mut PyObject, - firstlineno: c_int, - lnotab: *mut PyObject, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - #[cfg(Py_3_8)] - pub fn PyCode_NewWithPosOnlyArgs( - argcount: c_int, - posonlyargcount: c_int, - kwonlyargcount: c_int, - nlocals: c_int, - stacksize: c_int, - flags: c_int, - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - varnames: *mut PyObject, - freevars: *mut PyObject, - cellvars: *mut PyObject, - filename: *mut PyObject, - name: *mut PyObject, - firstlineno: c_int, - lnotab: *mut PyObject, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyCode_NewEmpty")] - pub fn PyCode_NewEmpty( - filename: *const c_char, - funcname: *const c_char, - firstlineno: c_int, - ) -> *mut PyCodeObject; - #[cfg(not(GraalPy))] - pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int; - // skipped PyCodeAddressRange "for internal use only" - // skipped _PyCode_CheckLineNumber - // skipped _PyCode_ConstantKey - pub fn PyCode_Optimize( - code: *mut PyObject, - consts: *mut PyObject, - names: *mut PyObject, - lnotab: *mut PyObject, - ) -> *mut PyObject; - pub fn _PyCode_GetExtra( - code: *mut PyObject, - index: Py_ssize_t, - extra: *const *mut c_void, - ) -> c_int; - pub fn _PyCode_SetExtra(code: *mut PyObject, index: Py_ssize_t, extra: *mut c_void) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/compile.rs b/vendor/pyo3/pyo3-ffi/src/cpython/compile.rs deleted file mode 100644 index 79f06c9200..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/compile.rs +++ /dev/null @@ -1,108 +0,0 @@ -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::object::PyObject; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::pyarena::*; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::pythonrun::*; -#[cfg(not(any(PyPy, Py_3_10)))] -use crate::PyCodeObject; -#[cfg(not(any(PyPy, Py_3_10)))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -// skipped non-limited PyCF_MASK -// skipped non-limited PyCF_MASK_OBSOLETE -// skipped non-limited PyCF_SOURCE_IS_UTF8 -// skipped non-limited PyCF_DONT_IMPLY_DEDENT -// skipped non-limited PyCF_ONLY_AST -// skipped non-limited PyCF_IGNORE_COOKIE -// skipped non-limited PyCF_TYPE_COMMENTS -// skipped non-limited PyCF_ALLOW_TOP_LEVEL_AWAIT -// skipped non-limited PyCF_COMPILE_MASK - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyCompilerFlags { - pub cf_flags: c_int, - #[cfg(Py_3_8)] - pub cf_feature_version: c_int, -} - -// skipped non-limited _PyCompilerFlags_INIT - -#[cfg(all(Py_3_12, not(any(Py_3_13, PyPy, GraalPy))))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _PyCompilerSrcLocation { - pub lineno: c_int, - pub end_lineno: c_int, - pub col_offset: c_int, - pub end_col_offset: c_int, -} - -// skipped SRC_LOCATION_FROM_AST - -#[cfg(not(any(PyPy, GraalPy, Py_3_13)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyFutureFeatures { - pub ff_features: c_int, - #[cfg(not(Py_3_12))] - pub ff_lineno: c_int, - #[cfg(Py_3_12)] - pub ff_location: _PyCompilerSrcLocation, -} - -pub const FUTURE_NESTED_SCOPES: &str = "nested_scopes"; -pub const FUTURE_GENERATORS: &str = "generators"; -pub const FUTURE_DIVISION: &str = "division"; -pub const FUTURE_ABSOLUTE_IMPORT: &str = "absolute_import"; -pub const FUTURE_WITH_STATEMENT: &str = "with_statement"; -pub const FUTURE_PRINT_FUNCTION: &str = "print_function"; -pub const FUTURE_UNICODE_LITERALS: &str = "unicode_literals"; -pub const FUTURE_BARRY_AS_BDFL: &str = "barry_as_FLUFL"; -pub const FUTURE_GENERATOR_STOP: &str = "generator_stop"; -// skipped non-limited FUTURE_ANNOTATIONS - -extern "C" { - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyNode_Compile(arg1: *mut _node, arg2: *const c_char) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyAST_CompileEx( - _mod: *mut _mod, - filename: *const c_char, - flags: *mut PyCompilerFlags, - optimize: c_int, - arena: *mut PyArena, - ) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyAST_CompileObject( - _mod: *mut _mod, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - optimize: c_int, - arena: *mut PyArena, - ) -> *mut PyCodeObject; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyFuture_FromAST(_mod: *mut _mod, filename: *const c_char) -> *mut PyFutureFeatures; - - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn PyFuture_FromASTObject( - _mod: *mut _mod, - filename: *mut PyObject, - ) -> *mut PyFutureFeatures; - - // skipped non-limited _Py_Mangle - // skipped non-limited PY_INVALID_STACK_EFFECT - - pub fn PyCompile_OpcodeStackEffect(opcode: c_int, oparg: c_int) -> c_int; - - #[cfg(Py_3_8)] - pub fn PyCompile_OpcodeStackEffectWithJump(opcode: c_int, oparg: c_int, jump: c_int) -> c_int; - - // skipped non-limited _PyASTOptimizeState - // skipped non-limited _PyAST_Optimize -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/complexobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/complexobject.rs deleted file mode 100644 index 4cc86db566..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/complexobject.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::PyObject; -use std::os::raw::c_double; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct Py_complex { - pub real: c_double, - pub imag: c_double, -} - -// skipped private function _Py_c_sum -// skipped private function _Py_c_diff -// skipped private function _Py_c_neg -// skipped private function _Py_c_prod -// skipped private function _Py_c_quot -// skipped private function _Py_c_pow -// skipped private function _Py_c_abs - -#[repr(C)] -pub struct PyComplexObject { - pub ob_base: PyObject, - pub cval: Py_complex, -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyComplex_FromCComplex")] - pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyComplex_AsCComplex")] - pub fn PyComplex_AsCComplex(op: *mut PyObject) -> Py_complex; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/critical_section.rs b/vendor/pyo3/pyo3-ffi/src/cpython/critical_section.rs deleted file mode 100644 index 97b2f5e055..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/critical_section.rs +++ /dev/null @@ -1,30 +0,0 @@ -#[cfg(Py_GIL_DISABLED)] -use crate::PyMutex; -use crate::PyObject; - -#[repr(C)] -#[cfg(Py_GIL_DISABLED)] -pub struct PyCriticalSection { - _cs_prev: usize, - _cs_mutex: *mut PyMutex, -} - -#[repr(C)] -#[cfg(Py_GIL_DISABLED)] -pub struct PyCriticalSection2 { - _cs_base: PyCriticalSection, - _cs_mutex2: *mut PyMutex, -} - -#[cfg(not(Py_GIL_DISABLED))] -opaque_struct!(PyCriticalSection); - -#[cfg(not(Py_GIL_DISABLED))] -opaque_struct!(PyCriticalSection2); - -extern "C" { - pub fn PyCriticalSection_Begin(c: *mut PyCriticalSection, op: *mut PyObject); - pub fn PyCriticalSection_End(c: *mut PyCriticalSection); - pub fn PyCriticalSection2_Begin(c: *mut PyCriticalSection2, a: *mut PyObject, b: *mut PyObject); - pub fn PyCriticalSection2_End(c: *mut PyCriticalSection2); -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/descrobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/descrobject.rs deleted file mode 100644 index 1b5ee466c8..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/descrobject.rs +++ /dev/null @@ -1,78 +0,0 @@ -use crate::{PyGetSetDef, PyMethodDef, PyObject, PyTypeObject}; -use std::os::raw::{c_char, c_int, c_void}; - -pub type wrapperfunc = Option< - unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - wrapped: *mut c_void, - ) -> *mut PyObject, ->; - -pub type wrapperfunc_kwds = Option< - unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - wrapped: *mut c_void, - kwds: *mut PyObject, - ) -> *mut PyObject, ->; - -#[repr(C)] -pub struct wrapperbase { - pub name: *const c_char, - pub offset: c_int, - pub function: *mut c_void, - pub wrapper: wrapperfunc, - pub doc: *const c_char, - pub flags: c_int, - pub name_strobj: *mut PyObject, -} - -pub const PyWrapperFlag_KEYWORDS: c_int = 1; - -#[repr(C)] -pub struct PyDescrObject { - pub ob_base: PyObject, - pub d_type: *mut PyTypeObject, - pub d_name: *mut PyObject, - pub d_qualname: *mut PyObject, -} - -// skipped non-limited PyDescr_TYPE -// skipped non-limited PyDescr_NAME - -#[repr(C)] -pub struct PyMethodDescrObject { - pub d_common: PyDescrObject, - pub d_method: *mut PyMethodDef, - #[cfg(all(not(PyPy), Py_3_8))] - pub vectorcall: Option, -} - -#[repr(C)] -pub struct PyMemberDescrObject { - pub d_common: PyDescrObject, - pub d_member: *mut PyGetSetDef, -} - -#[repr(C)] -pub struct PyGetSetDescrObject { - pub d_common: PyDescrObject, - pub d_getset: *mut PyGetSetDef, -} - -#[repr(C)] -pub struct PyWrapperDescrObject { - pub d_common: PyDescrObject, - pub d_base: *mut wrapperbase, - pub d_wrapped: *mut c_void, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PyMethodWrapper_Type: PyTypeObject; -} - -// skipped non-limited PyDescr_NewWrapper -// skipped non-limited PyDescr_IsData diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/dictobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/dictobject.rs deleted file mode 100644 index 79dcbfdb62..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/dictobject.rs +++ /dev/null @@ -1,80 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; - -opaque_struct!(PyDictKeysObject); - -#[cfg(Py_3_11)] -opaque_struct!(PyDictValues); - -#[cfg(not(GraalPy))] -#[repr(C)] -#[derive(Debug)] -pub struct PyDictObject { - pub ob_base: PyObject, - pub ma_used: Py_ssize_t, - #[cfg_attr( - Py_3_12, - deprecated(note = "Deprecated in Python 3.12 and will be removed in the future.") - )] - pub ma_version_tag: u64, - pub ma_keys: *mut PyDictKeysObject, - #[cfg(not(Py_3_11))] - pub ma_values: *mut *mut PyObject, - #[cfg(Py_3_11)] - pub ma_values: *mut PyDictValues, -} - -extern "C" { - // skipped _PyDict_GetItem_KnownHash - // skipped _PyDict_GetItemIdWithError - // skipped _PyDict_GetItemStringWithError - // skipped PyDict_SetDefault - pub fn _PyDict_SetItem_KnownHash( - mp: *mut PyObject, - key: *mut PyObject, - item: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; - // skipped _PyDict_DelItem_KnownHash - // skipped _PyDict_DelItemIf - // skipped _PyDict_NewKeysForClass - pub fn _PyDict_Next( - mp: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - value: *mut *mut PyObject, - hash: *mut crate::Py_hash_t, - ) -> c_int; - // skipped PyDict_GET_SIZE - // skipped _PyDict_ContainsId - pub fn _PyDict_NewPresized(minused: Py_ssize_t) -> *mut PyObject; - // skipped _PyDict_MaybeUntrack - // skipped _PyDict_HasOnlyStringKeys - // skipped _PyDict_KeysSize - // skipped _PyDict_SizeOf - // skipped _PyDict_Pop - // skipped _PyDict_Pop_KnownHash - // skipped _PyDict_FromKeys - // skipped _PyDict_HasSplitTable - // skipped _PyDict_MergeEx - // skipped _PyDict_SetItemId - // skipped _PyDict_DelItemId - // skipped _PyDict_DebugMallocStats - // skipped _PyObjectDict_SetItem - // skipped _PyDict_LoadGlobal - // skipped _PyDict_GetItemHint - // skipped _PyDictViewObject - // skipped _PyDictView_New - // skipped _PyDictView_Intersect - - #[cfg(Py_3_10)] - pub fn _PyDict_Contains_KnownHash( - op: *mut PyObject, - key: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; - - #[cfg(not(Py_3_10))] - pub fn _PyDict_Contains(mp: *mut PyObject, key: *mut PyObject, hash: Py_ssize_t) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/floatobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/floatobject.rs deleted file mode 100644 index e7caa441c5..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/floatobject.rs +++ /dev/null @@ -1,32 +0,0 @@ -#[cfg(GraalPy)] -use crate::PyFloat_AsDouble; -use crate::{PyFloat_Check, PyObject}; -use std::os::raw::c_double; - -#[repr(C)] -pub struct PyFloatObject { - pub ob_base: PyObject, - pub ob_fval: c_double, -} - -#[inline] -pub unsafe fn _PyFloat_CAST(op: *mut PyObject) -> *mut PyFloatObject { - debug_assert_eq!(PyFloat_Check(op), 1); - op.cast() -} - -#[inline] -pub unsafe fn PyFloat_AS_DOUBLE(op: *mut PyObject) -> c_double { - #[cfg(not(GraalPy))] - return (*_PyFloat_CAST(op)).ob_fval; - #[cfg(GraalPy)] - return PyFloat_AsDouble(op); -} - -// skipped PyFloat_Pack2 -// skipped PyFloat_Pack4 -// skipped PyFloat_Pack8 - -// skipped PyFloat_Unpack2 -// skipped PyFloat_Unpack4 -// skipped PyFloat_Unpack8 diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/frameobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/frameobject.rs deleted file mode 100644 index 6d2346f928..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/frameobject.rs +++ /dev/null @@ -1,96 +0,0 @@ -#[cfg(not(GraalPy))] -use crate::cpython::code::PyCodeObject; -use crate::object::*; -#[cfg(not(GraalPy))] -use crate::pystate::PyThreadState; -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub type PyFrameState = c_char; - -#[repr(C)] -#[derive(Copy, Clone)] -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub struct PyTryBlock { - pub b_type: c_int, - pub b_handler: c_int, - pub b_level: c_int, -} - -#[repr(C)] -#[cfg(not(any(PyPy, GraalPy, Py_3_11)))] -pub struct PyFrameObject { - pub ob_base: PyVarObject, - pub f_back: *mut PyFrameObject, - pub f_code: *mut PyCodeObject, - pub f_builtins: *mut PyObject, - pub f_globals: *mut PyObject, - pub f_locals: *mut PyObject, - pub f_valuestack: *mut *mut PyObject, - - #[cfg(not(Py_3_10))] - pub f_stacktop: *mut *mut PyObject, - pub f_trace: *mut PyObject, - #[cfg(Py_3_10)] - pub f_stackdepth: c_int, - pub f_trace_lines: c_char, - pub f_trace_opcodes: c_char, - - pub f_gen: *mut PyObject, - - pub f_lasti: c_int, - pub f_lineno: c_int, - pub f_iblock: c_int, - #[cfg(not(Py_3_10))] - pub f_executing: c_char, - #[cfg(Py_3_10)] - pub f_state: PyFrameState, - pub f_blockstack: [PyTryBlock; crate::CO_MAXBLOCKS], - pub f_localsplus: [*mut PyObject; 1], -} - -#[cfg(any(PyPy, GraalPy, Py_3_11))] -opaque_struct!(PyFrameObject); - -// skipped _PyFrame_IsRunnable -// skipped _PyFrame_IsExecuting -// skipped _PyFrameHasCompleted - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyFrame_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyFrame_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyFrame_Type)) as c_int -} - -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyFrame_New")] - pub fn PyFrame_New( - tstate: *mut PyThreadState, - code: *mut PyCodeObject, - globals: *mut PyObject, - locals: *mut PyObject, - ) -> *mut PyFrameObject; - // skipped _PyFrame_New_NoTrack - - pub fn PyFrame_BlockSetup(f: *mut PyFrameObject, _type: c_int, handler: c_int, level: c_int); - #[cfg(not(any(PyPy, GraalPy, Py_3_11)))] - pub fn PyFrame_BlockPop(f: *mut PyFrameObject) -> *mut PyTryBlock; - - pub fn PyFrame_LocalsToFast(f: *mut PyFrameObject, clear: c_int); - pub fn PyFrame_FastToLocalsWithError(f: *mut PyFrameObject) -> c_int; - pub fn PyFrame_FastToLocals(f: *mut PyFrameObject); - - // skipped _PyFrame_DebugMallocStats - // skipped PyFrame_GetBack - - #[cfg(not(Py_3_9))] - pub fn PyFrame_ClearFreeList() -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/funcobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/funcobject.rs deleted file mode 100644 index 25de30d57f..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/funcobject.rs +++ /dev/null @@ -1,108 +0,0 @@ -use std::os::raw::c_int; -#[cfg(not(all(PyPy, not(Py_3_8))))] -use std::ptr::addr_of_mut; - -use crate::PyObject; - -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_3_10)))] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_code: *mut PyObject, - pub func_globals: *mut PyObject, - pub func_defaults: *mut PyObject, - pub func_kwdefaults: *mut PyObject, - pub func_closure: *mut PyObject, - pub func_doc: *mut PyObject, - pub func_name: *mut PyObject, - pub func_dict: *mut PyObject, - pub func_weakreflist: *mut PyObject, - pub func_module: *mut PyObject, - pub func_annotations: *mut PyObject, - pub func_qualname: *mut PyObject, - #[cfg(Py_3_8)] - pub vectorcall: Option, -} - -#[cfg(all(not(any(PyPy, GraalPy)), Py_3_10))] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_globals: *mut PyObject, - pub func_builtins: *mut PyObject, - pub func_name: *mut PyObject, - pub func_qualname: *mut PyObject, - pub func_code: *mut PyObject, - pub func_defaults: *mut PyObject, - pub func_kwdefaults: *mut PyObject, - pub func_closure: *mut PyObject, - pub func_doc: *mut PyObject, - pub func_dict: *mut PyObject, - pub func_weakreflist: *mut PyObject, - pub func_module: *mut PyObject, - pub func_annotations: *mut PyObject, - #[cfg(Py_3_12)] - pub func_typeparams: *mut PyObject, - pub vectorcall: Option, - #[cfg(Py_3_11)] - pub func_version: u32, -} - -#[cfg(PyPy)] -#[repr(C)] -pub struct PyFunctionObject { - pub ob_base: PyObject, - pub func_name: *mut PyObject, -} - -#[cfg(GraalPy)] -pub struct PyFunctionObject { - pub ob_base: PyObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(all(PyPy, not(Py_3_8))))] - #[cfg_attr(PyPy, link_name = "PyPyFunction_Type")] - pub static mut PyFunction_Type: crate::PyTypeObject; -} - -#[cfg(not(all(PyPy, not(Py_3_8))))] -#[inline] -pub unsafe fn PyFunction_Check(op: *mut PyObject) -> c_int { - (crate::Py_TYPE(op) == addr_of_mut!(PyFunction_Type)) as c_int -} - -extern "C" { - pub fn PyFunction_New(code: *mut PyObject, globals: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_NewWithQualName( - code: *mut PyObject, - globals: *mut PyObject, - qualname: *mut PyObject, - ) -> *mut PyObject; - pub fn PyFunction_GetCode(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetGlobals(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetModule(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_GetDefaults(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetDefaults(op: *mut PyObject, defaults: *mut PyObject) -> c_int; - pub fn PyFunction_GetKwDefaults(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetKwDefaults(op: *mut PyObject, defaults: *mut PyObject) -> c_int; - pub fn PyFunction_GetClosure(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetClosure(op: *mut PyObject, closure: *mut PyObject) -> c_int; - pub fn PyFunction_GetAnnotations(op: *mut PyObject) -> *mut PyObject; - pub fn PyFunction_SetAnnotations(op: *mut PyObject, annotations: *mut PyObject) -> c_int; -} - -// skipped _PyFunction_Vectorcall -// skipped PyFunction_GET_CODE -// skipped PyFunction_GET_GLOBALS -// skipped PyFunction_GET_MODULE -// skipped PyFunction_GET_DEFAULTS -// skipped PyFunction_GET_KW_DEFAULTS -// skipped PyFunction_GET_CLOSURE -// skipped PyFunction_GET_ANNOTATIONS - -// skipped PyClassMethod_Type -// skipped PyStaticMethod_Type -// skipped PyClassMethod_New -// skipped PyStaticMethod_New diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/genobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/genobject.rs deleted file mode 100644 index 4be310a8c8..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/genobject.rs +++ /dev/null @@ -1,98 +0,0 @@ -use crate::object::*; -use crate::PyFrameObject; -#[cfg(not(any(PyPy, GraalPy)))] -use crate::_PyErr_StackItem; -#[cfg(all(Py_3_11, not(GraalPy)))] -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -pub struct PyGenObject { - pub ob_base: PyObject, - #[cfg(not(Py_3_11))] - pub gi_frame: *mut PyFrameObject, - #[cfg(not(Py_3_10))] - pub gi_running: c_int, - #[cfg(not(Py_3_12))] - pub gi_code: *mut PyObject, - pub gi_weakreflist: *mut PyObject, - pub gi_name: *mut PyObject, - pub gi_qualname: *mut PyObject, - pub gi_exc_state: _PyErr_StackItem, - #[cfg(Py_3_11)] - pub gi_origin_or_finalizer: *mut PyObject, - #[cfg(Py_3_11)] - pub gi_hooks_inited: c_char, - #[cfg(Py_3_11)] - pub gi_closed: c_char, - #[cfg(Py_3_11)] - pub gi_running_async: c_char, - #[cfg(Py_3_11)] - pub gi_frame_state: i8, - #[cfg(Py_3_11)] - pub gi_iframe: [*mut PyObject; 1], -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyGen_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyGen_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyGen_Type)) -} - -#[inline] -pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyGen_Type)) as c_int -} - -extern "C" { - pub fn PyGen_New(frame: *mut PyFrameObject) -> *mut PyObject; - // skipped PyGen_NewWithQualName - // skipped _PyGen_SetStopIterationValue - // skipped _PyGen_FetchStopIterationValue - // skipped _PyGen_yf - // skipped _PyGen_Finalize - #[cfg(not(any(Py_3_9, PyPy)))] - #[deprecated(note = "This function was never documented in the Python API.")] - pub fn PyGen_NeedsFinalizing(op: *mut PyGenObject) -> c_int; -} - -// skipped PyCoroObject - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCoro_Type: PyTypeObject; - pub static mut _PyCoroWrapper_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyCoro_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyCoro_Type)) -} - -// skipped _PyCoro_GetAwaitableIter -// skipped PyCoro_New - -// skipped PyAsyncGenObject - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyAsyncGen_Type: PyTypeObject; - // skipped _PyAsyncGenASend_Type - // skipped _PyAsyncGenWrappedValue_Type - // skipped _PyAsyncGenAThrow_Type -} - -// skipped PyAsyncGen_New - -#[inline] -pub unsafe fn PyAsyncGen_CheckExact(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyAsyncGen_Type)) -} - -// skipped _PyAsyncGenValueWrapperNew diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/import.rs b/vendor/pyo3/pyo3-ffi/src/cpython/import.rs deleted file mode 100644 index 697d68a419..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/import.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::{PyInterpreterState, PyObject}; -#[cfg(not(PyPy))] -use std::os::raw::c_uchar; -use std::os::raw::{c_char, c_int}; - -// skipped PyInit__imp - -extern "C" { - pub fn _PyImport_IsInitialized(state: *mut PyInterpreterState) -> c_int; - // skipped _PyImport_GetModuleId - pub fn _PyImport_SetModule(name: *mut PyObject, module: *mut PyObject) -> c_int; - pub fn _PyImport_SetModuleString(name: *const c_char, module: *mut PyObject) -> c_int; - pub fn _PyImport_AcquireLock(); - pub fn _PyImport_ReleaseLock() -> c_int; - #[cfg(not(Py_3_9))] - pub fn _PyImport_FindBuiltin(name: *const c_char, modules: *mut PyObject) -> *mut PyObject; - #[cfg(not(Py_3_11))] - pub fn _PyImport_FindExtensionObject(a: *mut PyObject, b: *mut PyObject) -> *mut PyObject; - pub fn _PyImport_FixupBuiltin( - module: *mut PyObject, - name: *const c_char, - modules: *mut PyObject, - ) -> c_int; - pub fn _PyImport_FixupExtensionObject( - a: *mut PyObject, - b: *mut PyObject, - c: *mut PyObject, - d: *mut PyObject, - ) -> c_int; -} - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _inittab { - pub name: *const c_char, - pub initfunc: Option *mut PyObject>, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(PyPy))] - pub static mut PyImport_Inittab: *mut _inittab; -} - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyImport_ExtendInittab(newtab: *mut _inittab) -> c_int; -} - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct _frozen { - pub name: *const c_char, - pub code: *const c_uchar, - pub size: c_int, - #[cfg(Py_3_11)] - pub is_package: c_int, - #[cfg(all(Py_3_11, not(Py_3_13)))] - pub get_code: Option *mut PyObject>, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(PyPy))] - pub static mut PyImport_FrozenModules: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenBootstrap: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenStdlib: *const _frozen; - #[cfg(all(not(PyPy), Py_3_11))] - pub static mut _PyImport_FrozenTest: *const _frozen; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/initconfig.rs b/vendor/pyo3/pyo3-ffi/src/cpython/initconfig.rs deleted file mode 100644 index 321d200e14..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/initconfig.rs +++ /dev/null @@ -1,223 +0,0 @@ -/* --- PyStatus ----------------------------------------------- */ - -use crate::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_ulong}; - -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum _PyStatus_TYPE { - _PyStatus_TYPE_OK = 0, - _PyStatus_TYPE_ERROR = 1, - _PyStatus_TYPE_EXIT = 2, -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStatus { - pub _type: _PyStatus_TYPE, - pub func: *const c_char, - pub err_msg: *const c_char, - pub exitcode: c_int, -} - -extern "C" { - pub fn PyStatus_Ok() -> PyStatus; - pub fn PyStatus_Error(err_msg: *const c_char) -> PyStatus; - pub fn PyStatus_NoMemory() -> PyStatus; - pub fn PyStatus_Exit(exitcode: c_int) -> PyStatus; - pub fn PyStatus_IsError(err: PyStatus) -> c_int; - pub fn PyStatus_IsExit(err: PyStatus) -> c_int; - pub fn PyStatus_Exception(err: PyStatus) -> c_int; -} - -/* --- PyWideStringList ------------------------------------------------ */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyWideStringList { - pub length: Py_ssize_t, - pub items: *mut *mut wchar_t, -} - -extern "C" { - pub fn PyWideStringList_Append(list: *mut PyWideStringList, item: *const wchar_t) -> PyStatus; - pub fn PyWideStringList_Insert( - list: *mut PyWideStringList, - index: Py_ssize_t, - item: *const wchar_t, - ) -> PyStatus; -} - -/* --- PyPreConfig ----------------------------------------------- */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyPreConfig { - pub _config_init: c_int, - pub parse_argv: c_int, - pub isolated: c_int, - pub use_environment: c_int, - pub configure_locale: c_int, - pub coerce_c_locale: c_int, - pub coerce_c_locale_warn: c_int, - - #[cfg(windows)] - pub legacy_windows_fs_encoding: c_int, - - pub utf8_mode: c_int, - pub dev_mode: c_int, - pub allocator: c_int, -} - -extern "C" { - pub fn PyPreConfig_InitPythonConfig(config: *mut PyPreConfig); - pub fn PyPreConfig_InitIsolatedConfig(config: *mut PyPreConfig); -} - -/* --- PyConfig ---------------------------------------------- */ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyConfig { - pub _config_init: c_int, - pub isolated: c_int, - pub use_environment: c_int, - pub dev_mode: c_int, - pub install_signal_handlers: c_int, - pub use_hash_seed: c_int, - pub hash_seed: c_ulong, - pub faulthandler: c_int, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub _use_peg_parser: c_int, - pub tracemalloc: c_int, - #[cfg(Py_3_12)] - pub perf_profiling: c_int, - pub import_time: c_int, - #[cfg(Py_3_11)] - pub code_debug_ranges: c_int, - pub show_ref_count: c_int, - #[cfg(not(Py_3_9))] - pub show_alloc_count: c_int, - pub dump_refs: c_int, - #[cfg(Py_3_11)] - pub dump_refs_file: *mut wchar_t, - pub malloc_stats: c_int, - pub filesystem_encoding: *mut wchar_t, - pub filesystem_errors: *mut wchar_t, - pub pycache_prefix: *mut wchar_t, - pub parse_argv: c_int, - #[cfg(Py_3_10)] - pub orig_argv: PyWideStringList, - pub argv: PyWideStringList, - #[cfg(not(Py_3_10))] - pub program_name: *mut wchar_t, - pub xoptions: PyWideStringList, - pub warnoptions: PyWideStringList, - pub site_import: c_int, - pub bytes_warning: c_int, - #[cfg(Py_3_10)] - pub warn_default_encoding: c_int, - pub inspect: c_int, - pub interactive: c_int, - pub optimization_level: c_int, - pub parser_debug: c_int, - pub write_bytecode: c_int, - pub verbose: c_int, - pub quiet: c_int, - pub user_site_directory: c_int, - pub configure_c_stdio: c_int, - pub buffered_stdio: c_int, - pub stdio_encoding: *mut wchar_t, - pub stdio_errors: *mut wchar_t, - - #[cfg(windows)] - pub legacy_windows_stdio: c_int, - - pub check_hash_pycs_mode: *mut wchar_t, - #[cfg(Py_3_11)] - pub use_frozen_modules: c_int, - #[cfg(Py_3_11)] - pub safe_path: c_int, - #[cfg(Py_3_12)] - pub int_max_str_digits: c_int, - #[cfg(Py_3_13)] - pub cpu_count: c_int, - #[cfg(Py_GIL_DISABLED)] - pub enable_gil: c_int, - pub pathconfig_warnings: c_int, - #[cfg(Py_3_10)] - pub program_name: *mut wchar_t, - pub pythonpath_env: *mut wchar_t, - pub home: *mut wchar_t, - #[cfg(Py_3_10)] - pub platlibdir: *mut wchar_t, - - pub module_search_paths_set: c_int, - pub module_search_paths: PyWideStringList, - #[cfg(Py_3_11)] - pub stdlib_dir: *mut wchar_t, - pub executable: *mut wchar_t, - pub base_executable: *mut wchar_t, - pub prefix: *mut wchar_t, - pub base_prefix: *mut wchar_t, - pub exec_prefix: *mut wchar_t, - pub base_exec_prefix: *mut wchar_t, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub platlibdir: *mut wchar_t, - pub skip_source_first_line: c_int, - pub run_command: *mut wchar_t, - pub run_module: *mut wchar_t, - pub run_filename: *mut wchar_t, - #[cfg(Py_3_13)] - pub sys_path_0: *mut wchar_t, - pub _install_importlib: c_int, - pub _init_main: c_int, - #[cfg(all(Py_3_9, not(Py_3_12)))] - pub _isolated_interpreter: c_int, - #[cfg(Py_3_11)] - pub _is_python_build: c_int, - #[cfg(all(Py_3_9, not(Py_3_10)))] - pub _orig_argv: PyWideStringList, - #[cfg(all(Py_3_13, py_sys_config = "Py_DEBUG"))] - pub run_presite: *mut wchar_t, -} - -extern "C" { - pub fn PyConfig_InitPythonConfig(config: *mut PyConfig); - pub fn PyConfig_InitIsolatedConfig(config: *mut PyConfig); - pub fn PyConfig_Clear(config: *mut PyConfig); - pub fn PyConfig_SetString( - config: *mut PyConfig, - config_str: *mut *mut wchar_t, - str: *const wchar_t, - ) -> PyStatus; - pub fn PyConfig_SetBytesString( - config: *mut PyConfig, - config_str: *mut *mut wchar_t, - str: *const c_char, - ) -> PyStatus; - pub fn PyConfig_Read(config: *mut PyConfig) -> PyStatus; - pub fn PyConfig_SetBytesArgv( - config: *mut PyConfig, - argc: Py_ssize_t, - argv: *mut *const c_char, - ) -> PyStatus; - pub fn PyConfig_SetArgv( - config: *mut PyConfig, - argc: Py_ssize_t, - argv: *mut *const wchar_t, - ) -> PyStatus; - pub fn PyConfig_SetWideStringList( - config: *mut PyConfig, - list: *mut PyWideStringList, - length: Py_ssize_t, - items: *mut *mut wchar_t, - ) -> PyStatus; -} - -/* --- Helper functions --------------------------------------- */ - -extern "C" { - pub fn Py_GetArgcArgv(argc: *mut c_int, argv: *mut *mut *mut wchar_t); -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/listobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/listobject.rs deleted file mode 100644 index 694e6bc429..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/listobject.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::object::*; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; - -#[cfg(not(PyPy))] -#[repr(C)] -pub struct PyListObject { - pub ob_base: PyVarObject, - pub ob_item: *mut *mut PyObject, - pub allocated: Py_ssize_t, -} - -#[cfg(PyPy)] -pub struct PyListObject { - pub ob_base: PyObject, -} - -// skipped _PyList_Extend -// skipped _PyList_DebugMallocStats -// skipped _PyList_CAST (used inline below) - -/// Macro, trading safety for speed -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - *(*(op as *mut PyListObject)).ob_item.offset(i) -} - -/// Macro, *only* to be used to fill in brand new lists -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - *(*(op as *mut PyListObject)).ob_item.offset(i) = v; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyList_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { - Py_SIZE(op) -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/lock.rs b/vendor/pyo3/pyo3-ffi/src/cpython/lock.rs deleted file mode 100644 index 6c80b00d3c..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/lock.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::marker::PhantomPinned; -use std::sync::atomic::AtomicU8; - -#[repr(transparent)] -#[derive(Debug)] -pub struct PyMutex { - pub(crate) _bits: AtomicU8, - pub(crate) _pin: PhantomPinned, -} - -extern "C" { - pub fn PyMutex_Lock(m: *mut PyMutex); - pub fn PyMutex_Unlock(m: *mut PyMutex); -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/longobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/longobject.rs deleted file mode 100644 index 45acaae577..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/longobject.rs +++ /dev/null @@ -1,74 +0,0 @@ -use crate::longobject::*; -use crate::object::*; -#[cfg(Py_3_13)] -use crate::pyport::Py_ssize_t; -use libc::size_t; -#[cfg(Py_3_13)] -use std::os::raw::c_void; -use std::os::raw::{c_int, c_uchar}; - -#[cfg(Py_3_13)] -extern "C" { - pub fn PyLong_FromUnicodeObject(u: *mut PyObject, base: c_int) -> *mut PyObject; -} - -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_DEFAULTS: c_int = -1; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_BIG_ENDIAN: c_int = 0; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_LITTLE_ENDIAN: c_int = 1; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_NATIVE_ENDIAN: c_int = 3; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_UNSIGNED_BUFFER: c_int = 4; -#[cfg(Py_3_13)] -pub const Py_ASNATIVEBYTES_REJECT_NEGATIVE: c_int = 8; - -extern "C" { - // skipped _PyLong_Sign - - #[cfg(Py_3_13)] - pub fn PyLong_AsNativeBytes( - v: *mut PyObject, - buffer: *mut c_void, - n_bytes: Py_ssize_t, - flags: c_int, - ) -> Py_ssize_t; - - #[cfg(Py_3_13)] - pub fn PyLong_FromNativeBytes( - buffer: *const c_void, - n_bytes: size_t, - flags: c_int, - ) -> *mut PyObject; - - #[cfg(Py_3_13)] - pub fn PyLong_FromUnsignedNativeBytes( - buffer: *const c_void, - n_bytes: size_t, - flags: c_int, - ) -> *mut PyObject; - - // skipped PyUnstable_Long_IsCompact - // skipped PyUnstable_Long_CompactValue - - #[cfg_attr(PyPy, link_name = "_PyPyLong_FromByteArray")] - pub fn _PyLong_FromByteArray( - bytes: *const c_uchar, - n: size_t, - little_endian: c_int, - is_signed: c_int, - ) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "_PyPyLong_AsByteArrayO")] - pub fn _PyLong_AsByteArray( - v: *mut PyLongObject, - bytes: *mut c_uchar, - n: size_t, - little_endian: c_int, - is_signed: c_int, - ) -> c_int; - - // skipped _PyLong_GCD -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/methodobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/methodobject.rs deleted file mode 100644 index 97ad9ce35f..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/methodobject.rs +++ /dev/null @@ -1,71 +0,0 @@ -use crate::object::*; -#[cfg(not(GraalPy))] -use crate::{PyCFunctionObject, PyMethodDefPointer, METH_METHOD, METH_STATIC}; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg(not(GraalPy))] -pub struct PyCMethodObject { - pub func: PyCFunctionObject, - pub mm_class: *mut PyTypeObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyCMethod_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyCMethod_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCMethod_Type)) as c_int -} - -#[inline] -pub unsafe fn PyCMethod_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyCMethod_Type)) -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_FUNCTION(func: *mut PyObject) -> PyMethodDefPointer { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - (*(*func).m_ml).ml_meth -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_SELF(func: *mut PyObject) -> *mut PyObject { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - if (*(*func).m_ml).ml_flags & METH_STATIC != 0 { - std::ptr::null_mut() - } else { - (*func).m_self - } -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_FLAGS(func: *mut PyObject) -> c_int { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - (*(*func).m_ml).ml_flags -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyCFunction_GET_CLASS(func: *mut PyObject) -> *mut PyTypeObject { - debug_assert_eq!(PyCMethod_Check(func), 1); - - let func = func.cast::(); - if (*(*func).m_ml).ml_flags & METH_METHOD != 0 { - let func = func.cast::(); - (*func).mm_class - } else { - std::ptr::null_mut() - } -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/mod.rs b/vendor/pyo3/pyo3-ffi/src/cpython/mod.rs deleted file mode 100644 index fe909f0cee..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/mod.rs +++ /dev/null @@ -1,84 +0,0 @@ -pub(crate) mod abstract_; -// skipped bytearrayobject.h -pub(crate) mod bytesobject; -#[cfg(not(PyPy))] -pub(crate) mod ceval; -pub(crate) mod code; -pub(crate) mod compile; -pub(crate) mod complexobject; -#[cfg(Py_3_13)] -pub(crate) mod critical_section; -pub(crate) mod descrobject; -#[cfg(not(PyPy))] -pub(crate) mod dictobject; -// skipped fileobject.h -// skipped fileutils.h -pub(crate) mod frameobject; -pub(crate) mod funcobject; -pub(crate) mod genobject; -pub(crate) mod import; -#[cfg(all(Py_3_8, not(PyPy)))] -pub(crate) mod initconfig; -// skipped interpreteridobject.h -pub(crate) mod listobject; -#[cfg(Py_3_13)] -pub(crate) mod lock; -pub(crate) mod longobject; -#[cfg(all(Py_3_9, not(PyPy)))] -pub(crate) mod methodobject; -pub(crate) mod object; -pub(crate) mod objimpl; -pub(crate) mod pydebug; -pub(crate) mod pyerrors; -#[cfg(all(Py_3_8, not(PyPy)))] -pub(crate) mod pylifecycle; -pub(crate) mod pymem; -pub(crate) mod pystate; -pub(crate) mod pythonrun; -// skipped sysmodule.h -pub(crate) mod floatobject; -pub(crate) mod pyframe; -pub(crate) mod tupleobject; -pub(crate) mod unicodeobject; -pub(crate) mod weakrefobject; - -pub use self::abstract_::*; -pub use self::bytesobject::*; -#[cfg(not(PyPy))] -pub use self::ceval::*; -pub use self::code::*; -pub use self::compile::*; -pub use self::complexobject::*; -#[cfg(Py_3_13)] -pub use self::critical_section::*; -pub use self::descrobject::*; -#[cfg(not(PyPy))] -pub use self::dictobject::*; -pub use self::floatobject::*; -pub use self::frameobject::*; -pub use self::funcobject::*; -pub use self::genobject::*; -pub use self::import::*; -#[cfg(all(Py_3_8, not(PyPy)))] -pub use self::initconfig::*; -pub use self::listobject::*; -#[cfg(Py_3_13)] -pub use self::lock::*; -pub use self::longobject::*; -#[cfg(all(Py_3_9, not(PyPy)))] -pub use self::methodobject::*; -pub use self::object::*; -pub use self::objimpl::*; -pub use self::pydebug::*; -pub use self::pyerrors::*; -#[cfg(Py_3_11)] -pub use self::pyframe::*; -#[cfg(all(Py_3_8, not(PyPy)))] -pub use self::pylifecycle::*; -pub use self::pymem::*; -pub use self::pystate::*; -pub use self::pythonrun::*; -pub use self::tupleobject::*; -pub use self::unicodeobject::*; -#[cfg(not(any(PyPy, GraalPy)))] -pub use self::weakrefobject::*; diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/object.rs b/vendor/pyo3/pyo3-ffi/src/cpython/object.rs deleted file mode 100644 index 75eef11aae..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/object.rs +++ /dev/null @@ -1,398 +0,0 @@ -#[cfg(Py_3_8)] -use crate::vectorcallfunc; -use crate::{object, PyGetSetDef, PyMemberDef, PyMethodDef, PyObject, Py_ssize_t}; -use std::mem; -use std::os::raw::{c_char, c_int, c_uint, c_void}; - -// skipped private _Py_NewReference -// skipped private _Py_NewReferenceNoTotal -// skipped private _Py_ResurrectReference - -// skipped private _Py_GetGlobalRefTotal -// skipped private _Py_GetRefTotal -// skipped private _Py_GetLegacyRefTotal -// skipped private _PyInterpreterState_GetRefTotal - -// skipped private _Py_Identifier - -// skipped private _Py_static_string_init -// skipped private _Py_static_string -// skipped private _Py_IDENTIFIER - -#[cfg(not(Py_3_11))] // moved to src/buffer.rs from Python -mod bufferinfo { - use crate::Py_ssize_t; - use std::os::raw::{c_char, c_int, c_void}; - use std::ptr; - - #[repr(C)] - #[derive(Copy, Clone)] - pub struct Py_buffer { - pub buf: *mut c_void, - /// Owned reference - pub obj: *mut crate::PyObject, - pub len: Py_ssize_t, - pub itemsize: Py_ssize_t, - pub readonly: c_int, - pub ndim: c_int, - pub format: *mut c_char, - pub shape: *mut Py_ssize_t, - pub strides: *mut Py_ssize_t, - pub suboffsets: *mut Py_ssize_t, - pub internal: *mut c_void, - #[cfg(PyPy)] - pub flags: c_int, - #[cfg(PyPy)] - pub _strides: [Py_ssize_t; PyBUF_MAX_NDIM as usize], - #[cfg(PyPy)] - pub _shape: [Py_ssize_t; PyBUF_MAX_NDIM as usize], - } - - impl Py_buffer { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - Py_buffer { - buf: ptr::null_mut(), - obj: ptr::null_mut(), - len: 0, - itemsize: 0, - readonly: 0, - ndim: 0, - format: ptr::null_mut(), - shape: ptr::null_mut(), - strides: ptr::null_mut(), - suboffsets: ptr::null_mut(), - internal: ptr::null_mut(), - #[cfg(PyPy)] - flags: 0, - #[cfg(PyPy)] - _strides: [0; PyBUF_MAX_NDIM as usize], - #[cfg(PyPy)] - _shape: [0; PyBUF_MAX_NDIM as usize], - } - } - } - - pub type getbufferproc = unsafe extern "C" fn( - arg1: *mut crate::PyObject, - arg2: *mut Py_buffer, - arg3: c_int, - ) -> c_int; - pub type releasebufferproc = - unsafe extern "C" fn(arg1: *mut crate::PyObject, arg2: *mut Py_buffer); - - /// Maximum number of dimensions - pub const PyBUF_MAX_NDIM: c_int = if cfg!(PyPy) { 36 } else { 64 }; - - /* Flags for getting buffers */ - pub const PyBUF_SIMPLE: c_int = 0; - pub const PyBUF_WRITABLE: c_int = 0x0001; - /* we used to include an E, backwards compatible alias */ - pub const PyBUF_WRITEABLE: c_int = PyBUF_WRITABLE; - pub const PyBUF_FORMAT: c_int = 0x0004; - pub const PyBUF_ND: c_int = 0x0008; - pub const PyBUF_STRIDES: c_int = 0x0010 | PyBUF_ND; - pub const PyBUF_C_CONTIGUOUS: c_int = 0x0020 | PyBUF_STRIDES; - pub const PyBUF_F_CONTIGUOUS: c_int = 0x0040 | PyBUF_STRIDES; - pub const PyBUF_ANY_CONTIGUOUS: c_int = 0x0080 | PyBUF_STRIDES; - pub const PyBUF_INDIRECT: c_int = 0x0100 | PyBUF_STRIDES; - - pub const PyBUF_CONTIG: c_int = PyBUF_ND | PyBUF_WRITABLE; - pub const PyBUF_CONTIG_RO: c_int = PyBUF_ND; - - pub const PyBUF_STRIDED: c_int = PyBUF_STRIDES | PyBUF_WRITABLE; - pub const PyBUF_STRIDED_RO: c_int = PyBUF_STRIDES; - - pub const PyBUF_RECORDS: c_int = PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT; - pub const PyBUF_RECORDS_RO: c_int = PyBUF_STRIDES | PyBUF_FORMAT; - - pub const PyBUF_FULL: c_int = PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT; - pub const PyBUF_FULL_RO: c_int = PyBUF_INDIRECT | PyBUF_FORMAT; - - pub const PyBUF_READ: c_int = 0x100; - pub const PyBUF_WRITE: c_int = 0x200; -} - -#[cfg(not(Py_3_11))] -pub use self::bufferinfo::*; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyNumberMethods { - pub nb_add: Option, - pub nb_subtract: Option, - pub nb_multiply: Option, - pub nb_remainder: Option, - pub nb_divmod: Option, - pub nb_power: Option, - pub nb_negative: Option, - pub nb_positive: Option, - pub nb_absolute: Option, - pub nb_bool: Option, - pub nb_invert: Option, - pub nb_lshift: Option, - pub nb_rshift: Option, - pub nb_and: Option, - pub nb_xor: Option, - pub nb_or: Option, - pub nb_int: Option, - pub nb_reserved: *mut c_void, - pub nb_float: Option, - pub nb_inplace_add: Option, - pub nb_inplace_subtract: Option, - pub nb_inplace_multiply: Option, - pub nb_inplace_remainder: Option, - pub nb_inplace_power: Option, - pub nb_inplace_lshift: Option, - pub nb_inplace_rshift: Option, - pub nb_inplace_and: Option, - pub nb_inplace_xor: Option, - pub nb_inplace_or: Option, - pub nb_floor_divide: Option, - pub nb_true_divide: Option, - pub nb_inplace_floor_divide: Option, - pub nb_inplace_true_divide: Option, - pub nb_index: Option, - pub nb_matrix_multiply: Option, - pub nb_inplace_matrix_multiply: Option, -} - -#[repr(C)] -#[derive(Clone)] -pub struct PySequenceMethods { - pub sq_length: Option, - pub sq_concat: Option, - pub sq_repeat: Option, - pub sq_item: Option, - pub was_sq_slice: *mut c_void, - pub sq_ass_item: Option, - pub was_sq_ass_slice: *mut c_void, - pub sq_contains: Option, - pub sq_inplace_concat: Option, - pub sq_inplace_repeat: Option, -} - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyMappingMethods { - pub mp_length: Option, - pub mp_subscript: Option, - pub mp_ass_subscript: Option, -} - -#[cfg(Py_3_10)] -pub type sendfunc = unsafe extern "C" fn( - iter: *mut PyObject, - value: *mut PyObject, - result: *mut *mut PyObject, -) -> object::PySendResult; - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyAsyncMethods { - pub am_await: Option, - pub am_aiter: Option, - pub am_anext: Option, - #[cfg(Py_3_10)] - pub am_send: Option, -} - -#[repr(C)] -#[derive(Clone, Default)] -pub struct PyBufferProcs { - pub bf_getbuffer: Option, - pub bf_releasebuffer: Option, -} - -pub type printfunc = - unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut ::libc::FILE, arg3: c_int) -> c_int; - -#[repr(C)] -#[derive(Debug)] -pub struct PyTypeObject { - pub ob_base: object::PyVarObject, - pub tp_name: *const c_char, - pub tp_basicsize: Py_ssize_t, - pub tp_itemsize: Py_ssize_t, - pub tp_dealloc: Option, - #[cfg(not(Py_3_8))] - pub tp_print: Option, - #[cfg(Py_3_8)] - pub tp_vectorcall_offset: Py_ssize_t, - pub tp_getattr: Option, - pub tp_setattr: Option, - pub tp_as_async: *mut PyAsyncMethods, - pub tp_repr: Option, - pub tp_as_number: *mut PyNumberMethods, - pub tp_as_sequence: *mut PySequenceMethods, - pub tp_as_mapping: *mut PyMappingMethods, - pub tp_hash: Option, - pub tp_call: Option, - pub tp_str: Option, - pub tp_getattro: Option, - pub tp_setattro: Option, - pub tp_as_buffer: *mut PyBufferProcs, - #[cfg(not(Py_GIL_DISABLED))] - pub tp_flags: std::os::raw::c_ulong, - #[cfg(Py_GIL_DISABLED)] - pub tp_flags: crate::impl_::AtomicCULong, - pub tp_doc: *const c_char, - pub tp_traverse: Option, - pub tp_clear: Option, - pub tp_richcompare: Option, - pub tp_weaklistoffset: Py_ssize_t, - pub tp_iter: Option, - pub tp_iternext: Option, - pub tp_methods: *mut PyMethodDef, - pub tp_members: *mut PyMemberDef, - pub tp_getset: *mut PyGetSetDef, - pub tp_base: *mut PyTypeObject, - pub tp_dict: *mut object::PyObject, - pub tp_descr_get: Option, - pub tp_descr_set: Option, - pub tp_dictoffset: Py_ssize_t, - pub tp_init: Option, - pub tp_alloc: Option, - pub tp_new: Option, - pub tp_free: Option, - pub tp_is_gc: Option, - pub tp_bases: *mut object::PyObject, - pub tp_mro: *mut object::PyObject, - pub tp_cache: *mut object::PyObject, - pub tp_subclasses: *mut object::PyObject, - pub tp_weaklist: *mut object::PyObject, - pub tp_del: Option, - pub tp_version_tag: c_uint, - pub tp_finalize: Option, - #[cfg(Py_3_8)] - pub tp_vectorcall: Option, - #[cfg(Py_3_12)] - pub tp_watched: c_char, - #[cfg(any(all(PyPy, Py_3_8, not(Py_3_10)), all(not(PyPy), Py_3_8, not(Py_3_9))))] - pub tp_print: Option, - #[cfg(all(PyPy, not(Py_3_10)))] - pub tp_pypy_flags: std::os::raw::c_long, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_allocs: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_frees: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_maxalloc: Py_ssize_t, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_prev: *mut PyTypeObject, - #[cfg(py_sys_config = "COUNT_ALLOCS")] - pub tp_next: *mut PyTypeObject, -} - -#[cfg(Py_3_11)] -#[repr(C)] -#[derive(Clone)] -struct _specialization_cache { - getitem: *mut PyObject, - #[cfg(Py_3_12)] - getitem_version: u32, - #[cfg(Py_3_13)] - init: *mut PyObject, -} - -#[repr(C)] -pub struct PyHeapTypeObject { - pub ht_type: PyTypeObject, - pub as_async: PyAsyncMethods, - pub as_number: PyNumberMethods, - pub as_mapping: PyMappingMethods, - pub as_sequence: PySequenceMethods, - pub as_buffer: PyBufferProcs, - pub ht_name: *mut object::PyObject, - pub ht_slots: *mut object::PyObject, - pub ht_qualname: *mut object::PyObject, - #[cfg(not(PyPy))] - pub ht_cached_keys: *mut c_void, - #[cfg(Py_3_9)] - pub ht_module: *mut object::PyObject, - #[cfg(Py_3_11)] - _ht_tpname: *mut c_char, - #[cfg(Py_3_11)] - _spec_cache: _specialization_cache, -} - -impl Default for PyHeapTypeObject { - #[inline] - fn default() -> Self { - unsafe { mem::zeroed() } - } -} - -#[inline] -#[cfg(not(Py_3_11))] -pub unsafe fn PyHeapType_GET_MEMBERS(etype: *mut PyHeapTypeObject) -> *mut PyMemberDef { - let py_type = object::Py_TYPE(etype as *mut object::PyObject); - let ptr = etype.offset((*py_type).tp_basicsize); - ptr as *mut PyMemberDef -} - -// skipped private _PyType_Name -// skipped private _PyType_Lookup -// skipped private _PyType_LookupRef - -extern "C" { - #[cfg(Py_3_12)] - pub fn PyType_GetDict(o: *mut PyTypeObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyObject_Print")] - pub fn PyObject_Print(o: *mut PyObject, fp: *mut ::libc::FILE, flags: c_int) -> c_int; - - // skipped private _Py_BreakPoint - // skipped private _PyObject_Dump - - // skipped _PyObject_GetAttrId - - // skipped private _PyObject_GetDictPtr - pub fn PyObject_CallFinalizer(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFinalizerFromDealloc")] - pub fn PyObject_CallFinalizerFromDealloc(arg1: *mut PyObject) -> c_int; - - // skipped private _PyObject_GenericGetAttrWithDict - // skipped private _PyObject_GenericSetAttrWithDict - // skipped private _PyObject_FunctionStr -} - -// skipped Py_SETREF -// skipped Py_XSETREF - -// skipped private _PyObject_ASSERT_FROM -// skipped private _PyObject_ASSERT_WITH_MSG -// skipped private _PyObject_ASSERT -// skipped private _PyObject_ASSERT_FAILED_MSG -// skipped private _PyObject_AssertFailed - -// skipped private _PyTrash_begin -// skipped private _PyTrash_end - -// skipped _PyTrash_thread_deposit_object -// skipped _PyTrash_thread_destroy_chain - -// skipped Py_TRASHCAN_BEGIN -// skipped Py_TRASHCAN_END - -// skipped PyObject_GetItemData - -// skipped PyObject_VisitManagedDict -// skipped _PyObject_SetManagedDict -// skipped PyObject_ClearManagedDict - -// skipped TYPE_MAX_WATCHERS - -// skipped PyType_WatchCallback -// skipped PyType_AddWatcher -// skipped PyType_ClearWatcher -// skipped PyType_Watch -// skipped PyType_Unwatch - -// skipped PyUnstable_Type_AssignVersionTag - -// skipped PyRefTracerEvent - -// skipped PyRefTracer -// skipped PyRefTracer_SetTracer -// skipped PyRefTracer_GetTracer diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/objimpl.rs b/vendor/pyo3/pyo3-ffi/src/cpython/objimpl.rs deleted file mode 100644 index 98a19abeb8..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/objimpl.rs +++ /dev/null @@ -1,72 +0,0 @@ -#[cfg(not(all(Py_3_11, GraalPy)))] -use libc::size_t; -use std::os::raw::c_int; - -#[cfg(not(any(PyPy, GraalPy)))] -use std::os::raw::c_void; - -use crate::object::*; - -// skipped _PyObject_SIZE -// skipped _PyObject_VAR_SIZE - -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _Py_GetAllocatedBlocks() -> crate::Py_ssize_t; -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyObjectArenaAllocator { - pub ctx: *mut c_void, - pub alloc: Option *mut c_void>, - pub free: Option, -} - -#[cfg(not(any(PyPy, GraalPy)))] -impl Default for PyObjectArenaAllocator { - #[inline] - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyObject_GetArenaAllocator(allocator: *mut PyObjectArenaAllocator); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyObject_SetArenaAllocator(allocator: *mut PyObjectArenaAllocator); - - #[cfg(Py_3_9)] - pub fn PyObject_IS_GC(o: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(Py_3_9))] -pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int { - (crate::PyType_IS_GC(Py_TYPE(o)) != 0 - && match (*Py_TYPE(o)).tp_is_gc { - Some(tp_is_gc) => tp_is_gc(o) != 0, - None => true, - }) as c_int -} - -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _PyObject_GC_Malloc(size: size_t) -> *mut PyObject; - pub fn _PyObject_GC_Calloc(size: size_t) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int { - ((*t).tp_weaklistoffset > 0) as c_int -} - -#[inline] -pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject { - let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset; - o.offset(weaklistoffset) as *mut *mut PyObject -} - -// skipped PyUnstable_Object_GC_NewWithExtraData diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pydebug.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pydebug.rs deleted file mode 100644 index a42848e8fd..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pydebug.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::os::raw::{c_char, c_int}; - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_DebugFlag")] - pub static mut Py_DebugFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_VerboseFlag")] - pub static mut Py_VerboseFlag: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_QuietFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_InteractiveFlag")] - pub static mut Py_InteractiveFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_InspectFlag")] - pub static mut Py_InspectFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_OptimizeFlag")] - pub static mut Py_OptimizeFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_NoSiteFlag")] - pub static mut Py_NoSiteFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_BytesWarningFlag")] - pub static mut Py_BytesWarningFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_UseClassExceptionsFlag")] - pub static mut Py_UseClassExceptionsFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_FrozenFlag")] - pub static mut Py_FrozenFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_IgnoreEnvironmentFlag")] - pub static mut Py_IgnoreEnvironmentFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_DontWriteBytecodeFlag")] - pub static mut Py_DontWriteBytecodeFlag: c_int; - #[deprecated(note = "Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPy_NoUserSiteDirectory")] - pub static mut Py_NoUserSiteDirectory: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_UnbufferedStdioFlag: c_int; - #[cfg_attr(PyPy, link_name = "PyPy_HashRandomizationFlag")] - pub static mut Py_HashRandomizationFlag: c_int; - #[deprecated(note = "Python 3.12")] - pub static mut Py_IsolatedFlag: c_int; - #[cfg(windows)] - #[deprecated(note = "Python 3.12")] - pub static mut Py_LegacyWindowsFSEncodingFlag: c_int; - #[cfg(windows)] - #[deprecated(note = "Python 3.12")] - pub static mut Py_LegacyWindowsStdioFlag: c_int; -} - -extern "C" { - #[cfg(Py_3_11)] - pub fn Py_GETENV(name: *const c_char) -> *mut c_char; -} - -#[cfg(not(Py_3_11))] -#[inline(always)] -pub unsafe fn Py_GETENV(name: *const c_char) -> *mut c_char { - #[allow(deprecated)] - if Py_IgnoreEnvironmentFlag != 0 { - std::ptr::null_mut() - } else { - libc::getenv(name) - } -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pyerrors.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pyerrors.rs deleted file mode 100644 index c6e10e5f07..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pyerrors.rs +++ /dev/null @@ -1,188 +0,0 @@ -use crate::PyObject; -#[cfg(not(any(PyPy, GraalPy)))] -use crate::Py_ssize_t; - -#[repr(C)] -#[derive(Debug)] -pub struct PyBaseExceptionObject { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub dict: *mut PyObject, - #[cfg(not(PyPy))] - pub args: *mut PyObject, - #[cfg(all(Py_3_11, not(PyPy)))] - pub notes: *mut PyObject, - #[cfg(not(PyPy))] - pub traceback: *mut PyObject, - #[cfg(not(PyPy))] - pub context: *mut PyObject, - #[cfg(not(PyPy))] - pub cause: *mut PyObject, - #[cfg(not(PyPy))] - pub suppress_context: char, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySyntaxErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub msg: *mut PyObject, - pub filename: *mut PyObject, - pub lineno: *mut PyObject, - pub offset: *mut PyObject, - #[cfg(Py_3_10)] - pub end_lineno: *mut PyObject, - #[cfg(Py_3_10)] - pub end_offset: *mut PyObject, - pub text: *mut PyObject, - pub print_file_and_line: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyImportErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub msg: *mut PyObject, - pub name: *mut PyObject, - pub path: *mut PyObject, - #[cfg(Py_3_12)] - pub name_from: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyUnicodeErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub encoding: *mut PyObject, - pub object: *mut PyObject, - pub start: Py_ssize_t, - pub end: Py_ssize_t, - pub reason: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySystemExitObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub code: *mut PyObject, -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PyOSErrorObject { - pub ob_base: PyObject, - pub dict: *mut PyObject, - pub args: *mut PyObject, - #[cfg(Py_3_11)] - pub notes: *mut PyObject, - pub traceback: *mut PyObject, - pub context: *mut PyObject, - pub cause: *mut PyObject, - pub suppress_context: char, - - pub myerrno: *mut PyObject, - pub strerror: *mut PyObject, - pub filename: *mut PyObject, - pub filename2: *mut PyObject, - #[cfg(windows)] - pub winerror: *mut PyObject, - pub written: Py_ssize_t, -} - -#[repr(C)] -#[derive(Debug)] -pub struct PyStopIterationObject { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub dict: *mut PyObject, - #[cfg(not(PyPy))] - pub args: *mut PyObject, - #[cfg(all(Py_3_11, not(PyPy)))] - pub notes: *mut PyObject, - #[cfg(not(PyPy))] - pub traceback: *mut PyObject, - #[cfg(not(PyPy))] - pub context: *mut PyObject, - #[cfg(not(PyPy))] - pub cause: *mut PyObject, - #[cfg(not(PyPy))] - pub suppress_context: char, - - pub value: *mut PyObject, -} - -// skipped _PyErr_ChainExceptions - -// skipped PyNameErrorObject -// skipped PyAttributeErrorObject - -// skipped PyEnvironmentErrorObject -// skipped PyWindowsErrorObject - -// skipped _PyErr_SetKeyError -// skipped _PyErr_GetTopmostException -// skipped _PyErr_GetExcInfo - -// skipped PyErr_SetFromErrnoWithUnicodeFilename - -// skipped _PyErr_FormatFromCause - -// skipped PyErr_SetFromWindowsErrWithUnicodeFilename -// skipped PyErr_SetExcFromWindowsErrWithUnicodeFilename - -// skipped _PyErr_TrySetFromCause - -// skipped PySignal_SetWakeupFd -// skipped _PyErr_CheckSignals - -// skipped PyErr_SyntaxLocationObject -// skipped PyErr_RangedSyntaxLocationObject -// skipped PyErr_ProgramTextObject - -// skipped _PyErr_ProgramDecodedTextObject -// skipped _PyUnicodeTranslateError_Create -// skipped _PyErr_WriteUnraisableMsg -// skipped _Py_FatalErrorFunc -// skipped _Py_FatalErrorFormat -// skipped Py_FatalError diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pyframe.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pyframe.rs deleted file mode 100644 index d0cfa0a2c6..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pyframe.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(Py_3_11)] -opaque_struct!(_PyInterpreterFrame); diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs deleted file mode 100644 index c259c369ef..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pylifecycle.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::{PyConfig, PyPreConfig, PyStatus, Py_ssize_t}; -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -// "private" functions in cpython/pylifecycle.h accepted in PEP 587 -extern "C" { - // skipped _Py_SetStandardStreamEncoding; - pub fn Py_PreInitialize(src_config: *const PyPreConfig) -> PyStatus; - pub fn Py_PreInitializeFromBytesArgs( - src_config: *const PyPreConfig, - argc: Py_ssize_t, - argv: *mut *mut c_char, - ) -> PyStatus; - pub fn Py_PreInitializeFromArgs( - src_config: *const PyPreConfig, - argc: Py_ssize_t, - argv: *mut *mut wchar_t, - ) -> PyStatus; - pub fn _Py_IsCoreInitialized() -> c_int; - - pub fn Py_InitializeFromConfig(config: *const PyConfig) -> PyStatus; - pub fn _Py_InitializeMain() -> PyStatus; - - pub fn Py_RunMain() -> c_int; - - pub fn Py_ExitStatusException(status: PyStatus) -> !; - - // skipped _Py_RestoreSignals - - // skipped Py_FdIsInteractive - // skipped _Py_FdIsInteractive - - // skipped _Py_SetProgramFullPath - - // skipped _Py_gitidentifier - // skipped _Py_getversion - - // skipped _Py_IsFinalizing - - // skipped _PyOS_URandom - // skipped _PyOS_URandomNonblock - - // skipped _Py_CoerceLegacyLocale - // skipped _Py_LegacyLocaleDetected - // skipped _Py_SetLocaleFromEnv - -} - -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_DEFAULT_GIL: c_int = 0; -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_SHARED_GIL: c_int = 1; -#[cfg(Py_3_12)] -pub const PyInterpreterConfig_OWN_GIL: c_int = 2; - -#[cfg(Py_3_12)] -#[repr(C)] -pub struct PyInterpreterConfig { - pub use_main_obmalloc: c_int, - pub allow_fork: c_int, - pub allow_exec: c_int, - pub allow_threads: c_int, - pub allow_daemon_threads: c_int, - pub check_multi_interp_extensions: c_int, - pub gil: c_int, -} - -#[cfg(Py_3_12)] -pub const _PyInterpreterConfig_INIT: PyInterpreterConfig = PyInterpreterConfig { - use_main_obmalloc: 0, - allow_fork: 0, - allow_exec: 0, - allow_threads: 1, - allow_daemon_threads: 0, - check_multi_interp_extensions: 1, - gil: PyInterpreterConfig_OWN_GIL, -}; - -#[cfg(Py_3_12)] -pub const _PyInterpreterConfig_LEGACY_INIT: PyInterpreterConfig = PyInterpreterConfig { - use_main_obmalloc: 1, - allow_fork: 1, - allow_exec: 1, - allow_threads: 1, - allow_daemon_threads: 1, - check_multi_interp_extensions: 0, - gil: PyInterpreterConfig_SHARED_GIL, -}; - -extern "C" { - #[cfg(Py_3_12)] - pub fn Py_NewInterpreterFromConfig( - tstate_p: *mut *mut crate::PyThreadState, - config: *const PyInterpreterConfig, - ) -> PyStatus; -} - -// skipped atexit_datacallbackfunc -// skipped _Py_AtExit diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pymem.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pymem.rs deleted file mode 100644 index c400fa30b0..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pymem.rs +++ /dev/null @@ -1,49 +0,0 @@ -use libc::size_t; -use std::os::raw::c_void; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")] - pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")] - pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")] - pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")] - pub fn PyMem_RawFree(ptr: *mut c_void); - - // skipped _PyMem_GetCurrentAllocatorName - // skipped _PyMem_RawStrdup - // skipped _PyMem_Strdup - // skipped _PyMem_RawWcsdup -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub enum PyMemAllocatorDomain { - PYMEM_DOMAIN_RAW, - PYMEM_DOMAIN_MEM, - PYMEM_DOMAIN_OBJ, -} - -// skipped PyMemAllocatorName -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyMemAllocatorEx { - pub ctx: *mut c_void, - pub malloc: Option *mut c_void>, - pub calloc: - Option *mut c_void>, - pub realloc: - Option *mut c_void>, - pub free: Option, -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx); - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyMem_SetupDebugHooks(); -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pystate.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pystate.rs deleted file mode 100644 index 5481265b55..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pystate.rs +++ /dev/null @@ -1,112 +0,0 @@ -#[cfg(not(PyPy))] -use crate::PyThreadState; -use crate::{PyFrameObject, PyInterpreterState, PyObject}; -use std::os::raw::c_int; - -// skipped _PyInterpreterState_RequiresIDRef -// skipped _PyInterpreterState_RequireIDRef - -// skipped _PyInterpreterState_GetMainModule - -pub type Py_tracefunc = unsafe extern "C" fn( - obj: *mut PyObject, - frame: *mut PyFrameObject, - what: c_int, - arg: *mut PyObject, -) -> c_int; - -pub const PyTrace_CALL: c_int = 0; -pub const PyTrace_EXCEPTION: c_int = 1; -pub const PyTrace_LINE: c_int = 2; -pub const PyTrace_RETURN: c_int = 3; -pub const PyTrace_C_CALL: c_int = 4; -pub const PyTrace_C_EXCEPTION: c_int = 5; -pub const PyTrace_C_RETURN: c_int = 6; -pub const PyTrace_OPCODE: c_int = 7; - -// skipped PyTraceInfo -// skipped CFrame - -#[cfg(not(PyPy))] -#[repr(C)] -#[derive(Clone, Copy)] -pub struct _PyErr_StackItem { - #[cfg(not(Py_3_11))] - pub exc_type: *mut PyObject, - pub exc_value: *mut PyObject, - #[cfg(not(Py_3_11))] - pub exc_traceback: *mut PyObject, - pub previous_item: *mut _PyErr_StackItem, -} - -// skipped _PyStackChunk -// skipped _ts (aka PyThreadState) - -extern "C" { - // skipped _PyThreadState_Prealloc - // skipped _PyThreadState_UncheckedGet - // skipped _PyThreadState_GetDict - - #[cfg_attr(PyPy, link_name = "PyPyGILState_Check")] - pub fn PyGILState_Check() -> c_int; - - // skipped _PyGILState_GetInterpreterStateUnsafe - // skipped _PyThread_CurrentFrames - // skipped _PyThread_CurrentExceptions - - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Main() -> *mut PyInterpreterState; - #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Head")] - pub fn PyInterpreterState_Head() -> *mut PyInterpreterState; - #[cfg_attr(PyPy, link_name = "PyPyInterpreterState_Next")] - pub fn PyInterpreterState_Next(interp: *mut PyInterpreterState) -> *mut PyInterpreterState; - #[cfg(not(PyPy))] - pub fn PyInterpreterState_ThreadHead(interp: *mut PyInterpreterState) -> *mut PyThreadState; - #[cfg(not(PyPy))] - pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState; - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")] - pub fn PyThreadState_DeleteCurrent(); -} - -#[cfg(all(Py_3_9, not(Py_3_11)))] -pub type _PyFrameEvalFunction = extern "C" fn( - *mut crate::PyThreadState, - *mut crate::PyFrameObject, - c_int, -) -> *mut crate::object::PyObject; - -#[cfg(Py_3_11)] -pub type _PyFrameEvalFunction = extern "C" fn( - *mut crate::PyThreadState, - *mut crate::_PyInterpreterFrame, - c_int, -) -> *mut crate::object::PyObject; - -#[cfg(Py_3_9)] -extern "C" { - /// Get the frame evaluation function. - pub fn _PyInterpreterState_GetEvalFrameFunc( - interp: *mut PyInterpreterState, - ) -> _PyFrameEvalFunction; - - ///Set the frame evaluation function. - pub fn _PyInterpreterState_SetEvalFrameFunc( - interp: *mut PyInterpreterState, - eval_frame: _PyFrameEvalFunction, - ); -} - -// skipped _PyInterpreterState_GetConfig -// skipped _PyInterpreterState_GetConfigCopy -// skipped _PyInterpreterState_SetConfig -// skipped _Py_GetConfig - -// skipped _PyCrossInterpreterData -// skipped _PyObject_GetCrossInterpreterData -// skipped _PyCrossInterpreterData_NewObject -// skipped _PyCrossInterpreterData_Release -// skipped _PyObject_CheckCrossInterpreterData -// skipped crossinterpdatafunc -// skipped _PyCrossInterpreterData_RegisterClass -// skipped _PyCrossInterpreterData_Lookup diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/pythonrun.rs b/vendor/pyo3/pyo3-ffi/src/cpython/pythonrun.rs deleted file mode 100644 index fe78f55ca0..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/pythonrun.rs +++ /dev/null @@ -1,250 +0,0 @@ -use crate::object::*; -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API, Py_3_10)))] -use crate::pyarena::PyArena; -use crate::PyCompilerFlags; -#[cfg(not(any(PyPy, GraalPy, Py_3_10)))] -use crate::{_mod, _node}; -use libc::FILE; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn PyRun_SimpleStringFlags(arg1: *const c_char, arg2: *mut PyCompilerFlags) -> c_int; - pub fn _PyRun_SimpleFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_AnyFileExFlags( - fp: *mut FILE, - filename: *const c_char, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn _PyRun_AnyFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_SimpleFileExFlags( - fp: *mut FILE, - filename: *const c_char, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveOneFlags( - fp: *mut FILE, - filename: *const c_char, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveOneObject( - fp: *mut FILE, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn PyRun_InteractiveLoopFlags( - fp: *mut FILE, - filename: *const c_char, - flags: *mut PyCompilerFlags, - ) -> c_int; - pub fn _PyRun_InteractiveLoopObject( - fp: *mut FILE, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> c_int; - - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromString( - s: *const c_char, - filename: *const c_char, - start: c_int, - flags: *mut PyCompilerFlags, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromStringObject( - s: *const c_char, - filename: *mut PyObject, - start: c_int, - flags: *mut PyCompilerFlags, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromFile( - fp: *mut FILE, - filename: *const c_char, - enc: *const c_char, - start: c_int, - ps1: *const c_char, - ps2: *const c_char, - flags: *mut PyCompilerFlags, - errcode: *mut c_int, - arena: *mut PyArena, - ) -> *mut _mod; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - pub fn PyParser_ASTFromFileObject( - fp: *mut FILE, - filename: *mut PyObject, - enc: *const c_char, - start: c_int, - ps1: *const c_char, - ps2: *const c_char, - flags: *mut PyCompilerFlags, - errcode: *mut c_int, - arena: *mut PyArena, - ) -> *mut _mod; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRun_StringFlags")] - pub fn PyRun_StringFlags( - arg1: *const c_char, - arg2: c_int, - arg3: *mut PyObject, - arg4: *mut PyObject, - arg5: *mut PyCompilerFlags, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileExFlags( - fp: *mut FILE, - filename: *const c_char, - start: c_int, - globals: *mut PyObject, - locals: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> *mut PyObject; - - #[cfg(not(any(PyPy, GraalPy)))] - pub fn Py_CompileStringExFlags( - str: *const c_char, - filename: *const c_char, - start: c_int, - flags: *mut PyCompilerFlags, - optimize: c_int, - ) -> *mut PyObject; - #[cfg(not(Py_LIMITED_API))] - pub fn Py_CompileStringObject( - str: *const c_char, - filename: *mut PyObject, - start: c_int, - flags: *mut PyCompilerFlags, - optimize: c_int, - ) -> *mut PyObject; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject { - Py_CompileStringExFlags(string, p, s, std::ptr::null_mut(), -1) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut PyCompilerFlags, -) -> *mut PyObject { - Py_CompileStringExFlags(string, p, s, f, -1) -} - -// skipped _Py_SourceAsString - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRun_String")] - pub fn PyRun_String( - string: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFile(fp: *mut FILE, name: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFileEx(fp: *mut FILE, name: *const c_char, closeit: c_int) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_AnyFileFlags( - arg1: *mut FILE, - arg2: *const c_char, - arg3: *mut PyCompilerFlags, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyRun_SimpleString")] - pub fn PyRun_SimpleString(s: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_SimpleFile(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_SimpleFileEx(f: *mut FILE, p: *const c_char, c: c_int) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_InteractiveOne(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_InteractiveLoop(f: *mut FILE, p: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyRun_File")] - pub fn PyRun_File( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileEx( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - c: c_int, - ) -> *mut PyObject; - #[cfg(not(any(PyPy, GraalPy)))] - pub fn PyRun_FileFlags( - fp: *mut FILE, - p: *const c_char, - s: c_int, - g: *mut PyObject, - l: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> *mut PyObject; -} - -// skipped macro PyRun_String -// skipped macro PyRun_AnyFile -// skipped macro PyRun_AnyFileEx -// skipped macro PyRun_AnyFileFlags - -extern "C" { - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseStringFlags( - arg1: *const c_char, - arg2: c_int, - arg3: c_int, - ) -> *mut _node; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseStringFlagsFilename( - arg1: *const c_char, - arg2: *const c_char, - arg3: c_int, - arg4: c_int, - ) -> *mut _node; - #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyParser_SimpleParseFileFlags( - arg1: *mut FILE, - arg2: *const c_char, - arg3: c_int, - arg4: c_int, - ) -> *mut _node; - - #[cfg(PyPy)] - #[cfg_attr(PyPy, link_name = "PyPy_CompileStringFlags")] - pub fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut PyCompilerFlags, - ) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/tupleobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/tupleobject.rs deleted file mode 100644 index 9616d4372c..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/tupleobject.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::object::*; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; - -#[repr(C)] -pub struct PyTupleObject { - pub ob_base: PyVarObject, - pub ob_item: [*mut PyObject; 1], -} - -// skipped _PyTuple_Resize -// skipped _PyTuple_MaybeUntrack - -// skipped _PyTuple_CAST - -/// Macro, trading safety for speed -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { - Py_SIZE(op) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - *(*(op as *mut PyTupleObject)).ob_item.as_ptr().offset(i) -} - -/// Macro, *only* to be used to fill in brand new tuples -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyTuple_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - *(*(op as *mut PyTupleObject)).ob_item.as_mut_ptr().offset(i) = v; -} - -// skipped _PyTuple_DebugMallocStats diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs deleted file mode 100644 index fae626b8d2..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/unicodeobject.rs +++ /dev/null @@ -1,784 +0,0 @@ -#[cfg(not(PyPy))] -use crate::Py_hash_t; -use crate::{PyObject, Py_UCS1, Py_UCS2, Py_UCS4, Py_ssize_t}; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_uint, c_void}; - -// skipped Py_UNICODE_ISSPACE() -// skipped Py_UNICODE_ISLOWER() -// skipped Py_UNICODE_ISUPPER() -// skipped Py_UNICODE_ISTITLE() -// skipped Py_UNICODE_ISLINEBREAK -// skipped Py_UNICODE_TOLOWER -// skipped Py_UNICODE_TOUPPER -// skipped Py_UNICODE_TOTITLE -// skipped Py_UNICODE_ISDECIMAL -// skipped Py_UNICODE_ISDIGIT -// skipped Py_UNICODE_ISNUMERIC -// skipped Py_UNICODE_ISPRINTABLE -// skipped Py_UNICODE_TODECIMAL -// skipped Py_UNICODE_TODIGIT -// skipped Py_UNICODE_TONUMERIC -// skipped Py_UNICODE_ISALPHA -// skipped Py_UNICODE_ISALNUM -// skipped Py_UNICODE_COPY -// skipped Py_UNICODE_FILL -// skipped Py_UNICODE_IS_SURROGATE -// skipped Py_UNICODE_IS_HIGH_SURROGATE -// skipped Py_UNICODE_IS_LOW_SURROGATE -// skipped Py_UNICODE_JOIN_SURROGATES -// skipped Py_UNICODE_HIGH_SURROGATE -// skipped Py_UNICODE_LOW_SURROGATE - -// generated by bindgen v0.63.0 (with small adaptations) -#[repr(C)] -struct BitfieldUnit { - storage: Storage, -} - -impl BitfieldUnit { - #[inline] - pub const fn new(storage: Storage) -> Self { - Self { storage } - } -} - -#[cfg(not(GraalPy))] -impl BitfieldUnit -where - Storage: AsRef<[u8]> + AsMut<[u8]>, -{ - #[inline] - fn get_bit(&self, index: usize) -> bool { - debug_assert!(index / 8 < self.storage.as_ref().len()); - let byte_index = index / 8; - let byte = self.storage.as_ref()[byte_index]; - let bit_index = if cfg!(target_endian = "big") { - 7 - (index % 8) - } else { - index % 8 - }; - let mask = 1 << bit_index; - byte & mask == mask - } - - #[inline] - fn set_bit(&mut self, index: usize, val: bool) { - debug_assert!(index / 8 < self.storage.as_ref().len()); - let byte_index = index / 8; - let byte = &mut self.storage.as_mut()[byte_index]; - let bit_index = if cfg!(target_endian = "big") { - 7 - (index % 8) - } else { - index % 8 - }; - let mask = 1 << bit_index; - if val { - *byte |= mask; - } else { - *byte &= !mask; - } - } - - #[inline] - fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { - debug_assert!(bit_width <= 64); - debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); - debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); - let mut val = 0; - for i in 0..(bit_width as usize) { - if self.get_bit(i + bit_offset) { - let index = if cfg!(target_endian = "big") { - bit_width as usize - 1 - i - } else { - i - }; - val |= 1 << index; - } - } - val - } - - #[inline] - fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) { - debug_assert!(bit_width <= 64); - debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); - debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); - for i in 0..(bit_width as usize) { - let mask = 1 << i; - let val_bit_is_set = val & mask == mask; - let index = if cfg!(target_endian = "big") { - bit_width as usize - 1 - i - } else { - i - }; - self.set_bit(index + bit_offset, val_bit_is_set); - } - } -} - -#[cfg(not(GraalPy))] -const STATE_INTERNED_INDEX: usize = 0; -#[cfg(not(GraalPy))] -const STATE_INTERNED_WIDTH: u8 = 2; - -#[cfg(not(GraalPy))] -const STATE_KIND_INDEX: usize = STATE_INTERNED_WIDTH as usize; -#[cfg(not(GraalPy))] -const STATE_KIND_WIDTH: u8 = 3; - -#[cfg(not(GraalPy))] -const STATE_COMPACT_INDEX: usize = (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH) as usize; -#[cfg(not(GraalPy))] -const STATE_COMPACT_WIDTH: u8 = 1; - -#[cfg(not(GraalPy))] -const STATE_ASCII_INDEX: usize = - (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH) as usize; -#[cfg(not(GraalPy))] -const STATE_ASCII_WIDTH: u8 = 1; - -#[cfg(not(any(Py_3_12, GraalPy)))] -const STATE_READY_INDEX: usize = - (STATE_INTERNED_WIDTH + STATE_KIND_WIDTH + STATE_COMPACT_WIDTH + STATE_ASCII_WIDTH) as usize; -#[cfg(not(any(Py_3_12, GraalPy)))] -const STATE_READY_WIDTH: u8 = 1; - -// generated by bindgen v0.63.0 (with small adaptations) -// The same code is generated for Python 3.7, 3.8, 3.9, 3.10, and 3.11, but the "ready" field -// has been removed from Python 3.12. - -/// Wrapper around the `PyASCIIObject.state` bitfield with getters and setters that work -/// on most little- and big-endian architectures. -/// -/// Memory layout of C bitfields is implementation defined, so these functions are still -/// unsafe. Users must verify that they work as expected on the architectures they target. -#[repr(C)] -#[repr(align(4))] -struct PyASCIIObjectState { - bitfield_align: [u8; 0], - bitfield: BitfieldUnit<[u8; 4usize]>, -} - -// c_uint and u32 are not necessarily the same type on all targets / architectures -#[cfg(not(GraalPy))] -#[allow(clippy::useless_transmute)] -impl PyASCIIObjectState { - #[inline] - unsafe fn interned(&self) -> c_uint { - std::mem::transmute( - self.bitfield - .get(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH) as u32, - ) - } - - #[inline] - unsafe fn set_interned(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_INTERNED_INDEX, STATE_INTERNED_WIDTH, val as u64) - } - - #[inline] - unsafe fn kind(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_KIND_INDEX, STATE_KIND_WIDTH) as u32) - } - - #[inline] - unsafe fn set_kind(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_KIND_INDEX, STATE_KIND_WIDTH, val as u64) - } - - #[inline] - unsafe fn compact(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH) as u32) - } - - #[inline] - unsafe fn set_compact(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_COMPACT_INDEX, STATE_COMPACT_WIDTH, val as u64) - } - - #[inline] - unsafe fn ascii(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_ASCII_INDEX, STATE_ASCII_WIDTH) as u32) - } - - #[inline] - unsafe fn set_ascii(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_ASCII_INDEX, STATE_ASCII_WIDTH, val as u64) - } - - #[cfg(not(Py_3_12))] - #[inline] - unsafe fn ready(&self) -> c_uint { - std::mem::transmute(self.bitfield.get(STATE_READY_INDEX, STATE_READY_WIDTH) as u32) - } - - #[cfg(not(Py_3_12))] - #[inline] - unsafe fn set_ready(&mut self, val: c_uint) { - let val: u32 = std::mem::transmute(val); - self.bitfield - .set(STATE_READY_INDEX, STATE_READY_WIDTH, val as u64) - } -} - -impl From for PyASCIIObjectState { - #[inline] - fn from(value: u32) -> Self { - PyASCIIObjectState { - bitfield_align: [], - bitfield: BitfieldUnit::new(value.to_ne_bytes()), - } - } -} - -impl From for u32 { - #[inline] - fn from(value: PyASCIIObjectState) -> Self { - u32::from_ne_bytes(value.bitfield.storage) - } -} - -#[repr(C)] -pub struct PyASCIIObject { - pub ob_base: PyObject, - pub length: Py_ssize_t, - #[cfg(not(PyPy))] - pub hash: Py_hash_t, - /// A bit field with various properties. - /// - /// Rust doesn't expose bitfields. So we have accessor functions for - /// retrieving values. - /// - /// unsigned int interned:2; // SSTATE_* constants. - /// unsigned int kind:3; // PyUnicode_*_KIND constants. - /// unsigned int compact:1; - /// unsigned int ascii:1; - /// unsigned int ready:1; - /// unsigned int :24; - pub state: u32, - #[cfg(not(Py_3_12))] - pub wstr: *mut wchar_t, -} - -/// Interacting with the bitfield is not actually well-defined, so we mark these APIs unsafe. -#[cfg(not(GraalPy))] -impl PyASCIIObject { - #[cfg_attr(not(Py_3_12), allow(rustdoc::broken_intra_doc_links))] // SSTATE_INTERNED_IMMORTAL_STATIC requires 3.12 - /// Get the `interned` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns one of: [`SSTATE_NOT_INTERNED`], [`SSTATE_INTERNED_MORTAL`], - /// [`SSTATE_INTERNED_IMMORTAL`], or [`SSTATE_INTERNED_IMMORTAL_STATIC`]. - #[inline] - pub unsafe fn interned(&self) -> c_uint { - PyASCIIObjectState::from(self.state).interned() - } - - #[cfg_attr(not(Py_3_12), allow(rustdoc::broken_intra_doc_links))] // SSTATE_INTERNED_IMMORTAL_STATIC requires 3.12 - /// Set the `interned` field of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is not [`SSTATE_NOT_INTERNED`], - /// [`SSTATE_INTERNED_MORTAL`], [`SSTATE_INTERNED_IMMORTAL`], or - /// [`SSTATE_INTERNED_IMMORTAL_STATIC`] is invalid. - #[inline] - pub unsafe fn set_interned(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_interned(val); - self.state = u32::from(state); - } - - /// Get the `kind` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns one of: - #[cfg_attr(not(Py_3_12), doc = "[`PyUnicode_WCHAR_KIND`], ")] - /// [`PyUnicode_1BYTE_KIND`], [`PyUnicode_2BYTE_KIND`], or [`PyUnicode_4BYTE_KIND`]. - #[inline] - pub unsafe fn kind(&self) -> c_uint { - PyASCIIObjectState::from(self.state).kind() - } - - /// Set the `kind` field of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is not - #[cfg_attr(not(Py_3_12), doc = "[`PyUnicode_WCHAR_KIND`], ")] - /// [`PyUnicode_1BYTE_KIND`], [`PyUnicode_2BYTE_KIND`], or [`PyUnicode_4BYTE_KIND`] is invalid. - #[inline] - pub unsafe fn set_kind(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_kind(val); - self.state = u32::from(state); - } - - /// Get the `compact` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[inline] - pub unsafe fn compact(&self) -> c_uint { - PyASCIIObjectState::from(self.state).compact() - } - - /// Set the `compact` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[inline] - pub unsafe fn set_compact(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_compact(val); - self.state = u32::from(state); - } - - /// Get the `ascii` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[inline] - pub unsafe fn ascii(&self) -> c_uint { - PyASCIIObjectState::from(self.state).ascii() - } - - /// Set the `ascii` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[inline] - pub unsafe fn set_ascii(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_ascii(val); - self.state = u32::from(state); - } - - /// Get the `ready` field of the [`PyASCIIObject`] state bitfield. - /// - /// Returns either `0` or `1`. - #[cfg(not(Py_3_12))] - #[inline] - pub unsafe fn ready(&self) -> c_uint { - PyASCIIObjectState::from(self.state).ready() - } - - /// Set the `ready` flag of the [`PyASCIIObject`] state bitfield. - /// - /// Calling this function with an argument that is neither `0` nor `1` is invalid. - #[cfg(not(Py_3_12))] - #[inline] - pub unsafe fn set_ready(&mut self, val: c_uint) { - let mut state = PyASCIIObjectState::from(self.state); - state.set_ready(val); - self.state = u32::from(state); - } -} - -#[repr(C)] -pub struct PyCompactUnicodeObject { - pub _base: PyASCIIObject, - pub utf8_length: Py_ssize_t, - pub utf8: *mut c_char, - #[cfg(not(Py_3_12))] - pub wstr_length: Py_ssize_t, -} - -#[repr(C)] -pub union PyUnicodeObjectData { - pub any: *mut c_void, - pub latin1: *mut Py_UCS1, - pub ucs2: *mut Py_UCS2, - pub ucs4: *mut Py_UCS4, -} - -#[repr(C)] -pub struct PyUnicodeObject { - pub _base: PyCompactUnicodeObject, - pub data: PyUnicodeObjectData, -} - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PyUnicode_CheckConsistency(op: *mut PyObject, check_content: c_int) -> c_int; -} - -// skipped PyUnicode_GET_SIZE -// skipped PyUnicode_GET_DATA_SIZE -// skipped PyUnicode_AS_UNICODE -// skipped PyUnicode_AS_DATA - -pub const SSTATE_NOT_INTERNED: c_uint = 0; -pub const SSTATE_INTERNED_MORTAL: c_uint = 1; -pub const SSTATE_INTERNED_IMMORTAL: c_uint = 2; -#[cfg(Py_3_12)] -pub const SSTATE_INTERNED_IMMORTAL_STATIC: c_uint = 3; - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_ASCII(op: *mut PyObject) -> c_uint { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).ascii() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_COMPACT(op: *mut PyObject) -> c_uint { - (*(op as *mut PyASCIIObject)).compact() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_COMPACT_ASCII(op: *mut PyObject) -> c_uint { - ((*(op as *mut PyASCIIObject)).ascii() != 0 && PyUnicode_IS_COMPACT(op) != 0).into() -} - -#[cfg(not(Py_3_12))] -#[deprecated(note = "Removed in Python 3.12")] -pub const PyUnicode_WCHAR_KIND: c_uint = 0; - -pub const PyUnicode_1BYTE_KIND: c_uint = 1; -pub const PyUnicode_2BYTE_KIND: c_uint = 2; -pub const PyUnicode_4BYTE_KIND: c_uint = 4; - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_1BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS1 { - PyUnicode_DATA(op) as *mut Py_UCS1 -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_2BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS2 { - PyUnicode_DATA(op) as *mut Py_UCS2 -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_4BYTE_DATA(op: *mut PyObject) -> *mut Py_UCS4 { - PyUnicode_DATA(op) as *mut Py_UCS4 -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_KIND(op: *mut PyObject) -> c_uint { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).kind() -} - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void { - if PyUnicode_IS_ASCII(op) != 0 { - (op as *mut PyASCIIObject).offset(1) as *mut c_void - } else { - (op as *mut PyCompactUnicodeObject).offset(1) as *mut c_void - } -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn _PyUnicode_NONCOMPACT_DATA(op: *mut PyObject) -> *mut c_void { - debug_assert!(!(*(op as *mut PyUnicodeObject)).data.any.is_null()); - - (*(op as *mut PyUnicodeObject)).data.any -} - -#[cfg(not(any(GraalPy, PyPy)))] -#[inline] -pub unsafe fn PyUnicode_DATA(op: *mut PyObject) -> *mut c_void { - debug_assert!(crate::PyUnicode_Check(op) != 0); - - if PyUnicode_IS_COMPACT(op) != 0 { - _PyUnicode_COMPACT_DATA(op) - } else { - _PyUnicode_NONCOMPACT_DATA(op) - } -} - -// skipped PyUnicode_WRITE -// skipped PyUnicode_READ -// skipped PyUnicode_READ_CHAR - -#[cfg(not(GraalPy))] -#[inline] -pub unsafe fn PyUnicode_GET_LENGTH(op: *mut PyObject) -> Py_ssize_t { - debug_assert!(crate::PyUnicode_Check(op) != 0); - #[cfg(not(Py_3_12))] - debug_assert!(PyUnicode_IS_READY(op) != 0); - - (*(op as *mut PyASCIIObject)).length -} - -#[cfg(any(Py_3_12, GraalPy))] -#[inline] -pub unsafe fn PyUnicode_IS_READY(_op: *mut PyObject) -> c_uint { - // kept in CPython for backwards compatibility - 1 -} - -#[cfg(not(any(GraalPy, Py_3_12)))] -#[inline] -pub unsafe fn PyUnicode_IS_READY(op: *mut PyObject) -> c_uint { - (*(op as *mut PyASCIIObject)).ready() -} - -#[cfg(any(Py_3_12, GraalPy))] -#[inline] -pub unsafe fn PyUnicode_READY(_op: *mut PyObject) -> c_int { - 0 -} - -#[cfg(not(any(Py_3_12, GraalPy)))] -#[inline] -pub unsafe fn PyUnicode_READY(op: *mut PyObject) -> c_int { - debug_assert!(crate::PyUnicode_Check(op) != 0); - - if PyUnicode_IS_READY(op) != 0 { - 0 - } else { - _PyUnicode_Ready(op) - } -} - -// skipped PyUnicode_MAX_CHAR_VALUE -// skipped _PyUnicode_get_wstr_length -// skipped PyUnicode_WSTR_LENGTH - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyUnicode_New")] - pub fn PyUnicode_New(size: Py_ssize_t, maxchar: Py_UCS4) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyUnicode_Ready")] - pub fn _PyUnicode_Ready(unicode: *mut PyObject) -> c_int; - - // skipped _PyUnicode_Copy - - #[cfg(not(PyPy))] - pub fn PyUnicode_CopyCharacters( - to: *mut PyObject, - to_start: Py_ssize_t, - from: *mut PyObject, - from_start: Py_ssize_t, - how_many: Py_ssize_t, - ) -> Py_ssize_t; - - // skipped _PyUnicode_FastCopyCharacters - - #[cfg(not(PyPy))] - pub fn PyUnicode_Fill( - unicode: *mut PyObject, - start: Py_ssize_t, - length: Py_ssize_t, - fill_char: Py_UCS4, - ) -> Py_ssize_t; - - // skipped _PyUnicode_FastFill - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromUnicode")] - pub fn PyUnicode_FromUnicode(u: *const wchar_t, size: Py_ssize_t) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromKindAndData")] - pub fn PyUnicode_FromKindAndData( - kind: c_int, - buffer: *const c_void, - size: Py_ssize_t, - ) -> *mut PyObject; - - // skipped _PyUnicode_FromASCII - // skipped _PyUnicode_FindMaxChar - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicode")] - pub fn PyUnicode_AsUnicode(unicode: *mut PyObject) -> *mut wchar_t; - - // skipped _PyUnicode_AsUnicode - - #[cfg(not(Py_3_12))] - #[deprecated] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicodeAndSize")] - pub fn PyUnicode_AsUnicodeAndSize( - unicode: *mut PyObject, - size: *mut Py_ssize_t, - ) -> *mut wchar_t; - - // skipped PyUnicode_GetMax -} - -// skipped _PyUnicodeWriter -// skipped _PyUnicodeWriter_Init -// skipped _PyUnicodeWriter_Prepare -// skipped _PyUnicodeWriter_PrepareInternal -// skipped _PyUnicodeWriter_PrepareKind -// skipped _PyUnicodeWriter_PrepareKindInternal -// skipped _PyUnicodeWriter_WriteChar -// skipped _PyUnicodeWriter_WriteStr -// skipped _PyUnicodeWriter_WriteSubstring -// skipped _PyUnicodeWriter_WriteASCIIString -// skipped _PyUnicodeWriter_WriteLatin1String -// skipped _PyUnicodeWriter_Finish -// skipped _PyUnicodeWriter_Dealloc -// skipped _PyUnicode_FormatAdvancedWriter - -extern "C" { - // skipped _PyUnicode_AsStringAndSize - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8")] - pub fn PyUnicode_AsUTF8(unicode: *mut PyObject) -> *const c_char; - - // skipped _PyUnicode_AsString - - pub fn PyUnicode_Encode( - s: *const wchar_t, - size: Py_ssize_t, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeUTF7( - data: *const wchar_t, - length: Py_ssize_t, - base64SetO: c_int, - base64WhiteSpace: c_int, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF7 - // skipped _PyUnicode_AsUTF8String - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeUTF8")] - pub fn PyUnicode_EncodeUTF8( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeUTF32( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - byteorder: c_int, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF32 - - pub fn PyUnicode_EncodeUTF16( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - byteorder: c_int, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeUTF16 - // skipped _PyUnicode_DecodeUnicodeEscape - - pub fn PyUnicode_EncodeUnicodeEscape(data: *const wchar_t, length: Py_ssize_t) - -> *mut PyObject; - - pub fn PyUnicode_EncodeRawUnicodeEscape( - data: *const wchar_t, - length: Py_ssize_t, - ) -> *mut PyObject; - - // skipped _PyUnicode_AsLatin1String - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeLatin1")] - pub fn PyUnicode_EncodeLatin1( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_AsASCIIString - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeASCII")] - pub fn PyUnicode_EncodeASCII( - data: *const wchar_t, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - - pub fn PyUnicode_EncodeCharmap( - data: *const wchar_t, - length: Py_ssize_t, - mapping: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped _PyUnicode_EncodeCharmap - - pub fn PyUnicode_TranslateCharmap( - data: *const wchar_t, - length: Py_ssize_t, - table: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - - // skipped PyUnicode_EncodeMBCS - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeDecimal")] - pub fn PyUnicode_EncodeDecimal( - s: *mut wchar_t, - length: Py_ssize_t, - output: *mut c_char, - errors: *const c_char, - ) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_TransformDecimalToASCII")] - pub fn PyUnicode_TransformDecimalToASCII(s: *mut wchar_t, length: Py_ssize_t) -> *mut PyObject; - - // skipped _PyUnicode_TransformDecimalAndSpaceToASCII -} - -// skipped _PyUnicode_JoinArray -// skipped _PyUnicode_EqualToASCIIId -// skipped _PyUnicode_EqualToASCIIString -// skipped _PyUnicode_XStrip -// skipped _PyUnicode_InsertThousandsGrouping - -// skipped _Py_ascii_whitespace - -// skipped _PyUnicode_IsLowercase -// skipped _PyUnicode_IsUppercase -// skipped _PyUnicode_IsTitlecase -// skipped _PyUnicode_IsXidStart -// skipped _PyUnicode_IsXidContinue -// skipped _PyUnicode_IsWhitespace -// skipped _PyUnicode_IsLinebreak -// skipped _PyUnicode_ToLowercase -// skipped _PyUnicode_ToUppercase -// skipped _PyUnicode_ToTitlecase -// skipped _PyUnicode_ToLowerFull -// skipped _PyUnicode_ToTitleFull -// skipped _PyUnicode_ToUpperFull -// skipped _PyUnicode_ToFoldedFull -// skipped _PyUnicode_IsCaseIgnorable -// skipped _PyUnicode_IsCased -// skipped _PyUnicode_ToDecimalDigit -// skipped _PyUnicode_ToDigit -// skipped _PyUnicode_ToNumeric -// skipped _PyUnicode_IsDecimalDigit -// skipped _PyUnicode_IsDigit -// skipped _PyUnicode_IsNumeric -// skipped _PyUnicode_IsPrintable -// skipped _PyUnicode_IsAlpha -// skipped Py_UNICODE_strlen -// skipped Py_UNICODE_strcpy -// skipped Py_UNICODE_strcat -// skipped Py_UNICODE_strncpy -// skipped Py_UNICODE_strcmp -// skipped Py_UNICODE_strncmp -// skipped Py_UNICODE_strchr -// skipped Py_UNICODE_strrchr -// skipped _PyUnicode_FormatLong -// skipped PyUnicode_AsUnicodeCopy -// skipped _PyUnicode_FromId -// skipped _PyUnicode_EQ -// skipped _PyUnicode_ScanIdentifier diff --git a/vendor/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs b/vendor/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs deleted file mode 100644 index 88bb501bcc..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/cpython/weakrefobject.rs +++ /dev/null @@ -1,17 +0,0 @@ -#[cfg(not(any(PyPy, GraalPy)))] -pub struct _PyWeakReference { - pub ob_base: crate::PyObject, - pub wr_object: *mut crate::PyObject, - pub wr_callback: *mut crate::PyObject, - pub hash: crate::Py_hash_t, - pub wr_prev: *mut crate::PyWeakReference, - pub wr_next: *mut crate::PyWeakReference, - #[cfg(Py_3_11)] - pub vectorcall: Option, - #[cfg(all(Py_3_13, Py_GIL_DISABLED))] - pub weakrefs_lock: *mut crate::PyMutex, -} - -// skipped _PyWeakref_GetWeakrefCount -// skipped _PyWeakref_ClearRef -// skipped PyWeakRef_GET_OBJECT diff --git a/vendor/pyo3/pyo3-ffi/src/datetime.rs b/vendor/pyo3/pyo3-ffi/src/datetime.rs deleted file mode 100644 index 7f2d795836..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/datetime.rs +++ /dev/null @@ -1,751 +0,0 @@ -//! FFI bindings to the functions and structs defined in `datetime.h` -//! -//! This is the unsafe thin wrapper around the [CPython C API](https://docs.python.org/3/c-api/datetime.html), -//! and covers the various date and time related objects in the Python `datetime` -//! standard library module. - -#[cfg(not(PyPy))] -use crate::PyCapsule_Import; -#[cfg(GraalPy)] -use crate::{PyLong_AsLong, PyLong_Check, PyObject_GetAttrString, Py_DecRef}; -use crate::{PyObject, PyObject_TypeCheck, PyTypeObject, Py_TYPE}; -use std::os::raw::c_char; -use std::os::raw::c_int; -use std::ptr; -use std::sync::Once; -use std::{cell::UnsafeCell, ffi::CStr}; -#[cfg(not(PyPy))] -use {crate::Py_hash_t, std::os::raw::c_uchar}; -// Type struct wrappers -const _PyDateTime_DATE_DATASIZE: usize = 4; -const _PyDateTime_TIME_DATASIZE: usize = 6; -const _PyDateTime_DATETIME_DATASIZE: usize = 10; - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.timedelta`. -pub struct PyDateTime_Delta { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub hashcode: Py_hash_t, - pub days: c_int, - pub seconds: c_int, - pub microseconds: c_int, -} - -// skipped non-limited PyDateTime_TZInfo -// skipped non-limited _PyDateTime_BaseTZInfo - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.time` without a `tzinfo` member. -pub struct _PyDateTime_BaseTime { - pub ob_base: PyObject, - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - pub data: [c_uchar; _PyDateTime_TIME_DATASIZE], -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.time`. -pub struct PyDateTime_Time { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - #[cfg(not(PyPy))] - pub data: [c_uchar; _PyDateTime_TIME_DATASIZE], - #[cfg(not(PyPy))] - pub fold: c_uchar, - /// # Safety - /// - /// Care should be taken when reading this field. If the time does not have a - /// tzinfo then CPython may allocate as a `_PyDateTime_BaseTime` without this field. - pub tzinfo: *mut PyObject, -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.date` -pub struct PyDateTime_Date { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub hashcode: Py_hash_t, - #[cfg(not(PyPy))] - pub hastzinfo: c_char, - #[cfg(not(PyPy))] - pub data: [c_uchar; _PyDateTime_DATE_DATASIZE], -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.datetime` without a `tzinfo` member. -pub struct _PyDateTime_BaseDateTime { - pub ob_base: PyObject, - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE], -} - -#[repr(C)] -#[derive(Debug)] -/// Structure representing a `datetime.datetime`. -pub struct PyDateTime_DateTime { - pub ob_base: PyObject, - #[cfg(not(PyPy))] - pub hashcode: Py_hash_t, - pub hastzinfo: c_char, - #[cfg(not(PyPy))] - pub data: [c_uchar; _PyDateTime_DATETIME_DATASIZE], - #[cfg(not(PyPy))] - pub fold: c_uchar, - /// # Safety - /// - /// Care should be taken when reading this field. If the time does not have a - /// tzinfo then CPython may allocate as a `_PyDateTime_BaseDateTime` without this field. - pub tzinfo: *mut PyObject, -} - -// skipped non-limited _PyDateTime_HAS_TZINFO - -// Accessor functions for PyDateTime_Date and PyDateTime_DateTime -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the year component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer greater than 0. -pub unsafe fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int { - // This should work for Date or DateTime - let data = (*(o as *mut PyDateTime_Date)).data; - (c_int::from(data[0]) << 8) | c_int::from(data[1]) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the month component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer in the range `[1, 12]`. -pub unsafe fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int { - let data = (*(o as *mut PyDateTime_Date)).data; - c_int::from(data[2]) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the day component of a `PyDateTime_Date` or `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[1, 31]`. -pub unsafe fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int { - let data = (*(o as *mut PyDateTime_Date)).data; - c_int::from(data[3]) -} - -// Accessor macros for times -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_HOUR { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 0]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_MINUTE { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 1]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_SECOND { - ($o: expr, $offset:expr) => { - c_int::from((*$o).data[$offset + 2]) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_MICROSECOND { - ($o: expr, $offset:expr) => { - (c_int::from((*$o).data[$offset + 3]) << 16) - | (c_int::from((*$o).data[$offset + 4]) << 8) - | (c_int::from((*$o).data[$offset + 5])) - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_FOLD { - ($o: expr) => { - (*$o).fold - }; -} - -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _PyDateTime_GET_TZINFO { - ($o: expr) => { - if (*$o).hastzinfo != 0 { - (*$o).tzinfo - } else { - $crate::Py_None() - } - }; -} - -// Accessor functions for DateTime -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the hour component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 23]` -pub unsafe fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int { - _PyDateTime_GET_HOUR!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the minute component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MINUTE!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the second component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_SECOND!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the microsecond component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 999999]` -pub unsafe fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MICROSECOND!((o as *mut PyDateTime_DateTime), _PyDateTime_DATE_DATASIZE) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the fold component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 1]` -pub unsafe fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_uchar { - _PyDateTime_GET_FOLD!(o as *mut PyDateTime_DateTime) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the tzinfo component of a `PyDateTime_DateTime`. -/// Returns a pointer to a `PyObject` that should be either NULL or an instance -/// of a `datetime.tzinfo` subclass. -pub unsafe fn PyDateTime_DATE_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - _PyDateTime_GET_TZINFO!(o as *mut PyDateTime_DateTime) -} - -// Accessor functions for Time -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the hour component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 23]` -pub unsafe fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int { - _PyDateTime_GET_HOUR!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the minute component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MINUTE!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the second component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 59]` -pub unsafe fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_SECOND!((o as *mut PyDateTime_Time), 0) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the microsecond component of a `PyDateTime_DateTime`. -/// Returns a signed integer in the interval `[0, 999999]` -pub unsafe fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _PyDateTime_GET_MICROSECOND!((o as *mut PyDateTime_Time), 0) -} - -#[cfg(not(any(PyPy, GraalPy)))] -#[inline] -/// Retrieve the fold component of a `PyDateTime_Time`. -/// Returns a signed integer in the interval `[0, 1]` -pub unsafe fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_uchar { - _PyDateTime_GET_FOLD!(o as *mut PyDateTime_Time) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the tzinfo component of a `PyDateTime_Time`. -/// Returns a pointer to a `PyObject` that should be either NULL or an instance -/// of a `datetime.tzinfo` subclass. -pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - _PyDateTime_GET_TZINFO!(o as *mut PyDateTime_Time) -} - -// Accessor functions -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _access_field { - ($obj:expr, $type: ident, $field:ident) => { - (*($obj as *mut $type)).$field - }; -} - -// Accessor functions for PyDateTime_Delta -#[cfg(not(any(PyPy, GraalPy)))] -macro_rules! _access_delta_field { - ($obj:expr, $field:ident) => { - _access_field!($obj, PyDateTime_Delta, $field) - }; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the days component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [-999999999, 999999999]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, days) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the seconds component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [0, 86399]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, seconds) -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -/// Retrieve the seconds component of a `PyDateTime_Delta`. -/// -/// Returns a signed integer in the interval [0, 999999]. -/// -/// Note: This retrieves a component from the underlying structure, it is *not* -/// a representation of the total duration of the structure. -pub unsafe fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int { - _access_delta_field!(o, microseconds) -} - -// Accessor functions for GraalPy. The macros on GraalPy work differently, -// but copying them seems suboptimal -#[inline] -#[cfg(GraalPy)] -pub unsafe fn _get_attr(obj: *mut PyObject, field: &std::ffi::CStr) -> c_int { - let result = PyObject_GetAttrString(obj, field.as_ptr()); - Py_DecRef(result); // the original macros are borrowing - if PyLong_Check(result) == 1 { - PyLong_AsLong(result) as c_int - } else { - 0 - } -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("year")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("month")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("day")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("hour")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("minute")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("second")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microsecond")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("fold")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DATE_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - let res = PyObject_GetAttrString(o, c_str!("tzinfo").as_ptr().cast()); - Py_DecRef(res); // the original macros are borrowing - res -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("hour")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("minute")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("second")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microsecond")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("fold")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_TIME_GET_TZINFO(o: *mut PyObject) -> *mut PyObject { - let res = PyObject_GetAttrString(o, c_str!("tzinfo").as_ptr().cast()); - Py_DecRef(res); // the original macros are borrowing - res -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("days")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("seconds")) -} - -#[inline] -#[cfg(GraalPy)] -pub unsafe fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int { - _get_attr(o, c_str!("microseconds")) -} - -#[cfg(PyPy)] -extern "C" { - // skipped _PyDateTime_HAS_TZINFO (not in PyPy) - #[link_name = "PyPyDateTime_GET_YEAR"] - pub fn PyDateTime_GET_YEAR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_MONTH"] - pub fn PyDateTime_GET_MONTH(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_DAY"] - pub fn PyDateTime_GET_DAY(o: *mut PyObject) -> c_int; - - #[link_name = "PyPyDateTime_DATE_GET_HOUR"] - pub fn PyDateTime_DATE_GET_HOUR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_MINUTE"] - pub fn PyDateTime_DATE_GET_MINUTE(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_SECOND"] - pub fn PyDateTime_DATE_GET_SECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DATE_GET_MICROSECOND"] - pub fn PyDateTime_DATE_GET_MICROSECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_GET_FOLD"] - pub fn PyDateTime_DATE_GET_FOLD(o: *mut PyObject) -> c_int; - // skipped PyDateTime_DATE_GET_TZINFO (not in PyPy) - - #[link_name = "PyPyDateTime_TIME_GET_HOUR"] - pub fn PyDateTime_TIME_GET_HOUR(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_MINUTE"] - pub fn PyDateTime_TIME_GET_MINUTE(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_SECOND"] - pub fn PyDateTime_TIME_GET_SECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_MICROSECOND"] - pub fn PyDateTime_TIME_GET_MICROSECOND(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_TIME_GET_FOLD"] - pub fn PyDateTime_TIME_GET_FOLD(o: *mut PyObject) -> c_int; - // skipped PyDateTime_TIME_GET_TZINFO (not in PyPy) - - #[link_name = "PyPyDateTime_DELTA_GET_DAYS"] - pub fn PyDateTime_DELTA_GET_DAYS(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DELTA_GET_SECONDS"] - pub fn PyDateTime_DELTA_GET_SECONDS(o: *mut PyObject) -> c_int; - #[link_name = "PyPyDateTime_DELTA_GET_MICROSECONDS"] - pub fn PyDateTime_DELTA_GET_MICROSECONDS(o: *mut PyObject) -> c_int; -} - -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct PyDateTime_CAPI { - pub DateType: *mut PyTypeObject, - pub DateTimeType: *mut PyTypeObject, - pub TimeType: *mut PyTypeObject, - pub DeltaType: *mut PyTypeObject, - pub TZInfoType: *mut PyTypeObject, - pub TimeZone_UTC: *mut PyObject, - pub Date_FromDate: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub DateTime_FromDateAndTime: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Time_FromTime: unsafe extern "C" fn( - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Delta_FromDelta: unsafe extern "C" fn( - days: c_int, - seconds: c_int, - microseconds: c_int, - normalize: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub TimeZone_FromTimeZone: - unsafe extern "C" fn(offset: *mut PyObject, name: *mut PyObject) -> *mut PyObject, - - pub DateTime_FromTimestamp: unsafe extern "C" fn( - cls: *mut PyTypeObject, - args: *mut PyObject, - kwargs: *mut PyObject, - ) -> *mut PyObject, - pub Date_FromTimestamp: - unsafe extern "C" fn(cls: *mut PyTypeObject, args: *mut PyObject) -> *mut PyObject, - pub DateTime_FromDateAndTimeAndFold: unsafe extern "C" fn( - year: c_int, - month: c_int, - day: c_int, - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - fold: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, - pub Time_FromTimeAndFold: unsafe extern "C" fn( - hour: c_int, - minute: c_int, - second: c_int, - microsecond: c_int, - tzinfo: *mut PyObject, - fold: c_int, - cls: *mut PyTypeObject, - ) -> *mut PyObject, -} - -// Python already shares this object between threads, so it's no more evil for us to do it too! -unsafe impl Sync for PyDateTime_CAPI {} - -pub const PyDateTime_CAPSULE_NAME: &CStr = c_str!("datetime.datetime_CAPI"); - -/// Returns a pointer to a `PyDateTime_CAPI` instance -/// -/// # Note -/// This function will return a null pointer until -/// `PyDateTime_IMPORT` is called -#[inline] -pub unsafe fn PyDateTimeAPI() -> *mut PyDateTime_CAPI { - *PyDateTimeAPI_impl.ptr.get() -} - -/// Populates the `PyDateTimeAPI` object -pub unsafe fn PyDateTime_IMPORT() { - if !PyDateTimeAPI_impl.once.is_completed() { - // PyPy expects the C-API to be initialized via PyDateTime_Import, so trying to use - // `PyCapsule_Import` will behave unexpectedly in pypy. - #[cfg(PyPy)] - let py_datetime_c_api = PyDateTime_Import(); - - #[cfg(not(PyPy))] - let py_datetime_c_api = - PyCapsule_Import(PyDateTime_CAPSULE_NAME.as_ptr(), 1) as *mut PyDateTime_CAPI; - - if py_datetime_c_api.is_null() { - return; - } - - // Protect against race conditions when the datetime API is concurrently - // initialized in multiple threads. UnsafeCell.get() cannot panic so this - // won't panic either. - PyDateTimeAPI_impl.once.call_once(|| { - *PyDateTimeAPI_impl.ptr.get() = py_datetime_c_api; - }); - } -} - -#[inline] -pub unsafe fn PyDateTime_TimeZone_UTC() -> *mut PyObject { - (*PyDateTimeAPI()).TimeZone_UTC -} - -/// Type Check macros -/// -/// These are bindings around the C API typecheck macros, all of them return -/// `1` if True and `0` if False. In all type check macros, the argument (`op`) -/// must not be `NULL`. -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DateType` or subtype. -pub unsafe fn PyDate_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DateType`. -pub unsafe fn PyDate_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DateType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DateTimeType` or subtype. -pub unsafe fn PyDateTime_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DateTimeType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DateTimeType`. -pub unsafe fn PyDateTime_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DateTimeType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.TimeType` or subtype. -pub unsafe fn PyTime_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).TimeType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.TimeType`. -pub unsafe fn PyTime_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).TimeType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.DetaType` or subtype. -pub unsafe fn PyDelta_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).DeltaType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.DeltaType`. -pub unsafe fn PyDelta_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).DeltaType) as c_int -} - -#[inline] -/// Check if `op` is a `PyDateTimeAPI.TZInfoType` or subtype. -pub unsafe fn PyTZInfo_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, (*PyDateTimeAPI()).TZInfoType) as c_int -} - -#[inline] -/// Check if `op`'s type is exactly `PyDateTimeAPI.TZInfoType`. -pub unsafe fn PyTZInfo_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == (*PyDateTimeAPI()).TZInfoType) as c_int -} - -// skipped non-limited PyDate_FromDate -// skipped non-limited PyDateTime_FromDateAndTime -// skipped non-limited PyDateTime_FromDateAndTimeAndFold -// skipped non-limited PyTime_FromTime -// skipped non-limited PyTime_FromTimeAndFold -// skipped non-limited PyDelta_FromDSU - -pub unsafe fn PyTimeZone_FromOffset(offset: *mut PyObject) -> *mut PyObject { - ((*PyDateTimeAPI()).TimeZone_FromTimeZone)(offset, std::ptr::null_mut()) -} - -pub unsafe fn PyTimeZone_FromOffsetAndName( - offset: *mut PyObject, - name: *mut PyObject, -) -> *mut PyObject { - ((*PyDateTimeAPI()).TimeZone_FromTimeZone)(offset, name) -} - -#[cfg(not(PyPy))] -pub unsafe fn PyDateTime_FromTimestamp(args: *mut PyObject) -> *mut PyObject { - let f = (*PyDateTimeAPI()).DateTime_FromTimestamp; - f((*PyDateTimeAPI()).DateTimeType, args, std::ptr::null_mut()) -} - -#[cfg(not(PyPy))] -pub unsafe fn PyDate_FromTimestamp(args: *mut PyObject) -> *mut PyObject { - let f = (*PyDateTimeAPI()).Date_FromTimestamp; - f((*PyDateTimeAPI()).DateType, args) -} - -#[cfg(PyPy)] -extern "C" { - #[link_name = "PyPyDate_FromTimestamp"] - pub fn PyDate_FromTimestamp(args: *mut PyObject) -> *mut PyObject; - #[link_name = "PyPyDateTime_FromTimestamp"] - pub fn PyDateTime_FromTimestamp(args: *mut PyObject) -> *mut PyObject; -} - -#[cfg(PyPy)] -extern "C" { - #[link_name = "_PyPyDateTime_Import"] - pub fn PyDateTime_Import() -> *mut PyDateTime_CAPI; -} - -// Rust specific implementation details - -struct PyDateTimeAPISingleton { - once: Once, - ptr: UnsafeCell<*mut PyDateTime_CAPI>, -} -unsafe impl Sync for PyDateTimeAPISingleton {} - -static PyDateTimeAPI_impl: PyDateTimeAPISingleton = PyDateTimeAPISingleton { - once: Once::new(), - ptr: UnsafeCell::new(ptr::null_mut()), -}; diff --git a/vendor/pyo3/pyo3-ffi/src/descrobject.rs b/vendor/pyo3/pyo3-ffi/src/descrobject.rs deleted file mode 100644 index f4a5ce00bf..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/descrobject.rs +++ /dev/null @@ -1,133 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::object::{PyObject, PyTypeObject}; -use crate::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr; - -pub type getter = unsafe extern "C" fn(slf: *mut PyObject, closure: *mut c_void) -> *mut PyObject; -pub type setter = - unsafe extern "C" fn(slf: *mut PyObject, value: *mut PyObject, closure: *mut c_void) -> c_int; - -/// Represents the [PyGetSetDef](https://docs.python.org/3/c-api/structures.html#c.PyGetSetDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must ensure that -/// `name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct PyGetSetDef { - pub name: *const c_char, - pub get: Option, - pub set: Option, - pub doc: *const c_char, - pub closure: *mut c_void, -} - -impl Default for PyGetSetDef { - fn default() -> PyGetSetDef { - PyGetSetDef { - name: ptr::null(), - get: None, - set: None, - doc: ptr::null(), - closure: ptr::null_mut(), - } - } -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyClassMethodDescr_Type")] - pub static mut PyClassMethodDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyGetSetDescr_Type")] - pub static mut PyGetSetDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyMemberDescr_Type")] - pub static mut PyMemberDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyMethodDescr_Type")] - pub static mut PyMethodDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyWrapperDescr_Type")] - pub static mut PyWrapperDescr_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyDictProxy_Type")] - pub static mut PyDictProxy_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyProperty_Type")] - pub static mut PyProperty_Type: PyTypeObject; -} - -extern "C" { - pub fn PyDescr_NewMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewClassMethod")] - pub fn PyDescr_NewClassMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) - -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewMember")] - pub fn PyDescr_NewMember(arg1: *mut PyTypeObject, arg2: *mut PyMemberDef) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDescr_NewGetSet")] - pub fn PyDescr_NewGetSet(arg1: *mut PyTypeObject, arg2: *mut PyGetSetDef) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyDictProxy_New")] - pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; -} - -/// Represents the [PyMemberDef](https://docs.python.org/3/c-api/structures.html#c.PyMemberDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must always ensure that -/// `name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct PyMemberDef { - pub name: *const c_char, - pub type_code: c_int, - pub offset: Py_ssize_t, - pub flags: c_int, - pub doc: *const c_char, -} - -impl Default for PyMemberDef { - fn default() -> PyMemberDef { - PyMemberDef { - name: ptr::null_mut(), - type_code: 0, - offset: 0, - flags: 0, - doc: ptr::null_mut(), - } - } -} - -/* Types */ -pub const Py_T_SHORT: c_int = 0; -pub const Py_T_INT: c_int = 1; -pub const Py_T_LONG: c_int = 2; -pub const Py_T_FLOAT: c_int = 3; -pub const Py_T_DOUBLE: c_int = 4; -pub const Py_T_STRING: c_int = 5; -#[deprecated(note = "Use Py_T_OBJECT_EX instead")] -pub const _Py_T_OBJECT: c_int = 6; -pub const Py_T_CHAR: c_int = 7; -pub const Py_T_BYTE: c_int = 8; -pub const Py_T_UBYTE: c_int = 9; -pub const Py_T_USHORT: c_int = 10; -pub const Py_T_UINT: c_int = 11; -pub const Py_T_ULONG: c_int = 12; -pub const Py_T_STRING_INPLACE: c_int = 13; -pub const Py_T_BOOL: c_int = 14; -pub const Py_T_OBJECT_EX: c_int = 16; -pub const Py_T_LONGLONG: c_int = 17; -pub const Py_T_ULONGLONG: c_int = 18; -pub const Py_T_PYSSIZET: c_int = 19; -#[deprecated(note = "Value is always none")] -pub const _Py_T_NONE: c_int = 20; - -/* Flags */ -pub const Py_READONLY: c_int = 1; -#[cfg(Py_3_10)] -pub const Py_AUDIT_READ: c_int = 2; // Added in 3.10, harmless no-op before that -#[deprecated] -pub const _Py_WRITE_RESTRICTED: c_int = 4; // Deprecated, no-op. Do not reuse the value. -pub const Py_RELATIVE_OFFSET: c_int = 8; - -extern "C" { - pub fn PyMember_GetOne(addr: *const c_char, l: *mut PyMemberDef) -> *mut PyObject; - pub fn PyMember_SetOne(addr: *mut c_char, l: *mut PyMemberDef, value: *mut PyObject) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/dictobject.rs b/vendor/pyo3/pyo3-ffi/src/dictobject.rs deleted file mode 100644 index 710be80243..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/dictobject.rs +++ /dev/null @@ -1,121 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyDict_Type")] - pub static mut PyDict_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyDict_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS) -} - -#[inline] -pub unsafe fn PyDict_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDict_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyDict_New")] - pub fn PyDict_New() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItem")] - pub fn PyDict_GetItem(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemWithError")] - pub fn PyDict_GetItemWithError(mp: *mut PyObject, key: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_SetItem")] - pub fn PyDict_SetItem(mp: *mut PyObject, key: *mut PyObject, item: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_DelItem")] - pub fn PyDict_DelItem(mp: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Clear")] - pub fn PyDict_Clear(mp: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyDict_Next")] - pub fn PyDict_Next( - mp: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - value: *mut *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Keys")] - pub fn PyDict_Keys(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Values")] - pub fn PyDict_Values(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Items")] - pub fn PyDict_Items(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Size")] - pub fn PyDict_Size(mp: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyDict_Copy")] - pub fn PyDict_Copy(mp: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_Contains")] - pub fn PyDict_Contains(mp: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Update")] - pub fn PyDict_Update(mp: *mut PyObject, other: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_Merge")] - pub fn PyDict_Merge(mp: *mut PyObject, other: *mut PyObject, _override: c_int) -> c_int; - pub fn PyDict_MergeFromSeq2(d: *mut PyObject, seq2: *mut PyObject, _override: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemString")] - pub fn PyDict_GetItemString(dp: *mut PyObject, key: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyDict_SetItemString")] - pub fn PyDict_SetItemString( - dp: *mut PyObject, - key: *const c_char, - item: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyDict_DelItemString")] - pub fn PyDict_DelItemString(dp: *mut PyObject, key: *const c_char) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyDict_GetItemRef")] - pub fn PyDict_GetItemRef( - dp: *mut PyObject, - key: *mut PyObject, - result: *mut *mut PyObject, - ) -> c_int; - // skipped 3.10 / ex-non-limited PyObject_GenericGetDict -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyDictKeys_Type: PyTypeObject; - pub static mut PyDictValues_Type: PyTypeObject; - pub static mut PyDictItems_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyDictKeys_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictKeys_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictValues_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictValues_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictItems_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyDictItems_Type)) as c_int -} - -#[inline] -pub unsafe fn PyDictViewSet_Check(op: *mut PyObject) -> c_int { - (PyDictKeys_Check(op) != 0 || PyDictItems_Check(op) != 0) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyDictIterKey_Type: PyTypeObject; - pub static mut PyDictIterValue_Type: PyTypeObject; - pub static mut PyDictIterItem_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterKey_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterValue_Type: PyTypeObject; - #[cfg(Py_3_8)] - pub static mut PyDictRevIterItem_Type: PyTypeObject; -} - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985) -opaque_struct!(PyDictObject); diff --git a/vendor/pyo3/pyo3-ffi/src/enumobject.rs b/vendor/pyo3/pyo3-ffi/src/enumobject.rs deleted file mode 100644 index e3f187d16c..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/enumobject.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::object::PyTypeObject; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyEnum_Type: PyTypeObject; - pub static mut PyReversed_Type: PyTypeObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/fileobject.rs b/vendor/pyo3/pyo3-ffi/src/fileobject.rs deleted file mode 100644 index 91618ab7fd..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/fileobject.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int}; - -pub const PY_STDIOTEXTMODE: &str = "b"; - -extern "C" { - pub fn PyFile_FromFd( - arg1: c_int, - arg2: *const c_char, - arg3: *const c_char, - arg4: c_int, - arg5: *const c_char, - arg6: *const c_char, - arg7: *const c_char, - arg8: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFile_GetLine")] - pub fn PyFile_GetLine(arg1: *mut PyObject, arg2: c_int) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFile_WriteObject")] - pub fn PyFile_WriteObject(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyFile_WriteString")] - pub fn PyFile_WriteString(arg1: *const c_char, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyFile_AsFileDescriptor")] - pub fn PyObject_AsFileDescriptor(arg1: *mut PyObject) -> c_int; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[deprecated(note = "Python 3.12")] - pub static mut Py_FileSystemDefaultEncoding: *const c_char; - #[deprecated(note = "Python 3.12")] - pub static mut Py_FileSystemDefaultEncodeErrors: *const c_char; - #[deprecated(note = "Python 3.12")] - pub static mut Py_HasFileSystemDefaultEncoding: c_int; - // skipped 3.12-deprecated Py_UTF8Mode -} - -// skipped _PyIsSelectable_fd diff --git a/vendor/pyo3/pyo3-ffi/src/fileutils.rs b/vendor/pyo3/pyo3-ffi/src/fileutils.rs deleted file mode 100644 index 3f053b72b5..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/fileutils.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::pyport::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::c_char; - -extern "C" { - pub fn Py_DecodeLocale(arg1: *const c_char, size: *mut Py_ssize_t) -> *mut wchar_t; - - pub fn Py_EncodeLocale(text: *const wchar_t, error_pos: *mut Py_ssize_t) -> *mut c_char; -} diff --git a/vendor/pyo3/pyo3-ffi/src/floatobject.rs b/vendor/pyo3/pyo3-ffi/src/floatobject.rs deleted file mode 100644 index 65fc1d4c31..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/floatobject.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_double, c_int}; -use std::ptr::addr_of_mut; - -#[cfg(Py_LIMITED_API)] -// TODO: remove (see https://github.com/PyO3/pyo3/pull/1341#issuecomment-751515985) -opaque_struct!(PyFloatObject); - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyFloat_Type")] - pub static mut PyFloat_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyFloat_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyFloat_Type)) -} - -#[inline] -pub unsafe fn PyFloat_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyFloat_Type)) as c_int -} - -// skipped Py_RETURN_NAN -// skipped Py_RETURN_INF - -extern "C" { - pub fn PyFloat_GetMax() -> c_double; - pub fn PyFloat_GetMin() -> c_double; - pub fn PyFloat_GetInfo() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_FromString")] - pub fn PyFloat_FromString(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_FromDouble")] - pub fn PyFloat_FromDouble(arg1: c_double) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFloat_AsDouble")] - pub fn PyFloat_AsDouble(arg1: *mut PyObject) -> c_double; -} - -// skipped non-limited _PyFloat_Pack2 -// skipped non-limited _PyFloat_Pack4 -// skipped non-limited _PyFloat_Pack8 -// skipped non-limited _PyFloat_Unpack2 -// skipped non-limited _PyFloat_Unpack4 -// skipped non-limited _PyFloat_Unpack8 -// skipped non-limited _PyFloat_DebugMallocStats -// skipped non-limited _PyFloat_FormatAdvancedWriter diff --git a/vendor/pyo3/pyo3-ffi/src/impl_/mod.rs b/vendor/pyo3/pyo3-ffi/src/impl_/mod.rs deleted file mode 100644 index 3058e852e6..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/impl_/mod.rs +++ /dev/null @@ -1,22 +0,0 @@ -#[cfg(Py_GIL_DISABLED)] -mod atomic_c_ulong { - pub struct GetAtomicCULong(); - - pub trait AtomicCULongType { - type Type; - } - impl AtomicCULongType for GetAtomicCULong<32> { - type Type = std::sync::atomic::AtomicU32; - } - impl AtomicCULongType for GetAtomicCULong<64> { - type Type = std::sync::atomic::AtomicU64; - } - - pub type TYPE = - () * 8 }> as AtomicCULongType>::Type; -} - -/// Typedef for an atomic integer to match the platform-dependent c_ulong type. -#[cfg(Py_GIL_DISABLED)] -#[doc(hidden)] -pub type AtomicCULong = atomic_c_ulong::TYPE; diff --git a/vendor/pyo3/pyo3-ffi/src/import.rs b/vendor/pyo3/pyo3-ffi/src/import.rs deleted file mode 100644 index e15a37b0a7..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/import.rs +++ /dev/null @@ -1,84 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_int, c_long}; - -extern "C" { - pub fn PyImport_GetMagicNumber() -> c_long; - pub fn PyImport_GetMagicTag() -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyImport_ExecCodeModule")] - pub fn PyImport_ExecCodeModule(name: *const c_char, co: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ExecCodeModuleEx")] - pub fn PyImport_ExecCodeModuleEx( - name: *const c_char, - co: *mut PyObject, - pathname: *const c_char, - ) -> *mut PyObject; - pub fn PyImport_ExecCodeModuleWithPathnames( - name: *const c_char, - co: *mut PyObject, - pathname: *const c_char, - cpathname: *const c_char, - ) -> *mut PyObject; - pub fn PyImport_ExecCodeModuleObject( - name: *mut PyObject, - co: *mut PyObject, - pathname: *mut PyObject, - cpathname: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_GetModuleDict")] - pub fn PyImport_GetModuleDict() -> *mut PyObject; - // skipped Python 3.7 / ex-non-limited PyImport_GetModule - pub fn PyImport_AddModuleObject(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_AddModule")] - pub fn PyImport_AddModule(name: *const c_char) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyImport_AddModuleRef")] - pub fn PyImport_AddModuleRef(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModule")] - pub fn PyImport_ImportModule(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleNoBlock")] - pub fn PyImport_ImportModuleNoBlock(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleLevel")] - pub fn PyImport_ImportModuleLevel( - name: *const c_char, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, - level: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleLevelObject")] - pub fn PyImport_ImportModuleLevelObject( - name: *mut PyObject, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, - level: c_int, - ) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyImport_ImportModuleEx( - name: *const c_char, - globals: *mut PyObject, - locals: *mut PyObject, - fromlist: *mut PyObject, -) -> *mut PyObject { - PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0) -} - -extern "C" { - pub fn PyImport_GetImporter(path: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_Import")] - pub fn PyImport_Import(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyImport_ReloadModule")] - pub fn PyImport_ReloadModule(m: *mut PyObject) -> *mut PyObject; - #[cfg(not(Py_3_9))] - #[deprecated(note = "Removed in Python 3.9 as it was \"For internal use only\".")] - pub fn PyImport_Cleanup(); - pub fn PyImport_ImportFrozenModuleObject(name: *mut PyObject) -> c_int; - pub fn PyImport_ImportFrozenModule(name: *const c_char) -> c_int; - - pub fn PyImport_AppendInittab( - name: *const c_char, - initfunc: Option *mut PyObject>, - ) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/intrcheck.rs b/vendor/pyo3/pyo3-ffi/src/intrcheck.rs deleted file mode 100644 index 3d8a58f7f2..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/intrcheck.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::os::raw::c_int; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_InterruptOccurred")] - pub fn PyOS_InterruptOccurred() -> c_int; - #[cfg(not(Py_3_10))] - #[deprecated(note = "Not documented in Python API; see Python 3.10 release notes")] - pub fn PyOS_InitInterrupts(); - - pub fn PyOS_BeforeFork(); - pub fn PyOS_AfterFork_Parent(); - pub fn PyOS_AfterFork_Child(); - #[deprecated(note = "use PyOS_AfterFork_Child instead")] - #[cfg_attr(PyPy, link_name = "PyPyOS_AfterFork")] - pub fn PyOS_AfterFork(); - - // skipped non-limited _PyOS_IsMainThread - // skipped non-limited Windows _PyOS_SigintEvent -} diff --git a/vendor/pyo3/pyo3-ffi/src/iterobject.rs b/vendor/pyo3/pyo3-ffi/src/iterobject.rs deleted file mode 100644 index aa0c7b26db..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/iterobject.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PySeqIter_Type: PyTypeObject; - pub static mut PyCallIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PySeqIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PySeqIter_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySeqIter_New")] - pub fn PySeqIter_New(arg1: *mut PyObject) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyCallIter_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyCallIter_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCallIter_New")] - pub fn PyCallIter_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/lib.rs b/vendor/pyo3/pyo3-ffi/src/lib.rs deleted file mode 100644 index 7bdba1173d..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/lib.rs +++ /dev/null @@ -1,555 +0,0 @@ -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -//! Raw FFI declarations for Python's C API. -//! -//! PyO3 can be used to write native Python modules or run Python code and modules from Rust. -//! -//! This crate just provides low level bindings to the Python interpreter. -//! It is meant for advanced users only - regular PyO3 users shouldn't -//! need to interact with this crate at all. -//! -//! The contents of this crate are not documented here, as it would entail -//! basically copying the documentation from CPython. Consult the [Python/C API Reference -//! Manual][capi] for up-to-date documentation. -//! -//! # Safety -//! -//! The functions in this crate lack individual safety documentation, but -//! generally the following apply: -//! - Pointer arguments have to point to a valid Python object of the correct type, -//! although null pointers are sometimes valid input. -//! - The vast majority can only be used safely while the GIL is held. -//! - Some functions have additional safety requirements, consult the -//! [Python/C API Reference Manual][capi] -//! for more information. -//! -//! -//! # Feature flags -//! -//! PyO3 uses [feature flags] to enable you to opt-in to additional functionality. For a detailed -//! description, see the [Features chapter of the guide]. -//! -//! ## Optional feature flags -//! -//! The following features customize PyO3's behavior: -//! -//! - `abi3`: Restricts PyO3's API to a subset of the full Python API which is guaranteed by -//! [PEP 384] to be forward-compatible with future Python versions. -//! - `extension-module`: This will tell the linker to keep the Python symbols unresolved, so that -//! your module can also be used with statically linked Python interpreters. Use this feature when -//! building an extension module. -//! -//! ## `rustc` environment flags -//! -//! PyO3 uses `rustc`'s `--cfg` flags to enable or disable code used for different Python versions. -//! If you want to do this for your own crate, you can do so with the [`pyo3-build-config`] crate. -//! -//! - `Py_3_7`, `Py_3_8`, `Py_3_9`, `Py_3_10`, `Py_3_11`, `Py_3_12`, `Py_3_13`: Marks code that is -//! only enabled when compiling for a given minimum Python version. -//! - `Py_LIMITED_API`: Marks code enabled when the `abi3` feature flag is enabled. -//! - `Py_GIL_DISABLED`: Marks code that runs only in the free-threaded build of CPython. -//! - `PyPy` - Marks code enabled when compiling for PyPy. -//! - `GraalPy` - Marks code enabled when compiling for GraalPy. -//! -//! Additionally, you can query for the values `Py_DEBUG`, `Py_REF_DEBUG`, -//! `Py_TRACE_REFS`, and `COUNT_ALLOCS` from `py_sys_config` to query for the -//! corresponding C build-time defines. For example, to conditionally define -//! debug code using `Py_DEBUG`, you could do: -//! -//! ```rust,ignore -//! #[cfg(py_sys_config = "Py_DEBUG")] -//! println!("only runs if python was compiled with Py_DEBUG") -//! ``` -//! -//! To use these attributes, add [`pyo3-build-config`] as a build dependency in -//! your `Cargo.toml`: -//! -//! ```toml -//! [build-dependencies] -#![doc = concat!("pyo3-build-config =\"", env!("CARGO_PKG_VERSION"), "\"")] -//! ``` -//! -//! And then either create a new `build.rs` file in the project root or modify -//! the existing `build.rs` file to call `use_pyo3_cfgs()`: -//! -//! ```rust,ignore -//! fn main() { -//! pyo3_build_config::use_pyo3_cfgs(); -//! } -//! ``` -//! -//! # Minimum supported Rust and Python versions -//! -//! `pyo3-ffi` supports the following Python distributions: -//! - CPython 3.7 or greater -//! - PyPy 7.3 (Python 3.9+) -//! - GraalPy 24.0 or greater (Python 3.10+) -//! -//! # Example: Building Python Native modules -//! -//! PyO3 can be used to generate a native Python module. The easiest way to try this out for the -//! first time is to use [`maturin`]. `maturin` is a tool for building and publishing Rust-based -//! Python packages with minimal configuration. The following steps set up some files for an example -//! Python module, install `maturin`, and then show how to build and import the Python module. -//! -//! First, create a new folder (let's call it `string_sum`) containing the following two files: -//! -//! **`Cargo.toml`** -//! -//! ```toml -//! [lib] -//! name = "string_sum" -//! # "cdylib" is necessary to produce a shared library for Python to import from. -//! # -//! # Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able -//! # to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: -//! # crate-type = ["cdylib", "rlib"] -//! crate-type = ["cdylib"] -//! -//! [dependencies.pyo3-ffi] -#![doc = concat!("version = \"", env!("CARGO_PKG_VERSION"), "\"")] -//! features = ["extension-module"] -//! -//! [build-dependencies] -//! # This is only necessary if you need to configure your build based on -//! # the Python version or the compile-time configuration for the interpreter. -#![doc = concat!("pyo3_build_config = \"", env!("CARGO_PKG_VERSION"), "\"")] -//! ``` -//! -//! If you need to use conditional compilation based on Python version or how -//! Python was compiled, you need to add `pyo3-build-config` as a -//! `build-dependency` in your `Cargo.toml` as in the example above and either -//! create a new `build.rs` file or modify an existing one so that -//! `pyo3_build_config::use_pyo3_cfgs()` gets called at build time: -//! -//! **`build.rs`** -//! ```rust,ignore -//! fn main() { -//! pyo3_build_config::use_pyo3_cfgs() -//! } -//! ``` -//! -//! **`src/lib.rs`** -//! ```rust -//! use std::os::raw::{c_char, c_long}; -//! use std::ptr; -//! -//! use pyo3_ffi::*; -//! -//! static mut MODULE_DEF: PyModuleDef = PyModuleDef { -//! m_base: PyModuleDef_HEAD_INIT, -//! m_name: c_str!("string_sum").as_ptr(), -//! m_doc: c_str!("A Python module written in Rust.").as_ptr(), -//! m_size: 0, -//! m_methods: unsafe { METHODS as *const [PyMethodDef] as *mut PyMethodDef }, -//! m_slots: std::ptr::null_mut(), -//! m_traverse: None, -//! m_clear: None, -//! m_free: None, -//! }; -//! -//! static mut METHODS: &[PyMethodDef] = &[ -//! PyMethodDef { -//! ml_name: c_str!("sum_as_string").as_ptr(), -//! ml_meth: PyMethodDefPointer { -//! PyCFunctionFast: sum_as_string, -//! }, -//! ml_flags: METH_FASTCALL, -//! ml_doc: c_str!("returns the sum of two integers as a string").as_ptr(), -//! }, -//! // A zeroed PyMethodDef to mark the end of the array. -//! PyMethodDef::zeroed(), -//! ]; -//! -//! // The module initialization function, which must be named `PyInit_`. -//! #[allow(non_snake_case)] -//! #[no_mangle] -//! pub unsafe extern "C" fn PyInit_string_sum() -> *mut PyObject { -//! let module = PyModule_Create(ptr::addr_of_mut!(MODULE_DEF)); -//! if module.is_null() { -//! return module; -//! } -//! #[cfg(Py_GIL_DISABLED)] -//! { -//! if PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) < 0 { -//! Py_DECREF(module); -//! return std::ptr::null_mut(); -//! } -//! } -//! module -//! } -//! -//! /// A helper to parse function arguments -//! /// If we used PyO3's proc macros they'd handle all of this boilerplate for us :) -//! unsafe fn parse_arg_as_i32(obj: *mut PyObject, n_arg: usize) -> Option { -//! if PyLong_Check(obj) == 0 { -//! let msg = format!( -//! "sum_as_string expected an int for positional argument {}\0", -//! n_arg -//! ); -//! PyErr_SetString(PyExc_TypeError, msg.as_ptr().cast::()); -//! return None; -//! } -//! -//! // Let's keep the behaviour consistent on platforms where `c_long` is bigger than 32 bits. -//! // In particular, it is an i32 on Windows but i64 on most Linux systems -//! let mut overflow = 0; -//! let i_long: c_long = PyLong_AsLongAndOverflow(obj, &mut overflow); -//! -//! #[allow(irrefutable_let_patterns)] // some platforms have c_long equal to i32 -//! if overflow != 0 { -//! raise_overflowerror(obj); -//! None -//! } else if let Ok(i) = i_long.try_into() { -//! Some(i) -//! } else { -//! raise_overflowerror(obj); -//! None -//! } -//! } -//! -//! unsafe fn raise_overflowerror(obj: *mut PyObject) { -//! let obj_repr = PyObject_Str(obj); -//! if !obj_repr.is_null() { -//! let mut size = 0; -//! let p = PyUnicode_AsUTF8AndSize(obj_repr, &mut size); -//! if !p.is_null() { -//! let s = std::str::from_utf8_unchecked(std::slice::from_raw_parts( -//! p.cast::(), -//! size as usize, -//! )); -//! let msg = format!("cannot fit {} in 32 bits\0", s); -//! -//! PyErr_SetString(PyExc_OverflowError, msg.as_ptr().cast::()); -//! } -//! Py_DECREF(obj_repr); -//! } -//! } -//! -//! pub unsafe extern "C" fn sum_as_string( -//! _self: *mut PyObject, -//! args: *mut *mut PyObject, -//! nargs: Py_ssize_t, -//! ) -> *mut PyObject { -//! if nargs != 2 { -//! PyErr_SetString( -//! PyExc_TypeError, -//! c_str!("sum_as_string expected 2 positional arguments").as_ptr(), -//! ); -//! return std::ptr::null_mut(); -//! } -//! -//! let (first, second) = (*args, *args.add(1)); -//! -//! let first = match parse_arg_as_i32(first, 1) { -//! Some(x) => x, -//! None => return std::ptr::null_mut(), -//! }; -//! let second = match parse_arg_as_i32(second, 2) { -//! Some(x) => x, -//! None => return std::ptr::null_mut(), -//! }; -//! -//! match first.checked_add(second) { -//! Some(sum) => { -//! let string = sum.to_string(); -//! PyUnicode_FromStringAndSize(string.as_ptr().cast::(), string.len() as isize) -//! } -//! None => { -//! PyErr_SetString( -//! PyExc_OverflowError, -//! c_str!("arguments too large to add").as_ptr(), -//! ); -//! std::ptr::null_mut() -//! } -//! } -//! } -//! ``` -//! -//! With those two files in place, now `maturin` needs to be installed. This can be done using -//! Python's package manager `pip`. First, load up a new Python `virtualenv`, and install `maturin` -//! into it: -//! ```bash -//! $ cd string_sum -//! $ python -m venv .env -//! $ source .env/bin/activate -//! $ pip install maturin -//! ``` -//! -//! Now build and execute the module: -//! ```bash -//! $ maturin develop -//! # lots of progress output as maturin runs the compilation... -//! $ python -//! >>> import string_sum -//! >>> string_sum.sum_as_string(5, 20) -//! '25' -//! ``` -//! -//! As well as with `maturin`, it is possible to build using [setuptools-rust] or -//! [manually][manual_builds]. Both offer more flexibility than `maturin` but require further -//! configuration. -//! -//! This example stores the module definition statically and uses the `PyModule_Create` function -//! in the CPython C API to register the module. This is the "old" style for registering modules -//! and has the limitation that it cannot support subinterpreters. You can also create a module -//! using the new multi-phase initialization API that does support subinterpreters. See the -//! `sequential` project located in the `examples` directory at the root of the `pyo3-ffi` crate -//! for a worked example of how to this using `pyo3-ffi`. -//! -//! # Using Python from Rust -//! -//! To embed Python into a Rust binary, you need to ensure that your Python installation contains a -//! shared library. The following steps demonstrate how to ensure this (for Ubuntu). -//! -//! To install the Python shared library on Ubuntu: -//! ```bash -//! sudo apt install python3-dev -//! ``` -//! -//! While most projects use the safe wrapper provided by pyo3, -//! you can take a look at the [`orjson`] library as an example on how to use `pyo3-ffi` directly. -//! For those well versed in C and Rust the [tutorials] from the CPython documentation -//! can be easily converted to rust as well. -//! -//! [tutorials]: https://docs.python.org/3/extending/ -//! [`orjson`]: https://github.com/ijl/orjson -//! [capi]: https://docs.python.org/3/c-api/index.html -//! [`maturin`]: https://github.com/PyO3/maturin "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" -//! [`pyo3-build-config`]: https://docs.rs/pyo3-build-config -//! [feature flags]: https://doc.rust-lang.org/cargo/reference/features.html "Features - The Cargo Book" -#![doc = concat!("[manual_builds]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/building-and-distribution.html#manual-builds \"Manual builds - Building and Distribution - PyO3 user guide\"")] -//! [setuptools-rust]: https://github.com/PyO3/setuptools-rust "Setuptools plugin for Rust extensions" -//! [PEP 384]: https://www.python.org/dev/peps/pep-0384 "PEP 384 -- Defining a Stable ABI" -#![doc = concat!("[Features chapter of the guide]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#features-reference \"Features eference - PyO3 user guide\"")] -#![allow( - missing_docs, - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - clippy::upper_case_acronyms, - clippy::missing_safety_doc -)] -#![warn(elided_lifetimes_in_paths, unused_lifetimes)] - -// Until `extern type` is stabilized, use the recommended approach to -// model opaque types: -// https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs -macro_rules! opaque_struct { - ($name:ident) => { - #[repr(C)] - pub struct $name([u8; 0]); - }; -} - -/// This is a helper macro to create a `&'static CStr`. -/// -/// It can be used on all Rust versions supported by PyO3, unlike c"" literals which -/// were stabilised in Rust 1.77. -/// -/// Due to the nature of PyO3 making heavy use of C FFI interop with Python, it is -/// common for PyO3 to use CStr. -/// -/// Examples: -/// -/// ```rust -/// use std::ffi::CStr; -/// -/// const HELLO: &CStr = pyo3_ffi::c_str!("hello"); -/// static WORLD: &CStr = pyo3_ffi::c_str!("world"); -/// ``` -#[macro_export] -macro_rules! c_str { - ($s:expr) => { - $crate::_cstr_from_utf8_with_nul_checked(concat!($s, "\0")) - }; -} - -/// Private helper for `c_str!` macro. -#[doc(hidden)] -pub const fn _cstr_from_utf8_with_nul_checked(s: &str) -> &CStr { - // TODO: Replace this implementation with `CStr::from_bytes_with_nul` when MSRV above 1.72. - let bytes = s.as_bytes(); - let len = bytes.len(); - assert!( - !bytes.is_empty() && bytes[bytes.len() - 1] == b'\0', - "string is not nul-terminated" - ); - let mut i = 0; - let non_null_len = len - 1; - while i < non_null_len { - assert!(bytes[i] != b'\0', "string contains null bytes"); - i += 1; - } - - unsafe { CStr::from_bytes_with_nul_unchecked(bytes) } -} - -use std::ffi::CStr; - -pub mod compat; -mod impl_; - -pub use self::abstract_::*; -pub use self::bltinmodule::*; -pub use self::boolobject::*; -pub use self::bytearrayobject::*; -pub use self::bytesobject::*; -pub use self::ceval::*; -#[cfg(Py_LIMITED_API)] -pub use self::code::*; -pub use self::codecs::*; -pub use self::compile::*; -pub use self::complexobject::*; -#[cfg(all(Py_3_8, not(Py_LIMITED_API)))] -pub use self::context::*; -#[cfg(not(Py_LIMITED_API))] -pub use self::datetime::*; -pub use self::descrobject::*; -pub use self::dictobject::*; -pub use self::enumobject::*; -pub use self::fileobject::*; -pub use self::fileutils::*; -pub use self::floatobject::*; -pub use self::import::*; -pub use self::intrcheck::*; -pub use self::iterobject::*; -pub use self::listobject::*; -pub use self::longobject::*; -#[cfg(not(Py_LIMITED_API))] -pub use self::marshal::*; -pub use self::memoryobject::*; -pub use self::methodobject::*; -pub use self::modsupport::*; -pub use self::moduleobject::*; -pub use self::object::*; -pub use self::objimpl::*; -pub use self::osmodule::*; -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -pub use self::pyarena::*; -#[cfg(Py_3_11)] -pub use self::pybuffer::*; -pub use self::pycapsule::*; -pub use self::pyerrors::*; -pub use self::pyframe::*; -pub use self::pyhash::*; -pub use self::pylifecycle::*; -pub use self::pymem::*; -pub use self::pyport::*; -pub use self::pystate::*; -pub use self::pystrtod::*; -pub use self::pythonrun::*; -pub use self::rangeobject::*; -pub use self::setobject::*; -pub use self::sliceobject::*; -pub use self::structseq::*; -pub use self::sysmodule::*; -pub use self::traceback::*; -pub use self::tupleobject::*; -pub use self::typeslots::*; -pub use self::unicodeobject::*; -pub use self::warnings::*; -pub use self::weakrefobject::*; - -mod abstract_; -// skipped asdl.h -// skipped ast.h -mod bltinmodule; -mod boolobject; -mod bytearrayobject; -mod bytesobject; -// skipped cellobject.h -mod ceval; -// skipped classobject.h -#[cfg(Py_LIMITED_API)] -mod code; -mod codecs; -mod compile; -mod complexobject; -#[cfg(all(Py_3_8, not(Py_LIMITED_API)))] -mod context; // It's actually 3.7.1, but no cfg for patches. -#[cfg(not(Py_LIMITED_API))] -pub(crate) mod datetime; -mod descrobject; -mod dictobject; -// skipped dynamic_annotations.h -mod enumobject; -// skipped errcode.h -// skipped exports.h -mod fileobject; -mod fileutils; -mod floatobject; -// skipped empty frameobject.h -// skipped genericaliasobject.h -mod import; -// skipped interpreteridobject.h -mod intrcheck; -mod iterobject; -mod listobject; -// skipped longintrepr.h -mod longobject; -#[cfg(not(Py_LIMITED_API))] -pub mod marshal; -mod memoryobject; -mod methodobject; -mod modsupport; -mod moduleobject; -// skipped namespaceobject.h -mod object; -mod objimpl; -// skipped odictobject.h -// skipped opcode.h -// skipped osdefs.h -mod osmodule; -// skipped parser_interface.h -// skipped patchlevel.h -// skipped picklebufobject.h -// skipped pyctype.h -// skipped py_curses.h -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -mod pyarena; -#[cfg(Py_3_11)] -mod pybuffer; -mod pycapsule; -// skipped pydtrace.h -mod pyerrors; -// skipped pyexpat.h -// skipped pyfpe.h -mod pyframe; -mod pyhash; -mod pylifecycle; -// skipped pymacconfig.h -// skipped pymacro.h -// skipped pymath.h -mod pymem; -mod pyport; -mod pystate; -// skipped pystats.h -mod pythonrun; -// skipped pystrhex.h -// skipped pystrcmp.h -mod pystrtod; -// skipped pythread.h -// skipped pytime.h -mod rangeobject; -mod setobject; -mod sliceobject; -mod structseq; -mod sysmodule; -mod traceback; -// skipped tracemalloc.h -mod tupleobject; -mod typeslots; -mod unicodeobject; -mod warnings; -mod weakrefobject; - -// Additional headers that are not exported by Python.h -#[deprecated(note = "Python 3.12")] -pub mod structmember; - -// "Limited API" definitions matching Python's `include/cpython` directory. -#[cfg(not(Py_LIMITED_API))] -mod cpython; - -#[cfg(not(Py_LIMITED_API))] -pub use self::cpython::*; diff --git a/vendor/pyo3/pyo3-ffi/src/listobject.rs b/vendor/pyo3/pyo3-ffi/src/listobject.rs deleted file mode 100644 index 881a8a8707..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/listobject.rs +++ /dev/null @@ -1,76 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyList_Type")] - pub static mut PyList_Type: PyTypeObject; - pub static mut PyListIter_Type: PyTypeObject; - pub static mut PyListRevIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyList_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LIST_SUBCLASS) -} - -#[inline] -pub unsafe fn PyList_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyList_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyList_New")] - pub fn PyList_New(size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_Size")] - pub fn PyList_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyList_GetItem")] - pub fn PyList_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyList_GetItemRef")] - pub fn PyList_GetItemRef(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_SetItem")] - pub fn PyList_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Insert")] - pub fn PyList_Insert(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Append")] - pub fn PyList_Append(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_GetSlice")] - pub fn PyList_GetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyList_SetSlice")] - pub fn PyList_SetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - arg4: *mut PyObject, - ) -> c_int; - #[cfg(Py_3_13)] - pub fn PyList_Extend(list: *mut PyObject, iterable: *mut PyObject) -> c_int; - #[cfg(Py_3_13)] - pub fn PyList_Clear(list: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Sort")] - pub fn PyList_Sort(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_Reverse")] - pub fn PyList_Reverse(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyList_AsTuple")] - pub fn PyList_AsTuple(arg1: *mut PyObject) -> *mut PyObject; - - // CPython macros exported as functions on PyPy or GraalPy - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_ITEM")] - #[cfg_attr(GraalPy, link_name = "PyList_GetItem")] - pub fn PyList_GET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg(PyPy)] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_SIZE")] - pub fn PyList_GET_SIZE(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_SET_ITEM")] - #[cfg_attr(GraalPy, link_name = "_PyList_SET_ITEM")] - pub fn PyList_SET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); -} diff --git a/vendor/pyo3/pyo3-ffi/src/longobject.rs b/vendor/pyo3/pyo3-ffi/src/longobject.rs deleted file mode 100644 index 68b4ecba54..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/longobject.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use libc::size_t; -use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong, c_ulong, c_ulonglong, c_void}; -use std::ptr::addr_of_mut; - -opaque_struct!(PyLongObject); - -#[inline] -pub unsafe fn PyLong_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS) -} - -#[inline] -pub unsafe fn PyLong_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyLong_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyLong_FromLong")] - pub fn PyLong_FromLong(arg1: c_long) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromUnsignedLong")] - pub fn PyLong_FromUnsignedLong(arg1: c_ulong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromSize_t")] - pub fn PyLong_FromSize_t(arg1: size_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromSsize_t")] - pub fn PyLong_FromSsize_t(arg1: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromDouble")] - pub fn PyLong_FromDouble(arg1: c_double) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLong")] - pub fn PyLong_AsLong(arg1: *mut PyObject) -> c_long; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongAndOverflow")] - pub fn PyLong_AsLongAndOverflow(arg1: *mut PyObject, arg2: *mut c_int) -> c_long; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsSsize_t")] - pub fn PyLong_AsSsize_t(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsSize_t")] - pub fn PyLong_AsSize_t(arg1: *mut PyObject) -> size_t; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLong")] - pub fn PyLong_AsUnsignedLong(arg1: *mut PyObject) -> c_ulong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongMask")] - pub fn PyLong_AsUnsignedLongMask(arg1: *mut PyObject) -> c_ulong; - // skipped non-limited _PyLong_AsInt - pub fn PyLong_GetInfo() -> *mut PyObject; - // skipped PyLong_AS_LONG - - // skipped PyLong_FromPid - // skipped PyLong_AsPid - // skipped _Py_PARSE_INTPTR - // skipped _Py_PARSE_UINTPTR - - // skipped non-limited _PyLong_UnsignedShort_Converter - // skipped non-limited _PyLong_UnsignedInt_Converter - // skipped non-limited _PyLong_UnsignedLong_Converter - // skipped non-limited _PyLong_UnsignedLongLong_Converter - // skipped non-limited _PyLong_Size_t_Converter - - // skipped non-limited _PyLong_DigitValue - // skipped non-limited _PyLong_Frexp - - #[cfg_attr(PyPy, link_name = "PyPyLong_AsDouble")] - pub fn PyLong_AsDouble(arg1: *mut PyObject) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromVoidPtr")] - pub fn PyLong_FromVoidPtr(arg1: *mut c_void) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsVoidPtr")] - pub fn PyLong_AsVoidPtr(arg1: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromLongLong")] - pub fn PyLong_FromLongLong(arg1: c_longlong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromUnsignedLongLong")] - pub fn PyLong_FromUnsignedLongLong(arg1: c_ulonglong) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongLong")] - pub fn PyLong_AsLongLong(arg1: *mut PyObject) -> c_longlong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongLong")] - pub fn PyLong_AsUnsignedLongLong(arg1: *mut PyObject) -> c_ulonglong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongLongMask")] - pub fn PyLong_AsUnsignedLongLongMask(arg1: *mut PyObject) -> c_ulonglong; - #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongLongAndOverflow")] - pub fn PyLong_AsLongLongAndOverflow(arg1: *mut PyObject, arg2: *mut c_int) -> c_longlong; - #[cfg_attr(PyPy, link_name = "PyPyLong_FromString")] - pub fn PyLong_FromString( - arg1: *const c_char, - arg2: *mut *mut c_char, - arg3: c_int, - ) -> *mut PyObject; -} - -#[cfg(not(Py_LIMITED_API))] -extern "C" { - #[cfg_attr(PyPy, link_name = "_PyPyLong_NumBits")] - pub fn _PyLong_NumBits(obj: *mut PyObject) -> size_t; -} - -// skipped non-limited _PyLong_Format -// skipped non-limited _PyLong_FormatWriter -// skipped non-limited _PyLong_FormatBytesWriter -// skipped non-limited _PyLong_FormatAdvancedWriter - -extern "C" { - pub fn PyOS_strtoul(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_ulong; - pub fn PyOS_strtol(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_long; -} - -// skipped non-limited _PyLong_Rshift -// skipped non-limited _PyLong_Lshift diff --git a/vendor/pyo3/pyo3-ffi/src/marshal.rs b/vendor/pyo3/pyo3-ffi/src/marshal.rs deleted file mode 100644 index 562f6f9162..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/marshal.rs +++ /dev/null @@ -1,19 +0,0 @@ -use super::{PyObject, Py_ssize_t}; -use std::os::raw::{c_char, c_int}; - -// skipped Py_MARSHAL_VERSION -// skipped PyMarshal_WriteLongToFile -// skipped PyMarshal_WriteObjectToFile - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMarshal_WriteObjectToString")] - pub fn PyMarshal_WriteObjectToString(object: *mut PyObject, version: c_int) -> *mut PyObject; - - // skipped non-limited PyMarshal_ReadLongFromFile - // skipped non-limited PyMarshal_ReadShortFromFile - // skipped non-limited PyMarshal_ReadObjectFromFile - // skipped non-limited PyMarshal_ReadLastObjectFromFile - - #[cfg_attr(PyPy, link_name = "PyPyMarshal_ReadObjectFromString")] - pub fn PyMarshal_ReadObjectFromString(data: *const c_char, len: Py_ssize_t) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/memoryobject.rs b/vendor/pyo3/pyo3-ffi/src/memoryobject.rs deleted file mode 100644 index b7ef9e2ef1..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/memoryobject.rs +++ /dev/null @@ -1,46 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(Py_LIMITED_API))] - pub static mut _PyManagedBuffer_Type: PyTypeObject; - - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_Type")] - pub static mut PyMemoryView_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyMemoryView_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyMemoryView_Type)) as c_int -} - -// skipped non-limited PyMemoryView_GET_BUFFER -// skipped non-limited PyMemeryView_GET_BASE - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromObject")] - pub fn PyMemoryView_FromObject(base: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromMemory")] - pub fn PyMemoryView_FromMemory( - mem: *mut c_char, - size: Py_ssize_t, - flags: c_int, - ) -> *mut PyObject; - #[cfg(any(Py_3_11, not(Py_LIMITED_API)))] - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_FromBuffer")] - pub fn PyMemoryView_FromBuffer(view: *const crate::Py_buffer) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_GetContiguous")] - pub fn PyMemoryView_GetContiguous( - base: *mut PyObject, - buffertype: c_int, - order: c_char, - ) -> *mut PyObject; -} - -// skipped remainder of file with comment: -/* The structs are declared here so that macros can work, but they shouldn't -be considered public. Don't access their fields directly, use the macros -and functions instead! */ diff --git a/vendor/pyo3/pyo3-ffi/src/methodobject.rs b/vendor/pyo3/pyo3-ffi/src/methodobject.rs deleted file mode 100644 index 3dfbbb5a20..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/methodobject.rs +++ /dev/null @@ -1,284 +0,0 @@ -use crate::object::{PyObject, PyTypeObject, Py_TYPE}; -#[cfg(Py_3_9)] -use crate::PyObject_TypeCheck; -use std::os::raw::{c_char, c_int, c_void}; -use std::{mem, ptr}; - -#[cfg(all(Py_3_9, not(Py_LIMITED_API), not(GraalPy)))] -pub struct PyCFunctionObject { - pub ob_base: PyObject, - pub m_ml: *mut PyMethodDef, - pub m_self: *mut PyObject, - pub m_module: *mut PyObject, - pub m_weakreflist: *mut PyObject, - #[cfg(not(PyPy))] - pub vectorcall: Option, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_Type")] - pub static mut PyCFunction_Type: PyTypeObject; -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == ptr::addr_of_mut!(PyCFunction_Type)) as c_int -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, ptr::addr_of_mut!(PyCFunction_Type)) -} - -#[cfg(not(Py_3_9))] -#[inline] -pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == ptr::addr_of_mut!(PyCFunction_Type)) as c_int -} - -pub type PyCFunction = - unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub type PyCFunctionFast = unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut *mut PyObject, - nargs: crate::pyport::Py_ssize_t, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -#[deprecated(note = "renamed to `PyCFunctionFast`")] -pub type _PyCFunctionFast = PyCFunctionFast; - -pub type PyCFunctionWithKeywords = unsafe extern "C" fn( - slf: *mut PyObject, - args: *mut PyObject, - kwds: *mut PyObject, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub type PyCFunctionFastWithKeywords = unsafe extern "C" fn( - slf: *mut PyObject, - args: *const *mut PyObject, - nargs: crate::pyport::Py_ssize_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -#[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")] -pub type _PyCFunctionFastWithKeywords = PyCFunctionFastWithKeywords; - -#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] -pub type PyCMethod = unsafe extern "C" fn( - slf: *mut PyObject, - defining_class: *mut PyTypeObject, - args: *const *mut PyObject, - nargs: crate::pyport::Py_ssize_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_GetFunction")] - pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option; - pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject; - pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int; - #[cfg(not(Py_3_13))] - #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] - pub fn PyCFunction_Call( - f: *mut PyObject, - args: *mut PyObject, - kwds: *mut PyObject, - ) -> *mut PyObject; -} - -/// Represents the [PyMethodDef](https://docs.python.org/3/c-api/structures.html#c.PyMethodDef) -/// structure. -/// -/// Note that CPython may leave fields uninitialized. You must ensure that -/// `ml_name` != NULL before dereferencing or reading other fields. -#[repr(C)] -#[derive(Copy, Clone, PartialEq, Eq)] -pub struct PyMethodDef { - pub ml_name: *const c_char, - pub ml_meth: PyMethodDefPointer, - pub ml_flags: c_int, - pub ml_doc: *const c_char, -} - -impl PyMethodDef { - pub const fn zeroed() -> PyMethodDef { - PyMethodDef { - ml_name: ptr::null(), - ml_meth: PyMethodDefPointer { - Void: ptr::null_mut(), - }, - ml_flags: 0, - ml_doc: ptr::null(), - } - } -} - -impl Default for PyMethodDef { - fn default() -> PyMethodDef { - PyMethodDef { - ml_name: ptr::null(), - ml_meth: PyMethodDefPointer { - Void: ptr::null_mut(), - }, - ml_flags: 0, - ml_doc: ptr::null(), - } - } -} - -/// Function types used to implement Python callables. -/// -/// This function pointer must be accompanied by the correct [ml_flags](PyMethodDef::ml_flags), -/// otherwise the behavior is undefined. -/// -/// See the [Python C API documentation][1] for more information. -/// -/// [1]: https://docs.python.org/3/c-api/structures.html#implementing-functions-and-methods -#[repr(C)] -#[derive(Copy, Clone, Eq)] -pub union PyMethodDefPointer { - /// This variant corresponds with [`METH_VARARGS`] *or* [`METH_NOARGS`] *or* [`METH_O`]. - pub PyCFunction: PyCFunction, - - /// This variant corresponds with [`METH_VARARGS`] | [`METH_KEYWORDS`]. - pub PyCFunctionWithKeywords: PyCFunctionWithKeywords, - - /// This variant corresponds with [`METH_FASTCALL`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[deprecated(note = "renamed to `PyCFunctionFast`")] - pub _PyCFunctionFast: PyCFunctionFast, - - /// This variant corresponds with [`METH_FASTCALL`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub PyCFunctionFast: PyCFunctionFast, - - /// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")] - pub _PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords, - - /// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords, - - /// This variant corresponds with [`METH_METHOD`] | [`METH_FASTCALL`] | [`METH_KEYWORDS`]. - #[cfg(all(Py_3_9, not(Py_LIMITED_API)))] - pub PyCMethod: PyCMethod, - - Void: *mut c_void, -} - -impl PyMethodDefPointer { - pub fn as_ptr(&self) -> *mut c_void { - unsafe { self.Void } - } - - pub fn is_null(&self) -> bool { - self.as_ptr().is_null() - } - - pub const fn zeroed() -> PyMethodDefPointer { - PyMethodDefPointer { - Void: ptr::null_mut(), - } - } -} - -impl PartialEq for PyMethodDefPointer { - fn eq(&self, other: &Self) -> bool { - unsafe { self.Void == other.Void } - } -} - -impl std::fmt::Pointer for PyMethodDefPointer { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let ptr = unsafe { self.Void }; - std::fmt::Pointer::fmt(&ptr, f) - } -} - -const _: () = - assert!(mem::size_of::() == mem::size_of::>()); - -#[cfg(not(Py_3_9))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCFunction_New")] - pub fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyCFunction_NewEx")] - pub fn PyCFunction_NewEx( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, - ) -> *mut PyObject; -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject { - PyCFunction_NewEx(ml, slf, std::ptr::null_mut()) -} - -#[cfg(Py_3_9)] -#[inline] -pub unsafe fn PyCFunction_NewEx( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, -) -> *mut PyObject { - PyCMethod_New(ml, slf, module, std::ptr::null_mut()) -} - -#[cfg(Py_3_9)] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCMethod_New")] - pub fn PyCMethod_New( - ml: *mut PyMethodDef, - slf: *mut PyObject, - module: *mut PyObject, - cls: *mut PyTypeObject, - ) -> *mut PyObject; -} - -/* Flag passed to newmethodobject */ -pub const METH_VARARGS: c_int = 0x0001; -pub const METH_KEYWORDS: c_int = 0x0002; -/* METH_NOARGS and METH_O must not be combined with the flags above. */ -pub const METH_NOARGS: c_int = 0x0004; -pub const METH_O: c_int = 0x0008; - -/* METH_CLASS and METH_STATIC are a little different; these control -the construction of methods for a class. These cannot be used for -functions in modules. */ -pub const METH_CLASS: c_int = 0x0010; -pub const METH_STATIC: c_int = 0x0020; - -/* METH_COEXIST allows a method to be entered eventhough a slot has -already filled the entry. When defined, the flag allows a separate -method, "__contains__" for example, to coexist with a defined -slot like sq_contains. */ - -pub const METH_COEXIST: c_int = 0x0040; - -/* METH_FASTCALL indicates the PEP 590 Vectorcall calling format. It may -be specified alone or with METH_KEYWORDS. */ -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -pub const METH_FASTCALL: c_int = 0x0080; - -// skipped METH_STACKLESS - -#[cfg(all(Py_3_9, not(Py_LIMITED_API)))] -pub const METH_METHOD: c_int = 0x0200; - -extern "C" { - #[cfg(not(Py_3_9))] - pub fn PyCFunction_ClearFreeList() -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/modsupport.rs b/vendor/pyo3/pyo3-ffi/src/modsupport.rs deleted file mode 100644 index 6da2795bbc..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/modsupport.rs +++ /dev/null @@ -1,151 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::moduleobject::PyModuleDef; -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_long}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyArg_Parse")] - pub fn PyArg_Parse(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTuple")] - pub fn PyArg_ParseTuple(arg1: *mut PyObject, arg2: *const c_char, ...) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_ParseTupleAndKeywords")] - pub fn PyArg_ParseTupleAndKeywords( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *const c_char, - #[cfg(not(Py_3_13))] arg4: *mut *mut c_char, - #[cfg(Py_3_13)] arg4: *const *const c_char, - ... - ) -> c_int; - - // skipped PyArg_VaParse - // skipped PyArg_VaParseTupleAndKeywords - - pub fn PyArg_ValidateKeywordArguments(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyArg_UnpackTuple")] - pub fn PyArg_UnpackTuple( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: Py_ssize_t, - arg4: Py_ssize_t, - ... - ) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPy_BuildValue")] - pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject; - // skipped Py_VaBuildValue - - #[cfg(Py_3_13)] - pub fn PyModule_Add( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> core::ffi::c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyModule_AddObjectRef")] - pub fn PyModule_AddObjectRef( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddObject")] - pub fn PyModule_AddObject( - module: *mut PyObject, - name: *const c_char, - value: *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddIntConstant")] - pub fn PyModule_AddIntConstant( - module: *mut PyObject, - name: *const c_char, - value: c_long, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyModule_AddStringConstant")] - pub fn PyModule_AddStringConstant( - module: *mut PyObject, - name: *const c_char, - value: *const c_char, - ) -> c_int; - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyModule_AddType")] - pub fn PyModule_AddType( - module: *mut PyObject, - type_: *mut crate::object::PyTypeObject, - ) -> c_int; - // skipped PyModule_AddIntMacro - // skipped PyModule_AddStringMacro - pub fn PyModule_SetDocString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - pub fn PyModule_AddFunctions(arg1: *mut PyObject, arg2: *mut PyMethodDef) -> c_int; - pub fn PyModule_ExecDef(module: *mut PyObject, def: *mut PyModuleDef) -> c_int; -} - -pub const Py_CLEANUP_SUPPORTED: i32 = 0x2_0000; - -pub const PYTHON_API_VERSION: i32 = 1013; -pub const PYTHON_ABI_VERSION: i32 = 3; - -extern "C" { - #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] - #[cfg_attr(PyPy, link_name = "PyPyModule_Create2")] - pub fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject; - - #[cfg(py_sys_config = "Py_TRACE_REFS")] - fn PyModule_Create2TraceRefs(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject; - - #[cfg(not(py_sys_config = "Py_TRACE_REFS"))] - pub fn PyModule_FromDefAndSpec2( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, - ) -> *mut PyObject; - - #[cfg(py_sys_config = "Py_TRACE_REFS")] - fn PyModule_FromDefAndSpec2TraceRefs( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, - ) -> *mut PyObject; -} - -#[cfg(py_sys_config = "Py_TRACE_REFS")] -#[inline] -pub unsafe fn PyModule_Create2(module: *mut PyModuleDef, apiver: c_int) -> *mut PyObject { - PyModule_Create2TraceRefs(module, apiver) -} - -#[cfg(py_sys_config = "Py_TRACE_REFS")] -#[inline] -pub unsafe fn PyModule_FromDefAndSpec2( - def: *mut PyModuleDef, - spec: *mut PyObject, - module_api_version: c_int, -) -> *mut PyObject { - PyModule_FromDefAndSpec2TraceRefs(def, spec, module_api_version) -} - -#[inline] -pub unsafe fn PyModule_Create(module: *mut PyModuleDef) -> *mut PyObject { - PyModule_Create2( - module, - if cfg!(Py_LIMITED_API) { - PYTHON_ABI_VERSION - } else { - PYTHON_API_VERSION - }, - ) -} - -#[inline] -pub unsafe fn PyModule_FromDefAndSpec(def: *mut PyModuleDef, spec: *mut PyObject) -> *mut PyObject { - PyModule_FromDefAndSpec2( - def, - spec, - if cfg!(Py_LIMITED_API) { - PYTHON_ABI_VERSION - } else { - PYTHON_API_VERSION - }, - ) -} diff --git a/vendor/pyo3/pyo3-ffi/src/moduleobject.rs b/vendor/pyo3/pyo3-ffi/src/moduleobject.rs deleted file mode 100644 index 2417664a42..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/moduleobject.rs +++ /dev/null @@ -1,124 +0,0 @@ -use crate::methodobject::PyMethodDef; -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyModule_Type")] - pub static mut PyModule_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyModule_Check(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(PyModule_Type)) -} - -#[inline] -pub unsafe fn PyModule_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyModule_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyModule_NewObject")] - pub fn PyModule_NewObject(name: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_New")] - pub fn PyModule_New(name: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetDict")] - pub fn PyModule_GetDict(arg1: *mut PyObject) -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyModule_GetNameObject(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetName")] - pub fn PyModule_GetName(arg1: *mut PyObject) -> *const c_char; - #[cfg(not(all(windows, PyPy)))] - #[deprecated(note = "Python 3.2")] - pub fn PyModule_GetFilename(arg1: *mut PyObject) -> *const c_char; - #[cfg(not(PyPy))] - pub fn PyModule_GetFilenameObject(arg1: *mut PyObject) -> *mut PyObject; - // skipped non-limited _PyModule_Clear - // skipped non-limited _PyModule_ClearDict - // skipped non-limited _PyModuleSpec_IsInitializing - #[cfg_attr(PyPy, link_name = "PyPyModule_GetDef")] - pub fn PyModule_GetDef(arg1: *mut PyObject) -> *mut PyModuleDef; - #[cfg_attr(PyPy, link_name = "PyPyModule_GetState")] - pub fn PyModule_GetState(arg1: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyModuleDef_Init")] - pub fn PyModuleDef_Init(arg1: *mut PyModuleDef) -> *mut PyObject; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut PyModuleDef_Type: PyTypeObject; -} - -#[repr(C)] -pub struct PyModuleDef_Base { - pub ob_base: PyObject, - pub m_init: Option *mut PyObject>, - pub m_index: Py_ssize_t, - pub m_copy: *mut PyObject, -} - -#[allow(clippy::declare_interior_mutable_const)] -pub const PyModuleDef_HEAD_INIT: PyModuleDef_Base = PyModuleDef_Base { - ob_base: PyObject_HEAD_INIT, - m_init: None, - m_index: 0, - m_copy: std::ptr::null_mut(), -}; - -#[repr(C)] -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct PyModuleDef_Slot { - pub slot: c_int, - pub value: *mut c_void, -} - -impl Default for PyModuleDef_Slot { - fn default() -> PyModuleDef_Slot { - PyModuleDef_Slot { - slot: 0, - value: std::ptr::null_mut(), - } - } -} - -pub const Py_mod_create: c_int = 1; -pub const Py_mod_exec: c_int = 2; -#[cfg(Py_3_12)] -pub const Py_mod_multiple_interpreters: c_int = 3; -#[cfg(Py_3_13)] -pub const Py_mod_gil: c_int = 4; - -// skipped private _Py_mod_LAST_SLOT - -#[cfg(Py_3_12)] -pub const Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED: *mut c_void = 0 as *mut c_void; -#[cfg(Py_3_12)] -pub const Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED: *mut c_void = 1 as *mut c_void; -#[cfg(Py_3_12)] -pub const Py_MOD_PER_INTERPRETER_GIL_SUPPORTED: *mut c_void = 2 as *mut c_void; - -#[cfg(Py_3_13)] -pub const Py_MOD_GIL_USED: *mut c_void = 0 as *mut c_void; -#[cfg(Py_3_13)] -pub const Py_MOD_GIL_NOT_USED: *mut c_void = 1 as *mut c_void; - -#[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] -extern "C" { - pub fn PyUnstable_Module_SetGIL(module: *mut PyObject, gil: *mut c_void) -> c_int; -} - -#[repr(C)] -pub struct PyModuleDef { - pub m_base: PyModuleDef_Base, - pub m_name: *const c_char, - pub m_doc: *const c_char, - pub m_size: Py_ssize_t, - pub m_methods: *mut PyMethodDef, - pub m_slots: *mut PyModuleDef_Slot, - pub m_traverse: Option, - pub m_clear: Option, - pub m_free: Option, -} diff --git a/vendor/pyo3/pyo3-ffi/src/object.rs b/vendor/pyo3/pyo3-ffi/src/object.rs deleted file mode 100644 index 51083a8e4b..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/object.rs +++ /dev/null @@ -1,945 +0,0 @@ -use crate::pyport::{Py_hash_t, Py_ssize_t}; -#[cfg(Py_GIL_DISABLED)] -use crate::PyMutex; -#[cfg(Py_GIL_DISABLED)] -use std::marker::PhantomPinned; -use std::mem; -use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void}; -use std::ptr; -#[cfg(Py_GIL_DISABLED)] -use std::sync::atomic::{AtomicIsize, AtomicU32, AtomicU8, Ordering::Relaxed}; - -#[cfg(Py_LIMITED_API)] -opaque_struct!(PyTypeObject); - -#[cfg(not(Py_LIMITED_API))] -pub use crate::cpython::object::PyTypeObject; - -#[cfg(Py_3_12)] -const _Py_IMMORTAL_REFCNT: Py_ssize_t = { - if cfg!(target_pointer_width = "64") { - c_uint::MAX as Py_ssize_t - } else { - // for 32-bit systems, use the lower 30 bits (see comment in CPython's object.h) - (c_uint::MAX >> 2) as Py_ssize_t - } -}; - -#[cfg(Py_GIL_DISABLED)] -const _Py_IMMORTAL_REFCNT_LOCAL: u32 = u32::MAX; - -#[allow(clippy::declare_interior_mutable_const)] -pub const PyObject_HEAD_INIT: PyObject = PyObject { - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_next: std::ptr::null_mut(), - #[cfg(py_sys_config = "Py_TRACE_REFS")] - _ob_prev: std::ptr::null_mut(), - #[cfg(Py_GIL_DISABLED)] - ob_tid: 0, - #[cfg(Py_GIL_DISABLED)] - _padding: 0, - #[cfg(Py_GIL_DISABLED)] - ob_mutex: PyMutex { - _bits: AtomicU8::new(0), - _pin: PhantomPinned, - }, - #[cfg(Py_GIL_DISABLED)] - ob_gc_bits: 0, - #[cfg(Py_GIL_DISABLED)] - ob_ref_local: AtomicU32::new(_Py_IMMORTAL_REFCNT_LOCAL), - #[cfg(Py_GIL_DISABLED)] - ob_ref_shared: AtomicIsize::new(0), - #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] - ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 }, - #[cfg(not(Py_3_12))] - ob_refcnt: 1, - #[cfg(PyPy)] - ob_pypy_link: 0, - ob_type: std::ptr::null_mut(), -}; - -// skipped PyObject_VAR_HEAD -// skipped Py_INVALID_SIZE - -// skipped private _Py_UNOWNED_TID - -#[cfg(Py_GIL_DISABLED)] -const _Py_REF_SHARED_SHIFT: isize = 2; -// skipped private _Py_REF_SHARED_FLAG_MASK - -// skipped private _Py_REF_SHARED_INIT -// skipped private _Py_REF_MAYBE_WEAKREF -// skipped private _Py_REF_QUEUED -// skipped private _Py_REF_MERGED - -// skipped private _Py_REF_SHARED - -#[repr(C)] -#[derive(Copy, Clone)] -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -/// This union is anonymous in CPython, so the name was given by PyO3 because -/// Rust unions need a name. -pub union PyObjectObRefcnt { - pub ob_refcnt: Py_ssize_t, - #[cfg(target_pointer_width = "64")] - pub ob_refcnt_split: [crate::PY_UINT32_T; 2], -} - -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -impl std::fmt::Debug for PyObjectObRefcnt { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", unsafe { self.ob_refcnt }) - } -} - -#[cfg(all(not(Py_3_12), not(Py_GIL_DISABLED)))] -pub type PyObjectObRefcnt = Py_ssize_t; - -#[repr(C)] -#[derive(Debug)] -pub struct PyObject { - #[cfg(py_sys_config = "Py_TRACE_REFS")] - pub _ob_next: *mut PyObject, - #[cfg(py_sys_config = "Py_TRACE_REFS")] - pub _ob_prev: *mut PyObject, - #[cfg(Py_GIL_DISABLED)] - pub ob_tid: libc::uintptr_t, - #[cfg(Py_GIL_DISABLED)] - pub _padding: u16, - #[cfg(Py_GIL_DISABLED)] - pub ob_mutex: PyMutex, // per-object lock - #[cfg(Py_GIL_DISABLED)] - pub ob_gc_bits: u8, // gc-related state - #[cfg(Py_GIL_DISABLED)] - pub ob_ref_local: AtomicU32, // local reference count - #[cfg(Py_GIL_DISABLED)] - pub ob_ref_shared: AtomicIsize, // shared reference count - #[cfg(not(Py_GIL_DISABLED))] - pub ob_refcnt: PyObjectObRefcnt, - #[cfg(PyPy)] - pub ob_pypy_link: Py_ssize_t, - pub ob_type: *mut PyTypeObject, -} - -// skipped private _PyObject_CAST - -#[repr(C)] -#[derive(Debug)] -pub struct PyVarObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub ob_size: Py_ssize_t, - // On GraalPy the field is physically there, but not always populated. We hide it to prevent accidental misuse - #[cfg(GraalPy)] - pub _ob_size_graalpy: Py_ssize_t, -} - -// skipped private _PyVarObject_CAST - -#[inline] -#[cfg(not(all(PyPy, Py_3_10)))] -#[cfg_attr(docsrs, doc(cfg(all())))] -pub unsafe fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int { - (x == y).into() -} - -#[cfg(all(PyPy, Py_3_10))] -#[cfg_attr(docsrs, doc(cfg(all())))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPy_Is")] - pub fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int; -} - -// skipped private _Py_GetThreadLocal_Addr - -// skipped private _Py_ThreadId - -// skipped private _Py_IsOwnedByCurrentThread - -#[inline] -pub unsafe fn Py_REFCNT(ob: *mut PyObject) -> Py_ssize_t { - #[cfg(Py_GIL_DISABLED)] - { - let local = (*ob).ob_ref_local.load(Relaxed); - if local == _Py_IMMORTAL_REFCNT_LOCAL { - return _Py_IMMORTAL_REFCNT; - } - let shared = (*ob).ob_ref_shared.load(Relaxed); - local as Py_ssize_t + Py_ssize_t::from(shared >> _Py_REF_SHARED_SHIFT) - } - - #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))] - { - (*ob).ob_refcnt.ob_refcnt - } - - #[cfg(all(not(Py_GIL_DISABLED), not(Py_3_12), not(GraalPy)))] - { - (*ob).ob_refcnt - } - - #[cfg(all(not(Py_GIL_DISABLED), not(Py_3_12), GraalPy))] - { - _Py_REFCNT(ob) - } -} - -#[inline] -pub unsafe fn Py_TYPE(ob: *mut PyObject) -> *mut PyTypeObject { - #[cfg(not(GraalPy))] - return (*ob).ob_type; - #[cfg(GraalPy)] - return _Py_TYPE(ob); -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyLong_Type")] - pub static mut PyLong_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyBool_Type")] - pub static mut PyBool_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn Py_SIZE(ob: *mut PyObject) -> Py_ssize_t { - #[cfg(not(GraalPy))] - { - debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyLong_Type)); - debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyBool_Type)); - (*ob.cast::()).ob_size - } - #[cfg(GraalPy)] - _Py_SIZE(ob) -} - -#[inline(always)] -#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] -pub unsafe fn _Py_IsImmortal(op: *mut PyObject) -> c_int { - #[cfg(target_pointer_width = "64")] - { - (((*op).ob_refcnt.ob_refcnt as crate::PY_INT32_T) < 0) as c_int - } - - #[cfg(target_pointer_width = "32")] - { - ((*op).ob_refcnt.ob_refcnt == _Py_IMMORTAL_REFCNT) as c_int - } -} - -#[inline] -pub unsafe fn Py_IS_TYPE(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int { - (Py_TYPE(ob) == tp) as c_int -} - -// skipped _Py_SetRefCnt - -// skipped Py_SET_REFCNT - -// skipped Py_SET_TYPE - -// skipped Py_SET_SIZE - -pub type unaryfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type binaryfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject; -pub type ternaryfunc = - unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type inquiry = unsafe extern "C" fn(*mut PyObject) -> c_int; -pub type lenfunc = unsafe extern "C" fn(*mut PyObject) -> Py_ssize_t; -pub type ssizeargfunc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t) -> *mut PyObject; -pub type ssizessizeargfunc = - unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t) -> *mut PyObject; -pub type ssizeobjargproc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t, *mut PyObject) -> c_int; -pub type ssizessizeobjargproc = - unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t, arg4: *mut PyObject) -> c_int; -pub type objobjargproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; - -pub type objobjproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> c_int; -pub type visitproc = unsafe extern "C" fn(object: *mut PyObject, arg: *mut c_void) -> c_int; -pub type traverseproc = - unsafe extern "C" fn(slf: *mut PyObject, visit: visitproc, arg: *mut c_void) -> c_int; - -pub type freefunc = unsafe extern "C" fn(*mut c_void); -pub type destructor = unsafe extern "C" fn(*mut PyObject); -pub type getattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char) -> *mut PyObject; -pub type getattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject; -pub type setattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char, *mut PyObject) -> c_int; -pub type setattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type reprfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type hashfunc = unsafe extern "C" fn(*mut PyObject) -> Py_hash_t; -pub type richcmpfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, c_int) -> *mut PyObject; -pub type getiterfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type iternextfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject; -pub type descrgetfunc = - unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type descrsetfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type initproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int; -pub type newfunc = - unsafe extern "C" fn(*mut PyTypeObject, *mut PyObject, *mut PyObject) -> *mut PyObject; -pub type allocfunc = unsafe extern "C" fn(*mut PyTypeObject, Py_ssize_t) -> *mut PyObject; - -#[cfg(Py_3_8)] -pub type vectorcallfunc = unsafe extern "C" fn( - callable: *mut PyObject, - args: *const *mut PyObject, - nargsf: libc::size_t, - kwnames: *mut PyObject, -) -> *mut PyObject; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyType_Slot { - pub slot: c_int, - pub pfunc: *mut c_void, -} - -impl Default for PyType_Slot { - fn default() -> PyType_Slot { - unsafe { mem::zeroed() } - } -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyType_Spec { - pub name: *const c_char, - pub basicsize: c_int, - pub itemsize: c_int, - pub flags: c_uint, - pub slots: *mut PyType_Slot, -} - -impl Default for PyType_Spec { - fn default() -> PyType_Spec { - unsafe { mem::zeroed() } - } -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyType_FromSpec")] - pub fn PyType_FromSpec(arg1: *mut PyType_Spec) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyType_FromSpecWithBases")] - pub fn PyType_FromSpecWithBases(arg1: *mut PyType_Spec, arg2: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyType_GetSlot")] - pub fn PyType_GetSlot(arg1: *mut PyTypeObject, arg2: c_int) -> *mut c_void; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_FromModuleAndSpec")] - pub fn PyType_FromModuleAndSpec( - module: *mut PyObject, - spec: *mut PyType_Spec, - bases: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModule")] - pub fn PyType_GetModule(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleState")] - pub fn PyType_GetModuleState(arg1: *mut PyTypeObject) -> *mut c_void; - - #[cfg(Py_3_11)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetName")] - pub fn PyType_GetName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_11)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetQualName")] - pub fn PyType_GetQualName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetFullyQualifiedName")] - pub fn PyType_GetFullyQualifiedName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleName")] - pub fn PyType_GetModuleName(arg1: *mut PyTypeObject) -> *mut PyObject; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyType_FromMetaclass")] - pub fn PyType_FromMetaclass( - metaclass: *mut PyTypeObject, - module: *mut PyObject, - spec: *mut PyType_Spec, - bases: *mut PyObject, - ) -> *mut PyObject; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeData")] - pub fn PyObject_GetTypeData(obj: *mut PyObject, cls: *mut PyTypeObject) -> *mut c_void; - - #[cfg(Py_3_12)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeDataSize")] - pub fn PyObject_GetTypeDataSize(cls: *mut PyTypeObject) -> Py_ssize_t; - - #[cfg_attr(PyPy, link_name = "PyPyType_IsSubtype")] - pub fn PyType_IsSubtype(a: *mut PyTypeObject, b: *mut PyTypeObject) -> c_int; -} - -#[inline] -pub unsafe fn PyObject_TypeCheck(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int { - (Py_IS_TYPE(ob, tp) != 0 || PyType_IsSubtype(Py_TYPE(ob), tp) != 0) as c_int -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - /// built-in 'type' - #[cfg_attr(PyPy, link_name = "PyPyType_Type")] - pub static mut PyType_Type: PyTypeObject; - /// built-in 'object' - #[cfg_attr(PyPy, link_name = "PyPyBaseObject_Type")] - pub static mut PyBaseObject_Type: PyTypeObject; - /// built-in 'super' - pub static mut PySuper_Type: PyTypeObject; -} - -extern "C" { - pub fn PyType_GetFlags(arg1: *mut PyTypeObject) -> c_ulong; - - #[cfg_attr(PyPy, link_name = "PyPyType_Ready")] - pub fn PyType_Ready(t: *mut PyTypeObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyType_GenericAlloc")] - pub fn PyType_GenericAlloc(t: *mut PyTypeObject, nitems: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyType_GenericNew")] - pub fn PyType_GenericNew( - t: *mut PyTypeObject, - args: *mut PyObject, - kwds: *mut PyObject, - ) -> *mut PyObject; - pub fn PyType_ClearCache() -> c_uint; - #[cfg_attr(PyPy, link_name = "PyPyType_Modified")] - pub fn PyType_Modified(t: *mut PyTypeObject); - - #[cfg_attr(PyPy, link_name = "PyPyObject_Repr")] - pub fn PyObject_Repr(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Str")] - pub fn PyObject_Str(o: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_ASCII")] - pub fn PyObject_ASCII(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_Bytes")] - pub fn PyObject_Bytes(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompare")] - pub fn PyObject_RichCompare( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompareBool")] - pub fn PyObject_RichCompareBool(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int) - -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttrString")] - pub fn PyObject_GetAttrString(arg1: *mut PyObject, arg2: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttrString")] - pub fn PyObject_SetAttrString( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: *mut PyObject, - ) -> c_int; - #[cfg(any(Py_3_13, PyPy))] // CPython defined in 3.12 as an inline function in abstract.h - #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttrString")] - pub fn PyObject_DelAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrString")] - pub fn PyObject_HasAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttr")] - pub fn PyObject_GetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttr")] - pub fn PyObject_GetOptionalAttr( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut *mut PyObject, - ) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttrString")] - pub fn PyObject_GetOptionalAttrString( - arg1: *mut PyObject, - arg2: *const c_char, - arg3: *mut *mut PyObject, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttr")] - pub fn PyObject_SetAttr(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) - -> c_int; - #[cfg(any(Py_3_13, PyPy))] // CPython defined in 3.12 as an inline function in abstract.h - #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttr")] - pub fn PyObject_DelAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttr")] - pub fn PyObject_HasAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrWithError")] - pub fn PyObject_HasAttrWithError(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrStringWithError")] - pub fn PyObject_HasAttrStringWithError(arg1: *mut PyObject, arg2: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_SelfIter")] - pub fn PyObject_SelfIter(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetAttr")] - pub fn PyObject_GenericGetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetAttr")] - pub fn PyObject_GenericSetAttr( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> c_int; - #[cfg(not(all(Py_LIMITED_API, not(Py_3_10))))] - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetDict")] - pub fn PyObject_GenericGetDict(arg1: *mut PyObject, arg2: *mut c_void) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetDict")] - pub fn PyObject_GenericSetDict( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut c_void, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_Hash")] - pub fn PyObject_Hash(arg1: *mut PyObject) -> Py_hash_t; - #[cfg_attr(PyPy, link_name = "PyPyObject_HashNotImplemented")] - pub fn PyObject_HashNotImplemented(arg1: *mut PyObject) -> Py_hash_t; - #[cfg_attr(PyPy, link_name = "PyPyObject_IsTrue")] - pub fn PyObject_IsTrue(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_Not")] - pub fn PyObject_Not(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCallable_Check")] - pub fn PyCallable_Check(arg1: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyObject_ClearWeakRefs")] - pub fn PyObject_ClearWeakRefs(arg1: *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPyObject_Dir")] - pub fn PyObject_Dir(arg1: *mut PyObject) -> *mut PyObject; - pub fn Py_ReprEnter(arg1: *mut PyObject) -> c_int; - pub fn Py_ReprLeave(arg1: *mut PyObject); -} - -// Flag bits for printing: -pub const Py_PRINT_RAW: c_int = 1; // No string quotes etc. - -// skipped because is a private API -// const _Py_TPFLAGS_STATIC_BUILTIN: c_ulong = 1 << 1; - -#[cfg(all(Py_3_12, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MANAGED_WEAKREF: c_ulong = 1 << 3; - -#[cfg(all(Py_3_11, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MANAGED_DICT: c_ulong = 1 << 4; - -#[cfg(all(Py_3_10, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_SEQUENCE: c_ulong = 1 << 5; - -#[cfg(all(Py_3_10, not(Py_LIMITED_API)))] -pub const Py_TPFLAGS_MAPPING: c_ulong = 1 << 6; - -#[cfg(Py_3_10)] -pub const Py_TPFLAGS_DISALLOW_INSTANTIATION: c_ulong = 1 << 7; - -#[cfg(Py_3_10)] -pub const Py_TPFLAGS_IMMUTABLETYPE: c_ulong = 1 << 8; - -/// Set if the type object is dynamically allocated -pub const Py_TPFLAGS_HEAPTYPE: c_ulong = 1 << 9; - -/// Set if the type allows subclassing -pub const Py_TPFLAGS_BASETYPE: c_ulong = 1 << 10; - -/// Set if the type implements the vectorcall protocol (PEP 590) -#[cfg(any(Py_3_12, all(Py_3_8, not(Py_LIMITED_API))))] -pub const Py_TPFLAGS_HAVE_VECTORCALL: c_ulong = 1 << 11; -// skipped backwards-compatibility alias _Py_TPFLAGS_HAVE_VECTORCALL - -/// Set if the type is 'ready' -- fully initialized -pub const Py_TPFLAGS_READY: c_ulong = 1 << 12; - -/// Set while the type is being 'readied', to prevent recursive ready calls -pub const Py_TPFLAGS_READYING: c_ulong = 1 << 13; - -/// Objects support garbage collection (see objimp.h) -pub const Py_TPFLAGS_HAVE_GC: c_ulong = 1 << 14; - -const Py_TPFLAGS_HAVE_STACKLESS_EXTENSION: c_ulong = 0; - -#[cfg(Py_3_8)] -pub const Py_TPFLAGS_METHOD_DESCRIPTOR: c_ulong = 1 << 17; - -pub const Py_TPFLAGS_VALID_VERSION_TAG: c_ulong = 1 << 19; - -/* Type is abstract and cannot be instantiated */ -pub const Py_TPFLAGS_IS_ABSTRACT: c_ulong = 1 << 20; - -// skipped non-limited / 3.10 Py_TPFLAGS_HAVE_AM_SEND -#[cfg(Py_3_12)] -pub const Py_TPFLAGS_ITEMS_AT_END: c_ulong = 1 << 23; - -/* These flags are used to determine if a type is a subclass. */ -pub const Py_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24; -pub const Py_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25; -pub const Py_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26; -pub const Py_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27; -pub const Py_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28; -pub const Py_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29; -pub const Py_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30; -pub const Py_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31; - -pub const Py_TPFLAGS_DEFAULT: c_ulong = if cfg!(Py_3_10) { - Py_TPFLAGS_HAVE_STACKLESS_EXTENSION -} else { - Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | Py_TPFLAGS_HAVE_VERSION_TAG -}; - -pub const Py_TPFLAGS_HAVE_FINALIZE: c_ulong = 1; -pub const Py_TPFLAGS_HAVE_VERSION_TAG: c_ulong = 1 << 18; - -extern "C" { - #[cfg(all(py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_NegativeRefcount(filename: *const c_char, lineno: c_int, op: *mut PyObject); - #[cfg(all(Py_3_12, py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_INCREF_IncRefTotal(); - #[cfg(all(Py_3_12, py_sys_config = "Py_REF_DEBUG", not(Py_LIMITED_API)))] - fn _Py_DECREF_DecRefTotal(); - - #[cfg_attr(PyPy, link_name = "_PyPy_Dealloc")] - fn _Py_Dealloc(arg1: *mut PyObject); - - #[cfg_attr(PyPy, link_name = "PyPy_IncRef")] - #[cfg_attr(GraalPy, link_name = "_Py_IncRef")] - pub fn Py_IncRef(o: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPy_DecRef")] - #[cfg_attr(GraalPy, link_name = "_Py_DecRef")] - pub fn Py_DecRef(o: *mut PyObject); - - #[cfg(all(Py_3_10, not(PyPy)))] - fn _Py_IncRef(o: *mut PyObject); - #[cfg(all(Py_3_10, not(PyPy)))] - fn _Py_DecRef(o: *mut PyObject); - - #[cfg(GraalPy)] - fn _Py_REFCNT(arg1: *const PyObject) -> Py_ssize_t; - - #[cfg(GraalPy)] - fn _Py_TYPE(arg1: *const PyObject) -> *mut PyTypeObject; - - #[cfg(GraalPy)] - fn _Py_SIZE(arg1: *const PyObject) -> Py_ssize_t; -} - -#[inline(always)] -pub unsafe fn Py_INCREF(op: *mut PyObject) { - // On limited API, the free-threaded build, or with refcount debugging, let the interpreter do refcounting - // TODO: reimplement the logic in the header in the free-threaded build, for a little bit of performance. - #[cfg(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - py_sys_config = "Py_REF_DEBUG", - GraalPy - ))] - { - // _Py_IncRef was added to the ABI in 3.10; skips null checks - #[cfg(all(Py_3_10, not(PyPy)))] - { - _Py_IncRef(op); - } - - #[cfg(any(not(Py_3_10), PyPy))] - { - Py_IncRef(op); - } - } - - // version-specific builds are allowed to directly manipulate the reference count - #[cfg(not(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - py_sys_config = "Py_REF_DEBUG", - GraalPy - )))] - { - #[cfg(all(Py_3_12, target_pointer_width = "64"))] - { - let cur_refcnt = (*op).ob_refcnt.ob_refcnt_split[crate::PY_BIG_ENDIAN]; - let new_refcnt = cur_refcnt.wrapping_add(1); - if new_refcnt == 0 { - return; - } - (*op).ob_refcnt.ob_refcnt_split[crate::PY_BIG_ENDIAN] = new_refcnt; - } - - #[cfg(all(Py_3_12, target_pointer_width = "32"))] - { - if _Py_IsImmortal(op) != 0 { - return; - } - (*op).ob_refcnt.ob_refcnt += 1 - } - - #[cfg(not(Py_3_12))] - { - (*op).ob_refcnt += 1 - } - - // Skipped _Py_INCREF_STAT_INC - if anyone wants this, please file an issue - // or submit a PR supporting Py_STATS build option and pystats.h - } -} - -#[inline(always)] -#[cfg_attr( - all(py_sys_config = "Py_REF_DEBUG", Py_3_12, not(Py_LIMITED_API)), - track_caller -)] -pub unsafe fn Py_DECREF(op: *mut PyObject) { - // On limited API, the free-threaded build, or with refcount debugging, let the interpreter do refcounting - // On 3.12+ we implement refcount debugging to get better assertion locations on negative refcounts - // TODO: reimplement the logic in the header in the free-threaded build, for a little bit of performance. - #[cfg(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - all(py_sys_config = "Py_REF_DEBUG", not(Py_3_12)), - GraalPy - ))] - { - // _Py_DecRef was added to the ABI in 3.10; skips null checks - #[cfg(all(Py_3_10, not(PyPy)))] - { - _Py_DecRef(op); - } - - #[cfg(any(not(Py_3_10), PyPy))] - { - Py_DecRef(op); - } - } - - #[cfg(not(any( - Py_GIL_DISABLED, - Py_LIMITED_API, - all(py_sys_config = "Py_REF_DEBUG", not(Py_3_12)), - GraalPy - )))] - { - #[cfg(Py_3_12)] - if _Py_IsImmortal(op) != 0 { - return; - } - - // Skipped _Py_DECREF_STAT_INC - if anyone needs this, please file an issue - // or submit a PR supporting Py_STATS build option and pystats.h - - #[cfg(py_sys_config = "Py_REF_DEBUG")] - _Py_DECREF_DecRefTotal(); - - #[cfg(Py_3_12)] - { - (*op).ob_refcnt.ob_refcnt -= 1; - - #[cfg(py_sys_config = "Py_REF_DEBUG")] - if (*op).ob_refcnt.ob_refcnt < 0 { - let location = std::panic::Location::caller(); - let filename = std::ffi::CString::new(location.file()).unwrap(); - _Py_NegativeRefcount(filename.as_ptr(), location.line() as i32, op); - } - - if (*op).ob_refcnt.ob_refcnt == 0 { - _Py_Dealloc(op); - } - } - - #[cfg(not(Py_3_12))] - { - (*op).ob_refcnt -= 1; - - if (*op).ob_refcnt == 0 { - _Py_Dealloc(op); - } - } - } -} - -#[inline] -pub unsafe fn Py_CLEAR(op: *mut *mut PyObject) { - let tmp = *op; - if !tmp.is_null() { - *op = ptr::null_mut(); - Py_DECREF(tmp); - } -} - -#[inline] -pub unsafe fn Py_XINCREF(op: *mut PyObject) { - if !op.is_null() { - Py_INCREF(op) - } -} - -#[inline] -pub unsafe fn Py_XDECREF(op: *mut PyObject) { - if !op.is_null() { - Py_DECREF(op) - } -} - -extern "C" { - #[cfg(all(Py_3_10, Py_LIMITED_API, not(PyPy)))] - #[cfg_attr(docsrs, doc(cfg(Py_3_10)))] - pub fn Py_NewRef(obj: *mut PyObject) -> *mut PyObject; - #[cfg(all(Py_3_10, Py_LIMITED_API, not(PyPy)))] - #[cfg_attr(docsrs, doc(cfg(Py_3_10)))] - pub fn Py_XNewRef(obj: *mut PyObject) -> *mut PyObject; -} - -// macro _Py_NewRef not public; reimplemented directly inside Py_NewRef here -// macro _Py_XNewRef not public; reimplemented directly inside Py_XNewRef here - -#[cfg(all(Py_3_10, any(not(Py_LIMITED_API), PyPy)))] -#[cfg_attr(docsrs, doc(cfg(Py_3_10)))] -#[inline] -pub unsafe fn Py_NewRef(obj: *mut PyObject) -> *mut PyObject { - Py_INCREF(obj); - obj -} - -#[cfg(all(Py_3_10, any(not(Py_LIMITED_API), PyPy)))] -#[cfg_attr(docsrs, doc(cfg(Py_3_10)))] -#[inline] -pub unsafe fn Py_XNewRef(obj: *mut PyObject) -> *mut PyObject { - Py_XINCREF(obj); - obj -} - -#[cfg(Py_3_13)] -pub const Py_CONSTANT_NONE: c_uint = 0; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_FALSE: c_uint = 1; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_TRUE: c_uint = 2; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ELLIPSIS: c_uint = 3; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_NOT_IMPLEMENTED: c_uint = 4; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ZERO: c_uint = 5; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_ONE: c_uint = 6; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_STR: c_uint = 7; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_BYTES: c_uint = 8; -#[cfg(Py_3_13)] -pub const Py_CONSTANT_EMPTY_TUPLE: c_uint = 9; - -extern "C" { - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPy_GetConstant")] - pub fn Py_GetConstant(constant_id: c_uint) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPy_GetConstantBorrowed")] - pub fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "_PyPy_NoneStruct")] - static mut _Py_NoneStruct: PyObject; - - #[cfg(GraalPy)] - static mut _Py_NoneStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_None() -> *mut PyObject { - #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))] - return Py_GetConstantBorrowed(Py_CONSTANT_NONE); - - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - return ptr::addr_of_mut!(_Py_NoneStruct); - - #[cfg(GraalPy)] - return _Py_NoneStructReference; -} - -#[inline] -pub unsafe fn Py_IsNone(x: *mut PyObject) -> c_int { - Py_Is(x, Py_None()) -} - -// skipped Py_RETURN_NONE - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "_PyPy_NotImplementedStruct")] - static mut _Py_NotImplementedStruct: PyObject; - - #[cfg(GraalPy)] - static mut _Py_NotImplementedStructReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_NotImplemented() -> *mut PyObject { - #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))] - return Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED); - - #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))] - return ptr::addr_of_mut!(_Py_NotImplementedStruct); - - #[cfg(GraalPy)] - return _Py_NotImplementedStructReference; -} - -// skipped Py_RETURN_NOTIMPLEMENTED - -/* Rich comparison opcodes */ -pub const Py_LT: c_int = 0; -pub const Py_LE: c_int = 1; -pub const Py_EQ: c_int = 2; -pub const Py_NE: c_int = 3; -pub const Py_GT: c_int = 4; -pub const Py_GE: c_int = 5; - -#[cfg(Py_3_10)] -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum PySendResult { - PYGEN_RETURN = 0, - PYGEN_ERROR = -1, - PYGEN_NEXT = 1, -} - -// skipped Py_RETURN_RICHCOMPARE - -#[inline] -pub unsafe fn PyType_HasFeature(ty: *mut PyTypeObject, feature: c_ulong) -> c_int { - #[cfg(Py_LIMITED_API)] - let flags = PyType_GetFlags(ty); - - #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] - let flags = (*ty).tp_flags.load(std::sync::atomic::Ordering::Relaxed); - - #[cfg(all(not(Py_LIMITED_API), not(Py_GIL_DISABLED)))] - let flags = (*ty).tp_flags; - - ((flags & feature) != 0) as c_int -} - -#[inline] -pub unsafe fn PyType_FastSubclass(t: *mut PyTypeObject, f: c_ulong) -> c_int { - PyType_HasFeature(t, f) -} - -#[inline] -pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS) -} - -// skipped _PyType_CAST - -#[inline] -pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int { - Py_IS_TYPE(op, ptr::addr_of_mut!(PyType_Type)) -} - -extern "C" { - #[cfg(any(Py_3_13, all(Py_3_11, not(Py_LIMITED_API))))] - #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleByDef")] - pub fn PyType_GetModuleByDef( - arg1: *mut crate::PyTypeObject, - arg2: *mut crate::PyModuleDef, - ) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/objimpl.rs b/vendor/pyo3/pyo3-ffi/src/objimpl.rs deleted file mode 100644 index 76835a6d15..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/objimpl.rs +++ /dev/null @@ -1,94 +0,0 @@ -use libc::size_t; -use std::os::raw::{c_int, c_void}; - -use crate::object::*; -use crate::pyport::Py_ssize_t; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyObject_Malloc")] - pub fn PyObject_Malloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Calloc")] - pub fn PyObject_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Realloc")] - pub fn PyObject_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyObject_Free")] - pub fn PyObject_Free(ptr: *mut c_void); - - // skipped PyObject_MALLOC - // skipped PyObject_REALLOC - // skipped PyObject_FREE - // skipped PyObject_Del - // skipped PyObject_DEL - - #[cfg_attr(PyPy, link_name = "PyPyObject_Init")] - pub fn PyObject_Init(arg1: *mut PyObject, arg2: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_InitVar")] - pub fn PyObject_InitVar( - arg1: *mut PyVarObject, - arg2: *mut PyTypeObject, - arg3: Py_ssize_t, - ) -> *mut PyVarObject; - - // skipped PyObject_INIT - // skipped PyObject_INIT_VAR - - #[cfg_attr(PyPy, link_name = "_PyPyObject_New")] - pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyObject_NewVar")] - pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; - - // skipped PyObject_New - // skipped PyObject_NEW - // skipped PyObject_NewVar - // skipped PyObject_NEW_VAR - - pub fn PyGC_Collect() -> Py_ssize_t; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_Enable")] - pub fn PyGC_Enable() -> c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_Disable")] - pub fn PyGC_Disable() -> c_int; - - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyGC_IsEnabled")] - pub fn PyGC_IsEnabled() -> c_int; - - // skipped PyUnstable_GC_VisitObjects -} - -#[inline] -pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int { - PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC) -} - -extern "C" { - pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject; - - // skipped PyObject_GC_Resize - - #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_New")] - pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_NewVar")] - pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; - #[cfg(not(PyPy))] - pub fn PyObject_GC_Track(arg1: *mut c_void); - #[cfg(not(PyPy))] - pub fn PyObject_GC_UnTrack(arg1: *mut c_void); - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_Del")] - pub fn PyObject_GC_Del(arg1: *mut c_void); - - // skipped PyObject_GC_New - // skipped PyObject_GC_NewVar - - #[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsTracked")] - pub fn PyObject_GC_IsTracked(arg1: *mut PyObject) -> c_int; - #[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy - #[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsFinalized")] - pub fn PyObject_GC_IsFinalized(arg1: *mut PyObject) -> c_int; -} - -// skipped Py_VISIT diff --git a/vendor/pyo3/pyo3-ffi/src/osmodule.rs b/vendor/pyo3/pyo3-ffi/src/osmodule.rs deleted file mode 100644 index e6aefc362d..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/osmodule.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::object::PyObject; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_FSPath")] - pub fn PyOS_FSPath(path: *mut PyObject) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pyarena.rs b/vendor/pyo3/pyo3-ffi/src/pyarena.rs deleted file mode 100644 index 87d5f28a7a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pyarena.rs +++ /dev/null @@ -1 +0,0 @@ -opaque_struct!(PyArena); diff --git a/vendor/pyo3/pyo3-ffi/src/pybuffer.rs b/vendor/pyo3/pyo3-ffi/src/pybuffer.rs deleted file mode 100644 index 50bf4e6109..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pybuffer.rs +++ /dev/null @@ -1,134 +0,0 @@ -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct Py_buffer { - pub buf: *mut c_void, - /// Owned reference - pub obj: *mut crate::PyObject, - pub len: Py_ssize_t, - pub itemsize: Py_ssize_t, - pub readonly: c_int, - pub ndim: c_int, - pub format: *mut c_char, - pub shape: *mut Py_ssize_t, - pub strides: *mut Py_ssize_t, - pub suboffsets: *mut Py_ssize_t, - pub internal: *mut c_void, - #[cfg(PyPy)] - pub flags: c_int, - #[cfg(PyPy)] - pub _strides: [Py_ssize_t; PyBUF_MAX_NDIM], - #[cfg(PyPy)] - pub _shape: [Py_ssize_t; PyBUF_MAX_NDIM], -} - -impl Py_buffer { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - Py_buffer { - buf: ptr::null_mut(), - obj: ptr::null_mut(), - len: 0, - itemsize: 0, - readonly: 0, - ndim: 0, - format: ptr::null_mut(), - shape: ptr::null_mut(), - strides: ptr::null_mut(), - suboffsets: ptr::null_mut(), - internal: ptr::null_mut(), - #[cfg(PyPy)] - flags: 0, - #[cfg(PyPy)] - _strides: [0; PyBUF_MAX_NDIM], - #[cfg(PyPy)] - _shape: [0; PyBUF_MAX_NDIM], - } - } -} - -pub type getbufferproc = unsafe extern "C" fn(*mut PyObject, *mut crate::Py_buffer, c_int) -> c_int; -pub type releasebufferproc = unsafe extern "C" fn(*mut PyObject, *mut crate::Py_buffer); - -/* Return 1 if the getbuffer function is available, otherwise return 0. */ -extern "C" { - #[cfg(not(PyPy))] - pub fn PyObject_CheckBuffer(obj: *mut PyObject) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyObject_GetBuffer")] - pub fn PyObject_GetBuffer(obj: *mut PyObject, view: *mut Py_buffer, flags: c_int) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_GetPointer")] - pub fn PyBuffer_GetPointer(view: *const Py_buffer, indices: *const Py_ssize_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_SizeFromFormat")] - pub fn PyBuffer_SizeFromFormat(format: *const c_char) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_ToContiguous")] - pub fn PyBuffer_ToContiguous( - buf: *mut c_void, - view: *const Py_buffer, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FromContiguous")] - pub fn PyBuffer_FromContiguous( - view: *const Py_buffer, - buf: *const c_void, - len: Py_ssize_t, - order: c_char, - ) -> c_int; - pub fn PyObject_CopyData(dest: *mut PyObject, src: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_IsContiguous")] - pub fn PyBuffer_IsContiguous(view: *const Py_buffer, fort: c_char) -> c_int; - pub fn PyBuffer_FillContiguousStrides( - ndims: c_int, - shape: *mut Py_ssize_t, - strides: *mut Py_ssize_t, - itemsize: c_int, - fort: c_char, - ); - #[cfg_attr(PyPy, link_name = "PyPyBuffer_FillInfo")] - pub fn PyBuffer_FillInfo( - view: *mut Py_buffer, - o: *mut PyObject, - buf: *mut c_void, - len: Py_ssize_t, - readonly: c_int, - flags: c_int, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyBuffer_Release")] - pub fn PyBuffer_Release(view: *mut Py_buffer); -} - -/// Maximum number of dimensions -pub const PyBUF_MAX_NDIM: c_int = if cfg!(PyPy) { 36 } else { 64 }; - -/* Flags for getting buffers */ -pub const PyBUF_SIMPLE: c_int = 0; -pub const PyBUF_WRITABLE: c_int = 0x0001; -/* we used to include an E, backwards compatible alias */ -pub const PyBUF_WRITEABLE: c_int = PyBUF_WRITABLE; -pub const PyBUF_FORMAT: c_int = 0x0004; -pub const PyBUF_ND: c_int = 0x0008; -pub const PyBUF_STRIDES: c_int = 0x0010 | PyBUF_ND; -pub const PyBUF_C_CONTIGUOUS: c_int = 0x0020 | PyBUF_STRIDES; -pub const PyBUF_F_CONTIGUOUS: c_int = 0x0040 | PyBUF_STRIDES; -pub const PyBUF_ANY_CONTIGUOUS: c_int = 0x0080 | PyBUF_STRIDES; -pub const PyBUF_INDIRECT: c_int = 0x0100 | PyBUF_STRIDES; - -pub const PyBUF_CONTIG: c_int = PyBUF_ND | PyBUF_WRITABLE; -pub const PyBUF_CONTIG_RO: c_int = PyBUF_ND; - -pub const PyBUF_STRIDED: c_int = PyBUF_STRIDES | PyBUF_WRITABLE; -pub const PyBUF_STRIDED_RO: c_int = PyBUF_STRIDES; - -pub const PyBUF_RECORDS: c_int = PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT; -pub const PyBUF_RECORDS_RO: c_int = PyBUF_STRIDES | PyBUF_FORMAT; - -pub const PyBUF_FULL: c_int = PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT; -pub const PyBUF_FULL_RO: c_int = PyBUF_INDIRECT | PyBUF_FORMAT; - -pub const PyBUF_READ: c_int = 0x100; -pub const PyBUF_WRITE: c_int = 0x200; diff --git a/vendor/pyo3/pyo3-ffi/src/pycapsule.rs b/vendor/pyo3/pyo3-ffi/src/pycapsule.rs deleted file mode 100644 index 5b77841c23..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pycapsule.rs +++ /dev/null @@ -1,48 +0,0 @@ -use crate::object::*; -use std::os::raw::{c_char, c_int, c_void}; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCapsule_Type")] - pub static mut PyCapsule_Type: PyTypeObject; -} - -pub type PyCapsule_Destructor = unsafe extern "C" fn(o: *mut PyObject); - -#[inline] -pub unsafe fn PyCapsule_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyCapsule_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyCapsule_New")] - pub fn PyCapsule_New( - pointer: *mut c_void, - name: *const c_char, - destructor: Option, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetPointer")] - pub fn PyCapsule_GetPointer(capsule: *mut PyObject, name: *const c_char) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetDestructor")] - pub fn PyCapsule_GetDestructor(capsule: *mut PyObject) -> Option; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetName")] - pub fn PyCapsule_GetName(capsule: *mut PyObject) -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_GetContext")] - pub fn PyCapsule_GetContext(capsule: *mut PyObject) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_IsValid")] - pub fn PyCapsule_IsValid(capsule: *mut PyObject, name: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetPointer")] - pub fn PyCapsule_SetPointer(capsule: *mut PyObject, pointer: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetDestructor")] - pub fn PyCapsule_SetDestructor( - capsule: *mut PyObject, - destructor: Option, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetName")] - pub fn PyCapsule_SetName(capsule: *mut PyObject, name: *const c_char) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_SetContext")] - pub fn PyCapsule_SetContext(capsule: *mut PyObject, context: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyCapsule_Import")] - pub fn PyCapsule_Import(name: *const c_char, no_block: c_int) -> *mut c_void; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pyerrors.rs b/vendor/pyo3/pyo3-ffi/src/pyerrors.rs deleted file mode 100644 index 6c9313c4ab..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pyerrors.rs +++ /dev/null @@ -1,363 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_SetNone")] - pub fn PyErr_SetNone(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetObject")] - pub fn PyErr_SetObject(arg1: *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetString")] - pub fn PyErr_SetString(exception: *mut PyObject, string: *const c_char); - #[cfg_attr(PyPy, link_name = "PyPyErr_Occurred")] - pub fn PyErr_Occurred() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_Clear")] - pub fn PyErr_Clear(); - #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_GetRaisedException() instead."))] - #[cfg_attr(PyPy, link_name = "PyPyErr_Fetch")] - pub fn PyErr_Fetch( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_SetRaisedException() instead."))] - #[cfg_attr(PyPy, link_name = "PyPyErr_Restore")] - pub fn PyErr_Restore(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_GetExcInfo")] - pub fn PyErr_GetExcInfo( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg_attr(PyPy, link_name = "PyPyErr_SetExcInfo")] - pub fn PyErr_SetExcInfo(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPy_FatalError")] - pub fn Py_FatalError(message: *const c_char) -> !; - #[cfg_attr(PyPy, link_name = "PyPyErr_GivenExceptionMatches")] - pub fn PyErr_GivenExceptionMatches(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_ExceptionMatches")] - pub fn PyErr_ExceptionMatches(arg1: *mut PyObject) -> c_int; - #[cfg_attr( - Py_3_12, - deprecated( - note = "Use PyErr_GetRaisedException() instead, to avoid any possible de-normalization." - ) - )] - #[cfg_attr(PyPy, link_name = "PyPyErr_NormalizeException")] - pub fn PyErr_NormalizeException( - arg1: *mut *mut PyObject, - arg2: *mut *mut PyObject, - arg3: *mut *mut PyObject, - ); - #[cfg(Py_3_12)] - pub fn PyErr_GetRaisedException() -> *mut PyObject; - #[cfg(Py_3_12)] - pub fn PyErr_SetRaisedException(exc: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyException_SetTraceback")] - pub fn PyException_SetTraceback(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyException_GetTraceback")] - pub fn PyException_GetTraceback(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_GetCause")] - pub fn PyException_GetCause(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_SetCause")] - pub fn PyException_SetCause(arg1: *mut PyObject, arg2: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyException_GetContext")] - pub fn PyException_GetContext(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyException_SetContext")] - pub fn PyException_SetContext(arg1: *mut PyObject, arg2: *mut PyObject); - - #[cfg(PyPy)] - #[link_name = "PyPyExceptionInstance_Class"] - pub fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject; -} - -#[inline] -pub unsafe fn PyExceptionClass_Check(x: *mut PyObject) -> c_int { - (PyType_Check(x) != 0 - && PyType_FastSubclass(x as *mut PyTypeObject, Py_TPFLAGS_BASE_EXC_SUBCLASS) != 0) - as c_int -} - -#[inline] -pub unsafe fn PyExceptionInstance_Check(x: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject { - Py_TYPE(x) as *mut PyObject -} - -// ported from cpython exception.c (line 2096) -#[cfg(PyPy)] -pub unsafe fn PyUnicodeDecodeError_Create( - encoding: *const c_char, - object: *const c_char, - length: Py_ssize_t, - start: Py_ssize_t, - end: Py_ssize_t, - reason: *const c_char, -) -> *mut PyObject { - crate::_PyObject_CallFunction_SizeT( - PyExc_UnicodeDecodeError, - c_str!("sy#nns").as_ptr(), - encoding, - object, - length, - start, - end, - reason, - ) -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyExc_BaseException")] - pub static mut PyExc_BaseException: *mut PyObject; - #[cfg(Py_3_11)] - pub static mut PyExc_BaseExceptionGroup: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_Exception")] - pub static mut PyExc_Exception: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_StopAsyncIteration")] - pub static mut PyExc_StopAsyncIteration: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_StopIteration")] - pub static mut PyExc_StopIteration: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_GeneratorExit")] - pub static mut PyExc_GeneratorExit: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ArithmeticError")] - pub static mut PyExc_ArithmeticError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_LookupError")] - pub static mut PyExc_LookupError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_AssertionError")] - pub static mut PyExc_AssertionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_AttributeError")] - pub static mut PyExc_AttributeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BufferError")] - pub static mut PyExc_BufferError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_EOFError")] - pub static mut PyExc_EOFError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FloatingPointError")] - pub static mut PyExc_FloatingPointError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_OSError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ImportError")] - pub static mut PyExc_ImportError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ModuleNotFoundError")] - pub static mut PyExc_ModuleNotFoundError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IndexError")] - pub static mut PyExc_IndexError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_KeyError")] - pub static mut PyExc_KeyError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_KeyboardInterrupt")] - pub static mut PyExc_KeyboardInterrupt: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_MemoryError")] - pub static mut PyExc_MemoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NameError")] - pub static mut PyExc_NameError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OverflowError")] - pub static mut PyExc_OverflowError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeError")] - pub static mut PyExc_RuntimeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RecursionError")] - pub static mut PyExc_RecursionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NotImplementedError")] - pub static mut PyExc_NotImplementedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxError")] - pub static mut PyExc_SyntaxError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IndentationError")] - pub static mut PyExc_IndentationError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TabError")] - pub static mut PyExc_TabError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ReferenceError")] - pub static mut PyExc_ReferenceError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SystemError")] - pub static mut PyExc_SystemError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SystemExit")] - pub static mut PyExc_SystemExit: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TypeError")] - pub static mut PyExc_TypeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnboundLocalError")] - pub static mut PyExc_UnboundLocalError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeError")] - pub static mut PyExc_UnicodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeEncodeError")] - pub static mut PyExc_UnicodeEncodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeDecodeError")] - pub static mut PyExc_UnicodeDecodeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeTranslateError")] - pub static mut PyExc_UnicodeTranslateError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ValueError")] - pub static mut PyExc_ValueError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ZeroDivisionError")] - pub static mut PyExc_ZeroDivisionError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_BlockingIOError")] - pub static mut PyExc_BlockingIOError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BrokenPipeError")] - pub static mut PyExc_BrokenPipeError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ChildProcessError")] - pub static mut PyExc_ChildProcessError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionError")] - pub static mut PyExc_ConnectionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionAbortedError")] - pub static mut PyExc_ConnectionAbortedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionRefusedError")] - pub static mut PyExc_ConnectionRefusedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionResetError")] - pub static mut PyExc_ConnectionResetError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FileExistsError")] - pub static mut PyExc_FileExistsError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FileNotFoundError")] - pub static mut PyExc_FileNotFoundError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_InterruptedError")] - pub static mut PyExc_InterruptedError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_IsADirectoryError")] - pub static mut PyExc_IsADirectoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_NotADirectoryError")] - pub static mut PyExc_NotADirectoryError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_PermissionError")] - pub static mut PyExc_PermissionError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ProcessLookupError")] - pub static mut PyExc_ProcessLookupError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_TimeoutError")] - pub static mut PyExc_TimeoutError: *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_EnvironmentError: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_IOError: *mut PyObject; - #[cfg(windows)] - #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")] - pub static mut PyExc_WindowsError: *mut PyObject; - - pub static mut PyExc_RecursionErrorInst: *mut PyObject; - - /* Predefined warning categories */ - #[cfg_attr(PyPy, link_name = "PyPyExc_Warning")] - pub static mut PyExc_Warning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UserWarning")] - pub static mut PyExc_UserWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_DeprecationWarning")] - pub static mut PyExc_DeprecationWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_PendingDeprecationWarning")] - pub static mut PyExc_PendingDeprecationWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxWarning")] - pub static mut PyExc_SyntaxWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeWarning")] - pub static mut PyExc_RuntimeWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_FutureWarning")] - pub static mut PyExc_FutureWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ImportWarning")] - pub static mut PyExc_ImportWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeWarning")] - pub static mut PyExc_UnicodeWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_BytesWarning")] - pub static mut PyExc_BytesWarning: *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyExc_ResourceWarning")] - pub static mut PyExc_ResourceWarning: *mut PyObject; - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyExc_EncodingWarning")] - pub static mut PyExc_EncodingWarning: *mut PyObject; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_BadArgument")] - pub fn PyErr_BadArgument() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_NoMemory")] - pub fn PyErr_NoMemory() -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrno")] - pub fn PyErr_SetFromErrno(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrnoWithFilenameObject")] - pub fn PyErr_SetFromErrnoWithFilenameObject( - arg1: *mut PyObject, - arg2: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetFromErrnoWithFilenameObjects( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetFromErrnoWithFilename( - exc: *mut PyObject, - filename: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_Format")] - pub fn PyErr_Format(exception: *mut PyObject, format: *const c_char, ...) -> *mut PyObject; - pub fn PyErr_SetImportErrorSubclass( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - arg4: *mut PyObject, - ) -> *mut PyObject; - pub fn PyErr_SetImportError( - arg1: *mut PyObject, - arg2: *mut PyObject, - arg3: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_BadInternalCall")] - pub fn PyErr_BadInternalCall(); - pub fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_NewException")] - pub fn PyErr_NewException( - name: *const c_char, - base: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_NewExceptionWithDoc")] - pub fn PyErr_NewExceptionWithDoc( - name: *const c_char, - doc: *const c_char, - base: *mut PyObject, - dict: *mut PyObject, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyErr_WriteUnraisable")] - pub fn PyErr_WriteUnraisable(arg1: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyErr_CheckSignals")] - pub fn PyErr_CheckSignals() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterrupt")] - pub fn PyErr_SetInterrupt(); - #[cfg(Py_3_10)] - #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterruptEx")] - pub fn PyErr_SetInterruptEx(signum: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocation")] - pub fn PyErr_SyntaxLocation(filename: *const c_char, lineno: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocationEx")] - pub fn PyErr_SyntaxLocationEx(filename: *const c_char, lineno: c_int, col_offset: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_ProgramText")] - pub fn PyErr_ProgramText(filename: *const c_char, lineno: c_int) -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyUnicodeDecodeError_Create( - encoding: *const c_char, - object: *const c_char, - length: Py_ssize_t, - start: Py_ssize_t, - end: Py_ssize_t, - reason: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeTranslateError_GetObject(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeDecodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeTranslateError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int; - pub fn PyUnicodeEncodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeDecodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeTranslateError_GetReason(arg1: *mut PyObject) -> *mut PyObject; - pub fn PyUnicodeEncodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; - pub fn PyUnicodeDecodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; - pub fn PyUnicodeTranslateError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pyframe.rs b/vendor/pyo3/pyo3-ffi/src/pyframe.rs deleted file mode 100644 index 4dd3d2b31a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pyframe.rs +++ /dev/null @@ -1,16 +0,0 @@ -#[cfg(not(GraalPy))] -#[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] -use crate::PyCodeObject; -#[cfg(not(Py_LIMITED_API))] -use crate::PyFrameObject; -use std::os::raw::c_int; - -#[cfg(Py_LIMITED_API)] -opaque_struct!(PyFrameObject); - -extern "C" { - pub fn PyFrame_GetLineNumber(f: *mut PyFrameObject) -> c_int; - #[cfg(not(GraalPy))] - #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))] - pub fn PyFrame_GetCode(f: *mut PyFrameObject) -> *mut PyCodeObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pyhash.rs b/vendor/pyo3/pyo3-ffi/src/pyhash.rs deleted file mode 100644 index 4f14e04a69..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pyhash.rs +++ /dev/null @@ -1,54 +0,0 @@ -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -use crate::pyport::{Py_hash_t, Py_ssize_t}; -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use std::os::raw::c_char; -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -use std::os::raw::c_void; - -use std::os::raw::{c_int, c_ulong}; - -extern "C" { - // skipped non-limited _Py_HashDouble - // skipped non-limited _Py_HashPointer - // skipped non-limited _Py_HashPointerRaw - - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - pub fn _Py_HashBytes(src: *const c_void, len: Py_ssize_t) -> Py_hash_t; -} - -pub const _PyHASH_MULTIPLIER: c_ulong = 1000003; - -// skipped _PyHASH_BITS - -// skipped non-limited _Py_HashSecret_t - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyHash_FuncDef { - pub hash: Option Py_hash_t>, - pub name: *const c_char, - pub hash_bits: c_int, - pub seed_bits: c_int, -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -impl Default for PyHash_FuncDef { - #[inline] - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} - -extern "C" { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - pub fn PyHash_GetFuncDef() -> *mut PyHash_FuncDef; -} - -// skipped Py_HASH_CUTOFF - -pub const Py_HASH_EXTERNAL: c_int = 0; -pub const Py_HASH_SIPHASH24: c_int = 1; -pub const Py_HASH_FNV: c_int = 2; - -// skipped Py_HASH_ALGORITHM diff --git a/vendor/pyo3/pyo3-ffi/src/pylifecycle.rs b/vendor/pyo3/pyo3-ffi/src/pylifecycle.rs deleted file mode 100644 index 3f051c54f7..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pylifecycle.rs +++ /dev/null @@ -1,92 +0,0 @@ -use crate::pystate::PyThreadState; - -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - pub fn Py_Initialize(); - pub fn Py_InitializeEx(arg1: c_int); - pub fn Py_Finalize(); - pub fn Py_FinalizeEx() -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPy_IsInitialized")] - pub fn Py_IsInitialized() -> c_int; - - pub fn Py_NewInterpreter() -> *mut PyThreadState; - pub fn Py_EndInterpreter(arg1: *mut PyThreadState); - - #[cfg_attr(PyPy, link_name = "PyPy_AtExit")] - pub fn Py_AtExit(func: Option) -> c_int; - - pub fn Py_Exit(arg1: c_int); - - pub fn Py_Main(argc: c_int, argv: *mut *mut wchar_t) -> c_int; - pub fn Py_BytesMain(argc: c_int, argv: *mut *mut c_char) -> c_int; - - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `PyConfig.program_name` instead.") - )] - pub fn Py_SetProgramName(arg1: *const wchar_t); - #[cfg_attr(PyPy, link_name = "PyPy_GetProgramName")] - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.executable` instead.") - )] - pub fn Py_GetProgramName() -> *mut wchar_t; - - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `PyConfig.home` instead.") - )] - pub fn Py_SetPythonHome(arg1: *const wchar_t); - #[cfg_attr( - Py_3_13, - deprecated( - note = "Deprecated since Python 3.13. Use `PyConfig.home` or the value of the `PYTHONHOME` environment variable instead." - ) - )] - pub fn Py_GetPythonHome() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.executable` instead.") - )] - pub fn Py_GetProgramFullPath() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.prefix` instead.") - )] - pub fn Py_GetPrefix() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.exec_prefix` instead.") - )] - pub fn Py_GetExecPrefix() -> *mut wchar_t; - #[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `sys.path` instead.") - )] - pub fn Py_GetPath() -> *mut wchar_t; - #[cfg(not(Py_3_13))] - #[cfg_attr( - Py_3_11, - deprecated(note = "Deprecated since Python 3.11. Use `sys.path` instead.") - )] - pub fn Py_SetPath(arg1: *const wchar_t); - - // skipped _Py_CheckPython3 - - #[cfg_attr(PyPy, link_name = "PyPy_GetVersion")] - pub fn Py_GetVersion() -> *const c_char; - pub fn Py_GetPlatform() -> *const c_char; - pub fn Py_GetCopyright() -> *const c_char; - pub fn Py_GetCompiler() -> *const c_char; - pub fn Py_GetBuildInfo() -> *const c_char; -} - -type PyOS_sighandler_t = unsafe extern "C" fn(arg1: c_int); - -extern "C" { - pub fn PyOS_getsig(arg1: c_int) -> PyOS_sighandler_t; - pub fn PyOS_setsig(arg1: c_int, arg2: PyOS_sighandler_t) -> PyOS_sighandler_t; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pymem.rs b/vendor/pyo3/pyo3-ffi/src/pymem.rs deleted file mode 100644 index ab0c3d74af..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pymem.rs +++ /dev/null @@ -1,13 +0,0 @@ -use libc::size_t; -use std::os::raw::c_void; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyMem_Malloc")] - pub fn PyMem_Malloc(size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Calloc")] - pub fn PyMem_Calloc(nelem: size_t, elsize: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Realloc")] - pub fn PyMem_Realloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void; - #[cfg_attr(PyPy, link_name = "PyPyMem_Free")] - pub fn PyMem_Free(ptr: *mut c_void); -} diff --git a/vendor/pyo3/pyo3-ffi/src/pyport.rs b/vendor/pyo3/pyo3-ffi/src/pyport.rs deleted file mode 100644 index a144c67fb1..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pyport.rs +++ /dev/null @@ -1,25 +0,0 @@ -pub type PY_UINT32_T = u32; -pub type PY_UINT64_T = u64; - -pub type PY_INT32_T = i32; -pub type PY_INT64_T = i64; - -pub type Py_uintptr_t = ::libc::uintptr_t; -pub type Py_intptr_t = ::libc::intptr_t; -pub type Py_ssize_t = ::libc::ssize_t; - -pub type Py_hash_t = Py_ssize_t; -pub type Py_uhash_t = ::libc::size_t; - -pub const PY_SSIZE_T_MIN: Py_ssize_t = isize::MIN as Py_ssize_t; -pub const PY_SSIZE_T_MAX: Py_ssize_t = isize::MAX as Py_ssize_t; - -#[cfg(target_endian = "big")] -pub const PY_BIG_ENDIAN: usize = 1; -#[cfg(target_endian = "big")] -pub const PY_LITTLE_ENDIAN: usize = 0; - -#[cfg(target_endian = "little")] -pub const PY_BIG_ENDIAN: usize = 0; -#[cfg(target_endian = "little")] -pub const PY_LITTLE_ENDIAN: usize = 1; diff --git a/vendor/pyo3/pyo3-ffi/src/pystate.rs b/vendor/pyo3/pyo3-ffi/src/pystate.rs deleted file mode 100644 index 23aeea3a1d..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pystate.rs +++ /dev/null @@ -1,83 +0,0 @@ -use crate::moduleobject::PyModuleDef; -use crate::object::PyObject; -use std::os::raw::c_int; - -#[cfg(not(PyPy))] -use std::os::raw::c_long; - -pub const MAX_CO_EXTRA_USERS: c_int = 255; - -opaque_struct!(PyThreadState); -opaque_struct!(PyInterpreterState); - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyInterpreterState_New() -> *mut PyInterpreterState; - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState); - #[cfg(not(PyPy))] - pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState); - - #[cfg(all(Py_3_9, not(PyPy)))] - pub fn PyInterpreterState_Get() -> *mut PyInterpreterState; - - #[cfg(all(Py_3_8, not(PyPy)))] - pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject; - - #[cfg(not(PyPy))] - pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64; - - #[cfg_attr(PyPy, link_name = "PyPyState_AddModule")] - pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int; - - #[cfg_attr(PyPy, link_name = "PyPyState_RemoveModule")] - pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int; - - // only has PyPy prefix since 3.10 - #[cfg_attr(all(PyPy, Py_3_10), link_name = "PyPyState_FindModule")] - pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_New")] - pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")] - pub fn PyThreadState_Clear(arg1: *mut PyThreadState); - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")] - pub fn PyThreadState_Delete(arg1: *mut PyThreadState); - - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")] - pub fn PyThreadState_Get() -> *mut PyThreadState; -} - -#[inline] -pub unsafe fn PyThreadState_GET() -> *mut PyThreadState { - PyThreadState_Get() -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")] - pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState; - #[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")] - pub fn PyThreadState_GetDict() -> *mut PyObject; - #[cfg(not(PyPy))] - pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int; -} - -// skipped non-limited / 3.9 PyThreadState_GetInterpreter -// skipped non-limited / 3.9 PyThreadState_GetFrame -// skipped non-limited / 3.9 PyThreadState_GetID - -#[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum PyGILState_STATE { - PyGILState_LOCKED, - PyGILState_UNLOCKED, -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyGILState_Ensure")] - pub fn PyGILState_Ensure() -> PyGILState_STATE; - #[cfg_attr(PyPy, link_name = "PyPyGILState_Release")] - pub fn PyGILState_Release(arg1: PyGILState_STATE); - #[cfg(not(PyPy))] - pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState; -} diff --git a/vendor/pyo3/pyo3-ffi/src/pystrtod.rs b/vendor/pyo3/pyo3-ffi/src/pystrtod.rs deleted file mode 100644 index 1f027686bd..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pystrtod.rs +++ /dev/null @@ -1,32 +0,0 @@ -use crate::object::PyObject; -use std::os::raw::{c_char, c_double, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyOS_string_to_double")] - pub fn PyOS_string_to_double( - str: *const c_char, - endptr: *mut *mut c_char, - overflow_exception: *mut PyObject, - ) -> c_double; - #[cfg_attr(PyPy, link_name = "PyPyOS_double_to_string")] - pub fn PyOS_double_to_string( - val: c_double, - format_code: c_char, - precision: c_int, - flags: c_int, - _type: *mut c_int, - ) -> *mut c_char; -} - -// skipped non-limited _Py_string_to_number_with_underscores -// skipped non-limited _Py_parse_inf_or_nan - -/* PyOS_double_to_string's "flags" parameter can be set to 0 or more of: */ -pub const Py_DTSF_SIGN: c_int = 0x01; /* always add the sign */ -pub const Py_DTSF_ADD_DOT_0: c_int = 0x02; /* if the result is an integer add ".0" */ -pub const Py_DTSF_ALT: c_int = 0x04; /* "alternate" formatting. it's format_code specific */ - -/* PyOS_double_to_string's "type", if non-NULL, will be set to one of: */ -pub const Py_DTST_FINITE: c_int = 0; -pub const Py_DTST_INFINITE: c_int = 1; -pub const Py_DTST_NAN: c_int = 2; diff --git a/vendor/pyo3/pyo3-ffi/src/pythonrun.rs b/vendor/pyo3/pyo3-ffi/src/pythonrun.rs deleted file mode 100644 index e7ea2d2efd..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/pythonrun.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::object::*; -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -use libc::FILE; -#[cfg(any(Py_LIMITED_API, not(Py_3_10), PyPy, GraalPy))] -use std::os::raw::c_char; -use std::os::raw::c_int; - -extern "C" { - #[cfg(any(all(Py_LIMITED_API, not(PyPy)), GraalPy))] - pub fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPyErr_Print")] - pub fn PyErr_Print(); - #[cfg_attr(PyPy, link_name = "PyPyErr_PrintEx")] - pub fn PyErr_PrintEx(arg1: c_int); - #[cfg_attr(PyPy, link_name = "PyPyErr_Display")] - pub fn PyErr_Display(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject); - - #[cfg(Py_3_12)] - pub fn PyErr_DisplayException(exc: *mut PyObject); -} - -#[inline] -#[cfg(PyPy)] -pub unsafe fn Py_CompileString(string: *const c_char, p: *const c_char, s: c_int) -> *mut PyObject { - // PyPy's implementation of Py_CompileString always forwards to Py_CompileStringFlags; this - // is only available in the non-limited API and has a real definition for all versions in - // the cpython/ subdirectory. - #[cfg(Py_LIMITED_API)] - extern "C" { - #[link_name = "PyPy_CompileStringFlags"] - pub fn Py_CompileStringFlags( - string: *const c_char, - p: *const c_char, - s: c_int, - f: *mut std::os::raw::c_void, // Actually *mut Py_CompilerFlags in the real definition - ) -> *mut PyObject; - } - #[cfg(not(Py_LIMITED_API))] - use crate::Py_CompileStringFlags; - - Py_CompileStringFlags(string, p, s, std::ptr::null_mut()) -} - -// skipped PyOS_InputHook - -pub const PYOS_STACK_MARGIN: c_int = 2048; - -// skipped PyOS_CheckStack under Microsoft C - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -opaque_struct!(_mod); - -#[cfg(not(any(PyPy, Py_3_10)))] -opaque_struct!(symtable); -#[cfg(not(any(PyPy, Py_3_10)))] -opaque_struct!(_node); - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyParser_SimpleParseString(s: *const c_char, b: c_int) -> *mut _node { - #[allow(deprecated)] - crate::PyParser_SimpleParseStringFlags(s, b, 0) -} - -#[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] -#[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))] -#[inline] -pub unsafe fn PyParser_SimpleParseFile(fp: *mut FILE, s: *const c_char, b: c_int) -> *mut _node { - #[allow(deprecated)] - crate::PyParser_SimpleParseFileFlags(fp, s, b, 0) -} - -extern "C" { - #[cfg(not(any(PyPy, Py_3_10)))] - pub fn Py_SymtableString( - str: *const c_char, - filename: *const c_char, - start: c_int, - ) -> *mut symtable; - #[cfg(not(any(PyPy, Py_LIMITED_API, Py_3_10)))] - pub fn Py_SymtableStringObject( - str: *const c_char, - filename: *mut PyObject, - start: c_int, - ) -> *mut symtable; -} diff --git a/vendor/pyo3/pyo3-ffi/src/rangeobject.rs b/vendor/pyo3/pyo3-ffi/src/rangeobject.rs deleted file mode 100644 index 408b5cdc5a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/rangeobject.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyRange_Type")] - pub static mut PyRange_Type: PyTypeObject; - pub static mut PyRangeIter_Type: PyTypeObject; - pub static mut PyLongRangeIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyRange_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyRange_Type)) as c_int -} diff --git a/vendor/pyo3/pyo3-ffi/src/setobject.rs b/vendor/pyo3/pyo3-ffi/src/setobject.rs deleted file mode 100644 index 9d5351fc79..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/setobject.rs +++ /dev/null @@ -1,150 +0,0 @@ -use crate::object::*; -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use crate::pyport::Py_hash_t; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -pub const PySet_MINSIZE: usize = 8; - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct setentry { - pub key: *mut PyObject, - pub hash: Py_hash_t, -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Debug)] -pub struct PySetObject { - pub ob_base: PyObject, - pub fill: Py_ssize_t, - pub used: Py_ssize_t, - pub mask: Py_ssize_t, - pub table: *mut setentry, - pub hash: Py_hash_t, - pub finger: Py_ssize_t, - pub smalltable: [setentry; PySet_MINSIZE], - pub weakreflist: *mut PyObject, -} - -// skipped -#[inline] -#[cfg(all(not(any(PyPy, GraalPy)), not(Py_LIMITED_API)))] -pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t { - debug_assert_eq!(PyAnySet_Check(so), 1); - let so = so.cast::(); - (*so).used -} - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PySet_Dummy: *mut PyObject; -} - -extern "C" { - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "_PyPySet_NextEntry")] - pub fn _PySet_NextEntry( - set: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - hash: *mut super::Py_hash_t, - ) -> c_int; - - // skipped non-limited _PySet_Update -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySet_Type")] - pub static mut PySet_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_Type")] - pub static mut PyFrozenSet_Type: PyTypeObject; - pub static mut PySetIter_Type: PyTypeObject; -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySet_New")] - pub fn PySet_New(arg1: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyFrozenSet_New")] - pub fn PyFrozenSet_New(arg1: *mut PyObject) -> *mut PyObject; - - #[cfg_attr(PyPy, link_name = "PyPySet_Add")] - pub fn PySet_Add(set: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Clear")] - pub fn PySet_Clear(set: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Contains")] - pub fn PySet_Contains(anyset: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Discard")] - pub fn PySet_Discard(set: *mut PyObject, key: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPySet_Pop")] - pub fn PySet_Pop(set: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySet_Size")] - pub fn PySet_Size(anyset: *mut PyObject) -> Py_ssize_t; - - #[cfg(PyPy)] - #[link_name = "PyPyFrozenSet_CheckExact"] - pub fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) as c_int -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyFrozenSet_Check"] - pub fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type) - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPyAnySet_CheckExact"] - pub fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PySet_Type) || Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) - as c_int -} - -#[inline] -pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int { - (PyAnySet_CheckExact(ob) != 0 - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PySet_Type)) != 0 - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int -} - -#[inline] -#[cfg(Py_3_10)] -pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int { - crate::Py_IS_TYPE(op, addr_of_mut!(PySet_Type)) -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "PyPySet_Check"] - pub fn PySet_Check(ob: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int { - (Py_TYPE(ob) == addr_of_mut!(PySet_Type) - || PyType_IsSubtype(Py_TYPE(ob), addr_of_mut!(PySet_Type)) != 0) as c_int -} diff --git a/vendor/pyo3/pyo3-ffi/src/sliceobject.rs b/vendor/pyo3/pyo3-ffi/src/sliceobject.rs deleted file mode 100644 index a3ea153987..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/sliceobject.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg(not(GraalPy))] - #[cfg_attr(PyPy, link_name = "_PyPy_EllipsisObject")] - static mut _Py_EllipsisObject: PyObject; - - #[cfg(GraalPy)] - static mut _Py_EllipsisObjectReference: *mut PyObject; -} - -#[inline] -pub unsafe fn Py_Ellipsis() -> *mut PyObject { - #[cfg(not(GraalPy))] - return addr_of_mut!(_Py_EllipsisObject); - #[cfg(GraalPy)] - return _Py_EllipsisObjectReference; -} - -#[cfg(not(Py_LIMITED_API))] -#[repr(C)] -pub struct PySliceObject { - pub ob_base: PyObject, - #[cfg(not(GraalPy))] - pub start: *mut PyObject, - #[cfg(not(GraalPy))] - pub stop: *mut PyObject, - #[cfg(not(GraalPy))] - pub step: *mut PyObject, -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_Type")] - pub static mut PySlice_Type: PyTypeObject; - pub static mut PyEllipsis_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PySlice_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PySlice_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_New")] - pub fn PySlice_New( - start: *mut PyObject, - stop: *mut PyObject, - step: *mut PyObject, - ) -> *mut PyObject; - - // skipped non-limited _PySlice_FromIndices - // skipped non-limited _PySlice_GetLongIndices - - #[cfg_attr(PyPy, link_name = "PyPySlice_GetIndices")] - pub fn PySlice_GetIndices( - r: *mut PyObject, - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - ) -> c_int; -} - -#[inline] -pub unsafe fn PySlice_GetIndicesEx( - slice: *mut PyObject, - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - slicelength: *mut Py_ssize_t, -) -> c_int { - if PySlice_Unpack(slice, start, stop, step) < 0 { - *slicelength = 0; - -1 - } else { - *slicelength = PySlice_AdjustIndices(length, start, stop, *step); - 0 - } -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySlice_Unpack")] - pub fn PySlice_Unpack( - slice: *mut PyObject, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: *mut Py_ssize_t, - ) -> c_int; - - #[cfg_attr(all(PyPy, Py_3_10), link_name = "PyPySlice_AdjustIndices")] - pub fn PySlice_AdjustIndices( - length: Py_ssize_t, - start: *mut Py_ssize_t, - stop: *mut Py_ssize_t, - step: Py_ssize_t, - ) -> Py_ssize_t; -} diff --git a/vendor/pyo3/pyo3-ffi/src/structmember.rs b/vendor/pyo3/pyo3-ffi/src/structmember.rs deleted file mode 100644 index afae165c5b..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/structmember.rs +++ /dev/null @@ -1,33 +0,0 @@ -use std::os::raw::c_int; - -pub use crate::PyMemberDef; - -pub use crate::Py_T_BOOL as T_BOOL; -pub use crate::Py_T_BYTE as T_BYTE; -pub use crate::Py_T_CHAR as T_CHAR; -pub use crate::Py_T_DOUBLE as T_DOUBLE; -pub use crate::Py_T_FLOAT as T_FLOAT; -pub use crate::Py_T_INT as T_INT; -pub use crate::Py_T_LONG as T_LONG; -pub use crate::Py_T_LONGLONG as T_LONGLONG; -pub use crate::Py_T_OBJECT_EX as T_OBJECT_EX; -pub use crate::Py_T_SHORT as T_SHORT; -pub use crate::Py_T_STRING as T_STRING; -pub use crate::Py_T_STRING_INPLACE as T_STRING_INPLACE; -pub use crate::Py_T_UBYTE as T_UBYTE; -pub use crate::Py_T_UINT as T_UINT; -pub use crate::Py_T_ULONG as T_ULONG; -pub use crate::Py_T_ULONGLONG as T_ULONGLONG; -pub use crate::Py_T_USHORT as T_USHORT; -#[allow(deprecated)] -pub use crate::_Py_T_OBJECT as T_OBJECT; - -pub use crate::Py_T_PYSSIZET as T_PYSSIZET; -#[allow(deprecated)] -pub use crate::_Py_T_NONE as T_NONE; - -/* Flags */ -pub use crate::Py_READONLY as READONLY; -pub const READ_RESTRICTED: c_int = 2; -pub const PY_WRITE_RESTRICTED: c_int = 4; -pub const RESTRICTED: c_int = READ_RESTRICTED | PY_WRITE_RESTRICTED; diff --git a/vendor/pyo3/pyo3-ffi/src/structseq.rs b/vendor/pyo3/pyo3-ffi/src/structseq.rs deleted file mode 100644 index f8566787b5..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/structseq.rs +++ /dev/null @@ -1,63 +0,0 @@ -use crate::object::{PyObject, PyTypeObject}; -#[cfg(not(PyPy))] -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStructSequence_Field { - pub name: *const c_char, - pub doc: *const c_char, -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyStructSequence_Desc { - pub name: *const c_char, - pub doc: *const c_char, - pub fields: *mut PyStructSequence_Field, - pub n_in_sequence: c_int, -} - -// skipped PyStructSequence_UnnamedField; - -extern "C" { - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_InitType")] - pub fn PyStructSequence_InitType(_type: *mut PyTypeObject, desc: *mut PyStructSequence_Desc); - - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_InitType2")] - pub fn PyStructSequence_InitType2( - _type: *mut PyTypeObject, - desc: *mut PyStructSequence_Desc, - ) -> c_int; - - #[cfg(not(PyPy))] - pub fn PyStructSequence_NewType(desc: *mut PyStructSequence_Desc) -> *mut PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyStructSequence_New")] - pub fn PyStructSequence_New(_type: *mut PyTypeObject) -> *mut PyObject; -} - -#[cfg(not(Py_LIMITED_API))] -pub type PyStructSequence = crate::PyTupleObject; - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[inline] -pub unsafe fn PyStructSequence_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) { - crate::PyTuple_SET_ITEM(op, i, v) -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[inline] -pub unsafe fn PyStructSequence_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject { - crate::PyTuple_GET_ITEM(op, i) -} - -extern "C" { - #[cfg(not(PyPy))] - pub fn PyStructSequence_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); - - #[cfg(not(PyPy))] - pub fn PyStructSequence_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/sysmodule.rs b/vendor/pyo3/pyo3-ffi/src/sysmodule.rs deleted file mode 100644 index 6f402197ec..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/sysmodule.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::object::PyObject; -use libc::wchar_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPySys_GetObject")] - pub fn PySys_GetObject(arg1: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPySys_SetObject")] - pub fn PySys_SetObject(arg1: *const c_char, arg2: *mut PyObject) -> c_int; - - #[cfg_attr( - Py_3_11, - deprecated( - note = "Deprecated in Python 3.11, use `PyConfig.argv` and `PyConfig.parse_argv` instead" - ) - )] - pub fn PySys_SetArgv(arg1: c_int, arg2: *mut *mut wchar_t); - #[cfg_attr( - Py_3_11, - deprecated( - note = "Deprecated in Python 3.11, use `PyConfig.argv` and `PyConfig.parse_argv` instead" - ) - )] - pub fn PySys_SetArgvEx(arg1: c_int, arg2: *mut *mut wchar_t, arg3: c_int); - pub fn PySys_SetPath(arg1: *const wchar_t); - - #[cfg_attr(PyPy, link_name = "PyPySys_WriteStdout")] - pub fn PySys_WriteStdout(format: *const c_char, ...); - #[cfg_attr(PyPy, link_name = "PyPySys_WriteStderr")] - pub fn PySys_WriteStderr(format: *const c_char, ...); - pub fn PySys_FormatStdout(format: *const c_char, ...); - pub fn PySys_FormatStderr(format: *const c_char, ...); - - #[cfg_attr( - Py_3_13, - deprecated( - note = "Deprecated since Python 3.13. Clear sys.warnoptions and warnings.filters instead." - ) - )] - pub fn PySys_ResetWarnOptions(); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_AddWarnOption(arg1: *const wchar_t); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_AddWarnOptionUnicode(arg1: *mut PyObject); - #[cfg_attr(Py_3_11, deprecated(note = "Python 3.11"))] - pub fn PySys_HasWarnOptions() -> c_int; - - pub fn PySys_AddXOption(arg1: *const wchar_t); - pub fn PySys_GetXOptions() -> *mut PyObject; -} diff --git a/vendor/pyo3/pyo3-ffi/src/traceback.rs b/vendor/pyo3/pyo3-ffi/src/traceback.rs deleted file mode 100644 index 432b6980ef..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/traceback.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Here")] - pub fn PyTraceBack_Here(arg1: *mut crate::PyFrameObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Print")] - pub fn PyTraceBack_Print(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int; -} - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTraceBack_Type")] - pub static mut PyTraceBack_Type: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyTraceBack_Check"] - pub fn PyTraceBack_Check(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyTraceBack_Check(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyTraceBack_Type)) as c_int -} diff --git a/vendor/pyo3/pyo3-ffi/src/tupleobject.rs b/vendor/pyo3/pyo3-ffi/src/tupleobject.rs deleted file mode 100644 index d265c91a4b..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/tupleobject.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; -use std::ptr::addr_of_mut; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTuple_Type")] - pub static mut PyTuple_Type: PyTypeObject; - pub static mut PyTupleIter_Type: PyTypeObject; -} - -#[inline] -pub unsafe fn PyTuple_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TUPLE_SUBCLASS) -} - -#[inline] -pub unsafe fn PyTuple_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyTuple_Type)) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyTuple_New")] - pub fn PyTuple_New(size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_Size")] - pub fn PyTuple_Size(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyTuple_GetItem")] - pub fn PyTuple_GetItem(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_SetItem")] - pub fn PyTuple_SetItem(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyTuple_GetSlice")] - pub fn PyTuple_GetSlice( - arg1: *mut PyObject, - arg2: Py_ssize_t, - arg3: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyTuple_Pack")] - pub fn PyTuple_Pack(arg1: Py_ssize_t, ...) -> *mut PyObject; - #[cfg(not(Py_3_9))] - pub fn PyTuple_ClearFreeList() -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/typeslots.rs b/vendor/pyo3/pyo3-ffi/src/typeslots.rs deleted file mode 100644 index da7c60b310..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/typeslots.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::os::raw::c_int; - -pub const Py_bf_getbuffer: c_int = 1; -pub const Py_bf_releasebuffer: c_int = 2; -pub const Py_mp_ass_subscript: c_int = 3; -pub const Py_mp_length: c_int = 4; -pub const Py_mp_subscript: c_int = 5; -pub const Py_nb_absolute: c_int = 6; -pub const Py_nb_add: c_int = 7; -pub const Py_nb_and: c_int = 8; -pub const Py_nb_bool: c_int = 9; -pub const Py_nb_divmod: c_int = 10; -pub const Py_nb_float: c_int = 11; -pub const Py_nb_floor_divide: c_int = 12; -pub const Py_nb_index: c_int = 13; -pub const Py_nb_inplace_add: c_int = 14; -pub const Py_nb_inplace_and: c_int = 15; -pub const Py_nb_inplace_floor_divide: c_int = 16; -pub const Py_nb_inplace_lshift: c_int = 17; -pub const Py_nb_inplace_multiply: c_int = 18; -pub const Py_nb_inplace_or: c_int = 19; -pub const Py_nb_inplace_power: c_int = 20; -pub const Py_nb_inplace_remainder: c_int = 21; -pub const Py_nb_inplace_rshift: c_int = 22; -pub const Py_nb_inplace_subtract: c_int = 23; -pub const Py_nb_inplace_true_divide: c_int = 24; -pub const Py_nb_inplace_xor: c_int = 25; -pub const Py_nb_int: c_int = 26; -pub const Py_nb_invert: c_int = 27; -pub const Py_nb_lshift: c_int = 28; -pub const Py_nb_multiply: c_int = 29; -pub const Py_nb_negative: c_int = 30; -pub const Py_nb_or: c_int = 31; -pub const Py_nb_positive: c_int = 32; -pub const Py_nb_power: c_int = 33; -pub const Py_nb_remainder: c_int = 34; -pub const Py_nb_rshift: c_int = 35; -pub const Py_nb_subtract: c_int = 36; -pub const Py_nb_true_divide: c_int = 37; -pub const Py_nb_xor: c_int = 38; -pub const Py_sq_ass_item: c_int = 39; -pub const Py_sq_concat: c_int = 40; -pub const Py_sq_contains: c_int = 41; -pub const Py_sq_inplace_concat: c_int = 42; -pub const Py_sq_inplace_repeat: c_int = 43; -pub const Py_sq_item: c_int = 44; -pub const Py_sq_length: c_int = 45; -pub const Py_sq_repeat: c_int = 46; -pub const Py_tp_alloc: c_int = 47; -pub const Py_tp_base: c_int = 48; -pub const Py_tp_bases: c_int = 49; -pub const Py_tp_call: c_int = 50; -pub const Py_tp_clear: c_int = 51; -pub const Py_tp_dealloc: c_int = 52; -pub const Py_tp_del: c_int = 53; -pub const Py_tp_descr_get: c_int = 54; -pub const Py_tp_descr_set: c_int = 55; -pub const Py_tp_doc: c_int = 56; -pub const Py_tp_getattr: c_int = 57; -pub const Py_tp_getattro: c_int = 58; -pub const Py_tp_hash: c_int = 59; -pub const Py_tp_init: c_int = 60; -pub const Py_tp_is_gc: c_int = 61; -pub const Py_tp_iter: c_int = 62; -pub const Py_tp_iternext: c_int = 63; -pub const Py_tp_methods: c_int = 64; -pub const Py_tp_new: c_int = 65; -pub const Py_tp_repr: c_int = 66; -pub const Py_tp_richcompare: c_int = 67; -pub const Py_tp_setattr: c_int = 68; -pub const Py_tp_setattro: c_int = 69; -pub const Py_tp_str: c_int = 70; -pub const Py_tp_traverse: c_int = 71; -pub const Py_tp_members: c_int = 72; -pub const Py_tp_getset: c_int = 73; -pub const Py_tp_free: c_int = 74; -pub const Py_nb_matrix_multiply: c_int = 75; -pub const Py_nb_inplace_matrix_multiply: c_int = 76; -pub const Py_am_await: c_int = 77; -pub const Py_am_aiter: c_int = 78; -pub const Py_am_anext: c_int = 79; -pub const Py_tp_finalize: c_int = 80; diff --git a/vendor/pyo3/pyo3-ffi/src/unicodeobject.rs b/vendor/pyo3/pyo3-ffi/src/unicodeobject.rs deleted file mode 100644 index 1e0425ce2a..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/unicodeobject.rs +++ /dev/null @@ -1,353 +0,0 @@ -use crate::object::*; -use crate::pyport::Py_ssize_t; -use libc::wchar_t; -use std::os::raw::{c_char, c_int, c_void}; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr( - Py_3_13, - deprecated(note = "Deprecated since Python 3.13. Use `libc::wchar_t` instead.") -)] -pub type Py_UNICODE = wchar_t; - -pub type Py_UCS4 = u32; -pub type Py_UCS2 = u16; -pub type Py_UCS1 = u8; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Type")] - pub static mut PyUnicode_Type: PyTypeObject; - pub static mut PyUnicodeIter_Type: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyUnicode_Check"] - pub fn PyUnicode_Check(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyUnicode_CheckExact"] - pub fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyUnicode_Check(op: *mut PyObject) -> c_int { - PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(PyUnicode_Type)) as c_int -} - -pub const Py_UNICODE_REPLACEMENT_CHARACTER: Py_UCS4 = 0xFFFD; - -extern "C" { - - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromStringAndSize")] - pub fn PyUnicode_FromStringAndSize(u: *const c_char, size: Py_ssize_t) -> *mut PyObject; - pub fn PyUnicode_FromString(u: *const c_char) -> *mut PyObject; - - pub fn PyUnicode_Substring( - str: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - ) -> *mut PyObject; - pub fn PyUnicode_AsUCS4( - unicode: *mut PyObject, - buffer: *mut Py_UCS4, - buflen: Py_ssize_t, - copy_null: c_int, - ) -> *mut Py_UCS4; - pub fn PyUnicode_AsUCS4Copy(unicode: *mut PyObject) -> *mut Py_UCS4; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetLength")] - pub fn PyUnicode_GetLength(unicode: *mut PyObject) -> Py_ssize_t; - #[cfg(not(Py_3_12))] - #[deprecated(note = "Removed in Python 3.12")] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetSize")] - pub fn PyUnicode_GetSize(unicode: *mut PyObject) -> Py_ssize_t; - pub fn PyUnicode_ReadChar(unicode: *mut PyObject, index: Py_ssize_t) -> Py_UCS4; - pub fn PyUnicode_WriteChar( - unicode: *mut PyObject, - index: Py_ssize_t, - character: Py_UCS4, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Resize")] - pub fn PyUnicode_Resize(unicode: *mut *mut PyObject, length: Py_ssize_t) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromEncodedObject")] - pub fn PyUnicode_FromEncodedObject( - obj: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromObject")] - pub fn PyUnicode_FromObject(obj: *mut PyObject) -> *mut PyObject; - // #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormatV")] - // pub fn PyUnicode_FromFormatV(format: *const c_char, vargs: va_list) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormat")] - pub fn PyUnicode_FromFormat(format: *const c_char, ...) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternInPlace")] - pub fn PyUnicode_InternInPlace(arg1: *mut *mut PyObject); - #[cfg(not(Py_3_12))] - #[cfg_attr(Py_3_10, deprecated(note = "Python 3.10"))] - pub fn PyUnicode_InternImmortal(arg1: *mut *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternFromString")] - pub fn PyUnicode_InternFromString(u: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromWideChar")] - pub fn PyUnicode_FromWideChar(w: *const wchar_t, size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideChar")] - pub fn PyUnicode_AsWideChar( - unicode: *mut PyObject, - w: *mut wchar_t, - size: Py_ssize_t, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideCharString")] - pub fn PyUnicode_AsWideCharString( - unicode: *mut PyObject, - size: *mut Py_ssize_t, - ) -> *mut wchar_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromOrdinal")] - pub fn PyUnicode_FromOrdinal(ordinal: c_int) -> *mut PyObject; - pub fn PyUnicode_ClearFreeList() -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetDefaultEncoding")] - pub fn PyUnicode_GetDefaultEncoding() -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Decode")] - pub fn PyUnicode_Decode( - s: *const c_char, - size: Py_ssize_t, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsDecodedObject( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsDecodedUnicode( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedObject")] - pub fn PyUnicode_AsEncodedObject( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedString")] - pub fn PyUnicode_AsEncodedString( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsEncodedUnicode( - unicode: *mut PyObject, - encoding: *const c_char, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_BuildEncodingMap(string: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF7( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF7Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF8")] - pub fn PyUnicode_DecodeUTF8( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF8Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8String")] - pub fn PyUnicode_AsUTF8String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8AndSize")] - pub fn PyUnicode_AsUTF8AndSize(unicode: *mut PyObject, size: *mut Py_ssize_t) -> *const c_char; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF32")] - pub fn PyUnicode_DecodeUTF32( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF32Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF32String")] - pub fn PyUnicode_AsUTF32String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF16")] - pub fn PyUnicode_DecodeUTF16( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeUTF16Stateful( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - byteorder: *mut c_int, - consumed: *mut Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF16String")] - pub fn PyUnicode_AsUTF16String(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeUnicodeEscape( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicodeEscapeString")] - pub fn PyUnicode_AsUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeRawUnicodeEscape( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsRawUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeLatin1")] - pub fn PyUnicode_DecodeLatin1( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsLatin1String")] - pub fn PyUnicode_AsLatin1String(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeASCII")] - pub fn PyUnicode_DecodeASCII( - string: *const c_char, - length: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsASCIIString")] - pub fn PyUnicode_AsASCIIString(unicode: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_DecodeCharmap( - string: *const c_char, - length: Py_ssize_t, - mapping: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_AsCharmapString( - unicode: *mut PyObject, - mapping: *mut PyObject, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeLocaleAndSize( - str: *const c_char, - len: Py_ssize_t, - errors: *const c_char, - ) -> *mut PyObject; - pub fn PyUnicode_DecodeLocale(str: *const c_char, errors: *const c_char) -> *mut PyObject; - pub fn PyUnicode_EncodeLocale(unicode: *mut PyObject, errors: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSConverter")] - pub fn PyUnicode_FSConverter(arg1: *mut PyObject, arg2: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSDecoder")] - pub fn PyUnicode_FSDecoder(arg1: *mut PyObject, arg2: *mut c_void) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefault")] - pub fn PyUnicode_DecodeFSDefault(s: *const c_char) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefaultAndSize")] - pub fn PyUnicode_DecodeFSDefaultAndSize(s: *const c_char, size: Py_ssize_t) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeFSDefault")] - pub fn PyUnicode_EncodeFSDefault(unicode: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Concat")] - pub fn PyUnicode_Concat(left: *mut PyObject, right: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_Append(pleft: *mut *mut PyObject, right: *mut PyObject); - pub fn PyUnicode_AppendAndDel(pleft: *mut *mut PyObject, right: *mut PyObject); - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Split")] - pub fn PyUnicode_Split( - s: *mut PyObject, - sep: *mut PyObject, - maxsplit: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Splitlines")] - pub fn PyUnicode_Splitlines(s: *mut PyObject, keepends: c_int) -> *mut PyObject; - pub fn PyUnicode_Partition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_RPartition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_RSplit( - s: *mut PyObject, - sep: *mut PyObject, - maxsplit: Py_ssize_t, - ) -> *mut PyObject; - pub fn PyUnicode_Translate( - str: *mut PyObject, - table: *mut PyObject, - errors: *const c_char, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Join")] - pub fn PyUnicode_Join(separator: *mut PyObject, seq: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Tailmatch")] - pub fn PyUnicode_Tailmatch( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Find")] - pub fn PyUnicode_Find( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - pub fn PyUnicode_FindChar( - str: *mut PyObject, - ch: Py_UCS4, - start: Py_ssize_t, - end: Py_ssize_t, - direction: c_int, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Count")] - pub fn PyUnicode_Count( - str: *mut PyObject, - substr: *mut PyObject, - start: Py_ssize_t, - end: Py_ssize_t, - ) -> Py_ssize_t; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Replace")] - pub fn PyUnicode_Replace( - str: *mut PyObject, - substr: *mut PyObject, - replstr: *mut PyObject, - maxcount: Py_ssize_t, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Compare")] - pub fn PyUnicode_Compare(left: *mut PyObject, right: *mut PyObject) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_CompareWithASCIIString")] - pub fn PyUnicode_CompareWithASCIIString(left: *mut PyObject, right: *const c_char) -> c_int; - #[cfg(Py_3_13)] - pub fn PyUnicode_EqualToUTF8(unicode: *mut PyObject, string: *const c_char) -> c_int; - #[cfg(Py_3_13)] - pub fn PyUnicode_EqualToUTF8AndSize( - unicode: *mut PyObject, - string: *const c_char, - size: Py_ssize_t, - ) -> c_int; - - pub fn PyUnicode_RichCompare( - left: *mut PyObject, - right: *mut PyObject, - op: c_int, - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyUnicode_Format")] - pub fn PyUnicode_Format(format: *mut PyObject, args: *mut PyObject) -> *mut PyObject; - pub fn PyUnicode_Contains(container: *mut PyObject, element: *mut PyObject) -> c_int; - pub fn PyUnicode_IsIdentifier(s: *mut PyObject) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/warnings.rs b/vendor/pyo3/pyo3-ffi/src/warnings.rs deleted file mode 100644 index d58f37435d..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/warnings.rs +++ /dev/null @@ -1,34 +0,0 @@ -use crate::object::PyObject; -use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnEx")] - pub fn PyErr_WarnEx( - category: *mut PyObject, - message: *const c_char, - stack_level: Py_ssize_t, - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnFormat")] - pub fn PyErr_WarnFormat( - category: *mut PyObject, - stack_level: Py_ssize_t, - format: *const c_char, - ... - ) -> c_int; - pub fn PyErr_ResourceWarning( - source: *mut PyObject, - stack_level: Py_ssize_t, - format: *const c_char, - ... - ) -> c_int; - #[cfg_attr(PyPy, link_name = "PyPyErr_WarnExplicit")] - pub fn PyErr_WarnExplicit( - category: *mut PyObject, - message: *const c_char, - filename: *const c_char, - lineno: c_int, - module: *const c_char, - registry: *mut PyObject, - ) -> c_int; -} diff --git a/vendor/pyo3/pyo3-ffi/src/weakrefobject.rs b/vendor/pyo3/pyo3-ffi/src/weakrefobject.rs deleted file mode 100644 index 305dc290fa..0000000000 --- a/vendor/pyo3/pyo3-ffi/src/weakrefobject.rs +++ /dev/null @@ -1,69 +0,0 @@ -use crate::object::*; -use std::os::raw::c_int; -#[cfg(not(PyPy))] -use std::ptr::addr_of_mut; - -#[cfg(all(not(PyPy), Py_LIMITED_API, not(GraalPy)))] -opaque_struct!(PyWeakReference); - -#[cfg(all(not(PyPy), not(Py_LIMITED_API), not(GraalPy)))] -pub use crate::_PyWeakReference as PyWeakReference; - -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PyWeakref_RefType: PyTypeObject; - pub static mut _PyWeakref_ProxyType: PyTypeObject; - pub static mut _PyWeakref_CallableProxyType: PyTypeObject; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckRef"] - pub fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckRefExact"] - pub fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int; - - #[cfg(PyPy)] - #[link_name = "PyPyWeakref_CheckProxy"] - pub fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int; -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int { - PyObject_TypeCheck(op, addr_of_mut!(_PyWeakref_RefType)) -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int { - (Py_TYPE(op) == addr_of_mut!(_PyWeakref_RefType)) as c_int -} - -#[inline] -#[cfg(not(PyPy))] -pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int { - ((Py_TYPE(op) == addr_of_mut!(_PyWeakref_ProxyType)) - || (Py_TYPE(op) == addr_of_mut!(_PyWeakref_CallableProxyType))) as c_int -} - -#[inline] -pub unsafe fn PyWeakref_Check(op: *mut PyObject) -> c_int { - (PyWeakref_CheckRef(op) != 0 || PyWeakref_CheckProxy(op) != 0) as c_int -} - -extern "C" { - #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewRef")] - pub fn PyWeakref_NewRef(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewProxy")] - pub fn PyWeakref_NewProxy(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetObject")] - #[cfg_attr( - Py_3_13, - deprecated(note = "deprecated since Python 3.13. Use `PyWeakref_GetRef` instead.") - )] - pub fn PyWeakref_GetObject(reference: *mut PyObject) -> *mut PyObject; - #[cfg(Py_3_13)] - #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetRef")] - pub fn PyWeakref_GetRef(reference: *mut PyObject, pobj: *mut *mut PyObject) -> c_int; -} diff --git a/vendor/pyo3/pyo3-macros-backend/Cargo.toml b/vendor/pyo3/pyo3-macros-backend/Cargo.toml deleted file mode 100644 index 704c263cb4..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/Cargo.toml +++ /dev/null @@ -1,35 +0,0 @@ -[package] -name = "pyo3-macros-backend" -version = "0.23.4" -description = "Code generation for PyO3 package" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" -rust-version = "1.63" - -# Note: we use default-features = false for proc-macro related crates -# not to depend on proc-macro itself. -# See https://github.com/PyO3/pyo3/pull/810 for more. -[dependencies] -heck = "0.5" -proc-macro2 = { version = "1.0.60", default-features = false } -pyo3-build-config = { path = "../pyo3-build-config", version = "=0.23.4", features = ["resolve-config"] } -quote = { version = "1", default-features = false } - -[dependencies.syn] -version = "2.0.59" # for `LitCStr` -default-features = false -features = ["derive", "parsing", "printing", "clone-impls", "full", "extra-traits"] - -[build-dependencies] -pyo3-build-config = { path = "../pyo3-build-config", version = "=0.23.4" } - -[lints] -workspace = true - -[features] -experimental-async = [] diff --git a/vendor/pyo3/pyo3-macros-backend/LICENSE-APACHE b/vendor/pyo3/pyo3-macros-backend/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/pyo3/pyo3-macros-backend/LICENSE-MIT b/vendor/pyo3/pyo3-macros-backend/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/pyo3/pyo3-macros-backend/build.rs b/vendor/pyo3/pyo3-macros-backend/build.rs deleted file mode 100644 index 55aa0ba03c..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/build.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - pyo3_build_config::print_expected_cfgs(); - pyo3_build_config::print_feature_cfgs(); -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/attributes.rs b/vendor/pyo3/pyo3-macros-backend/src/attributes.rs deleted file mode 100644 index 6fe75e4430..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/attributes.rs +++ /dev/null @@ -1,426 +0,0 @@ -use proc_macro2::TokenStream; -use quote::{quote, ToTokens}; -use syn::parse::Parser; -use syn::{ - ext::IdentExt, - parse::{Parse, ParseStream}, - punctuated::Punctuated, - spanned::Spanned, - token::Comma, - Attribute, Expr, ExprPath, Ident, Index, LitBool, LitStr, Member, Path, Result, Token, -}; - -pub mod kw { - syn::custom_keyword!(annotation); - syn::custom_keyword!(attribute); - syn::custom_keyword!(cancel_handle); - syn::custom_keyword!(constructor); - syn::custom_keyword!(dict); - syn::custom_keyword!(eq); - syn::custom_keyword!(eq_int); - syn::custom_keyword!(extends); - syn::custom_keyword!(freelist); - syn::custom_keyword!(from_py_with); - syn::custom_keyword!(frozen); - syn::custom_keyword!(get); - syn::custom_keyword!(get_all); - syn::custom_keyword!(hash); - syn::custom_keyword!(item); - syn::custom_keyword!(from_item_all); - syn::custom_keyword!(mapping); - syn::custom_keyword!(module); - syn::custom_keyword!(name); - syn::custom_keyword!(ord); - syn::custom_keyword!(pass_module); - syn::custom_keyword!(rename_all); - syn::custom_keyword!(sequence); - syn::custom_keyword!(set); - syn::custom_keyword!(set_all); - syn::custom_keyword!(signature); - syn::custom_keyword!(str); - syn::custom_keyword!(subclass); - syn::custom_keyword!(submodule); - syn::custom_keyword!(text_signature); - syn::custom_keyword!(transparent); - syn::custom_keyword!(unsendable); - syn::custom_keyword!(weakref); - syn::custom_keyword!(gil_used); -} - -fn take_int(read: &mut &str, tracker: &mut usize) -> String { - let mut int = String::new(); - for (i, ch) in read.char_indices() { - match ch { - '0'..='9' => { - *tracker += 1; - int.push(ch) - } - _ => { - *read = &read[i..]; - break; - } - } - } - int -} - -fn take_ident(read: &mut &str, tracker: &mut usize) -> Ident { - let mut ident = String::new(); - if read.starts_with("r#") { - ident.push_str("r#"); - *tracker += 2; - *read = &read[2..]; - } - for (i, ch) in read.char_indices() { - match ch { - 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => { - *tracker += 1; - ident.push(ch) - } - _ => { - *read = &read[i..]; - break; - } - } - } - Ident::parse_any.parse_str(&ident).unwrap() -} - -// shorthand parsing logic inspiration taken from https://github.com/dtolnay/thiserror/blob/master/impl/src/fmt.rs -fn parse_shorthand_format(fmt: LitStr) -> Result<(LitStr, Vec)> { - let span = fmt.span(); - let token = fmt.token(); - let value = fmt.value(); - let mut read = value.as_str(); - let mut out = String::new(); - let mut members = Vec::new(); - let mut tracker = 1; - while let Some(brace) = read.find('{') { - tracker += brace; - out += &read[..brace + 1]; - read = &read[brace + 1..]; - if read.starts_with('{') { - out.push('{'); - read = &read[1..]; - tracker += 2; - continue; - } - let next = match read.chars().next() { - Some(next) => next, - None => break, - }; - tracker += 1; - let member = match next { - '0'..='9' => { - let start = tracker; - let index = take_int(&mut read, &mut tracker).parse::().unwrap(); - let end = tracker; - let subspan = token.subspan(start..end).unwrap_or(span); - let idx = Index { - index, - span: subspan, - }; - Member::Unnamed(idx) - } - 'a'..='z' | 'A'..='Z' | '_' => { - let start = tracker; - let mut ident = take_ident(&mut read, &mut tracker); - let end = tracker; - let subspan = token.subspan(start..end).unwrap_or(span); - ident.set_span(subspan); - Member::Named(ident) - } - '}' | ':' => { - let start = tracker; - tracker += 1; - let end = tracker; - let subspan = token.subspan(start..end).unwrap_or(span); - // we found a closing bracket or formatting ':' without finding a member, we assume the user wants the instance formatted here - bail_spanned!(subspan.span() => "No member found, you must provide a named or positionally specified member.") - } - _ => continue, - }; - members.push(member); - } - out += read; - Ok((LitStr::new(&out, span), members)) -} - -#[derive(Clone, Debug)] -pub struct StringFormatter { - pub fmt: LitStr, - pub args: Vec, -} - -impl Parse for crate::attributes::StringFormatter { - fn parse(input: ParseStream<'_>) -> Result { - let (fmt, args) = parse_shorthand_format(input.parse()?)?; - Ok(Self { fmt, args }) - } -} - -impl ToTokens for crate::attributes::StringFormatter { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.fmt.to_tokens(tokens); - tokens.extend(quote! {self.args}) - } -} - -#[derive(Clone, Debug)] -pub struct KeywordAttribute { - pub kw: K, - pub value: V, -} - -#[derive(Clone, Debug)] -pub struct OptionalKeywordAttribute { - pub kw: K, - pub value: Option, -} - -/// A helper type which parses the inner type via a literal string -/// e.g. `LitStrValue` -> parses "some::path" in quotes. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct LitStrValue(pub T); - -impl Parse for LitStrValue { - fn parse(input: ParseStream<'_>) -> Result { - let lit_str: LitStr = input.parse()?; - lit_str.parse().map(LitStrValue) - } -} - -impl ToTokens for LitStrValue { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.0.to_tokens(tokens) - } -} - -/// A helper type which parses a name via a literal string -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct NameLitStr(pub Ident); - -impl Parse for NameLitStr { - fn parse(input: ParseStream<'_>) -> Result { - let string_literal: LitStr = input.parse()?; - if let Ok(ident) = string_literal.parse_with(Ident::parse_any) { - Ok(NameLitStr(ident)) - } else { - bail_spanned!(string_literal.span() => "expected a single identifier in double quotes") - } - } -} - -impl ToTokens for NameLitStr { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.0.to_tokens(tokens) - } -} - -/// Available renaming rules -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum RenamingRule { - CamelCase, - KebabCase, - Lowercase, - PascalCase, - ScreamingKebabCase, - ScreamingSnakeCase, - SnakeCase, - Uppercase, -} - -/// A helper type which parses a renaming rule via a literal string -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct RenamingRuleLitStr { - pub lit: LitStr, - pub rule: RenamingRule, -} - -impl Parse for RenamingRuleLitStr { - fn parse(input: ParseStream<'_>) -> Result { - let string_literal: LitStr = input.parse()?; - let rule = match string_literal.value().as_ref() { - "camelCase" => RenamingRule::CamelCase, - "kebab-case" => RenamingRule::KebabCase, - "lowercase" => RenamingRule::Lowercase, - "PascalCase" => RenamingRule::PascalCase, - "SCREAMING-KEBAB-CASE" => RenamingRule::ScreamingKebabCase, - "SCREAMING_SNAKE_CASE" => RenamingRule::ScreamingSnakeCase, - "snake_case" => RenamingRule::SnakeCase, - "UPPERCASE" => RenamingRule::Uppercase, - _ => { - bail_spanned!(string_literal.span() => "expected a valid renaming rule, possible values are: \"camelCase\", \"kebab-case\", \"lowercase\", \"PascalCase\", \"SCREAMING-KEBAB-CASE\", \"SCREAMING_SNAKE_CASE\", \"snake_case\", \"UPPERCASE\"") - } - }; - Ok(Self { - lit: string_literal, - rule, - }) - } -} - -impl ToTokens for RenamingRuleLitStr { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.lit.to_tokens(tokens) - } -} - -/// Text signatue can be either a literal string or opt-in/out -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum TextSignatureAttributeValue { - Str(LitStr), - // `None` ident to disable automatic text signature generation - Disabled(Ident), -} - -impl Parse for TextSignatureAttributeValue { - fn parse(input: ParseStream<'_>) -> Result { - if let Ok(lit_str) = input.parse::() { - return Ok(TextSignatureAttributeValue::Str(lit_str)); - } - - let err_span = match input.parse::() { - Ok(ident) if ident == "None" => { - return Ok(TextSignatureAttributeValue::Disabled(ident)); - } - Ok(other_ident) => other_ident.span(), - Err(e) => e.span(), - }; - - Err(err_spanned!(err_span => "expected a string literal or `None`")) - } -} - -impl ToTokens for TextSignatureAttributeValue { - fn to_tokens(&self, tokens: &mut TokenStream) { - match self { - TextSignatureAttributeValue::Str(s) => s.to_tokens(tokens), - TextSignatureAttributeValue::Disabled(b) => b.to_tokens(tokens), - } - } -} - -pub type ExtendsAttribute = KeywordAttribute; -pub type FreelistAttribute = KeywordAttribute>; -pub type ModuleAttribute = KeywordAttribute; -pub type NameAttribute = KeywordAttribute; -pub type RenameAllAttribute = KeywordAttribute; -pub type StrFormatterAttribute = OptionalKeywordAttribute; -pub type TextSignatureAttribute = KeywordAttribute; -pub type SubmoduleAttribute = kw::submodule; -pub type GILUsedAttribute = KeywordAttribute; - -impl Parse for KeywordAttribute { - fn parse(input: ParseStream<'_>) -> Result { - let kw: K = input.parse()?; - let _: Token![=] = input.parse()?; - let value = input.parse()?; - Ok(KeywordAttribute { kw, value }) - } -} - -impl ToTokens for KeywordAttribute { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.kw.to_tokens(tokens); - Token![=](self.kw.span()).to_tokens(tokens); - self.value.to_tokens(tokens); - } -} - -impl Parse for OptionalKeywordAttribute { - fn parse(input: ParseStream<'_>) -> Result { - let kw: K = input.parse()?; - let value = match input.parse::() { - Ok(_) => Some(input.parse()?), - Err(_) => None, - }; - Ok(OptionalKeywordAttribute { kw, value }) - } -} - -impl ToTokens for OptionalKeywordAttribute { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.kw.to_tokens(tokens); - if self.value.is_some() { - Token![=](self.kw.span()).to_tokens(tokens); - self.value.to_tokens(tokens); - } - } -} - -pub type FromPyWithAttribute = KeywordAttribute>; - -/// For specifying the path to the pyo3 crate. -pub type CrateAttribute = KeywordAttribute>; - -pub fn get_pyo3_options(attr: &syn::Attribute) -> Result>> { - if attr.path().is_ident("pyo3") { - attr.parse_args_with(Punctuated::parse_terminated).map(Some) - } else { - Ok(None) - } -} - -/// Takes attributes from an attribute vector. -/// -/// For each attribute in `attrs`, `extractor` is called. If `extractor` returns `Ok(true)`, then -/// the attribute will be removed from the vector. -/// -/// This is similar to `Vec::retain` except the closure is fallible and the condition is reversed. -/// (In `retain`, returning `true` keeps the element, here it removes it.) -pub fn take_attributes( - attrs: &mut Vec, - mut extractor: impl FnMut(&Attribute) -> Result, -) -> Result<()> { - *attrs = attrs - .drain(..) - .filter_map(|attr| { - extractor(&attr) - .map(move |attribute_handled| if attribute_handled { None } else { Some(attr) }) - .transpose() - }) - .collect::>()?; - Ok(()) -} - -pub fn take_pyo3_options(attrs: &mut Vec) -> Result> { - let mut out = Vec::new(); - let mut all_errors = ErrorCombiner(None); - take_attributes(attrs, |attr| match get_pyo3_options(attr) { - Ok(result) => { - if let Some(options) = result { - out.extend(options); - Ok(true) - } else { - Ok(false) - } - } - Err(err) => { - all_errors.combine(err); - Ok(true) - } - })?; - all_errors.ensure_empty()?; - Ok(out) -} - -pub struct ErrorCombiner(pub Option); - -impl ErrorCombiner { - pub fn combine(&mut self, error: syn::Error) { - if let Some(existing) = &mut self.0 { - existing.combine(error); - } else { - self.0 = Some(error); - } - } - - pub fn ensure_empty(self) -> Result<()> { - if let Some(error) = self.0 { - Err(error) - } else { - Ok(()) - } - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/deprecations.rs b/vendor/pyo3/pyo3-macros-backend/src/deprecations.rs deleted file mode 100644 index df48c9da41..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/deprecations.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::method::{FnArg, FnSpec}; -use proc_macro2::TokenStream; -use quote::quote_spanned; - -pub(crate) fn deprecate_trailing_option_default(spec: &FnSpec<'_>) -> TokenStream { - if spec.signature.attribute.is_none() - && spec.tp.signature_attribute_allowed() - && spec.signature.arguments.iter().any(|arg| { - if let FnArg::Regular(arg) = arg { - arg.option_wrapped_type.is_some() - } else { - false - } - }) - { - use std::fmt::Write; - let mut deprecation_msg = String::from( - "this function has implicit defaults for the trailing `Option` arguments \n\ - = note: these implicit defaults are being phased out \n\ - = help: add `#[pyo3(signature = (", - ); - spec.signature.arguments.iter().for_each(|arg| { - match arg { - FnArg::Regular(arg) => { - if arg.option_wrapped_type.is_some() { - write!(deprecation_msg, "{}=None, ", arg.name) - } else { - write!(deprecation_msg, "{}, ", arg.name) - } - } - FnArg::VarArgs(arg) => write!(deprecation_msg, "{}, ", arg.name), - FnArg::KwArgs(arg) => write!(deprecation_msg, "{}, ", arg.name), - FnArg::Py(_) | FnArg::CancelHandle(_) => Ok(()), - } - .expect("writing to `String` should not fail"); - }); - - //remove trailing space and comma - deprecation_msg.pop(); - deprecation_msg.pop(); - - deprecation_msg.push_str( - "))]` to this function to silence this warning and keep the current behavior", - ); - quote_spanned! { spec.name.span() => - #[deprecated(note = #deprecation_msg)] - #[allow(dead_code)] - const SIGNATURE: () = (); - const _: () = SIGNATURE; - } - } else { - TokenStream::new() - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/frompyobject.rs b/vendor/pyo3/pyo3-macros-backend/src/frompyobject.rs deleted file mode 100644 index 565c54da1f..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/frompyobject.rs +++ /dev/null @@ -1,629 +0,0 @@ -use crate::attributes::{self, get_pyo3_options, CrateAttribute, FromPyWithAttribute}; -use crate::utils::Ctx; -use proc_macro2::TokenStream; -use quote::{format_ident, quote}; -use syn::{ - ext::IdentExt, - parenthesized, - parse::{Parse, ParseStream}, - parse_quote, - punctuated::Punctuated, - spanned::Spanned, - Attribute, DataEnum, DeriveInput, Fields, Ident, LitStr, Result, Token, -}; - -/// Describes derivation input of an enum. -struct Enum<'a> { - enum_ident: &'a Ident, - variants: Vec>, -} - -impl<'a> Enum<'a> { - /// Construct a new enum representation. - /// - /// `data_enum` is the `syn` representation of the input enum, `ident` is the - /// `Identifier` of the enum. - fn new(data_enum: &'a DataEnum, ident: &'a Ident) -> Result { - ensure_spanned!( - !data_enum.variants.is_empty(), - ident.span() => "cannot derive FromPyObject for empty enum" - ); - let variants = data_enum - .variants - .iter() - .map(|variant| { - let attrs = ContainerOptions::from_attrs(&variant.attrs)?; - let var_ident = &variant.ident; - Container::new(&variant.fields, parse_quote!(#ident::#var_ident), attrs) - }) - .collect::>>()?; - - Ok(Enum { - enum_ident: ident, - variants, - }) - } - - /// Build derivation body for enums. - fn build(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let mut var_extracts = Vec::new(); - let mut variant_names = Vec::new(); - let mut error_names = Vec::new(); - - for var in &self.variants { - let struct_derive = var.build(ctx); - let ext = quote!({ - let maybe_ret = || -> #pyo3_path::PyResult { - #struct_derive - }(); - - match maybe_ret { - ok @ ::std::result::Result::Ok(_) => return ok, - ::std::result::Result::Err(err) => err - } - }); - - var_extracts.push(ext); - variant_names.push(var.path.segments.last().unwrap().ident.to_string()); - error_names.push(&var.err_name); - } - let ty_name = self.enum_ident.to_string(); - quote!( - let errors = [ - #(#var_extracts),* - ]; - ::std::result::Result::Err( - #pyo3_path::impl_::frompyobject::failed_to_extract_enum( - obj.py(), - #ty_name, - &[#(#variant_names),*], - &[#(#error_names),*], - &errors - ) - ) - ) - } -} - -struct NamedStructField<'a> { - ident: &'a syn::Ident, - getter: Option, - from_py_with: Option, -} - -struct TupleStructField { - from_py_with: Option, -} - -/// Container Style -/// -/// Covers Structs, Tuplestructs and corresponding Newtypes. -enum ContainerType<'a> { - /// Struct Container, e.g. `struct Foo { a: String }` - /// - /// Variant contains the list of field identifiers and the corresponding extraction call. - Struct(Vec>), - /// Newtype struct container, e.g. `#[transparent] struct Foo { a: String }` - /// - /// The field specified by the identifier is extracted directly from the object. - StructNewtype(&'a syn::Ident, Option), - /// Tuple struct, e.g. `struct Foo(String)`. - /// - /// Variant contains a list of conversion methods for each of the fields that are directly - /// extracted from the tuple. - Tuple(Vec), - /// Tuple newtype, e.g. `#[transparent] struct Foo(String)` - /// - /// The wrapped field is directly extracted from the object. - TupleNewtype(Option), -} - -/// Data container -/// -/// Either describes a struct or an enum variant. -struct Container<'a> { - path: syn::Path, - ty: ContainerType<'a>, - err_name: String, -} - -impl<'a> Container<'a> { - /// Construct a container based on fields, identifier and attributes. - /// - /// Fails if the variant has no fields or incompatible attributes. - fn new(fields: &'a Fields, path: syn::Path, options: ContainerOptions) -> Result { - let style = match fields { - Fields::Unnamed(unnamed) if !unnamed.unnamed.is_empty() => { - let mut tuple_fields = unnamed - .unnamed - .iter() - .map(|field| { - let attrs = FieldPyO3Attributes::from_attrs(&field.attrs)?; - ensure_spanned!( - attrs.getter.is_none(), - field.span() => "`getter` is not permitted on tuple struct elements." - ); - Ok(TupleStructField { - from_py_with: attrs.from_py_with, - }) - }) - .collect::>>()?; - - if tuple_fields.len() == 1 { - // Always treat a 1-length tuple struct as "transparent", even without the - // explicit annotation. - let field = tuple_fields.pop().unwrap(); - ContainerType::TupleNewtype(field.from_py_with) - } else if options.transparent { - bail_spanned!( - fields.span() => "transparent structs and variants can only have 1 field" - ); - } else { - ContainerType::Tuple(tuple_fields) - } - } - Fields::Named(named) if !named.named.is_empty() => { - let mut struct_fields = named - .named - .iter() - .map(|field| { - let ident = field - .ident - .as_ref() - .expect("Named fields should have identifiers"); - let mut attrs = FieldPyO3Attributes::from_attrs(&field.attrs)?; - - if let Some(ref from_item_all) = options.from_item_all { - if let Some(replaced) = attrs.getter.replace(FieldGetter::GetItem(None)) - { - match replaced { - FieldGetter::GetItem(Some(item_name)) => { - attrs.getter = Some(FieldGetter::GetItem(Some(item_name))); - } - FieldGetter::GetItem(None) => bail_spanned!(from_item_all.span() => "Useless `item` - the struct is already annotated with `from_item_all`"), - FieldGetter::GetAttr(_) => bail_spanned!( - from_item_all.span() => "The struct is already annotated with `from_item_all`, `attribute` is not allowed" - ), - } - } - } - - Ok(NamedStructField { - ident, - getter: attrs.getter, - from_py_with: attrs.from_py_with, - }) - }) - .collect::>>()?; - if options.transparent { - ensure_spanned!( - struct_fields.len() == 1, - fields.span() => "transparent structs and variants can only have 1 field" - ); - let field = struct_fields.pop().unwrap(); - ensure_spanned!( - field.getter.is_none(), - field.ident.span() => "`transparent` structs may not have a `getter` for the inner field" - ); - ContainerType::StructNewtype(field.ident, field.from_py_with) - } else { - ContainerType::Struct(struct_fields) - } - } - _ => bail_spanned!( - fields.span() => "cannot derive FromPyObject for empty structs and variants" - ), - }; - let err_name = options.annotation.map_or_else( - || path.segments.last().unwrap().ident.to_string(), - |lit_str| lit_str.value(), - ); - - let v = Container { - path, - ty: style, - err_name, - }; - Ok(v) - } - - fn name(&self) -> String { - let mut value = String::new(); - for segment in &self.path.segments { - if !value.is_empty() { - value.push_str("::"); - } - value.push_str(&segment.ident.to_string()); - } - value - } - - /// Build derivation body for a struct. - fn build(&self, ctx: &Ctx) -> TokenStream { - match &self.ty { - ContainerType::StructNewtype(ident, from_py_with) => { - self.build_newtype_struct(Some(ident), from_py_with, ctx) - } - ContainerType::TupleNewtype(from_py_with) => { - self.build_newtype_struct(None, from_py_with, ctx) - } - ContainerType::Tuple(tups) => self.build_tuple_struct(tups, ctx), - ContainerType::Struct(tups) => self.build_struct(tups, ctx), - } - } - - fn build_newtype_struct( - &self, - field_ident: Option<&Ident>, - from_py_with: &Option, - ctx: &Ctx, - ) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let self_ty = &self.path; - let struct_name = self.name(); - if let Some(ident) = field_ident { - let field_name = ident.to_string(); - match from_py_with { - None => quote! { - Ok(#self_ty { - #ident: #pyo3_path::impl_::frompyobject::extract_struct_field(obj, #struct_name, #field_name)? - }) - }, - Some(FromPyWithAttribute { - value: expr_path, .. - }) => quote! { - Ok(#self_ty { - #ident: #pyo3_path::impl_::frompyobject::extract_struct_field_with(#expr_path as fn(_) -> _, obj, #struct_name, #field_name)? - }) - }, - } - } else { - match from_py_with { - None => quote! { - #pyo3_path::impl_::frompyobject::extract_tuple_struct_field(obj, #struct_name, 0).map(#self_ty) - }, - - Some(FromPyWithAttribute { - value: expr_path, .. - }) => quote! { - #pyo3_path::impl_::frompyobject::extract_tuple_struct_field_with(#expr_path as fn(_) -> _, obj, #struct_name, 0).map(#self_ty) - }, - } - } - } - - fn build_tuple_struct(&self, struct_fields: &[TupleStructField], ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let self_ty = &self.path; - let struct_name = &self.name(); - let field_idents: Vec<_> = (0..struct_fields.len()) - .map(|i| format_ident!("arg{}", i)) - .collect(); - let fields = struct_fields.iter().zip(&field_idents).enumerate().map(|(index, (field, ident))| { - match &field.from_py_with { - None => quote!( - #pyo3_path::impl_::frompyobject::extract_tuple_struct_field(&#ident, #struct_name, #index)? - ), - Some(FromPyWithAttribute { - value: expr_path, .. - }) => quote! ( - #pyo3_path::impl_::frompyobject::extract_tuple_struct_field_with(#expr_path as fn(_) -> _, &#ident, #struct_name, #index)? - ), - } - }); - - quote!( - match #pyo3_path::types::PyAnyMethods::extract(obj) { - ::std::result::Result::Ok((#(#field_idents),*)) => ::std::result::Result::Ok(#self_ty(#(#fields),*)), - ::std::result::Result::Err(err) => ::std::result::Result::Err(err), - } - ) - } - - fn build_struct(&self, struct_fields: &[NamedStructField<'_>], ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let self_ty = &self.path; - let struct_name = self.name(); - let mut fields: Punctuated = Punctuated::new(); - for field in struct_fields { - let ident = field.ident; - let field_name = ident.unraw().to_string(); - let getter = match field.getter.as_ref().unwrap_or(&FieldGetter::GetAttr(None)) { - FieldGetter::GetAttr(Some(name)) => { - quote!(#pyo3_path::types::PyAnyMethods::getattr(obj, #pyo3_path::intern!(obj.py(), #name))) - } - FieldGetter::GetAttr(None) => { - quote!(#pyo3_path::types::PyAnyMethods::getattr(obj, #pyo3_path::intern!(obj.py(), #field_name))) - } - FieldGetter::GetItem(Some(syn::Lit::Str(key))) => { - quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #pyo3_path::intern!(obj.py(), #key))) - } - FieldGetter::GetItem(Some(key)) => { - quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #key)) - } - FieldGetter::GetItem(None) => { - quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #pyo3_path::intern!(obj.py(), #field_name))) - } - }; - let extractor = match &field.from_py_with { - None => { - quote!(#pyo3_path::impl_::frompyobject::extract_struct_field(&#getter?, #struct_name, #field_name)?) - } - Some(FromPyWithAttribute { - value: expr_path, .. - }) => { - quote! (#pyo3_path::impl_::frompyobject::extract_struct_field_with(#expr_path as fn(_) -> _, &#getter?, #struct_name, #field_name)?) - } - }; - - fields.push(quote!(#ident: #extractor)); - } - - quote!(::std::result::Result::Ok(#self_ty{#fields})) - } -} - -#[derive(Default)] -struct ContainerOptions { - /// Treat the Container as a Wrapper, directly extract its fields from the input object. - transparent: bool, - /// Force every field to be extracted from item of source Python object. - from_item_all: Option, - /// Change the name of an enum variant in the generated error message. - annotation: Option, - /// Change the path for the pyo3 crate - krate: Option, -} - -/// Attributes for deriving FromPyObject scoped on containers. -enum ContainerPyO3Attribute { - /// Treat the Container as a Wrapper, directly extract its fields from the input object. - Transparent(attributes::kw::transparent), - /// Force every field to be extracted from item of source Python object. - ItemAll(attributes::kw::from_item_all), - /// Change the name of an enum variant in the generated error message. - ErrorAnnotation(LitStr), - /// Change the path for the pyo3 crate - Crate(CrateAttribute), -} - -impl Parse for ContainerPyO3Attribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::transparent) { - let kw: attributes::kw::transparent = input.parse()?; - Ok(ContainerPyO3Attribute::Transparent(kw)) - } else if lookahead.peek(attributes::kw::from_item_all) { - let kw: attributes::kw::from_item_all = input.parse()?; - Ok(ContainerPyO3Attribute::ItemAll(kw)) - } else if lookahead.peek(attributes::kw::annotation) { - let _: attributes::kw::annotation = input.parse()?; - let _: Token![=] = input.parse()?; - input.parse().map(ContainerPyO3Attribute::ErrorAnnotation) - } else if lookahead.peek(Token![crate]) { - input.parse().map(ContainerPyO3Attribute::Crate) - } else { - Err(lookahead.error()) - } - } -} - -impl ContainerOptions { - fn from_attrs(attrs: &[Attribute]) -> Result { - let mut options = ContainerOptions::default(); - - for attr in attrs { - if let Some(pyo3_attrs) = get_pyo3_options(attr)? { - for pyo3_attr in pyo3_attrs { - match pyo3_attr { - ContainerPyO3Attribute::Transparent(kw) => { - ensure_spanned!( - !options.transparent, - kw.span() => "`transparent` may only be provided once" - ); - options.transparent = true; - } - ContainerPyO3Attribute::ItemAll(kw) => { - ensure_spanned!( - options.from_item_all.is_none(), - kw.span() => "`from_item_all` may only be provided once" - ); - options.from_item_all = Some(kw); - } - ContainerPyO3Attribute::ErrorAnnotation(lit_str) => { - ensure_spanned!( - options.annotation.is_none(), - lit_str.span() => "`annotation` may only be provided once" - ); - options.annotation = Some(lit_str); - } - ContainerPyO3Attribute::Crate(path) => { - ensure_spanned!( - options.krate.is_none(), - path.span() => "`crate` may only be provided once" - ); - options.krate = Some(path); - } - } - } - } - } - Ok(options) - } -} - -/// Attributes for deriving FromPyObject scoped on fields. -#[derive(Clone, Debug)] -struct FieldPyO3Attributes { - getter: Option, - from_py_with: Option, -} - -#[derive(Clone, Debug)] -enum FieldGetter { - GetItem(Option), - GetAttr(Option), -} - -enum FieldPyO3Attribute { - Getter(FieldGetter), - FromPyWith(FromPyWithAttribute), -} - -impl Parse for FieldPyO3Attribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::attribute) { - let _: attributes::kw::attribute = input.parse()?; - if input.peek(syn::token::Paren) { - let content; - let _ = parenthesized!(content in input); - let attr_name: LitStr = content.parse()?; - if !content.is_empty() { - return Err(content.error( - "expected at most one argument: `attribute` or `attribute(\"name\")`", - )); - } - ensure_spanned!( - !attr_name.value().is_empty(), - attr_name.span() => "attribute name cannot be empty" - ); - Ok(FieldPyO3Attribute::Getter(FieldGetter::GetAttr(Some( - attr_name, - )))) - } else { - Ok(FieldPyO3Attribute::Getter(FieldGetter::GetAttr(None))) - } - } else if lookahead.peek(attributes::kw::item) { - let _: attributes::kw::item = input.parse()?; - if input.peek(syn::token::Paren) { - let content; - let _ = parenthesized!(content in input); - let key = content.parse()?; - if !content.is_empty() { - return Err( - content.error("expected at most one argument: `item` or `item(key)`") - ); - } - Ok(FieldPyO3Attribute::Getter(FieldGetter::GetItem(Some(key)))) - } else { - Ok(FieldPyO3Attribute::Getter(FieldGetter::GetItem(None))) - } - } else if lookahead.peek(attributes::kw::from_py_with) { - input.parse().map(FieldPyO3Attribute::FromPyWith) - } else { - Err(lookahead.error()) - } - } -} - -impl FieldPyO3Attributes { - /// Extract the field attributes. - fn from_attrs(attrs: &[Attribute]) -> Result { - let mut getter = None; - let mut from_py_with = None; - - for attr in attrs { - if let Some(pyo3_attrs) = get_pyo3_options(attr)? { - for pyo3_attr in pyo3_attrs { - match pyo3_attr { - FieldPyO3Attribute::Getter(field_getter) => { - ensure_spanned!( - getter.is_none(), - attr.span() => "only one of `attribute` or `item` can be provided" - ); - getter = Some(field_getter); - } - FieldPyO3Attribute::FromPyWith(from_py_with_attr) => { - ensure_spanned!( - from_py_with.is_none(), - attr.span() => "`from_py_with` may only be provided once" - ); - from_py_with = Some(from_py_with_attr); - } - } - } - } - } - - Ok(FieldPyO3Attributes { - getter, - from_py_with, - }) - } -} - -fn verify_and_get_lifetime(generics: &syn::Generics) -> Result> { - let mut lifetimes = generics.lifetimes(); - let lifetime = lifetimes.next(); - ensure_spanned!( - lifetimes.next().is_none(), - generics.span() => "FromPyObject can be derived with at most one lifetime parameter" - ); - Ok(lifetime) -} - -/// Derive FromPyObject for enums and structs. -/// -/// * Max 1 lifetime specifier, will be tied to `FromPyObject`'s specifier -/// * At least one field, in case of `#[transparent]`, exactly one field -/// * At least one variant for enums. -/// * Fields of input structs and enums must implement `FromPyObject` or be annotated with `from_py_with` -/// * Derivation for structs with generic fields like `struct Foo(T)` -/// adds `T: FromPyObject` on the derived implementation. -pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result { - let options = ContainerOptions::from_attrs(&tokens.attrs)?; - let ctx = &Ctx::new(&options.krate, None); - let Ctx { pyo3_path, .. } = &ctx; - - let (_, ty_generics, _) = tokens.generics.split_for_impl(); - let mut trait_generics = tokens.generics.clone(); - let lt_param = if let Some(lt) = verify_and_get_lifetime(&trait_generics)? { - lt.clone() - } else { - trait_generics.params.push(parse_quote!('py)); - parse_quote!('py) - }; - let (impl_generics, _, where_clause) = trait_generics.split_for_impl(); - - let mut where_clause = where_clause.cloned().unwrap_or_else(|| parse_quote!(where)); - for param in trait_generics.type_params() { - let gen_ident = ¶m.ident; - where_clause - .predicates - .push(parse_quote!(#gen_ident: #pyo3_path::FromPyObject<'py>)) - } - - let derives = match &tokens.data { - syn::Data::Enum(en) => { - if options.transparent || options.annotation.is_some() { - bail_spanned!(tokens.span() => "`transparent` or `annotation` is not supported \ - at top level for enums"); - } - let en = Enum::new(en, &tokens.ident)?; - en.build(ctx) - } - syn::Data::Struct(st) => { - if let Some(lit_str) = &options.annotation { - bail_spanned!(lit_str.span() => "`annotation` is unsupported for structs"); - } - let ident = &tokens.ident; - let st = Container::new(&st.fields, parse_quote!(#ident), options)?; - st.build(ctx) - } - syn::Data::Union(_) => bail_spanned!( - tokens.span() => "#[derive(FromPyObject)] is not supported for unions" - ), - }; - - let ident = &tokens.ident; - Ok(quote!( - #[automatically_derived] - impl #impl_generics #pyo3_path::FromPyObject<#lt_param> for #ident #ty_generics #where_clause { - fn extract_bound(obj: &#pyo3_path::Bound<#lt_param, #pyo3_path::PyAny>) -> #pyo3_path::PyResult { - #derives - } - } - )) -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/intopyobject.rs b/vendor/pyo3/pyo3-macros-backend/src/intopyobject.rs deleted file mode 100644 index a60a5486cb..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/intopyobject.rs +++ /dev/null @@ -1,628 +0,0 @@ -use crate::attributes::{self, get_pyo3_options, CrateAttribute}; -use crate::utils::Ctx; -use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned}; -use syn::ext::IdentExt; -use syn::parse::{Parse, ParseStream}; -use syn::spanned::Spanned as _; -use syn::{ - parenthesized, parse_quote, Attribute, DataEnum, DeriveInput, Fields, Ident, Index, Result, - Token, -}; - -/// Attributes for deriving `IntoPyObject` scoped on containers. -enum ContainerPyO3Attribute { - /// Treat the Container as a Wrapper, directly convert its field into the output object. - Transparent(attributes::kw::transparent), - /// Change the path for the pyo3 crate - Crate(CrateAttribute), -} - -impl Parse for ContainerPyO3Attribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::transparent) { - let kw: attributes::kw::transparent = input.parse()?; - Ok(ContainerPyO3Attribute::Transparent(kw)) - } else if lookahead.peek(Token![crate]) { - input.parse().map(ContainerPyO3Attribute::Crate) - } else { - Err(lookahead.error()) - } - } -} - -#[derive(Default)] -struct ContainerOptions { - /// Treat the Container as a Wrapper, directly convert its field into the output object. - transparent: Option, - /// Change the path for the pyo3 crate - krate: Option, -} - -impl ContainerOptions { - fn from_attrs(attrs: &[Attribute]) -> Result { - let mut options = ContainerOptions::default(); - - for attr in attrs { - if let Some(pyo3_attrs) = get_pyo3_options(attr)? { - pyo3_attrs - .into_iter() - .try_for_each(|opt| options.set_option(opt))?; - } - } - Ok(options) - } - - fn set_option(&mut self, option: ContainerPyO3Attribute) -> syn::Result<()> { - macro_rules! set_option { - ($key:ident) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once") - ); - self.$key = Some($key); - } - }; - } - - match option { - ContainerPyO3Attribute::Transparent(transparent) => set_option!(transparent), - ContainerPyO3Attribute::Crate(krate) => set_option!(krate), - } - Ok(()) - } -} - -#[derive(Debug, Clone)] -struct ItemOption { - field: Option, - span: Span, -} - -impl ItemOption { - fn span(&self) -> Span { - self.span - } -} - -enum FieldAttribute { - Item(ItemOption), -} - -impl Parse for FieldAttribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::attribute) { - let attr: attributes::kw::attribute = input.parse()?; - bail_spanned!(attr.span => "`attribute` is not supported by `IntoPyObject`"); - } else if lookahead.peek(attributes::kw::item) { - let attr: attributes::kw::item = input.parse()?; - if input.peek(syn::token::Paren) { - let content; - let _ = parenthesized!(content in input); - let key = content.parse()?; - if !content.is_empty() { - return Err( - content.error("expected at most one argument: `item` or `item(key)`") - ); - } - Ok(FieldAttribute::Item(ItemOption { - field: Some(key), - span: attr.span, - })) - } else { - Ok(FieldAttribute::Item(ItemOption { - field: None, - span: attr.span, - })) - } - } else { - Err(lookahead.error()) - } - } -} - -#[derive(Clone, Debug, Default)] -struct FieldAttributes { - item: Option, -} - -impl FieldAttributes { - /// Extract the field attributes. - fn from_attrs(attrs: &[Attribute]) -> Result { - let mut options = FieldAttributes::default(); - - for attr in attrs { - if let Some(pyo3_attrs) = get_pyo3_options(attr)? { - pyo3_attrs - .into_iter() - .try_for_each(|opt| options.set_option(opt))?; - } - } - Ok(options) - } - - fn set_option(&mut self, option: FieldAttribute) -> syn::Result<()> { - macro_rules! set_option { - ($key:ident) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once") - ); - self.$key = Some($key); - } - }; - } - - match option { - FieldAttribute::Item(item) => set_option!(item), - } - Ok(()) - } -} - -enum IntoPyObjectTypes { - Transparent(syn::Type), - Opaque { - target: TokenStream, - output: TokenStream, - error: TokenStream, - }, -} - -struct IntoPyObjectImpl { - types: IntoPyObjectTypes, - body: TokenStream, -} - -struct NamedStructField<'a> { - ident: &'a syn::Ident, - field: &'a syn::Field, - item: Option, -} - -struct TupleStructField<'a> { - field: &'a syn::Field, -} - -/// Container Style -/// -/// Covers Structs, Tuplestructs and corresponding Newtypes. -enum ContainerType<'a> { - /// Struct Container, e.g. `struct Foo { a: String }` - /// - /// Variant contains the list of field identifiers and the corresponding extraction call. - Struct(Vec>), - /// Newtype struct container, e.g. `#[transparent] struct Foo { a: String }` - /// - /// The field specified by the identifier is extracted directly from the object. - StructNewtype(&'a syn::Field), - /// Tuple struct, e.g. `struct Foo(String)`. - /// - /// Variant contains a list of conversion methods for each of the fields that are directly - /// extracted from the tuple. - Tuple(Vec>), - /// Tuple newtype, e.g. `#[transparent] struct Foo(String)` - /// - /// The wrapped field is directly extracted from the object. - TupleNewtype(&'a syn::Field), -} - -/// Data container -/// -/// Either describes a struct or an enum variant. -struct Container<'a> { - path: syn::Path, - receiver: Option, - ty: ContainerType<'a>, -} - -/// Construct a container based on fields, identifier and attributes. -impl<'a> Container<'a> { - /// - /// Fails if the variant has no fields or incompatible attributes. - fn new( - receiver: Option, - fields: &'a Fields, - path: syn::Path, - options: ContainerOptions, - ) -> Result { - let style = match fields { - Fields::Unnamed(unnamed) if !unnamed.unnamed.is_empty() => { - let mut tuple_fields = unnamed - .unnamed - .iter() - .map(|field| { - let attrs = FieldAttributes::from_attrs(&field.attrs)?; - ensure_spanned!( - attrs.item.is_none(), - attrs.item.unwrap().span() => "`item` is not permitted on tuple struct elements." - ); - Ok(TupleStructField { field }) - }) - .collect::>>()?; - if tuple_fields.len() == 1 { - // Always treat a 1-length tuple struct as "transparent", even without the - // explicit annotation. - let TupleStructField { field } = tuple_fields.pop().unwrap(); - ContainerType::TupleNewtype(field) - } else if options.transparent.is_some() { - bail_spanned!( - fields.span() => "transparent structs and variants can only have 1 field" - ); - } else { - ContainerType::Tuple(tuple_fields) - } - } - Fields::Named(named) if !named.named.is_empty() => { - if options.transparent.is_some() { - ensure_spanned!( - named.named.iter().count() == 1, - fields.span() => "transparent structs and variants can only have 1 field" - ); - - let field = named.named.iter().next().unwrap(); - let attrs = FieldAttributes::from_attrs(&field.attrs)?; - ensure_spanned!( - attrs.item.is_none(), - attrs.item.unwrap().span() => "`transparent` structs may not have `item` for the inner field" - ); - ContainerType::StructNewtype(field) - } else { - let struct_fields = named - .named - .iter() - .map(|field| { - let ident = field - .ident - .as_ref() - .expect("Named fields should have identifiers"); - - let attrs = FieldAttributes::from_attrs(&field.attrs)?; - - Ok(NamedStructField { - ident, - field, - item: attrs.item, - }) - }) - .collect::>>()?; - ContainerType::Struct(struct_fields) - } - } - _ => bail_spanned!( - fields.span() => "cannot derive `IntoPyObject` for empty structs" - ), - }; - - let v = Container { - path, - receiver, - ty: style, - }; - Ok(v) - } - - fn match_pattern(&self) -> TokenStream { - let path = &self.path; - let pattern = match &self.ty { - ContainerType::Struct(fields) => fields - .iter() - .enumerate() - .map(|(i, f)| { - let ident = f.ident; - let new_ident = format_ident!("arg{i}"); - quote! {#ident: #new_ident,} - }) - .collect::(), - ContainerType::StructNewtype(field) => { - let ident = field.ident.as_ref().unwrap(); - quote!(#ident: arg0) - } - ContainerType::Tuple(fields) => { - let i = (0..fields.len()).map(Index::from); - let idents = (0..fields.len()).map(|i| format_ident!("arg{i}")); - quote! { #(#i: #idents,)* } - } - ContainerType::TupleNewtype(_) => quote!(0: arg0), - }; - - quote! { #path{ #pattern } } - } - - /// Build derivation body for a struct. - fn build(&self, ctx: &Ctx) -> IntoPyObjectImpl { - match &self.ty { - ContainerType::StructNewtype(field) | ContainerType::TupleNewtype(field) => { - self.build_newtype_struct(field, ctx) - } - ContainerType::Tuple(fields) => self.build_tuple_struct(fields, ctx), - ContainerType::Struct(fields) => self.build_struct(fields, ctx), - } - } - - fn build_newtype_struct(&self, field: &syn::Field, ctx: &Ctx) -> IntoPyObjectImpl { - let Ctx { pyo3_path, .. } = ctx; - let ty = &field.ty; - - let unpack = self - .receiver - .as_ref() - .map(|i| { - let pattern = self.match_pattern(); - quote! { let #pattern = #i;} - }) - .unwrap_or_default(); - - IntoPyObjectImpl { - types: IntoPyObjectTypes::Transparent(ty.clone()), - body: quote_spanned! { ty.span() => - #unpack - #pyo3_path::conversion::IntoPyObject::into_pyobject(arg0, py) - }, - } - } - - fn build_struct(&self, fields: &[NamedStructField<'_>], ctx: &Ctx) -> IntoPyObjectImpl { - let Ctx { pyo3_path, .. } = ctx; - - let unpack = self - .receiver - .as_ref() - .map(|i| { - let pattern = self.match_pattern(); - quote! { let #pattern = #i;} - }) - .unwrap_or_default(); - - let setter = fields - .iter() - .enumerate() - .map(|(i, f)| { - let key = f - .item - .as_ref() - .and_then(|item| item.field.as_ref()) - .map(|item| item.value()) - .unwrap_or_else(|| f.ident.unraw().to_string()); - let value = Ident::new(&format!("arg{i}"), f.field.ty.span()); - quote! { - #pyo3_path::types::PyDictMethods::set_item(&dict, #key, #value)?; - } - }) - .collect::(); - - IntoPyObjectImpl { - types: IntoPyObjectTypes::Opaque { - target: quote!(#pyo3_path::types::PyDict), - output: quote!(#pyo3_path::Bound<'py, Self::Target>), - error: quote!(#pyo3_path::PyErr), - }, - body: quote! { - #unpack - let dict = #pyo3_path::types::PyDict::new(py); - #setter - ::std::result::Result::Ok::<_, Self::Error>(dict) - }, - } - } - - fn build_tuple_struct(&self, fields: &[TupleStructField<'_>], ctx: &Ctx) -> IntoPyObjectImpl { - let Ctx { pyo3_path, .. } = ctx; - - let unpack = self - .receiver - .as_ref() - .map(|i| { - let pattern = self.match_pattern(); - quote! { let #pattern = #i;} - }) - .unwrap_or_default(); - - let setter = fields - .iter() - .enumerate() - .map(|(i, f)| { - let value = Ident::new(&format!("arg{i}"), f.field.ty.span()); - quote_spanned! { f.field.ty.span() => - #pyo3_path::conversion::IntoPyObject::into_pyobject(#value, py) - .map(#pyo3_path::BoundObject::into_any) - .map(#pyo3_path::BoundObject::into_bound)?, - } - }) - .collect::(); - - IntoPyObjectImpl { - types: IntoPyObjectTypes::Opaque { - target: quote!(#pyo3_path::types::PyTuple), - output: quote!(#pyo3_path::Bound<'py, Self::Target>), - error: quote!(#pyo3_path::PyErr), - }, - body: quote! { - #unpack - #pyo3_path::types::PyTuple::new(py, [#setter]) - }, - } - } -} - -/// Describes derivation input of an enum. -struct Enum<'a> { - variants: Vec>, -} - -impl<'a> Enum<'a> { - /// Construct a new enum representation. - /// - /// `data_enum` is the `syn` representation of the input enum, `ident` is the - /// `Identifier` of the enum. - fn new(data_enum: &'a DataEnum, ident: &'a Ident) -> Result { - ensure_spanned!( - !data_enum.variants.is_empty(), - ident.span() => "cannot derive `IntoPyObject` for empty enum" - ); - let variants = data_enum - .variants - .iter() - .map(|variant| { - let attrs = ContainerOptions::from_attrs(&variant.attrs)?; - let var_ident = &variant.ident; - - ensure_spanned!( - !variant.fields.is_empty(), - variant.ident.span() => "cannot derive `IntoPyObject` for empty variants" - ); - - Container::new( - None, - &variant.fields, - parse_quote!(#ident::#var_ident), - attrs, - ) - }) - .collect::>>()?; - - Ok(Enum { variants }) - } - - /// Build derivation body for enums. - fn build(&self, ctx: &Ctx) -> IntoPyObjectImpl { - let Ctx { pyo3_path, .. } = ctx; - - let variants = self - .variants - .iter() - .map(|v| { - let IntoPyObjectImpl { body, .. } = v.build(ctx); - let pattern = v.match_pattern(); - quote! { - #pattern => { - {#body} - .map(#pyo3_path::BoundObject::into_any) - .map(#pyo3_path::BoundObject::into_bound) - .map_err(::std::convert::Into::<#pyo3_path::PyErr>::into) - } - } - }) - .collect::(); - - IntoPyObjectImpl { - types: IntoPyObjectTypes::Opaque { - target: quote!(#pyo3_path::types::PyAny), - output: quote!(#pyo3_path::Bound<'py, >::Target>), - error: quote!(#pyo3_path::PyErr), - }, - body: quote! { - match self { - #variants - } - }, - } - } -} - -// if there is a `'py` lifetime, we treat it as the `Python<'py>` lifetime -fn verify_and_get_lifetime(generics: &syn::Generics) -> Option<&syn::LifetimeParam> { - let mut lifetimes = generics.lifetimes(); - lifetimes.find(|l| l.lifetime.ident == "py") -} - -pub fn build_derive_into_pyobject(tokens: &DeriveInput) -> Result { - let options = ContainerOptions::from_attrs(&tokens.attrs)?; - let ctx = &Ctx::new(&options.krate, None); - let Ctx { pyo3_path, .. } = &ctx; - - let (_, ty_generics, _) = tokens.generics.split_for_impl(); - let mut trait_generics = tokens.generics.clone(); - if REF { - trait_generics.params.push(parse_quote!('_a)); - } - let lt_param = if let Some(lt) = verify_and_get_lifetime(&trait_generics) { - lt.clone() - } else { - trait_generics.params.push(parse_quote!('py)); - parse_quote!('py) - }; - let (impl_generics, _, where_clause) = trait_generics.split_for_impl(); - - let mut where_clause = where_clause.cloned().unwrap_or_else(|| parse_quote!(where)); - for param in trait_generics.type_params() { - let gen_ident = ¶m.ident; - where_clause.predicates.push(if REF { - parse_quote!(&'_a #gen_ident: #pyo3_path::conversion::IntoPyObject<'py>) - } else { - parse_quote!(#gen_ident: #pyo3_path::conversion::IntoPyObject<'py>) - }) - } - - let IntoPyObjectImpl { types, body } = match &tokens.data { - syn::Data::Enum(en) => { - if options.transparent.is_some() { - bail_spanned!(tokens.span() => "`transparent` is not supported at top level for enums"); - } - let en = Enum::new(en, &tokens.ident)?; - en.build(ctx) - } - syn::Data::Struct(st) => { - let ident = &tokens.ident; - let st = Container::new( - Some(Ident::new("self", Span::call_site())), - &st.fields, - parse_quote!(#ident), - options, - )?; - st.build(ctx) - } - syn::Data::Union(_) => bail_spanned!( - tokens.span() => "#[derive(`IntoPyObject`)] is not supported for unions" - ), - }; - - let (target, output, error) = match types { - IntoPyObjectTypes::Transparent(ty) => { - if REF { - ( - quote! { <&'_a #ty as #pyo3_path::IntoPyObject<'py>>::Target }, - quote! { <&'_a #ty as #pyo3_path::IntoPyObject<'py>>::Output }, - quote! { <&'_a #ty as #pyo3_path::IntoPyObject<'py>>::Error }, - ) - } else { - ( - quote! { <#ty as #pyo3_path::IntoPyObject<'py>>::Target }, - quote! { <#ty as #pyo3_path::IntoPyObject<'py>>::Output }, - quote! { <#ty as #pyo3_path::IntoPyObject<'py>>::Error }, - ) - } - } - IntoPyObjectTypes::Opaque { - target, - output, - error, - } => (target, output, error), - }; - - let ident = &tokens.ident; - let ident = if REF { - quote! { &'_a #ident} - } else { - quote! { #ident } - }; - Ok(quote!( - #[automatically_derived] - impl #impl_generics #pyo3_path::conversion::IntoPyObject<#lt_param> for #ident #ty_generics #where_clause { - type Target = #target; - type Output = #output; - type Error = #error; - - fn into_pyobject(self, py: #pyo3_path::Python<#lt_param>) -> ::std::result::Result< - ::Output, - ::Error, - > { - #body - } - } - )) -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/konst.rs b/vendor/pyo3/pyo3-macros-backend/src/konst.rs deleted file mode 100644 index ae88f78524..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/konst.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::borrow::Cow; -use std::ffi::CString; - -use crate::attributes::{self, get_pyo3_options, take_attributes, NameAttribute}; -use crate::utils::{Ctx, LitCStr}; -use proc_macro2::{Ident, Span}; -use syn::{ - ext::IdentExt, - parse::{Parse, ParseStream}, - spanned::Spanned, - Result, -}; - -pub struct ConstSpec { - pub rust_ident: syn::Ident, - pub attributes: ConstAttributes, -} - -impl ConstSpec { - pub fn python_name(&self) -> Cow<'_, Ident> { - if let Some(name) = &self.attributes.name { - Cow::Borrowed(&name.value.0) - } else { - Cow::Owned(self.rust_ident.unraw()) - } - } - - /// Null-terminated Python name - pub fn null_terminated_python_name(&self, ctx: &Ctx) -> LitCStr { - let name = self.python_name().to_string(); - LitCStr::new(CString::new(name).unwrap(), Span::call_site(), ctx) - } -} - -pub struct ConstAttributes { - pub is_class_attr: bool, - pub name: Option, -} - -pub enum PyO3ConstAttribute { - Name(NameAttribute), -} - -impl Parse for PyO3ConstAttribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::name) { - input.parse().map(PyO3ConstAttribute::Name) - } else { - Err(lookahead.error()) - } - } -} - -impl ConstAttributes { - pub fn from_attrs(attrs: &mut Vec) -> syn::Result { - let mut attributes = ConstAttributes { - is_class_attr: false, - name: None, - }; - - take_attributes(attrs, |attr| { - if attr.path().is_ident("classattr") { - ensure_spanned!( - matches!(attr.meta, syn::Meta::Path(..)), - attr.span() => "`#[classattr]` does not take any arguments" - ); - attributes.is_class_attr = true; - Ok(true) - } else if let Some(pyo3_attributes) = get_pyo3_options(attr)? { - for pyo3_attr in pyo3_attributes { - match pyo3_attr { - PyO3ConstAttribute::Name(name) => attributes.set_name(name)?, - } - } - Ok(true) - } else { - Ok(false) - } - })?; - - Ok(attributes) - } - - fn set_name(&mut self, name: NameAttribute) -> Result<()> { - ensure_spanned!( - self.name.is_none(), - name.span() => "`name` may only be specified once" - ); - self.name = Some(name); - Ok(()) - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/lib.rs b/vendor/pyo3/pyo3-macros-backend/src/lib.rs deleted file mode 100644 index d6c8f28733..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/lib.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! This crate contains the implementation of the proc macro attributes - -#![warn(elided_lifetimes_in_paths, unused_lifetimes)] -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -#![recursion_limit = "1024"] - -// Listed first so that macros in this module are available in the rest of the crate. -#[macro_use] -mod utils; - -mod attributes; -mod deprecations; -mod frompyobject; -mod intopyobject; -mod konst; -mod method; -mod module; -mod params; -mod pyclass; -mod pyfunction; -mod pyimpl; -mod pymethod; -mod pyversions; -mod quotes; - -pub use frompyobject::build_derive_from_pyobject; -pub use intopyobject::build_derive_into_pyobject; -pub use module::{pymodule_function_impl, pymodule_module_impl, PyModuleOptions}; -pub use pyclass::{build_py_class, build_py_enum, PyClassArgs}; -pub use pyfunction::{build_py_function, PyFunctionOptions}; -pub use pyimpl::{build_py_methods, PyClassMethodsType}; -pub use utils::get_doc; diff --git a/vendor/pyo3/pyo3-macros-backend/src/method.rs b/vendor/pyo3/pyo3-macros-backend/src/method.rs deleted file mode 100644 index f99e64562b..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/method.rs +++ /dev/null @@ -1,1120 +0,0 @@ -use std::borrow::Cow; -use std::ffi::CString; -use std::fmt::Display; - -use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned, ToTokens}; -use syn::{ext::IdentExt, spanned::Spanned, Ident, Result}; - -use crate::deprecations::deprecate_trailing_option_default; -use crate::pyversions::is_abi3_before; -use crate::utils::{Ctx, LitCStr}; -use crate::{ - attributes::{FromPyWithAttribute, TextSignatureAttribute, TextSignatureAttributeValue}, - params::{impl_arg_params, Holders}, - pyfunction::{ - FunctionSignature, PyFunctionArgPyO3Attributes, PyFunctionOptions, SignatureAttribute, - }, - quotes, - utils::{self, PythonDoc}, -}; - -#[derive(Clone, Debug)] -pub struct RegularArg<'a> { - pub name: Cow<'a, syn::Ident>, - pub ty: &'a syn::Type, - pub from_py_with: Option, - pub default_value: Option, - pub option_wrapped_type: Option<&'a syn::Type>, -} - -/// Pythons *args argument -#[derive(Clone, Debug)] -pub struct VarargsArg<'a> { - pub name: Cow<'a, syn::Ident>, - pub ty: &'a syn::Type, -} - -/// Pythons **kwarg argument -#[derive(Clone, Debug)] -pub struct KwargsArg<'a> { - pub name: Cow<'a, syn::Ident>, - pub ty: &'a syn::Type, -} - -#[derive(Clone, Debug)] -pub struct CancelHandleArg<'a> { - pub name: &'a syn::Ident, - pub ty: &'a syn::Type, -} - -#[derive(Clone, Debug)] -pub struct PyArg<'a> { - pub name: &'a syn::Ident, - pub ty: &'a syn::Type, -} - -#[derive(Clone, Debug)] -pub enum FnArg<'a> { - Regular(RegularArg<'a>), - VarArgs(VarargsArg<'a>), - KwArgs(KwargsArg<'a>), - Py(PyArg<'a>), - CancelHandle(CancelHandleArg<'a>), -} - -impl<'a> FnArg<'a> { - pub fn name(&self) -> &syn::Ident { - match self { - FnArg::Regular(RegularArg { name, .. }) => name, - FnArg::VarArgs(VarargsArg { name, .. }) => name, - FnArg::KwArgs(KwargsArg { name, .. }) => name, - FnArg::Py(PyArg { name, .. }) => name, - FnArg::CancelHandle(CancelHandleArg { name, .. }) => name, - } - } - - pub fn ty(&self) -> &'a syn::Type { - match self { - FnArg::Regular(RegularArg { ty, .. }) => ty, - FnArg::VarArgs(VarargsArg { ty, .. }) => ty, - FnArg::KwArgs(KwargsArg { ty, .. }) => ty, - FnArg::Py(PyArg { ty, .. }) => ty, - FnArg::CancelHandle(CancelHandleArg { ty, .. }) => ty, - } - } - - #[allow(clippy::wrong_self_convention)] - pub fn from_py_with(&self) -> Option<&FromPyWithAttribute> { - if let FnArg::Regular(RegularArg { from_py_with, .. }) = self { - from_py_with.as_ref() - } else { - None - } - } - - pub fn to_varargs_mut(&mut self) -> Result<&mut Self> { - if let Self::Regular(RegularArg { - name, - ty, - option_wrapped_type: None, - .. - }) = self - { - *self = Self::VarArgs(VarargsArg { - name: name.clone(), - ty, - }); - Ok(self) - } else { - bail_spanned!(self.name().span() => "args cannot be optional") - } - } - - pub fn to_kwargs_mut(&mut self) -> Result<&mut Self> { - if let Self::Regular(RegularArg { - name, - ty, - option_wrapped_type: Some(..), - .. - }) = self - { - *self = Self::KwArgs(KwargsArg { - name: name.clone(), - ty, - }); - Ok(self) - } else { - bail_spanned!(self.name().span() => "kwargs must be Option<_>") - } - } - - /// Transforms a rust fn arg parsed with syn into a method::FnArg - pub fn parse(arg: &'a mut syn::FnArg) -> Result { - match arg { - syn::FnArg::Receiver(recv) => { - bail_spanned!(recv.span() => "unexpected receiver") - } // checked in parse_fn_type - syn::FnArg::Typed(cap) => { - if let syn::Type::ImplTrait(_) = &*cap.ty { - bail_spanned!(cap.ty.span() => IMPL_TRAIT_ERR); - } - - let PyFunctionArgPyO3Attributes { - from_py_with, - cancel_handle, - } = PyFunctionArgPyO3Attributes::from_attrs(&mut cap.attrs)?; - let ident = match &*cap.pat { - syn::Pat::Ident(syn::PatIdent { ident, .. }) => ident, - other => return Err(handle_argument_error(other)), - }; - - if utils::is_python(&cap.ty) { - return Ok(Self::Py(PyArg { - name: ident, - ty: &cap.ty, - })); - } - - if cancel_handle.is_some() { - // `PyFunctionArgPyO3Attributes::from_attrs` validates that - // only compatible attributes are specified, either - // `cancel_handle` or `from_py_with`, dublicates and any - // combination of the two are already rejected. - return Ok(Self::CancelHandle(CancelHandleArg { - name: ident, - ty: &cap.ty, - })); - } - - Ok(Self::Regular(RegularArg { - name: Cow::Borrowed(ident), - ty: &cap.ty, - from_py_with, - default_value: None, - option_wrapped_type: utils::option_type_argument(&cap.ty), - })) - } - } - } -} - -fn handle_argument_error(pat: &syn::Pat) -> syn::Error { - let span = pat.span(); - let msg = match pat { - syn::Pat::Wild(_) => "wildcard argument names are not supported", - syn::Pat::Struct(_) - | syn::Pat::Tuple(_) - | syn::Pat::TupleStruct(_) - | syn::Pat::Slice(_) => "destructuring in arguments is not supported", - _ => "unsupported argument", - }; - syn::Error::new(span, msg) -} - -/// Represents what kind of a function a pyfunction or pymethod is -#[derive(Clone, Debug)] -pub enum FnType { - /// Represents a pymethod annotated with `#[getter]` - Getter(SelfType), - /// Represents a pymethod annotated with `#[setter]` - Setter(SelfType), - /// Represents a regular pymethod - Fn(SelfType), - /// Represents a pymethod annotated with `#[new]`, i.e. the `__new__` dunder. - FnNew, - /// Represents a pymethod annotated with both `#[new]` and `#[classmethod]` (in either order) - FnNewClass(Span), - /// Represents a pymethod annotated with `#[classmethod]`, like a `@classmethod` - FnClass(Span), - /// Represents a pyfunction or a pymethod annotated with `#[staticmethod]`, like a `@staticmethod` - FnStatic, - /// Represents a pyfunction annotated with `#[pyo3(pass_module)] - FnModule(Span), - /// Represents a pymethod or associated constant annotated with `#[classattr]` - ClassAttribute, -} - -impl FnType { - pub fn skip_first_rust_argument_in_python_signature(&self) -> bool { - match self { - FnType::Getter(_) - | FnType::Setter(_) - | FnType::Fn(_) - | FnType::FnClass(_) - | FnType::FnNewClass(_) - | FnType::FnModule(_) => true, - FnType::FnNew | FnType::FnStatic | FnType::ClassAttribute => false, - } - } - - pub fn signature_attribute_allowed(&self) -> bool { - match self { - FnType::Fn(_) - | FnType::FnNew - | FnType::FnStatic - | FnType::FnClass(_) - | FnType::FnNewClass(_) - | FnType::FnModule(_) => true, - // Setter, Getter and ClassAttribute all have fixed signatures (either take 0 or 1 - // arguments) so cannot have a `signature = (...)` attribute. - FnType::Getter(_) | FnType::Setter(_) | FnType::ClassAttribute => false, - } - } - - pub fn self_arg( - &self, - cls: Option<&syn::Type>, - error_mode: ExtractErrorMode, - holders: &mut Holders, - ctx: &Ctx, - ) -> Option { - let Ctx { pyo3_path, .. } = ctx; - match self { - FnType::Getter(st) | FnType::Setter(st) | FnType::Fn(st) => { - let mut receiver = st.receiver( - cls.expect("no class given for Fn with a \"self\" receiver"), - error_mode, - holders, - ctx, - ); - syn::Token![,](Span::call_site()).to_tokens(&mut receiver); - Some(receiver) - } - FnType::FnClass(span) | FnType::FnNewClass(span) => { - let py = syn::Ident::new("py", Span::call_site()); - let slf: Ident = syn::Ident::new("_slf", Span::call_site()); - let pyo3_path = pyo3_path.to_tokens_spanned(*span); - let ret = quote_spanned! { *span => - #[allow(clippy::useless_conversion)] - ::std::convert::Into::into( - #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _)) - .downcast_unchecked::<#pyo3_path::types::PyType>() - ) - }; - Some(quote! { unsafe { #ret }, }) - } - FnType::FnModule(span) => { - let py = syn::Ident::new("py", Span::call_site()); - let slf: Ident = syn::Ident::new("_slf", Span::call_site()); - let pyo3_path = pyo3_path.to_tokens_spanned(*span); - let ret = quote_spanned! { *span => - #[allow(clippy::useless_conversion)] - ::std::convert::Into::into( - #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _)) - .downcast_unchecked::<#pyo3_path::types::PyModule>() - ) - }; - Some(quote! { unsafe { #ret }, }) - } - FnType::FnNew | FnType::FnStatic | FnType::ClassAttribute => None, - } - } -} - -#[derive(Clone, Debug)] -pub enum SelfType { - Receiver { mutable: bool, span: Span }, - TryFromBoundRef(Span), -} - -#[derive(Clone, Copy)] -pub enum ExtractErrorMode { - NotImplemented, - Raise, -} - -impl ExtractErrorMode { - pub fn handle_error(self, extract: TokenStream, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - match self { - ExtractErrorMode::Raise => quote! { #extract? }, - ExtractErrorMode::NotImplemented => quote! { - match #extract { - ::std::result::Result::Ok(value) => value, - ::std::result::Result::Err(_) => { return #pyo3_path::impl_::callback::convert(py, py.NotImplemented()); }, - } - }, - } - } -} - -impl SelfType { - pub fn receiver( - &self, - cls: &syn::Type, - error_mode: ExtractErrorMode, - holders: &mut Holders, - ctx: &Ctx, - ) -> TokenStream { - // Due to use of quote_spanned in this function, need to bind these idents to the - // main macro callsite. - let py = syn::Ident::new("py", Span::call_site()); - let slf = syn::Ident::new("_slf", Span::call_site()); - let Ctx { pyo3_path, .. } = ctx; - let bound_ref = - quote! { unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &#slf) } }; - match self { - SelfType::Receiver { span, mutable } => { - let method = if *mutable { - syn::Ident::new("extract_pyclass_ref_mut", *span) - } else { - syn::Ident::new("extract_pyclass_ref", *span) - }; - let holder = holders.push_holder(*span); - let pyo3_path = pyo3_path.to_tokens_spanned(*span); - error_mode.handle_error( - quote_spanned! { *span => - #pyo3_path::impl_::extract_argument::#method::<#cls>( - #bound_ref.0, - &mut #holder, - ) - }, - ctx, - ) - } - SelfType::TryFromBoundRef(span) => { - let pyo3_path = pyo3_path.to_tokens_spanned(*span); - error_mode.handle_error( - quote_spanned! { *span => - #bound_ref.downcast::<#cls>() - .map_err(::std::convert::Into::<#pyo3_path::PyErr>::into) - .and_then( - #[allow(unknown_lints, clippy::unnecessary_fallible_conversions)] // In case slf is Py (unknown_lints can be removed when MSRV is 1.75+) - |bound| ::std::convert::TryFrom::try_from(bound).map_err(::std::convert::Into::into) - ) - - }, - ctx - ) - } - } - } -} - -/// Determines which CPython calling convention a given FnSpec uses. -#[derive(Clone, Debug)] -pub enum CallingConvention { - Noargs, // METH_NOARGS - Varargs, // METH_VARARGS | METH_KEYWORDS - Fastcall, // METH_FASTCALL | METH_KEYWORDS (not compatible with `abi3` feature before 3.10) - TpNew, // special convention for tp_new -} - -impl CallingConvention { - /// Determine default calling convention from an argument signature. - /// - /// Different other slots (tp_call, tp_new) can have other requirements - /// and are set manually (see `parse_fn_type` below). - pub fn from_signature(signature: &FunctionSignature<'_>) -> Self { - if signature.python_signature.has_no_args() { - Self::Noargs - } else if signature.python_signature.kwargs.is_none() && !is_abi3_before(3, 10) { - // For functions that accept **kwargs, always prefer varargs for now based on - // historical performance testing. - // - // FASTCALL not compatible with `abi3` before 3.10 - Self::Fastcall - } else { - Self::Varargs - } - } -} - -pub struct FnSpec<'a> { - pub tp: FnType, - // Rust function name - pub name: &'a syn::Ident, - // Wrapped python name. This should not have any leading r#. - // r# can be removed by syn::ext::IdentExt::unraw() - pub python_name: syn::Ident, - pub signature: FunctionSignature<'a>, - pub convention: CallingConvention, - pub text_signature: Option, - pub asyncness: Option, - pub unsafety: Option, -} - -pub fn parse_method_receiver(arg: &syn::FnArg) -> Result { - match arg { - syn::FnArg::Receiver( - recv @ syn::Receiver { - reference: None, .. - }, - ) => { - bail_spanned!(recv.span() => RECEIVER_BY_VALUE_ERR); - } - syn::FnArg::Receiver(recv @ syn::Receiver { mutability, .. }) => Ok(SelfType::Receiver { - mutable: mutability.is_some(), - span: recv.span(), - }), - syn::FnArg::Typed(syn::PatType { ty, .. }) => { - if let syn::Type::ImplTrait(_) = &**ty { - bail_spanned!(ty.span() => IMPL_TRAIT_ERR); - } - Ok(SelfType::TryFromBoundRef(ty.span())) - } - } -} - -impl<'a> FnSpec<'a> { - /// Parser function signature and function attributes - pub fn parse( - // Signature is mutable to remove the `Python` argument. - sig: &'a mut syn::Signature, - meth_attrs: &mut Vec, - options: PyFunctionOptions, - ) -> Result> { - let PyFunctionOptions { - text_signature, - name, - signature, - .. - } = options; - - let mut python_name = name.map(|name| name.value.0); - - let fn_type = Self::parse_fn_type(sig, meth_attrs, &mut python_name)?; - ensure_signatures_on_valid_method(&fn_type, signature.as_ref(), text_signature.as_ref())?; - - let name = &sig.ident; - let python_name = python_name.as_ref().unwrap_or(name).unraw(); - - let arguments: Vec<_> = sig - .inputs - .iter_mut() - .skip(if fn_type.skip_first_rust_argument_in_python_signature() { - 1 - } else { - 0 - }) - .map(FnArg::parse) - .collect::>()?; - - let signature = if let Some(signature) = signature { - FunctionSignature::from_arguments_and_attribute(arguments, signature)? - } else { - FunctionSignature::from_arguments(arguments)? - }; - - let convention = if matches!(fn_type, FnType::FnNew | FnType::FnNewClass(_)) { - CallingConvention::TpNew - } else { - CallingConvention::from_signature(&signature) - }; - - Ok(FnSpec { - tp: fn_type, - name, - convention, - python_name, - signature, - text_signature, - asyncness: sig.asyncness, - unsafety: sig.unsafety, - }) - } - - pub fn null_terminated_python_name(&self, ctx: &Ctx) -> LitCStr { - let name = self.python_name.to_string(); - let name = CString::new(name).unwrap(); - LitCStr::new(name, self.python_name.span(), ctx) - } - - fn parse_fn_type( - sig: &syn::Signature, - meth_attrs: &mut Vec, - python_name: &mut Option, - ) -> Result { - let mut method_attributes = parse_method_attributes(meth_attrs)?; - - let name = &sig.ident; - let parse_receiver = |msg: &'static str| { - let first_arg = sig - .inputs - .first() - .ok_or_else(|| err_spanned!(sig.span() => msg))?; - parse_method_receiver(first_arg) - }; - - // strip get_ or set_ - let strip_fn_name = |prefix: &'static str| { - name.unraw() - .to_string() - .strip_prefix(prefix) - .map(|stripped| syn::Ident::new(stripped, name.span())) - }; - - let mut set_name_to_new = || { - if let Some(name) = &python_name { - bail_spanned!(name.span() => "`name` not allowed with `#[new]`"); - } - *python_name = Some(syn::Ident::new("__new__", Span::call_site())); - Ok(()) - }; - - let fn_type = match method_attributes.as_mut_slice() { - [] => FnType::Fn(parse_receiver( - "static method needs #[staticmethod] attribute", - )?), - [MethodTypeAttribute::StaticMethod(_)] => FnType::FnStatic, - [MethodTypeAttribute::ClassAttribute(_)] => FnType::ClassAttribute, - [MethodTypeAttribute::New(_)] => { - set_name_to_new()?; - FnType::FnNew - } - [MethodTypeAttribute::New(_), MethodTypeAttribute::ClassMethod(span)] - | [MethodTypeAttribute::ClassMethod(span), MethodTypeAttribute::New(_)] => { - set_name_to_new()?; - FnType::FnNewClass(*span) - } - [MethodTypeAttribute::ClassMethod(_)] => { - // Add a helpful hint if the classmethod doesn't look like a classmethod - let span = match sig.inputs.first() { - // Don't actually bother checking the type of the first argument, the compiler - // will error on incorrect type. - Some(syn::FnArg::Typed(first_arg)) => first_arg.ty.span(), - Some(syn::FnArg::Receiver(_)) | None => bail_spanned!( - sig.paren_token.span.join() => "Expected `&Bound` or `Py` as the first argument to `#[classmethod]`" - ), - }; - FnType::FnClass(span) - } - [MethodTypeAttribute::Getter(_, name)] => { - if let Some(name) = name.take() { - ensure_spanned!( - python_name.replace(name).is_none(), - python_name.span() => "`name` may only be specified once" - ); - } else if python_name.is_none() { - // Strip off "get_" prefix if needed - *python_name = strip_fn_name("get_"); - } - - FnType::Getter(parse_receiver("expected receiver for `#[getter]`")?) - } - [MethodTypeAttribute::Setter(_, name)] => { - if let Some(name) = name.take() { - ensure_spanned!( - python_name.replace(name).is_none(), - python_name.span() => "`name` may only be specified once" - ); - } else if python_name.is_none() { - // Strip off "set_" prefix if needed - *python_name = strip_fn_name("set_"); - } - - FnType::Setter(parse_receiver("expected receiver for `#[setter]`")?) - } - [first, rest @ .., last] => { - // Join as many of the spans together as possible - let span = rest - .iter() - .fold(first.span(), |s, next| s.join(next.span()).unwrap_or(s)); - let span = span.join(last.span()).unwrap_or(span); - // List all the attributes in the error message - let mut msg = format!("`{}` may not be combined with", first); - let mut is_first = true; - for attr in &*rest { - msg.push_str(&format!(" `{}`", attr)); - if is_first { - is_first = false; - } else { - msg.push(','); - } - } - if !rest.is_empty() { - msg.push_str(" and"); - } - msg.push_str(&format!(" `{}`", last)); - bail_spanned!(span => msg) - } - }; - Ok(fn_type) - } - - /// Return a C wrapper function for this signature. - pub fn get_wrapper_function( - &self, - ident: &proc_macro2::Ident, - cls: Option<&syn::Type>, - ctx: &Ctx, - ) -> Result { - let Ctx { - pyo3_path, - output_span, - } = ctx; - let mut cancel_handle_iter = self - .signature - .arguments - .iter() - .filter(|arg| matches!(arg, FnArg::CancelHandle(..))); - let cancel_handle = cancel_handle_iter.next(); - if let Some(FnArg::CancelHandle(CancelHandleArg { name, .. })) = cancel_handle { - ensure_spanned!(self.asyncness.is_some(), name.span() => "`cancel_handle` attribute can only be used with `async fn`"); - if let Some(FnArg::CancelHandle(CancelHandleArg { name, .. })) = - cancel_handle_iter.next() - { - bail_spanned!(name.span() => "`cancel_handle` may only be specified once"); - } - } - - if self.asyncness.is_some() { - ensure_spanned!( - cfg!(feature = "experimental-async"), - self.asyncness.span() => "async functions are only supported with the `experimental-async` feature" - ); - } - - let rust_call = |args: Vec, holders: &mut Holders| { - let mut self_arg = || self.tp.self_arg(cls, ExtractErrorMode::Raise, holders, ctx); - - let call = if self.asyncness.is_some() { - let throw_callback = if cancel_handle.is_some() { - quote! { Some(__throw_callback) } - } else { - quote! { None } - }; - let python_name = &self.python_name; - let qualname_prefix = match cls { - Some(cls) => quote!(Some(<#cls as #pyo3_path::PyTypeInfo>::NAME)), - None => quote!(None), - }; - let arg_names = (0..args.len()) - .map(|i| format_ident!("arg_{}", i)) - .collect::>(); - let future = match self.tp { - FnType::Fn(SelfType::Receiver { mutable: false, .. }) => { - quote! {{ - #(let #arg_names = #args;)* - let __guard = unsafe { #pyo3_path::impl_::coroutine::RefGuard::<#cls>::new(&#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_slf))? }; - async move { function(&__guard, #(#arg_names),*).await } - }} - } - FnType::Fn(SelfType::Receiver { mutable: true, .. }) => { - quote! {{ - #(let #arg_names = #args;)* - let mut __guard = unsafe { #pyo3_path::impl_::coroutine::RefMutGuard::<#cls>::new(&#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_slf))? }; - async move { function(&mut __guard, #(#arg_names),*).await } - }} - } - _ => { - if let Some(self_arg) = self_arg() { - quote! { - function( - // NB #self_arg includes a comma, so none inserted here - #self_arg - #(#args),* - ) - } - } else { - quote! { function(#(#args),*) } - } - } - }; - let mut call = quote! {{ - let future = #future; - #pyo3_path::impl_::coroutine::new_coroutine( - #pyo3_path::intern!(py, stringify!(#python_name)), - #qualname_prefix, - #throw_callback, - async move { - let fut = future.await; - #pyo3_path::impl_::wrap::converter(&fut).wrap(fut) - }, - ) - }}; - if cancel_handle.is_some() { - call = quote! {{ - let __cancel_handle = #pyo3_path::coroutine::CancelHandle::new(); - let __throw_callback = __cancel_handle.throw_callback(); - #call - }}; - } - call - } else if let Some(self_arg) = self_arg() { - quote! { - function( - // NB #self_arg includes a comma, so none inserted here - #self_arg - #(#args),* - ) - } - } else { - quote! { function(#(#args),*) } - }; - - // We must assign the output_span to the return value of the call, - // but *not* of the call itself otherwise the spans get really weird - let ret_ident = Ident::new("ret", *output_span); - let ret_expr = quote! { let #ret_ident = #call; }; - let return_conversion = - quotes::map_result_into_ptr(quotes::ok_wrap(ret_ident.to_token_stream(), ctx), ctx); - quote! { - { - #ret_expr - #return_conversion - } - } - }; - - let func_name = &self.name; - let rust_name = if let Some(cls) = cls { - quote!(#cls::#func_name) - } else { - quote!(#func_name) - }; - - let deprecation = deprecate_trailing_option_default(self); - - Ok(match self.convention { - CallingConvention::Noargs => { - let mut holders = Holders::new(); - let args = self - .signature - .arguments - .iter() - .map(|arg| match arg { - FnArg::Py(..) => quote!(py), - FnArg::CancelHandle(..) => quote!(__cancel_handle), - _ => unreachable!("`CallingConvention::Noargs` should not contain any arguments (reaching Python) except for `self`, which is handled below."), - }) - .collect(); - let call = rust_call(args, &mut holders); - let init_holders = holders.init_holders(ctx); - quote! { - unsafe fn #ident<'py>( - py: #pyo3_path::Python<'py>, - _slf: *mut #pyo3_path::ffi::PyObject, - ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { - #deprecation - let function = #rust_name; // Shadow the function name to avoid #3017 - #init_holders - let result = #call; - result - } - } - } - CallingConvention::Fastcall => { - let mut holders = Holders::new(); - let (arg_convert, args) = impl_arg_params(self, cls, true, &mut holders, ctx); - let call = rust_call(args, &mut holders); - let init_holders = holders.init_holders(ctx); - - quote! { - unsafe fn #ident<'py>( - py: #pyo3_path::Python<'py>, - _slf: *mut #pyo3_path::ffi::PyObject, - _args: *const *mut #pyo3_path::ffi::PyObject, - _nargs: #pyo3_path::ffi::Py_ssize_t, - _kwnames: *mut #pyo3_path::ffi::PyObject - ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { - #deprecation - let function = #rust_name; // Shadow the function name to avoid #3017 - #arg_convert - #init_holders - let result = #call; - result - } - } - } - CallingConvention::Varargs => { - let mut holders = Holders::new(); - let (arg_convert, args) = impl_arg_params(self, cls, false, &mut holders, ctx); - let call = rust_call(args, &mut holders); - let init_holders = holders.init_holders(ctx); - - quote! { - unsafe fn #ident<'py>( - py: #pyo3_path::Python<'py>, - _slf: *mut #pyo3_path::ffi::PyObject, - _args: *mut #pyo3_path::ffi::PyObject, - _kwargs: *mut #pyo3_path::ffi::PyObject - ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { - #deprecation - let function = #rust_name; // Shadow the function name to avoid #3017 - #arg_convert - #init_holders - let result = #call; - result - } - } - } - CallingConvention::TpNew => { - let mut holders = Holders::new(); - let (arg_convert, args) = impl_arg_params(self, cls, false, &mut holders, ctx); - let self_arg = self - .tp - .self_arg(cls, ExtractErrorMode::Raise, &mut holders, ctx); - let call = quote_spanned! {*output_span=> #rust_name(#self_arg #(#args),*) }; - let init_holders = holders.init_holders(ctx); - quote! { - unsafe fn #ident( - py: #pyo3_path::Python<'_>, - _slf: *mut #pyo3_path::ffi::PyTypeObject, - _args: *mut #pyo3_path::ffi::PyObject, - _kwargs: *mut #pyo3_path::ffi::PyObject - ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { - use #pyo3_path::impl_::callback::IntoPyCallbackOutput; - #deprecation - let function = #rust_name; // Shadow the function name to avoid #3017 - #arg_convert - #init_holders - let result = #call; - let initializer: #pyo3_path::PyClassInitializer::<#cls> = result.convert(py)?; - #pyo3_path::impl_::pymethods::tp_new_impl(py, initializer, _slf) - } - } - } - }) - } - - /// Return a `PyMethodDef` constructor for this function, matching the selected - /// calling convention. - pub fn get_methoddef(&self, wrapper: impl ToTokens, doc: &PythonDoc, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let python_name = self.null_terminated_python_name(ctx); - match self.convention { - CallingConvention::Noargs => quote! { - #pyo3_path::impl_::pymethods::PyMethodDef::noargs( - #python_name, - { - unsafe extern "C" fn trampoline( - _slf: *mut #pyo3_path::ffi::PyObject, - _args: *mut #pyo3_path::ffi::PyObject, - ) -> *mut #pyo3_path::ffi::PyObject - { - unsafe { - #pyo3_path::impl_::trampoline::noargs( - _slf, - _args, - #wrapper - ) - } - } - trampoline - }, - #doc, - ) - }, - CallingConvention::Fastcall => quote! { - #pyo3_path::impl_::pymethods::PyMethodDef::fastcall_cfunction_with_keywords( - #python_name, - { - unsafe extern "C" fn trampoline( - _slf: *mut #pyo3_path::ffi::PyObject, - _args: *const *mut #pyo3_path::ffi::PyObject, - _nargs: #pyo3_path::ffi::Py_ssize_t, - _kwnames: *mut #pyo3_path::ffi::PyObject - ) -> *mut #pyo3_path::ffi::PyObject - { - #pyo3_path::impl_::trampoline::fastcall_with_keywords( - _slf, - _args, - _nargs, - _kwnames, - #wrapper - ) - } - trampoline - }, - #doc, - ) - }, - CallingConvention::Varargs => quote! { - #pyo3_path::impl_::pymethods::PyMethodDef::cfunction_with_keywords( - #python_name, - { - unsafe extern "C" fn trampoline( - _slf: *mut #pyo3_path::ffi::PyObject, - _args: *mut #pyo3_path::ffi::PyObject, - _kwargs: *mut #pyo3_path::ffi::PyObject, - ) -> *mut #pyo3_path::ffi::PyObject - { - #pyo3_path::impl_::trampoline::cfunction_with_keywords( - _slf, - _args, - _kwargs, - #wrapper - ) - } - trampoline - }, - #doc, - ) - }, - CallingConvention::TpNew => unreachable!("tp_new cannot get a methoddef"), - } - } - - /// Forwards to [utils::get_doc] with the text signature of this spec. - pub fn get_doc(&self, attrs: &[syn::Attribute], ctx: &Ctx) -> PythonDoc { - let text_signature = self - .text_signature_call_signature() - .map(|sig| format!("{}{}", self.python_name, sig)); - utils::get_doc(attrs, text_signature, ctx) - } - - /// Creates the parenthesised arguments list for `__text_signature__` snippet based on this spec's signature - /// and/or attributes. Prepend the callable name to make a complete `__text_signature__`. - pub fn text_signature_call_signature(&self) -> Option { - let self_argument = match &self.tp { - // Getters / Setters / ClassAttribute are not callables on the Python side - FnType::Getter(_) | FnType::Setter(_) | FnType::ClassAttribute => return None, - FnType::Fn(_) => Some("self"), - FnType::FnModule(_) => Some("module"), - FnType::FnClass(_) | FnType::FnNewClass(_) => Some("cls"), - FnType::FnStatic | FnType::FnNew => None, - }; - - match self.text_signature.as_ref().map(|attr| &attr.value) { - Some(TextSignatureAttributeValue::Str(s)) => Some(s.value()), - None => Some(self.signature.text_signature(self_argument)), - Some(TextSignatureAttributeValue::Disabled(_)) => None, - } - } -} - -enum MethodTypeAttribute { - New(Span), - ClassMethod(Span), - StaticMethod(Span), - Getter(Span, Option), - Setter(Span, Option), - ClassAttribute(Span), -} - -impl MethodTypeAttribute { - fn span(&self) -> Span { - match self { - MethodTypeAttribute::New(span) - | MethodTypeAttribute::ClassMethod(span) - | MethodTypeAttribute::StaticMethod(span) - | MethodTypeAttribute::Getter(span, _) - | MethodTypeAttribute::Setter(span, _) - | MethodTypeAttribute::ClassAttribute(span) => *span, - } - } - - /// Attempts to parse a method type attribute. - /// - /// If the attribute does not match one of the attribute names, returns `Ok(None)`. - /// - /// Otherwise will either return a parse error or the attribute. - fn parse_if_matching_attribute(attr: &syn::Attribute) -> Result> { - fn ensure_no_arguments(meta: &syn::Meta, ident: &str) -> syn::Result<()> { - match meta { - syn::Meta::Path(_) => Ok(()), - syn::Meta::List(l) => bail_spanned!( - l.span() => format!( - "`#[{ident}]` does not take any arguments\n= help: did you mean `#[{ident}] #[pyo3({meta})]`?", - ident = ident, - meta = l.tokens, - ) - ), - syn::Meta::NameValue(nv) => { - bail_spanned!(nv.eq_token.span() => format!( - "`#[{}]` does not take any arguments\n= note: this was previously accepted and ignored", - ident - )) - } - } - } - - fn extract_name(meta: &syn::Meta, ident: &str) -> Result> { - match meta { - syn::Meta::Path(_) => Ok(None), - syn::Meta::NameValue(nv) => bail_spanned!( - nv.eq_token.span() => format!("expected `#[{}(name)]` to set the name", ident) - ), - syn::Meta::List(l) => { - if let Ok(name) = l.parse_args::() { - Ok(Some(name)) - } else if let Ok(name) = l.parse_args::() { - name.parse().map(Some) - } else { - bail_spanned!(l.tokens.span() => "expected ident or string literal for property name"); - } - } - } - } - - let meta = &attr.meta; - let path = meta.path(); - - if path.is_ident("new") { - ensure_no_arguments(meta, "new")?; - Ok(Some(MethodTypeAttribute::New(path.span()))) - } else if path.is_ident("classmethod") { - ensure_no_arguments(meta, "classmethod")?; - Ok(Some(MethodTypeAttribute::ClassMethod(path.span()))) - } else if path.is_ident("staticmethod") { - ensure_no_arguments(meta, "staticmethod")?; - Ok(Some(MethodTypeAttribute::StaticMethod(path.span()))) - } else if path.is_ident("classattr") { - ensure_no_arguments(meta, "classattr")?; - Ok(Some(MethodTypeAttribute::ClassAttribute(path.span()))) - } else if path.is_ident("getter") { - let name = extract_name(meta, "getter")?; - Ok(Some(MethodTypeAttribute::Getter(path.span(), name))) - } else if path.is_ident("setter") { - let name = extract_name(meta, "setter")?; - Ok(Some(MethodTypeAttribute::Setter(path.span(), name))) - } else { - Ok(None) - } - } -} - -impl Display for MethodTypeAttribute { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - MethodTypeAttribute::New(_) => "#[new]".fmt(f), - MethodTypeAttribute::ClassMethod(_) => "#[classmethod]".fmt(f), - MethodTypeAttribute::StaticMethod(_) => "#[staticmethod]".fmt(f), - MethodTypeAttribute::Getter(_, _) => "#[getter]".fmt(f), - MethodTypeAttribute::Setter(_, _) => "#[setter]".fmt(f), - MethodTypeAttribute::ClassAttribute(_) => "#[classattr]".fmt(f), - } - } -} - -fn parse_method_attributes(attrs: &mut Vec) -> Result> { - let mut new_attrs = Vec::new(); - let mut found_attrs = Vec::new(); - - for attr in attrs.drain(..) { - match MethodTypeAttribute::parse_if_matching_attribute(&attr)? { - Some(attr) => found_attrs.push(attr), - None => new_attrs.push(attr), - } - } - - *attrs = new_attrs; - - Ok(found_attrs) -} - -const IMPL_TRAIT_ERR: &str = "Python functions cannot have `impl Trait` arguments"; -const RECEIVER_BY_VALUE_ERR: &str = - "Python objects are shared, so 'self' cannot be moved out of the Python interpreter. -Try `&self`, `&mut self, `slf: PyRef<'_, Self>` or `slf: PyRefMut<'_, Self>`."; - -fn ensure_signatures_on_valid_method( - fn_type: &FnType, - signature: Option<&SignatureAttribute>, - text_signature: Option<&TextSignatureAttribute>, -) -> syn::Result<()> { - if let Some(signature) = signature { - match fn_type { - FnType::Getter(_) => { - debug_assert!(!fn_type.signature_attribute_allowed()); - bail_spanned!(signature.kw.span() => "`signature` not allowed with `getter`") - } - FnType::Setter(_) => { - debug_assert!(!fn_type.signature_attribute_allowed()); - bail_spanned!(signature.kw.span() => "`signature` not allowed with `setter`") - } - FnType::ClassAttribute => { - debug_assert!(!fn_type.signature_attribute_allowed()); - bail_spanned!(signature.kw.span() => "`signature` not allowed with `classattr`") - } - _ => debug_assert!(fn_type.signature_attribute_allowed()), - } - } - if let Some(text_signature) = text_signature { - match fn_type { - FnType::Getter(_) => { - bail_spanned!(text_signature.kw.span() => "`text_signature` not allowed with `getter`") - } - FnType::Setter(_) => { - bail_spanned!(text_signature.kw.span() => "`text_signature` not allowed with `setter`") - } - FnType::ClassAttribute => { - bail_spanned!(text_signature.kw.span() => "`text_signature` not allowed with `classattr`") - } - _ => {} - } - } - Ok(()) -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/module.rs b/vendor/pyo3/pyo3-macros-backend/src/module.rs deleted file mode 100644 index d9fac3cbd7..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/module.rs +++ /dev/null @@ -1,639 +0,0 @@ -//! Code generation for the function that initializes a python module and adds classes and function. - -use crate::{ - attributes::{ - self, kw, take_attributes, take_pyo3_options, CrateAttribute, GILUsedAttribute, - ModuleAttribute, NameAttribute, SubmoduleAttribute, - }, - get_doc, - pyclass::PyClassPyO3Option, - pyfunction::{impl_wrap_pyfunction, PyFunctionOptions}, - utils::{has_attribute, has_attribute_with_namespace, Ctx, IdentOrStr, LitCStr}, -}; -use proc_macro2::{Span, TokenStream}; -use quote::quote; -use std::ffi::CString; -use syn::{ - ext::IdentExt, - parse::{Parse, ParseStream}, - parse_quote, parse_quote_spanned, - punctuated::Punctuated, - spanned::Spanned, - token::Comma, - Item, Meta, Path, Result, -}; - -#[derive(Default)] -pub struct PyModuleOptions { - krate: Option, - name: Option, - module: Option, - submodule: Option, - gil_used: Option, -} - -impl Parse for PyModuleOptions { - fn parse(input: ParseStream<'_>) -> syn::Result { - let mut options: PyModuleOptions = Default::default(); - - options.add_attributes( - Punctuated::::parse_terminated(input)?, - )?; - - Ok(options) - } -} - -impl PyModuleOptions { - fn take_pyo3_options(&mut self, attrs: &mut Vec) -> Result<()> { - self.add_attributes(take_pyo3_options(attrs)?) - } - - fn add_attributes( - &mut self, - attrs: impl IntoIterator, - ) -> Result<()> { - macro_rules! set_option { - ($key:ident $(, $extra:literal)?) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once" $(, $extra)?) - ); - self.$key = Some($key); - } - }; - } - for attr in attrs { - match attr { - PyModulePyO3Option::Crate(krate) => set_option!(krate), - PyModulePyO3Option::Name(name) => set_option!(name), - PyModulePyO3Option::Module(module) => set_option!(module), - PyModulePyO3Option::Submodule(submodule) => set_option!( - submodule, - " (it is implicitly always specified for nested modules)" - ), - PyModulePyO3Option::GILUsed(gil_used) => { - set_option!(gil_used) - } - } - } - Ok(()) - } -} - -pub fn pymodule_module_impl( - module: &mut syn::ItemMod, - mut options: PyModuleOptions, -) -> Result { - let syn::ItemMod { - attrs, - vis, - unsafety: _, - ident, - mod_token, - content, - semi: _, - } = module; - let items = if let Some((_, items)) = content { - items - } else { - bail_spanned!(mod_token.span() => "`#[pymodule]` can only be used on inline modules") - }; - options.take_pyo3_options(attrs)?; - let ctx = &Ctx::new(&options.krate, None); - let Ctx { pyo3_path, .. } = ctx; - let doc = get_doc(attrs, None, ctx); - let name = options - .name - .map_or_else(|| ident.unraw(), |name| name.value.0); - let full_name = if let Some(module) = &options.module { - format!("{}.{}", module.value.value(), name) - } else { - name.to_string() - }; - - let mut module_items = Vec::new(); - let mut module_items_cfg_attrs = Vec::new(); - - fn extract_use_items( - source: &syn::UseTree, - cfg_attrs: &[syn::Attribute], - target_items: &mut Vec, - target_cfg_attrs: &mut Vec>, - ) -> Result<()> { - match source { - syn::UseTree::Name(name) => { - target_items.push(name.ident.clone()); - target_cfg_attrs.push(cfg_attrs.to_vec()); - } - syn::UseTree::Path(path) => { - extract_use_items(&path.tree, cfg_attrs, target_items, target_cfg_attrs)? - } - syn::UseTree::Group(group) => { - for tree in &group.items { - extract_use_items(tree, cfg_attrs, target_items, target_cfg_attrs)? - } - } - syn::UseTree::Glob(glob) => { - bail_spanned!(glob.span() => "#[pymodule] cannot import glob statements") - } - syn::UseTree::Rename(rename) => { - target_items.push(rename.rename.clone()); - target_cfg_attrs.push(cfg_attrs.to_vec()); - } - } - Ok(()) - } - - let mut pymodule_init = None; - - for item in &mut *items { - match item { - Item::Use(item_use) => { - let is_pymodule_export = - find_and_remove_attribute(&mut item_use.attrs, "pymodule_export"); - if is_pymodule_export { - let cfg_attrs = get_cfg_attributes(&item_use.attrs); - extract_use_items( - &item_use.tree, - &cfg_attrs, - &mut module_items, - &mut module_items_cfg_attrs, - )?; - } - } - Item::Fn(item_fn) => { - ensure_spanned!( - !has_attribute(&item_fn.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - let is_pymodule_init = - find_and_remove_attribute(&mut item_fn.attrs, "pymodule_init"); - let ident = &item_fn.sig.ident; - if is_pymodule_init { - ensure_spanned!( - !has_attribute(&item_fn.attrs, "pyfunction"), - item_fn.span() => "`#[pyfunction]` cannot be used alongside `#[pymodule_init]`" - ); - ensure_spanned!(pymodule_init.is_none(), item_fn.span() => "only one `#[pymodule_init]` may be specified"); - pymodule_init = Some(quote! { #ident(module)?; }); - } else if has_attribute(&item_fn.attrs, "pyfunction") - || has_attribute_with_namespace( - &item_fn.attrs, - Some(pyo3_path), - &["pyfunction"], - ) - || has_attribute_with_namespace( - &item_fn.attrs, - Some(pyo3_path), - &["prelude", "pyfunction"], - ) - { - module_items.push(ident.clone()); - module_items_cfg_attrs.push(get_cfg_attributes(&item_fn.attrs)); - } - } - Item::Struct(item_struct) => { - ensure_spanned!( - !has_attribute(&item_struct.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - if has_attribute(&item_struct.attrs, "pyclass") - || has_attribute_with_namespace( - &item_struct.attrs, - Some(pyo3_path), - &["pyclass"], - ) - || has_attribute_with_namespace( - &item_struct.attrs, - Some(pyo3_path), - &["prelude", "pyclass"], - ) - { - module_items.push(item_struct.ident.clone()); - module_items_cfg_attrs.push(get_cfg_attributes(&item_struct.attrs)); - if !has_pyo3_module_declared::( - &item_struct.attrs, - "pyclass", - |option| matches!(option, PyClassPyO3Option::Module(_)), - )? { - set_module_attribute(&mut item_struct.attrs, &full_name); - } - } - } - Item::Enum(item_enum) => { - ensure_spanned!( - !has_attribute(&item_enum.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - if has_attribute(&item_enum.attrs, "pyclass") - || has_attribute_with_namespace(&item_enum.attrs, Some(pyo3_path), &["pyclass"]) - || has_attribute_with_namespace( - &item_enum.attrs, - Some(pyo3_path), - &["prelude", "pyclass"], - ) - { - module_items.push(item_enum.ident.clone()); - module_items_cfg_attrs.push(get_cfg_attributes(&item_enum.attrs)); - if !has_pyo3_module_declared::( - &item_enum.attrs, - "pyclass", - |option| matches!(option, PyClassPyO3Option::Module(_)), - )? { - set_module_attribute(&mut item_enum.attrs, &full_name); - } - } - } - Item::Mod(item_mod) => { - ensure_spanned!( - !has_attribute(&item_mod.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - if has_attribute(&item_mod.attrs, "pymodule") - || has_attribute_with_namespace(&item_mod.attrs, Some(pyo3_path), &["pymodule"]) - || has_attribute_with_namespace( - &item_mod.attrs, - Some(pyo3_path), - &["prelude", "pymodule"], - ) - { - module_items.push(item_mod.ident.clone()); - module_items_cfg_attrs.push(get_cfg_attributes(&item_mod.attrs)); - if !has_pyo3_module_declared::( - &item_mod.attrs, - "pymodule", - |option| matches!(option, PyModulePyO3Option::Module(_)), - )? { - set_module_attribute(&mut item_mod.attrs, &full_name); - } - item_mod - .attrs - .push(parse_quote_spanned!(item_mod.mod_token.span()=> #[pyo3(submodule)])); - } - } - Item::ForeignMod(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Trait(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Const(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Static(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Macro(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::ExternCrate(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Impl(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::TraitAlias(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Type(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - Item::Union(item) => { - ensure_spanned!( - !has_attribute(&item.attrs, "pymodule_export"), - item.span() => "`#[pymodule_export]` may only be used on `use` statements" - ); - } - _ => (), - } - } - - let module_def = quote! {{ - use #pyo3_path::impl_::pymodule as impl_; - const INITIALIZER: impl_::ModuleInitializer = impl_::ModuleInitializer(__pyo3_pymodule); - unsafe { - impl_::ModuleDef::new( - __PYO3_NAME, - #doc, - INITIALIZER - ) - } - }}; - let initialization = module_initialization( - &name, - ctx, - module_def, - options.submodule.is_some(), - options.gil_used.map_or(true, |op| op.value.value), - ); - - Ok(quote!( - #(#attrs)* - #vis #mod_token #ident { - #(#items)* - - #initialization - - fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> { - use #pyo3_path::impl_::pymodule::PyAddToModule; - #( - #(#module_items_cfg_attrs)* - #module_items::_PYO3_DEF.add_to_module(module)?; - )* - #pymodule_init - ::std::result::Result::Ok(()) - } - } - )) -} - -/// Generates the function that is called by the python interpreter to initialize the native -/// module -pub fn pymodule_function_impl( - function: &mut syn::ItemFn, - mut options: PyModuleOptions, -) -> Result { - options.take_pyo3_options(&mut function.attrs)?; - process_functions_in_module(&options, function)?; - let ctx = &Ctx::new(&options.krate, None); - let Ctx { pyo3_path, .. } = ctx; - let ident = &function.sig.ident; - let name = options - .name - .map_or_else(|| ident.unraw(), |name| name.value.0); - let vis = &function.vis; - let doc = get_doc(&function.attrs, None, ctx); - - let initialization = module_initialization( - &name, - ctx, - quote! { MakeDef::make_def() }, - false, - options.gil_used.map_or(true, |op| op.value.value), - ); - - // Module function called with optional Python<'_> marker as first arg, followed by the module. - let mut module_args = Vec::new(); - if function.sig.inputs.len() == 2 { - module_args.push(quote!(module.py())); - } - module_args - .push(quote!(::std::convert::Into::into(#pyo3_path::impl_::pymethods::BoundRef(module)))); - - Ok(quote! { - #[doc(hidden)] - #vis mod #ident { - #initialization - } - - // Generate the definition inside an anonymous function in the same scope as the original function - - // this avoids complications around the fact that the generated module has a different scope - // (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is - // inside a function body) - #[allow(unknown_lints, non_local_definitions)] - impl #ident::MakeDef { - const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef { - fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> { - #ident(#(#module_args),*) - } - - const INITIALIZER: #pyo3_path::impl_::pymodule::ModuleInitializer = #pyo3_path::impl_::pymodule::ModuleInitializer(__pyo3_pymodule); - unsafe { - #pyo3_path::impl_::pymodule::ModuleDef::new( - #ident::__PYO3_NAME, - #doc, - INITIALIZER - ) - } - } - } - }) -} - -fn module_initialization( - name: &syn::Ident, - ctx: &Ctx, - module_def: TokenStream, - is_submodule: bool, - gil_used: bool, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let pyinit_symbol = format!("PyInit_{}", name); - let name = name.to_string(); - let pyo3_name = LitCStr::new(CString::new(name).unwrap(), Span::call_site(), ctx); - - let mut result = quote! { - #[doc(hidden)] - pub const __PYO3_NAME: &'static ::std::ffi::CStr = #pyo3_name; - - pub(super) struct MakeDef; - #[doc(hidden)] - pub static _PYO3_DEF: #pyo3_path::impl_::pymodule::ModuleDef = #module_def; - #[doc(hidden)] - // so wrapped submodules can see what gil_used is - pub static __PYO3_GIL_USED: bool = #gil_used; - }; - if !is_submodule { - result.extend(quote! { - /// This autogenerated function is called by the python interpreter when importing - /// the module. - #[doc(hidden)] - #[export_name = #pyinit_symbol] - pub unsafe extern "C" fn __pyo3_init() -> *mut #pyo3_path::ffi::PyObject { - unsafe { #pyo3_path::impl_::trampoline::module_init(|py| _PYO3_DEF.make_module(py, #gil_used)) } - } - }); - } - result -} - -/// Finds and takes care of the #[pyfn(...)] in `#[pymodule]` -fn process_functions_in_module(options: &PyModuleOptions, func: &mut syn::ItemFn) -> Result<()> { - let ctx = &Ctx::new(&options.krate, None); - let Ctx { pyo3_path, .. } = ctx; - let mut stmts: Vec = Vec::new(); - - for mut stmt in func.block.stmts.drain(..) { - if let syn::Stmt::Item(Item::Fn(func)) = &mut stmt { - if let Some(pyfn_args) = get_pyfn_attr(&mut func.attrs)? { - let module_name = pyfn_args.modname; - let wrapped_function = impl_wrap_pyfunction(func, pyfn_args.options)?; - let name = &func.sig.ident; - let statements: Vec = syn::parse_quote! { - #wrapped_function - { - use #pyo3_path::types::PyModuleMethods; - #module_name.add_function(#pyo3_path::wrap_pyfunction!(#name, #module_name.as_borrowed())?)?; - } - }; - stmts.extend(statements); - } - }; - stmts.push(stmt); - } - - func.block.stmts = stmts; - Ok(()) -} - -pub struct PyFnArgs { - modname: Path, - options: PyFunctionOptions, -} - -impl Parse for PyFnArgs { - fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result { - let modname = input.parse().map_err( - |e| err_spanned!(e.span() => "expected module as first argument to #[pyfn()]"), - )?; - - if input.is_empty() { - return Ok(Self { - modname, - options: Default::default(), - }); - } - - let _: Comma = input.parse()?; - - Ok(Self { - modname, - options: input.parse()?, - }) - } -} - -/// Extracts the data from the #[pyfn(...)] attribute of a function -fn get_pyfn_attr(attrs: &mut Vec) -> syn::Result> { - let mut pyfn_args: Option = None; - - take_attributes(attrs, |attr| { - if attr.path().is_ident("pyfn") { - ensure_spanned!( - pyfn_args.is_none(), - attr.span() => "`#[pyfn] may only be specified once" - ); - pyfn_args = Some(attr.parse_args()?); - Ok(true) - } else { - Ok(false) - } - })?; - - if let Some(pyfn_args) = &mut pyfn_args { - pyfn_args - .options - .add_attributes(take_pyo3_options(attrs)?)?; - } - - Ok(pyfn_args) -} - -fn get_cfg_attributes(attrs: &[syn::Attribute]) -> Vec { - attrs - .iter() - .filter(|attr| attr.path().is_ident("cfg")) - .cloned() - .collect() -} - -fn find_and_remove_attribute(attrs: &mut Vec, ident: &str) -> bool { - let mut found = false; - attrs.retain(|attr| { - if attr.path().is_ident(ident) { - found = true; - false - } else { - true - } - }); - found -} - -impl PartialEq for IdentOrStr<'_> { - fn eq(&self, other: &syn::Ident) -> bool { - match self { - IdentOrStr::Str(s) => other == s, - IdentOrStr::Ident(i) => other == i, - } - } -} - -fn set_module_attribute(attrs: &mut Vec, module_name: &str) { - attrs.push(parse_quote!(#[pyo3(module = #module_name)])); -} - -fn has_pyo3_module_declared( - attrs: &[syn::Attribute], - root_attribute_name: &str, - is_module_option: impl Fn(&T) -> bool + Copy, -) -> Result { - for attr in attrs { - if (attr.path().is_ident("pyo3") || attr.path().is_ident(root_attribute_name)) - && matches!(attr.meta, Meta::List(_)) - { - for option in &attr.parse_args_with(Punctuated::::parse_terminated)? { - if is_module_option(option) { - return Ok(true); - } - } - } - } - Ok(false) -} - -enum PyModulePyO3Option { - Submodule(SubmoduleAttribute), - Crate(CrateAttribute), - Name(NameAttribute), - Module(ModuleAttribute), - GILUsed(GILUsedAttribute), -} - -impl Parse for PyModulePyO3Option { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::name) { - input.parse().map(PyModulePyO3Option::Name) - } else if lookahead.peek(syn::Token![crate]) { - input.parse().map(PyModulePyO3Option::Crate) - } else if lookahead.peek(attributes::kw::module) { - input.parse().map(PyModulePyO3Option::Module) - } else if lookahead.peek(attributes::kw::submodule) { - input.parse().map(PyModulePyO3Option::Submodule) - } else if lookahead.peek(attributes::kw::gil_used) { - input.parse().map(PyModulePyO3Option::GILUsed) - } else { - Err(lookahead.error()) - } - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/params.rs b/vendor/pyo3/pyo3-macros-backend/src/params.rs deleted file mode 100644 index 338feb1b0c..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/params.rs +++ /dev/null @@ -1,314 +0,0 @@ -use crate::utils::Ctx; -use crate::{ - method::{FnArg, FnSpec, RegularArg}, - pyfunction::FunctionSignature, - quotes::some_wrap, -}; -use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned}; -use syn::spanned::Spanned; - -pub struct Holders { - holders: Vec, -} - -impl Holders { - pub fn new() -> Self { - Holders { - holders: Vec::new(), - } - } - - pub fn push_holder(&mut self, span: Span) -> syn::Ident { - let holder = syn::Ident::new(&format!("holder_{}", self.holders.len()), span); - self.holders.push(holder.clone()); - holder - } - - pub fn init_holders(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let holders = &self.holders; - quote! { - #[allow(clippy::let_unit_value)] - #(let mut #holders = #pyo3_path::impl_::extract_argument::FunctionArgumentHolder::INIT;)* - } - } -} - -/// Return true if the argument list is simply (*args, **kwds). -pub fn is_forwarded_args(signature: &FunctionSignature<'_>) -> bool { - matches!( - signature.arguments.as_slice(), - [FnArg::VarArgs(..), FnArg::KwArgs(..),] - ) -} - -pub fn impl_arg_params( - spec: &FnSpec<'_>, - self_: Option<&syn::Type>, - fastcall: bool, - holders: &mut Holders, - ctx: &Ctx, -) -> (TokenStream, Vec) { - let args_array = syn::Ident::new("output", Span::call_site()); - let Ctx { pyo3_path, .. } = ctx; - - let from_py_with = spec - .signature - .arguments - .iter() - .enumerate() - .filter_map(|(i, arg)| { - let from_py_with = &arg.from_py_with()?.value; - let from_py_with_holder = format_ident!("from_py_with_{}", i); - Some(quote_spanned! { from_py_with.span() => - let #from_py_with_holder = #from_py_with; - }) - }) - .collect::(); - - if !fastcall && is_forwarded_args(&spec.signature) { - // In the varargs convention, we can just pass though if the signature - // is (*args, **kwds). - let arg_convert = spec - .signature - .arguments - .iter() - .enumerate() - .map(|(i, arg)| impl_arg_param(arg, i, &mut 0, holders, ctx)) - .collect(); - return ( - quote! { - let _args = unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &_args) }; - let _kwargs = #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr_or_opt(py, &_kwargs); - #from_py_with - }, - arg_convert, - ); - }; - - let positional_parameter_names = &spec.signature.python_signature.positional_parameters; - let positional_only_parameters = &spec.signature.python_signature.positional_only_parameters; - let required_positional_parameters = &spec - .signature - .python_signature - .required_positional_parameters; - let keyword_only_parameters = spec - .signature - .python_signature - .keyword_only_parameters - .iter() - .map(|(name, required)| { - quote! { - #pyo3_path::impl_::extract_argument::KeywordOnlyParameterDescription { - name: #name, - required: #required, - } - } - }); - - let num_params = positional_parameter_names.len() + keyword_only_parameters.len(); - - let mut option_pos = 0usize; - let param_conversion = spec - .signature - .arguments - .iter() - .enumerate() - .map(|(i, arg)| impl_arg_param(arg, i, &mut option_pos, holders, ctx)) - .collect(); - - let args_handler = if spec.signature.python_signature.varargs.is_some() { - quote! { #pyo3_path::impl_::extract_argument::TupleVarargs } - } else { - quote! { #pyo3_path::impl_::extract_argument::NoVarargs } - }; - let kwargs_handler = if spec.signature.python_signature.kwargs.is_some() { - quote! { #pyo3_path::impl_::extract_argument::DictVarkeywords } - } else { - quote! { #pyo3_path::impl_::extract_argument::NoVarkeywords } - }; - - let cls_name = if let Some(cls) = self_ { - quote! { ::std::option::Option::Some(<#cls as #pyo3_path::type_object::PyTypeInfo>::NAME) } - } else { - quote! { ::std::option::Option::None } - }; - let python_name = &spec.python_name; - - let extract_expression = if fastcall { - quote! { - DESCRIPTION.extract_arguments_fastcall::<#args_handler, #kwargs_handler>( - py, - _args, - _nargs, - _kwnames, - &mut #args_array - )? - } - } else { - quote! { - DESCRIPTION.extract_arguments_tuple_dict::<#args_handler, #kwargs_handler>( - py, - _args, - _kwargs, - &mut #args_array - )? - } - }; - - // create array of arguments, and then parse - ( - quote! { - const DESCRIPTION: #pyo3_path::impl_::extract_argument::FunctionDescription = #pyo3_path::impl_::extract_argument::FunctionDescription { - cls_name: #cls_name, - func_name: stringify!(#python_name), - positional_parameter_names: &[#(#positional_parameter_names),*], - positional_only_parameters: #positional_only_parameters, - required_positional_parameters: #required_positional_parameters, - keyword_only_parameters: &[#(#keyword_only_parameters),*], - }; - let mut #args_array = [::std::option::Option::None; #num_params]; - let (_args, _kwargs) = #extract_expression; - #from_py_with - }, - param_conversion, - ) -} - -fn impl_arg_param( - arg: &FnArg<'_>, - pos: usize, - option_pos: &mut usize, - holders: &mut Holders, - ctx: &Ctx, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let args_array = syn::Ident::new("output", Span::call_site()); - - match arg { - FnArg::Regular(arg) => { - let from_py_with = format_ident!("from_py_with_{}", pos); - let arg_value = quote!(#args_array[#option_pos].as_deref()); - *option_pos += 1; - impl_regular_arg_param(arg, from_py_with, arg_value, holders, ctx) - } - FnArg::VarArgs(arg) => { - let holder = holders.push_holder(arg.name.span()); - let name_str = arg.name.to_string(); - quote_spanned! { arg.name.span() => - #pyo3_path::impl_::extract_argument::extract_argument( - &_args, - &mut #holder, - #name_str - )? - } - } - FnArg::KwArgs(arg) => { - let holder = holders.push_holder(arg.name.span()); - let name_str = arg.name.to_string(); - quote_spanned! { arg.name.span() => - #pyo3_path::impl_::extract_argument::extract_optional_argument( - _kwargs.as_deref(), - &mut #holder, - #name_str, - || ::std::option::Option::None - )? - } - } - FnArg::Py(..) => quote! { py }, - FnArg::CancelHandle(..) => quote! { __cancel_handle }, - } -} - -/// Re option_pos: The option slice doesn't contain the py: Python argument, so the argument -/// index and the index in option diverge when using py: Python -pub(crate) fn impl_regular_arg_param( - arg: &RegularArg<'_>, - from_py_with: syn::Ident, - arg_value: TokenStream, // expected type: Option<&'a Bound<'py, PyAny>> - holders: &mut Holders, - ctx: &Ctx, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let pyo3_path = pyo3_path.to_tokens_spanned(arg.ty.span()); - - // Use this macro inside this function, to ensure that all code generated here is associated - // with the function argument - macro_rules! quote_arg_span { - ($($tokens:tt)*) => { quote_spanned!(arg.ty.span() => $($tokens)*) } - } - - let name_str = arg.name.to_string(); - let mut default = arg.default_value.as_ref().map(|expr| quote!(#expr)); - - // Option arguments have special treatment: the default should be specified _without_ the - // Some() wrapper. Maybe this should be changed in future?! - if arg.option_wrapped_type.is_some() { - default = Some(default.map_or_else( - || quote!(::std::option::Option::None), - |tokens| some_wrap(tokens, ctx), - )); - } - - if arg.from_py_with.is_some() { - if let Some(default) = default { - quote_arg_span! { - #pyo3_path::impl_::extract_argument::from_py_with_with_default( - #arg_value, - #name_str, - #from_py_with as fn(_) -> _, - #[allow(clippy::redundant_closure)] - { - || #default - } - )? - } - } else { - let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value) }}; - quote_arg_span! { - #pyo3_path::impl_::extract_argument::from_py_with( - #unwrap, - #name_str, - #from_py_with as fn(_) -> _, - )? - } - } - } else if arg.option_wrapped_type.is_some() { - let holder = holders.push_holder(arg.name.span()); - quote_arg_span! { - #pyo3_path::impl_::extract_argument::extract_optional_argument( - #arg_value, - &mut #holder, - #name_str, - #[allow(clippy::redundant_closure)] - { - || #default - } - )? - } - } else if let Some(default) = default { - let holder = holders.push_holder(arg.name.span()); - quote_arg_span! { - #pyo3_path::impl_::extract_argument::extract_argument_with_default( - #arg_value, - &mut #holder, - #name_str, - #[allow(clippy::redundant_closure)] - { - || #default - } - )? - } - } else { - let holder = holders.push_holder(arg.name.span()); - let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value) }}; - quote_arg_span! { - #pyo3_path::impl_::extract_argument::extract_argument( - #unwrap, - &mut #holder, - #name_str - )? - } - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/pyclass.rs b/vendor/pyo3/pyo3-macros-backend/src/pyclass.rs deleted file mode 100644 index 7697ae4451..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pyclass.rs +++ /dev/null @@ -1,2503 +0,0 @@ -use std::borrow::Cow; -use std::fmt::Debug; - -use proc_macro2::{Ident, Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned, ToTokens}; -use syn::ext::IdentExt; -use syn::parse::{Parse, ParseStream}; -use syn::punctuated::Punctuated; -use syn::{parse_quote, parse_quote_spanned, spanned::Spanned, ImplItemFn, Result, Token}; - -use crate::attributes::kw::frozen; -use crate::attributes::{ - self, kw, take_pyo3_options, CrateAttribute, ErrorCombiner, ExtendsAttribute, - FreelistAttribute, ModuleAttribute, NameAttribute, NameLitStr, RenameAllAttribute, - StrFormatterAttribute, -}; -use crate::konst::{ConstAttributes, ConstSpec}; -use crate::method::{FnArg, FnSpec, PyArg, RegularArg}; -use crate::pyfunction::ConstructorAttribute; -use crate::pyimpl::{gen_py_const, get_cfg_attributes, PyClassMethodsType}; -use crate::pymethod::{ - impl_py_class_attribute, impl_py_getter_def, impl_py_setter_def, MethodAndMethodDef, - MethodAndSlotDef, PropertyType, SlotDef, __GETITEM__, __HASH__, __INT__, __LEN__, __REPR__, - __RICHCMP__, __STR__, -}; -use crate::pyversions::is_abi3_before; -use crate::utils::{self, apply_renaming_rule, Ctx, LitCStr, PythonDoc}; -use crate::PyFunctionOptions; - -/// If the class is derived from a Rust `struct` or `enum`. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub enum PyClassKind { - Struct, - Enum, -} - -/// The parsed arguments of the pyclass macro -#[derive(Clone)] -pub struct PyClassArgs { - pub class_kind: PyClassKind, - pub options: PyClassPyO3Options, -} - -impl PyClassArgs { - fn parse(input: ParseStream<'_>, kind: PyClassKind) -> Result { - Ok(PyClassArgs { - class_kind: kind, - options: PyClassPyO3Options::parse(input)?, - }) - } - - pub fn parse_struct_args(input: ParseStream<'_>) -> syn::Result { - Self::parse(input, PyClassKind::Struct) - } - - pub fn parse_enum_args(input: ParseStream<'_>) -> syn::Result { - Self::parse(input, PyClassKind::Enum) - } -} - -#[derive(Clone, Default)] -pub struct PyClassPyO3Options { - pub krate: Option, - pub dict: Option, - pub eq: Option, - pub eq_int: Option, - pub extends: Option, - pub get_all: Option, - pub freelist: Option, - pub frozen: Option, - pub hash: Option, - pub mapping: Option, - pub module: Option, - pub name: Option, - pub ord: Option, - pub rename_all: Option, - pub sequence: Option, - pub set_all: Option, - pub str: Option, - pub subclass: Option, - pub unsendable: Option, - pub weakref: Option, -} - -pub enum PyClassPyO3Option { - Crate(CrateAttribute), - Dict(kw::dict), - Eq(kw::eq), - EqInt(kw::eq_int), - Extends(ExtendsAttribute), - Freelist(FreelistAttribute), - Frozen(kw::frozen), - GetAll(kw::get_all), - Hash(kw::hash), - Mapping(kw::mapping), - Module(ModuleAttribute), - Name(NameAttribute), - Ord(kw::ord), - RenameAll(RenameAllAttribute), - Sequence(kw::sequence), - SetAll(kw::set_all), - Str(StrFormatterAttribute), - Subclass(kw::subclass), - Unsendable(kw::unsendable), - Weakref(kw::weakref), -} - -impl Parse for PyClassPyO3Option { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(Token![crate]) { - input.parse().map(PyClassPyO3Option::Crate) - } else if lookahead.peek(kw::dict) { - input.parse().map(PyClassPyO3Option::Dict) - } else if lookahead.peek(kw::eq) { - input.parse().map(PyClassPyO3Option::Eq) - } else if lookahead.peek(kw::eq_int) { - input.parse().map(PyClassPyO3Option::EqInt) - } else if lookahead.peek(kw::extends) { - input.parse().map(PyClassPyO3Option::Extends) - } else if lookahead.peek(attributes::kw::freelist) { - input.parse().map(PyClassPyO3Option::Freelist) - } else if lookahead.peek(attributes::kw::frozen) { - input.parse().map(PyClassPyO3Option::Frozen) - } else if lookahead.peek(attributes::kw::get_all) { - input.parse().map(PyClassPyO3Option::GetAll) - } else if lookahead.peek(attributes::kw::hash) { - input.parse().map(PyClassPyO3Option::Hash) - } else if lookahead.peek(attributes::kw::mapping) { - input.parse().map(PyClassPyO3Option::Mapping) - } else if lookahead.peek(attributes::kw::module) { - input.parse().map(PyClassPyO3Option::Module) - } else if lookahead.peek(kw::name) { - input.parse().map(PyClassPyO3Option::Name) - } else if lookahead.peek(attributes::kw::ord) { - input.parse().map(PyClassPyO3Option::Ord) - } else if lookahead.peek(kw::rename_all) { - input.parse().map(PyClassPyO3Option::RenameAll) - } else if lookahead.peek(attributes::kw::sequence) { - input.parse().map(PyClassPyO3Option::Sequence) - } else if lookahead.peek(attributes::kw::set_all) { - input.parse().map(PyClassPyO3Option::SetAll) - } else if lookahead.peek(attributes::kw::str) { - input.parse().map(PyClassPyO3Option::Str) - } else if lookahead.peek(attributes::kw::subclass) { - input.parse().map(PyClassPyO3Option::Subclass) - } else if lookahead.peek(attributes::kw::unsendable) { - input.parse().map(PyClassPyO3Option::Unsendable) - } else if lookahead.peek(attributes::kw::weakref) { - input.parse().map(PyClassPyO3Option::Weakref) - } else { - Err(lookahead.error()) - } - } -} - -impl Parse for PyClassPyO3Options { - fn parse(input: ParseStream<'_>) -> syn::Result { - let mut options: PyClassPyO3Options = Default::default(); - - for option in Punctuated::::parse_terminated(input)? { - options.set_option(option)?; - } - - Ok(options) - } -} - -impl PyClassPyO3Options { - pub fn take_pyo3_options(&mut self, attrs: &mut Vec) -> syn::Result<()> { - take_pyo3_options(attrs)? - .into_iter() - .try_for_each(|option| self.set_option(option)) - } - - fn set_option(&mut self, option: PyClassPyO3Option) -> syn::Result<()> { - macro_rules! set_option { - ($key:ident) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once") - ); - self.$key = Some($key); - } - }; - } - - match option { - PyClassPyO3Option::Crate(krate) => set_option!(krate), - PyClassPyO3Option::Dict(dict) => { - ensure_spanned!( - !is_abi3_before(3, 9), - dict.span() => "`dict` requires Python >= 3.9 when using the `abi3` feature" - ); - set_option!(dict); - } - PyClassPyO3Option::Eq(eq) => set_option!(eq), - PyClassPyO3Option::EqInt(eq_int) => set_option!(eq_int), - PyClassPyO3Option::Extends(extends) => set_option!(extends), - PyClassPyO3Option::Freelist(freelist) => set_option!(freelist), - PyClassPyO3Option::Frozen(frozen) => set_option!(frozen), - PyClassPyO3Option::GetAll(get_all) => set_option!(get_all), - PyClassPyO3Option::Hash(hash) => set_option!(hash), - PyClassPyO3Option::Mapping(mapping) => set_option!(mapping), - PyClassPyO3Option::Module(module) => set_option!(module), - PyClassPyO3Option::Name(name) => set_option!(name), - PyClassPyO3Option::Ord(ord) => set_option!(ord), - PyClassPyO3Option::RenameAll(rename_all) => set_option!(rename_all), - PyClassPyO3Option::Sequence(sequence) => set_option!(sequence), - PyClassPyO3Option::SetAll(set_all) => set_option!(set_all), - PyClassPyO3Option::Str(str) => set_option!(str), - PyClassPyO3Option::Subclass(subclass) => set_option!(subclass), - PyClassPyO3Option::Unsendable(unsendable) => set_option!(unsendable), - PyClassPyO3Option::Weakref(weakref) => { - ensure_spanned!( - !is_abi3_before(3, 9), - weakref.span() => "`weakref` requires Python >= 3.9 when using the `abi3` feature" - ); - set_option!(weakref); - } - } - Ok(()) - } -} - -pub fn build_py_class( - class: &mut syn::ItemStruct, - mut args: PyClassArgs, - methods_type: PyClassMethodsType, -) -> syn::Result { - args.options.take_pyo3_options(&mut class.attrs)?; - - let ctx = &Ctx::new(&args.options.krate, None); - let doc = utils::get_doc(&class.attrs, None, ctx); - - if let Some(lt) = class.generics.lifetimes().next() { - bail_spanned!( - lt.span() => concat!( - "#[pyclass] cannot have lifetime parameters. For an explanation, see \ - https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#no-lifetime-parameters" - ) - ); - } - - ensure_spanned!( - class.generics.params.is_empty(), - class.generics.span() => concat!( - "#[pyclass] cannot have generic parameters. For an explanation, see \ - https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#no-generic-parameters" - ) - ); - - let mut all_errors = ErrorCombiner(None); - - let mut field_options: Vec<(&syn::Field, FieldPyO3Options)> = match &mut class.fields { - syn::Fields::Named(fields) => fields - .named - .iter_mut() - .filter_map( - |field| match FieldPyO3Options::take_pyo3_options(&mut field.attrs) { - Ok(options) => Some((&*field, options)), - Err(e) => { - all_errors.combine(e); - None - } - }, - ) - .collect::>(), - syn::Fields::Unnamed(fields) => fields - .unnamed - .iter_mut() - .filter_map( - |field| match FieldPyO3Options::take_pyo3_options(&mut field.attrs) { - Ok(options) => Some((&*field, options)), - Err(e) => { - all_errors.combine(e); - None - } - }, - ) - .collect::>(), - syn::Fields::Unit => { - if let Some(attr) = args.options.set_all { - return Err(syn::Error::new_spanned(attr, UNIT_SET)); - }; - if let Some(attr) = args.options.get_all { - return Err(syn::Error::new_spanned(attr, UNIT_GET)); - }; - // No fields for unit struct - Vec::new() - } - }; - - all_errors.ensure_empty()?; - - if let Some(attr) = args.options.get_all { - for (_, FieldPyO3Options { get, .. }) in &mut field_options { - if let Some(old_get) = get.replace(Annotated::Struct(attr)) { - return Err(syn::Error::new(old_get.span(), DUPE_GET)); - } - } - } - - if let Some(attr) = args.options.set_all { - for (_, FieldPyO3Options { set, .. }) in &mut field_options { - if let Some(old_set) = set.replace(Annotated::Struct(attr)) { - return Err(syn::Error::new(old_set.span(), DUPE_SET)); - } - } - } - - impl_class(&class.ident, &args, doc, field_options, methods_type, ctx) -} - -enum Annotated { - Field(X), - Struct(Y), -} - -impl Annotated { - fn span(&self) -> Span { - match self { - Self::Field(x) => x.span(), - Self::Struct(y) => y.span(), - } - } -} - -/// `#[pyo3()]` options for pyclass fields -struct FieldPyO3Options { - get: Option>, - set: Option>, - name: Option, -} - -enum FieldPyO3Option { - Get(attributes::kw::get), - Set(attributes::kw::set), - Name(NameAttribute), -} - -impl Parse for FieldPyO3Option { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::get) { - input.parse().map(FieldPyO3Option::Get) - } else if lookahead.peek(attributes::kw::set) { - input.parse().map(FieldPyO3Option::Set) - } else if lookahead.peek(attributes::kw::name) { - input.parse().map(FieldPyO3Option::Name) - } else { - Err(lookahead.error()) - } - } -} - -impl FieldPyO3Options { - fn take_pyo3_options(attrs: &mut Vec) -> Result { - let mut options = FieldPyO3Options { - get: None, - set: None, - name: None, - }; - - for option in take_pyo3_options(attrs)? { - match option { - FieldPyO3Option::Get(kw) => { - if options.get.replace(Annotated::Field(kw)).is_some() { - return Err(syn::Error::new(kw.span(), UNIQUE_GET)); - } - } - FieldPyO3Option::Set(kw) => { - if options.set.replace(Annotated::Field(kw)).is_some() { - return Err(syn::Error::new(kw.span(), UNIQUE_SET)); - } - } - FieldPyO3Option::Name(name) => { - if options.name.replace(name).is_some() { - return Err(syn::Error::new(options.name.span(), UNIQUE_NAME)); - } - } - } - } - - Ok(options) - } -} - -fn get_class_python_name<'a>(cls: &'a syn::Ident, args: &'a PyClassArgs) -> Cow<'a, syn::Ident> { - args.options - .name - .as_ref() - .map(|name_attr| Cow::Borrowed(&name_attr.value.0)) - .unwrap_or_else(|| Cow::Owned(cls.unraw())) -} - -fn impl_class( - cls: &syn::Ident, - args: &PyClassArgs, - doc: PythonDoc, - field_options: Vec<(&syn::Field, FieldPyO3Options)>, - methods_type: PyClassMethodsType, - ctx: &Ctx, -) -> syn::Result { - let Ctx { pyo3_path, .. } = ctx; - let pytypeinfo_impl = impl_pytypeinfo(cls, args, ctx); - - if let Some(str) = &args.options.str { - if str.value.is_some() { - // check if any renaming is present - let no_naming_conflict = field_options.iter().all(|x| x.1.name.is_none()) - & args.options.name.is_none() - & args.options.rename_all.is_none(); - ensure_spanned!(no_naming_conflict, str.value.span() => "The format string syntax is incompatible with any renaming via `name` or `rename_all`"); - } - } - - let (default_str, default_str_slot) = - implement_pyclass_str(&args.options, &syn::parse_quote!(#cls), ctx); - - let (default_richcmp, default_richcmp_slot) = - pyclass_richcmp(&args.options, &syn::parse_quote!(#cls), ctx)?; - - let (default_hash, default_hash_slot) = - pyclass_hash(&args.options, &syn::parse_quote!(#cls), ctx)?; - - let mut slots = Vec::new(); - slots.extend(default_richcmp_slot); - slots.extend(default_hash_slot); - slots.extend(default_str_slot); - - let py_class_impl = PyClassImplsBuilder::new( - cls, - args, - methods_type, - descriptors_to_items( - cls, - args.options.rename_all.as_ref(), - args.options.frozen, - field_options, - ctx, - )?, - slots, - ) - .doc(doc) - .impl_all(ctx)?; - - Ok(quote! { - impl #pyo3_path::types::DerefToPyAny for #cls {} - - #pytypeinfo_impl - - #py_class_impl - - #[doc(hidden)] - #[allow(non_snake_case)] - impl #cls { - #default_richcmp - #default_hash - #default_str - } - }) -} - -enum PyClassEnum<'a> { - Simple(PyClassSimpleEnum<'a>), - Complex(PyClassComplexEnum<'a>), -} - -impl<'a> PyClassEnum<'a> { - fn new(enum_: &'a mut syn::ItemEnum) -> syn::Result { - let has_only_unit_variants = enum_ - .variants - .iter() - .all(|variant| matches!(variant.fields, syn::Fields::Unit)); - - Ok(if has_only_unit_variants { - let simple_enum = PyClassSimpleEnum::new(enum_)?; - Self::Simple(simple_enum) - } else { - let complex_enum = PyClassComplexEnum::new(enum_)?; - Self::Complex(complex_enum) - }) - } -} - -pub fn build_py_enum( - enum_: &mut syn::ItemEnum, - mut args: PyClassArgs, - method_type: PyClassMethodsType, -) -> syn::Result { - args.options.take_pyo3_options(&mut enum_.attrs)?; - - let ctx = &Ctx::new(&args.options.krate, None); - if let Some(extends) = &args.options.extends { - bail_spanned!(extends.span() => "enums can't extend from other classes"); - } else if let Some(subclass) = &args.options.subclass { - bail_spanned!(subclass.span() => "enums can't be inherited by other classes"); - } else if enum_.variants.is_empty() { - bail_spanned!(enum_.brace_token.span.join() => "#[pyclass] can't be used on enums without any variants"); - } - - let doc = utils::get_doc(&enum_.attrs, None, ctx); - let enum_ = PyClassEnum::new(enum_)?; - impl_enum(enum_, &args, doc, method_type, ctx) -} - -struct PyClassSimpleEnum<'a> { - ident: &'a syn::Ident, - // The underlying #[repr] of the enum, used to implement __int__ and __richcmp__. - // This matters when the underlying representation may not fit in `isize`. - repr_type: syn::Ident, - variants: Vec>, -} - -impl<'a> PyClassSimpleEnum<'a> { - fn new(enum_: &'a mut syn::ItemEnum) -> syn::Result { - fn is_numeric_type(t: &syn::Ident) -> bool { - [ - "u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64", "u128", "i128", "usize", - "isize", - ] - .iter() - .any(|&s| t == s) - } - - fn extract_unit_variant_data( - variant: &mut syn::Variant, - ) -> syn::Result> { - use syn::Fields; - let ident = match &variant.fields { - Fields::Unit => &variant.ident, - _ => bail_spanned!(variant.span() => "Must be a unit variant."), - }; - let options = EnumVariantPyO3Options::take_pyo3_options(&mut variant.attrs)?; - let cfg_attrs = get_cfg_attributes(&variant.attrs); - Ok(PyClassEnumUnitVariant { - ident, - options, - cfg_attrs, - }) - } - - let ident = &enum_.ident; - - // According to the [reference](https://doc.rust-lang.org/reference/items/enumerations.html), - // "Under the default representation, the specified discriminant is interpreted as an isize - // value", so `isize` should be enough by default. - let mut repr_type = syn::Ident::new("isize", proc_macro2::Span::call_site()); - if let Some(attr) = enum_.attrs.iter().find(|attr| attr.path().is_ident("repr")) { - let args = - attr.parse_args_with(Punctuated::::parse_terminated)?; - if let Some(ident) = args - .into_iter() - .filter_map(|ts| syn::parse2::(ts).ok()) - .find(is_numeric_type) - { - repr_type = ident; - } - } - - let variants: Vec<_> = enum_ - .variants - .iter_mut() - .map(extract_unit_variant_data) - .collect::>()?; - Ok(Self { - ident, - repr_type, - variants, - }) - } -} - -struct PyClassComplexEnum<'a> { - ident: &'a syn::Ident, - variants: Vec>, -} - -impl<'a> PyClassComplexEnum<'a> { - fn new(enum_: &'a mut syn::ItemEnum) -> syn::Result { - let witness = enum_ - .variants - .iter() - .find(|variant| !matches!(variant.fields, syn::Fields::Unit)) - .expect("complex enum has a non-unit variant") - .ident - .to_owned(); - - let extract_variant_data = - |variant: &'a mut syn::Variant| -> syn::Result> { - use syn::Fields; - let ident = &variant.ident; - let options = EnumVariantPyO3Options::take_pyo3_options(&mut variant.attrs)?; - - let variant = match &variant.fields { - Fields::Unit => { - bail_spanned!(variant.span() => format!( - "Unit variant `{ident}` is not yet supported in a complex enum\n\ - = help: change to an empty tuple variant instead: `{ident}()`\n\ - = note: the enum is complex because of non-unit variant `{witness}`", - ident=ident, witness=witness)) - } - Fields::Named(fields) => { - let fields = fields - .named - .iter() - .map(|field| PyClassEnumVariantNamedField { - ident: field.ident.as_ref().expect("named field has an identifier"), - ty: &field.ty, - span: field.span(), - }) - .collect(); - - PyClassEnumVariant::Struct(PyClassEnumStructVariant { - ident, - fields, - options, - }) - } - Fields::Unnamed(types) => { - let fields = types - .unnamed - .iter() - .map(|field| PyClassEnumVariantUnnamedField { - ty: &field.ty, - span: field.span(), - }) - .collect(); - - PyClassEnumVariant::Tuple(PyClassEnumTupleVariant { - ident, - fields, - options, - }) - } - }; - - Ok(variant) - }; - - let ident = &enum_.ident; - - let variants: Vec<_> = enum_ - .variants - .iter_mut() - .map(extract_variant_data) - .collect::>()?; - - Ok(Self { ident, variants }) - } -} - -enum PyClassEnumVariant<'a> { - // TODO(mkovaxx): Unit(PyClassEnumUnitVariant<'a>), - Struct(PyClassEnumStructVariant<'a>), - Tuple(PyClassEnumTupleVariant<'a>), -} - -trait EnumVariant { - fn get_ident(&self) -> &syn::Ident; - fn get_options(&self) -> &EnumVariantPyO3Options; - - fn get_python_name(&self, args: &PyClassArgs) -> Cow<'_, syn::Ident> { - self.get_options() - .name - .as_ref() - .map(|name_attr| Cow::Borrowed(&name_attr.value.0)) - .unwrap_or_else(|| { - let name = self.get_ident().unraw(); - if let Some(attr) = &args.options.rename_all { - let new_name = apply_renaming_rule(attr.value.rule, &name.to_string()); - Cow::Owned(Ident::new(&new_name, Span::call_site())) - } else { - Cow::Owned(name) - } - }) - } -} - -impl EnumVariant for PyClassEnumVariant<'_> { - fn get_ident(&self) -> &syn::Ident { - match self { - PyClassEnumVariant::Struct(struct_variant) => struct_variant.ident, - PyClassEnumVariant::Tuple(tuple_variant) => tuple_variant.ident, - } - } - - fn get_options(&self) -> &EnumVariantPyO3Options { - match self { - PyClassEnumVariant::Struct(struct_variant) => &struct_variant.options, - PyClassEnumVariant::Tuple(tuple_variant) => &tuple_variant.options, - } - } -} - -/// A unit variant has no fields -struct PyClassEnumUnitVariant<'a> { - ident: &'a syn::Ident, - options: EnumVariantPyO3Options, - cfg_attrs: Vec<&'a syn::Attribute>, -} - -impl EnumVariant for PyClassEnumUnitVariant<'_> { - fn get_ident(&self) -> &syn::Ident { - self.ident - } - - fn get_options(&self) -> &EnumVariantPyO3Options { - &self.options - } -} - -/// A struct variant has named fields -struct PyClassEnumStructVariant<'a> { - ident: &'a syn::Ident, - fields: Vec>, - options: EnumVariantPyO3Options, -} - -struct PyClassEnumTupleVariant<'a> { - ident: &'a syn::Ident, - fields: Vec>, - options: EnumVariantPyO3Options, -} - -struct PyClassEnumVariantNamedField<'a> { - ident: &'a syn::Ident, - ty: &'a syn::Type, - span: Span, -} - -struct PyClassEnumVariantUnnamedField<'a> { - ty: &'a syn::Type, - span: Span, -} - -/// `#[pyo3()]` options for pyclass enum variants -#[derive(Clone, Default)] -struct EnumVariantPyO3Options { - name: Option, - constructor: Option, -} - -enum EnumVariantPyO3Option { - Name(NameAttribute), - Constructor(ConstructorAttribute), -} - -impl Parse for EnumVariantPyO3Option { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::name) { - input.parse().map(EnumVariantPyO3Option::Name) - } else if lookahead.peek(attributes::kw::constructor) { - input.parse().map(EnumVariantPyO3Option::Constructor) - } else { - Err(lookahead.error()) - } - } -} - -impl EnumVariantPyO3Options { - fn take_pyo3_options(attrs: &mut Vec) -> Result { - let mut options = EnumVariantPyO3Options::default(); - - take_pyo3_options(attrs)? - .into_iter() - .try_for_each(|option| options.set_option(option))?; - - Ok(options) - } - - fn set_option(&mut self, option: EnumVariantPyO3Option) -> syn::Result<()> { - macro_rules! set_option { - ($key:ident) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once") - ); - self.$key = Some($key); - } - }; - } - - match option { - EnumVariantPyO3Option::Constructor(constructor) => set_option!(constructor), - EnumVariantPyO3Option::Name(name) => set_option!(name), - } - Ok(()) - } -} - -// todo(remove this dead code allowance once __repr__ is implemented -#[allow(dead_code)] -pub enum PyFmtName { - Str, - Repr, -} - -fn implement_py_formatting( - ty: &syn::Type, - ctx: &Ctx, - option: &StrFormatterAttribute, -) -> (ImplItemFn, MethodAndSlotDef) { - let mut fmt_impl = match &option.value { - Some(opt) => { - let fmt = &opt.fmt; - let args = &opt - .args - .iter() - .map(|member| quote! {self.#member}) - .collect::>(); - let fmt_impl: ImplItemFn = syn::parse_quote! { - fn __pyo3__generated____str__(&self) -> ::std::string::String { - ::std::format!(#fmt, #(#args, )*) - } - }; - fmt_impl - } - None => { - let fmt_impl: syn::ImplItemFn = syn::parse_quote! { - fn __pyo3__generated____str__(&self) -> ::std::string::String { - ::std::format!("{}", &self) - } - }; - fmt_impl - } - }; - let fmt_slot = generate_protocol_slot(ty, &mut fmt_impl, &__STR__, "__str__", ctx).unwrap(); - (fmt_impl, fmt_slot) -} - -fn implement_pyclass_str( - options: &PyClassPyO3Options, - ty: &syn::Type, - ctx: &Ctx, -) -> (Option, Option) { - match &options.str { - Some(option) => { - let (default_str, default_str_slot) = implement_py_formatting(ty, ctx, option); - (Some(default_str), Some(default_str_slot)) - } - _ => (None, None), - } -} - -fn impl_enum( - enum_: PyClassEnum<'_>, - args: &PyClassArgs, - doc: PythonDoc, - methods_type: PyClassMethodsType, - ctx: &Ctx, -) -> Result { - if let Some(str_fmt) = &args.options.str { - ensure_spanned!(str_fmt.value.is_none(), str_fmt.value.span() => "The format string syntax cannot be used with enums") - } - - match enum_ { - PyClassEnum::Simple(simple_enum) => { - impl_simple_enum(simple_enum, args, doc, methods_type, ctx) - } - PyClassEnum::Complex(complex_enum) => { - impl_complex_enum(complex_enum, args, doc, methods_type, ctx) - } - } -} - -fn impl_simple_enum( - simple_enum: PyClassSimpleEnum<'_>, - args: &PyClassArgs, - doc: PythonDoc, - methods_type: PyClassMethodsType, - ctx: &Ctx, -) -> Result { - let cls = simple_enum.ident; - let ty: syn::Type = syn::parse_quote!(#cls); - let variants = simple_enum.variants; - let pytypeinfo = impl_pytypeinfo(cls, args, ctx); - - for variant in &variants { - ensure_spanned!(variant.options.constructor.is_none(), variant.options.constructor.span() => "`constructor` can't be used on a simple enum variant"); - } - - let variant_cfg_check = generate_cfg_check(&variants, cls); - - let (default_repr, default_repr_slot) = { - let variants_repr = variants.iter().map(|variant| { - let variant_name = variant.ident; - let cfg_attrs = &variant.cfg_attrs; - // Assuming all variants are unit variants because they are the only type we support. - let repr = format!( - "{}.{}", - get_class_python_name(cls, args), - variant.get_python_name(args), - ); - quote! { #(#cfg_attrs)* #cls::#variant_name => #repr, } - }); - let mut repr_impl: syn::ImplItemFn = syn::parse_quote! { - fn __pyo3__repr__(&self) -> &'static str { - match *self { - #(#variants_repr)* - } - } - }; - let repr_slot = - generate_default_protocol_slot(&ty, &mut repr_impl, &__REPR__, ctx).unwrap(); - (repr_impl, repr_slot) - }; - - let (default_str, default_str_slot) = implement_pyclass_str(&args.options, &ty, ctx); - - let repr_type = &simple_enum.repr_type; - - let (default_int, default_int_slot) = { - // This implementation allows us to convert &T to #repr_type without implementing `Copy` - let variants_to_int = variants.iter().map(|variant| { - let variant_name = variant.ident; - let cfg_attrs = &variant.cfg_attrs; - quote! { #(#cfg_attrs)* #cls::#variant_name => #cls::#variant_name as #repr_type, } - }); - let mut int_impl: syn::ImplItemFn = syn::parse_quote! { - fn __pyo3__int__(&self) -> #repr_type { - match *self { - #(#variants_to_int)* - } - } - }; - let int_slot = generate_default_protocol_slot(&ty, &mut int_impl, &__INT__, ctx).unwrap(); - (int_impl, int_slot) - }; - - let (default_richcmp, default_richcmp_slot) = - pyclass_richcmp_simple_enum(&args.options, &ty, repr_type, ctx)?; - let (default_hash, default_hash_slot) = pyclass_hash(&args.options, &ty, ctx)?; - - let mut default_slots = vec![default_repr_slot, default_int_slot]; - default_slots.extend(default_richcmp_slot); - default_slots.extend(default_hash_slot); - default_slots.extend(default_str_slot); - - let pyclass_impls = PyClassImplsBuilder::new( - cls, - args, - methods_type, - simple_enum_default_methods( - cls, - variants - .iter() - .map(|v| (v.ident, v.get_python_name(args), &v.cfg_attrs)), - ctx, - ), - default_slots, - ) - .doc(doc) - .impl_all(ctx)?; - - Ok(quote! { - #variant_cfg_check - - #pytypeinfo - - #pyclass_impls - - #[doc(hidden)] - #[allow(non_snake_case)] - impl #cls { - #default_repr - #default_int - #default_richcmp - #default_hash - #default_str - } - }) -} - -fn impl_complex_enum( - complex_enum: PyClassComplexEnum<'_>, - args: &PyClassArgs, - doc: PythonDoc, - methods_type: PyClassMethodsType, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let cls = complex_enum.ident; - let ty: syn::Type = syn::parse_quote!(#cls); - - // Need to rig the enum PyClass options - let args = { - let mut rigged_args = args.clone(); - // Needs to be frozen to disallow `&mut self` methods, which could break a runtime invariant - rigged_args.options.frozen = parse_quote!(frozen); - // Needs to be subclassable by the variant PyClasses - rigged_args.options.subclass = parse_quote!(subclass); - rigged_args - }; - - let ctx = &Ctx::new(&args.options.krate, None); - let cls = complex_enum.ident; - let variants = complex_enum.variants; - let pytypeinfo = impl_pytypeinfo(cls, &args, ctx); - - let (default_richcmp, default_richcmp_slot) = pyclass_richcmp(&args.options, &ty, ctx)?; - let (default_hash, default_hash_slot) = pyclass_hash(&args.options, &ty, ctx)?; - - let (default_str, default_str_slot) = implement_pyclass_str(&args.options, &ty, ctx); - - let mut default_slots = vec![]; - default_slots.extend(default_richcmp_slot); - default_slots.extend(default_hash_slot); - default_slots.extend(default_str_slot); - - let impl_builder = PyClassImplsBuilder::new( - cls, - &args, - methods_type, - complex_enum_default_methods( - cls, - variants - .iter() - .map(|v| (v.get_ident(), v.get_python_name(&args))), - ctx, - ), - default_slots, - ) - .doc(doc); - - // Need to customize the into_py impl so that it returns the variant PyClass - let enum_into_py_impl = { - let match_arms: Vec = variants - .iter() - .map(|variant| { - let variant_ident = variant.get_ident(); - let variant_cls = gen_complex_enum_variant_class_ident(cls, variant.get_ident()); - quote! { - #cls::#variant_ident { .. } => { - let pyclass_init = <#pyo3_path::PyClassInitializer as ::std::convert::From>::from(self).add_subclass(#variant_cls); - let variant_value = #pyo3_path::Py::new(py, pyclass_init).unwrap(); - #pyo3_path::IntoPy::into_py(variant_value, py) - } - } - }) - .collect(); - - quote! { - #[allow(deprecated)] - impl #pyo3_path::IntoPy<#pyo3_path::PyObject> for #cls { - fn into_py(self, py: #pyo3_path::Python) -> #pyo3_path::PyObject { - match self { - #(#match_arms)* - } - } - } - } - }; - - let enum_into_pyobject_impl = { - let match_arms = variants - .iter() - .map(|variant| { - let variant_ident = variant.get_ident(); - let variant_cls = gen_complex_enum_variant_class_ident(cls, variant.get_ident()); - quote! { - #cls::#variant_ident { .. } => { - let pyclass_init = <#pyo3_path::PyClassInitializer as ::std::convert::From>::from(self).add_subclass(#variant_cls); - unsafe { #pyo3_path::Bound::new(py, pyclass_init).map(|b| #pyo3_path::types::PyAnyMethods::downcast_into_unchecked(b.into_any())) } - } - } - }); - - quote! { - impl<'py> #pyo3_path::conversion::IntoPyObject<'py> for #cls { - type Target = Self; - type Output = #pyo3_path::Bound<'py, >::Target>; - type Error = #pyo3_path::PyErr; - - fn into_pyobject(self, py: #pyo3_path::Python<'py>) -> ::std::result::Result< - ::Output, - ::Error, - > { - match self { - #(#match_arms)* - } - } - } - } - }; - - let pyclass_impls: TokenStream = [ - impl_builder.impl_pyclass(ctx), - impl_builder.impl_extractext(ctx), - enum_into_py_impl, - enum_into_pyobject_impl, - impl_builder.impl_pyclassimpl(ctx)?, - impl_builder.impl_add_to_module(ctx), - impl_builder.impl_freelist(ctx), - ] - .into_iter() - .collect(); - - let mut variant_cls_zsts = vec![]; - let mut variant_cls_pytypeinfos = vec![]; - let mut variant_cls_pyclass_impls = vec![]; - let mut variant_cls_impls = vec![]; - for variant in variants { - let variant_cls = gen_complex_enum_variant_class_ident(cls, variant.get_ident()); - - let variant_cls_zst = quote! { - #[doc(hidden)] - #[allow(non_camel_case_types)] - struct #variant_cls; - }; - variant_cls_zsts.push(variant_cls_zst); - - let variant_args = PyClassArgs { - class_kind: PyClassKind::Struct, - // TODO(mkovaxx): propagate variant.options - options: { - let mut rigged_options: PyClassPyO3Options = parse_quote!(extends = #cls, frozen); - // If a specific module was given to the base class, use it for all variants. - rigged_options.module.clone_from(&args.options.module); - rigged_options - }, - }; - - let variant_cls_pytypeinfo = impl_pytypeinfo(&variant_cls, &variant_args, ctx); - variant_cls_pytypeinfos.push(variant_cls_pytypeinfo); - - let (variant_cls_impl, field_getters, mut slots) = - impl_complex_enum_variant_cls(cls, &variant, ctx)?; - variant_cls_impls.push(variant_cls_impl); - - let variant_new = complex_enum_variant_new(cls, variant, ctx)?; - slots.push(variant_new); - - let pyclass_impl = PyClassImplsBuilder::new( - &variant_cls, - &variant_args, - methods_type, - field_getters, - slots, - ) - .impl_all(ctx)?; - - variant_cls_pyclass_impls.push(pyclass_impl); - } - - Ok(quote! { - #pytypeinfo - - #pyclass_impls - - #[doc(hidden)] - #[allow(non_snake_case)] - impl #cls { - #default_richcmp - #default_hash - #default_str - } - - #(#variant_cls_zsts)* - - #(#variant_cls_pytypeinfos)* - - #(#variant_cls_pyclass_impls)* - - #(#variant_cls_impls)* - }) -} - -fn impl_complex_enum_variant_cls( - enum_name: &syn::Ident, - variant: &PyClassEnumVariant<'_>, - ctx: &Ctx, -) -> Result<(TokenStream, Vec, Vec)> { - match variant { - PyClassEnumVariant::Struct(struct_variant) => { - impl_complex_enum_struct_variant_cls(enum_name, struct_variant, ctx) - } - PyClassEnumVariant::Tuple(tuple_variant) => { - impl_complex_enum_tuple_variant_cls(enum_name, tuple_variant, ctx) - } - } -} - -fn impl_complex_enum_variant_match_args( - ctx @ Ctx { pyo3_path, .. }: &Ctx, - variant_cls_type: &syn::Type, - field_names: &mut Vec, -) -> syn::Result<(MethodAndMethodDef, syn::ImplItemFn)> { - let ident = format_ident!("__match_args__"); - let mut match_args_impl: syn::ImplItemFn = { - parse_quote! { - #[classattr] - fn #ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::Bound<'_, #pyo3_path::types::PyTuple>> { - #pyo3_path::types::PyTuple::new::<&str, _>(py, [ - #(stringify!(#field_names),)* - ]) - } - } - }; - - let spec = FnSpec::parse( - &mut match_args_impl.sig, - &mut match_args_impl.attrs, - Default::default(), - )?; - let variant_match_args = impl_py_class_attribute(variant_cls_type, &spec, ctx)?; - - Ok((variant_match_args, match_args_impl)) -} - -fn impl_complex_enum_struct_variant_cls( - enum_name: &syn::Ident, - variant: &PyClassEnumStructVariant<'_>, - ctx: &Ctx, -) -> Result<(TokenStream, Vec, Vec)> { - let Ctx { pyo3_path, .. } = ctx; - let variant_ident = &variant.ident; - let variant_cls = gen_complex_enum_variant_class_ident(enum_name, variant.ident); - let variant_cls_type = parse_quote!(#variant_cls); - - let mut field_names: Vec = vec![]; - let mut fields_with_types: Vec = vec![]; - let mut field_getters = vec![]; - let mut field_getter_impls: Vec = vec![]; - for field in &variant.fields { - let field_name = field.ident; - let field_type = field.ty; - let field_with_type = quote! { #field_name: #field_type }; - - let field_getter = - complex_enum_variant_field_getter(&variant_cls_type, field_name, field.span, ctx)?; - - let field_getter_impl = quote! { - fn #field_name(slf: #pyo3_path::PyRef) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - #[allow(unused_imports)] - use #pyo3_path::impl_::pyclass::Probe; - let py = slf.py(); - match &*slf.into_super() { - #enum_name::#variant_ident { #field_name, .. } => - #pyo3_path::impl_::pyclass::ConvertField::< - { #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#field_type>::VALUE }, - { #pyo3_path::impl_::pyclass::IsIntoPyObject::<#field_type>::VALUE }, - >::convert_field::<#field_type>(#field_name, py), - _ => ::core::unreachable!("Wrong complex enum variant found in variant wrapper PyClass"), - } - } - }; - - field_names.push(field_name.clone()); - fields_with_types.push(field_with_type); - field_getters.push(field_getter); - field_getter_impls.push(field_getter_impl); - } - - let (variant_match_args, match_args_const_impl) = - impl_complex_enum_variant_match_args(ctx, &variant_cls_type, &mut field_names)?; - - field_getters.push(variant_match_args); - - let cls_impl = quote! { - #[doc(hidden)] - #[allow(non_snake_case)] - impl #variant_cls { - #[allow(clippy::too_many_arguments)] - fn __pymethod_constructor__(py: #pyo3_path::Python<'_>, #(#fields_with_types,)*) -> #pyo3_path::PyClassInitializer<#variant_cls> { - let base_value = #enum_name::#variant_ident { #(#field_names,)* }; - <#pyo3_path::PyClassInitializer<#enum_name> as ::std::convert::From<#enum_name>>::from(base_value).add_subclass(#variant_cls) - } - - #match_args_const_impl - - #(#field_getter_impls)* - } - }; - - Ok((cls_impl, field_getters, Vec::new())) -} - -fn impl_complex_enum_tuple_variant_field_getters( - ctx: &Ctx, - variant: &PyClassEnumTupleVariant<'_>, - enum_name: &syn::Ident, - variant_cls_type: &syn::Type, - variant_ident: &&Ident, - field_names: &mut Vec, - fields_types: &mut Vec, -) -> Result<(Vec, Vec)> { - let Ctx { pyo3_path, .. } = ctx; - - let mut field_getters = vec![]; - let mut field_getter_impls = vec![]; - - for (index, field) in variant.fields.iter().enumerate() { - let field_name = format_ident!("_{}", index); - let field_type = field.ty; - - let field_getter = - complex_enum_variant_field_getter(variant_cls_type, &field_name, field.span, ctx)?; - - // Generate the match arms needed to destructure the tuple and access the specific field - let field_access_tokens: Vec<_> = (0..variant.fields.len()) - .map(|i| { - if i == index { - quote! { val } - } else { - quote! { _ } - } - }) - .collect(); - let field_getter_impl: syn::ImplItemFn = parse_quote! { - fn #field_name(slf: #pyo3_path::PyRef) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - #[allow(unused_imports)] - use #pyo3_path::impl_::pyclass::Probe; - let py = slf.py(); - match &*slf.into_super() { - #enum_name::#variant_ident ( #(#field_access_tokens), *) => - #pyo3_path::impl_::pyclass::ConvertField::< - { #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#field_type>::VALUE }, - { #pyo3_path::impl_::pyclass::IsIntoPyObject::<#field_type>::VALUE }, - >::convert_field::<#field_type>(val, py), - _ => ::core::unreachable!("Wrong complex enum variant found in variant wrapper PyClass"), - } - } - }; - - field_names.push(field_name); - fields_types.push(field_type.clone()); - field_getters.push(field_getter); - field_getter_impls.push(field_getter_impl); - } - - Ok((field_getters, field_getter_impls)) -} - -fn impl_complex_enum_tuple_variant_len( - ctx: &Ctx, - - variant_cls_type: &syn::Type, - num_fields: usize, -) -> Result<(MethodAndSlotDef, syn::ImplItemFn)> { - let Ctx { pyo3_path, .. } = ctx; - - let mut len_method_impl: syn::ImplItemFn = parse_quote! { - fn __len__(slf: #pyo3_path::PyRef) -> #pyo3_path::PyResult { - ::std::result::Result::Ok(#num_fields) - } - }; - - let variant_len = - generate_default_protocol_slot(variant_cls_type, &mut len_method_impl, &__LEN__, ctx)?; - - Ok((variant_len, len_method_impl)) -} - -fn impl_complex_enum_tuple_variant_getitem( - ctx: &Ctx, - variant_cls: &syn::Ident, - variant_cls_type: &syn::Type, - num_fields: usize, -) -> Result<(MethodAndSlotDef, syn::ImplItemFn)> { - let Ctx { pyo3_path, .. } = ctx; - - let match_arms: Vec<_> = (0..num_fields) - .map(|i| { - let field_access = format_ident!("_{}", i); - quote! { #i => - #pyo3_path::IntoPyObjectExt::into_py_any(#variant_cls::#field_access(slf)?, py) - } - }) - .collect(); - - let mut get_item_method_impl: syn::ImplItemFn = parse_quote! { - fn __getitem__(slf: #pyo3_path::PyRef, idx: usize) -> #pyo3_path::PyResult< #pyo3_path::PyObject> { - let py = slf.py(); - match idx { - #( #match_arms, )* - _ => ::std::result::Result::Err(#pyo3_path::exceptions::PyIndexError::new_err("tuple index out of range")), - } - } - }; - - let variant_getitem = generate_default_protocol_slot( - variant_cls_type, - &mut get_item_method_impl, - &__GETITEM__, - ctx, - )?; - - Ok((variant_getitem, get_item_method_impl)) -} - -fn impl_complex_enum_tuple_variant_cls( - enum_name: &syn::Ident, - variant: &PyClassEnumTupleVariant<'_>, - ctx: &Ctx, -) -> Result<(TokenStream, Vec, Vec)> { - let Ctx { pyo3_path, .. } = ctx; - let variant_ident = &variant.ident; - let variant_cls = gen_complex_enum_variant_class_ident(enum_name, variant.ident); - let variant_cls_type = parse_quote!(#variant_cls); - - let mut slots = vec![]; - - // represents the index of the field - let mut field_names: Vec = vec![]; - let mut field_types: Vec = vec![]; - - let (mut field_getters, field_getter_impls) = impl_complex_enum_tuple_variant_field_getters( - ctx, - variant, - enum_name, - &variant_cls_type, - variant_ident, - &mut field_names, - &mut field_types, - )?; - - let num_fields = variant.fields.len(); - - let (variant_len, len_method_impl) = - impl_complex_enum_tuple_variant_len(ctx, &variant_cls_type, num_fields)?; - - slots.push(variant_len); - - let (variant_getitem, getitem_method_impl) = - impl_complex_enum_tuple_variant_getitem(ctx, &variant_cls, &variant_cls_type, num_fields)?; - - slots.push(variant_getitem); - - let (variant_match_args, match_args_method_impl) = - impl_complex_enum_variant_match_args(ctx, &variant_cls_type, &mut field_names)?; - - field_getters.push(variant_match_args); - - let cls_impl = quote! { - #[doc(hidden)] - #[allow(non_snake_case)] - impl #variant_cls { - #[allow(clippy::too_many_arguments)] - fn __pymethod_constructor__(py: #pyo3_path::Python<'_>, #(#field_names : #field_types,)*) -> #pyo3_path::PyClassInitializer<#variant_cls> { - let base_value = #enum_name::#variant_ident ( #(#field_names,)* ); - <#pyo3_path::PyClassInitializer<#enum_name> as ::std::convert::From<#enum_name>>::from(base_value).add_subclass(#variant_cls) - } - - #len_method_impl - - #getitem_method_impl - - #match_args_method_impl - - #(#field_getter_impls)* - } - }; - - Ok((cls_impl, field_getters, slots)) -} - -fn gen_complex_enum_variant_class_ident(enum_: &syn::Ident, variant: &syn::Ident) -> syn::Ident { - format_ident!("{}_{}", enum_, variant) -} - -fn generate_protocol_slot( - cls: &syn::Type, - method: &mut syn::ImplItemFn, - slot: &SlotDef, - name: &str, - ctx: &Ctx, -) -> syn::Result { - let spec = FnSpec::parse( - &mut method.sig, - &mut Vec::new(), - PyFunctionOptions::default(), - ) - .unwrap(); - slot.generate_type_slot(&syn::parse_quote!(#cls), &spec, name, ctx) -} - -fn generate_default_protocol_slot( - cls: &syn::Type, - method: &mut syn::ImplItemFn, - slot: &SlotDef, - ctx: &Ctx, -) -> syn::Result { - let spec = FnSpec::parse( - &mut method.sig, - &mut Vec::new(), - PyFunctionOptions::default(), - ) - .unwrap(); - let name = spec.name.to_string(); - slot.generate_type_slot( - &syn::parse_quote!(#cls), - &spec, - &format!("__default_{}__", name), - ctx, - ) -} - -fn simple_enum_default_methods<'a>( - cls: &'a syn::Ident, - unit_variant_names: impl IntoIterator< - Item = ( - &'a syn::Ident, - Cow<'a, syn::Ident>, - &'a Vec<&'a syn::Attribute>, - ), - >, - ctx: &Ctx, -) -> Vec { - let cls_type = syn::parse_quote!(#cls); - let variant_to_attribute = |var_ident: &syn::Ident, py_ident: &syn::Ident| ConstSpec { - rust_ident: var_ident.clone(), - attributes: ConstAttributes { - is_class_attr: true, - name: Some(NameAttribute { - kw: syn::parse_quote! { name }, - value: NameLitStr(py_ident.clone()), - }), - }, - }; - unit_variant_names - .into_iter() - .map(|(var, py_name, attrs)| { - let method = gen_py_const(&cls_type, &variant_to_attribute(var, &py_name), ctx); - let associated_method_tokens = method.associated_method; - let method_def_tokens = method.method_def; - - let associated_method = quote! { - #(#attrs)* - #associated_method_tokens - }; - let method_def = quote! { - #(#attrs)* - #method_def_tokens - }; - - MethodAndMethodDef { - associated_method, - method_def, - } - }) - .collect() -} - -fn complex_enum_default_methods<'a>( - cls: &'a syn::Ident, - variant_names: impl IntoIterator)>, - ctx: &Ctx, -) -> Vec { - let cls_type = syn::parse_quote!(#cls); - let variant_to_attribute = |var_ident: &syn::Ident, py_ident: &syn::Ident| ConstSpec { - rust_ident: var_ident.clone(), - attributes: ConstAttributes { - is_class_attr: true, - name: Some(NameAttribute { - kw: syn::parse_quote! { name }, - value: NameLitStr(py_ident.clone()), - }), - }, - }; - variant_names - .into_iter() - .map(|(var, py_name)| { - gen_complex_enum_variant_attr(cls, &cls_type, &variant_to_attribute(var, &py_name), ctx) - }) - .collect() -} - -pub fn gen_complex_enum_variant_attr( - cls: &syn::Ident, - cls_type: &syn::Type, - spec: &ConstSpec, - ctx: &Ctx, -) -> MethodAndMethodDef { - let Ctx { pyo3_path, .. } = ctx; - let member = &spec.rust_ident; - let wrapper_ident = format_ident!("__pymethod_variant_cls_{}__", member); - let python_name = spec.null_terminated_python_name(ctx); - - let variant_cls = format_ident!("{}_{}", cls, member); - let associated_method = quote! { - fn #wrapper_ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - ::std::result::Result::Ok(py.get_type::<#variant_cls>().into_any().unbind()) - } - }; - - let method_def = quote! { - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::ClassAttribute({ - #pyo3_path::impl_::pymethods::PyClassAttributeDef::new( - #python_name, - #cls_type::#wrapper_ident - ) - }) - ) - }; - - MethodAndMethodDef { - associated_method, - method_def, - } -} - -fn complex_enum_variant_new<'a>( - cls: &'a syn::Ident, - variant: PyClassEnumVariant<'a>, - ctx: &Ctx, -) -> Result { - match variant { - PyClassEnumVariant::Struct(struct_variant) => { - complex_enum_struct_variant_new(cls, struct_variant, ctx) - } - PyClassEnumVariant::Tuple(tuple_variant) => { - complex_enum_tuple_variant_new(cls, tuple_variant, ctx) - } - } -} - -fn complex_enum_struct_variant_new<'a>( - cls: &'a syn::Ident, - variant: PyClassEnumStructVariant<'a>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let variant_cls = format_ident!("{}_{}", cls, variant.ident); - let variant_cls_type: syn::Type = parse_quote!(#variant_cls); - - let arg_py_ident: syn::Ident = parse_quote!(py); - let arg_py_type: syn::Type = parse_quote!(#pyo3_path::Python<'_>); - - let args = { - let mut args = vec![ - // py: Python<'_> - FnArg::Py(PyArg { - name: &arg_py_ident, - ty: &arg_py_type, - }), - ]; - - for field in &variant.fields { - args.push(FnArg::Regular(RegularArg { - name: Cow::Borrowed(field.ident), - ty: field.ty, - from_py_with: None, - default_value: None, - option_wrapped_type: None, - })); - } - args - }; - - let signature = if let Some(constructor) = variant.options.constructor { - crate::pyfunction::FunctionSignature::from_arguments_and_attribute( - args, - constructor.into_signature(), - )? - } else { - crate::pyfunction::FunctionSignature::from_arguments(args)? - }; - - let spec = FnSpec { - tp: crate::method::FnType::FnNew, - name: &format_ident!("__pymethod_constructor__"), - python_name: format_ident!("__new__"), - signature, - convention: crate::method::CallingConvention::TpNew, - text_signature: None, - asyncness: None, - unsafety: None, - }; - - crate::pymethod::impl_py_method_def_new(&variant_cls_type, &spec, ctx) -} - -fn complex_enum_tuple_variant_new<'a>( - cls: &'a syn::Ident, - variant: PyClassEnumTupleVariant<'a>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - - let variant_cls: Ident = format_ident!("{}_{}", cls, variant.ident); - let variant_cls_type: syn::Type = parse_quote!(#variant_cls); - - let arg_py_ident: syn::Ident = parse_quote!(py); - let arg_py_type: syn::Type = parse_quote!(#pyo3_path::Python<'_>); - - let args = { - let mut args = vec![FnArg::Py(PyArg { - name: &arg_py_ident, - ty: &arg_py_type, - })]; - - for (i, field) in variant.fields.iter().enumerate() { - args.push(FnArg::Regular(RegularArg { - name: std::borrow::Cow::Owned(format_ident!("_{}", i)), - ty: field.ty, - from_py_with: None, - default_value: None, - option_wrapped_type: None, - })); - } - args - }; - - let signature = if let Some(constructor) = variant.options.constructor { - crate::pyfunction::FunctionSignature::from_arguments_and_attribute( - args, - constructor.into_signature(), - )? - } else { - crate::pyfunction::FunctionSignature::from_arguments(args)? - }; - - let spec = FnSpec { - tp: crate::method::FnType::FnNew, - name: &format_ident!("__pymethod_constructor__"), - python_name: format_ident!("__new__"), - signature, - convention: crate::method::CallingConvention::TpNew, - text_signature: None, - asyncness: None, - unsafety: None, - }; - - crate::pymethod::impl_py_method_def_new(&variant_cls_type, &spec, ctx) -} - -fn complex_enum_variant_field_getter<'a>( - variant_cls_type: &'a syn::Type, - field_name: &'a syn::Ident, - field_span: Span, - ctx: &Ctx, -) -> Result { - let signature = crate::pyfunction::FunctionSignature::from_arguments(vec![])?; - - let self_type = crate::method::SelfType::TryFromBoundRef(field_span); - - let spec = FnSpec { - tp: crate::method::FnType::Getter(self_type.clone()), - name: field_name, - python_name: field_name.clone(), - signature, - convention: crate::method::CallingConvention::Noargs, - text_signature: None, - asyncness: None, - unsafety: None, - }; - - let property_type = crate::pymethod::PropertyType::Function { - self_type: &self_type, - spec: &spec, - doc: crate::get_doc(&[], None, ctx), - }; - - let getter = crate::pymethod::impl_py_getter_def(variant_cls_type, property_type, ctx)?; - Ok(getter) -} - -fn descriptors_to_items( - cls: &syn::Ident, - rename_all: Option<&RenameAllAttribute>, - frozen: Option, - field_options: Vec<(&syn::Field, FieldPyO3Options)>, - ctx: &Ctx, -) -> syn::Result> { - let ty = syn::parse_quote!(#cls); - let mut items = Vec::new(); - for (field_index, (field, options)) in field_options.into_iter().enumerate() { - if let FieldPyO3Options { - name: Some(name), - get: None, - set: None, - } = options - { - return Err(syn::Error::new_spanned(name, USELESS_NAME)); - } - - if options.get.is_some() { - let getter = impl_py_getter_def( - &ty, - PropertyType::Descriptor { - field_index, - field, - python_name: options.name.as_ref(), - renaming_rule: rename_all.map(|rename_all| rename_all.value.rule), - }, - ctx, - )?; - items.push(getter); - } - - if let Some(set) = options.set { - ensure_spanned!(frozen.is_none(), set.span() => "cannot use `#[pyo3(set)]` on a `frozen` class"); - let setter = impl_py_setter_def( - &ty, - PropertyType::Descriptor { - field_index, - field, - python_name: options.name.as_ref(), - renaming_rule: rename_all.map(|rename_all| rename_all.value.rule), - }, - ctx, - )?; - items.push(setter); - }; - } - Ok(items) -} - -fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let cls_name = get_class_python_name(cls, attr).to_string(); - - let module = if let Some(ModuleAttribute { value, .. }) = &attr.options.module { - quote! { ::core::option::Option::Some(#value) } - } else { - quote! { ::core::option::Option::None } - }; - - quote! { - unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls { - const NAME: &'static str = #cls_name; - const MODULE: ::std::option::Option<&'static str> = #module; - - #[inline] - fn type_object_raw(py: #pyo3_path::Python<'_>) -> *mut #pyo3_path::ffi::PyTypeObject { - use #pyo3_path::prelude::PyTypeMethods; - <#cls as #pyo3_path::impl_::pyclass::PyClassImpl>::lazy_type_object() - .get_or_init(py) - .as_type_ptr() - } - } - } -} - -fn pyclass_richcmp_arms( - options: &PyClassPyO3Options, - ctx: &Ctx, -) -> std::result::Result { - let Ctx { pyo3_path, .. } = ctx; - - let eq_arms = options - .eq - .map(|eq| eq.span) - .or(options.eq_int.map(|eq_int| eq_int.span)) - .map(|span| { - quote_spanned! { span => - #pyo3_path::pyclass::CompareOp::Eq => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val == other, py) - }, - #pyo3_path::pyclass::CompareOp::Ne => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val != other, py) - }, - } - }) - .unwrap_or_default(); - - if let Some(ord) = options.ord { - ensure_spanned!(options.eq.is_some(), ord.span() => "The `ord` option requires the `eq` option."); - } - - let ord_arms = options - .ord - .map(|ord| { - quote_spanned! { ord.span() => - #pyo3_path::pyclass::CompareOp::Gt => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val > other, py) - }, - #pyo3_path::pyclass::CompareOp::Lt => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val < other, py) - }, - #pyo3_path::pyclass::CompareOp::Le => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val <= other, py) - }, - #pyo3_path::pyclass::CompareOp::Ge => { - #pyo3_path::IntoPyObjectExt::into_py_any(self_val >= other, py) - }, - } - }) - .unwrap_or_else(|| quote! { _ => ::std::result::Result::Ok(py.NotImplemented()) }); - - Ok(quote! { - #eq_arms - #ord_arms - }) -} - -fn pyclass_richcmp_simple_enum( - options: &PyClassPyO3Options, - cls: &syn::Type, - repr_type: &syn::Ident, - ctx: &Ctx, -) -> Result<(Option, Option)> { - let Ctx { pyo3_path, .. } = ctx; - - if let Some(eq_int) = options.eq_int { - ensure_spanned!(options.eq.is_some(), eq_int.span() => "The `eq_int` option requires the `eq` option."); - } - - let deprecation = (options.eq_int.is_none() && options.eq.is_none()) - .then(|| { - quote! { - let _ = #pyo3_path::impl_::pyclass::DeprecationTest::<#cls>::new().autogenerated_equality(); - } - }) - .unwrap_or_default(); - - let mut options = options.clone(); - if options.eq.is_none() { - options.eq_int = Some(parse_quote!(eq_int)); - } - - if options.eq.is_none() && options.eq_int.is_none() { - return Ok((None, None)); - } - - let arms = pyclass_richcmp_arms(&options, ctx)?; - - let eq = options.eq.map(|eq| { - quote_spanned! { eq.span() => - let self_val = self; - if let ::std::result::Result::Ok(other) = #pyo3_path::types::PyAnyMethods::downcast::(other) { - let other = &*other.borrow(); - return match op { - #arms - } - } - } - }); - - let eq_int = options.eq_int.map(|eq_int| { - quote_spanned! { eq_int.span() => - let self_val = self.__pyo3__int__(); - if let ::std::result::Result::Ok(other) = #pyo3_path::types::PyAnyMethods::extract::<#repr_type>(other).or_else(|_| { - #pyo3_path::types::PyAnyMethods::downcast::(other).map(|o| o.borrow().__pyo3__int__()) - }) { - return match op { - #arms - } - } - } - }); - - let mut richcmp_impl = parse_quote! { - fn __pyo3__generated____richcmp__( - &self, - py: #pyo3_path::Python, - other: &#pyo3_path::Bound<'_, #pyo3_path::PyAny>, - op: #pyo3_path::pyclass::CompareOp - ) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - #deprecation - - #eq - - #eq_int - - ::std::result::Result::Ok(py.NotImplemented()) - } - }; - let richcmp_slot = if options.eq.is_some() { - generate_protocol_slot(cls, &mut richcmp_impl, &__RICHCMP__, "__richcmp__", ctx).unwrap() - } else { - generate_default_protocol_slot(cls, &mut richcmp_impl, &__RICHCMP__, ctx).unwrap() - }; - Ok((Some(richcmp_impl), Some(richcmp_slot))) -} - -fn pyclass_richcmp( - options: &PyClassPyO3Options, - cls: &syn::Type, - ctx: &Ctx, -) -> Result<(Option, Option)> { - let Ctx { pyo3_path, .. } = ctx; - if let Some(eq_int) = options.eq_int { - bail_spanned!(eq_int.span() => "`eq_int` can only be used on simple enums.") - } - - let arms = pyclass_richcmp_arms(options, ctx)?; - if options.eq.is_some() { - let mut richcmp_impl = parse_quote! { - fn __pyo3__generated____richcmp__( - &self, - py: #pyo3_path::Python, - other: &#pyo3_path::Bound<'_, #pyo3_path::PyAny>, - op: #pyo3_path::pyclass::CompareOp - ) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - let self_val = self; - if let ::std::result::Result::Ok(other) = #pyo3_path::types::PyAnyMethods::downcast::(other) { - let other = &*other.borrow(); - match op { - #arms - } - } else { - ::std::result::Result::Ok(py.NotImplemented()) - } - } - }; - let richcmp_slot = - generate_protocol_slot(cls, &mut richcmp_impl, &__RICHCMP__, "__richcmp__", ctx) - .unwrap(); - Ok((Some(richcmp_impl), Some(richcmp_slot))) - } else { - Ok((None, None)) - } -} - -fn pyclass_hash( - options: &PyClassPyO3Options, - cls: &syn::Type, - ctx: &Ctx, -) -> Result<(Option, Option)> { - if options.hash.is_some() { - ensure_spanned!( - options.frozen.is_some(), options.hash.span() => "The `hash` option requires the `frozen` option."; - options.eq.is_some(), options.hash.span() => "The `hash` option requires the `eq` option."; - ); - } - // FIXME: Use hash.map(...).unzip() on MSRV >= 1.66 - match options.hash { - Some(opt) => { - let mut hash_impl = parse_quote_spanned! { opt.span() => - fn __pyo3__generated____hash__(&self) -> u64 { - let mut s = ::std::collections::hash_map::DefaultHasher::new(); - ::std::hash::Hash::hash(self, &mut s); - ::std::hash::Hasher::finish(&s) - } - }; - let hash_slot = - generate_protocol_slot(cls, &mut hash_impl, &__HASH__, "__hash__", ctx).unwrap(); - Ok((Some(hash_impl), Some(hash_slot))) - } - None => Ok((None, None)), - } -} - -/// Implements most traits used by `#[pyclass]`. -/// -/// Specifically, it implements traits that only depend on class name, -/// and attributes of `#[pyclass]`, and docstrings. -/// Therefore it doesn't implement traits that depends on struct fields and enum variants. -struct PyClassImplsBuilder<'a> { - cls: &'a syn::Ident, - attr: &'a PyClassArgs, - methods_type: PyClassMethodsType, - default_methods: Vec, - default_slots: Vec, - doc: Option, -} - -impl<'a> PyClassImplsBuilder<'a> { - fn new( - cls: &'a syn::Ident, - attr: &'a PyClassArgs, - methods_type: PyClassMethodsType, - default_methods: Vec, - default_slots: Vec, - ) -> Self { - Self { - cls, - attr, - methods_type, - default_methods, - default_slots, - doc: None, - } - } - - fn doc(self, doc: PythonDoc) -> Self { - Self { - doc: Some(doc), - ..self - } - } - - fn impl_all(&self, ctx: &Ctx) -> Result { - let tokens = [ - self.impl_pyclass(ctx), - self.impl_extractext(ctx), - self.impl_into_py(ctx), - self.impl_pyclassimpl(ctx)?, - self.impl_add_to_module(ctx), - self.impl_freelist(ctx), - ] - .into_iter() - .collect(); - Ok(tokens) - } - - fn impl_pyclass(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - - let frozen = if self.attr.options.frozen.is_some() { - quote! { #pyo3_path::pyclass::boolean_struct::True } - } else { - quote! { #pyo3_path::pyclass::boolean_struct::False } - }; - - quote! { - impl #pyo3_path::PyClass for #cls { - type Frozen = #frozen; - } - } - } - fn impl_extractext(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - if self.attr.options.frozen.is_some() { - quote! { - impl<'a, 'py> #pyo3_path::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a #cls - { - type Holder = ::std::option::Option<#pyo3_path::PyRef<'py, #cls>>; - - #[inline] - fn extract(obj: &'a #pyo3_path::Bound<'py, #pyo3_path::PyAny>, holder: &'a mut Self::Holder) -> #pyo3_path::PyResult { - #pyo3_path::impl_::extract_argument::extract_pyclass_ref(obj, holder) - } - } - } - } else { - quote! { - impl<'a, 'py> #pyo3_path::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a #cls - { - type Holder = ::std::option::Option<#pyo3_path::PyRef<'py, #cls>>; - - #[inline] - fn extract(obj: &'a #pyo3_path::Bound<'py, #pyo3_path::PyAny>, holder: &'a mut Self::Holder) -> #pyo3_path::PyResult { - #pyo3_path::impl_::extract_argument::extract_pyclass_ref(obj, holder) - } - } - - impl<'a, 'py> #pyo3_path::impl_::extract_argument::PyFunctionArgument<'a, 'py> for &'a mut #cls - { - type Holder = ::std::option::Option<#pyo3_path::PyRefMut<'py, #cls>>; - - #[inline] - fn extract(obj: &'a #pyo3_path::Bound<'py, #pyo3_path::PyAny>, holder: &'a mut Self::Holder) -> #pyo3_path::PyResult { - #pyo3_path::impl_::extract_argument::extract_pyclass_ref_mut(obj, holder) - } - } - } - } - } - - fn impl_into_py(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - let attr = self.attr; - // If #cls is not extended type, we allow Self->PyObject conversion - if attr.options.extends.is_none() { - quote! { - #[allow(deprecated)] - impl #pyo3_path::IntoPy<#pyo3_path::PyObject> for #cls { - fn into_py(self, py: #pyo3_path::Python<'_>) -> #pyo3_path::PyObject { - #pyo3_path::IntoPy::into_py(#pyo3_path::Py::new(py, self).unwrap(), py) - } - } - - impl<'py> #pyo3_path::conversion::IntoPyObject<'py> for #cls { - type Target = Self; - type Output = #pyo3_path::Bound<'py, >::Target>; - type Error = #pyo3_path::PyErr; - - fn into_pyobject(self, py: #pyo3_path::Python<'py>) -> ::std::result::Result< - ::Output, - ::Error, - > { - #pyo3_path::Bound::new(py, self) - } - } - } - } else { - quote! {} - } - } - fn impl_pyclassimpl(&self, ctx: &Ctx) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - let doc = self.doc.as_ref().map_or( - LitCStr::empty(ctx).to_token_stream(), - PythonDoc::to_token_stream, - ); - let is_basetype = self.attr.options.subclass.is_some(); - let base = match &self.attr.options.extends { - Some(extends_attr) => extends_attr.value.clone(), - None => parse_quote! { #pyo3_path::PyAny }, - }; - let is_subclass = self.attr.options.extends.is_some(); - let is_mapping: bool = self.attr.options.mapping.is_some(); - let is_sequence: bool = self.attr.options.sequence.is_some(); - - ensure_spanned!( - !(is_mapping && is_sequence), - self.cls.span() => "a `#[pyclass]` cannot be both a `mapping` and a `sequence`" - ); - - let dict_offset = if self.attr.options.dict.is_some() { - quote! { - fn dict_offset() -> ::std::option::Option<#pyo3_path::ffi::Py_ssize_t> { - ::std::option::Option::Some(#pyo3_path::impl_::pyclass::dict_offset::()) - } - } - } else { - TokenStream::new() - }; - - // insert space for weak ref - let weaklist_offset = if self.attr.options.weakref.is_some() { - quote! { - fn weaklist_offset() -> ::std::option::Option<#pyo3_path::ffi::Py_ssize_t> { - ::std::option::Option::Some(#pyo3_path::impl_::pyclass::weaklist_offset::()) - } - } - } else { - TokenStream::new() - }; - - let thread_checker = if self.attr.options.unsendable.is_some() { - quote! { #pyo3_path::impl_::pyclass::ThreadCheckerImpl } - } else { - quote! { #pyo3_path::impl_::pyclass::SendablePyClass<#cls> } - }; - - let (pymethods_items, inventory, inventory_class) = match self.methods_type { - PyClassMethodsType::Specialization => (quote! { collector.py_methods() }, None, None), - PyClassMethodsType::Inventory => { - // To allow multiple #[pymethods] block, we define inventory types. - let inventory_class_name = syn::Ident::new( - &format!("Pyo3MethodsInventoryFor{}", cls.unraw()), - Span::call_site(), - ); - ( - quote! { - ::std::boxed::Box::new( - ::std::iter::Iterator::map( - #pyo3_path::inventory::iter::<::Inventory>(), - #pyo3_path::impl_::pyclass::PyClassInventory::items - ) - ) - }, - Some(quote! { type Inventory = #inventory_class_name; }), - Some(define_inventory_class(&inventory_class_name, ctx)), - ) - } - }; - - let default_methods = self - .default_methods - .iter() - .map(|meth| &meth.associated_method) - .chain( - self.default_slots - .iter() - .map(|meth| &meth.associated_method), - ); - - let default_method_defs = self.default_methods.iter().map(|meth| &meth.method_def); - let default_slot_defs = self.default_slots.iter().map(|slot| &slot.slot_def); - let freelist_slots = self.freelist_slots(ctx); - - let class_mutability = if self.attr.options.frozen.is_some() { - quote! { - ImmutableChild - } - } else { - quote! { - MutableChild - } - }; - - let cls = self.cls; - let attr = self.attr; - let dict = if attr.options.dict.is_some() { - quote! { #pyo3_path::impl_::pyclass::PyClassDictSlot } - } else { - quote! { #pyo3_path::impl_::pyclass::PyClassDummySlot } - }; - - // insert space for weak ref - let weakref = if attr.options.weakref.is_some() { - quote! { #pyo3_path::impl_::pyclass::PyClassWeakRefSlot } - } else { - quote! { #pyo3_path::impl_::pyclass::PyClassDummySlot } - }; - - let base_nativetype = if attr.options.extends.is_some() { - quote! { ::BaseNativeType } - } else { - quote! { #pyo3_path::PyAny } - }; - - let pyclass_base_type_impl = attr.options.subclass.map(|subclass| { - quote_spanned! { subclass.span() => - impl #pyo3_path::impl_::pyclass::PyClassBaseType for #cls { - type LayoutAsBase = #pyo3_path::impl_::pycell::PyClassObject; - type BaseNativeType = ::BaseNativeType; - type Initializer = #pyo3_path::pyclass_init::PyClassInitializer; - type PyClassMutability = ::PyClassMutability; - } - } - }); - - let assertions = if attr.options.unsendable.is_some() { - TokenStream::new() - } else { - let assert = quote_spanned! { cls.span() => #pyo3_path::impl_::pyclass::assert_pyclass_sync::<#cls>(); }; - quote! { - const _: () = { - #assert - }; - } - }; - - Ok(quote! { - #assertions - - #pyclass_base_type_impl - - impl #pyo3_path::impl_::pyclass::PyClassImpl for #cls { - const IS_BASETYPE: bool = #is_basetype; - const IS_SUBCLASS: bool = #is_subclass; - const IS_MAPPING: bool = #is_mapping; - const IS_SEQUENCE: bool = #is_sequence; - - type BaseType = #base; - type ThreadChecker = #thread_checker; - #inventory - type PyClassMutability = <<#base as #pyo3_path::impl_::pyclass::PyClassBaseType>::PyClassMutability as #pyo3_path::impl_::pycell::PyClassMutability>::#class_mutability; - type Dict = #dict; - type WeakRef = #weakref; - type BaseNativeType = #base_nativetype; - - fn items_iter() -> #pyo3_path::impl_::pyclass::PyClassItemsIter { - use #pyo3_path::impl_::pyclass::*; - let collector = PyClassImplCollector::::new(); - static INTRINSIC_ITEMS: PyClassItems = PyClassItems { - methods: &[#(#default_method_defs),*], - slots: &[#(#default_slot_defs),* #(#freelist_slots),*], - }; - PyClassItemsIter::new(&INTRINSIC_ITEMS, #pymethods_items) - } - - fn doc(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<&'static ::std::ffi::CStr> { - use #pyo3_path::impl_::pyclass::*; - static DOC: #pyo3_path::sync::GILOnceCell<::std::borrow::Cow<'static, ::std::ffi::CStr>> = #pyo3_path::sync::GILOnceCell::new(); - DOC.get_or_try_init(py, || { - let collector = PyClassImplCollector::::new(); - build_pyclass_doc(::NAME, #doc, collector.new_text_signature()) - }).map(::std::ops::Deref::deref) - } - - #dict_offset - - #weaklist_offset - - fn lazy_type_object() -> &'static #pyo3_path::impl_::pyclass::LazyTypeObject { - use #pyo3_path::impl_::pyclass::LazyTypeObject; - static TYPE_OBJECT: LazyTypeObject<#cls> = LazyTypeObject::new(); - &TYPE_OBJECT - } - } - - #[doc(hidden)] - #[allow(non_snake_case)] - impl #cls { - #(#default_methods)* - } - - #inventory_class - }) - } - - fn impl_add_to_module(&self, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - quote! { - impl #cls { - #[doc(hidden)] - pub const _PYO3_DEF: #pyo3_path::impl_::pymodule::AddClassToModule = #pyo3_path::impl_::pymodule::AddClassToModule::new(); - } - } - } - - fn impl_freelist(&self, ctx: &Ctx) -> TokenStream { - let cls = self.cls; - let Ctx { pyo3_path, .. } = ctx; - - self.attr.options.freelist.as_ref().map_or(quote!{}, |freelist| { - let freelist = &freelist.value; - quote! { - impl #pyo3_path::impl_::pyclass::PyClassWithFreeList for #cls { - #[inline] - fn get_free_list(py: #pyo3_path::Python<'_>) -> &mut #pyo3_path::impl_::freelist::FreeList<*mut #pyo3_path::ffi::PyObject> { - static mut FREELIST: *mut #pyo3_path::impl_::freelist::FreeList<*mut #pyo3_path::ffi::PyObject> = 0 as *mut _; - unsafe { - if FREELIST.is_null() { - FREELIST = ::std::boxed::Box::into_raw(::std::boxed::Box::new( - #pyo3_path::impl_::freelist::FreeList::with_capacity(#freelist))); - } - &mut *FREELIST - } - } - } - } - }) - } - - fn freelist_slots(&self, ctx: &Ctx) -> Vec { - let Ctx { pyo3_path, .. } = ctx; - let cls = self.cls; - - if self.attr.options.freelist.is_some() { - vec![ - quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_alloc, - pfunc: #pyo3_path::impl_::pyclass::alloc_with_freelist::<#cls> as *mut _, - } - }, - quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_free, - pfunc: #pyo3_path::impl_::pyclass::free_with_freelist::<#cls> as *mut _, - } - }, - ] - } else { - Vec::new() - } - } -} - -fn define_inventory_class(inventory_class_name: &syn::Ident, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - quote! { - #[doc(hidden)] - pub struct #inventory_class_name { - items: #pyo3_path::impl_::pyclass::PyClassItems, - } - impl #inventory_class_name { - pub const fn new(items: #pyo3_path::impl_::pyclass::PyClassItems) -> Self { - Self { items } - } - } - - impl #pyo3_path::impl_::pyclass::PyClassInventory for #inventory_class_name { - fn items(&self) -> &#pyo3_path::impl_::pyclass::PyClassItems { - &self.items - } - } - - #pyo3_path::inventory::collect!(#inventory_class_name); - } -} - -fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident) -> TokenStream { - if variants.is_empty() { - return quote! {}; - } - - let mut conditions = Vec::new(); - - for variant in variants { - let cfg_attrs = &variant.cfg_attrs; - - if cfg_attrs.is_empty() { - // There's at least one variant of the enum without cfg attributes, - // so the check is not necessary - return quote! {}; - } - - for attr in cfg_attrs { - if let syn::Meta::List(meta) = &attr.meta { - let cfg_tokens = &meta.tokens; - conditions.push(quote! { not(#cfg_tokens) }); - } - } - } - - quote_spanned! { - cls.span() => - #[cfg(all(#(#conditions),*))] - ::core::compile_error!(concat!("#[pyclass] can't be used on enums without any variants - all variants of enum `", stringify!(#cls), "` have been configured out by cfg attributes")); - } -} - -const UNIQUE_GET: &str = "`get` may only be specified once"; -const UNIQUE_SET: &str = "`set` may only be specified once"; -const UNIQUE_NAME: &str = "`name` may only be specified once"; - -const DUPE_SET: &str = "useless `set` - the struct is already annotated with `set_all`"; -const DUPE_GET: &str = "useless `get` - the struct is already annotated with `get_all`"; -const UNIT_GET: &str = - "`get_all` on an unit struct does nothing, because unit structs have no fields"; -const UNIT_SET: &str = - "`set_all` on an unit struct does nothing, because unit structs have no fields"; - -const USELESS_NAME: &str = "`name` is useless without `get` or `set`"; diff --git a/vendor/pyo3/pyo3-macros-backend/src/pyfunction.rs b/vendor/pyo3/pyo3-macros-backend/src/pyfunction.rs deleted file mode 100644 index 3059025caf..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pyfunction.rs +++ /dev/null @@ -1,286 +0,0 @@ -use crate::utils::Ctx; -use crate::{ - attributes::{ - self, get_pyo3_options, take_attributes, take_pyo3_options, CrateAttribute, - FromPyWithAttribute, NameAttribute, TextSignatureAttribute, - }, - method::{self, CallingConvention, FnArg}, - pymethod::check_generic, -}; -use proc_macro2::TokenStream; -use quote::{format_ident, quote}; -use syn::{ext::IdentExt, spanned::Spanned, Result}; -use syn::{ - parse::{Parse, ParseStream}, - token::Comma, -}; - -mod signature; - -pub use self::signature::{ConstructorAttribute, FunctionSignature, SignatureAttribute}; - -#[derive(Clone, Debug)] -pub struct PyFunctionArgPyO3Attributes { - pub from_py_with: Option, - pub cancel_handle: Option, -} - -enum PyFunctionArgPyO3Attribute { - FromPyWith(FromPyWithAttribute), - CancelHandle(attributes::kw::cancel_handle), -} - -impl Parse for PyFunctionArgPyO3Attribute { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::cancel_handle) { - input.parse().map(PyFunctionArgPyO3Attribute::CancelHandle) - } else if lookahead.peek(attributes::kw::from_py_with) { - input.parse().map(PyFunctionArgPyO3Attribute::FromPyWith) - } else { - Err(lookahead.error()) - } - } -} - -impl PyFunctionArgPyO3Attributes { - /// Parses #[pyo3(from_python_with = "func")] - pub fn from_attrs(attrs: &mut Vec) -> syn::Result { - let mut attributes = PyFunctionArgPyO3Attributes { - from_py_with: None, - cancel_handle: None, - }; - take_attributes(attrs, |attr| { - if let Some(pyo3_attrs) = get_pyo3_options(attr)? { - for attr in pyo3_attrs { - match attr { - PyFunctionArgPyO3Attribute::FromPyWith(from_py_with) => { - ensure_spanned!( - attributes.from_py_with.is_none(), - from_py_with.span() => "`from_py_with` may only be specified once per argument" - ); - attributes.from_py_with = Some(from_py_with); - } - PyFunctionArgPyO3Attribute::CancelHandle(cancel_handle) => { - ensure_spanned!( - attributes.cancel_handle.is_none(), - cancel_handle.span() => "`cancel_handle` may only be specified once per argument" - ); - attributes.cancel_handle = Some(cancel_handle); - } - } - ensure_spanned!( - attributes.from_py_with.is_none() || attributes.cancel_handle.is_none(), - attributes.cancel_handle.unwrap().span() => "`from_py_with` and `cancel_handle` cannot be specified together" - ); - } - Ok(true) - } else { - Ok(false) - } - })?; - Ok(attributes) - } -} - -#[derive(Default)] -pub struct PyFunctionOptions { - pub pass_module: Option, - pub name: Option, - pub signature: Option, - pub text_signature: Option, - pub krate: Option, -} - -impl Parse for PyFunctionOptions { - fn parse(input: ParseStream<'_>) -> Result { - let mut options = PyFunctionOptions::default(); - - while !input.is_empty() { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::name) - || lookahead.peek(attributes::kw::pass_module) - || lookahead.peek(attributes::kw::signature) - || lookahead.peek(attributes::kw::text_signature) - { - options.add_attributes(std::iter::once(input.parse()?))?; - if !input.is_empty() { - let _: Comma = input.parse()?; - } - } else if lookahead.peek(syn::Token![crate]) { - // TODO needs duplicate check? - options.krate = Some(input.parse()?); - } else { - return Err(lookahead.error()); - } - } - - Ok(options) - } -} - -pub enum PyFunctionOption { - Name(NameAttribute), - PassModule(attributes::kw::pass_module), - Signature(SignatureAttribute), - TextSignature(TextSignatureAttribute), - Crate(CrateAttribute), -} - -impl Parse for PyFunctionOption { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(attributes::kw::name) { - input.parse().map(PyFunctionOption::Name) - } else if lookahead.peek(attributes::kw::pass_module) { - input.parse().map(PyFunctionOption::PassModule) - } else if lookahead.peek(attributes::kw::signature) { - input.parse().map(PyFunctionOption::Signature) - } else if lookahead.peek(attributes::kw::text_signature) { - input.parse().map(PyFunctionOption::TextSignature) - } else if lookahead.peek(syn::Token![crate]) { - input.parse().map(PyFunctionOption::Crate) - } else { - Err(lookahead.error()) - } - } -} - -impl PyFunctionOptions { - pub fn from_attrs(attrs: &mut Vec) -> syn::Result { - let mut options = PyFunctionOptions::default(); - options.add_attributes(take_pyo3_options(attrs)?)?; - Ok(options) - } - - pub fn add_attributes( - &mut self, - attrs: impl IntoIterator, - ) -> Result<()> { - macro_rules! set_option { - ($key:ident) => { - { - ensure_spanned!( - self.$key.is_none(), - $key.span() => concat!("`", stringify!($key), "` may only be specified once") - ); - self.$key = Some($key); - } - }; - } - for attr in attrs { - match attr { - PyFunctionOption::Name(name) => set_option!(name), - PyFunctionOption::PassModule(pass_module) => set_option!(pass_module), - PyFunctionOption::Signature(signature) => set_option!(signature), - PyFunctionOption::TextSignature(text_signature) => set_option!(text_signature), - PyFunctionOption::Crate(krate) => set_option!(krate), - } - } - Ok(()) - } -} - -pub fn build_py_function( - ast: &mut syn::ItemFn, - mut options: PyFunctionOptions, -) -> syn::Result { - options.add_attributes(take_pyo3_options(&mut ast.attrs)?)?; - impl_wrap_pyfunction(ast, options) -} - -/// Generates python wrapper over a function that allows adding it to a python module as a python -/// function -pub fn impl_wrap_pyfunction( - func: &mut syn::ItemFn, - options: PyFunctionOptions, -) -> syn::Result { - check_generic(&func.sig)?; - let PyFunctionOptions { - pass_module, - name, - signature, - text_signature, - krate, - } = options; - - let ctx = &Ctx::new(&krate, Some(&func.sig)); - let Ctx { pyo3_path, .. } = &ctx; - - let python_name = name - .as_ref() - .map_or_else(|| &func.sig.ident, |name| &name.value.0) - .unraw(); - - let tp = if pass_module.is_some() { - let span = match func.sig.inputs.first() { - Some(syn::FnArg::Typed(first_arg)) => first_arg.ty.span(), - Some(syn::FnArg::Receiver(_)) | None => bail_spanned!( - func.sig.paren_token.span.join() => "expected `&PyModule` or `Py` as first argument with `pass_module`" - ), - }; - method::FnType::FnModule(span) - } else { - method::FnType::FnStatic - }; - - let arguments = func - .sig - .inputs - .iter_mut() - .skip(if tp.skip_first_rust_argument_in_python_signature() { - 1 - } else { - 0 - }) - .map(FnArg::parse) - .collect::>>()?; - - let signature = if let Some(signature) = signature { - FunctionSignature::from_arguments_and_attribute(arguments, signature)? - } else { - FunctionSignature::from_arguments(arguments)? - }; - - let spec = method::FnSpec { - tp, - name: &func.sig.ident, - convention: CallingConvention::from_signature(&signature), - python_name, - signature, - text_signature, - asyncness: func.sig.asyncness, - unsafety: func.sig.unsafety, - }; - - let vis = &func.vis; - let name = &func.sig.ident; - - let wrapper_ident = format_ident!("__pyfunction_{}", spec.name); - let wrapper = spec.get_wrapper_function(&wrapper_ident, None, ctx)?; - let methoddef = spec.get_methoddef(wrapper_ident, &spec.get_doc(&func.attrs, ctx), ctx); - - let wrapped_pyfunction = quote! { - - // Create a module with the same name as the `#[pyfunction]` - this way `use ` - // will actually bring both the module and the function into scope. - #[doc(hidden)] - #vis mod #name { - pub(crate) struct MakeDef; - pub const _PYO3_DEF: #pyo3_path::impl_::pymethods::PyMethodDef = MakeDef::_PYO3_DEF; - } - - // Generate the definition inside an anonymous function in the same scope as the original function - - // this avoids complications around the fact that the generated module has a different scope - // (and `super` doesn't always refer to the outer scope, e.g. if the `#[pyfunction] is - // inside a function body) - #[allow(unknown_lints, non_local_definitions)] - impl #name::MakeDef { - const _PYO3_DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef; - } - - #[allow(non_snake_case)] - #wrapper - }; - Ok(wrapped_pyfunction) -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/pyfunction/signature.rs b/vendor/pyo3/pyo3-macros-backend/src/pyfunction/signature.rs deleted file mode 100644 index 0a2d861d2b..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pyfunction/signature.rs +++ /dev/null @@ -1,609 +0,0 @@ -use proc_macro2::{Span, TokenStream}; -use quote::ToTokens; -use syn::{ - ext::IdentExt, - parse::{Parse, ParseStream}, - punctuated::Punctuated, - spanned::Spanned, - Token, -}; - -use crate::{ - attributes::{kw, KeywordAttribute}, - method::{FnArg, RegularArg}, -}; - -#[derive(Clone)] -pub struct Signature { - paren_token: syn::token::Paren, - pub items: Punctuated, -} - -impl Parse for Signature { - fn parse(input: ParseStream<'_>) -> syn::Result { - let content; - let paren_token = syn::parenthesized!(content in input); - - let items = content.parse_terminated(SignatureItem::parse, Token![,])?; - - Ok(Signature { paren_token, items }) - } -} - -impl ToTokens for Signature { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.paren_token - .surround(tokens, |tokens| self.items.to_tokens(tokens)) - } -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SignatureItemArgument { - pub ident: syn::Ident, - pub eq_and_default: Option<(Token![=], syn::Expr)>, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SignatureItemPosargsSep { - pub slash: Token![/], -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SignatureItemVarargsSep { - pub asterisk: Token![*], -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SignatureItemVarargs { - pub sep: SignatureItemVarargsSep, - pub ident: syn::Ident, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct SignatureItemKwargs { - pub asterisks: (Token![*], Token![*]), - pub ident: syn::Ident, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum SignatureItem { - Argument(Box), - PosargsSep(SignatureItemPosargsSep), - VarargsSep(SignatureItemVarargsSep), - Varargs(SignatureItemVarargs), - Kwargs(SignatureItemKwargs), -} - -impl Parse for SignatureItem { - fn parse(input: ParseStream<'_>) -> syn::Result { - let lookahead = input.lookahead1(); - if lookahead.peek(Token![*]) { - if input.peek2(Token![*]) { - input.parse().map(SignatureItem::Kwargs) - } else { - let sep = input.parse()?; - if input.is_empty() || input.peek(Token![,]) { - Ok(SignatureItem::VarargsSep(sep)) - } else { - Ok(SignatureItem::Varargs(SignatureItemVarargs { - sep, - ident: input.parse()?, - })) - } - } - } else if lookahead.peek(Token![/]) { - input.parse().map(SignatureItem::PosargsSep) - } else { - input.parse().map(SignatureItem::Argument) - } - } -} - -impl ToTokens for SignatureItem { - fn to_tokens(&self, tokens: &mut TokenStream) { - match self { - SignatureItem::Argument(arg) => arg.to_tokens(tokens), - SignatureItem::Varargs(varargs) => varargs.to_tokens(tokens), - SignatureItem::VarargsSep(sep) => sep.to_tokens(tokens), - SignatureItem::Kwargs(kwargs) => kwargs.to_tokens(tokens), - SignatureItem::PosargsSep(sep) => sep.to_tokens(tokens), - } - } -} - -impl Parse for SignatureItemArgument { - fn parse(input: ParseStream<'_>) -> syn::Result { - Ok(Self { - ident: input.parse()?, - eq_and_default: if input.peek(Token![=]) { - Some((input.parse()?, input.parse()?)) - } else { - None - }, - }) - } -} - -impl ToTokens for SignatureItemArgument { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.ident.to_tokens(tokens); - if let Some((eq, default)) = &self.eq_and_default { - eq.to_tokens(tokens); - default.to_tokens(tokens); - } - } -} - -impl Parse for SignatureItemVarargsSep { - fn parse(input: ParseStream<'_>) -> syn::Result { - Ok(Self { - asterisk: input.parse()?, - }) - } -} - -impl ToTokens for SignatureItemVarargsSep { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.asterisk.to_tokens(tokens); - } -} - -impl Parse for SignatureItemVarargs { - fn parse(input: ParseStream<'_>) -> syn::Result { - Ok(Self { - sep: input.parse()?, - ident: input.parse()?, - }) - } -} - -impl ToTokens for SignatureItemVarargs { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.sep.to_tokens(tokens); - self.ident.to_tokens(tokens); - } -} - -impl Parse for SignatureItemKwargs { - fn parse(input: ParseStream<'_>) -> syn::Result { - Ok(Self { - asterisks: (input.parse()?, input.parse()?), - ident: input.parse()?, - }) - } -} - -impl ToTokens for SignatureItemKwargs { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.asterisks.0.to_tokens(tokens); - self.asterisks.1.to_tokens(tokens); - self.ident.to_tokens(tokens); - } -} - -impl Parse for SignatureItemPosargsSep { - fn parse(input: ParseStream<'_>) -> syn::Result { - Ok(Self { - slash: input.parse()?, - }) - } -} - -impl ToTokens for SignatureItemPosargsSep { - fn to_tokens(&self, tokens: &mut TokenStream) { - self.slash.to_tokens(tokens); - } -} - -pub type SignatureAttribute = KeywordAttribute; -pub type ConstructorAttribute = KeywordAttribute; - -impl ConstructorAttribute { - pub fn into_signature(self) -> SignatureAttribute { - SignatureAttribute { - kw: kw::signature(self.kw.span), - value: self.value, - } - } -} - -#[derive(Default)] -pub struct PythonSignature { - pub positional_parameters: Vec, - pub positional_only_parameters: usize, - pub required_positional_parameters: usize, - pub varargs: Option, - // Tuples of keyword name and whether it is required - pub keyword_only_parameters: Vec<(String, bool)>, - pub kwargs: Option, -} - -impl PythonSignature { - pub fn has_no_args(&self) -> bool { - self.positional_parameters.is_empty() - && self.keyword_only_parameters.is_empty() - && self.varargs.is_none() - && self.kwargs.is_none() - } -} - -pub struct FunctionSignature<'a> { - pub arguments: Vec>, - pub python_signature: PythonSignature, - pub attribute: Option, -} - -pub enum ParseState { - /// Accepting positional parameters, which might be positional only - Positional, - /// Accepting positional parameters after '/' - PositionalAfterPosargs, - /// Accepting keyword-only parameters after '*' or '*args' - Keywords, - /// After `**kwargs` nothing is allowed - Done, -} - -impl ParseState { - fn add_argument( - &mut self, - signature: &mut PythonSignature, - name: String, - required: bool, - span: Span, - ) -> syn::Result<()> { - match self { - ParseState::Positional | ParseState::PositionalAfterPosargs => { - signature.positional_parameters.push(name); - if required { - signature.required_positional_parameters += 1; - ensure_spanned!( - signature.required_positional_parameters == signature.positional_parameters.len(), - span => "cannot have required positional parameter after an optional parameter" - ); - } - Ok(()) - } - ParseState::Keywords => { - signature.keyword_only_parameters.push((name, required)); - Ok(()) - } - ParseState::Done => { - bail_spanned!(span => format!("no more arguments are allowed after `**{}`", signature.kwargs.as_deref().unwrap_or(""))) - } - } - } - - fn add_varargs( - &mut self, - signature: &mut PythonSignature, - varargs: &SignatureItemVarargs, - ) -> syn::Result<()> { - match self { - ParseState::Positional | ParseState::PositionalAfterPosargs => { - signature.varargs = Some(varargs.ident.to_string()); - *self = ParseState::Keywords; - Ok(()) - } - ParseState::Keywords => { - bail_spanned!(varargs.span() => format!("`*{}` not allowed after `*{}`", varargs.ident, signature.varargs.as_deref().unwrap_or(""))) - } - ParseState::Done => { - bail_spanned!(varargs.span() => format!("`*{}` not allowed after `**{}`", varargs.ident, signature.kwargs.as_deref().unwrap_or(""))) - } - } - } - - fn add_kwargs( - &mut self, - signature: &mut PythonSignature, - kwargs: &SignatureItemKwargs, - ) -> syn::Result<()> { - match self { - ParseState::Positional | ParseState::PositionalAfterPosargs | ParseState::Keywords => { - signature.kwargs = Some(kwargs.ident.to_string()); - *self = ParseState::Done; - Ok(()) - } - ParseState::Done => { - bail_spanned!(kwargs.span() => format!("`**{}` not allowed after `**{}`", kwargs.ident, signature.kwargs.as_deref().unwrap_or(""))) - } - } - } - - fn finish_pos_only_args( - &mut self, - signature: &mut PythonSignature, - span: Span, - ) -> syn::Result<()> { - match self { - ParseState::Positional => { - signature.positional_only_parameters = signature.positional_parameters.len(); - *self = ParseState::PositionalAfterPosargs; - Ok(()) - } - ParseState::PositionalAfterPosargs => { - bail_spanned!(span => "`/` not allowed after `/`") - } - ParseState::Keywords => { - bail_spanned!(span => format!("`/` not allowed after `*{}`", signature.varargs.as_deref().unwrap_or(""))) - } - ParseState::Done => { - bail_spanned!(span => format!("`/` not allowed after `**{}`", signature.kwargs.as_deref().unwrap_or(""))) - } - } - } - - fn finish_pos_args(&mut self, signature: &PythonSignature, span: Span) -> syn::Result<()> { - match self { - ParseState::Positional | ParseState::PositionalAfterPosargs => { - *self = ParseState::Keywords; - Ok(()) - } - ParseState::Keywords => { - bail_spanned!(span => format!("`*` not allowed after `*{}`", signature.varargs.as_deref().unwrap_or(""))) - } - ParseState::Done => { - bail_spanned!(span => format!("`*` not allowed after `**{}`", signature.kwargs.as_deref().unwrap_or(""))) - } - } - } -} - -impl<'a> FunctionSignature<'a> { - pub fn from_arguments_and_attribute( - mut arguments: Vec>, - attribute: SignatureAttribute, - ) -> syn::Result { - let mut parse_state = ParseState::Positional; - let mut python_signature = PythonSignature::default(); - - let mut args_iter = arguments.iter_mut(); - - let mut next_non_py_argument_checked = |name: &syn::Ident| { - for fn_arg in args_iter.by_ref() { - match fn_arg { - crate::method::FnArg::Py(..) => { - // If the user incorrectly tried to include py: Python in the - // signature, give a useful error as a hint. - ensure_spanned!( - name != fn_arg.name(), - name.span() => "arguments of type `Python` must not be part of the signature" - ); - // Otherwise try next argument. - continue; - } - crate::method::FnArg::CancelHandle(..) => { - // If the user incorrectly tried to include cancel: CoroutineCancel in the - // signature, give a useful error as a hint. - ensure_spanned!( - name != fn_arg.name(), - name.span() => "`cancel_handle` argument must not be part of the signature" - ); - // Otherwise try next argument. - continue; - } - _ => { - ensure_spanned!( - name == fn_arg.name(), - name.span() => format!( - "expected argument from function definition `{}` but got argument `{}`", - fn_arg.name().unraw(), - name.unraw(), - ) - ); - return Ok(fn_arg); - } - } - } - bail_spanned!( - name.span() => "signature entry does not have a corresponding function argument" - ) - }; - - for item in &attribute.value.items { - match item { - SignatureItem::Argument(arg) => { - let fn_arg = next_non_py_argument_checked(&arg.ident)?; - parse_state.add_argument( - &mut python_signature, - arg.ident.unraw().to_string(), - arg.eq_and_default.is_none(), - arg.span(), - )?; - if let Some((_, default)) = &arg.eq_and_default { - if let FnArg::Regular(arg) = fn_arg { - arg.default_value = Some(default.clone()); - } else { - unreachable!( - "`Python` and `CancelHandle` are already handled above and `*args`/`**kwargs` are \ - parsed and transformed below. Because the have to come last and are only allowed \ - once, this has to be a regular argument." - ); - } - } - } - SignatureItem::VarargsSep(sep) => { - parse_state.finish_pos_args(&python_signature, sep.span())? - } - SignatureItem::Varargs(varargs) => { - let fn_arg = next_non_py_argument_checked(&varargs.ident)?; - fn_arg.to_varargs_mut()?; - parse_state.add_varargs(&mut python_signature, varargs)?; - } - SignatureItem::Kwargs(kwargs) => { - let fn_arg = next_non_py_argument_checked(&kwargs.ident)?; - fn_arg.to_kwargs_mut()?; - parse_state.add_kwargs(&mut python_signature, kwargs)?; - } - SignatureItem::PosargsSep(sep) => { - parse_state.finish_pos_only_args(&mut python_signature, sep.span())? - } - }; - } - - // Ensure no non-py arguments remain - if let Some(arg) = - args_iter.find(|arg| !matches!(arg, FnArg::Py(..) | FnArg::CancelHandle(..))) - { - bail_spanned!( - attribute.kw.span() => format!("missing signature entry for argument `{}`", arg.name()) - ); - } - - Ok(FunctionSignature { - arguments, - python_signature, - attribute: Some(attribute), - }) - } - - /// Without `#[pyo3(signature)]` or `#[args]` - just take the Rust function arguments as positional. - pub fn from_arguments(arguments: Vec>) -> syn::Result { - let mut python_signature = PythonSignature::default(); - for arg in &arguments { - // Python<'_> arguments don't show in Python signature - if matches!(arg, FnArg::Py(..) | FnArg::CancelHandle(..)) { - continue; - } - - if let FnArg::Regular(RegularArg { - ty, - option_wrapped_type: None, - .. - }) = arg - { - // This argument is required, all previous arguments must also have been required - ensure_spanned!( - python_signature.required_positional_parameters == python_signature.positional_parameters.len(), - ty.span() => "required arguments after an `Option<_>` argument are ambiguous\n\ - = help: add a `#[pyo3(signature)]` annotation on this function to unambiguously specify the default values for all optional parameters" - ); - - python_signature.required_positional_parameters = - python_signature.positional_parameters.len() + 1; - } - - python_signature - .positional_parameters - .push(arg.name().unraw().to_string()); - } - - Ok(Self { - arguments, - python_signature, - attribute: None, - }) - } - - fn default_value_for_parameter(&self, parameter: &str) -> String { - let mut default = "...".to_string(); - if let Some(fn_arg) = self.arguments.iter().find(|arg| arg.name() == parameter) { - if let FnArg::Regular(RegularArg { - default_value: Some(arg_default), - .. - }) = fn_arg - { - match arg_default { - // literal values - syn::Expr::Lit(syn::ExprLit { lit, .. }) => match lit { - syn::Lit::Str(s) => default = s.token().to_string(), - syn::Lit::Char(c) => default = c.token().to_string(), - syn::Lit::Int(i) => default = i.base10_digits().to_string(), - syn::Lit::Float(f) => default = f.base10_digits().to_string(), - syn::Lit::Bool(b) => { - default = if b.value() { - "True".to_string() - } else { - "False".to_string() - } - } - _ => {} - }, - // None - syn::Expr::Path(syn::ExprPath { - qself: None, path, .. - }) if path.is_ident("None") => { - default = "None".to_string(); - } - // others, unsupported yet so defaults to `...` - _ => {} - } - } else if let FnArg::Regular(RegularArg { - option_wrapped_type: Some(..), - .. - }) = fn_arg - { - // functions without a `#[pyo3(signature = (...))]` option - // will treat trailing `Option` arguments as having a default of `None` - default = "None".to_string(); - } - } - default - } - - pub fn text_signature(&self, self_argument: Option<&str>) -> String { - let mut output = String::new(); - output.push('('); - - if let Some(arg) = self_argument { - output.push('$'); - output.push_str(arg); - } - - let mut maybe_push_comma = { - let mut first = self_argument.is_none(); - move |output: &mut String| { - if !first { - output.push_str(", "); - } else { - first = false; - } - } - }; - - let py_sig = &self.python_signature; - - for (i, parameter) in py_sig.positional_parameters.iter().enumerate() { - maybe_push_comma(&mut output); - - output.push_str(parameter); - - if i >= py_sig.required_positional_parameters { - output.push('='); - output.push_str(&self.default_value_for_parameter(parameter)); - } - - if py_sig.positional_only_parameters > 0 && i + 1 == py_sig.positional_only_parameters { - output.push_str(", /") - } - } - - if let Some(varargs) = &py_sig.varargs { - maybe_push_comma(&mut output); - output.push('*'); - output.push_str(varargs); - } else if !py_sig.keyword_only_parameters.is_empty() { - maybe_push_comma(&mut output); - output.push('*'); - } - - for (parameter, required) in &py_sig.keyword_only_parameters { - maybe_push_comma(&mut output); - output.push_str(parameter); - if !required { - output.push('='); - output.push_str(&self.default_value_for_parameter(parameter)); - } - } - - if let Some(kwargs) = &py_sig.kwargs { - maybe_push_comma(&mut output); - output.push_str("**"); - output.push_str(kwargs); - } - - output.push(')'); - output - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/pyimpl.rs b/vendor/pyo3/pyo3-macros-backend/src/pyimpl.rs deleted file mode 100644 index 72f06721ec..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pyimpl.rs +++ /dev/null @@ -1,338 +0,0 @@ -use std::collections::HashSet; - -use crate::utils::{has_attribute, has_attribute_with_namespace, Ctx, PyO3CratePath}; -use crate::{ - attributes::{take_pyo3_options, CrateAttribute}, - konst::{ConstAttributes, ConstSpec}, - pyfunction::PyFunctionOptions, - pymethod::{self, is_proto_method, MethodAndMethodDef, MethodAndSlotDef}, -}; -use proc_macro2::TokenStream; -use pymethod::GeneratedPyMethod; -use quote::{format_ident, quote}; -use syn::ImplItemFn; -use syn::{ - parse::{Parse, ParseStream}, - spanned::Spanned, - Result, -}; - -/// The mechanism used to collect `#[pymethods]` into the type object -#[derive(Copy, Clone)] -pub enum PyClassMethodsType { - Specialization, - Inventory, -} - -enum PyImplPyO3Option { - Crate(CrateAttribute), -} - -impl Parse for PyImplPyO3Option { - fn parse(input: ParseStream<'_>) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(syn::Token![crate]) { - input.parse().map(PyImplPyO3Option::Crate) - } else { - Err(lookahead.error()) - } - } -} - -#[derive(Default)] -pub struct PyImplOptions { - krate: Option, -} - -impl PyImplOptions { - pub fn from_attrs(attrs: &mut Vec) -> Result { - let mut options: PyImplOptions = Default::default(); - - for option in take_pyo3_options(attrs)? { - match option { - PyImplPyO3Option::Crate(path) => options.set_crate(path)?, - } - } - - Ok(options) - } - - fn set_crate(&mut self, path: CrateAttribute) -> Result<()> { - ensure_spanned!( - self.krate.is_none(), - path.span() => "`crate` may only be specified once" - ); - - self.krate = Some(path); - Ok(()) - } -} - -pub fn build_py_methods( - ast: &mut syn::ItemImpl, - methods_type: PyClassMethodsType, -) -> syn::Result { - if let Some((_, path, _)) = &ast.trait_ { - bail_spanned!(path.span() => "#[pymethods] cannot be used on trait impl blocks"); - } else if ast.generics != Default::default() { - bail_spanned!( - ast.generics.span() => - "#[pymethods] cannot be used with lifetime parameters or generics" - ); - } else { - let options = PyImplOptions::from_attrs(&mut ast.attrs)?; - impl_methods(&ast.self_ty, &mut ast.items, methods_type, options) - } -} - -fn check_pyfunction(pyo3_path: &PyO3CratePath, meth: &mut ImplItemFn) -> syn::Result<()> { - let mut error = None; - - meth.attrs.retain(|attr| { - let attrs = [attr.clone()]; - - if has_attribute(&attrs, "pyfunction") - || has_attribute_with_namespace(&attrs, Some(pyo3_path), &["pyfunction"]) - || has_attribute_with_namespace(&attrs, Some(pyo3_path), &["prelude", "pyfunction"]) { - error = Some(err_spanned!(meth.sig.span() => "functions inside #[pymethods] do not need to be annotated with #[pyfunction]")); - false - } else { - true - } - }); - - error.map_or(Ok(()), Err) -} - -pub fn impl_methods( - ty: &syn::Type, - impls: &mut [syn::ImplItem], - methods_type: PyClassMethodsType, - options: PyImplOptions, -) -> syn::Result { - let mut trait_impls = Vec::new(); - let mut proto_impls = Vec::new(); - let mut methods = Vec::new(); - let mut associated_methods = Vec::new(); - - let mut implemented_proto_fragments = HashSet::new(); - - for iimpl in impls { - match iimpl { - syn::ImplItem::Fn(meth) => { - let ctx = &Ctx::new(&options.krate, Some(&meth.sig)); - let mut fun_options = PyFunctionOptions::from_attrs(&mut meth.attrs)?; - fun_options.krate = fun_options.krate.or_else(|| options.krate.clone()); - - check_pyfunction(&ctx.pyo3_path, meth)?; - - match pymethod::gen_py_method(ty, &mut meth.sig, &mut meth.attrs, fun_options, ctx)? - { - GeneratedPyMethod::Method(MethodAndMethodDef { - associated_method, - method_def, - }) => { - let attrs = get_cfg_attributes(&meth.attrs); - associated_methods.push(quote!(#(#attrs)* #associated_method)); - methods.push(quote!(#(#attrs)* #method_def)); - } - GeneratedPyMethod::SlotTraitImpl(method_name, token_stream) => { - implemented_proto_fragments.insert(method_name); - let attrs = get_cfg_attributes(&meth.attrs); - trait_impls.push(quote!(#(#attrs)* #token_stream)); - } - GeneratedPyMethod::Proto(MethodAndSlotDef { - associated_method, - slot_def, - }) => { - let attrs = get_cfg_attributes(&meth.attrs); - proto_impls.push(quote!(#(#attrs)* #slot_def)); - associated_methods.push(quote!(#(#attrs)* #associated_method)); - } - } - } - syn::ImplItem::Const(konst) => { - let ctx = &Ctx::new(&options.krate, None); - let attributes = ConstAttributes::from_attrs(&mut konst.attrs)?; - if attributes.is_class_attr { - let spec = ConstSpec { - rust_ident: konst.ident.clone(), - attributes, - }; - let attrs = get_cfg_attributes(&konst.attrs); - let MethodAndMethodDef { - associated_method, - method_def, - } = gen_py_const(ty, &spec, ctx); - methods.push(quote!(#(#attrs)* #method_def)); - associated_methods.push(quote!(#(#attrs)* #associated_method)); - if is_proto_method(&spec.python_name().to_string()) { - // If this is a known protocol method e.g. __contains__, then allow this - // symbol even though it's not an uppercase constant. - konst - .attrs - .push(syn::parse_quote!(#[allow(non_upper_case_globals)])); - } - } - } - syn::ImplItem::Macro(m) => bail_spanned!( - m.span() => - "macros cannot be used as items in `#[pymethods]` impl blocks\n\ - = note: this was previously accepted and ignored" - ), - _ => {} - } - } - let ctx = &Ctx::new(&options.krate, None); - - add_shared_proto_slots(ty, &mut proto_impls, implemented_proto_fragments, ctx); - - let items = match methods_type { - PyClassMethodsType::Specialization => impl_py_methods(ty, methods, proto_impls, ctx), - PyClassMethodsType::Inventory => submit_methods_inventory(ty, methods, proto_impls, ctx), - }; - - Ok(quote! { - #(#trait_impls)* - - #items - - #[doc(hidden)] - #[allow(non_snake_case)] - impl #ty { - #(#associated_methods)* - } - }) -} - -pub fn gen_py_const(cls: &syn::Type, spec: &ConstSpec, ctx: &Ctx) -> MethodAndMethodDef { - let member = &spec.rust_ident; - let wrapper_ident = format_ident!("__pymethod_{}__", member); - let python_name = spec.null_terminated_python_name(ctx); - let Ctx { pyo3_path, .. } = ctx; - - let associated_method = quote! { - fn #wrapper_ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - #pyo3_path::IntoPyObjectExt::into_py_any(#cls::#member, py) - } - }; - - let method_def = quote! { - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::ClassAttribute({ - #pyo3_path::impl_::pymethods::PyClassAttributeDef::new( - #python_name, - #cls::#wrapper_ident - ) - }) - ) - }; - - MethodAndMethodDef { - associated_method, - method_def, - } -} - -fn impl_py_methods( - ty: &syn::Type, - methods: Vec, - proto_impls: Vec, - ctx: &Ctx, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - quote! { - #[allow(unknown_lints, non_local_definitions)] - impl #pyo3_path::impl_::pyclass::PyMethods<#ty> - for #pyo3_path::impl_::pyclass::PyClassImplCollector<#ty> - { - fn py_methods(self) -> &'static #pyo3_path::impl_::pyclass::PyClassItems { - static ITEMS: #pyo3_path::impl_::pyclass::PyClassItems = #pyo3_path::impl_::pyclass::PyClassItems { - methods: &[#(#methods),*], - slots: &[#(#proto_impls),*] - }; - &ITEMS - } - } - } -} - -fn add_shared_proto_slots( - ty: &syn::Type, - proto_impls: &mut Vec, - mut implemented_proto_fragments: HashSet, - ctx: &Ctx, -) { - let Ctx { pyo3_path, .. } = ctx; - macro_rules! try_add_shared_slot { - ($slot:ident, $($fragments:literal),*) => {{ - let mut implemented = false; - $(implemented |= implemented_proto_fragments.remove($fragments));*; - if implemented { - proto_impls.push(quote! { #pyo3_path::impl_::pyclass::$slot!(#ty) }) - } - }}; - } - - try_add_shared_slot!( - generate_pyclass_getattro_slot, - "__getattribute__", - "__getattr__" - ); - try_add_shared_slot!(generate_pyclass_setattr_slot, "__setattr__", "__delattr__"); - try_add_shared_slot!(generate_pyclass_setdescr_slot, "__set__", "__delete__"); - try_add_shared_slot!(generate_pyclass_setitem_slot, "__setitem__", "__delitem__"); - try_add_shared_slot!(generate_pyclass_add_slot, "__add__", "__radd__"); - try_add_shared_slot!(generate_pyclass_sub_slot, "__sub__", "__rsub__"); - try_add_shared_slot!(generate_pyclass_mul_slot, "__mul__", "__rmul__"); - try_add_shared_slot!(generate_pyclass_mod_slot, "__mod__", "__rmod__"); - try_add_shared_slot!(generate_pyclass_divmod_slot, "__divmod__", "__rdivmod__"); - try_add_shared_slot!(generate_pyclass_lshift_slot, "__lshift__", "__rlshift__"); - try_add_shared_slot!(generate_pyclass_rshift_slot, "__rshift__", "__rrshift__"); - try_add_shared_slot!(generate_pyclass_and_slot, "__and__", "__rand__"); - try_add_shared_slot!(generate_pyclass_or_slot, "__or__", "__ror__"); - try_add_shared_slot!(generate_pyclass_xor_slot, "__xor__", "__rxor__"); - try_add_shared_slot!(generate_pyclass_matmul_slot, "__matmul__", "__rmatmul__"); - try_add_shared_slot!(generate_pyclass_truediv_slot, "__truediv__", "__rtruediv__"); - try_add_shared_slot!( - generate_pyclass_floordiv_slot, - "__floordiv__", - "__rfloordiv__" - ); - try_add_shared_slot!(generate_pyclass_pow_slot, "__pow__", "__rpow__"); - try_add_shared_slot!( - generate_pyclass_richcompare_slot, - "__lt__", - "__le__", - "__eq__", - "__ne__", - "__gt__", - "__ge__" - ); - - // if this assertion trips, a slot fragment has been implemented which has not been added in the - // list above - assert!(implemented_proto_fragments.is_empty()); -} - -fn submit_methods_inventory( - ty: &syn::Type, - methods: Vec, - proto_impls: Vec, - ctx: &Ctx, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - quote! { - #pyo3_path::inventory::submit! { - type Inventory = <#ty as #pyo3_path::impl_::pyclass::PyClassImpl>::Inventory; - Inventory::new(#pyo3_path::impl_::pyclass::PyClassItems { methods: &[#(#methods),*], slots: &[#(#proto_impls),*] }) - } - } -} - -pub(crate) fn get_cfg_attributes(attrs: &[syn::Attribute]) -> Vec<&syn::Attribute> { - attrs - .iter() - .filter(|attr| attr.path().is_ident("cfg")) - .collect() -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/pymethod.rs b/vendor/pyo3/pyo3-macros-backend/src/pymethod.rs deleted file mode 100644 index 58574c8bcc..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pymethod.rs +++ /dev/null @@ -1,1634 +0,0 @@ -use std::borrow::Cow; -use std::ffi::CString; - -use crate::attributes::{NameAttribute, RenamingRule}; -use crate::deprecations::deprecate_trailing_option_default; -use crate::method::{CallingConvention, ExtractErrorMode, PyArg}; -use crate::params::{impl_regular_arg_param, Holders}; -use crate::utils::PythonDoc; -use crate::utils::{Ctx, LitCStr}; -use crate::{ - method::{FnArg, FnSpec, FnType, SelfType}, - pyfunction::PyFunctionOptions, -}; -use crate::{quotes, utils}; -use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, quote_spanned, ToTokens}; -use syn::{ext::IdentExt, spanned::Spanned, Result}; - -/// Generated code for a single pymethod item. -pub struct MethodAndMethodDef { - /// The implementation of the Python wrapper for the pymethod - pub associated_method: TokenStream, - /// The method def which will be used to register this pymethod - pub method_def: TokenStream, -} - -/// Generated code for a single pymethod item which is registered by a slot. -pub struct MethodAndSlotDef { - /// The implementation of the Python wrapper for the pymethod - pub associated_method: TokenStream, - /// The slot def which will be used to register this pymethod - pub slot_def: TokenStream, -} - -pub enum GeneratedPyMethod { - Method(MethodAndMethodDef), - Proto(MethodAndSlotDef), - SlotTraitImpl(String, TokenStream), -} - -pub struct PyMethod<'a> { - kind: PyMethodKind, - method_name: String, - spec: FnSpec<'a>, -} - -enum PyMethodKind { - Fn, - Proto(PyMethodProtoKind), -} - -impl PyMethodKind { - fn from_name(name: &str) -> Self { - match name { - // Protocol implemented through slots - "__str__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__STR__)), - "__repr__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__REPR__)), - "__hash__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__HASH__)), - "__richcmp__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__RICHCMP__)), - "__get__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GET__)), - "__iter__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ITER__)), - "__next__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__NEXT__)), - "__await__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__AWAIT__)), - "__aiter__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__AITER__)), - "__anext__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ANEXT__)), - "__len__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__LEN__)), - "__contains__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__CONTAINS__)), - "__concat__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__CONCAT__)), - "__repeat__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__REPEAT__)), - "__inplace_concat__" => { - PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INPLACE_CONCAT__)) - } - "__inplace_repeat__" => { - PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INPLACE_REPEAT__)) - } - "__getitem__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GETITEM__)), - "__pos__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__POS__)), - "__neg__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__NEG__)), - "__abs__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ABS__)), - "__invert__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INVERT__)), - "__index__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INDEX__)), - "__int__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INT__)), - "__float__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__FLOAT__)), - "__bool__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__BOOL__)), - "__iadd__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IADD__)), - "__isub__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ISUB__)), - "__imul__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMUL__)), - "__imatmul__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMATMUL__)), - "__itruediv__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ITRUEDIV__)), - "__ifloordiv__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IFLOORDIV__)), - "__imod__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMOD__)), - "__ipow__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IPOW__)), - "__ilshift__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ILSHIFT__)), - "__irshift__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IRSHIFT__)), - "__iand__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IAND__)), - "__ixor__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IXOR__)), - "__ior__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IOR__)), - "__getbuffer__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GETBUFFER__)), - "__releasebuffer__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__RELEASEBUFFER__)), - // Protocols implemented through traits - "__getattribute__" => { - PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GETATTRIBUTE__)) - } - "__getattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GETATTR__)), - "__setattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SETATTR__)), - "__delattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELATTR__)), - "__set__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SET__)), - "__delete__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELETE__)), - "__setitem__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SETITEM__)), - "__delitem__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELITEM__)), - "__add__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__ADD__)), - "__radd__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RADD__)), - "__sub__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SUB__)), - "__rsub__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RSUB__)), - "__mul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MUL__)), - "__rmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMUL__)), - "__matmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MATMUL__)), - "__rmatmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMATMUL__)), - "__floordiv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__FLOORDIV__)), - "__rfloordiv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RFLOORDIV__)), - "__truediv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__TRUEDIV__)), - "__rtruediv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RTRUEDIV__)), - "__divmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DIVMOD__)), - "__rdivmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RDIVMOD__)), - "__mod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MOD__)), - "__rmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMOD__)), - "__lshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LSHIFT__)), - "__rlshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RLSHIFT__)), - "__rshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RSHIFT__)), - "__rrshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RRSHIFT__)), - "__and__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__AND__)), - "__rand__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RAND__)), - "__xor__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__XOR__)), - "__rxor__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RXOR__)), - "__or__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__OR__)), - "__ror__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__ROR__)), - "__pow__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__POW__)), - "__rpow__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RPOW__)), - "__lt__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LT__)), - "__le__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LE__)), - "__eq__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__EQ__)), - "__ne__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__NE__)), - "__gt__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GT__)), - "__ge__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GE__)), - // Some tricky protocols which don't fit the pattern of the rest - "__call__" => PyMethodKind::Proto(PyMethodProtoKind::Call), - "__traverse__" => PyMethodKind::Proto(PyMethodProtoKind::Traverse), - "__clear__" => PyMethodKind::Proto(PyMethodProtoKind::Clear), - // Not a proto - _ => PyMethodKind::Fn, - } - } -} - -enum PyMethodProtoKind { - Slot(&'static SlotDef), - Call, - Traverse, - Clear, - SlotFragment(&'static SlotFragmentDef), -} - -impl<'a> PyMethod<'a> { - fn parse( - sig: &'a mut syn::Signature, - meth_attrs: &mut Vec, - options: PyFunctionOptions, - ) -> Result { - let spec = FnSpec::parse(sig, meth_attrs, options)?; - - let method_name = spec.python_name.to_string(); - let kind = PyMethodKind::from_name(&method_name); - - Ok(Self { - kind, - method_name, - spec, - }) - } -} - -pub fn is_proto_method(name: &str) -> bool { - match PyMethodKind::from_name(name) { - PyMethodKind::Fn => false, - PyMethodKind::Proto(_) => true, - } -} - -pub fn gen_py_method( - cls: &syn::Type, - sig: &mut syn::Signature, - meth_attrs: &mut Vec, - options: PyFunctionOptions, - ctx: &Ctx, -) -> Result { - check_generic(sig)?; - ensure_function_options_valid(&options)?; - let method = PyMethod::parse(sig, meth_attrs, options)?; - let spec = &method.spec; - let Ctx { pyo3_path, .. } = ctx; - - Ok(match (method.kind, &spec.tp) { - // Class attributes go before protos so that class attributes can be used to set proto - // method to None. - (_, FnType::ClassAttribute) => { - GeneratedPyMethod::Method(impl_py_class_attribute(cls, spec, ctx)?) - } - (PyMethodKind::Proto(proto_kind), _) => { - ensure_no_forbidden_protocol_attributes(&proto_kind, spec, &method.method_name)?; - match proto_kind { - PyMethodProtoKind::Slot(slot_def) => { - let slot = slot_def.generate_type_slot(cls, spec, &method.method_name, ctx)?; - GeneratedPyMethod::Proto(slot) - } - PyMethodProtoKind::Call => { - GeneratedPyMethod::Proto(impl_call_slot(cls, method.spec, ctx)?) - } - PyMethodProtoKind::Traverse => { - GeneratedPyMethod::Proto(impl_traverse_slot(cls, spec, ctx)?) - } - PyMethodProtoKind::Clear => { - GeneratedPyMethod::Proto(impl_clear_slot(cls, spec, ctx)?) - } - PyMethodProtoKind::SlotFragment(slot_fragment_def) => { - let proto = slot_fragment_def.generate_pyproto_fragment(cls, spec, ctx)?; - GeneratedPyMethod::SlotTraitImpl(method.method_name, proto) - } - } - } - // ordinary functions (with some specialties) - (_, FnType::Fn(_)) => GeneratedPyMethod::Method(impl_py_method_def( - cls, - spec, - &spec.get_doc(meth_attrs, ctx), - None, - ctx, - )?), - (_, FnType::FnClass(_)) => GeneratedPyMethod::Method(impl_py_method_def( - cls, - spec, - &spec.get_doc(meth_attrs, ctx), - Some(quote!(#pyo3_path::ffi::METH_CLASS)), - ctx, - )?), - (_, FnType::FnStatic) => GeneratedPyMethod::Method(impl_py_method_def( - cls, - spec, - &spec.get_doc(meth_attrs, ctx), - Some(quote!(#pyo3_path::ffi::METH_STATIC)), - ctx, - )?), - // special prototypes - (_, FnType::FnNew) | (_, FnType::FnNewClass(_)) => { - GeneratedPyMethod::Proto(impl_py_method_def_new(cls, spec, ctx)?) - } - - (_, FnType::Getter(self_type)) => GeneratedPyMethod::Method(impl_py_getter_def( - cls, - PropertyType::Function { - self_type, - spec, - doc: spec.get_doc(meth_attrs, ctx), - }, - ctx, - )?), - (_, FnType::Setter(self_type)) => GeneratedPyMethod::Method(impl_py_setter_def( - cls, - PropertyType::Function { - self_type, - spec, - doc: spec.get_doc(meth_attrs, ctx), - }, - ctx, - )?), - (_, FnType::FnModule(_)) => { - unreachable!("methods cannot be FnModule") - } - }) -} - -pub fn check_generic(sig: &syn::Signature) -> syn::Result<()> { - let err_msg = |typ| format!("Python functions cannot have generic {} parameters", typ); - for param in &sig.generics.params { - match param { - syn::GenericParam::Lifetime(_) => {} - syn::GenericParam::Type(_) => bail_spanned!(param.span() => err_msg("type")), - syn::GenericParam::Const(_) => bail_spanned!(param.span() => err_msg("const")), - } - } - Ok(()) -} - -fn ensure_function_options_valid(options: &PyFunctionOptions) -> syn::Result<()> { - if let Some(pass_module) = &options.pass_module { - bail_spanned!(pass_module.span() => "`pass_module` cannot be used on Python methods"); - } - Ok(()) -} - -fn ensure_no_forbidden_protocol_attributes( - proto_kind: &PyMethodProtoKind, - spec: &FnSpec<'_>, - method_name: &str, -) -> syn::Result<()> { - if let Some(signature) = &spec.signature.attribute { - // __call__ is allowed to have a signature, but nothing else is. - if !matches!(proto_kind, PyMethodProtoKind::Call) { - bail_spanned!(signature.kw.span() => format!("`signature` cannot be used with magic method `{}`", method_name)); - } - } - if let Some(text_signature) = &spec.text_signature { - bail_spanned!(text_signature.kw.span() => format!("`text_signature` cannot be used with magic method `{}`", method_name)); - } - Ok(()) -} - -/// Also used by pyfunction. -pub fn impl_py_method_def( - cls: &syn::Type, - spec: &FnSpec<'_>, - doc: &PythonDoc, - flags: Option, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let wrapper_ident = format_ident!("__pymethod_{}__", spec.python_name); - let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?; - let add_flags = flags.map(|flags| quote!(.flags(#flags))); - let methoddef_type = match spec.tp { - FnType::FnStatic => quote!(Static), - FnType::FnClass(_) => quote!(Class), - _ => quote!(Method), - }; - let methoddef = spec.get_methoddef(quote! { #cls::#wrapper_ident }, doc, ctx); - let method_def = quote! { - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::#methoddef_type(#methoddef #add_flags) - ) - }; - Ok(MethodAndMethodDef { - associated_method, - method_def, - }) -} - -/// Also used by pyclass. -pub fn impl_py_method_def_new( - cls: &syn::Type, - spec: &FnSpec<'_>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let wrapper_ident = syn::Ident::new("__pymethod___new____", Span::call_site()); - let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?; - // Use just the text_signature_call_signature() because the class' Python name - // isn't known to `#[pymethods]` - that has to be attached at runtime from the PyClassImpl - // trait implementation created by `#[pyclass]`. - let text_signature_body = spec.text_signature_call_signature().map_or_else( - || quote!(::std::option::Option::None), - |text_signature| quote!(::std::option::Option::Some(#text_signature)), - ); - let slot_def = quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_new, - pfunc: { - unsafe extern "C" fn trampoline( - subtype: *mut #pyo3_path::ffi::PyTypeObject, - args: *mut #pyo3_path::ffi::PyObject, - kwargs: *mut #pyo3_path::ffi::PyObject, - ) -> *mut #pyo3_path::ffi::PyObject { - use #pyo3_path::impl_::pyclass::*; - #[allow(unknown_lints, non_local_definitions)] - impl PyClassNewTextSignature<#cls> for PyClassImplCollector<#cls> { - #[inline] - fn new_text_signature(self) -> ::std::option::Option<&'static str> { - #text_signature_body - } - } - - #pyo3_path::impl_::trampoline::newfunc( - subtype, - args, - kwargs, - #cls::#wrapper_ident - ) - } - trampoline - } as #pyo3_path::ffi::newfunc as _ - } - }; - Ok(MethodAndSlotDef { - associated_method, - slot_def, - }) -} - -fn impl_call_slot(cls: &syn::Type, mut spec: FnSpec<'_>, ctx: &Ctx) -> Result { - let Ctx { pyo3_path, .. } = ctx; - - // HACK: __call__ proto slot must always use varargs calling convention, so change the spec. - // Probably indicates there's a refactoring opportunity somewhere. - spec.convention = CallingConvention::Varargs; - - let wrapper_ident = syn::Ident::new("__pymethod___call____", Span::call_site()); - let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?; - let slot_def = quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_call, - pfunc: { - unsafe extern "C" fn trampoline( - slf: *mut #pyo3_path::ffi::PyObject, - args: *mut #pyo3_path::ffi::PyObject, - kwargs: *mut #pyo3_path::ffi::PyObject, - ) -> *mut #pyo3_path::ffi::PyObject - { - #pyo3_path::impl_::trampoline::ternaryfunc( - slf, - args, - kwargs, - #cls::#wrapper_ident - ) - } - trampoline - } as #pyo3_path::ffi::ternaryfunc as _ - } - }; - Ok(MethodAndSlotDef { - associated_method, - slot_def, - }) -} - -fn impl_traverse_slot( - cls: &syn::Type, - spec: &FnSpec<'_>, - ctx: &Ctx, -) -> syn::Result { - let Ctx { pyo3_path, .. } = ctx; - if let (Some(py_arg), _) = split_off_python_arg(&spec.signature.arguments) { - return Err(syn::Error::new_spanned(py_arg.ty, "__traverse__ may not take `Python`. \ - Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` \ - should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited \ - inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic.")); - } - - // check that the receiver does not try to smuggle an (implicit) `Python` token into here - if let FnType::Fn(SelfType::TryFromBoundRef(span)) - | FnType::Fn(SelfType::Receiver { - mutable: true, - span, - }) = spec.tp - { - bail_spanned! { span => - "__traverse__ may not take a receiver other than `&self`. Usually, an implementation of \ - `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` \ - should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited \ - inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic." - } - } - - let rust_fn_ident = spec.name; - - let associated_method = quote! { - pub unsafe extern "C" fn __pymethod_traverse__( - slf: *mut #pyo3_path::ffi::PyObject, - visit: #pyo3_path::ffi::visitproc, - arg: *mut ::std::os::raw::c_void, - ) -> ::std::os::raw::c_int { - #pyo3_path::impl_::pymethods::_call_traverse::<#cls>(slf, #cls::#rust_fn_ident, visit, arg, #cls::__pymethod_traverse__) - } - }; - let slot_def = quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_traverse, - pfunc: #cls::__pymethod_traverse__ as #pyo3_path::ffi::traverseproc as _ - } - }; - Ok(MethodAndSlotDef { - associated_method, - slot_def, - }) -} - -fn impl_clear_slot(cls: &syn::Type, spec: &FnSpec<'_>, ctx: &Ctx) -> syn::Result { - let Ctx { pyo3_path, .. } = ctx; - let (py_arg, args) = split_off_python_arg(&spec.signature.arguments); - let self_type = match &spec.tp { - FnType::Fn(self_type) => self_type, - _ => bail_spanned!(spec.name.span() => "expected instance method for `__clear__` function"), - }; - let mut holders = Holders::new(); - let slf = self_type.receiver(cls, ExtractErrorMode::Raise, &mut holders, ctx); - - if let [arg, ..] = args { - bail_spanned!(arg.ty().span() => "`__clear__` function expected to have no arguments"); - } - - let name = &spec.name; - let holders = holders.init_holders(ctx); - let fncall = if py_arg.is_some() { - quote!(#cls::#name(#slf, py)) - } else { - quote!(#cls::#name(#slf)) - }; - - let associated_method = quote! { - pub unsafe extern "C" fn __pymethod___clear____( - _slf: *mut #pyo3_path::ffi::PyObject, - ) -> ::std::os::raw::c_int { - #pyo3_path::impl_::pymethods::_call_clear(_slf, |py, _slf| { - #holders - let result = #fncall; - let result = #pyo3_path::impl_::wrap::converter(&result).wrap(result)?; - ::std::result::Result::Ok(result) - }, #cls::__pymethod___clear____) - } - }; - let slot_def = quote! { - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::Py_tp_clear, - pfunc: #cls::__pymethod___clear____ as #pyo3_path::ffi::inquiry as _ - } - }; - Ok(MethodAndSlotDef { - associated_method, - slot_def, - }) -} - -pub(crate) fn impl_py_class_attribute( - cls: &syn::Type, - spec: &FnSpec<'_>, - ctx: &Ctx, -) -> syn::Result { - let Ctx { pyo3_path, .. } = ctx; - let (py_arg, args) = split_off_python_arg(&spec.signature.arguments); - ensure_spanned!( - args.is_empty(), - args[0].ty().span() => "#[classattr] can only have one argument (of type pyo3::Python)" - ); - - let name = &spec.name; - let fncall = if py_arg.is_some() { - quote!(function(py)) - } else { - quote!(function()) - }; - - let wrapper_ident = format_ident!("__pymethod_{}__", name); - let python_name = spec.null_terminated_python_name(ctx); - let body = quotes::ok_wrap(fncall, ctx); - - let associated_method = quote! { - fn #wrapper_ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> { - let function = #cls::#name; // Shadow the method name to avoid #3017 - let result = #body; - #pyo3_path::impl_::wrap::converter(&result).map_into_pyobject(py, result) - } - }; - - let method_def = quote! { - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::ClassAttribute({ - #pyo3_path::impl_::pymethods::PyClassAttributeDef::new( - #python_name, - #cls::#wrapper_ident - ) - }) - ) - }; - - Ok(MethodAndMethodDef { - associated_method, - method_def, - }) -} - -fn impl_call_setter( - cls: &syn::Type, - spec: &FnSpec<'_>, - self_type: &SelfType, - holders: &mut Holders, - ctx: &Ctx, -) -> syn::Result { - let (py_arg, args) = split_off_python_arg(&spec.signature.arguments); - let slf = self_type.receiver(cls, ExtractErrorMode::Raise, holders, ctx); - - if args.is_empty() { - bail_spanned!(spec.name.span() => "setter function expected to have one argument"); - } else if args.len() > 1 { - bail_spanned!( - args[1].ty().span() => - "setter function can have at most two arguments ([pyo3::Python,] and value)" - ); - } - - let name = &spec.name; - let fncall = if py_arg.is_some() { - quote!(#cls::#name(#slf, py, _val)) - } else { - quote!(#cls::#name(#slf, _val)) - }; - - Ok(fncall) -} - -// Used here for PropertyType::Function, used in pyclass for descriptors. -pub fn impl_py_setter_def( - cls: &syn::Type, - property_type: PropertyType<'_>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let python_name = property_type.null_terminated_python_name(ctx)?; - let doc = property_type.doc(ctx); - let mut holders = Holders::new(); - let setter_impl = match property_type { - PropertyType::Descriptor { - field_index, field, .. - } => { - let slf = SelfType::Receiver { - mutable: true, - span: Span::call_site(), - } - .receiver(cls, ExtractErrorMode::Raise, &mut holders, ctx); - if let Some(ident) = &field.ident { - // named struct field - quote!({ #slf.#ident = _val; }) - } else { - // tuple struct field - let index = syn::Index::from(field_index); - quote!({ #slf.#index = _val; }) - } - } - PropertyType::Function { - spec, self_type, .. - } => impl_call_setter(cls, spec, self_type, &mut holders, ctx)?, - }; - - let wrapper_ident = match property_type { - PropertyType::Descriptor { - field: syn::Field { - ident: Some(ident), .. - }, - .. - } => { - format_ident!("__pymethod_set_{}__", ident) - } - PropertyType::Descriptor { field_index, .. } => { - format_ident!("__pymethod_set_field_{}__", field_index) - } - PropertyType::Function { spec, .. } => { - format_ident!("__pymethod_set_{}__", spec.name) - } - }; - - let extract = match &property_type { - PropertyType::Function { spec, .. } => { - let (_, args) = split_off_python_arg(&spec.signature.arguments); - let value_arg = &args[0]; - let (from_py_with, ident) = - if let Some(from_py_with) = &value_arg.from_py_with().as_ref().map(|f| &f.value) { - let ident = syn::Ident::new("from_py_with", from_py_with.span()); - ( - quote_spanned! { from_py_with.span() => - let #ident = #from_py_with; - }, - ident, - ) - } else { - (quote!(), syn::Ident::new("dummy", Span::call_site())) - }; - - let arg = if let FnArg::Regular(arg) = &value_arg { - arg - } else { - bail_spanned!(value_arg.name().span() => "The #[setter] value argument can't be *args, **kwargs or `cancel_handle`."); - }; - - let extract = impl_regular_arg_param( - arg, - ident, - quote!(::std::option::Option::Some(_value.into())), - &mut holders, - ctx, - ); - - let deprecation = deprecate_trailing_option_default(spec); - quote! { - #deprecation - #from_py_with - let _val = #extract; - } - } - PropertyType::Descriptor { field, .. } => { - let span = field.ty.span(); - let name = field - .ident - .as_ref() - .map(|i| i.to_string()) - .unwrap_or_default(); - - let holder = holders.push_holder(span); - quote! { - let _val = #pyo3_path::impl_::extract_argument::extract_argument(_value.into(), &mut #holder, #name)?; - } - } - }; - - let mut cfg_attrs = TokenStream::new(); - if let PropertyType::Descriptor { field, .. } = &property_type { - for attr in field - .attrs - .iter() - .filter(|attr| attr.path().is_ident("cfg")) - { - attr.to_tokens(&mut cfg_attrs); - } - } - - let init_holders = holders.init_holders(ctx); - let associated_method = quote! { - #cfg_attrs - unsafe fn #wrapper_ident( - py: #pyo3_path::Python<'_>, - _slf: *mut #pyo3_path::ffi::PyObject, - _value: *mut #pyo3_path::ffi::PyObject, - ) -> #pyo3_path::PyResult<::std::os::raw::c_int> { - use ::std::convert::Into; - let _value = #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr_or_opt(py, &_value) - .ok_or_else(|| { - #pyo3_path::exceptions::PyAttributeError::new_err("can't delete attribute") - })?; - #init_holders - #extract - let result = #setter_impl; - #pyo3_path::impl_::callback::convert(py, result) - } - }; - - let method_def = quote! { - #cfg_attrs - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::Setter( - #pyo3_path::impl_::pymethods::PySetterDef::new( - #python_name, - #cls::#wrapper_ident, - #doc - ) - ) - ) - }; - - Ok(MethodAndMethodDef { - associated_method, - method_def, - }) -} - -fn impl_call_getter( - cls: &syn::Type, - spec: &FnSpec<'_>, - self_type: &SelfType, - holders: &mut Holders, - ctx: &Ctx, -) -> syn::Result { - let (py_arg, args) = split_off_python_arg(&spec.signature.arguments); - let slf = self_type.receiver(cls, ExtractErrorMode::Raise, holders, ctx); - ensure_spanned!( - args.is_empty(), - args[0].ty().span() => "getter function can only have one argument (of type pyo3::Python)" - ); - - let name = &spec.name; - let fncall = if py_arg.is_some() { - quote!(#cls::#name(#slf, py)) - } else { - quote!(#cls::#name(#slf)) - }; - - Ok(fncall) -} - -// Used here for PropertyType::Function, used in pyclass for descriptors. -pub fn impl_py_getter_def( - cls: &syn::Type, - property_type: PropertyType<'_>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let python_name = property_type.null_terminated_python_name(ctx)?; - let doc = property_type.doc(ctx); - - let mut cfg_attrs = TokenStream::new(); - if let PropertyType::Descriptor { field, .. } = &property_type { - for attr in field - .attrs - .iter() - .filter(|attr| attr.path().is_ident("cfg")) - { - attr.to_tokens(&mut cfg_attrs); - } - } - - let mut holders = Holders::new(); - match property_type { - PropertyType::Descriptor { - field_index, field, .. - } => { - let ty = &field.ty; - let field = if let Some(ident) = &field.ident { - ident.to_token_stream() - } else { - syn::Index::from(field_index).to_token_stream() - }; - - // TODO: on MSRV 1.77+, we can use `::std::mem::offset_of!` here, and it should - // make it possible for the `MaybeRuntimePyMethodDef` to be a `Static` variant. - let generator = quote_spanned! { ty.span() => - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime( - || GENERATOR.generate(#python_name, #doc) - ) - }; - // This is separate so that the unsafe below does not inherit the span and thus does not - // trigger the `unsafe_code` lint - let method_def = quote! { - #cfg_attrs - { - #[allow(unused_imports)] // might not be used if all probes are positve - use #pyo3_path::impl_::pyclass::Probe; - - struct Offset; - unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset { - fn offset() -> usize { - #pyo3_path::impl_::pyclass::class_offset::<#cls>() + - #pyo3_path::impl_::pyclass::offset_of!(#cls, #field) - } - } - - const GENERATOR: #pyo3_path::impl_::pyclass::PyClassGetterGenerator::< - #cls, - #ty, - Offset, - { #pyo3_path::impl_::pyclass::IsPyT::<#ty>::VALUE }, - { #pyo3_path::impl_::pyclass::IsToPyObject::<#ty>::VALUE }, - { #pyo3_path::impl_::pyclass::IsIntoPy::<#ty>::VALUE }, - { #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#ty>::VALUE }, - { #pyo3_path::impl_::pyclass::IsIntoPyObject::<#ty>::VALUE }, - > = unsafe { #pyo3_path::impl_::pyclass::PyClassGetterGenerator::new() }; - #generator - } - }; - - Ok(MethodAndMethodDef { - associated_method: quote! {}, - method_def, - }) - } - // Forward to `IntoPyCallbackOutput`, to handle `#[getter]`s returning results. - PropertyType::Function { - spec, self_type, .. - } => { - let wrapper_ident = format_ident!("__pymethod_get_{}__", spec.name); - let call = impl_call_getter(cls, spec, self_type, &mut holders, ctx)?; - let body = quote! { - #pyo3_path::impl_::callback::convert(py, #call) - }; - - let init_holders = holders.init_holders(ctx); - let associated_method = quote! { - #cfg_attrs - unsafe fn #wrapper_ident( - py: #pyo3_path::Python<'_>, - _slf: *mut #pyo3_path::ffi::PyObject - ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> { - #init_holders - let result = #body; - result - } - }; - - let method_def = quote! { - #cfg_attrs - #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static( - #pyo3_path::impl_::pymethods::PyMethodDefType::Getter( - #pyo3_path::impl_::pymethods::PyGetterDef::new( - #python_name, - #cls::#wrapper_ident, - #doc - ) - ) - ) - }; - - Ok(MethodAndMethodDef { - associated_method, - method_def, - }) - } - } -} - -/// Split an argument of pyo3::Python from the front of the arg list, if present -fn split_off_python_arg<'a, 'b>(args: &'a [FnArg<'b>]) -> (Option<&'a PyArg<'b>>, &'a [FnArg<'b>]) { - match args { - [FnArg::Py(py), args @ ..] => (Some(py), args), - args => (None, args), - } -} - -pub enum PropertyType<'a> { - Descriptor { - field_index: usize, - field: &'a syn::Field, - python_name: Option<&'a NameAttribute>, - renaming_rule: Option, - }, - Function { - self_type: &'a SelfType, - spec: &'a FnSpec<'a>, - doc: PythonDoc, - }, -} - -impl PropertyType<'_> { - fn null_terminated_python_name(&self, ctx: &Ctx) -> Result { - match self { - PropertyType::Descriptor { - field, - python_name, - renaming_rule, - .. - } => { - let name = match (python_name, &field.ident) { - (Some(name), _) => name.value.0.to_string(), - (None, Some(field_name)) => { - let mut name = field_name.unraw().to_string(); - if let Some(rule) = renaming_rule { - name = utils::apply_renaming_rule(*rule, &name); - } - name - } - (None, None) => { - bail_spanned!(field.span() => "`get` and `set` with tuple struct fields require `name`"); - } - }; - let name = CString::new(name).unwrap(); - Ok(LitCStr::new(name, field.span(), ctx)) - } - PropertyType::Function { spec, .. } => Ok(spec.null_terminated_python_name(ctx)), - } - } - - fn doc(&self, ctx: &Ctx) -> Cow<'_, PythonDoc> { - match self { - PropertyType::Descriptor { field, .. } => { - Cow::Owned(utils::get_doc(&field.attrs, None, ctx)) - } - PropertyType::Function { doc, .. } => Cow::Borrowed(doc), - } - } -} - -pub const __STR__: SlotDef = SlotDef::new("Py_tp_str", "reprfunc"); -pub const __REPR__: SlotDef = SlotDef::new("Py_tp_repr", "reprfunc"); -pub const __HASH__: SlotDef = SlotDef::new("Py_tp_hash", "hashfunc") - .ret_ty(Ty::PyHashT) - .return_conversion(TokenGenerator( - |Ctx { pyo3_path, .. }: &Ctx| quote! { #pyo3_path::impl_::callback::HashCallbackOutput }, - )); -pub const __RICHCMP__: SlotDef = SlotDef::new("Py_tp_richcompare", "richcmpfunc") - .extract_error_mode(ExtractErrorMode::NotImplemented) - .arguments(&[Ty::Object, Ty::CompareOp]); -const __GET__: SlotDef = SlotDef::new("Py_tp_descr_get", "descrgetfunc") - .arguments(&[Ty::MaybeNullObject, Ty::MaybeNullObject]); -const __ITER__: SlotDef = SlotDef::new("Py_tp_iter", "getiterfunc"); -const __NEXT__: SlotDef = SlotDef::new("Py_tp_iternext", "iternextfunc") - .return_specialized_conversion( - TokenGenerator(|_| quote! { IterBaseKind, IterOptionKind, IterResultOptionKind }), - TokenGenerator(|_| quote! { iter_tag }), - ); -const __AWAIT__: SlotDef = SlotDef::new("Py_am_await", "unaryfunc"); -const __AITER__: SlotDef = SlotDef::new("Py_am_aiter", "unaryfunc"); -const __ANEXT__: SlotDef = SlotDef::new("Py_am_anext", "unaryfunc").return_specialized_conversion( - TokenGenerator( - |_| quote! { AsyncIterBaseKind, AsyncIterOptionKind, AsyncIterResultOptionKind }, - ), - TokenGenerator(|_| quote! { async_iter_tag }), -); -pub const __LEN__: SlotDef = SlotDef::new("Py_mp_length", "lenfunc").ret_ty(Ty::PySsizeT); -const __CONTAINS__: SlotDef = SlotDef::new("Py_sq_contains", "objobjproc") - .arguments(&[Ty::Object]) - .ret_ty(Ty::Int); -const __CONCAT__: SlotDef = SlotDef::new("Py_sq_concat", "binaryfunc").arguments(&[Ty::Object]); -const __REPEAT__: SlotDef = SlotDef::new("Py_sq_repeat", "ssizeargfunc").arguments(&[Ty::PySsizeT]); -const __INPLACE_CONCAT__: SlotDef = - SlotDef::new("Py_sq_concat", "binaryfunc").arguments(&[Ty::Object]); -const __INPLACE_REPEAT__: SlotDef = - SlotDef::new("Py_sq_repeat", "ssizeargfunc").arguments(&[Ty::PySsizeT]); -pub const __GETITEM__: SlotDef = - SlotDef::new("Py_mp_subscript", "binaryfunc").arguments(&[Ty::Object]); - -const __POS__: SlotDef = SlotDef::new("Py_nb_positive", "unaryfunc"); -const __NEG__: SlotDef = SlotDef::new("Py_nb_negative", "unaryfunc"); -const __ABS__: SlotDef = SlotDef::new("Py_nb_absolute", "unaryfunc"); -const __INVERT__: SlotDef = SlotDef::new("Py_nb_invert", "unaryfunc"); -const __INDEX__: SlotDef = SlotDef::new("Py_nb_index", "unaryfunc"); -pub const __INT__: SlotDef = SlotDef::new("Py_nb_int", "unaryfunc"); -const __FLOAT__: SlotDef = SlotDef::new("Py_nb_float", "unaryfunc"); -const __BOOL__: SlotDef = SlotDef::new("Py_nb_bool", "inquiry").ret_ty(Ty::Int); - -const __IADD__: SlotDef = SlotDef::new("Py_nb_inplace_add", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __ISUB__: SlotDef = SlotDef::new("Py_nb_inplace_subtract", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IMUL__: SlotDef = SlotDef::new("Py_nb_inplace_multiply", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IMATMUL__: SlotDef = SlotDef::new("Py_nb_inplace_matrix_multiply", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __ITRUEDIV__: SlotDef = SlotDef::new("Py_nb_inplace_true_divide", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IFLOORDIV__: SlotDef = SlotDef::new("Py_nb_inplace_floor_divide", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IMOD__: SlotDef = SlotDef::new("Py_nb_inplace_remainder", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IPOW__: SlotDef = SlotDef::new("Py_nb_inplace_power", "ipowfunc") - .arguments(&[Ty::Object, Ty::IPowModulo]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __ILSHIFT__: SlotDef = SlotDef::new("Py_nb_inplace_lshift", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IRSHIFT__: SlotDef = SlotDef::new("Py_nb_inplace_rshift", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IAND__: SlotDef = SlotDef::new("Py_nb_inplace_and", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IXOR__: SlotDef = SlotDef::new("Py_nb_inplace_xor", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __IOR__: SlotDef = SlotDef::new("Py_nb_inplace_or", "binaryfunc") - .arguments(&[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .return_self(); -const __GETBUFFER__: SlotDef = SlotDef::new("Py_bf_getbuffer", "getbufferproc") - .arguments(&[Ty::PyBuffer, Ty::Int]) - .ret_ty(Ty::Int) - .require_unsafe(); -const __RELEASEBUFFER__: SlotDef = SlotDef::new("Py_bf_releasebuffer", "releasebufferproc") - .arguments(&[Ty::PyBuffer]) - .ret_ty(Ty::Void) - .require_unsafe(); -const __CLEAR__: SlotDef = SlotDef::new("Py_tp_clear", "inquiry") - .arguments(&[]) - .ret_ty(Ty::Int); - -#[derive(Clone, Copy)] -enum Ty { - Object, - MaybeNullObject, - NonNullObject, - IPowModulo, - CompareOp, - Int, - PyHashT, - PySsizeT, - Void, - PyBuffer, -} - -impl Ty { - fn ffi_type(self, ctx: &Ctx) -> TokenStream { - let Ctx { - pyo3_path, - output_span, - } = ctx; - let pyo3_path = pyo3_path.to_tokens_spanned(*output_span); - match self { - Ty::Object | Ty::MaybeNullObject => quote! { *mut #pyo3_path::ffi::PyObject }, - Ty::NonNullObject => quote! { ::std::ptr::NonNull<#pyo3_path::ffi::PyObject> }, - Ty::IPowModulo => quote! { #pyo3_path::impl_::pymethods::IPowModulo }, - Ty::Int | Ty::CompareOp => quote! { ::std::os::raw::c_int }, - Ty::PyHashT => quote! { #pyo3_path::ffi::Py_hash_t }, - Ty::PySsizeT => quote! { #pyo3_path::ffi::Py_ssize_t }, - Ty::Void => quote! { () }, - Ty::PyBuffer => quote! { *mut #pyo3_path::ffi::Py_buffer }, - } - } - - fn extract( - self, - ident: &syn::Ident, - arg: &FnArg<'_>, - extract_error_mode: ExtractErrorMode, - holders: &mut Holders, - ctx: &Ctx, - ) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - match self { - Ty::Object => extract_object( - extract_error_mode, - holders, - arg, - quote! { #ident }, - ctx - ), - Ty::MaybeNullObject => extract_object( - extract_error_mode, - holders, - arg, - quote! { - if #ident.is_null() { - #pyo3_path::ffi::Py_None() - } else { - #ident - } - }, - ctx - ), - Ty::NonNullObject => extract_object( - extract_error_mode, - holders, - arg, - quote! { #ident.as_ptr() }, - ctx - ), - Ty::IPowModulo => extract_object( - extract_error_mode, - holders, - arg, - quote! { #ident.as_ptr() }, - ctx - ), - Ty::CompareOp => extract_error_mode.handle_error( - quote! { - #pyo3_path::class::basic::CompareOp::from_raw(#ident) - .ok_or_else(|| #pyo3_path::exceptions::PyValueError::new_err("invalid comparison operator")) - }, - ctx - ), - Ty::PySsizeT => { - let ty = arg.ty(); - extract_error_mode.handle_error( - quote! { - ::std::convert::TryInto::<#ty>::try_into(#ident).map_err(|e| #pyo3_path::exceptions::PyValueError::new_err(e.to_string())) - }, - ctx - ) - } - // Just pass other types through unmodified - Ty::PyBuffer | Ty::Int | Ty::PyHashT | Ty::Void => quote! { #ident }, - } - } -} - -fn extract_object( - extract_error_mode: ExtractErrorMode, - holders: &mut Holders, - arg: &FnArg<'_>, - source_ptr: TokenStream, - ctx: &Ctx, -) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - let name = arg.name().unraw().to_string(); - - let extract = if let Some(from_py_with) = - arg.from_py_with().map(|from_py_with| &from_py_with.value) - { - quote! { - #pyo3_path::impl_::extract_argument::from_py_with( - unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &#source_ptr).0 }, - #name, - #from_py_with as fn(_) -> _, - ) - } - } else { - let holder = holders.push_holder(Span::call_site()); - quote! { - #pyo3_path::impl_::extract_argument::extract_argument( - unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &#source_ptr).0 }, - &mut #holder, - #name - ) - } - }; - - let extracted = extract_error_mode.handle_error(extract, ctx); - quote!(#extracted) -} - -enum ReturnMode { - ReturnSelf, - Conversion(TokenGenerator), - SpecializedConversion(TokenGenerator, TokenGenerator), -} - -impl ReturnMode { - fn return_call_output(&self, call: TokenStream, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - match self { - ReturnMode::Conversion(conversion) => { - let conversion = TokenGeneratorCtx(*conversion, ctx); - quote! { - let _result: #pyo3_path::PyResult<#conversion> = #pyo3_path::impl_::callback::convert(py, #call); - #pyo3_path::impl_::callback::convert(py, _result) - } - } - ReturnMode::SpecializedConversion(traits, tag) => { - let traits = TokenGeneratorCtx(*traits, ctx); - let tag = TokenGeneratorCtx(*tag, ctx); - quote! { - let _result = #call; - use #pyo3_path::impl_::pymethods::{#traits}; - (&_result).#tag().convert(py, _result) - } - } - ReturnMode::ReturnSelf => quote! { - let _result: #pyo3_path::PyResult<()> = #pyo3_path::impl_::callback::convert(py, #call); - _result?; - #pyo3_path::ffi::Py_XINCREF(_raw_slf); - ::std::result::Result::Ok(_raw_slf) - }, - } - } -} - -pub struct SlotDef { - slot: StaticIdent, - func_ty: StaticIdent, - arguments: &'static [Ty], - ret_ty: Ty, - extract_error_mode: ExtractErrorMode, - return_mode: Option, - require_unsafe: bool, -} - -const NO_ARGUMENTS: &[Ty] = &[]; - -impl SlotDef { - const fn new(slot: &'static str, func_ty: &'static str) -> Self { - SlotDef { - slot: StaticIdent(slot), - func_ty: StaticIdent(func_ty), - arguments: NO_ARGUMENTS, - ret_ty: Ty::Object, - extract_error_mode: ExtractErrorMode::Raise, - return_mode: None, - require_unsafe: false, - } - } - - const fn arguments(mut self, arguments: &'static [Ty]) -> Self { - self.arguments = arguments; - self - } - - const fn ret_ty(mut self, ret_ty: Ty) -> Self { - self.ret_ty = ret_ty; - self - } - - const fn return_conversion(mut self, return_conversion: TokenGenerator) -> Self { - self.return_mode = Some(ReturnMode::Conversion(return_conversion)); - self - } - - const fn return_specialized_conversion( - mut self, - traits: TokenGenerator, - tag: TokenGenerator, - ) -> Self { - self.return_mode = Some(ReturnMode::SpecializedConversion(traits, tag)); - self - } - - const fn extract_error_mode(mut self, extract_error_mode: ExtractErrorMode) -> Self { - self.extract_error_mode = extract_error_mode; - self - } - - const fn return_self(mut self) -> Self { - self.return_mode = Some(ReturnMode::ReturnSelf); - self - } - - const fn require_unsafe(mut self) -> Self { - self.require_unsafe = true; - self - } - - pub fn generate_type_slot( - &self, - cls: &syn::Type, - spec: &FnSpec<'_>, - method_name: &str, - ctx: &Ctx, - ) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let SlotDef { - slot, - func_ty, - arguments, - extract_error_mode, - ret_ty, - return_mode, - require_unsafe, - } = self; - if *require_unsafe { - ensure_spanned!( - spec.unsafety.is_some(), - spec.name.span() => format!("`{}` must be `unsafe fn`", method_name) - ); - } - let arg_types: &Vec<_> = &arguments.iter().map(|arg| arg.ffi_type(ctx)).collect(); - let arg_idents: &Vec<_> = &(0..arguments.len()) - .map(|i| format_ident!("arg{}", i)) - .collect(); - let wrapper_ident = format_ident!("__pymethod_{}__", method_name); - let ret_ty = ret_ty.ffi_type(ctx); - let mut holders = Holders::new(); - let body = generate_method_body( - cls, - spec, - arguments, - *extract_error_mode, - &mut holders, - return_mode.as_ref(), - ctx, - )?; - let name = spec.name; - let holders = holders.init_holders(ctx); - let dep = if method_name == "__richcmp__" { - quote! { - #[allow(unknown_lints, non_local_definitions)] - impl #pyo3_path::impl_::pyclass::HasCustomRichCmp for #cls {} - } - } else { - TokenStream::default() - }; - let associated_method = quote! { - #[allow(non_snake_case)] - unsafe fn #wrapper_ident( - py: #pyo3_path::Python<'_>, - _raw_slf: *mut #pyo3_path::ffi::PyObject, - #(#arg_idents: #arg_types),* - ) -> #pyo3_path::PyResult<#ret_ty> { - #dep - let function = #cls::#name; // Shadow the method name to avoid #3017 - let _slf = _raw_slf; - #holders - #body - } - }; - let slot_def = quote! {{ - unsafe extern "C" fn trampoline( - _slf: *mut #pyo3_path::ffi::PyObject, - #(#arg_idents: #arg_types),* - ) -> #ret_ty - { - #pyo3_path::impl_::trampoline:: #func_ty ( - _slf, - #(#arg_idents,)* - #cls::#wrapper_ident - ) - } - - #pyo3_path::ffi::PyType_Slot { - slot: #pyo3_path::ffi::#slot, - pfunc: trampoline as #pyo3_path::ffi::#func_ty as _ - } - }}; - Ok(MethodAndSlotDef { - associated_method, - slot_def, - }) - } -} - -fn generate_method_body( - cls: &syn::Type, - spec: &FnSpec<'_>, - arguments: &[Ty], - extract_error_mode: ExtractErrorMode, - holders: &mut Holders, - return_mode: Option<&ReturnMode>, - ctx: &Ctx, -) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let self_arg = spec - .tp - .self_arg(Some(cls), extract_error_mode, holders, ctx); - let rust_name = spec.name; - let args = extract_proto_arguments(spec, arguments, extract_error_mode, holders, ctx)?; - let call = quote! { #cls::#rust_name(#self_arg #(#args),*) }; - Ok(if let Some(return_mode) = return_mode { - return_mode.return_call_output(call, ctx) - } else { - quote! { - let result = #call; - #pyo3_path::impl_::callback::convert(py, result) - } - }) -} - -struct SlotFragmentDef { - fragment: &'static str, - arguments: &'static [Ty], - extract_error_mode: ExtractErrorMode, - ret_ty: Ty, -} - -impl SlotFragmentDef { - const fn new(fragment: &'static str, arguments: &'static [Ty]) -> Self { - SlotFragmentDef { - fragment, - arguments, - extract_error_mode: ExtractErrorMode::Raise, - ret_ty: Ty::Void, - } - } - - const fn extract_error_mode(mut self, extract_error_mode: ExtractErrorMode) -> Self { - self.extract_error_mode = extract_error_mode; - self - } - - const fn ret_ty(mut self, ret_ty: Ty) -> Self { - self.ret_ty = ret_ty; - self - } - - fn generate_pyproto_fragment( - &self, - cls: &syn::Type, - spec: &FnSpec<'_>, - ctx: &Ctx, - ) -> Result { - let Ctx { pyo3_path, .. } = ctx; - let SlotFragmentDef { - fragment, - arguments, - extract_error_mode, - ret_ty, - } = self; - let fragment_trait = format_ident!("PyClass{}SlotFragment", fragment); - let method = syn::Ident::new(fragment, Span::call_site()); - let wrapper_ident = format_ident!("__pymethod_{}__", fragment); - let arg_types: &Vec<_> = &arguments.iter().map(|arg| arg.ffi_type(ctx)).collect(); - let arg_idents: &Vec<_> = &(0..arguments.len()) - .map(|i| format_ident!("arg{}", i)) - .collect(); - let mut holders = Holders::new(); - let body = generate_method_body( - cls, - spec, - arguments, - *extract_error_mode, - &mut holders, - None, - ctx, - )?; - let ret_ty = ret_ty.ffi_type(ctx); - let holders = holders.init_holders(ctx); - Ok(quote! { - impl #cls { - #[allow(non_snake_case)] - unsafe fn #wrapper_ident( - py: #pyo3_path::Python, - _raw_slf: *mut #pyo3_path::ffi::PyObject, - #(#arg_idents: #arg_types),* - ) -> #pyo3_path::PyResult<#ret_ty> { - let _slf = _raw_slf; - #holders - #body - } - } - - impl #pyo3_path::impl_::pyclass::#fragment_trait<#cls> for #pyo3_path::impl_::pyclass::PyClassImplCollector<#cls> { - - #[inline] - unsafe fn #method( - self, - py: #pyo3_path::Python, - _raw_slf: *mut #pyo3_path::ffi::PyObject, - #(#arg_idents: #arg_types),* - ) -> #pyo3_path::PyResult<#ret_ty> { - #cls::#wrapper_ident(py, _raw_slf, #(#arg_idents),*) - } - } - }) - } -} - -const __GETATTRIBUTE__: SlotFragmentDef = - SlotFragmentDef::new("__getattribute__", &[Ty::Object]).ret_ty(Ty::Object); -const __GETATTR__: SlotFragmentDef = - SlotFragmentDef::new("__getattr__", &[Ty::Object]).ret_ty(Ty::Object); -const __SETATTR__: SlotFragmentDef = - SlotFragmentDef::new("__setattr__", &[Ty::Object, Ty::NonNullObject]); -const __DELATTR__: SlotFragmentDef = SlotFragmentDef::new("__delattr__", &[Ty::Object]); -const __SET__: SlotFragmentDef = SlotFragmentDef::new("__set__", &[Ty::Object, Ty::NonNullObject]); -const __DELETE__: SlotFragmentDef = SlotFragmentDef::new("__delete__", &[Ty::Object]); -const __SETITEM__: SlotFragmentDef = - SlotFragmentDef::new("__setitem__", &[Ty::Object, Ty::NonNullObject]); -const __DELITEM__: SlotFragmentDef = SlotFragmentDef::new("__delitem__", &[Ty::Object]); - -macro_rules! binary_num_slot_fragment_def { - ($ident:ident, $name:literal) => { - const $ident: SlotFragmentDef = SlotFragmentDef::new($name, &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); - }; -} - -binary_num_slot_fragment_def!(__ADD__, "__add__"); -binary_num_slot_fragment_def!(__RADD__, "__radd__"); -binary_num_slot_fragment_def!(__SUB__, "__sub__"); -binary_num_slot_fragment_def!(__RSUB__, "__rsub__"); -binary_num_slot_fragment_def!(__MUL__, "__mul__"); -binary_num_slot_fragment_def!(__RMUL__, "__rmul__"); -binary_num_slot_fragment_def!(__MATMUL__, "__matmul__"); -binary_num_slot_fragment_def!(__RMATMUL__, "__rmatmul__"); -binary_num_slot_fragment_def!(__FLOORDIV__, "__floordiv__"); -binary_num_slot_fragment_def!(__RFLOORDIV__, "__rfloordiv__"); -binary_num_slot_fragment_def!(__TRUEDIV__, "__truediv__"); -binary_num_slot_fragment_def!(__RTRUEDIV__, "__rtruediv__"); -binary_num_slot_fragment_def!(__DIVMOD__, "__divmod__"); -binary_num_slot_fragment_def!(__RDIVMOD__, "__rdivmod__"); -binary_num_slot_fragment_def!(__MOD__, "__mod__"); -binary_num_slot_fragment_def!(__RMOD__, "__rmod__"); -binary_num_slot_fragment_def!(__LSHIFT__, "__lshift__"); -binary_num_slot_fragment_def!(__RLSHIFT__, "__rlshift__"); -binary_num_slot_fragment_def!(__RSHIFT__, "__rshift__"); -binary_num_slot_fragment_def!(__RRSHIFT__, "__rrshift__"); -binary_num_slot_fragment_def!(__AND__, "__and__"); -binary_num_slot_fragment_def!(__RAND__, "__rand__"); -binary_num_slot_fragment_def!(__XOR__, "__xor__"); -binary_num_slot_fragment_def!(__RXOR__, "__rxor__"); -binary_num_slot_fragment_def!(__OR__, "__or__"); -binary_num_slot_fragment_def!(__ROR__, "__ror__"); - -const __POW__: SlotFragmentDef = SlotFragmentDef::new("__pow__", &[Ty::Object, Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __RPOW__: SlotFragmentDef = SlotFragmentDef::new("__rpow__", &[Ty::Object, Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); - -const __LT__: SlotFragmentDef = SlotFragmentDef::new("__lt__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __LE__: SlotFragmentDef = SlotFragmentDef::new("__le__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __EQ__: SlotFragmentDef = SlotFragmentDef::new("__eq__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __NE__: SlotFragmentDef = SlotFragmentDef::new("__ne__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __GT__: SlotFragmentDef = SlotFragmentDef::new("__gt__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); -const __GE__: SlotFragmentDef = SlotFragmentDef::new("__ge__", &[Ty::Object]) - .extract_error_mode(ExtractErrorMode::NotImplemented) - .ret_ty(Ty::Object); - -fn extract_proto_arguments( - spec: &FnSpec<'_>, - proto_args: &[Ty], - extract_error_mode: ExtractErrorMode, - holders: &mut Holders, - ctx: &Ctx, -) -> Result> { - let mut args = Vec::with_capacity(spec.signature.arguments.len()); - let mut non_python_args = 0; - - for arg in &spec.signature.arguments { - if let FnArg::Py(..) = arg { - args.push(quote! { py }); - } else { - let ident = syn::Ident::new(&format!("arg{}", non_python_args), Span::call_site()); - let conversions = proto_args.get(non_python_args) - .ok_or_else(|| err_spanned!(arg.ty().span() => format!("Expected at most {} non-python arguments", proto_args.len())))? - .extract(&ident, arg, extract_error_mode, holders, ctx); - non_python_args += 1; - args.push(conversions); - } - } - - if non_python_args != proto_args.len() { - bail_spanned!(spec.name.span() => format!("Expected {} arguments, got {}", proto_args.len(), non_python_args)); - } - Ok(args) -} - -struct StaticIdent(&'static str); - -impl ToTokens for StaticIdent { - fn to_tokens(&self, tokens: &mut TokenStream) { - syn::Ident::new(self.0, Span::call_site()).to_tokens(tokens) - } -} - -#[derive(Clone, Copy)] -struct TokenGenerator(fn(&Ctx) -> TokenStream); - -struct TokenGeneratorCtx<'ctx>(TokenGenerator, &'ctx Ctx); - -impl ToTokens for TokenGeneratorCtx<'_> { - fn to_tokens(&self, tokens: &mut TokenStream) { - let Self(TokenGenerator(gen), ctx) = self; - (gen)(ctx).to_tokens(tokens) - } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/pyversions.rs b/vendor/pyo3/pyo3-macros-backend/src/pyversions.rs deleted file mode 100644 index 4c0998667d..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/pyversions.rs +++ /dev/null @@ -1,6 +0,0 @@ -use pyo3_build_config::PythonVersion; - -pub fn is_abi3_before(major: u8, minor: u8) -> bool { - let config = pyo3_build_config::get(); - config.abi3 && config.version < PythonVersion { major, minor } -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/quotes.rs b/vendor/pyo3/pyo3-macros-backend/src/quotes.rs deleted file mode 100644 index d961b4c0ac..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/quotes.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::utils::Ctx; -use proc_macro2::TokenStream; -use quote::{quote, quote_spanned}; - -pub(crate) fn some_wrap(obj: TokenStream, ctx: &Ctx) -> TokenStream { - let Ctx { pyo3_path, .. } = ctx; - quote! { - #pyo3_path::impl_::wrap::SomeWrap::wrap(#obj) - } -} - -pub(crate) fn ok_wrap(obj: TokenStream, ctx: &Ctx) -> TokenStream { - let Ctx { - pyo3_path, - output_span, - } = ctx; - let pyo3_path = pyo3_path.to_tokens_spanned(*output_span); - quote_spanned! { *output_span => { - let obj = #obj; - #[allow(clippy::useless_conversion)] - #pyo3_path::impl_::wrap::converter(&obj).wrap(obj).map_err(::core::convert::Into::<#pyo3_path::PyErr>::into) - }} -} - -pub(crate) fn map_result_into_ptr(result: TokenStream, ctx: &Ctx) -> TokenStream { - let Ctx { - pyo3_path, - output_span, - } = ctx; - let pyo3_path = pyo3_path.to_tokens_spanned(*output_span); - let py = syn::Ident::new("py", proc_macro2::Span::call_site()); - quote_spanned! { *output_span => { - let result = #result; - #pyo3_path::impl_::wrap::converter(&result).map_into_ptr(#py, result) - }} -} diff --git a/vendor/pyo3/pyo3-macros-backend/src/utils.rs b/vendor/pyo3/pyo3-macros-backend/src/utils.rs deleted file mode 100644 index 191ee165bb..0000000000 --- a/vendor/pyo3/pyo3-macros-backend/src/utils.rs +++ /dev/null @@ -1,325 +0,0 @@ -use crate::attributes::{CrateAttribute, RenamingRule}; -use proc_macro2::{Span, TokenStream}; -use quote::{quote, ToTokens}; -use std::ffi::CString; -use syn::spanned::Spanned; -use syn::{punctuated::Punctuated, Token}; - -/// Macro inspired by `anyhow::anyhow!` to create a compiler error with the given span. -macro_rules! err_spanned { - ($span:expr => $msg:expr) => { - syn::Error::new($span, $msg) - }; -} - -/// Macro inspired by `anyhow::bail!` to return a compiler error with the given span. -macro_rules! bail_spanned { - ($span:expr => $msg:expr) => { - return Err(err_spanned!($span => $msg)) - }; -} - -/// Macro inspired by `anyhow::ensure!` to return a compiler error with the given span if the -/// specified condition is not met. -macro_rules! ensure_spanned { - ($condition:expr, $span:expr => $msg:expr) => { - if !($condition) { - bail_spanned!($span => $msg); - } - }; - ($($condition:expr, $span:expr => $msg:expr;)*) => { - if let Some(e) = [$( - (!($condition)).then(|| err_spanned!($span => $msg)), - )*] - .into_iter() - .flatten() - .reduce(|mut acc, e| { - acc.combine(e); - acc - }) { - return Err(e); - } - }; -} - -/// Check if the given type `ty` is `pyo3::Python`. -pub fn is_python(ty: &syn::Type) -> bool { - match unwrap_ty_group(ty) { - syn::Type::Path(typath) => typath - .path - .segments - .last() - .map(|seg| seg.ident == "Python") - .unwrap_or(false), - _ => false, - } -} - -/// If `ty` is `Option`, return `Some(T)`, else `None`. -pub fn option_type_argument(ty: &syn::Type) -> Option<&syn::Type> { - if let syn::Type::Path(syn::TypePath { path, .. }) = ty { - let seg = path.segments.last().filter(|s| s.ident == "Option")?; - if let syn::PathArguments::AngleBracketed(params) = &seg.arguments { - if let syn::GenericArgument::Type(ty) = params.args.first()? { - return Some(ty); - } - } - } - None -} - -// TODO: Replace usage of this by [`syn::LitCStr`] when on MSRV 1.77 -#[derive(Clone)] -pub struct LitCStr { - lit: CString, - span: Span, - pyo3_path: PyO3CratePath, -} - -impl LitCStr { - pub fn new(lit: CString, span: Span, ctx: &Ctx) -> Self { - Self { - lit, - span, - pyo3_path: ctx.pyo3_path.clone(), - } - } - - pub fn empty(ctx: &Ctx) -> Self { - Self { - lit: CString::new("").unwrap(), - span: Span::call_site(), - pyo3_path: ctx.pyo3_path.clone(), - } - } -} - -impl quote::ToTokens for LitCStr { - fn to_tokens(&self, tokens: &mut TokenStream) { - if cfg!(c_str_lit) { - syn::LitCStr::new(&self.lit, self.span).to_tokens(tokens); - } else { - let pyo3_path = &self.pyo3_path; - let lit = self.lit.to_str().unwrap(); - tokens.extend(quote::quote_spanned!(self.span => #pyo3_path::ffi::c_str!(#lit))); - } - } -} - -/// A syntax tree which evaluates to a nul-terminated docstring for Python. -/// -/// Typically the tokens will just be that string, but if the original docs included macro -/// expressions then the tokens will be a concat!("...", "\n", "\0") expression of the strings and -/// macro parts. contents such as parse the string contents. -#[derive(Clone)] -pub struct PythonDoc(PythonDocKind); - -#[derive(Clone)] -enum PythonDocKind { - LitCStr(LitCStr), - // There is currently no way to `concat!` c-string literals, we fallback to the `c_str!` macro in - // this case. - Tokens(TokenStream), -} - -/// Collects all #[doc = "..."] attributes into a TokenStream evaluating to a null-terminated string. -/// -/// If this doc is for a callable, the provided `text_signature` can be passed to prepend -/// this to the documentation suitable for Python to extract this into the `__text_signature__` -/// attribute. -pub fn get_doc( - attrs: &[syn::Attribute], - mut text_signature: Option, - ctx: &Ctx, -) -> PythonDoc { - let Ctx { pyo3_path, .. } = ctx; - // insert special divider between `__text_signature__` and doc - // (assume text_signature is itself well-formed) - if let Some(text_signature) = &mut text_signature { - text_signature.push_str("\n--\n\n"); - } - - let mut parts = Punctuated::::new(); - let mut first = true; - let mut current_part = text_signature.unwrap_or_default(); - - for attr in attrs { - if attr.path().is_ident("doc") { - if let Ok(nv) = attr.meta.require_name_value() { - if !first { - current_part.push('\n'); - } else { - first = false; - } - if let syn::Expr::Lit(syn::ExprLit { - lit: syn::Lit::Str(lit_str), - .. - }) = &nv.value - { - // Strip single left space from literal strings, if needed. - // e.g. `/// Hello world` expands to #[doc = " Hello world"] - let doc_line = lit_str.value(); - current_part.push_str(doc_line.strip_prefix(' ').unwrap_or(&doc_line)); - } else { - // This is probably a macro doc from Rust 1.54, e.g. #[doc = include_str!(...)] - // Reset the string buffer, write that part, and then push this macro part too. - parts.push(current_part.to_token_stream()); - current_part.clear(); - parts.push(nv.value.to_token_stream()); - } - } - } - } - - if !parts.is_empty() { - // Doc contained macro pieces - return as `concat!` expression - if !current_part.is_empty() { - parts.push(current_part.to_token_stream()); - } - - let mut tokens = TokenStream::new(); - - syn::Ident::new("concat", Span::call_site()).to_tokens(&mut tokens); - syn::token::Not(Span::call_site()).to_tokens(&mut tokens); - syn::token::Bracket(Span::call_site()).surround(&mut tokens, |tokens| { - parts.to_tokens(tokens); - syn::token::Comma(Span::call_site()).to_tokens(tokens); - }); - - PythonDoc(PythonDocKind::Tokens( - quote!(#pyo3_path::ffi::c_str!(#tokens)), - )) - } else { - // Just a string doc - return directly with nul terminator - let docs = CString::new(current_part).unwrap(); - PythonDoc(PythonDocKind::LitCStr(LitCStr::new( - docs, - Span::call_site(), - ctx, - ))) - } -} - -impl quote::ToTokens for PythonDoc { - fn to_tokens(&self, tokens: &mut TokenStream) { - match &self.0 { - PythonDocKind::LitCStr(lit) => lit.to_tokens(tokens), - PythonDocKind::Tokens(toks) => toks.to_tokens(tokens), - } - } -} - -pub fn unwrap_ty_group(mut ty: &syn::Type) -> &syn::Type { - while let syn::Type::Group(g) = ty { - ty = &*g.elem; - } - ty -} - -pub struct Ctx { - /// Where we can find the pyo3 crate - pub pyo3_path: PyO3CratePath, - - /// If we are in a pymethod or pyfunction, - /// this will be the span of the return type - pub output_span: Span, -} - -impl Ctx { - pub(crate) fn new(attr: &Option, signature: Option<&syn::Signature>) -> Self { - let pyo3_path = match attr { - Some(attr) => PyO3CratePath::Given(attr.value.0.clone()), - None => PyO3CratePath::Default, - }; - - let output_span = if let Some(syn::Signature { - output: syn::ReturnType::Type(_, output_type), - .. - }) = &signature - { - output_type.span() - } else { - Span::call_site() - }; - - Self { - pyo3_path, - output_span, - } - } -} - -#[derive(Clone)] -pub enum PyO3CratePath { - Given(syn::Path), - Default, -} - -impl PyO3CratePath { - pub fn to_tokens_spanned(&self, span: Span) -> TokenStream { - match self { - Self::Given(path) => quote::quote_spanned! { span => #path }, - Self::Default => quote::quote_spanned! { span => ::pyo3 }, - } - } -} - -impl quote::ToTokens for PyO3CratePath { - fn to_tokens(&self, tokens: &mut TokenStream) { - match self { - Self::Given(path) => path.to_tokens(tokens), - Self::Default => quote::quote! { ::pyo3 }.to_tokens(tokens), - } - } -} - -pub fn apply_renaming_rule(rule: RenamingRule, name: &str) -> String { - use heck::*; - - match rule { - RenamingRule::CamelCase => name.to_lower_camel_case(), - RenamingRule::KebabCase => name.to_kebab_case(), - RenamingRule::Lowercase => name.to_lowercase(), - RenamingRule::PascalCase => name.to_upper_camel_case(), - RenamingRule::ScreamingKebabCase => name.to_shouty_kebab_case(), - RenamingRule::ScreamingSnakeCase => name.to_shouty_snake_case(), - RenamingRule::SnakeCase => name.to_snake_case(), - RenamingRule::Uppercase => name.to_uppercase(), - } -} - -pub(crate) enum IdentOrStr<'a> { - Str(&'a str), - Ident(syn::Ident), -} - -pub(crate) fn has_attribute(attrs: &[syn::Attribute], ident: &str) -> bool { - has_attribute_with_namespace(attrs, None, &[ident]) -} - -pub(crate) fn has_attribute_with_namespace( - attrs: &[syn::Attribute], - crate_path: Option<&PyO3CratePath>, - idents: &[&str], -) -> bool { - let mut segments = vec![]; - if let Some(c) = crate_path { - match c { - PyO3CratePath::Given(paths) => { - for p in &paths.segments { - segments.push(IdentOrStr::Ident(p.ident.clone())); - } - } - PyO3CratePath::Default => segments.push(IdentOrStr::Str("pyo3")), - } - }; - for i in idents { - segments.push(IdentOrStr::Str(i)); - } - - attrs.iter().any(|attr| { - segments - .iter() - .eq(attr.path().segments.iter().map(|v| &v.ident)) - }) -} diff --git a/vendor/pyo3/pyo3-macros/Cargo.toml b/vendor/pyo3/pyo3-macros/Cargo.toml deleted file mode 100644 index 55961376c7..0000000000 --- a/vendor/pyo3/pyo3-macros/Cargo.toml +++ /dev/null @@ -1,28 +0,0 @@ -[package] -name = "pyo3-macros" -version = "0.23.4" -description = "Proc macros for PyO3 package" -authors = ["PyO3 Project and Contributors "] -keywords = ["pyo3", "python", "cpython", "ffi"] -homepage = "https://github.com/pyo3/pyo3" -repository = "https://github.com/pyo3/pyo3" -categories = ["api-bindings", "development-tools::ffi"] -license = "MIT OR Apache-2.0" -edition = "2021" -rust-version = "1.63" - -[lib] -proc-macro = true - -[features] -multiple-pymethods = [] -experimental-async = ["pyo3-macros-backend/experimental-async"] - -[dependencies] -proc-macro2 = { version = "1.0.60", default-features = false } -quote = "1" -syn = { version = "2", features = ["full", "extra-traits"] } -pyo3-macros-backend = { path = "../pyo3-macros-backend", version = "=0.23.4" } - -[lints] -workspace = true diff --git a/vendor/pyo3/pyo3-macros/LICENSE-APACHE b/vendor/pyo3/pyo3-macros/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/pyo3/pyo3-macros/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/pyo3/pyo3-macros/LICENSE-MIT b/vendor/pyo3/pyo3-macros/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/pyo3/pyo3-macros/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/pyo3/pyo3-macros/src/lib.rs b/vendor/pyo3/pyo3-macros/src/lib.rs deleted file mode 100644 index 2621bea4c6..0000000000 --- a/vendor/pyo3/pyo3-macros/src/lib.rs +++ /dev/null @@ -1,253 +0,0 @@ -//! This crate declares only the proc macro attributes, as a crate defining proc macro attributes -//! must not contain any other public items. - -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -use proc_macro::TokenStream; -use proc_macro2::TokenStream as TokenStream2; -use pyo3_macros_backend::{ - build_derive_from_pyobject, build_derive_into_pyobject, build_py_class, build_py_enum, - build_py_function, build_py_methods, pymodule_function_impl, pymodule_module_impl, PyClassArgs, - PyClassMethodsType, PyFunctionOptions, PyModuleOptions, -}; -use quote::quote; -use syn::{parse_macro_input, Item}; - -/// A proc macro used to implement Python modules. -/// -/// The name of the module will be taken from the function name, unless `#[pyo3(name = "my_name")]` -/// is also annotated on the function to override the name. **Important**: the module name should -/// match the `lib.name` setting in `Cargo.toml`, so that Python is able to import the module -/// without needing a custom import loader. -/// -/// Functions annotated with `#[pymodule]` can also be annotated with the following: -/// -/// | Annotation | Description | -/// | :- | :- | -/// | `#[pyo3(name = "...")]` | Defines the name of the module in Python. | -/// | `#[pyo3(submodule)]` | Skips adding a `PyInit_` FFI symbol to the compiled binary. | -/// | `#[pyo3(module = "...")]` | Defines the Python `dotted.path` to the parent module for use in introspection. | -/// | `#[pyo3(crate = "pyo3")]` | Defines the path to PyO3 to use code generated by the macro. | -/// -/// For more on creating Python modules see the [module section of the guide][1]. -/// -/// Due to technical limitations on how `#[pymodule]` is implemented, a function marked -/// `#[pymodule]` cannot have a module with the same name in the same scope. (The -/// `#[pymodule]` implementation generates a hidden module with the same name containing -/// metadata about the module, which is used by `wrap_pymodule!`). -/// -#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/module.html")] -#[proc_macro_attribute] -pub fn pymodule(args: TokenStream, input: TokenStream) -> TokenStream { - let options = parse_macro_input!(args as PyModuleOptions); - - let mut ast = parse_macro_input!(input as Item); - let expanded = match &mut ast { - Item::Mod(module) => { - match pymodule_module_impl(module, options) { - // #[pymodule] on a module will rebuild the original ast, so we don't emit it here - Ok(expanded) => return expanded.into(), - Err(e) => Err(e), - } - } - Item::Fn(function) => pymodule_function_impl(function, options), - unsupported => Err(syn::Error::new_spanned( - unsupported, - "#[pymodule] only supports modules and functions.", - )), - } - .unwrap_or_compile_error(); - - quote!( - #ast - #expanded - ) - .into() -} - -#[proc_macro_attribute] -pub fn pyclass(attr: TokenStream, input: TokenStream) -> TokenStream { - let item = parse_macro_input!(input as Item); - match item { - Item::Struct(struct_) => pyclass_impl(attr, struct_, methods_type()), - Item::Enum(enum_) => pyclass_enum_impl(attr, enum_, methods_type()), - unsupported => { - syn::Error::new_spanned(unsupported, "#[pyclass] only supports structs and enums.") - .into_compile_error() - .into() - } - } -} - -/// A proc macro used to expose methods to Python. -/// -/// Methods within a `#[pymethods]` block can be annotated with as well as the following: -/// -/// | Annotation | Description | -/// | :- | :- | -/// | [`#[new]`][4] | Defines the class constructor, like Python's `__new__` method. | -/// | [`#[getter]`][5] and [`#[setter]`][5] | These define getters and setters, similar to Python's `@property` decorator. This is useful for getters/setters that require computation or side effects; if that is not the case consider using [`#[pyo3(get, set)]`][11] on the struct's field(s).| -/// | [`#[staticmethod]`][6]| Defines the method as a staticmethod, like Python's `@staticmethod` decorator.| -/// | [`#[classmethod]`][7] | Defines the method as a classmethod, like Python's `@classmethod` decorator.| -/// | [`#[classattr]`][9] | Defines a class variable. | -/// | [`#[args]`][10] | Deprecated way to define a method's default arguments and allows the function to receive `*args` and `**kwargs`. Use `#[pyo3(signature = (...))]` instead. | -/// | [`#[pyo3( | Any of the `#[pyo3]` options supported on [`macro@pyfunction`]. | -/// -/// For more on creating class methods, -/// see the [class section of the guide][1]. -/// -/// If the [`multiple-pymethods`][2] feature is enabled, it is possible to implement -/// multiple `#[pymethods]` blocks for a single `#[pyclass]`. -/// This will add a transitive dependency on the [`inventory`][3] crate. -/// -#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#instance-methods")] -#[doc = concat!("[2]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#multiple-pymethods")] -/// [3]: https://docs.rs/inventory/ -#[doc = concat!("[4]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#constructor")] -#[doc = concat!("[5]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#object-properties-using-getter-and-setter")] -#[doc = concat!("[6]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#static-methods")] -#[doc = concat!("[7]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#class-methods")] -#[doc = concat!("[8]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#callable-objects")] -#[doc = concat!("[9]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#class-attributes")] -#[doc = concat!("[10]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#method-arguments")] -#[doc = concat!("[11]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#object-properties-using-pyo3get-set")] -#[proc_macro_attribute] -pub fn pymethods(attr: TokenStream, input: TokenStream) -> TokenStream { - let methods_type = if cfg!(feature = "multiple-pymethods") { - PyClassMethodsType::Inventory - } else { - PyClassMethodsType::Specialization - }; - pymethods_impl(attr, input, methods_type) -} - -/// A proc macro used to expose Rust functions to Python. -/// -/// Functions annotated with `#[pyfunction]` can also be annotated with the following `#[pyo3]` -/// options: -/// -/// | Annotation | Description | -/// | :- | :- | -/// | `#[pyo3(name = "...")]` | Defines the name of the function in Python. | -/// | `#[pyo3(text_signature = "...")]` | Defines the `__text_signature__` attribute of the function in Python. | -/// | `#[pyo3(pass_module)]` | Passes the module containing the function as a `&PyModule` first argument to the function. | -/// -/// For more on exposing functions see the [function section of the guide][1]. -/// -/// Due to technical limitations on how `#[pyfunction]` is implemented, a function marked -/// `#[pyfunction]` cannot have a module with the same name in the same scope. (The -/// `#[pyfunction]` implementation generates a hidden module with the same name containing -/// metadata about the function, which is used by `wrap_pyfunction!`). -/// -#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/function.html")] -#[proc_macro_attribute] -pub fn pyfunction(attr: TokenStream, input: TokenStream) -> TokenStream { - let mut ast = parse_macro_input!(input as syn::ItemFn); - let options = parse_macro_input!(attr as PyFunctionOptions); - - let expanded = build_py_function(&mut ast, options).unwrap_or_compile_error(); - - quote!( - #ast - #expanded - ) - .into() -} - -#[proc_macro_derive(IntoPyObject, attributes(pyo3))] -pub fn derive_into_py_object(item: TokenStream) -> TokenStream { - let ast = parse_macro_input!(item as syn::DeriveInput); - let expanded = build_derive_into_pyobject::(&ast).unwrap_or_compile_error(); - quote!( - #expanded - ) - .into() -} - -#[proc_macro_derive(IntoPyObjectRef, attributes(pyo3))] -pub fn derive_into_py_object_ref(item: TokenStream) -> TokenStream { - let ast = parse_macro_input!(item as syn::DeriveInput); - let expanded = - pyo3_macros_backend::build_derive_into_pyobject::(&ast).unwrap_or_compile_error(); - quote!( - #expanded - ) - .into() -} - -#[proc_macro_derive(FromPyObject, attributes(pyo3))] -pub fn derive_from_py_object(item: TokenStream) -> TokenStream { - let ast = parse_macro_input!(item as syn::DeriveInput); - let expanded = build_derive_from_pyobject(&ast).unwrap_or_compile_error(); - quote!( - #expanded - ) - .into() -} - -fn pyclass_impl( - attrs: TokenStream, - mut ast: syn::ItemStruct, - methods_type: PyClassMethodsType, -) -> TokenStream { - let args = parse_macro_input!(attrs with PyClassArgs::parse_struct_args); - let expanded = build_py_class(&mut ast, args, methods_type).unwrap_or_compile_error(); - - quote!( - #ast - #expanded - ) - .into() -} - -fn pyclass_enum_impl( - attrs: TokenStream, - mut ast: syn::ItemEnum, - methods_type: PyClassMethodsType, -) -> TokenStream { - let args = parse_macro_input!(attrs with PyClassArgs::parse_enum_args); - let expanded = build_py_enum(&mut ast, args, methods_type).unwrap_or_compile_error(); - - quote!( - #ast - #expanded - ) - .into() -} - -fn pymethods_impl( - attr: TokenStream, - input: TokenStream, - methods_type: PyClassMethodsType, -) -> TokenStream { - let mut ast = parse_macro_input!(input as syn::ItemImpl); - // Apply all options as a #[pyo3] attribute on the ItemImpl - // e.g. #[pymethods(crate = "crate")] impl Foo { } - // -> #[pyo3(crate = "crate")] impl Foo { } - let attr: TokenStream2 = attr.into(); - ast.attrs.push(syn::parse_quote!( #[pyo3(#attr)] )); - let expanded = build_py_methods(&mut ast, methods_type).unwrap_or_compile_error(); - - quote!( - #ast - #expanded - ) - .into() -} - -fn methods_type() -> PyClassMethodsType { - if cfg!(feature = "multiple-pymethods") { - PyClassMethodsType::Inventory - } else { - PyClassMethodsType::Specialization - } -} - -trait UnwrapOrCompileError { - fn unwrap_or_compile_error(self) -> TokenStream2; -} - -impl UnwrapOrCompileError for syn::Result { - fn unwrap_or_compile_error(self) -> TokenStream2 { - self.unwrap_or_else(|e| e.into_compile_error()) - } -} diff --git a/vendor/pyo3/pyo3-runtime/LICENSE-APACHE b/vendor/pyo3/pyo3-runtime/LICENSE-APACHE deleted file mode 120000 index 965b606f33..0000000000 --- a/vendor/pyo3/pyo3-runtime/LICENSE-APACHE +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-APACHE \ No newline at end of file diff --git a/vendor/pyo3/pyo3-runtime/LICENSE-MIT b/vendor/pyo3/pyo3-runtime/LICENSE-MIT deleted file mode 120000 index 76219eb72e..0000000000 --- a/vendor/pyo3/pyo3-runtime/LICENSE-MIT +++ /dev/null @@ -1 +0,0 @@ -../LICENSE-MIT \ No newline at end of file diff --git a/vendor/pyo3/pyo3-runtime/README.md b/vendor/pyo3/pyo3-runtime/README.md deleted file mode 100644 index 6863c09819..0000000000 --- a/vendor/pyo3/pyo3-runtime/README.md +++ /dev/null @@ -1 +0,0 @@ -Coming soon! diff --git a/vendor/pyo3/pyo3-runtime/pyproject.toml b/vendor/pyo3/pyo3-runtime/pyproject.toml deleted file mode 100644 index b02c64dd88..0000000000 --- a/vendor/pyo3/pyo3-runtime/pyproject.toml +++ /dev/null @@ -1,34 +0,0 @@ -[build-system] -requires = ["hatchling"] -build-backend = "hatchling.build" - -[project] -name = "pyo3-runtime" -dynamic = ["version"] -description = '' -readme = "README.md" -requires-python = ">=3.7" -license = "MIT OR Apache-2.0" -keywords = [] -authors = [ - { name = "David Hewitt", email = "1939362+davidhewitt@users.noreply.github.com" }, -] -classifiers = [ - "Development Status :: 4 - Beta", - "Programming Language :: Python", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", -] -dependencies = [] - -[project.urls] -Homepage = "https://github.com/PyO3/pyo3" - -[tool.hatch.version] -path = "src/pyo3_runtime/__init__.py" diff --git a/vendor/pyo3/pyo3-runtime/src/pyo3_runtime/__init__.py b/vendor/pyo3/pyo3-runtime/src/pyo3_runtime/__init__.py deleted file mode 100644 index f102a9cadf..0000000000 --- a/vendor/pyo3/pyo3-runtime/src/pyo3_runtime/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "0.0.1" diff --git a/vendor/pyo3/pyo3-runtime/tests/__init__.py b/vendor/pyo3/pyo3-runtime/tests/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/vendor/pyo3/pyproject.toml b/vendor/pyo3/pyproject.toml deleted file mode 100644 index f74a09ed94..0000000000 --- a/vendor/pyo3/pyproject.toml +++ /dev/null @@ -1,26 +0,0 @@ -[tool.ruff.lint.extend-per-file-ignores] -"__init__.py" = ["F403"] - -[tool.towncrier] -filename = "CHANGELOG.md" -version = "0.23.4" -start_string = "\n" -template = ".towncrier.template.md" -title_format = "## [{version}] - {project_date}" -issue_format = "[#{issue}](https://github.com/PyO3/pyo3/pull/{issue})" # Note PyO3 shows pulls, not issues, in the CHANGELOG -underlines = ["", "", ""] - -[tool.towncrier.fragment.packaging] -name = "Packaging" - -[tool.towncrier.fragment.added] -name = "Added" - -[tool.towncrier.fragment.changed] -name = "Changed" - -[tool.towncrier.fragment.removed] -name = "Removed" - -[tool.towncrier.fragment.fixed] -name = "Fixed" diff --git a/vendor/pyo3/pytests/Cargo.toml b/vendor/pyo3/pytests/Cargo.toml deleted file mode 100644 index 1fee309327..0000000000 --- a/vendor/pyo3/pytests/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -authors = ["PyO3 Authors"] -name = "pyo3-pytests" -version = "0.1.0" -description = "Python-based tests for PyO3" -edition = "2021" -publish = false -rust-version = "1.63" - -[dependencies] -pyo3 = { path = "../", features = ["extension-module"] } - -[build-dependencies] -pyo3-build-config = { path = "../pyo3-build-config" } - -[lib] -name = "pyo3_pytests" -crate-type = ["cdylib"] - -[lints] -workspace = true diff --git a/vendor/pyo3/pytests/MANIFEST.in b/vendor/pyo3/pytests/MANIFEST.in deleted file mode 100644 index becccf7bc9..0000000000 --- a/vendor/pyo3/pytests/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include pyproject.toml Cargo.toml -recursive-include src * diff --git a/vendor/pyo3/pytests/README.md b/vendor/pyo3/pytests/README.md deleted file mode 100644 index 7ced072aa3..0000000000 --- a/vendor/pyo3/pytests/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# pyo3-pytests - -An extension module built using PyO3, used to test and benchmark PyO3 from Python. - -## Testing - -This package is intended to be built using `maturin`. Once built, you can run the tests using `pytest`: - -```shell -pip install maturin -maturin develop -pytest -``` - -Alternatively, install nox and run the tests inside an isolated environment: - -```shell -nox -``` - -## Running benchmarks - -You can install the module in your Python environment and then run the benchmarks with pytest: - -```shell -pip install . -pytest --benchmark-enable -``` - -Or with nox: - -```shell -nox -s bench -``` diff --git a/vendor/pyo3/pytests/build.rs b/vendor/pyo3/pytests/build.rs deleted file mode 100644 index 90a1aa322d..0000000000 --- a/vendor/pyo3/pytests/build.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - pyo3_build_config::use_pyo3_cfgs(); - pyo3_build_config::add_extension_module_link_args(); -} diff --git a/vendor/pyo3/pytests/conftest.py b/vendor/pyo3/pytests/conftest.py deleted file mode 100644 index ce72968935..0000000000 --- a/vendor/pyo3/pytests/conftest.py +++ /dev/null @@ -1,22 +0,0 @@ -import sysconfig -import sys -import pytest - -FREE_THREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) - -gil_enabled_at_start = True -if FREE_THREADED_BUILD: - gil_enabled_at_start = sys._is_gil_enabled() - - -def pytest_terminal_summary(terminalreporter, exitstatus, config): - if FREE_THREADED_BUILD and not gil_enabled_at_start and sys._is_gil_enabled(): - tr = terminalreporter - tr.ensure_newline() - tr.section("GIL re-enabled", sep="=", red=True, bold=True) - tr.line("The GIL was re-enabled at runtime during the tests.") - tr.line("") - tr.line("Please ensure all new modules declare support for running") - tr.line("without the GIL. Any new tests that intentionally imports ") - tr.line("code that re-enables the GIL should do so in a subprocess.") - pytest.exit("GIL re-enabled during tests", returncode=1) diff --git a/vendor/pyo3/pytests/noxfile.py b/vendor/pyo3/pytests/noxfile.py deleted file mode 100644 index f5a332c7a3..0000000000 --- a/vendor/pyo3/pytests/noxfile.py +++ /dev/null @@ -1,36 +0,0 @@ -import nox -import sys -from nox.command import CommandFailed - -nox.options.sessions = ["test"] - - -@nox.session -def test(session: nox.Session): - session.env["MATURIN_PEP517_ARGS"] = "--profile=dev" - session.run_always("python", "-m", "pip", "install", "-v", ".[dev]") - - def try_install_binary(package: str, constraint: str): - try: - session.install("--only-binary=:all:", f"{package}{constraint}") - except CommandFailed: - # No binary wheel available on this platform - pass - - try_install_binary("numpy", ">=1.16") - # https://github.com/zopefoundation/zope.interface/issues/316 - # - is a dependency of gevent - try_install_binary("zope.interface", "<7") - try_install_binary("gevent", ">=22.10.2") - ignored_paths = [] - if sys.version_info < (3, 10): - # Match syntax is only available in Python >= 3.10 - ignored_paths.append("tests/test_enums_match.py") - ignore_args = [f"--ignore={path}" for path in ignored_paths] - session.run("pytest", *ignore_args, *session.posargs) - - -@nox.session -def bench(session: nox.Session): - session.install(".[dev]") - session.run("pytest", "--benchmark-enable", "--benchmark-only", *session.posargs) diff --git a/vendor/pyo3/pytests/pyproject.toml b/vendor/pyo3/pytests/pyproject.toml deleted file mode 100644 index 5f78a57312..0000000000 --- a/vendor/pyo3/pytests/pyproject.toml +++ /dev/null @@ -1,29 +0,0 @@ -[build-system] -requires = ["maturin>=1,<2"] -build-backend = "maturin" - -[tool.pytest.ini_options] -addopts = "--benchmark-disable" - -[project] -name = "pyo3_pytests" -version = "0.1.0" -classifiers = [ - "License :: OSI Approved :: MIT License", - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "Programming Language :: Python", - "Programming Language :: Rust", - "Operating System :: POSIX", - "Operating System :: MacOS :: MacOS X", -] - -[project.optional-dependencies] -dev = [ - "hypothesis>=3.55", - "pytest-asyncio>=0.21", - "pytest-benchmark>=3.4", - # pinned < 8.1 because https://github.com/CodSpeedHQ/pytest-codspeed/issues/27 - "pytest>=7,<8.1", - "typing_extensions>=4.0.0" -] diff --git a/vendor/pyo3/pytests/src/awaitable.rs b/vendor/pyo3/pytests/src/awaitable.rs deleted file mode 100644 index fb04c33ed0..0000000000 --- a/vendor/pyo3/pytests/src/awaitable.rs +++ /dev/null @@ -1,86 +0,0 @@ -//! The following classes are examples of objects which implement Python's -//! awaitable protocol. -//! -//! Both IterAwaitable and FutureAwaitable will return a value immediately -//! when awaited, see guide examples related to pyo3-asyncio for ways -//! to suspend tasks and await results. - -use pyo3::exceptions::PyStopIteration; -use pyo3::prelude::*; - -#[pyclass] -#[derive(Debug)] -pub(crate) struct IterAwaitable { - result: Option>, -} - -#[pymethods] -impl IterAwaitable { - #[new] - fn new(result: PyObject) -> Self { - IterAwaitable { - result: Some(Ok(result)), - } - } - - fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { - pyself - } - - fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { - pyself - } - - fn __next__(&mut self, py: Python<'_>) -> PyResult { - match self.result.take() { - Some(res) => match res { - Ok(v) => Err(PyStopIteration::new_err(v)), - Err(err) => Err(err), - }, - _ => Ok(py.None()), - } - } -} - -#[pyclass] -pub(crate) struct FutureAwaitable { - #[pyo3(get, set, name = "_asyncio_future_blocking")] - py_block: bool, - result: Option>, -} - -#[pymethods] -impl FutureAwaitable { - #[new] - fn new(result: PyObject) -> Self { - FutureAwaitable { - py_block: false, - result: Some(Ok(result)), - } - } - - fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { - pyself - } - - fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { - pyself - } - - fn __next__(mut pyself: PyRefMut<'_, Self>) -> PyResult> { - match pyself.result { - Some(_) => match pyself.result.take().unwrap() { - Ok(v) => Err(PyStopIteration::new_err(v)), - Err(err) => Err(err), - }, - _ => Ok(pyself), - } - } -} - -#[pymodule(gil_used = false)] -pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/buf_and_str.rs b/vendor/pyo3/pytests/src/buf_and_str.rs deleted file mode 100644 index 15230a5e15..0000000000 --- a/vendor/pyo3/pytests/src/buf_and_str.rs +++ /dev/null @@ -1,55 +0,0 @@ -#![cfg(not(Py_LIMITED_API))] - -//! Objects related to PyBuffer and PyStr -use pyo3::buffer::PyBuffer; -use pyo3::prelude::*; -use pyo3::types::{PyBytes, PyMemoryView, PyString}; - -/// This is for confirming that PyBuffer does not cause memory leak -#[pyclass] -struct BytesExtractor {} - -#[pymethods] -impl BytesExtractor { - #[new] - pub fn __new__() -> Self { - BytesExtractor {} - } - - #[staticmethod] - pub fn from_bytes(bytes: &Bound<'_, PyBytes>) -> PyResult { - let byte_vec: Vec = bytes.extract()?; - Ok(byte_vec.len()) - } - - #[staticmethod] - pub fn from_str(string: &Bound<'_, PyString>) -> PyResult { - let rust_string: String = string.extract()?; - Ok(rust_string.len()) - } - - #[staticmethod] - pub fn from_str_lossy(string: &Bound<'_, PyString>) -> usize { - let rust_string_lossy: String = string.to_string_lossy().to_string(); - rust_string_lossy.len() - } - - #[staticmethod] - pub fn from_buffer(buf: &Bound<'_, PyAny>) -> PyResult { - let buf = PyBuffer::::get(buf)?; - Ok(buf.item_count()) - } -} - -#[pyfunction] -fn return_memoryview(py: Python<'_>) -> PyResult> { - let bytes = PyBytes::new(py, b"hello world"); - PyMemoryView::from(&bytes) -} - -#[pymodule(gil_used = false)] -pub fn buf_and_str(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_function(wrap_pyfunction!(return_memoryview, m)?)?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/comparisons.rs b/vendor/pyo3/pytests/src/comparisons.rs deleted file mode 100644 index 4ed79e4279..0000000000 --- a/vendor/pyo3/pytests/src/comparisons.rs +++ /dev/null @@ -1,123 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct Eq(i64); - -#[pymethods] -impl Eq { - #[new] - fn new(value: i64) -> Self { - Self(value) - } - - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } - - fn __ne__(&self, other: &Self) -> bool { - self.0 != other.0 - } -} - -#[pyclass] -struct EqDefaultNe(i64); - -#[pymethods] -impl EqDefaultNe { - #[new] - fn new(value: i64) -> Self { - Self(value) - } - - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -#[pyclass(eq)] -#[derive(PartialEq, Eq)] -struct EqDerived(i64); - -#[pymethods] -impl EqDerived { - #[new] - fn new(value: i64) -> Self { - Self(value) - } -} - -#[pyclass] -struct Ordered(i64); - -#[pymethods] -impl Ordered { - #[new] - fn new(value: i64) -> Self { - Self(value) - } - - fn __lt__(&self, other: &Self) -> bool { - self.0 < other.0 - } - - fn __le__(&self, other: &Self) -> bool { - self.0 <= other.0 - } - - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } - - fn __ne__(&self, other: &Self) -> bool { - self.0 != other.0 - } - - fn __gt__(&self, other: &Self) -> bool { - self.0 > other.0 - } - - fn __ge__(&self, other: &Self) -> bool { - self.0 >= other.0 - } -} - -#[pyclass] -struct OrderedDefaultNe(i64); - -#[pymethods] -impl OrderedDefaultNe { - #[new] - fn new(value: i64) -> Self { - Self(value) - } - - fn __lt__(&self, other: &Self) -> bool { - self.0 < other.0 - } - - fn __le__(&self, other: &Self) -> bool { - self.0 <= other.0 - } - - fn __eq__(&self, other: &Self) -> bool { - self.0 == other.0 - } - - fn __gt__(&self, other: &Self) -> bool { - self.0 > other.0 - } - - fn __ge__(&self, other: &Self) -> bool { - self.0 >= other.0 - } -} - -#[pymodule(gil_used = false)] -pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/datetime.rs b/vendor/pyo3/pytests/src/datetime.rs deleted file mode 100644 index 5162b3508a..0000000000 --- a/vendor/pyo3/pytests/src/datetime.rs +++ /dev/null @@ -1,228 +0,0 @@ -#![cfg(not(Py_LIMITED_API))] - -use pyo3::prelude::*; -use pyo3::types::{ - PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, PyTuple, - PyTzInfo, PyTzInfoAccess, -}; - -#[pyfunction] -fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult> { - PyDate::new(py, year, month, day) -} - -#[pyfunction] -fn get_date_tuple<'py>(d: &Bound<'py, PyDate>) -> PyResult> { - PyTuple::new( - d.py(), - [d.get_year(), d.get_month() as i32, d.get_day() as i32], - ) -} - -#[pyfunction] -fn date_from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult> { - PyDate::from_timestamp(py, timestamp) -} - -#[pyfunction] -#[pyo3(signature=(hour, minute, second, microsecond, tzinfo=None))] -fn make_time<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, -) -> PyResult> { - PyTime::new(py, hour, minute, second, microsecond, tzinfo) -} - -#[pyfunction] -#[pyo3(signature = (hour, minute, second, microsecond, tzinfo, fold))] -fn time_with_fold<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - fold: bool, -) -> PyResult> { - PyTime::new_with_fold(py, hour, minute, second, microsecond, tzinfo, fold) -} - -#[pyfunction] -fn get_time_tuple<'py>(dt: &Bound<'py, PyTime>) -> PyResult> { - PyTuple::new( - dt.py(), - [ - dt.get_hour() as u32, - dt.get_minute() as u32, - dt.get_second() as u32, - dt.get_microsecond(), - ], - ) -} - -#[pyfunction] -fn get_time_tuple_fold<'py>(dt: &Bound<'py, PyTime>) -> PyResult> { - PyTuple::new( - dt.py(), - [ - dt.get_hour() as u32, - dt.get_minute() as u32, - dt.get_second() as u32, - dt.get_microsecond(), - dt.get_fold() as u32, - ], - ) -} - -#[pyfunction] -fn make_delta( - py: Python<'_>, - days: i32, - seconds: i32, - microseconds: i32, -) -> PyResult> { - PyDelta::new(py, days, seconds, microseconds, true) -} - -#[pyfunction] -fn get_delta_tuple<'py>(delta: &Bound<'py, PyDelta>) -> PyResult> { - PyTuple::new( - delta.py(), - [ - delta.get_days(), - delta.get_seconds(), - delta.get_microseconds(), - ], - ) -} - -#[allow(clippy::too_many_arguments)] -#[pyfunction] -#[pyo3(signature=(year, month, day, hour, minute, second, microsecond, tzinfo=None))] -fn make_datetime<'py>( - py: Python<'py>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, -) -> PyResult> { - PyDateTime::new( - py, - year, - month, - day, - hour, - minute, - second, - microsecond, - tzinfo, - ) -} - -#[pyfunction] -fn get_datetime_tuple<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult> { - PyTuple::new( - dt.py(), - [ - dt.get_year(), - dt.get_month() as i32, - dt.get_day() as i32, - dt.get_hour() as i32, - dt.get_minute() as i32, - dt.get_second() as i32, - dt.get_microsecond() as i32, - ], - ) -} - -#[pyfunction] -fn get_datetime_tuple_fold<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult> { - PyTuple::new( - dt.py(), - [ - dt.get_year(), - dt.get_month() as i32, - dt.get_day() as i32, - dt.get_hour() as i32, - dt.get_minute() as i32, - dt.get_second() as i32, - dt.get_microsecond() as i32, - dt.get_fold() as i32, - ], - ) -} - -#[pyfunction] -#[pyo3(signature=(ts, tz=None))] -fn datetime_from_timestamp<'py>( - py: Python<'py>, - ts: f64, - tz: Option<&Bound<'py, PyTzInfo>>, -) -> PyResult> { - PyDateTime::from_timestamp(py, ts, tz) -} - -#[pyfunction] -fn get_datetime_tzinfo<'py>(dt: &Bound<'py, PyDateTime>) -> Option> { - dt.get_tzinfo() -} - -#[pyfunction] -fn get_time_tzinfo<'py>(dt: &Bound<'py, PyTime>) -> Option> { - dt.get_tzinfo() -} - -#[pyclass(extends=PyTzInfo)] -pub struct TzClass {} - -#[pymethods] -impl TzClass { - #[new] - fn new() -> Self { - TzClass {} - } - - fn utcoffset<'py>(&self, dt: &Bound<'py, PyDateTime>) -> PyResult> { - PyDelta::new(dt.py(), 0, 3600, 0, true) - } - - fn tzname(&self, _dt: &Bound<'_, PyDateTime>) -> String { - String::from("+01:00") - } - - fn dst<'py>(&self, _dt: &Bound<'py, PyDateTime>) -> Option> { - None - } -} - -#[pymodule(gil_used = false)] -pub fn datetime(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(make_date, m)?)?; - m.add_function(wrap_pyfunction!(get_date_tuple, m)?)?; - m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?; - m.add_function(wrap_pyfunction!(make_time, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tuple, m)?)?; - m.add_function(wrap_pyfunction!(make_delta, m)?)?; - m.add_function(wrap_pyfunction!(get_delta_tuple, m)?)?; - m.add_function(wrap_pyfunction!(make_datetime, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tuple, m)?)?; - m.add_function(wrap_pyfunction!(datetime_from_timestamp, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tzinfo, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tzinfo, m)?)?; - - m.add_function(wrap_pyfunction!(time_with_fold, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tuple_fold, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tuple_fold, m)?)?; - - m.add_class::()?; - - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/dict_iter.rs b/vendor/pyo3/pytests/src/dict_iter.rs deleted file mode 100644 index c312fbb5f8..0000000000 --- a/vendor/pyo3/pytests/src/dict_iter.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::exceptions::PyRuntimeError; -use pyo3::prelude::*; -use pyo3::types::PyDict; - -#[pymodule] -pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} - -#[pyclass] -pub struct DictSize { - expected: u32, -} - -#[pymethods] -impl DictSize { - #[new] - fn new(expected: u32) -> Self { - DictSize { expected } - } - - fn iter_dict(&mut self, _py: Python<'_>, dict: &Bound<'_, PyDict>) -> PyResult { - let mut seen = 0u32; - for (sym, values) in dict { - seen += 1; - println!( - "{:4}/{:4} iterations:{}=>{}", - seen, self.expected, sym, values - ); - } - - if seen == self.expected { - Ok(seen) - } else { - Err(PyErr::new::(format!( - "Expected {} iterations - performed {}", - self.expected, seen - ))) - } - } -} diff --git a/vendor/pyo3/pytests/src/enums.rs b/vendor/pyo3/pytests/src/enums.rs deleted file mode 100644 index 8652321700..0000000000 --- a/vendor/pyo3/pytests/src/enums.rs +++ /dev/null @@ -1,124 +0,0 @@ -use pyo3::{ - pyclass, pyfunction, pymodule, - types::{PyModule, PyModuleMethods}, - wrap_pyfunction, Bound, PyResult, -}; - -#[pymodule(gil_used = false)] -pub fn enums(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_complex_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_tuple_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_mixed_complex_stuff))?; - Ok(()) -} - -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -pub enum SimpleEnum { - Sunday, - Monday, - Tuesday, - Wednesday, - Thursday, - Friday, - Saturday, -} - -#[pyfunction] -pub fn do_simple_stuff(thing: &SimpleEnum) -> SimpleEnum { - match thing { - SimpleEnum::Sunday => SimpleEnum::Monday, - SimpleEnum::Monday => SimpleEnum::Tuesday, - SimpleEnum::Tuesday => SimpleEnum::Wednesday, - SimpleEnum::Wednesday => SimpleEnum::Thursday, - SimpleEnum::Thursday => SimpleEnum::Friday, - SimpleEnum::Friday => SimpleEnum::Saturday, - SimpleEnum::Saturday => SimpleEnum::Sunday, - } -} - -#[pyclass] -pub enum ComplexEnum { - Int { - i: i32, - }, - Float { - f: f64, - }, - Str { - s: String, - }, - EmptyStruct {}, - MultiFieldStruct { - a: i32, - b: f64, - c: bool, - }, - #[pyo3(constructor = (a = 42, b = None))] - VariantWithDefault { - a: i32, - b: Option, - }, -} - -#[pyfunction] -pub fn do_complex_stuff(thing: &ComplexEnum) -> ComplexEnum { - match thing { - ComplexEnum::Int { i } => ComplexEnum::Str { s: i.to_string() }, - ComplexEnum::Float { f } => ComplexEnum::Float { f: f * f }, - ComplexEnum::Str { s } => ComplexEnum::Int { i: s.len() as i32 }, - ComplexEnum::EmptyStruct {} => ComplexEnum::EmptyStruct {}, - ComplexEnum::MultiFieldStruct { a, b, c } => ComplexEnum::MultiFieldStruct { - a: *a, - b: *b, - c: *c, - }, - ComplexEnum::VariantWithDefault { a, b } => ComplexEnum::VariantWithDefault { - a: 2 * a, - b: b.as_ref().map(|s| s.to_uppercase()), - }, - } -} - -#[pyclass] -enum SimpleTupleEnum { - Int(i32), - Str(String), -} - -#[pyclass] -pub enum TupleEnum { - #[pyo3(constructor = (_0 = 1, _1 = 1.0, _2 = true))] - FullWithDefault(i32, f64, bool), - Full(i32, f64, bool), - EmptyTuple(), -} - -#[pyfunction] -pub fn do_tuple_stuff(thing: &TupleEnum) -> TupleEnum { - match thing { - TupleEnum::FullWithDefault(a, b, c) => TupleEnum::FullWithDefault(*a, *b, *c), - TupleEnum::Full(a, b, c) => TupleEnum::Full(*a, *b, *c), - TupleEnum::EmptyTuple() => TupleEnum::EmptyTuple(), - } -} - -#[pyclass] -pub enum MixedComplexEnum { - Nothing {}, - Empty(), -} - -#[pyfunction] -pub fn do_mixed_complex_stuff(thing: &MixedComplexEnum) -> MixedComplexEnum { - match thing { - MixedComplexEnum::Nothing {} => MixedComplexEnum::Empty(), - MixedComplexEnum::Empty() => MixedComplexEnum::Nothing {}, - } -} diff --git a/vendor/pyo3/pytests/src/lib.rs b/vendor/pyo3/pytests/src/lib.rs deleted file mode 100644 index b6c32230da..0000000000 --- a/vendor/pyo3/pytests/src/lib.rs +++ /dev/null @@ -1,60 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyDict; -use pyo3::wrap_pymodule; - -pub mod awaitable; -pub mod buf_and_str; -pub mod comparisons; -pub mod datetime; -pub mod dict_iter; -pub mod enums; -pub mod misc; -pub mod objstore; -pub mod othermod; -pub mod path; -pub mod pyclasses; -pub mod pyfunctions; -pub mod sequence; -pub mod subclassing; - -#[pymodule(gil_used = false)] -fn pyo3_pytests(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_wrapped(wrap_pymodule!(awaitable::awaitable))?; - #[cfg(not(Py_LIMITED_API))] - m.add_wrapped(wrap_pymodule!(buf_and_str::buf_and_str))?; - m.add_wrapped(wrap_pymodule!(comparisons::comparisons))?; - #[cfg(not(Py_LIMITED_API))] - m.add_wrapped(wrap_pymodule!(datetime::datetime))?; - m.add_wrapped(wrap_pymodule!(dict_iter::dict_iter))?; - m.add_wrapped(wrap_pymodule!(enums::enums))?; - m.add_wrapped(wrap_pymodule!(misc::misc))?; - m.add_wrapped(wrap_pymodule!(objstore::objstore))?; - m.add_wrapped(wrap_pymodule!(othermod::othermod))?; - m.add_wrapped(wrap_pymodule!(path::path))?; - m.add_wrapped(wrap_pymodule!(pyclasses::pyclasses))?; - m.add_wrapped(wrap_pymodule!(pyfunctions::pyfunctions))?; - m.add_wrapped(wrap_pymodule!(sequence::sequence))?; - m.add_wrapped(wrap_pymodule!(subclassing::subclassing))?; - - // Inserting to sys.modules allows importing submodules nicely from Python - // e.g. import pyo3_pytests.buf_and_str as bas - - let sys = PyModule::import(py, "sys")?; - let sys_modules = sys.getattr("modules")?.downcast_into::()?; - sys_modules.set_item("pyo3_pytests.awaitable", m.getattr("awaitable")?)?; - sys_modules.set_item("pyo3_pytests.buf_and_str", m.getattr("buf_and_str")?)?; - sys_modules.set_item("pyo3_pytests.comparisons", m.getattr("comparisons")?)?; - sys_modules.set_item("pyo3_pytests.datetime", m.getattr("datetime")?)?; - sys_modules.set_item("pyo3_pytests.dict_iter", m.getattr("dict_iter")?)?; - sys_modules.set_item("pyo3_pytests.enums", m.getattr("enums")?)?; - sys_modules.set_item("pyo3_pytests.misc", m.getattr("misc")?)?; - sys_modules.set_item("pyo3_pytests.objstore", m.getattr("objstore")?)?; - sys_modules.set_item("pyo3_pytests.othermod", m.getattr("othermod")?)?; - sys_modules.set_item("pyo3_pytests.path", m.getattr("path")?)?; - sys_modules.set_item("pyo3_pytests.pyclasses", m.getattr("pyclasses")?)?; - sys_modules.set_item("pyo3_pytests.pyfunctions", m.getattr("pyfunctions")?)?; - sys_modules.set_item("pyo3_pytests.sequence", m.getattr("sequence")?)?; - sys_modules.set_item("pyo3_pytests.subclassing", m.getattr("subclassing")?)?; - - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/misc.rs b/vendor/pyo3/pytests/src/misc.rs deleted file mode 100644 index e44d1aa0ec..0000000000 --- a/vendor/pyo3/pytests/src/misc.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::{ - prelude::*, - types::{PyDict, PyString}, -}; - -#[pyfunction] -fn issue_219() { - // issue 219: acquiring GIL inside #[pyfunction] deadlocks. - Python::with_gil(|_| {}); -} - -#[pyfunction] -fn get_type_fully_qualified_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult> { - obj.get_type().fully_qualified_name() -} - -#[pyfunction] -fn accepts_bool(val: bool) -> bool { - val -} - -#[pyfunction] -fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny>) -> PyResult<()> { - // This function gives the opportunity to run a pure-Python callback so that - // gevent can instigate a context switch. This had problematic interactions - // with PyO3's removed "GIL Pool". - // For context, see https://github.com/PyO3/pyo3/issues/3668 - let item = dict.get_item("key")?.expect("key not found in dict"); - let string = item.to_string(); - callback.call0()?; - assert_eq!(item.to_string(), string); - Ok(()) -} - -#[pymodule(gil_used = false)] -pub fn misc(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(issue_219, m)?)?; - m.add_function(wrap_pyfunction!(get_type_fully_qualified_name, m)?)?; - m.add_function(wrap_pyfunction!(accepts_bool, m)?)?; - m.add_function(wrap_pyfunction!(get_item_and_run_callback, m)?)?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/objstore.rs b/vendor/pyo3/pytests/src/objstore.rs deleted file mode 100644 index 8e72905299..0000000000 --- a/vendor/pyo3/pytests/src/objstore.rs +++ /dev/null @@ -1,24 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -#[derive(Default)] -pub struct ObjStore { - obj: Vec, -} - -#[pymethods] -impl ObjStore { - #[new] - fn new() -> Self { - ObjStore::default() - } - - fn push(&mut self, obj: &Bound<'_, PyAny>) { - self.obj.push(obj.clone().unbind()); - } -} - -#[pymodule(gil_used = false)] -pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::() -} diff --git a/vendor/pyo3/pytests/src/othermod.rs b/vendor/pyo3/pytests/src/othermod.rs deleted file mode 100644 index 0de912d7d0..0000000000 --- a/vendor/pyo3/pytests/src/othermod.rs +++ /dev/null @@ -1,41 +0,0 @@ -//! -//! -//! The code below just tries to use the most important code generation paths - -use pyo3::prelude::*; - -#[pyclass] -pub struct ModClass { - _somefield: String, -} - -#[pymethods] -impl ModClass { - #[new] - fn new() -> Self { - ModClass { - _somefield: String::from("contents"), - } - } - - fn noop(&self, x: usize) -> usize { - x - } -} - -#[pyfunction] -fn double(x: i32) -> i32 { - x * 2 -} - -#[pymodule(gil_used = false)] -pub fn othermod(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; - - m.add_class::()?; - - m.add("USIZE_MIN", usize::MIN)?; - m.add("USIZE_MAX", usize::MAX)?; - - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/path.rs b/vendor/pyo3/pytests/src/path.rs deleted file mode 100644 index b52c038ed3..0000000000 --- a/vendor/pyo3/pytests/src/path.rs +++ /dev/null @@ -1,20 +0,0 @@ -use pyo3::prelude::*; -use std::path::{Path, PathBuf}; - -#[pyfunction] -fn make_path() -> PathBuf { - Path::new("/root").to_owned() -} - -#[pyfunction] -fn take_pathbuf(path: PathBuf) -> PathBuf { - path -} - -#[pymodule(gil_used = false)] -pub fn path(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(make_path, m)?)?; - m.add_function(wrap_pyfunction!(take_pathbuf, m)?)?; - - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/pyclasses.rs b/vendor/pyo3/pytests/src/pyclasses.rs deleted file mode 100644 index 3af08c053c..0000000000 --- a/vendor/pyo3/pytests/src/pyclasses.rs +++ /dev/null @@ -1,118 +0,0 @@ -use std::{thread, time}; - -use pyo3::exceptions::{PyStopIteration, PyValueError}; -use pyo3::prelude::*; -use pyo3::types::PyType; - -#[pyclass] -struct EmptyClass {} - -#[pymethods] -impl EmptyClass { - #[new] - fn new() -> Self { - EmptyClass {} - } - - fn method(&self) {} - - fn __len__(&self) -> usize { - 0 - } -} - -/// This is for demonstrating how to return a value from __next__ -#[pyclass] -#[derive(Default)] -struct PyClassIter { - count: usize, -} - -#[pymethods] -impl PyClassIter { - #[new] - pub fn new() -> Self { - Default::default() - } - - fn __next__(&mut self) -> PyResult { - if self.count < 5 { - self.count += 1; - Ok(self.count) - } else { - Err(PyStopIteration::new_err("Ended")) - } - } -} - -#[pyclass] -#[derive(Default)] -struct PyClassThreadIter { - count: usize, -} - -#[pymethods] -impl PyClassThreadIter { - #[new] - pub fn new() -> Self { - Default::default() - } - - fn __next__(&mut self, py: Python<'_>) -> usize { - let current_count = self.count; - self.count += 1; - if current_count == 0 { - py.allow_threads(|| thread::sleep(time::Duration::from_millis(100))); - } - self.count - } -} - -/// Demonstrates a base class which can operate on the relevant subclass in its constructor. -#[pyclass(subclass)] -#[derive(Clone, Debug)] -struct AssertingBaseClass; - -#[pymethods] -impl AssertingBaseClass { - #[new] - #[classmethod] - fn new(cls: &Bound<'_, PyType>, expected_type: Bound<'_, PyType>) -> PyResult { - if !cls.is(&expected_type) { - return Err(PyValueError::new_err(format!( - "{:?} != {:?}", - cls, expected_type - ))); - } - Ok(Self) - } -} - -#[pyclass] -struct ClassWithoutConstructor; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -#[pyclass(dict)] -struct ClassWithDict; - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -#[pymethods] -impl ClassWithDict { - #[new] - fn new() -> Self { - ClassWithDict - } -} - -#[pymodule(gil_used = false)] -pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - m.add_class::()?; - - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/pyfunctions.rs b/vendor/pyo3/pytests/src/pyfunctions.rs deleted file mode 100644 index 024641d3d2..0000000000 --- a/vendor/pyo3/pytests/src/pyfunctions.rs +++ /dev/null @@ -1,79 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; - -#[pyfunction(signature = ())] -fn none() {} - -type Any<'py> = Bound<'py, PyAny>; -type Dict<'py> = Bound<'py, PyDict>; -type Tuple<'py> = Bound<'py, PyTuple>; - -#[pyfunction(signature = (a, b = None, *, c = None))] -fn simple<'py>( - a: Any<'py>, - b: Option>, - c: Option>, -) -> (Any<'py>, Option>, Option>) { - (a, b, c) -} - -#[pyfunction(signature = (a, b = None, *args, c = None))] -fn simple_args<'py>( - a: Any<'py>, - b: Option>, - args: Tuple<'py>, - c: Option>, -) -> (Any<'py>, Option>, Tuple<'py>, Option>) { - (a, b, args, c) -} - -#[pyfunction(signature = (a, b = None, c = None, **kwargs))] -fn simple_kwargs<'py>( - a: Any<'py>, - b: Option>, - c: Option>, - kwargs: Option>, -) -> ( - Any<'py>, - Option>, - Option>, - Option>, -) { - (a, b, c, kwargs) -} - -#[pyfunction(signature = (a, b = None, *args, c = None, **kwargs))] -fn simple_args_kwargs<'py>( - a: Any<'py>, - b: Option>, - args: Tuple<'py>, - c: Option>, - kwargs: Option>, -) -> ( - Any<'py>, - Option>, - Tuple<'py>, - Option>, - Option>, -) { - (a, b, args, c, kwargs) -} - -#[pyfunction(signature = (*args, **kwargs))] -fn args_kwargs<'py>( - args: Tuple<'py>, - kwargs: Option>, -) -> (Tuple<'py>, Option>) { - (args, kwargs) -} - -#[pymodule(gil_used = false)] -pub fn pyfunctions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(none, m)?)?; - m.add_function(wrap_pyfunction!(simple, m)?)?; - m.add_function(wrap_pyfunction!(simple_args, m)?)?; - m.add_function(wrap_pyfunction!(simple_kwargs, m)?)?; - m.add_function(wrap_pyfunction!(simple_args_kwargs, m)?)?; - m.add_function(wrap_pyfunction!(args_kwargs, m)?)?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/sequence.rs b/vendor/pyo3/pytests/src/sequence.rs deleted file mode 100644 index 175f5fba8a..0000000000 --- a/vendor/pyo3/pytests/src/sequence.rs +++ /dev/null @@ -1,25 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyString; - -#[pyfunction] -fn vec_to_vec_i32(vec: Vec) -> Vec { - vec -} - -#[pyfunction] -fn array_to_array_i32(arr: [i32; 3]) -> [i32; 3] { - arr -} - -#[pyfunction] -fn vec_to_vec_pystring(vec: Vec>) -> Vec> { - vec -} - -#[pymodule(gil_used = false)] -pub fn sequence(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(vec_to_vec_i32, m)?)?; - m.add_function(wrap_pyfunction!(array_to_array_i32, m)?)?; - m.add_function(wrap_pyfunction!(vec_to_vec_pystring, m)?)?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/src/subclassing.rs b/vendor/pyo3/pytests/src/subclassing.rs deleted file mode 100644 index 0f00e74c19..0000000000 --- a/vendor/pyo3/pytests/src/subclassing.rs +++ /dev/null @@ -1,24 +0,0 @@ -//! Test for [#220](https://github.com/PyO3/pyo3/issues/220) - -use pyo3::prelude::*; - -#[pyclass(subclass)] -pub struct Subclassable {} - -#[pymethods] -impl Subclassable { - #[new] - fn new() -> Self { - Subclassable {} - } - - fn __str__(&self) -> &'static str { - "Subclassable" - } -} - -#[pymodule(gil_used = false)] -pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - Ok(()) -} diff --git a/vendor/pyo3/pytests/tests/test_awaitable.py b/vendor/pyo3/pytests/tests/test_awaitable.py deleted file mode 100644 index 40f1e1cc01..0000000000 --- a/vendor/pyo3/pytests/tests/test_awaitable.py +++ /dev/null @@ -1,22 +0,0 @@ -import pytest -import sys - -from pyo3_pytests.awaitable import IterAwaitable, FutureAwaitable - - -@pytest.mark.skipif( - sys.implementation.name == "graalpy", - reason="GraalPy's asyncio module has a bug with native classes, see oracle/graalpython#365", -) -@pytest.mark.asyncio -async def test_iter_awaitable(): - assert await IterAwaitable(5) == 5 - - -@pytest.mark.skipif( - sys.implementation.name == "graalpy", - reason="GraalPy's asyncio module has a bug with native classes, see oracle/graalpython#365", -) -@pytest.mark.asyncio -async def test_future_awaitable(): - assert await FutureAwaitable(5) == 5 diff --git a/vendor/pyo3/pytests/tests/test_buf_and_str.py b/vendor/pyo3/pytests/tests/test_buf_and_str.py deleted file mode 100644 index 08346c8489..0000000000 --- a/vendor/pyo3/pytests/tests/test_buf_and_str.py +++ /dev/null @@ -1,36 +0,0 @@ -from pyo3_pytests.buf_and_str import BytesExtractor, return_memoryview - - -def test_extract_bytes(): - extractor = BytesExtractor() - message = b'\\(-"-;) A message written in bytes' - assert extractor.from_bytes(message) == len(message) - - -def test_extract_str(): - extractor = BytesExtractor() - message = '\\(-"-;) A message written as a string' - assert extractor.from_str(message) == len(message) - - -def test_extract_str_lossy(): - extractor = BytesExtractor() - message = '\\(-"-;) A message written with a trailing surrogate \ud800' - rust_surrogate_len = extractor.from_str_lossy("\ud800") - assert extractor.from_str_lossy(message) == len(message) - 1 + rust_surrogate_len - - -def test_extract_buffer(): - extractor = BytesExtractor() - message = b'\\(-"-;) A message written in bytes' - assert extractor.from_buffer(message) == len(message) - - arr = bytearray(b'\\(-"-;) A message written in bytes') - assert extractor.from_buffer(arr) == len(arr) - - -def test_return_memoryview(): - view = return_memoryview() - assert view.readonly - assert view.contiguous - assert view.tobytes() == b"hello world" diff --git a/vendor/pyo3/pytests/tests/test_comparisons.py b/vendor/pyo3/pytests/tests/test_comparisons.py deleted file mode 100644 index 50bba81cb1..0000000000 --- a/vendor/pyo3/pytests/tests/test_comparisons.py +++ /dev/null @@ -1,206 +0,0 @@ -from typing import Type, Union - -import pytest -from pyo3_pytests.comparisons import ( - Eq, - EqDefaultNe, - EqDerived, - Ordered, - OrderedDefaultNe, -) -from typing_extensions import Self - - -class PyEq: - def __init__(self, x: int) -> None: - self.x = x - - def __eq__(self, other: object) -> bool: - if isinstance(other, self.__class__): - return self.x == other.x - else: - return NotImplemented - - def __ne__(self, other: Self) -> bool: - if isinstance(other, self.__class__): - return self.x != other.x - else: - return NotImplemented - - -@pytest.mark.parametrize( - "ty", (Eq, EqDerived, PyEq), ids=("rust", "rust-derived", "python") -) -def test_eq(ty: Type[Union[Eq, EqDerived, PyEq]]): - a = ty(0) - b = ty(0) - c = ty(1) - - assert a == b - assert not (a != b) - assert a != c - assert not (a == c) - - assert b == a - assert not (a != b) - assert b != c - assert not (b == c) - - assert not a == 0 - assert a != 0 - assert not b == 0 - assert b != 1 - assert not c == 1 - assert c != 1 - - with pytest.raises(TypeError): - assert a <= b - - with pytest.raises(TypeError): - assert a >= b - - with pytest.raises(TypeError): - assert a < c - - with pytest.raises(TypeError): - assert c > a - - -class PyEqDefaultNe: - def __init__(self, x: int) -> None: - self.x = x - - def __eq__(self, other: Self) -> bool: - return self.x == other.x - - -@pytest.mark.parametrize("ty", (EqDefaultNe, PyEqDefaultNe), ids=("rust", "python")) -def test_eq_default_ne(ty: Type[Union[EqDefaultNe, PyEqDefaultNe]]): - a = ty(0) - b = ty(0) - c = ty(1) - - assert a == b - assert not (a != b) - assert a != c - assert not (a == c) - - assert b == a - assert not (a != b) - assert b != c - assert not (b == c) - - with pytest.raises(TypeError): - assert a <= b - - with pytest.raises(TypeError): - assert a >= b - - with pytest.raises(TypeError): - assert a < c - - with pytest.raises(TypeError): - assert c > a - - -class PyOrdered: - def __init__(self, x: int) -> None: - self.x = x - - def __lt__(self, other: Self) -> bool: - return self.x < other.x - - def __le__(self, other: Self) -> bool: - return self.x <= other.x - - def __eq__(self, other: Self) -> bool: - return self.x == other.x - - def __ne__(self, other: Self) -> bool: - return self.x != other.x - - def __gt__(self, other: Self) -> bool: - return self.x >= other.x - - def __ge__(self, other: Self) -> bool: - return self.x >= other.x - - -@pytest.mark.parametrize("ty", (Ordered, PyOrdered), ids=("rust", "python")) -def test_ordered(ty: Type[Union[Ordered, PyOrdered]]): - a = ty(0) - b = ty(0) - c = ty(1) - - assert a == b - assert a <= b - assert a >= b - assert a != c - assert a <= c - - assert b == a - assert b <= a - assert b >= a - assert b != c - assert b <= c - - assert c != a - assert c != b - assert c > a - assert c >= a - assert c > b - assert c >= b - - -class PyOrderedDefaultNe: - def __init__(self, x: int) -> None: - self.x = x - - def __lt__(self, other: Self) -> bool: - return self.x < other.x - - def __le__(self, other: Self) -> bool: - return self.x <= other.x - - def __eq__(self, other: Self) -> bool: - return self.x == other.x - - def __gt__(self, other: Self) -> bool: - return self.x >= other.x - - def __ge__(self, other: Self) -> bool: - return self.x >= other.x - - -@pytest.mark.parametrize( - "ty", (OrderedDefaultNe, PyOrderedDefaultNe), ids=("rust", "python") -) -def test_ordered_default_ne(ty: Type[Union[OrderedDefaultNe, PyOrderedDefaultNe]]): - a = ty(0) - b = ty(0) - c = ty(1) - - assert a == b - assert not (a != b) - assert a <= b - assert a >= b - assert a != c - assert not (a == c) - assert a <= c - - assert b == a - assert not (b != a) - assert b <= a - assert b >= a - assert b != c - assert not (b == c) - assert b <= c - - assert c != a - assert not (c == a) - assert c != b - assert not (c == b) - assert c > a - assert c >= a - assert c > b - assert c >= b diff --git a/vendor/pyo3/pytests/tests/test_datetime.py b/vendor/pyo3/pytests/tests/test_datetime.py deleted file mode 100644 index 6484b926a9..0000000000 --- a/vendor/pyo3/pytests/tests/test_datetime.py +++ /dev/null @@ -1,330 +0,0 @@ -import datetime as pdt -import platform -import re -import struct -import sys - -import pyo3_pytests.datetime as rdt -import pytest -from hypothesis import example, given -from hypothesis import strategies as st - - -# Constants -def _get_utc(): - timezone = getattr(pdt, "timezone", None) - if timezone: - return timezone.utc - else: - - class UTC(pdt.tzinfo): - def utcoffset(self, dt): - return pdt.timedelta(0) - - def dst(self, dt): - return pdt.timedelta(0) - - def tzname(self, dt): - return "UTC" - - return UTC() - - -UTC = _get_utc() - -MAX_SECONDS = int(pdt.timedelta.max.total_seconds()) -MIN_SECONDS = int(pdt.timedelta.min.total_seconds()) - -MAX_DAYS = pdt.timedelta.max // pdt.timedelta(days=1) -MIN_DAYS = pdt.timedelta.min // pdt.timedelta(days=1) - -MAX_MICROSECONDS = int(pdt.timedelta.max.total_seconds() * 1e6) -MIN_MICROSECONDS = int(pdt.timedelta.min.total_seconds() * 1e6) - -# The reason we don't use platform.architecture() here is that it's not -# reliable on macOS. See https://stackoverflow.com/a/1405971/823869. Similarly, -# sys.maxsize is not reliable on Windows. See -# https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os/1405971#comment6209952_1405971 -# and https://stackoverflow.com/a/3411134/823869. -_pointer_size = struct.calcsize("P") -if _pointer_size == 8: - IS_32_BIT = False -elif _pointer_size == 4: - IS_32_BIT = True -else: - raise RuntimeError("unexpected pointer size: " + repr(_pointer_size)) -IS_WINDOWS = sys.platform == "win32" - -if IS_WINDOWS: - MIN_DATETIME = pdt.datetime(1970, 1, 1, 0, 0, 0) - if IS_32_BIT: - MAX_DATETIME = pdt.datetime(2038, 1, 18, 23, 59, 59) - else: - MAX_DATETIME = pdt.datetime(3000, 12, 31, 23, 59, 59) -else: - if IS_32_BIT: - # TS ±2147483648 (2**31) - MIN_DATETIME = pdt.datetime(1901, 12, 13, 20, 45, 52) - MAX_DATETIME = pdt.datetime(2038, 1, 19, 3, 14, 8) - else: - MIN_DATETIME = pdt.datetime(1, 1, 2, 0, 0) - MAX_DATETIME = pdt.datetime(9999, 12, 31, 18, 59, 59) - -PYPY = platform.python_implementation() == "PyPy" - - -# Tests -def test_date(): - assert rdt.make_date(2017, 9, 1) == pdt.date(2017, 9, 1) - - -@given(d=st.dates()) -def test_date_accessors(d): - act = rdt.get_date_tuple(d) - exp = (d.year, d.month, d.day) - - assert act == exp - - -def test_invalid_date_fails(): - with pytest.raises(ValueError): - rdt.make_date(2017, 2, 30) - - -@given(d=st.dates(MIN_DATETIME.date(), MAX_DATETIME.date())) -def test_date_from_timestamp(d): - try: - ts = pdt.datetime.timestamp(d) - except Exception: - # out of range for timestamp - return - - try: - expected = pdt.date.fromtimestamp(ts) - except Exception as pdt_fail: - # date from timestamp failed; expect the same from Rust binding - with pytest.raises(type(pdt_fail)) as exc_info: - rdt.date_from_timestamp(ts) - assert str(exc_info.value) == str(pdt_fail) - else: - assert rdt.date_from_timestamp(int(ts)) == expected - - -@pytest.mark.parametrize( - "args, kwargs", - [ - ((0, 0, 0, 0, None), {}), - ((1, 12, 14, 124731), {}), - ((1, 12, 14, 124731), {"tzinfo": UTC}), - ], -) -def test_time(args, kwargs): - act = rdt.make_time(*args, **kwargs) - exp = pdt.time(*args, **kwargs) - - assert act == exp - assert act.tzinfo is exp.tzinfo - assert rdt.get_time_tzinfo(act) == exp.tzinfo - - -@given(t=st.times()) -def test_time_hypothesis(t): - act = rdt.get_time_tuple(t) - exp = (t.hour, t.minute, t.second, t.microsecond) - - assert act == exp - - -@given(t=st.times()) -def test_time_tuple_fold(t): - t_nofold = t.replace(fold=0) - t_fold = t.replace(fold=1) - - for t in (t_nofold, t_fold): - act = rdt.get_time_tuple_fold(t) - exp = (t.hour, t.minute, t.second, t.microsecond, t.fold) - - assert act == exp - - -@pytest.mark.parametrize("fold", [False, True]) -def test_time_with_fold(fold): - t = rdt.time_with_fold(0, 0, 0, 0, None, fold) - assert t.fold == fold - - -@pytest.mark.parametrize( - "args", [(-1, 0, 0, 0), (0, -1, 0, 0), (0, 0, -1, 0), (0, 0, 0, -1)] -) -def test_invalid_time_fails_overflow(args): - with pytest.raises(OverflowError): - rdt.make_time(*args) - - -@pytest.mark.parametrize( - "args", - [ - (24, 0, 0, 0), - (25, 0, 0, 0), - (0, 60, 0, 0), - (0, 61, 0, 0), - (0, 0, 60, 0), - (0, 0, 61, 0), - (0, 0, 0, 1000000), - ], -) -def test_invalid_time_fails(args): - with pytest.raises(ValueError): - rdt.make_time(*args) - - -@pytest.mark.parametrize( - "args", - [ - ("0", 0, 0, 0), - (0, "0", 0, 0), - (0, 0, "0", 0), - (0, 0, 0, "0"), - (0, 0, 0, 0, "UTC"), - ], -) -def test_time_typeerror(args): - with pytest.raises(TypeError): - rdt.make_time(*args) - - -@pytest.mark.parametrize( - "args, kwargs", - [((2017, 9, 1, 12, 45, 30, 0), {}), ((2017, 9, 1, 12, 45, 30, 0), {"tzinfo": UTC})], -) -def test_datetime(args, kwargs): - act = rdt.make_datetime(*args, **kwargs) - exp = pdt.datetime(*args, **kwargs) - - assert act == exp - assert act.tzinfo is exp.tzinfo - assert rdt.get_datetime_tzinfo(act) == exp.tzinfo - - -@given(dt=st.datetimes()) -def test_datetime_tuple(dt): - act = rdt.get_datetime_tuple(dt) - exp = dt.timetuple()[0:6] + (dt.microsecond,) - - assert act == exp - - -@given(dt=st.datetimes()) -def test_datetime_tuple_fold(dt): - dt_fold = dt.replace(fold=1) - dt_nofold = dt.replace(fold=0) - - for dt in (dt_fold, dt_nofold): - act = rdt.get_datetime_tuple_fold(dt) - exp = dt.timetuple()[0:6] + (dt.microsecond, dt.fold) - - assert act == exp - - -def test_invalid_datetime_fails(): - with pytest.raises(ValueError): - rdt.make_datetime(2011, 1, 42, 0, 0, 0, 0) - - -def test_datetime_typeerror(): - with pytest.raises(TypeError): - rdt.make_datetime("2011", 1, 1, 0, 0, 0, 0) - - -@given(dt=st.datetimes(MIN_DATETIME, MAX_DATETIME)) -@example(dt=pdt.datetime(1971, 1, 2, 0, 0)) -def test_datetime_from_timestamp(dt): - try: - ts = pdt.datetime.timestamp(dt) - except Exception: - # out of range for timestamp - return - - try: - expected = pdt.datetime.fromtimestamp(ts) - except Exception as pdt_fail: - # datetime from timestamp failed; expect the same from Rust binding - with pytest.raises(type(pdt_fail)) as exc_info: - rdt.datetime_from_timestamp(ts) - assert str(exc_info.value) == str(pdt_fail) - else: - assert rdt.datetime_from_timestamp(ts) == expected - - -def test_datetime_from_timestamp_tzinfo(): - d1 = rdt.datetime_from_timestamp(0, tz=UTC) - d2 = rdt.datetime_from_timestamp(0, tz=UTC) - - assert d1 == d2 - assert d1.tzinfo is d2.tzinfo - - -@pytest.mark.parametrize( - "args", - [ - (0, 0, 0), - (1, 0, 0), - (-1, 0, 0), - (0, 1, 0), - (0, -1, 0), - (1, -1, 0), - (-1, 1, 0), - (0, 0, 123456), - (0, 0, -123456), - ], -) -def test_delta(args): - act = pdt.timedelta(*args) - exp = rdt.make_delta(*args) - - assert act == exp - - -@given(td=st.timedeltas()) -def test_delta_accessors(td): - act = rdt.get_delta_tuple(td) - exp = (td.days, td.seconds, td.microseconds) - - assert act == exp - - -@pytest.mark.parametrize( - "args,err_type", - [ - ((MAX_DAYS + 1, 0, 0), OverflowError), - ((MIN_DAYS - 1, 0, 0), OverflowError), - ((0, MAX_SECONDS + 1, 0), OverflowError), - ((0, MIN_SECONDS - 1, 0), OverflowError), - ((0, 0, MAX_MICROSECONDS + 1), OverflowError), - ((0, 0, MIN_MICROSECONDS - 1), OverflowError), - (("0", 0, 0), TypeError), - ((0, "0", 0), TypeError), - ((0, 0, "0"), TypeError), - ], -) -def test_delta_err(args, err_type): - with pytest.raises(err_type): - rdt.make_delta(*args) - - -def test_tz_class(): - tzi = rdt.TzClass() - - dt = pdt.datetime(2018, 1, 1, tzinfo=tzi) - - assert dt.tzname() == "+01:00" - assert dt.utcoffset() == pdt.timedelta(hours=1) - assert dt.dst() is None - - -def test_tz_class_introspection(): - tzi = rdt.TzClass() - - assert tzi.__class__ == rdt.TzClass - # PyPy generates for some reason. - assert re.match(r"^<[\w\.]*TzClass object at", repr(tzi)) diff --git a/vendor/pyo3/pytests/tests/test_dict_iter.py b/vendor/pyo3/pytests/tests/test_dict_iter.py deleted file mode 100644 index 3704a65b44..0000000000 --- a/vendor/pyo3/pytests/tests/test_dict_iter.py +++ /dev/null @@ -1,10 +0,0 @@ -import pytest -from pyo3_pytests.dict_iter import DictSize - - -@pytest.mark.parametrize("size", [64, 128, 256]) -def test_size(size): - d = {} - for i in range(size): - d[i] = str(i) - assert DictSize(len(d)).iter_dict(d) == size diff --git a/vendor/pyo3/pytests/tests/test_enums.py b/vendor/pyo3/pytests/tests/test_enums.py deleted file mode 100644 index cd4f7e124c..0000000000 --- a/vendor/pyo3/pytests/tests/test_enums.py +++ /dev/null @@ -1,203 +0,0 @@ -import pytest -from pyo3_pytests import enums - - -def test_complex_enum_variant_constructors(): - int_variant = enums.ComplexEnum.Int(42) - assert isinstance(int_variant, enums.ComplexEnum.Int) - - float_variant = enums.ComplexEnum.Float(3.14) - assert isinstance(float_variant, enums.ComplexEnum.Float) - - str_variant = enums.ComplexEnum.Str("hello") - assert isinstance(str_variant, enums.ComplexEnum.Str) - - empty_struct_variant = enums.ComplexEnum.EmptyStruct() - assert isinstance(empty_struct_variant, enums.ComplexEnum.EmptyStruct) - - multi_field_struct_variant = enums.ComplexEnum.MultiFieldStruct(42, 3.14, True) - assert isinstance(multi_field_struct_variant, enums.ComplexEnum.MultiFieldStruct) - - variant_with_default_1 = enums.ComplexEnum.VariantWithDefault() - assert isinstance(variant_with_default_1, enums.ComplexEnum.VariantWithDefault) - - variant_with_default_2 = enums.ComplexEnum.VariantWithDefault(25, "Hello") - assert isinstance(variant_with_default_2, enums.ComplexEnum.VariantWithDefault) - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.Int(42), - enums.ComplexEnum.Float(3.14), - enums.ComplexEnum.Str("hello"), - enums.ComplexEnum.EmptyStruct(), - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - enums.ComplexEnum.VariantWithDefault(), - ], -) -def test_complex_enum_variant_subclasses(variant: enums.ComplexEnum): - assert isinstance(variant, enums.ComplexEnum) - - -def test_complex_enum_field_getters(): - int_variant = enums.ComplexEnum.Int(42) - assert int_variant.i == 42 - - float_variant = enums.ComplexEnum.Float(3.14) - assert float_variant.f == 3.14 - - str_variant = enums.ComplexEnum.Str("hello") - assert str_variant.s == "hello" - - multi_field_struct_variant = enums.ComplexEnum.MultiFieldStruct(42, 3.14, True) - assert multi_field_struct_variant.a == 42 - assert multi_field_struct_variant.b == 3.14 - assert multi_field_struct_variant.c is True - - variant_with_default = enums.ComplexEnum.VariantWithDefault() - assert variant_with_default.a == 42 - assert variant_with_default.b is None - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.Int(42), - enums.ComplexEnum.Float(3.14), - enums.ComplexEnum.Str("hello"), - enums.ComplexEnum.EmptyStruct(), - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - enums.ComplexEnum.VariantWithDefault(), - ], -) -def test_complex_enum_desugared_match(variant: enums.ComplexEnum): - if isinstance(variant, enums.ComplexEnum.Int): - x = variant.i - assert x == 42 - elif isinstance(variant, enums.ComplexEnum.Float): - x = variant.f - assert x == 3.14 - elif isinstance(variant, enums.ComplexEnum.Str): - x = variant.s - assert x == "hello" - elif isinstance(variant, enums.ComplexEnum.EmptyStruct): - assert True - elif isinstance(variant, enums.ComplexEnum.MultiFieldStruct): - x = variant.a - y = variant.b - z = variant.c - assert x == 42 - assert y == 3.14 - assert z is True - elif isinstance(variant, enums.ComplexEnum.VariantWithDefault): - x = variant.a - y = variant.b - assert x == 42 - assert y is None - else: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.Int(42), - enums.ComplexEnum.Float(3.14), - enums.ComplexEnum.Str("hello"), - enums.ComplexEnum.EmptyStruct(), - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - enums.ComplexEnum.VariantWithDefault(b="hello"), - ], -) -def test_complex_enum_pyfunction_in_out_desugared_match(variant: enums.ComplexEnum): - variant = enums.do_complex_stuff(variant) - if isinstance(variant, enums.ComplexEnum.Int): - x = variant.i - assert x == 5 - elif isinstance(variant, enums.ComplexEnum.Float): - x = variant.f - assert x == 9.8596 - elif isinstance(variant, enums.ComplexEnum.Str): - x = variant.s - assert x == "42" - elif isinstance(variant, enums.ComplexEnum.EmptyStruct): - assert True - elif isinstance(variant, enums.ComplexEnum.MultiFieldStruct): - x = variant.a - y = variant.b - z = variant.c - assert x == 42 - assert y == 3.14 - assert z is True - elif isinstance(variant, enums.ComplexEnum.VariantWithDefault): - x = variant.a - y = variant.b - assert x == 84 - assert y == "HELLO" - else: - assert False - - -def test_tuple_enum_variant_constructors(): - tuple_variant = enums.TupleEnum.Full(42, 3.14, False) - assert isinstance(tuple_variant, enums.TupleEnum.Full) - - empty_tuple_variant = enums.TupleEnum.EmptyTuple() - assert isinstance(empty_tuple_variant, enums.TupleEnum.EmptyTuple) - - -@pytest.mark.parametrize( - "variant", - [ - enums.TupleEnum.FullWithDefault(), - enums.TupleEnum.Full(42, 3.14, False), - enums.TupleEnum.EmptyTuple(), - ], -) -def test_tuple_enum_variant_subclasses(variant: enums.TupleEnum): - assert isinstance(variant, enums.TupleEnum) - - -def test_tuple_enum_defaults(): - variant = enums.TupleEnum.FullWithDefault() - assert variant._0 == 1 - assert variant._1 == 1.0 - assert variant._2 is True - - -def test_tuple_enum_field_getters(): - tuple_variant = enums.TupleEnum.Full(42, 3.14, False) - assert tuple_variant._0 == 42 - assert tuple_variant._1 == 3.14 - assert tuple_variant._2 is False - - -def test_tuple_enum_index_getter(): - tuple_variant = enums.TupleEnum.Full(42, 3.14, False) - assert len(tuple_variant) == 3 - assert tuple_variant[0] == 42 - - -@pytest.mark.parametrize( - "variant", - [enums.MixedComplexEnum.Nothing()], -) -def test_mixed_complex_enum_pyfunction_instance_nothing( - variant: enums.MixedComplexEnum, -): - assert isinstance(variant, enums.MixedComplexEnum.Nothing) - assert isinstance( - enums.do_mixed_complex_stuff(variant), enums.MixedComplexEnum.Empty - ) - - -@pytest.mark.parametrize( - "variant", - [enums.MixedComplexEnum.Empty()], -) -def test_mixed_complex_enum_pyfunction_instance_empty(variant: enums.MixedComplexEnum): - assert isinstance(variant, enums.MixedComplexEnum.Empty) - assert isinstance( - enums.do_mixed_complex_stuff(variant), enums.MixedComplexEnum.Nothing - ) diff --git a/vendor/pyo3/pytests/tests/test_enums_match.py b/vendor/pyo3/pytests/tests/test_enums_match.py deleted file mode 100644 index 6c4b5f6aa0..0000000000 --- a/vendor/pyo3/pytests/tests/test_enums_match.py +++ /dev/null @@ -1,158 +0,0 @@ -# This file is only collected when Python >= 3.10, because it tests match syntax. -import pytest -from pyo3_pytests import enums - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.Int(42), - enums.ComplexEnum.Float(3.14), - enums.ComplexEnum.Str("hello"), - enums.ComplexEnum.EmptyStruct(), - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - ], -) -def test_complex_enum_match_statement(variant: enums.ComplexEnum): - match variant: - case enums.ComplexEnum.Int(i=x): - assert x == 42 - case enums.ComplexEnum.Float(f=x): - assert x == 3.14 - case enums.ComplexEnum.Str(s=x): - assert x == "hello" - case enums.ComplexEnum.EmptyStruct(): - assert True - case enums.ComplexEnum.MultiFieldStruct(a=x, b=y, c=z): - assert x == 42 - assert y == 3.14 - assert z is True - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.Int(42), - enums.ComplexEnum.Float(3.14), - enums.ComplexEnum.Str("hello"), - enums.ComplexEnum.EmptyStruct(), - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - ], -) -def test_complex_enum_pyfunction_in_out(variant: enums.ComplexEnum): - match enums.do_complex_stuff(variant): - case enums.ComplexEnum.Int(i=x): - assert x == 5 - case enums.ComplexEnum.Float(f=x): - assert x == 9.8596 - case enums.ComplexEnum.Str(s=x): - assert x == "42" - case enums.ComplexEnum.EmptyStruct(): - assert True - case enums.ComplexEnum.MultiFieldStruct(a=x, b=y, c=z): - assert x == 42 - assert y == 3.14 - assert z is True - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.ComplexEnum.MultiFieldStruct(42, 3.14, True), - ], -) -def test_complex_enum_partial_match(variant: enums.ComplexEnum): - match variant: - case enums.ComplexEnum.MultiFieldStruct(a): - assert a == 42 - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.TupleEnum.Full(42, 3.14, True), - enums.TupleEnum.EmptyTuple(), - ], -) -def test_tuple_enum_match_statement(variant: enums.TupleEnum): - match variant: - case enums.TupleEnum.Full(_0=x, _1=y, _2=z): - assert x == 42 - assert y == 3.14 - assert z is True - case enums.TupleEnum.EmptyTuple(): - assert True - case _: - print(variant) - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.SimpleTupleEnum.Int(42), - enums.SimpleTupleEnum.Str("hello"), - ], -) -def test_simple_tuple_enum_match_statement(variant: enums.SimpleTupleEnum): - match variant: - case enums.SimpleTupleEnum.Int(x): - assert x == 42 - case enums.SimpleTupleEnum.Str(x): - assert x == "hello" - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.TupleEnum.Full(42, 3.14, True), - ], -) -def test_tuple_enum_match_match_args(variant: enums.TupleEnum): - match variant: - case enums.TupleEnum.Full(x, y, z): - assert x == 42 - assert y == 3.14 - assert z is True - assert True - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.TupleEnum.Full(42, 3.14, True), - ], -) -def test_tuple_enum_partial_match(variant: enums.TupleEnum): - match variant: - case enums.TupleEnum.Full(a): - assert a == 42 - case _: - assert False - - -@pytest.mark.parametrize( - "variant", - [ - enums.MixedComplexEnum.Nothing(), - enums.MixedComplexEnum.Empty(), - ], -) -def test_mixed_complex_enum_match_statement(variant: enums.MixedComplexEnum): - match variant: - case enums.MixedComplexEnum.Nothing(): - assert True - case enums.MixedComplexEnum.Empty(): - assert True - case _: - assert False diff --git a/vendor/pyo3/pytests/tests/test_misc.py b/vendor/pyo3/pytests/tests/test_misc.py deleted file mode 100644 index dd7f8007e8..0000000000 --- a/vendor/pyo3/pytests/tests/test_misc.py +++ /dev/null @@ -1,119 +0,0 @@ -import importlib -import platform -import sys - -import pyo3_pytests.misc -import pytest - -if sys.version_info >= (3, 13): - subinterpreters = pytest.importorskip("_interpreters") -else: - subinterpreters = pytest.importorskip("_xxsubinterpreters") - - -def test_issue_219(): - # Should not deadlock - pyo3_pytests.misc.issue_219() - - -@pytest.mark.xfail( - platform.python_implementation() == "CPython" and sys.version_info < (3, 9), - reason="Cannot identify subinterpreters on Python older than 3.9", -) -def test_multiple_imports_same_interpreter_ok(): - spec = importlib.util.find_spec("pyo3_pytests.pyo3_pytests") - - module = importlib.util.module_from_spec(spec) - assert dir(module) == dir(pyo3_pytests.pyo3_pytests) - - -@pytest.mark.xfail( - platform.python_implementation() == "CPython" and sys.version_info < (3, 9), - reason="Cannot identify subinterpreters on Python older than 3.9", -) -@pytest.mark.skipif( - platform.python_implementation() in ("PyPy", "GraalVM"), - reason="PyPy and GraalPy do not support subinterpreters", -) -def test_import_in_subinterpreter_forbidden(): - sub_interpreter = subinterpreters.create() - if sys.version_info < (3, 12): - expected_error = "PyO3 modules do not yet support subinterpreters, see https://github.com/PyO3/pyo3/issues/576" - else: - expected_error = "module pyo3_pytests.pyo3_pytests does not support loading in subinterpreters" - - if sys.version_info < (3, 13): - # Python 3.12 subinterpreters had a special error for this - with pytest.raises( - subinterpreters.RunFailedError, - match=expected_error, - ): - subinterpreters.run_string( - sub_interpreter, "import pyo3_pytests.pyo3_pytests" - ) - else: - res = subinterpreters.run_string( - sub_interpreter, "import pyo3_pytests.pyo3_pytests" - ) - assert res.type.__name__ == "ImportError" - assert res.msg == expected_error - - subinterpreters.destroy(sub_interpreter) - - -def test_type_fully_qualified_name_includes_module(): - numpy = pytest.importorskip("numpy") - - # For numpy 1.x and 2.x - assert pyo3_pytests.misc.get_type_fully_qualified_name(numpy.bool_(True)) in [ - "numpy.bool", - "numpy.bool_", - ] - - -def test_accepts_numpy_bool(): - # binary numpy wheel not available on all platforms - numpy = pytest.importorskip("numpy") - - assert pyo3_pytests.misc.accepts_bool(True) is True - assert pyo3_pytests.misc.accepts_bool(False) is False - assert pyo3_pytests.misc.accepts_bool(numpy.bool_(True)) is True - assert pyo3_pytests.misc.accepts_bool(numpy.bool_(False)) is False - - -class ArbitraryClass: - worker_id: int - iteration: int - - def __init__(self, worker_id: int, iteration: int): - self.worker_id = worker_id - self.iteration = iteration - - def __repr__(self): - return f"ArbitraryClass({self.worker_id}, {self.iteration})" - - def __del__(self): - print("del", self.worker_id, self.iteration) - - -def test_gevent(): - gevent = pytest.importorskip("gevent") - - def worker(worker_id: int) -> None: - for iteration in range(2): - d = {"key": ArbitraryClass(worker_id, iteration)} - - def arbitrary_python_code(): - # remove the dictionary entry so that the class value can be - # garbage collected - del d["key"] - print("gevent sleep", worker_id, iteration) - gevent.sleep(0) - print("after gevent sleep", worker_id, iteration) - - print("start", worker_id, iteration) - pyo3_pytests.misc.get_item_and_run_callback(d, arbitrary_python_code) - print("end", worker_id, iteration) - - workers = [gevent.spawn(worker, i) for i in range(2)] - gevent.joinall(workers) diff --git a/vendor/pyo3/pytests/tests/test_objstore.py b/vendor/pyo3/pytests/tests/test_objstore.py deleted file mode 100644 index 3f0d23fa97..0000000000 --- a/vendor/pyo3/pytests/tests/test_objstore.py +++ /dev/null @@ -1,28 +0,0 @@ -import gc -import sys - -from pyo3_pytests.objstore import ObjStore - - -def test_objstore_doesnot_leak_memory(): - N = 10000 - message = b'\\(-"-;) Praying that memory leak would not happen..' - - # PyPy does not have sys.getrefcount, provide a no-op lambda and don't - # check refcount on PyPy - getrefcount = getattr(sys, "getrefcount", lambda obj: 0) - - if sys.implementation.name == "graalpy": - # GraalPy has an incomplete sys.getrefcount implementation - def getrefcount(obj): - return 0 - - before = getrefcount(message) - store = ObjStore() - for _ in range(N): - store.push(message) - del store - gc.collect() - after = getrefcount(message) - - assert after - before == 0 diff --git a/vendor/pyo3/pytests/tests/test_othermod.py b/vendor/pyo3/pytests/tests/test_othermod.py deleted file mode 100644 index ff67bba435..0000000000 --- a/vendor/pyo3/pytests/tests/test_othermod.py +++ /dev/null @@ -1,38 +0,0 @@ -from hypothesis import given, assume -from hypothesis import strategies as st - -from pyo3_pytests import othermod - -INTEGER32_ST = st.integers(min_value=(-(2**31)), max_value=(2**31 - 1)) -USIZE_ST = st.integers(min_value=othermod.USIZE_MIN, max_value=othermod.USIZE_MAX) - - -@given(x=INTEGER32_ST) -def test_double(x): - expected = x * 2 - assume(-(2**31) <= expected <= (2**31 - 1)) - assert othermod.double(x) == expected - - -def test_modclass(): - # Test that the repr of the class itself doesn't crash anything - repr(othermod.ModClass) - - assert isinstance(othermod.ModClass, type) - - -def test_modclass_instance(): - mi = othermod.ModClass() - - repr(mi) - repr(mi.__class__) - - assert isinstance(mi, othermod.ModClass) - assert isinstance(mi, object) - - -@given(x=USIZE_ST) -def test_modclas_noop(x): - mi = othermod.ModClass() - - assert mi.noop(x) == x diff --git a/vendor/pyo3/pytests/tests/test_path.py b/vendor/pyo3/pytests/tests/test_path.py deleted file mode 100644 index 2124018735..0000000000 --- a/vendor/pyo3/pytests/tests/test_path.py +++ /dev/null @@ -1,42 +0,0 @@ -import pathlib - -import pytest - -import pyo3_pytests.path as rpath - - -def test_make_path(): - p = rpath.make_path() - assert p == "/root" - - -def test_take_pathbuf(): - p = "/root" - assert rpath.take_pathbuf(p) == p - - -def test_take_pathlib(): - p = pathlib.Path("/root") - assert rpath.take_pathbuf(p) == str(p) - - -def test_take_pathlike(): - assert rpath.take_pathbuf(PathLike("/root")) == "/root" - - -def test_take_invalid_pathlike(): - with pytest.raises(TypeError): - assert rpath.take_pathbuf(PathLike(1)) - - -def test_take_invalid(): - with pytest.raises(TypeError): - assert rpath.take_pathbuf(3) - - -class PathLike: - def __init__(self, path): - self._path = path - - def __fspath__(self): - return self._path diff --git a/vendor/pyo3/pytests/tests/test_pyclasses.py b/vendor/pyo3/pytests/tests/test_pyclasses.py deleted file mode 100644 index 9f611b634b..0000000000 --- a/vendor/pyo3/pytests/tests/test_pyclasses.py +++ /dev/null @@ -1,123 +0,0 @@ -import platform -from typing import Type - -import pytest -from pyo3_pytests import pyclasses - - -def test_empty_class_init(benchmark): - benchmark(pyclasses.EmptyClass) - - -def test_method_call(benchmark): - obj = pyclasses.EmptyClass() - assert benchmark(obj.method) is None - - -def test_proto_call(benchmark): - obj = pyclasses.EmptyClass() - assert benchmark(len, obj) == 0 - - -class EmptyClassPy: - def method(self): - pass - - def __len__(self) -> int: - return 0 - - -def test_empty_class_init_py(benchmark): - benchmark(EmptyClassPy) - - -def test_method_call_py(benchmark): - obj = EmptyClassPy() - assert benchmark(obj.method) == pyclasses.EmptyClass().method() - - -def test_proto_call_py(benchmark): - obj = EmptyClassPy() - assert benchmark(len, obj) == len(pyclasses.EmptyClass()) - - -def test_iter(): - i = pyclasses.PyClassIter() - assert next(i) == 1 - assert next(i) == 2 - assert next(i) == 3 - assert next(i) == 4 - assert next(i) == 5 - - with pytest.raises(StopIteration) as excinfo: - next(i) - assert excinfo.value.value == "Ended" - - -@pytest.mark.skipif( - platform.machine() in ["wasm32", "wasm64"], - reason="not supporting threads in CI for WASM yet", -) -def test_parallel_iter(): - import concurrent.futures - - i = pyclasses.PyClassThreadIter() - - def func(): - next(i) - - # the second thread attempts to borrow a reference to the instance's - # state while the first thread is still sleeping, so we trigger a - # runtime borrow-check error - with pytest.raises(RuntimeError, match="Already borrowed"): - with concurrent.futures.ThreadPoolExecutor(max_workers=2) as tpe: - futures = [tpe.submit(func), tpe.submit(func)] - [f.result() for f in futures] - - -class AssertingSubClass(pyclasses.AssertingBaseClass): - pass - - -def test_new_classmethod(): - # The `AssertingBaseClass` constructor errors if it is not passed the - # relevant subclass. - _ = AssertingSubClass(expected_type=AssertingSubClass) - with pytest.raises(ValueError): - _ = AssertingSubClass(expected_type=str) - - -class ClassWithoutConstructor: - def __new__(cls): - raise TypeError("No constructor defined for ClassWithoutConstructor") - - -@pytest.mark.parametrize( - "cls", [pyclasses.ClassWithoutConstructor, ClassWithoutConstructor] -) -def test_no_constructor_defined_propagates_cause(cls: Type): - original_error = ValueError("Original message") - with pytest.raises(Exception) as exc_info: - try: - raise original_error - except Exception: - cls() # should raise TypeError("No constructor defined for ...") - - assert exc_info.type is TypeError - assert exc_info.value.args == ( - "No constructor defined for ClassWithoutConstructor", - ) - assert exc_info.value.__context__ is original_error - - -def test_dict(): - try: - ClassWithDict = pyclasses.ClassWithDict - except AttributeError: - pytest.skip("not defined using abi3 < 3.9") - - d = ClassWithDict() - assert d.__dict__ == {} - - d.foo = 42 - assert d.__dict__ == {"foo": 42} diff --git a/vendor/pyo3/pytests/tests/test_pyfunctions.py b/vendor/pyo3/pytests/tests/test_pyfunctions.py deleted file mode 100644 index c6fb448248..0000000000 --- a/vendor/pyo3/pytests/tests/test_pyfunctions.py +++ /dev/null @@ -1,85 +0,0 @@ -from pyo3_pytests import pyfunctions - - -def none_py(): - return None - - -def test_none_py(benchmark): - benchmark(none_py) - - -def test_none_rs(benchmark): - rust = benchmark(pyfunctions.none) - py = none_py() - assert rust == py - - -def simple_py(a, b=None, *, c=None): - return a, b, c - - -def test_simple_py(benchmark): - benchmark(simple_py, 1, "foo", c={1: 2}) - - -def test_simple_rs(benchmark): - rust = benchmark(pyfunctions.simple, 1, "foo", c={1: 2}) - py = simple_py(1, "foo", c={1: 2}) - assert rust == py - - -def simple_args_py(a, b=None, *args, c=None): - return a, b, args, c - - -def test_simple_args_py(benchmark): - benchmark(simple_args_py, 1, "foo", 4, 5, 6, c={1: 2}) - - -def test_simple_args_rs(benchmark): - rust = benchmark(pyfunctions.simple_args, 1, "foo", 4, 5, 6, c={1: 2}) - py = simple_args_py(1, "foo", 4, 5, 6, c={1: 2}) - assert rust == py - - -def simple_kwargs_py(a, b=None, c=None, **kwargs): - return a, b, c, kwargs - - -def test_simple_kwargs_py(benchmark): - benchmark(simple_kwargs_py, 1, "foo", c={1: 2}, bar=4, foo=10) - - -def test_simple_kwargs_rs(benchmark): - rust = benchmark(pyfunctions.simple_kwargs, 1, "foo", c={1: 2}, bar=4, foo=10) - py = simple_kwargs_py(1, "foo", c={1: 2}, bar=4, foo=10) - assert rust == py - - -def simple_args_kwargs_py(a, b=None, *args, c=None, **kwargs): - return (a, b, args, c, kwargs) - - -def test_simple_args_kwargs_py(benchmark): - benchmark(simple_args_kwargs_py, 1, "foo", "baz", bar=4, foo=10) - - -def test_simple_args_kwargs_rs(benchmark): - rust = benchmark(pyfunctions.simple_args_kwargs, 1, "foo", "baz", bar=4, foo=10) - py = simple_args_kwargs_py(1, "foo", "baz", bar=4, foo=10) - assert rust == py - - -def args_kwargs_py(*args, **kwargs): - return (args, kwargs) - - -def test_args_kwargs_py(benchmark): - benchmark(args_kwargs_py, 1, "foo", {1: 2}, bar=4, foo=10) - - -def test_args_kwargs_rs(benchmark): - rust = benchmark(pyfunctions.args_kwargs, 1, "foo", {1: 2}, bar=4, foo=10) - py = args_kwargs_py(1, "foo", {1: 2}, bar=4, foo=10) - assert rust == py diff --git a/vendor/pyo3/pytests/tests/test_sequence.py b/vendor/pyo3/pytests/tests/test_sequence.py deleted file mode 100644 index b943ba48fa..0000000000 --- a/vendor/pyo3/pytests/tests/test_sequence.py +++ /dev/null @@ -1,34 +0,0 @@ -import pytest - -from pyo3_pytests import sequence - - -def test_vec_from_list_i32(): - assert sequence.vec_to_vec_i32([1, 2, 3]) == [1, 2, 3] - - -def test_vec_from_list_pystring(): - assert sequence.vec_to_vec_pystring(["1", "2", "3"]) == ["1", "2", "3"] - - -def test_vec_from_bytes(): - assert sequence.vec_to_vec_i32(b"123") == [49, 50, 51] - - -def test_vec_from_str(): - with pytest.raises(TypeError): - sequence.vec_to_vec_pystring("123") - - -def test_vec_from_array(): - # binary numpy wheel not available on all platforms - numpy = pytest.importorskip("numpy") - - assert sequence.vec_to_vec_i32(numpy.array([1, 2, 3])) == [1, 2, 3] - - -def test_rust_array_from_array(): - # binary numpy wheel not available on all platforms - numpy = pytest.importorskip("numpy") - - assert sequence.array_to_array_i32(numpy.array([1, 2, 3])) == [1, 2, 3] diff --git a/vendor/pyo3/pytests/tests/test_subclassing.py b/vendor/pyo3/pytests/tests/test_subclassing.py deleted file mode 100644 index 2cee283dda..0000000000 --- a/vendor/pyo3/pytests/tests/test_subclassing.py +++ /dev/null @@ -1,12 +0,0 @@ -from pyo3_pytests.subclassing import Subclassable - - -class SomeSubClass(Subclassable): - def __str__(self): - return "SomeSubclass" - - -def test_subclassing(): - a = SomeSubClass() - assert str(a) == "SomeSubclass" - assert type(a) is SomeSubClass diff --git a/vendor/pyo3/src/buffer.rs b/vendor/pyo3/src/buffer.rs deleted file mode 100644 index 2d94681a5c..0000000000 --- a/vendor/pyo3/src/buffer.rs +++ /dev/null @@ -1,943 +0,0 @@ -#![cfg(any(not(Py_LIMITED_API), Py_3_11))] -// Copyright (c) 2017 Daniel Grunwald -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of this -// software and associated documentation files (the "Software"), to deal in the Software -// without restriction, including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons -// to whom the Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all copies or -// substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -//! `PyBuffer` implementation -use crate::Bound; -use crate::{err, exceptions::PyBufferError, ffi, FromPyObject, PyAny, PyResult, Python}; -use std::marker::PhantomData; -use std::os::raw; -use std::pin::Pin; -use std::{cell, mem, ptr, slice}; -use std::{ffi::CStr, fmt::Debug}; - -/// Allows access to the underlying buffer used by a python object such as `bytes`, `bytearray` or `array.array`. -// use Pin because Python expects that the Py_buffer struct has a stable memory address -#[repr(transparent)] -pub struct PyBuffer(Pin>, PhantomData); - -// PyBuffer is thread-safe: the shape of the buffer is immutable while a Py_buffer exists. -// Accessing the buffer contents is protected using the GIL. -unsafe impl Send for PyBuffer {} -unsafe impl Sync for PyBuffer {} - -impl Debug for PyBuffer { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("PyBuffer") - .field("buf", &self.0.buf) - .field("obj", &self.0.obj) - .field("len", &self.0.len) - .field("itemsize", &self.0.itemsize) - .field("readonly", &self.0.readonly) - .field("ndim", &self.0.ndim) - .field("format", &self.0.format) - .field("shape", &self.0.shape) - .field("strides", &self.0.strides) - .field("suboffsets", &self.0.suboffsets) - .field("internal", &self.0.internal) - .finish() - } -} - -/// Represents the type of a Python buffer element. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum ElementType { - /// A signed integer type. - SignedInteger { - /// The width of the signed integer in bytes. - bytes: usize, - }, - /// An unsigned integer type. - UnsignedInteger { - /// The width of the unsigned integer in bytes. - bytes: usize, - }, - /// A boolean type. - Bool, - /// A float type. - Float { - /// The width of the float in bytes. - bytes: usize, - }, - /// An unknown type. This may occur when parsing has failed. - Unknown, -} - -impl ElementType { - /// Determines the `ElementType` from a Python `struct` module format string. - /// - /// See for more information - /// about struct format strings. - pub fn from_format(format: &CStr) -> ElementType { - match format.to_bytes() { - [size] | [b'@', size] => native_element_type_from_type_char(*size), - [b'=' | b'<' | b'>' | b'!', size] => standard_element_type_from_type_char(*size), - _ => ElementType::Unknown, - } - } -} - -fn native_element_type_from_type_char(type_char: u8) -> ElementType { - use self::ElementType::*; - match type_char { - b'c' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'b' => SignedInteger { - bytes: mem::size_of::(), - }, - b'B' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'?' => Bool, - b'h' => SignedInteger { - bytes: mem::size_of::(), - }, - b'H' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'i' => SignedInteger { - bytes: mem::size_of::(), - }, - b'I' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'l' => SignedInteger { - bytes: mem::size_of::(), - }, - b'L' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'q' => SignedInteger { - bytes: mem::size_of::(), - }, - b'Q' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'n' => SignedInteger { - bytes: mem::size_of::(), - }, - b'N' => UnsignedInteger { - bytes: mem::size_of::(), - }, - b'e' => Float { bytes: 2 }, - b'f' => Float { bytes: 4 }, - b'd' => Float { bytes: 8 }, - _ => Unknown, - } -} - -fn standard_element_type_from_type_char(type_char: u8) -> ElementType { - use self::ElementType::*; - match type_char { - b'c' | b'B' => UnsignedInteger { bytes: 1 }, - b'b' => SignedInteger { bytes: 1 }, - b'?' => Bool, - b'h' => SignedInteger { bytes: 2 }, - b'H' => UnsignedInteger { bytes: 2 }, - b'i' | b'l' => SignedInteger { bytes: 4 }, - b'I' | b'L' => UnsignedInteger { bytes: 4 }, - b'q' => SignedInteger { bytes: 8 }, - b'Q' => UnsignedInteger { bytes: 8 }, - b'e' => Float { bytes: 2 }, - b'f' => Float { bytes: 4 }, - b'd' => Float { bytes: 8 }, - _ => Unknown, - } -} - -#[cfg(target_endian = "little")] -fn is_matching_endian(c: u8) -> bool { - c == b'@' || c == b'=' || c == b'>' -} - -#[cfg(target_endian = "big")] -fn is_matching_endian(c: u8) -> bool { - c == b'@' || c == b'=' || c == b'>' || c == b'!' -} - -/// Trait implemented for possible element types of `PyBuffer`. -/// -/// # Safety -/// -/// This trait must only be implemented for types which represent valid elements of Python buffers. -pub unsafe trait Element: Copy { - /// Gets whether the element specified in the format string is potentially compatible. - /// Alignment and size are checked separately from this function. - fn is_compatible_format(format: &CStr) -> bool; -} - -impl FromPyObject<'_> for PyBuffer { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult> { - Self::get(obj) - } -} - -impl PyBuffer { - /// Gets the underlying buffer from the specified python object. - pub fn get(obj: &Bound<'_, PyAny>) -> PyResult> { - // TODO: use nightly API Box::new_uninit() once our MSRV is 1.82 - let mut buf = Box::new(mem::MaybeUninit::uninit()); - let buf: Box = { - err::error_on_minusone(obj.py(), unsafe { - ffi::PyObject_GetBuffer(obj.as_ptr(), buf.as_mut_ptr(), ffi::PyBUF_FULL_RO) - })?; - // Safety: buf is initialized by PyObject_GetBuffer. - // TODO: use nightly API Box::assume_init() once our MSRV is 1.82 - unsafe { mem::transmute(buf) } - }; - // Create PyBuffer immediately so that if validation checks fail, the PyBuffer::drop code - // will call PyBuffer_Release (thus avoiding any leaks). - let buf = PyBuffer(Pin::from(buf), PhantomData); - - if buf.0.shape.is_null() { - Err(PyBufferError::new_err("shape is null")) - } else if buf.0.strides.is_null() { - Err(PyBufferError::new_err("strides is null")) - } else if mem::size_of::() != buf.item_size() || !T::is_compatible_format(buf.format()) { - Err(PyBufferError::new_err(format!( - "buffer contents are not compatible with {}", - std::any::type_name::() - ))) - } else if buf.0.buf.align_offset(mem::align_of::()) != 0 { - Err(PyBufferError::new_err(format!( - "buffer contents are insufficiently aligned for {}", - std::any::type_name::() - ))) - } else { - Ok(buf) - } - } - - /// Deprecated name for [`PyBuffer::get`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyBuffer::get`")] - #[inline] - pub fn get_bound(obj: &Bound<'_, PyAny>) -> PyResult> { - Self::get(obj) - } - - /// Gets the pointer to the start of the buffer memory. - /// - /// Warning: the buffer memory might be mutated by other Python functions, - /// and thus may only be accessed while the GIL is held. - #[inline] - pub fn buf_ptr(&self) -> *mut raw::c_void { - self.0.buf - } - - /// Gets a pointer to the specified item. - /// - /// If `indices.len() < self.dimensions()`, returns the start address of the sub-array at the specified dimension. - pub fn get_ptr(&self, indices: &[usize]) -> *mut raw::c_void { - let shape = &self.shape()[..indices.len()]; - for i in 0..indices.len() { - assert!(indices[i] < shape[i]); - } - unsafe { - ffi::PyBuffer_GetPointer( - #[cfg(Py_3_11)] - &*self.0, - #[cfg(not(Py_3_11))] - { - &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer - }, - #[cfg(Py_3_11)] - { - indices.as_ptr().cast() - }, - #[cfg(not(Py_3_11))] - { - indices.as_ptr() as *mut ffi::Py_ssize_t - }, - ) - } - } - - /// Gets whether the underlying buffer is read-only. - #[inline] - pub fn readonly(&self) -> bool { - self.0.readonly != 0 - } - - /// Gets the size of a single element, in bytes. - /// Important exception: when requesting an unformatted buffer, item_size still has the value - #[inline] - pub fn item_size(&self) -> usize { - self.0.itemsize as usize - } - - /// Gets the total number of items. - #[inline] - pub fn item_count(&self) -> usize { - (self.0.len as usize) / (self.0.itemsize as usize) - } - - /// `item_size() * item_count()`. - /// For contiguous arrays, this is the length of the underlying memory block. - /// For non-contiguous arrays, it is the length that the logical structure would have if it were copied to a contiguous representation. - #[inline] - pub fn len_bytes(&self) -> usize { - self.0.len as usize - } - - /// Gets the number of dimensions. - /// - /// May be 0 to indicate a single scalar value. - #[inline] - pub fn dimensions(&self) -> usize { - self.0.ndim as usize - } - - /// Returns an array of length `dimensions`. `shape()[i]` is the length of the array in dimension number `i`. - /// - /// May return None for single-dimensional arrays or scalar values (`dimensions() <= 1`); - /// You can call `item_count()` to get the length of the single dimension. - /// - /// Despite Python using an array of signed integers, the values are guaranteed to be non-negative. - /// However, dimensions of length 0 are possible and might need special attention. - #[inline] - pub fn shape(&self) -> &[usize] { - unsafe { slice::from_raw_parts(self.0.shape.cast(), self.0.ndim as usize) } - } - - /// Returns an array that holds, for each dimension, the number of bytes to skip to get to the next element in the dimension. - /// - /// Stride values can be any integer. For regular arrays, strides are usually positive, - /// but a consumer MUST be able to handle the case `strides[n] <= 0`. - #[inline] - pub fn strides(&self) -> &[isize] { - unsafe { slice::from_raw_parts(self.0.strides, self.0.ndim as usize) } - } - - /// An array of length ndim. - /// If `suboffsets[n] >= 0`, the values stored along the nth dimension are pointers and the suboffset value dictates how many bytes to add to each pointer after de-referencing. - /// A suboffset value that is negative indicates that no de-referencing should occur (striding in a contiguous memory block). - /// - /// If all suboffsets are negative (i.e. no de-referencing is needed), then this field must be NULL (the default value). - #[inline] - pub fn suboffsets(&self) -> Option<&[isize]> { - unsafe { - if self.0.suboffsets.is_null() { - None - } else { - Some(slice::from_raw_parts( - self.0.suboffsets, - self.0.ndim as usize, - )) - } - } - } - - /// A NUL terminated string in struct module style syntax describing the contents of a single item. - #[inline] - pub fn format(&self) -> &CStr { - if self.0.format.is_null() { - ffi::c_str!("B") - } else { - unsafe { CStr::from_ptr(self.0.format) } - } - } - - /// Gets whether the buffer is contiguous in C-style order (last index varies fastest when visiting items in order of memory address). - #[inline] - pub fn is_c_contiguous(&self) -> bool { - unsafe { ffi::PyBuffer_IsContiguous(&*self.0, b'C' as std::os::raw::c_char) != 0 } - } - - /// Gets whether the buffer is contiguous in Fortran-style order (first index varies fastest when visiting items in order of memory address). - #[inline] - pub fn is_fortran_contiguous(&self) -> bool { - unsafe { ffi::PyBuffer_IsContiguous(&*self.0, b'F' as std::os::raw::c_char) != 0 } - } - - /// Gets the buffer memory as a slice. - /// - /// This function succeeds if: - /// * the buffer format is compatible with `T` - /// * alignment and size of buffer elements is matching the expectations for type `T` - /// * the buffer is C-style contiguous - /// - /// The returned slice uses type `Cell` because it's theoretically possible for any call into the Python runtime - /// to modify the values in the slice. - pub fn as_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [ReadOnlyCell]> { - if self.is_c_contiguous() { - unsafe { - Some(slice::from_raw_parts( - self.0.buf as *mut ReadOnlyCell, - self.item_count(), - )) - } - } else { - None - } - } - - /// Gets the buffer memory as a slice. - /// - /// This function succeeds if: - /// * the buffer is not read-only - /// * the buffer format is compatible with `T` - /// * alignment and size of buffer elements is matching the expectations for type `T` - /// * the buffer is C-style contiguous - /// - /// The returned slice uses type `Cell` because it's theoretically possible for any call into the Python runtime - /// to modify the values in the slice. - pub fn as_mut_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [cell::Cell]> { - if !self.readonly() && self.is_c_contiguous() { - unsafe { - Some(slice::from_raw_parts( - self.0.buf as *mut cell::Cell, - self.item_count(), - )) - } - } else { - None - } - } - - /// Gets the buffer memory as a slice. - /// - /// This function succeeds if: - /// * the buffer format is compatible with `T` - /// * alignment and size of buffer elements is matching the expectations for type `T` - /// * the buffer is Fortran-style contiguous - /// - /// The returned slice uses type `Cell` because it's theoretically possible for any call into the Python runtime - /// to modify the values in the slice. - pub fn as_fortran_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [ReadOnlyCell]> { - if mem::size_of::() == self.item_size() && self.is_fortran_contiguous() { - unsafe { - Some(slice::from_raw_parts( - self.0.buf as *mut ReadOnlyCell, - self.item_count(), - )) - } - } else { - None - } - } - - /// Gets the buffer memory as a slice. - /// - /// This function succeeds if: - /// * the buffer is not read-only - /// * the buffer format is compatible with `T` - /// * alignment and size of buffer elements is matching the expectations for type `T` - /// * the buffer is Fortran-style contiguous - /// - /// The returned slice uses type `Cell` because it's theoretically possible for any call into the Python runtime - /// to modify the values in the slice. - pub fn as_fortran_mut_slice<'a>(&'a self, _py: Python<'a>) -> Option<&'a [cell::Cell]> { - if !self.readonly() && self.is_fortran_contiguous() { - unsafe { - Some(slice::from_raw_parts( - self.0.buf as *mut cell::Cell, - self.item_count(), - )) - } - } else { - None - } - } - - /// Copies the buffer elements to the specified slice. - /// If the buffer is multi-dimensional, the elements are written in C-style order. - /// - /// * Fails if the slice does not have the correct length (`buf.item_count()`). - /// * Fails if the buffer format is not compatible with type `T`. - /// - /// To check whether the buffer format is compatible before calling this method, - /// you can use `::is_compatible_format(buf.format())`. - /// Alternatively, `match buffer::ElementType::from_format(buf.format())`. - pub fn copy_to_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> { - self._copy_to_slice(py, target, b'C') - } - - /// Copies the buffer elements to the specified slice. - /// If the buffer is multi-dimensional, the elements are written in Fortran-style order. - /// - /// * Fails if the slice does not have the correct length (`buf.item_count()`). - /// * Fails if the buffer format is not compatible with type `T`. - /// - /// To check whether the buffer format is compatible before calling this method, - /// you can use `::is_compatible_format(buf.format())`. - /// Alternatively, `match buffer::ElementType::from_format(buf.format())`. - pub fn copy_to_fortran_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> { - self._copy_to_slice(py, target, b'F') - } - - fn _copy_to_slice(&self, py: Python<'_>, target: &mut [T], fort: u8) -> PyResult<()> { - if mem::size_of_val(target) != self.len_bytes() { - return Err(PyBufferError::new_err(format!( - "slice to copy to (of length {}) does not match buffer length of {}", - target.len(), - self.item_count() - ))); - } - - err::error_on_minusone(py, unsafe { - ffi::PyBuffer_ToContiguous( - target.as_mut_ptr().cast(), - #[cfg(Py_3_11)] - &*self.0, - #[cfg(not(Py_3_11))] - { - &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer - }, - self.0.len, - fort as std::os::raw::c_char, - ) - }) - } - - /// Copies the buffer elements to a newly allocated vector. - /// If the buffer is multi-dimensional, the elements are written in C-style order. - /// - /// Fails if the buffer format is not compatible with type `T`. - pub fn to_vec(&self, py: Python<'_>) -> PyResult> { - self._to_vec(py, b'C') - } - - /// Copies the buffer elements to a newly allocated vector. - /// If the buffer is multi-dimensional, the elements are written in Fortran-style order. - /// - /// Fails if the buffer format is not compatible with type `T`. - pub fn to_fortran_vec(&self, py: Python<'_>) -> PyResult> { - self._to_vec(py, b'F') - } - - fn _to_vec(&self, py: Python<'_>, fort: u8) -> PyResult> { - let item_count = self.item_count(); - let mut vec: Vec = Vec::with_capacity(item_count); - - // Copy the buffer into the uninitialized space in the vector. - // Due to T:Copy, we don't need to be concerned with Drop impls. - err::error_on_minusone(py, unsafe { - ffi::PyBuffer_ToContiguous( - vec.as_ptr() as *mut raw::c_void, - #[cfg(Py_3_11)] - &*self.0, - #[cfg(not(Py_3_11))] - { - &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer - }, - self.0.len, - fort as std::os::raw::c_char, - ) - })?; - // set vector length to mark the now-initialized space as usable - unsafe { vec.set_len(item_count) }; - Ok(vec) - } - - /// Copies the specified slice into the buffer. - /// If the buffer is multi-dimensional, the elements in the slice are expected to be in C-style order. - /// - /// * Fails if the buffer is read-only. - /// * Fails if the slice does not have the correct length (`buf.item_count()`). - /// * Fails if the buffer format is not compatible with type `T`. - /// - /// To check whether the buffer format is compatible before calling this method, - /// use `::is_compatible_format(buf.format())`. - /// Alternatively, `match buffer::ElementType::from_format(buf.format())`. - pub fn copy_from_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> { - self._copy_from_slice(py, source, b'C') - } - - /// Copies the specified slice into the buffer. - /// If the buffer is multi-dimensional, the elements in the slice are expected to be in Fortran-style order. - /// - /// * Fails if the buffer is read-only. - /// * Fails if the slice does not have the correct length (`buf.item_count()`). - /// * Fails if the buffer format is not compatible with type `T`. - /// - /// To check whether the buffer format is compatible before calling this method, - /// use `::is_compatible_format(buf.format())`. - /// Alternatively, `match buffer::ElementType::from_format(buf.format())`. - pub fn copy_from_fortran_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> { - self._copy_from_slice(py, source, b'F') - } - - fn _copy_from_slice(&self, py: Python<'_>, source: &[T], fort: u8) -> PyResult<()> { - if self.readonly() { - return Err(PyBufferError::new_err("cannot write to read-only buffer")); - } else if mem::size_of_val(source) != self.len_bytes() { - return Err(PyBufferError::new_err(format!( - "slice to copy from (of length {}) does not match buffer length of {}", - source.len(), - self.item_count() - ))); - } - - err::error_on_minusone(py, unsafe { - ffi::PyBuffer_FromContiguous( - #[cfg(Py_3_11)] - &*self.0, - #[cfg(not(Py_3_11))] - { - &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer - }, - #[cfg(Py_3_11)] - { - source.as_ptr().cast() - }, - #[cfg(not(Py_3_11))] - { - source.as_ptr() as *mut raw::c_void - }, - self.0.len, - fort as std::os::raw::c_char, - ) - }) - } - - /// Releases the buffer object, freeing the reference to the Python object - /// which owns the buffer. - /// - /// This will automatically be called on drop. - pub fn release(self, _py: Python<'_>) { - // First move self into a ManuallyDrop, so that PyBuffer::drop will - // never be called. (It would acquire the GIL and call PyBuffer_Release - // again.) - let mut mdself = mem::ManuallyDrop::new(self); - unsafe { - // Next, make the actual PyBuffer_Release call. - ffi::PyBuffer_Release(&mut *mdself.0); - - // Finally, drop the contained Pin> in place, to free the - // Box memory. - let inner: *mut Pin> = &mut mdself.0; - ptr::drop_in_place(inner); - } - } -} - -impl Drop for PyBuffer { - fn drop(&mut self) { - Python::with_gil(|_| unsafe { ffi::PyBuffer_Release(&mut *self.0) }); - } -} - -/// Like [std::cell::Cell], but only provides read-only access to the data. -/// -/// `&ReadOnlyCell` is basically a safe version of `*const T`: -/// The data cannot be modified through the reference, but other references may -/// be modifying the data. -#[repr(transparent)] -pub struct ReadOnlyCell(cell::UnsafeCell); - -impl ReadOnlyCell { - /// Returns a copy of the current value. - #[inline] - pub fn get(&self) -> T { - unsafe { *self.0.get() } - } - - /// Returns a pointer to the current value. - #[inline] - pub fn as_ptr(&self) -> *const T { - self.0.get() - } -} - -macro_rules! impl_element( - ($t:ty, $f:ident) => { - unsafe impl Element for $t { - fn is_compatible_format(format: &CStr) -> bool { - let slice = format.to_bytes(); - if slice.len() > 1 && !is_matching_endian(slice[0]) { - return false; - } - ElementType::from_format(format) == ElementType::$f { bytes: mem::size_of::<$t>() } - } - } - } -); - -impl_element!(u8, UnsignedInteger); -impl_element!(u16, UnsignedInteger); -impl_element!(u32, UnsignedInteger); -impl_element!(u64, UnsignedInteger); -impl_element!(usize, UnsignedInteger); -impl_element!(i8, SignedInteger); -impl_element!(i16, SignedInteger); -impl_element!(i32, SignedInteger); -impl_element!(i64, SignedInteger); -impl_element!(isize, SignedInteger); -impl_element!(f32, Float); -impl_element!(f64, Float); - -#[cfg(test)] -mod tests { - use super::PyBuffer; - use crate::ffi; - use crate::types::any::PyAnyMethods; - use crate::Python; - - #[test] - fn test_debug() { - Python::with_gil(|py| { - let bytes = py.eval(ffi::c_str!("b'abcde'"), None, None).unwrap(); - let buffer: PyBuffer = PyBuffer::get(&bytes).unwrap(); - let expected = format!( - concat!( - "PyBuffer {{ buf: {:?}, obj: {:?}, ", - "len: 5, itemsize: 1, readonly: 1, ", - "ndim: 1, format: {:?}, shape: {:?}, ", - "strides: {:?}, suboffsets: {:?}, internal: {:?} }}", - ), - buffer.0.buf, - buffer.0.obj, - buffer.0.format, - buffer.0.shape, - buffer.0.strides, - buffer.0.suboffsets, - buffer.0.internal - ); - let debug_repr = format!("{:?}", buffer); - assert_eq!(debug_repr, expected); - }); - } - - #[test] - fn test_element_type_from_format() { - use super::ElementType; - use super::ElementType::*; - use std::mem::size_of; - use std::os::raw; - - for (cstr, expected) in [ - // @ prefix goes to native_element_type_from_type_char - ( - ffi::c_str!("@b"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@c"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@b"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@B"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - (ffi::c_str!("@?"), Bool), - ( - ffi::c_str!("@h"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@H"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@i"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@I"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@l"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@L"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@q"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@Q"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@n"), - SignedInteger { - bytes: size_of::(), - }, - ), - ( - ffi::c_str!("@N"), - UnsignedInteger { - bytes: size_of::(), - }, - ), - (ffi::c_str!("@e"), Float { bytes: 2 }), - (ffi::c_str!("@f"), Float { bytes: 4 }), - (ffi::c_str!("@d"), Float { bytes: 8 }), - (ffi::c_str!("@z"), Unknown), - // = prefix goes to standard_element_type_from_type_char - (ffi::c_str!("=b"), SignedInteger { bytes: 1 }), - (ffi::c_str!("=c"), UnsignedInteger { bytes: 1 }), - (ffi::c_str!("=B"), UnsignedInteger { bytes: 1 }), - (ffi::c_str!("=?"), Bool), - (ffi::c_str!("=h"), SignedInteger { bytes: 2 }), - (ffi::c_str!("=H"), UnsignedInteger { bytes: 2 }), - (ffi::c_str!("=l"), SignedInteger { bytes: 4 }), - (ffi::c_str!("=l"), SignedInteger { bytes: 4 }), - (ffi::c_str!("=I"), UnsignedInteger { bytes: 4 }), - (ffi::c_str!("=L"), UnsignedInteger { bytes: 4 }), - (ffi::c_str!("=q"), SignedInteger { bytes: 8 }), - (ffi::c_str!("=Q"), UnsignedInteger { bytes: 8 }), - (ffi::c_str!("=e"), Float { bytes: 2 }), - (ffi::c_str!("=f"), Float { bytes: 4 }), - (ffi::c_str!("=d"), Float { bytes: 8 }), - (ffi::c_str!("=z"), Unknown), - (ffi::c_str!("=0"), Unknown), - // unknown prefix -> Unknown - (ffi::c_str!(":b"), Unknown), - ] { - assert_eq!( - ElementType::from_format(cstr), - expected, - "element from format &Cstr: {:?}", - cstr, - ); - } - } - - #[test] - fn test_compatible_size() { - // for the cast in PyBuffer::shape() - assert_eq!( - std::mem::size_of::(), - std::mem::size_of::() - ); - } - - #[test] - fn test_bytes_buffer() { - Python::with_gil(|py| { - let bytes = py.eval(ffi::c_str!("b'abcde'"), None, None).unwrap(); - let buffer = PyBuffer::get(&bytes).unwrap(); - assert_eq!(buffer.dimensions(), 1); - assert_eq!(buffer.item_count(), 5); - assert_eq!(buffer.format().to_str().unwrap(), "B"); - assert_eq!(buffer.shape(), [5]); - // single-dimensional buffer is always contiguous - assert!(buffer.is_c_contiguous()); - assert!(buffer.is_fortran_contiguous()); - - let slice = buffer.as_slice(py).unwrap(); - assert_eq!(slice.len(), 5); - assert_eq!(slice[0].get(), b'a'); - assert_eq!(slice[2].get(), b'c'); - - assert_eq!(unsafe { *(buffer.get_ptr(&[1]) as *mut u8) }, b'b'); - - assert!(buffer.as_mut_slice(py).is_none()); - - assert!(buffer.copy_to_slice(py, &mut [0u8]).is_err()); - let mut arr = [0; 5]; - buffer.copy_to_slice(py, &mut arr).unwrap(); - assert_eq!(arr, b"abcde" as &[u8]); - - assert!(buffer.copy_from_slice(py, &[0u8; 5]).is_err()); - assert_eq!(buffer.to_vec(py).unwrap(), b"abcde"); - }); - } - - #[test] - fn test_array_buffer() { - Python::with_gil(|py| { - let array = py - .import("array") - .unwrap() - .call_method("array", ("f", (1.0, 1.5, 2.0, 2.5)), None) - .unwrap(); - let buffer = PyBuffer::get(&array).unwrap(); - assert_eq!(buffer.dimensions(), 1); - assert_eq!(buffer.item_count(), 4); - assert_eq!(buffer.format().to_str().unwrap(), "f"); - assert_eq!(buffer.shape(), [4]); - - // array creates a 1D contiguious buffer, so it's both C and F contiguous. This would - // be more interesting if we can come up with a 2D buffer but I think it would need a - // third-party lib or a custom class. - - // C-contiguous fns - let slice = buffer.as_slice(py).unwrap(); - assert_eq!(slice.len(), 4); - assert_eq!(slice[0].get(), 1.0); - assert_eq!(slice[3].get(), 2.5); - - let mut_slice = buffer.as_mut_slice(py).unwrap(); - assert_eq!(mut_slice.len(), 4); - assert_eq!(mut_slice[0].get(), 1.0); - mut_slice[3].set(2.75); - assert_eq!(slice[3].get(), 2.75); - - buffer - .copy_from_slice(py, &[10.0f32, 11.0, 12.0, 13.0]) - .unwrap(); - assert_eq!(slice[2].get(), 12.0); - - assert_eq!(buffer.to_vec(py).unwrap(), [10.0, 11.0, 12.0, 13.0]); - - // F-contiguous fns - let buffer = PyBuffer::get(&array).unwrap(); - let slice = buffer.as_fortran_slice(py).unwrap(); - assert_eq!(slice.len(), 4); - assert_eq!(slice[1].get(), 11.0); - - let mut_slice = buffer.as_fortran_mut_slice(py).unwrap(); - assert_eq!(mut_slice.len(), 4); - assert_eq!(mut_slice[2].get(), 12.0); - mut_slice[3].set(2.75); - assert_eq!(slice[3].get(), 2.75); - - buffer - .copy_from_fortran_slice(py, &[10.0f32, 11.0, 12.0, 13.0]) - .unwrap(); - assert_eq!(slice[2].get(), 12.0); - - assert_eq!(buffer.to_fortran_vec(py).unwrap(), [10.0, 11.0, 12.0, 13.0]); - }); - } -} diff --git a/vendor/pyo3/src/conversion.rs b/vendor/pyo3/src/conversion.rs deleted file mode 100644 index 82ad4d8497..0000000000 --- a/vendor/pyo3/src/conversion.rs +++ /dev/null @@ -1,615 +0,0 @@ -//! Defines conversions between Rust and Python types. -use crate::err::PyResult; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::pyclass::boolean_struct::False; -use crate::types::any::PyAnyMethods; -use crate::types::PyTuple; -use crate::{ - ffi, Borrowed, Bound, BoundObject, Py, PyAny, PyClass, PyErr, PyObject, PyRef, PyRefMut, Python, -}; -use std::convert::Infallible; - -/// Returns a borrowed pointer to a Python object. -/// -/// The returned pointer will be valid for as long as `self` is. It may be null depending on the -/// implementation. -/// -/// # Examples -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::ffi; -/// -/// Python::with_gil(|py| { -/// let s = "foo".into_pyobject(py)?; -/// let ptr = s.as_ptr(); -/// -/// let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) }; -/// assert_eq!(is_really_a_pystring, 1); -/// # Ok::<_, PyErr>(()) -/// }) -/// # .unwrap(); -/// ``` -/// -/// # Safety -/// -/// For callers, it is your responsibility to make sure that the underlying Python object is not dropped too -/// early. For example, the following code will cause undefined behavior: -/// -/// ```rust,no_run -/// # use pyo3::prelude::*; -/// # use pyo3::ffi; -/// # -/// Python::with_gil(|py| { -/// // ERROR: calling `.as_ptr()` will throw away the temporary object and leave `ptr` dangling. -/// let ptr: *mut ffi::PyObject = 0xabad1dea_u32.into_pyobject(py)?.as_ptr(); -/// -/// let isnt_a_pystring = unsafe { -/// // `ptr` is dangling, this is UB -/// ffi::PyUnicode_CheckExact(ptr) -/// }; -/// # assert_eq!(isnt_a_pystring, 0); -/// # Ok::<_, PyErr>(()) -/// }) -/// # .unwrap(); -/// ``` -/// -/// This happens because the pointer returned by `as_ptr` does not carry any lifetime information -/// and the Python object is dropped immediately after the `0xabad1dea_u32.into_pyobject(py).as_ptr()` -/// expression is evaluated. To fix the problem, bind Python object to a local variable like earlier -/// to keep the Python object alive until the end of its scope. -/// -/// Implementors must ensure this returns a valid pointer to a Python object, which borrows a reference count from `&self`. -pub unsafe trait AsPyPointer { - /// Returns the underlying FFI pointer as a borrowed pointer. - fn as_ptr(&self) -> *mut ffi::PyObject; -} - -/// Conversion trait that allows various objects to be converted into `PyObject`. -#[deprecated( - since = "0.23.0", - note = "`ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information." -)] -pub trait ToPyObject { - /// Converts self into a Python object. - fn to_object(&self, py: Python<'_>) -> PyObject; -} - -/// Defines a conversion from a Rust type to a Python object. -/// -/// It functions similarly to std's [`Into`] trait, but requires a [GIL token](Python) -/// as an argument. Many functions and traits internal to PyO3 require this trait as a bound, -/// so a lack of this trait can manifest itself in different error messages. -/// -/// # Examples -/// ## With `#[pyclass]` -/// The easiest way to implement `IntoPy` is by exposing a struct as a native Python object -/// by annotating it with [`#[pyclass]`](crate::prelude::pyclass). -/// -/// ```rust -/// use pyo3::prelude::*; -/// -/// # #[allow(dead_code)] -/// #[pyclass] -/// struct Number { -/// #[pyo3(get, set)] -/// value: i32, -/// } -/// ``` -/// Python code will see this as an instance of the `Number` class with a `value` attribute. -/// -/// ## Conversion to a Python object -/// -/// However, it may not be desirable to expose the existence of `Number` to Python code. -/// `IntoPy` allows us to define a conversion to an appropriate Python object. -/// ```rust -/// #![allow(deprecated)] -/// use pyo3::prelude::*; -/// -/// # #[allow(dead_code)] -/// struct Number { -/// value: i32, -/// } -/// -/// impl IntoPy for Number { -/// fn into_py(self, py: Python<'_>) -> PyObject { -/// // delegates to i32's IntoPy implementation. -/// self.value.into_py(py) -/// } -/// } -/// ``` -/// Python code will see this as an `int` object. -/// -/// ## Dynamic conversion into Python objects. -/// It is also possible to return a different Python object depending on some condition. -/// This is useful for types like enums that can carry different types. -/// -/// ```rust -/// #![allow(deprecated)] -/// use pyo3::prelude::*; -/// -/// enum Value { -/// Integer(i32), -/// String(String), -/// None, -/// } -/// -/// impl IntoPy for Value { -/// fn into_py(self, py: Python<'_>) -> PyObject { -/// match self { -/// Self::Integer(val) => val.into_py(py), -/// Self::String(val) => val.into_py(py), -/// Self::None => py.None(), -/// } -/// } -/// } -/// # fn main() { -/// # Python::with_gil(|py| { -/// # let v = Value::Integer(73).into_py(py); -/// # let v = v.extract::(py).unwrap(); -/// # -/// # let v = Value::String("foo".into()).into_py(py); -/// # let v = v.extract::(py).unwrap(); -/// # -/// # let v = Value::None.into_py(py); -/// # let v = v.extract::>>(py).unwrap(); -/// # }); -/// # } -/// ``` -/// Python code will see this as any of the `int`, `string` or `None` objects. -#[cfg_attr( - diagnostic_namespace, - diagnostic::on_unimplemented( - message = "`{Self}` cannot be converted to a Python object", - note = "`IntoPy` is automatically implemented by the `#[pyclass]` macro", - note = "if you do not wish to have a corresponding Python type, implement it manually", - note = "if you do not own `{Self}` you can perform a manual conversion to one of the types in `pyo3::types::*`" - ) -)] -#[deprecated( - since = "0.23.0", - note = "`IntoPy` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information." -)] -pub trait IntoPy: Sized { - /// Performs the conversion. - fn into_py(self, py: Python<'_>) -> T; -} - -/// Defines a conversion from a Rust type to a Python object, which may fail. -/// -/// This trait has `#[derive(IntoPyObject)]` to automatically implement it for simple types and -/// `#[derive(IntoPyObjectRef)]` to implement the same for references. -/// -/// It functions similarly to std's [`TryInto`] trait, but requires a [GIL token](Python) -/// as an argument. -/// -/// The [`into_pyobject`][IntoPyObject::into_pyobject] method is designed for maximum flexibility and efficiency; it -/// - allows for a concrete Python type to be returned (the [`Target`][IntoPyObject::Target] associated type) -/// - allows for the smart pointer containing the Python object to be either `Bound<'py, Self::Target>` or `Borrowed<'a, 'py, Self::Target>` -/// to avoid unnecessary reference counting overhead -/// - allows for a custom error type to be returned in the event of a conversion error to avoid -/// unnecessarily creating a Python exception -/// -/// # See also -/// -/// - The [`IntoPyObjectExt`] trait, which provides convenience methods for common usages of -/// `IntoPyObject` which erase type information and convert errors to `PyErr`. -#[cfg_attr( - diagnostic_namespace, - diagnostic::on_unimplemented( - message = "`{Self}` cannot be converted to a Python object", - note = "`IntoPyObject` is automatically implemented by the `#[pyclass]` macro", - note = "if you do not wish to have a corresponding Python type, implement it manually", - note = "if you do not own `{Self}` you can perform a manual conversion to one of the types in `pyo3::types::*`" - ) -)] -pub trait IntoPyObject<'py>: Sized { - /// The Python output type - type Target; - /// The smart pointer type to use. - /// - /// This will usually be [`Bound<'py, Target>`], but in special cases [`Borrowed<'a, 'py, Target>`] can be - /// used to minimize reference counting overhead. - type Output: BoundObject<'py, Self::Target>; - /// The type returned in the event of a conversion error. - type Error: Into; - - /// Performs the conversion. - fn into_pyobject(self, py: Python<'py>) -> Result; - - /// Extracts the type hint information for this type when it appears as a return value. - /// - /// For example, `Vec` would return `List[int]`. - /// The default implementation returns `Any`, which is correct for any type. - /// - /// For most types, the return value for this method will be identical to that of [`FromPyObject::type_input`]. - /// It may be different for some types, such as `Dict`, to allow duck-typing: functions return `Dict` but take `Mapping` as argument. - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::Any - } - - /// Converts sequence of Self into a Python object. Used to specialize `Vec`, `[u8; N]` - /// and `SmallVec<[u8; N]>` as a sequence of bytes into a `bytes` object. - #[doc(hidden)] - fn owned_sequence_into_pyobject( - iter: I, - py: Python<'py>, - _: private::Token, - ) -> Result, PyErr> - where - I: IntoIterator + AsRef<[Self]>, - I::IntoIter: ExactSizeIterator, - { - let mut iter = iter.into_iter().map(|e| e.into_bound_py_any(py)); - let list = crate::types::list::try_new_from_iter(py, &mut iter); - list.map(Bound::into_any) - } - - /// Converts sequence of Self into a Python object. Used to specialize `&[u8]` and `Cow<[u8]>` - /// as a sequence of bytes into a `bytes` object. - #[doc(hidden)] - fn borrowed_sequence_into_pyobject( - iter: I, - py: Python<'py>, - _: private::Token, - ) -> Result, PyErr> - where - Self: private::Reference, - I: IntoIterator + AsRef<[::BaseType]>, - I::IntoIter: ExactSizeIterator, - { - let mut iter = iter.into_iter().map(|e| e.into_bound_py_any(py)); - let list = crate::types::list::try_new_from_iter(py, &mut iter); - list.map(Bound::into_any) - } -} - -pub(crate) mod private { - pub struct Token; - - pub trait Reference { - type BaseType; - } - - impl Reference for &'_ T { - type BaseType = T; - } -} - -impl<'py, T> IntoPyObject<'py> for Bound<'py, T> { - type Target = T; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &'a Bound<'py, T> { - type Target = T; - type Output = Borrowed<'a, 'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self.as_borrowed()) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for Borrowed<'a, 'py, T> { - type Target = T; - type Output = Borrowed<'a, 'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &Borrowed<'a, 'py, T> { - type Target = T; - type Output = Borrowed<'a, 'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(*self) - } -} - -impl<'py, T> IntoPyObject<'py> for Py { - type Target = T; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.into_bound(py)) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &'a Py { - type Target = T; - type Output = Borrowed<'a, 'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.bind_borrowed(py)) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &&'a T -where - &'a T: IntoPyObject<'py>, -{ - type Target = <&'a T as IntoPyObject<'py>>::Target; - type Output = <&'a T as IntoPyObject<'py>>::Output; - type Error = <&'a T as IntoPyObject<'py>>::Error; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -mod into_pyobject_ext { - pub trait Sealed {} - impl<'py, T> Sealed for T where T: super::IntoPyObject<'py> {} -} - -/// Convenience methods for common usages of [`IntoPyObject`]. Every type that implements -/// [`IntoPyObject`] also implements this trait. -/// -/// These methods: -/// - Drop type information from the output, returning a `PyAny` object. -/// - Always convert the `Error` type to `PyErr`, which may incur a performance penalty but it -/// more convenient in contexts where the `?` operator would produce a `PyErr` anyway. -pub trait IntoPyObjectExt<'py>: IntoPyObject<'py> + into_pyobject_ext::Sealed { - /// Converts `self` into an owned Python object, dropping type information. - #[inline] - fn into_bound_py_any(self, py: Python<'py>) -> PyResult> { - match self.into_pyobject(py) { - Ok(obj) => Ok(obj.into_any().into_bound()), - Err(err) => Err(err.into()), - } - } - - /// Converts `self` into an owned Python object, dropping type information and unbinding it - /// from the `'py` lifetime. - #[inline] - fn into_py_any(self, py: Python<'py>) -> PyResult> { - match self.into_pyobject(py) { - Ok(obj) => Ok(obj.into_any().unbind()), - Err(err) => Err(err.into()), - } - } - - /// Converts `self` into a Python object. - /// - /// This is equivalent to calling [`into_pyobject`][IntoPyObject::into_pyobject] followed - /// with `.map_err(Into::into)` to convert the error type to [`PyErr`]. This is helpful - /// for generic code which wants to make use of the `?` operator. - #[inline] - fn into_pyobject_or_pyerr(self, py: Python<'py>) -> PyResult { - match self.into_pyobject(py) { - Ok(obj) => Ok(obj), - Err(err) => Err(err.into()), - } - } -} - -impl<'py, T> IntoPyObjectExt<'py> for T where T: IntoPyObject<'py> {} - -/// Extract a type from a Python object. -/// -/// -/// Normal usage is through the `extract` methods on [`Bound`] and [`Py`], which forward to this trait. -/// -/// # Examples -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::types::PyString; -/// -/// # fn main() -> PyResult<()> { -/// Python::with_gil(|py| { -/// // Calling `.extract()` on a `Bound` smart pointer -/// let obj: Bound<'_, PyString> = PyString::new(py, "blah"); -/// let s: String = obj.extract()?; -/// # assert_eq!(s, "blah"); -/// -/// // Calling `.extract(py)` on a `Py` smart pointer -/// let obj: Py = obj.unbind(); -/// let s: String = obj.extract(py)?; -/// # assert_eq!(s, "blah"); -/// # Ok(()) -/// }) -/// # } -/// ``` -/// -// /// FIXME: until `FromPyObject` can pick up a second lifetime, the below commentary is no longer -// /// true. Update and restore this documentation at that time. -// /// -// /// Note: depending on the implementation, the lifetime of the extracted result may -// /// depend on the lifetime of the `obj` or the `prepared` variable. -// /// -// /// For example, when extracting `&str` from a Python byte string, the resulting string slice will -// /// point to the existing string data (lifetime: `'py`). -// /// On the other hand, when extracting `&str` from a Python Unicode string, the preparation step -// /// will convert the string to UTF-8, and the resulting string slice will have lifetime `'prepared`. -// /// Since which case applies depends on the runtime type of the Python object, -// /// both the `obj` and `prepared` variables must outlive the resulting string slice. -/// -/// During the migration of PyO3 from the "GIL Refs" API to the `Bound` smart pointer, this trait -/// has two methods `extract` and `extract_bound` which are defaulted to call each other. To avoid -/// infinite recursion, implementors must implement at least one of these methods. The recommendation -/// is to implement `extract_bound` and leave `extract` as the default implementation. -pub trait FromPyObject<'py>: Sized { - /// Extracts `Self` from the bound smart pointer `obj`. - /// - /// Implementors are encouraged to implement this method and leave `extract` defaulted, as - /// this will be most compatible with PyO3's future API. - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult; - - /// Extracts the type hint information for this type when it appears as an argument. - /// - /// For example, `Vec` would return `Sequence[int]`. - /// The default implementation returns `Any`, which is correct for any type. - /// - /// For most types, the return value for this method will be identical to that of - /// [`IntoPyObject::type_output`]. It may be different for some types, such as `Dict`, - /// to allow duck-typing: functions return `Dict` but take `Mapping` as argument. - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::Any - } -} - -mod from_py_object_bound_sealed { - /// Private seal for the `FromPyObjectBound` trait. - /// - /// This prevents downstream types from implementing the trait before - /// PyO3 is ready to declare the trait as public API. - pub trait Sealed {} - - // This generic implementation is why the seal is separate from - // `crate::sealed::Sealed`. - impl<'py, T> Sealed for T where T: super::FromPyObject<'py> {} - impl Sealed for &'_ str {} - impl Sealed for std::borrow::Cow<'_, str> {} - impl Sealed for &'_ [u8] {} - impl Sealed for std::borrow::Cow<'_, [u8]> {} -} - -/// Expected form of [`FromPyObject`] to be used in a future PyO3 release. -/// -/// The difference between this and `FromPyObject` is that this trait takes an -/// additional lifetime `'a`, which is the lifetime of the input `Bound`. -/// -/// This allows implementations for `&'a str` and `&'a [u8]`, which could not -/// be expressed by the existing `FromPyObject` trait once the GIL Refs API was -/// removed. -/// -/// # Usage -/// -/// Users are prevented from implementing this trait, instead they should implement -/// the normal `FromPyObject` trait. This trait has a blanket implementation -/// for `T: FromPyObject`. -/// -/// The only case where this trait may have a use case to be implemented is when the -/// lifetime of the extracted value is tied to the lifetime `'a` of the input `Bound` -/// instead of the GIL lifetime `py`, as is the case for the `&'a str` implementation. -/// -/// Please contact the PyO3 maintainers if you believe you have a use case for implementing -/// this trait before PyO3 is ready to change the main `FromPyObject` trait to take an -/// additional lifetime. -/// -/// Similarly, users should typically not call these trait methods and should instead -/// use this via the `extract` method on `Bound` and `Py`. -pub trait FromPyObjectBound<'a, 'py>: Sized + from_py_object_bound_sealed::Sealed { - /// Extracts `Self` from the bound smart pointer `obj`. - /// - /// Users are advised against calling this method directly: instead, use this via - /// [`Bound<'_, PyAny>::extract`] or [`Py::extract`]. - fn from_py_object_bound(ob: Borrowed<'a, 'py, PyAny>) -> PyResult; - - /// Extracts the type hint information for this type when it appears as an argument. - /// - /// For example, `Vec` would return `Sequence[int]`. - /// The default implementation returns `Any`, which is correct for any type. - /// - /// For most types, the return value for this method will be identical to that of - /// [`IntoPyObject::type_output`]. It may be different for some types, such as `Dict`, - /// to allow duck-typing: functions return `Dict` but take `Mapping` as argument. - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::Any - } -} - -impl<'py, T> FromPyObjectBound<'_, 'py> for T -where - T: FromPyObject<'py>, -{ - fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> PyResult { - Self::extract_bound(&ob) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - ::type_input() - } -} - -/// Identity conversion: allows using existing `PyObject` instances where -/// `T: ToPyObject` is expected. -#[allow(deprecated)] -impl ToPyObject for &'_ T { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - ::to_object(*self, py) - } -} - -impl FromPyObject<'_> for T -where - T: PyClass + Clone, -{ - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let bound = obj.downcast::()?; - Ok(bound.try_borrow()?.clone()) - } -} - -impl<'py, T> FromPyObject<'py> for PyRef<'py, T> -where - T: PyClass, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - obj.downcast::()?.try_borrow().map_err(Into::into) - } -} - -impl<'py, T> FromPyObject<'py> for PyRefMut<'py, T> -where - T: PyClass, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - obj.downcast::()?.try_borrow_mut().map_err(Into::into) - } -} - -/// Converts `()` to an empty Python tuple. -#[allow(deprecated)] -impl IntoPy> for () { - fn into_py(self, py: Python<'_>) -> Py { - PyTuple::empty(py).unbind() - } -} - -impl<'py> IntoPyObject<'py> for () { - type Target = PyTuple; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyTuple::empty(py)) - } -} - -/// ```rust,compile_fail -/// use pyo3::prelude::*; -/// -/// #[pyclass] -/// struct TestClass { -/// num: u32, -/// } -/// -/// let t = TestClass { num: 10 }; -/// -/// Python::with_gil(|py| { -/// let pyvalue = Py::new(py, t).unwrap().to_object(py); -/// let t: TestClass = pyvalue.extract(py).unwrap(); -/// }) -/// ``` -mod test_no_clone {} diff --git a/vendor/pyo3/src/conversions/anyhow.rs b/vendor/pyo3/src/conversions/anyhow.rs deleted file mode 100644 index d2cb3f3eb6..0000000000 --- a/vendor/pyo3/src/conversions/anyhow.rs +++ /dev/null @@ -1,194 +0,0 @@ -#![cfg(feature = "anyhow")] - -//! A conversion from [anyhow]’s [`Error`][anyhow-error] type to [`PyErr`]. -//! -//! Use of an error handling library like [anyhow] is common in application code and when you just -//! want error handling to be easy. If you are writing a library or you need more control over your -//! errors you might want to design your own error type instead. -//! -//! When the inner error is a [`PyErr`] without source, it will be extracted out. -//! Otherwise a Python [`RuntimeError`] will be created. -//! You might find that you need to map the error from your Rust code into another Python exception. -//! See [`PyErr::new`] for more information about that. -//! -//! For information about error handling in general, see the [Error handling] chapter of the Rust -//! book. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! ## change * to the version you want to use, ideally the latest. -//! anyhow = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"anyhow\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of anyhow and PyO3. -//! The required anyhow version may vary based on the version of PyO3. -//! -//! # Example: Propagating a `PyErr` into [`anyhow::Error`] -//! -//! ```rust -//! use pyo3::prelude::*; -//! use std::path::PathBuf; -//! -//! // A wrapper around a Rust function. -//! // The pyfunction macro performs the conversion to a PyErr -//! #[pyfunction] -//! fn py_open(filename: PathBuf) -> anyhow::Result> { -//! let data = std::fs::read(filename)?; -//! Ok(data) -//! } -//! -//! fn main() { -//! let error = Python::with_gil(|py| -> PyResult> { -//! let fun = wrap_pyfunction!(py_open, py)?; -//! let text = fun.call1(("foo.txt",))?.extract::>()?; -//! Ok(text) -//! }).unwrap_err(); -//! -//! println!("{}", error); -//! } -//! ``` -//! -//! # Example: Using `anyhow` in general -//! -//! Note that you don't need this feature to convert a [`PyErr`] into an [`anyhow::Error`], because -//! it can already convert anything that implements [`Error`](std::error::Error): -//! -//! ```rust -//! use pyo3::prelude::*; -//! use pyo3::types::PyBytes; -//! -//! // An example function that must handle multiple error types. -//! // -//! // To do this you usually need to design your own error type or use -//! // `Box`. `anyhow` is a convenient alternative for this. -//! pub fn decompress(bytes: &[u8]) -> anyhow::Result { -//! // An arbitrary example of a Python api you -//! // could call inside an application... -//! // This might return a `PyErr`. -//! let res = Python::with_gil(|py| { -//! let zlib = PyModule::import(py, "zlib")?; -//! let decompress = zlib.getattr("decompress")?; -//! let bytes = PyBytes::new(py, bytes); -//! let value = decompress.call1((bytes,))?; -//! value.extract::>() -//! })?; -//! -//! // This might be a `FromUtf8Error`. -//! let text = String::from_utf8(res)?; -//! -//! Ok(text) -//! } -//! -//! fn main() -> anyhow::Result<()> { -//! let bytes: &[u8] = b"x\x9c\x8b\xcc/U(\xce\xc8/\xcdIQ((\xcaOJL\xca\xa9T\ -//! (-NU(\xc9HU\xc8\xc9LJ\xcbI,IUH.\x02\x91\x99y\xc5%\ -//! \xa9\x89)z\x00\xf2\x15\x12\xfe"; -//! let text = decompress(bytes)?; -//! -//! println!("The text is \"{}\"", text); -//! # assert_eq!(text, "You should probably use the libflate crate instead."); -//! Ok(()) -//! } -//! ``` -//! -//! [anyhow]: https://docs.rs/anyhow/ "A trait object based error system for easy idiomatic error handling in Rust applications." -//! [anyhow-error]: https://docs.rs/anyhow/latest/anyhow/struct.Error.html "Anyhows `Error` type, a wrapper around a dynamic error type" -//! [`RuntimeError`]: https://docs.python.org/3/library/exceptions.html#RuntimeError "Built-in Exceptions — Python documentation" -//! [Error handling]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html "Recoverable Errors with Result - The Rust Programming Language" - -use crate::exceptions::PyRuntimeError; -use crate::PyErr; - -impl From for PyErr { - fn from(mut error: anyhow::Error) -> Self { - // Errors containing a PyErr without chain or context are returned as the underlying error - if error.source().is_none() { - error = match error.downcast::() { - Ok(py_err) => return py_err, - Err(error) => error, - }; - } - PyRuntimeError::new_err(format!("{:?}", error)) - } -} - -#[cfg(test)] -mod test_anyhow { - use crate::exceptions::{PyRuntimeError, PyValueError}; - use crate::types::IntoPyDict; - use crate::{ffi, prelude::*}; - - use anyhow::{anyhow, bail, Context, Result}; - - fn f() -> Result<()> { - use std::io; - bail!(io::Error::new(io::ErrorKind::PermissionDenied, "oh no!")); - } - - fn g() -> Result<()> { - f().context("f failed") - } - - fn h() -> Result<()> { - g().context("g failed") - } - - #[test] - fn test_pyo3_exception_contents() { - let err = h().unwrap_err(); - let expected_contents = format!("{:?}", err); - let pyerr = PyErr::from(err); - - Python::with_gil(|py| { - let locals = [("err", pyerr)].into_py_dict(py).unwrap(); - let pyerr = py - .run(ffi::c_str!("raise err"), None, Some(&locals)) - .unwrap_err(); - assert_eq!(pyerr.value(py).to_string(), expected_contents); - }) - } - - fn k() -> Result<()> { - Err(anyhow!("Some sort of error")) - } - - #[test] - fn test_pyo3_exception_contents2() { - let err = k().unwrap_err(); - let expected_contents = format!("{:?}", err); - let pyerr = PyErr::from(err); - - Python::with_gil(|py| { - let locals = [("err", pyerr)].into_py_dict(py).unwrap(); - let pyerr = py - .run(ffi::c_str!("raise err"), None, Some(&locals)) - .unwrap_err(); - assert_eq!(pyerr.value(py).to_string(), expected_contents); - }) - } - - #[test] - fn test_pyo3_unwrap_simple_err() { - let origin_exc = PyValueError::new_err("Value Error"); - let err: anyhow::Error = origin_exc.into(); - let converted: PyErr = err.into(); - assert!(Python::with_gil( - |py| converted.is_instance_of::(py) - )) - } - #[test] - fn test_pyo3_unwrap_complex_err() { - let origin_exc = PyValueError::new_err("Value Error"); - let mut err: anyhow::Error = origin_exc.into(); - err = err.context("Context"); - let converted: PyErr = err.into(); - assert!(Python::with_gil( - |py| converted.is_instance_of::(py) - )) - } -} diff --git a/vendor/pyo3/src/conversions/chrono.rs b/vendor/pyo3/src/conversions/chrono.rs deleted file mode 100644 index 04febb43b7..0000000000 --- a/vendor/pyo3/src/conversions/chrono.rs +++ /dev/null @@ -1,1630 +0,0 @@ -#![cfg(feature = "chrono")] - -//! Conversions to and from [chrono](https://docs.rs/chrono/)’s `Duration`, -//! `NaiveDate`, `NaiveTime`, `DateTime`, `FixedOffset`, and `Utc`. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! chrono = "0.4" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"chrono\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of chrono and PyO3. -//! The required chrono version may vary based on the version of PyO3. -//! -//! # Example: Convert a `datetime.datetime` to chrono's `DateTime` -//! -//! ```rust -//! use chrono::{DateTime, Duration, TimeZone, Utc}; -//! use pyo3::{Python, PyResult, IntoPyObject, types::PyAnyMethods}; -//! -//! fn main() -> PyResult<()> { -//! pyo3::prepare_freethreaded_python(); -//! Python::with_gil(|py| { -//! // Build some chrono values -//! let chrono_datetime = Utc.with_ymd_and_hms(2022, 1, 1, 12, 0, 0).unwrap(); -//! let chrono_duration = Duration::seconds(1); -//! // Convert them to Python -//! let py_datetime = chrono_datetime.into_pyobject(py)?; -//! let py_timedelta = chrono_duration.into_pyobject(py)?; -//! // Do an operation in Python -//! let py_sum = py_datetime.call_method1("__add__", (py_timedelta,))?; -//! // Convert back to Rust -//! let chrono_sum: DateTime = py_sum.extract()?; -//! println!("DateTime: {}", chrono_datetime); -//! Ok(()) -//! }) -//! } -//! ``` - -use crate::conversion::IntoPyObject; -use crate::exceptions::{PyTypeError, PyUserWarning, PyValueError}; -#[cfg(Py_LIMITED_API)] -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -#[cfg(not(Py_LIMITED_API))] -use crate::types::datetime::timezone_from_offset; -#[cfg(Py_LIMITED_API)] -use crate::types::IntoPyDict; -use crate::types::PyNone; -#[cfg(not(Py_LIMITED_API))] -use crate::types::{ - timezone_utc, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess, - PyTzInfo, PyTzInfoAccess, -}; -use crate::{ffi, Bound, FromPyObject, IntoPyObjectExt, PyAny, PyErr, PyObject, PyResult, Python}; -#[cfg(Py_LIMITED_API)] -use crate::{intern, DowncastError}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use chrono::offset::{FixedOffset, Utc}; -use chrono::{ - DateTime, Datelike, Duration, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Offset, - TimeZone, Timelike, -}; - -#[allow(deprecated)] -impl ToPyObject for Duration { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Duration { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Duration { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDelta; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - // Total number of days - let days = self.num_days(); - // Remainder of seconds - let secs_dur = self - Duration::days(days); - let secs = secs_dur.num_seconds(); - // Fractional part of the microseconds - let micros = (secs_dur - Duration::seconds(secs_dur.num_seconds())) - .num_microseconds() - // This should never panic since we are just getting the fractional - // part of the total microseconds, which should never overflow. - .unwrap(); - - #[cfg(not(Py_LIMITED_API))] - { - // We do not need to check the days i64 to i32 cast from rust because - // python will panic with OverflowError. - // We pass true as the `normalize` parameter since we'd need to do several checks here to - // avoid that, and it shouldn't have a big performance impact. - // The seconds and microseconds cast should never overflow since it's at most the number of seconds per day - PyDelta::new( - py, - days.try_into().unwrap_or(i32::MAX), - secs.try_into()?, - micros.try_into()?, - true, - ) - } - - #[cfg(Py_LIMITED_API)] - { - DatetimeTypes::try_get(py) - .and_then(|dt| dt.timedelta.bind(py).call1((days, secs, micros))) - } - } -} - -impl<'py> IntoPyObject<'py> for &Duration { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDelta; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for Duration { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - // Python size are much lower than rust size so we do not need bound checks. - // 0 <= microseconds < 1000000 - // 0 <= seconds < 3600*24 - // -999999999 <= days <= 999999999 - #[cfg(not(Py_LIMITED_API))] - let (days, seconds, microseconds) = { - let delta = ob.downcast::()?; - ( - delta.get_days().into(), - delta.get_seconds().into(), - delta.get_microseconds().into(), - ) - }; - #[cfg(Py_LIMITED_API)] - let (days, seconds, microseconds) = { - check_type(ob, &DatetimeTypes::get(ob.py()).timedelta, "PyDelta")?; - ( - ob.getattr(intern!(ob.py(), "days"))?.extract()?, - ob.getattr(intern!(ob.py(), "seconds"))?.extract()?, - ob.getattr(intern!(ob.py(), "microseconds"))?.extract()?, - ) - }; - Ok( - Duration::days(days) - + Duration::seconds(seconds) - + Duration::microseconds(microseconds), - ) - } -} - -#[allow(deprecated)] -impl ToPyObject for NaiveDate { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for NaiveDate { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for NaiveDate { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDate; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let DateArgs { year, month, day } = (&self).into(); - #[cfg(not(Py_LIMITED_API))] - { - PyDate::new(py, year, month, day) - } - - #[cfg(Py_LIMITED_API)] - { - DatetimeTypes::try_get(py).and_then(|dt| dt.date.bind(py).call1((year, month, day))) - } - } -} - -impl<'py> IntoPyObject<'py> for &NaiveDate { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDate; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for NaiveDate { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - #[cfg(not(Py_LIMITED_API))] - { - let date = ob.downcast::()?; - py_date_to_naive_date(date) - } - #[cfg(Py_LIMITED_API)] - { - check_type(ob, &DatetimeTypes::get(ob.py()).date, "PyDate")?; - py_date_to_naive_date(ob) - } - } -} - -#[allow(deprecated)] -impl ToPyObject for NaiveTime { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for NaiveTime { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for NaiveTime { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let TimeArgs { - hour, - min, - sec, - micro, - truncated_leap_second, - } = (&self).into(); - - #[cfg(not(Py_LIMITED_API))] - let time = PyTime::new(py, hour, min, sec, micro, None)?; - - #[cfg(Py_LIMITED_API)] - let time = DatetimeTypes::try_get(py) - .and_then(|dt| dt.time.bind(py).call1((hour, min, sec, micro)))?; - - if truncated_leap_second { - warn_truncated_leap_second(&time); - } - - Ok(time) - } -} - -impl<'py> IntoPyObject<'py> for &NaiveTime { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for NaiveTime { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - #[cfg(not(Py_LIMITED_API))] - { - let time = ob.downcast::()?; - py_time_to_naive_time(time) - } - #[cfg(Py_LIMITED_API)] - { - check_type(ob, &DatetimeTypes::get(ob.py()).time, "PyTime")?; - py_time_to_naive_time(ob) - } - } -} - -#[allow(deprecated)] -impl ToPyObject for NaiveDateTime { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for NaiveDateTime { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for NaiveDateTime { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDateTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let DateArgs { year, month, day } = (&self.date()).into(); - let TimeArgs { - hour, - min, - sec, - micro, - truncated_leap_second, - } = (&self.time()).into(); - - #[cfg(not(Py_LIMITED_API))] - let datetime = PyDateTime::new(py, year, month, day, hour, min, sec, micro, None)?; - - #[cfg(Py_LIMITED_API)] - let datetime = DatetimeTypes::try_get(py).and_then(|dt| { - dt.datetime - .bind(py) - .call1((year, month, day, hour, min, sec, micro)) - })?; - - if truncated_leap_second { - warn_truncated_leap_second(&datetime); - } - - Ok(datetime) - } -} - -impl<'py> IntoPyObject<'py> for &NaiveDateTime { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDateTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for NaiveDateTime { - fn extract_bound(dt: &Bound<'_, PyAny>) -> PyResult { - #[cfg(not(Py_LIMITED_API))] - let dt = dt.downcast::()?; - #[cfg(Py_LIMITED_API)] - check_type(dt, &DatetimeTypes::get(dt.py()).datetime, "PyDateTime")?; - - // If the user tries to convert a timezone aware datetime into a naive one, - // we return a hard error. We could silently remove tzinfo, or assume local timezone - // and do a conversion, but better leave this decision to the user of the library. - #[cfg(not(Py_LIMITED_API))] - let has_tzinfo = dt.get_tzinfo().is_some(); - #[cfg(Py_LIMITED_API)] - let has_tzinfo = !dt.getattr(intern!(dt.py(), "tzinfo"))?.is_none(); - if has_tzinfo { - return Err(PyTypeError::new_err("expected a datetime without tzinfo")); - } - - let dt = NaiveDateTime::new(py_date_to_naive_date(dt)?, py_time_to_naive_time(dt)?); - Ok(dt) - } -} - -#[allow(deprecated)] -impl ToPyObject for DateTime { - fn to_object(&self, py: Python<'_>) -> PyObject { - // FIXME: convert to better timezone representation here than just convert to fixed offset - // See https://github.com/PyO3/pyo3/issues/3266 - let tz = self.offset().fix().to_object(py); - let tz = tz.bind(py).downcast().unwrap(); - naive_datetime_to_py_datetime(py, &self.naive_local(), Some(tz)) - } -} - -#[allow(deprecated)] -impl IntoPy for DateTime { - fn into_py(self, py: Python<'_>) -> PyObject { - self.to_object(py) - } -} - -impl<'py, Tz: TimeZone> IntoPyObject<'py> for DateTime -where - Tz: IntoPyObject<'py>, -{ - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDateTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (&self).into_pyobject(py) - } -} - -impl<'py, Tz: TimeZone> IntoPyObject<'py> for &DateTime -where - Tz: IntoPyObject<'py>, -{ - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyDateTime; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let tz = self.timezone().into_bound_py_any(py)?; - - #[cfg(not(Py_LIMITED_API))] - let tz = tz.downcast()?; - - let DateArgs { year, month, day } = (&self.naive_local().date()).into(); - let TimeArgs { - hour, - min, - sec, - micro, - truncated_leap_second, - } = (&self.naive_local().time()).into(); - - let fold = matches!( - self.timezone().offset_from_local_datetime(&self.naive_local()), - LocalResult::Ambiguous(_, latest) if self.offset().fix() == latest.fix() - ); - - #[cfg(not(Py_LIMITED_API))] - let datetime = - PyDateTime::new_with_fold(py, year, month, day, hour, min, sec, micro, Some(tz), fold)?; - - #[cfg(Py_LIMITED_API)] - let datetime = DatetimeTypes::try_get(py).and_then(|dt| { - dt.datetime.bind(py).call( - (year, month, day, hour, min, sec, micro, tz), - Some(&[("fold", fold as u8)].into_py_dict(py)?), - ) - })?; - - if truncated_leap_second { - warn_truncated_leap_second(&datetime); - } - - Ok(datetime) - } -} - -impl FromPyObject<'py>> FromPyObject<'_> for DateTime { - fn extract_bound(dt: &Bound<'_, PyAny>) -> PyResult> { - #[cfg(not(Py_LIMITED_API))] - let dt = dt.downcast::()?; - #[cfg(Py_LIMITED_API)] - check_type(dt, &DatetimeTypes::get(dt.py()).datetime, "PyDateTime")?; - - #[cfg(not(Py_LIMITED_API))] - let tzinfo = dt.get_tzinfo(); - #[cfg(Py_LIMITED_API)] - let tzinfo: Option> = dt.getattr(intern!(dt.py(), "tzinfo"))?.extract()?; - - let tz = if let Some(tzinfo) = tzinfo { - tzinfo.extract()? - } else { - return Err(PyTypeError::new_err( - "expected a datetime with non-None tzinfo", - )); - }; - let naive_dt = NaiveDateTime::new(py_date_to_naive_date(dt)?, py_time_to_naive_time(dt)?); - match naive_dt.and_local_timezone(tz) { - LocalResult::Single(value) => Ok(value), - LocalResult::Ambiguous(earliest, latest) => { - #[cfg(not(Py_LIMITED_API))] - let fold = dt.get_fold(); - - #[cfg(Py_LIMITED_API)] - let fold = dt.getattr(intern!(dt.py(), "fold"))?.extract::()? > 0; - - if fold { - Ok(latest) - } else { - Ok(earliest) - } - } - LocalResult::None => Err(PyValueError::new_err(format!( - "The datetime {:?} contains an incompatible timezone", - dt - ))), - } - } -} - -#[allow(deprecated)] -impl ToPyObject for FixedOffset { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for FixedOffset { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for FixedOffset { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTzInfo; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let seconds_offset = self.local_minus_utc(); - #[cfg(not(Py_LIMITED_API))] - { - let td = PyDelta::new(py, 0, seconds_offset, 0, true)?; - timezone_from_offset(&td) - } - - #[cfg(Py_LIMITED_API)] - { - let td = Duration::seconds(seconds_offset.into()).into_pyobject(py)?; - DatetimeTypes::try_get(py).and_then(|dt| dt.timezone.bind(py).call1((td,))) - } - } -} - -impl<'py> IntoPyObject<'py> for &FixedOffset { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTzInfo; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for FixedOffset { - /// Convert python tzinfo to rust [`FixedOffset`]. - /// - /// Note that the conversion will result in precision lost in microseconds as chrono offset - /// does not supports microseconds. - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - #[cfg(not(Py_LIMITED_API))] - let ob = ob.downcast::()?; - #[cfg(Py_LIMITED_API)] - check_type(ob, &DatetimeTypes::get(ob.py()).tzinfo, "PyTzInfo")?; - - // Passing Python's None to the `utcoffset` function will only - // work for timezones defined as fixed offsets in Python. - // Any other timezone would require a datetime as the parameter, and return - // None if the datetime is not provided. - // Trying to convert None to a PyDelta in the next line will then fail. - let py_timedelta = ob.call_method1("utcoffset", (PyNone::get(ob.py()),))?; - if py_timedelta.is_none() { - return Err(PyTypeError::new_err(format!( - "{:?} is not a fixed offset timezone", - ob - ))); - } - let total_seconds: Duration = py_timedelta.extract()?; - // This cast is safe since the timedelta is limited to -24 hours and 24 hours. - let total_seconds = total_seconds.num_seconds() as i32; - FixedOffset::east_opt(total_seconds) - .ok_or_else(|| PyValueError::new_err("fixed offset out of bounds")) - } -} - -#[allow(deprecated)] -impl ToPyObject for Utc { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Utc { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Utc { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTzInfo; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - #[cfg(Py_LIMITED_API)] - { - Ok(timezone_utc(py).into_any()) - } - #[cfg(not(Py_LIMITED_API))] - { - Ok(timezone_utc(py)) - } - } -} - -impl<'py> IntoPyObject<'py> for &Utc { - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - #[cfg(not(Py_LIMITED_API))] - type Target = PyTzInfo; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for Utc { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - let py_utc = timezone_utc(ob.py()); - if ob.eq(py_utc)? { - Ok(Utc) - } else { - Err(PyValueError::new_err("expected datetime.timezone.utc")) - } - } -} - -struct DateArgs { - year: i32, - month: u8, - day: u8, -} - -impl From<&NaiveDate> for DateArgs { - fn from(value: &NaiveDate) -> Self { - Self { - year: value.year(), - month: value.month() as u8, - day: value.day() as u8, - } - } -} - -struct TimeArgs { - hour: u8, - min: u8, - sec: u8, - micro: u32, - truncated_leap_second: bool, -} - -impl From<&NaiveTime> for TimeArgs { - fn from(value: &NaiveTime) -> Self { - let ns = value.nanosecond(); - let checked_sub = ns.checked_sub(1_000_000_000); - let truncated_leap_second = checked_sub.is_some(); - let micro = checked_sub.unwrap_or(ns) / 1000; - Self { - hour: value.hour() as u8, - min: value.minute() as u8, - sec: value.second() as u8, - micro, - truncated_leap_second, - } - } -} - -fn naive_datetime_to_py_datetime( - py: Python<'_>, - naive_datetime: &NaiveDateTime, - #[cfg(not(Py_LIMITED_API))] tzinfo: Option<&Bound<'_, PyTzInfo>>, - #[cfg(Py_LIMITED_API)] tzinfo: Option<&Bound<'_, PyAny>>, -) -> PyObject { - let DateArgs { year, month, day } = (&naive_datetime.date()).into(); - let TimeArgs { - hour, - min, - sec, - micro, - truncated_leap_second, - } = (&naive_datetime.time()).into(); - #[cfg(not(Py_LIMITED_API))] - let datetime = PyDateTime::new(py, year, month, day, hour, min, sec, micro, tzinfo) - .expect("failed to construct datetime"); - #[cfg(Py_LIMITED_API)] - let datetime = DatetimeTypes::get(py) - .datetime - .bind(py) - .call1((year, month, day, hour, min, sec, micro, tzinfo)) - .expect("failed to construct datetime.datetime"); - if truncated_leap_second { - warn_truncated_leap_second(&datetime); - } - datetime.into() -} - -fn warn_truncated_leap_second(obj: &Bound<'_, PyAny>) { - let py = obj.py(); - if let Err(e) = PyErr::warn( - py, - &py.get_type::(), - ffi::c_str!("ignored leap-second, `datetime` does not support leap-seconds"), - 0, - ) { - e.write_unraisable(py, Some(obj)) - }; -} - -#[cfg(not(Py_LIMITED_API))] -fn py_date_to_naive_date(py_date: &impl PyDateAccess) -> PyResult { - NaiveDate::from_ymd_opt( - py_date.get_year(), - py_date.get_month().into(), - py_date.get_day().into(), - ) - .ok_or_else(|| PyValueError::new_err("invalid or out-of-range date")) -} - -#[cfg(Py_LIMITED_API)] -fn py_date_to_naive_date(py_date: &Bound<'_, PyAny>) -> PyResult { - NaiveDate::from_ymd_opt( - py_date.getattr(intern!(py_date.py(), "year"))?.extract()?, - py_date.getattr(intern!(py_date.py(), "month"))?.extract()?, - py_date.getattr(intern!(py_date.py(), "day"))?.extract()?, - ) - .ok_or_else(|| PyValueError::new_err("invalid or out-of-range date")) -} - -#[cfg(not(Py_LIMITED_API))] -fn py_time_to_naive_time(py_time: &impl PyTimeAccess) -> PyResult { - NaiveTime::from_hms_micro_opt( - py_time.get_hour().into(), - py_time.get_minute().into(), - py_time.get_second().into(), - py_time.get_microsecond(), - ) - .ok_or_else(|| PyValueError::new_err("invalid or out-of-range time")) -} - -#[cfg(Py_LIMITED_API)] -fn py_time_to_naive_time(py_time: &Bound<'_, PyAny>) -> PyResult { - NaiveTime::from_hms_micro_opt( - py_time.getattr(intern!(py_time.py(), "hour"))?.extract()?, - py_time - .getattr(intern!(py_time.py(), "minute"))? - .extract()?, - py_time - .getattr(intern!(py_time.py(), "second"))? - .extract()?, - py_time - .getattr(intern!(py_time.py(), "microsecond"))? - .extract()?, - ) - .ok_or_else(|| PyValueError::new_err("invalid or out-of-range time")) -} - -#[cfg(Py_LIMITED_API)] -fn check_type(value: &Bound<'_, PyAny>, t: &PyObject, type_name: &'static str) -> PyResult<()> { - if !value.is_instance(t.bind(value.py()))? { - return Err(DowncastError::new(value, type_name).into()); - } - Ok(()) -} - -#[cfg(Py_LIMITED_API)] -struct DatetimeTypes { - date: PyObject, - datetime: PyObject, - time: PyObject, - timedelta: PyObject, - timezone: PyObject, - timezone_utc: PyObject, - tzinfo: PyObject, -} - -#[cfg(Py_LIMITED_API)] -impl DatetimeTypes { - fn get(py: Python<'_>) -> &Self { - Self::try_get(py).expect("failed to load datetime module") - } - - fn try_get(py: Python<'_>) -> PyResult<&Self> { - static TYPES: GILOnceCell = GILOnceCell::new(); - TYPES.get_or_try_init(py, || { - let datetime = py.import("datetime")?; - let timezone = datetime.getattr("timezone")?; - Ok::<_, PyErr>(Self { - date: datetime.getattr("date")?.into(), - datetime: datetime.getattr("datetime")?.into(), - time: datetime.getattr("time")?.into(), - timedelta: datetime.getattr("timedelta")?.into(), - timezone_utc: timezone.getattr("utc")?.into(), - timezone: timezone.into(), - tzinfo: datetime.getattr("tzinfo")?.into(), - }) - }) - } -} - -#[cfg(Py_LIMITED_API)] -fn timezone_utc(py: Python<'_>) -> Bound<'_, PyAny> { - DatetimeTypes::get(py).timezone_utc.bind(py).clone() -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{types::PyTuple, BoundObject}; - use std::{cmp::Ordering, panic}; - - #[test] - // Only Python>=3.9 has the zoneinfo package - // We skip the test on windows too since we'd need to install - // tzdata there to make this work. - #[cfg(all(Py_3_9, not(target_os = "windows")))] - fn test_zoneinfo_is_not_fixed_offset() { - use crate::ffi; - use crate::types::any::PyAnyMethods; - use crate::types::dict::PyDictMethods; - - Python::with_gil(|py| { - let locals = crate::types::PyDict::new(py); - py.run( - ffi::c_str!("import zoneinfo; zi = zoneinfo.ZoneInfo('Europe/London')"), - None, - Some(&locals), - ) - .unwrap(); - let result: PyResult = locals.get_item("zi").unwrap().unwrap().extract(); - assert!(result.is_err()); - let res = result.err().unwrap(); - // Also check the error message is what we expect - let msg = res.value(py).repr().unwrap().to_string(); - assert_eq!(msg, "TypeError(\"zoneinfo.ZoneInfo(key='Europe/London') is not a fixed offset timezone\")"); - }); - } - - #[test] - fn test_timezone_aware_to_naive_fails() { - // Test that if a user tries to convert a python's timezone aware datetime into a naive - // one, the conversion fails. - Python::with_gil(|py| { - let py_datetime = - new_py_datetime_ob(py, "datetime", (2022, 1, 1, 1, 0, 0, 0, python_utc(py))); - // Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails - let res: PyResult = py_datetime.extract(); - assert_eq!( - res.unwrap_err().value(py).repr().unwrap().to_string(), - "TypeError('expected a datetime without tzinfo')" - ); - }); - } - - #[test] - fn test_naive_to_timezone_aware_fails() { - // Test that if a user tries to convert a python's timezone aware datetime into a naive - // one, the conversion fails. - Python::with_gil(|py| { - let py_datetime = new_py_datetime_ob(py, "datetime", (2022, 1, 1, 1, 0, 0, 0)); - // Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails - let res: PyResult> = py_datetime.extract(); - assert_eq!( - res.unwrap_err().value(py).repr().unwrap().to_string(), - "TypeError('expected a datetime with non-None tzinfo')" - ); - - // Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails - let res: PyResult> = py_datetime.extract(); - assert_eq!( - res.unwrap_err().value(py).repr().unwrap().to_string(), - "TypeError('expected a datetime with non-None tzinfo')" - ); - }); - } - - #[test] - fn test_invalid_types_fail() { - // Test that if a user tries to convert a python's timezone aware datetime into a naive - // one, the conversion fails. - Python::with_gil(|py| { - let none = py.None().into_bound(py); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyDelta'" - ); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyTzInfo'" - ); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "ValueError: expected datetime.timezone.utc" - ); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyTime'" - ); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyDate'" - ); - assert_eq!( - none.extract::().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyDateTime'" - ); - assert_eq!( - none.extract::>().unwrap_err().to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyDateTime'" - ); - assert_eq!( - none.extract::>() - .unwrap_err() - .to_string(), - "TypeError: 'NoneType' object cannot be converted to 'PyDateTime'" - ); - }); - } - - #[test] - fn test_pyo3_timedelta_into_pyobject() { - // Utility function used to check different durations. - // The `name` parameter is used to identify the check in case of a failure. - let check = |name: &'static str, delta: Duration, py_days, py_seconds, py_ms| { - Python::with_gil(|py| { - let delta = delta.into_pyobject(py).unwrap(); - let py_delta = new_py_datetime_ob(py, "timedelta", (py_days, py_seconds, py_ms)); - assert!( - delta.eq(&py_delta).unwrap(), - "{}: {} != {}", - name, - delta, - py_delta - ); - }); - }; - - let delta = Duration::days(-1) + Duration::seconds(1) + Duration::microseconds(-10); - check("delta normalization", delta, -1, 1, -10); - - // Check the minimum value allowed by PyDelta, which is different - // from the minimum value allowed in Duration. This should pass. - let delta = Duration::seconds(-86399999913600); // min - check("delta min value", delta, -999999999, 0, 0); - - // Same, for max value - let delta = Duration::seconds(86399999999999) + Duration::nanoseconds(999999000); // max - check("delta max value", delta, 999999999, 86399, 999999); - - // Also check that trying to convert an out of bound value errors. - Python::with_gil(|py| { - // min_value and max_value were deprecated in chrono 0.4.39 - #[allow(deprecated)] - { - assert!(Duration::min_value().into_pyobject(py).is_err()); - assert!(Duration::max_value().into_pyobject(py).is_err()); - } - }); - } - - #[test] - fn test_pyo3_timedelta_frompyobject() { - // Utility function used to check different durations. - // The `name` parameter is used to identify the check in case of a failure. - let check = |name: &'static str, delta: Duration, py_days, py_seconds, py_ms| { - Python::with_gil(|py| { - let py_delta = new_py_datetime_ob(py, "timedelta", (py_days, py_seconds, py_ms)); - let py_delta: Duration = py_delta.extract().unwrap(); - assert_eq!(py_delta, delta, "{}: {} != {}", name, py_delta, delta); - }) - }; - - // Check the minimum value allowed by PyDelta, which is different - // from the minimum value allowed in Duration. This should pass. - check( - "min py_delta value", - Duration::seconds(-86399999913600), - -999999999, - 0, - 0, - ); - // Same, for max value - check( - "max py_delta value", - Duration::seconds(86399999999999) + Duration::microseconds(999999), - 999999999, - 86399, - 999999, - ); - - // This check is to assert that we can't construct every possible Duration from a PyDelta - // since they have different bounds. - Python::with_gil(|py| { - let low_days: i32 = -1000000000; - // This is possible - assert!(panic::catch_unwind(|| Duration::days(low_days as i64)).is_ok()); - // This panics on PyDelta::new - assert!(panic::catch_unwind(|| { - let py_delta = new_py_datetime_ob(py, "timedelta", (low_days, 0, 0)); - if let Ok(_duration) = py_delta.extract::() { - // So we should never get here - } - }) - .is_err()); - - let high_days: i32 = 1000000000; - // This is possible - assert!(panic::catch_unwind(|| Duration::days(high_days as i64)).is_ok()); - // This panics on PyDelta::new - assert!(panic::catch_unwind(|| { - let py_delta = new_py_datetime_ob(py, "timedelta", (high_days, 0, 0)); - if let Ok(_duration) = py_delta.extract::() { - // So we should never get here - } - }) - .is_err()); - }); - } - - #[test] - fn test_pyo3_date_into_pyobject() { - let eq_ymd = |name: &'static str, year, month, day| { - Python::with_gil(|py| { - let date = NaiveDate::from_ymd_opt(year, month, day) - .unwrap() - .into_pyobject(py) - .unwrap(); - let py_date = new_py_datetime_ob(py, "date", (year, month, day)); - assert_eq!( - date.compare(&py_date).unwrap(), - Ordering::Equal, - "{}: {} != {}", - name, - date, - py_date - ); - }) - }; - - eq_ymd("past date", 2012, 2, 29); - eq_ymd("min date", 1, 1, 1); - eq_ymd("future date", 3000, 6, 5); - eq_ymd("max date", 9999, 12, 31); - } - - #[test] - fn test_pyo3_date_frompyobject() { - let eq_ymd = |name: &'static str, year, month, day| { - Python::with_gil(|py| { - let py_date = new_py_datetime_ob(py, "date", (year, month, day)); - let py_date: NaiveDate = py_date.extract().unwrap(); - let date = NaiveDate::from_ymd_opt(year, month, day).unwrap(); - assert_eq!(py_date, date, "{}: {} != {}", name, date, py_date); - }) - }; - - eq_ymd("past date", 2012, 2, 29); - eq_ymd("min date", 1, 1, 1); - eq_ymd("future date", 3000, 6, 5); - eq_ymd("max date", 9999, 12, 31); - } - - #[test] - fn test_pyo3_datetime_into_pyobject_utc() { - Python::with_gil(|py| { - let check_utc = - |name: &'static str, year, month, day, hour, minute, second, ms, py_ms| { - let datetime = NaiveDate::from_ymd_opt(year, month, day) - .unwrap() - .and_hms_micro_opt(hour, minute, second, ms) - .unwrap() - .and_utc(); - let datetime = datetime.into_pyobject(py).unwrap(); - let py_datetime = new_py_datetime_ob( - py, - "datetime", - ( - year, - month, - day, - hour, - minute, - second, - py_ms, - python_utc(py), - ), - ); - assert_eq!( - datetime.compare(&py_datetime).unwrap(), - Ordering::Equal, - "{}: {} != {}", - name, - datetime, - py_datetime - ); - }; - - check_utc("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999); - - #[cfg(not(Py_GIL_DISABLED))] - assert_warnings!( - py, - check_utc("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999), - [( - PyUserWarning, - "ignored leap-second, `datetime` does not support leap-seconds" - )] - ); - }) - } - - #[test] - fn test_pyo3_datetime_into_pyobject_fixed_offset() { - Python::with_gil(|py| { - let check_fixed_offset = - |name: &'static str, year, month, day, hour, minute, second, ms, py_ms| { - let offset = FixedOffset::east_opt(3600).unwrap(); - let datetime = NaiveDate::from_ymd_opt(year, month, day) - .unwrap() - .and_hms_micro_opt(hour, minute, second, ms) - .unwrap() - .and_local_timezone(offset) - .unwrap(); - let datetime = datetime.into_pyobject(py).unwrap(); - let py_tz = offset.into_pyobject(py).unwrap(); - let py_datetime = new_py_datetime_ob( - py, - "datetime", - (year, month, day, hour, minute, second, py_ms, py_tz), - ); - assert_eq!( - datetime.compare(&py_datetime).unwrap(), - Ordering::Equal, - "{}: {} != {}", - name, - datetime, - py_datetime - ); - }; - - check_fixed_offset("regular", 2014, 5, 6, 7, 8, 9, 999_999, 999_999); - - #[cfg(not(Py_GIL_DISABLED))] - assert_warnings!( - py, - check_fixed_offset("leap second", 2014, 5, 6, 7, 8, 59, 1_999_999, 999_999), - [( - PyUserWarning, - "ignored leap-second, `datetime` does not support leap-seconds" - )] - ); - }) - } - - #[test] - #[cfg(all(Py_3_9, feature = "chrono-tz", not(windows)))] - fn test_pyo3_datetime_into_pyobject_tz() { - Python::with_gil(|py| { - let datetime = NaiveDate::from_ymd_opt(2024, 12, 11) - .unwrap() - .and_hms_opt(23, 3, 13) - .unwrap() - .and_local_timezone(chrono_tz::Tz::Europe__London) - .unwrap(); - let datetime = datetime.into_pyobject(py).unwrap(); - let py_datetime = new_py_datetime_ob( - py, - "datetime", - ( - 2024, - 12, - 11, - 23, - 3, - 13, - 0, - python_zoneinfo(py, "Europe/London"), - ), - ); - assert_eq!(datetime.compare(&py_datetime).unwrap(), Ordering::Equal); - }) - } - - #[test] - fn test_pyo3_datetime_frompyobject_utc() { - Python::with_gil(|py| { - let year = 2014; - let month = 5; - let day = 6; - let hour = 7; - let minute = 8; - let second = 9; - let micro = 999_999; - let tz_utc = timezone_utc(py); - let py_datetime = new_py_datetime_ob( - py, - "datetime", - (year, month, day, hour, minute, second, micro, tz_utc), - ); - let py_datetime: DateTime = py_datetime.extract().unwrap(); - let datetime = NaiveDate::from_ymd_opt(year, month, day) - .unwrap() - .and_hms_micro_opt(hour, minute, second, micro) - .unwrap() - .and_utc(); - assert_eq!(py_datetime, datetime,); - }) - } - - #[test] - fn test_pyo3_datetime_frompyobject_fixed_offset() { - Python::with_gil(|py| { - let year = 2014; - let month = 5; - let day = 6; - let hour = 7; - let minute = 8; - let second = 9; - let micro = 999_999; - let offset = FixedOffset::east_opt(3600).unwrap(); - let py_tz = offset.into_pyobject(py).unwrap(); - let py_datetime = new_py_datetime_ob( - py, - "datetime", - (year, month, day, hour, minute, second, micro, py_tz), - ); - let datetime_from_py: DateTime = py_datetime.extract().unwrap(); - let datetime = NaiveDate::from_ymd_opt(year, month, day) - .unwrap() - .and_hms_micro_opt(hour, minute, second, micro) - .unwrap(); - let datetime = datetime.and_local_timezone(offset).unwrap(); - - assert_eq!(datetime_from_py, datetime); - assert!( - py_datetime.extract::>().is_err(), - "Extracting Utc from nonzero FixedOffset timezone will fail" - ); - - let utc = python_utc(py); - let py_datetime_utc = new_py_datetime_ob( - py, - "datetime", - (year, month, day, hour, minute, second, micro, utc), - ); - assert!( - py_datetime_utc.extract::>().is_ok(), - "Extracting FixedOffset from Utc timezone will succeed" - ); - }) - } - - #[test] - fn test_pyo3_offset_fixed_into_pyobject() { - Python::with_gil(|py| { - // Chrono offset - let offset = FixedOffset::east_opt(3600) - .unwrap() - .into_pyobject(py) - .unwrap(); - // Python timezone from timedelta - let td = new_py_datetime_ob(py, "timedelta", (0, 3600, 0)); - let py_timedelta = new_py_datetime_ob(py, "timezone", (td,)); - // Should be equal - assert!(offset.eq(py_timedelta).unwrap()); - - // Same but with negative values - let offset = FixedOffset::east_opt(-3600) - .unwrap() - .into_pyobject(py) - .unwrap(); - let td = new_py_datetime_ob(py, "timedelta", (0, -3600, 0)); - let py_timedelta = new_py_datetime_ob(py, "timezone", (td,)); - assert!(offset.eq(py_timedelta).unwrap()); - }) - } - - #[test] - fn test_pyo3_offset_fixed_frompyobject() { - Python::with_gil(|py| { - let py_timedelta = new_py_datetime_ob(py, "timedelta", (0, 3600, 0)); - let py_tzinfo = new_py_datetime_ob(py, "timezone", (py_timedelta,)); - let offset: FixedOffset = py_tzinfo.extract().unwrap(); - assert_eq!(FixedOffset::east_opt(3600).unwrap(), offset); - }) - } - - #[test] - fn test_pyo3_offset_utc_into_pyobject() { - Python::with_gil(|py| { - let utc = Utc.into_pyobject(py).unwrap(); - let py_utc = python_utc(py); - assert!(utc.is(&py_utc)); - }) - } - - #[test] - fn test_pyo3_offset_utc_frompyobject() { - Python::with_gil(|py| { - let py_utc = python_utc(py); - let py_utc: Utc = py_utc.extract().unwrap(); - assert_eq!(Utc, py_utc); - - let py_timedelta = new_py_datetime_ob(py, "timedelta", (0, 0, 0)); - let py_timezone_utc = new_py_datetime_ob(py, "timezone", (py_timedelta,)); - let py_timezone_utc: Utc = py_timezone_utc.extract().unwrap(); - assert_eq!(Utc, py_timezone_utc); - - let py_timedelta = new_py_datetime_ob(py, "timedelta", (0, 3600, 0)); - let py_timezone = new_py_datetime_ob(py, "timezone", (py_timedelta,)); - assert!(py_timezone.extract::().is_err()); - }) - } - - #[test] - fn test_pyo3_time_into_pyobject() { - Python::with_gil(|py| { - let check_time = |name: &'static str, hour, minute, second, ms, py_ms| { - let time = NaiveTime::from_hms_micro_opt(hour, minute, second, ms) - .unwrap() - .into_pyobject(py) - .unwrap(); - let py_time = new_py_datetime_ob(py, "time", (hour, minute, second, py_ms)); - assert!( - time.eq(&py_time).unwrap(), - "{}: {} != {}", - name, - time, - py_time - ); - }; - - check_time("regular", 3, 5, 7, 999_999, 999_999); - - #[cfg(not(Py_GIL_DISABLED))] - assert_warnings!( - py, - check_time("leap second", 3, 5, 59, 1_999_999, 999_999), - [( - PyUserWarning, - "ignored leap-second, `datetime` does not support leap-seconds" - )] - ); - }) - } - - #[test] - fn test_pyo3_time_frompyobject() { - let hour = 3; - let minute = 5; - let second = 7; - let micro = 999_999; - Python::with_gil(|py| { - let py_time = new_py_datetime_ob(py, "time", (hour, minute, second, micro)); - let py_time: NaiveTime = py_time.extract().unwrap(); - let time = NaiveTime::from_hms_micro_opt(hour, minute, second, micro).unwrap(); - assert_eq!(py_time, time); - }) - } - - fn new_py_datetime_ob<'py, A>(py: Python<'py>, name: &str, args: A) -> Bound<'py, PyAny> - where - A: IntoPyObject<'py, Target = PyTuple>, - { - py.import("datetime") - .unwrap() - .getattr(name) - .unwrap() - .call1( - args.into_pyobject(py) - .map_err(Into::into) - .unwrap() - .into_bound(), - ) - .unwrap() - } - - fn python_utc(py: Python<'_>) -> Bound<'_, PyAny> { - py.import("datetime") - .unwrap() - .getattr("timezone") - .unwrap() - .getattr("utc") - .unwrap() - } - - #[cfg(all(Py_3_9, feature = "chrono-tz", not(windows)))] - fn python_zoneinfo<'py>(py: Python<'py>, timezone: &str) -> Bound<'py, PyAny> { - py.import("zoneinfo") - .unwrap() - .getattr("ZoneInfo") - .unwrap() - .call1((timezone,)) - .unwrap() - } - - #[cfg(not(any(target_arch = "wasm32", Py_GIL_DISABLED)))] - mod proptests { - use super::*; - use crate::tests::common::CatchWarnings; - use crate::types::IntoPyDict; - use proptest::prelude::*; - use std::ffi::CString; - - proptest! { - - // Range is limited to 1970 to 2038 due to windows limitations - #[test] - fn test_pyo3_offset_fixed_frompyobject_created_in_python(timestamp in 0..(i32::MAX as i64), timedelta in -86399i32..=86399i32) { - Python::with_gil(|py| { - - let globals = [("datetime", py.import("datetime").unwrap())].into_py_dict(py).unwrap(); - let code = format!("datetime.datetime.fromtimestamp({}).replace(tzinfo=datetime.timezone(datetime.timedelta(seconds={})))", timestamp, timedelta); - let t = py.eval(&CString::new(code).unwrap(), Some(&globals), None).unwrap(); - - // Get ISO 8601 string from python - let py_iso_str = t.call_method0("isoformat").unwrap(); - - // Get ISO 8601 string from rust - let t = t.extract::>().unwrap(); - // Python doesn't print the seconds of the offset if they are 0 - let rust_iso_str = if timedelta % 60 == 0 { - t.format("%Y-%m-%dT%H:%M:%S%:z").to_string() - } else { - t.format("%Y-%m-%dT%H:%M:%S%::z").to_string() - }; - - // They should be equal - assert_eq!(py_iso_str.to_string(), rust_iso_str); - }) - } - - #[test] - fn test_duration_roundtrip(days in -999999999i64..=999999999i64) { - // Test roundtrip conversion rust->python->rust for all allowed - // python values of durations (from -999999999 to 999999999 days), - Python::with_gil(|py| { - let dur = Duration::days(days); - let py_delta = dur.into_pyobject(py).unwrap(); - let roundtripped: Duration = py_delta.extract().expect("Round trip"); - assert_eq!(dur, roundtripped); - }) - } - - #[test] - fn test_fixed_offset_roundtrip(secs in -86399i32..=86399i32) { - Python::with_gil(|py| { - let offset = FixedOffset::east_opt(secs).unwrap(); - let py_offset = offset.into_pyobject(py).unwrap(); - let roundtripped: FixedOffset = py_offset.extract().expect("Round trip"); - assert_eq!(offset, roundtripped); - }) - } - - #[test] - fn test_naive_date_roundtrip( - year in 1i32..=9999i32, - month in 1u32..=12u32, - day in 1u32..=31u32 - ) { - // Test roundtrip conversion rust->python->rust for all allowed - // python dates (from year 1 to year 9999) - Python::with_gil(|py| { - // We use to `from_ymd_opt` constructor so that we only test valid `NaiveDate`s. - // This is to skip the test if we are creating an invalid date, like February 31. - if let Some(date) = NaiveDate::from_ymd_opt(year, month, day) { - let py_date = date.into_pyobject(py).unwrap(); - let roundtripped: NaiveDate = py_date.extract().expect("Round trip"); - assert_eq!(date, roundtripped); - } - }) - } - - #[test] - fn test_naive_time_roundtrip( - hour in 0u32..=23u32, - min in 0u32..=59u32, - sec in 0u32..=59u32, - micro in 0u32..=1_999_999u32 - ) { - // Test roundtrip conversion rust->python->rust for naive times. - // Python time has a resolution of microseconds, so we only test - // NaiveTimes with microseconds resolution, even if NaiveTime has nanosecond - // resolution. - Python::with_gil(|py| { - if let Some(time) = NaiveTime::from_hms_micro_opt(hour, min, sec, micro) { - // Wrap in CatchWarnings to avoid to_object firing warning for truncated leap second - let py_time = CatchWarnings::enter(py, |_| time.into_pyobject(py)).unwrap(); - let roundtripped: NaiveTime = py_time.extract().expect("Round trip"); - // Leap seconds are not roundtripped - let expected_roundtrip_time = micro.checked_sub(1_000_000).map(|micro| NaiveTime::from_hms_micro_opt(hour, min, sec, micro).unwrap()).unwrap_or(time); - assert_eq!(expected_roundtrip_time, roundtripped); - } - }) - } - - #[test] - fn test_naive_datetime_roundtrip( - year in 1i32..=9999i32, - month in 1u32..=12u32, - day in 1u32..=31u32, - hour in 0u32..=24u32, - min in 0u32..=60u32, - sec in 0u32..=60u32, - micro in 0u32..=999_999u32 - ) { - Python::with_gil(|py| { - let date_opt = NaiveDate::from_ymd_opt(year, month, day); - let time_opt = NaiveTime::from_hms_micro_opt(hour, min, sec, micro); - if let (Some(date), Some(time)) = (date_opt, time_opt) { - let dt = NaiveDateTime::new(date, time); - let pydt = dt.into_pyobject(py).unwrap(); - let roundtripped: NaiveDateTime = pydt.extract().expect("Round trip"); - assert_eq!(dt, roundtripped); - } - }) - } - - #[test] - fn test_utc_datetime_roundtrip( - year in 1i32..=9999i32, - month in 1u32..=12u32, - day in 1u32..=31u32, - hour in 0u32..=23u32, - min in 0u32..=59u32, - sec in 0u32..=59u32, - micro in 0u32..=1_999_999u32 - ) { - Python::with_gil(|py| { - let date_opt = NaiveDate::from_ymd_opt(year, month, day); - let time_opt = NaiveTime::from_hms_micro_opt(hour, min, sec, micro); - if let (Some(date), Some(time)) = (date_opt, time_opt) { - let dt: DateTime = NaiveDateTime::new(date, time).and_utc(); - // Wrap in CatchWarnings to avoid into_py firing warning for truncated leap second - let py_dt = CatchWarnings::enter(py, |_| dt.into_pyobject(py)).unwrap(); - let roundtripped: DateTime = py_dt.extract().expect("Round trip"); - // Leap seconds are not roundtripped - let expected_roundtrip_time = micro.checked_sub(1_000_000).map(|micro| NaiveTime::from_hms_micro_opt(hour, min, sec, micro).unwrap()).unwrap_or(time); - let expected_roundtrip_dt: DateTime = NaiveDateTime::new(date, expected_roundtrip_time).and_utc(); - assert_eq!(expected_roundtrip_dt, roundtripped); - } - }) - } - - #[test] - fn test_fixed_offset_datetime_roundtrip( - year in 1i32..=9999i32, - month in 1u32..=12u32, - day in 1u32..=31u32, - hour in 0u32..=23u32, - min in 0u32..=59u32, - sec in 0u32..=59u32, - micro in 0u32..=1_999_999u32, - offset_secs in -86399i32..=86399i32 - ) { - Python::with_gil(|py| { - let date_opt = NaiveDate::from_ymd_opt(year, month, day); - let time_opt = NaiveTime::from_hms_micro_opt(hour, min, sec, micro); - let offset = FixedOffset::east_opt(offset_secs).unwrap(); - if let (Some(date), Some(time)) = (date_opt, time_opt) { - let dt: DateTime = NaiveDateTime::new(date, time).and_local_timezone(offset).unwrap(); - // Wrap in CatchWarnings to avoid into_py firing warning for truncated leap second - let py_dt = CatchWarnings::enter(py, |_| dt.into_pyobject(py)).unwrap(); - let roundtripped: DateTime = py_dt.extract().expect("Round trip"); - // Leap seconds are not roundtripped - let expected_roundtrip_time = micro.checked_sub(1_000_000).map(|micro| NaiveTime::from_hms_micro_opt(hour, min, sec, micro).unwrap()).unwrap_or(time); - let expected_roundtrip_dt: DateTime = NaiveDateTime::new(date, expected_roundtrip_time).and_local_timezone(offset).unwrap(); - assert_eq!(expected_roundtrip_dt, roundtripped); - } - }) - } - } - } -} diff --git a/vendor/pyo3/src/conversions/chrono_tz.rs b/vendor/pyo3/src/conversions/chrono_tz.rs deleted file mode 100644 index 60a3bab491..0000000000 --- a/vendor/pyo3/src/conversions/chrono_tz.rs +++ /dev/null @@ -1,196 +0,0 @@ -#![cfg(all(Py_3_9, feature = "chrono-tz"))] - -//! Conversions to and from [chrono-tz](https://docs.rs/chrono-tz/)’s `Tz`. -//! -//! This feature requires at least Python 3.9. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! chrono-tz = "0.8" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"chrono-tz\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of chrono, chrono-tz and PyO3. -//! The required chrono version may vary based on the version of PyO3. -//! -//! # Example: Convert a `zoneinfo.ZoneInfo` to chrono-tz's `Tz` -//! -//! ```rust,no_run -//! use chrono_tz::Tz; -//! use pyo3::{Python, PyResult, IntoPyObject, types::PyAnyMethods}; -//! -//! fn main() -> PyResult<()> { -//! pyo3::prepare_freethreaded_python(); -//! Python::with_gil(|py| { -//! // Convert to Python -//! let py_tzinfo = Tz::Europe__Paris.into_pyobject(py)?; -//! // Convert back to Rust -//! assert_eq!(py_tzinfo.extract::()?, Tz::Europe__Paris); -//! Ok(()) -//! }) -//! } -//! ``` -use crate::conversion::IntoPyObject; -use crate::exceptions::PyValueError; -use crate::pybacked::PyBackedStr; -use crate::sync::GILOnceCell; -use crate::types::{any::PyAnyMethods, PyType}; -use crate::{intern, Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use chrono_tz::Tz; -use std::str::FromStr; - -#[allow(deprecated)] -impl ToPyObject for Tz { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Tz { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Tz { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - static ZONE_INFO: GILOnceCell> = GILOnceCell::new(); - ZONE_INFO - .import(py, "zoneinfo", "ZoneInfo") - .and_then(|obj| obj.call1((self.name(),))) - } -} - -impl<'py> IntoPyObject<'py> for &Tz { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl FromPyObject<'_> for Tz { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - Tz::from_str( - &ob.getattr(intern!(ob.py(), "key"))? - .extract::()?, - ) - .map_err(|e| PyValueError::new_err(e.to_string())) - } -} - -#[cfg(all(test, not(windows)))] // Troubles loading timezones on Windows -mod tests { - use super::*; - use crate::prelude::PyAnyMethods; - use crate::Python; - use chrono::{DateTime, Utc}; - use chrono_tz::Tz; - - #[test] - fn test_frompyobject() { - Python::with_gil(|py| { - assert_eq!( - new_zoneinfo(py, "Europe/Paris").extract::().unwrap(), - Tz::Europe__Paris - ); - assert_eq!(new_zoneinfo(py, "UTC").extract::().unwrap(), Tz::UTC); - assert_eq!( - new_zoneinfo(py, "Etc/GMT-5").extract::().unwrap(), - Tz::Etc__GMTMinus5 - ); - }); - } - - #[test] - fn test_ambiguous_datetime_to_pyobject() { - let dates = [ - DateTime::::from_str("2020-10-24 23:00:00 UTC").unwrap(), - DateTime::::from_str("2020-10-25 00:00:00 UTC").unwrap(), - DateTime::::from_str("2020-10-25 01:00:00 UTC").unwrap(), - ]; - - let dates = dates.map(|dt| dt.with_timezone(&Tz::Europe__London)); - - assert_eq!( - dates.map(|dt| dt.to_string()), - [ - "2020-10-25 00:00:00 BST", - "2020-10-25 01:00:00 BST", - "2020-10-25 01:00:00 GMT" - ] - ); - - let dates = Python::with_gil(|py| { - let pydates = dates.map(|dt| dt.into_pyobject(py).unwrap()); - assert_eq!( - pydates - .clone() - .map(|dt| dt.getattr("hour").unwrap().extract::().unwrap()), - [0, 1, 1] - ); - - assert_eq!( - pydates - .clone() - .map(|dt| dt.getattr("fold").unwrap().extract::().unwrap() > 0), - [false, false, true] - ); - - pydates.map(|dt| dt.extract::>().unwrap()) - }); - - assert_eq!( - dates.map(|dt| dt.to_string()), - [ - "2020-10-25 00:00:00 BST", - "2020-10-25 01:00:00 BST", - "2020-10-25 01:00:00 GMT" - ] - ); - } - - #[test] - #[cfg(not(Py_GIL_DISABLED))] // https://github.com/python/cpython/issues/116738#issuecomment-2404360445 - fn test_into_pyobject() { - Python::with_gil(|py| { - let assert_eq = |l: Bound<'_, PyAny>, r: Bound<'_, PyAny>| { - assert!(l.eq(&r).unwrap(), "{:?} != {:?}", l, r); - }; - - assert_eq( - Tz::Europe__Paris.into_pyobject(py).unwrap(), - new_zoneinfo(py, "Europe/Paris"), - ); - assert_eq(Tz::UTC.into_pyobject(py).unwrap(), new_zoneinfo(py, "UTC")); - assert_eq( - Tz::Etc__GMTMinus5.into_pyobject(py).unwrap(), - new_zoneinfo(py, "Etc/GMT-5"), - ); - }); - } - - fn new_zoneinfo<'py>(py: Python<'py>, name: &str) -> Bound<'py, PyAny> { - zoneinfo_class(py).call1((name,)).unwrap() - } - - fn zoneinfo_class(py: Python<'_>) -> Bound<'_, PyAny> { - py.import("zoneinfo").unwrap().getattr("ZoneInfo").unwrap() - } -} diff --git a/vendor/pyo3/src/conversions/either.rs b/vendor/pyo3/src/conversions/either.rs deleted file mode 100644 index a514b1fde8..0000000000 --- a/vendor/pyo3/src/conversions/either.rs +++ /dev/null @@ -1,199 +0,0 @@ -#![cfg(feature = "either")] - -//! Conversion to/from -//! [either](https://docs.rs/either/ "A library for easy idiomatic error handling and reporting in Rust applications")’s -//! [`Either`] type to a union of two Python types. -//! -//! Use of a generic sum type like [either] is common when you want to either accept one of two possible -//! types as an argument or return one of two possible types from a function, without having to define -//! a helper type manually yourself. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! ## change * to the version you want to use, ideally the latest. -//! either = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"either\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of either and PyO3. -//! The required either version may vary based on the version of PyO3. -//! -//! # Example: Convert a `int | str` to `Either`. -//! -//! ```rust -//! use either::Either; -//! use pyo3::{Python, PyResult, IntoPyObject, types::PyAnyMethods}; -//! -//! fn main() -> PyResult<()> { -//! pyo3::prepare_freethreaded_python(); -//! Python::with_gil(|py| { -//! // Create a string and an int in Python. -//! let py_str = "crab".into_pyobject(py)?; -//! let py_int = 42i32.into_pyobject(py)?; -//! // Now convert it to an Either. -//! let either_str: Either = py_str.extract()?; -//! let either_int: Either = py_int.extract()?; -//! Ok(()) -//! }) -//! } -//! ``` -//! -//! [either](https://docs.rs/either/ "A library for easy idiomatic error handling and reporting in Rust applications")’s - -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - exceptions::PyTypeError, types::any::PyAnyMethods, Bound, FromPyObject, IntoPyObject, - IntoPyObjectExt, PyAny, PyErr, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use either::Either; - -#[cfg_attr(docsrs, doc(cfg(feature = "either")))] -#[allow(deprecated)] -impl IntoPy for Either -where - L: IntoPy, - R: IntoPy, -{ - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Either::Left(l) => l.into_py(py), - Either::Right(r) => r.into_py(py), - } - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "either")))] -impl<'py, L, R> IntoPyObject<'py> for Either -where - L: IntoPyObject<'py>, - R: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - match self { - Either::Left(l) => l.into_bound_py_any(py), - Either::Right(r) => r.into_bound_py_any(py), - } - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "either")))] -impl<'a, 'py, L, R> IntoPyObject<'py> for &'a Either -where - &'a L: IntoPyObject<'py>, - &'a R: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - match self { - Either::Left(l) => l.into_bound_py_any(py), - Either::Right(r) => r.into_bound_py_any(py), - } - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "either")))] -#[allow(deprecated)] -impl ToPyObject for Either -where - L: ToPyObject, - R: ToPyObject, -{ - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - match self { - Either::Left(l) => l.to_object(py), - Either::Right(r) => r.to_object(py), - } - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "either")))] -impl<'py, L, R> FromPyObject<'py> for Either -where - L: FromPyObject<'py>, - R: FromPyObject<'py>, -{ - #[inline] - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - if let Ok(l) = obj.extract::() { - Ok(Either::Left(l)) - } else if let Ok(r) = obj.extract::() { - Ok(Either::Right(r)) - } else { - // TODO: it might be nice to use the `type_input()` name here once `type_input` - // is not experimental, rather than the Rust type names. - let err_msg = format!( - "failed to convert the value to 'Union[{}, {}]'", - std::any::type_name::(), - std::any::type_name::() - ); - Err(PyTypeError::new_err(err_msg)) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::union_of(&[L::type_input(), R::type_input()]) - } -} - -#[cfg(test)] -mod tests { - use std::borrow::Cow; - - use crate::exceptions::PyTypeError; - use crate::{IntoPyObject, Python}; - - use crate::types::PyAnyMethods; - use either::Either; - - #[test] - fn test_either_conversion() { - type E = Either; - type E1 = Either; - type E2 = Either; - - Python::with_gil(|py| { - let l = E::Left(42); - let obj_l = (&l).into_pyobject(py).unwrap(); - assert_eq!(obj_l.extract::().unwrap(), 42); - assert_eq!(obj_l.extract::().unwrap(), l); - - let r = E::Right("foo".to_owned()); - let obj_r = (&r).into_pyobject(py).unwrap(); - assert_eq!(obj_r.extract::>().unwrap(), "foo"); - assert_eq!(obj_r.extract::().unwrap(), r); - - let obj_s = "foo".into_pyobject(py).unwrap(); - let err = obj_s.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - assert_eq!( - err.to_string(), - "TypeError: failed to convert the value to 'Union[i32, f32]'" - ); - - let obj_i = 42i32.into_pyobject(py).unwrap(); - assert_eq!(obj_i.extract::().unwrap(), E1::Left(42)); - assert_eq!(obj_i.extract::().unwrap(), E2::Left(42.0)); - - let obj_f = 42.0f64.into_pyobject(py).unwrap(); - assert_eq!(obj_f.extract::().unwrap(), E1::Right(42.0)); - assert_eq!(obj_f.extract::().unwrap(), E2::Left(42.0)); - }); - } -} diff --git a/vendor/pyo3/src/conversions/eyre.rs b/vendor/pyo3/src/conversions/eyre.rs deleted file mode 100644 index 42d7a12c87..0000000000 --- a/vendor/pyo3/src/conversions/eyre.rs +++ /dev/null @@ -1,200 +0,0 @@ -#![cfg(feature = "eyre")] - -//! A conversion from -//! [eyre](https://docs.rs/eyre/ "A library for easy idiomatic error handling and reporting in Rust applications.")’s -//! [`Report`] type to [`PyErr`]. -//! -//! Use of an error handling library like [eyre] is common in application code and when you just -//! want error handling to be easy. If you are writing a library or you need more control over your -//! errors you might want to design your own error type instead. -//! -//! When the inner error is a [`PyErr`] without source, it will be extracted out. -//! Otherwise a Python [`RuntimeError`] will be created. -//! You might find that you need to map the error from your Rust code into another Python exception. -//! See [`PyErr::new`] for more information about that. -//! -//! For information about error handling in general, see the [Error handling] chapter of the Rust -//! book. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! ## change * to the version you want to use, ideally the latest. -//! eyre = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"eyre\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of eyre and PyO3. -//! The required eyre version may vary based on the version of PyO3. -//! -//! # Example: Propagating a `PyErr` into [`eyre::Report`] -//! -//! ```rust -//! use pyo3::prelude::*; -//! use std::path::PathBuf; -//! -//! // A wrapper around a Rust function. -//! // The pyfunction macro performs the conversion to a PyErr -//! #[pyfunction] -//! fn py_open(filename: PathBuf) -> eyre::Result> { -//! let data = std::fs::read(filename)?; -//! Ok(data) -//! } -//! -//! fn main() { -//! let error = Python::with_gil(|py| -> PyResult> { -//! let fun = wrap_pyfunction!(py_open, py)?; -//! let text = fun.call1(("foo.txt",))?.extract::>()?; -//! Ok(text) -//! }).unwrap_err(); -//! -//! println!("{}", error); -//! } -//! ``` -//! -//! # Example: Using `eyre` in general -//! -//! Note that you don't need this feature to convert a [`PyErr`] into an [`eyre::Report`], because -//! it can already convert anything that implements [`Error`](std::error::Error): -//! -//! ```rust -//! use pyo3::prelude::*; -//! use pyo3::types::PyBytes; -//! -//! // An example function that must handle multiple error types. -//! // -//! // To do this you usually need to design your own error type or use -//! // `Box`. `eyre` is a convenient alternative for this. -//! pub fn decompress(bytes: &[u8]) -> eyre::Result { -//! // An arbitrary example of a Python api you -//! // could call inside an application... -//! // This might return a `PyErr`. -//! let res = Python::with_gil(|py| { -//! let zlib = PyModule::import(py, "zlib")?; -//! let decompress = zlib.getattr("decompress")?; -//! let bytes = PyBytes::new(py, bytes); -//! let value = decompress.call1((bytes,))?; -//! value.extract::>() -//! })?; -//! -//! // This might be a `FromUtf8Error`. -//! let text = String::from_utf8(res)?; -//! -//! Ok(text) -//! } -//! -//! fn main() -> eyre::Result<()> { -//! let bytes: &[u8] = b"x\x9c\x8b\xcc/U(\xce\xc8/\xcdIQ((\xcaOJL\xca\xa9T\ -//! (-NU(\xc9HU\xc8\xc9LJ\xcbI,IUH.\x02\x91\x99y\xc5%\ -//! \xa9\x89)z\x00\xf2\x15\x12\xfe"; -//! let text = decompress(bytes)?; -//! -//! println!("The text is \"{}\"", text); -//! # assert_eq!(text, "You should probably use the libflate crate instead."); -//! Ok(()) -//! } -//! ``` -//! -//! [eyre]: https://docs.rs/eyre/ "A library for easy idiomatic error handling and reporting in Rust applications." -//! [`RuntimeError`]: https://docs.python.org/3/library/exceptions.html#RuntimeError "Built-in Exceptions — Python documentation" -//! [Error handling]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html "Recoverable Errors with Result - The Rust Programming Language" - -use crate::exceptions::PyRuntimeError; -use crate::PyErr; -use eyre::Report; - -/// Converts [`eyre::Report`] to a [`PyErr`] containing a [`PyRuntimeError`]. -/// -/// If you want to raise a different Python exception you will have to do so manually. See -/// [`PyErr::new`] for more information about that. -impl From for PyErr { - fn from(mut error: Report) -> Self { - // Errors containing a PyErr without chain or context are returned as the underlying error - if error.source().is_none() { - error = match error.downcast::() { - Ok(py_err) => return py_err, - Err(error) => error, - }; - } - PyRuntimeError::new_err(format!("{:?}", error)) - } -} - -#[cfg(test)] -mod tests { - use crate::exceptions::{PyRuntimeError, PyValueError}; - use crate::types::IntoPyDict; - use crate::{ffi, prelude::*}; - - use eyre::{bail, eyre, Report, Result, WrapErr}; - - fn f() -> Result<()> { - use std::io; - bail!(io::Error::new(io::ErrorKind::PermissionDenied, "oh no!")); - } - - fn g() -> Result<()> { - f().wrap_err("f failed") - } - - fn h() -> Result<()> { - g().wrap_err("g failed") - } - - #[test] - fn test_pyo3_exception_contents() { - let err = h().unwrap_err(); - let expected_contents = format!("{:?}", err); - let pyerr = PyErr::from(err); - - Python::with_gil(|py| { - let locals = [("err", pyerr)].into_py_dict(py).unwrap(); - let pyerr = py - .run(ffi::c_str!("raise err"), None, Some(&locals)) - .unwrap_err(); - assert_eq!(pyerr.value(py).to_string(), expected_contents); - }) - } - - fn k() -> Result<()> { - Err(eyre!("Some sort of error")) - } - - #[test] - fn test_pyo3_exception_contents2() { - let err = k().unwrap_err(); - let expected_contents = format!("{:?}", err); - let pyerr = PyErr::from(err); - - Python::with_gil(|py| { - let locals = [("err", pyerr)].into_py_dict(py).unwrap(); - let pyerr = py - .run(ffi::c_str!("raise err"), None, Some(&locals)) - .unwrap_err(); - assert_eq!(pyerr.value(py).to_string(), expected_contents); - }) - } - - #[test] - fn test_pyo3_unwrap_simple_err() { - let origin_exc = PyValueError::new_err("Value Error"); - let report: Report = origin_exc.into(); - let converted: PyErr = report.into(); - assert!(Python::with_gil( - |py| converted.is_instance_of::(py) - )) - } - #[test] - fn test_pyo3_unwrap_complex_err() { - let origin_exc = PyValueError::new_err("Value Error"); - let mut report: Report = origin_exc.into(); - report = report.wrap_err("Wrapped"); - let converted: PyErr = report.into(); - assert!(Python::with_gil( - |py| converted.is_instance_of::(py) - )) - } -} diff --git a/vendor/pyo3/src/conversions/hashbrown.rs b/vendor/pyo3/src/conversions/hashbrown.rs deleted file mode 100644 index 0efe7f5161..0000000000 --- a/vendor/pyo3/src/conversions/hashbrown.rs +++ /dev/null @@ -1,263 +0,0 @@ -#![cfg(feature = "hashbrown")] - -//! Conversions to and from [hashbrown](https://docs.rs/hashbrown/)’s -//! `HashMap` and `HashSet`. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! # change * to the latest versions -//! hashbrown = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"hashbrown\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of hashbrown and PyO3. -//! The required hashbrown version may vary based on the version of PyO3. -use crate::{ - conversion::IntoPyObject, - types::{ - any::PyAnyMethods, - dict::PyDictMethods, - frozenset::PyFrozenSetMethods, - set::{new_from_iter, try_new_from_iter, PySetMethods}, - PyDict, PyFrozenSet, PySet, - }, - Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::{cmp, hash}; - -#[allow(deprecated)] -impl ToPyObject for hashbrown::HashMap -where - K: hash::Hash + cmp::Eq + ToPyObject, - V: ToPyObject, - H: hash::BuildHasher, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.to_object(py), v.to_object(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for hashbrown::HashMap -where - K: hash::Hash + cmp::Eq + IntoPy, - V: IntoPy, - H: hash::BuildHasher, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.into_py(py), v.into_py(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -impl<'py, K, V, H> IntoPyObject<'py> for hashbrown::HashMap -where - K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - V: IntoPyObject<'py>, - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } -} - -impl<'a, 'py, K, V, H> IntoPyObject<'py> for &'a hashbrown::HashMap -where - &'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - &'a V: IntoPyObject<'py>, - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } -} - -impl<'py, K, V, S> FromPyObject<'py> for hashbrown::HashMap -where - K: FromPyObject<'py> + cmp::Eq + hash::Hash, - V: FromPyObject<'py>, - S: hash::BuildHasher + Default, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { - let dict = ob.downcast::()?; - let mut ret = hashbrown::HashMap::with_capacity_and_hasher(dict.len(), S::default()); - for (k, v) in dict { - ret.insert(k.extract()?, v.extract()?); - } - Ok(ret) - } -} - -#[allow(deprecated)] -impl ToPyObject for hashbrown::HashSet -where - T: hash::Hash + Eq + ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - new_from_iter(py, self) - .expect("Failed to create Python set from hashbrown::HashSet") - .into() - } -} - -#[allow(deprecated)] -impl IntoPy for hashbrown::HashSet -where - K: IntoPy + Eq + hash::Hash, - S: hash::BuildHasher + Default, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - new_from_iter(py, self.into_iter().map(|item| item.into_py(py))) - .expect("Failed to create Python set from hashbrown::HashSet") - .into() - } -} - -impl<'py, K, H> IntoPyObject<'py> for hashbrown::HashSet -where - K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - H: hash::BuildHasher, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self) - } -} - -impl<'a, 'py, K, H> IntoPyObject<'py> for &'a hashbrown::HashSet -where - &'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - H: hash::BuildHasher, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self) - } -} - -impl<'py, K, S> FromPyObject<'py> for hashbrown::HashSet -where - K: FromPyObject<'py> + cmp::Eq + hash::Hash, - S: hash::BuildHasher + Default, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - match ob.downcast::() { - Ok(set) => set.iter().map(|any| any.extract()).collect(), - Err(err) => { - if let Ok(frozen_set) = ob.downcast::() { - frozen_set.iter().map(|any| any.extract()).collect() - } else { - Err(PyErr::from(err)) - } - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::IntoPyDict; - - #[test] - fn test_hashbrown_hashmap_into_pyobject() { - Python::with_gil(|py| { - let mut map = hashbrown::HashMap::::new(); - map.insert(1, 1); - - let py_map = (&map).into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - assert_eq!(map, py_map.extract().unwrap()); - }); - } - - #[test] - fn test_hashbrown_hashmap_into_dict() { - Python::with_gil(|py| { - let mut map = hashbrown::HashMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 1); - assert_eq!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[test] - fn test_extract_hashbrown_hashset() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: hashbrown::HashSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - - let set = PyFrozenSet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: hashbrown::HashSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - }); - } - - #[test] - fn test_hashbrown_hashset_into_pyobject() { - Python::with_gil(|py| { - let hs: hashbrown::HashSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - - let hso = hs.clone().into_pyobject(py).unwrap(); - - assert_eq!(hs, hso.extract().unwrap()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/indexmap.rs b/vendor/pyo3/src/conversions/indexmap.rs deleted file mode 100644 index e3787e6809..0000000000 --- a/vendor/pyo3/src/conversions/indexmap.rs +++ /dev/null @@ -1,264 +0,0 @@ -#![cfg(feature = "indexmap")] - -//! Conversions to and from [indexmap](https://docs.rs/indexmap/)’s -//! `IndexMap`. -//! -//! [`indexmap::IndexMap`] is a hash table that is closely compatible with the standard [`std::collections::HashMap`], -//! with the difference that it preserves the insertion order when iterating over keys. It was inspired -//! by Python's 3.6+ dict implementation. -//! -//! Dictionary order is guaranteed to be insertion order in Python, hence IndexMap is a good candidate -//! for maintaining an equivalent behaviour in Rust. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! # change * to the latest versions -//! indexmap = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"indexmap\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of indexmap and PyO3. -//! The required indexmap version may vary based on the version of PyO3. -//! -//! # Examples -//! -//! Using [indexmap](https://docs.rs/indexmap) to return a dictionary with some statistics -//! about a list of numbers. Because of the insertion order guarantees, the Python code will -//! always print the same result, matching users' expectations about Python's dict. -//! ```rust -//! use indexmap::{indexmap, IndexMap}; -//! use pyo3::prelude::*; -//! -//! fn median(data: &Vec) -> f32 { -//! let sorted_data = data.clone().sort(); -//! let mid = data.len() / 2; -//! if data.len() % 2 == 0 { -//! data[mid] as f32 -//! } -//! else { -//! (data[mid] + data[mid - 1]) as f32 / 2.0 -//! } -//! } -//! -//! fn mean(data: &Vec) -> f32 { -//! data.iter().sum::() as f32 / data.len() as f32 -//! } -//! fn mode(data: &Vec) -> f32 { -//! let mut frequency = IndexMap::new(); // we can use IndexMap as any hash table -//! -//! for &element in data { -//! *frequency.entry(element).or_insert(0) += 1; -//! } -//! -//! frequency -//! .iter() -//! .max_by(|a, b| a.1.cmp(&b.1)) -//! .map(|(k, _v)| *k) -//! .unwrap() as f32 -//! } -//! -//! #[pyfunction] -//! fn calculate_statistics(data: Vec) -> IndexMap<&'static str, f32> { -//! indexmap! { -//! "median" => median(&data), -//! "mean" => mean(&data), -//! "mode" => mode(&data), -//! } -//! } -//! -//! #[pymodule] -//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?; -//! Ok(()) -//! } -//! ``` -//! -//! Python code: -//! ```python -//! from my_module import calculate_statistics -//! -//! data = [1, 1, 1, 3, 4, 5] -//! print(calculate_statistics(data)) -//! # always prints {"median": 2.0, "mean": 2.5, "mode": 1.0} in the same order -//! # if another hash table was used, the order could be random -//! ``` - -use crate::conversion::IntoPyObject; -use crate::types::*; -use crate::{Bound, FromPyObject, PyErr, PyObject, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::{cmp, hash}; - -#[allow(deprecated)] -impl ToPyObject for indexmap::IndexMap -where - K: hash::Hash + cmp::Eq + ToPyObject, - V: ToPyObject, - H: hash::BuildHasher, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.to_object(py), v.to_object(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for indexmap::IndexMap -where - K: hash::Hash + cmp::Eq + IntoPy, - V: IntoPy, - H: hash::BuildHasher, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.into_py(py), v.into_py(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -impl<'py, K, V, H> IntoPyObject<'py> for indexmap::IndexMap -where - K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - V: IntoPyObject<'py>, - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } -} - -impl<'a, 'py, K, V, H> IntoPyObject<'py> for &'a indexmap::IndexMap -where - &'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - &'a V: IntoPyObject<'py>, - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } -} - -impl<'py, K, V, S> FromPyObject<'py> for indexmap::IndexMap -where - K: FromPyObject<'py> + cmp::Eq + hash::Hash, - V: FromPyObject<'py>, - S: hash::BuildHasher + Default, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { - let dict = ob.downcast::()?; - let mut ret = indexmap::IndexMap::with_capacity_and_hasher(dict.len(), S::default()); - for (k, v) in dict { - ret.insert(k.extract()?, v.extract()?); - } - Ok(ret) - } -} - -#[cfg(test)] -mod test_indexmap { - - use crate::types::*; - use crate::{IntoPyObject, Python}; - - #[test] - fn test_indexmap_indexmap_into_pyobject() { - Python::with_gil(|py| { - let mut map = indexmap::IndexMap::::new(); - map.insert(1, 1); - - let py_map = (&map).into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - assert_eq!( - map, - py_map.extract::>().unwrap() - ); - }); - } - - #[test] - fn test_indexmap_indexmap_into_dict() { - Python::with_gil(|py| { - let mut map = indexmap::IndexMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 1); - assert_eq!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[test] - fn test_indexmap_indexmap_insertion_order_round_trip() { - Python::with_gil(|py| { - let n = 20; - let mut map = indexmap::IndexMap::::new(); - - for i in 1..=n { - if i % 2 == 1 { - map.insert(i, i); - } else { - map.insert(n - i, i); - } - } - - let py_map = (&map).into_py_dict(py).unwrap(); - - let trip_map = py_map.extract::>().unwrap(); - - for (((k1, v1), (k2, v2)), (k3, v3)) in - map.iter().zip(py_map.iter()).zip(trip_map.iter()) - { - let k2 = k2.extract::().unwrap(); - let v2 = v2.extract::().unwrap(); - assert_eq!((k1, v1), (&k2, &v2)); - assert_eq!((k1, v1), (k3, v3)); - assert_eq!((&k2, &v2), (k3, v3)); - } - }); - } -} diff --git a/vendor/pyo3/src/conversions/mod.rs b/vendor/pyo3/src/conversions/mod.rs deleted file mode 100644 index 53ecf849c0..0000000000 --- a/vendor/pyo3/src/conversions/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! This module contains conversions between various Rust object and their representation in Python. - -pub mod anyhow; -pub mod chrono; -pub mod chrono_tz; -pub mod either; -pub mod eyre; -pub mod hashbrown; -pub mod indexmap; -pub mod num_bigint; -pub mod num_complex; -pub mod num_rational; -pub mod rust_decimal; -pub mod serde; -pub mod smallvec; -mod std; diff --git a/vendor/pyo3/src/conversions/num_bigint.rs b/vendor/pyo3/src/conversions/num_bigint.rs deleted file mode 100644 index d91973b557..0000000000 --- a/vendor/pyo3/src/conversions/num_bigint.rs +++ /dev/null @@ -1,478 +0,0 @@ -#![cfg(feature = "num-bigint")] -//! Conversions to and from [num-bigint](https://docs.rs/num-bigint)’s [`BigInt`] and [`BigUint`] types. -//! -//! This is useful for converting Python integers when they may not fit in Rust's built-in integer types. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! num-bigint = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"num-bigint\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of num-bigint and PyO3. -//! The required num-bigint version may vary based on the version of PyO3. -//! -//! ## Examples -//! -//! Using [`BigInt`] to correctly increment an arbitrary precision integer. -//! This is not possible with Rust's native integers if the Python integer is too large, -//! in which case it will fail its conversion and raise `OverflowError`. -//! ```rust -//! use num_bigint::BigInt; -//! use pyo3::prelude::*; -//! -//! #[pyfunction] -//! fn add_one(n: BigInt) -> BigInt { -//! n + 1 -//! } -//! -//! #[pymodule] -//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(add_one, m)?)?; -//! Ok(()) -//! } -//! ``` -//! -//! Python code: -//! ```python -//! from my_module import add_one -//! -//! n = 1 << 1337 -//! value = add_one(n) -//! -//! assert n + 1 == value -//! ``` - -#[cfg(Py_LIMITED_API)] -use crate::types::{bytes::PyBytesMethods, PyBytes}; -use crate::{ - conversion::IntoPyObject, - ffi, - instance::Bound, - types::{any::PyAnyMethods, PyInt}, - FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -use num_bigint::{BigInt, BigUint}; - -#[cfg(not(Py_LIMITED_API))] -use num_bigint::Sign; - -// for identical functionality between BigInt and BigUint -macro_rules! bigint_conversion { - ($rust_ty: ty, $is_signed: literal, $to_bytes: path) => { - #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] - #[allow(deprecated)] - impl ToPyObject for $rust_ty { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] - #[allow(deprecated)] - impl IntoPy for $rust_ty { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] - impl<'py> IntoPyObject<'py> for $rust_ty { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (&self).into_pyobject(py) - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] - impl<'py> IntoPyObject<'py> for &$rust_ty { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[cfg(not(Py_LIMITED_API))] - fn into_pyobject(self, py: Python<'py>) -> Result { - use crate::ffi_ptr_ext::FfiPtrExt; - let bytes = $to_bytes(&self); - unsafe { - Ok(ffi::_PyLong_FromByteArray( - bytes.as_ptr().cast(), - bytes.len(), - 1, - $is_signed.into(), - ) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - - #[cfg(Py_LIMITED_API)] - fn into_pyobject(self, py: Python<'py>) -> Result { - use $crate::py_result_ext::PyResultExt; - let bytes = $to_bytes(&self); - let bytes_obj = PyBytes::new(py, &bytes); - let kwargs = if $is_signed { - let kwargs = crate::types::PyDict::new(py); - kwargs.set_item(crate::intern!(py, "signed"), true)?; - Some(kwargs) - } else { - None - }; - unsafe { - py.get_type::() - .call_method("from_bytes", (bytes_obj, "little"), kwargs.as_ref()) - .downcast_into_unchecked() - } - } - } - }; -} - -bigint_conversion!(BigUint, false, BigUint::to_bytes_le); -bigint_conversion!(BigInt, true, BigInt::to_signed_bytes_le); - -#[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] -impl<'py> FromPyObject<'py> for BigInt { - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - let py = ob.py(); - // fast path - checking for subclass of `int` just checks a bit in the type object - let num_owned: Py; - let num = if let Ok(long) = ob.downcast::() { - long - } else { - num_owned = unsafe { Py::from_owned_ptr_or_err(py, ffi::PyNumber_Index(ob.as_ptr()))? }; - num_owned.bind(py) - }; - #[cfg(not(Py_LIMITED_API))] - { - let mut buffer = int_to_u32_vec::(num)?; - let sign = if buffer.last().copied().map_or(false, |last| last >> 31 != 0) { - // BigInt::new takes an unsigned array, so need to convert from two's complement - // flip all bits, 'subtract' 1 (by adding one to the unsigned array) - let mut elements = buffer.iter_mut(); - for element in elements.by_ref() { - *element = (!*element).wrapping_add(1); - if *element != 0 { - // if the element didn't wrap over, no need to keep adding further ... - break; - } - } - // ... so just two's complement the rest - for element in elements { - *element = !*element; - } - Sign::Minus - } else { - Sign::Plus - }; - Ok(BigInt::new(sign, buffer)) - } - #[cfg(Py_LIMITED_API)] - { - let n_bits = int_n_bits(num)?; - if n_bits == 0 { - return Ok(BigInt::from(0isize)); - } - let bytes = int_to_py_bytes(num, (n_bits + 8) / 8, true)?; - Ok(BigInt::from_signed_bytes_le(bytes.as_bytes())) - } - } -} - -#[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] -impl<'py> FromPyObject<'py> for BigUint { - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - let py = ob.py(); - // fast path - checking for subclass of `int` just checks a bit in the type object - let num_owned: Py; - let num = if let Ok(long) = ob.downcast::() { - long - } else { - num_owned = unsafe { Py::from_owned_ptr_or_err(py, ffi::PyNumber_Index(ob.as_ptr()))? }; - num_owned.bind(py) - }; - #[cfg(not(Py_LIMITED_API))] - { - let buffer = int_to_u32_vec::(num)?; - Ok(BigUint::new(buffer)) - } - #[cfg(Py_LIMITED_API)] - { - let n_bits = int_n_bits(num)?; - if n_bits == 0 { - return Ok(BigUint::from(0usize)); - } - let bytes = int_to_py_bytes(num, (n_bits + 7) / 8, false)?; - Ok(BigUint::from_bytes_le(bytes.as_bytes())) - } - } -} - -#[cfg(not(any(Py_LIMITED_API, Py_3_13)))] -#[inline] -fn int_to_u32_vec(long: &Bound<'_, PyInt>) -> PyResult> { - let mut buffer = Vec::new(); - let n_bits = int_n_bits(long)?; - if n_bits == 0 { - return Ok(buffer); - } - let n_digits = if SIGNED { - (n_bits + 32) / 32 - } else { - (n_bits + 31) / 32 - }; - buffer.reserve_exact(n_digits); - unsafe { - crate::err::error_on_minusone( - long.py(), - ffi::_PyLong_AsByteArray( - long.as_ptr().cast(), - buffer.as_mut_ptr() as *mut u8, - n_digits * 4, - 1, - SIGNED.into(), - ), - )?; - buffer.set_len(n_digits) - }; - buffer - .iter_mut() - .for_each(|chunk| *chunk = u32::from_le(*chunk)); - - Ok(buffer) -} - -#[cfg(all(not(Py_LIMITED_API), Py_3_13))] -#[inline] -fn int_to_u32_vec(long: &Bound<'_, PyInt>) -> PyResult> { - let mut buffer = Vec::new(); - let mut flags = ffi::Py_ASNATIVEBYTES_LITTLE_ENDIAN; - if !SIGNED { - flags |= ffi::Py_ASNATIVEBYTES_UNSIGNED_BUFFER | ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE; - } - let n_bytes = - unsafe { ffi::PyLong_AsNativeBytes(long.as_ptr().cast(), std::ptr::null_mut(), 0, flags) }; - let n_bytes_unsigned: usize = n_bytes - .try_into() - .map_err(|_| crate::PyErr::fetch(long.py()))?; - if n_bytes == 0 { - return Ok(buffer); - } - // TODO: use div_ceil when MSRV >= 1.73 - let n_digits = { - let adjust = if n_bytes % 4 == 0 { 0 } else { 1 }; - (n_bytes_unsigned / 4) + adjust - }; - buffer.reserve_exact(n_digits); - unsafe { - ffi::PyLong_AsNativeBytes( - long.as_ptr().cast(), - buffer.as_mut_ptr().cast(), - (n_digits * 4).try_into().unwrap(), - flags, - ); - buffer.set_len(n_digits); - }; - buffer - .iter_mut() - .for_each(|chunk| *chunk = u32::from_le(*chunk)); - - Ok(buffer) -} - -#[cfg(Py_LIMITED_API)] -fn int_to_py_bytes<'py>( - long: &Bound<'py, PyInt>, - n_bytes: usize, - is_signed: bool, -) -> PyResult> { - use crate::intern; - let py = long.py(); - let kwargs = if is_signed { - let kwargs = crate::types::PyDict::new(py); - kwargs.set_item(intern!(py, "signed"), true)?; - Some(kwargs) - } else { - None - }; - let bytes = long.call_method( - intern!(py, "to_bytes"), - (n_bytes, intern!(py, "little")), - kwargs.as_ref(), - )?; - Ok(bytes.downcast_into()?) -} - -#[inline] -#[cfg(any(not(Py_3_13), Py_LIMITED_API))] -fn int_n_bits(long: &Bound<'_, PyInt>) -> PyResult { - let py = long.py(); - #[cfg(not(Py_LIMITED_API))] - { - // fast path - let n_bits = unsafe { ffi::_PyLong_NumBits(long.as_ptr()) }; - if n_bits == (-1isize as usize) { - return Err(crate::PyErr::fetch(py)); - } - Ok(n_bits) - } - - #[cfg(Py_LIMITED_API)] - { - // slow path - long.call_method0(crate::intern!(py, "bit_length")) - .and_then(|any| any.extract()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::common::generate_unique_module_name; - use crate::types::{PyDict, PyModule}; - use indoc::indoc; - use pyo3_ffi::c_str; - - fn rust_fib() -> impl Iterator - where - T: From, - for<'a> &'a T: std::ops::Add, - { - let mut f0: T = T::from(1); - let mut f1: T = T::from(1); - std::iter::from_fn(move || { - let f2 = &f0 + &f1; - Some(std::mem::replace(&mut f0, std::mem::replace(&mut f1, f2))) - }) - } - - fn python_fib(py: Python<'_>) -> impl Iterator> + '_ { - let mut f0 = 1i32.into_pyobject(py).unwrap().into_any(); - let mut f1 = 1i32.into_pyobject(py).unwrap().into_any(); - std::iter::from_fn(move || { - let f2 = f0.call_method1("__add__", (&f1,)).unwrap(); - Some(std::mem::replace(&mut f0, std::mem::replace(&mut f1, f2))) - }) - } - - #[test] - fn convert_biguint() { - Python::with_gil(|py| { - // check the first 2000 numbers in the fibonacci sequence - for (py_result, rs_result) in python_fib(py).zip(rust_fib::()).take(2000) { - // Python -> Rust - assert_eq!(py_result.extract::().unwrap(), rs_result); - // Rust -> Python - assert!(py_result.eq(rs_result).unwrap()); - } - }); - } - - #[test] - fn convert_bigint() { - Python::with_gil(|py| { - // check the first 2000 numbers in the fibonacci sequence - for (py_result, rs_result) in python_fib(py).zip(rust_fib::()).take(2000) { - // Python -> Rust - assert_eq!(py_result.extract::().unwrap(), rs_result); - // Rust -> Python - assert!(py_result.eq(&rs_result).unwrap()); - - // negate - - let rs_result = rs_result * -1; - let py_result = py_result.call_method0("__neg__").unwrap(); - - // Python -> Rust - assert_eq!(py_result.extract::().unwrap(), rs_result); - // Rust -> Python - assert!(py_result.eq(rs_result).unwrap()); - } - }); - } - - fn python_index_class(py: Python<'_>) -> Bound<'_, PyModule> { - let index_code = c_str!(indoc!( - r#" - class C: - def __init__(self, x): - self.x = x - def __index__(self): - return self.x - "# - )); - PyModule::from_code( - py, - index_code, - c_str!("index.py"), - &generate_unique_module_name("index"), - ) - .unwrap() - } - - #[test] - fn convert_index_class() { - Python::with_gil(|py| { - let index = python_index_class(py); - let locals = PyDict::new(py); - locals.set_item("index", index).unwrap(); - let ob = py - .eval(ffi::c_str!("index.C(10)"), None, Some(&locals)) - .unwrap(); - let _: BigInt = ob.extract().unwrap(); - }); - } - - #[test] - fn handle_zero() { - Python::with_gil(|py| { - let zero: BigInt = 0i32.into_pyobject(py).unwrap().extract().unwrap(); - assert_eq!(zero, BigInt::from(0)); - }) - } - - /// `OverflowError` on converting Python int to BigInt, see issue #629 - #[test] - fn check_overflow() { - Python::with_gil(|py| { - macro_rules! test { - ($T:ty, $value:expr, $py:expr) => { - let value = $value; - println!("{}: {}", stringify!($T), value); - let python_value = value.clone().into_pyobject(py).unwrap(); - let roundtrip_value = python_value.extract::<$T>().unwrap(); - assert_eq!(value, roundtrip_value); - }; - } - - for i in 0..=256usize { - // test a lot of values to help catch other bugs too - test!(BigInt, BigInt::from(i), py); - test!(BigUint, BigUint::from(i), py); - test!(BigInt, -BigInt::from(i), py); - test!(BigInt, BigInt::from(1) << i, py); - test!(BigUint, BigUint::from(1u32) << i, py); - test!(BigInt, -BigInt::from(1) << i, py); - test!(BigInt, (BigInt::from(1) << i) + 1u32, py); - test!(BigUint, (BigUint::from(1u32) << i) + 1u32, py); - test!(BigInt, (-BigInt::from(1) << i) + 1u32, py); - test!(BigInt, (BigInt::from(1) << i) - 1u32, py); - test!(BigUint, (BigUint::from(1u32) << i) - 1u32, py); - test!(BigInt, (-BigInt::from(1) << i) - 1u32, py); - } - }); - } -} diff --git a/vendor/pyo3/src/conversions/num_complex.rs b/vendor/pyo3/src/conversions/num_complex.rs deleted file mode 100644 index db981a4179..0000000000 --- a/vendor/pyo3/src/conversions/num_complex.rs +++ /dev/null @@ -1,387 +0,0 @@ -#![cfg(feature = "num-complex")] - -//! Conversions to and from [num-complex](https://docs.rs/num-complex)’ -//! [`Complex`]`<`[`f32`]`>` and [`Complex`]`<`[`f64`]`>`. -//! -//! num-complex’ [`Complex`] supports more operations than PyO3's [`PyComplex`] -//! and can be used with the rest of the Rust ecosystem. -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! # change * to the latest versions -//! num-complex = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"num-complex\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of num-complex and PyO3. -//! The required num-complex version may vary based on the version of PyO3. -//! -//! # Examples -//! -//! Using [num-complex](https://docs.rs/num-complex) and [nalgebra](https://docs.rs/nalgebra) -//! to create a pyfunction that calculates the eigenvalues of a 2x2 matrix. -//! ```ignore -//! # // not tested because nalgebra isn't supported on msrv -//! # // please file an issue if it breaks! -//! use nalgebra::base::{dimension::Const, Matrix}; -//! use num_complex::Complex; -//! use pyo3::prelude::*; -//! -//! type T = Complex; -//! -//! #[pyfunction] -//! fn get_eigenvalues(m11: T, m12: T, m21: T, m22: T) -> Vec { -//! let mat = Matrix::, Const<2>, _>::new(m11, m12, m21, m22); -//! -//! match mat.eigenvalues() { -//! Some(e) => e.data.as_slice().to_vec(), -//! None => vec![], -//! } -//! } -//! -//! #[pymodule] -//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(get_eigenvalues, m)?)?; -//! Ok(()) -//! } -//! # // test -//! # use assert_approx_eq::assert_approx_eq; -//! # use nalgebra::ComplexField; -//! # use pyo3::types::PyComplex; -//! # -//! # fn main() -> PyResult<()> { -//! # Python::with_gil(|py| -> PyResult<()> { -//! # let module = PyModule::new(py, "my_module")?; -//! # -//! # module.add_function(&wrap_pyfunction!(get_eigenvalues, module)?)?; -//! # -//! # let m11 = PyComplex::from_doubles(py, 0_f64, -1_f64); -//! # let m12 = PyComplex::from_doubles(py, 1_f64, 0_f64); -//! # let m21 = PyComplex::from_doubles(py, 2_f64, -1_f64); -//! # let m22 = PyComplex::from_doubles(py, -1_f64, 0_f64); -//! # -//! # let result = module -//! # .getattr("get_eigenvalues")? -//! # .call1((m11, m12, m21, m22))?; -//! # println!("eigenvalues: {:?}", result); -//! # -//! # let result = result.extract::>()?; -//! # let e0 = result[0]; -//! # let e1 = result[1]; -//! # -//! # assert_approx_eq!(e0, Complex::new(1_f64, -1_f64)); -//! # assert_approx_eq!(e1, Complex::new(-2_f64, 0_f64)); -//! # -//! # Ok(()) -//! # }) -//! # } -//! ``` -//! -//! Python code: -//! ```python -//! from my_module import get_eigenvalues -//! -//! m11 = complex(0,-1) -//! m12 = complex(1,0) -//! m21 = complex(2,-1) -//! m22 = complex(-1,0) -//! -//! result = get_eigenvalues(m11,m12,m21,m22) -//! assert result == [complex(1,-1), complex(-2,0)] -//! ``` -#[allow(deprecated)] -use crate::ToPyObject; -use crate::{ - ffi, - ffi_ptr_ext::FfiPtrExt, - types::{any::PyAnyMethods, PyComplex}, - Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, -}; -use num_complex::Complex; -use std::os::raw::c_double; - -impl PyComplex { - /// Creates a new Python `PyComplex` object from `num_complex`'s [`Complex`]. - pub fn from_complex_bound>( - py: Python<'_>, - complex: Complex, - ) -> Bound<'_, PyComplex> { - unsafe { - ffi::PyComplex_FromDoubles(complex.re.into(), complex.im.into()) - .assume_owned(py) - .downcast_into_unchecked() - } - } -} - -macro_rules! complex_conversion { - ($float: ty) => { - #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] - #[allow(deprecated)] - impl ToPyObject for Complex<$float> { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - crate::IntoPy::::into_py(self.to_owned(), py) - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] - #[allow(deprecated)] - impl crate::IntoPy for Complex<$float> { - fn into_py(self, py: Python<'_>) -> PyObject { - unsafe { - let raw_obj = - ffi::PyComplex_FromDoubles(self.re as c_double, self.im as c_double); - PyObject::from_owned_ptr(py, raw_obj) - } - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] - impl<'py> crate::conversion::IntoPyObject<'py> for Complex<$float> { - type Target = PyComplex; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - unsafe { - Ok( - ffi::PyComplex_FromDoubles(self.re as c_double, self.im as c_double) - .assume_owned(py) - .downcast_into_unchecked(), - ) - } - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] - impl<'py> crate::conversion::IntoPyObject<'py> for &Complex<$float> { - type Target = PyComplex; - type Output = Bound<'py, Self::Target>; - type Error = std::convert::Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - } - - #[cfg_attr(docsrs, doc(cfg(feature = "num-complex")))] - impl FromPyObject<'_> for Complex<$float> { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult> { - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - unsafe { - let val = ffi::PyComplex_AsCComplex(obj.as_ptr()); - if val.real == -1.0 { - if let Some(err) = PyErr::take(obj.py()) { - return Err(err); - } - } - Ok(Complex::new(val.real as $float, val.imag as $float)) - } - - #[cfg(any(Py_LIMITED_API, PyPy))] - unsafe { - let complex; - let obj = if obj.is_instance_of::() { - obj - } else if let Some(method) = - obj.lookup_special(crate::intern!(obj.py(), "__complex__"))? - { - complex = method.call0()?; - &complex - } else { - // `obj` might still implement `__float__` or `__index__`, which will be - // handled by `PyComplex_{Real,Imag}AsDouble`, including propagating any - // errors if those methods don't exist / raise exceptions. - obj - }; - let ptr = obj.as_ptr(); - let real = ffi::PyComplex_RealAsDouble(ptr); - if real == -1.0 { - if let Some(err) = PyErr::take(obj.py()) { - return Err(err); - } - } - let imag = ffi::PyComplex_ImagAsDouble(ptr); - Ok(Complex::new(real as $float, imag as $float)) - } - } - } - }; -} -complex_conversion!(f32); -complex_conversion!(f64); - -#[cfg(test)] -mod tests { - use super::*; - use crate::tests::common::generate_unique_module_name; - use crate::types::{complex::PyComplexMethods, PyModule}; - use crate::IntoPyObject; - use pyo3_ffi::c_str; - - #[test] - fn from_complex() { - Python::with_gil(|py| { - let complex = Complex::new(3.0, 1.2); - let py_c = PyComplex::from_complex_bound(py, complex); - assert_eq!(py_c.real(), 3.0); - assert_eq!(py_c.imag(), 1.2); - }); - } - #[test] - fn to_from_complex() { - Python::with_gil(|py| { - let val = Complex::new(3.0f64, 1.2); - let obj = val.into_pyobject(py).unwrap(); - assert_eq!(obj.extract::>().unwrap(), val); - }); - } - #[test] - fn from_complex_err() { - Python::with_gil(|py| { - let obj = vec![1i32].into_pyobject(py).unwrap(); - assert!(obj.extract::>().is_err()); - }); - } - #[test] - fn from_python_magic() { - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class A: - def __complex__(self): return 3.0+1.2j -class B: - def __float__(self): return 3.0 -class C: - def __index__(self): return 3 - "# - ), - c_str!("test.py"), - &generate_unique_module_name("test"), - ) - .unwrap(); - let from_complex = module.getattr("A").unwrap().call0().unwrap(); - assert_eq!( - from_complex.extract::>().unwrap(), - Complex::new(3.0, 1.2) - ); - let from_float = module.getattr("B").unwrap().call0().unwrap(); - assert_eq!( - from_float.extract::>().unwrap(), - Complex::new(3.0, 0.0) - ); - // Before Python 3.8, `__index__` wasn't tried by `float`/`complex`. - #[cfg(Py_3_8)] - { - let from_index = module.getattr("C").unwrap().call0().unwrap(); - assert_eq!( - from_index.extract::>().unwrap(), - Complex::new(3.0, 0.0) - ); - } - }) - } - #[test] - fn from_python_inherited_magic() { - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class First: pass -class ComplexMixin: - def __complex__(self): return 3.0+1.2j -class FloatMixin: - def __float__(self): return 3.0 -class IndexMixin: - def __index__(self): return 3 -class A(First, ComplexMixin): pass -class B(First, FloatMixin): pass -class C(First, IndexMixin): pass - "# - ), - c_str!("test.py"), - &generate_unique_module_name("test"), - ) - .unwrap(); - let from_complex = module.getattr("A").unwrap().call0().unwrap(); - assert_eq!( - from_complex.extract::>().unwrap(), - Complex::new(3.0, 1.2) - ); - let from_float = module.getattr("B").unwrap().call0().unwrap(); - assert_eq!( - from_float.extract::>().unwrap(), - Complex::new(3.0, 0.0) - ); - #[cfg(Py_3_8)] - { - let from_index = module.getattr("C").unwrap().call0().unwrap(); - assert_eq!( - from_index.extract::>().unwrap(), - Complex::new(3.0, 0.0) - ); - } - }) - } - #[test] - fn from_python_noncallable_descriptor_magic() { - // Functions and lambdas implement the descriptor protocol in a way that makes - // `type(inst).attr(inst)` equivalent to `inst.attr()` for methods, but this isn't the only - // way the descriptor protocol might be implemented. - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class A: - @property - def __complex__(self): - return lambda: 3.0+1.2j - "# - ), - c_str!("test.py"), - &generate_unique_module_name("test"), - ) - .unwrap(); - let obj = module.getattr("A").unwrap().call0().unwrap(); - assert_eq!( - obj.extract::>().unwrap(), - Complex::new(3.0, 1.2) - ); - }) - } - #[test] - fn from_python_nondescriptor_magic() { - // Magic methods don't need to implement the descriptor protocol, if they're callable. - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class MyComplex: - def __call__(self): return 3.0+1.2j -class A: - __complex__ = MyComplex() - "# - ), - c_str!("test.py"), - &generate_unique_module_name("test"), - ) - .unwrap(); - let obj = module.getattr("A").unwrap().call0().unwrap(); - assert_eq!( - obj.extract::>().unwrap(), - Complex::new(3.0, 1.2) - ); - }) - } -} diff --git a/vendor/pyo3/src/conversions/num_rational.rs b/vendor/pyo3/src/conversions/num_rational.rs deleted file mode 100644 index 6c48f67fcf..0000000000 --- a/vendor/pyo3/src/conversions/num_rational.rs +++ /dev/null @@ -1,283 +0,0 @@ -#![cfg(feature = "num-rational")] -//! Conversions to and from [num-rational](https://docs.rs/num-rational) types. -//! -//! This is useful for converting between Python's [fractions.Fraction](https://docs.python.org/3/library/fractions.html) into and from a native Rust -//! type. -//! -//! -//! To use this feature, add to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"num-rational\"] }")] -//! num-rational = "0.4.1" -//! ``` -//! -//! # Example -//! -//! Rust code to create a function that adds five to a fraction: -//! -//! ```rust -//! use num_rational::Ratio; -//! use pyo3::prelude::*; -//! -//! #[pyfunction] -//! fn add_five_to_fraction(fraction: Ratio) -> Ratio { -//! fraction + Ratio::new(5, 1) -//! } -//! -//! #[pymodule] -//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(add_five_to_fraction, m)?)?; -//! Ok(()) -//! } -//! ``` -//! -//! Python code that validates the functionality: -//! ```python -//! from my_module import add_five_to_fraction -//! from fractions import Fraction -//! -//! fraction = Fraction(2,1) -//! fraction_plus_five = add_five_to_fraction(f) -//! assert fraction + 5 == fraction_plus_five -//! ``` - -use crate::conversion::IntoPyObject; -use crate::ffi; -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -use crate::types::PyType; -use crate::{Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[cfg(feature = "num-bigint")] -use num_bigint::BigInt; -use num_rational::Ratio; - -static FRACTION_CLS: GILOnceCell> = GILOnceCell::new(); - -fn get_fraction_cls(py: Python<'_>) -> PyResult<&Bound<'_, PyType>> { - FRACTION_CLS.import(py, "fractions", "Fraction") -} - -macro_rules! rational_conversion { - ($int: ty) => { - impl<'py> FromPyObject<'py> for Ratio<$int> { - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - let py = obj.py(); - let py_numerator_obj = obj.getattr(crate::intern!(py, "numerator"))?; - let py_denominator_obj = obj.getattr(crate::intern!(py, "denominator"))?; - let numerator_owned = unsafe { - Bound::from_owned_ptr_or_err(py, ffi::PyNumber_Long(py_numerator_obj.as_ptr()))? - }; - let denominator_owned = unsafe { - Bound::from_owned_ptr_or_err( - py, - ffi::PyNumber_Long(py_denominator_obj.as_ptr()), - )? - }; - let rs_numerator: $int = numerator_owned.extract()?; - let rs_denominator: $int = denominator_owned.extract()?; - Ok(Ratio::new(rs_numerator, rs_denominator)) - } - } - - #[allow(deprecated)] - impl ToPyObject for Ratio<$int> { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - #[allow(deprecated)] - impl IntoPy for Ratio<$int> { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for Ratio<$int> { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (&self).into_pyobject(py) - } - } - - impl<'py> IntoPyObject<'py> for &Ratio<$int> { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - get_fraction_cls(py)?.call1((self.numer().clone(), self.denom().clone())) - } - } - }; -} -rational_conversion!(i8); -rational_conversion!(i16); -rational_conversion!(i32); -rational_conversion!(isize); -rational_conversion!(i64); -#[cfg(feature = "num-bigint")] -rational_conversion!(BigInt); -#[cfg(test)] -mod tests { - use super::*; - use crate::types::dict::PyDictMethods; - use crate::types::PyDict; - - #[cfg(not(target_arch = "wasm32"))] - use proptest::prelude::*; - #[test] - fn test_negative_fraction() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import fractions\npy_frac = fractions.Fraction(-0.125)"), - None, - Some(&locals), - ) - .unwrap(); - let py_frac = locals.get_item("py_frac").unwrap().unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - let rs_frac = Ratio::new(-1, 8); - assert_eq!(roundtripped, rs_frac); - }) - } - #[test] - fn test_obj_with_incorrect_atts() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("not_fraction = \"contains_incorrect_atts\""), - None, - Some(&locals), - ) - .unwrap(); - let py_frac = locals.get_item("not_fraction").unwrap().unwrap(); - assert!(py_frac.extract::>().is_err()); - }) - } - - #[test] - fn test_fraction_with_fraction_type() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!( - "import fractions\npy_frac = fractions.Fraction(fractions.Fraction(10))" - ), - None, - Some(&locals), - ) - .unwrap(); - let py_frac = locals.get_item("py_frac").unwrap().unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - let rs_frac = Ratio::new(10, 1); - assert_eq!(roundtripped, rs_frac); - }) - } - - #[test] - fn test_fraction_with_decimal() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import fractions\n\nfrom decimal import Decimal\npy_frac = fractions.Fraction(Decimal(\"1.1\"))"), - None, - Some(&locals), - ) - .unwrap(); - let py_frac = locals.get_item("py_frac").unwrap().unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - let rs_frac = Ratio::new(11, 10); - assert_eq!(roundtripped, rs_frac); - }) - } - - #[test] - fn test_fraction_with_num_den() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import fractions\npy_frac = fractions.Fraction(10,5)"), - None, - Some(&locals), - ) - .unwrap(); - let py_frac = locals.get_item("py_frac").unwrap().unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - let rs_frac = Ratio::new(10, 5); - assert_eq!(roundtripped, rs_frac); - }) - } - - #[cfg(target_arch = "wasm32")] - #[test] - fn test_int_roundtrip() { - Python::with_gil(|py| { - let rs_frac = Ratio::new(1i32, 2); - let py_frac = rs_frac.into_pyobject(py).unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - assert_eq!(rs_frac, roundtripped); - // float conversion - }) - } - - #[cfg(target_arch = "wasm32")] - #[test] - fn test_big_int_roundtrip() { - Python::with_gil(|py| { - let rs_frac = Ratio::from_float(5.5).unwrap(); - let py_frac = rs_frac.clone().into_pyobject(py).unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - assert_eq!(rs_frac, roundtripped); - }) - } - - #[cfg(not(target_arch = "wasm32"))] - proptest! { - #[test] - fn test_int_roundtrip(num in any::(), den in any::()) { - Python::with_gil(|py| { - let rs_frac = Ratio::new(num, den); - let py_frac = rs_frac.into_pyobject(py).unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - assert_eq!(rs_frac, roundtripped); - }) - } - - #[test] - #[cfg(feature = "num-bigint")] - fn test_big_int_roundtrip(num in any::()) { - Python::with_gil(|py| { - let rs_frac = Ratio::from_float(num).unwrap(); - let py_frac = rs_frac.clone().into_pyobject(py).unwrap(); - let roundtripped: Ratio = py_frac.extract().unwrap(); - assert_eq!(roundtripped, rs_frac); - }) - } - - } - - #[test] - fn test_infinity() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - let py_bound = py.run( - ffi::c_str!("import fractions\npy_frac = fractions.Fraction(\"Infinity\")"), - None, - Some(&locals), - ); - assert!(py_bound.is_err()); - }) - } -} diff --git a/vendor/pyo3/src/conversions/rust_decimal.rs b/vendor/pyo3/src/conversions/rust_decimal.rs deleted file mode 100644 index 7c6eda21a6..0000000000 --- a/vendor/pyo3/src/conversions/rust_decimal.rs +++ /dev/null @@ -1,257 +0,0 @@ -#![cfg(feature = "rust_decimal")] -//! Conversions to and from [rust_decimal](https://docs.rs/rust_decimal)'s [`Decimal`] type. -//! -//! This is useful for converting Python's decimal.Decimal into and from a native Rust type. -//! -//! # Setup -//! -//! To use this feature, add to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"rust_decimal\"] }")] -//! rust_decimal = "1.0" -//! ``` -//! -//! Note that you must use a compatible version of rust_decimal and PyO3. -//! The required rust_decimal version may vary based on the version of PyO3. -//! -//! # Example -//! -//! Rust code to create a function that adds one to a Decimal -//! -//! ```rust -//! use rust_decimal::Decimal; -//! use pyo3::prelude::*; -//! -//! #[pyfunction] -//! fn add_one(d: Decimal) -> Decimal { -//! d + Decimal::ONE -//! } -//! -//! #[pymodule] -//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(add_one, m)?)?; -//! Ok(()) -//! } -//! ``` -//! -//! Python code that validates the functionality -//! -//! -//! ```python -//! from my_module import add_one -//! from decimal import Decimal -//! -//! d = Decimal("2") -//! value = add_one(d) -//! -//! assert d + 1 == value -//! ``` - -use crate::conversion::IntoPyObject; -use crate::exceptions::PyValueError; -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -use crate::types::string::PyStringMethods; -use crate::types::PyType; -use crate::{Bound, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use rust_decimal::Decimal; -use std::str::FromStr; - -impl FromPyObject<'_> for Decimal { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - // use the string representation to not be lossy - if let Ok(val) = obj.extract() { - Ok(Decimal::new(val, 0)) - } else { - let py_str = &obj.str()?; - let rs_str = &py_str.to_cow()?; - Decimal::from_str(rs_str).or_else(|_| { - Decimal::from_scientific(rs_str).map_err(|e| PyValueError::new_err(e.to_string())) - }) - } - } -} - -static DECIMAL_CLS: GILOnceCell> = GILOnceCell::new(); - -fn get_decimal_cls(py: Python<'_>) -> PyResult<&Bound<'_, PyType>> { - DECIMAL_CLS.import(py, "decimal", "Decimal") -} - -#[allow(deprecated)] -impl ToPyObject for Decimal { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Decimal { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Decimal { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dec_cls = get_decimal_cls(py)?; - // now call the constructor with the Rust Decimal string-ified - // to not be lossy - dec_cls.call1((self.to_string(),)) - } -} - -impl<'py> IntoPyObject<'py> for &Decimal { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -#[cfg(test)] -mod test_rust_decimal { - use super::*; - use crate::types::dict::PyDictMethods; - use crate::types::PyDict; - use std::ffi::CString; - - use crate::ffi; - #[cfg(not(target_arch = "wasm32"))] - use proptest::prelude::*; - - macro_rules! convert_constants { - ($name:ident, $rs:expr, $py:literal) => { - #[test] - fn $name() { - Python::with_gil(|py| { - let rs_orig = $rs; - let rs_dec = rs_orig.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("rs_dec", &rs_dec).unwrap(); - // Checks if Rust Decimal -> Python Decimal conversion is correct - py.run( - &CString::new(format!( - "import decimal\npy_dec = decimal.Decimal({})\nassert py_dec == rs_dec", - $py - )) - .unwrap(), - None, - Some(&locals), - ) - .unwrap(); - // Checks if Python Decimal -> Rust Decimal conversion is correct - let py_dec = locals.get_item("py_dec").unwrap().unwrap(); - let py_result: Decimal = py_dec.extract().unwrap(); - assert_eq!(rs_orig, py_result); - }) - } - }; - } - - convert_constants!(convert_zero, Decimal::ZERO, "0"); - convert_constants!(convert_one, Decimal::ONE, "1"); - convert_constants!(convert_neg_one, Decimal::NEGATIVE_ONE, "-1"); - convert_constants!(convert_two, Decimal::TWO, "2"); - convert_constants!(convert_ten, Decimal::TEN, "10"); - convert_constants!(convert_one_hundred, Decimal::ONE_HUNDRED, "100"); - convert_constants!(convert_one_thousand, Decimal::ONE_THOUSAND, "1000"); - - #[cfg(not(target_arch = "wasm32"))] - proptest! { - #[test] - fn test_roundtrip( - lo in any::(), - mid in any::(), - high in any::(), - negative in any::(), - scale in 0..28u32 - ) { - let num = Decimal::from_parts(lo, mid, high, negative, scale); - Python::with_gil(|py| { - let rs_dec = num.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("rs_dec", &rs_dec).unwrap(); - py.run( - &CString::new(format!( - "import decimal\npy_dec = decimal.Decimal(\"{}\")\nassert py_dec == rs_dec", - num)).unwrap(), - None, Some(&locals)).unwrap(); - let roundtripped: Decimal = rs_dec.extract().unwrap(); - assert_eq!(num, roundtripped); - }) - } - - #[test] - fn test_integers(num in any::()) { - Python::with_gil(|py| { - let py_num = num.into_pyobject(py).unwrap(); - let roundtripped: Decimal = py_num.extract().unwrap(); - let rs_dec = Decimal::new(num, 0); - assert_eq!(rs_dec, roundtripped); - }) - } - } - - #[test] - fn test_nan() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import decimal\npy_dec = decimal.Decimal(\"NaN\")"), - None, - Some(&locals), - ) - .unwrap(); - let py_dec = locals.get_item("py_dec").unwrap().unwrap(); - let roundtripped: Result = py_dec.extract(); - assert!(roundtripped.is_err()); - }) - } - - #[test] - fn test_scientific_notation() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import decimal\npy_dec = decimal.Decimal(\"1e3\")"), - None, - Some(&locals), - ) - .unwrap(); - let py_dec = locals.get_item("py_dec").unwrap().unwrap(); - let roundtripped: Decimal = py_dec.extract().unwrap(); - let rs_dec = Decimal::from_scientific("1e3").unwrap(); - assert_eq!(rs_dec, roundtripped); - }) - } - - #[test] - fn test_infinity() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - py.run( - ffi::c_str!("import decimal\npy_dec = decimal.Decimal(\"Infinity\")"), - None, - Some(&locals), - ) - .unwrap(); - let py_dec = locals.get_item("py_dec").unwrap().unwrap(); - let roundtripped: Result = py_dec.extract(); - assert!(roundtripped.is_err()); - }) - } -} diff --git a/vendor/pyo3/src/conversions/serde.rs b/vendor/pyo3/src/conversions/serde.rs deleted file mode 100644 index a769bd7027..0000000000 --- a/vendor/pyo3/src/conversions/serde.rs +++ /dev/null @@ -1,48 +0,0 @@ -#![cfg(feature = "serde")] - -//! Enables (de)serialization of [`Py`]`` objects via [serde](https://docs.rs/serde). -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"serde\"] }")] -//! serde = "1.0" -//! ``` - -use crate::{Py, PyAny, PyClass, Python}; -use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer}; - -impl Serialize for Py -where - T: Serialize + PyClass, -{ - fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - Python::with_gil(|py| { - self.try_borrow(py) - .map_err(|e| ser::Error::custom(e.to_string()))? - .serialize(serializer) - }) - } -} - -impl<'de, T> Deserialize<'de> for Py -where - T: PyClass + Deserialize<'de>, -{ - fn deserialize(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - let deserialized = T::deserialize(deserializer)?; - - Python::with_gil(|py| { - Py::new(py, deserialized).map_err(|e| de::Error::custom(e.to_string())) - }) - } -} diff --git a/vendor/pyo3/src/conversions/smallvec.rs b/vendor/pyo3/src/conversions/smallvec.rs deleted file mode 100644 index 99244d8141..0000000000 --- a/vendor/pyo3/src/conversions/smallvec.rs +++ /dev/null @@ -1,201 +0,0 @@ -#![cfg(feature = "smallvec")] - -//! Conversions to and from [smallvec](https://docs.rs/smallvec/). -//! -//! # Setup -//! -//! To use this feature, add this to your **`Cargo.toml`**: -//! -//! ```toml -//! [dependencies] -//! # change * to the latest versions -//! smallvec = "*" -#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"smallvec\"] }")] -//! ``` -//! -//! Note that you must use compatible versions of smallvec and PyO3. -//! The required smallvec version may vary based on the version of PyO3. -use crate::conversion::IntoPyObject; -use crate::exceptions::PyTypeError; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::types::any::PyAnyMethods; -use crate::types::list::new_from_iter; -use crate::types::{PySequence, PyString}; -use crate::PyErr; -use crate::{err::DowncastError, ffi, Bound, FromPyObject, PyAny, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use smallvec::{Array, SmallVec}; - -#[allow(deprecated)] -impl ToPyObject for SmallVec -where - A: Array, - A::Item: ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - self.as_slice().to_object(py) - } -} - -#[allow(deprecated)] -impl IntoPy for SmallVec -where - A: Array, - A::Item: IntoPy, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let mut iter = self.into_iter().map(|e| e.into_py(py)); - let list = new_from_iter(py, &mut iter); - list.into() - } -} - -impl<'py, A> IntoPyObject<'py> for SmallVec -where - A: Array, - A::Item: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - /// Turns [`SmallVec`] into [`PyBytes`], all other `T`s will be turned into a [`PyList`] - /// - /// [`PyBytes`]: crate::types::PyBytes - /// [`PyList`]: crate::types::PyList - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - ::owned_sequence_into_pyobject(self, py, crate::conversion::private::Token) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::list_of(A::Item::type_output()) - } -} - -impl<'a, 'py, A> IntoPyObject<'py> for &'a SmallVec -where - A: Array, - &'a A::Item: IntoPyObject<'py>, - A::Item: 'a, // MSRV -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_slice().into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::list_of(<&A::Item>::type_output()) - } -} - -impl<'py, A> FromPyObject<'py> for SmallVec -where - A: Array, - A::Item: FromPyObject<'py>, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - if obj.is_instance_of::() { - return Err(PyTypeError::new_err("Can't extract `str` to `SmallVec`")); - } - extract_sequence(obj) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::sequence_of(A::Item::type_input()) - } -} - -fn extract_sequence<'py, A>(obj: &Bound<'py, PyAny>) -> PyResult> -where - A: Array, - A::Item: FromPyObject<'py>, -{ - // Types that pass `PySequence_Check` usually implement enough of the sequence protocol - // to support this function and if not, we will only fail extraction safely. - let seq = unsafe { - if ffi::PySequence_Check(obj.as_ptr()) != 0 { - obj.downcast_unchecked::() - } else { - return Err(DowncastError::new(obj, "Sequence").into()); - } - }; - - let mut sv = SmallVec::with_capacity(seq.len().unwrap_or(0)); - for item in seq.try_iter()? { - sv.push(item?.extract::()?); - } - Ok(sv) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::{PyBytes, PyBytesMethods, PyDict, PyList}; - - #[test] - #[allow(deprecated)] - fn test_smallvec_into_py() { - Python::with_gil(|py| { - let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); - let hso: PyObject = sv.clone().into_py(py); - let l = PyList::new(py, [1, 2, 3, 4, 5]).unwrap(); - assert!(l.eq(hso).unwrap()); - }); - } - - #[test] - fn test_smallvec_from_py_object() { - Python::with_gil(|py| { - let l = PyList::new(py, [1, 2, 3, 4, 5]).unwrap(); - let sv: SmallVec<[u64; 8]> = l.extract().unwrap(); - assert_eq!(sv.as_slice(), [1, 2, 3, 4, 5]); - }); - } - - #[test] - fn test_smallvec_from_py_object_fails() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - let sv: PyResult> = dict.extract(); - assert_eq!( - sv.unwrap_err().to_string(), - "TypeError: 'dict' object cannot be converted to 'Sequence'" - ); - }); - } - - #[test] - fn test_smallvec_into_pyobject() { - Python::with_gil(|py| { - let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); - let hso = sv.into_pyobject(py).unwrap(); - let l = PyList::new(py, [1, 2, 3, 4, 5]).unwrap(); - assert!(l.eq(hso).unwrap()); - }); - } - - #[test] - fn test_smallvec_intopyobject_impl() { - Python::with_gil(|py| { - let bytes: SmallVec<[u8; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); - let obj = bytes.clone().into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &*bytes); - - let nums: SmallVec<[u16; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); - let obj = nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/array.rs b/vendor/pyo3/src/conversions/std/array.rs deleted file mode 100644 index 1c33a61027..0000000000 --- a/vendor/pyo3/src/conversions/std/array.rs +++ /dev/null @@ -1,318 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::instance::Bound; -use crate::types::any::PyAnyMethods; -use crate::types::PySequence; -use crate::{err::DowncastError, ffi, FromPyObject, Py, PyAny, PyObject, PyResult, Python}; -use crate::{exceptions, PyErr}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[allow(deprecated)] -impl IntoPy for [T; N] -where - T: IntoPy, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - unsafe { - let len = N as ffi::Py_ssize_t; - - let ptr = ffi::PyList_New(len); - - // We create the `Py` pointer here for two reasons: - // - panics if the ptr is null - // - its Drop cleans up the list if user code panics. - let list: Py = Py::from_owned_ptr(py, ptr); - - for (i, obj) in (0..len).zip(self) { - let obj = obj.into_py(py).into_ptr(); - - #[cfg(not(Py_LIMITED_API))] - ffi::PyList_SET_ITEM(ptr, i, obj); - #[cfg(Py_LIMITED_API)] - ffi::PyList_SetItem(ptr, i, obj); - } - - list - } - } -} - -impl<'py, T, const N: usize> IntoPyObject<'py> for [T; N] -where - T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - /// Turns [`[u8; N]`](std::array) into [`PyBytes`], all other `T`s will be turned into a [`PyList`] - /// - /// [`PyBytes`]: crate::types::PyBytes - /// [`PyList`]: crate::types::PyList - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - T::owned_sequence_into_pyobject(self, py, crate::conversion::private::Token) - } -} - -impl<'a, 'py, T, const N: usize> IntoPyObject<'py> for &'a [T; N] -where - &'a T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_slice().into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for [T; N] -where - T: ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - self.as_ref().to_object(py) - } -} - -impl<'py, T, const N: usize> FromPyObject<'py> for [T; N] -where - T: FromPyObject<'py>, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - create_array_from_obj(obj) - } -} - -fn create_array_from_obj<'py, T, const N: usize>(obj: &Bound<'py, PyAny>) -> PyResult<[T; N]> -where - T: FromPyObject<'py>, -{ - // Types that pass `PySequence_Check` usually implement enough of the sequence protocol - // to support this function and if not, we will only fail extraction safely. - let seq = unsafe { - if ffi::PySequence_Check(obj.as_ptr()) != 0 { - obj.downcast_unchecked::() - } else { - return Err(DowncastError::new(obj, "Sequence").into()); - } - }; - let seq_len = seq.len()?; - if seq_len != N { - return Err(invalid_sequence_length(N, seq_len)); - } - array_try_from_fn(|idx| seq.get_item(idx).and_then(|any| any.extract())) -} - -// TODO use std::array::try_from_fn, if that stabilises: -// (https://github.com/rust-lang/rust/issues/89379) -fn array_try_from_fn(mut cb: F) -> Result<[T; N], E> -where - F: FnMut(usize) -> Result, -{ - // Helper to safely create arrays since the standard library doesn't - // provide one yet. Shouldn't be necessary in the future. - struct ArrayGuard { - dst: *mut T, - initialized: usize, - } - - impl Drop for ArrayGuard { - fn drop(&mut self) { - debug_assert!(self.initialized <= N); - let initialized_part = core::ptr::slice_from_raw_parts_mut(self.dst, self.initialized); - unsafe { - core::ptr::drop_in_place(initialized_part); - } - } - } - - // [MaybeUninit; N] would be "nicer" but is actually difficult to create - there are nightly - // APIs which would make this easier. - let mut array: core::mem::MaybeUninit<[T; N]> = core::mem::MaybeUninit::uninit(); - let mut guard: ArrayGuard = ArrayGuard { - dst: array.as_mut_ptr() as _, - initialized: 0, - }; - unsafe { - let mut value_ptr = array.as_mut_ptr() as *mut T; - for i in 0..N { - core::ptr::write(value_ptr, cb(i)?); - value_ptr = value_ptr.offset(1); - guard.initialized += 1; - } - core::mem::forget(guard); - Ok(array.assume_init()) - } -} - -fn invalid_sequence_length(expected: usize, actual: usize) -> PyErr { - exceptions::PyValueError::new_err(format!( - "expected a sequence of length {} (got {})", - expected, actual - )) -} - -#[cfg(test)] -mod tests { - use std::{ - panic, - sync::atomic::{AtomicUsize, Ordering}, - }; - - use crate::{ - conversion::IntoPyObject, - ffi, - types::{any::PyAnyMethods, PyBytes, PyBytesMethods}, - }; - use crate::{types::PyList, PyResult, Python}; - - #[test] - fn array_try_from_fn() { - static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0); - struct CountDrop; - impl Drop for CountDrop { - fn drop(&mut self) { - DROP_COUNTER.fetch_add(1, Ordering::SeqCst); - } - } - let _ = catch_unwind_silent(move || { - let _: Result<[CountDrop; 4], ()> = super::array_try_from_fn(|idx| { - #[allow(clippy::manual_assert)] - if idx == 2 { - panic!("peek a boo"); - } - Ok(CountDrop) - }); - }); - assert_eq!(DROP_COUNTER.load(Ordering::SeqCst), 2); - } - - #[test] - fn test_extract_bytearray_to_array() { - Python::with_gil(|py| { - let v: [u8; 33] = py - .eval( - ffi::c_str!("bytearray(b'abcabcabcabcabcabcabcabcabcabcabc')"), - None, - None, - ) - .unwrap() - .extract() - .unwrap(); - assert!(&v == b"abcabcabcabcabcabcabcabcabcabcabc"); - }) - } - - #[test] - fn test_extract_small_bytearray_to_array() { - Python::with_gil(|py| { - let v: [u8; 3] = py - .eval(ffi::c_str!("bytearray(b'abc')"), None, None) - .unwrap() - .extract() - .unwrap(); - assert!(&v == b"abc"); - }); - } - #[test] - fn test_into_pyobject_array_conversion() { - Python::with_gil(|py| { - let array: [f32; 4] = [0.0, -16.0, 16.0, 42.0]; - let pyobject = array.into_pyobject(py).unwrap(); - let pylist = pyobject.downcast::().unwrap(); - assert_eq!(pylist.get_item(0).unwrap().extract::().unwrap(), 0.0); - assert_eq!(pylist.get_item(1).unwrap().extract::().unwrap(), -16.0); - assert_eq!(pylist.get_item(2).unwrap().extract::().unwrap(), 16.0); - assert_eq!(pylist.get_item(3).unwrap().extract::().unwrap(), 42.0); - }); - } - - #[test] - fn test_extract_invalid_sequence_length() { - Python::with_gil(|py| { - let v: PyResult<[u8; 3]> = py - .eval(ffi::c_str!("bytearray(b'abcdefg')"), None, None) - .unwrap() - .extract(); - assert_eq!( - v.unwrap_err().to_string(), - "ValueError: expected a sequence of length 3 (got 7)" - ); - }) - } - - #[test] - fn test_intopyobject_array_conversion() { - Python::with_gil(|py| { - let array: [f32; 4] = [0.0, -16.0, 16.0, 42.0]; - let pylist = array - .into_pyobject(py) - .unwrap() - .downcast_into::() - .unwrap(); - - assert_eq!(pylist.get_item(0).unwrap().extract::().unwrap(), 0.0); - assert_eq!(pylist.get_item(1).unwrap().extract::().unwrap(), -16.0); - assert_eq!(pylist.get_item(2).unwrap().extract::().unwrap(), 16.0); - assert_eq!(pylist.get_item(3).unwrap().extract::().unwrap(), 42.0); - }); - } - - #[test] - fn test_array_intopyobject_impl() { - Python::with_gil(|py| { - let bytes: [u8; 6] = *b"foobar"; - let obj = bytes.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &bytes); - - let nums: [u16; 4] = [0, 1, 2, 3]; - let obj = nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } - - #[test] - fn test_extract_non_iterable_to_array() { - Python::with_gil(|py| { - let v = py.eval(ffi::c_str!("42"), None, None).unwrap(); - v.extract::().unwrap(); - v.extract::<[i32; 1]>().unwrap_err(); - }); - } - - #[cfg(feature = "macros")] - #[test] - fn test_pyclass_intopy_array_conversion() { - #[crate::pyclass(crate = "crate")] - struct Foo; - - Python::with_gil(|py| { - let array: [Foo; 8] = [Foo, Foo, Foo, Foo, Foo, Foo, Foo, Foo]; - let list = array - .into_pyobject(py) - .unwrap() - .downcast_into::() - .unwrap(); - let _bound = list.get_item(4).unwrap().downcast::().unwrap(); - }); - } - - // https://stackoverflow.com/a/59211505 - fn catch_unwind_silent(f: F) -> std::thread::Result - where - F: FnOnce() -> R + panic::UnwindSafe, - { - let prev_hook = panic::take_hook(); - panic::set_hook(Box::new(|_| {})); - let result = panic::catch_unwind(f); - panic::set_hook(prev_hook); - result - } -} diff --git a/vendor/pyo3/src/conversions/std/cell.rs b/vendor/pyo3/src/conversions/std/cell.rs deleted file mode 100644 index 70da688b70..0000000000 --- a/vendor/pyo3/src/conversions/std/cell.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::cell::Cell; - -use crate::{ - conversion::IntoPyObject, types::any::PyAnyMethods, Bound, FromPyObject, PyAny, PyObject, - PyResult, Python, -}; - -#[allow(deprecated)] -impl crate::ToPyObject for Cell { - fn to_object(&self, py: Python<'_>) -> PyObject { - self.get().to_object(py) - } -} - -#[allow(deprecated)] -impl> crate::IntoPy for Cell { - fn into_py(self, py: Python<'_>) -> PyObject { - self.get().into_py(py) - } -} - -impl<'py, T: Copy + IntoPyObject<'py>> IntoPyObject<'py> for Cell { - type Target = T::Target; - type Output = T::Output; - type Error = T::Error; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.get().into_pyobject(py) - } -} - -impl<'py, T: Copy + IntoPyObject<'py>> IntoPyObject<'py> for &Cell { - type Target = T::Target; - type Output = T::Output; - type Error = T::Error; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.get().into_pyobject(py) - } -} - -impl<'py, T: FromPyObject<'py>> FromPyObject<'py> for Cell { - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - ob.extract().map(Cell::new) - } -} diff --git a/vendor/pyo3/src/conversions/std/ipaddr.rs b/vendor/pyo3/src/conversions/std/ipaddr.rs deleted file mode 100755 index 7ba8da8774..0000000000 --- a/vendor/pyo3/src/conversions/std/ipaddr.rs +++ /dev/null @@ -1,182 +0,0 @@ -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; - -use crate::conversion::IntoPyObject; -use crate::exceptions::PyValueError; -use crate::instance::Bound; -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -use crate::types::string::PyStringMethods; -use crate::types::PyType; -use crate::{intern, FromPyObject, Py, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -impl FromPyObject<'_> for IpAddr { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - match obj.getattr(intern!(obj.py(), "packed")) { - Ok(packed) => { - if let Ok(packed) = packed.extract::<[u8; 4]>() { - Ok(IpAddr::V4(Ipv4Addr::from(packed))) - } else if let Ok(packed) = packed.extract::<[u8; 16]>() { - Ok(IpAddr::V6(Ipv6Addr::from(packed))) - } else { - Err(PyValueError::new_err("invalid packed length")) - } - } - Err(_) => { - // We don't have a .packed attribute, so we try to construct an IP from str(). - obj.str()?.to_cow()?.parse().map_err(PyValueError::new_err) - } - } - } -} - -#[allow(deprecated)] -impl ToPyObject for Ipv4Addr { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Ipv4Addr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - static IPV4_ADDRESS: GILOnceCell> = GILOnceCell::new(); - IPV4_ADDRESS - .import(py, "ipaddress", "IPv4Address")? - .call1((u32::from_be_bytes(self.octets()),)) - } -} - -impl<'py> IntoPyObject<'py> for &Ipv4Addr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for Ipv6Addr { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Ipv6Addr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - static IPV6_ADDRESS: GILOnceCell> = GILOnceCell::new(); - IPV6_ADDRESS - .import(py, "ipaddress", "IPv6Address")? - .call1((u128::from_be_bytes(self.octets()),)) - } -} - -impl<'py> IntoPyObject<'py> for &Ipv6Addr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for IpAddr { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for IpAddr { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().unbind() - } -} - -impl<'py> IntoPyObject<'py> for IpAddr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - match self { - IpAddr::V4(ip) => ip.into_pyobject(py), - IpAddr::V6(ip) => ip.into_pyobject(py), - } - } -} - -impl<'py> IntoPyObject<'py> for &IpAddr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -#[cfg(test)] -mod test_ipaddr { - use std::str::FromStr; - - use crate::types::PyString; - - use super::*; - - #[test] - fn test_roundtrip() { - Python::with_gil(|py| { - fn roundtrip(py: Python<'_>, ip: &str) { - let ip = IpAddr::from_str(ip).unwrap(); - let py_cls = if ip.is_ipv4() { - "IPv4Address" - } else { - "IPv6Address" - }; - - let pyobj = ip.into_pyobject(py).unwrap(); - let repr = pyobj.repr().unwrap(); - let repr = repr.to_string_lossy(); - assert_eq!(repr, format!("{}('{}')", py_cls, ip)); - - let ip2: IpAddr = pyobj.extract().unwrap(); - assert_eq!(ip, ip2); - } - roundtrip(py, "127.0.0.1"); - roundtrip(py, "::1"); - roundtrip(py, "0.0.0.0"); - }); - } - - #[test] - fn test_from_pystring() { - Python::with_gil(|py| { - let py_str = PyString::new(py, "0:0:0:0:0:0:0:1"); - let ip: IpAddr = py_str.extract().unwrap(); - assert_eq!(ip, IpAddr::from_str("::1").unwrap()); - - let py_str = PyString::new(py, "invalid"); - assert!(py_str.extract::().is_err()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/map.rs b/vendor/pyo3/src/conversions/std/map.rs deleted file mode 100644 index 8a6ba57012..0000000000 --- a/vendor/pyo3/src/conversions/std/map.rs +++ /dev/null @@ -1,305 +0,0 @@ -use std::{cmp, collections, hash}; - -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - conversion::IntoPyObject, - instance::Bound, - types::{any::PyAnyMethods, dict::PyDictMethods, PyDict}, - FromPyObject, PyAny, PyErr, PyObject, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[allow(deprecated)] -impl ToPyObject for collections::HashMap -where - K: hash::Hash + cmp::Eq + ToPyObject, - V: ToPyObject, - H: hash::BuildHasher, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.to_object(py), v.to_object(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -#[allow(deprecated)] -impl ToPyObject for collections::BTreeMap -where - K: cmp::Eq + ToPyObject, - V: ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.to_object(py), v.to_object(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for collections::HashMap -where - K: hash::Hash + cmp::Eq + IntoPy, - V: IntoPy, - H: hash::BuildHasher, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.into_py(py), v.into_py(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -impl<'py, K, V, H> IntoPyObject<'py> for collections::HashMap -where - K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - V: IntoPyObject<'py>, - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::dict_of(K::type_output(), V::type_output()) - } -} - -impl<'a, 'py, K, V, H> IntoPyObject<'py> for &'a collections::HashMap -where - &'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash, - &'a V: IntoPyObject<'py>, - K: 'a, // MSRV - V: 'a, // MSRV - H: hash::BuildHasher, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::dict_of(<&K>::type_output(), <&V>::type_output()) - } -} - -#[allow(deprecated)] -impl IntoPy for collections::BTreeMap -where - K: cmp::Eq + IntoPy, - V: IntoPy, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k.into_py(py), v.into_py(py)).unwrap(); - } - dict.into_any().unbind() - } -} - -impl<'py, K, V> IntoPyObject<'py> for collections::BTreeMap -where - K: IntoPyObject<'py> + cmp::Eq, - V: IntoPyObject<'py>, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::dict_of(K::type_output(), V::type_output()) - } -} - -impl<'a, 'py, K, V> IntoPyObject<'py> for &'a collections::BTreeMap -where - &'a K: IntoPyObject<'py> + cmp::Eq, - &'a V: IntoPyObject<'py>, - K: 'a, - V: 'a, -{ - type Target = PyDict; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let dict = PyDict::new(py); - for (k, v) in self { - dict.set_item(k, v)?; - } - Ok(dict) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::dict_of(<&K>::type_output(), <&V>::type_output()) - } -} - -impl<'py, K, V, S> FromPyObject<'py> for collections::HashMap -where - K: FromPyObject<'py> + cmp::Eq + hash::Hash, - V: FromPyObject<'py>, - S: hash::BuildHasher + Default, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { - let dict = ob.downcast::()?; - let mut ret = collections::HashMap::with_capacity_and_hasher(dict.len(), S::default()); - for (k, v) in dict { - ret.insert(k.extract()?, v.extract()?); - } - Ok(ret) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::mapping_of(K::type_input(), V::type_input()) - } -} - -impl<'py, K, V> FromPyObject<'py> for collections::BTreeMap -where - K: FromPyObject<'py> + cmp::Ord, - V: FromPyObject<'py>, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { - let dict = ob.downcast::()?; - let mut ret = collections::BTreeMap::new(); - for (k, v) in dict { - ret.insert(k.extract()?, v.extract()?); - } - Ok(ret) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::mapping_of(K::type_input(), V::type_input()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use std::collections::{BTreeMap, HashMap}; - - #[test] - fn test_hashmap_to_python() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let py_map = (&map).into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - assert_eq!(map, py_map.extract().unwrap()); - }); - } - - #[test] - fn test_btreemap_to_python() { - Python::with_gil(|py| { - let mut map = BTreeMap::::new(); - map.insert(1, 1); - - let py_map = (&map).into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - assert_eq!(map, py_map.extract().unwrap()); - }); - } - - #[test] - fn test_hashmap_into_python() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let py_map = map.into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - }); - } - - #[test] - fn test_btreemap_into_py() { - Python::with_gil(|py| { - let mut map = BTreeMap::::new(); - map.insert(1, 1); - - let py_map = map.into_pyobject(py).unwrap(); - - assert!(py_map.len() == 1); - assert!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap() - == 1 - ); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/mod.rs b/vendor/pyo3/src/conversions/std/mod.rs deleted file mode 100644 index 305344b128..0000000000 --- a/vendor/pyo3/src/conversions/std/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -mod array; -mod cell; -mod ipaddr; -mod map; -mod num; -mod option; -mod osstr; -mod path; -mod set; -mod slice; -mod string; -mod time; -mod vec; diff --git a/vendor/pyo3/src/conversions/std/num.rs b/vendor/pyo3/src/conversions/std/num.rs deleted file mode 100644 index 0450c591ae..0000000000 --- a/vendor/pyo3/src/conversions/std/num.rs +++ /dev/null @@ -1,1109 +0,0 @@ -use crate::conversion::private::Reference; -use crate::conversion::IntoPyObject; -use crate::ffi_ptr_ext::FfiPtrExt; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::types::any::PyAnyMethods; -use crate::types::{PyBytes, PyInt}; -use crate::{exceptions, ffi, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::convert::Infallible; -use std::num::{ - NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, - NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, -}; -use std::os::raw::c_long; - -macro_rules! int_fits_larger_int { - ($rust_type:ty, $larger_type:ty) => { - #[allow(deprecated)] - impl ToPyObject for $rust_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - #[allow(deprecated)] - impl IntoPy for $rust_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for $rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - (self as $larger_type).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - <$larger_type>::type_output() - } - } - - impl<'py> IntoPyObject<'py> for &$rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - <$larger_type>::type_output() - } - } - - impl FromPyObject<'_> for $rust_type { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let val: $larger_type = obj.extract()?; - <$rust_type>::try_from(val) - .map_err(|e| exceptions::PyOverflowError::new_err(e.to_string())) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - <$larger_type>::type_input() - } - } - }; -} - -macro_rules! extract_int { - ($obj:ident, $error_val:expr, $pylong_as:expr) => { - extract_int!($obj, $error_val, $pylong_as, false) - }; - - ($obj:ident, $error_val:expr, $pylong_as:expr, $force_index_call: literal) => { - // In python 3.8+ `PyLong_AsLong` and friends takes care of calling `PyNumber_Index`, - // however 3.8 & 3.9 do lossy conversion of floats, hence we only use the - // simplest logic for 3.10+ where that was fixed - python/cpython#82180. - // `PyLong_AsUnsignedLongLong` does not call `PyNumber_Index`, hence the `force_index_call` argument - // See https://github.com/PyO3/pyo3/pull/3742 for detials - if cfg!(Py_3_10) && !$force_index_call { - err_if_invalid_value($obj.py(), $error_val, unsafe { $pylong_as($obj.as_ptr()) }) - } else if let Ok(long) = $obj.downcast::() { - // fast path - checking for subclass of `int` just checks a bit in the type $object - err_if_invalid_value($obj.py(), $error_val, unsafe { $pylong_as(long.as_ptr()) }) - } else { - unsafe { - let num = ffi::PyNumber_Index($obj.as_ptr()).assume_owned_or_err($obj.py())?; - err_if_invalid_value($obj.py(), $error_val, $pylong_as(num.as_ptr())) - } - } - }; -} - -macro_rules! int_convert_u64_or_i64 { - ($rust_type:ty, $pylong_from_ll_or_ull:expr, $pylong_as_ll_or_ull:expr, $force_index_call:literal) => { - #[allow(deprecated)] - impl ToPyObject for $rust_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - #[allow(deprecated)] - impl IntoPy for $rust_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - impl<'py> IntoPyObject<'py> for $rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - unsafe { - Ok($pylong_from_ll_or_ull(self) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - impl<'py> IntoPyObject<'py> for &$rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - impl FromPyObject<'_> for $rust_type { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<$rust_type> { - extract_int!(obj, !0, $pylong_as_ll_or_ull, $force_index_call) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } - } - }; -} - -macro_rules! int_fits_c_long { - ($rust_type:ty) => { - #[allow(deprecated)] - impl ToPyObject for $rust_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - #[allow(deprecated)] - impl IntoPy for $rust_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for $rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - unsafe { - Ok(ffi::PyLong_FromLong(self as c_long) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl<'py> IntoPyObject<'py> for &$rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl<'py> FromPyObject<'py> for $rust_type { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let val: c_long = extract_int!(obj, -1, ffi::PyLong_AsLong)?; - <$rust_type>::try_from(val) - .map_err(|e| exceptions::PyOverflowError::new_err(e.to_string())) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } - } - }; -} - -#[allow(deprecated)] -impl ToPyObject for u8 { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} -#[allow(deprecated)] -impl IntoPy for u8 { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} -impl<'py> IntoPyObject<'py> for u8 { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - unsafe { - Ok(ffi::PyLong_FromLong(self as c_long) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - - #[inline] - fn owned_sequence_into_pyobject( - iter: I, - py: Python<'py>, - _: crate::conversion::private::Token, - ) -> Result, PyErr> - where - I: AsRef<[u8]>, - { - Ok(PyBytes::new(py, iter.as_ref()).into_any()) - } -} - -impl<'py> IntoPyObject<'py> for &'_ u8 { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - u8::into_pyobject(*self, py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - - #[inline] - fn borrowed_sequence_into_pyobject( - iter: I, - py: Python<'py>, - _: crate::conversion::private::Token, - ) -> Result, PyErr> - where - // I: AsRef<[u8]>, but the compiler needs it expressed via the trait for some reason - I: AsRef<[::BaseType]>, - { - Ok(PyBytes::new(py, iter.as_ref()).into_any()) - } -} - -impl FromPyObject<'_> for u8 { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let val: c_long = extract_int!(obj, -1, ffi::PyLong_AsLong)?; - u8::try_from(val).map_err(|e| exceptions::PyOverflowError::new_err(e.to_string())) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -int_fits_c_long!(i8); -int_fits_c_long!(i16); -int_fits_c_long!(u16); -int_fits_c_long!(i32); - -// If c_long is 64-bits, we can use more types with int_fits_c_long!: -#[cfg(all(target_pointer_width = "64", not(target_os = "windows")))] -int_fits_c_long!(u32); -#[cfg(any(target_pointer_width = "32", target_os = "windows"))] -int_fits_larger_int!(u32, u64); - -#[cfg(all(target_pointer_width = "64", not(target_os = "windows")))] -int_fits_c_long!(i64); - -// manual implementation for i64 on systems with 32-bit long -#[cfg(any(target_pointer_width = "32", target_os = "windows"))] -int_convert_u64_or_i64!(i64, ffi::PyLong_FromLongLong, ffi::PyLong_AsLongLong, false); - -#[cfg(all(target_pointer_width = "64", not(target_os = "windows")))] -int_fits_c_long!(isize); -#[cfg(any(target_pointer_width = "32", target_os = "windows"))] -int_fits_larger_int!(isize, i64); - -int_fits_larger_int!(usize, u64); - -// u64 has a manual implementation as it never fits into signed long -int_convert_u64_or_i64!( - u64, - ffi::PyLong_FromUnsignedLongLong, - ffi::PyLong_AsUnsignedLongLong, - true -); - -#[cfg(all(not(Py_LIMITED_API), not(GraalPy)))] -mod fast_128bit_int_conversion { - use super::*; - - // for 128bit Integers - macro_rules! int_convert_128 { - ($rust_type: ty, $is_signed: literal) => { - #[allow(deprecated)] - impl ToPyObject for $rust_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - #[allow(deprecated)] - impl IntoPy for $rust_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for $rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - #[cfg(not(Py_3_13))] - { - let bytes = self.to_le_bytes(); - unsafe { - Ok(ffi::_PyLong_FromByteArray( - bytes.as_ptr().cast(), - bytes.len(), - 1, - $is_signed.into(), - ) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - #[cfg(Py_3_13)] - { - let bytes = self.to_ne_bytes(); - - if $is_signed { - unsafe { - Ok(ffi::PyLong_FromNativeBytes( - bytes.as_ptr().cast(), - bytes.len(), - ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN, - ) - .assume_owned(py) - .downcast_into_unchecked()) - } - } else { - unsafe { - Ok(ffi::PyLong_FromUnsignedNativeBytes( - bytes.as_ptr().cast(), - bytes.len(), - ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN, - ) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl<'py> IntoPyObject<'py> for &$rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl FromPyObject<'_> for $rust_type { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<$rust_type> { - let num = - unsafe { ffi::PyNumber_Index(ob.as_ptr()).assume_owned_or_err(ob.py())? }; - let mut buffer = [0u8; std::mem::size_of::<$rust_type>()]; - #[cfg(not(Py_3_13))] - { - crate::err::error_on_minusone(ob.py(), unsafe { - ffi::_PyLong_AsByteArray( - num.as_ptr() as *mut ffi::PyLongObject, - buffer.as_mut_ptr(), - buffer.len(), - 1, - $is_signed.into(), - ) - })?; - Ok(<$rust_type>::from_le_bytes(buffer)) - } - #[cfg(Py_3_13)] - { - let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN; - if !$is_signed { - flags |= ffi::Py_ASNATIVEBYTES_UNSIGNED_BUFFER - | ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE; - } - let actual_size: usize = unsafe { - ffi::PyLong_AsNativeBytes( - num.as_ptr(), - buffer.as_mut_ptr().cast(), - buffer - .len() - .try_into() - .expect("length of buffer fits in Py_ssize_t"), - flags, - ) - } - .try_into() - .map_err(|_| PyErr::fetch(ob.py()))?; - if actual_size as usize > buffer.len() { - return Err(crate::exceptions::PyOverflowError::new_err( - "Python int larger than 128 bits", - )); - } - Ok(<$rust_type>::from_ne_bytes(buffer)) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } - } - }; - } - - int_convert_128!(i128, true); - int_convert_128!(u128, false); -} - -// For ABI3 we implement the conversion manually. -#[cfg(any(Py_LIMITED_API, GraalPy))] -mod slow_128bit_int_conversion { - use super::*; - const SHIFT: usize = 64; - - // for 128bit Integers - macro_rules! int_convert_128 { - ($rust_type: ty, $half_type: ty) => { - #[allow(deprecated)] - impl ToPyObject for $rust_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - #[allow(deprecated)] - impl IntoPy for $rust_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for $rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let lower = (self as u64).into_pyobject(py)?; - let upper = ((self >> SHIFT) as $half_type).into_pyobject(py)?; - let shift = SHIFT.into_pyobject(py)?; - unsafe { - let shifted = - ffi::PyNumber_Lshift(upper.as_ptr(), shift.as_ptr()).assume_owned(py); - - Ok(ffi::PyNumber_Or(shifted.as_ptr(), lower.as_ptr()) - .assume_owned(py) - .downcast_into_unchecked()) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl<'py> IntoPyObject<'py> for &$rust_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl FromPyObject<'_> for $rust_type { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<$rust_type> { - let py = ob.py(); - unsafe { - let lower = err_if_invalid_value( - py, - -1 as _, - ffi::PyLong_AsUnsignedLongLongMask(ob.as_ptr()), - )? as $rust_type; - let shift = SHIFT.into_pyobject(py)?; - let shifted = PyObject::from_owned_ptr_or_err( - py, - ffi::PyNumber_Rshift(ob.as_ptr(), shift.as_ptr()), - )?; - let upper: $half_type = shifted.extract(py)?; - Ok((<$rust_type>::from(upper) << SHIFT) | lower) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } - } - }; - } - - int_convert_128!(i128, i64); - int_convert_128!(u128, u64); -} - -fn err_if_invalid_value( - py: Python<'_>, - invalid_value: T, - actual_value: T, -) -> PyResult { - if actual_value == invalid_value { - if let Some(err) = PyErr::take(py) { - return Err(err); - } - } - - Ok(actual_value) -} - -macro_rules! nonzero_int_impl { - ($nonzero_type:ty, $primitive_type:ty) => { - #[allow(deprecated)] - impl ToPyObject for $nonzero_type { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - #[allow(deprecated)] - impl IntoPy for $nonzero_type { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } - } - - impl<'py> IntoPyObject<'py> for $nonzero_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.get().into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl<'py> IntoPyObject<'py> for &$nonzero_type { - type Target = PyInt; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("int") - } - } - - impl FromPyObject<'_> for $nonzero_type { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let val: $primitive_type = obj.extract()?; - <$nonzero_type>::try_from(val) - .map_err(|_| exceptions::PyValueError::new_err("invalid zero value")) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - <$primitive_type>::type_input() - } - } - }; -} - -nonzero_int_impl!(NonZeroI8, i8); -nonzero_int_impl!(NonZeroI16, i16); -nonzero_int_impl!(NonZeroI32, i32); -nonzero_int_impl!(NonZeroI64, i64); -nonzero_int_impl!(NonZeroI128, i128); -nonzero_int_impl!(NonZeroIsize, isize); -nonzero_int_impl!(NonZeroU8, u8); -nonzero_int_impl!(NonZeroU16, u16); -nonzero_int_impl!(NonZeroU32, u32); -nonzero_int_impl!(NonZeroU64, u64); -nonzero_int_impl!(NonZeroU128, u128); -nonzero_int_impl!(NonZeroUsize, usize); - -#[cfg(test)] -mod test_128bit_integers { - use super::*; - - #[cfg(not(target_arch = "wasm32"))] - use crate::types::PyDict; - - #[cfg(not(target_arch = "wasm32"))] - use crate::types::dict::PyDictMethods; - - #[cfg(not(target_arch = "wasm32"))] - use proptest::prelude::*; - - #[cfg(not(target_arch = "wasm32"))] - use std::ffi::CString; - - #[cfg(not(target_arch = "wasm32"))] - proptest! { - #[test] - fn test_i128_roundtrip(x: i128) { - Python::with_gil(|py| { - let x_py = x.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("x_py", &x_py).unwrap(); - py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: i128 = x_py.extract().unwrap(); - assert_eq!(x, roundtripped); - }) - } - - #[test] - fn test_nonzero_i128_roundtrip( - x in any::() - .prop_filter("Values must not be 0", |x| x != &0) - .prop_map(|x| NonZeroI128::new(x).unwrap()) - ) { - Python::with_gil(|py| { - let x_py = x.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("x_py", &x_py).unwrap(); - py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: NonZeroI128 = x_py.extract().unwrap(); - assert_eq!(x, roundtripped); - }) - } - } - - #[cfg(not(target_arch = "wasm32"))] - proptest! { - #[test] - fn test_u128_roundtrip(x: u128) { - Python::with_gil(|py| { - let x_py = x.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("x_py", &x_py).unwrap(); - py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: u128 = x_py.extract().unwrap(); - assert_eq!(x, roundtripped); - }) - } - - #[test] - fn test_nonzero_u128_roundtrip( - x in any::() - .prop_filter("Values must not be 0", |x| x != &0) - .prop_map(|x| NonZeroU128::new(x).unwrap()) - ) { - Python::with_gil(|py| { - let x_py = x.into_pyobject(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("x_py", &x_py).unwrap(); - py.run(&CString::new(format!("assert x_py == {}", x)).unwrap(), None, Some(&locals)).unwrap(); - let roundtripped: NonZeroU128 = x_py.extract().unwrap(); - assert_eq!(x, roundtripped); - }) - } - } - - #[test] - fn test_i128_max() { - Python::with_gil(|py| { - let v = i128::MAX; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!(v as u128, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_i128_min() { - Python::with_gil(|py| { - let v = i128::MIN; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_u128_max() { - Python::with_gil(|py| { - let v = u128::MAX; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_i128_overflow() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("(1 << 130) * -1"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } - - #[test] - fn test_u128_overflow() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("1 << 130"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } - - #[test] - fn test_nonzero_i128_max() { - Python::with_gil(|py| { - let v = NonZeroI128::new(i128::MAX).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!( - NonZeroU128::new(v.get() as u128).unwrap(), - obj.extract::().unwrap() - ); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_nonzero_i128_min() { - Python::with_gil(|py| { - let v = NonZeroI128::new(i128::MIN).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_nonzero_u128_max() { - Python::with_gil(|py| { - let v = NonZeroU128::new(u128::MAX).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }) - } - - #[test] - fn test_nonzero_i128_overflow() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("(1 << 130) * -1"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } - - #[test] - fn test_nonzero_u128_overflow() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("1 << 130"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } - - #[test] - fn test_nonzero_i128_zero_value() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("0"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } - - #[test] - fn test_nonzero_u128_zero_value() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("0"), None, None).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } -} - -#[cfg(test)] -mod tests { - use crate::types::PyAnyMethods; - use crate::{IntoPyObject, Python}; - use std::num::*; - - #[test] - fn test_u32_max() { - Python::with_gil(|py| { - let v = u32::MAX; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!(u64::from(v), obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_i64_max() { - Python::with_gil(|py| { - let v = i64::MAX; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!(v as u64, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_i64_min() { - Python::with_gil(|py| { - let v = i64::MIN; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_u64_max() { - Python::with_gil(|py| { - let v = u64::MAX; - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }); - } - - macro_rules! test_common ( - ($test_mod_name:ident, $t:ty) => ( - mod $test_mod_name { - use crate::exceptions; - use crate::conversion::IntoPyObject; - use crate::types::PyAnyMethods; - use crate::Python; - - #[test] - fn from_py_string_type_error() { - Python::with_gil(|py| { - let obj = ("123").into_pyobject(py).unwrap(); - let err = obj.extract::<$t>().unwrap_err(); - assert!(err.is_instance_of::(py)); - }); - } - - #[test] - fn from_py_float_type_error() { - Python::with_gil(|py| { - let obj = (12.3f64).into_pyobject(py).unwrap(); - let err = obj.extract::<$t>().unwrap_err(); - assert!(err.is_instance_of::(py));}); - } - - #[test] - fn to_py_object_and_back() { - Python::with_gil(|py| { - let val = 123 as $t; - let obj = val.into_pyobject(py).unwrap(); - assert_eq!(obj.extract::<$t>().unwrap(), val as $t);}); - } - } - ) - ); - - test_common!(i8, i8); - test_common!(u8, u8); - test_common!(i16, i16); - test_common!(u16, u16); - test_common!(i32, i32); - test_common!(u32, u32); - test_common!(i64, i64); - test_common!(u64, u64); - test_common!(isize, isize); - test_common!(usize, usize); - test_common!(i128, i128); - test_common!(u128, u128); - - #[test] - fn test_nonzero_u32_max() { - Python::with_gil(|py| { - let v = NonZeroU32::new(u32::MAX).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!(NonZeroU64::from(v), obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_nonzero_i64_max() { - Python::with_gil(|py| { - let v = NonZeroI64::new(i64::MAX).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert_eq!( - NonZeroU64::new(v.get() as u64).unwrap(), - obj.extract::().unwrap() - ); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_nonzero_i64_min() { - Python::with_gil(|py| { - let v = NonZeroI64::new(i64::MIN).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - assert!(obj.extract::().is_err()); - }); - } - - #[test] - fn test_nonzero_u64_max() { - Python::with_gil(|py| { - let v = NonZeroU64::new(u64::MAX).unwrap(); - let obj = v.into_pyobject(py).unwrap(); - assert_eq!(v, obj.extract::().unwrap()); - assert!(obj.extract::().is_err()); - }); - } - - macro_rules! test_nonzero_common ( - ($test_mod_name:ident, $t:ty) => ( - mod $test_mod_name { - use crate::exceptions; - use crate::conversion::IntoPyObject; - use crate::types::PyAnyMethods; - use crate::Python; - use std::num::*; - - #[test] - fn from_py_string_type_error() { - Python::with_gil(|py| { - let obj = ("123").into_pyobject(py).unwrap(); - let err = obj.extract::<$t>().unwrap_err(); - assert!(err.is_instance_of::(py)); - }); - } - - #[test] - fn from_py_float_type_error() { - Python::with_gil(|py| { - let obj = (12.3f64).into_pyobject(py).unwrap(); - let err = obj.extract::<$t>().unwrap_err(); - assert!(err.is_instance_of::(py));}); - } - - #[test] - fn to_py_object_and_back() { - Python::with_gil(|py| { - let val = <$t>::new(123).unwrap(); - let obj = val.into_pyobject(py).unwrap(); - assert_eq!(obj.extract::<$t>().unwrap(), val);}); - } - } - ) - ); - - test_nonzero_common!(nonzero_i8, NonZeroI8); - test_nonzero_common!(nonzero_u8, NonZeroU8); - test_nonzero_common!(nonzero_i16, NonZeroI16); - test_nonzero_common!(nonzero_u16, NonZeroU16); - test_nonzero_common!(nonzero_i32, NonZeroI32); - test_nonzero_common!(nonzero_u32, NonZeroU32); - test_nonzero_common!(nonzero_i64, NonZeroI64); - test_nonzero_common!(nonzero_u64, NonZeroU64); - test_nonzero_common!(nonzero_isize, NonZeroIsize); - test_nonzero_common!(nonzero_usize, NonZeroUsize); - test_nonzero_common!(nonzero_i128, NonZeroI128); - test_nonzero_common!(nonzero_u128, NonZeroU128); - - #[test] - fn test_i64_bool() { - Python::with_gil(|py| { - let obj = true.into_pyobject(py).unwrap(); - assert_eq!(1, obj.extract::().unwrap()); - let obj = false.into_pyobject(py).unwrap(); - assert_eq!(0, obj.extract::().unwrap()); - }) - } - - #[test] - fn test_i64_f64() { - Python::with_gil(|py| { - let obj = 12.34f64.into_pyobject(py).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - // with no remainder - let obj = 12f64.into_pyobject(py).unwrap(); - let err = obj.extract::().unwrap_err(); - assert!(err.is_instance_of::(py)); - }) - } -} diff --git a/vendor/pyo3/src/conversions/std/option.rs b/vendor/pyo3/src/conversions/std/option.rs deleted file mode 100644 index aac8c7ab21..0000000000 --- a/vendor/pyo3/src/conversions/std/option.rs +++ /dev/null @@ -1,109 +0,0 @@ -use crate::{ - conversion::IntoPyObject, ffi, types::any::PyAnyMethods, AsPyPointer, Bound, BoundObject, - FromPyObject, PyAny, PyObject, PyResult, Python, -}; - -/// `Option::Some` is converted like `T`. -/// `Option::None` is converted to Python `None`. -#[allow(deprecated)] -impl crate::ToPyObject for Option -where - T: crate::ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - self.as_ref() - .map_or_else(|| py.None(), |val| val.to_object(py)) - } -} - -#[allow(deprecated)] -impl crate::IntoPy for Option -where - T: crate::IntoPy, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - self.map_or_else(|| py.None(), |val| val.into_py(py)) - } -} - -impl<'py, T> IntoPyObject<'py> for Option -where - T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = T::Error; - - fn into_pyobject(self, py: Python<'py>) -> Result { - self.map_or_else( - || Ok(py.None().into_bound(py)), - |val| { - val.into_pyobject(py) - .map(BoundObject::into_any) - .map(BoundObject::into_bound) - }, - ) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &'a Option -where - &'a T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = <&'a T as IntoPyObject<'py>>::Error; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_ref().into_pyobject(py) - } -} - -impl<'py, T> FromPyObject<'py> for Option -where - T: FromPyObject<'py>, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - if obj.is_none() { - Ok(None) - } else { - obj.extract().map(Some) - } - } -} - -/// Convert `None` into a null pointer. -unsafe impl AsPyPointer for Option -where - T: AsPyPointer, -{ - #[inline] - fn as_ptr(&self) -> *mut ffi::PyObject { - self.as_ref() - .map_or_else(std::ptr::null_mut, |t| t.as_ptr()) - } -} - -#[cfg(test)] -mod tests { - use crate::{PyObject, Python}; - - #[test] - fn test_option_as_ptr() { - Python::with_gil(|py| { - use crate::AsPyPointer; - let mut option: Option = None; - assert_eq!(option.as_ptr(), std::ptr::null_mut()); - - let none = py.None(); - option = Some(none.clone_ref(py)); - - let ref_cnt = none.get_refcnt(py); - assert_eq!(option.as_ptr(), none.as_ptr()); - - // Ensure ref count not changed by as_ptr call - assert_eq!(none.get_refcnt(py), ref_cnt); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/osstr.rs b/vendor/pyo3/src/conversions/std/osstr.rs deleted file mode 100644 index 70b5bd152a..0000000000 --- a/vendor/pyo3/src/conversions/std/osstr.rs +++ /dev/null @@ -1,280 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Bound; -use crate::types::any::PyAnyMethods; -use crate::types::PyString; -use crate::{ffi, FromPyObject, PyAny, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::borrow::Cow; -use std::convert::Infallible; -use std::ffi::{OsStr, OsString}; - -#[allow(deprecated)] -impl ToPyObject for OsStr { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &OsStr { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - // If the string is UTF-8, take the quick and easy shortcut - if let Some(valid_utf8_path) = self.to_str() { - return valid_utf8_path.into_pyobject(py); - } - - // All targets besides windows support the std::os::unix::ffi::OsStrExt API: - // https://doc.rust-lang.org/src/std/sys_common/mod.rs.html#59 - #[cfg(not(windows))] - { - #[cfg(target_os = "wasi")] - let bytes = std::os::wasi::ffi::OsStrExt::as_bytes(self); - #[cfg(not(target_os = "wasi"))] - let bytes = std::os::unix::ffi::OsStrExt::as_bytes(self); - - let ptr = bytes.as_ptr().cast(); - let len = bytes.len() as ffi::Py_ssize_t; - unsafe { - // DecodeFSDefault automatically chooses an appropriate decoding mechanism to - // parse os strings losslessly (i.e. surrogateescape most of the time) - Ok(ffi::PyUnicode_DecodeFSDefaultAndSize(ptr, len) - .assume_owned(py) - .downcast_into_unchecked::()) - } - } - - #[cfg(windows)] - { - let wstr: Vec = std::os::windows::ffi::OsStrExt::encode_wide(self).collect(); - - unsafe { - // This will not panic because the data from encode_wide is well-formed Windows - // string data - - Ok( - ffi::PyUnicode_FromWideChar(wstr.as_ptr(), wstr.len() as ffi::Py_ssize_t) - .assume_owned(py) - .downcast_into_unchecked::(), - ) - } - } - } -} - -impl<'py> IntoPyObject<'py> for &&OsStr { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -// There's no FromPyObject implementation for &OsStr because albeit possible on Unix, this would -// be impossible to implement on Windows. Hence it's omitted entirely - -impl FromPyObject<'_> for OsString { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - let pystring = ob.downcast::()?; - - #[cfg(not(windows))] - { - // Decode from Python's lossless bytes string representation back into raw bytes - let fs_encoded_bytes = unsafe { - crate::Py::::from_owned_ptr( - ob.py(), - ffi::PyUnicode_EncodeFSDefault(pystring.as_ptr()), - ) - }; - - // Create an OsStr view into the raw bytes from Python - #[cfg(target_os = "wasi")] - let os_str: &OsStr = - std::os::wasi::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py())); - #[cfg(not(target_os = "wasi"))] - let os_str: &OsStr = - std::os::unix::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py())); - - Ok(os_str.to_os_string()) - } - - #[cfg(windows)] - { - use crate::types::string::PyStringMethods; - - // Take the quick and easy shortcut if UTF-8 - if let Ok(utf8_string) = pystring.to_cow() { - return Ok(utf8_string.into_owned().into()); - } - - // Get an owned allocated wide char buffer from PyString, which we have to deallocate - // ourselves - let size = - unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), std::ptr::null_mut(), 0) }; - crate::err::error_on_minusone(ob.py(), size)?; - - let mut buffer = vec![0; size as usize]; - let bytes_read = - unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), buffer.as_mut_ptr(), size) }; - assert_eq!(bytes_read, size); - - // Copy wide char buffer into OsString - let os_string = std::os::windows::ffi::OsStringExt::from_wide(&buffer); - - Ok(os_string) - } - } -} - -#[allow(deprecated)] -impl IntoPy for &'_ OsStr { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl ToPyObject for Cow<'_, OsStr> { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Cow<'_, OsStr> { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Cow<'_, OsStr> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -impl<'py> IntoPyObject<'py> for &Cow<'_, OsStr> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (&**self).into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for OsString { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for OsString { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for OsString { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -#[allow(deprecated)] -impl IntoPy for &OsString { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &OsString { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -#[cfg(test)] -mod tests { - use crate::types::{PyAnyMethods, PyString, PyStringMethods}; - use crate::{BoundObject, IntoPyObject, Python}; - use std::fmt::Debug; - use std::{ - borrow::Cow, - ffi::{OsStr, OsString}, - }; - - #[test] - #[cfg(not(windows))] - fn test_non_utf8_conversion() { - Python::with_gil(|py| { - #[cfg(not(target_os = "wasi"))] - use std::os::unix::ffi::OsStrExt; - #[cfg(target_os = "wasi")] - use std::os::wasi::ffi::OsStrExt; - - // this is not valid UTF-8 - let payload = &[250, 251, 252, 253, 254, 255, 0, 255]; - let os_str = OsStr::from_bytes(payload); - - // do a roundtrip into Pythonland and back and compare - let py_str = os_str.into_pyobject(py).unwrap(); - let os_str_2: OsString = py_str.extract().unwrap(); - assert_eq!(os_str, os_str_2); - }); - } - - #[test] - fn test_intopyobject_roundtrip() { - Python::with_gil(|py| { - fn test_roundtrip<'py, T>(py: Python<'py>, obj: T) - where - T: IntoPyObject<'py> + AsRef + Debug + Clone, - T::Error: Debug, - { - let pyobject = obj.clone().into_pyobject(py).unwrap().into_any(); - let pystring = pyobject.as_borrowed().downcast::().unwrap(); - assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy()); - let roundtripped_obj: OsString = pystring.extract().unwrap(); - assert_eq!(obj.as_ref(), roundtripped_obj.as_os_str()); - } - let os_str = OsStr::new("Hello\0\n🐍"); - test_roundtrip::<&OsStr>(py, os_str); - test_roundtrip::>(py, Cow::Borrowed(os_str)); - test_roundtrip::>(py, Cow::Owned(os_str.to_os_string())); - test_roundtrip::(py, os_str.to_os_string()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/path.rs b/vendor/pyo3/src/conversions/std/path.rs deleted file mode 100644 index dc528ee359..0000000000 --- a/vendor/pyo3/src/conversions/std/path.rs +++ /dev/null @@ -1,196 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Bound; -use crate::types::any::PyAnyMethods; -use crate::types::PyString; -use crate::{ffi, FromPyObject, PyAny, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::borrow::Cow; -use std::convert::Infallible; -use std::ffi::OsString; -use std::path::{Path, PathBuf}; - -#[allow(deprecated)] -impl ToPyObject for Path { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -// See osstr.rs for why there's no FromPyObject impl for &Path - -impl FromPyObject<'_> for PathBuf { - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - // We use os.fspath to get the underlying path as bytes or str - let path = unsafe { ffi::PyOS_FSPath(ob.as_ptr()).assume_owned_or_err(ob.py())? }; - Ok(path.extract::()?.into()) - } -} - -#[allow(deprecated)] -impl IntoPy for &Path { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &Path { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -impl<'py> IntoPyObject<'py> for &&Path { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for Cow<'_, Path> { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Cow<'_, Path> { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Cow<'_, Path> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -impl<'py> IntoPyObject<'py> for &Cow<'_, Path> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -#[allow(deprecated)] -impl ToPyObject for PathBuf { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for PathBuf { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for PathBuf { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -#[allow(deprecated)] -impl IntoPy for &PathBuf { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &PathBuf { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.as_os_str().into_pyobject(py) - } -} - -#[cfg(test)] -mod tests { - use crate::types::{PyAnyMethods, PyString, PyStringMethods}; - use crate::{BoundObject, IntoPyObject, Python}; - use std::borrow::Cow; - use std::fmt::Debug; - use std::path::{Path, PathBuf}; - - #[test] - #[cfg(not(windows))] - fn test_non_utf8_conversion() { - Python::with_gil(|py| { - use std::ffi::OsStr; - #[cfg(not(target_os = "wasi"))] - use std::os::unix::ffi::OsStrExt; - #[cfg(target_os = "wasi")] - use std::os::wasi::ffi::OsStrExt; - - // this is not valid UTF-8 - let payload = &[250, 251, 252, 253, 254, 255, 0, 255]; - let path = Path::new(OsStr::from_bytes(payload)); - - // do a roundtrip into Pythonland and back and compare - let py_str = path.into_pyobject(py).unwrap(); - let path_2: PathBuf = py_str.extract().unwrap(); - assert_eq!(path, path_2); - }); - } - - #[test] - fn test_intopyobject_roundtrip() { - Python::with_gil(|py| { - fn test_roundtrip<'py, T>(py: Python<'py>, obj: T) - where - T: IntoPyObject<'py> + AsRef + Debug + Clone, - T::Error: Debug, - { - let pyobject = obj.clone().into_pyobject(py).unwrap().into_any(); - let pystring = pyobject.as_borrowed().downcast::().unwrap(); - assert_eq!(pystring.to_string_lossy(), obj.as_ref().to_string_lossy()); - let roundtripped_obj: PathBuf = pystring.extract().unwrap(); - assert_eq!(obj.as_ref(), roundtripped_obj.as_path()); - } - let path = Path::new("Hello\0\n🐍"); - test_roundtrip::<&Path>(py, path); - test_roundtrip::>(py, Cow::Borrowed(path)); - test_roundtrip::>(py, Cow::Owned(path.to_path_buf())); - test_roundtrip::(py, path.to_path_buf()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/set.rs b/vendor/pyo3/src/conversions/std/set.rs deleted file mode 100644 index 3f1e6733f1..0000000000 --- a/vendor/pyo3/src/conversions/std/set.rs +++ /dev/null @@ -1,253 +0,0 @@ -use std::{cmp, collections, hash}; - -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - conversion::IntoPyObject, - instance::Bound, - types::{ - any::PyAnyMethods, - frozenset::PyFrozenSetMethods, - set::{new_from_iter, try_new_from_iter, PySetMethods}, - PyFrozenSet, PySet, - }, - FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[allow(deprecated)] -impl ToPyObject for collections::HashSet -where - T: hash::Hash + Eq + ToPyObject, - S: hash::BuildHasher + Default, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - new_from_iter(py, self) - .expect("Failed to create Python set from HashSet") - .into() - } -} - -#[allow(deprecated)] -impl ToPyObject for collections::BTreeSet -where - T: hash::Hash + Eq + ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - new_from_iter(py, self) - .expect("Failed to create Python set from BTreeSet") - .into() - } -} - -#[allow(deprecated)] -impl IntoPy for collections::HashSet -where - K: IntoPy + Eq + hash::Hash, - S: hash::BuildHasher + Default, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - new_from_iter(py, self.into_iter().map(|item| item.into_py(py))) - .expect("Failed to create Python set from HashSet") - .into() - } -} - -impl<'py, K, S> IntoPyObject<'py> for collections::HashSet -where - K: IntoPyObject<'py> + Eq + hash::Hash, - S: hash::BuildHasher + Default, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::set_of(K::type_output()) - } -} - -impl<'a, 'py, K, H> IntoPyObject<'py> for &'a collections::HashSet -where - &'a K: IntoPyObject<'py> + Eq + hash::Hash, - K: 'a, // MSRV - H: hash::BuildHasher, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self.iter()) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::set_of(<&K>::type_output()) - } -} - -impl<'py, K, S> FromPyObject<'py> for collections::HashSet -where - K: FromPyObject<'py> + cmp::Eq + hash::Hash, - S: hash::BuildHasher + Default, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - match ob.downcast::() { - Ok(set) => set.iter().map(|any| any.extract()).collect(), - Err(err) => { - if let Ok(frozen_set) = ob.downcast::() { - frozen_set.iter().map(|any| any.extract()).collect() - } else { - Err(PyErr::from(err)) - } - } - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::set_of(K::type_input()) - } -} - -#[allow(deprecated)] -impl IntoPy for collections::BTreeSet -where - K: IntoPy + cmp::Ord, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - new_from_iter(py, self.into_iter().map(|item| item.into_py(py))) - .expect("Failed to create Python set from BTreeSet") - .into() - } -} - -impl<'py, K> IntoPyObject<'py> for collections::BTreeSet -where - K: IntoPyObject<'py> + cmp::Ord, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::set_of(K::type_output()) - } -} - -impl<'a, 'py, K> IntoPyObject<'py> for &'a collections::BTreeSet -where - &'a K: IntoPyObject<'py> + cmp::Ord, - K: 'a, -{ - type Target = PySet; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - try_new_from_iter(py, self.iter()) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::set_of(<&K>::type_output()) - } -} - -impl<'py, K> FromPyObject<'py> for collections::BTreeSet -where - K: FromPyObject<'py> + cmp::Ord, -{ - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - match ob.downcast::() { - Ok(set) => set.iter().map(|any| any.extract()).collect(), - Err(err) => { - if let Ok(frozen_set) = ob.downcast::() { - frozen_set.iter().map(|any| any.extract()).collect() - } else { - Err(PyErr::from(err)) - } - } - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::set_of(K::type_input()) - } -} - -#[cfg(test)] -mod tests { - use crate::types::{any::PyAnyMethods, PyFrozenSet, PySet}; - use crate::{IntoPyObject, PyObject, Python}; - use std::collections::{BTreeSet, HashSet}; - - #[test] - fn test_extract_hashset() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: HashSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - - let set = PyFrozenSet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: HashSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - }); - } - - #[test] - fn test_extract_btreeset() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: BTreeSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - - let set = PyFrozenSet::new(py, [1, 2, 3, 4, 5]).unwrap(); - let hash_set: BTreeSet = set.extract().unwrap(); - assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect()); - }); - } - - #[test] - #[allow(deprecated)] - fn test_set_into_py() { - use crate::IntoPy; - Python::with_gil(|py| { - let bt: BTreeSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - let hs: HashSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - - let bto: PyObject = bt.clone().into_py(py); - let hso: PyObject = hs.clone().into_py(py); - - assert_eq!(bt, bto.extract(py).unwrap()); - assert_eq!(hs, hso.extract(py).unwrap()); - }); - } - - #[test] - fn test_set_into_pyobject() { - Python::with_gil(|py| { - let bt: BTreeSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - let hs: HashSet = [1, 2, 3, 4, 5].iter().cloned().collect(); - - let bto = (&bt).into_pyobject(py).unwrap(); - let hso = (&hs).into_pyobject(py).unwrap(); - - assert_eq!(bt, bto.extract().unwrap()); - assert_eq!(hs, hso.extract().unwrap()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/slice.rs b/vendor/pyo3/src/conversions/std/slice.rs deleted file mode 100644 index 798e5b54c4..0000000000 --- a/vendor/pyo3/src/conversions/std/slice.rs +++ /dev/null @@ -1,199 +0,0 @@ -use std::borrow::Cow; - -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - conversion::IntoPyObject, - types::{PyByteArray, PyByteArrayMethods, PyBytes}, - Bound, Py, PyAny, PyErr, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[allow(deprecated)] -impl IntoPy for &[u8] { - fn into_py(self, py: Python<'_>) -> PyObject { - PyBytes::new(py, self).unbind().into() - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &'a [T] -where - &'a T: IntoPyObject<'py>, - T: 'a, // MSRV -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - /// Turns [`&[u8]`](std::slice) into [`PyBytes`], all other `T`s will be turned into a [`PyList`] - /// - /// [`PyBytes`]: crate::types::PyBytes - /// [`PyList`]: crate::types::PyList - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - <&T>::borrowed_sequence_into_pyobject(self, py, crate::conversion::private::Token) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::union_of(&[ - TypeInfo::builtin("bytes"), - TypeInfo::list_of(<&T>::type_output()), - ]) - } -} - -impl<'a> crate::conversion::FromPyObjectBound<'a, '_> for &'a [u8] { - fn from_py_object_bound(obj: crate::Borrowed<'a, '_, PyAny>) -> PyResult { - Ok(obj.downcast::()?.as_bytes()) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::builtin("bytes") - } -} - -/// Special-purpose trait impl to efficiently handle both `bytes` and `bytearray` -/// -/// If the source object is a `bytes` object, the `Cow` will be borrowed and -/// pointing into the source object, and no copying or heap allocations will happen. -/// If it is a `bytearray`, its contents will be copied to an owned `Cow`. -impl<'a> crate::conversion::FromPyObjectBound<'a, '_> for Cow<'a, [u8]> { - fn from_py_object_bound(ob: crate::Borrowed<'a, '_, PyAny>) -> PyResult { - if let Ok(bytes) = ob.downcast::() { - return Ok(Cow::Borrowed(bytes.as_bytes())); - } - - let byte_array = ob.downcast::()?; - Ok(Cow::Owned(byte_array.to_vec())) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -#[allow(deprecated)] -impl ToPyObject for Cow<'_, [u8]> { - fn to_object(&self, py: Python<'_>) -> Py { - PyBytes::new(py, self.as_ref()).into() - } -} - -#[allow(deprecated)] -impl IntoPy> for Cow<'_, [u8]> { - fn into_py(self, py: Python<'_>) -> Py { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py, T> IntoPyObject<'py> for Cow<'_, [T]> -where - T: Clone, - for<'a> &'a T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - /// Turns `Cow<[u8]>` into [`PyBytes`], all other `T`s will be turned into a [`PyList`] - /// - /// [`PyBytes`]: crate::types::PyBytes - /// [`PyList`]: crate::types::PyList - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - <&T>::borrowed_sequence_into_pyobject(self.as_ref(), py, crate::conversion::private::Token) - } -} - -#[cfg(test)] -mod tests { - use std::borrow::Cow; - - use crate::{ - conversion::IntoPyObject, - ffi, - types::{any::PyAnyMethods, PyBytes, PyBytesMethods, PyList}, - Python, - }; - - #[test] - fn test_extract_bytes() { - Python::with_gil(|py| { - let py_bytes = py.eval(ffi::c_str!("b'Hello Python'"), None, None).unwrap(); - let bytes: &[u8] = py_bytes.extract().unwrap(); - assert_eq!(bytes, b"Hello Python"); - }); - } - - #[test] - fn test_cow_impl() { - Python::with_gil(|py| { - let bytes = py.eval(ffi::c_str!(r#"b"foobar""#), None, None).unwrap(); - let cow = bytes.extract::>().unwrap(); - assert_eq!(cow, Cow::<[u8]>::Borrowed(b"foobar")); - - let byte_array = py - .eval(ffi::c_str!(r#"bytearray(b"foobar")"#), None, None) - .unwrap(); - let cow = byte_array.extract::>().unwrap(); - assert_eq!(cow, Cow::<[u8]>::Owned(b"foobar".to_vec())); - - let something_else_entirely = py.eval(ffi::c_str!("42"), None, None).unwrap(); - something_else_entirely - .extract::>() - .unwrap_err(); - - let cow = Cow::<[u8]>::Borrowed(b"foobar").into_pyobject(py).unwrap(); - assert!(cow.is_instance_of::()); - - let cow = Cow::<[u8]>::Owned(b"foobar".to_vec()) - .into_pyobject(py) - .unwrap(); - assert!(cow.is_instance_of::()); - }); - } - - #[test] - fn test_slice_intopyobject_impl() { - Python::with_gil(|py| { - let bytes: &[u8] = b"foobar"; - let obj = bytes.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), bytes); - - let nums: &[u16] = &[0, 1, 2, 3]; - let obj = nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } - - #[test] - fn test_cow_intopyobject_impl() { - Python::with_gil(|py| { - let borrowed_bytes = Cow::<[u8]>::Borrowed(b"foobar"); - let obj = borrowed_bytes.clone().into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &*borrowed_bytes); - - let owned_bytes = Cow::<[u8]>::Owned(b"foobar".to_vec()); - let obj = owned_bytes.clone().into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &*owned_bytes); - - let borrowed_nums = Cow::<[u16]>::Borrowed(&[0, 1, 2, 3]); - let obj = borrowed_nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - - let owned_nums = Cow::<[u16]>::Owned(vec![0, 1, 2, 3]); - let obj = owned_nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } -} diff --git a/vendor/pyo3/src/conversions/std/string.rs b/vendor/pyo3/src/conversions/std/string.rs deleted file mode 100644 index 074b5c2ba7..0000000000 --- a/vendor/pyo3/src/conversions/std/string.rs +++ /dev/null @@ -1,384 +0,0 @@ -use std::{borrow::Cow, convert::Infallible}; - -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - conversion::IntoPyObject, - instance::Bound, - types::{any::PyAnyMethods, string::PyStringMethods, PyString}, - FromPyObject, Py, PyAny, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -/// Converts a Rust `str` to a Python object. -/// See `PyString::new` for details on the conversion. -#[allow(deprecated)] -impl ToPyObject for str { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for &str { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy> for &str { - #[inline] - fn into_py(self, py: Python<'_>) -> Py { - self.into_pyobject(py).unwrap().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &str { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, self)) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -impl<'py> IntoPyObject<'py> for &&str { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -/// Converts a Rust `Cow<'_, str>` to a Python object. -/// See `PyString::new` for details on the conversion. -#[allow(deprecated)] -impl ToPyObject for Cow<'_, str> { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Cow<'_, str> { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Cow<'_, str> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -impl<'py> IntoPyObject<'py> for &Cow<'_, str> { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (&**self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -/// Converts a Rust `String` to a Python object. -/// See `PyString::new` for details on the conversion. -#[allow(deprecated)] -impl ToPyObject for String { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl ToPyObject for char { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for char { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for char { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let mut bytes = [0u8; 4]; - Ok(PyString::new(py, self.encode_utf8(&mut bytes))) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -impl<'py> IntoPyObject<'py> for &char { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -#[allow(deprecated)] -impl IntoPy for String { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for String { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, &self)) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("str") - } -} - -#[allow(deprecated)] -impl IntoPy for &String { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for &String { - type Target = PyString; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, self)) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - ::type_output() - } -} - -#[cfg(any(Py_3_10, not(Py_LIMITED_API)))] -impl<'a> crate::conversion::FromPyObjectBound<'a, '_> for &'a str { - fn from_py_object_bound(ob: crate::Borrowed<'a, '_, PyAny>) -> PyResult { - ob.downcast::()?.to_str() - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - ::type_input() - } -} - -impl<'a> crate::conversion::FromPyObjectBound<'a, '_> for Cow<'a, str> { - fn from_py_object_bound(ob: crate::Borrowed<'a, '_, PyAny>) -> PyResult { - ob.downcast::()?.to_cow() - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - ::type_input() - } -} - -/// Allows extracting strings from Python objects. -/// Accepts Python `str` and `unicode` objects. -impl FromPyObject<'_> for String { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - obj.downcast::()?.to_cow().map(Cow::into_owned) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -impl FromPyObject<'_> for char { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let s = obj.downcast::()?.to_cow()?; - let mut iter = s.chars(); - if let (Some(ch), None) = (iter.next(), iter.next()) { - Ok(ch) - } else { - Err(crate::exceptions::PyValueError::new_err( - "expected a string of length 1", - )) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - ::type_input() - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::{IntoPyObject, PyObject, Python}; - use std::borrow::Cow; - - #[test] - #[allow(deprecated)] - fn test_cow_into_py() { - use crate::IntoPy; - Python::with_gil(|py| { - let s = "Hello Python"; - let py_string: PyObject = Cow::Borrowed(s).into_py(py); - assert_eq!(s, py_string.extract::>(py).unwrap()); - let py_string: PyObject = Cow::::Owned(s.into()).into_py(py); - assert_eq!(s, py_string.extract::>(py).unwrap()); - }) - } - - #[test] - fn test_cow_into_pyobject() { - Python::with_gil(|py| { - let s = "Hello Python"; - let py_string = Cow::Borrowed(s).into_pyobject(py).unwrap(); - assert_eq!(s, py_string.extract::>().unwrap()); - let py_string = Cow::::Owned(s.into()).into_pyobject(py).unwrap(); - assert_eq!(s, py_string.extract::>().unwrap()); - }) - } - - #[test] - fn test_non_bmp() { - Python::with_gil(|py| { - let s = "\u{1F30F}"; - let py_string = s.into_pyobject(py).unwrap(); - assert_eq!(s, py_string.extract::().unwrap()); - }) - } - - #[test] - fn test_extract_str() { - Python::with_gil(|py| { - let s = "Hello Python"; - let py_string = s.into_pyobject(py).unwrap(); - - let s2: Cow<'_, str> = py_string.extract().unwrap(); - assert_eq!(s, s2); - }) - } - - #[test] - fn test_extract_char() { - Python::with_gil(|py| { - let ch = '😃'; - let py_string = ch.into_pyobject(py).unwrap(); - let ch2: char = py_string.extract().unwrap(); - assert_eq!(ch, ch2); - }) - } - - #[test] - fn test_extract_char_err() { - Python::with_gil(|py| { - let s = "Hello Python"; - let py_string = s.into_pyobject(py).unwrap(); - let err: crate::PyResult = py_string.extract(); - assert!(err - .unwrap_err() - .to_string() - .contains("expected a string of length 1")); - }) - } - - #[test] - fn test_string_into_pyobject() { - Python::with_gil(|py| { - let s = "Hello Python"; - let s2 = s.to_owned(); - let s3 = &s2; - assert_eq!( - s, - s3.into_pyobject(py) - .unwrap() - .extract::>() - .unwrap() - ); - assert_eq!( - s, - s2.into_pyobject(py) - .unwrap() - .extract::>() - .unwrap() - ); - assert_eq!( - s, - s.into_pyobject(py) - .unwrap() - .extract::>() - .unwrap() - ); - }) - } -} diff --git a/vendor/pyo3/src/conversions/std/time.rs b/vendor/pyo3/src/conversions/std/time.rs deleted file mode 100755 index 741c28ee90..0000000000 --- a/vendor/pyo3/src/conversions/std/time.rs +++ /dev/null @@ -1,445 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::exceptions::{PyOverflowError, PyValueError}; -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -#[cfg(Py_LIMITED_API)] -use crate::types::PyType; -#[cfg(not(Py_LIMITED_API))] -use crate::types::{timezone_utc, PyDateTime, PyDelta, PyDeltaAccess}; -#[cfg(Py_LIMITED_API)] -use crate::Py; -use crate::{intern, Bound, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; - -const SECONDS_PER_DAY: u64 = 24 * 60 * 60; - -impl FromPyObject<'_> for Duration { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - #[cfg(not(Py_LIMITED_API))] - let (days, seconds, microseconds) = { - let delta = obj.downcast::()?; - ( - delta.get_days(), - delta.get_seconds(), - delta.get_microseconds(), - ) - }; - #[cfg(Py_LIMITED_API)] - let (days, seconds, microseconds): (i32, i32, i32) = { - ( - obj.getattr(intern!(obj.py(), "days"))?.extract()?, - obj.getattr(intern!(obj.py(), "seconds"))?.extract()?, - obj.getattr(intern!(obj.py(), "microseconds"))?.extract()?, - ) - }; - - // We cast - let days = u64::try_from(days).map_err(|_| { - PyValueError::new_err( - "It is not possible to convert a negative timedelta to a Rust Duration", - ) - })?; - let seconds = u64::try_from(seconds).unwrap(); // 0 <= seconds < 3600*24 - let microseconds = u32::try_from(microseconds).unwrap(); // 0 <= microseconds < 1000000 - - // We convert - let total_seconds = days * SECONDS_PER_DAY + seconds; // We casted from i32, this can't overflow - let nanoseconds = microseconds.checked_mul(1_000).unwrap(); // 0 <= microseconds < 1000000 - - Ok(Duration::new(total_seconds, nanoseconds)) - } -} - -#[allow(deprecated)] -impl ToPyObject for Duration { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for Duration { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for Duration { - #[cfg(not(Py_LIMITED_API))] - type Target = PyDelta; - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let days = self.as_secs() / SECONDS_PER_DAY; - let seconds = self.as_secs() % SECONDS_PER_DAY; - let microseconds = self.subsec_micros(); - - #[cfg(not(Py_LIMITED_API))] - { - PyDelta::new( - py, - days.try_into()?, - seconds.try_into().unwrap(), - microseconds.try_into().unwrap(), - false, - ) - } - #[cfg(Py_LIMITED_API)] - { - static TIMEDELTA: GILOnceCell> = GILOnceCell::new(); - TIMEDELTA - .import(py, "datetime", "timedelta")? - .call1((days, seconds, microseconds)) - } - } -} - -impl<'py> IntoPyObject<'py> for &Duration { - #[cfg(not(Py_LIMITED_API))] - type Target = PyDelta; - #[cfg(Py_LIMITED_API)] - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -// Conversions between SystemTime and datetime do not rely on the floating point timestamp of the -// timestamp/fromtimestamp APIs to avoid possible precision loss but goes through the -// timedelta/std::time::Duration types by taking for reference point the UNIX epoch. -// -// TODO: it might be nice to investigate using timestamps anyway, at least when the datetime is a safe range. - -impl FromPyObject<'_> for SystemTime { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let duration_since_unix_epoch: Duration = obj - .call_method1(intern!(obj.py(), "__sub__"), (unix_epoch_py(obj.py())?,))? - .extract()?; - UNIX_EPOCH - .checked_add(duration_since_unix_epoch) - .ok_or_else(|| { - PyOverflowError::new_err("Overflow error when converting the time to Rust") - }) - } -} - -#[allow(deprecated)] -impl ToPyObject for SystemTime { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for SystemTime { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for SystemTime { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - let duration_since_unix_epoch = - self.duration_since(UNIX_EPOCH).unwrap().into_pyobject(py)?; - unix_epoch_py(py)? - .bind(py) - .call_method1(intern!(py, "__add__"), (duration_since_unix_epoch,)) - } -} - -impl<'py> IntoPyObject<'py> for &SystemTime { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } -} - -fn unix_epoch_py(py: Python<'_>) -> PyResult<&PyObject> { - static UNIX_EPOCH: GILOnceCell = GILOnceCell::new(); - UNIX_EPOCH.get_or_try_init(py, || { - #[cfg(not(Py_LIMITED_API))] - { - Ok::<_, PyErr>( - PyDateTime::new(py, 1970, 1, 1, 0, 0, 0, 0, Some(&timezone_utc(py)))?.into(), - ) - } - #[cfg(Py_LIMITED_API)] - { - let datetime = py.import("datetime")?; - let utc = datetime.getattr("timezone")?.getattr("utc")?; - Ok::<_, PyErr>( - datetime - .getattr("datetime")? - .call1((1970, 1, 1, 0, 0, 0, 0, utc)) - .unwrap() - .into(), - ) - } - }) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::PyDict; - - #[test] - fn test_duration_frompyobject() { - Python::with_gil(|py| { - assert_eq!( - new_timedelta(py, 0, 0, 0).extract::().unwrap(), - Duration::new(0, 0) - ); - assert_eq!( - new_timedelta(py, 1, 0, 0).extract::().unwrap(), - Duration::new(86400, 0) - ); - assert_eq!( - new_timedelta(py, 0, 1, 0).extract::().unwrap(), - Duration::new(1, 0) - ); - assert_eq!( - new_timedelta(py, 0, 0, 1).extract::().unwrap(), - Duration::new(0, 1_000) - ); - assert_eq!( - new_timedelta(py, 1, 1, 1).extract::().unwrap(), - Duration::new(86401, 1_000) - ); - assert_eq!( - timedelta_class(py) - .getattr("max") - .unwrap() - .extract::() - .unwrap(), - Duration::new(86399999999999, 999999000) - ); - }); - } - - #[test] - fn test_duration_frompyobject_negative() { - Python::with_gil(|py| { - assert_eq!( - new_timedelta(py, 0, -1, 0) - .extract::() - .unwrap_err() - .to_string(), - "ValueError: It is not possible to convert a negative timedelta to a Rust Duration" - ); - }) - } - - #[test] - fn test_duration_into_pyobject() { - Python::with_gil(|py| { - let assert_eq = |l: Bound<'_, PyAny>, r: Bound<'_, PyAny>| { - assert!(l.eq(r).unwrap()); - }; - - assert_eq( - Duration::new(0, 0).into_pyobject(py).unwrap().into_any(), - new_timedelta(py, 0, 0, 0), - ); - assert_eq( - Duration::new(86400, 0) - .into_pyobject(py) - .unwrap() - .into_any(), - new_timedelta(py, 1, 0, 0), - ); - assert_eq( - Duration::new(1, 0).into_pyobject(py).unwrap().into_any(), - new_timedelta(py, 0, 1, 0), - ); - assert_eq( - Duration::new(0, 1_000) - .into_pyobject(py) - .unwrap() - .into_any(), - new_timedelta(py, 0, 0, 1), - ); - assert_eq( - Duration::new(0, 1).into_pyobject(py).unwrap().into_any(), - new_timedelta(py, 0, 0, 0), - ); - assert_eq( - Duration::new(86401, 1_000) - .into_pyobject(py) - .unwrap() - .into_any(), - new_timedelta(py, 1, 1, 1), - ); - assert_eq( - Duration::new(86399999999999, 999999000) - .into_pyobject(py) - .unwrap() - .into_any(), - timedelta_class(py).getattr("max").unwrap(), - ); - }); - } - - #[test] - fn test_duration_into_pyobject_overflow() { - Python::with_gil(|py| { - assert!(Duration::MAX.into_pyobject(py).is_err()); - }) - } - - #[test] - fn test_time_frompyobject() { - Python::with_gil(|py| { - assert_eq!( - new_datetime(py, 1970, 1, 1, 0, 0, 0, 0) - .extract::() - .unwrap(), - UNIX_EPOCH - ); - assert_eq!( - new_datetime(py, 2020, 2, 3, 4, 5, 6, 7) - .extract::() - .unwrap(), - UNIX_EPOCH - .checked_add(Duration::new(1580702706, 7000)) - .unwrap() - ); - assert_eq!( - max_datetime(py).extract::().unwrap(), - UNIX_EPOCH - .checked_add(Duration::new(253402300799, 999999000)) - .unwrap() - ); - }); - } - - #[test] - fn test_time_frompyobject_before_epoch() { - Python::with_gil(|py| { - assert_eq!( - new_datetime(py, 1950, 1, 1, 0, 0, 0, 0) - .extract::() - .unwrap_err() - .to_string(), - "ValueError: It is not possible to convert a negative timedelta to a Rust Duration" - ); - }) - } - - #[test] - fn test_time_intopyobject() { - Python::with_gil(|py| { - let assert_eq = |l: Bound<'_, PyAny>, r: Bound<'_, PyAny>| { - assert!(l.eq(r).unwrap()); - }; - - assert_eq( - UNIX_EPOCH - .checked_add(Duration::new(1580702706, 7123)) - .unwrap() - .into_pyobject(py) - .unwrap(), - new_datetime(py, 2020, 2, 3, 4, 5, 6, 7), - ); - assert_eq( - UNIX_EPOCH - .checked_add(Duration::new(253402300799, 999999000)) - .unwrap() - .into_pyobject(py) - .unwrap(), - max_datetime(py), - ); - }); - } - - #[allow(clippy::too_many_arguments)] - fn new_datetime( - py: Python<'_>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - ) -> Bound<'_, PyAny> { - datetime_class(py) - .call1(( - year, - month, - day, - hour, - minute, - second, - microsecond, - tz_utc(py), - )) - .unwrap() - } - - fn max_datetime(py: Python<'_>) -> Bound<'_, PyAny> { - let naive_max = datetime_class(py).getattr("max").unwrap(); - let kargs = PyDict::new(py); - kargs.set_item("tzinfo", tz_utc(py)).unwrap(); - naive_max.call_method("replace", (), Some(&kargs)).unwrap() - } - - #[test] - fn test_time_intopyobject_overflow() { - let big_system_time = UNIX_EPOCH - .checked_add(Duration::new(300000000000, 0)) - .unwrap(); - Python::with_gil(|py| { - assert!(big_system_time.into_pyobject(py).is_err()); - }) - } - - fn tz_utc(py: Python<'_>) -> Bound<'_, PyAny> { - py.import("datetime") - .unwrap() - .getattr("timezone") - .unwrap() - .getattr("utc") - .unwrap() - } - - fn new_timedelta( - py: Python<'_>, - days: i32, - seconds: i32, - microseconds: i32, - ) -> Bound<'_, PyAny> { - timedelta_class(py) - .call1((days, seconds, microseconds)) - .unwrap() - } - - fn datetime_class(py: Python<'_>) -> Bound<'_, PyAny> { - py.import("datetime").unwrap().getattr("datetime").unwrap() - } - - fn timedelta_class(py: Python<'_>) -> Bound<'_, PyAny> { - py.import("datetime").unwrap().getattr("timedelta").unwrap() - } -} diff --git a/vendor/pyo3/src/conversions/std/vec.rs b/vendor/pyo3/src/conversions/std/vec.rs deleted file mode 100644 index b630ca4019..0000000000 --- a/vendor/pyo3/src/conversions/std/vec.rs +++ /dev/null @@ -1,124 +0,0 @@ -use crate::conversion::IntoPyObject; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::types::list::new_from_iter; -use crate::{Bound, PyAny, PyErr, PyObject, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[allow(deprecated)] -impl ToPyObject for [T] -where - T: ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - let mut iter = self.iter().map(|e| e.to_object(py)); - let list = new_from_iter(py, &mut iter); - list.into() - } -} - -#[allow(deprecated)] -impl ToPyObject for Vec -where - T: ToPyObject, -{ - fn to_object(&self, py: Python<'_>) -> PyObject { - self.as_slice().to_object(py) - } -} - -#[allow(deprecated)] -impl IntoPy for Vec -where - T: IntoPy, -{ - fn into_py(self, py: Python<'_>) -> PyObject { - let mut iter = self.into_iter().map(|e| e.into_py(py)); - let list = new_from_iter(py, &mut iter); - list.into() - } -} - -impl<'py, T> IntoPyObject<'py> for Vec -where - T: IntoPyObject<'py>, -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - /// Turns [`Vec`] into [`PyBytes`], all other `T`s will be turned into a [`PyList`] - /// - /// [`PyBytes`]: crate::types::PyBytes - /// [`PyList`]: crate::types::PyList - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - T::owned_sequence_into_pyobject(self, py, crate::conversion::private::Token) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::list_of(T::type_output()) - } -} - -impl<'a, 'py, T> IntoPyObject<'py> for &'a Vec -where - &'a T: IntoPyObject<'py>, - T: 'a, // MSRV -{ - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - // NB: we could actually not cast to `PyAny`, which would be nice for - // `&Vec`, but that'd be inconsistent with the `IntoPyObject` impl - // above which always returns a `PyAny` for `Vec`. - self.as_slice().into_pyobject(py).map(Bound::into_any) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::list_of(<&T>::type_output()) - } -} - -#[cfg(test)] -mod tests { - use crate::conversion::IntoPyObject; - use crate::types::{PyAnyMethods, PyBytes, PyBytesMethods, PyList}; - use crate::Python; - - #[test] - fn test_vec_intopyobject_impl() { - Python::with_gil(|py| { - let bytes: Vec = b"foobar".to_vec(); - let obj = bytes.clone().into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &bytes); - - let nums: Vec = vec![0, 1, 2, 3]; - let obj = nums.into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } - - #[test] - fn test_vec_reference_intopyobject_impl() { - Python::with_gil(|py| { - let bytes: Vec = b"foobar".to_vec(); - let obj = (&bytes).into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - let obj = obj.downcast_into::().unwrap(); - assert_eq!(obj.as_bytes(), &bytes); - - let nums: Vec = vec![0, 1, 2, 3]; - let obj = (&nums).into_pyobject(py).unwrap(); - assert!(obj.is_instance_of::()); - }); - } -} diff --git a/vendor/pyo3/src/coroutine.rs b/vendor/pyo3/src/coroutine.rs deleted file mode 100644 index 671defb177..0000000000 --- a/vendor/pyo3/src/coroutine.rs +++ /dev/null @@ -1,169 +0,0 @@ -//! Python coroutine implementation, used notably when wrapping `async fn` -//! with `#[pyfunction]`/`#[pymethods]`. -use std::{ - future::Future, - panic, - pin::Pin, - sync::Arc, - task::{Context, Poll, Waker}, -}; - -use pyo3_macros::{pyclass, pymethods}; - -use crate::{ - coroutine::{cancel::ThrowCallback, waker::AsyncioWaker}, - exceptions::{PyAttributeError, PyRuntimeError, PyStopIteration}, - panic::PanicException, - types::{string::PyStringMethods, PyIterator, PyString}, - Bound, IntoPyObject, IntoPyObjectExt, Py, PyAny, PyErr, PyObject, PyResult, Python, -}; - -pub(crate) mod cancel; -mod waker; - -pub use cancel::CancelHandle; - -const COROUTINE_REUSED_ERROR: &str = "cannot reuse already awaited coroutine"; - -/// Python coroutine wrapping a [`Future`]. -#[pyclass(crate = "crate")] -pub struct Coroutine { - name: Option>, - qualname_prefix: Option<&'static str>, - throw_callback: Option, - future: Option> + Send>>>, - waker: Option>, -} - -// Safety: `Coroutine` is allowed to be `Sync` even though the future is not, -// because the future is polled with `&mut self` receiver -unsafe impl Sync for Coroutine {} - -impl Coroutine { - /// Wrap a future into a Python coroutine. - /// - /// Coroutine `send` polls the wrapped future, ignoring the value passed - /// (should always be `None` anyway). - /// - /// `Coroutine `throw` drop the wrapped future and reraise the exception passed - pub(crate) fn new<'py, F, T, E>( - name: Option>, - qualname_prefix: Option<&'static str>, - throw_callback: Option, - future: F, - ) -> Self - where - F: Future> + Send + 'static, - T: IntoPyObject<'py>, - E: Into, - { - let wrap = async move { - let obj = future.await.map_err(Into::into)?; - // SAFETY: GIL is acquired when future is polled (see `Coroutine::poll`) - obj.into_py_any(unsafe { Python::assume_gil_acquired() }) - }; - Self { - name: name.map(Bound::unbind), - qualname_prefix, - throw_callback, - future: Some(Box::pin(wrap)), - waker: None, - } - } - - fn poll(&mut self, py: Python<'_>, throw: Option) -> PyResult { - // raise if the coroutine has already been run to completion - let future_rs = match self.future { - Some(ref mut fut) => fut, - None => return Err(PyRuntimeError::new_err(COROUTINE_REUSED_ERROR)), - }; - // reraise thrown exception it - match (throw, &self.throw_callback) { - (Some(exc), Some(cb)) => cb.throw(exc), - (Some(exc), None) => { - self.close(); - return Err(PyErr::from_value(exc.into_bound(py))); - } - (None, _) => {} - } - // create a new waker, or try to reset it in place - if let Some(waker) = self.waker.as_mut().and_then(Arc::get_mut) { - waker.reset(); - } else { - self.waker = Some(Arc::new(AsyncioWaker::new())); - } - let waker = Waker::from(self.waker.clone().unwrap()); - // poll the Rust future and forward its results if ready - // polling is UnwindSafe because the future is dropped in case of panic - let poll = || future_rs.as_mut().poll(&mut Context::from_waker(&waker)); - match panic::catch_unwind(panic::AssertUnwindSafe(poll)) { - Ok(Poll::Ready(res)) => { - self.close(); - return Err(PyStopIteration::new_err((res?,))); - } - Err(err) => { - self.close(); - return Err(PanicException::from_panic_payload(err)); - } - _ => {} - } - // otherwise, initialize the waker `asyncio.Future` - if let Some(future) = self.waker.as_ref().unwrap().initialize_future(py)? { - // `asyncio.Future` must be awaited; fortunately, it implements `__iter__ = __await__` - // and will yield itself if its result has not been set in polling above - if let Some(future) = PyIterator::from_object(future).unwrap().next() { - // future has not been leaked into Python for now, and Rust code can only call - // `set_result(None)` in `Wake` implementation, so it's safe to unwrap - return Ok(future.unwrap().into()); - } - } - // if waker has been waken during future polling, this is roughly equivalent to - // `await asyncio.sleep(0)`, so just yield `None`. - Ok(py.None()) - } -} - -#[pymethods(crate = "crate")] -impl Coroutine { - #[getter] - fn __name__(&self, py: Python<'_>) -> PyResult> { - match &self.name { - Some(name) => Ok(name.clone_ref(py)), - None => Err(PyAttributeError::new_err("__name__")), - } - } - - #[getter] - fn __qualname__<'py>(&self, py: Python<'py>) -> PyResult> { - match (&self.name, &self.qualname_prefix) { - (Some(name), Some(prefix)) => Ok(PyString::new( - py, - &format!("{}.{}", prefix, name.bind(py).to_cow()?), - )), - (Some(name), None) => Ok(name.bind(py).clone()), - (None, _) => Err(PyAttributeError::new_err("__qualname__")), - } - } - - fn send(&mut self, py: Python<'_>, _value: &Bound<'_, PyAny>) -> PyResult { - self.poll(py, None) - } - - fn throw(&mut self, py: Python<'_>, exc: PyObject) -> PyResult { - self.poll(py, Some(exc)) - } - - fn close(&mut self) { - // the Rust future is dropped, and the field set to `None` - // to indicate the coroutine has been run to completion - drop(self.future.take()); - } - - fn __await__(self_: Py) -> Py { - self_ - } - - fn __next__(&mut self, py: Python<'_>) -> PyResult { - self.poll(py, None) - } -} diff --git a/vendor/pyo3/src/coroutine/cancel.rs b/vendor/pyo3/src/coroutine/cancel.rs deleted file mode 100644 index 47f5d69430..0000000000 --- a/vendor/pyo3/src/coroutine/cancel.rs +++ /dev/null @@ -1,77 +0,0 @@ -use crate::{Py, PyAny, PyObject}; -use std::future::Future; -use std::pin::Pin; -use std::sync::{Arc, Mutex}; -use std::task::{Context, Poll, Waker}; - -#[derive(Debug, Default)] -struct Inner { - exception: Option, - waker: Option, -} - -/// Helper used to wait and retrieve exception thrown in [`Coroutine`](super::Coroutine). -/// -/// Only the last exception thrown can be retrieved. -#[derive(Debug, Default)] -pub struct CancelHandle(Arc>); - -impl CancelHandle { - /// Create a new `CoroutineCancel`. - pub fn new() -> Self { - Default::default() - } - - /// Returns whether the associated coroutine has been cancelled. - pub fn is_cancelled(&self) -> bool { - self.0.lock().unwrap().exception.is_some() - } - - /// Poll to retrieve the exception thrown in the associated coroutine. - pub fn poll_cancelled(&mut self, cx: &mut Context<'_>) -> Poll { - let mut inner = self.0.lock().unwrap(); - if let Some(exc) = inner.exception.take() { - return Poll::Ready(exc); - } - if let Some(ref waker) = inner.waker { - if cx.waker().will_wake(waker) { - return Poll::Pending; - } - } - inner.waker = Some(cx.waker().clone()); - Poll::Pending - } - - /// Retrieve the exception thrown in the associated coroutine. - pub async fn cancelled(&mut self) -> PyObject { - Cancelled(self).await - } - - #[doc(hidden)] - pub fn throw_callback(&self) -> ThrowCallback { - ThrowCallback(self.0.clone()) - } -} - -// Because `poll_fn` is not available in MSRV -struct Cancelled<'a>(&'a mut CancelHandle); - -impl Future for Cancelled<'_> { - type Output = PyObject; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - self.0.poll_cancelled(cx) - } -} - -#[doc(hidden)] -pub struct ThrowCallback(Arc>); - -impl ThrowCallback { - pub(super) fn throw(&self, exc: Py) { - let mut inner = self.0.lock().unwrap(); - inner.exception = Some(exc); - if let Some(waker) = inner.waker.take() { - waker.wake(); - } - } -} diff --git a/vendor/pyo3/src/coroutine/waker.rs b/vendor/pyo3/src/coroutine/waker.rs deleted file mode 100644 index 1600f56d9c..0000000000 --- a/vendor/pyo3/src/coroutine/waker.rs +++ /dev/null @@ -1,106 +0,0 @@ -use crate::sync::GILOnceCell; -use crate::types::any::PyAnyMethods; -use crate::types::PyCFunction; -use crate::{intern, wrap_pyfunction, Bound, Py, PyAny, PyObject, PyResult, Python}; -use pyo3_macros::pyfunction; -use std::sync::Arc; -use std::task::Wake; - -/// Lazy `asyncio.Future` wrapper, implementing [`Wake`] by calling `Future.set_result`. -/// -/// asyncio future is let uninitialized until [`initialize_future`][1] is called. -/// If [`wake`][2] is called before future initialization (during Rust future polling), -/// [`initialize_future`][1] will return `None` (it is roughly equivalent to `asyncio.sleep(0)`) -/// -/// [1]: AsyncioWaker::initialize_future -/// [2]: AsyncioWaker::wake -pub struct AsyncioWaker(GILOnceCell>); - -impl AsyncioWaker { - pub(super) fn new() -> Self { - Self(GILOnceCell::new()) - } - - pub(super) fn reset(&mut self) { - self.0.take(); - } - - pub(super) fn initialize_future<'py>( - &self, - py: Python<'py>, - ) -> PyResult>> { - let init = || LoopAndFuture::new(py).map(Some); - let loop_and_future = self.0.get_or_try_init(py, init)?.as_ref(); - Ok(loop_and_future.map(|LoopAndFuture { future, .. }| future.bind(py))) - } -} - -impl Wake for AsyncioWaker { - fn wake(self: Arc) { - self.wake_by_ref() - } - - fn wake_by_ref(self: &Arc) { - Python::with_gil(|gil| { - if let Some(loop_and_future) = self.0.get_or_init(gil, || None) { - loop_and_future - .set_result(gil) - .expect("unexpected error in coroutine waker"); - } - }); - } -} - -struct LoopAndFuture { - event_loop: PyObject, - future: PyObject, -} - -impl LoopAndFuture { - fn new(py: Python<'_>) -> PyResult { - static GET_RUNNING_LOOP: GILOnceCell = GILOnceCell::new(); - let import = || -> PyResult<_> { - let module = py.import("asyncio")?; - Ok(module.getattr("get_running_loop")?.into()) - }; - let event_loop = GET_RUNNING_LOOP.get_or_try_init(py, import)?.call0(py)?; - let future = event_loop.call_method0(py, "create_future")?; - Ok(Self { event_loop, future }) - } - - fn set_result(&self, py: Python<'_>) -> PyResult<()> { - static RELEASE_WAITER: GILOnceCell> = GILOnceCell::new(); - let release_waiter = RELEASE_WAITER.get_or_try_init(py, || { - wrap_pyfunction!(release_waiter, py).map(Bound::unbind) - })?; - // `Future.set_result` must be called in event loop thread, - // so it requires `call_soon_threadsafe` - let call_soon_threadsafe = self.event_loop.call_method1( - py, - intern!(py, "call_soon_threadsafe"), - (release_waiter, self.future.bind(py)), - ); - if let Err(err) = call_soon_threadsafe { - // `call_soon_threadsafe` will raise if the event loop is closed; - // instead of catching an unspecific `RuntimeError`, check directly if it's closed. - let is_closed = self.event_loop.call_method0(py, "is_closed")?; - if !is_closed.extract(py)? { - return Err(err); - } - } - Ok(()) - } -} - -/// Call `future.set_result` if the future is not done. -/// -/// Future can be cancelled by the event loop before being waken. -/// See -#[pyfunction(crate = "crate")] -fn release_waiter(future: &Bound<'_, PyAny>) -> PyResult<()> { - let done = future.call_method0(intern!(future.py(), "done"))?; - if !done.extract::()? { - future.call_method1(intern!(future.py(), "set_result"), (future.py().None(),))?; - } - Ok(()) -} diff --git a/vendor/pyo3/src/err/err_state.rs b/vendor/pyo3/src/err/err_state.rs deleted file mode 100644 index 98be633e91..0000000000 --- a/vendor/pyo3/src/err/err_state.rs +++ /dev/null @@ -1,440 +0,0 @@ -use std::{ - cell::UnsafeCell, - sync::{Mutex, Once}, - thread::ThreadId, -}; - -use crate::{ - exceptions::{PyBaseException, PyTypeError}, - ffi, - ffi_ptr_ext::FfiPtrExt, - types::{PyAnyMethods, PyTraceback, PyType}, - Bound, Py, PyAny, PyErrArguments, PyObject, PyTypeInfo, Python, -}; - -pub(crate) struct PyErrState { - // Safety: can only hand out references when in the "normalized" state. Will never change - // after normalization. - normalized: Once, - // Guard against re-entrancy when normalizing the exception state. - normalizing_thread: Mutex>, - inner: UnsafeCell>, -} - -// Safety: The inner value is protected by locking to ensure that only the normalized state is -// handed out as a reference. -unsafe impl Send for PyErrState {} -unsafe impl Sync for PyErrState {} -#[cfg(feature = "nightly")] -unsafe impl crate::marker::Ungil for PyErrState {} - -impl PyErrState { - pub(crate) fn lazy(f: Box) -> Self { - Self::from_inner(PyErrStateInner::Lazy(f)) - } - - pub(crate) fn lazy_arguments(ptype: Py, args: impl PyErrArguments + 'static) -> Self { - Self::from_inner(PyErrStateInner::Lazy(Box::new(move |py| { - PyErrStateLazyFnOutput { - ptype, - pvalue: args.arguments(py), - } - }))) - } - - pub(crate) fn normalized(normalized: PyErrStateNormalized) -> Self { - let state = Self::from_inner(PyErrStateInner::Normalized(normalized)); - // This state is already normalized, by completing the Once immediately we avoid - // reaching the `py.allow_threads` in `make_normalized` which is less efficient - // and introduces a GIL switch which could deadlock. - // See https://github.com/PyO3/pyo3/issues/4764 - state.normalized.call_once(|| {}); - state - } - - pub(crate) fn restore(self, py: Python<'_>) { - self.inner - .into_inner() - .expect("PyErr state should never be invalid outside of normalization") - .restore(py) - } - - fn from_inner(inner: PyErrStateInner) -> Self { - Self { - normalized: Once::new(), - normalizing_thread: Mutex::new(None), - inner: UnsafeCell::new(Some(inner)), - } - } - - #[inline] - pub(crate) fn as_normalized(&self, py: Python<'_>) -> &PyErrStateNormalized { - if self.normalized.is_completed() { - match unsafe { - // Safety: self.inner will never be written again once normalized. - &*self.inner.get() - } { - Some(PyErrStateInner::Normalized(n)) => return n, - _ => unreachable!(), - } - } - - self.make_normalized(py) - } - - #[cold] - fn make_normalized(&self, py: Python<'_>) -> &PyErrStateNormalized { - // This process is safe because: - // - Access is guaranteed not to be concurrent thanks to `Python` GIL token - // - Write happens only once, and then never will change again. - - // Guard against re-entrant normalization, because `Once` does not provide - // re-entrancy guarantees. - if let Some(thread) = self.normalizing_thread.lock().unwrap().as_ref() { - assert!( - !(*thread == std::thread::current().id()), - "Re-entrant normalization of PyErrState detected" - ); - } - - // avoid deadlock of `.call_once` with the GIL - py.allow_threads(|| { - self.normalized.call_once(|| { - self.normalizing_thread - .lock() - .unwrap() - .replace(std::thread::current().id()); - - // Safety: no other thread can access the inner value while we are normalizing it. - let state = unsafe { - (*self.inner.get()) - .take() - .expect("Cannot normalize a PyErr while already normalizing it.") - }; - - let normalized_state = - Python::with_gil(|py| PyErrStateInner::Normalized(state.normalize(py))); - - // Safety: no other thread can access the inner value while we are normalizing it. - unsafe { - *self.inner.get() = Some(normalized_state); - } - }) - }); - - match unsafe { - // Safety: self.inner will never be written again once normalized. - &*self.inner.get() - } { - Some(PyErrStateInner::Normalized(n)) => n, - _ => unreachable!(), - } - } -} - -pub(crate) struct PyErrStateNormalized { - #[cfg(not(Py_3_12))] - ptype: Py, - pub pvalue: Py, - #[cfg(not(Py_3_12))] - ptraceback: Option>, -} - -impl PyErrStateNormalized { - pub(crate) fn new(pvalue: Bound<'_, PyBaseException>) -> Self { - Self { - #[cfg(not(Py_3_12))] - ptype: pvalue.get_type().into(), - #[cfg(not(Py_3_12))] - ptraceback: unsafe { - Py::from_owned_ptr_or_opt( - pvalue.py(), - ffi::PyException_GetTraceback(pvalue.as_ptr()), - ) - }, - pvalue: pvalue.into(), - } - } - - #[cfg(not(Py_3_12))] - pub(crate) fn ptype<'py>(&self, py: Python<'py>) -> Bound<'py, PyType> { - self.ptype.bind(py).clone() - } - - #[cfg(Py_3_12)] - pub(crate) fn ptype<'py>(&self, py: Python<'py>) -> Bound<'py, PyType> { - self.pvalue.bind(py).get_type() - } - - #[cfg(not(Py_3_12))] - pub(crate) fn ptraceback<'py>(&self, py: Python<'py>) -> Option> { - self.ptraceback - .as_ref() - .map(|traceback| traceback.bind(py).clone()) - } - - #[cfg(Py_3_12)] - pub(crate) fn ptraceback<'py>(&self, py: Python<'py>) -> Option> { - unsafe { - ffi::PyException_GetTraceback(self.pvalue.as_ptr()) - .assume_owned_or_opt(py) - .map(|b| b.downcast_into_unchecked()) - } - } - - pub(crate) fn take(py: Python<'_>) -> Option { - #[cfg(Py_3_12)] - { - // Safety: PyErr_GetRaisedException can be called when attached to Python and - // returns either NULL or an owned reference. - unsafe { ffi::PyErr_GetRaisedException().assume_owned_or_opt(py) }.map(|pvalue| { - PyErrStateNormalized { - // Safety: PyErr_GetRaisedException returns a valid exception type. - pvalue: unsafe { pvalue.downcast_into_unchecked() }.unbind(), - } - }) - } - - #[cfg(not(Py_3_12))] - { - let (ptype, pvalue, ptraceback) = unsafe { - let mut ptype: *mut ffi::PyObject = std::ptr::null_mut(); - let mut pvalue: *mut ffi::PyObject = std::ptr::null_mut(); - let mut ptraceback: *mut ffi::PyObject = std::ptr::null_mut(); - - ffi::PyErr_Fetch(&mut ptype, &mut pvalue, &mut ptraceback); - - // Ensure that the exception coming from the interpreter is normalized. - if !ptype.is_null() { - ffi::PyErr_NormalizeException(&mut ptype, &mut pvalue, &mut ptraceback); - } - - // Safety: PyErr_NormalizeException will have produced up to three owned - // references of the correct types. - ( - ptype - .assume_owned_or_opt(py) - .map(|b| b.downcast_into_unchecked()), - pvalue - .assume_owned_or_opt(py) - .map(|b| b.downcast_into_unchecked()), - ptraceback - .assume_owned_or_opt(py) - .map(|b| b.downcast_into_unchecked()), - ) - }; - - ptype.map(|ptype| PyErrStateNormalized { - ptype: ptype.unbind(), - pvalue: pvalue.expect("normalized exception value missing").unbind(), - ptraceback: ptraceback.map(Bound::unbind), - }) - } - } - - #[cfg(not(Py_3_12))] - unsafe fn from_normalized_ffi_tuple( - py: Python<'_>, - ptype: *mut ffi::PyObject, - pvalue: *mut ffi::PyObject, - ptraceback: *mut ffi::PyObject, - ) -> Self { - PyErrStateNormalized { - ptype: Py::from_owned_ptr_or_opt(py, ptype).expect("Exception type missing"), - pvalue: Py::from_owned_ptr_or_opt(py, pvalue).expect("Exception value missing"), - ptraceback: Py::from_owned_ptr_or_opt(py, ptraceback), - } - } - - pub fn clone_ref(&self, py: Python<'_>) -> Self { - Self { - #[cfg(not(Py_3_12))] - ptype: self.ptype.clone_ref(py), - pvalue: self.pvalue.clone_ref(py), - #[cfg(not(Py_3_12))] - ptraceback: self - .ptraceback - .as_ref() - .map(|ptraceback| ptraceback.clone_ref(py)), - } - } -} - -pub(crate) struct PyErrStateLazyFnOutput { - pub(crate) ptype: PyObject, - pub(crate) pvalue: PyObject, -} - -pub(crate) type PyErrStateLazyFn = - dyn for<'py> FnOnce(Python<'py>) -> PyErrStateLazyFnOutput + Send + Sync; - -enum PyErrStateInner { - Lazy(Box), - Normalized(PyErrStateNormalized), -} - -impl PyErrStateInner { - fn normalize(self, py: Python<'_>) -> PyErrStateNormalized { - match self { - #[cfg(not(Py_3_12))] - PyErrStateInner::Lazy(lazy) => { - let (ptype, pvalue, ptraceback) = lazy_into_normalized_ffi_tuple(py, lazy); - unsafe { - PyErrStateNormalized::from_normalized_ffi_tuple(py, ptype, pvalue, ptraceback) - } - } - #[cfg(Py_3_12)] - PyErrStateInner::Lazy(lazy) => { - // To keep the implementation simple, just write the exception into the interpreter, - // which will cause it to be normalized - raise_lazy(py, lazy); - PyErrStateNormalized::take(py) - .expect("exception missing after writing to the interpreter") - } - PyErrStateInner::Normalized(normalized) => normalized, - } - } - - #[cfg(not(Py_3_12))] - fn restore(self, py: Python<'_>) { - let (ptype, pvalue, ptraceback) = match self { - PyErrStateInner::Lazy(lazy) => lazy_into_normalized_ffi_tuple(py, lazy), - PyErrStateInner::Normalized(PyErrStateNormalized { - ptype, - pvalue, - ptraceback, - }) => ( - ptype.into_ptr(), - pvalue.into_ptr(), - ptraceback.map_or(std::ptr::null_mut(), Py::into_ptr), - ), - }; - unsafe { ffi::PyErr_Restore(ptype, pvalue, ptraceback) } - } - - #[cfg(Py_3_12)] - fn restore(self, py: Python<'_>) { - match self { - PyErrStateInner::Lazy(lazy) => raise_lazy(py, lazy), - PyErrStateInner::Normalized(PyErrStateNormalized { pvalue }) => unsafe { - ffi::PyErr_SetRaisedException(pvalue.into_ptr()) - }, - } - } -} - -#[cfg(not(Py_3_12))] -fn lazy_into_normalized_ffi_tuple( - py: Python<'_>, - lazy: Box, -) -> (*mut ffi::PyObject, *mut ffi::PyObject, *mut ffi::PyObject) { - // To be consistent with 3.12 logic, go via raise_lazy, but also then normalize - // the resulting exception - raise_lazy(py, lazy); - let mut ptype = std::ptr::null_mut(); - let mut pvalue = std::ptr::null_mut(); - let mut ptraceback = std::ptr::null_mut(); - unsafe { - ffi::PyErr_Fetch(&mut ptype, &mut pvalue, &mut ptraceback); - ffi::PyErr_NormalizeException(&mut ptype, &mut pvalue, &mut ptraceback); - } - (ptype, pvalue, ptraceback) -} - -/// Raises a "lazy" exception state into the Python interpreter. -/// -/// In principle this could be split in two; first a function to create an exception -/// in a normalized state, and then a call to `PyErr_SetRaisedException` to raise it. -/// -/// This would require either moving some logic from C to Rust, or requesting a new -/// API in CPython. -fn raise_lazy(py: Python<'_>, lazy: Box) { - let PyErrStateLazyFnOutput { ptype, pvalue } = lazy(py); - unsafe { - if ffi::PyExceptionClass_Check(ptype.as_ptr()) == 0 { - ffi::PyErr_SetString( - PyTypeError::type_object_raw(py).cast(), - ffi::c_str!("exceptions must derive from BaseException").as_ptr(), - ) - } else { - ffi::PyErr_SetObject(ptype.as_ptr(), pvalue.as_ptr()) - } - } -} - -#[cfg(test)] -mod tests { - - use crate::{ - exceptions::PyValueError, sync::GILOnceCell, PyErr, PyErrArguments, PyObject, Python, - }; - - #[test] - #[should_panic(expected = "Re-entrant normalization of PyErrState detected")] - fn test_reentrant_normalization() { - static ERR: GILOnceCell = GILOnceCell::new(); - - struct RecursiveArgs; - - impl PyErrArguments for RecursiveArgs { - fn arguments(self, py: Python<'_>) -> PyObject { - // .value(py) triggers normalization - ERR.get(py) - .expect("is set just below") - .value(py) - .clone() - .into() - } - } - - Python::with_gil(|py| { - ERR.set(py, PyValueError::new_err(RecursiveArgs)).unwrap(); - ERR.get(py).expect("is set just above").value(py); - }) - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled - fn test_no_deadlock_thread_switch() { - static ERR: GILOnceCell = GILOnceCell::new(); - - struct GILSwitchArgs; - - impl PyErrArguments for GILSwitchArgs { - fn arguments(self, py: Python<'_>) -> PyObject { - // releasing the GIL potentially allows for other threads to deadlock - // with the normalization going on here - py.allow_threads(|| { - std::thread::sleep(std::time::Duration::from_millis(10)); - }); - py.None() - } - } - - Python::with_gil(|py| ERR.set(py, PyValueError::new_err(GILSwitchArgs)).unwrap()); - - // Let many threads attempt to read the normalized value at the same time - let handles = (0..10) - .map(|_| { - std::thread::spawn(|| { - Python::with_gil(|py| { - ERR.get(py).expect("is set just above").value(py); - }); - }) - }) - .collect::>(); - - for handle in handles { - handle.join().unwrap(); - } - - // We should never have deadlocked, and should be able to run - // this assertion - Python::with_gil(|py| { - assert!(ERR - .get(py) - .expect("is set above") - .is_instance_of::(py)) - }); - } -} diff --git a/vendor/pyo3/src/err/impls.rs b/vendor/pyo3/src/err/impls.rs deleted file mode 100644 index b84f46d430..0000000000 --- a/vendor/pyo3/src/err/impls.rs +++ /dev/null @@ -1,171 +0,0 @@ -use crate::IntoPyObject; -use crate::{err::PyErrArguments, exceptions, PyErr, PyObject, Python}; -use std::io; - -/// Convert `PyErr` to `io::Error` -impl From for io::Error { - fn from(err: PyErr) -> Self { - let kind = Python::with_gil(|py| { - if err.is_instance_of::(py) { - io::ErrorKind::BrokenPipe - } else if err.is_instance_of::(py) { - io::ErrorKind::ConnectionRefused - } else if err.is_instance_of::(py) { - io::ErrorKind::ConnectionAborted - } else if err.is_instance_of::(py) { - io::ErrorKind::ConnectionReset - } else if err.is_instance_of::(py) { - io::ErrorKind::Interrupted - } else if err.is_instance_of::(py) { - io::ErrorKind::NotFound - } else if err.is_instance_of::(py) { - io::ErrorKind::PermissionDenied - } else if err.is_instance_of::(py) { - io::ErrorKind::AlreadyExists - } else if err.is_instance_of::(py) { - io::ErrorKind::WouldBlock - } else if err.is_instance_of::(py) { - io::ErrorKind::TimedOut - } else { - io::ErrorKind::Other - } - }); - io::Error::new(kind, err) - } -} - -/// Create `PyErr` from `io::Error` -/// (`OSError` except if the `io::Error` is wrapping a Python exception, -/// in this case the exception is returned) -impl From for PyErr { - fn from(err: io::Error) -> PyErr { - // If the error wraps a Python error we return it - if err.get_ref().map_or(false, |e| e.is::()) { - return *err.into_inner().unwrap().downcast().unwrap(); - } - match err.kind() { - io::ErrorKind::BrokenPipe => exceptions::PyBrokenPipeError::new_err(err), - io::ErrorKind::ConnectionRefused => exceptions::PyConnectionRefusedError::new_err(err), - io::ErrorKind::ConnectionAborted => exceptions::PyConnectionAbortedError::new_err(err), - io::ErrorKind::ConnectionReset => exceptions::PyConnectionResetError::new_err(err), - io::ErrorKind::Interrupted => exceptions::PyInterruptedError::new_err(err), - io::ErrorKind::NotFound => exceptions::PyFileNotFoundError::new_err(err), - io::ErrorKind::PermissionDenied => exceptions::PyPermissionError::new_err(err), - io::ErrorKind::AlreadyExists => exceptions::PyFileExistsError::new_err(err), - io::ErrorKind::WouldBlock => exceptions::PyBlockingIOError::new_err(err), - io::ErrorKind::TimedOut => exceptions::PyTimeoutError::new_err(err), - _ => exceptions::PyOSError::new_err(err), - } - } -} - -impl PyErrArguments for io::Error { - fn arguments(self, py: Python<'_>) -> PyObject { - //FIXME(icxolu) remove unwrap - self.to_string() - .into_pyobject(py) - .unwrap() - .into_any() - .unbind() - } -} - -impl From> for PyErr { - fn from(err: io::IntoInnerError) -> PyErr { - err.into_error().into() - } -} - -impl PyErrArguments for io::IntoInnerError { - fn arguments(self, py: Python<'_>) -> PyObject { - self.into_error().arguments(py) - } -} - -impl From for PyErr { - fn from(_: std::convert::Infallible) -> PyErr { - unreachable!() - } -} - -macro_rules! impl_to_pyerr { - ($err: ty, $pyexc: ty) => { - impl PyErrArguments for $err { - fn arguments(self, py: Python<'_>) -> PyObject { - // FIXME(icxolu) remove unwrap - self.to_string() - .into_pyobject(py) - .unwrap() - .into_any() - .unbind() - } - } - - impl std::convert::From<$err> for PyErr { - fn from(err: $err) -> PyErr { - <$pyexc>::new_err(err) - } - } - }; -} - -impl_to_pyerr!(std::array::TryFromSliceError, exceptions::PyValueError); -impl_to_pyerr!(std::num::ParseIntError, exceptions::PyValueError); -impl_to_pyerr!(std::num::ParseFloatError, exceptions::PyValueError); -impl_to_pyerr!(std::num::TryFromIntError, exceptions::PyValueError); -impl_to_pyerr!(std::str::ParseBoolError, exceptions::PyValueError); -impl_to_pyerr!(std::ffi::IntoStringError, exceptions::PyUnicodeDecodeError); -impl_to_pyerr!(std::ffi::NulError, exceptions::PyValueError); -impl_to_pyerr!(std::str::Utf8Error, exceptions::PyUnicodeDecodeError); -impl_to_pyerr!(std::string::FromUtf8Error, exceptions::PyUnicodeDecodeError); -impl_to_pyerr!( - std::string::FromUtf16Error, - exceptions::PyUnicodeDecodeError -); -impl_to_pyerr!( - std::char::DecodeUtf16Error, - exceptions::PyUnicodeDecodeError -); -impl_to_pyerr!(std::net::AddrParseError, exceptions::PyValueError); - -#[cfg(test)] -mod tests { - use crate::{PyErr, Python}; - use std::io; - - #[test] - fn io_errors() { - use crate::types::any::PyAnyMethods; - - let check_err = |kind, expected_ty| { - Python::with_gil(|py| { - let rust_err = io::Error::new(kind, "some error msg"); - - let py_err: PyErr = rust_err.into(); - let py_err_msg = format!("{}: some error msg", expected_ty); - assert_eq!(py_err.to_string(), py_err_msg); - let py_error_clone = py_err.clone_ref(py); - - let rust_err_from_py_err: io::Error = py_err.into(); - assert_eq!(rust_err_from_py_err.to_string(), py_err_msg); - assert_eq!(rust_err_from_py_err.kind(), kind); - - let py_err_recovered_from_rust_err: PyErr = rust_err_from_py_err.into(); - assert!(py_err_recovered_from_rust_err - .value(py) - .is(py_error_clone.value(py))); // It should be the same exception - }) - }; - - check_err(io::ErrorKind::BrokenPipe, "BrokenPipeError"); - check_err(io::ErrorKind::ConnectionRefused, "ConnectionRefusedError"); - check_err(io::ErrorKind::ConnectionAborted, "ConnectionAbortedError"); - check_err(io::ErrorKind::ConnectionReset, "ConnectionResetError"); - check_err(io::ErrorKind::Interrupted, "InterruptedError"); - check_err(io::ErrorKind::NotFound, "FileNotFoundError"); - check_err(io::ErrorKind::PermissionDenied, "PermissionError"); - check_err(io::ErrorKind::AlreadyExists, "FileExistsError"); - check_err(io::ErrorKind::WouldBlock, "BlockingIOError"); - check_err(io::ErrorKind::TimedOut, "TimeoutError"); - } -} diff --git a/vendor/pyo3/src/err/mod.rs b/vendor/pyo3/src/err/mod.rs deleted file mode 100644 index 20108f6e8d..0000000000 --- a/vendor/pyo3/src/err/mod.rs +++ /dev/null @@ -1,1226 +0,0 @@ -use crate::instance::Bound; -use crate::panic::PanicException; -use crate::type_object::PyTypeInfo; -use crate::types::any::PyAnyMethods; -use crate::types::{string::PyStringMethods, typeobject::PyTypeMethods, PyTraceback, PyType}; -use crate::{ - exceptions::{self, PyBaseException}, - ffi, -}; -use crate::{Borrowed, BoundObject, Py, PyAny, PyObject, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::borrow::Cow; -use std::ffi::{CStr, CString}; - -mod err_state; -mod impls; - -use crate::conversion::IntoPyObject; -use err_state::{PyErrState, PyErrStateLazyFnOutput, PyErrStateNormalized}; -use std::convert::Infallible; - -/// Represents a Python exception. -/// -/// To avoid needing access to [`Python`] in `Into` conversions to create `PyErr` (thus improving -/// compatibility with `?` and other Rust errors) this type supports creating exceptions instances -/// in a lazy fashion, where the full Python object for the exception is created only when needed. -/// -/// Accessing the contained exception in any way, such as with [`value_bound`](PyErr::value_bound), -/// [`get_type_bound`](PyErr::get_type_bound), or [`is_instance_bound`](PyErr::is_instance_bound) -/// will create the full exception object if it was not already created. -pub struct PyErr { - state: PyErrState, -} - -// The inner value is only accessed through ways that require proving the gil is held -#[cfg(feature = "nightly")] -unsafe impl crate::marker::Ungil for PyErr {} - -/// Represents the result of a Python call. -pub type PyResult = Result; - -/// Error that indicates a failure to convert a PyAny to a more specific Python type. -#[derive(Debug)] -pub struct DowncastError<'a, 'py> { - from: Borrowed<'a, 'py, PyAny>, - to: Cow<'static, str>, -} - -impl<'a, 'py> DowncastError<'a, 'py> { - /// Create a new `PyDowncastError` representing a failure to convert the object - /// `from` into the type named in `to`. - pub fn new(from: &'a Bound<'py, PyAny>, to: impl Into>) -> Self { - DowncastError { - from: from.as_borrowed(), - to: to.into(), - } - } - pub(crate) fn new_from_borrowed( - from: Borrowed<'a, 'py, PyAny>, - to: impl Into>, - ) -> Self { - DowncastError { - from, - to: to.into(), - } - } -} - -/// Error that indicates a failure to convert a PyAny to a more specific Python type. -#[derive(Debug)] -pub struct DowncastIntoError<'py> { - from: Bound<'py, PyAny>, - to: Cow<'static, str>, -} - -impl<'py> DowncastIntoError<'py> { - /// Create a new `DowncastIntoError` representing a failure to convert the object - /// `from` into the type named in `to`. - pub fn new(from: Bound<'py, PyAny>, to: impl Into>) -> Self { - DowncastIntoError { - from, - to: to.into(), - } - } - - /// Consumes this `DowncastIntoError` and returns the original object, allowing continued - /// use of it after a failed conversion. - /// - /// See [`downcast_into`][PyAnyMethods::downcast_into] for an example. - pub fn into_inner(self) -> Bound<'py, PyAny> { - self.from - } -} - -/// Helper conversion trait that allows to use custom arguments for lazy exception construction. -pub trait PyErrArguments: Send + Sync { - /// Arguments for exception - fn arguments(self, py: Python<'_>) -> PyObject; -} - -impl PyErrArguments for T -where - T: for<'py> IntoPyObject<'py> + Send + Sync, -{ - fn arguments(self, py: Python<'_>) -> PyObject { - // FIXME: `arguments` should become fallible - match self.into_pyobject(py) { - Ok(obj) => obj.into_any().unbind(), - Err(e) => panic!("Converting PyErr arguments failed: {}", e.into()), - } - } -} - -impl PyErr { - /// Creates a new PyErr of type `T`. - /// - /// `args` can be: - /// * a tuple: the exception instance will be created using the equivalent to the Python - /// expression `T(*tuple)` - /// * any other value: the exception instance will be created using the equivalent to the Python - /// expression `T(value)` - /// - /// This exception instance will be initialized lazily. This avoids the need for the Python GIL - /// to be held, but requires `args` to be `Send` and `Sync`. If `args` is not `Send` or `Sync`, - /// consider using [`PyErr::from_value_bound`] instead. - /// - /// If `T` does not inherit from `BaseException`, then a `TypeError` will be returned. - /// - /// If calling T's constructor with `args` raises an exception, that exception will be returned. - /// - /// # Examples - /// - /// ``` - /// use pyo3::prelude::*; - /// use pyo3::exceptions::PyTypeError; - /// - /// #[pyfunction] - /// fn always_throws() -> PyResult<()> { - /// Err(PyErr::new::("Error message")) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let fun = pyo3::wrap_pyfunction!(always_throws, py).unwrap(); - /// # let err = fun.call0().expect_err("called a function that should always return an error but the return value was Ok"); - /// # assert!(err.is_instance_of::(py)) - /// # }); - /// ``` - /// - /// In most cases, you can use a concrete exception's constructor instead: - /// - /// ``` - /// use pyo3::prelude::*; - /// use pyo3::exceptions::PyTypeError; - /// - /// #[pyfunction] - /// fn always_throws() -> PyResult<()> { - /// Err(PyTypeError::new_err("Error message")) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let fun = pyo3::wrap_pyfunction!(always_throws, py).unwrap(); - /// # let err = fun.call0().expect_err("called a function that should always return an error but the return value was Ok"); - /// # assert!(err.is_instance_of::(py)) - /// # }); - /// ``` - #[inline] - pub fn new(args: A) -> PyErr - where - T: PyTypeInfo, - A: PyErrArguments + Send + Sync + 'static, - { - PyErr::from_state(PyErrState::lazy(Box::new(move |py| { - PyErrStateLazyFnOutput { - ptype: T::type_object(py).into(), - pvalue: args.arguments(py), - } - }))) - } - - /// Constructs a new PyErr from the given Python type and arguments. - /// - /// `ty` is the exception type; usually one of the standard exceptions - /// like `exceptions::PyRuntimeError`. - /// - /// `args` is either a tuple or a single value, with the same meaning as in [`PyErr::new`]. - /// - /// If `ty` does not inherit from `BaseException`, then a `TypeError` will be returned. - /// - /// If calling `ty` with `args` raises an exception, that exception will be returned. - pub fn from_type(ty: Bound<'_, PyType>, args: A) -> PyErr - where - A: PyErrArguments + Send + Sync + 'static, - { - PyErr::from_state(PyErrState::lazy_arguments(ty.unbind().into_any(), args)) - } - - /// Deprecated name for [`PyErr::from_type`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::from_type`")] - #[inline] - pub fn from_type_bound(ty: Bound<'_, PyType>, args: A) -> PyErr - where - A: PyErrArguments + Send + Sync + 'static, - { - Self::from_type(ty, args) - } - - /// Creates a new PyErr. - /// - /// If `obj` is a Python exception object, the PyErr will contain that object. - /// - /// If `obj` is a Python exception type object, this is equivalent to `PyErr::from_type(obj, ())`. - /// - /// Otherwise, a `TypeError` is created. - /// - /// # Examples - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::PyTypeInfo; - /// use pyo3::exceptions::PyTypeError; - /// use pyo3::types::PyString; - /// - /// Python::with_gil(|py| { - /// // Case #1: Exception object - /// let err = PyErr::from_value(PyTypeError::new_err("some type error") - /// .value(py).clone().into_any()); - /// assert_eq!(err.to_string(), "TypeError: some type error"); - /// - /// // Case #2: Exception type - /// let err = PyErr::from_value(PyTypeError::type_object(py).into_any()); - /// assert_eq!(err.to_string(), "TypeError: "); - /// - /// // Case #3: Invalid exception value - /// let err = PyErr::from_value(PyString::new(py, "foo").into_any()); - /// assert_eq!( - /// err.to_string(), - /// "TypeError: exceptions must derive from BaseException" - /// ); - /// }); - /// ``` - pub fn from_value(obj: Bound<'_, PyAny>) -> PyErr { - let state = match obj.downcast_into::() { - Ok(obj) => PyErrState::normalized(PyErrStateNormalized::new(obj)), - Err(err) => { - // Assume obj is Type[Exception]; let later normalization handle if this - // is not the case - let obj = err.into_inner(); - let py = obj.py(); - PyErrState::lazy_arguments(obj.unbind(), py.None()) - } - }; - - PyErr::from_state(state) - } - - /// Deprecated name for [`PyErr::from_value`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::from_value`")] - #[inline] - pub fn from_value_bound(obj: Bound<'_, PyAny>) -> PyErr { - Self::from_value(obj) - } - - /// Returns the type of this exception. - /// - /// # Examples - /// ```rust - /// use pyo3::{prelude::*, exceptions::PyTypeError, types::PyType}; - /// - /// Python::with_gil(|py| { - /// let err: PyErr = PyTypeError::new_err(("some type error",)); - /// assert!(err.get_type(py).is(&PyType::new::(py))); - /// }); - /// ``` - pub fn get_type<'py>(&self, py: Python<'py>) -> Bound<'py, PyType> { - self.normalized(py).ptype(py) - } - - /// Deprecated name for [`PyErr::get_type`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::get_type`")] - #[inline] - pub fn get_type_bound<'py>(&self, py: Python<'py>) -> Bound<'py, PyType> { - self.get_type(py) - } - - /// Returns the value of this exception. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::{exceptions::PyTypeError, PyErr, Python}; - /// - /// Python::with_gil(|py| { - /// let err: PyErr = PyTypeError::new_err(("some type error",)); - /// assert!(err.is_instance_of::(py)); - /// assert_eq!(err.value(py).to_string(), "some type error"); - /// }); - /// ``` - pub fn value<'py>(&self, py: Python<'py>) -> &Bound<'py, PyBaseException> { - self.normalized(py).pvalue.bind(py) - } - - /// Deprecated name for [`PyErr::value`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::value`")] - #[inline] - pub fn value_bound<'py>(&self, py: Python<'py>) -> &Bound<'py, PyBaseException> { - self.value(py) - } - - /// Consumes self to take ownership of the exception value contained in this error. - pub fn into_value(self, py: Python<'_>) -> Py { - // NB technically this causes one reference count increase and decrease in quick succession - // on pvalue, but it's probably not worth optimizing this right now for the additional code - // complexity. - let normalized = self.normalized(py); - let exc = normalized.pvalue.clone_ref(py); - if let Some(tb) = normalized.ptraceback(py) { - unsafe { - ffi::PyException_SetTraceback(exc.as_ptr(), tb.as_ptr()); - } - } - exc - } - - /// Returns the traceback of this exception object. - /// - /// # Examples - /// ```rust - /// use pyo3::{exceptions::PyTypeError, Python}; - /// - /// Python::with_gil(|py| { - /// let err = PyTypeError::new_err(("some type error",)); - /// assert!(err.traceback(py).is_none()); - /// }); - /// ``` - pub fn traceback<'py>(&self, py: Python<'py>) -> Option> { - self.normalized(py).ptraceback(py) - } - - /// Deprecated name for [`PyErr::traceback`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::traceback`")] - #[inline] - pub fn traceback_bound<'py>(&self, py: Python<'py>) -> Option> { - self.traceback(py) - } - - /// Gets whether an error is present in the Python interpreter's global state. - #[inline] - pub fn occurred(_: Python<'_>) -> bool { - unsafe { !ffi::PyErr_Occurred().is_null() } - } - - /// Takes the current error from the Python interpreter's global state and clears the global - /// state. If no error is set, returns `None`. - /// - /// If the error is a `PanicException` (which would have originated from a panic in a pyo3 - /// callback) then this function will resume the panic. - /// - /// Use this function when it is not known if an error should be present. If the error is - /// expected to have been set, for example from [`PyErr::occurred`] or by an error return value - /// from a C FFI function, use [`PyErr::fetch`]. - pub fn take(py: Python<'_>) -> Option { - let state = PyErrStateNormalized::take(py)?; - let pvalue = state.pvalue.bind(py); - if pvalue.get_type().as_ptr() == PanicException::type_object_raw(py).cast() { - let msg: String = pvalue - .str() - .map(|py_str| py_str.to_string_lossy().into()) - .unwrap_or_else(|_| String::from("Unwrapped panic from Python code")); - Self::print_panic_and_unwind(py, PyErrState::normalized(state), msg) - } - - Some(PyErr::from_state(PyErrState::normalized(state))) - } - - fn print_panic_and_unwind(py: Python<'_>, state: PyErrState, msg: String) -> ! { - eprintln!("--- PyO3 is resuming a panic after fetching a PanicException from Python. ---"); - eprintln!("Python stack trace below:"); - - state.restore(py); - - unsafe { - ffi::PyErr_PrintEx(0); - } - - std::panic::resume_unwind(Box::new(msg)) - } - - /// Equivalent to [PyErr::take], but when no error is set: - /// - Panics in debug mode. - /// - Returns a `SystemError` in release mode. - /// - /// This behavior is consistent with Python's internal handling of what happens when a C return - /// value indicates an error occurred but the global error state is empty. (A lack of exception - /// should be treated as a bug in the code which returned an error code but did not set an - /// exception.) - /// - /// Use this function when the error is expected to have been set, for example from - /// [PyErr::occurred] or by an error return value from a C FFI function. - #[cfg_attr(debug_assertions, track_caller)] - #[inline] - pub fn fetch(py: Python<'_>) -> PyErr { - const FAILED_TO_FETCH: &str = "attempted to fetch exception but none was set"; - match PyErr::take(py) { - Some(err) => err, - #[cfg(debug_assertions)] - None => panic!("{}", FAILED_TO_FETCH), - #[cfg(not(debug_assertions))] - None => exceptions::PySystemError::new_err(FAILED_TO_FETCH), - } - } - - /// Creates a new exception type with the given name and docstring. - /// - /// - `base` can be an existing exception type to subclass, or a tuple of classes. - /// - `dict` specifies an optional dictionary of class variables and methods. - /// - `doc` will be the docstring seen by python users. - /// - /// - /// # Errors - /// - /// This function returns an error if `name` is not of the form `.`. - pub fn new_type<'py>( - py: Python<'py>, - name: &CStr, - doc: Option<&CStr>, - base: Option<&Bound<'py, PyType>>, - dict: Option, - ) -> PyResult> { - let base: *mut ffi::PyObject = match base { - None => std::ptr::null_mut(), - Some(obj) => obj.as_ptr(), - }; - - let dict: *mut ffi::PyObject = match dict { - None => std::ptr::null_mut(), - Some(obj) => obj.as_ptr(), - }; - - let doc_ptr = match doc.as_ref() { - Some(c) => c.as_ptr(), - None => std::ptr::null(), - }; - - let ptr = unsafe { ffi::PyErr_NewExceptionWithDoc(name.as_ptr(), doc_ptr, base, dict) }; - - unsafe { Py::from_owned_ptr_or_err(py, ptr) } - } - - /// Deprecated name for [`PyErr::new_type`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::new_type`")] - #[inline] - pub fn new_type_bound<'py>( - py: Python<'py>, - name: &str, - doc: Option<&str>, - base: Option<&Bound<'py, PyType>>, - dict: Option, - ) -> PyResult> { - let null_terminated_name = - CString::new(name).expect("Failed to initialize nul terminated exception name"); - let null_terminated_doc = - doc.map(|d| CString::new(d).expect("Failed to initialize nul terminated docstring")); - Self::new_type( - py, - &null_terminated_name, - null_terminated_doc.as_deref(), - base, - dict, - ) - } - - /// Prints a standard traceback to `sys.stderr`. - pub fn display(&self, py: Python<'_>) { - #[cfg(Py_3_12)] - unsafe { - ffi::PyErr_DisplayException(self.value(py).as_ptr()) - } - - #[cfg(not(Py_3_12))] - unsafe { - // keep the bound `traceback` alive for entire duration of - // PyErr_Display. if we inline this, the `Bound` will be dropped - // after the argument got evaluated, leading to call with a dangling - // pointer. - let traceback = self.traceback(py); - let type_bound = self.get_type(py); - ffi::PyErr_Display( - type_bound.as_ptr(), - self.value(py).as_ptr(), - traceback - .as_ref() - .map_or(std::ptr::null_mut(), |traceback| traceback.as_ptr()), - ) - } - } - - /// Calls `sys.excepthook` and then prints a standard traceback to `sys.stderr`. - pub fn print(&self, py: Python<'_>) { - self.clone_ref(py).restore(py); - unsafe { ffi::PyErr_PrintEx(0) } - } - - /// Calls `sys.excepthook` and then prints a standard traceback to `sys.stderr`. - /// - /// Additionally sets `sys.last_{type,value,traceback,exc}` attributes to this exception. - pub fn print_and_set_sys_last_vars(&self, py: Python<'_>) { - self.clone_ref(py).restore(py); - unsafe { ffi::PyErr_PrintEx(1) } - } - - /// Returns true if the current exception matches the exception in `exc`. - /// - /// If `exc` is a class object, this also returns `true` when `self` is an instance of a subclass. - /// If `exc` is a tuple, all exceptions in the tuple (and recursively in subtuples) are searched for a match. - pub fn matches<'py, T>(&self, py: Python<'py>, exc: T) -> Result - where - T: IntoPyObject<'py>, - { - Ok(self.is_instance(py, &exc.into_pyobject(py)?.into_any().as_borrowed())) - } - - /// Returns true if the current exception is instance of `T`. - #[inline] - pub fn is_instance(&self, py: Python<'_>, ty: &Bound<'_, PyAny>) -> bool { - let type_bound = self.get_type(py); - (unsafe { ffi::PyErr_GivenExceptionMatches(type_bound.as_ptr(), ty.as_ptr()) }) != 0 - } - - /// Deprecated name for [`PyErr::is_instance`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::is_instance`")] - #[inline] - pub fn is_instance_bound(&self, py: Python<'_>, ty: &Bound<'_, PyAny>) -> bool { - self.is_instance(py, ty) - } - - /// Returns true if the current exception is instance of `T`. - #[inline] - pub fn is_instance_of(&self, py: Python<'_>) -> bool - where - T: PyTypeInfo, - { - self.is_instance(py, &T::type_object(py)) - } - - /// Writes the error back to the Python interpreter's global state. - /// This is the opposite of `PyErr::fetch()`. - #[inline] - pub fn restore(self, py: Python<'_>) { - self.state.restore(py) - } - - /// Reports the error as unraisable. - /// - /// This calls `sys.unraisablehook()` using the current exception and obj argument. - /// - /// This method is useful to report errors in situations where there is no good mechanism - /// to report back to the Python land. In Python this is used to indicate errors in - /// background threads or destructors which are protected. In Rust code this is commonly - /// useful when you are calling into a Python callback which might fail, but there is no - /// obvious way to handle this error other than logging it. - /// - /// Calling this method has the benefit that the error goes back into a standardized callback - /// in Python which for instance allows unittests to ensure that no unraisable error - /// actually happend by hooking `sys.unraisablehook`. - /// - /// Example: - /// ```rust - /// # use pyo3::prelude::*; - /// # use pyo3::exceptions::PyRuntimeError; - /// # fn failing_function() -> PyResult<()> { Err(PyRuntimeError::new_err("foo")) } - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// match failing_function() { - /// Err(pyerr) => pyerr.write_unraisable(py, None), - /// Ok(..) => { /* do something here */ } - /// } - /// Ok(()) - /// }) - /// # } - #[inline] - pub fn write_unraisable(self, py: Python<'_>, obj: Option<&Bound<'_, PyAny>>) { - self.restore(py); - unsafe { ffi::PyErr_WriteUnraisable(obj.map_or(std::ptr::null_mut(), Bound::as_ptr)) } - } - - /// Deprecated name for [`PyErr::write_unraisable`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::write_unraisable`")] - #[inline] - pub fn write_unraisable_bound(self, py: Python<'_>, obj: Option<&Bound<'_, PyAny>>) { - self.write_unraisable(py, obj) - } - - /// Issues a warning message. - /// - /// May return an `Err(PyErr)` if warnings-as-errors is enabled. - /// - /// Equivalent to `warnings.warn()` in Python. - /// - /// The `category` should be one of the `Warning` classes available in - /// [`pyo3::exceptions`](crate::exceptions), or a subclass. The Python - /// object can be retrieved using [`Python::get_type_bound()`]. - /// - /// Example: - /// ```rust - /// # use pyo3::prelude::*; - /// # use pyo3::ffi::c_str; - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let user_warning = py.get_type::(); - /// PyErr::warn(py, &user_warning, c_str!("I am warning you"), 0)?; - /// Ok(()) - /// }) - /// # } - /// ``` - pub fn warn<'py>( - py: Python<'py>, - category: &Bound<'py, PyAny>, - message: &CStr, - stacklevel: i32, - ) -> PyResult<()> { - error_on_minusone(py, unsafe { - ffi::PyErr_WarnEx( - category.as_ptr(), - message.as_ptr(), - stacklevel as ffi::Py_ssize_t, - ) - }) - } - - /// Deprecated name for [`PyErr::warn`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::warn`")] - #[inline] - pub fn warn_bound<'py>( - py: Python<'py>, - category: &Bound<'py, PyAny>, - message: &str, - stacklevel: i32, - ) -> PyResult<()> { - let message = CString::new(message)?; - Self::warn(py, category, &message, stacklevel) - } - - /// Issues a warning message, with more control over the warning attributes. - /// - /// May return a `PyErr` if warnings-as-errors is enabled. - /// - /// Equivalent to `warnings.warn_explicit()` in Python. - /// - /// The `category` should be one of the `Warning` classes available in - /// [`pyo3::exceptions`](crate::exceptions), or a subclass. - pub fn warn_explicit<'py>( - py: Python<'py>, - category: &Bound<'py, PyAny>, - message: &CStr, - filename: &CStr, - lineno: i32, - module: Option<&CStr>, - registry: Option<&Bound<'py, PyAny>>, - ) -> PyResult<()> { - let module_ptr = match module { - None => std::ptr::null_mut(), - Some(s) => s.as_ptr(), - }; - let registry: *mut ffi::PyObject = match registry { - None => std::ptr::null_mut(), - Some(obj) => obj.as_ptr(), - }; - error_on_minusone(py, unsafe { - ffi::PyErr_WarnExplicit( - category.as_ptr(), - message.as_ptr(), - filename.as_ptr(), - lineno, - module_ptr, - registry, - ) - }) - } - - /// Deprecated name for [`PyErr::warn_explicit`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyErr::warn`")] - #[inline] - pub fn warn_explicit_bound<'py>( - py: Python<'py>, - category: &Bound<'py, PyAny>, - message: &str, - filename: &str, - lineno: i32, - module: Option<&str>, - registry: Option<&Bound<'py, PyAny>>, - ) -> PyResult<()> { - let message = CString::new(message)?; - let filename = CString::new(filename)?; - let module = module.map(CString::new).transpose()?; - Self::warn_explicit( - py, - category, - &message, - &filename, - lineno, - module.as_deref(), - registry, - ) - } - - /// Clone the PyErr. This requires the GIL, which is why PyErr does not implement Clone. - /// - /// # Examples - /// ```rust - /// use pyo3::{exceptions::PyTypeError, PyErr, Python, prelude::PyAnyMethods}; - /// Python::with_gil(|py| { - /// let err: PyErr = PyTypeError::new_err(("some type error",)); - /// let err_clone = err.clone_ref(py); - /// assert!(err.get_type(py).is(&err_clone.get_type(py))); - /// assert!(err.value(py).is(err_clone.value(py))); - /// match err.traceback(py) { - /// None => assert!(err_clone.traceback(py).is_none()), - /// Some(tb) => assert!(err_clone.traceback(py).unwrap().is(&tb)), - /// } - /// }); - /// ``` - #[inline] - pub fn clone_ref(&self, py: Python<'_>) -> PyErr { - PyErr::from_state(PyErrState::normalized(self.normalized(py).clone_ref(py))) - } - - /// Return the cause (either an exception instance, or None, set by `raise ... from ...`) - /// associated with the exception, as accessible from Python through `__cause__`. - pub fn cause(&self, py: Python<'_>) -> Option { - use crate::ffi_ptr_ext::FfiPtrExt; - let obj = - unsafe { ffi::PyException_GetCause(self.value(py).as_ptr()).assume_owned_or_opt(py) }; - // PyException_GetCause is documented as potentially returning PyNone, but only GraalPy seems to actually do that - #[cfg(GraalPy)] - if let Some(cause) = &obj { - if cause.is_none() { - return None; - } - } - obj.map(Self::from_value) - } - - /// Set the cause associated with the exception, pass `None` to clear it. - pub fn set_cause(&self, py: Python<'_>, cause: Option) { - let value = self.value(py); - let cause = cause.map(|err| err.into_value(py)); - unsafe { - // PyException_SetCause _steals_ a reference to cause, so must use .into_ptr() - ffi::PyException_SetCause( - value.as_ptr(), - cause.map_or(std::ptr::null_mut(), Py::into_ptr), - ); - } - } - - #[inline] - fn from_state(state: PyErrState) -> PyErr { - PyErr { state } - } - - #[inline] - fn normalized(&self, py: Python<'_>) -> &PyErrStateNormalized { - self.state.as_normalized(py) - } -} - -impl std::fmt::Debug for PyErr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - Python::with_gil(|py| { - f.debug_struct("PyErr") - .field("type", &self.get_type(py)) - .field("value", self.value(py)) - .field("traceback", &self.traceback(py)) - .finish() - }) - } -} - -impl std::fmt::Display for PyErr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Python::with_gil(|py| { - let value = self.value(py); - let type_name = value.get_type().qualname().map_err(|_| std::fmt::Error)?; - write!(f, "{}", type_name)?; - if let Ok(s) = value.str() { - write!(f, ": {}", &s.to_string_lossy()) - } else { - write!(f, ": ") - } - }) - } -} - -impl std::error::Error for PyErr {} - -#[allow(deprecated)] -impl IntoPy for PyErr { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl ToPyObject for PyErr { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for &PyErr { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for PyErr { - type Target = PyBaseException; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.into_value(py).into_bound(py)) - } -} - -impl<'py> IntoPyObject<'py> for &PyErr { - type Target = PyBaseException; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - self.clone_ref(py).into_pyobject(py) - } -} - -struct PyDowncastErrorArguments { - from: Py, - to: Cow<'static, str>, -} - -impl PyErrArguments for PyDowncastErrorArguments { - fn arguments(self, py: Python<'_>) -> PyObject { - const FAILED_TO_EXTRACT: Cow<'_, str> = Cow::Borrowed(""); - let from = self.from.bind(py).qualname(); - let from = match &from { - Ok(qn) => qn.to_cow().unwrap_or(FAILED_TO_EXTRACT), - Err(_) => FAILED_TO_EXTRACT, - }; - format!("'{}' object cannot be converted to '{}'", from, self.to) - .into_pyobject(py) - .unwrap() - .into_any() - .unbind() - } -} - -/// Python exceptions that can be converted to [`PyErr`]. -/// -/// This is used to implement [`From> for PyErr`]. -/// -/// Users should not need to implement this trait directly. It is implemented automatically in the -/// [`crate::import_exception!`] and [`crate::create_exception!`] macros. -pub trait ToPyErr {} - -impl<'py, T> std::convert::From> for PyErr -where - T: ToPyErr, -{ - #[inline] - fn from(err: Bound<'py, T>) -> PyErr { - PyErr::from_value(err.into_any()) - } -} - -/// Convert `DowncastError` to Python `TypeError`. -impl std::convert::From> for PyErr { - fn from(err: DowncastError<'_, '_>) -> PyErr { - let args = PyDowncastErrorArguments { - from: err.from.get_type().into(), - to: err.to, - }; - - exceptions::PyTypeError::new_err(args) - } -} - -impl std::error::Error for DowncastError<'_, '_> {} - -impl std::fmt::Display for DowncastError<'_, '_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - display_downcast_error(f, &self.from, &self.to) - } -} - -/// Convert `DowncastIntoError` to Python `TypeError`. -impl std::convert::From> for PyErr { - fn from(err: DowncastIntoError<'_>) -> PyErr { - let args = PyDowncastErrorArguments { - from: err.from.get_type().into(), - to: err.to, - }; - - exceptions::PyTypeError::new_err(args) - } -} - -impl std::error::Error for DowncastIntoError<'_> {} - -impl std::fmt::Display for DowncastIntoError<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - display_downcast_error(f, &self.from, &self.to) - } -} - -fn display_downcast_error( - f: &mut std::fmt::Formatter<'_>, - from: &Bound<'_, PyAny>, - to: &str, -) -> std::fmt::Result { - write!( - f, - "'{}' object cannot be converted to '{}'", - from.get_type().qualname().map_err(|_| std::fmt::Error)?, - to - ) -} - -#[track_caller] -pub fn panic_after_error(_py: Python<'_>) -> ! { - unsafe { - ffi::PyErr_Print(); - } - panic!("Python API call failed"); -} - -/// Returns Ok if the error code is not -1. -#[inline] -pub(crate) fn error_on_minusone(py: Python<'_>, result: T) -> PyResult<()> { - if result != T::MINUS_ONE { - Ok(()) - } else { - Err(PyErr::fetch(py)) - } -} - -pub(crate) trait SignedInteger: Eq { - const MINUS_ONE: Self; -} - -macro_rules! impl_signed_integer { - ($t:ty) => { - impl SignedInteger for $t { - const MINUS_ONE: Self = -1; - } - }; -} - -impl_signed_integer!(i8); -impl_signed_integer!(i16); -impl_signed_integer!(i32); -impl_signed_integer!(i64); -impl_signed_integer!(i128); -impl_signed_integer!(isize); - -#[cfg(test)] -mod tests { - use super::PyErrState; - use crate::exceptions::{self, PyTypeError, PyValueError}; - use crate::{ffi, PyErr, PyTypeInfo, Python}; - - #[test] - fn no_error() { - assert!(Python::with_gil(PyErr::take).is_none()); - } - - #[test] - fn set_valueerror() { - Python::with_gil(|py| { - let err: PyErr = exceptions::PyValueError::new_err("some exception message"); - assert!(err.is_instance_of::(py)); - err.restore(py); - assert!(PyErr::occurred(py)); - let err = PyErr::fetch(py); - assert!(err.is_instance_of::(py)); - assert_eq!(err.to_string(), "ValueError: some exception message"); - }) - } - - #[test] - fn invalid_error_type() { - Python::with_gil(|py| { - let err: PyErr = PyErr::new::(()); - assert!(err.is_instance_of::(py)); - err.restore(py); - let err = PyErr::fetch(py); - - assert!(err.is_instance_of::(py)); - assert_eq!( - err.to_string(), - "TypeError: exceptions must derive from BaseException" - ); - }) - } - - #[test] - fn set_typeerror() { - Python::with_gil(|py| { - let err: PyErr = exceptions::PyTypeError::new_err(()); - err.restore(py); - assert!(PyErr::occurred(py)); - drop(PyErr::fetch(py)); - }); - } - - #[test] - #[should_panic(expected = "new panic")] - fn fetching_panic_exception_resumes_unwind() { - use crate::panic::PanicException; - - Python::with_gil(|py| { - let err: PyErr = PanicException::new_err("new panic"); - err.restore(py); - assert!(PyErr::occurred(py)); - - // should resume unwind - let _ = PyErr::fetch(py); - }); - } - - #[test] - #[should_panic(expected = "new panic")] - #[cfg(not(Py_3_12))] - fn fetching_normalized_panic_exception_resumes_unwind() { - use crate::panic::PanicException; - - Python::with_gil(|py| { - let err: PyErr = PanicException::new_err("new panic"); - // Restoring an error doesn't normalize it before Python 3.12, - // so we have to explicitly test this case. - let _ = err.normalized(py); - err.restore(py); - assert!(PyErr::occurred(py)); - - // should resume unwind - let _ = PyErr::fetch(py); - }); - } - - #[test] - fn err_debug() { - // Debug representation should be like the following (without the newlines): - // PyErr { - // type: , - // value: Exception('banana'), - // traceback: Some("); - assert_eq!(fields.next().unwrap(), "value: Exception('banana')"); - - let traceback = fields.next().unwrap(); - assert!(traceback.starts_with("traceback: Some()")); - - assert!(fields.next().is_none()); - }); - } - - #[test] - fn err_display() { - Python::with_gil(|py| { - let err = py - .run(ffi::c_str!("raise Exception('banana')"), None, None) - .expect_err("raising should have given us an error"); - assert_eq!(err.to_string(), "Exception: banana"); - }); - } - - #[test] - fn test_pyerr_send_sync() { - fn is_send() {} - fn is_sync() {} - - is_send::(); - is_sync::(); - - is_send::(); - is_sync::(); - } - - #[test] - fn test_pyerr_matches() { - Python::with_gil(|py| { - let err = PyErr::new::("foo"); - assert!(err.matches(py, PyValueError::type_object(py)).unwrap()); - - assert!(err - .matches( - py, - (PyValueError::type_object(py), PyTypeError::type_object(py)) - ) - .unwrap()); - - assert!(!err.matches(py, PyTypeError::type_object(py)).unwrap()); - - // String is not a valid exception class, so we should get a TypeError - let err: PyErr = PyErr::from_type(crate::types::PyString::type_object(py), "foo"); - assert!(err.matches(py, PyTypeError::type_object(py)).unwrap()); - }) - } - - #[test] - fn test_pyerr_cause() { - Python::with_gil(|py| { - let err = py - .run(ffi::c_str!("raise Exception('banana')"), None, None) - .expect_err("raising should have given us an error"); - assert!(err.cause(py).is_none()); - - let err = py - .run( - ffi::c_str!("raise Exception('banana') from Exception('apple')"), - None, - None, - ) - .expect_err("raising should have given us an error"); - let cause = err - .cause(py) - .expect("raising from should have given us a cause"); - assert_eq!(cause.to_string(), "Exception: apple"); - - err.set_cause(py, None); - assert!(err.cause(py).is_none()); - - let new_cause = exceptions::PyValueError::new_err("orange"); - err.set_cause(py, Some(new_cause)); - let cause = err - .cause(py) - .expect("set_cause should have given us a cause"); - assert_eq!(cause.to_string(), "ValueError: orange"); - }); - } - - #[test] - fn warnings() { - use crate::types::any::PyAnyMethods; - // Note: although the warning filter is interpreter global, keeping the - // GIL locked should prevent effects to be visible to other testing - // threads. - Python::with_gil(|py| { - let cls = py.get_type::(); - - // Reset warning filter to default state - let warnings = py.import("warnings").unwrap(); - warnings.call_method0("resetwarnings").unwrap(); - - // First, test the warning is emitted - #[cfg(not(Py_GIL_DISABLED))] - assert_warnings!( - py, - { PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() }, - [(exceptions::PyUserWarning, "I am warning you")] - ); - - // Test with raising - warnings - .call_method1("simplefilter", ("error", &cls)) - .unwrap(); - PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap_err(); - - // Test with error for an explicit module - warnings.call_method0("resetwarnings").unwrap(); - warnings - .call_method1("filterwarnings", ("error", "", &cls, "pyo3test")) - .unwrap(); - - // This has the wrong module and will not raise, just be emitted - #[cfg(not(Py_GIL_DISABLED))] - assert_warnings!( - py, - { PyErr::warn(py, &cls, ffi::c_str!("I am warning you"), 0).unwrap() }, - [(exceptions::PyUserWarning, "I am warning you")] - ); - - let err = PyErr::warn_explicit( - py, - &cls, - ffi::c_str!("I am warning you"), - ffi::c_str!("pyo3test.py"), - 427, - None, - None, - ) - .unwrap_err(); - assert!(err - .value(py) - .getattr("args") - .unwrap() - .get_item(0) - .unwrap() - .eq("I am warning you") - .unwrap()); - - // Finally, reset filter again - warnings.call_method0("resetwarnings").unwrap(); - }); - } -} diff --git a/vendor/pyo3/src/exceptions.rs b/vendor/pyo3/src/exceptions.rs deleted file mode 100644 index 6f0fa3e674..0000000000 --- a/vendor/pyo3/src/exceptions.rs +++ /dev/null @@ -1,1150 +0,0 @@ -//! Exception and warning types defined by Python. -//! -//! The structs in this module represent Python's built-in exceptions and -//! warnings, while the modules comprise structs representing errors defined in -//! Python code. -//! -//! The latter are created with the -//! [`import_exception`](crate::import_exception) macro, which you can use -//! yourself to import Python classes that are ultimately derived from -//! `BaseException`. - -use crate::{ffi, Bound, PyResult, Python}; -use std::ffi::CStr; -use std::ops; - -/// The boilerplate to convert between a Rust type and a Python exception. -#[doc(hidden)] -#[macro_export] -macro_rules! impl_exception_boilerplate { - ($name: ident) => { - $crate::impl_exception_boilerplate_bound!($name); - - impl $crate::ToPyErr for $name {} - }; -} - -#[doc(hidden)] -#[macro_export] -macro_rules! impl_exception_boilerplate_bound { - ($name: ident) => { - impl $name { - /// Creates a new [`PyErr`] of this type. - /// - /// [`PyErr`]: https://docs.rs/pyo3/latest/pyo3/struct.PyErr.html "PyErr in pyo3" - #[inline] - #[allow(dead_code)] - pub fn new_err(args: A) -> $crate::PyErr - where - A: $crate::PyErrArguments + ::std::marker::Send + ::std::marker::Sync + 'static, - { - $crate::PyErr::new::<$name, A>(args) - } - } - }; -} - -/// Defines a Rust type for an exception defined in Python code. -/// -/// # Syntax -/// -/// ```import_exception!(module, MyError)``` -/// -/// * `module` is the name of the containing module. -/// * `MyError` is the name of the new exception type. -/// -/// # Examples -/// ``` -/// use pyo3::import_exception; -/// use pyo3::types::IntoPyDict; -/// use pyo3::Python; -/// -/// import_exception!(socket, gaierror); -/// -/// # fn main() -> pyo3::PyResult<()> { -/// Python::with_gil(|py| { -/// let ctx = [("gaierror", py.get_type::())].into_py_dict(py)?; -/// pyo3::py_run!(py, *ctx, "import socket; assert gaierror is socket.gaierror"); -/// # Ok(()) -/// }) -/// # } -/// -/// ``` -#[macro_export] -macro_rules! import_exception { - ($module: expr, $name: ident) => { - /// A Rust type representing an exception defined in Python code. - /// - /// This type was created by the [`pyo3::import_exception!`] macro - see its documentation - /// for more information. - /// - /// [`pyo3::import_exception!`]: https://docs.rs/pyo3/latest/pyo3/macro.import_exception.html "import_exception in pyo3" - #[repr(transparent)] - #[allow(non_camel_case_types)] // E.g. `socket.herror` - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - - $crate::pyobject_native_type_core!( - $name, - $name::type_object_raw, - #module=::std::option::Option::Some(stringify!($module)) - ); - - impl $name { - fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject { - use $crate::types::PyTypeMethods; - static TYPE_OBJECT: $crate::impl_::exceptions::ImportedExceptionTypeObject = - $crate::impl_::exceptions::ImportedExceptionTypeObject::new(stringify!($module), stringify!($name)); - TYPE_OBJECT.get(py).as_type_ptr() - } - } - }; -} - -/// Variant of [`import_exception`](crate::import_exception) that does not emit code needed to -/// use the imported exception type as a GIL Ref. -/// -/// This is useful only during migration as a way to avoid generating needless code. -#[macro_export] -macro_rules! import_exception_bound { - ($module: expr, $name: ident) => { - /// A Rust type representing an exception defined in Python code. - /// - /// This type was created by the [`pyo3::import_exception_bound!`] macro - see its documentation - /// for more information. - /// - /// [`pyo3::import_exception_bound!`]: https://docs.rs/pyo3/latest/pyo3/macro.import_exception.html "import_exception in pyo3" - #[repr(transparent)] - #[allow(non_camel_case_types)] // E.g. `socket.herror` - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate_bound!($name); - - $crate::pyobject_native_type_info!( - $name, - $name::type_object_raw, - ::std::option::Option::Some(stringify!($module)) - ); - - impl $crate::types::DerefToPyAny for $name {} - - impl $name { - fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject { - use $crate::types::PyTypeMethods; - static TYPE_OBJECT: $crate::impl_::exceptions::ImportedExceptionTypeObject = - $crate::impl_::exceptions::ImportedExceptionTypeObject::new( - stringify!($module), - stringify!($name), - ); - TYPE_OBJECT.get(py).as_type_ptr() - } - } - }; -} - -/// Defines a new exception type. -/// -/// # Syntax -/// -/// * `module` is the name of the containing module. -/// * `name` is the name of the new exception type. -/// * `base` is the base class of `MyError`, usually [`PyException`]. -/// * `doc` (optional) is the docstring visible to users (with `.__doc__` and `help()`) and -/// -/// accompanies your error type in your crate's documentation. -/// -/// # Examples -/// -/// ``` -/// use pyo3::prelude::*; -/// use pyo3::create_exception; -/// use pyo3::exceptions::PyException; -/// -/// create_exception!(my_module, MyError, PyException, "Some description."); -/// -/// #[pyfunction] -/// fn raise_myerror() -> PyResult<()> { -/// let err = MyError::new_err("Some error happened."); -/// Err(err) -/// } -/// -/// #[pymodule] -/// fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { -/// m.add("MyError", m.py().get_type::())?; -/// m.add_function(wrap_pyfunction!(raise_myerror, m)?)?; -/// Ok(()) -/// } -/// # fn main() -> PyResult<()> { -/// # Python::with_gil(|py| -> PyResult<()> { -/// # let fun = wrap_pyfunction!(raise_myerror, py)?; -/// # let locals = pyo3::types::PyDict::new(py); -/// # locals.set_item("MyError", py.get_type::())?; -/// # locals.set_item("raise_myerror", fun)?; -/// # -/// # py.run(pyo3::ffi::c_str!( -/// # "try: -/// # raise_myerror() -/// # except MyError as e: -/// # assert e.__doc__ == 'Some description.' -/// # assert str(e) == 'Some error happened.'"), -/// # None, -/// # Some(&locals), -/// # )?; -/// # -/// # Ok(()) -/// # }) -/// # } -/// ``` -/// -/// Python code can handle this exception like any other exception: -/// -/// ```python -/// from my_module import MyError, raise_myerror -/// -/// try: -/// raise_myerror() -/// except MyError as e: -/// assert e.__doc__ == 'Some description.' -/// assert str(e) == 'Some error happened.' -/// ``` -/// -#[macro_export] -macro_rules! create_exception { - ($module: expr, $name: ident, $base: ty) => { - #[repr(transparent)] - #[allow(non_camel_case_types)] // E.g. `socket.herror` - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - - $crate::create_exception_type_object!($module, $name, $base, None); - }; - ($module: expr, $name: ident, $base: ty, $doc: expr) => { - #[repr(transparent)] - #[allow(non_camel_case_types)] // E.g. `socket.herror` - #[doc = $doc] - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - - $crate::create_exception_type_object!($module, $name, $base, Some($doc)); - }; -} - -/// `impl PyTypeInfo for $name` where `$name` is an -/// exception newly defined in Rust code. -#[doc(hidden)] -#[macro_export] -macro_rules! create_exception_type_object { - ($module: expr, $name: ident, $base: ty, None) => { - $crate::create_exception_type_object!($module, $name, $base, ::std::option::Option::None); - }; - ($module: expr, $name: ident, $base: ty, Some($doc: expr)) => { - $crate::create_exception_type_object!($module, $name, $base, ::std::option::Option::Some($crate::ffi::c_str!($doc))); - }; - ($module: expr, $name: ident, $base: ty, $doc: expr) => { - $crate::pyobject_native_type_core!( - $name, - $name::type_object_raw, - #module=::std::option::Option::Some(stringify!($module)) - ); - - impl $name { - fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject { - use $crate::sync::GILOnceCell; - static TYPE_OBJECT: GILOnceCell<$crate::Py<$crate::types::PyType>> = - GILOnceCell::new(); - - TYPE_OBJECT - .get_or_init(py, || - $crate::PyErr::new_type( - py, - $crate::ffi::c_str!(concat!(stringify!($module), ".", stringify!($name))), - $doc, - ::std::option::Option::Some(&py.get_type::<$base>()), - ::std::option::Option::None, - ).expect("Failed to initialize new exception type.") - ).as_ptr() as *mut $crate::ffi::PyTypeObject - } - } - }; -} - -macro_rules! impl_native_exception ( - ($name:ident, $exc_name:ident, $doc:expr, $layout:path $(, #checkfunction=$checkfunction:path)?) => ( - #[doc = $doc] - #[allow(clippy::upper_case_acronyms)] - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - $crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject } $(, #checkfunction=$checkfunction)?); - $crate::pyobject_subclassable_native_type!($name, $layout); - ); - ($name:ident, $exc_name:ident, $doc:expr) => ( - impl_native_exception!($name, $exc_name, $doc, $crate::ffi::PyBaseExceptionObject); - ) -); - -#[cfg(windows)] -macro_rules! impl_windows_native_exception ( - ($name:ident, $exc_name:ident, $doc:expr, $layout:path) => ( - #[cfg(windows)] - #[doc = $doc] - #[allow(clippy::upper_case_acronyms)] - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - $crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject }); - ); - ($name:ident, $exc_name:ident, $doc:expr) => ( - impl_windows_native_exception!($name, $exc_name, $doc, $crate::ffi::PyBaseExceptionObject); - ) -); - -macro_rules! native_doc( - ($name: literal, $alt: literal) => ( - concat!( -"Represents Python's [`", $name, "`](https://docs.python.org/3/library/exceptions.html#", $name, ") exception. - -", $alt - ) - ); - ($name: literal) => ( - concat!( -" -Represents Python's [`", $name, "`](https://docs.python.org/3/library/exceptions.html#", $name, ") exception. - -# Example: Raising ", $name, " from Rust - -This exception can be sent to Python code by converting it into a -[`PyErr`](crate::PyErr), where Python code can then catch it. -``` -use pyo3::prelude::*; -use pyo3::exceptions::Py", $name, "; - -#[pyfunction] -fn always_throws() -> PyResult<()> { - let message = \"I'm ", $name ,", and I was raised from Rust.\"; - Err(Py", $name, "::new_err(message)) -} -# -# Python::with_gil(|py| { -# let fun = pyo3::wrap_pyfunction!(always_throws, py).unwrap(); -# let err = fun.call0().expect_err(\"called a function that should always return an error but the return value was Ok\"); -# assert!(err.is_instance_of::(py)) -# }); -``` - -Python code: - ```python - from my_module import always_throws - -try: - always_throws() -except ", $name, " as e: - print(f\"Caught an exception: {e}\") -``` - -# Example: Catching ", $name, " in Rust - -``` -use pyo3::prelude::*; -use pyo3::exceptions::Py", $name, "; -use pyo3::ffi::c_str; - -Python::with_gil(|py| { - let result: PyResult<()> = py.run(c_str!(\"raise ", $name, "\"), None, None); - - let error_type = match result { - Ok(_) => \"Not an error\", - Err(error) if error.is_instance_of::(py) => \"" , $name, "\", - Err(_) => \"Some other error\", - }; - - assert_eq!(error_type, \"", $name, "\"); -}); -``` -" - ) - ); -); - -impl_native_exception!( - PyBaseException, - PyExc_BaseException, - native_doc!("BaseException"), - ffi::PyBaseExceptionObject, - #checkfunction=ffi::PyExceptionInstance_Check -); -impl_native_exception!(PyException, PyExc_Exception, native_doc!("Exception")); -impl_native_exception!( - PyStopAsyncIteration, - PyExc_StopAsyncIteration, - native_doc!("StopAsyncIteration") -); -impl_native_exception!( - PyStopIteration, - PyExc_StopIteration, - native_doc!("StopIteration"), - ffi::PyStopIterationObject -); -impl_native_exception!( - PyGeneratorExit, - PyExc_GeneratorExit, - native_doc!("GeneratorExit") -); -impl_native_exception!( - PyArithmeticError, - PyExc_ArithmeticError, - native_doc!("ArithmeticError") -); -impl_native_exception!(PyLookupError, PyExc_LookupError, native_doc!("LookupError")); - -impl_native_exception!( - PyAssertionError, - PyExc_AssertionError, - native_doc!("AssertionError") -); -impl_native_exception!( - PyAttributeError, - PyExc_AttributeError, - native_doc!("AttributeError") -); -impl_native_exception!(PyBufferError, PyExc_BufferError, native_doc!("BufferError")); -impl_native_exception!(PyEOFError, PyExc_EOFError, native_doc!("EOFError")); -impl_native_exception!( - PyFloatingPointError, - PyExc_FloatingPointError, - native_doc!("FloatingPointError") -); -#[cfg(not(any(PyPy, GraalPy)))] -impl_native_exception!( - PyOSError, - PyExc_OSError, - native_doc!("OSError"), - ffi::PyOSErrorObject -); -#[cfg(any(PyPy, GraalPy))] -impl_native_exception!(PyOSError, PyExc_OSError, native_doc!("OSError")); -impl_native_exception!(PyImportError, PyExc_ImportError, native_doc!("ImportError")); - -impl_native_exception!( - PyModuleNotFoundError, - PyExc_ModuleNotFoundError, - native_doc!("ModuleNotFoundError") -); - -impl_native_exception!(PyIndexError, PyExc_IndexError, native_doc!("IndexError")); -impl_native_exception!(PyKeyError, PyExc_KeyError, native_doc!("KeyError")); -impl_native_exception!( - PyKeyboardInterrupt, - PyExc_KeyboardInterrupt, - native_doc!("KeyboardInterrupt") -); -impl_native_exception!(PyMemoryError, PyExc_MemoryError, native_doc!("MemoryError")); -impl_native_exception!(PyNameError, PyExc_NameError, native_doc!("NameError")); -impl_native_exception!( - PyOverflowError, - PyExc_OverflowError, - native_doc!("OverflowError") -); -impl_native_exception!( - PyRuntimeError, - PyExc_RuntimeError, - native_doc!("RuntimeError") -); -impl_native_exception!( - PyRecursionError, - PyExc_RecursionError, - native_doc!("RecursionError") -); -impl_native_exception!( - PyNotImplementedError, - PyExc_NotImplementedError, - native_doc!("NotImplementedError") -); -#[cfg(not(any(PyPy, GraalPy)))] -impl_native_exception!( - PySyntaxError, - PyExc_SyntaxError, - native_doc!("SyntaxError"), - ffi::PySyntaxErrorObject -); -#[cfg(any(PyPy, GraalPy))] -impl_native_exception!(PySyntaxError, PyExc_SyntaxError, native_doc!("SyntaxError")); -impl_native_exception!( - PyReferenceError, - PyExc_ReferenceError, - native_doc!("ReferenceError") -); -impl_native_exception!(PySystemError, PyExc_SystemError, native_doc!("SystemError")); -#[cfg(not(any(PyPy, GraalPy)))] -impl_native_exception!( - PySystemExit, - PyExc_SystemExit, - native_doc!("SystemExit"), - ffi::PySystemExitObject -); -#[cfg(any(PyPy, GraalPy))] -impl_native_exception!(PySystemExit, PyExc_SystemExit, native_doc!("SystemExit")); -impl_native_exception!(PyTypeError, PyExc_TypeError, native_doc!("TypeError")); -impl_native_exception!( - PyUnboundLocalError, - PyExc_UnboundLocalError, - native_doc!("UnboundLocalError") -); -#[cfg(not(any(PyPy, GraalPy)))] -impl_native_exception!( - PyUnicodeError, - PyExc_UnicodeError, - native_doc!("UnicodeError"), - ffi::PyUnicodeErrorObject -); -#[cfg(any(PyPy, GraalPy))] -impl_native_exception!( - PyUnicodeError, - PyExc_UnicodeError, - native_doc!("UnicodeError") -); -// these four errors need arguments, so they're too annoying to write tests for using macros... -impl_native_exception!( - PyUnicodeDecodeError, - PyExc_UnicodeDecodeError, - native_doc!("UnicodeDecodeError", "") -); -impl_native_exception!( - PyUnicodeEncodeError, - PyExc_UnicodeEncodeError, - native_doc!("UnicodeEncodeError", "") -); -impl_native_exception!( - PyUnicodeTranslateError, - PyExc_UnicodeTranslateError, - native_doc!("UnicodeTranslateError", "") -); -#[cfg(Py_3_11)] -impl_native_exception!( - PyBaseExceptionGroup, - PyExc_BaseExceptionGroup, - native_doc!("BaseExceptionGroup", "") -); -impl_native_exception!(PyValueError, PyExc_ValueError, native_doc!("ValueError")); -impl_native_exception!( - PyZeroDivisionError, - PyExc_ZeroDivisionError, - native_doc!("ZeroDivisionError") -); - -impl_native_exception!( - PyBlockingIOError, - PyExc_BlockingIOError, - native_doc!("BlockingIOError") -); -impl_native_exception!( - PyBrokenPipeError, - PyExc_BrokenPipeError, - native_doc!("BrokenPipeError") -); -impl_native_exception!( - PyChildProcessError, - PyExc_ChildProcessError, - native_doc!("ChildProcessError") -); -impl_native_exception!( - PyConnectionError, - PyExc_ConnectionError, - native_doc!("ConnectionError") -); -impl_native_exception!( - PyConnectionAbortedError, - PyExc_ConnectionAbortedError, - native_doc!("ConnectionAbortedError") -); -impl_native_exception!( - PyConnectionRefusedError, - PyExc_ConnectionRefusedError, - native_doc!("ConnectionRefusedError") -); -impl_native_exception!( - PyConnectionResetError, - PyExc_ConnectionResetError, - native_doc!("ConnectionResetError") -); -impl_native_exception!( - PyFileExistsError, - PyExc_FileExistsError, - native_doc!("FileExistsError") -); -impl_native_exception!( - PyFileNotFoundError, - PyExc_FileNotFoundError, - native_doc!("FileNotFoundError") -); -impl_native_exception!( - PyInterruptedError, - PyExc_InterruptedError, - native_doc!("InterruptedError") -); -impl_native_exception!( - PyIsADirectoryError, - PyExc_IsADirectoryError, - native_doc!("IsADirectoryError") -); -impl_native_exception!( - PyNotADirectoryError, - PyExc_NotADirectoryError, - native_doc!("NotADirectoryError") -); -impl_native_exception!( - PyPermissionError, - PyExc_PermissionError, - native_doc!("PermissionError") -); -impl_native_exception!( - PyProcessLookupError, - PyExc_ProcessLookupError, - native_doc!("ProcessLookupError") -); -impl_native_exception!( - PyTimeoutError, - PyExc_TimeoutError, - native_doc!("TimeoutError") -); - -impl_native_exception!( - PyEnvironmentError, - PyExc_EnvironmentError, - native_doc!("EnvironmentError") -); -impl_native_exception!(PyIOError, PyExc_IOError, native_doc!("IOError")); - -#[cfg(windows)] -impl_windows_native_exception!( - PyWindowsError, - PyExc_WindowsError, - native_doc!("WindowsError") -); - -impl PyUnicodeDecodeError { - /// Creates a Python `UnicodeDecodeError`. - pub fn new<'py>( - py: Python<'py>, - encoding: &CStr, - input: &[u8], - range: ops::Range, - reason: &CStr, - ) -> PyResult> { - use crate::ffi_ptr_ext::FfiPtrExt; - use crate::py_result_ext::PyResultExt; - unsafe { - ffi::PyUnicodeDecodeError_Create( - encoding.as_ptr(), - input.as_ptr().cast(), - input.len() as ffi::Py_ssize_t, - range.start as ffi::Py_ssize_t, - range.end as ffi::Py_ssize_t, - reason.as_ptr(), - ) - .assume_owned_or_err(py) - } - .downcast_into() - } - - /// Deprecated name for [`PyUnicodeDecodeError::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyUnicodeDecodeError::new`")] - #[inline] - pub fn new_bound<'py>( - py: Python<'py>, - encoding: &CStr, - input: &[u8], - range: ops::Range, - reason: &CStr, - ) -> PyResult> { - Self::new(py, encoding, input, range, reason) - } - - /// Creates a Python `UnicodeDecodeError` from a Rust UTF-8 decoding error. - /// - /// # Examples - /// - /// ``` - /// #![cfg_attr(invalid_from_utf8_lint, allow(invalid_from_utf8))] - /// use pyo3::prelude::*; - /// use pyo3::exceptions::PyUnicodeDecodeError; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let invalid_utf8 = b"fo\xd8o"; - /// let err = std::str::from_utf8(invalid_utf8).expect_err("should be invalid utf8"); - /// let decode_err = PyUnicodeDecodeError::new_utf8(py, invalid_utf8, err)?; - /// assert_eq!( - /// decode_err.to_string(), - /// "'utf-8' codec can't decode byte 0xd8 in position 2: invalid utf-8" - /// ); - /// Ok(()) - /// }) - /// # } - pub fn new_utf8<'py>( - py: Python<'py>, - input: &[u8], - err: std::str::Utf8Error, - ) -> PyResult> { - let pos = err.valid_up_to(); - PyUnicodeDecodeError::new( - py, - ffi::c_str!("utf-8"), - input, - pos..(pos + 1), - ffi::c_str!("invalid utf-8"), - ) - } - - /// Deprecated name for [`PyUnicodeDecodeError::new_utf8`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyUnicodeDecodeError::new_utf8`")] - #[inline] - pub fn new_utf8_bound<'py>( - py: Python<'py>, - input: &[u8], - err: std::str::Utf8Error, - ) -> PyResult> { - Self::new_utf8(py, input, err) - } -} - -impl_native_exception!(PyWarning, PyExc_Warning, native_doc!("Warning")); -impl_native_exception!(PyUserWarning, PyExc_UserWarning, native_doc!("UserWarning")); -impl_native_exception!( - PyDeprecationWarning, - PyExc_DeprecationWarning, - native_doc!("DeprecationWarning") -); -impl_native_exception!( - PyPendingDeprecationWarning, - PyExc_PendingDeprecationWarning, - native_doc!("PendingDeprecationWarning") -); -impl_native_exception!( - PySyntaxWarning, - PyExc_SyntaxWarning, - native_doc!("SyntaxWarning") -); -impl_native_exception!( - PyRuntimeWarning, - PyExc_RuntimeWarning, - native_doc!("RuntimeWarning") -); -impl_native_exception!( - PyFutureWarning, - PyExc_FutureWarning, - native_doc!("FutureWarning") -); -impl_native_exception!( - PyImportWarning, - PyExc_ImportWarning, - native_doc!("ImportWarning") -); -impl_native_exception!( - PyUnicodeWarning, - PyExc_UnicodeWarning, - native_doc!("UnicodeWarning") -); -impl_native_exception!( - PyBytesWarning, - PyExc_BytesWarning, - native_doc!("BytesWarning") -); -impl_native_exception!( - PyResourceWarning, - PyExc_ResourceWarning, - native_doc!("ResourceWarning") -); - -#[cfg(Py_3_10)] -impl_native_exception!( - PyEncodingWarning, - PyExc_EncodingWarning, - native_doc!("EncodingWarning") -); - -#[cfg(test)] -macro_rules! test_exception { - ($exc_ty:ident $(, |$py:tt| $constructor:expr )?) => { - #[allow(non_snake_case)] - #[test] - fn $exc_ty () { - use super::$exc_ty; - - $crate::Python::with_gil(|py| { - use $crate::types::PyAnyMethods; - let err: $crate::PyErr = { - None - $( - .or(Some({ let $py = py; $constructor })) - )? - .unwrap_or($exc_ty::new_err("a test exception")) - }; - - assert!(err.is_instance_of::<$exc_ty>(py)); - - let value = err.value(py).as_any().downcast::<$exc_ty>().unwrap(); - - assert!($crate::PyErr::from(value.clone()).is_instance_of::<$exc_ty>(py)); - }) - } - }; -} - -/// Exceptions defined in Python's [`asyncio`](https://docs.python.org/3/library/asyncio.html) -/// module. -pub mod asyncio { - import_exception!(asyncio, CancelledError); - import_exception!(asyncio, InvalidStateError); - import_exception!(asyncio, TimeoutError); - import_exception!(asyncio, IncompleteReadError); - import_exception!(asyncio, LimitOverrunError); - import_exception!(asyncio, QueueEmpty); - import_exception!(asyncio, QueueFull); - - #[cfg(test)] - mod tests { - test_exception!(CancelledError); - test_exception!(InvalidStateError); - test_exception!(TimeoutError); - test_exception!(IncompleteReadError, |_| IncompleteReadError::new_err(( - "partial", "expected" - ))); - test_exception!(LimitOverrunError, |_| LimitOverrunError::new_err(( - "message", "consumed" - ))); - test_exception!(QueueEmpty); - test_exception!(QueueFull); - } -} - -/// Exceptions defined in Python's [`socket`](https://docs.python.org/3/library/socket.html) -/// module. -pub mod socket { - import_exception!(socket, herror); - import_exception!(socket, gaierror); - import_exception!(socket, timeout); - - #[cfg(test)] - mod tests { - test_exception!(herror); - test_exception!(gaierror); - test_exception!(timeout); - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::any::PyAnyMethods; - use crate::types::{IntoPyDict, PyDict}; - use crate::PyErr; - - import_exception_bound!(socket, gaierror); - import_exception_bound!(email.errors, MessageError); - - #[test] - fn test_check_exception() { - Python::with_gil(|py| { - let err: PyErr = gaierror::new_err(()); - let socket = py - .import("socket") - .map_err(|e| e.display(py)) - .expect("could not import socket"); - - let d = PyDict::new(py); - d.set_item("socket", socket) - .map_err(|e| e.display(py)) - .expect("could not setitem"); - - d.set_item("exc", err) - .map_err(|e| e.display(py)) - .expect("could not setitem"); - - py.run( - ffi::c_str!("assert isinstance(exc, socket.gaierror)"), - None, - Some(&d), - ) - .map_err(|e| e.display(py)) - .expect("assertion failed"); - }); - } - - #[test] - fn test_check_exception_nested() { - Python::with_gil(|py| { - let err: PyErr = MessageError::new_err(()); - let email = py - .import("email") - .map_err(|e| e.display(py)) - .expect("could not import email"); - - let d = PyDict::new(py); - d.set_item("email", email) - .map_err(|e| e.display(py)) - .expect("could not setitem"); - d.set_item("exc", err) - .map_err(|e| e.display(py)) - .expect("could not setitem"); - - py.run( - ffi::c_str!("assert isinstance(exc, email.errors.MessageError)"), - None, - Some(&d), - ) - .map_err(|e| e.display(py)) - .expect("assertion failed"); - }); - } - - #[test] - fn custom_exception() { - create_exception!(mymodule, CustomError, PyException); - - Python::with_gil(|py| { - let error_type = py.get_type::(); - let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap(); - let type_description: String = py - .eval(ffi::c_str!("str(CustomError)"), None, Some(&ctx)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(type_description, ""); - py.run( - ffi::c_str!("assert CustomError('oops').args == ('oops',)"), - None, - Some(&ctx), - ) - .unwrap(); - py.run( - ffi::c_str!("assert CustomError.__doc__ is None"), - None, - Some(&ctx), - ) - .unwrap(); - }); - } - - #[test] - fn custom_exception_dotted_module() { - create_exception!(mymodule.exceptions, CustomError, PyException); - Python::with_gil(|py| { - let error_type = py.get_type::(); - let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap(); - let type_description: String = py - .eval(ffi::c_str!("str(CustomError)"), None, Some(&ctx)) - .unwrap() - .extract() - .unwrap(); - assert_eq!( - type_description, - "" - ); - }); - } - - #[test] - fn custom_exception_doc() { - create_exception!(mymodule, CustomError, PyException, "Some docs"); - - Python::with_gil(|py| { - let error_type = py.get_type::(); - let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap(); - let type_description: String = py - .eval(ffi::c_str!("str(CustomError)"), None, Some(&ctx)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(type_description, ""); - py.run( - ffi::c_str!("assert CustomError('oops').args == ('oops',)"), - None, - Some(&ctx), - ) - .unwrap(); - py.run( - ffi::c_str!("assert CustomError.__doc__ == 'Some docs'"), - None, - Some(&ctx), - ) - .unwrap(); - }); - } - - #[test] - fn custom_exception_doc_expr() { - create_exception!( - mymodule, - CustomError, - PyException, - concat!("Some", " more ", stringify!(docs)) - ); - - Python::with_gil(|py| { - let error_type = py.get_type::(); - let ctx = [("CustomError", error_type)].into_py_dict(py).unwrap(); - let type_description: String = py - .eval(ffi::c_str!("str(CustomError)"), None, Some(&ctx)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(type_description, ""); - py.run( - ffi::c_str!("assert CustomError('oops').args == ('oops',)"), - None, - Some(&ctx), - ) - .unwrap(); - py.run( - ffi::c_str!("assert CustomError.__doc__ == 'Some more docs'"), - None, - Some(&ctx), - ) - .unwrap(); - }); - } - - #[test] - fn native_exception_debug() { - Python::with_gil(|py| { - let exc = py - .run(ffi::c_str!("raise Exception('banana')"), None, None) - .expect_err("raising should have given us an error") - .into_value(py) - .into_bound(py); - assert_eq!( - format!("{:?}", exc), - exc.repr().unwrap().extract::().unwrap() - ); - }); - } - - #[test] - fn native_exception_display() { - Python::with_gil(|py| { - let exc = py - .run(ffi::c_str!("raise Exception('banana')"), None, None) - .expect_err("raising should have given us an error") - .into_value(py) - .into_bound(py); - assert_eq!( - exc.to_string(), - exc.str().unwrap().extract::().unwrap() - ); - }); - } - - #[test] - fn unicode_decode_error() { - let invalid_utf8 = b"fo\xd8o"; - #[cfg_attr(invalid_from_utf8_lint, allow(invalid_from_utf8))] - let err = std::str::from_utf8(invalid_utf8).expect_err("should be invalid utf8"); - Python::with_gil(|py| { - let decode_err = PyUnicodeDecodeError::new_utf8(py, invalid_utf8, err).unwrap(); - assert_eq!( - format!("{:?}", decode_err), - "UnicodeDecodeError('utf-8', b'fo\\xd8o', 2, 3, 'invalid utf-8')" - ); - - // Restoring should preserve the same error - let e: PyErr = decode_err.into(); - e.restore(py); - - assert_eq!( - PyErr::fetch(py).to_string(), - "UnicodeDecodeError: \'utf-8\' codec can\'t decode byte 0xd8 in position 2: invalid utf-8" - ); - }); - } - #[cfg(Py_3_11)] - test_exception!(PyBaseExceptionGroup, |_| PyBaseExceptionGroup::new_err(( - "msg", - vec![PyValueError::new_err("err")] - ))); - test_exception!(PyBaseException); - test_exception!(PyException); - test_exception!(PyStopAsyncIteration); - test_exception!(PyStopIteration); - test_exception!(PyGeneratorExit); - test_exception!(PyArithmeticError); - test_exception!(PyLookupError); - test_exception!(PyAssertionError); - test_exception!(PyAttributeError); - test_exception!(PyBufferError); - test_exception!(PyEOFError); - test_exception!(PyFloatingPointError); - test_exception!(PyOSError); - test_exception!(PyImportError); - test_exception!(PyModuleNotFoundError); - test_exception!(PyIndexError); - test_exception!(PyKeyError); - test_exception!(PyKeyboardInterrupt); - test_exception!(PyMemoryError); - test_exception!(PyNameError); - test_exception!(PyOverflowError); - test_exception!(PyRuntimeError); - test_exception!(PyRecursionError); - test_exception!(PyNotImplementedError); - test_exception!(PySyntaxError); - test_exception!(PyReferenceError); - test_exception!(PySystemError); - test_exception!(PySystemExit); - test_exception!(PyTypeError); - test_exception!(PyUnboundLocalError); - test_exception!(PyUnicodeError); - test_exception!(PyUnicodeDecodeError, |py| { - let invalid_utf8 = b"fo\xd8o"; - #[cfg_attr(invalid_from_utf8_lint, allow(invalid_from_utf8))] - let err = std::str::from_utf8(invalid_utf8).expect_err("should be invalid utf8"); - PyErr::from_value( - PyUnicodeDecodeError::new_utf8(py, invalid_utf8, err) - .unwrap() - .into_any(), - ) - }); - test_exception!(PyUnicodeEncodeError, |py| py - .eval(ffi::c_str!("chr(40960).encode('ascii')"), None, None) - .unwrap_err()); - test_exception!(PyUnicodeTranslateError, |_| { - PyUnicodeTranslateError::new_err(("\u{3042}", 0, 1, "ouch")) - }); - test_exception!(PyValueError); - test_exception!(PyZeroDivisionError); - test_exception!(PyBlockingIOError); - test_exception!(PyBrokenPipeError); - test_exception!(PyChildProcessError); - test_exception!(PyConnectionError); - test_exception!(PyConnectionAbortedError); - test_exception!(PyConnectionRefusedError); - test_exception!(PyConnectionResetError); - test_exception!(PyFileExistsError); - test_exception!(PyFileNotFoundError); - test_exception!(PyInterruptedError); - test_exception!(PyIsADirectoryError); - test_exception!(PyNotADirectoryError); - test_exception!(PyPermissionError); - test_exception!(PyProcessLookupError); - test_exception!(PyTimeoutError); - test_exception!(PyEnvironmentError); - test_exception!(PyIOError); - #[cfg(windows)] - test_exception!(PyWindowsError); - - test_exception!(PyWarning); - test_exception!(PyUserWarning); - test_exception!(PyDeprecationWarning); - test_exception!(PyPendingDeprecationWarning); - test_exception!(PySyntaxWarning); - test_exception!(PyRuntimeWarning); - test_exception!(PyFutureWarning); - test_exception!(PyImportWarning); - test_exception!(PyUnicodeWarning); - test_exception!(PyBytesWarning); - #[cfg(Py_3_10)] - test_exception!(PyEncodingWarning); -} diff --git a/vendor/pyo3/src/ffi/mod.rs b/vendor/pyo3/src/ffi/mod.rs deleted file mode 100644 index 53cb2695b8..0000000000 --- a/vendor/pyo3/src/ffi/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -//! Raw FFI declarations for Python's C API. -//! -//! This module provides low level bindings to the Python interpreter. -//! It is meant for advanced users only - regular PyO3 users shouldn't -//! need to interact with this module at all. -//! -//! The contents of this module are not documented here, as it would entail -//! basically copying the documentation from CPython. Consult the [Python/C API Reference -//! Manual][capi] for up-to-date documentation. -//! -//! # Safety -//! -//! The functions in this module lack individual safety documentation, but -//! generally the following apply: -//! - Pointer arguments have to point to a valid Python object of the correct type, -//! although null pointers are sometimes valid input. -//! - The vast majority can only be used safely while the GIL is held. -//! - Some functions have additional safety requirements, consult the -//! [Python/C API Reference Manual][capi] for more information. -//! -//! [capi]: https://docs.python.org/3/c-api/index.html - -#[cfg(test)] -mod tests; - -// reexport raw bindings exposed in pyo3_ffi -pub use pyo3_ffi::*; - -/// Helper to enable #\[pymethods\] to see the workaround for __ipow__ on Python 3.7 -#[doc(hidden)] -pub use crate::impl_::pymethods::ipowfunc; diff --git a/vendor/pyo3/src/ffi/tests.rs b/vendor/pyo3/src/ffi/tests.rs deleted file mode 100644 index 3396e40936..0000000000 --- a/vendor/pyo3/src/ffi/tests.rs +++ /dev/null @@ -1,324 +0,0 @@ -use crate::ffi::{self, *}; -use crate::types::any::PyAnyMethods; -use crate::Python; - -#[cfg(all(not(Py_LIMITED_API), any(not(PyPy), feature = "macros")))] -use crate::types::PyString; - -#[cfg(not(Py_LIMITED_API))] -use crate::{types::PyDict, Bound, PyAny}; -#[cfg(not(any(Py_3_12, Py_LIMITED_API)))] -use libc::wchar_t; - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -#[test] -fn test_datetime_fromtimestamp() { - use crate::IntoPyObject; - Python::with_gil(|py| { - let args = (100,).into_pyobject(py).unwrap(); - let dt = unsafe { - PyDateTime_IMPORT(); - Bound::from_owned_ptr(py, PyDateTime_FromTimestamp(args.as_ptr())) - }; - let locals = PyDict::new(py); - locals.set_item("dt", dt).unwrap(); - py.run( - ffi::c_str!("import datetime; assert dt == datetime.datetime.fromtimestamp(100)"), - None, - Some(&locals), - ) - .unwrap(); - }) -} - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -#[test] -fn test_date_fromtimestamp() { - use crate::IntoPyObject; - Python::with_gil(|py| { - let args = (100,).into_pyobject(py).unwrap(); - let dt = unsafe { - PyDateTime_IMPORT(); - Bound::from_owned_ptr(py, PyDate_FromTimestamp(args.as_ptr())) - }; - let locals = PyDict::new(py); - locals.set_item("dt", dt).unwrap(); - py.run( - ffi::c_str!("import datetime; assert dt == datetime.date.fromtimestamp(100)"), - None, - Some(&locals), - ) - .unwrap(); - }) -} - -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -#[test] -fn test_utc_timezone() { - Python::with_gil(|py| { - let utc_timezone: Bound<'_, PyAny> = unsafe { - PyDateTime_IMPORT(); - Bound::from_borrowed_ptr(py, PyDateTime_TimeZone_UTC()) - }; - let locals = PyDict::new(py); - locals.set_item("utc_timezone", utc_timezone).unwrap(); - py.run( - ffi::c_str!("import datetime; assert utc_timezone is datetime.timezone.utc"), - None, - Some(&locals), - ) - .unwrap(); - }) -} - -#[test] -#[cfg(not(Py_LIMITED_API))] -#[cfg(feature = "macros")] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -fn test_timezone_from_offset() { - use crate::{ffi_ptr_ext::FfiPtrExt, types::PyDelta}; - - Python::with_gil(|py| { - let delta = PyDelta::new(py, 0, 100, 0, false).unwrap(); - let tz = unsafe { PyTimeZone_FromOffset(delta.as_ptr()).assume_owned(py) }; - crate::py_run!( - py, - tz, - "import datetime; assert tz == datetime.timezone(datetime.timedelta(seconds=100))" - ); - }) -} - -#[test] -#[cfg(not(Py_LIMITED_API))] -#[cfg(feature = "macros")] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -fn test_timezone_from_offset_and_name() { - use crate::{ffi_ptr_ext::FfiPtrExt, types::PyDelta}; - - Python::with_gil(|py| { - let delta = PyDelta::new(py, 0, 100, 0, false).unwrap(); - let tzname = PyString::new(py, "testtz"); - let tz = unsafe { - PyTimeZone_FromOffsetAndName(delta.as_ptr(), tzname.as_ptr()).assume_owned(py) - }; - crate::py_run!( - py, - tz, - "import datetime; assert tz == datetime.timezone(datetime.timedelta(seconds=100), 'testtz')" - ); - }) -} - -#[test] -#[cfg(not(Py_LIMITED_API))] -fn ascii_object_bitfield() { - let ob_base: PyObject = unsafe { std::mem::zeroed() }; - - let mut o = PyASCIIObject { - ob_base, - length: 0, - #[cfg(not(PyPy))] - hash: 0, - state: 0u32, - #[cfg(not(Py_3_12))] - wstr: std::ptr::null_mut() as *mut wchar_t, - }; - - unsafe { - assert_eq!(o.interned(), 0); - assert_eq!(o.kind(), 0); - assert_eq!(o.compact(), 0); - assert_eq!(o.ascii(), 0); - #[cfg(not(Py_3_12))] - assert_eq!(o.ready(), 0); - - let interned_count = if cfg!(Py_3_12) { 2 } else { 4 }; - - for i in 0..interned_count { - o.set_interned(i); - assert_eq!(o.interned(), i); - } - - for i in 0..8 { - o.set_kind(i); - assert_eq!(o.kind(), i); - } - - o.set_compact(1); - assert_eq!(o.compact(), 1); - - o.set_ascii(1); - assert_eq!(o.ascii(), 1); - - #[cfg(not(Py_3_12))] - o.set_ready(1); - #[cfg(not(Py_3_12))] - assert_eq!(o.ready(), 1); - } -} - -#[test] -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -fn ascii() { - Python::with_gil(|py| { - // This test relies on implementation details of PyString. - let s = PyString::new(py, "hello, world"); - let ptr = s.as_ptr(); - - unsafe { - let ascii_ptr = ptr as *mut PyASCIIObject; - let ascii = ascii_ptr.as_ref().unwrap(); - - assert_eq!(ascii.interned(), 0); - assert_eq!(ascii.kind(), PyUnicode_1BYTE_KIND); - assert_eq!(ascii.compact(), 1); - assert_eq!(ascii.ascii(), 1); - #[cfg(not(Py_3_12))] - assert_eq!(ascii.ready(), 1); - - assert_eq!(PyUnicode_IS_ASCII(ptr), 1); - assert_eq!(PyUnicode_IS_COMPACT(ptr), 1); - assert_eq!(PyUnicode_IS_COMPACT_ASCII(ptr), 1); - - assert!(!PyUnicode_1BYTE_DATA(ptr).is_null()); - // 2 and 4 byte macros return nonsense for this string instance. - assert_eq!(PyUnicode_KIND(ptr), PyUnicode_1BYTE_KIND); - - assert!(!_PyUnicode_COMPACT_DATA(ptr).is_null()); - // _PyUnicode_NONCOMPACT_DATA isn't valid for compact strings. - assert!(!PyUnicode_DATA(ptr).is_null()); - - assert_eq!(PyUnicode_GET_LENGTH(ptr), s.len().unwrap() as Py_ssize_t); - assert_eq!(PyUnicode_IS_READY(ptr), 1); - - // This has potential to mutate object. But it should be a no-op since - // we're already ready. - assert_eq!(PyUnicode_READY(ptr), 0); - } - }) -} - -#[test] -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -fn ucs4() { - Python::with_gil(|py| { - let s = "哈哈🐈"; - let py_string = PyString::new(py, s); - let ptr = py_string.as_ptr(); - - unsafe { - let ascii_ptr = ptr as *mut PyASCIIObject; - let ascii = ascii_ptr.as_ref().unwrap(); - - assert_eq!(ascii.interned(), 0); - assert_eq!(ascii.kind(), PyUnicode_4BYTE_KIND); - assert_eq!(ascii.compact(), 1); - assert_eq!(ascii.ascii(), 0); - #[cfg(not(Py_3_12))] - assert_eq!(ascii.ready(), 1); - - assert_eq!(PyUnicode_IS_ASCII(ptr), 0); - assert_eq!(PyUnicode_IS_COMPACT(ptr), 1); - assert_eq!(PyUnicode_IS_COMPACT_ASCII(ptr), 0); - - assert!(!PyUnicode_4BYTE_DATA(ptr).is_null()); - assert_eq!(PyUnicode_KIND(ptr), PyUnicode_4BYTE_KIND); - - assert!(!_PyUnicode_COMPACT_DATA(ptr).is_null()); - // _PyUnicode_NONCOMPACT_DATA isn't valid for compact strings. - assert!(!PyUnicode_DATA(ptr).is_null()); - - assert_eq!( - PyUnicode_GET_LENGTH(ptr), - py_string.len().unwrap() as Py_ssize_t - ); - assert_eq!(PyUnicode_IS_READY(ptr), 1); - - // This has potential to mutate object. But it should be a no-op since - // we're already ready. - assert_eq!(PyUnicode_READY(ptr), 0); - } - }) -} - -#[test] -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons -#[cfg(not(PyPy))] -fn test_get_tzinfo() { - use crate::types::timezone_utc; - - crate::Python::with_gil(|py| { - use crate::types::{PyDateTime, PyTime}; - - let utc = &timezone_utc(py); - - let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, Some(utc)).unwrap(); - - assert!( - unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) } - .is(utc) - ); - - let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, None).unwrap(); - - assert!( - unsafe { Bound::from_borrowed_ptr(py, PyDateTime_DATE_GET_TZINFO(dt.as_ptr())) } - .is_none() - ); - - let t = PyTime::new(py, 0, 0, 0, 0, Some(utc)).unwrap(); - - assert!( - unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) }.is(utc) - ); - - let t = PyTime::new(py, 0, 0, 0, 0, None).unwrap(); - - assert!( - unsafe { Bound::from_borrowed_ptr(py, PyDateTime_TIME_GET_TZINFO(t.as_ptr())) } - .is_none() - ); - }) -} - -#[test] -fn test_inc_dec_ref() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - - let ref_count = obj.get_refcnt(); - let ptr = obj.as_ptr(); - - unsafe { Py_INCREF(ptr) }; - - assert_eq!(obj.get_refcnt(), ref_count + 1); - - unsafe { Py_DECREF(ptr) }; - - assert_eq!(obj.get_refcnt(), ref_count); - }) -} - -#[test] -#[cfg(Py_3_12)] -fn test_inc_dec_ref_immortal() { - Python::with_gil(|py| { - let obj = py.None(); - - let ref_count = obj.get_refcnt(py); - let ptr = obj.as_ptr(); - - unsafe { Py_INCREF(ptr) }; - - assert_eq!(obj.get_refcnt(py), ref_count); - - unsafe { Py_DECREF(ptr) }; - - assert_eq!(obj.get_refcnt(py), ref_count); - }) -} diff --git a/vendor/pyo3/src/ffi_ptr_ext.rs b/vendor/pyo3/src/ffi_ptr_ext.rs deleted file mode 100644 index 956b4e8406..0000000000 --- a/vendor/pyo3/src/ffi_ptr_ext.rs +++ /dev/null @@ -1,85 +0,0 @@ -use crate::sealed::Sealed; -use crate::{ - ffi, - instance::{Borrowed, Bound}, - PyAny, PyResult, Python, -}; - -pub(crate) trait FfiPtrExt: Sealed { - /// Assumes this pointer carries a Python reference which needs to be decref'd. - /// - /// If the pointer is NULL, this function will fetch an error. - unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult>; - - /// Same as `assume_owned_or_err`, but doesn't fetch an error on NULL. - unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option>; - - /// Same as `assume_owned_or_err`, but panics on NULL. - unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny>; - - /// Same as `assume_owned_or_err`, but does not check for NULL. - unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny>; - - /// Assumes this pointer is borrowed from a parent object. - /// - /// Warning: the lifetime `'a` is not bounded by the function arguments; the caller is - /// responsible to ensure this is tied to some appropriate lifetime. - unsafe fn assume_borrowed_or_err<'a>(self, py: Python<'_>) - -> PyResult>; - - /// Same as `assume_borrowed_or_err`, but doesn't fetch an error on NULL. - unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option>; - - /// Same as `assume_borrowed_or_err`, but panics on NULL. - unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>; - - /// Same as `assume_borrowed_or_err`, but does not check for NULL. - unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny>; -} - -impl FfiPtrExt for *mut ffi::PyObject { - #[inline] - unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult> { - Bound::from_owned_ptr_or_err(py, self) - } - - #[inline] - unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option> { - Bound::from_owned_ptr_or_opt(py, self) - } - - #[inline] - #[track_caller] - unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> { - Bound::from_owned_ptr(py, self) - } - - #[inline] - unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny> { - Bound::from_owned_ptr_unchecked(py, self) - } - - #[inline] - unsafe fn assume_borrowed_or_err<'a>( - self, - py: Python<'_>, - ) -> PyResult> { - Borrowed::from_ptr_or_err(py, self) - } - - #[inline] - unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option> { - Borrowed::from_ptr_or_opt(py, self) - } - - #[inline] - #[track_caller] - unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> { - Borrowed::from_ptr(py, self) - } - - #[inline] - unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> { - Borrowed::from_ptr_unchecked(py, self) - } -} diff --git a/vendor/pyo3/src/gil.rs b/vendor/pyo3/src/gil.rs deleted file mode 100644 index d7e6daae91..0000000000 --- a/vendor/pyo3/src/gil.rs +++ /dev/null @@ -1,666 +0,0 @@ -//! Interaction with Python's global interpreter lock - -#[cfg(pyo3_disable_reference_pool)] -use crate::impl_::panic::PanicTrap; -use crate::{ffi, Python}; -#[cfg(not(pyo3_disable_reference_pool))] -use once_cell::sync::Lazy; -use std::cell::Cell; -use std::{mem, ptr::NonNull, sync}; - -static START: sync::Once = sync::Once::new(); - -std::thread_local! { - /// This is an internal counter in pyo3 monitoring whether this thread has the GIL. - /// - /// It will be incremented whenever a GILGuard or GILPool is created, and decremented whenever - /// they are dropped. - /// - /// As a result, if this thread has the GIL, GIL_COUNT is greater than zero. - /// - /// Additionally, we sometimes need to prevent safe access to the GIL, - /// e.g. when implementing `__traverse__`, which is represented by a negative value. - static GIL_COUNT: Cell = const { Cell::new(0) }; -} - -const GIL_LOCKED_DURING_TRAVERSE: isize = -1; - -/// Checks whether the GIL is acquired. -/// -/// Note: This uses pyo3's internal count rather than PyGILState_Check for two reasons: -/// 1) for performance -/// 2) PyGILState_Check always returns 1 if the sub-interpreter APIs have ever been called, -/// which could lead to incorrect conclusions that the GIL is held. -#[inline(always)] -fn gil_is_acquired() -> bool { - GIL_COUNT.try_with(|c| c.get() > 0).unwrap_or(false) -} - -/// Prepares the use of Python in a free-threaded context. -/// -/// If the Python interpreter is not already initialized, this function will initialize it with -/// signal handling disabled (Python will not raise the `KeyboardInterrupt` exception). Python -/// signal handling depends on the notion of a 'main thread', which must be the thread that -/// initializes the Python interpreter. -/// -/// If the Python interpreter is already initialized, this function has no effect. -/// -/// This function is unavailable under PyPy because PyPy cannot be embedded in Rust (or any other -/// software). Support for this is tracked on the -/// [PyPy issue tracker](https://github.com/pypy/pypy/issues/3836). -/// -/// # Examples -/// ```rust -/// use pyo3::prelude::*; -/// -/// # fn main() -> PyResult<()> { -/// pyo3::prepare_freethreaded_python(); -/// Python::with_gil(|py| py.run(pyo3::ffi::c_str!("print('Hello World')"), None, None)) -/// # } -/// ``` -#[cfg(not(any(PyPy, GraalPy)))] -pub fn prepare_freethreaded_python() { - // Protect against race conditions when Python is not yet initialized and multiple threads - // concurrently call 'prepare_freethreaded_python()'. Note that we do not protect against - // concurrent initialization of the Python runtime by other users of the Python C API. - START.call_once_force(|_| unsafe { - // Use call_once_force because if initialization panics, it's okay to try again. - if ffi::Py_IsInitialized() == 0 { - ffi::Py_InitializeEx(0); - - // Release the GIL. - ffi::PyEval_SaveThread(); - } - }); -} - -/// Executes the provided closure with an embedded Python interpreter. -/// -/// This function initializes the Python interpreter, executes the provided closure, and then -/// finalizes the Python interpreter. -/// -/// After execution all Python resources are cleaned up, and no further Python APIs can be called. -/// Because many Python modules implemented in C do not support multiple Python interpreters in a -/// single process, it is not safe to call this function more than once. (Many such modules will not -/// initialize correctly on the second run.) -/// -/// # Panics -/// - If the Python interpreter is already initialized before calling this function. -/// -/// # Safety -/// - This function should only ever be called once per process (usually as part of the `main` -/// function). It is also not thread-safe. -/// - No Python APIs can be used after this function has finished executing. -/// - The return value of the closure must not contain any Python value, _including_ `PyResult`. -/// -/// # Examples -/// -/// ```rust -/// unsafe { -/// pyo3::with_embedded_python_interpreter(|py| { -/// if let Err(e) = py.run(pyo3::ffi::c_str!("print('Hello World')"), None, None) { -/// // We must make sure to not return a `PyErr`! -/// e.print(py); -/// } -/// }); -/// } -/// ``` -#[cfg(not(any(PyPy, GraalPy)))] -pub unsafe fn with_embedded_python_interpreter(f: F) -> R -where - F: for<'p> FnOnce(Python<'p>) -> R, -{ - assert_eq!( - ffi::Py_IsInitialized(), - 0, - "called `with_embedded_python_interpreter` but a Python interpreter is already running." - ); - - ffi::Py_InitializeEx(0); - - let result = { - let guard = GILGuard::assume(); - let py = guard.python(); - // Import the threading module - this ensures that it will associate this thread as the "main" - // thread, which is important to avoid an `AssertionError` at finalization. - py.import("threading").unwrap(); - - // Execute the closure. - f(py) - }; - - // Finalize the Python interpreter. - ffi::Py_Finalize(); - - result -} - -/// RAII type that represents the Global Interpreter Lock acquisition. -pub(crate) enum GILGuard { - /// Indicates the GIL was already held with this GILGuard was acquired. - Assumed, - /// Indicates that we actually acquired the GIL when this GILGuard was acquired - Ensured { gstate: ffi::PyGILState_STATE }, -} - -impl GILGuard { - /// PyO3 internal API for acquiring the GIL. The public API is Python::with_gil. - /// - /// If the GIL was already acquired via PyO3, this returns - /// `GILGuard::Assumed`. Otherwise, the GIL will be acquired and - /// `GILGuard::Ensured` will be returned. - pub(crate) fn acquire() -> Self { - if gil_is_acquired() { - // SAFETY: We just checked that the GIL is already acquired. - return unsafe { Self::assume() }; - } - - // Maybe auto-initialize the GIL: - // - If auto-initialize feature set and supported, try to initialize the interpreter. - // - If the auto-initialize feature is set but unsupported, emit hard errors only when the - // extension-module feature is not activated - extension modules don't care about - // auto-initialize so this avoids breaking existing builds. - // - Otherwise, just check the GIL is initialized. - cfg_if::cfg_if! { - if #[cfg(all(feature = "auto-initialize", not(any(PyPy, GraalPy))))] { - prepare_freethreaded_python(); - } else { - // This is a "hack" to make running `cargo test` for PyO3 convenient (i.e. no need - // to specify `--features auto-initialize` manually. Tests within the crate itself - // all depend on the auto-initialize feature for conciseness but Cargo does not - // provide a mechanism to specify required features for tests. - #[cfg(not(any(PyPy, GraalPy)))] - if option_env!("CARGO_PRIMARY_PACKAGE").is_some() { - prepare_freethreaded_python(); - } - - START.call_once_force(|_| unsafe { - // Use call_once_force because if there is a panic because the interpreter is - // not initialized, it's fine for the user to initialize the interpreter and - // retry. - assert_ne!( - ffi::Py_IsInitialized(), - 0, - "The Python interpreter is not initialized and the `auto-initialize` \ - feature is not enabled.\n\n\ - Consider calling `pyo3::prepare_freethreaded_python()` before attempting \ - to use Python APIs." - ); - }); - } - } - - // SAFETY: We have ensured the Python interpreter is initialized. - unsafe { Self::acquire_unchecked() } - } - - /// Acquires the `GILGuard` without performing any state checking. - /// - /// This can be called in "unsafe" contexts where the normal interpreter state - /// checking performed by `GILGuard::acquire` may fail. This includes calling - /// as part of multi-phase interpreter initialization. - pub(crate) unsafe fn acquire_unchecked() -> Self { - if gil_is_acquired() { - return Self::assume(); - } - - let gstate = ffi::PyGILState_Ensure(); // acquire GIL - increment_gil_count(); - - #[cfg(not(pyo3_disable_reference_pool))] - if let Some(pool) = Lazy::get(&POOL) { - pool.update_counts(Python::assume_gil_acquired()); - } - GILGuard::Ensured { gstate } - } - - /// Acquires the `GILGuard` while assuming that the GIL is already held. - pub(crate) unsafe fn assume() -> Self { - increment_gil_count(); - let guard = GILGuard::Assumed; - #[cfg(not(pyo3_disable_reference_pool))] - if let Some(pool) = Lazy::get(&POOL) { - pool.update_counts(guard.python()); - } - guard - } - - /// Gets the Python token associated with this [`GILGuard`]. - #[inline] - pub fn python(&self) -> Python<'_> { - unsafe { Python::assume_gil_acquired() } - } -} - -/// The Drop implementation for `GILGuard` will release the GIL. -impl Drop for GILGuard { - fn drop(&mut self) { - match self { - GILGuard::Assumed => {} - GILGuard::Ensured { gstate } => unsafe { - // Drop the objects in the pool before attempting to release the thread state - ffi::PyGILState_Release(*gstate); - }, - } - decrement_gil_count(); - } -} - -// Vector of PyObject -type PyObjVec = Vec>; - -#[cfg(not(pyo3_disable_reference_pool))] -/// Thread-safe storage for objects which were dec_ref while the GIL was not held. -struct ReferencePool { - pending_decrefs: sync::Mutex, -} - -#[cfg(not(pyo3_disable_reference_pool))] -impl ReferencePool { - const fn new() -> Self { - Self { - pending_decrefs: sync::Mutex::new(Vec::new()), - } - } - - fn register_decref(&self, obj: NonNull) { - self.pending_decrefs.lock().unwrap().push(obj); - } - - fn update_counts(&self, _py: Python<'_>) { - let mut pending_decrefs = self.pending_decrefs.lock().unwrap(); - if pending_decrefs.is_empty() { - return; - } - - let decrefs = mem::take(&mut *pending_decrefs); - drop(pending_decrefs); - - for ptr in decrefs { - unsafe { ffi::Py_DECREF(ptr.as_ptr()) }; - } - } -} - -#[cfg(not(pyo3_disable_reference_pool))] -unsafe impl Send for ReferencePool {} - -#[cfg(not(pyo3_disable_reference_pool))] -unsafe impl Sync for ReferencePool {} - -#[cfg(not(pyo3_disable_reference_pool))] -static POOL: Lazy = Lazy::new(ReferencePool::new); - -/// A guard which can be used to temporarily release the GIL and restore on `Drop`. -pub(crate) struct SuspendGIL { - count: isize, - tstate: *mut ffi::PyThreadState, -} - -impl SuspendGIL { - pub(crate) unsafe fn new() -> Self { - let count = GIL_COUNT.with(|c| c.replace(0)); - let tstate = ffi::PyEval_SaveThread(); - - Self { count, tstate } - } -} - -impl Drop for SuspendGIL { - fn drop(&mut self) { - GIL_COUNT.with(|c| c.set(self.count)); - unsafe { - ffi::PyEval_RestoreThread(self.tstate); - - // Update counts of PyObjects / Py that were cloned or dropped while the GIL was released. - #[cfg(not(pyo3_disable_reference_pool))] - if let Some(pool) = Lazy::get(&POOL) { - pool.update_counts(Python::assume_gil_acquired()); - } - } - } -} - -/// Used to lock safe access to the GIL -pub(crate) struct LockGIL { - count: isize, -} - -impl LockGIL { - /// Lock access to the GIL while an implementation of `__traverse__` is running - pub fn during_traverse() -> Self { - Self::new(GIL_LOCKED_DURING_TRAVERSE) - } - - fn new(reason: isize) -> Self { - let count = GIL_COUNT.with(|c| c.replace(reason)); - - Self { count } - } - - #[cold] - fn bail(current: isize) { - match current { - GIL_LOCKED_DURING_TRAVERSE => panic!( - "Access to the GIL is prohibited while a __traverse__ implmentation is running." - ), - _ => panic!("Access to the GIL is currently prohibited."), - } - } -} - -impl Drop for LockGIL { - fn drop(&mut self) { - GIL_COUNT.with(|c| c.set(self.count)); - } -} - -/// Increments the reference count of a Python object if the GIL is held. If -/// the GIL is not held, this function will panic. -/// -/// # Safety -/// The object must be an owned Python reference. -#[cfg(feature = "py-clone")] -#[track_caller] -pub unsafe fn register_incref(obj: NonNull) { - if gil_is_acquired() { - ffi::Py_INCREF(obj.as_ptr()) - } else { - panic!("Cannot clone pointer into Python heap without the GIL being held."); - } -} - -/// Registers a Python object pointer inside the release pool, to have its reference count decreased -/// the next time the GIL is acquired in pyo3. -/// -/// If the GIL is held, the reference count will be decreased immediately instead of being queued -/// for later. -/// -/// # Safety -/// The object must be an owned Python reference. -#[track_caller] -pub unsafe fn register_decref(obj: NonNull) { - if gil_is_acquired() { - ffi::Py_DECREF(obj.as_ptr()) - } else { - #[cfg(not(pyo3_disable_reference_pool))] - POOL.register_decref(obj); - #[cfg(all( - pyo3_disable_reference_pool, - not(pyo3_leak_on_drop_without_reference_pool) - ))] - { - let _trap = PanicTrap::new("Aborting the process to avoid panic-from-drop."); - panic!("Cannot drop pointer into Python heap without the GIL being held."); - } - } -} - -/// Increments pyo3's internal GIL count - to be called whenever GILPool or GILGuard is created. -#[inline(always)] -fn increment_gil_count() { - // Ignores the error in case this function called from `atexit`. - let _ = GIL_COUNT.try_with(|c| { - let current = c.get(); - if current < 0 { - LockGIL::bail(current); - } - c.set(current + 1); - }); -} - -/// Decrements pyo3's internal GIL count - to be called whenever GILPool or GILGuard is dropped. -#[inline(always)] -fn decrement_gil_count() { - // Ignores the error in case this function called from `atexit`. - let _ = GIL_COUNT.try_with(|c| { - let current = c.get(); - debug_assert!( - current > 0, - "Negative GIL count detected. Please report this error to the PyO3 repo as a bug." - ); - c.set(current - 1); - }); -} - -#[cfg(test)] -mod tests { - use super::GIL_COUNT; - #[cfg(not(pyo3_disable_reference_pool))] - use super::{gil_is_acquired, POOL}; - use crate::{ffi, PyObject, Python}; - use crate::{gil::GILGuard, types::any::PyAnyMethods}; - use std::ptr::NonNull; - - fn get_object(py: Python<'_>) -> PyObject { - py.eval(ffi::c_str!("object()"), None, None) - .unwrap() - .unbind() - } - - #[cfg(not(pyo3_disable_reference_pool))] - fn pool_dec_refs_does_not_contain(obj: &PyObject) -> bool { - !POOL - .pending_decrefs - .lock() - .unwrap() - .contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) }) - } - - // with no GIL, threads can empty the POOL at any time, so this - // function does not test anything meaningful - #[cfg(not(any(pyo3_disable_reference_pool, Py_GIL_DISABLED)))] - fn pool_dec_refs_contains(obj: &PyObject) -> bool { - POOL.pending_decrefs - .lock() - .unwrap() - .contains(&unsafe { NonNull::new_unchecked(obj.as_ptr()) }) - } - - #[test] - fn test_pyobject_drop_with_gil_decreases_refcnt() { - Python::with_gil(|py| { - let obj = get_object(py); - - // Create a reference to drop with the GIL. - let reference = obj.clone_ref(py); - - assert_eq!(obj.get_refcnt(py), 2); - #[cfg(not(pyo3_disable_reference_pool))] - assert!(pool_dec_refs_does_not_contain(&obj)); - - // With the GIL held, reference count will be decreased immediately. - drop(reference); - - assert_eq!(obj.get_refcnt(py), 1); - #[cfg(not(any(pyo3_disable_reference_pool)))] - assert!(pool_dec_refs_does_not_contain(&obj)); - }); - } - - #[test] - #[cfg(all(not(pyo3_disable_reference_pool), not(target_arch = "wasm32")))] // We are building wasm Python with pthreads disabled - fn test_pyobject_drop_without_gil_doesnt_decrease_refcnt() { - let obj = Python::with_gil(|py| { - let obj = get_object(py); - // Create a reference to drop without the GIL. - let reference = obj.clone_ref(py); - - assert_eq!(obj.get_refcnt(py), 2); - assert!(pool_dec_refs_does_not_contain(&obj)); - - // Drop reference in a separate thread which doesn't have the GIL. - std::thread::spawn(move || drop(reference)).join().unwrap(); - - // The reference count should not have changed (the GIL has always - // been held by this thread), it is remembered to release later. - assert_eq!(obj.get_refcnt(py), 2); - #[cfg(not(Py_GIL_DISABLED))] - assert!(pool_dec_refs_contains(&obj)); - obj - }); - - // Next time the GIL is acquired, the reference is released - #[allow(unused)] - Python::with_gil(|py| { - // with no GIL, another thread could still be processing - // DECREFs after releasing the lock on the POOL, so the - // refcnt could still be 2 when this assert happens - #[cfg(not(Py_GIL_DISABLED))] - assert_eq!(obj.get_refcnt(py), 1); - assert!(pool_dec_refs_does_not_contain(&obj)); - }); - } - - #[test] - #[allow(deprecated)] - fn test_gil_counts() { - // Check with_gil and GILGuard both increase counts correctly - let get_gil_count = || GIL_COUNT.with(|c| c.get()); - - assert_eq!(get_gil_count(), 0); - Python::with_gil(|_| { - assert_eq!(get_gil_count(), 1); - - let pool = unsafe { GILGuard::assume() }; - assert_eq!(get_gil_count(), 2); - - let pool2 = unsafe { GILGuard::assume() }; - assert_eq!(get_gil_count(), 3); - - drop(pool); - assert_eq!(get_gil_count(), 2); - - Python::with_gil(|_| { - // nested with_gil updates gil count - assert_eq!(get_gil_count(), 3); - }); - assert_eq!(get_gil_count(), 2); - - drop(pool2); - assert_eq!(get_gil_count(), 1); - }); - assert_eq!(get_gil_count(), 0); - } - - #[test] - fn test_allow_threads() { - assert!(!gil_is_acquired()); - - Python::with_gil(|py| { - assert!(gil_is_acquired()); - - py.allow_threads(move || { - assert!(!gil_is_acquired()); - - Python::with_gil(|_| assert!(gil_is_acquired())); - - assert!(!gil_is_acquired()); - }); - - assert!(gil_is_acquired()); - }); - - assert!(!gil_is_acquired()); - } - - #[cfg(feature = "py-clone")] - #[test] - #[should_panic] - fn test_allow_threads_updates_refcounts() { - Python::with_gil(|py| { - // Make a simple object with 1 reference - let obj = get_object(py); - assert!(obj.get_refcnt(py) == 1); - // Clone the object without the GIL which should panic - py.allow_threads(|| obj.clone()); - }); - } - - #[test] - fn dropping_gil_does_not_invalidate_references() { - // Acquiring GIL for the second time should be safe - see #864 - Python::with_gil(|py| { - let obj = Python::with_gil(|_| py.eval(ffi::c_str!("object()"), None, None).unwrap()); - - // After gil2 drops, obj should still have a reference count of one - assert_eq!(obj.get_refcnt(), 1); - }) - } - - #[cfg(feature = "py-clone")] - #[test] - fn test_clone_with_gil() { - Python::with_gil(|py| { - let obj = get_object(py); - let count = obj.get_refcnt(py); - - // Cloning with the GIL should increase reference count immediately - #[allow(clippy::redundant_clone)] - let c = obj.clone(); - assert_eq!(count + 1, c.get_refcnt(py)); - }) - } - - #[test] - #[cfg(not(pyo3_disable_reference_pool))] - fn test_update_counts_does_not_deadlock() { - // update_counts can run arbitrary Python code during Py_DECREF. - // if the locking is implemented incorrectly, it will deadlock. - - use crate::ffi; - use crate::gil::GILGuard; - - Python::with_gil(|py| { - let obj = get_object(py); - - unsafe extern "C" fn capsule_drop(capsule: *mut ffi::PyObject) { - // This line will implicitly call update_counts - // -> and so cause deadlock if update_counts is not handling recursion correctly. - let pool = GILGuard::assume(); - - // Rebuild obj so that it can be dropped - PyObject::from_owned_ptr( - pool.python(), - ffi::PyCapsule_GetPointer(capsule, std::ptr::null()) as _, - ); - } - - let ptr = obj.into_ptr(); - - let capsule = - unsafe { ffi::PyCapsule_New(ptr as _, std::ptr::null(), Some(capsule_drop)) }; - - POOL.register_decref(NonNull::new(capsule).unwrap()); - - // Updating the counts will call decref on the capsule, which calls capsule_drop - POOL.update_counts(py); - }) - } - - #[test] - #[cfg(not(pyo3_disable_reference_pool))] - fn test_gil_guard_update_counts() { - use crate::gil::GILGuard; - - Python::with_gil(|py| { - let obj = get_object(py); - - // For GILGuard::acquire - - POOL.register_decref(NonNull::new(obj.clone_ref(py).into_ptr()).unwrap()); - #[cfg(not(Py_GIL_DISABLED))] - assert!(pool_dec_refs_contains(&obj)); - let _guard = GILGuard::acquire(); - assert!(pool_dec_refs_does_not_contain(&obj)); - - // For GILGuard::assume - - POOL.register_decref(NonNull::new(obj.clone_ref(py).into_ptr()).unwrap()); - #[cfg(not(Py_GIL_DISABLED))] - assert!(pool_dec_refs_contains(&obj)); - let _guard2 = unsafe { GILGuard::assume() }; - assert!(pool_dec_refs_does_not_contain(&obj)); - }) - } -} diff --git a/vendor/pyo3/src/impl_.rs b/vendor/pyo3/src/impl_.rs deleted file mode 100644 index 7ac7139985..0000000000 --- a/vendor/pyo3/src/impl_.rs +++ /dev/null @@ -1,26 +0,0 @@ -#![allow(missing_docs)] - -//! Internals of PyO3 which are accessed by code expanded from PyO3's procedural macros. -//! -//! Usage of any of these APIs in downstream code is implicitly acknowledging that these -//! APIs may may change at any time without documentation in the CHANGELOG and without -//! breaking semver guarantees. - -pub mod callback; -#[cfg(feature = "experimental-async")] -pub mod coroutine; -pub mod exceptions; -pub mod extract_argument; -pub mod freelist; -pub mod frompyobject; -pub(crate) mod not_send; -pub mod panic; -pub mod pycell; -pub mod pyclass; -pub mod pyclass_init; -pub mod pyfunction; -pub mod pymethods; -pub mod pymodule; -#[doc(hidden)] -pub mod trampoline; -pub mod wrap; diff --git a/vendor/pyo3/src/impl_/callback.rs b/vendor/pyo3/src/impl_/callback.rs deleted file mode 100644 index 9f06340bc5..0000000000 --- a/vendor/pyo3/src/impl_/callback.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! Utilities for a Python callable object that invokes a Rust function. - -use crate::err::{PyErr, PyResult}; -use crate::exceptions::PyOverflowError; -use crate::ffi::{self, Py_hash_t}; -use crate::{BoundObject, IntoPyObject, PyObject, Python}; -use std::os::raw::c_int; - -/// A type which can be the return type of a python C-API callback -pub trait PyCallbackOutput: Copy { - /// The error value to return to python if the callback raised an exception - const ERR_VALUE: Self; -} - -impl PyCallbackOutput for *mut ffi::PyObject { - const ERR_VALUE: Self = std::ptr::null_mut(); -} - -impl PyCallbackOutput for std::os::raw::c_int { - const ERR_VALUE: Self = -1; -} - -impl PyCallbackOutput for ffi::Py_ssize_t { - const ERR_VALUE: Self = -1; -} - -/// Convert the result of callback function into the appropriate return value. -pub trait IntoPyCallbackOutput<'py, Target> { - fn convert(self, py: Python<'py>) -> PyResult; -} - -impl<'py, T, E, U> IntoPyCallbackOutput<'py, U> for Result -where - T: IntoPyCallbackOutput<'py, U>, - E: Into, -{ - #[inline] - fn convert(self, py: Python<'py>) -> PyResult { - match self { - Ok(v) => v.convert(py), - Err(e) => Err(e.into()), - } - } -} - -impl<'py, T> IntoPyCallbackOutput<'py, *mut ffi::PyObject> for T -where - T: IntoPyObject<'py>, -{ - #[inline] - fn convert(self, py: Python<'py>) -> PyResult<*mut ffi::PyObject> { - self.into_pyobject(py) - .map(BoundObject::into_ptr) - .map_err(Into::into) - } -} - -impl IntoPyCallbackOutput<'_, Self> for *mut ffi::PyObject { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult { - Ok(self) - } -} - -impl IntoPyCallbackOutput<'_, std::os::raw::c_int> for () { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult { - Ok(0) - } -} - -impl IntoPyCallbackOutput<'_, std::os::raw::c_int> for bool { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult { - Ok(self as c_int) - } -} - -impl IntoPyCallbackOutput<'_, ()> for () { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult<()> { - Ok(()) - } -} - -impl IntoPyCallbackOutput<'_, ffi::Py_ssize_t> for usize { - #[inline] - fn convert(self, _py: Python<'_>) -> PyResult { - self.try_into().map_err(|_err| PyOverflowError::new_err(())) - } -} - -// Converters needed for `#[pyproto]` implementations - -impl IntoPyCallbackOutput<'_, bool> for bool { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult { - Ok(self) - } -} - -impl IntoPyCallbackOutput<'_, usize> for usize { - #[inline] - fn convert(self, _: Python<'_>) -> PyResult { - Ok(self) - } -} - -impl<'py, T> IntoPyCallbackOutput<'py, PyObject> for T -where - T: IntoPyObject<'py>, -{ - #[inline] - fn convert(self, py: Python<'py>) -> PyResult { - self.into_pyobject(py) - .map(BoundObject::into_any) - .map(BoundObject::unbind) - .map_err(Into::into) - } -} - -pub trait WrappingCastTo { - fn wrapping_cast(self) -> T; -} - -macro_rules! wrapping_cast { - ($from:ty, $to:ty) => { - impl WrappingCastTo<$to> for $from { - #[inline] - fn wrapping_cast(self) -> $to { - self as $to - } - } - }; -} -wrapping_cast!(u8, Py_hash_t); -wrapping_cast!(u16, Py_hash_t); -wrapping_cast!(u32, Py_hash_t); -wrapping_cast!(usize, Py_hash_t); -wrapping_cast!(u64, Py_hash_t); -wrapping_cast!(i8, Py_hash_t); -wrapping_cast!(i16, Py_hash_t); -wrapping_cast!(i32, Py_hash_t); -wrapping_cast!(isize, Py_hash_t); -wrapping_cast!(i64, Py_hash_t); - -pub struct HashCallbackOutput(Py_hash_t); - -impl IntoPyCallbackOutput<'_, Py_hash_t> for HashCallbackOutput { - #[inline] - fn convert(self, _py: Python<'_>) -> PyResult { - let hash = self.0; - if hash == -1 { - Ok(-2) - } else { - Ok(hash) - } - } -} - -impl IntoPyCallbackOutput<'_, HashCallbackOutput> for T -where - T: WrappingCastTo, -{ - #[inline] - fn convert(self, _py: Python<'_>) -> PyResult { - Ok(HashCallbackOutput(self.wrapping_cast())) - } -} - -#[doc(hidden)] -#[inline] -pub fn convert<'py, T, U>(py: Python<'py>, value: T) -> PyResult -where - T: IntoPyCallbackOutput<'py, U>, -{ - value.convert(py) -} diff --git a/vendor/pyo3/src/impl_/coroutine.rs b/vendor/pyo3/src/impl_/coroutine.rs deleted file mode 100644 index f893a2c2fe..0000000000 --- a/vendor/pyo3/src/impl_/coroutine.rs +++ /dev/null @@ -1,98 +0,0 @@ -use std::{ - future::Future, - ops::{Deref, DerefMut}, -}; - -use crate::{ - coroutine::{cancel::ThrowCallback, Coroutine}, - instance::Bound, - pycell::impl_::PyClassBorrowChecker, - pyclass::boolean_struct::False, - types::{PyAnyMethods, PyString}, - IntoPyObject, Py, PyAny, PyClass, PyErr, PyResult, Python, -}; - -pub fn new_coroutine<'py, F, T, E>( - name: &Bound<'py, PyString>, - qualname_prefix: Option<&'static str>, - throw_callback: Option, - future: F, -) -> Coroutine -where - F: Future> + Send + 'static, - T: IntoPyObject<'py>, - E: Into, -{ - Coroutine::new(Some(name.clone()), qualname_prefix, throw_callback, future) -} - -fn get_ptr(obj: &Py) -> *mut T { - obj.get_class_object().get_ptr() -} - -pub struct RefGuard(Py); - -impl RefGuard { - pub fn new(obj: &Bound<'_, PyAny>) -> PyResult { - let bound = obj.downcast::()?; - bound.get_class_object().borrow_checker().try_borrow()?; - Ok(RefGuard(bound.clone().unbind())) - } -} - -impl Deref for RefGuard { - type Target = T; - fn deref(&self) -> &Self::Target { - // SAFETY: `RefGuard` has been built from `PyRef` and provides the same guarantees - unsafe { &*get_ptr(&self.0) } - } -} - -impl Drop for RefGuard { - fn drop(&mut self) { - Python::with_gil(|gil| { - self.0 - .bind(gil) - .get_class_object() - .borrow_checker() - .release_borrow() - }) - } -} - -pub struct RefMutGuard>(Py); - -impl> RefMutGuard { - pub fn new(obj: &Bound<'_, PyAny>) -> PyResult { - let bound = obj.downcast::()?; - bound.get_class_object().borrow_checker().try_borrow_mut()?; - Ok(RefMutGuard(bound.clone().unbind())) - } -} - -impl> Deref for RefMutGuard { - type Target = T; - fn deref(&self) -> &Self::Target { - // SAFETY: `RefMutGuard` has been built from `PyRefMut` and provides the same guarantees - unsafe { &*get_ptr(&self.0) } - } -} - -impl> DerefMut for RefMutGuard { - fn deref_mut(&mut self) -> &mut Self::Target { - // SAFETY: `RefMutGuard` has been built from `PyRefMut` and provides the same guarantees - unsafe { &mut *get_ptr(&self.0) } - } -} - -impl> Drop for RefMutGuard { - fn drop(&mut self) { - Python::with_gil(|gil| { - self.0 - .bind(gil) - .get_class_object() - .borrow_checker() - .release_borrow_mut() - }) - } -} diff --git a/vendor/pyo3/src/impl_/exceptions.rs b/vendor/pyo3/src/impl_/exceptions.rs deleted file mode 100644 index 15b6f53bbe..0000000000 --- a/vendor/pyo3/src/impl_/exceptions.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::{sync::GILOnceCell, types::PyType, Bound, Py, Python}; - -pub struct ImportedExceptionTypeObject { - imported_value: GILOnceCell>, - module: &'static str, - name: &'static str, -} - -impl ImportedExceptionTypeObject { - pub const fn new(module: &'static str, name: &'static str) -> Self { - Self { - imported_value: GILOnceCell::new(), - module, - name, - } - } - - pub fn get<'py>(&self, py: Python<'py>) -> &Bound<'py, PyType> { - self.imported_value - .import(py, self.module, self.name) - .unwrap_or_else(|e| { - panic!( - "failed to import exception {}.{}: {}", - self.module, self.name, e - ) - }) - } -} diff --git a/vendor/pyo3/src/impl_/extract_argument.rs b/vendor/pyo3/src/impl_/extract_argument.rs deleted file mode 100644 index cbf79a1470..0000000000 --- a/vendor/pyo3/src/impl_/extract_argument.rs +++ /dev/null @@ -1,918 +0,0 @@ -use crate::{ - conversion::FromPyObjectBound, - exceptions::PyTypeError, - ffi, - pyclass::boolean_struct::False, - types::{any::PyAnyMethods, dict::PyDictMethods, tuple::PyTupleMethods, PyDict, PyTuple}, - Borrowed, Bound, PyAny, PyClass, PyErr, PyRef, PyRefMut, PyResult, PyTypeCheck, Python, -}; - -/// Helper type used to keep implementation more concise. -/// -/// (Function argument extraction borrows input arguments.) -type PyArg<'py> = Borrowed<'py, 'py, PyAny>; - -/// A trait which is used to help PyO3 macros extract function arguments. -/// -/// `#[pyclass]` structs need to extract as `PyRef` and `PyRefMut` -/// wrappers rather than extracting `&T` and `&mut T` directly. The `Holder` type is used -/// to hold these temporary wrappers - the way the macro is constructed, these wrappers -/// will be dropped as soon as the pyfunction call ends. -/// -/// There exists a trivial blanket implementation for `T: FromPyObject` with `Holder = ()`. -pub trait PyFunctionArgument<'a, 'py>: Sized + 'a { - type Holder: FunctionArgumentHolder; - fn extract(obj: &'a Bound<'py, PyAny>, holder: &'a mut Self::Holder) -> PyResult; -} - -impl<'a, 'py, T> PyFunctionArgument<'a, 'py> for T -where - T: FromPyObjectBound<'a, 'py> + 'a, -{ - type Holder = (); - - #[inline] - fn extract(obj: &'a Bound<'py, PyAny>, _: &'a mut ()) -> PyResult { - obj.extract() - } -} - -impl<'a, 'py, T: 'py> PyFunctionArgument<'a, 'py> for &'a Bound<'py, T> -where - T: PyTypeCheck, -{ - type Holder = Option<()>; - - #[inline] - fn extract(obj: &'a Bound<'py, PyAny>, _: &'a mut Option<()>) -> PyResult { - obj.downcast().map_err(Into::into) - } -} - -impl<'a, 'py, T: 'py> PyFunctionArgument<'a, 'py> for Option<&'a Bound<'py, T>> -where - T: PyTypeCheck, -{ - type Holder = (); - - #[inline] - fn extract(obj: &'a Bound<'py, PyAny>, _: &'a mut ()) -> PyResult { - if obj.is_none() { - Ok(None) - } else { - Ok(Some(obj.downcast()?)) - } - } -} - -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -impl<'a> PyFunctionArgument<'a, '_> for &'a str { - type Holder = Option>; - - #[inline] - fn extract( - obj: &'a Bound<'_, PyAny>, - holder: &'a mut Option>, - ) -> PyResult { - Ok(holder.insert(obj.extract()?)) - } -} - -/// Trait for types which can be a function argument holder - they should -/// to be able to const-initialize to an empty value. -pub trait FunctionArgumentHolder: Sized { - const INIT: Self; -} - -impl FunctionArgumentHolder for () { - const INIT: Self = (); -} - -impl FunctionArgumentHolder for Option { - const INIT: Self = None; -} - -#[inline] -pub fn extract_pyclass_ref<'a, 'py: 'a, T: PyClass>( - obj: &'a Bound<'py, PyAny>, - holder: &'a mut Option>, -) -> PyResult<&'a T> { - Ok(&*holder.insert(obj.extract()?)) -} - -#[inline] -pub fn extract_pyclass_ref_mut<'a, 'py: 'a, T: PyClass>( - obj: &'a Bound<'py, PyAny>, - holder: &'a mut Option>, -) -> PyResult<&'a mut T> { - Ok(&mut *holder.insert(obj.extract()?)) -} - -/// The standard implementation of how PyO3 extracts a `#[pyfunction]` or `#[pymethod]` function argument. -#[doc(hidden)] -pub fn extract_argument<'a, 'py, T>( - obj: &'a Bound<'py, PyAny>, - holder: &'a mut T::Holder, - arg_name: &str, -) -> PyResult -where - T: PyFunctionArgument<'a, 'py>, -{ - match PyFunctionArgument::extract(obj, holder) { - Ok(value) => Ok(value), - Err(e) => Err(argument_extraction_error(obj.py(), arg_name, e)), - } -} - -/// Alternative to [`extract_argument`] used for `Option` arguments. This is necessary because Option<&T> -/// does not implement `PyFunctionArgument` for `T: PyClass`. -#[doc(hidden)] -pub fn extract_optional_argument<'a, 'py, T>( - obj: Option<&'a Bound<'py, PyAny>>, - holder: &'a mut T::Holder, - arg_name: &str, - default: fn() -> Option, -) -> PyResult> -where - T: PyFunctionArgument<'a, 'py>, -{ - match obj { - Some(obj) => { - if obj.is_none() { - // Explicit `None` will result in None being used as the function argument - Ok(None) - } else { - extract_argument(obj, holder, arg_name).map(Some) - } - } - _ => Ok(default()), - } -} - -/// Alternative to [`extract_argument`] used when the argument has a default value provided by an annotation. -#[doc(hidden)] -pub fn extract_argument_with_default<'a, 'py, T>( - obj: Option<&'a Bound<'py, PyAny>>, - holder: &'a mut T::Holder, - arg_name: &str, - default: fn() -> T, -) -> PyResult -where - T: PyFunctionArgument<'a, 'py>, -{ - match obj { - Some(obj) => extract_argument(obj, holder, arg_name), - None => Ok(default()), - } -} - -/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation. -#[doc(hidden)] -pub fn from_py_with<'a, 'py, T>( - obj: &'a Bound<'py, PyAny>, - arg_name: &str, - extractor: fn(&'a Bound<'py, PyAny>) -> PyResult, -) -> PyResult { - match extractor(obj) { - Ok(value) => Ok(value), - Err(e) => Err(argument_extraction_error(obj.py(), arg_name, e)), - } -} - -/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation and also a default value. -#[doc(hidden)] -pub fn from_py_with_with_default<'a, 'py, T>( - obj: Option<&'a Bound<'py, PyAny>>, - arg_name: &str, - extractor: fn(&'a Bound<'py, PyAny>) -> PyResult, - default: fn() -> T, -) -> PyResult { - match obj { - Some(obj) => from_py_with(obj, arg_name, extractor), - None => Ok(default()), - } -} - -/// Adds the argument name to the error message of an error which occurred during argument extraction. -/// -/// Only modifies TypeError. (Cannot guarantee all exceptions have constructors from -/// single string.) -#[doc(hidden)] -#[cold] -pub fn argument_extraction_error(py: Python<'_>, arg_name: &str, error: PyErr) -> PyErr { - if error.get_type(py).is(&py.get_type::()) { - let remapped_error = - PyTypeError::new_err(format!("argument '{}': {}", arg_name, error.value(py))); - remapped_error.set_cause(py, error.cause(py)); - remapped_error - } else { - error - } -} - -/// Unwraps the Option<&PyAny> produced by the FunctionDescription `extract_arguments_` methods. -/// They check if required methods are all provided. -/// -/// # Safety -/// `argument` must not be `None` -#[doc(hidden)] -#[inline] -pub unsafe fn unwrap_required_argument<'a, 'py>( - argument: Option<&'a Bound<'py, PyAny>>, -) -> &'a Bound<'py, PyAny> { - match argument { - Some(value) => value, - #[cfg(debug_assertions)] - None => unreachable!("required method argument was not extracted"), - #[cfg(not(debug_assertions))] - None => std::hint::unreachable_unchecked(), - } -} - -pub struct KeywordOnlyParameterDescription { - pub name: &'static str, - pub required: bool, -} - -/// Function argument specification for a `#[pyfunction]` or `#[pymethod]`. -pub struct FunctionDescription { - pub cls_name: Option<&'static str>, - pub func_name: &'static str, - pub positional_parameter_names: &'static [&'static str], - pub positional_only_parameters: usize, - pub required_positional_parameters: usize, - pub keyword_only_parameters: &'static [KeywordOnlyParameterDescription], -} - -impl FunctionDescription { - fn full_name(&self) -> String { - if let Some(cls_name) = self.cls_name { - format!("{}.{}()", cls_name, self.func_name) - } else { - format!("{}()", self.func_name) - } - } - - /// Equivalent of `extract_arguments_tuple_dict` which uses the Python C-API "fastcall" convention. - /// - /// # Safety - /// - `args` must be a pointer to a C-style array of valid `ffi::PyObject` pointers, or NULL. - /// - `kwnames` must be a pointer to a PyTuple, or NULL. - /// - `nargs + kwnames.len()` is the total length of the `args` array. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub unsafe fn extract_arguments_fastcall<'py, V, K>( - &self, - py: Python<'py>, - args: *const *mut ffi::PyObject, - nargs: ffi::Py_ssize_t, - kwnames: *mut ffi::PyObject, - output: &mut [Option>], - ) -> PyResult<(V::Varargs, K::Varkeywords)> - where - V: VarargsHandler<'py>, - K: VarkeywordsHandler<'py>, - { - let num_positional_parameters = self.positional_parameter_names.len(); - - debug_assert!(nargs >= 0); - debug_assert!(self.positional_only_parameters <= num_positional_parameters); - debug_assert!(self.required_positional_parameters <= num_positional_parameters); - debug_assert_eq!( - output.len(), - num_positional_parameters + self.keyword_only_parameters.len() - ); - - // Handle positional arguments - // Safety: - // - Option has the same memory layout as `*mut ffi::PyObject` - // - we both have the GIL and can borrow these input references for the `'py` lifetime. - let args: *const Option> = args.cast(); - let positional_args_provided = nargs as usize; - let remaining_positional_args = if args.is_null() { - debug_assert_eq!(positional_args_provided, 0); - &[] - } else { - // Can consume at most the number of positional parameters in the function definition, - // the rest are varargs. - let positional_args_to_consume = - num_positional_parameters.min(positional_args_provided); - let (positional_parameters, remaining) = - std::slice::from_raw_parts(args, positional_args_provided) - .split_at(positional_args_to_consume); - output[..positional_args_to_consume].copy_from_slice(positional_parameters); - remaining - }; - let varargs = V::handle_varargs_fastcall(py, remaining_positional_args, self)?; - - // Handle keyword arguments - let mut varkeywords = K::Varkeywords::default(); - - // Safety: kwnames is known to be a pointer to a tuple, or null - // - we both have the GIL and can borrow this input reference for the `'py` lifetime. - let kwnames: Option> = - Borrowed::from_ptr_or_opt(py, kwnames).map(|kwnames| kwnames.downcast_unchecked()); - if let Some(kwnames) = kwnames { - let kwargs = ::std::slice::from_raw_parts( - // Safety: PyArg has the same memory layout as `*mut ffi::PyObject` - args.offset(nargs).cast::>(), - kwnames.len(), - ); - - self.handle_kwargs::( - kwnames.iter_borrowed().zip(kwargs.iter().copied()), - &mut varkeywords, - num_positional_parameters, - output, - )? - } - - // Once all inputs have been processed, check that all required arguments have been provided. - - self.ensure_no_missing_required_positional_arguments(output, positional_args_provided)?; - self.ensure_no_missing_required_keyword_arguments(output)?; - - Ok((varargs, varkeywords)) - } - - /// Extracts the `args` and `kwargs` provided into `output`, according to this function - /// definition. - /// - /// `output` must have the same length as this function has positional and keyword-only - /// parameters (as per the `positional_parameter_names` and `keyword_only_parameters` - /// respectively). - /// - /// Unexpected, duplicate or invalid arguments will cause this function to return `TypeError`. - /// - /// # Safety - /// - `args` must be a pointer to a PyTuple. - /// - `kwargs` must be a pointer to a PyDict, or NULL. - pub unsafe fn extract_arguments_tuple_dict<'py, V, K>( - &self, - py: Python<'py>, - args: *mut ffi::PyObject, - kwargs: *mut ffi::PyObject, - output: &mut [Option>], - ) -> PyResult<(V::Varargs, K::Varkeywords)> - where - V: VarargsHandler<'py>, - K: VarkeywordsHandler<'py>, - { - // Safety: - // - `args` is known to be a tuple - // - `kwargs` is known to be a dict or null - // - we both have the GIL and can borrow these input references for the `'py` lifetime. - let args: Borrowed<'py, 'py, PyTuple> = - Borrowed::from_ptr(py, args).downcast_unchecked::(); - let kwargs: Option> = - Borrowed::from_ptr_or_opt(py, kwargs).map(|kwargs| kwargs.downcast_unchecked()); - - let num_positional_parameters = self.positional_parameter_names.len(); - - debug_assert!(self.positional_only_parameters <= num_positional_parameters); - debug_assert!(self.required_positional_parameters <= num_positional_parameters); - debug_assert_eq!( - output.len(), - num_positional_parameters + self.keyword_only_parameters.len() - ); - - // Copy positional arguments into output - for (i, arg) in args - .iter_borrowed() - .take(num_positional_parameters) - .enumerate() - { - output[i] = Some(arg); - } - - // If any arguments remain, push them to varargs (if possible) or error - let varargs = V::handle_varargs_tuple(&args, self)?; - - // Handle keyword arguments - let mut varkeywords = K::Varkeywords::default(); - if let Some(kwargs) = kwargs { - self.handle_kwargs::( - kwargs.iter_borrowed(), - &mut varkeywords, - num_positional_parameters, - output, - )? - } - - // Once all inputs have been processed, check that all required arguments have been provided. - - self.ensure_no_missing_required_positional_arguments(output, args.len())?; - self.ensure_no_missing_required_keyword_arguments(output)?; - - Ok((varargs, varkeywords)) - } - - #[inline] - fn handle_kwargs<'py, K, I>( - &self, - kwargs: I, - varkeywords: &mut K::Varkeywords, - num_positional_parameters: usize, - output: &mut [Option>], - ) -> PyResult<()> - where - K: VarkeywordsHandler<'py>, - I: IntoIterator, PyArg<'py>)>, - { - debug_assert_eq!( - num_positional_parameters, - self.positional_parameter_names.len() - ); - debug_assert_eq!( - output.len(), - num_positional_parameters + self.keyword_only_parameters.len() - ); - let mut positional_only_keyword_arguments = Vec::new(); - for (kwarg_name_py, value) in kwargs { - // Safety: All keyword arguments should be UTF-8 strings, but if it's not, `.to_str()` - // will return an error anyway. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - let kwarg_name = - unsafe { kwarg_name_py.downcast_unchecked::() }.to_str(); - - #[cfg(all(not(Py_3_10), Py_LIMITED_API))] - let kwarg_name = kwarg_name_py.extract::(); - - if let Ok(kwarg_name_owned) = kwarg_name { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - let kwarg_name = kwarg_name_owned; - #[cfg(all(not(Py_3_10), Py_LIMITED_API))] - let kwarg_name: &str = &kwarg_name_owned; - - // Try to place parameter in keyword only parameters - if let Some(i) = self.find_keyword_parameter_in_keyword_only(kwarg_name) { - if output[i + num_positional_parameters] - .replace(value) - .is_some() - { - return Err(self.multiple_values_for_argument(kwarg_name)); - } - continue; - } - - // Repeat for positional parameters - if let Some(i) = self.find_keyword_parameter_in_positional(kwarg_name) { - if i < self.positional_only_parameters { - // If accepting **kwargs, then it's allowed for the name of the - // kwarg to conflict with a postional-only argument - the value - // will go into **kwargs anyway. - if K::handle_varkeyword(varkeywords, kwarg_name_py, value, self).is_err() { - positional_only_keyword_arguments.push(kwarg_name_owned); - } - } else if output[i].replace(value).is_some() { - return Err(self.multiple_values_for_argument(kwarg_name)); - } - continue; - } - }; - - K::handle_varkeyword(varkeywords, kwarg_name_py, value, self)? - } - - if !positional_only_keyword_arguments.is_empty() { - #[cfg(all(not(Py_3_10), Py_LIMITED_API))] - let positional_only_keyword_arguments: Vec<_> = positional_only_keyword_arguments - .iter() - .map(std::ops::Deref::deref) - .collect(); - return Err(self.positional_only_keyword_arguments(&positional_only_keyword_arguments)); - } - - Ok(()) - } - - #[inline] - fn find_keyword_parameter_in_positional(&self, kwarg_name: &str) -> Option { - self.positional_parameter_names - .iter() - .position(|¶m_name| param_name == kwarg_name) - } - - #[inline] - fn find_keyword_parameter_in_keyword_only(&self, kwarg_name: &str) -> Option { - // Compare the keyword name against each parameter in turn. This is exactly the same method - // which CPython uses to map keyword names. Although it's O(num_parameters), the number of - // parameters is expected to be small so it's not worth constructing a mapping. - self.keyword_only_parameters - .iter() - .position(|param_desc| param_desc.name == kwarg_name) - } - - #[inline] - fn ensure_no_missing_required_positional_arguments( - &self, - output: &[Option>], - positional_args_provided: usize, - ) -> PyResult<()> { - if positional_args_provided < self.required_positional_parameters { - for out in &output[positional_args_provided..self.required_positional_parameters] { - if out.is_none() { - return Err(self.missing_required_positional_arguments(output)); - } - } - } - Ok(()) - } - - #[inline] - fn ensure_no_missing_required_keyword_arguments( - &self, - output: &[Option>], - ) -> PyResult<()> { - let keyword_output = &output[self.positional_parameter_names.len()..]; - for (param, out) in self.keyword_only_parameters.iter().zip(keyword_output) { - if param.required && out.is_none() { - return Err(self.missing_required_keyword_arguments(keyword_output)); - } - } - Ok(()) - } - - #[cold] - fn too_many_positional_arguments(&self, args_provided: usize) -> PyErr { - let was = if args_provided == 1 { "was" } else { "were" }; - let msg = if self.required_positional_parameters != self.positional_parameter_names.len() { - format!( - "{} takes from {} to {} positional arguments but {} {} given", - self.full_name(), - self.required_positional_parameters, - self.positional_parameter_names.len(), - args_provided, - was - ) - } else { - format!( - "{} takes {} positional arguments but {} {} given", - self.full_name(), - self.positional_parameter_names.len(), - args_provided, - was - ) - }; - PyTypeError::new_err(msg) - } - - #[cold] - fn multiple_values_for_argument(&self, argument: &str) -> PyErr { - PyTypeError::new_err(format!( - "{} got multiple values for argument '{}'", - self.full_name(), - argument - )) - } - - #[cold] - fn unexpected_keyword_argument(&self, argument: PyArg<'_>) -> PyErr { - PyTypeError::new_err(format!( - "{} got an unexpected keyword argument '{}'", - self.full_name(), - argument.as_any() - )) - } - - #[cold] - fn positional_only_keyword_arguments(&self, parameter_names: &[&str]) -> PyErr { - let mut msg = format!( - "{} got some positional-only arguments passed as keyword arguments: ", - self.full_name() - ); - push_parameter_list(&mut msg, parameter_names); - PyTypeError::new_err(msg) - } - - #[cold] - fn missing_required_arguments(&self, argument_type: &str, parameter_names: &[&str]) -> PyErr { - let arguments = if parameter_names.len() == 1 { - "argument" - } else { - "arguments" - }; - let mut msg = format!( - "{} missing {} required {} {}: ", - self.full_name(), - parameter_names.len(), - argument_type, - arguments, - ); - push_parameter_list(&mut msg, parameter_names); - PyTypeError::new_err(msg) - } - - #[cold] - fn missing_required_keyword_arguments(&self, keyword_outputs: &[Option>]) -> PyErr { - debug_assert_eq!(self.keyword_only_parameters.len(), keyword_outputs.len()); - - let missing_keyword_only_arguments: Vec<_> = self - .keyword_only_parameters - .iter() - .zip(keyword_outputs) - .filter_map(|(keyword_desc, out)| { - if keyword_desc.required && out.is_none() { - Some(keyword_desc.name) - } else { - None - } - }) - .collect(); - - debug_assert!(!missing_keyword_only_arguments.is_empty()); - self.missing_required_arguments("keyword", &missing_keyword_only_arguments) - } - - #[cold] - fn missing_required_positional_arguments(&self, output: &[Option>]) -> PyErr { - let missing_positional_arguments: Vec<_> = self - .positional_parameter_names - .iter() - .take(self.required_positional_parameters) - .zip(output) - .filter_map(|(param, out)| if out.is_none() { Some(*param) } else { None }) - .collect(); - - debug_assert!(!missing_positional_arguments.is_empty()); - self.missing_required_arguments("positional", &missing_positional_arguments) - } -} - -/// A trait used to control whether to accept varargs in FunctionDescription::extract_argument_(method) functions. -pub trait VarargsHandler<'py> { - type Varargs; - /// Called by `FunctionDescription::extract_arguments_fastcall` with any additional arguments. - fn handle_varargs_fastcall( - py: Python<'py>, - varargs: &[Option>], - function_description: &FunctionDescription, - ) -> PyResult; - /// Called by `FunctionDescription::extract_arguments_tuple_dict` with the original tuple. - /// - /// Additional arguments are those in the tuple slice starting from `function_description.positional_parameter_names.len()`. - fn handle_varargs_tuple( - args: &Bound<'py, PyTuple>, - function_description: &FunctionDescription, - ) -> PyResult; -} - -/// Marker struct which indicates varargs are not allowed. -pub struct NoVarargs; - -impl<'py> VarargsHandler<'py> for NoVarargs { - type Varargs = (); - - #[inline] - fn handle_varargs_fastcall( - _py: Python<'py>, - varargs: &[Option>], - function_description: &FunctionDescription, - ) -> PyResult { - let extra_arguments = varargs.len(); - if extra_arguments > 0 { - return Err(function_description.too_many_positional_arguments( - function_description.positional_parameter_names.len() + extra_arguments, - )); - } - Ok(()) - } - - #[inline] - fn handle_varargs_tuple( - args: &Bound<'py, PyTuple>, - function_description: &FunctionDescription, - ) -> PyResult { - let positional_parameter_count = function_description.positional_parameter_names.len(); - let provided_args_count = args.len(); - if provided_args_count <= positional_parameter_count { - Ok(()) - } else { - Err(function_description.too_many_positional_arguments(provided_args_count)) - } - } -} - -/// Marker struct which indicates varargs should be collected into a `PyTuple`. -pub struct TupleVarargs; - -impl<'py> VarargsHandler<'py> for TupleVarargs { - type Varargs = Bound<'py, PyTuple>; - #[inline] - fn handle_varargs_fastcall( - py: Python<'py>, - varargs: &[Option>], - _function_description: &FunctionDescription, - ) -> PyResult { - PyTuple::new(py, varargs) - } - - #[inline] - fn handle_varargs_tuple( - args: &Bound<'py, PyTuple>, - function_description: &FunctionDescription, - ) -> PyResult { - let positional_parameters = function_description.positional_parameter_names.len(); - Ok(args.get_slice(positional_parameters, args.len())) - } -} - -/// A trait used to control whether to accept varkeywords in FunctionDescription::extract_argument_(method) functions. -pub trait VarkeywordsHandler<'py> { - type Varkeywords: Default; - fn handle_varkeyword( - varkeywords: &mut Self::Varkeywords, - name: PyArg<'py>, - value: PyArg<'py>, - function_description: &FunctionDescription, - ) -> PyResult<()>; -} - -/// Marker struct which indicates unknown keywords are not permitted. -pub struct NoVarkeywords; - -impl<'py> VarkeywordsHandler<'py> for NoVarkeywords { - type Varkeywords = (); - #[inline] - fn handle_varkeyword( - _varkeywords: &mut Self::Varkeywords, - name: PyArg<'py>, - _value: PyArg<'py>, - function_description: &FunctionDescription, - ) -> PyResult<()> { - Err(function_description.unexpected_keyword_argument(name)) - } -} - -/// Marker struct which indicates unknown keywords should be collected into a `PyDict`. -pub struct DictVarkeywords; - -impl<'py> VarkeywordsHandler<'py> for DictVarkeywords { - type Varkeywords = Option>; - #[inline] - fn handle_varkeyword( - varkeywords: &mut Self::Varkeywords, - name: PyArg<'py>, - value: PyArg<'py>, - _function_description: &FunctionDescription, - ) -> PyResult<()> { - varkeywords - .get_or_insert_with(|| PyDict::new(name.py())) - .set_item(name, value) - } -} - -fn push_parameter_list(msg: &mut String, parameter_names: &[&str]) { - let len = parameter_names.len(); - for (i, parameter) in parameter_names.iter().enumerate() { - if i != 0 { - if len > 2 { - msg.push(','); - } - - if i == len - 1 { - msg.push_str(" and ") - } else { - msg.push(' ') - } - } - - msg.push('\''); - msg.push_str(parameter); - msg.push('\''); - } -} - -#[cfg(test)] -mod tests { - use crate::types::{IntoPyDict, PyTuple}; - use crate::Python; - - use super::{push_parameter_list, FunctionDescription, NoVarargs, NoVarkeywords}; - - #[test] - fn unexpected_keyword_argument() { - let function_description = FunctionDescription { - cls_name: None, - func_name: "example", - positional_parameter_names: &[], - positional_only_parameters: 0, - required_positional_parameters: 0, - keyword_only_parameters: &[], - }; - - Python::with_gil(|py| { - let args = PyTuple::empty(py); - let kwargs = [("foo", 0u8)].into_py_dict(py).unwrap(); - let err = unsafe { - function_description - .extract_arguments_tuple_dict::( - py, - args.as_ptr(), - kwargs.as_ptr(), - &mut [], - ) - .unwrap_err() - }; - assert_eq!( - err.to_string(), - "TypeError: example() got an unexpected keyword argument 'foo'" - ); - }) - } - - #[test] - fn keyword_not_string() { - let function_description = FunctionDescription { - cls_name: None, - func_name: "example", - positional_parameter_names: &[], - positional_only_parameters: 0, - required_positional_parameters: 0, - keyword_only_parameters: &[], - }; - - Python::with_gil(|py| { - let args = PyTuple::empty(py); - let kwargs = [(1u8, 1u8)].into_py_dict(py).unwrap(); - let err = unsafe { - function_description - .extract_arguments_tuple_dict::( - py, - args.as_ptr(), - kwargs.as_ptr(), - &mut [], - ) - .unwrap_err() - }; - assert_eq!( - err.to_string(), - "TypeError: example() got an unexpected keyword argument '1'" - ); - }) - } - - #[test] - fn missing_required_arguments() { - let function_description = FunctionDescription { - cls_name: None, - func_name: "example", - positional_parameter_names: &["foo", "bar"], - positional_only_parameters: 0, - required_positional_parameters: 2, - keyword_only_parameters: &[], - }; - - Python::with_gil(|py| { - let args = PyTuple::empty(py); - let mut output = [None, None]; - let err = unsafe { - function_description.extract_arguments_tuple_dict::( - py, - args.as_ptr(), - std::ptr::null_mut(), - &mut output, - ) - } - .unwrap_err(); - assert_eq!( - err.to_string(), - "TypeError: example() missing 2 required positional arguments: 'foo' and 'bar'" - ); - }) - } - - #[test] - fn push_parameter_list_empty() { - let mut s = String::new(); - push_parameter_list(&mut s, &[]); - assert_eq!(&s, ""); - } - - #[test] - fn push_parameter_list_one() { - let mut s = String::new(); - push_parameter_list(&mut s, &["a"]); - assert_eq!(&s, "'a'"); - } - - #[test] - fn push_parameter_list_two() { - let mut s = String::new(); - push_parameter_list(&mut s, &["a", "b"]); - assert_eq!(&s, "'a' and 'b'"); - } - - #[test] - fn push_parameter_list_three() { - let mut s = String::new(); - push_parameter_list(&mut s, &["a", "b", "c"]); - assert_eq!(&s, "'a', 'b', and 'c'"); - } - - #[test] - fn push_parameter_list_four() { - let mut s = String::new(); - push_parameter_list(&mut s, &["a", "b", "c", "d"]); - assert_eq!(&s, "'a', 'b', 'c', and 'd'"); - } -} diff --git a/vendor/pyo3/src/impl_/freelist.rs b/vendor/pyo3/src/impl_/freelist.rs deleted file mode 100644 index 0e611d421d..0000000000 --- a/vendor/pyo3/src/impl_/freelist.rs +++ /dev/null @@ -1,69 +0,0 @@ -//! Support for [free allocation lists][1]. -//! -//! This can improve performance for types that are often created and deleted in quick succession. -//! -//! Rather than implementing this manually, -//! implement it by annotating a struct with `#[pyclass(freelist = N)]`, -//! where `N` is the size of the freelist. -//! -//! [1]: https://en.wikipedia.org/wiki/Free_list - -use std::mem; - -/// Represents a slot of a [`FreeList`]. -pub enum Slot { - /// A free slot. - Empty, - /// An allocated slot. - Filled(T), -} - -/// A free allocation list. -/// -/// See [the parent module](crate::impl_::freelist) for more details. -pub struct FreeList { - entries: Vec>, - split: usize, - capacity: usize, -} - -impl FreeList { - /// Creates a new `FreeList` instance with specified capacity. - pub fn with_capacity(capacity: usize) -> FreeList { - let entries = (0..capacity).map(|_| Slot::Empty).collect::>(); - - FreeList { - entries, - split: 0, - capacity, - } - } - - /// Pops the first non empty item. - pub fn pop(&mut self) -> Option { - let idx = self.split; - if idx == 0 { - None - } else { - match mem::replace(&mut self.entries[idx - 1], Slot::Empty) { - Slot::Filled(v) => { - self.split = idx - 1; - Some(v) - } - _ => panic!("FreeList is corrupt"), - } - } - } - - /// Inserts a value into the list. Returns `Some(val)` if the `FreeList` is full. - pub fn insert(&mut self, val: T) -> Option { - let next = self.split + 1; - if next < self.capacity { - self.entries[self.split] = Slot::Filled(val); - self.split = next; - None - } else { - Some(val) - } - } -} diff --git a/vendor/pyo3/src/impl_/frompyobject.rs b/vendor/pyo3/src/impl_/frompyobject.rs deleted file mode 100644 index 9f3f77340b..0000000000 --- a/vendor/pyo3/src/impl_/frompyobject.rs +++ /dev/null @@ -1,143 +0,0 @@ -use crate::types::any::PyAnyMethods; -use crate::Bound; -use crate::{exceptions::PyTypeError, FromPyObject, PyAny, PyErr, PyResult, Python}; - -#[cold] -pub fn failed_to_extract_enum( - py: Python<'_>, - type_name: &str, - variant_names: &[&str], - error_names: &[&str], - errors: &[PyErr], -) -> PyErr { - // TODO maybe use ExceptionGroup on Python 3.11+ ? - let mut err_msg = format!( - "failed to extract enum {} ('{}')", - type_name, - error_names.join(" | ") - ); - for ((variant_name, error_name), error) in variant_names.iter().zip(error_names).zip(errors) { - use std::fmt::Write; - write!( - &mut err_msg, - "\n- variant {variant_name} ({error_name}): {error_msg}", - variant_name = variant_name, - error_name = error_name, - error_msg = extract_traceback(py, error.clone_ref(py)), - ) - .unwrap(); - } - PyTypeError::new_err(err_msg) -} - -/// Flattens a chain of errors into a single string. -fn extract_traceback(py: Python<'_>, mut error: PyErr) -> String { - use std::fmt::Write; - - let mut error_msg = error.to_string(); - while let Some(cause) = error.cause(py) { - write!(&mut error_msg, ", caused by {}", cause).unwrap(); - error = cause - } - error_msg -} - -pub fn extract_struct_field<'py, T>( - obj: &Bound<'py, PyAny>, - struct_name: &str, - field_name: &str, -) -> PyResult -where - T: FromPyObject<'py>, -{ - match obj.extract() { - Ok(value) => Ok(value), - Err(err) => Err(failed_to_extract_struct_field( - obj.py(), - err, - struct_name, - field_name, - )), - } -} - -pub fn extract_struct_field_with<'a, 'py, T>( - extractor: fn(&'a Bound<'py, PyAny>) -> PyResult, - obj: &'a Bound<'py, PyAny>, - struct_name: &str, - field_name: &str, -) -> PyResult { - match extractor(obj) { - Ok(value) => Ok(value), - Err(err) => Err(failed_to_extract_struct_field( - obj.py(), - err, - struct_name, - field_name, - )), - } -} - -#[cold] -fn failed_to_extract_struct_field( - py: Python<'_>, - inner_err: PyErr, - struct_name: &str, - field_name: &str, -) -> PyErr { - let new_err = PyTypeError::new_err(format!( - "failed to extract field {}.{}", - struct_name, field_name - )); - new_err.set_cause(py, ::std::option::Option::Some(inner_err)); - new_err -} - -pub fn extract_tuple_struct_field<'py, T>( - obj: &Bound<'py, PyAny>, - struct_name: &str, - index: usize, -) -> PyResult -where - T: FromPyObject<'py>, -{ - match obj.extract() { - Ok(value) => Ok(value), - Err(err) => Err(failed_to_extract_tuple_struct_field( - obj.py(), - err, - struct_name, - index, - )), - } -} - -pub fn extract_tuple_struct_field_with<'a, 'py, T>( - extractor: fn(&'a Bound<'py, PyAny>) -> PyResult, - obj: &'a Bound<'py, PyAny>, - struct_name: &str, - index: usize, -) -> PyResult { - match extractor(obj) { - Ok(value) => Ok(value), - Err(err) => Err(failed_to_extract_tuple_struct_field( - obj.py(), - err, - struct_name, - index, - )), - } -} - -#[cold] -fn failed_to_extract_tuple_struct_field( - py: Python<'_>, - inner_err: PyErr, - struct_name: &str, - index: usize, -) -> PyErr { - let new_err = - PyTypeError::new_err(format!("failed to extract field {}.{}", struct_name, index)); - new_err.set_cause(py, ::std::option::Option::Some(inner_err)); - new_err -} diff --git a/vendor/pyo3/src/impl_/not_send.rs b/vendor/pyo3/src/impl_/not_send.rs deleted file mode 100644 index 3304d79a1b..0000000000 --- a/vendor/pyo3/src/impl_/not_send.rs +++ /dev/null @@ -1,7 +0,0 @@ -use std::marker::PhantomData; - -use crate::Python; - -/// A marker type that makes the type !Send. -/// Workaround for lack of !Send on stable (). -pub(crate) struct NotSend(PhantomData<*mut Python<'static>>); diff --git a/vendor/pyo3/src/impl_/panic.rs b/vendor/pyo3/src/impl_/panic.rs deleted file mode 100644 index 70cdab1ece..0000000000 --- a/vendor/pyo3/src/impl_/panic.rs +++ /dev/null @@ -1,27 +0,0 @@ -/// Type which will panic if dropped. -/// -/// If this is dropped during a panic, this will cause an abort. -/// -/// Use this to avoid letting unwinds cross through the FFI boundary, which is UB. -pub struct PanicTrap { - msg: &'static str, -} - -impl PanicTrap { - #[inline] - pub const fn new(msg: &'static str) -> Self { - Self { msg } - } - - #[inline] - pub const fn disarm(self) { - std::mem::forget(self) - } -} - -impl Drop for PanicTrap { - fn drop(&mut self) { - // Panic here will abort the process, assuming in an unwind. - panic!("{}", self.msg) - } -} diff --git a/vendor/pyo3/src/impl_/pycell.rs b/vendor/pyo3/src/impl_/pycell.rs deleted file mode 100644 index 93514c7bb2..0000000000 --- a/vendor/pyo3/src/impl_/pycell.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Externally-accessible implementation of pycell -pub use crate::pycell::impl_::{ - GetBorrowChecker, PyClassMutability, PyClassObject, PyClassObjectBase, PyClassObjectLayout, -}; diff --git a/vendor/pyo3/src/impl_/pyclass.rs b/vendor/pyo3/src/impl_/pyclass.rs deleted file mode 100644 index 7bb61442ec..0000000000 --- a/vendor/pyo3/src/impl_/pyclass.rs +++ /dev/null @@ -1,1667 +0,0 @@ -use crate::{ - exceptions::{PyAttributeError, PyNotImplementedError, PyRuntimeError, PyValueError}, - ffi, - impl_::{ - freelist::FreeList, - pycell::{GetBorrowChecker, PyClassMutability, PyClassObjectLayout}, - pyclass_init::PyObjectInit, - pymethods::{PyGetterDef, PyMethodDefType}, - }, - pycell::PyBorrowError, - types::{any::PyAnyMethods, PyBool}, - Borrowed, BoundObject, IntoPyObject, IntoPyObjectExt, Py, PyAny, PyClass, PyErr, PyRef, - PyResult, PyTypeInfo, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::{ - borrow::Cow, - ffi::{CStr, CString}, - marker::PhantomData, - os::raw::{c_int, c_void}, - ptr::NonNull, - thread, -}; - -mod assertions; -mod lazy_type_object; -mod probes; - -pub use assertions::*; -pub use lazy_type_object::LazyTypeObject; -pub use probes::*; - -/// Gets the offset of the dictionary from the start of the object in bytes. -#[inline] -pub fn dict_offset() -> ffi::Py_ssize_t { - PyClassObject::::dict_offset() -} - -/// Gets the offset of the weakref list from the start of the object in bytes. -#[inline] -pub fn weaklist_offset() -> ffi::Py_ssize_t { - PyClassObject::::weaklist_offset() -} - -/// Represents the `__dict__` field for `#[pyclass]`. -pub trait PyClassDict { - /// Initial form of a [PyObject](crate::ffi::PyObject) `__dict__` reference. - const INIT: Self; - /// Empties the dictionary of its key-value pairs. - #[inline] - fn clear_dict(&mut self, _py: Python<'_>) {} - private_decl! {} -} - -/// Represents the `__weakref__` field for `#[pyclass]`. -pub trait PyClassWeakRef { - /// Initializes a `weakref` instance. - const INIT: Self; - /// Clears the weak references to the given object. - /// - /// # Safety - /// - `_obj` must be a pointer to the pyclass instance which contains `self`. - /// - The GIL must be held. - #[inline] - unsafe fn clear_weakrefs(&mut self, _obj: *mut ffi::PyObject, _py: Python<'_>) {} - private_decl! {} -} - -/// Zero-sized dummy field. -pub struct PyClassDummySlot; - -impl PyClassDict for PyClassDummySlot { - private_impl! {} - const INIT: Self = PyClassDummySlot; -} - -impl PyClassWeakRef for PyClassDummySlot { - private_impl! {} - const INIT: Self = PyClassDummySlot; -} - -/// Actual dict field, which holds the pointer to `__dict__`. -/// -/// `#[pyclass(dict)]` automatically adds this. -#[repr(transparent)] -#[allow(dead_code)] // These are constructed in INIT and used by the macro code -pub struct PyClassDictSlot(*mut ffi::PyObject); - -impl PyClassDict for PyClassDictSlot { - private_impl! {} - const INIT: Self = Self(std::ptr::null_mut()); - #[inline] - fn clear_dict(&mut self, _py: Python<'_>) { - if !self.0.is_null() { - unsafe { ffi::PyDict_Clear(self.0) } - } - } -} - -/// Actual weakref field, which holds the pointer to `__weakref__`. -/// -/// `#[pyclass(weakref)]` automatically adds this. -#[repr(transparent)] -#[allow(dead_code)] // These are constructed in INIT and used by the macro code -pub struct PyClassWeakRefSlot(*mut ffi::PyObject); - -impl PyClassWeakRef for PyClassWeakRefSlot { - private_impl! {} - const INIT: Self = Self(std::ptr::null_mut()); - #[inline] - unsafe fn clear_weakrefs(&mut self, obj: *mut ffi::PyObject, _py: Python<'_>) { - if !self.0.is_null() { - ffi::PyObject_ClearWeakRefs(obj) - } - } -} - -/// This type is used as a "dummy" type on which dtolnay specializations are -/// applied to apply implementations from `#[pymethods]` -pub struct PyClassImplCollector(PhantomData); - -impl PyClassImplCollector { - pub fn new() -> Self { - Self(PhantomData) - } -} - -impl Default for PyClassImplCollector { - fn default() -> Self { - Self::new() - } -} - -impl Clone for PyClassImplCollector { - fn clone(&self) -> Self { - *self - } -} - -impl Copy for PyClassImplCollector {} - -pub enum MaybeRuntimePyMethodDef { - /// Used in cases where const functionality is not sufficient to define the method - /// purely at compile time. - Runtime(fn() -> PyMethodDefType), - Static(PyMethodDefType), -} - -pub struct PyClassItems { - pub methods: &'static [MaybeRuntimePyMethodDef], - pub slots: &'static [ffi::PyType_Slot], -} - -// Allow PyClassItems in statics -unsafe impl Sync for PyClassItems {} - -/// Implements the underlying functionality of `#[pyclass]`, assembled by various proc macros. -/// -/// Users are discouraged from implementing this trait manually; it is a PyO3 implementation detail -/// and may be changed at any time. -pub trait PyClassImpl: Sized + 'static { - /// #[pyclass(subclass)] - const IS_BASETYPE: bool = false; - - /// #[pyclass(extends=...)] - const IS_SUBCLASS: bool = false; - - /// #[pyclass(mapping)] - const IS_MAPPING: bool = false; - - /// #[pyclass(sequence)] - const IS_SEQUENCE: bool = false; - - /// Base class - type BaseType: PyTypeInfo + PyClassBaseType; - - /// Immutable or mutable - type PyClassMutability: PyClassMutability + GetBorrowChecker; - - /// Specify this class has `#[pyclass(dict)]` or not. - type Dict: PyClassDict; - - /// Specify this class has `#[pyclass(weakref)]` or not. - type WeakRef: PyClassWeakRef; - - /// The closest native ancestor. This is `PyAny` by default, and when you declare - /// `#[pyclass(extends=PyDict)]`, it's `PyDict`. - type BaseNativeType: PyTypeInfo; - - /// This handles following two situations: - /// 1. In case `T` is `Send`, stub `ThreadChecker` is used and does nothing. - /// This implementation is used by default. Compile fails if `T: !Send`. - /// 2. In case `T` is `!Send`, `ThreadChecker` panics when `T` is accessed by another thread. - /// This implementation is used when `#[pyclass(unsendable)]` is given. - /// Panicking makes it safe to expose `T: !Send` to the Python interpreter, where all objects - /// can be accessed by multiple threads by `threading` module. - type ThreadChecker: PyClassThreadChecker; - - #[cfg(feature = "multiple-pymethods")] - type Inventory: PyClassInventory; - - /// Rendered class doc - fn doc(py: Python<'_>) -> PyResult<&'static CStr>; - - fn items_iter() -> PyClassItemsIter; - - #[inline] - fn dict_offset() -> Option { - None - } - - #[inline] - fn weaklist_offset() -> Option { - None - } - - fn lazy_type_object() -> &'static LazyTypeObject; -} - -/// Runtime helper to build a class docstring from the `doc` and `text_signature`. -/// -/// This is done at runtime because the class text signature is collected via dtolnay -/// specialization in to the `#[pyclass]` macro from the `#[pymethods]` macro. -pub fn build_pyclass_doc( - class_name: &'static str, - doc: &'static CStr, - text_signature: Option<&'static str>, -) -> PyResult> { - if let Some(text_signature) = text_signature { - let doc = CString::new(format!( - "{}{}\n--\n\n{}", - class_name, - text_signature, - doc.to_str().unwrap(), - )) - .map_err(|_| PyValueError::new_err("class doc cannot contain nul bytes"))?; - Ok(Cow::Owned(doc)) - } else { - Ok(Cow::Borrowed(doc)) - } -} - -/// Iterator used to process all class items during type instantiation. -pub struct PyClassItemsIter { - /// Iteration state - idx: usize, - /// Items from the `#[pyclass]` macro - pyclass_items: &'static PyClassItems, - /// Items from the `#[pymethods]` macro - #[cfg(not(feature = "multiple-pymethods"))] - pymethods_items: &'static PyClassItems, - /// Items from the `#[pymethods]` macro with inventory - #[cfg(feature = "multiple-pymethods")] - pymethods_items: Box>, -} - -impl PyClassItemsIter { - pub fn new( - pyclass_items: &'static PyClassItems, - #[cfg(not(feature = "multiple-pymethods"))] pymethods_items: &'static PyClassItems, - #[cfg(feature = "multiple-pymethods")] pymethods_items: Box< - dyn Iterator, - >, - ) -> Self { - Self { - idx: 0, - pyclass_items, - pymethods_items, - } - } -} - -impl Iterator for PyClassItemsIter { - type Item = &'static PyClassItems; - - #[cfg(not(feature = "multiple-pymethods"))] - fn next(&mut self) -> Option { - match self.idx { - 0 => { - self.idx += 1; - Some(self.pyclass_items) - } - 1 => { - self.idx += 1; - Some(self.pymethods_items) - } - // Termination clause - _ => None, - } - } - - #[cfg(feature = "multiple-pymethods")] - fn next(&mut self) -> Option { - match self.idx { - 0 => { - self.idx += 1; - Some(self.pyclass_items) - } - // Termination clause - _ => self.pymethods_items.next(), - } - } -} - -// Traits describing known special methods. - -macro_rules! slot_fragment_trait { - ($trait_name:ident, $($default_method:tt)*) => { - #[allow(non_camel_case_types)] - pub trait $trait_name: Sized { - $($default_method)* - } - - impl $trait_name for &'_ PyClassImplCollector {} - } -} - -slot_fragment_trait! { - PyClass__getattribute__SlotFragment, - - /// # Safety: _slf and _attr must be valid non-null Python objects - #[inline] - unsafe fn __getattribute__( - self, - py: Python<'_>, - slf: *mut ffi::PyObject, - attr: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - let res = ffi::PyObject_GenericGetAttr(slf, attr); - if res.is_null() { - Err(PyErr::fetch(py)) - } else { - Ok(res) - } - } -} - -slot_fragment_trait! { - PyClass__getattr__SlotFragment, - - /// # Safety: _slf and _attr must be valid non-null Python objects - #[inline] - unsafe fn __getattr__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - attr: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Err(PyErr::new::( - (Py::::from_borrowed_ptr(py, attr),) - )) - } -} - -#[doc(hidden)] -#[macro_export] -macro_rules! generate_pyclass_getattro_slot { - ($cls:ty) => {{ - unsafe extern "C" fn __wrap( - _slf: *mut $crate::ffi::PyObject, - attr: *mut $crate::ffi::PyObject, - ) -> *mut $crate::ffi::PyObject { - $crate::impl_::trampoline::getattrofunc(_slf, attr, |py, _slf, attr| { - use ::std::result::Result::*; - use $crate::impl_::pyclass::*; - let collector = PyClassImplCollector::<$cls>::new(); - - // Strategy: - // - Try __getattribute__ first. Its default is PyObject_GenericGetAttr. - // - If it returns a result, use it. - // - If it fails with AttributeError, try __getattr__. - // - If it fails otherwise, reraise. - match collector.__getattribute__(py, _slf, attr) { - Ok(obj) => Ok(obj), - Err(e) if e.is_instance_of::<$crate::exceptions::PyAttributeError>(py) => { - collector.__getattr__(py, _slf, attr) - } - Err(e) => Err(e), - } - }) - } - $crate::ffi::PyType_Slot { - slot: $crate::ffi::Py_tp_getattro, - pfunc: __wrap as $crate::ffi::getattrofunc as _, - } - }}; -} - -pub use generate_pyclass_getattro_slot; - -/// Macro which expands to three items -/// - Trait for a __setitem__ dunder -/// - Trait for the corresponding __delitem__ dunder -/// - A macro which will use dtolnay specialisation to generate the shared slot for the two dunders -macro_rules! define_pyclass_setattr_slot { - ( - $set_trait:ident, - $del_trait:ident, - $set:ident, - $del:ident, - $set_error:expr, - $del_error:expr, - $generate_macro:ident, - $slot:ident, - $func_ty:ident, - ) => { - slot_fragment_trait! { - $set_trait, - - /// # Safety: _slf and _attr must be valid non-null Python objects - #[inline] - unsafe fn $set( - self, - _py: Python<'_>, - _slf: *mut ffi::PyObject, - _attr: *mut ffi::PyObject, - _value: NonNull, - ) -> PyResult<()> { - $set_error - } - } - - slot_fragment_trait! { - $del_trait, - - /// # Safety: _slf and _attr must be valid non-null Python objects - #[inline] - unsafe fn $del( - self, - _py: Python<'_>, - _slf: *mut ffi::PyObject, - _attr: *mut ffi::PyObject, - ) -> PyResult<()> { - $del_error - } - } - - #[doc(hidden)] - #[macro_export] - macro_rules! $generate_macro { - ($cls:ty) => {{ - unsafe extern "C" fn __wrap( - _slf: *mut $crate::ffi::PyObject, - attr: *mut $crate::ffi::PyObject, - value: *mut $crate::ffi::PyObject, - ) -> ::std::os::raw::c_int { - $crate::impl_::trampoline::setattrofunc( - _slf, - attr, - value, - |py, _slf, attr, value| { - use ::std::option::Option::*; - use $crate::impl_::callback::IntoPyCallbackOutput; - use $crate::impl_::pyclass::*; - let collector = PyClassImplCollector::<$cls>::new(); - if let Some(value) = ::std::ptr::NonNull::new(value) { - collector.$set(py, _slf, attr, value).convert(py) - } else { - collector.$del(py, _slf, attr).convert(py) - } - }, - ) - } - $crate::ffi::PyType_Slot { - slot: $crate::ffi::$slot, - pfunc: __wrap as $crate::ffi::$func_ty as _, - } - }}; - } - pub use $generate_macro; - }; -} - -define_pyclass_setattr_slot! { - PyClass__setattr__SlotFragment, - PyClass__delattr__SlotFragment, - __setattr__, - __delattr__, - Err(PyAttributeError::new_err("can't set attribute")), - Err(PyAttributeError::new_err("can't delete attribute")), - generate_pyclass_setattr_slot, - Py_tp_setattro, - setattrofunc, -} - -define_pyclass_setattr_slot! { - PyClass__set__SlotFragment, - PyClass__delete__SlotFragment, - __set__, - __delete__, - Err(PyNotImplementedError::new_err("can't set descriptor")), - Err(PyNotImplementedError::new_err("can't delete descriptor")), - generate_pyclass_setdescr_slot, - Py_tp_descr_set, - descrsetfunc, -} - -define_pyclass_setattr_slot! { - PyClass__setitem__SlotFragment, - PyClass__delitem__SlotFragment, - __setitem__, - __delitem__, - Err(PyNotImplementedError::new_err("can't set item")), - Err(PyNotImplementedError::new_err("can't delete item")), - generate_pyclass_setitem_slot, - Py_mp_ass_subscript, - objobjargproc, -} - -/// Macro which expands to three items -/// - Trait for a lhs dunder e.g. __add__ -/// - Trait for the corresponding rhs e.g. __radd__ -/// - A macro which will use dtolnay specialisation to generate the shared slot for the two dunders -macro_rules! define_pyclass_binary_operator_slot { - ( - $lhs_trait:ident, - $rhs_trait:ident, - $lhs:ident, - $rhs:ident, - $generate_macro:ident, - $slot:ident, - $func_ty:ident, - ) => { - slot_fragment_trait! { - $lhs_trait, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn $lhs( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } - } - - slot_fragment_trait! { - $rhs_trait, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn $rhs( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } - } - - #[doc(hidden)] - #[macro_export] - macro_rules! $generate_macro { - ($cls:ty) => {{ - unsafe extern "C" fn __wrap( - _slf: *mut $crate::ffi::PyObject, - _other: *mut $crate::ffi::PyObject, - ) -> *mut $crate::ffi::PyObject { - $crate::impl_::trampoline::binaryfunc(_slf, _other, |py, _slf, _other| { - use $crate::impl_::pyclass::*; - let collector = PyClassImplCollector::<$cls>::new(); - let lhs_result = collector.$lhs(py, _slf, _other)?; - if lhs_result == $crate::ffi::Py_NotImplemented() { - $crate::ffi::Py_DECREF(lhs_result); - collector.$rhs(py, _other, _slf) - } else { - ::std::result::Result::Ok(lhs_result) - } - }) - } - $crate::ffi::PyType_Slot { - slot: $crate::ffi::$slot, - pfunc: __wrap as $crate::ffi::$func_ty as _, - } - }}; - } - pub use $generate_macro; - }; -} - -define_pyclass_binary_operator_slot! { - PyClass__add__SlotFragment, - PyClass__radd__SlotFragment, - __add__, - __radd__, - generate_pyclass_add_slot, - Py_nb_add, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__sub__SlotFragment, - PyClass__rsub__SlotFragment, - __sub__, - __rsub__, - generate_pyclass_sub_slot, - Py_nb_subtract, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__mul__SlotFragment, - PyClass__rmul__SlotFragment, - __mul__, - __rmul__, - generate_pyclass_mul_slot, - Py_nb_multiply, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__mod__SlotFragment, - PyClass__rmod__SlotFragment, - __mod__, - __rmod__, - generate_pyclass_mod_slot, - Py_nb_remainder, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__divmod__SlotFragment, - PyClass__rdivmod__SlotFragment, - __divmod__, - __rdivmod__, - generate_pyclass_divmod_slot, - Py_nb_divmod, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__lshift__SlotFragment, - PyClass__rlshift__SlotFragment, - __lshift__, - __rlshift__, - generate_pyclass_lshift_slot, - Py_nb_lshift, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__rshift__SlotFragment, - PyClass__rrshift__SlotFragment, - __rshift__, - __rrshift__, - generate_pyclass_rshift_slot, - Py_nb_rshift, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__and__SlotFragment, - PyClass__rand__SlotFragment, - __and__, - __rand__, - generate_pyclass_and_slot, - Py_nb_and, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__or__SlotFragment, - PyClass__ror__SlotFragment, - __or__, - __ror__, - generate_pyclass_or_slot, - Py_nb_or, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__xor__SlotFragment, - PyClass__rxor__SlotFragment, - __xor__, - __rxor__, - generate_pyclass_xor_slot, - Py_nb_xor, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__matmul__SlotFragment, - PyClass__rmatmul__SlotFragment, - __matmul__, - __rmatmul__, - generate_pyclass_matmul_slot, - Py_nb_matrix_multiply, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__truediv__SlotFragment, - PyClass__rtruediv__SlotFragment, - __truediv__, - __rtruediv__, - generate_pyclass_truediv_slot, - Py_nb_true_divide, - binaryfunc, -} - -define_pyclass_binary_operator_slot! { - PyClass__floordiv__SlotFragment, - PyClass__rfloordiv__SlotFragment, - __floordiv__, - __rfloordiv__, - generate_pyclass_floordiv_slot, - Py_nb_floor_divide, - binaryfunc, -} - -slot_fragment_trait! { - PyClass__pow__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __pow__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - _mod: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__rpow__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __rpow__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - _mod: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -#[doc(hidden)] -#[macro_export] -macro_rules! generate_pyclass_pow_slot { - ($cls:ty) => {{ - unsafe extern "C" fn __wrap( - _slf: *mut $crate::ffi::PyObject, - _other: *mut $crate::ffi::PyObject, - _mod: *mut $crate::ffi::PyObject, - ) -> *mut $crate::ffi::PyObject { - $crate::impl_::trampoline::ternaryfunc(_slf, _other, _mod, |py, _slf, _other, _mod| { - use $crate::impl_::pyclass::*; - let collector = PyClassImplCollector::<$cls>::new(); - let lhs_result = collector.__pow__(py, _slf, _other, _mod)?; - if lhs_result == $crate::ffi::Py_NotImplemented() { - $crate::ffi::Py_DECREF(lhs_result); - collector.__rpow__(py, _other, _slf, _mod) - } else { - ::std::result::Result::Ok(lhs_result) - } - }) - } - $crate::ffi::PyType_Slot { - slot: $crate::ffi::Py_nb_power, - pfunc: __wrap as $crate::ffi::ternaryfunc as _, - } - }}; -} -pub use generate_pyclass_pow_slot; - -slot_fragment_trait! { - PyClass__lt__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __lt__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__le__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __le__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__eq__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __eq__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__ne__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __ne__( - self, - py: Python<'_>, - slf: *mut ffi::PyObject, - other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - // By default `__ne__` will try `__eq__` and invert the result - let slf = Borrowed::from_ptr(py, slf); - let other = Borrowed::from_ptr(py, other); - slf.eq(other).map(|is_eq| PyBool::new(py, !is_eq).to_owned().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__gt__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __gt__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -slot_fragment_trait! { - PyClass__ge__SlotFragment, - - /// # Safety: _slf and _other must be valid non-null Python objects - #[inline] - unsafe fn __ge__( - self, - py: Python<'_>, - _slf: *mut ffi::PyObject, - _other: *mut ffi::PyObject, - ) -> PyResult<*mut ffi::PyObject> { - Ok(py.NotImplemented().into_ptr()) - } -} - -#[doc(hidden)] -#[macro_export] -macro_rules! generate_pyclass_richcompare_slot { - ($cls:ty) => {{ - #[allow(unknown_lints, non_local_definitions)] - impl $cls { - #[allow(non_snake_case)] - unsafe extern "C" fn __pymethod___richcmp____( - slf: *mut $crate::ffi::PyObject, - other: *mut $crate::ffi::PyObject, - op: ::std::os::raw::c_int, - ) -> *mut $crate::ffi::PyObject { - impl $crate::impl_::pyclass::HasCustomRichCmp for $cls {} - - $crate::impl_::trampoline::richcmpfunc(slf, other, op, |py, slf, other, op| { - use $crate::class::basic::CompareOp; - use $crate::impl_::pyclass::*; - let collector = PyClassImplCollector::<$cls>::new(); - match CompareOp::from_raw(op).expect("invalid compareop") { - CompareOp::Lt => collector.__lt__(py, slf, other), - CompareOp::Le => collector.__le__(py, slf, other), - CompareOp::Eq => collector.__eq__(py, slf, other), - CompareOp::Ne => collector.__ne__(py, slf, other), - CompareOp::Gt => collector.__gt__(py, slf, other), - CompareOp::Ge => collector.__ge__(py, slf, other), - } - }) - } - } - $crate::ffi::PyType_Slot { - slot: $crate::ffi::Py_tp_richcompare, - pfunc: <$cls>::__pymethod___richcmp____ as $crate::ffi::richcmpfunc as _, - } - }}; -} -pub use generate_pyclass_richcompare_slot; - -use super::{pycell::PyClassObject, pymethods::BoundRef}; - -/// Implements a freelist. -/// -/// Do not implement this trait manually. Instead, use `#[pyclass(freelist = N)]` -/// on a Rust struct to implement it. -pub trait PyClassWithFreeList: PyClass { - fn get_free_list(py: Python<'_>) -> &mut FreeList<*mut ffi::PyObject>; -} - -/// Implementation of tp_alloc for `freelist` classes. -/// -/// # Safety -/// - `subtype` must be a valid pointer to the type object of T or a subclass. -/// - The GIL must be held. -pub unsafe extern "C" fn alloc_with_freelist( - subtype: *mut ffi::PyTypeObject, - nitems: ffi::Py_ssize_t, -) -> *mut ffi::PyObject { - let py = Python::assume_gil_acquired(); - - #[cfg(not(Py_3_8))] - bpo_35810_workaround(py, subtype); - - let self_type = T::type_object_raw(py); - // If this type is a variable type or the subtype is not equal to this type, we cannot use the - // freelist - if nitems == 0 && subtype == self_type { - if let Some(obj) = T::get_free_list(py).pop() { - ffi::PyObject_Init(obj, subtype); - return obj as _; - } - } - - ffi::PyType_GenericAlloc(subtype, nitems) -} - -/// Implementation of tp_free for `freelist` classes. -/// -/// # Safety -/// - `obj` must be a valid pointer to an instance of T (not a subclass). -/// - The GIL must be held. -pub unsafe extern "C" fn free_with_freelist(obj: *mut c_void) { - let obj = obj as *mut ffi::PyObject; - debug_assert_eq!( - T::type_object_raw(Python::assume_gil_acquired()), - ffi::Py_TYPE(obj) - ); - if let Some(obj) = T::get_free_list(Python::assume_gil_acquired()).insert(obj) { - let ty = ffi::Py_TYPE(obj); - - // Deduce appropriate inverse of PyType_GenericAlloc - let free = if ffi::PyType_IS_GC(ty) != 0 { - ffi::PyObject_GC_Del - } else { - ffi::PyObject_Free - }; - free(obj as *mut c_void); - - #[cfg(Py_3_8)] - if ffi::PyType_HasFeature(ty, ffi::Py_TPFLAGS_HEAPTYPE) != 0 { - ffi::Py_DECREF(ty as *mut ffi::PyObject); - } - } -} - -/// Workaround for Python issue 35810; no longer necessary in Python 3.8 -#[inline] -#[cfg(not(Py_3_8))] -unsafe fn bpo_35810_workaround(py: Python<'_>, ty: *mut ffi::PyTypeObject) { - #[cfg(Py_LIMITED_API)] - { - // Must check version at runtime for abi3 wheels - they could run against a higher version - // than the build config suggests. - use crate::sync::GILOnceCell; - static IS_PYTHON_3_8: GILOnceCell = GILOnceCell::new(); - - if *IS_PYTHON_3_8.get_or_init(py, || py.version_info() >= (3, 8)) { - // No fix needed - the wheel is running on a sufficiently new interpreter. - return; - } - } - #[cfg(not(Py_LIMITED_API))] - { - // suppress unused variable warning - let _ = py; - } - - ffi::Py_INCREF(ty as *mut ffi::PyObject); -} - -/// Method storage for `#[pyclass]`. -/// -/// Implementation detail. Only to be used through our proc macro code. -/// Allows arbitrary `#[pymethod]` blocks to submit their methods, -/// which are eventually collected by `#[pyclass]`. -#[cfg(feature = "multiple-pymethods")] -pub trait PyClassInventory: inventory::Collect { - /// Returns the items for a single `#[pymethods] impl` block - fn items(&'static self) -> &'static PyClassItems; -} - -// Items from #[pymethods] if not using inventory. -#[cfg(not(feature = "multiple-pymethods"))] -pub trait PyMethods { - fn py_methods(self) -> &'static PyClassItems; -} - -#[cfg(not(feature = "multiple-pymethods"))] -impl PyMethods for &'_ PyClassImplCollector { - fn py_methods(self) -> &'static PyClassItems { - &PyClassItems { - methods: &[], - slots: &[], - } - } -} - -// Text signature for __new__ -pub trait PyClassNewTextSignature { - fn new_text_signature(self) -> Option<&'static str>; -} - -impl PyClassNewTextSignature for &'_ PyClassImplCollector { - #[inline] - fn new_text_signature(self) -> Option<&'static str> { - None - } -} - -// Thread checkers - -#[doc(hidden)] -pub trait PyClassThreadChecker: Sized { - fn ensure(&self); - fn check(&self) -> bool; - fn can_drop(&self, py: Python<'_>) -> bool; - fn new() -> Self; - private_decl! {} -} - -/// Default thread checker for `#[pyclass]`. -/// -/// Keeping the T: Send bound here slightly improves the compile -/// error message to hint to users to figure out what's wrong -/// when `#[pyclass]` types do not implement `Send`. -#[doc(hidden)] -pub struct SendablePyClass(PhantomData); - -impl PyClassThreadChecker for SendablePyClass { - fn ensure(&self) {} - fn check(&self) -> bool { - true - } - fn can_drop(&self, _py: Python<'_>) -> bool { - true - } - #[inline] - fn new() -> Self { - SendablePyClass(PhantomData) - } - private_impl! {} -} - -/// Thread checker for `#[pyclass(unsendable)]` types. -/// Panics when the value is accessed by another thread. -#[doc(hidden)] -pub struct ThreadCheckerImpl(thread::ThreadId); - -impl ThreadCheckerImpl { - fn ensure(&self, type_name: &'static str) { - assert_eq!( - thread::current().id(), - self.0, - "{} is unsendable, but sent to another thread", - type_name - ); - } - - fn check(&self) -> bool { - thread::current().id() == self.0 - } - - fn can_drop(&self, py: Python<'_>, type_name: &'static str) -> bool { - if thread::current().id() != self.0 { - PyRuntimeError::new_err(format!( - "{} is unsendable, but is being dropped on another thread", - type_name - )) - .write_unraisable(py, None); - return false; - } - - true - } -} - -impl PyClassThreadChecker for ThreadCheckerImpl { - fn ensure(&self) { - self.ensure(std::any::type_name::()); - } - fn check(&self) -> bool { - self.check() - } - fn can_drop(&self, py: Python<'_>) -> bool { - self.can_drop(py, std::any::type_name::()) - } - fn new() -> Self { - ThreadCheckerImpl(thread::current().id()) - } - private_impl! {} -} - -/// Trait denoting that this class is suitable to be used as a base type for PyClass. - -#[cfg_attr( - all(diagnostic_namespace, Py_LIMITED_API), - diagnostic::on_unimplemented( - message = "pyclass `{Self}` cannot be subclassed", - label = "required for `#[pyclass(extends={Self})]`", - note = "`{Self}` must have `#[pyclass(subclass)]` to be eligible for subclassing", - note = "with the `abi3` feature enabled, PyO3 does not support subclassing native types", - ) -)] -#[cfg_attr( - all(diagnostic_namespace, not(Py_LIMITED_API)), - diagnostic::on_unimplemented( - message = "pyclass `{Self}` cannot be subclassed", - label = "required for `#[pyclass(extends={Self})]`", - note = "`{Self}` must have `#[pyclass(subclass)]` to be eligible for subclassing", - ) -)] -pub trait PyClassBaseType: Sized { - type LayoutAsBase: PyClassObjectLayout; - type BaseNativeType; - type Initializer: PyObjectInit; - type PyClassMutability: PyClassMutability; -} - -/// Implementation of tp_dealloc for pyclasses without gc -pub(crate) unsafe extern "C" fn tp_dealloc(obj: *mut ffi::PyObject) { - crate::impl_::trampoline::dealloc(obj, PyClassObject::::tp_dealloc) -} - -/// Implementation of tp_dealloc for pyclasses with gc -pub(crate) unsafe extern "C" fn tp_dealloc_with_gc(obj: *mut ffi::PyObject) { - #[cfg(not(PyPy))] - { - ffi::PyObject_GC_UnTrack(obj.cast()); - } - crate::impl_::trampoline::dealloc(obj, PyClassObject::::tp_dealloc) -} - -pub(crate) unsafe extern "C" fn get_sequence_item_from_mapping( - obj: *mut ffi::PyObject, - index: ffi::Py_ssize_t, -) -> *mut ffi::PyObject { - let index = ffi::PyLong_FromSsize_t(index); - if index.is_null() { - return std::ptr::null_mut(); - } - let result = ffi::PyObject_GetItem(obj, index); - ffi::Py_DECREF(index); - result -} - -pub(crate) unsafe extern "C" fn assign_sequence_item_from_mapping( - obj: *mut ffi::PyObject, - index: ffi::Py_ssize_t, - value: *mut ffi::PyObject, -) -> c_int { - let index = ffi::PyLong_FromSsize_t(index); - if index.is_null() { - return -1; - } - let result = if value.is_null() { - ffi::PyObject_DelItem(obj, index) - } else { - ffi::PyObject_SetItem(obj, index, value) - }; - ffi::Py_DECREF(index); - result -} - -/// Helper trait to locate field within a `#[pyclass]` for a `#[pyo3(get)]`. -/// -/// Below MSRV 1.77 we can't use `std::mem::offset_of!`, and the replacement in -/// `memoffset::offset_of` doesn't work in const contexts for types containing `UnsafeCell`. -/// -/// # Safety -/// -/// The trait is unsafe to implement because producing an incorrect offset will lead to UB. -pub unsafe trait OffsetCalculator { - /// Offset to the field within a `PyClassObject`, in bytes. - fn offset() -> usize; -} - -// Used in generated implementations of OffsetCalculator -pub fn class_offset() -> usize { - offset_of!(PyClassObject, contents) -} - -// Used in generated implementations of OffsetCalculator -pub use memoffset::offset_of; - -/// Type which uses specialization on impl blocks to determine how to read a field from a Rust pyclass -/// as part of a `#[pyo3(get)]` annotation. -pub struct PyClassGetterGenerator< - // structural information about the field: class type, field type, where the field is within the - // class struct - ClassT: PyClass, - FieldT, - Offset: OffsetCalculator, // on Rust 1.77+ this could be a const OFFSET: usize - // additional metadata about the field which is used to switch between different implementations - // at compile time - const IS_PY_T: bool, - const IMPLEMENTS_TOPYOBJECT: bool, - const IMPLEMENTS_INTOPY: bool, - const IMPLEMENTS_INTOPYOBJECT_REF: bool, - const IMPLEMENTS_INTOPYOBJECT: bool, ->(PhantomData<(ClassT, FieldT, Offset)>); - -impl< - ClassT: PyClass, - FieldT, - Offset: OffsetCalculator, - const IS_PY_T: bool, - const IMPLEMENTS_TOPYOBJECT: bool, - const IMPLEMENTS_INTOPY: bool, - const IMPLEMENTS_INTOPYOBJECT_REF: bool, - const IMPLEMENTS_INTOPYOBJECT: bool, - > - PyClassGetterGenerator< - ClassT, - FieldT, - Offset, - IS_PY_T, - IMPLEMENTS_TOPYOBJECT, - IMPLEMENTS_INTOPY, - IMPLEMENTS_INTOPYOBJECT_REF, - IMPLEMENTS_INTOPYOBJECT, - > -{ - /// Safety: constructing this type requires that there exists a value of type FieldT - /// at the calculated offset within the type ClassT. - pub const unsafe fn new() -> Self { - Self(PhantomData) - } -} - -impl< - ClassT: PyClass, - U, - Offset: OffsetCalculator>, - const IMPLEMENTS_TOPYOBJECT: bool, - const IMPLEMENTS_INTOPY: bool, - const IMPLEMENTS_INTOPYOBJECT_REF: bool, - const IMPLEMENTS_INTOPYOBJECT: bool, - > - PyClassGetterGenerator< - ClassT, - Py, - Offset, - true, - IMPLEMENTS_TOPYOBJECT, - IMPLEMENTS_INTOPY, - IMPLEMENTS_INTOPYOBJECT_REF, - IMPLEMENTS_INTOPYOBJECT, - > -{ - /// `Py` fields have a potential optimization to use Python's "struct members" to read - /// the field directly from the struct, rather than using a getter function. - /// - /// This is the most efficient operation the Python interpreter could possibly do to - /// read a field, but it's only possible for us to allow this for frozen classes. - pub fn generate(&self, name: &'static CStr, doc: &'static CStr) -> PyMethodDefType { - use crate::pyclass::boolean_struct::private::Boolean; - if ClassT::Frozen::VALUE { - PyMethodDefType::StructMember(ffi::PyMemberDef { - name: name.as_ptr(), - type_code: ffi::Py_T_OBJECT_EX, - offset: Offset::offset() as ffi::Py_ssize_t, - flags: ffi::Py_READONLY, - doc: doc.as_ptr(), - }) - } else { - PyMethodDefType::Getter(PyGetterDef { - name, - meth: pyo3_get_value_topyobject::, Offset>, - doc, - }) - } - } -} - -/// Fallback case; Field is not `Py`; try to use `ToPyObject` to avoid potentially expensive -/// clones of containers like `Vec` -#[allow(deprecated)] -impl< - ClassT: PyClass, - FieldT: ToPyObject, - Offset: OffsetCalculator, - const IMPLEMENTS_INTOPY: bool, - > PyClassGetterGenerator -{ - pub const fn generate(&self, name: &'static CStr, doc: &'static CStr) -> PyMethodDefType { - PyMethodDefType::Getter(PyGetterDef { - name, - meth: pyo3_get_value_topyobject::, - doc, - }) - } -} - -/// Field is not `Py`; try to use `IntoPyObject` for `&T` (prefered over `ToPyObject`) to avoid -/// potentially expensive clones of containers like `Vec` -impl< - ClassT, - FieldT, - Offset, - const IMPLEMENTS_TOPYOBJECT: bool, - const IMPLEMENTS_INTOPY: bool, - const IMPLEMENTS_INTOPYOBJECT: bool, - > - PyClassGetterGenerator< - ClassT, - FieldT, - Offset, - false, - IMPLEMENTS_TOPYOBJECT, - IMPLEMENTS_INTOPY, - true, - IMPLEMENTS_INTOPYOBJECT, - > -where - ClassT: PyClass, - for<'a, 'py> &'a FieldT: IntoPyObject<'py>, - Offset: OffsetCalculator, -{ - pub const fn generate(&self, name: &'static CStr, doc: &'static CStr) -> PyMethodDefType { - PyMethodDefType::Getter(PyGetterDef { - name, - meth: pyo3_get_value_into_pyobject_ref::, - doc, - }) - } -} - -/// Temporary case to prefer `IntoPyObject + Clone` over `IntoPy + Clone`, while still showing the -/// `IntoPyObject` suggestion if neither is implemented; -impl - PyClassGetterGenerator< - ClassT, - FieldT, - Offset, - false, - IMPLEMENTS_TOPYOBJECT, - IMPLEMENTS_INTOPY, - false, - true, - > -where - ClassT: PyClass, - Offset: OffsetCalculator, - for<'py> FieldT: IntoPyObject<'py> + Clone, -{ - pub const fn generate(&self, name: &'static CStr, doc: &'static CStr) -> PyMethodDefType { - PyMethodDefType::Getter(PyGetterDef { - name, - meth: pyo3_get_value_into_pyobject::, - doc, - }) - } -} - -/// IntoPy + Clone fallback case, which was the only behaviour before PyO3 0.22. -#[allow(deprecated)] -impl - PyClassGetterGenerator -where - ClassT: PyClass, - Offset: OffsetCalculator, - FieldT: IntoPy> + Clone, -{ - pub const fn generate(&self, name: &'static CStr, doc: &'static CStr) -> PyMethodDefType { - PyMethodDefType::Getter(PyGetterDef { - name, - meth: pyo3_get_value::, - doc, - }) - } -} - -#[cfg_attr( - diagnostic_namespace, - diagnostic::on_unimplemented( - message = "`{Self}` cannot be converted to a Python object", - label = "required by `#[pyo3(get)]` to create a readable property from a field of type `{Self}`", - note = "implement `IntoPyObject` for `&{Self}` or `IntoPyObject + Clone` for `{Self}` to define the conversion" - ) -)] -pub trait PyO3GetField<'py>: IntoPyObject<'py> + Clone {} -impl<'py, T> PyO3GetField<'py> for T where T: IntoPyObject<'py> + Clone {} - -/// Base case attempts to use IntoPyObject + Clone -impl> - PyClassGetterGenerator -{ - pub const fn generate(&self, _name: &'static CStr, _doc: &'static CStr) -> PyMethodDefType - // The bound goes here rather than on the block so that this impl is always available - // if no specialization is used instead - where - for<'py> FieldT: PyO3GetField<'py>, - { - // unreachable not allowed in const - panic!( - "exists purely to emit diagnostics on unimplemented traits. When `ToPyObject` \ - and `IntoPy` are fully removed this will be replaced by the temporary `IntoPyObject` case above." - ) - } -} - -/// ensures `obj` is not mutably aliased -#[inline] -unsafe fn ensure_no_mutable_alias<'py, ClassT: PyClass>( - py: Python<'py>, - obj: &*mut ffi::PyObject, -) -> Result, PyBorrowError> { - BoundRef::ref_from_ptr(py, obj) - .downcast_unchecked::() - .try_borrow() -} - -/// calculates the field pointer from an PyObject pointer -#[inline] -fn field_from_object(obj: *mut ffi::PyObject) -> *mut FieldT -where - ClassT: PyClass, - Offset: OffsetCalculator, -{ - unsafe { obj.cast::().add(Offset::offset()).cast::() } -} - -#[allow(deprecated)] -fn pyo3_get_value_topyobject< - ClassT: PyClass, - FieldT: ToPyObject, - Offset: OffsetCalculator, ->( - py: Python<'_>, - obj: *mut ffi::PyObject, -) -> PyResult<*mut ffi::PyObject> { - let _holder = unsafe { ensure_no_mutable_alias::(py, &obj)? }; - let value = field_from_object::(obj); - - // SAFETY: Offset is known to describe the location of the value, and - // _holder is preventing mutable aliasing - Ok((unsafe { &*value }).to_object(py).into_ptr()) -} - -fn pyo3_get_value_into_pyobject_ref( - py: Python<'_>, - obj: *mut ffi::PyObject, -) -> PyResult<*mut ffi::PyObject> -where - ClassT: PyClass, - for<'a, 'py> &'a FieldT: IntoPyObject<'py>, - Offset: OffsetCalculator, -{ - let _holder = unsafe { ensure_no_mutable_alias::(py, &obj)? }; - let value = field_from_object::(obj); - - // SAFETY: Offset is known to describe the location of the value, and - // _holder is preventing mutable aliasing - Ok((unsafe { &*value }) - .into_pyobject(py) - .map_err(Into::into)? - .into_ptr()) -} - -fn pyo3_get_value_into_pyobject( - py: Python<'_>, - obj: *mut ffi::PyObject, -) -> PyResult<*mut ffi::PyObject> -where - ClassT: PyClass, - for<'py> FieldT: IntoPyObject<'py> + Clone, - Offset: OffsetCalculator, -{ - let _holder = unsafe { ensure_no_mutable_alias::(py, &obj)? }; - let value = field_from_object::(obj); - - // SAFETY: Offset is known to describe the location of the value, and - // _holder is preventing mutable aliasing - Ok((unsafe { &*value }) - .clone() - .into_pyobject(py) - .map_err(Into::into)? - .into_ptr()) -} - -#[allow(deprecated)] -fn pyo3_get_value< - ClassT: PyClass, - FieldT: IntoPy> + Clone, - Offset: OffsetCalculator, ->( - py: Python<'_>, - obj: *mut ffi::PyObject, -) -> PyResult<*mut ffi::PyObject> { - let _holder = unsafe { ensure_no_mutable_alias::(py, &obj)? }; - let value = field_from_object::(obj); - - // SAFETY: Offset is known to describe the location of the value, and - // _holder is preventing mutable aliasing - Ok((unsafe { &*value }).clone().into_py(py).into_ptr()) -} - -pub struct ConvertField< - const IMPLEMENTS_INTOPYOBJECT_REF: bool, - const IMPLEMENTS_INTOPYOBJECT: bool, ->; - -impl ConvertField { - #[inline] - pub fn convert_field<'a, 'py, T>(obj: &'a T, py: Python<'py>) -> PyResult> - where - &'a T: IntoPyObject<'py>, - { - obj.into_py_any(py) - } -} - -impl ConvertField { - #[inline] - pub fn convert_field<'py, T>(obj: &T, py: Python<'py>) -> PyResult> - where - T: PyO3GetField<'py>, - { - obj.clone().into_py_any(py) - } -} - -/// Marker trait whether a class implemented a custom comparison. Used to -/// silence deprecation of autogenerated `__richcmp__` for enums. -pub trait HasCustomRichCmp {} - -/// Autoref specialization setup to emit deprecation warnings for autogenerated -/// pyclass equality. -pub struct DeprecationTest(Deprecation, ::std::marker::PhantomData); -pub struct Deprecation; - -impl DeprecationTest { - #[inline] - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - DeprecationTest(Deprecation, ::std::marker::PhantomData) - } -} - -impl std::ops::Deref for DeprecationTest { - type Target = Deprecation; - #[inline] - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DeprecationTest -where - T: HasCustomRichCmp, -{ - /// For `HasCustomRichCmp` types; no deprecation warning. - #[inline] - pub fn autogenerated_equality(&self) {} -} - -impl Deprecation { - #[deprecated( - since = "0.22.0", - note = "Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior." - )] - /// For types which don't implement `HasCustomRichCmp`; emits deprecation warning. - #[inline] - pub fn autogenerated_equality(&self) {} -} - -#[cfg(test)] -#[cfg(feature = "macros")] -mod tests { - use super::*; - - #[test] - fn get_py_for_frozen_class() { - #[crate::pyclass(crate = "crate", frozen)] - struct FrozenClass { - #[pyo3(get)] - value: Py, - } - - let mut methods = Vec::new(); - let mut slots = Vec::new(); - - for items in FrozenClass::items_iter() { - methods.extend(items.methods.iter().map(|m| match m { - MaybeRuntimePyMethodDef::Static(m) => m.clone(), - MaybeRuntimePyMethodDef::Runtime(r) => r(), - })); - slots.extend_from_slice(items.slots); - } - - assert_eq!(methods.len(), 1); - assert!(slots.is_empty()); - - match methods.first() { - Some(PyMethodDefType::StructMember(member)) => { - assert_eq!(unsafe { CStr::from_ptr(member.name) }, ffi::c_str!("value")); - assert_eq!(member.type_code, ffi::Py_T_OBJECT_EX); - assert_eq!( - member.offset, - (memoffset::offset_of!(PyClassObject, contents) - + memoffset::offset_of!(FrozenClass, value)) - as ffi::Py_ssize_t - ); - assert_eq!(member.flags, ffi::Py_READONLY); - } - _ => panic!("Expected a StructMember"), - } - } - - #[test] - fn get_py_for_non_frozen_class() { - #[crate::pyclass(crate = "crate")] - struct FrozenClass { - #[pyo3(get)] - value: Py, - } - - let mut methods = Vec::new(); - let mut slots = Vec::new(); - - for items in FrozenClass::items_iter() { - methods.extend(items.methods.iter().map(|m| match m { - MaybeRuntimePyMethodDef::Static(m) => m.clone(), - MaybeRuntimePyMethodDef::Runtime(r) => r(), - })); - slots.extend_from_slice(items.slots); - } - - assert_eq!(methods.len(), 1); - assert!(slots.is_empty()); - - match methods.first() { - Some(PyMethodDefType::Getter(getter)) => { - assert_eq!(getter.name, ffi::c_str!("value")); - assert_eq!(getter.doc, ffi::c_str!("")); - // tests for the function pointer are in test_getter_setter.py - } - _ => panic!("Expected a StructMember"), - } - } -} diff --git a/vendor/pyo3/src/impl_/pyclass/assertions.rs b/vendor/pyo3/src/impl_/pyclass/assertions.rs deleted file mode 100644 index daf2ed5310..0000000000 --- a/vendor/pyo3/src/impl_/pyclass/assertions.rs +++ /dev/null @@ -1,40 +0,0 @@ -/// Helper function that can be used at compile time to emit a diagnostic if -/// the type does not implement `Sync` when it should. -/// -/// The mere act of invoking this function will cause the diagnostic to be -/// emitted if `T` does not implement `Sync` when it should. -/// -/// The additional `const IS_SYNC: bool` parameter is used to allow the custom -/// diagnostic to be emitted; if `PyClassSync` -#[allow(unused)] -pub const fn assert_pyclass_sync() -where - T: PyClassSync + Sync, -{ -} - -#[cfg_attr( - diagnostic_namespace, - diagnostic::on_unimplemented( - message = "the trait `Sync` is not implemented for `{Self}`", - label = "required by `#[pyclass]`", - note = "replace thread-unsafe fields with thread-safe alternatives", - note = "see for more information", - ) -)] -pub trait PyClassSync {} - -impl PyClassSync for T where T: Sync {} - -mod tests { - #[cfg(feature = "macros")] - #[test] - fn test_assert_pyclass_sync() { - use super::assert_pyclass_sync; - - #[crate::pyclass(crate = "crate")] - struct MyClass {} - - assert_pyclass_sync::(); - } -} diff --git a/vendor/pyo3/src/impl_/pyclass/lazy_type_object.rs b/vendor/pyo3/src/impl_/pyclass/lazy_type_object.rs deleted file mode 100644 index d3bede7b2f..0000000000 --- a/vendor/pyo3/src/impl_/pyclass/lazy_type_object.rs +++ /dev/null @@ -1,230 +0,0 @@ -use std::{ - ffi::CStr, - marker::PhantomData, - thread::{self, ThreadId}, -}; - -use crate::{ - exceptions::PyRuntimeError, - ffi, - impl_::pyclass::MaybeRuntimePyMethodDef, - impl_::pymethods::PyMethodDefType, - pyclass::{create_type_object, PyClassTypeObject}, - sync::GILOnceCell, - types::PyType, - Bound, PyClass, PyErr, PyObject, PyResult, Python, -}; - -use std::sync::Mutex; - -use super::PyClassItemsIter; - -/// Lazy type object for PyClass. -#[doc(hidden)] -pub struct LazyTypeObject(LazyTypeObjectInner, PhantomData); - -// Non-generic inner of LazyTypeObject to keep code size down -struct LazyTypeObjectInner { - value: GILOnceCell, - // Threads which have begun initialization of the `tp_dict`. Used for - // reentrant initialization detection. - initializing_threads: Mutex>, - tp_dict_filled: GILOnceCell<()>, -} - -impl LazyTypeObject { - /// Creates an uninitialized `LazyTypeObject`. - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - LazyTypeObject( - LazyTypeObjectInner { - value: GILOnceCell::new(), - initializing_threads: Mutex::new(Vec::new()), - tp_dict_filled: GILOnceCell::new(), - }, - PhantomData, - ) - } -} - -impl LazyTypeObject { - /// Gets the type object contained by this `LazyTypeObject`, initializing it if needed. - pub fn get_or_init<'py>(&self, py: Python<'py>) -> &Bound<'py, PyType> { - self.get_or_try_init(py).unwrap_or_else(|err| { - err.print(py); - panic!("failed to create type object for {}", T::NAME) - }) - } - - /// Fallible version of the above. - pub(crate) fn get_or_try_init<'py>(&self, py: Python<'py>) -> PyResult<&Bound<'py, PyType>> { - self.0 - .get_or_try_init(py, create_type_object::, T::NAME, T::items_iter()) - } -} - -impl LazyTypeObjectInner { - // Uses dynamically dispatched fn(Python<'py>) -> PyResult - // so that this code is only instantiated once, instead of for every T - // like the generic LazyTypeObject methods above. - fn get_or_try_init<'py>( - &self, - py: Python<'py>, - init: fn(Python<'py>) -> PyResult, - name: &str, - items_iter: PyClassItemsIter, - ) -> PyResult<&Bound<'py, PyType>> { - (|| -> PyResult<_> { - let type_object = self - .value - .get_or_try_init(py, || init(py))? - .type_object - .bind(py); - self.ensure_init(type_object, name, items_iter)?; - Ok(type_object) - })() - .map_err(|err| { - wrap_in_runtime_error( - py, - err, - format!("An error occurred while initializing class {}", name), - ) - }) - } - - fn ensure_init( - &self, - type_object: &Bound<'_, PyType>, - name: &str, - items_iter: PyClassItemsIter, - ) -> PyResult<()> { - let py = type_object.py(); - - // We might want to fill the `tp_dict` with python instances of `T` - // itself. In order to do so, we must first initialize the type object - // with an empty `tp_dict`: now we can create instances of `T`. - // - // Then we fill the `tp_dict`. Multiple threads may try to fill it at - // the same time, but only one of them will succeed. - // - // More importantly, if a thread is performing initialization of the - // `tp_dict`, it can still request the type object through `get_or_init`, - // but the `tp_dict` may appear empty of course. - - if self.tp_dict_filled.get(py).is_some() { - // `tp_dict` is already filled: ok. - return Ok(()); - } - - let thread_id = thread::current().id(); - { - let mut threads = self.initializing_threads.lock().unwrap(); - if threads.contains(&thread_id) { - // Reentrant call: just return the type object, even if the - // `tp_dict` is not filled yet. - return Ok(()); - } - threads.push(thread_id); - } - - struct InitializationGuard<'a> { - initializing_threads: &'a Mutex>, - thread_id: ThreadId, - } - impl Drop for InitializationGuard<'_> { - fn drop(&mut self) { - let mut threads = self.initializing_threads.lock().unwrap(); - threads.retain(|id| *id != self.thread_id); - } - } - - let guard = InitializationGuard { - initializing_threads: &self.initializing_threads, - thread_id, - }; - - // Pre-compute the class attribute objects: this can temporarily - // release the GIL since we're calling into arbitrary user code. It - // means that another thread can continue the initialization in the - // meantime: at worst, we'll just make a useless computation. - let mut items = vec![]; - for class_items in items_iter { - for def in class_items.methods { - let built_method; - let method = match def { - MaybeRuntimePyMethodDef::Runtime(builder) => { - built_method = builder(); - &built_method - } - MaybeRuntimePyMethodDef::Static(method) => method, - }; - if let PyMethodDefType::ClassAttribute(attr) = method { - match (attr.meth)(py) { - Ok(val) => items.push((attr.name, val)), - Err(err) => { - return Err(wrap_in_runtime_error( - py, - err, - format!( - "An error occurred while initializing `{}.{}`", - name, - attr.name.to_str().unwrap() - ), - )) - } - } - } - } - } - - // Now we hold the GIL and we can assume it won't be released until we - // return from the function. - let result = self.tp_dict_filled.get_or_try_init(py, move || { - let result = initialize_tp_dict(py, type_object.as_ptr(), items); - - // Initialization successfully complete, can clear the thread list. - // (No further calls to get_or_init() will try to init, on any thread.) - let mut threads = { - drop(guard); - self.initializing_threads.lock().unwrap() - }; - threads.clear(); - result - }); - - if let Err(err) = result { - return Err(wrap_in_runtime_error( - py, - err.clone_ref(py), - format!("An error occurred while initializing `{}.__dict__`", name), - )); - } - - Ok(()) - } -} - -fn initialize_tp_dict( - py: Python<'_>, - type_object: *mut ffi::PyObject, - items: Vec<(&'static CStr, PyObject)>, -) -> PyResult<()> { - // We hold the GIL: the dictionary update can be considered atomic from - // the POV of other threads. - for (key, val) in items { - crate::err::error_on_minusone(py, unsafe { - ffi::PyObject_SetAttrString(type_object, key.as_ptr(), val.into_ptr()) - })?; - } - Ok(()) -} - -// This is necessary for making static `LazyTypeObject`s -unsafe impl Sync for LazyTypeObject {} - -#[cold] -fn wrap_in_runtime_error(py: Python<'_>, err: PyErr, message: String) -> PyErr { - let runtime_err = PyRuntimeError::new_err(message); - runtime_err.set_cause(py, Some(err)); - runtime_err -} diff --git a/vendor/pyo3/src/impl_/pyclass/probes.rs b/vendor/pyo3/src/impl_/pyclass/probes.rs deleted file mode 100644 index f1c3468cf9..0000000000 --- a/vendor/pyo3/src/impl_/pyclass/probes.rs +++ /dev/null @@ -1,72 +0,0 @@ -use std::marker::PhantomData; - -use crate::{conversion::IntoPyObject, Py}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -/// Trait used to combine with zero-sized types to calculate at compile time -/// some property of a type. -/// -/// The trick uses the fact that an associated constant has higher priority -/// than a trait constant, so we can use the trait to define the false case. -/// -/// The true case is defined in the zero-sized type's impl block, which is -/// gated on some property like trait bound or only being implemented -/// for fixed concrete types. -pub trait Probe { - const VALUE: bool = false; -} - -macro_rules! probe { - ($name:ident) => { - pub struct $name(PhantomData); - impl Probe for $name {} - }; -} - -probe!(IsPyT); - -impl IsPyT> { - pub const VALUE: bool = true; -} - -probe!(IsToPyObject); - -#[allow(deprecated)] -impl IsToPyObject { - pub const VALUE: bool = true; -} - -probe!(IsIntoPy); - -#[allow(deprecated)] -impl> IsIntoPy { - pub const VALUE: bool = true; -} - -probe!(IsIntoPyObjectRef); - -// Possible clippy beta regression, -// see https://github.com/rust-lang/rust-clippy/issues/13578 -#[allow(clippy::extra_unused_lifetimes)] -impl<'a, 'py, T: 'a> IsIntoPyObjectRef -where - &'a T: IntoPyObject<'py>, -{ - pub const VALUE: bool = true; -} - -probe!(IsIntoPyObject); - -impl<'py, T> IsIntoPyObject -where - T: IntoPyObject<'py>, -{ - pub const VALUE: bool = true; -} - -probe!(IsSync); - -impl IsSync { - pub const VALUE: bool = true; -} diff --git a/vendor/pyo3/src/impl_/pyclass_init.rs b/vendor/pyo3/src/impl_/pyclass_init.rs deleted file mode 100644 index 7242b6186d..0000000000 --- a/vendor/pyo3/src/impl_/pyclass_init.rs +++ /dev/null @@ -1,89 +0,0 @@ -//! Contains initialization utilities for `#[pyclass]`. -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::internal::get_slot::TP_ALLOC; -use crate::types::PyType; -use crate::{ffi, Borrowed, PyErr, PyResult, Python}; -use crate::{ffi::PyTypeObject, sealed::Sealed, type_object::PyTypeInfo}; -use std::marker::PhantomData; - -/// Initializer for Python types. -/// -/// This trait is intended to use internally for distinguishing `#[pyclass]` and -/// Python native types. -pub trait PyObjectInit: Sized + Sealed { - /// # Safety - /// - `subtype` must be a valid pointer to a type object of T or a subclass. - unsafe fn into_new_object( - self, - py: Python<'_>, - subtype: *mut PyTypeObject, - ) -> PyResult<*mut ffi::PyObject>; - - #[doc(hidden)] - fn can_be_subclassed(&self) -> bool; -} - -/// Initializer for Python native types, like `PyDict`. -pub struct PyNativeTypeInitializer(pub PhantomData); - -impl PyObjectInit for PyNativeTypeInitializer { - unsafe fn into_new_object( - self, - py: Python<'_>, - subtype: *mut PyTypeObject, - ) -> PyResult<*mut ffi::PyObject> { - unsafe fn inner( - py: Python<'_>, - type_object: *mut PyTypeObject, - subtype: *mut PyTypeObject, - ) -> PyResult<*mut ffi::PyObject> { - // HACK (due to FIXME below): PyBaseObject_Type's tp_new isn't happy with NULL arguments - let is_base_object = type_object == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type); - let subtype_borrowed: Borrowed<'_, '_, PyType> = subtype - .cast::() - .assume_borrowed_unchecked(py) - .downcast_unchecked(); - - if is_base_object { - let alloc = subtype_borrowed - .get_slot(TP_ALLOC) - .unwrap_or(ffi::PyType_GenericAlloc); - - let obj = alloc(subtype, 0); - return if obj.is_null() { - Err(PyErr::fetch(py)) - } else { - Ok(obj) - }; - } - - #[cfg(Py_LIMITED_API)] - unreachable!("subclassing native types is not possible with the `abi3` feature"); - - #[cfg(not(Py_LIMITED_API))] - { - match (*type_object).tp_new { - // FIXME: Call __new__ with actual arguments - Some(newfunc) => { - let obj = newfunc(subtype, std::ptr::null_mut(), std::ptr::null_mut()); - if obj.is_null() { - Err(PyErr::fetch(py)) - } else { - Ok(obj) - } - } - None => Err(crate::exceptions::PyTypeError::new_err( - "base type without tp_new", - )), - } - } - } - let type_object = T::type_object_raw(py); - inner(py, type_object, subtype) - } - - #[inline] - fn can_be_subclassed(&self) -> bool { - true - } -} diff --git a/vendor/pyo3/src/impl_/pyfunction.rs b/vendor/pyo3/src/impl_/pyfunction.rs deleted file mode 100644 index c389165488..0000000000 --- a/vendor/pyo3/src/impl_/pyfunction.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::{ - types::{PyCFunction, PyModule}, - Borrowed, Bound, PyResult, Python, -}; - -pub use crate::impl_::pymethods::PyMethodDef; - -/// Trait to enable the use of `wrap_pyfunction` with both `Python` and `PyModule`, -/// and also to infer the return type of either `&'py PyCFunction` or `Bound<'py, PyCFunction>`. -pub trait WrapPyFunctionArg<'py, T> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult; -} - -impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Bound<'py, PyModule> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult> { - PyCFunction::internal_new(self.py(), method_def, Some(&self)) - } -} - -impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for &'_ Bound<'py, PyModule> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult> { - PyCFunction::internal_new(self.py(), method_def, Some(self)) - } -} - -impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Borrowed<'_, 'py, PyModule> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult> { - PyCFunction::internal_new(self.py(), method_def, Some(&self)) - } -} - -impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for &'_ Borrowed<'_, 'py, PyModule> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult> { - PyCFunction::internal_new(self.py(), method_def, Some(self)) - } -} - -impl<'py> WrapPyFunctionArg<'py, Bound<'py, PyCFunction>> for Python<'py> { - fn wrap_pyfunction(self, method_def: &PyMethodDef) -> PyResult> { - PyCFunction::internal_new(self, method_def, None) - } -} diff --git a/vendor/pyo3/src/impl_/pymethods.rs b/vendor/pyo3/src/impl_/pymethods.rs deleted file mode 100644 index e6cbaec86f..0000000000 --- a/vendor/pyo3/src/impl_/pymethods.rs +++ /dev/null @@ -1,745 +0,0 @@ -use crate::exceptions::PyStopAsyncIteration; -use crate::gil::LockGIL; -use crate::impl_::callback::IntoPyCallbackOutput; -use crate::impl_::panic::PanicTrap; -use crate::impl_::pycell::{PyClassObject, PyClassObjectLayout}; -use crate::internal::get_slot::{get_slot, TP_BASE, TP_CLEAR, TP_TRAVERSE}; -use crate::pycell::impl_::PyClassBorrowChecker as _; -use crate::pycell::{PyBorrowError, PyBorrowMutError}; -use crate::pyclass::boolean_struct::False; -use crate::types::any::PyAnyMethods; -use crate::types::PyType; -use crate::{ - ffi, Bound, DowncastError, Py, PyAny, PyClass, PyClassInitializer, PyErr, PyObject, PyRef, - PyRefMut, PyResult, PyTraverseError, PyTypeCheck, PyVisit, Python, -}; -use std::ffi::CStr; -use std::fmt; -use std::marker::PhantomData; -use std::os::raw::{c_int, c_void}; -use std::panic::{catch_unwind, AssertUnwindSafe}; -use std::ptr::null_mut; - -use super::trampoline; -use crate::internal_tricks::{clear_eq, traverse_eq}; - -/// Python 3.8 and up - __ipow__ has modulo argument correctly populated. -#[cfg(Py_3_8)] -#[repr(transparent)] -pub struct IPowModulo(*mut ffi::PyObject); - -/// Python 3.7 and older - __ipow__ does not have modulo argument correctly populated. -#[cfg(not(Py_3_8))] -#[repr(transparent)] -pub struct IPowModulo(#[allow(dead_code)] std::mem::MaybeUninit<*mut ffi::PyObject>); - -/// Helper to use as pymethod ffi definition -#[allow(non_camel_case_types)] -pub type ipowfunc = unsafe extern "C" fn( - arg1: *mut ffi::PyObject, - arg2: *mut ffi::PyObject, - arg3: IPowModulo, -) -> *mut ffi::PyObject; - -impl IPowModulo { - #[cfg(Py_3_8)] - #[inline] - pub fn as_ptr(self) -> *mut ffi::PyObject { - self.0 - } - - #[cfg(not(Py_3_8))] - #[inline] - pub fn as_ptr(self) -> *mut ffi::PyObject { - // Safety: returning a borrowed pointer to Python `None` singleton - unsafe { ffi::Py_None() } - } -} - -/// `PyMethodDefType` represents different types of Python callable objects. -/// It is used by the `#[pymethods]` attribute. -#[cfg_attr(test, derive(Clone))] -pub enum PyMethodDefType { - /// Represents class method - Class(PyMethodDef), - /// Represents static method - Static(PyMethodDef), - /// Represents normal method - Method(PyMethodDef), - /// Represents class attribute, used by `#[attribute]` - ClassAttribute(PyClassAttributeDef), - /// Represents getter descriptor, used by `#[getter]` - Getter(PyGetterDef), - /// Represents setter descriptor, used by `#[setter]` - Setter(PySetterDef), - /// Represents a struct member - StructMember(ffi::PyMemberDef), -} - -#[derive(Copy, Clone, Debug)] -pub enum PyMethodType { - PyCFunction(ffi::PyCFunction), - PyCFunctionWithKeywords(ffi::PyCFunctionWithKeywords), - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - PyCFunctionFastWithKeywords(ffi::PyCFunctionFastWithKeywords), -} - -pub type PyClassAttributeFactory = for<'p> fn(Python<'p>) -> PyResult; - -// TODO: it would be nice to use CStr in these types, but then the constructors can't be const fn -// until `CStr::from_bytes_with_nul_unchecked` is const fn. - -#[derive(Clone, Debug)] -pub struct PyMethodDef { - pub(crate) ml_name: &'static CStr, - pub(crate) ml_meth: PyMethodType, - pub(crate) ml_flags: c_int, - pub(crate) ml_doc: &'static CStr, -} - -#[derive(Copy, Clone)] -pub struct PyClassAttributeDef { - pub(crate) name: &'static CStr, - pub(crate) meth: PyClassAttributeFactory, -} - -#[derive(Clone)] -pub struct PyGetterDef { - pub(crate) name: &'static CStr, - pub(crate) meth: Getter, - pub(crate) doc: &'static CStr, -} - -#[derive(Clone)] -pub struct PySetterDef { - pub(crate) name: &'static CStr, - pub(crate) meth: Setter, - pub(crate) doc: &'static CStr, -} - -unsafe impl Sync for PyMethodDef {} - -unsafe impl Sync for PyGetterDef {} - -unsafe impl Sync for PySetterDef {} - -impl PyMethodDef { - /// Define a function with no `*args` and `**kwargs`. - pub const fn noargs( - ml_name: &'static CStr, - cfunction: ffi::PyCFunction, - ml_doc: &'static CStr, - ) -> Self { - Self { - ml_name, - ml_meth: PyMethodType::PyCFunction(cfunction), - ml_flags: ffi::METH_NOARGS, - ml_doc, - } - } - - /// Define a function that can take `*args` and `**kwargs`. - pub const fn cfunction_with_keywords( - ml_name: &'static CStr, - cfunction: ffi::PyCFunctionWithKeywords, - ml_doc: &'static CStr, - ) -> Self { - Self { - ml_name, - ml_meth: PyMethodType::PyCFunctionWithKeywords(cfunction), - ml_flags: ffi::METH_VARARGS | ffi::METH_KEYWORDS, - ml_doc, - } - } - - /// Define a function that can take `*args` and `**kwargs`. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub const fn fastcall_cfunction_with_keywords( - ml_name: &'static CStr, - cfunction: ffi::PyCFunctionFastWithKeywords, - ml_doc: &'static CStr, - ) -> Self { - Self { - ml_name, - ml_meth: PyMethodType::PyCFunctionFastWithKeywords(cfunction), - ml_flags: ffi::METH_FASTCALL | ffi::METH_KEYWORDS, - ml_doc, - } - } - - pub const fn flags(mut self, flags: c_int) -> Self { - self.ml_flags |= flags; - self - } - - /// Convert `PyMethodDef` to Python method definition struct `ffi::PyMethodDef` - pub(crate) fn as_method_def(&self) -> ffi::PyMethodDef { - let meth = match self.ml_meth { - PyMethodType::PyCFunction(meth) => ffi::PyMethodDefPointer { PyCFunction: meth }, - PyMethodType::PyCFunctionWithKeywords(meth) => ffi::PyMethodDefPointer { - PyCFunctionWithKeywords: meth, - }, - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - PyMethodType::PyCFunctionFastWithKeywords(meth) => ffi::PyMethodDefPointer { - PyCFunctionFastWithKeywords: meth, - }, - }; - - ffi::PyMethodDef { - ml_name: self.ml_name.as_ptr(), - ml_meth: meth, - ml_flags: self.ml_flags, - ml_doc: self.ml_doc.as_ptr(), - } - } -} - -impl PyClassAttributeDef { - /// Define a class attribute. - pub const fn new(name: &'static CStr, meth: PyClassAttributeFactory) -> Self { - Self { name, meth } - } -} - -// Manual implementation because `Python<'_>` does not implement `Debug` and -// trait bounds on `fn` compiler-generated derive impls are too restrictive. -impl fmt::Debug for PyClassAttributeDef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("PyClassAttributeDef") - .field("name", &self.name) - .finish() - } -} - -/// Class getter / setters -pub(crate) type Getter = - for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>; -pub(crate) type Setter = - for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject, *mut ffi::PyObject) -> PyResult; - -impl PyGetterDef { - /// Define a getter. - pub const fn new(name: &'static CStr, getter: Getter, doc: &'static CStr) -> Self { - Self { - name, - meth: getter, - doc, - } - } -} - -impl PySetterDef { - /// Define a setter. - pub const fn new(name: &'static CStr, setter: Setter, doc: &'static CStr) -> Self { - Self { - name, - meth: setter, - doc, - } - } -} - -/// Calls an implementation of __traverse__ for tp_traverse -/// -/// NB cannot accept `'static` visitor, this is a sanity check below: -/// -/// ```rust,compile_fail -/// use pyo3::prelude::*; -/// use pyo3::pyclass::{PyTraverseError, PyVisit}; -/// -/// #[pyclass] -/// struct Foo; -/// -/// #[pymethods] -/// impl Foo { -/// fn __traverse__(&self, _visit: PyVisit<'static>) -> Result<(), PyTraverseError> { -/// Ok(()) -/// } -/// } -/// ``` -/// -/// Elided lifetime should compile ok: -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::pyclass::{PyTraverseError, PyVisit}; -/// -/// #[pyclass] -/// struct Foo; -/// -/// #[pymethods] -/// impl Foo { -/// fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { -/// Ok(()) -/// } -/// } -/// ``` -#[doc(hidden)] -pub unsafe fn _call_traverse( - slf: *mut ffi::PyObject, - impl_: fn(&T, PyVisit<'_>) -> Result<(), PyTraverseError>, - visit: ffi::visitproc, - arg: *mut c_void, - current_traverse: ffi::traverseproc, -) -> c_int -where - T: PyClass, -{ - // It is important the implementation of `__traverse__` cannot safely access the GIL, - // c.f. https://github.com/PyO3/pyo3/issues/3165, and hence we do not expose our GIL - // token to the user code and lock safe methods for acquiring the GIL. - // (This includes enforcing the `&self` method receiver as e.g. `PyRef` could - // reconstruct a GIL token via `PyRef::py`.) - // Since we do not create a `GILPool` at all, it is important that our usage of the GIL - // token does not produce any owned objects thereby calling into `register_owned`. - let trap = PanicTrap::new("uncaught panic inside __traverse__ handler"); - let lock = LockGIL::during_traverse(); - - let super_retval = call_super_traverse(slf, visit, arg, current_traverse); - if super_retval != 0 { - return super_retval; - } - - // SAFETY: `slf` is a valid Python object pointer to a class object of type T, and - // traversal is running so no mutations can occur. - let class_object: &PyClassObject = &*slf.cast(); - - let retval = - // `#[pyclass(unsendable)]` types can only be deallocated by their own thread, so - // do not traverse them if not on their owning thread :( - if class_object.check_threadsafe().is_ok() - // ... and we cannot traverse a type which might be being mutated by a Rust thread - && class_object.borrow_checker().try_borrow().is_ok() { - struct TraverseGuard<'a, T: PyClass>(&'a PyClassObject); - impl Drop for TraverseGuard<'_, T> { - fn drop(&mut self) { - self.0.borrow_checker().release_borrow() - } - } - - // `.try_borrow()` above created a borrow, we need to release it when we're done - // traversing the object. This allows us to read `instance` safely. - let _guard = TraverseGuard(class_object); - let instance = &*class_object.contents.value.get(); - - let visit = PyVisit { visit, arg, _guard: PhantomData }; - - match catch_unwind(AssertUnwindSafe(move || impl_(instance, visit))) { - Ok(Ok(())) => 0, - Ok(Err(traverse_error)) => traverse_error.into_inner(), - Err(_err) => -1, - } - } else { - 0 - }; - - // Drop lock before trap just in case dropping lock panics - drop(lock); - trap.disarm(); - retval -} - -/// Call super-type traverse method, if necessary. -/// -/// Adapted from -/// -/// TODO: There are possible optimizations over looking up the base type in this way -/// - if the base type is known in this module, can potentially look it up directly in module state -/// (when we have it) -/// - if the base type is a Python builtin, can jut call the C function directly -/// - if the base type is a PyO3 type defined in the same module, can potentially do similar to -/// tp_alloc where we solve this at compile time -unsafe fn call_super_traverse( - obj: *mut ffi::PyObject, - visit: ffi::visitproc, - arg: *mut c_void, - current_traverse: ffi::traverseproc, -) -> c_int { - // SAFETY: in this function here it's ok to work with raw type objects `ffi::Py_TYPE` - // because the GC is running and so - // - (a) we cannot do refcounting and - // - (b) the type of the object cannot change. - let mut ty = ffi::Py_TYPE(obj); - let mut traverse: Option; - - // First find the current type by the current_traverse function - loop { - traverse = get_slot(ty, TP_TRAVERSE); - if traverse_eq(traverse, current_traverse) { - break; - } - ty = get_slot(ty, TP_BASE); - if ty.is_null() { - // FIXME: return an error if current type not in the MRO? Should be impossible. - return 0; - } - } - - // Get first base which has a different traverse function - while traverse_eq(traverse, current_traverse) { - ty = get_slot(ty, TP_BASE); - if ty.is_null() { - break; - } - traverse = get_slot(ty, TP_TRAVERSE); - } - - // If we found a type with a different traverse function, call it - if let Some(traverse) = traverse { - return traverse(obj, visit, arg); - } - - // FIXME same question as cython: what if the current type is not in the MRO? - 0 -} - -/// Calls an implementation of __clear__ for tp_clear -pub unsafe fn _call_clear( - slf: *mut ffi::PyObject, - impl_: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject) -> PyResult<()>, - current_clear: ffi::inquiry, -) -> c_int { - trampoline::trampoline(move |py| { - let super_retval = call_super_clear(py, slf, current_clear); - if super_retval != 0 { - return Err(PyErr::fetch(py)); - } - impl_(py, slf)?; - Ok(0) - }) -} - -/// Call super-type traverse method, if necessary. -/// -/// Adapted from -/// -/// TODO: There are possible optimizations over looking up the base type in this way -/// - if the base type is known in this module, can potentially look it up directly in module state -/// (when we have it) -/// - if the base type is a Python builtin, can jut call the C function directly -/// - if the base type is a PyO3 type defined in the same module, can potentially do similar to -/// tp_alloc where we solve this at compile time -unsafe fn call_super_clear( - py: Python<'_>, - obj: *mut ffi::PyObject, - current_clear: ffi::inquiry, -) -> c_int { - let mut ty = PyType::from_borrowed_type_ptr(py, ffi::Py_TYPE(obj)); - let mut clear: Option; - - // First find the current type by the current_clear function - loop { - clear = ty.get_slot(TP_CLEAR); - if clear_eq(clear, current_clear) { - break; - } - let base = ty.get_slot(TP_BASE); - if base.is_null() { - // FIXME: return an error if current type not in the MRO? Should be impossible. - return 0; - } - ty = PyType::from_borrowed_type_ptr(py, base); - } - - // Get first base which has a different clear function - while clear_eq(clear, current_clear) { - let base = ty.get_slot(TP_BASE); - if base.is_null() { - break; - } - ty = PyType::from_borrowed_type_ptr(py, base); - clear = ty.get_slot(TP_CLEAR); - } - - // If we found a type with a different clear function, call it - if let Some(clear) = clear { - return clear(obj); - } - - // FIXME same question as cython: what if the current type is not in the MRO? - 0 -} - -// Autoref-based specialization for handling `__next__` returning `Option` - -pub struct IterBaseTag; - -impl IterBaseTag { - #[inline] - pub fn convert<'py, Value, Target>(self, py: Python<'py>, value: Value) -> PyResult - where - Value: IntoPyCallbackOutput<'py, Target>, - { - value.convert(py) - } -} - -pub trait IterBaseKind { - #[inline] - fn iter_tag(&self) -> IterBaseTag { - IterBaseTag - } -} - -impl IterBaseKind for &Value {} - -pub struct IterOptionTag; - -impl IterOptionTag { - #[inline] - pub fn convert<'py, Value>( - self, - py: Python<'py>, - value: Option, - ) -> PyResult<*mut ffi::PyObject> - where - Value: IntoPyCallbackOutput<'py, *mut ffi::PyObject>, - { - match value { - Some(value) => value.convert(py), - None => Ok(null_mut()), - } - } -} - -pub trait IterOptionKind { - #[inline] - fn iter_tag(&self) -> IterOptionTag { - IterOptionTag - } -} - -impl IterOptionKind for Option {} - -pub struct IterResultOptionTag; - -impl IterResultOptionTag { - #[inline] - pub fn convert<'py, Value, Error>( - self, - py: Python<'py>, - value: Result, Error>, - ) -> PyResult<*mut ffi::PyObject> - where - Value: IntoPyCallbackOutput<'py, *mut ffi::PyObject>, - Error: Into, - { - match value { - Ok(Some(value)) => value.convert(py), - Ok(None) => Ok(null_mut()), - Err(err) => Err(err.into()), - } - } -} - -pub trait IterResultOptionKind { - #[inline] - fn iter_tag(&self) -> IterResultOptionTag { - IterResultOptionTag - } -} - -impl IterResultOptionKind for Result, Error> {} - -// Autoref-based specialization for handling `__anext__` returning `Option` - -pub struct AsyncIterBaseTag; - -impl AsyncIterBaseTag { - #[inline] - pub fn convert<'py, Value, Target>(self, py: Python<'py>, value: Value) -> PyResult - where - Value: IntoPyCallbackOutput<'py, Target>, - { - value.convert(py) - } -} - -pub trait AsyncIterBaseKind { - #[inline] - fn async_iter_tag(&self) -> AsyncIterBaseTag { - AsyncIterBaseTag - } -} - -impl AsyncIterBaseKind for &Value {} - -pub struct AsyncIterOptionTag; - -impl AsyncIterOptionTag { - #[inline] - pub fn convert<'py, Value>( - self, - py: Python<'py>, - value: Option, - ) -> PyResult<*mut ffi::PyObject> - where - Value: IntoPyCallbackOutput<'py, *mut ffi::PyObject>, - { - match value { - Some(value) => value.convert(py), - None => Err(PyStopAsyncIteration::new_err(())), - } - } -} - -pub trait AsyncIterOptionKind { - #[inline] - fn async_iter_tag(&self) -> AsyncIterOptionTag { - AsyncIterOptionTag - } -} - -impl AsyncIterOptionKind for Option {} - -pub struct AsyncIterResultOptionTag; - -impl AsyncIterResultOptionTag { - #[inline] - pub fn convert<'py, Value, Error>( - self, - py: Python<'py>, - value: Result, Error>, - ) -> PyResult<*mut ffi::PyObject> - where - Value: IntoPyCallbackOutput<'py, *mut ffi::PyObject>, - Error: Into, - { - match value { - Ok(Some(value)) => value.convert(py), - Ok(None) => Err(PyStopAsyncIteration::new_err(())), - Err(err) => Err(err.into()), - } - } -} - -pub trait AsyncIterResultOptionKind { - #[inline] - fn async_iter_tag(&self) -> AsyncIterResultOptionTag { - AsyncIterResultOptionTag - } -} - -impl AsyncIterResultOptionKind for Result, Error> {} - -/// Used in `#[classmethod]` to pass the class object to the method -/// and also in `#[pyfunction(pass_module)]`. -/// -/// This is a wrapper to avoid implementing `From` for GIL Refs. -/// -/// Once the GIL Ref API is fully removed, it should be possible to simplify -/// this to just `&'a Bound<'py, T>` and `From` implementations. -pub struct BoundRef<'a, 'py, T>(pub &'a Bound<'py, T>); - -impl<'a, 'py> BoundRef<'a, 'py, PyAny> { - pub unsafe fn ref_from_ptr(py: Python<'py>, ptr: &'a *mut ffi::PyObject) -> Self { - BoundRef(Bound::ref_from_ptr(py, ptr)) - } - - pub unsafe fn ref_from_ptr_or_opt( - py: Python<'py>, - ptr: &'a *mut ffi::PyObject, - ) -> Option { - Bound::ref_from_ptr_or_opt(py, ptr).as_ref().map(BoundRef) - } - - pub fn downcast(self) -> Result, DowncastError<'a, 'py>> { - self.0.downcast::().map(BoundRef) - } - - pub unsafe fn downcast_unchecked(self) -> BoundRef<'a, 'py, T> { - BoundRef(self.0.downcast_unchecked::()) - } -} - -impl<'a, 'py, T: PyClass> TryFrom> for PyRef<'py, T> { - type Error = PyBorrowError; - #[inline] - fn try_from(value: BoundRef<'a, 'py, T>) -> Result { - value.0.try_borrow() - } -} - -impl<'a, 'py, T: PyClass> TryFrom> for PyRefMut<'py, T> { - type Error = PyBorrowMutError; - #[inline] - fn try_from(value: BoundRef<'a, 'py, T>) -> Result { - value.0.try_borrow_mut() - } -} - -impl<'a, 'py, T> From> for Bound<'py, T> { - #[inline] - fn from(bound: BoundRef<'a, 'py, T>) -> Self { - bound.0.clone() - } -} - -impl<'a, 'py, T> From> for &'a Bound<'py, T> { - #[inline] - fn from(bound: BoundRef<'a, 'py, T>) -> Self { - bound.0 - } -} - -impl From> for Py { - #[inline] - fn from(bound: BoundRef<'_, '_, T>) -> Self { - bound.0.clone().unbind() - } -} - -impl<'py, T> std::ops::Deref for BoundRef<'_, 'py, T> { - type Target = Bound<'py, T>; - #[inline] - fn deref(&self) -> &Self::Target { - self.0 - } -} - -pub unsafe fn tp_new_impl( - py: Python<'_>, - initializer: PyClassInitializer, - target_type: *mut ffi::PyTypeObject, -) -> PyResult<*mut ffi::PyObject> { - initializer - .create_class_object_of_type(py, target_type) - .map(Bound::into_ptr) -} - -#[cfg(test)] -mod tests { - #[test] - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn test_fastcall_function_with_keywords() { - use super::PyMethodDef; - use crate::types::{PyAnyMethods, PyCFunction}; - use crate::{ffi, Python}; - - Python::with_gil(|py| { - unsafe extern "C" fn accepts_no_arguments( - _slf: *mut ffi::PyObject, - _args: *const *mut ffi::PyObject, - nargs: ffi::Py_ssize_t, - kwargs: *mut ffi::PyObject, - ) -> *mut ffi::PyObject { - assert_eq!(nargs, 0); - assert!(kwargs.is_null()); - Python::assume_gil_acquired().None().into_ptr() - } - - let f = PyCFunction::internal_new( - py, - &PyMethodDef::fastcall_cfunction_with_keywords( - ffi::c_str!("test"), - accepts_no_arguments, - ffi::c_str!("doc"), - ), - None, - ) - .unwrap(); - - f.call0().unwrap(); - }); - } -} diff --git a/vendor/pyo3/src/impl_/pymodule.rs b/vendor/pyo3/src/impl_/pymodule.rs deleted file mode 100644 index 08b1ead758..0000000000 --- a/vendor/pyo3/src/impl_/pymodule.rs +++ /dev/null @@ -1,303 +0,0 @@ -//! Implementation details of `#[pymodule]` which need to be accessible from proc-macro generated code. - -use std::{cell::UnsafeCell, ffi::CStr, marker::PhantomData}; - -#[cfg(all( - not(any(PyPy, GraalPy)), - Py_3_9, - not(all(windows, Py_LIMITED_API, not(Py_3_10))), - not(target_has_atomic = "64"), -))] -use portable_atomic::AtomicI64; -#[cfg(all( - not(any(PyPy, GraalPy)), - Py_3_9, - not(all(windows, Py_LIMITED_API, not(Py_3_10))), - target_has_atomic = "64", -))] -use std::sync::atomic::AtomicI64; -use std::sync::atomic::{AtomicBool, Ordering}; - -#[cfg(not(any(PyPy, GraalPy)))] -use crate::exceptions::PyImportError; -#[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] -use crate::PyErr; -use crate::{ - ffi, - impl_::pymethods::PyMethodDef, - sync::GILOnceCell, - types::{PyCFunction, PyModule, PyModuleMethods}, - Bound, Py, PyClass, PyResult, PyTypeInfo, Python, -}; - -/// `Sync` wrapper of `ffi::PyModuleDef`. -pub struct ModuleDef { - // wrapped in UnsafeCell so that Rust compiler treats this as interior mutability - ffi_def: UnsafeCell, - initializer: ModuleInitializer, - /// Interpreter ID where module was initialized (not applicable on PyPy). - #[cfg(all( - not(any(PyPy, GraalPy)), - Py_3_9, - not(all(windows, Py_LIMITED_API, not(Py_3_10))) - ))] - interpreter: AtomicI64, - /// Initialized module object, cached to avoid reinitialization. - module: GILOnceCell>, - /// Whether or not the module supports running without the GIL - gil_used: AtomicBool, -} - -/// Wrapper to enable initializer to be used in const fns. -pub struct ModuleInitializer(pub for<'py> fn(&Bound<'py, PyModule>) -> PyResult<()>); - -unsafe impl Sync for ModuleDef {} - -impl ModuleDef { - /// Make new module definition with given module name. - pub const unsafe fn new( - name: &'static CStr, - doc: &'static CStr, - initializer: ModuleInitializer, - ) -> Self { - #[allow(clippy::declare_interior_mutable_const)] - const INIT: ffi::PyModuleDef = ffi::PyModuleDef { - m_base: ffi::PyModuleDef_HEAD_INIT, - m_name: std::ptr::null(), - m_doc: std::ptr::null(), - m_size: 0, - m_methods: std::ptr::null_mut(), - m_slots: std::ptr::null_mut(), - m_traverse: None, - m_clear: None, - m_free: None, - }; - - let ffi_def = UnsafeCell::new(ffi::PyModuleDef { - m_name: name.as_ptr(), - m_doc: doc.as_ptr(), - ..INIT - }); - - ModuleDef { - ffi_def, - initializer, - // -1 is never expected to be a valid interpreter ID - #[cfg(all( - not(any(PyPy, GraalPy)), - Py_3_9, - not(all(windows, Py_LIMITED_API, not(Py_3_10))) - ))] - interpreter: AtomicI64::new(-1), - module: GILOnceCell::new(), - gil_used: AtomicBool::new(true), - } - } - /// Builds a module using user given initializer. Used for [`#[pymodule]`][crate::pymodule]. - #[cfg_attr(any(Py_LIMITED_API, not(Py_GIL_DISABLED)), allow(unused_variables))] - pub fn make_module(&'static self, py: Python<'_>, gil_used: bool) -> PyResult> { - // Check the interpreter ID has not changed, since we currently have no way to guarantee - // that static data is not reused across interpreters. - // - // PyPy does not have subinterpreters, so no need to check interpreter ID. - #[cfg(not(any(PyPy, GraalPy)))] - { - // PyInterpreterState_Get is only available on 3.9 and later, but is missing - // from python3.dll for Windows stable API on 3.9 - #[cfg(all(Py_3_9, not(all(windows, Py_LIMITED_API, not(Py_3_10)))))] - { - let current_interpreter = - unsafe { ffi::PyInterpreterState_GetID(ffi::PyInterpreterState_Get()) }; - crate::err::error_on_minusone(py, current_interpreter)?; - if let Err(initialized_interpreter) = self.interpreter.compare_exchange( - -1, - current_interpreter, - Ordering::SeqCst, - Ordering::SeqCst, - ) { - if initialized_interpreter != current_interpreter { - return Err(PyImportError::new_err( - "PyO3 modules do not yet support subinterpreters, see https://github.com/PyO3/pyo3/issues/576", - )); - } - } - } - #[cfg(not(all(Py_3_9, not(all(windows, Py_LIMITED_API, not(Py_3_10))))))] - { - // CPython before 3.9 does not have APIs to check the interpreter ID, so best that can be - // done to guard against subinterpreters is fail if the module is initialized twice - if self.module.get(py).is_some() { - return Err(PyImportError::new_err( - "PyO3 modules compiled for CPython 3.8 or older may only be initialized once per interpreter process" - )); - } - } - } - self.module - .get_or_try_init(py, || { - let module = unsafe { - Py::::from_owned_ptr_or_err( - py, - ffi::PyModule_Create(self.ffi_def.get()), - )? - }; - #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] - { - let gil_used_ptr = { - if gil_used { - ffi::Py_MOD_GIL_USED - } else { - ffi::Py_MOD_GIL_NOT_USED - } - }; - if unsafe { ffi::PyUnstable_Module_SetGIL(module.as_ptr(), gil_used_ptr) } < 0 { - return Err(PyErr::fetch(py)); - } - } - self.initializer.0(module.bind(py))?; - Ok(module) - }) - .map(|py_module| py_module.clone_ref(py)) - } -} - -/// Trait to add an element (class, function...) to a module. -/// -/// Currently only implemented for classes. -pub trait PyAddToModule: crate::sealed::Sealed { - fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()>; -} - -/// For adding native types (non-pyclass) to a module. -pub struct AddTypeToModule(PhantomData); - -impl AddTypeToModule { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - AddTypeToModule(PhantomData) - } -} - -impl PyAddToModule for AddTypeToModule { - fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add(T::NAME, T::type_object(module.py())) - } -} - -/// For adding a class to a module. -pub struct AddClassToModule(PhantomData); - -impl AddClassToModule { - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - AddClassToModule(PhantomData) - } -} - -impl PyAddToModule for AddClassToModule { - fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_class::() - } -} - -/// For adding a function to a module. -impl PyAddToModule for PyMethodDef { - fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_function(PyCFunction::internal_new(module.py(), self, Some(module))?) - } -} - -/// For adding a module to a module. -impl PyAddToModule for ModuleDef { - fn add_to_module(&'static self, module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_submodule( - self.make_module(module.py(), self.gil_used.load(Ordering::Relaxed))? - .bind(module.py()), - ) - } -} - -#[cfg(test)] -mod tests { - use std::{ - borrow::Cow, - ffi::CStr, - sync::atomic::{AtomicBool, Ordering}, - }; - - use crate::{ - ffi, - types::{any::PyAnyMethods, module::PyModuleMethods, PyModule}, - Bound, PyResult, Python, - }; - - use super::{ModuleDef, ModuleInitializer}; - - #[test] - fn module_init() { - static MODULE_DEF: ModuleDef = unsafe { - ModuleDef::new( - ffi::c_str!("test_module"), - ffi::c_str!("some doc"), - ModuleInitializer(|m| { - m.add("SOME_CONSTANT", 42)?; - Ok(()) - }), - ) - }; - Python::with_gil(|py| { - let module = MODULE_DEF.make_module(py, false).unwrap().into_bound(py); - assert_eq!( - module - .getattr("__name__") - .unwrap() - .extract::>() - .unwrap(), - "test_module", - ); - assert_eq!( - module - .getattr("__doc__") - .unwrap() - .extract::>() - .unwrap(), - "some doc", - ); - assert_eq!( - module - .getattr("SOME_CONSTANT") - .unwrap() - .extract::() - .unwrap(), - 42, - ); - }) - } - - #[test] - fn module_def_new() { - // To get coverage for ModuleDef::new() need to create a non-static ModuleDef, however init - // etc require static ModuleDef, so this test needs to be separated out. - static NAME: &CStr = ffi::c_str!("test_module"); - static DOC: &CStr = ffi::c_str!("some doc"); - - static INIT_CALLED: AtomicBool = AtomicBool::new(false); - - #[allow(clippy::unnecessary_wraps)] - fn init(_: &Bound<'_, PyModule>) -> PyResult<()> { - INIT_CALLED.store(true, Ordering::SeqCst); - Ok(()) - } - - unsafe { - let module_def: ModuleDef = ModuleDef::new(NAME, DOC, ModuleInitializer(init)); - assert_eq!((*module_def.ffi_def.get()).m_name, NAME.as_ptr() as _); - assert_eq!((*module_def.ffi_def.get()).m_doc, DOC.as_ptr() as _); - - Python::with_gil(|py| { - module_def.initializer.0(&py.import("builtins").unwrap()).unwrap(); - assert!(INIT_CALLED.load(Ordering::SeqCst)); - }) - } - } -} diff --git a/vendor/pyo3/src/impl_/trampoline.rs b/vendor/pyo3/src/impl_/trampoline.rs deleted file mode 100644 index 7ffad8abdc..0000000000 --- a/vendor/pyo3/src/impl_/trampoline.rs +++ /dev/null @@ -1,241 +0,0 @@ -//! Trampolines for various pyfunction and pymethod implementations. -//! -//! They exist to monomorphise std::panic::catch_unwind once into PyO3, rather than inline in every -//! function, thus saving a huge amount of compile-time complexity. - -use std::{ - any::Any, - os::raw::c_int, - panic::{self, UnwindSafe}, -}; - -use crate::gil::GILGuard; -use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, impl_::callback::PyCallbackOutput, impl_::panic::PanicTrap, - impl_::pymethods::IPowModulo, panic::PanicException, types::PyModule, Py, PyResult, Python, -}; - -#[inline] -pub unsafe fn module_init( - f: for<'py> unsafe fn(Python<'py>) -> PyResult>, -) -> *mut ffi::PyObject { - trampoline(|py| f(py).map(|module| module.into_ptr())) -} - -#[inline] -#[allow(clippy::used_underscore_binding)] -pub unsafe fn noargs( - slf: *mut ffi::PyObject, - _args: *mut ffi::PyObject, - f: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>, -) -> *mut ffi::PyObject { - #[cfg(not(GraalPy))] // this is not specified and GraalPy does not pass null here - debug_assert!(_args.is_null()); - trampoline(|py| f(py, slf)) -} - -macro_rules! trampoline { - (pub fn $name:ident($($arg_names:ident: $arg_types:ty),* $(,)?) -> $ret:ty;) => { - #[inline] - pub unsafe fn $name( - $($arg_names: $arg_types,)* - f: for<'py> unsafe fn (Python<'py>, $($arg_types),*) -> PyResult<$ret>, - ) -> $ret { - trampoline(|py| f(py, $($arg_names,)*)) - } - } -} - -macro_rules! trampolines { - ($(pub fn $name:ident($($arg_names:ident: $arg_types:ty),* $(,)?) -> $ret:ty);* ;) => { - $(trampoline!(pub fn $name($($arg_names: $arg_types),*) -> $ret;));*; - } -} - -trampolines!( - pub fn fastcall_with_keywords( - slf: *mut ffi::PyObject, - args: *const *mut ffi::PyObject, - nargs: ffi::Py_ssize_t, - kwnames: *mut ffi::PyObject, - ) -> *mut ffi::PyObject; - - pub fn cfunction_with_keywords( - slf: *mut ffi::PyObject, - args: *mut ffi::PyObject, - kwargs: *mut ffi::PyObject, - ) -> *mut ffi::PyObject; -); - -// Trampolines used by slot methods -trampolines!( - pub fn getattrofunc(slf: *mut ffi::PyObject, attr: *mut ffi::PyObject) -> *mut ffi::PyObject; - - pub fn setattrofunc( - slf: *mut ffi::PyObject, - attr: *mut ffi::PyObject, - value: *mut ffi::PyObject, - ) -> c_int; - - pub fn binaryfunc(slf: *mut ffi::PyObject, arg1: *mut ffi::PyObject) -> *mut ffi::PyObject; - - pub fn descrgetfunc( - slf: *mut ffi::PyObject, - arg1: *mut ffi::PyObject, - arg2: *mut ffi::PyObject, - ) -> *mut ffi::PyObject; - - pub fn getiterfunc(slf: *mut ffi::PyObject) -> *mut ffi::PyObject; - - pub fn hashfunc(slf: *mut ffi::PyObject) -> ffi::Py_hash_t; - - pub fn inquiry(slf: *mut ffi::PyObject) -> c_int; - - pub fn iternextfunc(slf: *mut ffi::PyObject) -> *mut ffi::PyObject; - - pub fn lenfunc(slf: *mut ffi::PyObject) -> ffi::Py_ssize_t; - - pub fn newfunc( - subtype: *mut ffi::PyTypeObject, - args: *mut ffi::PyObject, - kwargs: *mut ffi::PyObject, - ) -> *mut ffi::PyObject; - - pub fn objobjproc(slf: *mut ffi::PyObject, arg1: *mut ffi::PyObject) -> c_int; - - pub fn reprfunc(slf: *mut ffi::PyObject) -> *mut ffi::PyObject; - - pub fn richcmpfunc( - slf: *mut ffi::PyObject, - other: *mut ffi::PyObject, - op: c_int, - ) -> *mut ffi::PyObject; - - pub fn ssizeargfunc(arg1: *mut ffi::PyObject, arg2: ffi::Py_ssize_t) -> *mut ffi::PyObject; - - pub fn ternaryfunc( - slf: *mut ffi::PyObject, - arg1: *mut ffi::PyObject, - arg2: *mut ffi::PyObject, - ) -> *mut ffi::PyObject; - - pub fn unaryfunc(slf: *mut ffi::PyObject) -> *mut ffi::PyObject; -); - -#[cfg(any(not(Py_LIMITED_API), Py_3_11))] -trampoline! { - pub fn getbufferproc(slf: *mut ffi::PyObject, buf: *mut ffi::Py_buffer, flags: c_int) -> c_int; -} - -#[cfg(any(not(Py_LIMITED_API), Py_3_11))] -#[inline] -pub unsafe fn releasebufferproc( - slf: *mut ffi::PyObject, - buf: *mut ffi::Py_buffer, - f: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject, *mut ffi::Py_buffer) -> PyResult<()>, -) { - trampoline_unraisable(|py| f(py, slf, buf), slf) -} - -#[inline] -pub(crate) unsafe fn dealloc( - slf: *mut ffi::PyObject, - f: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject) -> (), -) { - // After calling tp_dealloc the object is no longer valid, - // so pass null_mut() to the context. - // - // (Note that we don't allow the implementation `f` to fail.) - trampoline_unraisable( - |py| { - f(py, slf); - Ok(()) - }, - std::ptr::null_mut(), - ) -} - -// Ipowfunc is a unique case where PyO3 has its own type -// to workaround a problem on 3.7 (see IPowModulo type definition). -// Once 3.7 support dropped can just remove this. -trampoline!( - pub fn ipowfunc( - arg1: *mut ffi::PyObject, - arg2: *mut ffi::PyObject, - arg3: IPowModulo, - ) -> *mut ffi::PyObject; -); - -/// Implementation of trampoline functions, which sets up a GILPool and calls F. -/// -/// Panics during execution are trapped so that they don't propagate through any -/// outer FFI boundary. -/// -/// The GIL must already be held when this is called. -#[inline] -pub(crate) unsafe fn trampoline(body: F) -> R -where - F: for<'py> FnOnce(Python<'py>) -> PyResult + UnwindSafe, - R: PyCallbackOutput, -{ - let trap = PanicTrap::new("uncaught panic at ffi boundary"); - - // SAFETY: This function requires the GIL to already be held. - let guard = GILGuard::assume(); - let py = guard.python(); - let out = panic_result_into_callback_output( - py, - panic::catch_unwind(move || -> PyResult<_> { body(py) }), - ); - trap.disarm(); - out -} - -/// Converts the output of std::panic::catch_unwind into a Python function output, either by raising a Python -/// exception or by unwrapping the contained success output. -#[inline] -fn panic_result_into_callback_output( - py: Python<'_>, - panic_result: Result, Box>, -) -> R -where - R: PyCallbackOutput, -{ - let py_err = match panic_result { - Ok(Ok(value)) => return value, - Ok(Err(py_err)) => py_err, - Err(payload) => PanicException::from_panic_payload(payload), - }; - py_err.restore(py); - R::ERR_VALUE -} - -/// Implementation of trampoline for functions which can't return an error. -/// -/// Panics during execution are trapped so that they don't propagate through any -/// outer FFI boundary. -/// -/// Exceptions produced are sent to `sys.unraisablehook`. -/// -/// # Safety -/// -/// - ctx must be either a valid ffi::PyObject or NULL -/// - The GIL must already be held when this is called. -#[inline] -unsafe fn trampoline_unraisable(body: F, ctx: *mut ffi::PyObject) -where - F: for<'py> FnOnce(Python<'py>) -> PyResult<()> + UnwindSafe, -{ - let trap = PanicTrap::new("uncaught panic at ffi boundary"); - - // SAFETY: The GIL is already held. - let guard = GILGuard::assume(); - let py = guard.python(); - - if let Err(py_err) = panic::catch_unwind(move || body(py)) - .unwrap_or_else(|payload| Err(PanicException::from_panic_payload(payload))) - { - py_err.write_unraisable(py, ctx.assume_borrowed_or_opt(py).as_deref()); - } - trap.disarm(); -} diff --git a/vendor/pyo3/src/impl_/wrap.rs b/vendor/pyo3/src/impl_/wrap.rs deleted file mode 100644 index 7b21421940..0000000000 --- a/vendor/pyo3/src/impl_/wrap.rs +++ /dev/null @@ -1,185 +0,0 @@ -use std::{convert::Infallible, marker::PhantomData, ops::Deref}; - -#[allow(deprecated)] -use crate::IntoPy; -use crate::{ffi, types::PyNone, Bound, IntoPyObject, IntoPyObjectExt, PyObject, PyResult, Python}; - -/// Used to wrap values in `Option` for default arguments. -pub trait SomeWrap { - fn wrap(self) -> Option; -} - -impl SomeWrap for T { - fn wrap(self) -> Option { - Some(self) - } -} - -impl SomeWrap for Option { - fn wrap(self) -> Self { - self - } -} - -// Hierarchy of conversions used in the `IntoPy` implementation -pub struct Converter(EmptyTupleConverter); -pub struct EmptyTupleConverter(IntoPyObjectConverter); -pub struct IntoPyObjectConverter(IntoPyConverter); -pub struct IntoPyConverter(UnknownReturnResultType); -pub struct UnknownReturnResultType(UnknownReturnType); -pub struct UnknownReturnType(PhantomData); - -pub fn converter(_: &T) -> Converter { - Converter(EmptyTupleConverter(IntoPyObjectConverter(IntoPyConverter( - UnknownReturnResultType(UnknownReturnType(PhantomData)), - )))) -} - -impl Deref for Converter { - type Target = EmptyTupleConverter; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Deref for EmptyTupleConverter { - type Target = IntoPyObjectConverter; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Deref for IntoPyObjectConverter { - type Target = IntoPyConverter; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Deref for IntoPyConverter { - type Target = UnknownReturnResultType; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl Deref for UnknownReturnResultType { - type Target = UnknownReturnType; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl EmptyTupleConverter> { - #[inline] - pub fn map_into_ptr(&self, py: Python<'_>, obj: PyResult<()>) -> PyResult<*mut ffi::PyObject> { - obj.map(|_| PyNone::get(py).to_owned().into_ptr()) - } -} - -impl<'py, T: IntoPyObject<'py>> IntoPyObjectConverter { - #[inline] - pub fn wrap(&self, obj: T) -> Result { - Ok(obj) - } -} - -impl<'py, T: IntoPyObject<'py>, E> IntoPyObjectConverter> { - #[inline] - pub fn wrap(&self, obj: Result) -> Result { - obj - } - - #[inline] - pub fn map_into_pyobject(&self, py: Python<'py>, obj: PyResult) -> PyResult - where - T: IntoPyObject<'py>, - { - obj.and_then(|obj| obj.into_py_any(py)) - } - - #[inline] - pub fn map_into_ptr(&self, py: Python<'py>, obj: PyResult) -> PyResult<*mut ffi::PyObject> - where - T: IntoPyObject<'py>, - { - obj.and_then(|obj| obj.into_bound_py_any(py)) - .map(Bound::into_ptr) - } -} - -#[allow(deprecated)] -impl> IntoPyConverter { - #[inline] - pub fn wrap(&self, obj: T) -> Result { - Ok(obj) - } -} - -#[allow(deprecated)] -impl, E> IntoPyConverter> { - #[inline] - pub fn wrap(&self, obj: Result) -> Result { - obj - } - - #[inline] - pub fn map_into_pyobject(&self, py: Python<'_>, obj: PyResult) -> PyResult { - obj.map(|obj| obj.into_py(py)) - } - - #[inline] - pub fn map_into_ptr(&self, py: Python<'_>, obj: PyResult) -> PyResult<*mut ffi::PyObject> { - obj.map(|obj| obj.into_py(py).into_ptr()) - } -} - -impl UnknownReturnResultType> { - #[inline] - pub fn wrap<'py>(&self, _: Result) -> Result - where - T: IntoPyObject<'py>, - { - unreachable!("should be handled by IntoPyObjectConverter") - } -} - -impl UnknownReturnType { - #[inline] - pub fn wrap<'py>(&self, _: T) -> T - where - T: IntoPyObject<'py>, - { - unreachable!("should be handled by IntoPyObjectConverter") - } - - #[inline] - pub fn map_into_pyobject<'py>(&self, _: Python<'py>, _: PyResult) -> PyResult - where - T: IntoPyObject<'py>, - { - unreachable!("should be handled by IntoPyObjectConverter") - } - - #[inline] - pub fn map_into_ptr<'py>(&self, _: Python<'py>, _: PyResult) -> PyResult<*mut ffi::PyObject> - where - T: IntoPyObject<'py>, - { - unreachable!("should be handled by IntoPyObjectConverter") - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn wrap_option() { - let a: Option = SomeWrap::wrap(42); - assert_eq!(a, Some(42)); - - let b: Option = SomeWrap::wrap(None); - assert_eq!(b, None); - } -} diff --git a/vendor/pyo3/src/inspect/mod.rs b/vendor/pyo3/src/inspect/mod.rs deleted file mode 100644 index f14f8a2d2a..0000000000 --- a/vendor/pyo3/src/inspect/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Runtime inspection of objects exposed to Python. -//! -//! Tracking issue: . -pub mod types; diff --git a/vendor/pyo3/src/inspect/types.rs b/vendor/pyo3/src/inspect/types.rs deleted file mode 100644 index 50674ce96f..0000000000 --- a/vendor/pyo3/src/inspect/types.rs +++ /dev/null @@ -1,488 +0,0 @@ -//! Data types used to describe runtime Python types. - -use std::borrow::Cow; -use std::fmt::{Display, Formatter}; - -/// Designation of a Python type. -/// -/// This enum is used to handle advanced types, such as types with generics. -/// Its [`Display`] implementation can be used to convert to the type hint notation (e.g. `List[int]`). -#[derive(Debug, Clone, Eq, PartialEq)] -pub enum TypeInfo { - /// The type `typing.Any`, which represents any possible value (unknown type). - Any, - /// The type `typing.None`. - None, - /// The type `typing.NoReturn`, which represents functions that never return (they can still panic / throw, similar to `never` in Rust). - NoReturn, - /// The type `typing.Callable`. - /// - /// The first argument represents the parameters of the callable: - /// - `Some` of a vector of types to represent the signature, - /// - `None` if the signature is unknown (allows any number of arguments with type `Any`). - /// - /// The second argument represents the return type. - Callable(Option>, Box), - /// The type `typing.tuple`. - /// - /// The argument represents the contents of the tuple: - /// - `Some` of a vector of types to represent the accepted types, - /// - `Some` of an empty vector for the empty tuple, - /// - `None` if the number and type of accepted values is unknown. - /// - /// If the number of accepted values is unknown, but their type is, use [`Self::UnsizedTypedTuple`]. - Tuple(Option>), - /// The type `typing.Tuple`. - /// - /// Use this variant to represent a tuple of unknown size but of known types. - /// - /// If the type is unknown, or if the number of elements is known, use [`Self::Tuple`]. - UnsizedTypedTuple(Box), - /// A Python class. - Class { - /// The module this class comes from. - module: ModuleName, - /// The name of this class, as it appears in a type hint. - name: Cow<'static, str>, - /// The generics accepted by this class (empty vector if this class is not generic). - type_vars: Vec, - }, -} - -/// Declares which module a type is a part of. -#[derive(Debug, Clone, Eq, PartialEq)] -pub enum ModuleName { - /// The type is built-in: it doesn't need to be imported. - Builtin, - /// The type is in the current module: it doesn't need to be imported in this module, but needs to be imported in others. - CurrentModule, - /// The type is in the specified module. - Module(Cow<'static, str>), -} - -impl TypeInfo { - /// Returns the module in which a type is declared. - /// - /// Returns `None` if the type is declared in the current module. - pub fn module_name(&self) -> Option<&str> { - match self { - TypeInfo::Any - | TypeInfo::None - | TypeInfo::NoReturn - | TypeInfo::Callable(_, _) - | TypeInfo::Tuple(_) - | TypeInfo::UnsizedTypedTuple(_) => Some("typing"), - TypeInfo::Class { module, .. } => match module { - ModuleName::Builtin => Some("builtins"), - ModuleName::CurrentModule => None, - ModuleName::Module(name) => Some(name), - }, - } - } - - /// Returns the name of a type. - /// - /// The name of a type is the part of the hint that is not generic (e.g. `List` instead of `List[int]`). - pub fn name(&self) -> Cow<'_, str> { - Cow::from(match self { - TypeInfo::Any => "Any", - TypeInfo::None => "None", - TypeInfo::NoReturn => "NoReturn", - TypeInfo::Callable(_, _) => "Callable", - TypeInfo::Tuple(_) => "Tuple", - TypeInfo::UnsizedTypedTuple(_) => "Tuple", - TypeInfo::Class { name, .. } => name, - }) - } -} - -// Utilities for easily instantiating TypeInfo structures for built-in/common types. -impl TypeInfo { - /// The Python `Optional` type. - pub fn optional_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Optional"), - type_vars: vec![t], - } - } - - /// The Python `Union` type. - pub fn union_of(types: &[TypeInfo]) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Union"), - type_vars: types.to_vec(), - } - } - - /// The Python `List` type. - pub fn list_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("List"), - type_vars: vec![t], - } - } - - /// The Python `Sequence` type. - pub fn sequence_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Sequence"), - type_vars: vec![t], - } - } - - /// The Python `Set` type. - pub fn set_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Set"), - type_vars: vec![t], - } - } - - /// The Python `FrozenSet` type. - pub fn frozen_set_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("FrozenSet"), - type_vars: vec![t], - } - } - - /// The Python `Iterable` type. - pub fn iterable_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Iterable"), - type_vars: vec![t], - } - } - - /// The Python `Iterator` type. - pub fn iterator_of(t: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Iterator"), - type_vars: vec![t], - } - } - - /// The Python `Dict` type. - pub fn dict_of(k: TypeInfo, v: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Dict"), - type_vars: vec![k, v], - } - } - - /// The Python `Mapping` type. - pub fn mapping_of(k: TypeInfo, v: TypeInfo) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Module(Cow::from("typing")), - name: Cow::from("Mapping"), - type_vars: vec![k, v], - } - } - - /// Convenience factory for non-generic builtins (e.g. `int`). - pub fn builtin(name: &'static str) -> TypeInfo { - TypeInfo::Class { - module: ModuleName::Builtin, - name: Cow::from(name), - type_vars: vec![], - } - } -} - -impl Display for TypeInfo { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - TypeInfo::Any | TypeInfo::None | TypeInfo::NoReturn => write!(f, "{}", self.name()), - TypeInfo::Callable(input, output) => { - write!(f, "Callable[")?; - - if let Some(input) = input { - write!(f, "[")?; - let mut comma = false; - for arg in input { - if comma { - write!(f, ", ")?; - } - write!(f, "{}", arg)?; - comma = true; - } - write!(f, "]")?; - } else { - write!(f, "...")?; - } - - write!(f, ", {}]", output) - } - TypeInfo::Tuple(types) => { - write!(f, "Tuple[")?; - - if let Some(types) = types { - if types.is_empty() { - write!(f, "()")?; - } else { - let mut comma = false; - for t in types { - if comma { - write!(f, ", ")?; - } - write!(f, "{}", t)?; - comma = true; - } - } - } else { - write!(f, "...")?; - } - - write!(f, "]") - } - TypeInfo::UnsizedTypedTuple(t) => write!(f, "Tuple[{}, ...]", t), - TypeInfo::Class { - name, type_vars, .. - } => { - write!(f, "{}", name)?; - - if !type_vars.is_empty() { - write!(f, "[")?; - - let mut comma = false; - for var in type_vars { - if comma { - write!(f, ", ")?; - } - write!(f, "{}", var)?; - comma = true; - } - - write!(f, "]") - } else { - Ok(()) - } - } - } - } -} - -#[cfg(test)] -mod test { - use std::borrow::Cow; - - use crate::inspect::types::{ModuleName, TypeInfo}; - - #[track_caller] - pub fn assert_display(t: &TypeInfo, expected: &str) { - assert_eq!(format!("{}", t), expected) - } - - #[test] - fn basic() { - assert_display(&TypeInfo::Any, "Any"); - assert_display(&TypeInfo::None, "None"); - assert_display(&TypeInfo::NoReturn, "NoReturn"); - - assert_display(&TypeInfo::builtin("int"), "int"); - } - - #[test] - fn callable() { - let any_to_int = TypeInfo::Callable(None, Box::new(TypeInfo::builtin("int"))); - assert_display(&any_to_int, "Callable[..., int]"); - - let sum = TypeInfo::Callable( - Some(vec![TypeInfo::builtin("int"), TypeInfo::builtin("int")]), - Box::new(TypeInfo::builtin("int")), - ); - assert_display(&sum, "Callable[[int, int], int]"); - } - - #[test] - fn tuple() { - let any = TypeInfo::Tuple(None); - assert_display(&any, "Tuple[...]"); - - let triple = TypeInfo::Tuple(Some(vec![ - TypeInfo::builtin("int"), - TypeInfo::builtin("str"), - TypeInfo::builtin("bool"), - ])); - assert_display(&triple, "Tuple[int, str, bool]"); - - let empty = TypeInfo::Tuple(Some(vec![])); - assert_display(&empty, "Tuple[()]"); - - let typed = TypeInfo::UnsizedTypedTuple(Box::new(TypeInfo::builtin("bool"))); - assert_display(&typed, "Tuple[bool, ...]"); - } - - #[test] - fn class() { - let class1 = TypeInfo::Class { - module: ModuleName::CurrentModule, - name: Cow::from("MyClass"), - type_vars: vec![], - }; - assert_display(&class1, "MyClass"); - - let class2 = TypeInfo::Class { - module: ModuleName::CurrentModule, - name: Cow::from("MyClass"), - type_vars: vec![TypeInfo::builtin("int"), TypeInfo::builtin("bool")], - }; - assert_display(&class2, "MyClass[int, bool]"); - } - - #[test] - fn collections() { - let int = TypeInfo::builtin("int"); - let bool = TypeInfo::builtin("bool"); - let str = TypeInfo::builtin("str"); - - let list = TypeInfo::list_of(int.clone()); - assert_display(&list, "List[int]"); - - let sequence = TypeInfo::sequence_of(bool.clone()); - assert_display(&sequence, "Sequence[bool]"); - - let optional = TypeInfo::optional_of(str.clone()); - assert_display(&optional, "Optional[str]"); - - let iterable = TypeInfo::iterable_of(int.clone()); - assert_display(&iterable, "Iterable[int]"); - - let iterator = TypeInfo::iterator_of(bool); - assert_display(&iterator, "Iterator[bool]"); - - let dict = TypeInfo::dict_of(int.clone(), str.clone()); - assert_display(&dict, "Dict[int, str]"); - - let mapping = TypeInfo::mapping_of(int, str.clone()); - assert_display(&mapping, "Mapping[int, str]"); - - let set = TypeInfo::set_of(str.clone()); - assert_display(&set, "Set[str]"); - - let frozen_set = TypeInfo::frozen_set_of(str); - assert_display(&frozen_set, "FrozenSet[str]"); - } - - #[test] - fn complicated() { - let int = TypeInfo::builtin("int"); - assert_display(&int, "int"); - - let bool = TypeInfo::builtin("bool"); - assert_display(&bool, "bool"); - - let str = TypeInfo::builtin("str"); - assert_display(&str, "str"); - - let any = TypeInfo::Any; - assert_display(&any, "Any"); - - let params = TypeInfo::union_of(&[int.clone(), str]); - assert_display(¶ms, "Union[int, str]"); - - let func = TypeInfo::Callable(Some(vec![params, any]), Box::new(bool)); - assert_display(&func, "Callable[[Union[int, str], Any], bool]"); - - let dict = TypeInfo::mapping_of(int, func); - assert_display( - &dict, - "Mapping[int, Callable[[Union[int, str], Any], bool]]", - ); - } -} - -#[cfg(test)] -mod conversion { - use std::collections::{HashMap, HashSet}; - - use crate::inspect::types::test::assert_display; - use crate::{FromPyObject, IntoPyObject}; - - #[test] - fn unsigned_int() { - assert_display(&usize::type_output(), "int"); - assert_display(&usize::type_input(), "int"); - - assert_display(&u8::type_output(), "int"); - assert_display(&u8::type_input(), "int"); - - assert_display(&u16::type_output(), "int"); - assert_display(&u16::type_input(), "int"); - - assert_display(&u32::type_output(), "int"); - assert_display(&u32::type_input(), "int"); - - assert_display(&u64::type_output(), "int"); - assert_display(&u64::type_input(), "int"); - } - - #[test] - fn signed_int() { - assert_display(&isize::type_output(), "int"); - assert_display(&isize::type_input(), "int"); - - assert_display(&i8::type_output(), "int"); - assert_display(&i8::type_input(), "int"); - - assert_display(&i16::type_output(), "int"); - assert_display(&i16::type_input(), "int"); - - assert_display(&i32::type_output(), "int"); - assert_display(&i32::type_input(), "int"); - - assert_display(&i64::type_output(), "int"); - assert_display(&i64::type_input(), "int"); - } - - #[test] - fn float() { - assert_display(&f32::type_output(), "float"); - assert_display(&f32::type_input(), "float"); - - assert_display(&f64::type_output(), "float"); - assert_display(&f64::type_input(), "float"); - } - - #[test] - fn bool() { - assert_display(&bool::type_output(), "bool"); - assert_display(&bool::type_input(), "bool"); - } - - #[test] - fn text() { - assert_display(&String::type_output(), "str"); - assert_display(&String::type_input(), "str"); - - assert_display(&<&[u8]>::type_output(), "Union[bytes, List[int]]"); - assert_display(&<&[String]>::type_output(), "Union[bytes, List[str]]"); - assert_display( - &<&[u8] as crate::conversion::FromPyObjectBound>::type_input(), - "bytes", - ); - } - - #[test] - fn collections() { - assert_display(&>::type_output(), "List[int]"); - assert_display(&>::type_input(), "Sequence[int]"); - - assert_display(&>::type_output(), "Set[int]"); - assert_display(&>::type_input(), "Set[int]"); - - assert_display(&>::type_output(), "Dict[int, float]"); - assert_display(&>::type_input(), "Mapping[int, float]"); - - assert_display(&<(usize, f32)>::type_input(), "Tuple[int, float]"); - } -} diff --git a/vendor/pyo3/src/instance.rs b/vendor/pyo3/src/instance.rs deleted file mode 100644 index 840416116f..0000000000 --- a/vendor/pyo3/src/instance.rs +++ /dev/null @@ -1,2385 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::err::{self, PyErr, PyResult}; -use crate::impl_::pycell::PyClassObject; -use crate::internal_tricks::ptr_from_ref; -use crate::pycell::{PyBorrowError, PyBorrowMutError}; -use crate::pyclass::boolean_struct::{False, True}; -use crate::types::{any::PyAnyMethods, string::PyStringMethods, typeobject::PyTypeMethods}; -use crate::types::{DerefToPyAny, PyDict, PyString, PyTuple}; -use crate::{ - ffi, AsPyPointer, DowncastError, FromPyObject, PyAny, PyClass, PyClassInitializer, PyRef, - PyRefMut, PyTypeInfo, Python, -}; -use crate::{gil, PyTypeCheck}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::marker::PhantomData; -use std::mem::ManuallyDrop; -use std::ops::Deref; -use std::ptr::NonNull; - -/// Owned or borrowed gil-bound Python smart pointer -/// -/// This is implemented for [`Bound`] and [`Borrowed`]. -pub trait BoundObject<'py, T>: bound_object_sealed::Sealed { - /// Type erased version of `Self` - type Any: BoundObject<'py, PyAny>; - /// Borrow this smart pointer. - fn as_borrowed(&self) -> Borrowed<'_, 'py, T>; - /// Turns this smart pointer into an owned [`Bound<'py, T>`] - fn into_bound(self) -> Bound<'py, T>; - /// Upcast the target type of this smart pointer - fn into_any(self) -> Self::Any; - /// Turn this smart pointer into a strong reference pointer - fn into_ptr(self) -> *mut ffi::PyObject; - /// Turn this smart pointer into a borrowed reference pointer - fn as_ptr(&self) -> *mut ffi::PyObject; - /// Turn this smart pointer into an owned [`Py`] - fn unbind(self) -> Py; -} - -mod bound_object_sealed { - /// # Safety - /// - /// Type must be layout-compatible with `*mut ffi::PyObject`. - pub unsafe trait Sealed {} - - // SAFETY: `Bound` is layout-compatible with `*mut ffi::PyObject`. - unsafe impl Sealed for super::Bound<'_, T> {} - // SAFETY: `Borrowed` is layout-compatible with `*mut ffi::PyObject`. - unsafe impl Sealed for super::Borrowed<'_, '_, T> {} -} - -/// A GIL-attached equivalent to [`Py`]. -/// -/// This type can be thought of as equivalent to the tuple `(Py, Python<'py>)`. By having the `'py` -/// lifetime of the [`Python<'py>`] token, this ties the lifetime of the [`Bound<'py, T>`] smart pointer -/// to the lifetime of the GIL and allows PyO3 to call Python APIs at maximum efficiency. -/// -/// To access the object in situations where the GIL is not held, convert it to [`Py`] -/// using [`.unbind()`][Bound::unbind]. This includes situations where the GIL is temporarily -/// released, such as [`Python::allow_threads`](crate::Python::allow_threads)'s closure. -/// -/// See -#[doc = concat!("[the guide](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/types.html#boundpy-t)")] -/// for more detail. -#[repr(transparent)] -pub struct Bound<'py, T>(Python<'py>, ManuallyDrop>); - -impl<'py, T> Bound<'py, T> -where - T: PyClass, -{ - /// Creates a new instance `Bound` of a `#[pyclass]` on the Python heap. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pyclass] - /// struct Foo {/* fields omitted */} - /// - /// # fn main() -> PyResult<()> { - /// let foo: Py = Python::with_gil(|py| -> PyResult<_> { - /// let foo: Bound<'_, Foo> = Bound::new(py, Foo {})?; - /// Ok(foo.into()) - /// })?; - /// # Python::with_gil(move |_py| drop(foo)); - /// # Ok(()) - /// # } - /// ``` - pub fn new( - py: Python<'py>, - value: impl Into>, - ) -> PyResult> { - value.into().create_class_object(py) - } -} - -impl<'py> Bound<'py, PyAny> { - /// Constructs a new `Bound<'py, PyAny>` from a pointer. Panics if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object - /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership - #[inline] - #[track_caller] - pub unsafe fn from_owned_ptr(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self { - Self(py, ManuallyDrop::new(Py::from_owned_ptr(py, ptr))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer. Returns `None` if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership - #[inline] - pub unsafe fn from_owned_ptr_or_opt(py: Python<'py>, ptr: *mut ffi::PyObject) -> Option { - Py::from_owned_ptr_or_opt(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer. Returns an `Err` by calling `PyErr::fetch` - /// if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - /// - `ptr` must be an owned Python reference, as the `Bound<'py, PyAny>` will assume ownership - #[inline] - pub unsafe fn from_owned_ptr_or_err( - py: Python<'py>, - ptr: *mut ffi::PyObject, - ) -> PyResult { - Py::from_owned_ptr_or_err(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer without checking for null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object - /// - `ptr` must be a strong/owned reference - pub(crate) unsafe fn from_owned_ptr_unchecked( - py: Python<'py>, - ptr: *mut ffi::PyObject, - ) -> Self { - Self(py, ManuallyDrop::new(Py::from_owned_ptr_unchecked(ptr))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer by creating a new Python reference. - /// Panics if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object - #[inline] - #[track_caller] - pub unsafe fn from_borrowed_ptr(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self { - Self(py, ManuallyDrop::new(Py::from_borrowed_ptr(py, ptr))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer by creating a new Python reference. - /// Returns `None` if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - #[inline] - pub unsafe fn from_borrowed_ptr_or_opt( - py: Python<'py>, - ptr: *mut ffi::PyObject, - ) -> Option { - Py::from_borrowed_ptr_or_opt(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj))) - } - - /// Constructs a new `Bound<'py, PyAny>` from a pointer by creating a new Python reference. - /// Returns an `Err` by calling `PyErr::fetch` if `ptr` is null. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - #[inline] - pub unsafe fn from_borrowed_ptr_or_err( - py: Python<'py>, - ptr: *mut ffi::PyObject, - ) -> PyResult { - Py::from_borrowed_ptr_or_err(py, ptr).map(|obj| Self(py, ManuallyDrop::new(obj))) - } - - /// This slightly strange method is used to obtain `&Bound` from a pointer in macro code - /// where we need to constrain the lifetime `'a` safely. - /// - /// Note that `'py` is required to outlive `'a` implicitly by the nature of the fact that - /// `&'a Bound<'py>` means that `Bound<'py>` exists for at least the lifetime `'a`. - /// - /// # Safety - /// - `ptr` must be a valid pointer to a Python object for the lifetime `'a`. The `ptr` can - /// be either a borrowed reference or an owned reference, it does not matter, as this is - /// just `&Bound` there will never be any ownership transfer. - #[inline] - pub(crate) unsafe fn ref_from_ptr<'a>( - _py: Python<'py>, - ptr: &'a *mut ffi::PyObject, - ) -> &'a Self { - &*ptr_from_ref(ptr).cast::>() - } - - /// Variant of the above which returns `None` for null pointers. - /// - /// # Safety - /// - `ptr` must be a valid pointer to a Python object for the lifetime `'a, or null. - #[inline] - pub(crate) unsafe fn ref_from_ptr_or_opt<'a>( - _py: Python<'py>, - ptr: &'a *mut ffi::PyObject, - ) -> &'a Option { - &*ptr_from_ref(ptr).cast::>>() - } -} - -impl<'py, T> Bound<'py, T> -where - T: PyClass, -{ - /// Immutably borrows the value `T`. - /// - /// This borrow lasts while the returned [`PyRef`] exists. - /// Multiple immutable borrows can be taken out at the same time. - /// - /// For frozen classes, the simpler [`get`][Self::get] is available. - /// - /// # Examples - /// - /// ```rust - /// # use pyo3::prelude::*; - /// # - /// #[pyclass] - /// struct Foo { - /// inner: u8, - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let foo: Bound<'_, Foo> = Bound::new(py, Foo { inner: 73 })?; - /// let inner: &u8 = &foo.borrow().inner; - /// - /// assert_eq!(*inner, 73); - /// Ok(()) - /// })?; - /// # Ok(()) - /// # } - /// ``` - /// - /// # Panics - /// - /// Panics if the value is currently mutably borrowed. For a non-panicking variant, use - /// [`try_borrow`](#method.try_borrow). - #[inline] - #[track_caller] - pub fn borrow(&self) -> PyRef<'py, T> { - PyRef::borrow(self) - } - - /// Mutably borrows the value `T`. - /// - /// This borrow lasts while the returned [`PyRefMut`] exists. - /// - /// # Examples - /// - /// ``` - /// # use pyo3::prelude::*; - /// # - /// #[pyclass] - /// struct Foo { - /// inner: u8, - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let foo: Bound<'_, Foo> = Bound::new(py, Foo { inner: 73 })?; - /// foo.borrow_mut().inner = 35; - /// - /// assert_eq!(foo.borrow().inner, 35); - /// Ok(()) - /// })?; - /// # Ok(()) - /// # } - /// ``` - /// - /// # Panics - /// Panics if the value is currently borrowed. For a non-panicking variant, use - /// [`try_borrow_mut`](#method.try_borrow_mut). - #[inline] - #[track_caller] - pub fn borrow_mut(&self) -> PyRefMut<'py, T> - where - T: PyClass, - { - PyRefMut::borrow(self) - } - - /// Attempts to immutably borrow the value `T`, returning an error if the value is currently mutably borrowed. - /// - /// The borrow lasts while the returned [`PyRef`] exists. - /// - /// This is the non-panicking variant of [`borrow`](#method.borrow). - /// - /// For frozen classes, the simpler [`get`][Self::get] is available. - #[inline] - pub fn try_borrow(&self) -> Result, PyBorrowError> { - PyRef::try_borrow(self) - } - - /// Attempts to mutably borrow the value `T`, returning an error if the value is currently borrowed. - /// - /// The borrow lasts while the returned [`PyRefMut`] exists. - /// - /// This is the non-panicking variant of [`borrow_mut`](#method.borrow_mut). - #[inline] - pub fn try_borrow_mut(&self) -> Result, PyBorrowMutError> - where - T: PyClass, - { - PyRefMut::try_borrow(self) - } - - /// Provide an immutable borrow of the value `T` without acquiring the GIL. - /// - /// This is available if the class is [`frozen`][macro@crate::pyclass] and [`Sync`]. - /// - /// # Examples - /// - /// ``` - /// use std::sync::atomic::{AtomicUsize, Ordering}; - /// # use pyo3::prelude::*; - /// - /// #[pyclass(frozen)] - /// struct FrozenCounter { - /// value: AtomicUsize, - /// } - /// - /// Python::with_gil(|py| { - /// let counter = FrozenCounter { value: AtomicUsize::new(0) }; - /// - /// let py_counter = Bound::new(py, counter).unwrap(); - /// - /// py_counter.get().value.fetch_add(1, Ordering::Relaxed); - /// }); - /// ``` - #[inline] - pub fn get(&self) -> &T - where - T: PyClass + Sync, - { - self.1.get() - } - - /// Upcast this `Bound` to its base type by reference. - /// - /// If this type defined an explicit base class in its `pyclass` declaration - /// (e.g. `#[pyclass(extends = BaseType)]`), the returned type will be - /// `&Bound`. If an explicit base class was _not_ declared, the - /// return value will be `&Bound` (making this method equivalent - /// to [`as_any`]). - /// - /// This method is particularly useful for calling methods defined in an - /// extension trait that has been implemented for `Bound`. - /// - /// See also the [`into_super`] method to upcast by value, and the - /// [`PyRef::as_super`]/[`PyRefMut::as_super`] methods for upcasting a pyclass - /// that has already been [`borrow`]ed. - /// - /// # Example: Calling a method defined on the `Bound` base type - /// - /// ```rust - /// # fn main() { - /// use pyo3::prelude::*; - /// - /// #[pyclass(subclass)] - /// struct BaseClass; - /// - /// trait MyClassMethods<'py> { - /// fn pyrepr(&self) -> PyResult; - /// } - /// impl<'py> MyClassMethods<'py> for Bound<'py, BaseClass> { - /// fn pyrepr(&self) -> PyResult { - /// self.call_method0("__repr__")?.extract() - /// } - /// } - /// - /// #[pyclass(extends = BaseClass)] - /// struct SubClass; - /// - /// Python::with_gil(|py| { - /// let obj = Bound::new(py, (SubClass, BaseClass)).unwrap(); - /// assert!(obj.as_super().pyrepr().is_ok()); - /// }) - /// # } - /// ``` - /// - /// [`as_any`]: Bound::as_any - /// [`into_super`]: Bound::into_super - /// [`borrow`]: Bound::borrow - #[inline] - pub fn as_super(&self) -> &Bound<'py, T::BaseType> { - // a pyclass can always be safely "downcast" to its base type - unsafe { self.as_any().downcast_unchecked() } - } - - /// Upcast this `Bound` to its base type by value. - /// - /// If this type defined an explicit base class in its `pyclass` declaration - /// (e.g. `#[pyclass(extends = BaseType)]`), the returned type will be - /// `Bound`. If an explicit base class was _not_ declared, the - /// return value will be `Bound` (making this method equivalent - /// to [`into_any`]). - /// - /// This method is particularly useful for calling methods defined in an - /// extension trait that has been implemented for `Bound`. - /// - /// See also the [`as_super`] method to upcast by reference, and the - /// [`PyRef::into_super`]/[`PyRefMut::into_super`] methods for upcasting a pyclass - /// that has already been [`borrow`]ed. - /// - /// # Example: Calling a method defined on the `Bound` base type - /// - /// ```rust - /// # fn main() { - /// use pyo3::prelude::*; - /// - /// #[pyclass(subclass)] - /// struct BaseClass; - /// - /// trait MyClassMethods<'py> { - /// fn pyrepr(self) -> PyResult; - /// } - /// impl<'py> MyClassMethods<'py> for Bound<'py, BaseClass> { - /// fn pyrepr(self) -> PyResult { - /// self.call_method0("__repr__")?.extract() - /// } - /// } - /// - /// #[pyclass(extends = BaseClass)] - /// struct SubClass; - /// - /// Python::with_gil(|py| { - /// let obj = Bound::new(py, (SubClass, BaseClass)).unwrap(); - /// assert!(obj.into_super().pyrepr().is_ok()); - /// }) - /// # } - /// ``` - /// - /// [`into_any`]: Bound::into_any - /// [`as_super`]: Bound::as_super - /// [`borrow`]: Bound::borrow - #[inline] - pub fn into_super(self) -> Bound<'py, T::BaseType> { - // a pyclass can always be safely "downcast" to its base type - unsafe { self.into_any().downcast_into_unchecked() } - } - - #[inline] - pub(crate) fn get_class_object(&self) -> &PyClassObject { - self.1.get_class_object() - } -} - -impl std::fmt::Debug for Bound<'_, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - let any = self.as_any(); - python_format(any, any.repr(), f) - } -} - -impl std::fmt::Display for Bound<'_, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - let any = self.as_any(); - python_format(any, any.str(), f) - } -} - -fn python_format( - any: &Bound<'_, PyAny>, - format_result: PyResult>, - f: &mut std::fmt::Formatter<'_>, -) -> Result<(), std::fmt::Error> { - match format_result { - Result::Ok(s) => return f.write_str(&s.to_string_lossy()), - Result::Err(err) => err.write_unraisable(any.py(), Some(any)), - } - - match any.get_type().name() { - Result::Ok(name) => std::write!(f, "", name), - Result::Err(_err) => f.write_str(""), - } -} - -// The trait bound is needed to avoid running into the auto-deref recursion -// limit (error[E0055]), because `Bound` would deref into itself. See: -// https://github.com/rust-lang/rust/issues/19509 -impl<'py, T> Deref for Bound<'py, T> -where - T: DerefToPyAny, -{ - type Target = Bound<'py, PyAny>; - - #[inline] - fn deref(&self) -> &Bound<'py, PyAny> { - self.as_any() - } -} - -impl<'py, T> AsRef> for Bound<'py, T> { - #[inline] - fn as_ref(&self) -> &Bound<'py, PyAny> { - self.as_any() - } -} - -impl Clone for Bound<'_, T> { - #[inline] - fn clone(&self) -> Self { - Self(self.0, ManuallyDrop::new(self.1.clone_ref(self.0))) - } -} - -impl Drop for Bound<'_, T> { - #[inline] - fn drop(&mut self) { - unsafe { ffi::Py_DECREF(self.as_ptr()) } - } -} - -impl<'py, T> Bound<'py, T> { - /// Returns the GIL token associated with this object. - #[inline] - pub fn py(&self) -> Python<'py> { - self.0 - } - - /// Returns the raw FFI pointer represented by self. - /// - /// # Safety - /// - /// Callers are responsible for ensuring that the pointer does not outlive self. - /// - /// The reference is borrowed; callers should not decrease the reference count - /// when they are finished with the pointer. - #[inline] - pub fn as_ptr(&self) -> *mut ffi::PyObject { - self.1.as_ptr() - } - - /// Returns an owned raw FFI pointer represented by self. - /// - /// # Safety - /// - /// The reference is owned; when finished the caller should either transfer ownership - /// of the pointer or decrease the reference count (e.g. with [`pyo3::ffi::Py_DecRef`](crate::ffi::Py_DecRef)). - #[inline] - pub fn into_ptr(self) -> *mut ffi::PyObject { - ManuallyDrop::new(self).as_ptr() - } - - /// Helper to cast to `Bound<'py, PyAny>`. - #[inline] - pub fn as_any(&self) -> &Bound<'py, PyAny> { - // Safety: all Bound have the same memory layout, and all Bound are valid - // Bound, so pointer casting is valid. - unsafe { &*ptr_from_ref(self).cast::>() } - } - - /// Helper to cast to `Bound<'py, PyAny>`, transferring ownership. - #[inline] - pub fn into_any(self) -> Bound<'py, PyAny> { - // Safety: all Bound are valid Bound - Bound(self.0, ManuallyDrop::new(self.unbind().into_any())) - } - - /// Casts this `Bound` to a `Borrowed` smart pointer. - #[inline] - pub fn as_borrowed<'a>(&'a self) -> Borrowed<'a, 'py, T> { - Borrowed( - unsafe { NonNull::new_unchecked(self.as_ptr()) }, - PhantomData, - self.py(), - ) - } - - /// Removes the connection for this `Bound` from the GIL, allowing - /// it to cross thread boundaries. - #[inline] - pub fn unbind(self) -> Py { - // Safety: the type T is known to be correct and the ownership of the - // pointer is transferred to the new Py instance. - let non_null = (ManuallyDrop::new(self).1).0; - unsafe { Py::from_non_null(non_null) } - } - - /// Removes the connection for this `Bound` from the GIL, allowing - /// it to cross thread boundaries, without transferring ownership. - #[inline] - pub fn as_unbound(&self) -> &Py { - &self.1 - } -} - -unsafe impl AsPyPointer for Bound<'_, T> { - #[inline] - fn as_ptr(&self) -> *mut ffi::PyObject { - self.1.as_ptr() - } -} - -impl<'py, T> BoundObject<'py, T> for Bound<'py, T> { - type Any = Bound<'py, PyAny>; - - fn as_borrowed(&self) -> Borrowed<'_, 'py, T> { - Bound::as_borrowed(self) - } - - fn into_bound(self) -> Bound<'py, T> { - self - } - - fn into_any(self) -> Self::Any { - self.into_any() - } - - fn into_ptr(self) -> *mut ffi::PyObject { - self.into_ptr() - } - - fn as_ptr(&self) -> *mut ffi::PyObject { - self.as_ptr() - } - - fn unbind(self) -> Py { - self.unbind() - } -} - -/// A borrowed equivalent to `Bound`. -/// -/// The advantage of this over `&Bound` is that it avoids the need to have a pointer-to-pointer, as Bound -/// is already a pointer to an `ffi::PyObject``. -/// -/// Similarly, this type is `Copy` and `Clone`, like a shared reference (`&T`). -#[repr(transparent)] -pub struct Borrowed<'a, 'py, T>(NonNull, PhantomData<&'a Py>, Python<'py>); - -impl<'a, 'py, T> Borrowed<'a, 'py, T> { - /// Creates a new owned [`Bound`] from this borrowed reference by - /// increasing the reference count. - /// - /// # Example - /// ``` - /// use pyo3::{prelude::*, types::PyTuple}; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let tuple = PyTuple::new(py, [1, 2, 3])?; - /// - /// // borrows from `tuple`, so can only be - /// // used while `tuple` stays alive - /// let borrowed = tuple.get_borrowed_item(0)?; - /// - /// // creates a new owned reference, which - /// // can be used indendently of `tuple` - /// let bound = borrowed.to_owned(); - /// drop(tuple); - /// - /// assert_eq!(bound.extract::().unwrap(), 1); - /// Ok(()) - /// }) - /// # } - pub fn to_owned(self) -> Bound<'py, T> { - (*self).clone() - } - - /// Returns the raw FFI pointer represented by self. - /// - /// # Safety - /// - /// Callers are responsible for ensuring that the pointer does not outlive self. - /// - /// The reference is borrowed; callers should not decrease the reference count - /// when they are finished with the pointer. - #[inline] - pub fn as_ptr(self) -> *mut ffi::PyObject { - self.0.as_ptr() - } - - pub(crate) fn to_any(self) -> Borrowed<'a, 'py, PyAny> { - Borrowed(self.0, PhantomData, self.2) - } -} - -impl<'a, 'py> Borrowed<'a, 'py, PyAny> { - /// Constructs a new `Borrowed<'a, 'py, PyAny>` from a pointer. Panics if `ptr` is null. - /// - /// Prefer to use [`Bound::from_borrowed_ptr`], as that avoids the major safety risk - /// of needing to precisely define the lifetime `'a` for which the borrow is valid. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object - /// - similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by - /// the caller and it is the caller's responsibility to ensure that the reference this is - /// derived from is valid for the lifetime `'a`. - #[inline] - #[track_caller] - pub unsafe fn from_ptr(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self { - Self( - NonNull::new(ptr).unwrap_or_else(|| crate::err::panic_after_error(py)), - PhantomData, - py, - ) - } - - /// Constructs a new `Borrowed<'a, 'py, PyAny>` from a pointer. Returns `None` if `ptr` is null. - /// - /// Prefer to use [`Bound::from_borrowed_ptr_or_opt`], as that avoids the major safety risk - /// of needing to precisely define the lifetime `'a` for which the borrow is valid. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - /// - similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by - /// the caller and it is the caller's responsibility to ensure that the reference this is - /// derived from is valid for the lifetime `'a`. - #[inline] - pub unsafe fn from_ptr_or_opt(py: Python<'py>, ptr: *mut ffi::PyObject) -> Option { - NonNull::new(ptr).map(|ptr| Self(ptr, PhantomData, py)) - } - - /// Constructs a new `Borrowed<'a, 'py, PyAny>` from a pointer. Returns an `Err` by calling `PyErr::fetch` - /// if `ptr` is null. - /// - /// Prefer to use [`Bound::from_borrowed_ptr_or_err`], as that avoids the major safety risk - /// of needing to precisely define the lifetime `'a` for which the borrow is valid. - /// - /// # Safety - /// - /// - `ptr` must be a valid pointer to a Python object, or null - /// - similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by - /// the caller and it is the caller's responsibility to ensure that the reference this is - /// derived from is valid for the lifetime `'a`. - #[inline] - pub unsafe fn from_ptr_or_err(py: Python<'py>, ptr: *mut ffi::PyObject) -> PyResult { - NonNull::new(ptr).map_or_else( - || Err(PyErr::fetch(py)), - |ptr| Ok(Self(ptr, PhantomData, py)), - ) - } - - /// # Safety - /// This is similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by - /// the caller and it's the caller's responsibility to ensure that the reference this is - /// derived from is valid for the lifetime `'a`. - #[inline] - pub(crate) unsafe fn from_ptr_unchecked(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self { - Self(NonNull::new_unchecked(ptr), PhantomData, py) - } - - #[inline] - pub(crate) fn downcast(self) -> Result, DowncastError<'a, 'py>> - where - T: PyTypeCheck, - { - if T::type_check(&self) { - // Safety: type_check is responsible for ensuring that the type is correct - Ok(unsafe { self.downcast_unchecked() }) - } else { - Err(DowncastError::new_from_borrowed(self, T::NAME)) - } - } - - /// Converts this `PyAny` to a concrete Python type without checking validity. - /// - /// # Safety - /// Callers must ensure that the type is valid or risk type confusion. - #[inline] - pub(crate) unsafe fn downcast_unchecked(self) -> Borrowed<'a, 'py, T> { - Borrowed(self.0, PhantomData, self.2) - } -} - -impl<'a, 'py, T> From<&'a Bound<'py, T>> for Borrowed<'a, 'py, T> { - /// Create borrow on a Bound - #[inline] - fn from(instance: &'a Bound<'py, T>) -> Self { - instance.as_borrowed() - } -} - -impl std::fmt::Debug for Borrowed<'_, '_, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Bound::fmt(self, f) - } -} - -impl<'py, T> Deref for Borrowed<'_, 'py, T> { - type Target = Bound<'py, T>; - - #[inline] - fn deref(&self) -> &Bound<'py, T> { - // safety: Bound has the same layout as NonNull - unsafe { &*ptr_from_ref(&self.0).cast() } - } -} - -impl Clone for Borrowed<'_, '_, T> { - #[inline] - fn clone(&self) -> Self { - *self - } -} - -impl Copy for Borrowed<'_, '_, T> {} - -#[allow(deprecated)] -impl ToPyObject for Borrowed<'_, '_, T> { - /// Converts `Py` instance -> PyObject. - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - (*self).into_py(py) - } -} - -#[allow(deprecated)] -impl IntoPy for Borrowed<'_, '_, T> { - /// Converts `Py` instance -> PyObject. - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.to_owned().into_py(py) - } -} - -impl<'a, 'py, T> BoundObject<'py, T> for Borrowed<'a, 'py, T> { - type Any = Borrowed<'a, 'py, PyAny>; - - fn as_borrowed(&self) -> Borrowed<'a, 'py, T> { - *self - } - - fn into_bound(self) -> Bound<'py, T> { - (*self).to_owned() - } - - fn into_any(self) -> Self::Any { - self.to_any() - } - - fn into_ptr(self) -> *mut ffi::PyObject { - (*self).to_owned().into_ptr() - } - - fn as_ptr(&self) -> *mut ffi::PyObject { - (*self).as_ptr() - } - - fn unbind(self) -> Py { - (*self).to_owned().unbind() - } -} - -/// A GIL-independent reference to an object allocated on the Python heap. -/// -/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it. -/// Instead, call one of its methods to access the inner object: -/// - [`Py::bind`] or [`Py::into_bound`], to borrow a GIL-bound reference to the contained object. -/// - [`Py::borrow`], [`Py::try_borrow`], [`Py::borrow_mut`], or [`Py::try_borrow_mut`], -/// -/// to get a (mutable) reference to a contained pyclass, using a scheme similar to std's [`RefCell`]. -/// See the -#[doc = concat!("[guide entry](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#bound-and-interior-mutability)")] -/// for more information. -/// - You can call methods directly on `Py` with [`Py::call_bound`], [`Py::call_method_bound`] and friends. -/// -/// These require passing in the [`Python<'py>`](crate::Python) token but are otherwise similar to the corresponding -/// methods on [`PyAny`]. -/// -/// # Example: Storing Python objects in `#[pyclass]` structs -/// -/// Usually `Bound<'py, T>` is recommended for interacting with Python objects as its lifetime `'py` -/// is an association to the GIL and that enables many operations to be done as efficiently as possible. -/// -/// However, `#[pyclass]` structs cannot carry a lifetime, so `Py` is the only way to store -/// a Python object in a `#[pyclass]` struct. -/// -/// For example, this won't compile: -/// -/// ```compile_fail -/// # use pyo3::prelude::*; -/// # use pyo3::types::PyDict; -/// # -/// #[pyclass] -/// struct Foo<'py> { -/// inner: Bound<'py, PyDict>, -/// } -/// -/// impl Foo { -/// fn new() -> Foo { -/// let foo = Python::with_gil(|py| { -/// // `py` will only last for this scope. -/// -/// // `Bound<'py, PyDict>` inherits the GIL lifetime from `py` and -/// // so won't be able to outlive this closure. -/// let dict: Bound<'_, PyDict> = PyDict::new(py); -/// -/// // because `Foo` contains `dict` its lifetime -/// // is now also tied to `py`. -/// Foo { inner: dict } -/// }); -/// // Foo is no longer valid. -/// // Returning it from this function is a 💥 compiler error 💥 -/// foo -/// } -/// } -/// ``` -/// -/// [`Py`]`` can be used to get around this by converting `dict` into a GIL-independent reference: -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::types::PyDict; -/// -/// #[pyclass] -/// struct Foo { -/// inner: Py, -/// } -/// -/// #[pymethods] -/// impl Foo { -/// #[new] -/// fn __new__() -> Foo { -/// Python::with_gil(|py| { -/// let dict: Py = PyDict::new(py).unbind(); -/// Foo { inner: dict } -/// }) -/// } -/// } -/// # -/// # fn main() -> PyResult<()> { -/// # Python::with_gil(|py| { -/// # let m = pyo3::types::PyModule::new(py, "test")?; -/// # m.add_class::()?; -/// # -/// # let foo: Bound<'_, Foo> = m.getattr("Foo")?.call0()?.downcast_into()?; -/// # let dict = &foo.borrow().inner; -/// # let dict: &Bound<'_, PyDict> = dict.bind(py); -/// # -/// # Ok(()) -/// # }) -/// # } -/// ``` -/// -/// This can also be done with other pyclasses: -/// ```rust -/// use pyo3::prelude::*; -/// -/// #[pyclass] -/// struct Bar {/* ... */} -/// -/// #[pyclass] -/// struct Foo { -/// inner: Py, -/// } -/// -/// #[pymethods] -/// impl Foo { -/// #[new] -/// fn __new__() -> PyResult { -/// Python::with_gil(|py| { -/// let bar: Py = Py::new(py, Bar {})?; -/// Ok(Foo { inner: bar }) -/// }) -/// } -/// } -/// # -/// # fn main() -> PyResult<()> { -/// # Python::with_gil(|py| { -/// # let m = pyo3::types::PyModule::new(py, "test")?; -/// # m.add_class::()?; -/// # -/// # let foo: Bound<'_, Foo> = m.getattr("Foo")?.call0()?.downcast_into()?; -/// # let bar = &foo.borrow().inner; -/// # let bar: &Bar = &*bar.borrow(py); -/// # -/// # Ok(()) -/// # }) -/// # } -/// ``` -/// -/// # Example: Shared ownership of Python objects -/// -/// `Py` can be used to share ownership of a Python object, similar to std's [`Rc`]``. -/// As with [`Rc`]``, cloning it increases its reference count rather than duplicating -/// the underlying object. -/// -/// This can be done using either [`Py::clone_ref`] or [`Py`]``'s [`Clone`] trait implementation. -/// [`Py::clone_ref`] will be faster if you happen to be already holding the GIL. -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::types::PyDict; -/// -/// # fn main() { -/// Python::with_gil(|py| { -/// let first: Py = PyDict::new(py).unbind(); -/// -/// // All of these are valid syntax -/// let second = Py::clone_ref(&first, py); -/// let third = first.clone_ref(py); -/// #[cfg(feature = "py-clone")] -/// let fourth = Py::clone(&first); -/// #[cfg(feature = "py-clone")] -/// let fifth = first.clone(); -/// -/// // Disposing of our original `Py` just decrements the reference count. -/// drop(first); -/// -/// // They all point to the same object -/// assert!(second.is(&third)); -/// #[cfg(feature = "py-clone")] -/// assert!(fourth.is(&fifth)); -/// #[cfg(feature = "py-clone")] -/// assert!(second.is(&fourth)); -/// }); -/// # } -/// ``` -/// -/// # Preventing reference cycles -/// -/// It is easy to accidentally create reference cycles using [`Py`]``. -/// The Python interpreter can break these reference cycles within pyclasses if they -/// [integrate with the garbage collector][gc]. If your pyclass contains other Python -/// objects you should implement it to avoid leaking memory. -/// -/// # A note on Python reference counts -/// -/// Dropping a [`Py`]`` will eventually decrease Python's reference count -/// of the pointed-to variable, allowing Python's garbage collector to free -/// the associated memory, but this may not happen immediately. This is -/// because a [`Py`]`` can be dropped at any time, but the Python reference -/// count can only be modified when the GIL is held. -/// -/// If a [`Py`]`` is dropped while its thread happens to be holding the -/// GIL then the Python reference count will be decreased immediately. -/// Otherwise, the reference count will be decreased the next time the GIL is -/// reacquired. -/// -/// If you happen to be already holding the GIL, [`Py::drop_ref`] will decrease -/// the Python reference count immediately and will execute slightly faster than -/// relying on implicit [`Drop`]s. -/// -/// # A note on `Send` and `Sync` -/// -/// Accessing this object is thread-safe, since any access to its API requires a [`Python<'py>`](crate::Python) token. -/// As you can only get this by acquiring the GIL, `Py<...>` implements [`Send`] and [`Sync`]. -/// -/// [`Rc`]: std::rc::Rc -/// [`RefCell`]: std::cell::RefCell -/// [gc]: https://pyo3.rs/main/class/protocols.html#garbage-collector-integration -#[repr(transparent)] -pub struct Py(NonNull, PhantomData); - -// The inner value is only accessed through ways that require proving the gil is held -#[cfg(feature = "nightly")] -unsafe impl crate::marker::Ungil for Py {} -unsafe impl Send for Py {} -unsafe impl Sync for Py {} - -impl Py -where - T: PyClass, -{ - /// Creates a new instance `Py` of a `#[pyclass]` on the Python heap. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pyclass] - /// struct Foo {/* fields omitted */} - /// - /// # fn main() -> PyResult<()> { - /// let foo = Python::with_gil(|py| -> PyResult<_> { - /// let foo: Py = Py::new(py, Foo {})?; - /// Ok(foo) - /// })?; - /// # Python::with_gil(move |_py| drop(foo)); - /// # Ok(()) - /// # } - /// ``` - pub fn new(py: Python<'_>, value: impl Into>) -> PyResult> { - Bound::new(py, value).map(Bound::unbind) - } -} - -impl Py { - /// Returns the raw FFI pointer represented by self. - /// - /// # Safety - /// - /// Callers are responsible for ensuring that the pointer does not outlive self. - /// - /// The reference is borrowed; callers should not decrease the reference count - /// when they are finished with the pointer. - #[inline] - pub fn as_ptr(&self) -> *mut ffi::PyObject { - self.0.as_ptr() - } - - /// Returns an owned raw FFI pointer represented by self. - /// - /// # Safety - /// - /// The reference is owned; when finished the caller should either transfer ownership - /// of the pointer or decrease the reference count (e.g. with [`pyo3::ffi::Py_DecRef`](crate::ffi::Py_DecRef)). - #[inline] - pub fn into_ptr(self) -> *mut ffi::PyObject { - ManuallyDrop::new(self).0.as_ptr() - } - - /// Helper to cast to `Py`. - #[inline] - pub fn as_any(&self) -> &Py { - // Safety: all Py have the same memory layout, and all Py are valid - // Py, so pointer casting is valid. - unsafe { &*ptr_from_ref(self).cast::>() } - } - - /// Helper to cast to `Py`, transferring ownership. - #[inline] - pub fn into_any(self) -> Py { - // Safety: all Py are valid Py - unsafe { Py::from_non_null(ManuallyDrop::new(self).0) } - } -} - -impl Py -where - T: PyClass, -{ - /// Immutably borrows the value `T`. - /// - /// This borrow lasts while the returned [`PyRef`] exists. - /// Multiple immutable borrows can be taken out at the same time. - /// - /// For frozen classes, the simpler [`get`][Self::get] is available. - /// - /// Equivalent to `self.bind(py).borrow()` - see [`Bound::borrow`]. - /// - /// # Examples - /// - /// ```rust - /// # use pyo3::prelude::*; - /// # - /// #[pyclass] - /// struct Foo { - /// inner: u8, - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let foo: Py = Py::new(py, Foo { inner: 73 })?; - /// let inner: &u8 = &foo.borrow(py).inner; - /// - /// assert_eq!(*inner, 73); - /// Ok(()) - /// })?; - /// # Ok(()) - /// # } - /// ``` - /// - /// # Panics - /// - /// Panics if the value is currently mutably borrowed. For a non-panicking variant, use - /// [`try_borrow`](#method.try_borrow). - #[inline] - #[track_caller] - pub fn borrow<'py>(&'py self, py: Python<'py>) -> PyRef<'py, T> { - self.bind(py).borrow() - } - - /// Mutably borrows the value `T`. - /// - /// This borrow lasts while the returned [`PyRefMut`] exists. - /// - /// Equivalent to `self.bind(py).borrow_mut()` - see [`Bound::borrow_mut`]. - /// - /// # Examples - /// - /// ``` - /// # use pyo3::prelude::*; - /// # - /// #[pyclass] - /// struct Foo { - /// inner: u8, - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let foo: Py = Py::new(py, Foo { inner: 73 })?; - /// foo.borrow_mut(py).inner = 35; - /// - /// assert_eq!(foo.borrow(py).inner, 35); - /// Ok(()) - /// })?; - /// # Ok(()) - /// # } - /// ``` - /// - /// # Panics - /// Panics if the value is currently borrowed. For a non-panicking variant, use - /// [`try_borrow_mut`](#method.try_borrow_mut). - #[inline] - #[track_caller] - pub fn borrow_mut<'py>(&'py self, py: Python<'py>) -> PyRefMut<'py, T> - where - T: PyClass, - { - self.bind(py).borrow_mut() - } - - /// Attempts to immutably borrow the value `T`, returning an error if the value is currently mutably borrowed. - /// - /// The borrow lasts while the returned [`PyRef`] exists. - /// - /// This is the non-panicking variant of [`borrow`](#method.borrow). - /// - /// For frozen classes, the simpler [`get`][Self::get] is available. - /// - /// Equivalent to `self.bind(py).try_borrow()` - see [`Bound::try_borrow`]. - #[inline] - pub fn try_borrow<'py>(&'py self, py: Python<'py>) -> Result, PyBorrowError> { - self.bind(py).try_borrow() - } - - /// Attempts to mutably borrow the value `T`, returning an error if the value is currently borrowed. - /// - /// The borrow lasts while the returned [`PyRefMut`] exists. - /// - /// This is the non-panicking variant of [`borrow_mut`](#method.borrow_mut). - /// - /// Equivalent to `self.bind(py).try_borrow_mut()` - see [`Bound::try_borrow_mut`]. - #[inline] - pub fn try_borrow_mut<'py>( - &'py self, - py: Python<'py>, - ) -> Result, PyBorrowMutError> - where - T: PyClass, - { - self.bind(py).try_borrow_mut() - } - - /// Provide an immutable borrow of the value `T` without acquiring the GIL. - /// - /// This is available if the class is [`frozen`][macro@crate::pyclass] and [`Sync`]. - /// - /// # Examples - /// - /// ``` - /// use std::sync::atomic::{AtomicUsize, Ordering}; - /// # use pyo3::prelude::*; - /// - /// #[pyclass(frozen)] - /// struct FrozenCounter { - /// value: AtomicUsize, - /// } - /// - /// let cell = Python::with_gil(|py| { - /// let counter = FrozenCounter { value: AtomicUsize::new(0) }; - /// - /// Py::new(py, counter).unwrap() - /// }); - /// - /// cell.get().value.fetch_add(1, Ordering::Relaxed); - /// # Python::with_gil(move |_py| drop(cell)); - /// ``` - #[inline] - pub fn get(&self) -> &T - where - T: PyClass + Sync, - { - // Safety: The class itself is frozen and `Sync` - unsafe { &*self.get_class_object().get_ptr() } - } - - /// Get a view on the underlying `PyClass` contents. - #[inline] - pub(crate) fn get_class_object(&self) -> &PyClassObject { - let class_object = self.as_ptr().cast::>(); - // Safety: Bound is known to contain an object which is laid out in memory as a - // PyClassObject. - unsafe { &*class_object } - } -} - -impl Py { - /// Attaches this `Py` to the given Python context, allowing access to further Python APIs. - #[inline] - pub fn bind<'py>(&self, _py: Python<'py>) -> &Bound<'py, T> { - // Safety: `Bound` has the same layout as `Py` - unsafe { &*ptr_from_ref(self).cast() } - } - - /// Same as `bind` but takes ownership of `self`. - #[inline] - pub fn into_bound(self, py: Python<'_>) -> Bound<'_, T> { - Bound(py, ManuallyDrop::new(self)) - } - - /// Same as `bind` but produces a `Borrowed` instead of a `Bound`. - #[inline] - pub fn bind_borrowed<'a, 'py>(&'a self, py: Python<'py>) -> Borrowed<'a, 'py, T> { - Borrowed(self.0, PhantomData, py) - } - - /// Returns whether `self` and `other` point to the same object. To compare - /// the equality of two objects (the `==` operator), use [`eq`](PyAnyMethods::eq). - /// - /// This is equivalent to the Python expression `self is other`. - #[inline] - pub fn is(&self, o: &U) -> bool { - self.as_ptr() == o.as_ptr() - } - - /// Gets the reference count of the `ffi::PyObject` pointer. - #[inline] - pub fn get_refcnt(&self, _py: Python<'_>) -> isize { - unsafe { ffi::Py_REFCNT(self.0.as_ptr()) } - } - - /// Makes a clone of `self`. - /// - /// This creates another pointer to the same object, increasing its reference count. - /// - /// You should prefer using this method over [`Clone`] if you happen to be holding the GIL already. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyDict; - /// - /// # fn main() { - /// Python::with_gil(|py| { - /// let first: Py = PyDict::new(py).unbind(); - /// let second = Py::clone_ref(&first, py); - /// - /// // Both point to the same object - /// assert!(first.is(&second)); - /// }); - /// # } - /// ``` - #[inline] - pub fn clone_ref(&self, _py: Python<'_>) -> Py { - unsafe { - ffi::Py_INCREF(self.as_ptr()); - Self::from_non_null(self.0) - } - } - - /// Drops `self` and immediately decreases its reference count. - /// - /// This method is a micro-optimisation over [`Drop`] if you happen to be holding the GIL - /// already. - /// - /// Note that if you are using [`Bound`], you do not need to use [`Self::drop_ref`] since - /// [`Bound`] guarantees that the GIL is held. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyDict; - /// - /// # fn main() { - /// Python::with_gil(|py| { - /// let object: Py = PyDict::new(py).unbind(); - /// - /// // some usage of object - /// - /// object.drop_ref(py); - /// }); - /// # } - /// ``` - #[inline] - pub fn drop_ref(self, py: Python<'_>) { - let _ = self.into_bound(py); - } - - /// Returns whether the object is considered to be None. - /// - /// This is equivalent to the Python expression `self is None`. - pub fn is_none(&self, _py: Python<'_>) -> bool { - unsafe { ffi::Py_None() == self.as_ptr() } - } - - /// Returns whether the object is considered to be true. - /// - /// This applies truth value testing equivalent to the Python expression `bool(self)`. - pub fn is_truthy(&self, py: Python<'_>) -> PyResult { - let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; - err::error_on_minusone(py, v)?; - Ok(v != 0) - } - - /// Extracts some type from the Python object. - /// - /// This is a wrapper function around `FromPyObject::extract()`. - pub fn extract<'a, 'py, D>(&'a self, py: Python<'py>) -> PyResult - where - D: crate::conversion::FromPyObjectBound<'a, 'py>, - // TODO it might be possible to relax this bound in future, to allow - // e.g. `.extract::<&str>(py)` where `py` is short-lived. - 'py: 'a, - { - self.bind(py).as_any().extract() - } - - /// Retrieves an attribute value. - /// - /// This is equivalent to the Python expression `self.attr_name`. - /// - /// If calling this method becomes performance-critical, the [`intern!`](crate::intern) macro - /// can be used to intern `attr_name`, thereby avoiding repeated temporary allocations of - /// Python strings. - /// - /// # Example: `intern!`ing the attribute name - /// - /// ``` - /// # use pyo3::{prelude::*, intern}; - /// # - /// #[pyfunction] - /// fn version(sys: Py, py: Python<'_>) -> PyResult { - /// sys.getattr(py, intern!(py, "version")) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let sys = py.import("sys").unwrap().unbind(); - /// # version(sys, py).unwrap(); - /// # }); - /// ``` - pub fn getattr<'py, N>(&self, py: Python<'py>, attr_name: N) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>, - { - self.bind(py).as_any().getattr(attr_name).map(Bound::unbind) - } - - /// Sets an attribute value. - /// - /// This is equivalent to the Python expression `self.attr_name = value`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`](crate::intern) - /// macro can be used to intern `attr_name`. - /// - /// # Example: `intern!`ing the attribute name - /// - /// ``` - /// # use pyo3::{intern, pyfunction, types::PyModule, IntoPyObjectExt, PyObject, Python, PyResult}; - /// # - /// #[pyfunction] - /// fn set_answer(ob: PyObject, py: Python<'_>) -> PyResult<()> { - /// ob.setattr(py, intern!(py, "answer"), 42) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let ob = PyModule::new(py, "empty").unwrap().into_py_any(py).unwrap(); - /// # set_answer(ob, py).unwrap(); - /// # }); - /// ``` - pub fn setattr<'py, N, V>(&self, py: Python<'py>, attr_name: N, value: V) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - V: IntoPyObject<'py>, - { - self.bind(py).as_any().setattr(attr_name, value) - } - - /// Calls the object. - /// - /// This is equivalent to the Python expression `self(*args, **kwargs)`. - pub fn call<'py, A>( - &self, - py: Python<'py>, - args: A, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult - where - A: IntoPyObject<'py, Target = PyTuple>, - { - self.bind(py) - .as_any() - .call( - // FIXME(icxolu): remove explicit args conversion - args.into_pyobject(py).map_err(Into::into)?.into_bound(), - kwargs, - ) - .map(Bound::unbind) - } - - /// Deprecated name for [`Py::call`]. - #[deprecated(since = "0.23.0", note = "renamed to `Py::call`")] - #[allow(deprecated)] - #[inline] - pub fn call_bound( - &self, - py: Python<'_>, - args: impl IntoPy>, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult { - self.call(py, args.into_py(py), kwargs) - } - - /// Calls the object with only positional arguments. - /// - /// This is equivalent to the Python expression `self(*args)`. - pub fn call1<'py, N>(&self, py: Python<'py>, args: N) -> PyResult - where - N: IntoPyObject<'py, Target = PyTuple>, - { - self.bind(py) - .as_any() - // FIXME(icxolu): remove explicit args conversion - .call1(args.into_pyobject(py).map_err(Into::into)?.into_bound()) - .map(Bound::unbind) - } - - /// Calls the object without arguments. - /// - /// This is equivalent to the Python expression `self()`. - pub fn call0(&self, py: Python<'_>) -> PyResult { - self.bind(py).as_any().call0().map(Bound::unbind) - } - - /// Calls a method on the object. - /// - /// This is equivalent to the Python expression `self.name(*args, **kwargs)`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`](crate::intern) - /// macro can be used to intern `name`. - pub fn call_method<'py, N, A>( - &self, - py: Python<'py>, - name: N, - args: A, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>, - { - self.bind(py) - .as_any() - .call_method( - name, - // FIXME(icxolu): remove explicit args conversion - args.into_pyobject(py).map_err(Into::into)?.into_bound(), - kwargs, - ) - .map(Bound::unbind) - } - - /// Deprecated name for [`Py::call_method`]. - #[deprecated(since = "0.23.0", note = "renamed to `Py::call_method`")] - #[allow(deprecated)] - #[inline] - pub fn call_method_bound( - &self, - py: Python<'_>, - name: N, - args: A, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult - where - N: IntoPy>, - A: IntoPy>, - { - self.call_method(py, name.into_py(py), args.into_py(py), kwargs) - } - - /// Calls a method on the object with only positional arguments. - /// - /// This is equivalent to the Python expression `self.name(*args)`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`](crate::intern) - /// macro can be used to intern `name`. - pub fn call_method1<'py, N, A>(&self, py: Python<'py>, name: N, args: A) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>, - { - self.bind(py) - .as_any() - .call_method1( - name, - // FIXME(icxolu): remove explicit args conversion - args.into_pyobject(py).map_err(Into::into)?.into_bound(), - ) - .map(Bound::unbind) - } - - /// Calls a method on the object with no arguments. - /// - /// This is equivalent to the Python expression `self.name()`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`](crate::intern) - /// macro can be used to intern `name`. - pub fn call_method0<'py, N>(&self, py: Python<'py>, name: N) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>, - { - self.bind(py).as_any().call_method0(name).map(Bound::unbind) - } - - /// Create a `Py` instance by taking ownership of the given FFI pointer. - /// - /// # Safety - /// `ptr` must be a pointer to a Python object of type T. - /// - /// Callers must own the object referred to by `ptr`, as this function - /// implicitly takes ownership of that object. - /// - /// # Panics - /// Panics if `ptr` is null. - #[inline] - #[track_caller] - pub unsafe fn from_owned_ptr(py: Python<'_>, ptr: *mut ffi::PyObject) -> Py { - match NonNull::new(ptr) { - Some(nonnull_ptr) => Py(nonnull_ptr, PhantomData), - None => crate::err::panic_after_error(py), - } - } - - /// Create a `Py` instance by taking ownership of the given FFI pointer. - /// - /// If `ptr` is null then the current Python exception is fetched as a [`PyErr`]. - /// - /// # Safety - /// If non-null, `ptr` must be a pointer to a Python object of type T. - #[inline] - pub unsafe fn from_owned_ptr_or_err( - py: Python<'_>, - ptr: *mut ffi::PyObject, - ) -> PyResult> { - match NonNull::new(ptr) { - Some(nonnull_ptr) => Ok(Py(nonnull_ptr, PhantomData)), - None => Err(PyErr::fetch(py)), - } - } - - /// Create a `Py` instance by taking ownership of the given FFI pointer. - /// - /// If `ptr` is null then `None` is returned. - /// - /// # Safety - /// If non-null, `ptr` must be a pointer to a Python object of type T. - #[inline] - pub unsafe fn from_owned_ptr_or_opt(_py: Python<'_>, ptr: *mut ffi::PyObject) -> Option { - NonNull::new(ptr).map(|nonnull_ptr| Py(nonnull_ptr, PhantomData)) - } - - /// Constructs a new `Py` instance by taking ownership of the given FFI pointer. - /// - /// # Safety - /// - /// - `ptr` must be a non-null pointer to a Python object or type `T`. - pub(crate) unsafe fn from_owned_ptr_unchecked(ptr: *mut ffi::PyObject) -> Self { - Py(NonNull::new_unchecked(ptr), PhantomData) - } - - /// Create a `Py` instance by creating a new reference from the given FFI pointer. - /// - /// # Safety - /// `ptr` must be a pointer to a Python object of type T. - /// - /// # Panics - /// Panics if `ptr` is null. - #[inline] - #[track_caller] - pub unsafe fn from_borrowed_ptr(py: Python<'_>, ptr: *mut ffi::PyObject) -> Py { - match Self::from_borrowed_ptr_or_opt(py, ptr) { - Some(slf) => slf, - None => crate::err::panic_after_error(py), - } - } - - /// Create a `Py` instance by creating a new reference from the given FFI pointer. - /// - /// If `ptr` is null then the current Python exception is fetched as a `PyErr`. - /// - /// # Safety - /// `ptr` must be a pointer to a Python object of type T. - #[inline] - pub unsafe fn from_borrowed_ptr_or_err( - py: Python<'_>, - ptr: *mut ffi::PyObject, - ) -> PyResult { - Self::from_borrowed_ptr_or_opt(py, ptr).ok_or_else(|| PyErr::fetch(py)) - } - - /// Create a `Py` instance by creating a new reference from the given FFI pointer. - /// - /// If `ptr` is null then `None` is returned. - /// - /// # Safety - /// `ptr` must be a pointer to a Python object of type T. - #[inline] - pub unsafe fn from_borrowed_ptr_or_opt( - _py: Python<'_>, - ptr: *mut ffi::PyObject, - ) -> Option { - NonNull::new(ptr).map(|nonnull_ptr| { - ffi::Py_INCREF(ptr); - Py(nonnull_ptr, PhantomData) - }) - } - - /// For internal conversions. - /// - /// # Safety - /// `ptr` must point to a Python object of type T. - unsafe fn from_non_null(ptr: NonNull) -> Self { - Self(ptr, PhantomData) - } -} - -#[allow(deprecated)] -impl ToPyObject for Py { - /// Converts `Py` instance -> PyObject. - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.clone_ref(py).into_any() - } -} - -#[allow(deprecated)] -impl IntoPy for Py { - /// Converts a `Py` instance to `PyObject`. - /// Consumes `self` without calling `Py_DECREF()`. - #[inline] - fn into_py(self, _py: Python<'_>) -> PyObject { - self.into_any() - } -} - -#[allow(deprecated)] -impl IntoPy for &'_ Py { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl ToPyObject for Bound<'_, T> { - /// Converts `&Bound` instance -> PyObject, increasing the reference count. - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.clone().into_py(py) - } -} - -#[allow(deprecated)] -impl IntoPy for Bound<'_, T> { - /// Converts a `Bound` instance to `PyObject`. - #[inline] - fn into_py(self, _py: Python<'_>) -> PyObject { - self.into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for &Bound<'_, T> { - /// Converts `&Bound` instance -> PyObject, increasing the reference count. - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -unsafe impl crate::AsPyPointer for Py { - /// Gets the underlying FFI pointer, returns a borrowed pointer. - #[inline] - fn as_ptr(&self) -> *mut ffi::PyObject { - self.0.as_ptr() - } -} - -impl std::convert::From> for PyObject -where - T: AsRef, -{ - #[inline] - fn from(other: Py) -> Self { - other.into_any() - } -} - -impl std::convert::From> for PyObject -where - T: AsRef, -{ - #[inline] - fn from(other: Bound<'_, T>) -> Self { - other.into_any().unbind() - } -} - -impl std::convert::From> for Py { - #[inline] - fn from(other: Bound<'_, T>) -> Self { - other.unbind() - } -} - -impl<'a, T> std::convert::From> for Py -where - T: PyClass, -{ - fn from(pyref: PyRef<'a, T>) -> Self { - unsafe { Py::from_borrowed_ptr(pyref.py(), pyref.as_ptr()) } - } -} - -impl<'a, T> std::convert::From> for Py -where - T: PyClass, -{ - fn from(pyref: PyRefMut<'a, T>) -> Self { - unsafe { Py::from_borrowed_ptr(pyref.py(), pyref.as_ptr()) } - } -} - -/// If the GIL is held this increments `self`'s reference count. -/// Otherwise, it will panic. -/// -/// Only available if the `py-clone` feature is enabled. -#[cfg(feature = "py-clone")] -impl Clone for Py { - #[track_caller] - fn clone(&self) -> Self { - unsafe { - gil::register_incref(self.0); - } - Self(self.0, PhantomData) - } -} - -/// Dropping a `Py` instance decrements the reference count -/// on the object by one if the GIL is held. -/// -/// Otherwise and by default, this registers the underlying pointer to have its reference count -/// decremented the next time PyO3 acquires the GIL. -/// -/// However, if the `pyo3_disable_reference_pool` conditional compilation flag -/// is enabled, it will abort the process. -impl Drop for Py { - #[track_caller] - fn drop(&mut self) { - unsafe { - gil::register_decref(self.0); - } - } -} - -impl FromPyObject<'_> for Py -where - T: PyTypeCheck, -{ - /// Extracts `Self` from the source `PyObject`. - fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult { - ob.extract::>().map(Bound::unbind) - } -} - -impl<'py, T> FromPyObject<'py> for Bound<'py, T> -where - T: PyTypeCheck, -{ - /// Extracts `Self` from the source `PyObject`. - fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { - ob.downcast().cloned().map_err(Into::into) - } -} - -impl std::fmt::Display for Py -where - T: PyTypeInfo, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Python::with_gil(|py| std::fmt::Display::fmt(self.bind(py), f)) - } -} - -impl std::fmt::Debug for Py { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Py").field(&self.0.as_ptr()).finish() - } -} - -/// A commonly-used alias for `Py`. -/// -/// This is an owned reference a Python object without any type information. This value can also be -/// safely sent between threads. -/// -/// See the documentation for [`Py`](struct.Py.html). -pub type PyObject = Py; - -impl PyObject { - /// Downcast this `PyObject` to a concrete Python type or pyclass. - /// - /// Note that you can often avoid downcasting yourself by just specifying - /// the desired type in function or method signatures. - /// However, manual downcasting is sometimes necessary. - /// - /// For extracting a Rust-only type, see [`Py::extract`](struct.Py.html#method.extract). - /// - /// # Example: Downcasting to a specific Python object - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyDict, PyList}; - /// - /// Python::with_gil(|py| { - /// let any: PyObject = PyDict::new(py).into(); - /// - /// assert!(any.downcast_bound::(py).is_ok()); - /// assert!(any.downcast_bound::(py).is_err()); - /// }); - /// ``` - /// - /// # Example: Getting a reference to a pyclass - /// - /// This is useful if you want to mutate a `PyObject` that - /// might actually be a pyclass. - /// - /// ```rust - /// # fn main() -> Result<(), pyo3::PyErr> { - /// use pyo3::prelude::*; - /// - /// #[pyclass] - /// struct Class { - /// i: i32, - /// } - /// - /// Python::with_gil(|py| { - /// let class: PyObject = Py::new(py, Class { i: 0 })?.into_any(); - /// - /// let class_bound = class.downcast_bound::(py)?; - /// - /// class_bound.borrow_mut().i += 1; - /// - /// // Alternatively you can get a `PyRefMut` directly - /// let class_ref: PyRefMut<'_, Class> = class.extract(py)?; - /// assert_eq!(class_ref.i, 1); - /// Ok(()) - /// }) - /// # } - /// ``` - #[inline] - pub fn downcast_bound<'py, T>( - &self, - py: Python<'py>, - ) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> - where - T: PyTypeCheck, - { - self.bind(py).downcast() - } - - /// Casts the PyObject to a concrete Python object type without checking validity. - /// - /// # Safety - /// - /// Callers must ensure that the type is valid or risk type confusion. - #[inline] - pub unsafe fn downcast_bound_unchecked<'py, T>(&self, py: Python<'py>) -> &Bound<'py, T> { - self.bind(py).downcast_unchecked() - } -} - -#[cfg(test)] -mod tests { - use super::{Bound, IntoPyObject, Py, PyObject}; - use crate::tests::common::generate_unique_module_name; - use crate::types::{dict::IntoPyDict, PyAnyMethods, PyCapsule, PyDict, PyString}; - use crate::{ffi, Borrowed, PyAny, PyResult, Python}; - use pyo3_ffi::c_str; - use std::ffi::CStr; - - #[test] - fn test_call() { - Python::with_gil(|py| { - let obj = py.get_type::().into_pyobject(py).unwrap(); - - let assert_repr = |obj: Bound<'_, PyAny>, expected: &str| { - assert_eq!(obj.repr().unwrap(), expected); - }; - - assert_repr(obj.call0().unwrap(), "{}"); - assert_repr(obj.call1(()).unwrap(), "{}"); - assert_repr(obj.call((), None).unwrap(), "{}"); - - assert_repr(obj.call1(((('x', 1),),)).unwrap(), "{'x': 1}"); - assert_repr( - obj.call((), Some(&[('x', 1)].into_py_dict(py).unwrap())) - .unwrap(), - "{'x': 1}", - ); - }) - } - - #[test] - fn test_call_for_non_existing_method() { - Python::with_gil(|py| { - let obj: PyObject = PyDict::new(py).into(); - assert!(obj.call_method0(py, "asdf").is_err()); - assert!(obj - .call_method(py, "nonexistent_method", (1,), None) - .is_err()); - assert!(obj.call_method0(py, "nonexistent_method").is_err()); - assert!(obj.call_method1(py, "nonexistent_method", (1,)).is_err()); - }); - } - - #[test] - fn py_from_dict() { - let dict: Py = Python::with_gil(|py| { - let native = PyDict::new(py); - Py::from(native) - }); - - Python::with_gil(move |py| { - assert_eq!(dict.get_refcnt(py), 1); - }); - } - - #[test] - fn pyobject_from_py() { - Python::with_gil(|py| { - let dict: Py = PyDict::new(py).unbind(); - let cnt = dict.get_refcnt(py); - let p: PyObject = dict.into(); - assert_eq!(p.get_refcnt(py), cnt); - }); - } - - #[test] - fn attr() -> PyResult<()> { - use crate::types::PyModule; - - Python::with_gil(|py| { - const CODE: &CStr = c_str!( - r#" -class A: - pass -a = A() - "# - ); - let module = - PyModule::from_code(py, CODE, c_str!(""), &generate_unique_module_name(""))?; - let instance: Py = module.getattr("a")?.into(); - - instance.getattr(py, "foo").unwrap_err(); - - instance.setattr(py, "foo", "bar")?; - - assert!(instance - .getattr(py, "foo")? - .bind(py) - .eq(PyString::new(py, "bar"))?); - - instance.getattr(py, "foo")?; - Ok(()) - }) - } - - #[test] - fn pystring_attr() -> PyResult<()> { - use crate::types::PyModule; - - Python::with_gil(|py| { - const CODE: &CStr = c_str!( - r#" -class A: - pass -a = A() - "# - ); - let module = - PyModule::from_code(py, CODE, c_str!(""), &generate_unique_module_name(""))?; - let instance: Py = module.getattr("a")?.into(); - - let foo = crate::intern!(py, "foo"); - let bar = crate::intern!(py, "bar"); - - instance.getattr(py, foo).unwrap_err(); - instance.setattr(py, foo, bar)?; - assert!(instance.getattr(py, foo)?.bind(py).eq(bar)?); - Ok(()) - }) - } - - #[test] - fn invalid_attr() -> PyResult<()> { - Python::with_gil(|py| { - let instance: Py = py.eval(ffi::c_str!("object()"), None, None)?.into(); - - instance.getattr(py, "foo").unwrap_err(); - - // Cannot assign arbitrary attributes to `object` - instance.setattr(py, "foo", "bar").unwrap_err(); - Ok(()) - }) - } - - #[test] - fn test_py2_from_py_object() { - Python::with_gil(|py| { - let instance = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - let ptr = instance.as_ptr(); - let instance: Bound<'_, PyAny> = instance.extract().unwrap(); - assert_eq!(instance.as_ptr(), ptr); - }) - } - - #[test] - fn test_py2_into_py_object() { - Python::with_gil(|py| { - let instance = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - let ptr = instance.as_ptr(); - let instance: PyObject = instance.clone().unbind(); - assert_eq!(instance.as_ptr(), ptr); - }) - } - - #[test] - fn test_debug_fmt() { - Python::with_gil(|py| { - let obj = "hello world".into_pyobject(py).unwrap(); - assert_eq!(format!("{:?}", obj), "'hello world'"); - }); - } - - #[test] - fn test_display_fmt() { - Python::with_gil(|py| { - let obj = "hello world".into_pyobject(py).unwrap(); - assert_eq!(format!("{}", obj), "hello world"); - }); - } - - #[test] - fn test_bound_as_any() { - Python::with_gil(|py| { - let obj = PyString::new(py, "hello world"); - let any = obj.as_any(); - assert_eq!(any.as_ptr(), obj.as_ptr()); - }); - } - - #[test] - fn test_bound_into_any() { - Python::with_gil(|py| { - let obj = PyString::new(py, "hello world"); - let any = obj.clone().into_any(); - assert_eq!(any.as_ptr(), obj.as_ptr()); - }); - } - - #[test] - fn test_bound_py_conversions() { - Python::with_gil(|py| { - let obj: Bound<'_, PyString> = PyString::new(py, "hello world"); - let obj_unbound: &Py = obj.as_unbound(); - let _: &Bound<'_, PyString> = obj_unbound.bind(py); - - let obj_unbound: Py = obj.unbind(); - let obj: Bound<'_, PyString> = obj_unbound.into_bound(py); - - assert_eq!(obj, "hello world"); - }); - } - - #[test] - fn bound_from_borrowed_ptr_constructors() { - // More detailed tests of the underlying semantics in pycell.rs - Python::with_gil(|py| { - fn check_drop<'py>( - py: Python<'py>, - method: impl FnOnce(*mut ffi::PyObject) -> Bound<'py, PyAny>, - ) { - let mut dropped = false; - let capsule = PyCapsule::new_with_destructor( - py, - (&mut dropped) as *mut _ as usize, - None, - |ptr, _| unsafe { std::ptr::write(ptr as *mut bool, true) }, - ) - .unwrap(); - - let bound = method(capsule.as_ptr()); - assert!(!dropped); - - // creating the bound should have increased the refcount - drop(capsule); - assert!(!dropped); - - // dropping the bound should now also decrease the refcount and free the object - drop(bound); - assert!(dropped); - } - - check_drop(py, |ptr| unsafe { Bound::from_borrowed_ptr(py, ptr) }); - check_drop(py, |ptr| unsafe { - Bound::from_borrowed_ptr_or_opt(py, ptr).unwrap() - }); - check_drop(py, |ptr| unsafe { - Bound::from_borrowed_ptr_or_err(py, ptr).unwrap() - }); - }) - } - - #[test] - fn borrowed_ptr_constructors() { - // More detailed tests of the underlying semantics in pycell.rs - Python::with_gil(|py| { - fn check_drop<'py>( - py: Python<'py>, - method: impl FnOnce(&*mut ffi::PyObject) -> Borrowed<'_, 'py, PyAny>, - ) { - let mut dropped = false; - let capsule = PyCapsule::new_with_destructor( - py, - (&mut dropped) as *mut _ as usize, - None, - |ptr, _| unsafe { std::ptr::write(ptr as *mut bool, true) }, - ) - .unwrap(); - - let ptr = &capsule.as_ptr(); - let _borrowed = method(ptr); - assert!(!dropped); - - // creating the borrow should not have increased the refcount - drop(capsule); - assert!(dropped); - } - - check_drop(py, |&ptr| unsafe { Borrowed::from_ptr(py, ptr) }); - check_drop(py, |&ptr| unsafe { - Borrowed::from_ptr_or_opt(py, ptr).unwrap() - }); - check_drop(py, |&ptr| unsafe { - Borrowed::from_ptr_or_err(py, ptr).unwrap() - }); - }) - } - - #[test] - fn explicit_drop_ref() { - Python::with_gil(|py| { - let object: Py = PyDict::new(py).unbind(); - let object2 = object.clone_ref(py); - - assert_eq!(object.as_ptr(), object2.as_ptr()); - assert_eq!(object.get_refcnt(py), 2); - - object.drop_ref(py); - - assert_eq!(object2.get_refcnt(py), 1); - - object2.drop_ref(py); - }); - } - - #[cfg(feature = "macros")] - mod using_macros { - use super::*; - - #[crate::pyclass(crate = "crate")] - struct SomeClass(i32); - - #[test] - fn py_borrow_methods() { - // More detailed tests of the underlying semantics in pycell.rs - Python::with_gil(|py| { - let instance = Py::new(py, SomeClass(0)).unwrap(); - assert_eq!(instance.borrow(py).0, 0); - assert_eq!(instance.try_borrow(py).unwrap().0, 0); - assert_eq!(instance.borrow_mut(py).0, 0); - assert_eq!(instance.try_borrow_mut(py).unwrap().0, 0); - - instance.borrow_mut(py).0 = 123; - - assert_eq!(instance.borrow(py).0, 123); - assert_eq!(instance.try_borrow(py).unwrap().0, 123); - assert_eq!(instance.borrow_mut(py).0, 123); - assert_eq!(instance.try_borrow_mut(py).unwrap().0, 123); - }) - } - - #[test] - fn bound_borrow_methods() { - // More detailed tests of the underlying semantics in pycell.rs - Python::with_gil(|py| { - let instance = Bound::new(py, SomeClass(0)).unwrap(); - assert_eq!(instance.borrow().0, 0); - assert_eq!(instance.try_borrow().unwrap().0, 0); - assert_eq!(instance.borrow_mut().0, 0); - assert_eq!(instance.try_borrow_mut().unwrap().0, 0); - - instance.borrow_mut().0 = 123; - - assert_eq!(instance.borrow().0, 123); - assert_eq!(instance.try_borrow().unwrap().0, 123); - assert_eq!(instance.borrow_mut().0, 123); - assert_eq!(instance.try_borrow_mut().unwrap().0, 123); - }) - } - - #[crate::pyclass(frozen, crate = "crate")] - struct FrozenClass(i32); - - #[test] - fn test_frozen_get() { - Python::with_gil(|py| { - for i in 0..10 { - let instance = Py::new(py, FrozenClass(i)).unwrap(); - assert_eq!(instance.get().0, i); - - assert_eq!(instance.bind(py).get().0, i); - } - }) - } - - #[crate::pyclass(crate = "crate", subclass)] - struct BaseClass; - - trait MyClassMethods<'py>: Sized { - fn pyrepr_by_ref(&self) -> PyResult; - fn pyrepr_by_val(self) -> PyResult { - self.pyrepr_by_ref() - } - } - impl<'py> MyClassMethods<'py> for Bound<'py, BaseClass> { - fn pyrepr_by_ref(&self) -> PyResult { - self.call_method0("__repr__")?.extract() - } - } - - #[crate::pyclass(crate = "crate", extends = BaseClass)] - struct SubClass; - - #[test] - fn test_as_super() { - Python::with_gil(|py| { - let obj = Bound::new(py, (SubClass, BaseClass)).unwrap(); - let _: &Bound<'_, BaseClass> = obj.as_super(); - let _: &Bound<'_, PyAny> = obj.as_super().as_super(); - assert!(obj.as_super().pyrepr_by_ref().is_ok()); - }) - } - - #[test] - fn test_into_super() { - Python::with_gil(|py| { - let obj = Bound::new(py, (SubClass, BaseClass)).unwrap(); - let _: Bound<'_, BaseClass> = obj.clone().into_super(); - let _: Bound<'_, PyAny> = obj.clone().into_super().into_super(); - assert!(obj.into_super().pyrepr_by_val().is_ok()); - }) - } - } -} diff --git a/vendor/pyo3/src/internal.rs b/vendor/pyo3/src/internal.rs deleted file mode 100644 index d42cc2b87f..0000000000 --- a/vendor/pyo3/src/internal.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Holding place for code which is not intended to be reachable from outside of PyO3. - -pub(crate) mod get_slot; diff --git a/vendor/pyo3/src/internal/get_slot.rs b/vendor/pyo3/src/internal/get_slot.rs deleted file mode 100644 index 260893d420..0000000000 --- a/vendor/pyo3/src/internal/get_slot.rs +++ /dev/null @@ -1,282 +0,0 @@ -use crate::{ - ffi, - types::{PyType, PyTypeMethods}, - Borrowed, Bound, -}; -use std::os::raw::c_int; - -impl Bound<'_, PyType> { - #[inline] - pub(crate) fn get_slot(&self, slot: Slot) -> as GetSlotImpl>::Type - where - Slot: GetSlotImpl, - { - // SAFETY: `self` is a valid type object. - unsafe { - slot.get_slot( - self.as_type_ptr(), - #[cfg(all(Py_LIMITED_API, not(Py_3_10)))] - is_runtime_3_10(self.py()), - ) - } - } -} - -impl Borrowed<'_, '_, PyType> { - #[inline] - pub(crate) fn get_slot(self, slot: Slot) -> as GetSlotImpl>::Type - where - Slot: GetSlotImpl, - { - // SAFETY: `self` is a valid type object. - unsafe { - slot.get_slot( - self.as_type_ptr(), - #[cfg(all(Py_LIMITED_API, not(Py_3_10)))] - is_runtime_3_10(self.py()), - ) - } - } -} - -/// Gets a slot from a raw FFI pointer. -/// -/// Safety: -/// - `ty` must be a valid non-null pointer to a `PyTypeObject`. -/// - The Python runtime must be initialized -pub(crate) unsafe fn get_slot( - ty: *mut ffi::PyTypeObject, - slot: Slot, -) -> as GetSlotImpl>::Type -where - Slot: GetSlotImpl, -{ - slot.get_slot( - ty, - // SAFETY: the Python runtime is initialized - #[cfg(all(Py_LIMITED_API, not(Py_3_10)))] - is_runtime_3_10(crate::Python::assume_gil_acquired()), - ) -} - -pub(crate) trait GetSlotImpl { - type Type; - - /// Gets the requested slot from a type object. - /// - /// Safety: - /// - `ty` must be a valid non-null pointer to a `PyTypeObject`. - /// - `is_runtime_3_10` must be `false` if the runtime is not Python 3.10 or later. - unsafe fn get_slot( - self, - ty: *mut ffi::PyTypeObject, - #[cfg(all(Py_LIMITED_API, not(Py_3_10)))] is_runtime_3_10: bool, - ) -> Self::Type; -} - -#[derive(Copy, Clone)] -pub(crate) struct Slot; - -macro_rules! impl_slots { - ($($name:ident: ($slot:ident, $field:ident) -> $tp:ty),+ $(,)?) => { - $( - pub (crate) const $name: Slot<{ ffi::$slot }> = Slot; - - impl GetSlotImpl for Slot<{ ffi::$slot }> { - type Type = $tp; - - #[inline] - unsafe fn get_slot( - self, - ty: *mut ffi::PyTypeObject, - #[cfg(all(Py_LIMITED_API, not(Py_3_10)))] is_runtime_3_10: bool - ) -> Self::Type { - #[cfg(not(Py_LIMITED_API))] - { - (*ty).$field - } - - #[cfg(Py_LIMITED_API)] - { - #[cfg(not(Py_3_10))] - { - // Calling PyType_GetSlot on static types is not valid before Python 3.10 - // ... so the workaround is to first do a runtime check for these versions - // (3.7, 3.8, 3.9) and then look in the type object anyway. This is only ok - // because we know that the interpreter is not going to change the size - // of the type objects for these historical versions. - if !is_runtime_3_10 && ffi::PyType_HasFeature(ty, ffi::Py_TPFLAGS_HEAPTYPE) == 0 - { - return (*ty.cast::()).$field; - } - } - - // SAFETY: slot type is set carefully to be valid - std::mem::transmute(ffi::PyType_GetSlot(ty, ffi::$slot)) - } - } - } - )* - }; -} - -// Slots are implemented on-demand as needed.) -impl_slots! { - TP_ALLOC: (Py_tp_alloc, tp_alloc) -> Option, - TP_BASE: (Py_tp_base, tp_base) -> *mut ffi::PyTypeObject, - TP_CLEAR: (Py_tp_clear, tp_clear) -> Option, - TP_DESCR_GET: (Py_tp_descr_get, tp_descr_get) -> Option, - TP_FREE: (Py_tp_free, tp_free) -> Option, - TP_TRAVERSE: (Py_tp_traverse, tp_traverse) -> Option, -} - -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -fn is_runtime_3_10(py: crate::Python<'_>) -> bool { - use crate::sync::GILOnceCell; - - static IS_RUNTIME_3_10: GILOnceCell = GILOnceCell::new(); - *IS_RUNTIME_3_10.get_or_init(py, || py.version_info() >= (3, 10)) -} - -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -pub struct PyNumberMethods39Snapshot { - pub nb_add: Option, - pub nb_subtract: Option, - pub nb_multiply: Option, - pub nb_remainder: Option, - pub nb_divmod: Option, - pub nb_power: Option, - pub nb_negative: Option, - pub nb_positive: Option, - pub nb_absolute: Option, - pub nb_bool: Option, - pub nb_invert: Option, - pub nb_lshift: Option, - pub nb_rshift: Option, - pub nb_and: Option, - pub nb_xor: Option, - pub nb_or: Option, - pub nb_int: Option, - pub nb_reserved: *mut std::os::raw::c_void, - pub nb_float: Option, - pub nb_inplace_add: Option, - pub nb_inplace_subtract: Option, - pub nb_inplace_multiply: Option, - pub nb_inplace_remainder: Option, - pub nb_inplace_power: Option, - pub nb_inplace_lshift: Option, - pub nb_inplace_rshift: Option, - pub nb_inplace_and: Option, - pub nb_inplace_xor: Option, - pub nb_inplace_or: Option, - pub nb_floor_divide: Option, - pub nb_true_divide: Option, - pub nb_inplace_floor_divide: Option, - pub nb_inplace_true_divide: Option, - pub nb_index: Option, - pub nb_matrix_multiply: Option, - pub nb_inplace_matrix_multiply: Option, -} - -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -pub struct PySequenceMethods39Snapshot { - pub sq_length: Option, - pub sq_concat: Option, - pub sq_repeat: Option, - pub sq_item: Option, - pub was_sq_slice: *mut std::os::raw::c_void, - pub sq_ass_item: Option, - pub was_sq_ass_slice: *mut std::os::raw::c_void, - pub sq_contains: Option, - pub sq_inplace_concat: Option, - pub sq_inplace_repeat: Option, -} - -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -pub struct PyMappingMethods39Snapshot { - pub mp_length: Option, - pub mp_subscript: Option, - pub mp_ass_subscript: Option, -} - -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -pub struct PyAsyncMethods39Snapshot { - pub am_await: Option, - pub am_aiter: Option, - pub am_anext: Option, -} - -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -pub struct PyBufferProcs39Snapshot { - // not available in limited api, but structure needs to have the right size - pub bf_getbuffer: *mut std::os::raw::c_void, - pub bf_releasebuffer: *mut std::os::raw::c_void, -} - -/// Snapshot of the structure of PyTypeObject for Python 3.7 through 3.9. -/// -/// This is used as a fallback for static types in abi3 when the Python version is less than 3.10; -/// this is a bit of a hack but there's no better option and the structure of the type object is -/// not going to change for those historical versions. -#[repr(C)] -#[cfg(all(Py_LIMITED_API, not(Py_3_10)))] -struct PyTypeObject39Snapshot { - pub ob_base: ffi::PyVarObject, - pub tp_name: *const std::os::raw::c_char, - pub tp_basicsize: ffi::Py_ssize_t, - pub tp_itemsize: ffi::Py_ssize_t, - pub tp_dealloc: Option, - #[cfg(not(Py_3_8))] - pub tp_print: *mut std::os::raw::c_void, // stubbed out, not available in limited API - #[cfg(Py_3_8)] - pub tp_vectorcall_offset: ffi::Py_ssize_t, - pub tp_getattr: Option, - pub tp_setattr: Option, - pub tp_as_async: *mut PyAsyncMethods39Snapshot, - pub tp_repr: Option, - pub tp_as_number: *mut PyNumberMethods39Snapshot, - pub tp_as_sequence: *mut PySequenceMethods39Snapshot, - pub tp_as_mapping: *mut PyMappingMethods39Snapshot, - pub tp_hash: Option, - pub tp_call: Option, - pub tp_str: Option, - pub tp_getattro: Option, - pub tp_setattro: Option, - pub tp_as_buffer: *mut PyBufferProcs39Snapshot, - pub tp_flags: std::os::raw::c_ulong, - pub tp_doc: *const std::os::raw::c_char, - pub tp_traverse: Option, - pub tp_clear: Option, - pub tp_richcompare: Option, - pub tp_weaklistoffset: ffi::Py_ssize_t, - pub tp_iter: Option, - pub tp_iternext: Option, - pub tp_methods: *mut ffi::PyMethodDef, - pub tp_members: *mut ffi::PyMemberDef, - pub tp_getset: *mut ffi::PyGetSetDef, - pub tp_base: *mut ffi::PyTypeObject, - pub tp_dict: *mut ffi::PyObject, - pub tp_descr_get: Option, - pub tp_descr_set: Option, - pub tp_dictoffset: ffi::Py_ssize_t, - pub tp_init: Option, - pub tp_alloc: Option, - pub tp_new: Option, - pub tp_free: Option, - pub tp_is_gc: Option, - pub tp_bases: *mut ffi::PyObject, - pub tp_mro: *mut ffi::PyObject, - pub tp_cache: *mut ffi::PyObject, - pub tp_subclasses: *mut ffi::PyObject, - pub tp_weaklist: *mut ffi::PyObject, - pub tp_del: Option, - pub tp_version_tag: std::os::raw::c_uint, - pub tp_finalize: Option, - #[cfg(Py_3_8)] - pub tp_vectorcall: Option, -} diff --git a/vendor/pyo3/src/internal_tricks.rs b/vendor/pyo3/src/internal_tricks.rs deleted file mode 100644 index dad3f7c4c0..0000000000 --- a/vendor/pyo3/src/internal_tricks.rs +++ /dev/null @@ -1,77 +0,0 @@ -use crate::ffi::{self, Py_ssize_t, PY_SSIZE_T_MAX}; -pub struct PrivateMarker; - -macro_rules! private_decl { - () => { - /// This trait is private to implement; this method exists to make it - /// impossible to implement outside the crate. - fn __private__(&self) -> crate::internal_tricks::PrivateMarker; - }; -} - -macro_rules! private_impl { - () => { - fn __private__(&self) -> crate::internal_tricks::PrivateMarker { - crate::internal_tricks::PrivateMarker - } - }; -} - -macro_rules! pyo3_exception { - ($doc: expr, $name: ident, $base: ty) => { - #[doc = $doc] - #[repr(transparent)] - #[allow(non_camel_case_types)] - pub struct $name($crate::PyAny); - - $crate::impl_exception_boilerplate!($name); - - $crate::create_exception_type_object!(pyo3_runtime, $name, $base, Some($doc)); - }; -} - -/// Convert an usize index into a Py_ssize_t index, clamping overflow to -/// PY_SSIZE_T_MAX. -pub(crate) fn get_ssize_index(index: usize) -> Py_ssize_t { - index.min(PY_SSIZE_T_MAX as usize) as Py_ssize_t -} - -// TODO: use ptr::from_ref on MSRV 1.76 -#[inline] -pub(crate) const fn ptr_from_ref(t: &T) -> *const T { - t as *const T -} - -// TODO: use ptr::from_mut on MSRV 1.76 -#[inline] -pub(crate) fn ptr_from_mut(t: &mut T) -> *mut T { - t as *mut T -} - -// TODO: use ptr::fn_addr_eq on MSRV 1.85 -pub(crate) fn clear_eq(f: Option, g: ffi::inquiry) -> bool { - #[cfg(fn_ptr_eq)] - { - let Some(f) = f else { return false }; - std::ptr::fn_addr_eq(f, g) - } - - #[cfg(not(fn_ptr_eq))] - { - f == Some(g) - } -} - -// TODO: use ptr::fn_addr_eq on MSRV 1.85 -pub(crate) fn traverse_eq(f: Option, g: ffi::traverseproc) -> bool { - #[cfg(fn_ptr_eq)] - { - let Some(f) = f else { return false }; - std::ptr::fn_addr_eq(f, g) - } - - #[cfg(not(fn_ptr_eq))] - { - f == Some(g) - } -} diff --git a/vendor/pyo3/src/lib.rs b/vendor/pyo3/src/lib.rs deleted file mode 100644 index 46a2fd53d3..0000000000 --- a/vendor/pyo3/src/lib.rs +++ /dev/null @@ -1,537 +0,0 @@ -#![warn(missing_docs)] -#![cfg_attr( - feature = "nightly", - feature(auto_traits, negative_impls, try_trait_v2) -)] -#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -// Deny some lints in doctests. -// Use `#[allow(...)]` locally to override. -#![doc(test(attr( - deny( - rust_2018_idioms, - unused_lifetimes, - rust_2021_prelude_collisions, - warnings - ), - allow( - unused_variables, - unused_assignments, - unused_extern_crates, - // FIXME https://github.com/rust-lang/rust/issues/121621#issuecomment-1965156376 - unknown_lints, - non_local_definitions, - ) -)))] - -//! Rust bindings to the Python interpreter. -//! -//! PyO3 can be used to write native Python modules or run Python code and modules from Rust. -//! -//! See [the guide] for a detailed introduction. -//! -//! # PyO3's object types -//! -//! PyO3 has several core types that you should familiarize yourself with: -//! -//! ## The `Python<'py>` object, and the `'py` lifetime -//! -//! Holding the [global interpreter lock] (GIL) is modeled with the [`Python<'py>`](Python) token. Many -//! Python APIs require that the GIL is held, and PyO3 uses this token as proof that these APIs -//! can be called safely. It can be explicitly acquired and is also implicitly acquired by PyO3 -//! as it wraps Rust functions and structs into Python functions and objects. -//! -//! The [`Python<'py>`](Python) token's lifetime `'py` is common to many PyO3 APIs: -//! - Types that also have the `'py` lifetime, such as the [`Bound<'py, T>`](Bound) smart pointer, are -//! bound to the Python GIL and rely on this to offer their functionality. These types often -//! have a [`.py()`](Bound::py) method to get the associated [`Python<'py>`](Python) token. -//! - Functions which depend on the `'py` lifetime, such as [`PyList::new`](types::PyList::new), -//! require a [`Python<'py>`](Python) token as an input. Sometimes the token is passed implicitly by -//! taking a [`Bound<'py, T>`](Bound) or other type which is bound to the `'py` lifetime. -//! - Traits which depend on the `'py` lifetime, such as [`FromPyObject<'py>`](FromPyObject), usually have -//! inputs or outputs which depend on the lifetime. Adding the lifetime to the trait allows -//! these inputs and outputs to express their binding to the GIL in the Rust type system. -//! -//! ## Python object smart pointers -//! -//! PyO3 has two core smart pointers to refer to Python objects, [`Py`](Py) and its GIL-bound -//! form [`Bound<'py, T>`](Bound) which carries the `'py` lifetime. (There is also -//! [`Borrowed<'a, 'py, T>`](instance::Borrowed), but it is used much more rarely). -//! -//! The type parameter `T` in these smart pointers can be filled by: -//! - [`PyAny`], e.g. `Py` or `Bound<'py, PyAny>`, where the Python object type is not -//! known. `Py` is so common it has a type alias [`PyObject`]. -//! - Concrete Python types like [`PyList`](types::PyList) or [`PyTuple`](types::PyTuple). -//! - Rust types which are exposed to Python using the [`#[pyclass]`](macro@pyclass) macro. -//! -//! See the [guide][types] for an explanation of the different Python object types. -//! -//! ## PyErr -//! -//! The vast majority of operations in this library will return [`PyResult<...>`](PyResult). -//! This is an alias for the type `Result<..., PyErr>`. -//! -//! A `PyErr` represents a Python exception. A `PyErr` returned to Python code will be raised as a -//! Python exception. Errors from `PyO3` itself are also exposed as Python exceptions. -//! -//! # Feature flags -//! -//! PyO3 uses [feature flags] to enable you to opt-in to additional functionality. For a detailed -//! description, see the [Features chapter of the guide]. -//! -//! ## Default feature flags -//! -//! The following features are turned on by default: -//! - `macros`: Enables various macros, including all the attribute macros. -//! -//! ## Optional feature flags -//! -//! The following features customize PyO3's behavior: -//! -//! - `abi3`: Restricts PyO3's API to a subset of the full Python API which is guaranteed by -//! [PEP 384] to be forward-compatible with future Python versions. -//! - `auto-initialize`: Changes [`Python::with_gil`] to automatically initialize the Python -//! interpreter if needed. -//! - `extension-module`: This will tell the linker to keep the Python symbols unresolved, so that -//! your module can also be used with statically linked Python interpreters. Use this feature when -//! building an extension module. -//! - `multiple-pymethods`: Enables the use of multiple [`#[pymethods]`](macro@crate::pymethods) -//! blocks per [`#[pyclass]`](macro@crate::pyclass). This adds a dependency on the [inventory] -//! crate, which is not supported on all platforms. -//! -//! The following features enable interactions with other crates in the Rust ecosystem: -//! - [`anyhow`]: Enables a conversion from [anyhow]’s [`Error`][anyhow_error] type to [`PyErr`]. -//! - [`chrono`]: Enables a conversion from [chrono]'s structures to the equivalent Python ones. -//! - [`chrono-tz`]: Enables a conversion from [chrono-tz]'s `Tz` enum. Requires Python 3.9+. -//! - [`either`]: Enables conversions between Python objects and [either]'s [`Either`] type. -//! - [`eyre`]: Enables a conversion from [eyre]’s [`Report`] type to [`PyErr`]. -//! - [`hashbrown`]: Enables conversions between Python objects and [hashbrown]'s [`HashMap`] and -//! [`HashSet`] types. -//! - [`indexmap`][indexmap_feature]: Enables conversions between Python dictionary and [indexmap]'s [`IndexMap`]. -//! - [`num-bigint`]: Enables conversions between Python objects and [num-bigint]'s [`BigInt`] and -//! [`BigUint`] types. -//! - [`num-complex`]: Enables conversions between Python objects and [num-complex]'s [`Complex`] -//! type. -//! - [`num-rational`]: Enables conversions between Python's fractions.Fraction and [num-rational]'s types -//! - [`rust_decimal`]: Enables conversions between Python's decimal.Decimal and [rust_decimal]'s -//! [`Decimal`] type. -//! - [`serde`]: Allows implementing [serde]'s [`Serialize`] and [`Deserialize`] traits for -//! [`Py`]`` for all `T` that implement [`Serialize`] and [`Deserialize`]. -//! - [`smallvec`][smallvec]: Enables conversions between Python list and [smallvec]'s [`SmallVec`]. -//! -//! ## Unstable features -//! -//! - `nightly`: Uses `#![feature(auto_traits, negative_impls)]` to define [`Ungil`] as an auto trait. -// -//! ## `rustc` environment flags -//! - `Py_3_7`, `Py_3_8`, `Py_3_9`, `Py_3_10`, `Py_3_11`, `Py_3_12`, `Py_3_13`: Marks code that is -//! only enabled when compiling for a given minimum Python version. -//! - `Py_LIMITED_API`: Marks code enabled when the `abi3` feature flag is enabled. -//! - `Py_GIL_DISABLED`: Marks code that runs only in the free-threaded build of CPython. -//! - `PyPy` - Marks code enabled when compiling for PyPy. -//! - `GraalPy` - Marks code enabled when compiling for GraalPy. -//! -//! Additionally, you can query for the values `Py_DEBUG`, `Py_REF_DEBUG`, -//! `Py_TRACE_REFS`, and `COUNT_ALLOCS` from `py_sys_config` to query for the -//! corresponding C build-time defines. For example, to conditionally define -//! debug code using `Py_DEBUG`, you could do: -//! -//! ```rust,ignore -//! #[cfg(py_sys_config = "Py_DEBUG")] -//! println!("only runs if python was compiled with Py_DEBUG") -//! ``` -//! To use these attributes, add [`pyo3-build-config`] as a build dependency in -//! your `Cargo.toml` and call `pyo3_build_config::use_pyo3_cfgs()` in a -//! `build.rs` file. -//! -//! # Minimum supported Rust and Python versions -//! -//! Requires Rust 1.63 or greater. -//! -//! PyO3 supports the following Python distributions: -//! - CPython 3.7 or greater -//! - PyPy 7.3 (Python 3.9+) -//! - GraalPy 24.0 or greater (Python 3.10+) -//! -//! # Example: Building a native Python module -//! -//! PyO3 can be used to generate a native Python module. The easiest way to try this out for the -//! first time is to use [`maturin`]. `maturin` is a tool for building and publishing Rust-based -//! Python packages with minimal configuration. The following steps set up some files for an example -//! Python module, install `maturin`, and then show how to build and import the Python module. -//! -//! First, create a new folder (let's call it `string_sum`) containing the following two files: -//! -//! **`Cargo.toml`** -//! -//! ```toml -//! [package] -//! name = "string-sum" -//! version = "0.1.0" -//! edition = "2021" -//! -//! [lib] -//! name = "string_sum" -//! # "cdylib" is necessary to produce a shared library for Python to import from. -//! # -//! # Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able -//! # to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.: -//! # crate-type = ["cdylib", "rlib"] -//! crate-type = ["cdylib"] -//! -//! [dependencies.pyo3] -#![doc = concat!("version = \"", env!("CARGO_PKG_VERSION"), "\"")] -//! features = ["extension-module"] -//! ``` -//! -//! **`src/lib.rs`** -//! ```rust -//! use pyo3::prelude::*; -//! -//! /// Formats the sum of two numbers as string. -//! #[pyfunction] -//! fn sum_as_string(a: usize, b: usize) -> PyResult { -//! Ok((a + b).to_string()) -//! } -//! -//! /// A Python module implemented in Rust. -//! #[pymodule] -//! fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> { -//! m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; -//! -//! Ok(()) -//! } -//! ``` -//! -//! With those two files in place, now `maturin` needs to be installed. This can be done using -//! Python's package manager `pip`. First, load up a new Python `virtualenv`, and install `maturin` -//! into it: -//! ```bash -//! $ cd string_sum -//! $ python -m venv .env -//! $ source .env/bin/activate -//! $ pip install maturin -//! ``` -//! -//! Now build and execute the module: -//! ```bash -//! $ maturin develop -//! # lots of progress output as maturin runs the compilation... -//! $ python -//! >>> import string_sum -//! >>> string_sum.sum_as_string(5, 20) -//! '25' -//! ``` -//! -//! As well as with `maturin`, it is possible to build using [setuptools-rust] or -//! [manually][manual_builds]. Both offer more flexibility than `maturin` but require further -//! configuration. -//! -//! # Example: Using Python from Rust -//! -//! To embed Python into a Rust binary, you need to ensure that your Python installation contains a -//! shared library. The following steps demonstrate how to ensure this (for Ubuntu), and then give -//! some example code which runs an embedded Python interpreter. -//! -//! To install the Python shared library on Ubuntu: -//! ```bash -//! sudo apt install python3-dev -//! ``` -//! -//! Start a new project with `cargo new` and add `pyo3` to the `Cargo.toml` like this: -//! ```toml -//! [dependencies.pyo3] -#![doc = concat!("version = \"", env!("CARGO_PKG_VERSION"), "\"")] -//! # this is necessary to automatically initialize the Python interpreter -//! features = ["auto-initialize"] -//! ``` -//! -//! Example program displaying the value of `sys.version` and the current user name: -//! ```rust -//! use pyo3::prelude::*; -//! use pyo3::types::IntoPyDict; -//! use pyo3::ffi::c_str; -//! -//! fn main() -> PyResult<()> { -//! Python::with_gil(|py| { -//! let sys = py.import("sys")?; -//! let version: String = sys.getattr("version")?.extract()?; -//! -//! let locals = [("os", py.import("os")?)].into_py_dict(py)?; -//! let code = c_str!("os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'"); -//! let user: String = py.eval(code, None, Some(&locals))?.extract()?; -//! -//! println!("Hello {}, I'm Python {}", user, version); -//! Ok(()) -//! }) -//! } -//! ``` -//! -//! The guide has [a section][calling_rust] with lots of examples about this topic. -//! -//! # Other Examples -//! -//! The PyO3 [README](https://github.com/PyO3/pyo3#readme) contains quick-start examples for both -//! using [Rust from Python] and [Python from Rust]. -//! -//! The PyO3 repository's [examples subdirectory] -//! contains some basic packages to demonstrate usage of PyO3. -//! -//! There are many projects using PyO3 - see a list of some at -//! . -//! -//! [anyhow]: https://docs.rs/anyhow/ "A trait object based error system for easy idiomatic error handling in Rust applications." -//! [anyhow_error]: https://docs.rs/anyhow/latest/anyhow/struct.Error.html "Anyhows `Error` type, a wrapper around a dynamic error type" -//! [`anyhow`]: ./anyhow/index.html "Documentation about the `anyhow` feature." -//! [inventory]: https://docs.rs/inventory -//! [`HashMap`]: https://docs.rs/hashbrown/latest/hashbrown/struct.HashMap.html -//! [`HashSet`]: https://docs.rs/hashbrown/latest/hashbrown/struct.HashSet.html -//! [`SmallVec`]: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html -//! [`IndexMap`]: https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html -//! [`BigInt`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigInt.html -//! [`BigUint`]: https://docs.rs/num-bigint/latest/num_bigint/struct.BigUint.html -//! [`Complex`]: https://docs.rs/num-complex/latest/num_complex/struct.Complex.html -//! [`Deserialize`]: https://docs.rs/serde/latest/serde/trait.Deserialize.html -//! [`Serialize`]: https://docs.rs/serde/latest/serde/trait.Serialize.html -//! [chrono]: https://docs.rs/chrono/ "Date and Time for Rust." -//! [chrono-tz]: https://docs.rs/chrono-tz/ "TimeZone implementations for chrono from the IANA database." -//! [`chrono`]: ./chrono/index.html "Documentation about the `chrono` feature." -//! [`chrono-tz`]: ./chrono-tz/index.html "Documentation about the `chrono-tz` feature." -//! [either]: https://docs.rs/either/ "A type that represents one of two alternatives." -//! [`either`]: ./either/index.html "Documentation about the `either` feature." -//! [`Either`]: https://docs.rs/either/latest/either/enum.Either.html -//! [eyre]: https://docs.rs/eyre/ "A library for easy idiomatic error handling and reporting in Rust applications." -//! [`Report`]: https://docs.rs/eyre/latest/eyre/struct.Report.html -//! [`eyre`]: ./eyre/index.html "Documentation about the `eyre` feature." -//! [`hashbrown`]: ./hashbrown/index.html "Documentation about the `hashbrown` feature." -//! [indexmap_feature]: ./indexmap/index.html "Documentation about the `indexmap` feature." -//! [`maturin`]: https://github.com/PyO3/maturin "Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages" -//! [`num-bigint`]: ./num_bigint/index.html "Documentation about the `num-bigint` feature." -//! [`num-complex`]: ./num_complex/index.html "Documentation about the `num-complex` feature." -//! [`num-rational`]: ./num_rational/index.html "Documentation about the `num-rational` feature." -//! [`pyo3-build-config`]: https://docs.rs/pyo3-build-config -//! [rust_decimal]: https://docs.rs/rust_decimal -//! [`rust_decimal`]: ./rust_decimal/index.html "Documenation about the `rust_decimal` feature." -//! [`Decimal`]: https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html -//! [`serde`]: <./serde/index.html> "Documentation about the `serde` feature." -#![doc = concat!("[calling_rust]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/python-from-rust.html \"Calling Python from Rust - PyO3 user guide\"")] -//! [examples subdirectory]: https://github.com/PyO3/pyo3/tree/main/examples -//! [feature flags]: https://doc.rust-lang.org/cargo/reference/features.html "Features - The Cargo Book" -//! [global interpreter lock]: https://docs.python.org/3/glossary.html#term-global-interpreter-lock -//! [hashbrown]: https://docs.rs/hashbrown -//! [smallvec]: https://docs.rs/smallvec -//! [indexmap]: https://docs.rs/indexmap -#![doc = concat!("[manual_builds]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/building-and-distribution.html#manual-builds \"Manual builds - Building and Distribution - PyO3 user guide\"")] -//! [num-bigint]: https://docs.rs/num-bigint -//! [num-complex]: https://docs.rs/num-complex -//! [num-rational]: https://docs.rs/num-rational -//! [serde]: https://docs.rs/serde -//! [setuptools-rust]: https://github.com/PyO3/setuptools-rust "Setuptools plugin for Rust extensions" -//! [the guide]: https://pyo3.rs "PyO3 user guide" -#![doc = concat!("[types]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/types.html \"GIL lifetimes, mutability and Python object types\"")] -//! [PEP 384]: https://www.python.org/dev/peps/pep-0384 "PEP 384 -- Defining a Stable ABI" -//! [Python from Rust]: https://github.com/PyO3/pyo3#using-python-from-rust -//! [Rust from Python]: https://github.com/PyO3/pyo3#using-rust-from-python -#![doc = concat!("[Features chapter of the guide]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#features-reference \"Features Reference - PyO3 user guide\"")] -//! [`Ungil`]: crate::marker::Ungil -pub use crate::class::*; -pub use crate::conversion::{AsPyPointer, FromPyObject, IntoPyObject, IntoPyObjectExt}; -#[allow(deprecated)] -pub use crate::conversion::{IntoPy, ToPyObject}; -pub use crate::err::{DowncastError, DowncastIntoError, PyErr, PyErrArguments, PyResult, ToPyErr}; -#[cfg(not(any(PyPy, GraalPy)))] -pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter}; -pub use crate::instance::{Borrowed, Bound, BoundObject, Py, PyObject}; -pub use crate::marker::Python; -pub use crate::pycell::{PyRef, PyRefMut}; -pub use crate::pyclass::PyClass; -pub use crate::pyclass_init::PyClassInitializer; -pub use crate::type_object::{PyTypeCheck, PyTypeInfo}; -pub use crate::types::PyAny; -pub use crate::version::PythonVersionInfo; - -pub(crate) mod ffi_ptr_ext; -pub(crate) mod py_result_ext; -pub(crate) mod sealed; - -/// Old module which contained some implementation details of the `#[pyproto]` module. -/// -/// Prefer using the same content from `pyo3::pyclass`, e.g. `use pyo3::pyclass::CompareOp` instead -/// of `use pyo3::class::basic::CompareOp`. -/// -/// For compatibility reasons this has not yet been removed, however will be done so -/// once is resolved. -pub mod class { - pub use self::gc::{PyTraverseError, PyVisit}; - - pub use self::methods::*; - - #[doc(hidden)] - pub mod methods { - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type IPowModulo = crate::impl_::pymethods::IPowModulo; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PyClassAttributeDef = crate::impl_::pymethods::PyClassAttributeDef; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PyGetterDef = crate::impl_::pymethods::PyGetterDef; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PyMethodDef = crate::impl_::pymethods::PyMethodDef; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PyMethodDefType = crate::impl_::pymethods::PyMethodDefType; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PyMethodType = crate::impl_::pymethods::PyMethodType; - #[deprecated(since = "0.23.0", note = "PyO3 implementation detail")] - pub type PySetterDef = crate::impl_::pymethods::PySetterDef; - } - - /// Old module which contained some implementation details of the `#[pyproto]` module. - /// - /// Prefer using the same content from `pyo3::pyclass`, e.g. `use pyo3::pyclass::CompareOp` instead - /// of `use pyo3::class::basic::CompareOp`. - /// - /// For compatibility reasons this has not yet been removed, however will be done so - /// once is resolved. - pub mod basic { - pub use crate::pyclass::CompareOp; - } - - /// Old module which contained some implementation details of the `#[pyproto]` module. - /// - /// Prefer using the same content from `pyo3::pyclass`, e.g. `use pyo3::pyclass::PyTraverseError` instead - /// of `use pyo3::class::gc::PyTraverseError`. - /// - /// For compatibility reasons this has not yet been removed, however will be done so - /// once is resolved. - pub mod gc { - pub use crate::pyclass::{PyTraverseError, PyVisit}; - } -} - -#[cfg(feature = "macros")] -#[doc(hidden)] -pub use { - indoc, // Re-exported for py_run - unindent, // Re-exported for py_run -}; - -#[cfg(all(feature = "macros", feature = "multiple-pymethods"))] -#[doc(hidden)] -pub use inventory; // Re-exported for `#[pyclass]` and `#[pymethods]` with `multiple-pymethods`. - -/// Tests and helpers which reside inside PyO3's main library. Declared first so that macros -/// are available in unit tests. -#[cfg(test)] -#[macro_use] -mod tests; - -#[macro_use] -mod internal_tricks; -mod internal; - -pub mod buffer; -pub mod conversion; -mod conversions; -#[cfg(feature = "experimental-async")] -pub mod coroutine; -mod err; -pub mod exceptions; -pub mod ffi; -mod gil; -#[doc(hidden)] -pub mod impl_; -mod instance; -pub mod marker; -pub mod marshal; -#[macro_use] -pub mod sync; -pub mod panic; -pub mod pybacked; -pub mod pycell; -pub mod pyclass; -pub mod pyclass_init; - -pub mod type_object; -pub mod types; -mod version; - -#[allow(unused_imports)] // with no features enabled this module has no public exports -pub use crate::conversions::*; - -#[cfg(feature = "macros")] -pub use pyo3_macros::{ - pyfunction, pymethods, pymodule, FromPyObject, IntoPyObject, IntoPyObjectRef, -}; - -/// A proc macro used to expose Rust structs and fieldless enums as Python objects. -/// -#[doc = include_str!("../guide/pyclass-parameters.md")] -/// -/// For more on creating Python classes, -/// see the [class section of the guide][1]. -/// -#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html")] -#[cfg(feature = "macros")] -pub use pyo3_macros::pyclass; - -#[cfg(feature = "macros")] -#[macro_use] -mod macros; - -#[cfg(feature = "experimental-inspect")] -pub mod inspect; - -// Putting the declaration of prelude at the end seems to help encourage rustc and rustdoc to prefer using -// other paths to the same items. (e.g. `pyo3::types::PyAnyMethods` instead of `pyo3::prelude::PyAnyMethods`). -pub mod prelude; - -/// Test readme and user guide -#[cfg(doctest)] -pub mod doc_test { - macro_rules! doctests { - ($($path:expr => $mod:ident),* $(,)?) => { - $( - #[doc = include_str!(concat!("../", $path))] - mod $mod{} - )* - }; - } - - doctests! { - "README.md" => readme_md, - "guide/src/advanced.md" => guide_advanced_md, - "guide/src/async-await.md" => guide_async_await_md, - "guide/src/building-and-distribution.md" => guide_building_and_distribution_md, - "guide/src/building-and-distribution/multiple-python-versions.md" => guide_bnd_multiple_python_versions_md, - "guide/src/class.md" => guide_class_md, - "guide/src/class/call.md" => guide_class_call, - "guide/src/class/object.md" => guide_class_object, - "guide/src/class/numeric.md" => guide_class_numeric, - "guide/src/class/protocols.md" => guide_class_protocols_md, - "guide/src/class/thread-safety.md" => guide_class_thread_safety_md, - "guide/src/conversions.md" => guide_conversions_md, - "guide/src/conversions/tables.md" => guide_conversions_tables_md, - "guide/src/conversions/traits.md" => guide_conversions_traits_md, - "guide/src/debugging.md" => guide_debugging_md, - - // deliberate choice not to test guide/ecosystem because those pages depend on external - // crates such as pyo3_asyncio. - - "guide/src/exception.md" => guide_exception_md, - "guide/src/faq.md" => guide_faq_md, - "guide/src/features.md" => guide_features_md, - "guide/src/free-threading.md" => guide_free_threading_md, - "guide/src/function.md" => guide_function_md, - "guide/src/function/error-handling.md" => guide_function_error_handling_md, - "guide/src/function/signature.md" => guide_function_signature_md, - "guide/src/migration.md" => guide_migration_md, - "guide/src/module.md" => guide_module_md, - "guide/src/parallelism.md" => guide_parallelism_md, - "guide/src/performance.md" => guide_performance_md, - "guide/src/python-from-rust.md" => guide_python_from_rust_md, - "guide/src/python-from-rust/calling-existing-code.md" => guide_pfr_calling_existing_code_md, - "guide/src/python-from-rust/function-calls.md" => guide_pfr_function_calls_md, - "guide/src/python-typing-hints.md" => guide_python_typing_hints_md, - "guide/src/rust-from-python.md" => guide_rust_from_python_md, - "guide/src/trait-bounds.md" => guide_trait_bounds_md, - "guide/src/types.md" => guide_types_md, - } -} diff --git a/vendor/pyo3/src/macros.rs b/vendor/pyo3/src/macros.rs deleted file mode 100644 index 53fcbcaad3..0000000000 --- a/vendor/pyo3/src/macros.rs +++ /dev/null @@ -1,206 +0,0 @@ -/// A convenient macro to execute a Python code snippet, with some local variables set. -/// -/// # Panics -/// -/// This macro internally calls [`Python::run_bound`](crate::Python::run_bound) and panics -/// if it returns `Err`, after printing the error to stdout. -/// -/// If you need to handle failures, please use [`Python::run_bound`](crate::marker::Python::run_bound) instead. -/// -/// # Examples -/// ``` -/// use pyo3::{prelude::*, py_run, types::PyList}; -/// -/// # fn main() -> PyResult<()> { -/// Python::with_gil(|py| { -/// let list = PyList::new(py, &[1, 2, 3])?; -/// py_run!(py, list, "assert list == [1, 2, 3]"); -/// # Ok(()) -/// }) -/// # } -/// ``` -/// -/// You can use this macro to test pyfunctions or pyclasses quickly. -/// -/// ``` -/// use pyo3::{prelude::*, py_run}; -/// -/// #[pyclass] -/// #[derive(Debug)] -/// struct Time { -/// hour: u32, -/// minute: u32, -/// second: u32, -/// } -/// -/// #[pymethods] -/// impl Time { -/// fn repl_japanese(&self) -> String { -/// format!("{}時{}分{}秒", self.hour, self.minute, self.second) -/// } -/// #[getter] -/// fn hour(&self) -> u32 { -/// self.hour -/// } -/// fn as_tuple(&self) -> (u32, u32, u32) { -/// (self.hour, self.minute, self.second) -/// } -/// } -/// -/// Python::with_gil(|py| { -/// let time = Py::new(py, Time {hour: 8, minute: 43, second: 16}).unwrap(); -/// let time_as_tuple = (8, 43, 16); -/// py_run!(py, time time_as_tuple, r#" -/// assert time.hour == 8 -/// assert time.repl_japanese() == "8時43分16秒" -/// assert time.as_tuple() == time_as_tuple -/// "#); -/// }); -/// ``` -/// -/// If you need to prepare the `locals` dict by yourself, you can pass it as `*locals`. -/// -/// ``` -/// use pyo3::prelude::*; -/// use pyo3::types::IntoPyDict; -/// -/// #[pyclass] -/// struct MyClass; -/// -/// #[pymethods] -/// impl MyClass { -/// #[new] -/// fn new() -> Self { -/// MyClass {} -/// } -/// } -/// -/// # fn main() -> PyResult<()> { -/// Python::with_gil(|py| { -/// let locals = [("C", py.get_type::())].into_py_dict(py)?; -/// pyo3::py_run!(py, *locals, "c = C()"); -/// # Ok(()) -/// }) -/// # } -/// ``` -#[macro_export] -macro_rules! py_run { - ($py:expr, $($val:ident)+, $code:literal) => {{ - $crate::py_run_impl!($py, $($val)+, $crate::indoc::indoc!($code)) - }}; - ($py:expr, $($val:ident)+, $code:expr) => {{ - $crate::py_run_impl!($py, $($val)+, $crate::unindent::unindent($code)) - }}; - ($py:expr, *$dict:expr, $code:literal) => {{ - $crate::py_run_impl!($py, *$dict, $crate::indoc::indoc!($code)) - }}; - ($py:expr, *$dict:expr, $code:expr) => {{ - $crate::py_run_impl!($py, *$dict, $crate::unindent::unindent($code)) - }}; -} - -#[macro_export] -#[doc(hidden)] -macro_rules! py_run_impl { - ($py:expr, $($val:ident)+, $code:expr) => {{ - use $crate::types::IntoPyDict; - use $crate::conversion::IntoPyObject; - use $crate::BoundObject; - let d = [$((stringify!($val), (&$val).into_pyobject($py).unwrap().into_any().into_bound()),)+].into_py_dict($py).unwrap(); - $crate::py_run_impl!($py, *d, $code) - }}; - ($py:expr, *$dict:expr, $code:expr) => {{ - use ::std::option::Option::*; - #[allow(unused_imports)] - if let ::std::result::Result::Err(e) = $py.run(&::std::ffi::CString::new($code).unwrap(), None, Some(&$dict)) { - e.print($py); - // So when this c api function the last line called printed the error to stderr, - // the output is only written into a buffer which is never flushed because we - // panic before flushing. This is where this hack comes into place - $py.run($crate::ffi::c_str!("import sys; sys.stderr.flush()"), None, None) - .unwrap(); - ::std::panic!("{}", $code) - } - }}; -} - -/// Wraps a Rust function annotated with [`#[pyfunction]`](macro@crate::pyfunction). -/// -/// This can be used with [`PyModule::add_function`](crate::types::PyModuleMethods::add_function) to -/// add free functions to a [`PyModule`](crate::types::PyModule) - see its documentation for more -/// information. -#[macro_export] -macro_rules! wrap_pyfunction { - ($function:path) => { - &|py_or_module| { - use $function as wrapped_pyfunction; - $crate::impl_::pyfunction::WrapPyFunctionArg::wrap_pyfunction( - py_or_module, - &wrapped_pyfunction::_PYO3_DEF, - ) - } - }; - ($function:path, $py_or_module:expr) => {{ - use $function as wrapped_pyfunction; - $crate::impl_::pyfunction::WrapPyFunctionArg::wrap_pyfunction( - $py_or_module, - &wrapped_pyfunction::_PYO3_DEF, - ) - }}; -} - -/// Wraps a Rust function annotated with [`#[pyfunction]`](macro@crate::pyfunction). -/// -/// This can be used with [`PyModule::add_function`](crate::types::PyModuleMethods::add_function) to -/// add free functions to a [`PyModule`](crate::types::PyModule) - see its documentation for more -/// information. -#[deprecated(since = "0.23.0", note = "renamed to `wrap_pyfunction!`")] -#[macro_export] -macro_rules! wrap_pyfunction_bound { - ($function:path) => { - $crate::wrap_pyfunction!($function) - }; - ($function:path, $py_or_module:expr) => { - $crate::wrap_pyfunction!($function, $py_or_module) - }; -} - -/// Returns a function that takes a [`Python`](crate::Python) instance and returns a -/// Python module. -/// -/// Use this together with [`#[pymodule]`](crate::pymodule) and -/// [`PyModule::add_wrapped`](crate::types::PyModuleMethods::add_wrapped). -#[macro_export] -macro_rules! wrap_pymodule { - ($module:path) => { - &|py| { - use $module as wrapped_pymodule; - wrapped_pymodule::_PYO3_DEF - .make_module(py, wrapped_pymodule::__PYO3_GIL_USED) - .expect("failed to wrap pymodule") - } - }; -} - -/// Add the module to the initialization table in order to make embedded Python code to use it. -/// Module name is the argument. -/// -/// Use it before [`prepare_freethreaded_python`](crate::prepare_freethreaded_python) and -/// leave feature `auto-initialize` off -#[cfg(not(any(PyPy, GraalPy)))] -#[macro_export] -macro_rules! append_to_inittab { - ($module:ident) => { - unsafe { - if $crate::ffi::Py_IsInitialized() != 0 { - ::std::panic!( - "called `append_to_inittab` but a Python interpreter is already running." - ); - } - $crate::ffi::PyImport_AppendInittab( - $module::__PYO3_NAME.as_ptr(), - ::std::option::Option::Some($module::__pyo3_init), - ); - } - }; -} diff --git a/vendor/pyo3/src/marker.rs b/vendor/pyo3/src/marker.rs deleted file mode 100644 index 5962b47b60..0000000000 --- a/vendor/pyo3/src/marker.rs +++ /dev/null @@ -1,1021 +0,0 @@ -//! Fundamental properties of objects tied to the Python interpreter. -//! -//! The Python interpreter is not thread-safe. To protect the Python interpreter in multithreaded -//! scenarios there is a global lock, the *global interpreter lock* (hereafter referred to as *GIL*) -//! that must be held to safely interact with Python objects. This is why in PyO3 when you acquire -//! the GIL you get a [`Python`] marker token that carries the *lifetime* of holding the GIL and all -//! borrowed references to Python objects carry this lifetime as well. This will statically ensure -//! that you can never use Python objects after dropping the lock - if you mess this up it will be -//! caught at compile time and your program will fail to compile. -//! -//! It also supports this pattern that many extension modules employ: -//! - Drop the GIL, so that other Python threads can acquire it and make progress themselves -//! - Do something independently of the Python interpreter, like IO, a long running calculation or -//! awaiting a future -//! - Once that is done, reacquire the GIL -//! -//! That API is provided by [`Python::allow_threads`] and enforced via the [`Ungil`] bound on the -//! closure and the return type. This is done by relying on the [`Send`] auto trait. `Ungil` is -//! defined as the following: -//! -//! ```rust -//! # #![allow(dead_code)] -//! pub unsafe trait Ungil {} -//! -//! unsafe impl Ungil for T {} -//! ``` -//! -//! We piggy-back off the `Send` auto trait because it is not possible to implement custom auto -//! traits on stable Rust. This is the solution which enables it for as many types as possible while -//! making the API usable. -//! -//! In practice this API works quite well, but it comes with some drawbacks: -//! -//! ## Drawbacks -//! -//! There is no reason to prevent `!Send` types like [`Rc`] from crossing the closure. After all, -//! [`Python::allow_threads`] just lets other Python threads run - it does not itself launch a new -//! thread. -//! -//! ```rust, compile_fail -//! # #[cfg(feature = "nightly")] -//! # compile_error!("this actually works on nightly") -//! use pyo3::prelude::*; -//! use std::rc::Rc; -//! -//! fn main() { -//! Python::with_gil(|py| { -//! let rc = Rc::new(5); -//! -//! py.allow_threads(|| { -//! // This would actually be fine... -//! println!("{:?}", *rc); -//! }); -//! }); -//! } -//! ``` -//! -//! Because we are using `Send` for something it's not quite meant for, other code that -//! (correctly) upholds the invariants of [`Send`] can cause problems. -//! -//! [`SendWrapper`] is one of those. Per its documentation: -//! -//! > A wrapper which allows you to move around non-Send-types between threads, as long as you -//! > access the contained value only from within the original thread and make sure that it is -//! > dropped from within the original thread. -//! -//! This will "work" to smuggle Python references across the closure, because we're not actually -//! doing anything with threads: -//! -//! ```rust, no_run -//! use pyo3::prelude::*; -//! use pyo3::types::PyString; -//! use send_wrapper::SendWrapper; -//! -//! Python::with_gil(|py| { -//! let string = PyString::new(py, "foo"); -//! -//! let wrapped = SendWrapper::new(string); -//! -//! py.allow_threads(|| { -//! # #[cfg(not(feature = "nightly"))] -//! # { -//! // 💥 Unsound! 💥 -//! let smuggled: &Bound<'_, PyString> = &*wrapped; -//! println!("{:?}", smuggled); -//! # } -//! }); -//! }); -//! ``` -//! -//! For now the answer to that is "don't do that". -//! -//! # A proper implementation using an auto trait -//! -//! However on nightly Rust and when PyO3's `nightly` feature is -//! enabled, `Ungil` is defined as the following: -//! -//! ```rust -//! # #[cfg(any())] -//! # { -//! #![feature(auto_traits, negative_impls)] -//! -//! pub unsafe auto trait Ungil {} -//! -//! // It is unimplemented for the `Python` struct and Python objects. -//! impl !Ungil for Python<'_> {} -//! impl !Ungil for ffi::PyObject {} -//! -//! // `Py` wraps it in a safe api, so this is OK -//! unsafe impl Ungil for Py {} -//! # } -//! ``` -//! -//! With this feature enabled, the above two examples will start working and not working, respectively. -//! -//! [`SendWrapper`]: https://docs.rs/send_wrapper/latest/send_wrapper/struct.SendWrapper.html -//! [`Rc`]: std::rc::Rc -//! [`Py`]: crate::Py -use crate::conversion::IntoPyObject; -#[cfg(any(doc, not(Py_3_10)))] -use crate::err::PyErr; -use crate::err::{self, PyResult}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::gil::{GILGuard, SuspendGIL}; -use crate::impl_::not_send::NotSend; -use crate::py_result_ext::PyResultExt; -use crate::types::any::PyAnyMethods; -use crate::types::{ - PyAny, PyDict, PyEllipsis, PyModule, PyNone, PyNotImplemented, PyString, PyType, -}; -use crate::version::PythonVersionInfo; -#[allow(deprecated)] -use crate::IntoPy; -use crate::{ffi, Bound, Py, PyObject, PyTypeInfo}; -use std::ffi::{CStr, CString}; -use std::marker::PhantomData; -use std::os::raw::c_int; - -/// Types that are safe to access while the GIL is not held. -/// -/// # Safety -/// -/// The type must not carry borrowed Python references or, if it does, not allow access to them if -/// the GIL is not held. -/// -/// See the [module-level documentation](self) for more information. -/// -/// # Examples -/// -/// This tracking is currently imprecise as it relies on the [`Send`] auto trait on stable Rust. -/// For example, an `Rc` smart pointer should be usable without the GIL, but we currently prevent that: -/// -/// ```compile_fail -/// # use pyo3::prelude::*; -/// use std::rc::Rc; -/// -/// Python::with_gil(|py| { -/// let rc = Rc::new(42); -/// -/// py.allow_threads(|| { -/// println!("{:?}", rc); -/// }); -/// }); -/// ``` -/// -/// This also implies that the interplay between `with_gil` and `allow_threads` is unsound, for example -/// one can circumvent this protection using the [`send_wrapper`](https://docs.rs/send_wrapper/) crate: -/// -/// ```no_run -/// # use pyo3::prelude::*; -/// # use pyo3::types::PyString; -/// use send_wrapper::SendWrapper; -/// -/// Python::with_gil(|py| { -/// let string = PyString::new(py, "foo"); -/// -/// let wrapped = SendWrapper::new(string); -/// -/// py.allow_threads(|| { -/// let sneaky: &Bound<'_, PyString> = &*wrapped; -/// -/// println!("{:?}", sneaky); -/// }); -/// }); -/// ``` -/// -/// Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using -/// nightly Rust and the `nightly` feature, c.f. [#2141](https://github.com/PyO3/pyo3/issues/2141). -#[cfg_attr(docsrs, doc(cfg(all())))] // Hide the cfg flag -#[cfg(not(feature = "nightly"))] -pub unsafe trait Ungil {} - -#[cfg_attr(docsrs, doc(cfg(all())))] // Hide the cfg flag -#[cfg(not(feature = "nightly"))] -unsafe impl Ungil for T {} - -#[cfg(feature = "nightly")] -mod nightly { - macro_rules! define { - ($($tt:tt)*) => { $($tt)* } - } - - define! { - /// Types that are safe to access while the GIL is not held. - /// - /// # Safety - /// - /// The type must not carry borrowed Python references or, if it does, not allow access to them if - /// the GIL is not held. - /// - /// See the [module-level documentation](self) for more information. - /// - /// # Examples - /// - /// Types which are `Ungil` cannot be used in contexts where the GIL was released, e.g. - /// - /// ```compile_fail - /// # use pyo3::prelude::*; - /// # use pyo3::types::PyString; - /// Python::with_gil(|py| { - /// let string = PyString::new_bound(py, "foo"); - /// - /// py.allow_threads(|| { - /// println!("{:?}", string); - /// }); - /// }); - /// ``` - /// - /// This applies to the GIL token `Python` itself as well, e.g. - /// - /// ```compile_fail - /// # use pyo3::prelude::*; - /// Python::with_gil(|py| { - /// py.allow_threads(|| { - /// drop(py); - /// }); - /// }); - /// ``` - /// - /// On nightly Rust, this is not based on the [`Send`] auto trait and hence we are able - /// to prevent incorrectly circumventing it using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate: - /// - /// ```compile_fail - /// # use pyo3::prelude::*; - /// # use pyo3::types::PyString; - /// use send_wrapper::SendWrapper; - /// - /// Python::with_gil(|py| { - /// let string = PyString::new_bound(py, "foo"); - /// - /// let wrapped = SendWrapper::new(string); - /// - /// py.allow_threads(|| { - /// let sneaky: &PyString = *wrapped; - /// - /// println!("{:?}", sneaky); - /// }); - /// }); - /// ``` - /// - /// This also enables using non-[`Send`] types in `allow_threads`, - /// at least if they are not also bound to the GIL: - /// - /// ```rust - /// # use pyo3::prelude::*; - /// use std::rc::Rc; - /// - /// Python::with_gil(|py| { - /// let rc = Rc::new(42); - /// - /// py.allow_threads(|| { - /// println!("{:?}", rc); - /// }); - /// }); - /// ``` - pub unsafe auto trait Ungil {} - } - - impl !Ungil for crate::Python<'_> {} - - // This means that PyString, PyList, etc all inherit !Ungil from this. - impl !Ungil for crate::PyAny {} - - impl !Ungil for crate::PyRef<'_, T> {} - impl !Ungil for crate::PyRefMut<'_, T> {} - - // FFI pointees - impl !Ungil for crate::ffi::PyObject {} - impl !Ungil for crate::ffi::PyLongObject {} - - impl !Ungil for crate::ffi::PyThreadState {} - impl !Ungil for crate::ffi::PyInterpreterState {} - impl !Ungil for crate::ffi::PyWeakReference {} - impl !Ungil for crate::ffi::PyFrameObject {} - impl !Ungil for crate::ffi::PyCodeObject {} - #[cfg(not(Py_LIMITED_API))] - impl !Ungil for crate::ffi::PyDictKeysObject {} - #[cfg(not(any(Py_LIMITED_API, Py_3_10)))] - impl !Ungil for crate::ffi::PyArena {} -} - -#[cfg(feature = "nightly")] -pub use nightly::Ungil; - -/// A marker token that represents holding the GIL. -/// -/// It serves three main purposes: -/// - It provides a global API for the Python interpreter, such as [`Python::eval_bound`]. -/// - It can be passed to functions that require a proof of holding the GIL, such as -/// [`Py::clone_ref`]. -/// - Its lifetime represents the scope of holding the GIL which can be used to create Rust -/// references that are bound to it, such as [`Bound<'py, PyAny>`]. -/// -/// Note that there are some caveats to using it that you might need to be aware of. See the -/// [Deadlocks](#deadlocks) and [Releasing and freeing memory](#releasing-and-freeing-memory) -/// paragraphs for more information about that. -/// -/// # Obtaining a Python token -/// -/// The following are the recommended ways to obtain a [`Python<'py>`] token, in order of preference: -/// - If you already have something with a lifetime bound to the GIL, such as [`Bound<'py, PyAny>`], you can -/// use its `.py()` method to get a token. -/// - In a function or method annotated with [`#[pyfunction]`](crate::pyfunction) or [`#[pymethods]`](crate::pymethods) you can declare it -/// as a parameter, and PyO3 will pass in the token when Python code calls it. -/// - When you need to acquire the GIL yourself, such as when calling Python code from Rust, you -/// should call [`Python::with_gil`] to do that and pass your code as a closure to it. -/// -/// The first two options are zero-cost; [`Python::with_gil`] requires runtime checking and may need to block -/// to acquire the GIL. -/// -/// # Deadlocks -/// -/// Note that the GIL can be temporarily released by the Python interpreter during a function call -/// (e.g. importing a module). In general, you don't need to worry about this because the GIL is -/// reacquired before returning to the Rust code: -/// -/// ```text -/// `Python` exists |=====================================| -/// GIL actually held |==========| |================| -/// Rust code running |=======| |==| |======| -/// ``` -/// -/// This behaviour can cause deadlocks when trying to lock a Rust mutex while holding the GIL: -/// -/// * Thread 1 acquires the GIL -/// * Thread 1 locks a mutex -/// * Thread 1 makes a call into the Python interpreter which releases the GIL -/// * Thread 2 acquires the GIL -/// * Thread 2 tries to locks the mutex, blocks -/// * Thread 1's Python interpreter call blocks trying to reacquire the GIL held by thread 2 -/// -/// To avoid deadlocking, you should release the GIL before trying to lock a mutex or `await`ing in -/// asynchronous code, e.g. with [`Python::allow_threads`]. -/// -/// # Releasing and freeing memory -/// -/// The [`Python<'py>`] type can be used to create references to variables owned by the Python -/// interpreter, using functions such as [`Python::eval_bound`] and [`PyModule::import`]. -#[derive(Copy, Clone)] -pub struct Python<'py>(PhantomData<(&'py GILGuard, NotSend)>); - -impl Python<'_> { - /// Acquires the global interpreter lock, allowing access to the Python interpreter. The - /// provided closure `F` will be executed with the acquired `Python` marker token. - /// - /// If implementing [`#[pymethods]`](crate::pymethods) or [`#[pyfunction]`](crate::pyfunction), - /// declare `py: Python` as an argument. PyO3 will pass in the token to grant access to the GIL - /// context in which the function is running, avoiding the need to call `with_gil`. - /// - /// If the [`auto-initialize`] feature is enabled and the Python runtime is not already - /// initialized, this function will initialize it. See - #[cfg_attr( - not(any(PyPy, GraalPy)), - doc = "[`prepare_freethreaded_python`](crate::prepare_freethreaded_python)" - )] - #[cfg_attr(PyPy, doc = "`prepare_freethreaded_python`")] - /// for details. - /// - /// If the current thread does not yet have a Python "thread state" associated with it, - /// a new one will be automatically created before `F` is executed and destroyed after `F` - /// completes. - /// - /// # Panics - /// - /// - If the [`auto-initialize`] feature is not enabled and the Python interpreter is not - /// initialized. - /// - /// # Examples - /// - /// ``` - /// use pyo3::prelude::*; - /// use pyo3::ffi::c_str; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let x: i32 = py.eval(c_str!("5"), None, None)?.extract()?; - /// assert_eq!(x, 5); - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// [`auto-initialize`]: https://pyo3.rs/main/features.html#auto-initialize - #[inline] - pub fn with_gil(f: F) -> R - where - F: for<'py> FnOnce(Python<'py>) -> R, - { - let guard = GILGuard::acquire(); - - // SAFETY: Either the GIL was already acquired or we just created a new `GILGuard`. - f(guard.python()) - } - - /// Like [`Python::with_gil`] except Python interpreter state checking is skipped. - /// - /// Normally when the GIL is acquired, we check that the Python interpreter is an - /// appropriate state (e.g. it is fully initialized). This function skips those - /// checks. - /// - /// # Safety - /// - /// If [`Python::with_gil`] would succeed, it is safe to call this function. - /// - /// In most cases, you should use [`Python::with_gil`]. - /// - /// A justified scenario for calling this function is during multi-phase interpreter - /// initialization when [`Python::with_gil`] would fail before - // this link is only valid on 3.8+not pypy and up. - #[cfg_attr( - all(Py_3_8, not(PyPy)), - doc = "[`_Py_InitializeMain`](crate::ffi::_Py_InitializeMain)" - )] - #[cfg_attr(any(not(Py_3_8), PyPy), doc = "`_Py_InitializeMain`")] - /// is called because the interpreter is only partially initialized. - /// - /// Behavior in other scenarios is not documented. - #[inline] - pub unsafe fn with_gil_unchecked(f: F) -> R - where - F: for<'py> FnOnce(Python<'py>) -> R, - { - let guard = GILGuard::acquire_unchecked(); - - f(guard.python()) - } -} - -impl<'py> Python<'py> { - /// Temporarily releases the GIL, thus allowing other Python threads to run. The GIL will be - /// reacquired when `F`'s scope ends. - /// - /// If you don't need to touch the Python - /// interpreter for some time and have other Python threads around, this will let you run - /// Rust-only code while letting those other Python threads make progress. - /// - /// Only types that implement [`Ungil`] can cross the closure. See the - /// [module level documentation](self) for more information. - /// - /// If you need to pass Python objects into the closure you can use [`Py`]``to create a - /// reference independent of the GIL lifetime. However, you cannot do much with those without a - /// [`Python`] token, for which you'd need to reacquire the GIL. - /// - /// # Example: Releasing the GIL while running a computation in Rust-only code - /// - /// ``` - /// use pyo3::prelude::*; - /// - /// #[pyfunction] - /// fn sum_numbers(py: Python<'_>, numbers: Vec) -> PyResult { - /// // We release the GIL here so any other Python threads get a chance to run. - /// py.allow_threads(move || { - /// // An example of an "expensive" Rust calculation - /// let sum = numbers.iter().sum(); - /// - /// Ok(sum) - /// }) - /// } - /// # - /// # fn main() -> PyResult<()> { - /// # Python::with_gil(|py| -> PyResult<()> { - /// # let fun = pyo3::wrap_pyfunction!(sum_numbers, py)?; - /// # let res = fun.call1((vec![1_u32, 2, 3],))?; - /// # assert_eq!(res.extract::()?, 6_u32); - /// # Ok(()) - /// # }) - /// # } - /// ``` - /// - /// Please see the [Parallelism] chapter of the guide for a thorough discussion of using - /// [`Python::allow_threads`] in this manner. - /// - /// # Example: Passing borrowed Python references into the closure is not allowed - /// - /// ```compile_fail - /// use pyo3::prelude::*; - /// use pyo3::types::PyString; - /// - /// fn parallel_print(py: Python<'_>) { - /// let s = PyString::new_bound(py, "This object cannot be accessed without holding the GIL >_<"); - /// py.allow_threads(move || { - /// println!("{:?}", s); // This causes a compile error. - /// }); - /// } - /// ``` - /// - /// [`Py`]: crate::Py - /// [`PyString`]: crate::types::PyString - /// [auto-traits]: https://doc.rust-lang.org/nightly/unstable-book/language-features/auto-traits.html - /// [Parallelism]: https://pyo3.rs/main/parallelism.html - pub fn allow_threads(self, f: F) -> T - where - F: Ungil + FnOnce() -> T, - T: Ungil, - { - // Use a guard pattern to handle reacquiring the GIL, - // so that the GIL will be reacquired even if `f` panics. - // The `Send` bound on the closure prevents the user from - // transferring the `Python` token into the closure. - let _guard = unsafe { SuspendGIL::new() }; - f() - } - - /// Evaluates a Python expression in the given context and returns the result. - /// - /// If `globals` is `None`, it defaults to Python module `__main__`. - /// If `locals` is `None`, it defaults to the value of `globals`. - /// - /// If `globals` doesn't contain `__builtins__`, default `__builtins__` - /// will be added automatically. - /// - /// # Examples - /// - /// ``` - /// # use pyo3::prelude::*; - /// # use pyo3::ffi::c_str; - /// # Python::with_gil(|py| { - /// let result = py.eval(c_str!("[i * 10 for i in range(5)]"), None, None).unwrap(); - /// let res: Vec = result.extract().unwrap(); - /// assert_eq!(res, vec![0, 10, 20, 30, 40]) - /// # }); - /// ``` - pub fn eval( - self, - code: &CStr, - globals: Option<&Bound<'py, PyDict>>, - locals: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - self.run_code(code, ffi::Py_eval_input, globals, locals) - } - - /// Deprecated name for [`Python::eval`]. - #[deprecated(since = "0.23.0", note = "renamed to `Python::eval`")] - #[track_caller] - #[inline] - pub fn eval_bound( - self, - code: &str, - globals: Option<&Bound<'py, PyDict>>, - locals: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - let code = CString::new(code)?; - self.eval(&code, globals, locals) - } - - /// Executes one or more Python statements in the given context. - /// - /// If `globals` is `None`, it defaults to Python module `__main__`. - /// If `locals` is `None`, it defaults to the value of `globals`. - /// - /// If `globals` doesn't contain `__builtins__`, default `__builtins__` - /// will be added automatically. - /// - /// # Examples - /// ``` - /// use pyo3::{ - /// prelude::*, - /// types::{PyBytes, PyDict}, - /// ffi::c_str, - /// }; - /// Python::with_gil(|py| { - /// let locals = PyDict::new(py); - /// py.run(c_str!( - /// r#" - /// import base64 - /// s = 'Hello Rust!' - /// ret = base64.b64encode(s.encode('utf-8')) - /// "#), - /// None, - /// Some(&locals), - /// ) - /// .unwrap(); - /// let ret = locals.get_item("ret").unwrap().unwrap(); - /// let b64 = ret.downcast::().unwrap(); - /// assert_eq!(b64.as_bytes(), b"SGVsbG8gUnVzdCE="); - /// }); - /// ``` - /// - /// You can use [`py_run!`](macro.py_run.html) for a handy alternative of `run` - /// if you don't need `globals` and unwrapping is OK. - pub fn run( - self, - code: &CStr, - globals: Option<&Bound<'py, PyDict>>, - locals: Option<&Bound<'py, PyDict>>, - ) -> PyResult<()> { - let res = self.run_code(code, ffi::Py_file_input, globals, locals); - res.map(|obj| { - debug_assert!(obj.is_none()); - }) - } - - /// Deprecated name for [`Python::run`]. - #[deprecated(since = "0.23.0", note = "renamed to `Python::run`")] - #[track_caller] - #[inline] - pub fn run_bound( - self, - code: &str, - globals: Option<&Bound<'py, PyDict>>, - locals: Option<&Bound<'py, PyDict>>, - ) -> PyResult<()> { - let code = CString::new(code)?; - self.run(&code, globals, locals) - } - - /// Runs code in the given context. - /// - /// `start` indicates the type of input expected: one of `Py_single_input`, - /// `Py_file_input`, or `Py_eval_input`. - /// - /// If `globals` is `None`, it defaults to Python module `__main__`. - /// If `locals` is `None`, it defaults to the value of `globals`. - fn run_code( - self, - code: &CStr, - start: c_int, - globals: Option<&Bound<'py, PyDict>>, - locals: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - let mptr = unsafe { - ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr()) - .assume_owned_or_err(self)? - }; - let attr = mptr.getattr(crate::intern!(self, "__dict__"))?; - let globals = match globals { - Some(globals) => globals, - None => attr.downcast::()?, - }; - let locals = locals.unwrap_or(globals); - - #[cfg(not(Py_3_10))] - { - // If `globals` don't provide `__builtins__`, most of the code will fail if Python - // version is <3.10. That's probably not what user intended, so insert `__builtins__` - // for them. - // - // See also: - // - https://github.com/python/cpython/pull/24564 (the same fix in CPython 3.10) - // - https://github.com/PyO3/pyo3/issues/3370 - let builtins_s = crate::intern!(self, "__builtins__").as_ptr(); - let has_builtins = unsafe { ffi::PyDict_Contains(globals.as_ptr(), builtins_s) }; - if has_builtins == -1 { - return Err(PyErr::fetch(self)); - } - if has_builtins == 0 { - // Inherit current builtins. - let builtins = unsafe { ffi::PyEval_GetBuiltins() }; - - // `PyDict_SetItem` doesn't take ownership of `builtins`, but `PyEval_GetBuiltins` - // seems to return a borrowed reference, so no leak here. - if unsafe { ffi::PyDict_SetItem(globals.as_ptr(), builtins_s, builtins) } == -1 { - return Err(PyErr::fetch(self)); - } - } - } - - let code_obj = unsafe { - ffi::Py_CompileString(code.as_ptr(), ffi::c_str!("").as_ptr(), start) - .assume_owned_or_err(self)? - }; - - unsafe { - ffi::PyEval_EvalCode(code_obj.as_ptr(), globals.as_ptr(), locals.as_ptr()) - .assume_owned_or_err(self) - .downcast_into_unchecked() - } - } - - /// Gets the Python type object for type `T`. - #[inline] - pub fn get_type(self) -> Bound<'py, PyType> - where - T: PyTypeInfo, - { - T::type_object(self) - } - - /// Deprecated name for [`Python::get_type`]. - #[deprecated(since = "0.23.0", note = "renamed to `Python::get_type`")] - #[track_caller] - #[inline] - pub fn get_type_bound(self) -> Bound<'py, PyType> - where - T: PyTypeInfo, - { - self.get_type::() - } - - /// Imports the Python module with the specified name. - pub fn import(self, name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - { - PyModule::import(self, name) - } - - /// Deprecated name for [`Python::import`]. - #[deprecated(since = "0.23.0", note = "renamed to `Python::import`")] - #[allow(deprecated)] - #[track_caller] - #[inline] - pub fn import_bound(self, name: N) -> PyResult> - where - N: IntoPy>, - { - self.import(name.into_py(self)) - } - - /// Gets the Python builtin value `None`. - #[allow(non_snake_case)] // the Python keyword starts with uppercase - #[inline] - pub fn None(self) -> PyObject { - PyNone::get(self).to_owned().into_any().unbind() - } - - /// Gets the Python builtin value `Ellipsis`, or `...`. - #[allow(non_snake_case)] // the Python keyword starts with uppercase - #[inline] - pub fn Ellipsis(self) -> PyObject { - PyEllipsis::get(self).to_owned().into_any().unbind() - } - - /// Gets the Python builtin value `NotImplemented`. - #[allow(non_snake_case)] // the Python keyword starts with uppercase - #[inline] - pub fn NotImplemented(self) -> PyObject { - PyNotImplemented::get(self).to_owned().into_any().unbind() - } - - /// Gets the running Python interpreter version as a string. - /// - /// # Examples - /// ```rust - /// # use pyo3::Python; - /// Python::with_gil(|py| { - /// // The full string could be, for example: - /// // "3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]" - /// assert!(py.version().starts_with("3.")); - /// }); - /// ``` - pub fn version(self) -> &'py str { - unsafe { - CStr::from_ptr(ffi::Py_GetVersion()) - .to_str() - .expect("Python version string not UTF-8") - } - } - - /// Gets the running Python interpreter version as a struct similar to - /// `sys.version_info`. - /// - /// # Examples - /// ```rust - /// # use pyo3::Python; - /// Python::with_gil(|py| { - /// // PyO3 supports Python 3.7 and up. - /// assert!(py.version_info() >= (3, 7)); - /// assert!(py.version_info() >= (3, 7, 0)); - /// }); - /// ``` - pub fn version_info(self) -> PythonVersionInfo<'py> { - let version_str = self.version(); - - // Portion of the version string returned by Py_GetVersion up to the first space is the - // version number. - let version_number_str = version_str.split(' ').next().unwrap_or(version_str); - - PythonVersionInfo::from_str(version_number_str).unwrap() - } - - /// Lets the Python interpreter check and handle any pending signals. This will invoke the - /// corresponding signal handlers registered in Python (if any). - /// - /// Returns `Err(`[`PyErr`]`)` if any signal handler raises an exception. - /// - /// These signals include `SIGINT` (normally raised by CTRL + C), which by default raises - /// `KeyboardInterrupt`. For this reason it is good practice to call this function regularly - /// as part of long-running Rust functions so that users can cancel it. - /// - /// # Example - /// - /// ```rust - /// # #![allow(dead_code)] // this example is quite impractical to test - /// use pyo3::prelude::*; - /// - /// # fn main() { - /// #[pyfunction] - /// fn loop_forever(py: Python<'_>) -> PyResult<()> { - /// loop { - /// // As this loop is infinite it should check for signals every once in a while. - /// // Using `?` causes any `PyErr` (potentially containing `KeyboardInterrupt`) - /// // to break out of the loop. - /// py.check_signals()?; - /// - /// // do work here - /// # break Ok(()) // don't actually loop forever - /// } - /// } - /// # } - /// ``` - /// - /// # Note - /// - /// This function calls [`PyErr_CheckSignals()`][1] which in turn may call signal handlers. - /// As Python's [`signal`][2] API allows users to define custom signal handlers, calling this - /// function allows arbitrary Python code inside signal handlers to run. - /// - /// If the function is called from a non-main thread, or under a non-main Python interpreter, - /// it does nothing yet still returns `Ok(())`. - /// - /// [1]: https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_checksignals#c.PyErr_CheckSignals - /// [2]: https://docs.python.org/3/library/signal.html - pub fn check_signals(self) -> PyResult<()> { - err::error_on_minusone(self, unsafe { ffi::PyErr_CheckSignals() }) - } -} - -impl<'unbound> Python<'unbound> { - /// Unsafely creates a Python token with an unbounded lifetime. - /// - /// Many of PyO3 APIs use `Python<'_>` as proof that the GIL is held, but this function can be - /// used to call them unsafely. - /// - /// # Safety - /// - /// - This token and any borrowed Python references derived from it can only be safely used - /// whilst the currently executing thread is actually holding the GIL. - /// - This function creates a token with an *unbounded* lifetime. Safe code can assume that - /// holding a `Python<'py>` token means the GIL is and stays acquired for the lifetime `'py`. - /// If you let it or borrowed Python references escape to safe code you are - /// responsible for bounding the lifetime `'unbound` appropriately. For more on unbounded - /// lifetimes, see the [nomicon]. - /// - /// [nomicon]: https://doc.rust-lang.org/nomicon/unbounded-lifetimes.html - #[inline] - pub unsafe fn assume_gil_acquired() -> Python<'unbound> { - Python(PhantomData) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::{IntoPyDict, PyList}; - - #[test] - fn test_eval() { - Python::with_gil(|py| { - // Make sure builtin names are accessible - let v: i32 = py - .eval(ffi::c_str!("min(1, 2)"), None, None) - .map_err(|e| e.display(py)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(v, 1); - - let d = [("foo", 13)].into_py_dict(py).unwrap(); - - // Inject our own global namespace - let v: i32 = py - .eval(ffi::c_str!("foo + 29"), Some(&d), None) - .unwrap() - .extract() - .unwrap(); - assert_eq!(v, 42); - - // Inject our own local namespace - let v: i32 = py - .eval(ffi::c_str!("foo + 29"), None, Some(&d)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(v, 42); - - // Make sure builtin names are still accessible when using a local namespace - let v: i32 = py - .eval(ffi::c_str!("min(foo, 2)"), None, Some(&d)) - .unwrap() - .extract() - .unwrap(); - assert_eq!(v, 2); - }); - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled - fn test_allow_threads_releases_and_acquires_gil() { - Python::with_gil(|py| { - let b = std::sync::Arc::new(std::sync::Barrier::new(2)); - - let b2 = b.clone(); - std::thread::spawn(move || Python::with_gil(|_| b2.wait())); - - py.allow_threads(|| { - // If allow_threads does not release the GIL, this will deadlock because - // the thread spawned above will never be able to acquire the GIL. - b.wait(); - }); - - unsafe { - // If the GIL is not reacquired at the end of allow_threads, this call - // will crash the Python interpreter. - let tstate = ffi::PyEval_SaveThread(); - ffi::PyEval_RestoreThread(tstate); - } - }); - } - - #[test] - fn test_allow_threads_panics_safely() { - Python::with_gil(|py| { - let result = std::panic::catch_unwind(|| unsafe { - let py = Python::assume_gil_acquired(); - py.allow_threads(|| { - panic!("There was a panic!"); - }); - }); - - // Check panic was caught - assert!(result.is_err()); - - // If allow_threads is implemented correctly, this thread still owns the GIL here - // so the following Python calls should not cause crashes. - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - assert_eq!(list.extract::>().unwrap(), vec![1, 2, 3, 4]); - }); - } - - #[cfg(not(pyo3_disable_reference_pool))] - #[test] - fn test_allow_threads_pass_stuff_in() { - let list = Python::with_gil(|py| PyList::new(py, vec!["foo", "bar"]).unwrap().unbind()); - let mut v = vec![1, 2, 3]; - let a = std::sync::Arc::new(String::from("foo")); - - Python::with_gil(|py| { - py.allow_threads(|| { - drop((list, &mut v, a)); - }); - }); - } - - #[test] - #[cfg(not(Py_LIMITED_API))] - fn test_acquire_gil() { - const GIL_NOT_HELD: c_int = 0; - const GIL_HELD: c_int = 1; - - // Before starting the interpreter the state of calling `PyGILState_Check` - // seems to be undefined, so let's ensure that Python is up. - #[cfg(not(any(PyPy, GraalPy)))] - crate::prepare_freethreaded_python(); - - let state = unsafe { crate::ffi::PyGILState_Check() }; - assert_eq!(state, GIL_NOT_HELD); - - Python::with_gil(|_| { - let state = unsafe { crate::ffi::PyGILState_Check() }; - assert_eq!(state, GIL_HELD); - }); - - let state = unsafe { crate::ffi::PyGILState_Check() }; - assert_eq!(state, GIL_NOT_HELD); - } - - #[test] - fn test_ellipsis() { - Python::with_gil(|py| { - assert_eq!(py.Ellipsis().to_string(), "Ellipsis"); - - let v = py - .eval(ffi::c_str!("..."), None, None) - .map_err(|e| e.display(py)) - .unwrap(); - - assert!(v.eq(py.Ellipsis()).unwrap()); - }); - } - - #[test] - fn test_py_run_inserts_globals() { - use crate::types::dict::PyDictMethods; - - Python::with_gil(|py| { - let namespace = PyDict::new(py); - py.run( - ffi::c_str!("class Foo: pass\na = int(3)"), - Some(&namespace), - Some(&namespace), - ) - .unwrap(); - assert!(matches!(namespace.get_item("Foo"), Ok(Some(..)))); - assert!(matches!(namespace.get_item("a"), Ok(Some(..)))); - // 3.9 and older did not automatically insert __builtins__ if it wasn't inserted "by hand" - #[cfg(not(Py_3_10))] - assert!(matches!(namespace.get_item("__builtins__"), Ok(Some(..)))); - }) - } -} diff --git a/vendor/pyo3/src/marshal.rs b/vendor/pyo3/src/marshal.rs deleted file mode 100644 index d541da7d69..0000000000 --- a/vendor/pyo3/src/marshal.rs +++ /dev/null @@ -1,93 +0,0 @@ -#![cfg(not(Py_LIMITED_API))] - -//! Support for the Python `marshal` format. - -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::types::{PyAny, PyBytes}; -use crate::{ffi, Bound}; -use crate::{PyResult, Python}; -use std::os::raw::c_int; - -/// The current version of the marshal binary format. -pub const VERSION: i32 = 4; - -/// Serialize an object to bytes using the Python built-in marshal module. -/// -/// The built-in marshalling only supports a limited range of objects. -/// The exact types supported depend on the version argument. -/// The [`VERSION`] constant holds the highest version currently supported. -/// -/// See the [Python documentation](https://docs.python.org/3/library/marshal.html) for more details. -/// -/// # Examples -/// ``` -/// # use pyo3::{marshal, types::PyDict, prelude::PyDictMethods}; -/// # pyo3::Python::with_gil(|py| { -/// let dict = PyDict::new(py); -/// dict.set_item("aap", "noot").unwrap(); -/// dict.set_item("mies", "wim").unwrap(); -/// dict.set_item("zus", "jet").unwrap(); -/// -/// let bytes = marshal::dumps(&dict, marshal::VERSION); -/// # }); -/// ``` -pub fn dumps<'py>(object: &Bound<'py, PyAny>, version: i32) -> PyResult> { - unsafe { - ffi::PyMarshal_WriteObjectToString(object.as_ptr(), version as c_int) - .assume_owned_or_err(object.py()) - .downcast_into_unchecked() - } -} - -/// Deprecated form of [`dumps`]. -#[deprecated(since = "0.23.0", note = "use `dumps` instead")] -pub fn dumps_bound<'py>( - py: Python<'py>, - object: &impl crate::AsPyPointer, - version: i32, -) -> PyResult> { - dumps( - unsafe { object.as_ptr().assume_borrowed(py) }.as_any(), - version, - ) -} - -/// Deserialize an object from bytes using the Python built-in marshal module. -pub fn loads<'py, B>(py: Python<'py>, data: &B) -> PyResult> -where - B: AsRef<[u8]> + ?Sized, -{ - let data = data.as_ref(); - unsafe { - ffi::PyMarshal_ReadObjectFromString(data.as_ptr().cast(), data.len() as isize) - .assume_owned_or_err(py) - } -} - -/// Deprecated form of [`loads`]. -#[deprecated(since = "0.23.0", note = "renamed to `loads`")] -pub fn loads_bound<'py>(py: Python<'py>, data: &[u8]) -> PyResult> { - loads(py, data) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::{bytes::PyBytesMethods, dict::PyDictMethods, PyAnyMethods, PyDict}; - - #[test] - fn marshal_roundtrip() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - dict.set_item("aap", "noot").unwrap(); - dict.set_item("mies", "wim").unwrap(); - dict.set_item("zus", "jet").unwrap(); - - let pybytes = dumps(&dict, VERSION).expect("marshalling failed"); - let deserialized = loads(py, pybytes.as_bytes()).expect("unmarshalling failed"); - - assert!(dict.eq(&deserialized).unwrap()); - }); - } -} diff --git a/vendor/pyo3/src/panic.rs b/vendor/pyo3/src/panic.rs deleted file mode 100644 index 50489d50ae..0000000000 --- a/vendor/pyo3/src/panic.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Helper to convert Rust panics to Python exceptions. -use crate::exceptions::PyBaseException; -use crate::PyErr; -use std::any::Any; - -pyo3_exception!( - " -The exception raised when Rust code called from Python panics. - -Like SystemExit, this exception is derived from BaseException so that -it will typically propagate all the way through the stack and cause the -Python interpreter to exit. -", - PanicException, - PyBaseException -); - -impl PanicException { - /// Creates a new PanicException from a panic payload. - /// - /// Attempts to format the error in the same way panic does. - #[cold] - pub(crate) fn from_panic_payload(payload: Box) -> PyErr { - if let Some(string) = payload.downcast_ref::() { - Self::new_err((string.clone(),)) - } else if let Some(s) = payload.downcast_ref::<&str>() { - Self::new_err((s.to_string(),)) - } else { - Self::new_err(("panic from Rust code",)) - } - } -} diff --git a/vendor/pyo3/src/prelude.rs b/vendor/pyo3/src/prelude.rs deleted file mode 100644 index d4f649f552..0000000000 --- a/vendor/pyo3/src/prelude.rs +++ /dev/null @@ -1,52 +0,0 @@ -//! PyO3's prelude. -//! -//! The purpose of this module is to alleviate imports of many commonly used items of the PyO3 crate -//! by adding a glob import to the top of pyo3 heavy modules: -//! -//! ``` -//! # #![allow(unused_imports)] -//! use pyo3::prelude::*; -//! ``` - -pub use crate::conversion::{FromPyObject, IntoPyObject}; -#[allow(deprecated)] -pub use crate::conversion::{IntoPy, ToPyObject}; -pub use crate::err::{PyErr, PyResult}; -pub use crate::instance::{Borrowed, Bound, Py, PyObject}; -pub use crate::marker::Python; -pub use crate::pycell::{PyRef, PyRefMut}; -pub use crate::pyclass_init::PyClassInitializer; -pub use crate::types::{PyAny, PyModule}; - -#[cfg(feature = "macros")] -pub use pyo3_macros::{ - pyclass, pyfunction, pymethods, pymodule, FromPyObject, IntoPyObject, IntoPyObjectRef, -}; - -#[cfg(feature = "macros")] -pub use crate::wrap_pyfunction; -#[cfg(feature = "macros")] -#[allow(deprecated)] -pub use crate::wrap_pyfunction_bound; - -pub use crate::types::any::PyAnyMethods; -pub use crate::types::boolobject::PyBoolMethods; -pub use crate::types::bytearray::PyByteArrayMethods; -pub use crate::types::bytes::PyBytesMethods; -pub use crate::types::capsule::PyCapsuleMethods; -pub use crate::types::complex::PyComplexMethods; -pub use crate::types::dict::PyDictMethods; -pub use crate::types::float::PyFloatMethods; -pub use crate::types::frozenset::PyFrozenSetMethods; -pub use crate::types::list::PyListMethods; -pub use crate::types::mapping::PyMappingMethods; -pub use crate::types::mappingproxy::PyMappingProxyMethods; -pub use crate::types::module::PyModuleMethods; -pub use crate::types::sequence::PySequenceMethods; -pub use crate::types::set::PySetMethods; -pub use crate::types::slice::PySliceMethods; -pub use crate::types::string::PyStringMethods; -pub use crate::types::traceback::PyTracebackMethods; -pub use crate::types::tuple::PyTupleMethods; -pub use crate::types::typeobject::PyTypeMethods; -pub use crate::types::weakref::PyWeakrefMethods; diff --git a/vendor/pyo3/src/py_result_ext.rs b/vendor/pyo3/src/py_result_ext.rs deleted file mode 100644 index 2ad079ed7a..0000000000 --- a/vendor/pyo3/src/py_result_ext.rs +++ /dev/null @@ -1,18 +0,0 @@ -use crate::{types::any::PyAnyMethods, Bound, PyAny, PyResult, PyTypeCheck}; - -pub(crate) trait PyResultExt<'py>: crate::sealed::Sealed { - fn downcast_into(self) -> PyResult>; - unsafe fn downcast_into_unchecked(self) -> PyResult>; -} - -impl<'py> PyResultExt<'py> for PyResult> { - #[inline] - fn downcast_into(self) -> PyResult> where { - self.and_then(|instance| instance.downcast_into().map_err(Into::into)) - } - - #[inline] - unsafe fn downcast_into_unchecked(self) -> PyResult> { - self.map(|instance| instance.downcast_into_unchecked()) - } -} diff --git a/vendor/pyo3/src/pybacked.rs b/vendor/pyo3/src/pybacked.rs deleted file mode 100644 index 173d8e0e9e..0000000000 --- a/vendor/pyo3/src/pybacked.rs +++ /dev/null @@ -1,648 +0,0 @@ -//! Contains types for working with Python objects that own the underlying data. - -use std::{convert::Infallible, ops::Deref, ptr::NonNull, sync::Arc}; - -use crate::{ - types::{ - any::PyAnyMethods, bytearray::PyByteArrayMethods, bytes::PyBytesMethods, - string::PyStringMethods, PyByteArray, PyBytes, PyString, - }, - Bound, DowncastError, FromPyObject, IntoPyObject, Py, PyAny, PyErr, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -/// A wrapper around `str` where the storage is owned by a Python `bytes` or `str` object. -/// -/// This type gives access to the underlying data via a `Deref` implementation. -#[cfg_attr(feature = "py-clone", derive(Clone))] -pub struct PyBackedStr { - #[allow(dead_code)] // only held so that the storage is not dropped - storage: Py, - data: NonNull, -} - -impl Deref for PyBackedStr { - type Target = str; - fn deref(&self) -> &str { - // Safety: `data` is known to be immutable and owned by self - unsafe { self.data.as_ref() } - } -} - -impl AsRef for PyBackedStr { - fn as_ref(&self) -> &str { - self - } -} - -impl AsRef<[u8]> for PyBackedStr { - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} - -// Safety: the underlying Python str (or bytes) is immutable and -// safe to share between threads -unsafe impl Send for PyBackedStr {} -unsafe impl Sync for PyBackedStr {} - -impl std::fmt::Display for PyBackedStr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.deref().fmt(f) - } -} - -impl_traits!(PyBackedStr, str); - -impl TryFrom> for PyBackedStr { - type Error = PyErr; - fn try_from(py_string: Bound<'_, PyString>) -> Result { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - { - let s = py_string.to_str()?; - let data = NonNull::from(s); - Ok(Self { - storage: py_string.into_any().unbind(), - data, - }) - } - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - { - let bytes = py_string.encode_utf8()?; - let s = unsafe { std::str::from_utf8_unchecked(bytes.as_bytes()) }; - let data = NonNull::from(s); - Ok(Self { - storage: bytes.into_any().unbind(), - data, - }) - } - } -} - -impl FromPyObject<'_> for PyBackedStr { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let py_string = obj.downcast::()?.to_owned(); - Self::try_from(py_string) - } -} - -#[allow(deprecated)] -impl ToPyObject for PyBackedStr { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn to_object(&self, py: Python<'_>) -> Py { - self.storage.as_any().clone_ref(py) - } - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - fn to_object(&self, py: Python<'_>) -> Py { - PyString::new(py, self).into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy> for PyBackedStr { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn into_py(self, _py: Python<'_>) -> Py { - self.storage.into_any() - } - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - fn into_py(self, py: Python<'_>) -> Py { - PyString::new(py, &self).into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for PyBackedStr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.storage.into_bound(py)) - } - - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, &self).into_any()) - } -} - -impl<'py> IntoPyObject<'py> for &PyBackedStr { - type Target = PyAny; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(self.storage.bind(py).to_owned()) - } - - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyString::new(py, self).into_any()) - } -} - -/// A wrapper around `[u8]` where the storage is either owned by a Python `bytes` object, or a Rust `Box<[u8]>`. -/// -/// This type gives access to the underlying data via a `Deref` implementation. -#[cfg_attr(feature = "py-clone", derive(Clone))] -pub struct PyBackedBytes { - #[allow(dead_code)] // only held so that the storage is not dropped - storage: PyBackedBytesStorage, - data: NonNull<[u8]>, -} - -#[allow(dead_code)] -#[cfg_attr(feature = "py-clone", derive(Clone))] -enum PyBackedBytesStorage { - Python(Py), - Rust(Arc<[u8]>), -} - -impl Deref for PyBackedBytes { - type Target = [u8]; - fn deref(&self) -> &[u8] { - // Safety: `data` is known to be immutable and owned by self - unsafe { self.data.as_ref() } - } -} - -impl AsRef<[u8]> for PyBackedBytes { - fn as_ref(&self) -> &[u8] { - self - } -} - -// Safety: the underlying Python bytes or Rust bytes is immutable and -// safe to share between threads -unsafe impl Send for PyBackedBytes {} -unsafe impl Sync for PyBackedBytes {} - -impl PartialEq<[u8; N]> for PyBackedBytes { - fn eq(&self, other: &[u8; N]) -> bool { - self.deref() == other - } -} - -impl PartialEq for [u8; N] { - fn eq(&self, other: &PyBackedBytes) -> bool { - self == other.deref() - } -} - -impl PartialEq<&[u8; N]> for PyBackedBytes { - fn eq(&self, other: &&[u8; N]) -> bool { - self.deref() == *other - } -} - -impl PartialEq for &[u8; N] { - fn eq(&self, other: &PyBackedBytes) -> bool { - self == &other.deref() - } -} - -impl_traits!(PyBackedBytes, [u8]); - -impl From> for PyBackedBytes { - fn from(py_bytes: Bound<'_, PyBytes>) -> Self { - let b = py_bytes.as_bytes(); - let data = NonNull::from(b); - Self { - storage: PyBackedBytesStorage::Python(py_bytes.to_owned().unbind()), - data, - } - } -} - -impl From> for PyBackedBytes { - fn from(py_bytearray: Bound<'_, PyByteArray>) -> Self { - let s = Arc::<[u8]>::from(py_bytearray.to_vec()); - let data = NonNull::from(s.as_ref()); - Self { - storage: PyBackedBytesStorage::Rust(s), - data, - } - } -} - -impl FromPyObject<'_> for PyBackedBytes { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - if let Ok(bytes) = obj.downcast::() { - Ok(Self::from(bytes.to_owned())) - } else if let Ok(bytearray) = obj.downcast::() { - Ok(Self::from(bytearray.to_owned())) - } else { - Err(DowncastError::new(obj, "`bytes` or `bytearray`").into()) - } - } -} - -#[allow(deprecated)] -impl ToPyObject for PyBackedBytes { - fn to_object(&self, py: Python<'_>) -> Py { - match &self.storage { - PyBackedBytesStorage::Python(bytes) => bytes.to_object(py), - PyBackedBytesStorage::Rust(bytes) => PyBytes::new(py, bytes).into_any().unbind(), - } - } -} - -#[allow(deprecated)] -impl IntoPy> for PyBackedBytes { - fn into_py(self, py: Python<'_>) -> Py { - match self.storage { - PyBackedBytesStorage::Python(bytes) => bytes.into_any(), - PyBackedBytesStorage::Rust(bytes) => PyBytes::new(py, &bytes).into_any().unbind(), - } - } -} - -impl<'py> IntoPyObject<'py> for PyBackedBytes { - type Target = PyBytes; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - match self.storage { - PyBackedBytesStorage::Python(bytes) => Ok(bytes.into_bound(py)), - PyBackedBytesStorage::Rust(bytes) => Ok(PyBytes::new(py, &bytes)), - } - } -} - -impl<'py> IntoPyObject<'py> for &PyBackedBytes { - type Target = PyBytes; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - match &self.storage { - PyBackedBytesStorage::Python(bytes) => Ok(bytes.bind(py).clone()), - PyBackedBytesStorage::Rust(bytes) => Ok(PyBytes::new(py, bytes)), - } - } -} - -macro_rules! impl_traits { - ($slf:ty, $equiv:ty) => { - impl std::fmt::Debug for $slf { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.deref().fmt(f) - } - } - - impl PartialEq for $slf { - fn eq(&self, other: &Self) -> bool { - self.deref() == other.deref() - } - } - - impl PartialEq<$equiv> for $slf { - fn eq(&self, other: &$equiv) -> bool { - self.deref() == other - } - } - - impl PartialEq<&$equiv> for $slf { - fn eq(&self, other: &&$equiv) -> bool { - self.deref() == *other - } - } - - impl PartialEq<$slf> for $equiv { - fn eq(&self, other: &$slf) -> bool { - self == other.deref() - } - } - - impl PartialEq<$slf> for &$equiv { - fn eq(&self, other: &$slf) -> bool { - self == &other.deref() - } - } - - impl Eq for $slf {} - - impl PartialOrd for $slf { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } - } - - impl PartialOrd<$equiv> for $slf { - fn partial_cmp(&self, other: &$equiv) -> Option { - self.deref().partial_cmp(other) - } - } - - impl PartialOrd<$slf> for $equiv { - fn partial_cmp(&self, other: &$slf) -> Option { - self.partial_cmp(other.deref()) - } - } - - impl Ord for $slf { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.deref().cmp(other.deref()) - } - } - - impl std::hash::Hash for $slf { - fn hash(&self, state: &mut H) { - self.deref().hash(state) - } - } - }; -} -use impl_traits; - -#[cfg(test)] -mod test { - use super::*; - use crate::{IntoPyObject, Python}; - use std::collections::hash_map::DefaultHasher; - use std::hash::{Hash, Hasher}; - - #[test] - fn py_backed_str_empty() { - Python::with_gil(|py| { - let s = PyString::new(py, ""); - let py_backed_str = s.extract::().unwrap(); - assert_eq!(&*py_backed_str, ""); - }); - } - - #[test] - fn py_backed_str() { - Python::with_gil(|py| { - let s = PyString::new(py, "hello"); - let py_backed_str = s.extract::().unwrap(); - assert_eq!(&*py_backed_str, "hello"); - }); - } - - #[test] - fn py_backed_str_try_from() { - Python::with_gil(|py| { - let s = PyString::new(py, "hello"); - let py_backed_str = PyBackedStr::try_from(s).unwrap(); - assert_eq!(&*py_backed_str, "hello"); - }); - } - - #[test] - fn py_backed_str_into_pyobject() { - Python::with_gil(|py| { - let orig_str = PyString::new(py, "hello"); - let py_backed_str = orig_str.extract::().unwrap(); - let new_str = py_backed_str.into_pyobject(py).unwrap(); - assert_eq!(new_str.extract::().unwrap(), "hello"); - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - assert!(new_str.is(&orig_str)); - }); - } - - #[test] - #[allow(deprecated)] - fn py_backed_str_into_py() { - Python::with_gil(|py| { - let orig_str = PyString::new(py, "hello"); - let py_backed_str = orig_str.extract::().unwrap(); - let new_str = py_backed_str.into_py(py); - assert_eq!(new_str.extract::(py).unwrap(), "hello"); - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - assert!(new_str.is(&orig_str)); - }); - } - - #[test] - fn py_backed_bytes_empty() { - Python::with_gil(|py| { - let b = PyBytes::new(py, b""); - let py_backed_bytes = b.extract::().unwrap(); - assert_eq!(&*py_backed_bytes, b""); - }); - } - - #[test] - fn py_backed_bytes() { - Python::with_gil(|py| { - let b = PyBytes::new(py, b"abcde"); - let py_backed_bytes = b.extract::().unwrap(); - assert_eq!(&*py_backed_bytes, b"abcde"); - }); - } - - #[test] - fn py_backed_bytes_from_bytes() { - Python::with_gil(|py| { - let b = PyBytes::new(py, b"abcde"); - let py_backed_bytes = PyBackedBytes::from(b); - assert_eq!(&*py_backed_bytes, b"abcde"); - }); - } - - #[test] - fn py_backed_bytes_from_bytearray() { - Python::with_gil(|py| { - let b = PyByteArray::new(py, b"abcde"); - let py_backed_bytes = PyBackedBytes::from(b); - assert_eq!(&*py_backed_bytes, b"abcde"); - }); - } - - #[test] - #[allow(deprecated)] - fn py_backed_bytes_into_pyobject() { - Python::with_gil(|py| { - let orig_bytes = PyBytes::new(py, b"abcde"); - let py_backed_bytes = PyBackedBytes::from(orig_bytes.clone()); - assert!((&py_backed_bytes) - .into_pyobject(py) - .unwrap() - .is(&orig_bytes)); - assert!(py_backed_bytes.into_py(py).is(&orig_bytes)); - }); - } - - #[test] - #[allow(deprecated)] - fn rust_backed_bytes_into_pyobject() { - Python::with_gil(|py| { - let orig_bytes = PyByteArray::new(py, b"abcde"); - let rust_backed_bytes = PyBackedBytes::from(orig_bytes); - assert!(matches!( - rust_backed_bytes.storage, - PyBackedBytesStorage::Rust(_) - )); - let to_object = (&rust_backed_bytes).into_pyobject(py).unwrap(); - assert!(&to_object.is_exact_instance_of::()); - assert_eq!(&to_object.extract::().unwrap(), b"abcde"); - let into_py = rust_backed_bytes.into_py(py).into_bound(py); - assert!(&into_py.is_exact_instance_of::()); - assert_eq!(&into_py.extract::().unwrap(), b"abcde"); - }); - } - - #[test] - fn test_backed_types_send_sync() { - fn is_send() {} - fn is_sync() {} - - is_send::(); - is_sync::(); - - is_send::(); - is_sync::(); - } - - #[cfg(feature = "py-clone")] - #[test] - fn test_backed_str_clone() { - Python::with_gil(|py| { - let s1: PyBackedStr = PyString::new(py, "hello").try_into().unwrap(); - let s2 = s1.clone(); - assert_eq!(s1, s2); - - drop(s1); - assert_eq!(s2, "hello"); - }); - } - - #[test] - fn test_backed_str_eq() { - Python::with_gil(|py| { - let s1: PyBackedStr = PyString::new(py, "hello").try_into().unwrap(); - let s2: PyBackedStr = PyString::new(py, "hello").try_into().unwrap(); - assert_eq!(s1, "hello"); - assert_eq!(s1, s2); - - let s3: PyBackedStr = PyString::new(py, "abcde").try_into().unwrap(); - assert_eq!("abcde", s3); - assert_ne!(s1, s3); - }); - } - - #[test] - fn test_backed_str_hash() { - Python::with_gil(|py| { - let h = { - let mut hasher = DefaultHasher::new(); - "abcde".hash(&mut hasher); - hasher.finish() - }; - - let s1: PyBackedStr = PyString::new(py, "abcde").try_into().unwrap(); - let h1 = { - let mut hasher = DefaultHasher::new(); - s1.hash(&mut hasher); - hasher.finish() - }; - - assert_eq!(h, h1); - }); - } - - #[test] - fn test_backed_str_ord() { - Python::with_gil(|py| { - let mut a = vec!["a", "c", "d", "b", "f", "g", "e"]; - let mut b = a - .iter() - .map(|s| PyString::new(py, s).try_into().unwrap()) - .collect::>(); - - a.sort(); - b.sort(); - - assert_eq!(a, b); - }) - } - - #[cfg(feature = "py-clone")] - #[test] - fn test_backed_bytes_from_bytes_clone() { - Python::with_gil(|py| { - let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into(); - let b2 = b1.clone(); - assert_eq!(b1, b2); - - drop(b1); - assert_eq!(b2, b"abcde"); - }); - } - - #[cfg(feature = "py-clone")] - #[test] - fn test_backed_bytes_from_bytearray_clone() { - Python::with_gil(|py| { - let b1: PyBackedBytes = PyByteArray::new(py, b"abcde").into(); - let b2 = b1.clone(); - assert_eq!(b1, b2); - - drop(b1); - assert_eq!(b2, b"abcde"); - }); - } - - #[test] - fn test_backed_bytes_eq() { - Python::with_gil(|py| { - let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into(); - let b2: PyBackedBytes = PyByteArray::new(py, b"abcde").into(); - - assert_eq!(b1, b"abcde"); - assert_eq!(b1, b2); - - let b3: PyBackedBytes = PyBytes::new(py, b"hello").into(); - assert_eq!(b"hello", b3); - assert_ne!(b1, b3); - }); - } - - #[test] - fn test_backed_bytes_hash() { - Python::with_gil(|py| { - let h = { - let mut hasher = DefaultHasher::new(); - b"abcde".hash(&mut hasher); - hasher.finish() - }; - - let b1: PyBackedBytes = PyBytes::new(py, b"abcde").into(); - let h1 = { - let mut hasher = DefaultHasher::new(); - b1.hash(&mut hasher); - hasher.finish() - }; - - let b2: PyBackedBytes = PyByteArray::new(py, b"abcde").into(); - let h2 = { - let mut hasher = DefaultHasher::new(); - b2.hash(&mut hasher); - hasher.finish() - }; - - assert_eq!(h, h1); - assert_eq!(h, h2); - }); - } - - #[test] - fn test_backed_bytes_ord() { - Python::with_gil(|py| { - let mut a = vec![b"a", b"c", b"d", b"b", b"f", b"g", b"e"]; - let mut b = a - .iter() - .map(|&b| PyBytes::new(py, b).into()) - .collect::>(); - - a.sort(); - b.sort(); - - assert_eq!(a, b); - }) - } -} diff --git a/vendor/pyo3/src/pycell.rs b/vendor/pyo3/src/pycell.rs deleted file mode 100644 index c7e5226a29..0000000000 --- a/vendor/pyo3/src/pycell.rs +++ /dev/null @@ -1,852 +0,0 @@ -//! PyO3's interior mutability primitive. -//! -//! Rust has strict aliasing rules - you can either have any number of immutable (shared) references or one mutable -//! reference. Python's ownership model is the complete opposite of that - any Python object -//! can be referenced any number of times, and mutation is allowed from any reference. -//! -//! PyO3 deals with these differences by employing the [Interior Mutability] -//! pattern. This requires that PyO3 enforces the borrowing rules and it has two mechanisms for -//! doing so: -//! - Statically it can enforce thread-safe access with the [`Python<'py>`](crate::Python) token. -//! All Rust code holding that token, or anything derived from it, can assume that they have -//! safe access to the Python interpreter's state. For this reason all the native Python objects -//! can be mutated through shared references. -//! - However, methods and functions in Rust usually *do* need `&mut` references. While PyO3 can -//! use the [`Python<'py>`](crate::Python) token to guarantee thread-safe access to them, it cannot -//! statically guarantee uniqueness of `&mut` references. As such those references have to be tracked -//! dynamically at runtime, using `PyCell` and the other types defined in this module. This works -//! similar to std's [`RefCell`](std::cell::RefCell) type. -//! -//! # When *not* to use PyCell -//! -//! Usually you can use `&mut` references as method and function receivers and arguments, and you -//! won't need to use `PyCell` directly: -//! -//! ```rust -//! use pyo3::prelude::*; -//! -//! #[pyclass] -//! struct Number { -//! inner: u32, -//! } -//! -//! #[pymethods] -//! impl Number { -//! fn increment(&mut self) { -//! self.inner += 1; -//! } -//! } -//! ``` -//! -//! The [`#[pymethods]`](crate::pymethods) proc macro will generate this wrapper function (and more), -//! using `PyCell` under the hood: -//! -//! ```rust,ignore -//! # use pyo3::prelude::*; -//! # #[pyclass] -//! # struct Number { -//! # inner: u32, -//! # } -//! # -//! # #[pymethods] -//! # impl Number { -//! # fn increment(&mut self) { -//! # self.inner += 1; -//! # } -//! # } -//! # -//! // The function which is exported to Python looks roughly like the following -//! unsafe extern "C" fn __pymethod_increment__( -//! _slf: *mut pyo3::ffi::PyObject, -//! _args: *mut pyo3::ffi::PyObject, -//! ) -> *mut pyo3::ffi::PyObject { -//! use :: pyo3 as _pyo3; -//! _pyo3::impl_::trampoline::noargs(_slf, _args, |py, _slf| { -//! # #[allow(deprecated)] -//! let _cell = py -//! .from_borrowed_ptr::<_pyo3::PyAny>(_slf) -//! .downcast::<_pyo3::PyCell>()?; -//! let mut _ref = _cell.try_borrow_mut()?; -//! let _slf: &mut Number = &mut *_ref; -//! _pyo3::impl_::callback::convert(py, Number::increment(_slf)) -//! }) -//! } -//! ``` -//! -//! # When to use PyCell -//! ## Using pyclasses from Rust -//! -//! However, we *do* need `PyCell` if we want to call its methods from Rust: -//! ```rust -//! # use pyo3::prelude::*; -//! # -//! # #[pyclass] -//! # struct Number { -//! # inner: u32, -//! # } -//! # -//! # #[pymethods] -//! # impl Number { -//! # fn increment(&mut self) { -//! # self.inner += 1; -//! # } -//! # } -//! # fn main() -> PyResult<()> { -//! Python::with_gil(|py| { -//! let n = Py::new(py, Number { inner: 0 })?; -//! -//! // We borrow the guard and then dereference -//! // it to get a mutable reference to Number -//! let mut guard: PyRefMut<'_, Number> = n.bind(py).borrow_mut(); -//! let n_mutable: &mut Number = &mut *guard; -//! -//! n_mutable.increment(); -//! -//! // To avoid panics we must dispose of the -//! // `PyRefMut` before borrowing again. -//! drop(guard); -//! -//! let n_immutable: &Number = &n.bind(py).borrow(); -//! assert_eq!(n_immutable.inner, 1); -//! -//! Ok(()) -//! }) -//! # } -//! ``` -//! ## Dealing with possibly overlapping mutable references -//! -//! It is also necessary to use `PyCell` if you can receive mutable arguments that may overlap. -//! Suppose the following function that swaps the values of two `Number`s: -//! ``` -//! # use pyo3::prelude::*; -//! # #[pyclass] -//! # pub struct Number { -//! # inner: u32, -//! # } -//! #[pyfunction] -//! fn swap_numbers(a: &mut Number, b: &mut Number) { -//! std::mem::swap(&mut a.inner, &mut b.inner); -//! } -//! # fn main() { -//! # Python::with_gil(|py| { -//! # let n = Py::new(py, Number{inner: 35}).unwrap(); -//! # let n2 = n.clone_ref(py); -//! # assert!(n.is(&n2)); -//! # let fun = pyo3::wrap_pyfunction!(swap_numbers, py).unwrap(); -//! # fun.call1((n, n2)).expect_err("Managed to create overlapping mutable references. Note: this is undefined behaviour."); -//! # }); -//! # } -//! ``` -//! When users pass in the same `Number` as both arguments, one of the mutable borrows will -//! fail and raise a `RuntimeError`: -//! ```text -//! >>> a = Number() -//! >>> swap_numbers(a, a) -//! Traceback (most recent call last): -//! File "", line 1, in -//! RuntimeError: Already borrowed -//! ``` -//! -//! It is better to write that function like this: -//! ```rust,ignore -//! # #![allow(deprecated)] -//! # use pyo3::prelude::*; -//! # #[pyclass] -//! # pub struct Number { -//! # inner: u32, -//! # } -//! #[pyfunction] -//! fn swap_numbers(a: &PyCell, b: &PyCell) { -//! // Check that the pointers are unequal -//! if !a.is(b) { -//! std::mem::swap(&mut a.borrow_mut().inner, &mut b.borrow_mut().inner); -//! } else { -//! // Do nothing - they are the same object, so don't need swapping. -//! } -//! } -//! # fn main() { -//! # // With duplicate numbers -//! # Python::with_gil(|py| { -//! # let n = Py::new(py, Number{inner: 35}).unwrap(); -//! # let n2 = n.clone_ref(py); -//! # assert!(n.is(&n2)); -//! # let fun = pyo3::wrap_pyfunction!(swap_numbers, py).unwrap(); -//! # fun.call1((n, n2)).unwrap(); -//! # }); -//! # -//! # // With two different numbers -//! # Python::with_gil(|py| { -//! # let n = Py::new(py, Number{inner: 35}).unwrap(); -//! # let n2 = Py::new(py, Number{inner: 42}).unwrap(); -//! # assert!(!n.is(&n2)); -//! # let fun = pyo3::wrap_pyfunction!(swap_numbers, py).unwrap(); -//! # fun.call1((&n, &n2)).unwrap(); -//! # let n: u32 = n.borrow(py).inner; -//! # let n2: u32 = n2.borrow(py).inner; -//! # assert_eq!(n, 42); -//! # assert_eq!(n2, 35); -//! # }); -//! # } -//! ``` -//! See the [guide] for more information. -//! -//! [guide]: https://pyo3.rs/latest/class.html#pycell-and-interior-mutability "PyCell and interior mutability" -//! [Interior Mutability]: https://doc.rust-lang.org/book/ch15-05-interior-mutability.html "RefCell and the Interior Mutability Pattern - The Rust Programming Language" - -use crate::conversion::{AsPyPointer, IntoPyObject}; -use crate::exceptions::PyRuntimeError; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::internal_tricks::{ptr_from_mut, ptr_from_ref}; -use crate::pyclass::{boolean_struct::False, PyClass}; -use crate::types::any::PyAnyMethods; -#[allow(deprecated)] -use crate::IntoPy; -use crate::{ffi, Borrowed, Bound, PyErr, PyObject, Python}; -use std::convert::Infallible; -use std::fmt; -use std::mem::ManuallyDrop; -use std::ops::{Deref, DerefMut}; - -pub(crate) mod impl_; -use impl_::{PyClassBorrowChecker, PyClassObjectLayout}; - -/// A wrapper type for an immutably borrowed value from a [`Bound<'py, T>`]. -/// -/// See the [`Bound`] documentation for more information. -/// -/// # Examples -/// -/// You can use [`PyRef`] as an alternative to a `&self` receiver when -/// - you need to access the pointer of the [`Bound`], or -/// - you want to get a super class. -/// ``` -/// # use pyo3::prelude::*; -/// #[pyclass(subclass)] -/// struct Parent { -/// basename: &'static str, -/// } -/// -/// #[pyclass(extends=Parent)] -/// struct Child { -/// name: &'static str, -/// } -/// -/// #[pymethods] -/// impl Child { -/// #[new] -/// fn new() -> (Self, Parent) { -/// (Child { name: "Caterpillar" }, Parent { basename: "Butterfly" }) -/// } -/// -/// fn format(slf: PyRef<'_, Self>) -> String { -/// // We can get *mut ffi::PyObject from PyRef -/// let refcnt = unsafe { pyo3::ffi::Py_REFCNT(slf.as_ptr()) }; -/// // We can get &Self::BaseType by as_ref -/// let basename = slf.as_ref().basename; -/// format!("{}(base: {}, cnt: {})", slf.name, basename, refcnt) -/// } -/// } -/// # Python::with_gil(|py| { -/// # let sub = Py::new(py, Child::new()).unwrap(); -/// # pyo3::py_run!(py, sub, "assert sub.format() == 'Caterpillar(base: Butterfly, cnt: 4)', sub.format()"); -/// # }); -/// ``` -/// -/// See the [module-level documentation](self) for more information. -#[repr(transparent)] -pub struct PyRef<'p, T: PyClass> { - // TODO: once the GIL Ref API is removed, consider adding a lifetime parameter to `PyRef` to - // store `Borrowed` here instead, avoiding reference counting overhead. - inner: Bound<'p, T>, -} - -impl<'p, T: PyClass> PyRef<'p, T> { - /// Returns a `Python` token that is bound to the lifetime of the `PyRef`. - pub fn py(&self) -> Python<'p> { - self.inner.py() - } -} - -impl AsRef for PyRef<'_, T> -where - T: PyClass, - U: PyClass, -{ - fn as_ref(&self) -> &T::BaseType { - self.as_super() - } -} - -impl<'py, T: PyClass> PyRef<'py, T> { - /// Returns the raw FFI pointer represented by self. - /// - /// # Safety - /// - /// Callers are responsible for ensuring that the pointer does not outlive self. - /// - /// The reference is borrowed; callers should not decrease the reference count - /// when they are finished with the pointer. - #[inline] - pub fn as_ptr(&self) -> *mut ffi::PyObject { - self.inner.as_ptr() - } - - /// Returns an owned raw FFI pointer represented by self. - /// - /// # Safety - /// - /// The reference is owned; when finished the caller should either transfer ownership - /// of the pointer or decrease the reference count (e.g. with [`pyo3::ffi::Py_DecRef`](crate::ffi::Py_DecRef)). - #[inline] - pub fn into_ptr(self) -> *mut ffi::PyObject { - self.inner.clone().into_ptr() - } - - #[track_caller] - pub(crate) fn borrow(obj: &Bound<'py, T>) -> Self { - Self::try_borrow(obj).expect("Already mutably borrowed") - } - - pub(crate) fn try_borrow(obj: &Bound<'py, T>) -> Result { - let cell = obj.get_class_object(); - cell.ensure_threadsafe(); - cell.borrow_checker() - .try_borrow() - .map(|_| Self { inner: obj.clone() }) - } -} - -impl<'p, T, U> PyRef<'p, T> -where - T: PyClass, - U: PyClass, -{ - /// Gets a `PyRef`. - /// - /// While `as_ref()` returns a reference of type `&T::BaseType`, this cannot be - /// used to get the base of `T::BaseType`. - /// - /// But with the help of this method, you can get hold of instances of the - /// super-superclass when needed. - /// - /// # Examples - /// ``` - /// # use pyo3::prelude::*; - /// #[pyclass(subclass)] - /// struct Base1 { - /// name1: &'static str, - /// } - /// - /// #[pyclass(extends=Base1, subclass)] - /// struct Base2 { - /// name2: &'static str, - /// } - /// - /// #[pyclass(extends=Base2)] - /// struct Sub { - /// name3: &'static str, - /// } - /// - /// #[pymethods] - /// impl Sub { - /// #[new] - /// fn new() -> PyClassInitializer { - /// PyClassInitializer::from(Base1 { name1: "base1" }) - /// .add_subclass(Base2 { name2: "base2" }) - /// .add_subclass(Self { name3: "sub" }) - /// } - /// fn name(slf: PyRef<'_, Self>) -> String { - /// let subname = slf.name3; - /// let super_ = slf.into_super(); - /// format!("{} {} {}", super_.as_ref().name1, super_.name2, subname) - /// } - /// } - /// # Python::with_gil(|py| { - /// # let sub = Py::new(py, Sub::new()).unwrap(); - /// # pyo3::py_run!(py, sub, "assert sub.name() == 'base1 base2 sub'") - /// # }); - /// ``` - pub fn into_super(self) -> PyRef<'p, U> { - let py = self.py(); - PyRef { - inner: unsafe { - ManuallyDrop::new(self) - .as_ptr() - .assume_owned_unchecked(py) - .downcast_into_unchecked() - }, - } - } - - /// Borrows a shared reference to `PyRef`. - /// - /// With the help of this method, you can access attributes and call methods - /// on the superclass without consuming the `PyRef`. This method can also - /// be chained to access the super-superclass (and so on). - /// - /// # Examples - /// ``` - /// # use pyo3::prelude::*; - /// #[pyclass(subclass)] - /// struct Base { - /// base_name: &'static str, - /// } - /// #[pymethods] - /// impl Base { - /// fn base_name_len(&self) -> usize { - /// self.base_name.len() - /// } - /// } - /// - /// #[pyclass(extends=Base)] - /// struct Sub { - /// sub_name: &'static str, - /// } - /// - /// #[pymethods] - /// impl Sub { - /// #[new] - /// fn new() -> (Self, Base) { - /// (Self { sub_name: "sub_name" }, Base { base_name: "base_name" }) - /// } - /// fn sub_name_len(&self) -> usize { - /// self.sub_name.len() - /// } - /// fn format_name_lengths(slf: PyRef<'_, Self>) -> String { - /// format!("{} {}", slf.as_super().base_name_len(), slf.sub_name_len()) - /// } - /// } - /// # Python::with_gil(|py| { - /// # let sub = Py::new(py, Sub::new()).unwrap(); - /// # pyo3::py_run!(py, sub, "assert sub.format_name_lengths() == '9 8'") - /// # }); - /// ``` - pub fn as_super(&self) -> &PyRef<'p, U> { - let ptr = ptr_from_ref::>(&self.inner) - // `Bound` has the same layout as `Bound` - .cast::>() - // `Bound` has the same layout as `PyRef` - .cast::>(); - unsafe { &*ptr } - } -} - -impl Deref for PyRef<'_, T> { - type Target = T; - - #[inline] - fn deref(&self) -> &T { - unsafe { &*self.inner.get_class_object().get_ptr() } - } -} - -impl Drop for PyRef<'_, T> { - fn drop(&mut self) { - self.inner - .get_class_object() - .borrow_checker() - .release_borrow() - } -} - -#[allow(deprecated)] -impl IntoPy for PyRef<'_, T> { - fn into_py(self, py: Python<'_>) -> PyObject { - unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } - } -} - -#[allow(deprecated)] -impl IntoPy for &'_ PyRef<'_, T> { - fn into_py(self, py: Python<'_>) -> PyObject { - unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } - } -} - -impl<'py, T: PyClass> IntoPyObject<'py> for PyRef<'py, T> { - type Target = T; - type Output = Bound<'py, T>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self.inner.clone()) - } -} - -impl<'a, 'py, T: PyClass> IntoPyObject<'py> for &'a PyRef<'py, T> { - type Target = T; - type Output = Borrowed<'a, 'py, T>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self.inner.as_borrowed()) - } -} - -unsafe impl AsPyPointer for PyRef<'_, T> { - fn as_ptr(&self) -> *mut ffi::PyObject { - self.inner.as_ptr() - } -} - -impl fmt::Debug for PyRef<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&**self, f) - } -} - -/// A wrapper type for a mutably borrowed value from a [`Bound<'py, T>`]. -/// -/// See the [module-level documentation](self) for more information. -#[repr(transparent)] -pub struct PyRefMut<'p, T: PyClass> { - // TODO: once the GIL Ref API is removed, consider adding a lifetime parameter to `PyRef` to - // store `Borrowed` here instead, avoiding reference counting overhead. - inner: Bound<'p, T>, -} - -impl<'p, T: PyClass> PyRefMut<'p, T> { - /// Returns a `Python` token that is bound to the lifetime of the `PyRefMut`. - pub fn py(&self) -> Python<'p> { - self.inner.py() - } -} - -impl AsRef for PyRefMut<'_, T> -where - T: PyClass, - U: PyClass, -{ - fn as_ref(&self) -> &T::BaseType { - PyRefMut::downgrade(self).as_super() - } -} - -impl AsMut for PyRefMut<'_, T> -where - T: PyClass, - U: PyClass, -{ - fn as_mut(&mut self) -> &mut T::BaseType { - self.as_super() - } -} - -impl<'py, T: PyClass> PyRefMut<'py, T> { - /// Returns the raw FFI pointer represented by self. - /// - /// # Safety - /// - /// Callers are responsible for ensuring that the pointer does not outlive self. - /// - /// The reference is borrowed; callers should not decrease the reference count - /// when they are finished with the pointer. - #[inline] - pub fn as_ptr(&self) -> *mut ffi::PyObject { - self.inner.as_ptr() - } - - /// Returns an owned raw FFI pointer represented by self. - /// - /// # Safety - /// - /// The reference is owned; when finished the caller should either transfer ownership - /// of the pointer or decrease the reference count (e.g. with [`pyo3::ffi::Py_DecRef`](crate::ffi::Py_DecRef)). - #[inline] - pub fn into_ptr(self) -> *mut ffi::PyObject { - self.inner.clone().into_ptr() - } - - #[inline] - #[track_caller] - pub(crate) fn borrow(obj: &Bound<'py, T>) -> Self { - Self::try_borrow(obj).expect("Already borrowed") - } - - pub(crate) fn try_borrow(obj: &Bound<'py, T>) -> Result { - let cell = obj.get_class_object(); - cell.ensure_threadsafe(); - cell.borrow_checker() - .try_borrow_mut() - .map(|_| Self { inner: obj.clone() }) - } - - pub(crate) fn downgrade(slf: &Self) -> &PyRef<'py, T> { - // `PyRefMut` and `PyRef` have the same layout - unsafe { &*ptr_from_ref(slf).cast() } - } -} - -impl<'p, T, U> PyRefMut<'p, T> -where - T: PyClass, - U: PyClass, -{ - /// Gets a `PyRef`. - /// - /// See [`PyRef::into_super`] for more. - pub fn into_super(self) -> PyRefMut<'p, U> { - let py = self.py(); - PyRefMut { - inner: unsafe { - ManuallyDrop::new(self) - .as_ptr() - .assume_owned_unchecked(py) - .downcast_into_unchecked() - }, - } - } - - /// Borrows a mutable reference to `PyRefMut`. - /// - /// With the help of this method, you can mutate attributes and call mutating - /// methods on the superclass without consuming the `PyRefMut`. This method - /// can also be chained to access the super-superclass (and so on). - /// - /// See [`PyRef::as_super`] for more. - pub fn as_super(&mut self) -> &mut PyRefMut<'p, U> { - let ptr = ptr_from_mut::>(&mut self.inner) - // `Bound` has the same layout as `Bound` - .cast::>() - // `Bound` has the same layout as `PyRefMut`, - // and the mutable borrow on `self` prevents aliasing - .cast::>(); - unsafe { &mut *ptr } - } -} - -impl> Deref for PyRefMut<'_, T> { - type Target = T; - - #[inline] - fn deref(&self) -> &T { - unsafe { &*self.inner.get_class_object().get_ptr() } - } -} - -impl> DerefMut for PyRefMut<'_, T> { - #[inline] - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.inner.get_class_object().get_ptr() } - } -} - -impl> Drop for PyRefMut<'_, T> { - fn drop(&mut self) { - self.inner - .get_class_object() - .borrow_checker() - .release_borrow_mut() - } -} - -#[allow(deprecated)] -impl> IntoPy for PyRefMut<'_, T> { - fn into_py(self, py: Python<'_>) -> PyObject { - unsafe { PyObject::from_borrowed_ptr(py, self.inner.as_ptr()) } - } -} - -#[allow(deprecated)] -impl> IntoPy for &'_ PyRefMut<'_, T> { - fn into_py(self, py: Python<'_>) -> PyObject { - self.inner.clone().into_py(py) - } -} - -impl<'py, T: PyClass> IntoPyObject<'py> for PyRefMut<'py, T> { - type Target = T; - type Output = Bound<'py, T>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self.inner.clone()) - } -} - -impl<'a, 'py, T: PyClass> IntoPyObject<'py> for &'a PyRefMut<'py, T> { - type Target = T; - type Output = Borrowed<'a, 'py, T>; - type Error = Infallible; - - fn into_pyobject(self, _py: Python<'py>) -> Result { - Ok(self.inner.as_borrowed()) - } -} - -impl + fmt::Debug> fmt::Debug for PyRefMut<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.deref(), f) - } -} - -/// An error type returned by [`Bound::try_borrow`]. -/// -/// If this error is allowed to bubble up into Python code it will raise a `RuntimeError`. -pub struct PyBorrowError { - _private: (), -} - -impl fmt::Debug for PyBorrowError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("PyBorrowError").finish() - } -} - -impl fmt::Display for PyBorrowError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt("Already mutably borrowed", f) - } -} - -impl From for PyErr { - fn from(other: PyBorrowError) -> Self { - PyRuntimeError::new_err(other.to_string()) - } -} - -/// An error type returned by [`Bound::try_borrow_mut`]. -/// -/// If this error is allowed to bubble up into Python code it will raise a `RuntimeError`. -pub struct PyBorrowMutError { - _private: (), -} - -impl fmt::Debug for PyBorrowMutError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("PyBorrowMutError").finish() - } -} - -impl fmt::Display for PyBorrowMutError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt("Already borrowed", f) - } -} - -impl From for PyErr { - fn from(other: PyBorrowMutError) -> Self { - PyRuntimeError::new_err(other.to_string()) - } -} - -#[cfg(test)] -#[cfg(feature = "macros")] -mod tests { - - use super::*; - - #[crate::pyclass] - #[pyo3(crate = "crate")] - #[derive(Copy, Clone, PartialEq, Eq, Debug)] - struct SomeClass(i32); - - #[test] - fn test_as_ptr() { - Python::with_gil(|py| { - let cell = Bound::new(py, SomeClass(0)).unwrap(); - let ptr = cell.as_ptr(); - - assert_eq!(cell.borrow().as_ptr(), ptr); - assert_eq!(cell.borrow_mut().as_ptr(), ptr); - }) - } - - #[test] - fn test_into_ptr() { - Python::with_gil(|py| { - let cell = Bound::new(py, SomeClass(0)).unwrap(); - let ptr = cell.as_ptr(); - - assert_eq!(cell.borrow().into_ptr(), ptr); - unsafe { ffi::Py_DECREF(ptr) }; - - assert_eq!(cell.borrow_mut().into_ptr(), ptr); - unsafe { ffi::Py_DECREF(ptr) }; - }) - } - - #[crate::pyclass] - #[pyo3(crate = "crate", subclass)] - struct BaseClass { - val1: usize, - } - - #[crate::pyclass] - #[pyo3(crate = "crate", extends=BaseClass, subclass)] - struct SubClass { - val2: usize, - } - - #[crate::pyclass] - #[pyo3(crate = "crate", extends=SubClass)] - struct SubSubClass { - val3: usize, - } - - #[crate::pymethods] - #[pyo3(crate = "crate")] - impl SubSubClass { - #[new] - fn new(py: Python<'_>) -> crate::Py { - let init = crate::PyClassInitializer::from(BaseClass { val1: 10 }) - .add_subclass(SubClass { val2: 15 }) - .add_subclass(SubSubClass { val3: 20 }); - crate::Py::new(py, init).expect("allocation error") - } - - fn get_values(self_: PyRef<'_, Self>) -> (usize, usize, usize) { - let val1 = self_.as_super().as_super().val1; - let val2 = self_.as_super().val2; - (val1, val2, self_.val3) - } - - fn double_values(mut self_: PyRefMut<'_, Self>) { - self_.as_super().as_super().val1 *= 2; - self_.as_super().val2 *= 2; - self_.val3 *= 2; - } - } - - #[test] - fn test_pyref_as_super() { - Python::with_gil(|py| { - let obj = SubSubClass::new(py).into_bound(py); - let pyref = obj.borrow(); - assert_eq!(pyref.as_super().as_super().val1, 10); - assert_eq!(pyref.as_super().val2, 15); - assert_eq!(pyref.as_ref().val2, 15); // `as_ref` also works - assert_eq!(pyref.val3, 20); - assert_eq!(SubSubClass::get_values(pyref), (10, 15, 20)); - }); - } - - #[test] - fn test_pyrefmut_as_super() { - Python::with_gil(|py| { - let obj = SubSubClass::new(py).into_bound(py); - assert_eq!(SubSubClass::get_values(obj.borrow()), (10, 15, 20)); - { - let mut pyrefmut = obj.borrow_mut(); - assert_eq!(pyrefmut.as_super().as_ref().val1, 10); - pyrefmut.as_super().as_super().val1 -= 5; - pyrefmut.as_super().val2 -= 3; - pyrefmut.as_mut().val2 -= 2; // `as_mut` also works - pyrefmut.val3 -= 5; - } - assert_eq!(SubSubClass::get_values(obj.borrow()), (5, 10, 15)); - SubSubClass::double_values(obj.borrow_mut()); - assert_eq!(SubSubClass::get_values(obj.borrow()), (10, 20, 30)); - }); - } - - #[test] - fn test_pyrefs_in_python() { - Python::with_gil(|py| { - let obj = SubSubClass::new(py); - crate::py_run!(py, obj, "assert obj.get_values() == (10, 15, 20)"); - crate::py_run!(py, obj, "assert obj.double_values() is None"); - crate::py_run!(py, obj, "assert obj.get_values() == (20, 30, 40)"); - }); - } -} diff --git a/vendor/pyo3/src/pycell/impl_.rs b/vendor/pyo3/src/pycell/impl_.rs deleted file mode 100644 index 1b0724d848..0000000000 --- a/vendor/pyo3/src/pycell/impl_.rs +++ /dev/null @@ -1,628 +0,0 @@ -#![allow(missing_docs)] -//! Crate-private implementation of PyClassObject - -use std::cell::UnsafeCell; -use std::marker::PhantomData; -use std::mem::ManuallyDrop; -use std::sync::atomic::{AtomicUsize, Ordering}; - -use crate::impl_::pyclass::{ - PyClassBaseType, PyClassDict, PyClassImpl, PyClassThreadChecker, PyClassWeakRef, -}; -use crate::internal::get_slot::TP_FREE; -use crate::type_object::{PyLayout, PySizedLayout}; -use crate::types::{PyType, PyTypeMethods}; -use crate::{ffi, PyClass, PyTypeInfo, Python}; - -use super::{PyBorrowError, PyBorrowMutError}; - -pub trait PyClassMutability { - // The storage for this inheritance layer. Only the first mutable class in - // an inheritance hierarchy needs to store the borrow flag. - type Storage: PyClassBorrowChecker; - // The borrow flag needed to implement this class' mutability. Empty until - // the first mutable class, at which point it is BorrowChecker and will be - // for all subclasses. - type Checker: PyClassBorrowChecker; - type ImmutableChild: PyClassMutability; - type MutableChild: PyClassMutability; -} - -pub struct ImmutableClass(()); -pub struct MutableClass(()); -pub struct ExtendsMutableAncestor(PhantomData); - -impl PyClassMutability for ImmutableClass { - type Storage = EmptySlot; - type Checker = EmptySlot; - type ImmutableChild = ImmutableClass; - type MutableChild = MutableClass; -} - -impl PyClassMutability for MutableClass { - type Storage = BorrowChecker; - type Checker = BorrowChecker; - type ImmutableChild = ExtendsMutableAncestor; - type MutableChild = ExtendsMutableAncestor; -} - -impl PyClassMutability for ExtendsMutableAncestor { - type Storage = EmptySlot; - type Checker = BorrowChecker; - type ImmutableChild = ExtendsMutableAncestor; - type MutableChild = ExtendsMutableAncestor; -} - -#[derive(Debug)] -struct BorrowFlag(AtomicUsize); - -impl BorrowFlag { - pub(crate) const UNUSED: usize = 0; - const HAS_MUTABLE_BORROW: usize = usize::MAX; - fn increment(&self) -> Result<(), PyBorrowError> { - let mut value = self.0.load(Ordering::Relaxed); - loop { - if value == BorrowFlag::HAS_MUTABLE_BORROW { - return Err(PyBorrowError { _private: () }); - } - match self.0.compare_exchange( - // only increment if the value hasn't changed since the - // last atomic load - value, - value + 1, - Ordering::Relaxed, - Ordering::Relaxed, - ) { - Ok(..) => { - // value has been successfully incremented, we need an acquire fence - // so that data this borrow flag protects can be read safely in this thread - std::sync::atomic::fence(Ordering::Acquire); - break Ok(()); - } - Err(changed_value) => { - // value changed under us, need to try again - value = changed_value; - } - } - } - } - fn decrement(&self) { - // impossible to get into a bad state from here so relaxed - // ordering is fine, the decrement only needs to eventually - // be visible - self.0.fetch_sub(1, Ordering::Relaxed); - } -} - -pub struct EmptySlot(()); -pub struct BorrowChecker(BorrowFlag); - -pub trait PyClassBorrowChecker { - /// Initial value for self - fn new() -> Self; - - /// Increments immutable borrow count, if possible - fn try_borrow(&self) -> Result<(), PyBorrowError>; - - /// Decrements immutable borrow count - fn release_borrow(&self); - /// Increments mutable borrow count, if possible - fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError>; - /// Decremements mutable borrow count - fn release_borrow_mut(&self); -} - -impl PyClassBorrowChecker for EmptySlot { - #[inline] - fn new() -> Self { - EmptySlot(()) - } - - #[inline] - fn try_borrow(&self) -> Result<(), PyBorrowError> { - Ok(()) - } - - #[inline] - fn release_borrow(&self) {} - - #[inline] - fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError> { - unreachable!() - } - - #[inline] - fn release_borrow_mut(&self) { - unreachable!() - } -} - -impl PyClassBorrowChecker for BorrowChecker { - #[inline] - fn new() -> Self { - Self(BorrowFlag(AtomicUsize::new(BorrowFlag::UNUSED))) - } - - fn try_borrow(&self) -> Result<(), PyBorrowError> { - self.0.increment() - } - - fn release_borrow(&self) { - self.0.decrement(); - } - - fn try_borrow_mut(&self) -> Result<(), PyBorrowMutError> { - let flag = &self.0; - match flag.0.compare_exchange( - // only allowed to transition to mutable borrow if the reference is - // currently unused - BorrowFlag::UNUSED, - BorrowFlag::HAS_MUTABLE_BORROW, - // On success, reading the flag and updating its state are an atomic - // operation - Ordering::AcqRel, - // It doesn't matter precisely when the failure gets turned - // into an error - Ordering::Relaxed, - ) { - Ok(..) => Ok(()), - Err(..) => Err(PyBorrowMutError { _private: () }), - } - } - - fn release_borrow_mut(&self) { - self.0 .0.store(BorrowFlag::UNUSED, Ordering::Release) - } -} - -pub trait GetBorrowChecker { - fn borrow_checker( - class_object: &PyClassObject, - ) -> &::Checker; -} - -impl> GetBorrowChecker for MutableClass { - fn borrow_checker(class_object: &PyClassObject) -> &BorrowChecker { - &class_object.contents.borrow_checker - } -} - -impl> GetBorrowChecker for ImmutableClass { - fn borrow_checker(class_object: &PyClassObject) -> &EmptySlot { - &class_object.contents.borrow_checker - } -} - -impl, M: PyClassMutability> GetBorrowChecker - for ExtendsMutableAncestor -where - T::BaseType: PyClassImpl + PyClassBaseType>, - ::PyClassMutability: PyClassMutability, -{ - fn borrow_checker(class_object: &PyClassObject) -> &BorrowChecker { - <::PyClassMutability as GetBorrowChecker>::borrow_checker(&class_object.ob_base) - } -} - -/// Base layout of PyClassObject. -#[doc(hidden)] -#[repr(C)] -pub struct PyClassObjectBase { - ob_base: T, -} - -unsafe impl PyLayout for PyClassObjectBase where U: PySizedLayout {} - -#[doc(hidden)] -pub trait PyClassObjectLayout: PyLayout { - fn ensure_threadsafe(&self); - fn check_threadsafe(&self) -> Result<(), PyBorrowError>; - /// Implementation of tp_dealloc. - /// # Safety - /// - slf must be a valid pointer to an instance of a T or a subclass. - /// - slf must not be used after this call (as it will be freed). - unsafe fn tp_dealloc(py: Python<'_>, slf: *mut ffi::PyObject); -} - -impl PyClassObjectLayout for PyClassObjectBase -where - U: PySizedLayout, - T: PyTypeInfo, -{ - fn ensure_threadsafe(&self) {} - fn check_threadsafe(&self) -> Result<(), PyBorrowError> { - Ok(()) - } - unsafe fn tp_dealloc(py: Python<'_>, slf: *mut ffi::PyObject) { - // FIXME: there is potentially subtle issues here if the base is overwritten - // at runtime? To be investigated. - let type_obj = T::type_object(py); - let type_ptr = type_obj.as_type_ptr(); - let actual_type = PyType::from_borrowed_type_ptr(py, ffi::Py_TYPE(slf)); - - // For `#[pyclass]` types which inherit from PyAny, we can just call tp_free - if type_ptr == std::ptr::addr_of_mut!(ffi::PyBaseObject_Type) { - let tp_free = actual_type - .get_slot(TP_FREE) - .expect("PyBaseObject_Type should have tp_free"); - return tp_free(slf.cast()); - } - - // More complex native types (e.g. `extends=PyDict`) require calling the base's dealloc. - #[cfg(not(Py_LIMITED_API))] - { - // FIXME: should this be using actual_type.tp_dealloc? - if let Some(dealloc) = (*type_ptr).tp_dealloc { - // Before CPython 3.11 BaseException_dealloc would use Py_GC_UNTRACK which - // assumes the exception is currently GC tracked, so we have to re-track - // before calling the dealloc so that it can safely call Py_GC_UNTRACK. - #[cfg(not(any(Py_3_11, PyPy)))] - if ffi::PyType_FastSubclass(type_ptr, ffi::Py_TPFLAGS_BASE_EXC_SUBCLASS) == 1 { - ffi::PyObject_GC_Track(slf.cast()); - } - dealloc(slf); - } else { - (*actual_type.as_type_ptr()) - .tp_free - .expect("type missing tp_free")(slf.cast()); - } - } - - #[cfg(Py_LIMITED_API)] - unreachable!("subclassing native types is not possible with the `abi3` feature"); - } -} - -/// The layout of a PyClass as a Python object -#[repr(C)] -pub struct PyClassObject { - pub(crate) ob_base: ::LayoutAsBase, - pub(crate) contents: PyClassObjectContents, -} - -#[repr(C)] -pub(crate) struct PyClassObjectContents { - pub(crate) value: ManuallyDrop>, - pub(crate) borrow_checker: ::Storage, - pub(crate) thread_checker: T::ThreadChecker, - pub(crate) dict: T::Dict, - pub(crate) weakref: T::WeakRef, -} - -impl PyClassObject { - pub(crate) fn get_ptr(&self) -> *mut T { - self.contents.value.get() - } - - /// Gets the offset of the dictionary from the start of the struct in bytes. - pub(crate) fn dict_offset() -> ffi::Py_ssize_t { - use memoffset::offset_of; - - let offset = - offset_of!(PyClassObject, contents) + offset_of!(PyClassObjectContents, dict); - - // Py_ssize_t may not be equal to isize on all platforms - #[allow(clippy::useless_conversion)] - offset.try_into().expect("offset should fit in Py_ssize_t") - } - - /// Gets the offset of the weakref list from the start of the struct in bytes. - pub(crate) fn weaklist_offset() -> ffi::Py_ssize_t { - use memoffset::offset_of; - - let offset = - offset_of!(PyClassObject, contents) + offset_of!(PyClassObjectContents, weakref); - - // Py_ssize_t may not be equal to isize on all platforms - #[allow(clippy::useless_conversion)] - offset.try_into().expect("offset should fit in Py_ssize_t") - } -} - -impl PyClassObject { - pub(crate) fn borrow_checker(&self) -> &::Checker { - T::PyClassMutability::borrow_checker(self) - } -} - -unsafe impl PyLayout for PyClassObject {} -impl PySizedLayout for PyClassObject {} - -impl PyClassObjectLayout for PyClassObject -where - ::LayoutAsBase: PyClassObjectLayout, -{ - fn ensure_threadsafe(&self) { - self.contents.thread_checker.ensure(); - self.ob_base.ensure_threadsafe(); - } - fn check_threadsafe(&self) -> Result<(), PyBorrowError> { - if !self.contents.thread_checker.check() { - return Err(PyBorrowError { _private: () }); - } - self.ob_base.check_threadsafe() - } - unsafe fn tp_dealloc(py: Python<'_>, slf: *mut ffi::PyObject) { - // Safety: Python only calls tp_dealloc when no references to the object remain. - let class_object = &mut *(slf.cast::>()); - if class_object.contents.thread_checker.can_drop(py) { - ManuallyDrop::drop(&mut class_object.contents.value); - } - class_object.contents.dict.clear_dict(py); - class_object.contents.weakref.clear_weakrefs(slf, py); - ::LayoutAsBase::tp_dealloc(py, slf) - } -} - -#[cfg(test)] -#[cfg(feature = "macros")] -mod tests { - use super::*; - - use crate::prelude::*; - use crate::pyclass::boolean_struct::{False, True}; - - #[pyclass(crate = "crate", subclass)] - struct MutableBase; - - #[pyclass(crate = "crate", extends = MutableBase, subclass)] - struct MutableChildOfMutableBase; - - #[pyclass(crate = "crate", extends = MutableBase, frozen, subclass)] - struct ImmutableChildOfMutableBase; - - #[pyclass(crate = "crate", extends = MutableChildOfMutableBase)] - struct MutableChildOfMutableChildOfMutableBase; - - #[pyclass(crate = "crate", extends = ImmutableChildOfMutableBase)] - struct MutableChildOfImmutableChildOfMutableBase; - - #[pyclass(crate = "crate", extends = MutableChildOfMutableBase, frozen)] - struct ImmutableChildOfMutableChildOfMutableBase; - - #[pyclass(crate = "crate", extends = ImmutableChildOfMutableBase, frozen)] - struct ImmutableChildOfImmutableChildOfMutableBase; - - #[pyclass(crate = "crate", frozen, subclass)] - struct ImmutableBase; - - #[pyclass(crate = "crate", extends = ImmutableBase, subclass)] - struct MutableChildOfImmutableBase; - - #[pyclass(crate = "crate", extends = ImmutableBase, frozen, subclass)] - struct ImmutableChildOfImmutableBase; - - #[pyclass(crate = "crate", extends = MutableChildOfImmutableBase)] - struct MutableChildOfMutableChildOfImmutableBase; - - #[pyclass(crate = "crate", extends = ImmutableChildOfImmutableBase)] - struct MutableChildOfImmutableChildOfImmutableBase; - - #[pyclass(crate = "crate", extends = MutableChildOfImmutableBase, frozen)] - struct ImmutableChildOfMutableChildOfImmutableBase; - - #[pyclass(crate = "crate", extends = ImmutableChildOfImmutableBase, frozen)] - struct ImmutableChildOfImmutableChildOfImmutableBase; - - fn assert_mutable>() {} - fn assert_immutable>() {} - fn assert_mutable_with_mutable_ancestor< - T: PyClass>, - >() { - } - fn assert_immutable_with_mutable_ancestor< - T: PyClass>, - >() { - } - - #[test] - fn test_inherited_mutability() { - // mutable base - assert_mutable::(); - - // children of mutable base have a mutable ancestor - assert_mutable_with_mutable_ancestor::(); - assert_immutable_with_mutable_ancestor::(); - - // grandchildren of mutable base have a mutable ancestor - assert_mutable_with_mutable_ancestor::(); - assert_mutable_with_mutable_ancestor::(); - assert_immutable_with_mutable_ancestor::(); - assert_immutable_with_mutable_ancestor::(); - - // immutable base and children - assert_immutable::(); - assert_immutable::(); - assert_immutable::(); - - // mutable children of immutable at any level are simply mutable - assert_mutable::(); - assert_mutable::(); - - // children of the mutable child display this property - assert_mutable_with_mutable_ancestor::(); - assert_immutable_with_mutable_ancestor::(); - } - - #[test] - fn test_mutable_borrow_prevents_further_borrows() { - Python::with_gil(|py| { - let mmm = Py::new( - py, - PyClassInitializer::from(MutableBase) - .add_subclass(MutableChildOfMutableBase) - .add_subclass(MutableChildOfMutableChildOfMutableBase), - ) - .unwrap(); - - let mmm_bound: &Bound<'_, MutableChildOfMutableChildOfMutableBase> = mmm.bind(py); - - let mmm_refmut = mmm_bound.borrow_mut(); - - // Cannot take any other mutable or immutable borrows whilst the object is borrowed mutably - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound.extract::>().is_err()); - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound.extract::>().is_err()); - - // With the borrow dropped, all other borrow attempts will succeed - drop(mmm_refmut); - - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound.extract::>().is_ok()); - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound.extract::>().is_ok()); - }) - } - - #[test] - fn test_immutable_borrows_prevent_mutable_borrows() { - Python::with_gil(|py| { - let mmm = Py::new( - py, - PyClassInitializer::from(MutableBase) - .add_subclass(MutableChildOfMutableBase) - .add_subclass(MutableChildOfMutableChildOfMutableBase), - ) - .unwrap(); - - let mmm_bound: &Bound<'_, MutableChildOfMutableChildOfMutableBase> = mmm.bind(py); - - let mmm_refmut = mmm_bound.borrow(); - - // Further immutable borrows are ok - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound.extract::>().is_ok()); - - // Further mutable borrows are not ok - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound - .extract::>() - .is_err()); - assert!(mmm_bound.extract::>().is_err()); - - // With the borrow dropped, all mutable borrow attempts will succeed - drop(mmm_refmut); - - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound - .extract::>() - .is_ok()); - assert!(mmm_bound.extract::>().is_ok()); - }) - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] - fn test_thread_safety() { - #[crate::pyclass(crate = "crate")] - struct MyClass { - x: u64, - } - - Python::with_gil(|py| { - let inst = Py::new(py, MyClass { x: 0 }).unwrap(); - - let total_modifications = py.allow_threads(|| { - std::thread::scope(|s| { - // Spawn a bunch of threads all racing to write to - // the same instance of `MyClass`. - let threads = (0..10) - .map(|_| { - s.spawn(|| { - Python::with_gil(|py| { - // Each thread records its own view of how many writes it made - let mut local_modifications = 0; - for _ in 0..100 { - if let Ok(mut i) = inst.try_borrow_mut(py) { - i.x += 1; - local_modifications += 1; - } - } - local_modifications - }) - }) - }) - .collect::>(); - - // Sum up the total number of writes made by all threads - threads.into_iter().map(|t| t.join().unwrap()).sum::() - }) - }); - - // If the implementation is free of data races, the total number of writes - // should match the final value of `x`. - assert_eq!(total_modifications, inst.borrow(py).x); - }); - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] - fn test_thread_safety_2() { - struct SyncUnsafeCell(UnsafeCell); - unsafe impl Sync for SyncUnsafeCell {} - - impl SyncUnsafeCell { - fn get(&self) -> *mut T { - self.0.get() - } - } - - let data = SyncUnsafeCell(UnsafeCell::new(0)); - let data2 = SyncUnsafeCell(UnsafeCell::new(0)); - let borrow_checker = BorrowChecker(BorrowFlag(AtomicUsize::new(BorrowFlag::UNUSED))); - - std::thread::scope(|s| { - s.spawn(|| { - for _ in 0..1_000_000 { - if borrow_checker.try_borrow_mut().is_ok() { - // thread 1 writes to both values during the mutable borrow - unsafe { *data.get() += 1 }; - unsafe { *data2.get() += 1 }; - borrow_checker.release_borrow_mut(); - } - } - }); - - s.spawn(|| { - for _ in 0..1_000_000 { - if borrow_checker.try_borrow().is_ok() { - // if the borrow checker is working correctly, it should be impossible - // for thread 2 to observe a difference in the two values - assert_eq!(unsafe { *data.get() }, unsafe { *data2.get() }); - borrow_checker.release_borrow(); - } - } - }); - }); - } -} diff --git a/vendor/pyo3/src/pyclass.rs b/vendor/pyo3/src/pyclass.rs deleted file mode 100644 index 0315ae5e8e..0000000000 --- a/vendor/pyo3/src/pyclass.rs +++ /dev/null @@ -1,131 +0,0 @@ -//! `PyClass` and related traits. -use crate::{ffi, impl_::pyclass::PyClassImpl, PyTypeInfo}; -use std::{cmp::Ordering, os::raw::c_int}; - -mod create_type_object; -mod gc; - -pub(crate) use self::create_type_object::{create_type_object, PyClassTypeObject}; - -pub use self::gc::{PyTraverseError, PyVisit}; - -/// Types that can be used as Python classes. -/// -/// The `#[pyclass]` attribute implements this trait for your Rust struct - -/// you shouldn't implement this trait directly. -pub trait PyClass: PyTypeInfo + PyClassImpl { - /// Whether the pyclass is frozen. - /// - /// This can be enabled via `#[pyclass(frozen)]`. - type Frozen: Frozen; -} - -/// Operators for the `__richcmp__` method -#[derive(Debug, Clone, Copy)] -pub enum CompareOp { - /// The *less than* operator. - Lt = ffi::Py_LT as isize, - /// The *less than or equal to* operator. - Le = ffi::Py_LE as isize, - /// The equality operator. - Eq = ffi::Py_EQ as isize, - /// The *not equal to* operator. - Ne = ffi::Py_NE as isize, - /// The *greater than* operator. - Gt = ffi::Py_GT as isize, - /// The *greater than or equal to* operator. - Ge = ffi::Py_GE as isize, -} - -impl CompareOp { - /// Conversion from the C enum. - pub fn from_raw(op: c_int) -> Option { - match op { - ffi::Py_LT => Some(CompareOp::Lt), - ffi::Py_LE => Some(CompareOp::Le), - ffi::Py_EQ => Some(CompareOp::Eq), - ffi::Py_NE => Some(CompareOp::Ne), - ffi::Py_GT => Some(CompareOp::Gt), - ffi::Py_GE => Some(CompareOp::Ge), - _ => None, - } - } - - /// Returns if a Rust [`std::cmp::Ordering`] matches this ordering query. - /// - /// Usage example: - /// - /// ```rust - /// # use pyo3::prelude::*; - /// # use pyo3::class::basic::CompareOp; - /// - /// #[pyclass] - /// struct Size { - /// size: usize, - /// } - /// - /// #[pymethods] - /// impl Size { - /// fn __richcmp__(&self, other: &Size, op: CompareOp) -> bool { - /// op.matches(self.size.cmp(&other.size)) - /// } - /// } - /// ``` - pub fn matches(&self, result: Ordering) -> bool { - match self { - CompareOp::Eq => result == Ordering::Equal, - CompareOp::Ne => result != Ordering::Equal, - CompareOp::Lt => result == Ordering::Less, - CompareOp::Le => result != Ordering::Greater, - CompareOp::Gt => result == Ordering::Greater, - CompareOp::Ge => result != Ordering::Less, - } - } -} - -/// A workaround for [associated const equality](https://github.com/rust-lang/rust/issues/92827). -/// -/// This serves to have True / False values in the [`PyClass`] trait's `Frozen` type. -#[doc(hidden)] -pub mod boolean_struct { - pub(crate) mod private { - use super::*; - - /// A way to "seal" the boolean traits. - pub trait Boolean { - const VALUE: bool; - } - - impl Boolean for True { - const VALUE: bool = true; - } - impl Boolean for False { - const VALUE: bool = false; - } - } - - pub struct True(()); - pub struct False(()); -} - -/// A trait which is used to describe whether a `#[pyclass]` is frozen. -#[doc(hidden)] -pub trait Frozen: boolean_struct::private::Boolean {} - -impl Frozen for boolean_struct::True {} -impl Frozen for boolean_struct::False {} - -mod tests { - #[test] - fn test_compare_op_matches() { - use super::CompareOp; - use std::cmp::Ordering; - - assert!(CompareOp::Eq.matches(Ordering::Equal)); - assert!(CompareOp::Ne.matches(Ordering::Less)); - assert!(CompareOp::Ge.matches(Ordering::Greater)); - assert!(CompareOp::Gt.matches(Ordering::Greater)); - assert!(CompareOp::Le.matches(Ordering::Equal)); - assert!(CompareOp::Lt.matches(Ordering::Less)); - } -} diff --git a/vendor/pyo3/src/pyclass/create_type_object.rs b/vendor/pyo3/src/pyclass/create_type_object.rs deleted file mode 100644 index 8a02baa8ad..0000000000 --- a/vendor/pyo3/src/pyclass/create_type_object.rs +++ /dev/null @@ -1,695 +0,0 @@ -use crate::{ - exceptions::PyTypeError, - ffi, - impl_::{ - pycell::PyClassObject, - pyclass::{ - assign_sequence_item_from_mapping, get_sequence_item_from_mapping, tp_dealloc, - tp_dealloc_with_gc, MaybeRuntimePyMethodDef, PyClassItemsIter, - }, - pymethods::{Getter, PyGetterDef, PyMethodDefType, PySetterDef, Setter, _call_clear}, - trampoline::trampoline, - }, - internal_tricks::ptr_from_ref, - types::{typeobject::PyTypeMethods, PyType}, - Py, PyClass, PyResult, PyTypeInfo, Python, -}; -use std::{ - collections::HashMap, - ffi::{CStr, CString}, - os::raw::{c_char, c_int, c_ulong, c_void}, - ptr, -}; - -pub(crate) struct PyClassTypeObject { - pub type_object: Py, - #[allow(dead_code)] // This is purely a cache that must live as long as the type object - getset_destructors: Vec, -} - -pub(crate) fn create_type_object(py: Python<'_>) -> PyResult -where - T: PyClass, -{ - // Written this way to monomorphize the majority of the logic. - #[allow(clippy::too_many_arguments)] - unsafe fn inner( - py: Python<'_>, - base: *mut ffi::PyTypeObject, - dealloc: unsafe extern "C" fn(*mut ffi::PyObject), - dealloc_with_gc: unsafe extern "C" fn(*mut ffi::PyObject), - is_mapping: bool, - is_sequence: bool, - doc: &'static CStr, - dict_offset: Option, - weaklist_offset: Option, - is_basetype: bool, - items_iter: PyClassItemsIter, - name: &'static str, - module: Option<&'static str>, - size_of: usize, - ) -> PyResult { - PyTypeBuilder { - slots: Vec::new(), - method_defs: Vec::new(), - member_defs: Vec::new(), - getset_builders: HashMap::new(), - cleanup: Vec::new(), - tp_base: base, - tp_dealloc: dealloc, - tp_dealloc_with_gc: dealloc_with_gc, - is_mapping, - is_sequence, - has_new: false, - has_dealloc: false, - has_getitem: false, - has_setitem: false, - has_traverse: false, - has_clear: false, - dict_offset: None, - class_flags: 0, - #[cfg(all(not(Py_3_9), not(Py_LIMITED_API)))] - buffer_procs: Default::default(), - } - .type_doc(doc) - .offsets(dict_offset, weaklist_offset) - .set_is_basetype(is_basetype) - .class_items(items_iter) - .build(py, name, module, size_of) - } - - unsafe { - inner( - py, - T::BaseType::type_object_raw(py), - tp_dealloc::, - tp_dealloc_with_gc::, - T::IS_MAPPING, - T::IS_SEQUENCE, - T::doc(py)?, - T::dict_offset(), - T::weaklist_offset(), - T::IS_BASETYPE, - T::items_iter(), - T::NAME, - T::MODULE, - std::mem::size_of::>(), - ) - } -} - -type PyTypeBuilderCleanup = Box; - -struct PyTypeBuilder { - slots: Vec, - method_defs: Vec, - member_defs: Vec, - getset_builders: HashMap<&'static CStr, GetSetDefBuilder>, - /// Used to patch the type objects for the things there's no - /// PyType_FromSpec API for... there's no reason this should work, - /// except for that it does and we have tests. - cleanup: Vec, - tp_base: *mut ffi::PyTypeObject, - tp_dealloc: ffi::destructor, - tp_dealloc_with_gc: ffi::destructor, - is_mapping: bool, - is_sequence: bool, - has_new: bool, - has_dealloc: bool, - has_getitem: bool, - has_setitem: bool, - has_traverse: bool, - has_clear: bool, - dict_offset: Option, - class_flags: c_ulong, - // Before Python 3.9, need to patch in buffer methods manually (they don't work in slots) - #[cfg(all(not(Py_3_9), not(Py_LIMITED_API)))] - buffer_procs: ffi::PyBufferProcs, -} - -impl PyTypeBuilder { - /// # Safety - /// The given pointer must be of the correct type for the given slot - unsafe fn push_slot(&mut self, slot: c_int, pfunc: *mut T) { - match slot { - ffi::Py_tp_new => self.has_new = true, - ffi::Py_tp_dealloc => self.has_dealloc = true, - ffi::Py_mp_subscript => self.has_getitem = true, - ffi::Py_mp_ass_subscript => self.has_setitem = true, - ffi::Py_tp_traverse => { - self.has_traverse = true; - self.class_flags |= ffi::Py_TPFLAGS_HAVE_GC; - } - ffi::Py_tp_clear => self.has_clear = true, - #[cfg(all(not(Py_3_9), not(Py_LIMITED_API)))] - ffi::Py_bf_getbuffer => { - // Safety: slot.pfunc is a valid function pointer - self.buffer_procs.bf_getbuffer = - Some(std::mem::transmute::<*mut T, ffi::getbufferproc>(pfunc)); - } - #[cfg(all(not(Py_3_9), not(Py_LIMITED_API)))] - ffi::Py_bf_releasebuffer => { - // Safety: slot.pfunc is a valid function pointer - self.buffer_procs.bf_releasebuffer = - Some(std::mem::transmute::<*mut T, ffi::releasebufferproc>(pfunc)); - } - _ => {} - } - - self.slots.push(ffi::PyType_Slot { - slot, - pfunc: pfunc as _, - }); - } - - /// # Safety - /// It is the caller's responsibility that `data` is of the correct type for the given slot. - unsafe fn push_raw_vec_slot(&mut self, slot: c_int, mut data: Vec) { - if !data.is_empty() { - // Python expects a zeroed entry to mark the end of the defs - data.push(std::mem::zeroed()); - self.push_slot(slot, Box::into_raw(data.into_boxed_slice()) as *mut c_void); - } - } - - fn pymethod_def(&mut self, def: &PyMethodDefType) { - match def { - PyMethodDefType::Getter(getter) => self - .getset_builders - .entry(getter.name) - .or_default() - .add_getter(getter), - PyMethodDefType::Setter(setter) => self - .getset_builders - .entry(setter.name) - .or_default() - .add_setter(setter), - PyMethodDefType::Method(def) - | PyMethodDefType::Class(def) - | PyMethodDefType::Static(def) => self.method_defs.push(def.as_method_def()), - // These class attributes are added after the type gets created by LazyStaticType - PyMethodDefType::ClassAttribute(_) => {} - PyMethodDefType::StructMember(def) => self.member_defs.push(*def), - } - } - - fn finalize_methods_and_properties(&mut self) -> Vec { - let method_defs: Vec = std::mem::take(&mut self.method_defs); - // Safety: Py_tp_methods expects a raw vec of PyMethodDef - unsafe { self.push_raw_vec_slot(ffi::Py_tp_methods, method_defs) }; - - let member_defs = std::mem::take(&mut self.member_defs); - // Safety: Py_tp_members expects a raw vec of PyMemberDef - unsafe { self.push_raw_vec_slot(ffi::Py_tp_members, member_defs) }; - - let mut getset_destructors = Vec::with_capacity(self.getset_builders.len()); - - #[allow(unused_mut)] - let mut property_defs: Vec<_> = self - .getset_builders - .iter() - .map(|(name, builder)| { - let (def, destructor) = builder.as_get_set_def(name); - getset_destructors.push(destructor); - def - }) - .collect(); - - // PyPy automatically adds __dict__ getter / setter. - #[cfg(not(PyPy))] - // Supported on unlimited API for all versions, and on 3.9+ for limited API - #[cfg(any(Py_3_9, not(Py_LIMITED_API)))] - if let Some(dict_offset) = self.dict_offset { - let get_dict; - let closure; - // PyObject_GenericGetDict not in the limited API until Python 3.10. - #[cfg(any(not(Py_LIMITED_API), Py_3_10))] - { - let _ = dict_offset; - get_dict = ffi::PyObject_GenericGetDict; - closure = ptr::null_mut(); - } - - // ... so we write a basic implementation ourselves - #[cfg(not(any(not(Py_LIMITED_API), Py_3_10)))] - { - extern "C" fn get_dict_impl( - object: *mut ffi::PyObject, - closure: *mut c_void, - ) -> *mut ffi::PyObject { - unsafe { - trampoline(|_| { - let dict_offset = closure as ffi::Py_ssize_t; - // we don't support negative dict_offset here; PyO3 doesn't set it negative - assert!(dict_offset > 0); - // TODO: use `.byte_offset` on MSRV 1.75 - let dict_ptr = object - .cast::() - .offset(dict_offset) - .cast::<*mut ffi::PyObject>(); - if (*dict_ptr).is_null() { - std::ptr::write(dict_ptr, ffi::PyDict_New()); - } - Ok(ffi::compat::Py_XNewRef(*dict_ptr)) - }) - } - } - - get_dict = get_dict_impl; - closure = dict_offset as _; - } - - property_defs.push(ffi::PyGetSetDef { - name: ffi::c_str!("__dict__").as_ptr(), - get: Some(get_dict), - set: Some(ffi::PyObject_GenericSetDict), - doc: ptr::null(), - closure, - }); - } - - // Safety: Py_tp_getset expects a raw vec of PyGetSetDef - unsafe { self.push_raw_vec_slot(ffi::Py_tp_getset, property_defs) }; - - // If mapping methods implemented, define sequence methods get implemented too. - // CPython does the same for Python `class` statements. - - // NB we don't implement sq_length to avoid annoying CPython behaviour of automatically adding - // the length to negative indices. - - // Don't add these methods for "pure" mappings. - - if !self.is_mapping && self.has_getitem { - // Safety: This is the correct slot type for Py_sq_item - unsafe { - self.push_slot( - ffi::Py_sq_item, - get_sequence_item_from_mapping as *mut c_void, - ) - } - } - - if !self.is_mapping && self.has_setitem { - // Safety: This is the correct slot type for Py_sq_ass_item - unsafe { - self.push_slot( - ffi::Py_sq_ass_item, - assign_sequence_item_from_mapping as *mut c_void, - ) - } - } - - getset_destructors - } - - fn set_is_basetype(mut self, is_basetype: bool) -> Self { - if is_basetype { - self.class_flags |= ffi::Py_TPFLAGS_BASETYPE; - } - self - } - - /// # Safety - /// All slots in the PyClassItemsIter should be correct - unsafe fn class_items(mut self, iter: PyClassItemsIter) -> Self { - for items in iter { - for slot in items.slots { - self.push_slot(slot.slot, slot.pfunc); - } - for method in items.methods { - let built_method; - let method = match method { - MaybeRuntimePyMethodDef::Runtime(builder) => { - built_method = builder(); - &built_method - } - MaybeRuntimePyMethodDef::Static(method) => method, - }; - self.pymethod_def(method); - } - } - self - } - - fn type_doc(mut self, type_doc: &'static CStr) -> Self { - let slice = type_doc.to_bytes(); - if !slice.is_empty() { - unsafe { self.push_slot(ffi::Py_tp_doc, type_doc.as_ptr() as *mut c_char) } - - // Running this causes PyPy to segfault. - #[cfg(all(not(PyPy), not(Py_LIMITED_API), not(Py_3_10)))] - { - // Until CPython 3.10, tp_doc was treated specially for - // heap-types, and it removed the text_signature value from it. - // We go in after the fact and replace tp_doc with something - // that _does_ include the text_signature value! - self.cleanup - .push(Box::new(move |_self, type_object| unsafe { - ffi::PyObject_Free((*type_object).tp_doc as _); - let data = ffi::PyMem_Malloc(slice.len()); - data.copy_from(slice.as_ptr() as _, slice.len()); - (*type_object).tp_doc = data as _; - })) - } - } - self - } - - fn offsets( - mut self, - dict_offset: Option, - #[allow(unused_variables)] weaklist_offset: Option, - ) -> Self { - self.dict_offset = dict_offset; - - #[cfg(Py_3_9)] - { - #[inline(always)] - fn offset_def(name: &'static CStr, offset: ffi::Py_ssize_t) -> ffi::PyMemberDef { - ffi::PyMemberDef { - name: name.as_ptr().cast(), - type_code: ffi::Py_T_PYSSIZET, - offset, - flags: ffi::Py_READONLY, - doc: std::ptr::null_mut(), - } - } - - // __dict__ support - if let Some(dict_offset) = dict_offset { - self.member_defs - .push(offset_def(ffi::c_str!("__dictoffset__"), dict_offset)); - } - - // weakref support - if let Some(weaklist_offset) = weaklist_offset { - self.member_defs.push(offset_def( - ffi::c_str!("__weaklistoffset__"), - weaklist_offset, - )); - } - } - - // Setting buffer protocols, tp_dictoffset and tp_weaklistoffset via slots doesn't work until - // Python 3.9, so on older versions we must manually fixup the type object. - #[cfg(all(not(Py_LIMITED_API), not(Py_3_9)))] - { - self.cleanup - .push(Box::new(move |builder, type_object| unsafe { - (*(*type_object).tp_as_buffer).bf_getbuffer = builder.buffer_procs.bf_getbuffer; - (*(*type_object).tp_as_buffer).bf_releasebuffer = - builder.buffer_procs.bf_releasebuffer; - - if let Some(dict_offset) = dict_offset { - (*type_object).tp_dictoffset = dict_offset; - } - - if let Some(weaklist_offset) = weaklist_offset { - (*type_object).tp_weaklistoffset = weaklist_offset; - } - })); - } - self - } - - fn build( - mut self, - py: Python<'_>, - name: &'static str, - module_name: Option<&'static str>, - basicsize: usize, - ) -> PyResult { - // `c_ulong` and `c_uint` have the same size - // on some platforms (like windows) - #![allow(clippy::useless_conversion)] - - let getset_destructors = self.finalize_methods_and_properties(); - - unsafe { self.push_slot(ffi::Py_tp_base, self.tp_base) } - - if !self.has_new { - // Safety: This is the correct slot type for Py_tp_new - unsafe { self.push_slot(ffi::Py_tp_new, no_constructor_defined as *mut c_void) } - } - - let base_is_gc = unsafe { ffi::PyType_IS_GC(self.tp_base) == 1 }; - let tp_dealloc = if self.has_traverse || base_is_gc { - self.tp_dealloc_with_gc - } else { - self.tp_dealloc - }; - unsafe { self.push_slot(ffi::Py_tp_dealloc, tp_dealloc as *mut c_void) } - - if self.has_clear && !self.has_traverse { - return Err(PyTypeError::new_err(format!( - "`#[pyclass]` {} implements __clear__ without __traverse__", - name - ))); - } - - // If this type is a GC type, and the base also is, we may need to add - // `tp_traverse` / `tp_clear` implementations to call the base, if this type didn't - // define `__traverse__` or `__clear__`. - // - // This is because when Py_TPFLAGS_HAVE_GC is set, then `tp_traverse` and - // `tp_clear` are not inherited. - if ((self.class_flags & ffi::Py_TPFLAGS_HAVE_GC) != 0) && base_is_gc { - // If this assertion breaks, need to consider doing the same for __traverse__. - assert!(self.has_traverse); // Py_TPFLAGS_HAVE_GC is set when a `__traverse__` method is found - - if !self.has_clear { - // Safety: This is the correct slot type for Py_tp_clear - unsafe { self.push_slot(ffi::Py_tp_clear, call_super_clear as *mut c_void) } - } - } - - // For sequences, implement sq_length instead of mp_length - if self.is_sequence { - for slot in &mut self.slots { - if slot.slot == ffi::Py_mp_length { - slot.slot = ffi::Py_sq_length; - } - } - } - - // Add empty sentinel at the end - // Safety: python expects this empty slot - unsafe { self.push_slot(0, ptr::null_mut::()) } - - let class_name = py_class_qualified_name(module_name, name)?; - let mut spec = ffi::PyType_Spec { - name: class_name.as_ptr() as _, - basicsize: basicsize as c_int, - itemsize: 0, - - flags: (ffi::Py_TPFLAGS_DEFAULT | self.class_flags) - .try_into() - .unwrap(), - slots: self.slots.as_mut_ptr(), - }; - - // Safety: We've correctly setup the PyType_Spec at this point - let type_object: Py = - unsafe { Py::from_owned_ptr_or_err(py, ffi::PyType_FromSpec(&mut spec))? }; - - #[cfg(not(Py_3_11))] - bpo_45315_workaround(py, class_name); - - for cleanup in std::mem::take(&mut self.cleanup) { - cleanup(&self, type_object.bind(py).as_type_ptr()); - } - - Ok(PyClassTypeObject { - type_object, - getset_destructors, - }) - } -} - -fn py_class_qualified_name(module_name: Option<&str>, class_name: &str) -> PyResult { - Ok(CString::new(format!( - "{}.{}", - module_name.unwrap_or("builtins"), - class_name - ))?) -} - -/// Workaround for Python issue 45315; no longer necessary in Python 3.11 -#[inline] -#[cfg(not(Py_3_11))] -fn bpo_45315_workaround(py: Python<'_>, class_name: CString) { - #[cfg(Py_LIMITED_API)] - { - // Must check version at runtime for abi3 wheels - they could run against a higher version - // than the build config suggests. - use crate::sync::GILOnceCell; - static IS_PYTHON_3_11: GILOnceCell = GILOnceCell::new(); - - if *IS_PYTHON_3_11.get_or_init(py, || py.version_info() >= (3, 11)) { - // No fix needed - the wheel is running on a sufficiently new interpreter. - return; - } - } - #[cfg(not(Py_LIMITED_API))] - { - // suppress unused variable warning - let _ = py; - } - - std::mem::forget(class_name); -} - -/// Default new implementation -unsafe extern "C" fn no_constructor_defined( - subtype: *mut ffi::PyTypeObject, - _args: *mut ffi::PyObject, - _kwds: *mut ffi::PyObject, -) -> *mut ffi::PyObject { - trampoline(|py| { - let tpobj = PyType::from_borrowed_type_ptr(py, subtype); - let name = tpobj - .name() - .map_or_else(|_| "".into(), |name| name.to_string()); - Err(crate::exceptions::PyTypeError::new_err(format!( - "No constructor defined for {}", - name - ))) - }) -} - -unsafe extern "C" fn call_super_clear(slf: *mut ffi::PyObject) -> c_int { - _call_clear(slf, |_, _| Ok(()), call_super_clear) -} - -#[derive(Default)] -struct GetSetDefBuilder { - doc: Option<&'static CStr>, - getter: Option, - setter: Option, -} - -impl GetSetDefBuilder { - fn add_getter(&mut self, getter: &PyGetterDef) { - // TODO: be smarter about merging getter and setter docs - if self.doc.is_none() { - self.doc = Some(getter.doc); - } - // TODO: return an error if getter already defined? - self.getter = Some(getter.meth) - } - - fn add_setter(&mut self, setter: &PySetterDef) { - // TODO: be smarter about merging getter and setter docs - if self.doc.is_none() { - self.doc = Some(setter.doc); - } - // TODO: return an error if setter already defined? - self.setter = Some(setter.meth) - } - - fn as_get_set_def(&self, name: &'static CStr) -> (ffi::PyGetSetDef, GetSetDefDestructor) { - let getset_type = match (self.getter, self.setter) { - (Some(getter), None) => GetSetDefType::Getter(getter), - (None, Some(setter)) => GetSetDefType::Setter(setter), - (Some(getter), Some(setter)) => { - GetSetDefType::GetterAndSetter(Box::new(GetterAndSetter { getter, setter })) - } - (None, None) => { - unreachable!("GetSetDefBuilder expected to always have either getter or setter") - } - }; - - let getset_def = getset_type.create_py_get_set_def(name, self.doc); - let destructor = GetSetDefDestructor { - closure: getset_type, - }; - (getset_def, destructor) - } -} - -#[allow(dead_code)] // a stack of fields which are purely to cache until dropped -struct GetSetDefDestructor { - closure: GetSetDefType, -} - -/// Possible forms of property - either a getter, setter, or both -enum GetSetDefType { - Getter(Getter), - Setter(Setter), - // The box is here so that the `GetterAndSetter` has a stable - // memory address even if the `GetSetDefType` enum is moved - GetterAndSetter(Box), -} - -pub(crate) struct GetterAndSetter { - getter: Getter, - setter: Setter, -} - -impl GetSetDefType { - /// Fills a PyGetSetDef structure - /// It is only valid for as long as this GetSetDefType remains alive, - /// as well as name and doc members - pub(crate) fn create_py_get_set_def( - &self, - name: &CStr, - doc: Option<&CStr>, - ) -> ffi::PyGetSetDef { - let (get, set, closure): (Option, Option, *mut c_void) = - match self { - &Self::Getter(closure) => { - unsafe extern "C" fn getter( - slf: *mut ffi::PyObject, - closure: *mut c_void, - ) -> *mut ffi::PyObject { - // Safety: PyO3 sets the closure when constructing the ffi getter so this cast should always be valid - let getter: Getter = std::mem::transmute(closure); - trampoline(|py| getter(py, slf)) - } - (Some(getter), None, closure as Getter as _) - } - &Self::Setter(closure) => { - unsafe extern "C" fn setter( - slf: *mut ffi::PyObject, - value: *mut ffi::PyObject, - closure: *mut c_void, - ) -> c_int { - // Safety: PyO3 sets the closure when constructing the ffi setter so this cast should always be valid - let setter: Setter = std::mem::transmute(closure); - trampoline(|py| setter(py, slf, value)) - } - (None, Some(setter), closure as Setter as _) - } - Self::GetterAndSetter(closure) => { - unsafe extern "C" fn getset_getter( - slf: *mut ffi::PyObject, - closure: *mut c_void, - ) -> *mut ffi::PyObject { - let getset: &GetterAndSetter = &*closure.cast(); - trampoline(|py| (getset.getter)(py, slf)) - } - - unsafe extern "C" fn getset_setter( - slf: *mut ffi::PyObject, - value: *mut ffi::PyObject, - closure: *mut c_void, - ) -> c_int { - let getset: &GetterAndSetter = &*closure.cast(); - trampoline(|py| (getset.setter)(py, slf, value)) - } - ( - Some(getset_getter), - Some(getset_setter), - ptr_from_ref::(closure) as *mut _, - ) - } - }; - ffi::PyGetSetDef { - name: name.as_ptr(), - doc: doc.map_or(ptr::null(), CStr::as_ptr), - get, - set, - closure, - } - } -} diff --git a/vendor/pyo3/src/pyclass/gc.rs b/vendor/pyo3/src/pyclass/gc.rs deleted file mode 100644 index b95bfa3804..0000000000 --- a/vendor/pyo3/src/pyclass/gc.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::{ - marker::PhantomData, - os::raw::{c_int, c_void}, -}; - -use crate::{ffi, AsPyPointer}; - -/// Error returned by a `__traverse__` visitor implementation. -#[repr(transparent)] -pub struct PyTraverseError(NonZeroCInt); - -impl PyTraverseError { - /// Returns the error code. - pub(crate) fn into_inner(self) -> c_int { - self.0.into() - } -} - -/// Object visitor for GC. -#[derive(Clone)] -pub struct PyVisit<'a> { - pub(crate) visit: ffi::visitproc, - pub(crate) arg: *mut c_void, - /// Prevents the `PyVisit` from outliving the `__traverse__` call. - pub(crate) _guard: PhantomData<&'a ()>, -} - -impl PyVisit<'_> { - /// Visit `obj`. - pub fn call(&self, obj: &T) -> Result<(), PyTraverseError> - where - T: AsPyPointer, - { - let ptr = obj.as_ptr(); - if !ptr.is_null() { - match NonZeroCInt::new(unsafe { (self.visit)(ptr, self.arg) }) { - None => Ok(()), - Some(r) => Err(PyTraverseError(r)), - } - } else { - Ok(()) - } - } -} - -/// Workaround for `NonZero` not being available until MSRV 1.79 -mod get_nonzero_c_int { - pub struct GetNonZeroCInt(); - - pub trait NonZeroCIntType { - type Type; - } - impl NonZeroCIntType for GetNonZeroCInt<16> { - type Type = std::num::NonZeroI16; - } - impl NonZeroCIntType for GetNonZeroCInt<32> { - type Type = std::num::NonZeroI32; - } - - pub type Type = - () * 8 }> as NonZeroCIntType>::Type; -} - -use get_nonzero_c_int::Type as NonZeroCInt; - -#[cfg(test)] -mod tests { - use super::PyVisit; - use static_assertions::assert_not_impl_any; - - #[test] - fn py_visit_not_send_sync() { - assert_not_impl_any!(PyVisit<'_>: Send, Sync); - } -} diff --git a/vendor/pyo3/src/pyclass_init.rs b/vendor/pyo3/src/pyclass_init.rs deleted file mode 100644 index 6dc6ec12c6..0000000000 --- a/vendor/pyo3/src/pyclass_init.rs +++ /dev/null @@ -1,291 +0,0 @@ -//! Contains initialization utilities for `#[pyclass]`. -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::impl_::callback::IntoPyCallbackOutput; -use crate::impl_::pyclass::{PyClassBaseType, PyClassDict, PyClassThreadChecker, PyClassWeakRef}; -use crate::impl_::pyclass_init::{PyNativeTypeInitializer, PyObjectInit}; -use crate::types::PyAnyMethods; -use crate::{ffi, Bound, Py, PyClass, PyResult, Python}; -use crate::{ - ffi::PyTypeObject, - pycell::impl_::{PyClassBorrowChecker, PyClassMutability, PyClassObjectContents}, -}; -use std::{ - cell::UnsafeCell, - marker::PhantomData, - mem::{ManuallyDrop, MaybeUninit}, -}; - -/// Initializer for our `#[pyclass]` system. -/// -/// You can use this type to initialize complicatedly nested `#[pyclass]`. -/// -/// # Examples -/// -/// ``` -/// # use pyo3::prelude::*; -/// # use pyo3::py_run; -/// #[pyclass(subclass)] -/// struct BaseClass { -/// #[pyo3(get)] -/// basename: &'static str, -/// } -/// #[pyclass(extends=BaseClass, subclass)] -/// struct SubClass { -/// #[pyo3(get)] -/// subname: &'static str, -/// } -/// #[pyclass(extends=SubClass)] -/// struct SubSubClass { -/// #[pyo3(get)] -/// subsubname: &'static str, -/// } -/// -/// #[pymethods] -/// impl SubSubClass { -/// #[new] -/// fn new() -> PyClassInitializer { -/// PyClassInitializer::from(BaseClass { basename: "base" }) -/// .add_subclass(SubClass { subname: "sub" }) -/// .add_subclass(SubSubClass { -/// subsubname: "subsub", -/// }) -/// } -/// } -/// Python::with_gil(|py| { -/// let typeobj = py.get_type::(); -/// let sub_sub_class = typeobj.call((), None).unwrap(); -/// py_run!( -/// py, -/// sub_sub_class, -/// r#" -/// assert sub_sub_class.basename == 'base' -/// assert sub_sub_class.subname == 'sub' -/// assert sub_sub_class.subsubname == 'subsub'"# -/// ); -/// }); -/// ``` -pub struct PyClassInitializer(PyClassInitializerImpl); - -enum PyClassInitializerImpl { - Existing(Py), - New { - init: T, - super_init: ::Initializer, - }, -} - -impl PyClassInitializer { - /// Constructs a new initializer from value `T` and base class' initializer. - /// - /// It is recommended to use `add_subclass` instead of this method for most usage. - #[track_caller] - #[inline] - pub fn new(init: T, super_init: ::Initializer) -> Self { - // This is unsound; see https://github.com/PyO3/pyo3/issues/4452. - assert!( - super_init.can_be_subclassed(), - "you cannot add a subclass to an existing value", - ); - Self(PyClassInitializerImpl::New { init, super_init }) - } - - /// Constructs a new initializer from an initializer for the base class. - /// - /// # Examples - /// ``` - /// use pyo3::prelude::*; - /// - /// #[pyclass(subclass)] - /// struct BaseClass { - /// #[pyo3(get)] - /// value: i32, - /// } - /// - /// impl BaseClass { - /// fn new(value: i32) -> PyResult { - /// Ok(Self { value }) - /// } - /// } - /// - /// #[pyclass(extends=BaseClass)] - /// struct SubClass {} - /// - /// #[pymethods] - /// impl SubClass { - /// #[new] - /// fn new(value: i32) -> PyResult> { - /// let base_init = PyClassInitializer::from(BaseClass::new(value)?); - /// Ok(base_init.add_subclass(SubClass {})) - /// } - /// } - /// - /// fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let m = PyModule::new(py, "example")?; - /// m.add_class::()?; - /// m.add_class::()?; - /// - /// let instance = m.getattr("SubClass")?.call1((92,))?; - /// - /// // `SubClass` does not have a `value` attribute, but `BaseClass` does. - /// let n = instance.getattr("value")?.extract::()?; - /// assert_eq!(n, 92); - /// - /// Ok(()) - /// }) - /// } - /// ``` - #[track_caller] - #[inline] - pub fn add_subclass(self, subclass_value: S) -> PyClassInitializer - where - S: PyClass, - S::BaseType: PyClassBaseType, - { - PyClassInitializer::new(subclass_value, self) - } - - /// Creates a new PyCell and initializes it. - pub(crate) fn create_class_object(self, py: Python<'_>) -> PyResult> - where - T: PyClass, - { - unsafe { self.create_class_object_of_type(py, T::type_object_raw(py)) } - } - - /// Creates a new class object and initializes it given a typeobject `subtype`. - /// - /// # Safety - /// `subtype` must be a valid pointer to the type object of T or a subclass. - pub(crate) unsafe fn create_class_object_of_type( - self, - py: Python<'_>, - target_type: *mut crate::ffi::PyTypeObject, - ) -> PyResult> - where - T: PyClass, - { - /// Layout of a PyClassObject after base new has been called, but the contents have not yet been - /// written. - #[repr(C)] - struct PartiallyInitializedClassObject { - _ob_base: ::LayoutAsBase, - contents: MaybeUninit>, - } - - let (init, super_init) = match self.0 { - PyClassInitializerImpl::Existing(value) => return Ok(value.into_bound(py)), - PyClassInitializerImpl::New { init, super_init } => (init, super_init), - }; - - let obj = super_init.into_new_object(py, target_type)?; - - let part_init: *mut PartiallyInitializedClassObject = obj.cast(); - std::ptr::write( - (*part_init).contents.as_mut_ptr(), - PyClassObjectContents { - value: ManuallyDrop::new(UnsafeCell::new(init)), - borrow_checker: ::Storage::new(), - thread_checker: T::ThreadChecker::new(), - dict: T::Dict::INIT, - weakref: T::WeakRef::INIT, - }, - ); - - // Safety: obj is a valid pointer to an object of type `target_type`, which` is a known - // subclass of `T` - Ok(obj.assume_owned(py).downcast_into_unchecked()) - } -} - -impl PyObjectInit for PyClassInitializer { - unsafe fn into_new_object( - self, - py: Python<'_>, - subtype: *mut PyTypeObject, - ) -> PyResult<*mut ffi::PyObject> { - self.create_class_object_of_type(py, subtype) - .map(Bound::into_ptr) - } - - #[inline] - fn can_be_subclassed(&self) -> bool { - !matches!(self.0, PyClassInitializerImpl::Existing(..)) - } -} - -impl From for PyClassInitializer -where - T: PyClass, - T::BaseType: PyClassBaseType>, -{ - #[inline] - fn from(value: T) -> PyClassInitializer { - Self::new(value, PyNativeTypeInitializer(PhantomData)) - } -} - -impl From<(S, B)> for PyClassInitializer -where - S: PyClass, - B: PyClass + PyClassBaseType>, - B::BaseType: PyClassBaseType>, -{ - #[track_caller] - #[inline] - fn from(sub_and_base: (S, B)) -> PyClassInitializer { - let (sub, base) = sub_and_base; - PyClassInitializer::from(base).add_subclass(sub) - } -} - -impl From> for PyClassInitializer { - #[inline] - fn from(value: Py) -> PyClassInitializer { - PyClassInitializer(PyClassInitializerImpl::Existing(value)) - } -} - -impl<'py, T: PyClass> From> for PyClassInitializer { - #[inline] - fn from(value: Bound<'py, T>) -> PyClassInitializer { - PyClassInitializer::from(value.unbind()) - } -} - -// Implementation used by proc macros to allow anything convertible to PyClassInitializer to be -// the return value of pyclass #[new] method (optionally wrapped in `Result`). -impl IntoPyCallbackOutput<'_, PyClassInitializer> for U -where - T: PyClass, - U: Into>, -{ - #[inline] - fn convert(self, _py: Python<'_>) -> PyResult> { - Ok(self.into()) - } -} - -#[cfg(all(test, feature = "macros"))] -mod tests { - //! See https://github.com/PyO3/pyo3/issues/4452. - - use crate::prelude::*; - - #[pyclass(crate = "crate", subclass)] - struct BaseClass {} - - #[pyclass(crate = "crate", extends=BaseClass)] - struct SubClass { - _data: i32, - } - - #[test] - #[should_panic] - fn add_subclass_to_py_is_unsound() { - Python::with_gil(|py| { - let base = Py::new(py, BaseClass {}).unwrap(); - let _subclass = PyClassInitializer::from(base).add_subclass(SubClass { _data: 42 }); - }); - } -} diff --git a/vendor/pyo3/src/sealed.rs b/vendor/pyo3/src/sealed.rs deleted file mode 100644 index 0a2846b134..0000000000 --- a/vendor/pyo3/src/sealed.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::types::{ - PyBool, PyByteArray, PyBytes, PyCapsule, PyComplex, PyDict, PyFloat, PyFrozenSet, PyList, - PyMapping, PyMappingProxy, PyModule, PySequence, PySet, PySlice, PyString, PyTraceback, - PyTuple, PyType, PyWeakref, PyWeakrefProxy, PyWeakrefReference, -}; -use crate::{ffi, Bound, PyAny, PyResult}; - -use crate::pyclass_init::PyClassInitializer; - -use crate::impl_::{ - pyclass_init::PyNativeTypeInitializer, - pymethods::PyMethodDef, - pymodule::{AddClassToModule, AddTypeToModule, ModuleDef}, -}; - -pub trait Sealed {} - -// for FfiPtrExt -impl Sealed for *mut ffi::PyObject {} - -// for PyResultExt -impl Sealed for PyResult> {} - -// for Py(...)Methods -impl Sealed for Bound<'_, PyAny> {} -impl Sealed for Bound<'_, PyBool> {} -impl Sealed for Bound<'_, PyByteArray> {} -impl Sealed for Bound<'_, PyBytes> {} -impl Sealed for Bound<'_, PyCapsule> {} -impl Sealed for Bound<'_, PyComplex> {} -impl Sealed for Bound<'_, PyDict> {} -impl Sealed for Bound<'_, PyFloat> {} -impl Sealed for Bound<'_, PyFrozenSet> {} -impl Sealed for Bound<'_, PyList> {} -impl Sealed for Bound<'_, PyMapping> {} -impl Sealed for Bound<'_, PyMappingProxy> {} -impl Sealed for Bound<'_, PyModule> {} -impl Sealed for Bound<'_, PySequence> {} -impl Sealed for Bound<'_, PySet> {} -impl Sealed for Bound<'_, PySlice> {} -impl Sealed for Bound<'_, PyString> {} -impl Sealed for Bound<'_, PyTraceback> {} -impl Sealed for Bound<'_, PyTuple> {} -impl Sealed for Bound<'_, PyType> {} -impl Sealed for Bound<'_, PyWeakref> {} -impl Sealed for Bound<'_, PyWeakrefProxy> {} -impl Sealed for Bound<'_, PyWeakrefReference> {} - -impl Sealed for AddTypeToModule {} -impl Sealed for AddClassToModule {} -impl Sealed for PyMethodDef {} -impl Sealed for ModuleDef {} - -impl Sealed for PyNativeTypeInitializer {} -impl Sealed for PyClassInitializer {} - -impl Sealed for std::sync::Once {} diff --git a/vendor/pyo3/src/sync.rs b/vendor/pyo3/src/sync.rs deleted file mode 100644 index 0845eaf8ce..0000000000 --- a/vendor/pyo3/src/sync.rs +++ /dev/null @@ -1,784 +0,0 @@ -//! Synchronization mechanisms based on the Python GIL. -//! -//! With the acceptance of [PEP 703] (aka a "freethreaded Python") for Python 3.13, these -//! are likely to undergo significant developments in the future. -//! -//! [PEP 703]: https://peps.python.org/pep-703/ -use crate::{ - ffi, - sealed::Sealed, - types::{any::PyAnyMethods, PyAny, PyString}, - Bound, Py, PyResult, PyTypeCheck, Python, -}; -use std::{ - cell::UnsafeCell, - marker::PhantomData, - mem::MaybeUninit, - sync::{Once, OnceState}, -}; - -#[cfg(not(Py_GIL_DISABLED))] -use crate::PyVisit; - -/// Value with concurrent access protected by the GIL. -/// -/// This is a synchronization primitive based on Python's global interpreter lock (GIL). -/// It ensures that only one thread at a time can access the inner value via shared references. -/// It can be combined with interior mutability to obtain mutable references. -/// -/// This type is not defined for extensions built against the free-threaded CPython ABI. -/// -/// # Example -/// -/// Combining `GILProtected` with `RefCell` enables mutable access to static data: -/// -/// ``` -/// # use pyo3::prelude::*; -/// use pyo3::sync::GILProtected; -/// use std::cell::RefCell; -/// -/// static NUMBERS: GILProtected>> = GILProtected::new(RefCell::new(Vec::new())); -/// -/// Python::with_gil(|py| { -/// NUMBERS.get(py).borrow_mut().push(42); -/// }); -/// ``` -#[cfg(not(Py_GIL_DISABLED))] -pub struct GILProtected { - value: T, -} - -#[cfg(not(Py_GIL_DISABLED))] -impl GILProtected { - /// Place the given value under the protection of the GIL. - pub const fn new(value: T) -> Self { - Self { value } - } - - /// Gain access to the inner value by giving proof of having acquired the GIL. - pub fn get<'py>(&'py self, _py: Python<'py>) -> &'py T { - &self.value - } - - /// Gain access to the inner value by giving proof that garbage collection is happening. - pub fn traverse<'py>(&'py self, _visit: PyVisit<'py>) -> &'py T { - &self.value - } -} - -#[cfg(not(Py_GIL_DISABLED))] -unsafe impl Sync for GILProtected where T: Send {} - -/// A write-once primitive similar to [`std::sync::OnceLock`]. -/// -/// Unlike `OnceLock` which blocks threads to achieve thread safety, `GilOnceCell` -/// allows calls to [`get_or_init`][GILOnceCell::get_or_init] and -/// [`get_or_try_init`][GILOnceCell::get_or_try_init] to race to create an initialized value. -/// (It is still guaranteed that only one thread will ever write to the cell.) -/// -/// On Python versions that run with the Global Interpreter Lock (GIL), this helps to avoid -/// deadlocks between initialization and the GIL. For an example of such a deadlock, see -#[doc = concat!( - "[the FAQ section](https://pyo3.rs/v", - env!("CARGO_PKG_VERSION"), - "/faq.html#im-experiencing-deadlocks-using-pyo3-with-stdsynconcelock-stdsynclazylock-lazy_static-and-once_cell)" -)] -/// of the guide. -/// -/// Note that because the GIL blocks concurrent execution, in practice the means that -/// [`get_or_init`][GILOnceCell::get_or_init] and -/// [`get_or_try_init`][GILOnceCell::get_or_try_init] may race if the initialization -/// function leads to the GIL being released and a thread context switch. This can -/// happen when importing or calling any Python code, as long as it releases the -/// GIL at some point. On free-threaded Python without any GIL, the race is -/// more likely since there is no GIL to prevent races. In the future, PyO3 may change -/// the semantics of GILOnceCell to behave more like the GIL build in the future. -/// -/// # Re-entrant initialization -/// -/// [`get_or_init`][GILOnceCell::get_or_init] and -/// [`get_or_try_init`][GILOnceCell::get_or_try_init] do not protect against infinite recursion -/// from reentrant initialization. -/// -/// # Examples -/// -/// The following example shows how to use `GILOnceCell` to share a reference to a Python list -/// between threads: -/// -/// ``` -/// use pyo3::sync::GILOnceCell; -/// use pyo3::prelude::*; -/// use pyo3::types::PyList; -/// -/// static LIST_CELL: GILOnceCell> = GILOnceCell::new(); -/// -/// pub fn get_shared_list(py: Python<'_>) -> &Bound<'_, PyList> { -/// LIST_CELL -/// .get_or_init(py, || PyList::empty(py).unbind()) -/// .bind(py) -/// } -/// # Python::with_gil(|py| assert_eq!(get_shared_list(py).len(), 0)); -/// ``` -pub struct GILOnceCell { - once: Once, - data: UnsafeCell>, - - /// (Copied from std::sync::OnceLock) - /// - /// `PhantomData` to make sure dropck understands we're dropping T in our Drop impl. - /// - /// ```compile_error,E0597 - /// use pyo3::Python; - /// use pyo3::sync::GILOnceCell; - /// - /// struct A<'a>(#[allow(dead_code)] &'a str); - /// - /// impl<'a> Drop for A<'a> { - /// fn drop(&mut self) {} - /// } - /// - /// let cell = GILOnceCell::new(); - /// { - /// let s = String::new(); - /// let _ = Python::with_gil(|py| cell.set(py,A(&s))); - /// } - /// ``` - _marker: PhantomData, -} - -impl Default for GILOnceCell { - fn default() -> Self { - Self::new() - } -} - -// T: Send is needed for Sync because the thread which drops the GILOnceCell can be different -// to the thread which fills it. (e.g. think scoped thread which fills the cell and then exits, -// leaving the cell to be dropped by the main thread). -unsafe impl Sync for GILOnceCell {} -unsafe impl Send for GILOnceCell {} - -impl GILOnceCell { - /// Create a `GILOnceCell` which does not yet contain a value. - pub const fn new() -> Self { - Self { - once: Once::new(), - data: UnsafeCell::new(MaybeUninit::uninit()), - _marker: PhantomData, - } - } - - /// Get a reference to the contained value, or `None` if the cell has not yet been written. - #[inline] - pub fn get(&self, _py: Python<'_>) -> Option<&T> { - if self.once.is_completed() { - // SAFETY: the cell has been written. - Some(unsafe { (*self.data.get()).assume_init_ref() }) - } else { - None - } - } - - /// Get a reference to the contained value, initializing it if needed using the provided - /// closure. - /// - /// See the type-level documentation for detail on re-entrancy and concurrent initialization. - #[inline] - pub fn get_or_init(&self, py: Python<'_>, f: F) -> &T - where - F: FnOnce() -> T, - { - if let Some(value) = self.get(py) { - return value; - } - - // .unwrap() will never panic because the result is always Ok - self.init(py, || Ok::(f())) - .unwrap() - } - - /// Like `get_or_init`, but accepts a fallible initialization function. If it fails, the cell - /// is left uninitialized. - /// - /// See the type-level documentation for detail on re-entrancy and concurrent initialization. - #[inline] - pub fn get_or_try_init(&self, py: Python<'_>, f: F) -> Result<&T, E> - where - F: FnOnce() -> Result, - { - if let Some(value) = self.get(py) { - return Ok(value); - } - - self.init(py, f) - } - - #[cold] - fn init(&self, py: Python<'_>, f: F) -> Result<&T, E> - where - F: FnOnce() -> Result, - { - // Note that f() could temporarily release the GIL, so it's possible that another thread - // writes to this GILOnceCell before f() finishes. That's fine; we'll just have to discard - // the value computed here and accept a bit of wasted computation. - - // TODO: on the freethreaded build, consider wrapping this pair of operations in a - // critical section (requires a critical section API which can use a PyMutex without - // an object.) - let value = f()?; - let _ = self.set(py, value); - - Ok(self.get(py).unwrap()) - } - - /// Get the contents of the cell mutably. This is only possible if the reference to the cell is - /// unique. - pub fn get_mut(&mut self) -> Option<&mut T> { - if self.once.is_completed() { - // SAFETY: the cell has been written. - Some(unsafe { (*self.data.get()).assume_init_mut() }) - } else { - None - } - } - - /// Set the value in the cell. - /// - /// If the cell has already been written, `Err(value)` will be returned containing the new - /// value which was not written. - pub fn set(&self, _py: Python<'_>, value: T) -> Result<(), T> { - let mut value = Some(value); - // NB this can block, but since this is only writing a single value and - // does not call arbitrary python code, we don't need to worry about - // deadlocks with the GIL. - self.once.call_once_force(|_| { - // SAFETY: no other threads can be writing this value, because we are - // inside the `call_once_force` closure. - unsafe { - // `.take().unwrap()` will never panic - (*self.data.get()).write(value.take().unwrap()); - } - }); - - match value { - // Some other thread wrote to the cell first - Some(value) => Err(value), - None => Ok(()), - } - } - - /// Takes the value out of the cell, moving it back to an uninitialized state. - /// - /// Has no effect and returns None if the cell has not yet been written. - pub fn take(&mut self) -> Option { - if self.once.is_completed() { - // Reset the cell to its default state so that it won't try to - // drop the value again. - self.once = Once::new(); - // SAFETY: the cell has been written. `self.once` has been reset, - // so when `self` is dropped the value won't be read again. - Some(unsafe { self.data.get_mut().assume_init_read() }) - } else { - None - } - } - - /// Consumes the cell, returning the wrapped value. - /// - /// Returns None if the cell has not yet been written. - pub fn into_inner(mut self) -> Option { - self.take() - } -} - -impl GILOnceCell> { - /// Creates a new cell that contains a new Python reference to the same contained object. - /// - /// Returns an uninitialized cell if `self` has not yet been initialized. - pub fn clone_ref(&self, py: Python<'_>) -> Self { - let cloned = Self { - once: Once::new(), - data: UnsafeCell::new(MaybeUninit::uninit()), - _marker: PhantomData, - }; - if let Some(value) = self.get(py) { - let _ = cloned.set(py, value.clone_ref(py)); - } - cloned - } -} - -impl GILOnceCell> -where - T: PyTypeCheck, -{ - /// Get a reference to the contained Python type, initializing the cell if needed. - /// - /// This is a shorthand method for `get_or_init` which imports the type from Python on init. - /// - /// # Example: Using `GILOnceCell` to store a class in a static variable. - /// - /// `GILOnceCell` can be used to avoid importing a class multiple times: - /// ``` - /// # use pyo3::prelude::*; - /// # use pyo3::sync::GILOnceCell; - /// # use pyo3::types::{PyDict, PyType}; - /// # use pyo3::intern; - /// # - /// #[pyfunction] - /// fn create_ordered_dict<'py>(py: Python<'py>, dict: Bound<'py, PyDict>) -> PyResult> { - /// // Even if this function is called multiple times, - /// // the `OrderedDict` class will be imported only once. - /// static ORDERED_DICT: GILOnceCell> = GILOnceCell::new(); - /// ORDERED_DICT - /// .import(py, "collections", "OrderedDict")? - /// .call1((dict,)) - /// } - /// - /// # Python::with_gil(|py| { - /// # let dict = PyDict::new(py); - /// # dict.set_item(intern!(py, "foo"), 42).unwrap(); - /// # let fun = wrap_pyfunction!(create_ordered_dict, py).unwrap(); - /// # let ordered_dict = fun.call1((&dict,)).unwrap(); - /// # assert!(dict.eq(ordered_dict).unwrap()); - /// # }); - /// ``` - pub fn import<'py>( - &self, - py: Python<'py>, - module_name: &str, - attr_name: &str, - ) -> PyResult<&Bound<'py, T>> { - self.get_or_try_init(py, || { - let type_object = py - .import(module_name)? - .getattr(attr_name)? - .downcast_into()?; - Ok(type_object.unbind()) - }) - .map(|ty| ty.bind(py)) - } -} - -impl Drop for GILOnceCell { - fn drop(&mut self) { - if self.once.is_completed() { - // SAFETY: the cell has been written. - unsafe { MaybeUninit::assume_init_drop(self.data.get_mut()) } - } - } -} - -/// Interns `text` as a Python string and stores a reference to it in static storage. -/// -/// A reference to the same Python string is returned on each invocation. -/// -/// # Example: Using `intern!` to avoid needlessly recreating the same Python string -/// -/// ``` -/// use pyo3::intern; -/// # use pyo3::{prelude::*, types::PyDict}; -/// -/// #[pyfunction] -/// fn create_dict(py: Python<'_>) -> PyResult> { -/// let dict = PyDict::new(py); -/// // 👇 A new `PyString` is created -/// // for every call of this function. -/// dict.set_item("foo", 42)?; -/// Ok(dict) -/// } -/// -/// #[pyfunction] -/// fn create_dict_faster(py: Python<'_>) -> PyResult> { -/// let dict = PyDict::new(py); -/// // 👇 A `PyString` is created once and reused -/// // for the lifetime of the program. -/// dict.set_item(intern!(py, "foo"), 42)?; -/// Ok(dict) -/// } -/// # -/// # Python::with_gil(|py| { -/// # let fun_slow = wrap_pyfunction!(create_dict, py).unwrap(); -/// # let dict = fun_slow.call0().unwrap(); -/// # assert!(dict.contains("foo").unwrap()); -/// # let fun = wrap_pyfunction!(create_dict_faster, py).unwrap(); -/// # let dict = fun.call0().unwrap(); -/// # assert!(dict.contains("foo").unwrap()); -/// # }); -/// ``` -#[macro_export] -macro_rules! intern { - ($py: expr, $text: expr) => {{ - static INTERNED: $crate::sync::Interned = $crate::sync::Interned::new($text); - INTERNED.get($py) - }}; -} - -/// Implementation detail for `intern!` macro. -#[doc(hidden)] -pub struct Interned(&'static str, GILOnceCell>); - -impl Interned { - /// Creates an empty holder for an interned `str`. - pub const fn new(value: &'static str) -> Self { - Interned(value, GILOnceCell::new()) - } - - /// Gets or creates the interned `str` value. - #[inline] - pub fn get<'py>(&self, py: Python<'py>) -> &Bound<'py, PyString> { - self.1 - .get_or_init(py, || PyString::intern(py, self.0).into()) - .bind(py) - } -} - -/// Executes a closure with a Python critical section held on an object. -/// -/// Acquires the per-object lock for the object `op` that is held -/// until the closure `f` is finished. -/// -/// This is structurally equivalent to the use of the paired -/// Py_BEGIN_CRITICAL_SECTION and Py_END_CRITICAL_SECTION C-API macros. -/// -/// A no-op on GIL-enabled builds, where the critical section API is exposed as -/// a no-op by the Python C API. -/// -/// Provides weaker locking guarantees than traditional locks, but can in some -/// cases be used to provide guarantees similar to the GIL without the risk of -/// deadlocks associated with traditional locks. -/// -/// Many CPython C API functions do not acquire the per-object lock on objects -/// passed to Python. You should not expect critical sections applied to -/// built-in types to prevent concurrent modification. This API is most useful -/// for user-defined types with full control over how the internal state for the -/// type is managed. -#[cfg_attr(not(Py_GIL_DISABLED), allow(unused_variables))] -pub fn with_critical_section(object: &Bound<'_, PyAny>, f: F) -> R -where - F: FnOnce() -> R, -{ - #[cfg(Py_GIL_DISABLED)] - { - struct Guard(crate::ffi::PyCriticalSection); - - impl Drop for Guard { - fn drop(&mut self) { - unsafe { - crate::ffi::PyCriticalSection_End(&mut self.0); - } - } - } - - let mut guard = Guard(unsafe { std::mem::zeroed() }); - unsafe { crate::ffi::PyCriticalSection_Begin(&mut guard.0, object.as_ptr()) }; - f() - } - #[cfg(not(Py_GIL_DISABLED))] - { - f() - } -} - -#[cfg(rustc_has_once_lock)] -mod once_lock_ext_sealed { - pub trait Sealed {} - impl Sealed for std::sync::OnceLock {} -} - -/// Helper trait for `Once` to help avoid deadlocking when using a `Once` when attached to a -/// Python thread. -pub trait OnceExt: Sealed { - /// Similar to [`call_once`][Once::call_once], but releases the Python GIL temporarily - /// if blocking on another thread currently calling this `Once`. - fn call_once_py_attached(&self, py: Python<'_>, f: impl FnOnce()); - - /// Similar to [`call_once_force`][Once::call_once_force], but releases the Python GIL - /// temporarily if blocking on another thread currently calling this `Once`. - fn call_once_force_py_attached(&self, py: Python<'_>, f: impl FnOnce(&OnceState)); -} - -// Extension trait for [`std::sync::OnceLock`] which helps avoid deadlocks between the Python -/// interpreter and initialization with the `OnceLock`. -#[cfg(rustc_has_once_lock)] -pub trait OnceLockExt: once_lock_ext_sealed::Sealed { - /// Initializes this `OnceLock` with the given closure if it has not been initialized yet. - /// - /// If this function would block, this function detaches from the Python interpreter and - /// reattaches before calling `f`. This avoids deadlocks between the Python interpreter and - /// the `OnceLock` in cases where `f` can call arbitrary Python code, as calling arbitrary - /// Python code can lead to `f` itself blocking on the Python interpreter. - /// - /// By detaching from the Python interpreter before blocking, this ensures that if `f` blocks - /// then the Python interpreter cannot be blocked by `f` itself. - fn get_or_init_py_attached(&self, py: Python<'_>, f: F) -> &T - where - F: FnOnce() -> T; -} - -struct Guard(*mut crate::ffi::PyThreadState); - -impl Drop for Guard { - fn drop(&mut self) { - unsafe { ffi::PyEval_RestoreThread(self.0) }; - } -} - -impl OnceExt for Once { - fn call_once_py_attached(&self, py: Python<'_>, f: impl FnOnce()) { - if self.is_completed() { - return; - } - - init_once_py_attached(self, py, f) - } - - fn call_once_force_py_attached(&self, py: Python<'_>, f: impl FnOnce(&OnceState)) { - if self.is_completed() { - return; - } - - init_once_force_py_attached(self, py, f); - } -} - -#[cfg(rustc_has_once_lock)] -impl OnceLockExt for std::sync::OnceLock { - fn get_or_init_py_attached(&self, py: Python<'_>, f: F) -> &T - where - F: FnOnce() -> T, - { - // this trait is guarded by a rustc version config - // so clippy's MSRV check is wrong - #[allow(clippy::incompatible_msrv)] - // Use self.get() first to create a fast path when initialized - self.get() - .unwrap_or_else(|| init_once_lock_py_attached(self, py, f)) - } -} - -#[cold] -fn init_once_py_attached(once: &Once, _py: Python<'_>, f: F) -where - F: FnOnce() -> T, -{ - // Safety: we are currently attached to the GIL, and we expect to block. We will save - // the current thread state and restore it as soon as we are done blocking. - let ts_guard = Guard(unsafe { ffi::PyEval_SaveThread() }); - - once.call_once(move || { - drop(ts_guard); - f(); - }); -} - -#[cold] -fn init_once_force_py_attached(once: &Once, _py: Python<'_>, f: F) -where - F: FnOnce(&OnceState) -> T, -{ - // Safety: we are currently attached to the GIL, and we expect to block. We will save - // the current thread state and restore it as soon as we are done blocking. - let ts_guard = Guard(unsafe { ffi::PyEval_SaveThread() }); - - once.call_once_force(move |state| { - drop(ts_guard); - f(state); - }); -} - -#[cfg(rustc_has_once_lock)] -#[cold] -fn init_once_lock_py_attached<'a, F, T>( - lock: &'a std::sync::OnceLock, - _py: Python<'_>, - f: F, -) -> &'a T -where - F: FnOnce() -> T, -{ - // SAFETY: we are currently attached to a Python thread - let ts_guard = Guard(unsafe { ffi::PyEval_SaveThread() }); - - // this trait is guarded by a rustc version config - // so clippy's MSRV check is wrong - #[allow(clippy::incompatible_msrv)] - // By having detached here, we guarantee that `.get_or_init` cannot deadlock with - // the Python interpreter - let value = lock.get_or_init(move || { - drop(ts_guard); - f() - }); - - value -} - -#[cfg(test)] -mod tests { - use super::*; - - use crate::types::{PyDict, PyDictMethods}; - - #[test] - fn test_intern() { - Python::with_gil(|py| { - let foo1 = "foo"; - let foo2 = intern!(py, "foo"); - let foo3 = intern!(py, stringify!(foo)); - - let dict = PyDict::new(py); - dict.set_item(foo1, 42_usize).unwrap(); - assert!(dict.contains(foo2).unwrap()); - assert_eq!( - dict.get_item(foo3) - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 42 - ); - }); - } - - #[test] - fn test_once_cell() { - Python::with_gil(|py| { - let mut cell = GILOnceCell::new(); - - assert!(cell.get(py).is_none()); - - assert_eq!(cell.get_or_try_init(py, || Err(5)), Err(5)); - assert!(cell.get(py).is_none()); - - assert_eq!(cell.get_or_try_init(py, || Ok::<_, ()>(2)), Ok(&2)); - assert_eq!(cell.get(py), Some(&2)); - - assert_eq!(cell.get_or_try_init(py, || Err(5)), Ok(&2)); - - assert_eq!(cell.take(), Some(2)); - assert_eq!(cell.into_inner(), None); - - let cell_py = GILOnceCell::new(); - assert!(cell_py.clone_ref(py).get(py).is_none()); - cell_py.get_or_init(py, || py.None()); - assert!(cell_py.clone_ref(py).get(py).unwrap().is_none(py)); - }) - } - - #[test] - fn test_once_cell_drop() { - #[derive(Debug)] - struct RecordDrop<'a>(&'a mut bool); - - impl Drop for RecordDrop<'_> { - fn drop(&mut self) { - *self.0 = true; - } - } - - Python::with_gil(|py| { - let mut dropped = false; - let cell = GILOnceCell::new(); - cell.set(py, RecordDrop(&mut dropped)).unwrap(); - let drop_container = cell.get(py).unwrap(); - - assert!(!*drop_container.0); - drop(cell); - assert!(dropped); - }); - } - - #[cfg(feature = "macros")] - #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled - #[test] - fn test_critical_section() { - use std::sync::{ - atomic::{AtomicBool, Ordering}, - Barrier, - }; - - let barrier = Barrier::new(2); - - #[crate::pyclass(crate = "crate")] - struct BoolWrapper(AtomicBool); - - let bool_wrapper = Python::with_gil(|py| -> Py { - Py::new(py, BoolWrapper(AtomicBool::new(false))).unwrap() - }); - - std::thread::scope(|s| { - s.spawn(|| { - Python::with_gil(|py| { - let b = bool_wrapper.bind(py); - with_critical_section(b, || { - barrier.wait(); - std::thread::sleep(std::time::Duration::from_millis(10)); - b.borrow().0.store(true, Ordering::Release); - }) - }); - }); - s.spawn(|| { - barrier.wait(); - Python::with_gil(|py| { - let b = bool_wrapper.bind(py); - // this blocks until the other thread's critical section finishes - with_critical_section(b, || { - assert!(b.borrow().0.load(Ordering::Acquire)); - }); - }); - }); - }); - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled - fn test_once_ext() { - // adapted from the example in the docs for Once::try_once_force - let init = Once::new(); - std::thread::scope(|s| { - // poison the once - let handle = s.spawn(|| { - Python::with_gil(|py| { - init.call_once_py_attached(py, || panic!()); - }) - }); - assert!(handle.join().is_err()); - - // poisoning propagates - let handle = s.spawn(|| { - Python::with_gil(|py| { - init.call_once_py_attached(py, || {}); - }); - }); - - assert!(handle.join().is_err()); - - // call_once_force will still run and reset the poisoned state - Python::with_gil(|py| { - init.call_once_force_py_attached(py, |state| { - assert!(state.is_poisoned()); - }); - - // once any success happens, we stop propagating the poison - init.call_once_py_attached(py, || {}); - }); - }); - } - - #[cfg(rustc_has_once_lock)] - #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled - #[test] - fn test_once_lock_ext() { - let cell = std::sync::OnceLock::new(); - std::thread::scope(|s| { - assert!(cell.get().is_none()); - - s.spawn(|| { - Python::with_gil(|py| { - assert_eq!(*cell.get_or_init_py_attached(py, || 12345), 12345); - }); - }); - }); - assert_eq!(cell.get(), Some(&12345)); - } -} diff --git a/vendor/pyo3/src/test_utils.rs b/vendor/pyo3/src/test_utils.rs deleted file mode 100644 index a007483fad..0000000000 --- a/vendor/pyo3/src/test_utils.rs +++ /dev/null @@ -1,2 +0,0 @@ -use crate as pyo3; -include!("../tests/common.rs"); diff --git a/vendor/pyo3/src/tests/common.rs b/vendor/pyo3/src/tests/common.rs deleted file mode 100644 index cd0374e901..0000000000 --- a/vendor/pyo3/src/tests/common.rs +++ /dev/null @@ -1,181 +0,0 @@ -// the inner mod enables the #![allow(dead_code)] to -// be applied - `test_utils.rs` uses `include!` to pull in this file - -/// Common macros and helpers for tests -#[allow(dead_code)] // many tests do not use the complete set of functionality offered here -#[allow(missing_docs)] // only used in tests -#[macro_use] -mod inner { - - #[allow(unused_imports)] // pulls in `use crate as pyo3` in `test_utils.rs` - use super::*; - - #[cfg(not(Py_GIL_DISABLED))] - use pyo3::prelude::*; - - #[cfg(not(Py_GIL_DISABLED))] - use pyo3::types::{IntoPyDict, PyList}; - - use uuid::Uuid; - - #[macro_export] - macro_rules! py_assert { - ($py:expr, $($val:ident)+, $assertion:literal) => { - pyo3::py_run!($py, $($val)+, concat!("assert ", $assertion)) - }; - ($py:expr, *$dict:expr, $assertion:literal) => { - pyo3::py_run!($py, *$dict, concat!("assert ", $assertion)) - }; - } - - #[macro_export] - macro_rules! assert_py_eq { - ($val:expr, $expected:expr) => { - assert!($val.eq($expected).unwrap()); - }; - } - - #[macro_export] - macro_rules! py_expect_exception { - // Case1: idents & no err_msg - ($py:expr, $($val:ident)+, $code:expr, $err:ident) => {{ - use pyo3::types::IntoPyDict; - use pyo3::BoundObject; - let d = [$((stringify!($val), (&$val).into_pyobject($py).unwrap().into_any().into_bound()),)+].into_py_dict($py).unwrap(); - py_expect_exception!($py, *d, $code, $err) - }}; - // Case2: dict & no err_msg - ($py:expr, *$dict:expr, $code:expr, $err:ident) => {{ - let res = $py.run(&std::ffi::CString::new($code).unwrap(), None, Some(&$dict.as_borrowed())); - let err = res.expect_err(&format!("Did not raise {}", stringify!($err))); - if !err.matches($py, $py.get_type::()).unwrap() { - panic!("Expected {} but got {:?}", stringify!($err), err) - } - err - }}; - // Case3: idents & err_msg - ($py:expr, $($val:ident)+, $code:expr, $err:ident, $err_msg:literal) => {{ - let err = py_expect_exception!($py, $($val)+, $code, $err); - // Suppose that the error message looks like 'TypeError: ~' - assert_eq!(format!("Py{}", err), concat!(stringify!($err), ": ", $err_msg)); - err - }}; - // Case4: dict & err_msg - ($py:expr, *$dict:expr, $code:expr, $err:ident, $err_msg:literal) => {{ - let err = py_expect_exception!($py, *$dict, $code, $err); - assert_eq!(format!("Py{}", err), concat!(stringify!($err), ": ", $err_msg)); - err - }}; - } - - // sys.unraisablehook not available until Python 3.8 - #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] - #[pyclass(crate = "pyo3")] - pub struct UnraisableCapture { - pub capture: Option<(PyErr, PyObject)>, - old_hook: Option, - } - - #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] - #[pymethods(crate = "pyo3")] - impl UnraisableCapture { - pub fn hook(&mut self, unraisable: Bound<'_, PyAny>) { - let err = PyErr::from_value(unraisable.getattr("exc_value").unwrap()); - let instance = unraisable.getattr("object").unwrap(); - self.capture = Some((err, instance.into())); - } - } - - #[cfg(all(feature = "macros", Py_3_8, not(Py_GIL_DISABLED)))] - impl UnraisableCapture { - pub fn install(py: Python<'_>) -> Py { - let sys = py.import("sys").unwrap(); - let old_hook = sys.getattr("unraisablehook").unwrap().into(); - - let capture = Py::new( - py, - UnraisableCapture { - capture: None, - old_hook: Some(old_hook), - }, - ) - .unwrap(); - - sys.setattr("unraisablehook", capture.getattr(py, "hook").unwrap()) - .unwrap(); - - capture - } - - pub fn uninstall(&mut self, py: Python<'_>) { - let old_hook = self.old_hook.take().unwrap(); - - let sys = py.import("sys").unwrap(); - sys.setattr("unraisablehook", old_hook).unwrap(); - } - } - - #[cfg(not(Py_GIL_DISABLED))] - pub struct CatchWarnings<'py> { - catch_warnings: Bound<'py, PyAny>, - } - - #[cfg(not(Py_GIL_DISABLED))] - impl<'py> CatchWarnings<'py> { - pub fn enter( - py: Python<'py>, - f: impl FnOnce(&Bound<'py, PyList>) -> PyResult, - ) -> PyResult { - let warnings = py.import("warnings")?; - let kwargs = [("record", true)].into_py_dict(py)?; - let catch_warnings = warnings - .getattr("catch_warnings")? - .call((), Some(&kwargs))?; - let list = catch_warnings.call_method0("__enter__")?.downcast_into()?; - let _guard = Self { catch_warnings }; - f(&list) - } - } - - #[cfg(not(Py_GIL_DISABLED))] - impl Drop for CatchWarnings<'_> { - fn drop(&mut self) { - let py = self.catch_warnings.py(); - self.catch_warnings - .call_method1("__exit__", (py.None(), py.None(), py.None())) - .unwrap(); - } - } - - #[cfg(not(Py_GIL_DISABLED))] - #[macro_export] - macro_rules! assert_warnings { - ($py:expr, $body:expr, [$(($category:ty, $message:literal)),+] $(,)? ) => {{ - $crate::tests::common::CatchWarnings::enter($py, |w| { - use $crate::types::{PyListMethods, PyStringMethods}; - $body; - let expected_warnings = [$((<$category as $crate::type_object::PyTypeInfo>::type_object($py), $message)),+]; - assert_eq!(w.len(), expected_warnings.len()); - for (warning, (category, message)) in w.iter().zip(expected_warnings) { - - assert!(warning.getattr("category").unwrap().is(&category)); - assert_eq!( - warning.getattr("message").unwrap().str().unwrap().to_string_lossy(), - message - ); - } - - Ok(()) - }) - .unwrap(); - }}; - } - - pub fn generate_unique_module_name(base: &str) -> std::ffi::CString { - let uuid = Uuid::new_v4().simple().to_string(); - std::ffi::CString::new(format!("{base}_{uuid}")).unwrap() - } -} - -#[allow(unused_imports)] // some tests use just the macros and none of the other functionality -pub use inner::*; diff --git a/vendor/pyo3/src/tests/hygiene/misc.rs b/vendor/pyo3/src/tests/hygiene/misc.rs deleted file mode 100644 index 6e00167ddb..0000000000 --- a/vendor/pyo3/src/tests/hygiene/misc.rs +++ /dev/null @@ -1,94 +0,0 @@ -#[derive(crate::FromPyObject)] -#[pyo3(crate = "crate")] -struct Derive1(i32); // newtype case - -#[derive(crate::FromPyObject)] -#[pyo3(crate = "crate")] -struct Derive2(i32, i32); // tuple case - -#[derive(crate::FromPyObject)] -#[pyo3(crate = "crate")] -struct Derive3 { - f: i32, - #[pyo3(item(42))] - g: i32, -} // struct case - -#[derive(crate::FromPyObject)] -#[pyo3(crate = "crate")] -enum Derive4 { - A(i32), - B { f: i32 }, -} // enum case - -crate::create_exception!(mymodule, CustomError, crate::exceptions::PyException); -crate::import_exception!(socket, gaierror); - -fn intern(py: crate::Python<'_>) { - let _foo = crate::intern!(py, "foo"); - let _bar = crate::intern!(py, stringify!(bar)); -} - -#[cfg(not(PyPy))] -fn append_to_inittab() { - #[crate::pymodule] - #[pyo3(crate = "crate")] - mod module_for_inittab {} - - crate::append_to_inittab!(module_for_inittab); -} - -macro_rules! macro_rules_hygiene { - ($name_a:ident, $name_b:ident) => { - #[crate::pyclass(crate = "crate")] - struct $name_a {} - - #[crate::pymethods(crate = "crate")] - impl $name_a { - fn finalize(&mut self) -> $name_b { - $name_b {} - } - } - - #[crate::pyclass(crate = "crate")] - struct $name_b {} - }; -} - -macro_rules_hygiene!(MyClass1, MyClass2); - -#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)] -#[pyo3(crate = "crate")] -struct IntoPyObject1(i32); // transparent newtype case - -#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)] -#[pyo3(crate = "crate", transparent)] -struct IntoPyObject2<'a> { - inner: &'a str, // transparent newtype case -} - -#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)] -#[pyo3(crate = "crate")] -struct IntoPyObject3<'py>(i32, crate::Bound<'py, crate::PyAny>); // tuple case - -#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)] -#[pyo3(crate = "crate")] -struct IntoPyObject4<'a, 'py> { - callable: &'a crate::Bound<'py, crate::PyAny>, // struct case - num: usize, -} - -#[derive(crate::IntoPyObject, crate::IntoPyObjectRef)] -#[pyo3(crate = "crate")] -enum IntoPyObject5<'a, 'py> { - TransparentTuple(i32), - #[pyo3(transparent)] - TransparentStruct { - f: crate::Py, - }, - Tuple(crate::Bound<'py, crate::types::PyString>, usize), - Struct { - f: i32, - g: &'a str, - }, -} // enum case diff --git a/vendor/pyo3/src/tests/hygiene/mod.rs b/vendor/pyo3/src/tests/hygiene/mod.rs deleted file mode 100644 index c950e18da9..0000000000 --- a/vendor/pyo3/src/tests/hygiene/mod.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![no_implicit_prelude] -#![allow(dead_code, unused_variables, clippy::unnecessary_wraps)] - -// The modules in this test are used to check PyO3 macro expansion is hygienic. By locating the test -// inside the crate the global `::pyo3` namespace is not available, so in combination with -// #[pyo3(crate = "crate")] this validates that all macro expansion respects the setting. - -mod misc; -mod pyclass; -mod pyfunction; -mod pymethods; -mod pymodule; diff --git a/vendor/pyo3/src/tests/hygiene/pyclass.rs b/vendor/pyo3/src/tests/hygiene/pyclass.rs deleted file mode 100644 index 17c3ce41e1..0000000000 --- a/vendor/pyo3/src/tests/hygiene/pyclass.rs +++ /dev/null @@ -1,153 +0,0 @@ -#[crate::pyclass] -#[pyo3(crate = "crate")] -#[derive(::std::clone::Clone)] -pub struct Foo; - -#[crate::pyclass] -#[pyo3(crate = "crate")] -pub struct Foo2; - -#[cfg_attr(any(Py_3_9, not(Py_LIMITED_API)), crate::pyclass( - name = "ActuallyBar", - freelist = 8, - unsendable, - subclass, - extends = crate::types::PyAny, - module = "Spam", - weakref, - dict -))] -#[cfg_attr(not(any(Py_3_9, not(Py_LIMITED_API))), crate::pyclass( - name = "ActuallyBar", - freelist = 8, - unsendable, - subclass, - extends = crate::types::PyAny, - module = "Spam" -))] -#[pyo3(crate = "crate")] -pub struct Bar { - #[pyo3(get, set)] - a: u8, - #[pyo3(get, set)] - b: Foo, - #[pyo3(set)] - c: ::std::option::Option>, -} - -#[crate::pyclass(eq, eq_int)] -#[pyo3(crate = "crate")] -#[derive(PartialEq)] -pub enum Enum { - Var0, -} - -#[crate::pyclass] -#[pyo3(crate = "crate")] -pub struct Foo3 { - #[pyo3(get, set)] - #[cfg(any())] - field: i32, - - #[pyo3(get, set)] - #[cfg(not(any()))] - field: u32, -} - -#[crate::pyclass] -#[pyo3(crate = "crate")] -pub struct Foo4 { - #[pyo3(get, set)] - #[cfg(any())] - #[cfg(not(any()))] - field: i32, - - #[pyo3(get, set)] - #[cfg(not(any()))] - field: u32, -} - -#[crate::pyclass(eq, ord)] -#[pyo3(crate = "crate")] -#[derive(PartialEq, PartialOrd)] -pub struct PointEqOrd { - x: u32, - y: u32, - z: u32, -} - -#[crate::pyclass(eq, ord)] -#[pyo3(crate = "crate")] -#[derive(PartialEq, PartialOrd)] -pub enum ComplexEnumEqOrd { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32 }, -} - -#[crate::pyclass(eq, ord)] -#[pyo3(crate = "crate")] -#[derive(PartialEq, PartialOrd)] -pub enum TupleEnumEqOrd { - Variant1(u32, u32), - Variant2(u32), -} - -#[crate::pyclass(crate = "crate")] -pub enum ComplexEnumManyVariantFields { - ManyStructFields { - field_1: u16, - field_2: u32, - field_3: u32, - field_4: i32, - field_5: u32, - field_6: u32, - field_7: u8, - field_8: u32, - field_9: i32, - field_10: u32, - field_11: u32, - field_12: u32, - field_13: u32, - field_14: i16, - field_15: u32, - }, - ManyTupleFields( - u16, - u32, - u32, - i32, - u32, - u32, - u8, - u32, - i32, - u32, - u32, - u32, - u32, - i16, - u32, - ), -} - -#[crate::pyclass(str = "{x}, {y}, {z}")] -#[pyo3(crate = "crate")] -pub struct PointFmt { - x: u32, - y: u32, - z: u32, -} - -#[crate::pyclass(str)] -#[pyo3(crate = "crate")] -pub struct Point { - x: i32, - y: i32, - z: i32, -} - -impl ::std::fmt::Display for Point { - fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { - ::std::write!(f, "({}, {}, {})", self.x, self.y, self.z) - } -} diff --git a/vendor/pyo3/src/tests/hygiene/pyfunction.rs b/vendor/pyo3/src/tests/hygiene/pyfunction.rs deleted file mode 100644 index 8dcdc369c4..0000000000 --- a/vendor/pyo3/src/tests/hygiene/pyfunction.rs +++ /dev/null @@ -1,13 +0,0 @@ -#[crate::pyfunction] -#[pyo3(crate = "crate")] -fn do_something(x: i32) -> crate::PyResult { - ::std::result::Result::Ok(x) -} - -#[test] -fn invoke_wrap_pyfunction() { - crate::Python::with_gil(|py| { - let func = crate::wrap_pyfunction!(do_something, py).unwrap(); - crate::py_run!(py, func, r#"func(5)"#); - }); -} diff --git a/vendor/pyo3/src/tests/hygiene/pymethods.rs b/vendor/pyo3/src/tests/hygiene/pymethods.rs deleted file mode 100644 index a5856e6413..0000000000 --- a/vendor/pyo3/src/tests/hygiene/pymethods.rs +++ /dev/null @@ -1,452 +0,0 @@ -#[crate::pyclass] -#[pyo3(crate = "crate")] -pub struct Dummy; - -#[crate::pyclass] -#[pyo3(crate = "crate")] -pub struct DummyIter; - -#[crate::pymethods] -#[pyo3(crate = "crate")] -impl Dummy { - ////////////////////// - // Basic customization - ////////////////////// - fn __repr__(&self) -> &'static str { - "Dummy" - } - - fn __str__(&self) -> &'static str { - "Dummy" - } - - fn __bytes__<'py>(&self, py: crate::Python<'py>) -> crate::Bound<'py, crate::types::PyBytes> { - crate::types::PyBytes::new(py, &[0]) - } - - fn __format__(&self, format_spec: ::std::string::String) -> ::std::string::String { - ::std::unimplemented!() - } - - fn __lt__(&self, other: &Self) -> bool { - false - } - - fn __le__(&self, other: &Self) -> bool { - false - } - fn __eq__(&self, other: &Self) -> bool { - false - } - fn __ne__(&self, other: &Self) -> bool { - false - } - fn __gt__(&self, other: &Self) -> bool { - false - } - fn __ge__(&self, other: &Self) -> bool { - false - } - - fn __hash__(&self) -> u64 { - 42 - } - - fn __bool__(&self) -> bool { - true - } - - ////////////////////// - // Customizing attribute access - ////////////////////// - - fn __getattr__(&self, name: ::std::string::String) -> &crate::Bound<'_, crate::PyAny> { - ::std::unimplemented!() - } - - fn __getattribute__(&self, name: ::std::string::String) -> &crate::Bound<'_, crate::PyAny> { - ::std::unimplemented!() - } - - fn __setattr__(&mut self, name: ::std::string::String, value: ::std::string::String) {} - - fn __delattr__(&mut self, name: ::std::string::String) {} - - fn __dir__<'py>( - &self, - py: crate::Python<'py>, - ) -> crate::PyResult> { - crate::types::PyList::new(py, ::std::vec![0_u8]) - } - - ////////////////////// - // Implementing Descriptors - ////////////////////// - - fn __get__( - &self, - instance: &crate::Bound<'_, crate::PyAny>, - owner: &crate::Bound<'_, crate::PyAny>, - ) -> crate::PyResult<&crate::Bound<'_, crate::PyAny>> { - ::std::unimplemented!() - } - - fn __set__( - &self, - instance: &crate::Bound<'_, crate::PyAny>, - owner: &crate::Bound<'_, crate::PyAny>, - ) { - } - - fn __delete__(&self, instance: &crate::Bound<'_, crate::PyAny>) {} - - fn __set_name__( - &self, - owner: &crate::Bound<'_, crate::PyAny>, - name: &crate::Bound<'_, crate::PyAny>, - ) { - } - - ////////////////////// - // Implementing Descriptors - ////////////////////// - - fn __len__(&self) -> usize { - 0 - } - - fn __getitem__(&self, key: u32) -> crate::PyResult { - ::std::result::Result::Err(crate::exceptions::PyKeyError::new_err("boo")) - } - - fn __setitem__(&self, key: u32, value: u32) {} - - fn __delitem__(&self, key: u32) {} - - fn __iter__(_: crate::pycell::PyRef<'_, Self>, py: crate::Python<'_>) -> crate::Py { - crate::Py::new(py, DummyIter {}).unwrap() - } - - fn __next__(&mut self) -> ::std::option::Option<()> { - ::std::option::Option::None - } - - fn __reversed__( - slf: crate::pycell::PyRef<'_, Self>, - py: crate::Python<'_>, - ) -> crate::Py { - crate::Py::new(py, DummyIter {}).unwrap() - } - - fn __contains__(&self, item: u32) -> bool { - false - } - - ////////////////////// - // Emulating numeric types - ////////////////////// - - fn __add__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __sub__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __mul__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __truediv__(&self, _other: &Self) -> crate::PyResult<()> { - ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) - } - - fn __floordiv__(&self, _other: &Self) -> crate::PyResult<()> { - ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) - } - - fn __mod__(&self, _other: &Self) -> u32 { - 0 - } - - fn __divmod__(&self, _other: &Self) -> (u32, u32) { - (0, 0) - } - - fn __pow__(&self, _other: &Self, modulo: ::std::option::Option) -> Dummy { - Dummy {} - } - - fn __lshift__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rshift__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __and__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __xor__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __or__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __radd__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rrsub__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rmul__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rtruediv__(&self, _other: &Self) -> crate::PyResult<()> { - ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) - } - - fn __rfloordiv__(&self, _other: &Self) -> crate::PyResult<()> { - ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) - } - - fn __rmod__(&self, _other: &Self) -> u32 { - 0 - } - - fn __rdivmod__(&self, _other: &Self) -> (u32, u32) { - (0, 0) - } - - fn __rpow__(&self, _other: &Self, modulo: ::std::option::Option) -> Dummy { - Dummy {} - } - - fn __rlshift__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rrshift__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rand__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __rxor__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __ror__(&self, other: &Self) -> Dummy { - Dummy {} - } - - fn __iadd__(&mut self, other: &Self) {} - - fn __irsub__(&mut self, other: &Self) {} - - fn __imul__(&mut self, other: &Self) {} - - fn __itruediv__(&mut self, _other: &Self) {} - - fn __ifloordiv__(&mut self, _other: &Self) {} - - fn __imod__(&mut self, _other: &Self) {} - - fn __ipow__(&mut self, _other: &Self, modulo: ::std::option::Option) {} - - fn __ilshift__(&mut self, other: &Self) {} - - fn __irshift__(&mut self, other: &Self) {} - - fn __iand__(&mut self, other: &Self) {} - - fn __ixor__(&mut self, other: &Self) {} - - fn __ior__(&mut self, other: &Self) {} - - fn __neg__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> { - slf - } - - fn __pos__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> { - slf - } - - fn __abs__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> { - slf - } - - fn __invert__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> { - slf - } - - fn __complex__<'py>( - &self, - py: crate::Python<'py>, - ) -> crate::Bound<'py, crate::types::PyComplex> { - crate::types::PyComplex::from_doubles(py, 0.0, 0.0) - } - - fn __int__(&self) -> u32 { - 0 - } - - fn __float__(&self) -> f64 { - 0.0 - } - - fn __index__(&self) -> u32 { - 0 - } - - #[pyo3(signature=(ndigits=::std::option::Option::None))] - fn __round__(&self, ndigits: ::std::option::Option) -> u32 { - 0 - } - - fn __trunc__(&self) -> u32 { - 0 - } - - fn __floor__(&self) -> u32 { - 0 - } - - fn __ceil__(&self) -> u32 { - 0 - } - - ////////////////////// - // With Statement Context Managers - ////////////////////// - - fn __enter__(&mut self) {} - - fn __exit__( - &mut self, - exc_type: &crate::Bound<'_, crate::PyAny>, - exc_value: &crate::Bound<'_, crate::PyAny>, - traceback: &crate::Bound<'_, crate::PyAny>, - ) { - } - - ////////////////////// - // Awaitable Objects - ////////////////////// - - fn __await__(slf: crate::pycell::PyRef<'_, Self>) -> crate::pycell::PyRef<'_, Self> { - slf - } - - ////////////////////// - - // Asynchronous Iterators - ////////////////////// - - fn __aiter__( - slf: crate::pycell::PyRef<'_, Self>, - py: crate::Python<'_>, - ) -> crate::Py { - crate::Py::new(py, DummyIter {}).unwrap() - } - - fn __anext__(&mut self) -> ::std::option::Option<()> { - ::std::option::Option::None - } - - ////////////////////// - // Asynchronous Context Managers - ////////////////////// - - fn __aenter__(&mut self) {} - - fn __aexit__( - &mut self, - exc_type: &crate::Bound<'_, crate::PyAny>, - exc_value: &crate::Bound<'_, crate::PyAny>, - traceback: &crate::Bound<'_, crate::PyAny>, - ) { - } - - // Things with attributes - - #[pyo3(signature = (_y, *, _z=2))] - fn test(&self, _y: &Dummy, _z: i32) {} - #[staticmethod] - fn staticmethod() {} - #[classmethod] - fn clsmethod(_: &crate::Bound<'_, crate::types::PyType>) {} - #[pyo3(signature = (*_args, **_kwds))] - fn __call__( - &self, - _args: &crate::Bound<'_, crate::types::PyTuple>, - _kwds: ::std::option::Option<&crate::Bound<'_, crate::types::PyDict>>, - ) -> crate::PyResult { - ::std::unimplemented!() - } - #[new] - fn new(a: u8) -> Self { - Dummy {} - } - #[getter] - fn get(&self) -> i32 { - 0 - } - #[setter] - fn set(&mut self, _v: i32) {} - #[classattr] - fn class_attr() -> i32 { - 0 - } - - // Dunder methods invented for protocols - - // PyGcProtocol - // Buffer protocol? -} - -#[crate::pyclass(crate = "crate")] -struct Clear; - -#[crate::pymethods(crate = "crate")] -impl Clear { - pub fn __traverse__( - &self, - visit: crate::PyVisit<'_>, - ) -> ::std::result::Result<(), crate::PyTraverseError> { - ::std::result::Result::Ok(()) - } - - pub fn __clear__(&self) {} - - #[pyo3(signature=(*, reuse=false))] - pub fn clear(&self, reuse: bool) {} -} - -// Ensure that crate argument is also accepted inline - -#[crate::pyclass(crate = "crate")] -struct Dummy2; - -#[crate::pymethods(crate = "crate")] -impl Dummy2 { - #[classmethod] - fn __len__(cls: &crate::Bound<'_, crate::types::PyType>) -> crate::PyResult { - ::std::result::Result::Ok(0) - } - - #[staticmethod] - fn __repr__() -> &'static str { - "Dummy" - } -} diff --git a/vendor/pyo3/src/tests/hygiene/pymodule.rs b/vendor/pyo3/src/tests/hygiene/pymodule.rs deleted file mode 100644 index 576cc75e0d..0000000000 --- a/vendor/pyo3/src/tests/hygiene/pymodule.rs +++ /dev/null @@ -1,36 +0,0 @@ -#[crate::pyfunction] -#[pyo3(crate = "crate")] -fn do_something(x: i32) -> crate::PyResult { - ::std::result::Result::Ok(x) -} - -#[crate::pymodule] -#[pyo3(crate = "crate")] -fn foo( - _py: crate::Python<'_>, - _m: &crate::Bound<'_, crate::types::PyModule>, -) -> crate::PyResult<()> { - ::std::result::Result::Ok(()) -} - -#[crate::pymodule] -#[pyo3(crate = "crate")] -fn my_module(m: &crate::Bound<'_, crate::types::PyModule>) -> crate::PyResult<()> { - as crate::types::PyModuleMethods>::add_function( - m, - crate::wrap_pyfunction!(do_something, m)?, - )?; - as crate::types::PyModuleMethods>::add_wrapped( - m, - crate::wrap_pymodule!(foo), - )?; - - ::std::result::Result::Ok(()) -} - -#[crate::pymodule(submodule)] -#[pyo3(crate = "crate")] -mod my_module_declarative { - #[pymodule_export] - use super::{do_something, foo}; -} diff --git a/vendor/pyo3/src/tests/mod.rs b/vendor/pyo3/src/tests/mod.rs deleted file mode 100644 index 136236d7db..0000000000 --- a/vendor/pyo3/src/tests/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -#[macro_use] -pub(crate) mod common { - #[cfg(not(Py_GIL_DISABLED))] - use crate as pyo3; - include!("./common.rs"); -} - -/// Test macro hygiene - this is in the crate since we won't have -/// `pyo3` available in the crate root. -#[cfg(all(test, feature = "macros"))] -mod hygiene; diff --git a/vendor/pyo3/src/type_object.rs b/vendor/pyo3/src/type_object.rs deleted file mode 100644 index b7cad4ab3b..0000000000 --- a/vendor/pyo3/src/type_object.rs +++ /dev/null @@ -1,120 +0,0 @@ -//! Python type object information - -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::types::any::PyAnyMethods; -use crate::types::{PyAny, PyType}; -use crate::{ffi, Bound, Python}; - -/// `T: PyLayout` represents that `T` is a concrete representation of `U` in the Python heap. -/// E.g., `PyClassObject` is a concrete representation of all `pyclass`es, and `ffi::PyObject` -/// is of `PyAny`. -/// -/// This trait is intended to be used internally. -/// -/// # Safety -/// -/// This trait must only be implemented for types which represent valid layouts of Python objects. -pub unsafe trait PyLayout {} - -/// `T: PySizedLayout` represents that `T` is not a instance of -/// [`PyVarObject`](https://docs.python.org/3/c-api/structures.html#c.PyVarObject). -/// -/// In addition, that `T` is a concrete representation of `U`. -pub trait PySizedLayout: PyLayout + Sized {} - -/// Python type information. -/// All Python native types (e.g., `PyDict`) and `#[pyclass]` structs implement this trait. -/// -/// This trait is marked unsafe because: -/// - specifying the incorrect layout can lead to memory errors -/// - the return value of type_object must always point to the same PyTypeObject instance -/// -/// It is safely implemented by the `pyclass` macro. -/// -/// # Safety -/// -/// Implementations must provide an implementation for `type_object_raw` which infallibly produces a -/// non-null pointer to the corresponding Python type object. -pub unsafe trait PyTypeInfo: Sized { - /// Class name. - const NAME: &'static str; - - /// Module name, if any. - const MODULE: Option<&'static str>; - - /// Returns the PyTypeObject instance for this type. - fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject; - - /// Returns the safe abstraction over the type object. - #[inline] - fn type_object(py: Python<'_>) -> Bound<'_, PyType> { - // Making the borrowed object `Bound` is necessary for soundness reasons. It's an extreme - // edge case, but arbitrary Python code _could_ change the __class__ of an object and cause - // the type object to be freed. - // - // By making `Bound` we assume ownership which is then safe against races. - unsafe { - Self::type_object_raw(py) - .cast::() - .assume_borrowed_unchecked(py) - .to_owned() - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyTypeInfo::type_object`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::type_object`")] - #[inline] - fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType> { - Self::type_object(py) - } - - /// Checks if `object` is an instance of this type or a subclass of this type. - #[inline] - fn is_type_of(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::PyObject_TypeCheck(object.as_ptr(), Self::type_object_raw(object.py())) != 0 } - } - - /// Deprecated name for [`PyTypeInfo::is_type_of`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::is_type_of`")] - #[inline] - fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool { - Self::is_type_of(object) - } - - /// Checks if `object` is an instance of this type. - #[inline] - fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::Py_TYPE(object.as_ptr()) == Self::type_object_raw(object.py()) } - } - - /// Deprecated name for [`PyTypeInfo::is_exact_type_of`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::is_exact_type_of`")] - #[inline] - fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool { - Self::is_exact_type_of(object) - } -} - -/// Implemented by types which can be used as a concrete Python type inside `Py` smart pointers. -pub trait PyTypeCheck { - /// Name of self. This is used in error messages, for example. - const NAME: &'static str; - - /// Checks if `object` is an instance of `Self`, which may include a subtype. - /// - /// This should be equivalent to the Python expression `isinstance(object, Self)`. - fn type_check(object: &Bound<'_, PyAny>) -> bool; -} - -impl PyTypeCheck for T -where - T: PyTypeInfo, -{ - const NAME: &'static str = ::NAME; - - #[inline] - fn type_check(object: &Bound<'_, PyAny>) -> bool { - T::is_type_of(object) - } -} diff --git a/vendor/pyo3/src/types/any.rs b/vendor/pyo3/src/types/any.rs deleted file mode 100644 index 8d1296ef15..0000000000 --- a/vendor/pyo3/src/types/any.rs +++ /dev/null @@ -1,2024 +0,0 @@ -use crate::class::basic::CompareOp; -use crate::conversion::{AsPyPointer, FromPyObjectBound, IntoPyObject}; -use crate::err::{DowncastError, DowncastIntoError, PyErr, PyResult}; -use crate::exceptions::{PyAttributeError, PyTypeError}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Bound; -use crate::internal::get_slot::TP_DESCR_GET; -use crate::internal_tricks::ptr_from_ref; -use crate::py_result_ext::PyResultExt; -use crate::type_object::{PyTypeCheck, PyTypeInfo}; -#[cfg(not(any(PyPy, GraalPy)))] -use crate::types::PySuper; -use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType}; -use crate::{err, ffi, Borrowed, BoundObject, IntoPyObjectExt, Python}; -use std::cell::UnsafeCell; -use std::cmp::Ordering; -use std::os::raw::c_int; - -/// Represents any Python object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyAny>`][Bound]. -/// -/// For APIs available on all Python objects, see the [`PyAnyMethods`] trait which is implemented for -/// [`Bound<'py, PyAny>`][Bound]. -/// -/// See -#[doc = concat!("[the guide](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/types.html#concrete-python-types)")] -/// for an explanation of the different Python object types. -#[repr(transparent)] -pub struct PyAny(UnsafeCell); - -#[allow(non_snake_case)] -// Copied here as the macro does not accept deprecated functions. -// Originally ffi::object::PyObject_Check, but this is not in the Python C API. -fn PyObject_Check(_: *mut ffi::PyObject) -> c_int { - 1 -} - -pyobject_native_type_info!( - PyAny, - pyobject_native_static_type_object!(ffi::PyBaseObject_Type), - Some("builtins"), - #checkfunction=PyObject_Check -); - -pyobject_native_type_sized!(PyAny, ffi::PyObject); -// We cannot use `pyobject_subclassable_native_type!()` because it cfgs out on `Py_LIMITED_API`. -impl crate::impl_::pyclass::PyClassBaseType for PyAny { - type LayoutAsBase = crate::impl_::pycell::PyClassObjectBase; - type BaseNativeType = PyAny; - type Initializer = crate::impl_::pyclass_init::PyNativeTypeInitializer; - type PyClassMutability = crate::pycell::impl_::ImmutableClass; -} - -/// This trait represents the Python APIs which are usable on all Python objects. -/// -/// It is recommended you import this trait via `use pyo3::prelude::*` rather than -/// by importing this trait directly. -#[doc(alias = "PyAny")] -pub trait PyAnyMethods<'py>: crate::sealed::Sealed { - /// Returns whether `self` and `other` point to the same object. To compare - /// the equality of two objects (the `==` operator), use [`eq`](PyAnyMethods::eq). - /// - /// This is equivalent to the Python expression `self is other`. - fn is(&self, other: &T) -> bool; - - /// Determines whether this object has the given attribute. - /// - /// This is equivalent to the Python expression `hasattr(self, attr_name)`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `attr_name`. - /// - /// # Example: `intern!`ing the attribute name - /// - /// ``` - /// # use pyo3::{prelude::*, intern}; - /// # - /// #[pyfunction] - /// fn has_version(sys: &Bound<'_, PyModule>) -> PyResult { - /// sys.hasattr(intern!(sys.py(), "version")) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let sys = py.import("sys").unwrap(); - /// # has_version(&sys).unwrap(); - /// # }); - /// ``` - fn hasattr(&self, attr_name: N) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>; - - /// Retrieves an attribute value. - /// - /// This is equivalent to the Python expression `self.attr_name`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `attr_name`. - /// - /// # Example: `intern!`ing the attribute name - /// - /// ``` - /// # use pyo3::{prelude::*, intern}; - /// # - /// #[pyfunction] - /// fn version<'py>(sys: &Bound<'py, PyModule>) -> PyResult> { - /// sys.getattr(intern!(sys.py(), "version")) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let sys = py.import("sys").unwrap(); - /// # version(&sys).unwrap(); - /// # }); - /// ``` - fn getattr(&self, attr_name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>; - - /// Sets an attribute value. - /// - /// This is equivalent to the Python expression `self.attr_name = value`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `name`. - /// - /// # Example: `intern!`ing the attribute name - /// - /// ``` - /// # use pyo3::{prelude::*, intern}; - /// # - /// #[pyfunction] - /// fn set_answer(ob: &Bound<'_, PyAny>) -> PyResult<()> { - /// ob.setattr(intern!(ob.py(), "answer"), 42) - /// } - /// # - /// # Python::with_gil(|py| { - /// # let ob = PyModule::new(py, "empty").unwrap(); - /// # set_answer(&ob).unwrap(); - /// # }); - /// ``` - fn setattr(&self, attr_name: N, value: V) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - V: IntoPyObject<'py>; - - /// Deletes an attribute. - /// - /// This is equivalent to the Python statement `del self.attr_name`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `attr_name`. - fn delattr(&self, attr_name: N) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>; - - /// Returns an [`Ordering`] between `self` and `other`. - /// - /// This is equivalent to the following Python code: - /// ```python - /// if self == other: - /// return Equal - /// elif a < b: - /// return Less - /// elif a > b: - /// return Greater - /// else: - /// raise TypeError("PyAny::compare(): All comparisons returned false") - /// ``` - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyFloat; - /// use std::cmp::Ordering; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let a = PyFloat::new(py, 0_f64); - /// let b = PyFloat::new(py, 42_f64); - /// assert_eq!(a.compare(b)?, Ordering::Less); - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - /// - /// It will return `PyErr` for values that cannot be compared: - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyFloat, PyString}; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let a = PyFloat::new(py, 0_f64); - /// let b = PyString::new(py, "zero"); - /// assert!(a.compare(b).is_err()); - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - fn compare(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether two Python objects obey a given [`CompareOp`]. - /// - /// [`lt`](Self::lt), [`le`](Self::le), [`eq`](Self::eq), [`ne`](Self::ne), - /// [`gt`](Self::gt) and [`ge`](Self::ge) are the specialized versions - /// of this function. - /// - /// Depending on the value of `compare_op`, this is equivalent to one of the - /// following Python expressions: - /// - /// | `compare_op` | Python expression | - /// | :---: | :----: | - /// | [`CompareOp::Eq`] | `self == other` | - /// | [`CompareOp::Ne`] | `self != other` | - /// | [`CompareOp::Lt`] | `self < other` | - /// | [`CompareOp::Le`] | `self <= other` | - /// | [`CompareOp::Gt`] | `self > other` | - /// | [`CompareOp::Ge`] | `self >= other` | - /// - /// # Examples - /// - /// ```rust - /// use pyo3::class::basic::CompareOp; - /// use pyo3::prelude::*; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let a = 0_u8.into_pyobject(py)?; - /// let b = 42_u8.into_pyobject(py)?; - /// assert!(a.rich_compare(b, CompareOp::Le)?.is_truthy()?); - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - fn rich_compare(&self, other: O, compare_op: CompareOp) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes the negative of self. - /// - /// Equivalent to the Python expression `-self`. - fn neg(&self) -> PyResult>; - - /// Computes the positive of self. - /// - /// Equivalent to the Python expression `+self`. - fn pos(&self) -> PyResult>; - - /// Computes the absolute of self. - /// - /// Equivalent to the Python expression `abs(self)`. - fn abs(&self) -> PyResult>; - - /// Computes `~self`. - fn bitnot(&self) -> PyResult>; - - /// Tests whether this object is less than another. - /// - /// This is equivalent to the Python expression `self < other`. - fn lt(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether this object is less than or equal to another. - /// - /// This is equivalent to the Python expression `self <= other`. - fn le(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether this object is equal to another. - /// - /// This is equivalent to the Python expression `self == other`. - fn eq(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether this object is not equal to another. - /// - /// This is equivalent to the Python expression `self != other`. - fn ne(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether this object is greater than another. - /// - /// This is equivalent to the Python expression `self > other`. - fn gt(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Tests whether this object is greater than or equal to another. - /// - /// This is equivalent to the Python expression `self >= other`. - fn ge(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>; - - /// Computes `self + other`. - fn add(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self - other`. - fn sub(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self * other`. - fn mul(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self @ other`. - fn matmul(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self / other`. - fn div(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self // other`. - fn floor_div(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self % other`. - fn rem(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `divmod(self, other)`. - fn divmod(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self << other`. - fn lshift(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self >> other`. - fn rshift(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self ** other % modulus` (`pow(self, other, modulus)`). - /// `py.None()` may be passed for the `modulus`. - fn pow(&self, other: O1, modulus: O2) -> PyResult> - where - O1: IntoPyObject<'py>, - O2: IntoPyObject<'py>; - - /// Computes `self & other`. - fn bitand(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self | other`. - fn bitor(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Computes `self ^ other`. - fn bitxor(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>; - - /// Determines whether this object appears callable. - /// - /// This is equivalent to Python's [`callable()`][1] function. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let builtins = PyModule::import(py, "builtins")?; - /// let print = builtins.getattr("print")?; - /// assert!(print.is_callable()); - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - /// - /// This is equivalent to the Python statement `assert callable(print)`. - /// - /// Note that unless an API needs to distinguish between callable and - /// non-callable objects, there is no point in checking for callability. - /// Instead, it is better to just do the call and handle potential - /// exceptions. - /// - /// [1]: https://docs.python.org/3/library/functions.html#callable - fn is_callable(&self) -> bool; - - /// Calls the object. - /// - /// This is equivalent to the Python expression `self(*args, **kwargs)`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyDict; - /// use pyo3_ffi::c_str; - /// use std::ffi::CStr; - /// - /// const CODE: &CStr = c_str!(r#" - /// def function(*args, **kwargs): - /// assert args == ("hello",) - /// assert kwargs == {"cruel": "world"} - /// return "called with args and kwargs" - /// "#); - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let module = PyModule::from_code(py, CODE, c_str!(""), c_str!(""))?; - /// let fun = module.getattr("function")?; - /// let args = ("hello",); - /// let kwargs = PyDict::new(py); - /// kwargs.set_item("cruel", "world")?; - /// let result = fun.call(args, Some(&kwargs))?; - /// assert_eq!(result.extract::()?, "called with args and kwargs"); - /// Ok(()) - /// }) - /// # } - /// ``` - fn call(&self, args: A, kwargs: Option<&Bound<'py, PyDict>>) -> PyResult> - where - A: IntoPyObject<'py, Target = PyTuple>; - - /// Calls the object without arguments. - /// - /// This is equivalent to the Python expression `self()`. - /// - /// # Examples - /// - /// ```no_run - /// use pyo3::prelude::*; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let module = PyModule::import(py, "builtins")?; - /// let help = module.getattr("help")?; - /// help.call0()?; - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - /// - /// This is equivalent to the Python expression `help()`. - fn call0(&self) -> PyResult>; - - /// Calls the object with only positional arguments. - /// - /// This is equivalent to the Python expression `self(*args)`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3_ffi::c_str; - /// use std::ffi::CStr; - /// - /// const CODE: &CStr = c_str!(r#" - /// def function(*args, **kwargs): - /// assert args == ("hello",) - /// assert kwargs == {} - /// return "called with args" - /// "#); - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let module = PyModule::from_code(py, CODE, c_str!(""), c_str!(""))?; - /// let fun = module.getattr("function")?; - /// let args = ("hello",); - /// let result = fun.call1(args)?; - /// assert_eq!(result.extract::()?, "called with args"); - /// Ok(()) - /// }) - /// # } - /// ``` - fn call1(&self, args: A) -> PyResult> - where - A: IntoPyObject<'py, Target = PyTuple>; - - /// Calls a method on the object. - /// - /// This is equivalent to the Python expression `self.name(*args, **kwargs)`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `name`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyDict; - /// use pyo3_ffi::c_str; - /// use std::ffi::CStr; - /// - /// const CODE: &CStr = c_str!(r#" - /// class A: - /// def method(self, *args, **kwargs): - /// assert args == ("hello",) - /// assert kwargs == {"cruel": "world"} - /// return "called with args and kwargs" - /// a = A() - /// "#); - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let module = PyModule::from_code(py, CODE, c_str!(""), c_str!(""))?; - /// let instance = module.getattr("a")?; - /// let args = ("hello",); - /// let kwargs = PyDict::new(py); - /// kwargs.set_item("cruel", "world")?; - /// let result = instance.call_method("method", args, Some(&kwargs))?; - /// assert_eq!(result.extract::()?, "called with args and kwargs"); - /// Ok(()) - /// }) - /// # } - /// ``` - fn call_method( - &self, - name: N, - args: A, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>; - - /// Calls a method on the object without arguments. - /// - /// This is equivalent to the Python expression `self.name()`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `name`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3_ffi::c_str; - /// use std::ffi::CStr; - /// - /// const CODE: &CStr = c_str!(r#" - /// class A: - /// def method(self, *args, **kwargs): - /// assert args == () - /// assert kwargs == {} - /// return "called with no arguments" - /// a = A() - /// "#); - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let module = PyModule::from_code(py, CODE, c_str!(""), c_str!(""))?; - /// let instance = module.getattr("a")?; - /// let result = instance.call_method0("method")?; - /// assert_eq!(result.extract::()?, "called with no arguments"); - /// Ok(()) - /// }) - /// # } - /// ``` - fn call_method0(&self, name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>; - - /// Calls a method on the object with only positional arguments. - /// - /// This is equivalent to the Python expression `self.name(*args)`. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `name`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3_ffi::c_str; - /// use std::ffi::CStr; - /// - /// const CODE: &CStr = c_str!(r#" - /// class A: - /// def method(self, *args, **kwargs): - /// assert args == ("hello",) - /// assert kwargs == {} - /// return "called with args" - /// a = A() - /// "#); - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let module = PyModule::from_code(py, CODE, c_str!(""), c_str!(""))?; - /// let instance = module.getattr("a")?; - /// let args = ("hello",); - /// let result = instance.call_method1("method", args)?; - /// assert_eq!(result.extract::()?, "called with args"); - /// Ok(()) - /// }) - /// # } - /// ``` - fn call_method1(&self, name: N, args: A) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>; - - /// Returns whether the object is considered to be true. - /// - /// This is equivalent to the Python expression `bool(self)`. - fn is_truthy(&self) -> PyResult; - - /// Returns whether the object is considered to be None. - /// - /// This is equivalent to the Python expression `self is None`. - fn is_none(&self) -> bool; - - /// Returns whether the object is Ellipsis, e.g. `...`. - /// - /// This is equivalent to the Python expression `self is ...`. - #[deprecated(since = "0.23.0", note = "use `.is(py.Ellipsis())` instead")] - fn is_ellipsis(&self) -> bool; - - /// Returns true if the sequence or mapping has a length of 0. - /// - /// This is equivalent to the Python expression `len(self) == 0`. - fn is_empty(&self) -> PyResult; - - /// Gets an item from the collection. - /// - /// This is equivalent to the Python expression `self[key]`. - fn get_item(&self, key: K) -> PyResult> - where - K: IntoPyObject<'py>; - - /// Sets a collection item value. - /// - /// This is equivalent to the Python expression `self[key] = value`. - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>; - - /// Deletes an item from the collection. - /// - /// This is equivalent to the Python expression `del self[key]`. - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>; - - /// Takes an object and returns an iterator for it. Returns an error if the object is not - /// iterable. - /// - /// This is typically a new iterator but if the argument is an iterator, - /// this returns itself. - /// - /// # Example: Checking a Python object for iterability - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyAny, PyNone}; - /// - /// fn is_iterable(obj: &Bound<'_, PyAny>) -> bool { - /// match obj.try_iter() { - /// Ok(_) => true, - /// Err(_) => false, - /// } - /// } - /// - /// Python::with_gil(|py| { - /// assert!(is_iterable(&vec![1, 2, 3].into_pyobject(py).unwrap())); - /// assert!(!is_iterable(&PyNone::get(py))); - /// }); - /// ``` - fn try_iter(&self) -> PyResult>; - - /// Takes an object and returns an iterator for it. - /// - /// This is typically a new iterator but if the argument is an iterator, - /// this returns itself. - #[deprecated(since = "0.23.0", note = "use `try_iter` instead")] - fn iter(&self) -> PyResult>; - - /// Returns the Python type object for this object's type. - fn get_type(&self) -> Bound<'py, PyType>; - - /// Returns the Python type pointer for this object. - fn get_type_ptr(&self) -> *mut ffi::PyTypeObject; - - /// Downcast this `PyAny` to a concrete Python type or pyclass. - /// - /// Note that you can often avoid downcasting yourself by just specifying - /// the desired type in function or method signatures. - /// However, manual downcasting is sometimes necessary. - /// - /// For extracting a Rust-only type, see [`PyAny::extract`](struct.PyAny.html#method.extract). - /// - /// # Example: Downcasting to a specific Python object - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyDict, PyList}; - /// - /// Python::with_gil(|py| { - /// let dict = PyDict::new(py); - /// assert!(dict.is_instance_of::()); - /// let any = dict.as_any(); - /// - /// assert!(any.downcast::().is_ok()); - /// assert!(any.downcast::().is_err()); - /// }); - /// ``` - /// - /// # Example: Getting a reference to a pyclass - /// - /// This is useful if you want to mutate a `PyObject` that - /// might actually be a pyclass. - /// - /// ```rust - /// # fn main() -> Result<(), pyo3::PyErr> { - /// use pyo3::prelude::*; - /// - /// #[pyclass] - /// struct Class { - /// i: i32, - /// } - /// - /// Python::with_gil(|py| { - /// let class = Py::new(py, Class { i: 0 }).unwrap().into_bound(py).into_any(); - /// - /// let class_bound: &Bound<'_, Class> = class.downcast()?; - /// - /// class_bound.borrow_mut().i += 1; - /// - /// // Alternatively you can get a `PyRefMut` directly - /// let class_ref: PyRefMut<'_, Class> = class.extract()?; - /// assert_eq!(class_ref.i, 1); - /// Ok(()) - /// }) - /// # } - /// ``` - fn downcast(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> - where - T: PyTypeCheck; - - /// Like `downcast` but takes ownership of `self`. - /// - /// In case of an error, it is possible to retrieve `self` again via [`DowncastIntoError::into_inner`]. - /// - /// # Example - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyDict, PyList}; - /// - /// Python::with_gil(|py| { - /// let obj: Bound<'_, PyAny> = PyDict::new(py).into_any(); - /// - /// let obj: Bound<'_, PyAny> = match obj.downcast_into::() { - /// Ok(_) => panic!("obj should not be a list"), - /// Err(err) => err.into_inner(), - /// }; - /// - /// // obj is a dictionary - /// assert!(obj.downcast_into::().is_ok()); - /// }) - /// ``` - fn downcast_into(self) -> Result, DowncastIntoError<'py>> - where - T: PyTypeCheck; - - /// Downcast this `PyAny` to a concrete Python type or pyclass (but not a subclass of it). - /// - /// It is almost always better to use [`PyAnyMethods::downcast`] because it accounts for Python - /// subtyping. Use this method only when you do not want to allow subtypes. - /// - /// The advantage of this method over [`PyAnyMethods::downcast`] is that it is faster. The implementation - /// of `downcast_exact` uses the equivalent of the Python expression `type(self) is T`, whereas - /// `downcast` uses `isinstance(self, T)`. - /// - /// For extracting a Rust-only type, see [`PyAny::extract`](struct.PyAny.html#method.extract). - /// - /// # Example: Downcasting to a specific Python object but not a subtype - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::{PyBool, PyInt}; - /// - /// Python::with_gil(|py| { - /// let b = PyBool::new(py, true); - /// assert!(b.is_instance_of::()); - /// let any: &Bound<'_, PyAny> = b.as_any(); - /// - /// // `bool` is a subtype of `int`, so `downcast` will accept a `bool` as an `int` - /// // but `downcast_exact` will not. - /// assert!(any.downcast::().is_ok()); - /// assert!(any.downcast_exact::().is_err()); - /// - /// assert!(any.downcast_exact::().is_ok()); - /// }); - /// ``` - fn downcast_exact(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> - where - T: PyTypeInfo; - - /// Like `downcast_exact` but takes ownership of `self`. - fn downcast_into_exact(self) -> Result, DowncastIntoError<'py>> - where - T: PyTypeInfo; - - /// Converts this `PyAny` to a concrete Python type without checking validity. - /// - /// # Safety - /// - /// Callers must ensure that the type is valid or risk type confusion. - unsafe fn downcast_unchecked(&self) -> &Bound<'py, T>; - - /// Like `downcast_unchecked` but takes ownership of `self`. - /// - /// # Safety - /// - /// Callers must ensure that the type is valid or risk type confusion. - unsafe fn downcast_into_unchecked(self) -> Bound<'py, T>; - - /// Extracts some type from the Python object. - /// - /// This is a wrapper function around - /// [`FromPyObject::extract_bound()`](crate::FromPyObject::extract_bound). - fn extract<'a, T>(&'a self) -> PyResult - where - T: FromPyObjectBound<'a, 'py>; - - /// Returns the reference count for the Python object. - fn get_refcnt(&self) -> isize; - - /// Computes the "repr" representation of self. - /// - /// This is equivalent to the Python expression `repr(self)`. - fn repr(&self) -> PyResult>; - - /// Computes the "str" representation of self. - /// - /// This is equivalent to the Python expression `str(self)`. - fn str(&self) -> PyResult>; - - /// Retrieves the hash code of self. - /// - /// This is equivalent to the Python expression `hash(self)`. - fn hash(&self) -> PyResult; - - /// Returns the length of the sequence or mapping. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> PyResult; - - /// Returns the list of attributes of this object. - /// - /// This is equivalent to the Python expression `dir(self)`. - fn dir(&self) -> PyResult>; - - /// Checks whether this object is an instance of type `ty`. - /// - /// This is equivalent to the Python expression `isinstance(self, ty)`. - fn is_instance(&self, ty: &Bound<'py, PyAny>) -> PyResult; - - /// Checks whether this object is an instance of exactly type `ty` (not a subclass). - /// - /// This is equivalent to the Python expression `type(self) is ty`. - fn is_exact_instance(&self, ty: &Bound<'py, PyAny>) -> bool; - - /// Checks whether this object is an instance of type `T`. - /// - /// This is equivalent to the Python expression `isinstance(self, T)`, - /// if the type `T` is known at compile time. - fn is_instance_of(&self) -> bool; - - /// Checks whether this object is an instance of exactly type `T`. - /// - /// This is equivalent to the Python expression `type(self) is T`, - /// if the type `T` is known at compile time. - fn is_exact_instance_of(&self) -> bool; - - /// Determines if self contains `value`. - /// - /// This is equivalent to the Python expression `value in self`. - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Return a proxy object that delegates method calls to a parent or sibling class of type. - /// - /// This is equivalent to the Python expression `super()` - #[cfg(not(any(PyPy, GraalPy)))] - fn py_super(&self) -> PyResult>; -} - -macro_rules! implement_binop { - ($name:ident, $c_api:ident, $op:expr) => { - #[doc = concat!("Computes `self ", $op, " other`.")] - fn $name(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - other: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { ffi::$c_api(any.as_ptr(), other.as_ptr()).assume_owned_or_err(any.py()) } - } - - let py = self.py(); - inner( - self, - other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - }; -} - -impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { - #[inline] - fn is(&self, other: &T) -> bool { - self.as_ptr() == other.as_ptr() - } - - fn hasattr(&self, attr_name: N) -> PyResult - where - N: IntoPyObject<'py, Target = PyString>, - { - // PyObject_HasAttr suppresses all exceptions, which was the behaviour of `hasattr` in Python 2. - // Use an implementation which suppresses only AttributeError, which is consistent with `hasattr` in Python 3. - fn inner(py: Python<'_>, getattr_result: PyResult>) -> PyResult { - match getattr_result { - Ok(_) => Ok(true), - Err(err) if err.is_instance_of::(py) => Ok(false), - Err(e) => Err(e), - } - } - - inner(self.py(), self.getattr(attr_name)) - } - - fn getattr(&self, attr_name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - attr_name: Borrowed<'_, '_, PyString>, - ) -> PyResult> { - unsafe { - ffi::PyObject_GetAttr(any.as_ptr(), attr_name.as_ptr()) - .assume_owned_or_err(any.py()) - } - } - - inner( - self, - attr_name - .into_pyobject(self.py()) - .map_err(Into::into)? - .as_borrowed(), - ) - } - - fn setattr(&self, attr_name: N, value: V) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - V: IntoPyObject<'py>, - { - fn inner( - any: &Bound<'_, PyAny>, - attr_name: Borrowed<'_, '_, PyString>, - value: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - err::error_on_minusone(any.py(), unsafe { - ffi::PyObject_SetAttr(any.as_ptr(), attr_name.as_ptr(), value.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - attr_name.into_pyobject_or_pyerr(py)?.as_borrowed(), - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn delattr(&self, attr_name: N) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - { - fn inner(any: &Bound<'_, PyAny>, attr_name: Borrowed<'_, '_, PyString>) -> PyResult<()> { - err::error_on_minusone(any.py(), unsafe { - ffi::PyObject_DelAttr(any.as_ptr(), attr_name.as_ptr()) - }) - } - - let py = self.py(); - inner(self, attr_name.into_pyobject_or_pyerr(py)?.as_borrowed()) - } - - fn compare(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - fn inner(any: &Bound<'_, PyAny>, other: Borrowed<'_, '_, PyAny>) -> PyResult { - let other = other.as_ptr(); - // Almost the same as ffi::PyObject_RichCompareBool, but this one doesn't try self == other. - // See https://github.com/PyO3/pyo3/issues/985 for more. - let do_compare = |other, op| unsafe { - ffi::PyObject_RichCompare(any.as_ptr(), other, op) - .assume_owned_or_err(any.py()) - .and_then(|obj| obj.is_truthy()) - }; - if do_compare(other, ffi::Py_EQ)? { - Ok(Ordering::Equal) - } else if do_compare(other, ffi::Py_LT)? { - Ok(Ordering::Less) - } else if do_compare(other, ffi::Py_GT)? { - Ok(Ordering::Greater) - } else { - Err(PyTypeError::new_err( - "PyAny::compare(): All comparisons returned false", - )) - } - } - - let py = self.py(); - inner( - self, - other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn rich_compare(&self, other: O, compare_op: CompareOp) -> PyResult> - where - O: IntoPyObject<'py>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - other: Borrowed<'_, 'py, PyAny>, - compare_op: CompareOp, - ) -> PyResult> { - unsafe { - ffi::PyObject_RichCompare(any.as_ptr(), other.as_ptr(), compare_op as c_int) - .assume_owned_or_err(any.py()) - } - } - - let py = self.py(); - inner( - self, - other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - compare_op, - ) - } - - fn neg(&self) -> PyResult> { - unsafe { ffi::PyNumber_Negative(self.as_ptr()).assume_owned_or_err(self.py()) } - } - - fn pos(&self) -> PyResult> { - fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult> { - unsafe { ffi::PyNumber_Positive(any.as_ptr()).assume_owned_or_err(any.py()) } - } - - inner(self) - } - - fn abs(&self) -> PyResult> { - fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult> { - unsafe { ffi::PyNumber_Absolute(any.as_ptr()).assume_owned_or_err(any.py()) } - } - - inner(self) - } - - fn bitnot(&self) -> PyResult> { - fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult> { - unsafe { ffi::PyNumber_Invert(any.as_ptr()).assume_owned_or_err(any.py()) } - } - - inner(self) - } - - fn lt(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Lt) - .and_then(|any| any.is_truthy()) - } - - fn le(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Le) - .and_then(|any| any.is_truthy()) - } - - fn eq(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Eq) - .and_then(|any| any.is_truthy()) - } - - fn ne(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Ne) - .and_then(|any| any.is_truthy()) - } - - fn gt(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Gt) - .and_then(|any| any.is_truthy()) - } - - fn ge(&self, other: O) -> PyResult - where - O: IntoPyObject<'py>, - { - self.rich_compare(other, CompareOp::Ge) - .and_then(|any| any.is_truthy()) - } - - implement_binop!(add, PyNumber_Add, "+"); - implement_binop!(sub, PyNumber_Subtract, "-"); - implement_binop!(mul, PyNumber_Multiply, "*"); - implement_binop!(matmul, PyNumber_MatrixMultiply, "@"); - implement_binop!(div, PyNumber_TrueDivide, "/"); - implement_binop!(floor_div, PyNumber_FloorDivide, "//"); - implement_binop!(rem, PyNumber_Remainder, "%"); - implement_binop!(lshift, PyNumber_Lshift, "<<"); - implement_binop!(rshift, PyNumber_Rshift, ">>"); - implement_binop!(bitand, PyNumber_And, "&"); - implement_binop!(bitor, PyNumber_Or, "|"); - implement_binop!(bitxor, PyNumber_Xor, "^"); - - /// Computes `divmod(self, other)`. - fn divmod(&self, other: O) -> PyResult> - where - O: IntoPyObject<'py>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - other: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { - ffi::PyNumber_Divmod(any.as_ptr(), other.as_ptr()).assume_owned_or_err(any.py()) - } - } - - let py = self.py(); - inner( - self, - other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - /// Computes `self ** other % modulus` (`pow(self, other, modulus)`). - /// `py.None()` may be passed for the `modulus`. - fn pow(&self, other: O1, modulus: O2) -> PyResult> - where - O1: IntoPyObject<'py>, - O2: IntoPyObject<'py>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - other: Borrowed<'_, 'py, PyAny>, - modulus: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { - ffi::PyNumber_Power(any.as_ptr(), other.as_ptr(), modulus.as_ptr()) - .assume_owned_or_err(any.py()) - } - } - - let py = self.py(); - inner( - self, - other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - modulus.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn is_callable(&self) -> bool { - unsafe { ffi::PyCallable_Check(self.as_ptr()) != 0 } - } - - fn call(&self, args: A, kwargs: Option<&Bound<'py, PyDict>>) -> PyResult> - where - A: IntoPyObject<'py, Target = PyTuple>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - args: Borrowed<'_, 'py, PyTuple>, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - unsafe { - ffi::PyObject_Call( - any.as_ptr(), - args.as_ptr(), - kwargs.map_or(std::ptr::null_mut(), |dict| dict.as_ptr()), - ) - .assume_owned_or_err(any.py()) - } - } - - let py = self.py(); - inner(self, args.into_pyobject_or_pyerr(py)?.as_borrowed(), kwargs) - } - - #[inline] - fn call0(&self) -> PyResult> { - unsafe { ffi::compat::PyObject_CallNoArgs(self.as_ptr()).assume_owned_or_err(self.py()) } - } - - fn call1(&self, args: A) -> PyResult> - where - A: IntoPyObject<'py, Target = PyTuple>, - { - self.call(args, None) - } - - #[inline] - fn call_method( - &self, - name: N, - args: A, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>, - { - self.getattr(name) - .and_then(|method| method.call(args, kwargs)) - } - - #[inline] - fn call_method0(&self, name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - { - let py = self.py(); - let name = name.into_pyobject_or_pyerr(py)?.into_bound(); - unsafe { - ffi::compat::PyObject_CallMethodNoArgs(self.as_ptr(), name.as_ptr()) - .assume_owned_or_err(py) - } - } - - fn call_method1(&self, name: N, args: A) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - A: IntoPyObject<'py, Target = PyTuple>, - { - self.call_method(name, args, None) - } - - fn is_truthy(&self) -> PyResult { - let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) }; - err::error_on_minusone(self.py(), v)?; - Ok(v != 0) - } - - #[inline] - fn is_none(&self) -> bool { - unsafe { ffi::Py_None() == self.as_ptr() } - } - - fn is_ellipsis(&self) -> bool { - unsafe { ffi::Py_Ellipsis() == self.as_ptr() } - } - - fn is_empty(&self) -> PyResult { - self.len().map(|l| l == 0) - } - - fn get_item(&self, key: K) -> PyResult> - where - K: IntoPyObject<'py>, - { - fn inner<'py>( - any: &Bound<'py, PyAny>, - key: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { - ffi::PyObject_GetItem(any.as_ptr(), key.as_ptr()).assume_owned_or_err(any.py()) - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>, - { - fn inner( - any: &Bound<'_, PyAny>, - key: Borrowed<'_, '_, PyAny>, - value: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - err::error_on_minusone(any.py(), unsafe { - ffi::PyObject_SetItem(any.as_ptr(), key.as_ptr(), value.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>, - { - fn inner(any: &Bound<'_, PyAny>, key: Borrowed<'_, '_, PyAny>) -> PyResult<()> { - err::error_on_minusone(any.py(), unsafe { - ffi::PyObject_DelItem(any.as_ptr(), key.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn try_iter(&self) -> PyResult> { - PyIterator::from_object(self) - } - - fn iter(&self) -> PyResult> { - self.try_iter() - } - - fn get_type(&self) -> Bound<'py, PyType> { - unsafe { PyType::from_borrowed_type_ptr(self.py(), ffi::Py_TYPE(self.as_ptr())) } - } - - #[inline] - fn get_type_ptr(&self) -> *mut ffi::PyTypeObject { - unsafe { ffi::Py_TYPE(self.as_ptr()) } - } - - #[inline] - fn downcast(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> - where - T: PyTypeCheck, - { - if T::type_check(self) { - // Safety: type_check is responsible for ensuring that the type is correct - Ok(unsafe { self.downcast_unchecked() }) - } else { - Err(DowncastError::new(self, T::NAME)) - } - } - - #[inline] - fn downcast_into(self) -> Result, DowncastIntoError<'py>> - where - T: PyTypeCheck, - { - if T::type_check(&self) { - // Safety: type_check is responsible for ensuring that the type is correct - Ok(unsafe { self.downcast_into_unchecked() }) - } else { - Err(DowncastIntoError::new(self, T::NAME)) - } - } - - #[inline] - fn downcast_exact(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> - where - T: PyTypeInfo, - { - if self.is_exact_instance_of::() { - // Safety: is_exact_instance_of is responsible for ensuring that the type is correct - Ok(unsafe { self.downcast_unchecked() }) - } else { - Err(DowncastError::new(self, T::NAME)) - } - } - - #[inline] - fn downcast_into_exact(self) -> Result, DowncastIntoError<'py>> - where - T: PyTypeInfo, - { - if self.is_exact_instance_of::() { - // Safety: is_exact_instance_of is responsible for ensuring that the type is correct - Ok(unsafe { self.downcast_into_unchecked() }) - } else { - Err(DowncastIntoError::new(self, T::NAME)) - } - } - - #[inline] - unsafe fn downcast_unchecked(&self) -> &Bound<'py, T> { - &*ptr_from_ref(self).cast() - } - - #[inline] - unsafe fn downcast_into_unchecked(self) -> Bound<'py, T> { - std::mem::transmute(self) - } - - fn extract<'a, T>(&'a self) -> PyResult - where - T: FromPyObjectBound<'a, 'py>, - { - FromPyObjectBound::from_py_object_bound(self.as_borrowed()) - } - - fn get_refcnt(&self) -> isize { - unsafe { ffi::Py_REFCNT(self.as_ptr()) } - } - - fn repr(&self) -> PyResult> { - unsafe { - ffi::PyObject_Repr(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - fn str(&self) -> PyResult> { - unsafe { - ffi::PyObject_Str(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - fn hash(&self) -> PyResult { - let v = unsafe { ffi::PyObject_Hash(self.as_ptr()) }; - crate::err::error_on_minusone(self.py(), v)?; - Ok(v) - } - - fn len(&self) -> PyResult { - let v = unsafe { ffi::PyObject_Size(self.as_ptr()) }; - crate::err::error_on_minusone(self.py(), v)?; - Ok(v as usize) - } - - fn dir(&self) -> PyResult> { - unsafe { - ffi::PyObject_Dir(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn is_instance(&self, ty: &Bound<'py, PyAny>) -> PyResult { - let result = unsafe { ffi::PyObject_IsInstance(self.as_ptr(), ty.as_ptr()) }; - err::error_on_minusone(self.py(), result)?; - Ok(result == 1) - } - - #[inline] - fn is_exact_instance(&self, ty: &Bound<'py, PyAny>) -> bool { - self.get_type().is(ty) - } - - #[inline] - fn is_instance_of(&self) -> bool { - T::is_type_of(self) - } - - #[inline] - fn is_exact_instance_of(&self) -> bool { - T::is_exact_type_of(self) - } - - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - fn inner(any: &Bound<'_, PyAny>, value: Borrowed<'_, '_, PyAny>) -> PyResult { - match unsafe { ffi::PySequence_Contains(any.as_ptr(), value.as_ptr()) } { - 0 => Ok(false), - 1 => Ok(true), - _ => Err(PyErr::fetch(any.py())), - } - } - - let py = self.py(); - inner( - self, - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - #[cfg(not(any(PyPy, GraalPy)))] - fn py_super(&self) -> PyResult> { - PySuper::new(&self.get_type(), self) - } -} - -impl<'py> Bound<'py, PyAny> { - /// Retrieve an attribute value, skipping the instance dictionary during the lookup but still - /// binding the object to the instance. - /// - /// This is useful when trying to resolve Python's "magic" methods like `__getitem__`, which - /// are looked up starting from the type object. This returns an `Option` as it is not - /// typically a direct error for the special lookup to fail, as magic methods are optional in - /// many situations in which they might be called. - /// - /// To avoid repeated temporary allocations of Python strings, the [`intern!`] macro can be used - /// to intern `attr_name`. - #[allow(dead_code)] // Currently only used with num-complex+abi3, so dead without that. - pub(crate) fn lookup_special(&self, attr_name: N) -> PyResult>> - where - N: IntoPyObject<'py, Target = PyString>, - { - let py = self.py(); - let self_type = self.get_type(); - let attr = if let Ok(attr) = self_type.getattr(attr_name) { - attr - } else { - return Ok(None); - }; - - // Manually resolve descriptor protocol. (Faster than going through Python.) - if let Some(descr_get) = attr.get_type().get_slot(TP_DESCR_GET) { - // attribute is a descriptor, resolve it - unsafe { - descr_get(attr.as_ptr(), self.as_ptr(), self_type.as_ptr()) - .assume_owned_or_err(py) - .map(Some) - } - } else { - Ok(Some(attr)) - } - } -} - -#[cfg(test)] -mod tests { - use crate::{ - basic::CompareOp, - ffi, - tests::common::generate_unique_module_name, - types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyInt, PyList, PyModule, PyTypeMethods}, - Bound, BoundObject, IntoPyObject, PyTypeInfo, Python, - }; - use pyo3_ffi::c_str; - use std::fmt::Debug; - - #[test] - fn test_lookup_special() { - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class CustomCallable: - def __call__(self): - return 1 - -class SimpleInt: - def __int__(self): - return 1 - -class InheritedInt(SimpleInt): pass - -class NoInt: pass - -class NoDescriptorInt: - __int__ = CustomCallable() - -class InstanceOverrideInt: - def __int__(self): - return 1 -instance_override = InstanceOverrideInt() -instance_override.__int__ = lambda self: 2 - -class ErrorInDescriptorInt: - @property - def __int__(self): - raise ValueError("uh-oh!") - -class NonHeapNonDescriptorInt: - # A static-typed callable that doesn't implement `__get__`. These are pretty hard to come by. - __int__ = int - "# - ), - c_str!("test.py"), - &generate_unique_module_name("test"), - ) - .unwrap(); - - let int = crate::intern!(py, "__int__"); - let eval_int = - |obj: Bound<'_, PyAny>| obj.lookup_special(int)?.unwrap().call0()?.extract::(); - - let simple = module.getattr("SimpleInt").unwrap().call0().unwrap(); - assert_eq!(eval_int(simple).unwrap(), 1); - let inherited = module.getattr("InheritedInt").unwrap().call0().unwrap(); - assert_eq!(eval_int(inherited).unwrap(), 1); - let no_descriptor = module.getattr("NoDescriptorInt").unwrap().call0().unwrap(); - assert_eq!(eval_int(no_descriptor).unwrap(), 1); - let missing = module.getattr("NoInt").unwrap().call0().unwrap(); - assert!(missing.lookup_special(int).unwrap().is_none()); - // Note the instance override should _not_ call the instance method that returns 2, - // because that's not how special lookups are meant to work. - let instance_override = module.getattr("instance_override").unwrap(); - assert_eq!(eval_int(instance_override).unwrap(), 1); - let descriptor_error = module - .getattr("ErrorInDescriptorInt") - .unwrap() - .call0() - .unwrap(); - assert!(descriptor_error.lookup_special(int).is_err()); - let nonheap_nondescriptor = module - .getattr("NonHeapNonDescriptorInt") - .unwrap() - .call0() - .unwrap(); - assert_eq!(eval_int(nonheap_nondescriptor).unwrap(), 0); - }) - } - - #[test] - fn test_call_for_non_existing_method() { - Python::with_gil(|py| { - let a = py.eval(ffi::c_str!("42"), None, None).unwrap(); - a.call_method0("__str__").unwrap(); // ok - assert!(a.call_method("nonexistent_method", (1,), None).is_err()); - assert!(a.call_method0("nonexistent_method").is_err()); - assert!(a.call_method1("nonexistent_method", (1,)).is_err()); - }); - } - - #[test] - fn test_call_with_kwargs() { - Python::with_gil(|py| { - let list = vec![3, 6, 5, 4, 7].into_pyobject(py).unwrap(); - let dict = vec![("reverse", true)].into_py_dict(py).unwrap(); - list.call_method("sort", (), Some(&dict)).unwrap(); - assert_eq!(list.extract::>().unwrap(), vec![7, 6, 5, 4, 3]); - }); - } - - #[test] - fn test_call_method0() { - Python::with_gil(|py| { - let module = PyModule::from_code( - py, - c_str!( - r#" -class SimpleClass: - def foo(self): - return 42 -"# - ), - c_str!(file!()), - &generate_unique_module_name("test_module"), - ) - .expect("module creation failed"); - - let simple_class = module.getattr("SimpleClass").unwrap().call0().unwrap(); - assert_eq!( - simple_class - .call_method0("foo") - .unwrap() - .extract::() - .unwrap(), - 42 - ); - }) - } - - #[test] - fn test_type() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("42"), None, None).unwrap(); - assert_eq!(obj.get_type().as_type_ptr(), obj.get_type_ptr()); - }); - } - - #[test] - fn test_dir() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("42"), None, None).unwrap(); - let dir = py - .eval(ffi::c_str!("dir(42)"), None, None) - .unwrap() - .downcast_into::() - .unwrap(); - let a = obj - .dir() - .unwrap() - .into_iter() - .map(|x| x.extract::().unwrap()); - let b = dir.into_iter().map(|x| x.extract::().unwrap()); - assert!(a.eq(b)); - }); - } - - #[test] - fn test_hasattr() { - Python::with_gil(|py| { - let x = 5i32.into_pyobject(py).unwrap(); - assert!(x.is_instance_of::()); - - assert!(x.hasattr("to_bytes").unwrap()); - assert!(!x.hasattr("bbbbbbytes").unwrap()); - }) - } - - #[cfg(feature = "macros")] - #[test] - #[allow(unknown_lints, non_local_definitions)] - fn test_hasattr_error() { - use crate::exceptions::PyValueError; - use crate::prelude::*; - - #[pyclass(crate = "crate")] - struct GetattrFail; - - #[pymethods(crate = "crate")] - impl GetattrFail { - fn __getattr__(&self, attr: PyObject) -> PyResult { - Err(PyValueError::new_err(attr)) - } - } - - Python::with_gil(|py| { - let obj = Py::new(py, GetattrFail).unwrap(); - let obj = obj.bind(py).as_ref(); - - assert!(obj - .hasattr("foo") - .unwrap_err() - .is_instance_of::(py)); - }) - } - - #[test] - fn test_nan_eq() { - Python::with_gil(|py| { - let nan = py.eval(ffi::c_str!("float('nan')"), None, None).unwrap(); - assert!(nan.compare(&nan).is_err()); - }); - } - - #[test] - fn test_any_is_instance_of() { - Python::with_gil(|py| { - let x = 5i32.into_pyobject(py).unwrap(); - assert!(x.is_instance_of::()); - - let l = vec![&x, &x].into_pyobject(py).unwrap(); - assert!(l.is_instance_of::()); - }); - } - - #[test] - fn test_any_is_instance() { - Python::with_gil(|py| { - let l = vec![1i8, 2].into_pyobject(py).unwrap(); - assert!(l.is_instance(&py.get_type::()).unwrap()); - }); - } - - #[test] - fn test_any_is_exact_instance_of() { - Python::with_gil(|py| { - let x = 5i32.into_pyobject(py).unwrap(); - assert!(x.is_exact_instance_of::()); - - let t = PyBool::new(py, true); - assert!(t.is_instance_of::()); - assert!(!t.is_exact_instance_of::()); - assert!(t.is_exact_instance_of::()); - - let l = vec![&x, &x].into_pyobject(py).unwrap(); - assert!(l.is_exact_instance_of::()); - }); - } - - #[test] - fn test_any_is_exact_instance() { - Python::with_gil(|py| { - let t = PyBool::new(py, true); - assert!(t.is_instance(&py.get_type::()).unwrap()); - assert!(!t.is_exact_instance(&py.get_type::())); - assert!(t.is_exact_instance(&py.get_type::())); - }); - } - - #[test] - fn test_any_contains() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - - let bad_needle = 7i32.into_pyobject(py).unwrap(); - assert!(!ob.contains(&bad_needle).unwrap()); - - let good_needle = 8i32.into_pyobject(py).unwrap(); - assert!(ob.contains(&good_needle).unwrap()); - - let type_coerced_needle = 8f32.into_pyobject(py).unwrap(); - assert!(ob.contains(&type_coerced_needle).unwrap()); - - let n: u32 = 42; - let bad_haystack = n.into_pyobject(py).unwrap(); - let irrelevant_needle = 0i32.into_pyobject(py).unwrap(); - assert!(bad_haystack.contains(&irrelevant_needle).is_err()); - }); - } - - // This is intentionally not a test, it's a generic function used by the tests below. - fn test_eq_methods_generic<'a, T>(list: &'a [T]) - where - T: PartialEq + PartialOrd, - for<'py> &'a T: IntoPyObject<'py>, - for<'py> <&'a T as IntoPyObject<'py>>::Error: Debug, - { - Python::with_gil(|py| { - for a in list { - for b in list { - let a_py = a.into_pyobject(py).unwrap().into_any().into_bound(); - let b_py = b.into_pyobject(py).unwrap().into_any().into_bound(); - - assert_eq!( - a.lt(b), - a_py.lt(&b_py).unwrap(), - "{} < {} should be {}.", - a_py, - b_py, - a.lt(b) - ); - assert_eq!( - a.le(b), - a_py.le(&b_py).unwrap(), - "{} <= {} should be {}.", - a_py, - b_py, - a.le(b) - ); - assert_eq!( - a.eq(b), - a_py.eq(&b_py).unwrap(), - "{} == {} should be {}.", - a_py, - b_py, - a.eq(b) - ); - assert_eq!( - a.ne(b), - a_py.ne(&b_py).unwrap(), - "{} != {} should be {}.", - a_py, - b_py, - a.ne(b) - ); - assert_eq!( - a.gt(b), - a_py.gt(&b_py).unwrap(), - "{} > {} should be {}.", - a_py, - b_py, - a.gt(b) - ); - assert_eq!( - a.ge(b), - a_py.ge(&b_py).unwrap(), - "{} >= {} should be {}.", - a_py, - b_py, - a.ge(b) - ); - } - } - }); - } - - #[test] - fn test_eq_methods_integers() { - let ints = [-4, -4, 1, 2, 0, -100, 1_000_000]; - test_eq_methods_generic::(&ints); - } - - #[test] - fn test_eq_methods_strings() { - let strings = ["Let's", "test", "some", "eq", "methods"]; - test_eq_methods_generic::<&str>(&strings); - } - - #[test] - fn test_eq_methods_floats() { - let floats = [ - -1.0, - 2.5, - 0.0, - 3.0, - std::f64::consts::PI, - 10.0, - 10.0 / 3.0, - -1_000_000.0, - ]; - test_eq_methods_generic::(&floats); - } - - #[test] - fn test_eq_methods_bools() { - let bools = [true, false]; - test_eq_methods_generic::(&bools); - } - - #[test] - fn test_rich_compare_type_error() { - Python::with_gil(|py| { - let py_int = 1i32.into_pyobject(py).unwrap(); - let py_str = "1".into_pyobject(py).unwrap(); - - assert!(py_int.rich_compare(&py_str, CompareOp::Lt).is_err()); - assert!(!py_int - .rich_compare(py_str, CompareOp::Eq) - .unwrap() - .is_truthy() - .unwrap()); - }) - } - - #[test] - #[allow(deprecated)] - fn test_is_ellipsis() { - Python::with_gil(|py| { - let v = py - .eval(ffi::c_str!("..."), None, None) - .map_err(|e| e.display(py)) - .unwrap(); - - assert!(v.is_ellipsis()); - - let not_ellipsis = 5i32.into_pyobject(py).unwrap(); - assert!(!not_ellipsis.is_ellipsis()); - }); - } - - #[test] - fn test_is_callable() { - Python::with_gil(|py| { - assert!(PyList::type_object(py).is_callable()); - - let not_callable = 5i32.into_pyobject(py).unwrap(); - assert!(!not_callable.is_callable()); - }); - } - - #[test] - fn test_is_empty() { - Python::with_gil(|py| { - let empty_list = PyList::empty(py).into_any(); - assert!(empty_list.is_empty().unwrap()); - - let list = PyList::new(py, vec![1, 2, 3]).unwrap().into_any(); - assert!(!list.is_empty().unwrap()); - - let not_container = 5i32.into_pyobject(py).unwrap(); - assert!(not_container.is_empty().is_err()); - }); - } - - #[cfg(feature = "macros")] - #[test] - #[allow(unknown_lints, non_local_definitions)] - fn test_fallible_dir() { - use crate::exceptions::PyValueError; - use crate::prelude::*; - - #[pyclass(crate = "crate")] - struct DirFail; - - #[pymethods(crate = "crate")] - impl DirFail { - fn __dir__(&self) -> PyResult { - Err(PyValueError::new_err("uh-oh!")) - } - } - - Python::with_gil(|py| { - let obj = Bound::new(py, DirFail).unwrap(); - assert!(obj.dir().unwrap_err().is_instance_of::(py)); - }) - } -} diff --git a/vendor/pyo3/src/types/boolobject.rs b/vendor/pyo3/src/types/boolobject.rs deleted file mode 100644 index 53043fa798..0000000000 --- a/vendor/pyo3/src/types/boolobject.rs +++ /dev/null @@ -1,349 +0,0 @@ -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - exceptions::PyTypeError, ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, - types::typeobject::PyTypeMethods, Borrowed, FromPyObject, PyAny, PyObject, PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -use super::any::PyAnyMethods; -use crate::conversion::IntoPyObject; -use crate::BoundObject; -use std::convert::Infallible; - -/// Represents a Python `bool`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyBool>`][Bound]. -/// -/// For APIs available on `bool` objects, see the [`PyBoolMethods`] trait which is implemented for -/// [`Bound<'py, PyBool>`][Bound]. -#[repr(transparent)] -pub struct PyBool(PyAny); - -pyobject_native_type!(PyBool, ffi::PyObject, pyobject_native_static_type_object!(ffi::PyBool_Type), #checkfunction=ffi::PyBool_Check); - -impl PyBool { - /// Depending on `val`, returns `true` or `false`. - /// - /// # Note - /// This returns a [`Borrowed`] reference to one of Pythons `True` or - /// `False` singletons - #[inline] - pub fn new(py: Python<'_>, val: bool) -> Borrowed<'_, '_, Self> { - unsafe { - if val { ffi::Py_True() } else { ffi::Py_False() } - .assume_borrowed(py) - .downcast_unchecked() - } - } - - /// Deprecated name for [`PyBool::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyBool::new`")] - #[inline] - pub fn new_bound(py: Python<'_>, val: bool) -> Borrowed<'_, '_, Self> { - Self::new(py, val) - } -} - -/// Implementation of functionality for [`PyBool`]. -/// -/// These methods are defined for the `Bound<'py, PyBool>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyBool")] -pub trait PyBoolMethods<'py>: crate::sealed::Sealed { - /// Gets whether this boolean is `true`. - fn is_true(&self) -> bool; -} - -impl<'py> PyBoolMethods<'py> for Bound<'py, PyBool> { - #[inline] - fn is_true(&self) -> bool { - self.as_ptr() == unsafe { crate::ffi::Py_True() } - } -} - -/// Compare `Bound` with `bool`. -impl PartialEq for Bound<'_, PyBool> { - #[inline] - fn eq(&self, other: &bool) -> bool { - self.as_borrowed() == *other - } -} - -/// Compare `&Bound` with `bool`. -impl PartialEq for &'_ Bound<'_, PyBool> { - #[inline] - fn eq(&self, other: &bool) -> bool { - self.as_borrowed() == *other - } -} - -/// Compare `Bound` with `&bool`. -impl PartialEq<&'_ bool> for Bound<'_, PyBool> { - #[inline] - fn eq(&self, other: &&bool) -> bool { - self.as_borrowed() == **other - } -} - -/// Compare `bool` with `Bound` -impl PartialEq> for bool { - #[inline] - fn eq(&self, other: &Bound<'_, PyBool>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compare `bool` with `&Bound` -impl PartialEq<&'_ Bound<'_, PyBool>> for bool { - #[inline] - fn eq(&self, other: &&'_ Bound<'_, PyBool>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compare `&bool` with `Bound` -impl PartialEq> for &'_ bool { - #[inline] - fn eq(&self, other: &Bound<'_, PyBool>) -> bool { - **self == other.as_borrowed() - } -} - -/// Compare `Borrowed` with `bool` -impl PartialEq for Borrowed<'_, '_, PyBool> { - #[inline] - fn eq(&self, other: &bool) -> bool { - self.is_true() == *other - } -} - -/// Compare `Borrowed` with `&bool` -impl PartialEq<&bool> for Borrowed<'_, '_, PyBool> { - #[inline] - fn eq(&self, other: &&bool) -> bool { - self.is_true() == **other - } -} - -/// Compare `bool` with `Borrowed` -impl PartialEq> for bool { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { - *self == other.is_true() - } -} - -/// Compare `&bool` with `Borrowed` -impl PartialEq> for &'_ bool { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyBool>) -> bool { - **self == other.is_true() - } -} - -/// Converts a Rust `bool` to a Python `bool`. -#[allow(deprecated)] -impl ToPyObject for bool { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for bool { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for bool { - type Target = PyBool; - type Output = Borrowed<'py, 'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyBool::new(py, self)) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("bool") - } -} - -impl<'py> IntoPyObject<'py> for &bool { - type Target = PyBool; - type Output = Borrowed<'py, 'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("bool") - } -} - -/// Converts a Python `bool` to a Rust `bool`. -/// -/// Fails with `TypeError` if the input is not a Python `bool`. -impl FromPyObject<'_> for bool { - fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { - let err = match obj.downcast::() { - Ok(obj) => return Ok(obj.is_true()), - Err(err) => err, - }; - - let is_numpy_bool = { - let ty = obj.get_type(); - ty.module().map_or(false, |module| module == "numpy") - && ty - .name() - .map_or(false, |name| name == "bool_" || name == "bool") - }; - - if is_numpy_bool { - let missing_conversion = |obj: &Bound<'_, PyAny>| { - PyTypeError::new_err(format!( - "object of type '{}' does not define a '__bool__' conversion", - obj.get_type() - )) - }; - - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - unsafe { - let ptr = obj.as_ptr(); - - if let Some(tp_as_number) = (*(*ptr).ob_type).tp_as_number.as_ref() { - if let Some(nb_bool) = tp_as_number.nb_bool { - match (nb_bool)(ptr) { - 0 => return Ok(false), - 1 => return Ok(true), - _ => return Err(crate::PyErr::fetch(obj.py())), - } - } - } - - return Err(missing_conversion(obj)); - } - - #[cfg(any(Py_LIMITED_API, PyPy))] - { - let meth = obj - .lookup_special(crate::intern!(obj.py(), "__bool__"))? - .ok_or_else(|| missing_conversion(obj))?; - - let obj = meth.call0()?.downcast_into::()?; - return Ok(obj.is_true()); - } - } - - Err(err.into()) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::types::boolobject::PyBoolMethods; - use crate::types::PyBool; - use crate::IntoPyObject; - use crate::Python; - - #[test] - fn test_true() { - Python::with_gil(|py| { - assert!(PyBool::new(py, true).is_true()); - let t = PyBool::new(py, true); - assert!(t.extract::().unwrap()); - assert!(true.into_pyobject(py).unwrap().is(&*PyBool::new(py, true))); - }); - } - - #[test] - fn test_false() { - Python::with_gil(|py| { - assert!(!PyBool::new(py, false).is_true()); - let t = PyBool::new(py, false); - assert!(!t.extract::().unwrap()); - assert!(false - .into_pyobject(py) - .unwrap() - .is(&*PyBool::new(py, false))); - }); - } - - #[test] - fn test_pybool_comparisons() { - Python::with_gil(|py| { - let py_bool = PyBool::new(py, true); - let py_bool_false = PyBool::new(py, false); - let rust_bool = true; - - // Bound<'_, PyBool> == bool - assert_eq!(*py_bool, rust_bool); - assert_ne!(*py_bool_false, rust_bool); - - // Bound<'_, PyBool> == &bool - assert_eq!(*py_bool, &rust_bool); - assert_ne!(*py_bool_false, &rust_bool); - - // &Bound<'_, PyBool> == bool - assert_eq!(&*py_bool, rust_bool); - assert_ne!(&*py_bool_false, rust_bool); - - // &Bound<'_, PyBool> == &bool - assert_eq!(&*py_bool, &rust_bool); - assert_ne!(&*py_bool_false, &rust_bool); - - // bool == Bound<'_, PyBool> - assert_eq!(rust_bool, *py_bool); - assert_ne!(rust_bool, *py_bool_false); - - // bool == &Bound<'_, PyBool> - assert_eq!(rust_bool, &*py_bool); - assert_ne!(rust_bool, &*py_bool_false); - - // &bool == Bound<'_, PyBool> - assert_eq!(&rust_bool, *py_bool); - assert_ne!(&rust_bool, *py_bool_false); - - // &bool == &Bound<'_, PyBool> - assert_eq!(&rust_bool, &*py_bool); - assert_ne!(&rust_bool, &*py_bool_false); - - // Borrowed<'_, '_, PyBool> == bool - assert_eq!(py_bool, rust_bool); - assert_ne!(py_bool_false, rust_bool); - - // Borrowed<'_, '_, PyBool> == &bool - assert_eq!(py_bool, &rust_bool); - assert_ne!(py_bool_false, &rust_bool); - - // bool == Borrowed<'_, '_, PyBool> - assert_eq!(rust_bool, py_bool); - assert_ne!(rust_bool, py_bool_false); - - // &bool == Borrowed<'_, '_, PyBool> - assert_eq!(&rust_bool, py_bool); - assert_ne!(&rust_bool, py_bool_false); - assert_eq!(py_bool, rust_bool); - assert_ne!(py_bool_false, rust_bool); - }) - } -} diff --git a/vendor/pyo3/src/types/bytearray.rs b/vendor/pyo3/src/types/bytearray.rs deleted file mode 100644 index d1bbd0ac7e..0000000000 --- a/vendor/pyo3/src/types/bytearray.rs +++ /dev/null @@ -1,475 +0,0 @@ -use crate::err::{PyErr, PyResult}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::{Borrowed, Bound}; -use crate::py_result_ext::PyResultExt; -use crate::types::any::PyAnyMethods; -use crate::{ffi, PyAny, Python}; -use std::slice; - -/// Represents a Python `bytearray`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyByteArray>`][Bound]. -/// -/// For APIs available on `bytearray` objects, see the [`PyByteArrayMethods`] trait which is implemented for -/// [`Bound<'py, PyByteArray>`][Bound]. -#[repr(transparent)] -pub struct PyByteArray(PyAny); - -pyobject_native_type_core!(PyByteArray, pyobject_native_static_type_object!(ffi::PyByteArray_Type), #checkfunction=ffi::PyByteArray_Check); - -impl PyByteArray { - /// Creates a new Python bytearray object. - /// - /// The byte string is initialized by copying the data from the `&[u8]`. - pub fn new<'py>(py: Python<'py>, src: &[u8]) -> Bound<'py, PyByteArray> { - let ptr = src.as_ptr().cast(); - let len = src.len() as ffi::Py_ssize_t; - unsafe { - ffi::PyByteArray_FromStringAndSize(ptr, len) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyByteArray::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::new`")] - #[inline] - pub fn new_bound<'py>(py: Python<'py>, src: &[u8]) -> Bound<'py, PyByteArray> { - Self::new(py, src) - } - - /// Creates a new Python `bytearray` object with an `init` closure to write its contents. - /// Before calling `init` the bytearray is zero-initialised. - /// * If Python raises a MemoryError on the allocation, `new_with` will return - /// it inside `Err`. - /// * If `init` returns `Err(e)`, `new_with` will return `Err(e)`. - /// * If `init` returns `Ok(())`, `new_with` will return `Ok(&PyByteArray)`. - /// - /// # Examples - /// - /// ``` - /// use pyo3::{prelude::*, types::PyByteArray}; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let py_bytearray = PyByteArray::new_with(py, 10, |bytes: &mut [u8]| { - /// bytes.copy_from_slice(b"Hello Rust"); - /// Ok(()) - /// })?; - /// let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() }; - /// assert_eq!(bytearray, b"Hello Rust"); - /// Ok(()) - /// }) - /// # } - /// ``` - pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - unsafe { - // Allocate buffer and check for an error - let pybytearray: Bound<'_, Self> = - ffi::PyByteArray_FromStringAndSize(std::ptr::null(), len as ffi::Py_ssize_t) - .assume_owned_or_err(py)? - .downcast_into_unchecked(); - - let buffer: *mut u8 = ffi::PyByteArray_AsString(pybytearray.as_ptr()).cast(); - debug_assert!(!buffer.is_null()); - // Zero-initialise the uninitialised bytearray - std::ptr::write_bytes(buffer, 0u8, len); - // (Further) Initialise the bytearray in init - // If init returns an Err, pypybytearray will automatically deallocate the buffer - init(std::slice::from_raw_parts_mut(buffer, len)).map(|_| pybytearray) - } - } - - /// Deprecated name for [`PyByteArray::new_with`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::new_with`")] - #[inline] - pub fn new_bound_with( - py: Python<'_>, - len: usize, - init: F, - ) -> PyResult> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - Self::new_with(py, len, init) - } - - /// Creates a new Python `bytearray` object from another Python object that - /// implements the buffer protocol. - pub fn from<'py>(src: &Bound<'py, PyAny>) -> PyResult> { - unsafe { - ffi::PyByteArray_FromObject(src.as_ptr()) - .assume_owned_or_err(src.py()) - .downcast_into_unchecked() - } - } - - ///Deprecated name for [`PyByteArray::from`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyByteArray::from`")] - #[inline] - pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult> { - Self::from(src) - } -} - -/// Implementation of functionality for [`PyByteArray`]. -/// -/// These methods are defined for the `Bound<'py, PyByteArray>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyByteArray")] -pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed { - /// Gets the length of the bytearray. - fn len(&self) -> usize; - - /// Checks if the bytearray is empty. - fn is_empty(&self) -> bool; - - /// Gets the start of the buffer containing the contents of the bytearray. - /// - /// # Safety - /// - /// See the safety requirements of [`PyByteArrayMethods::as_bytes`] and [`PyByteArrayMethods::as_bytes_mut`]. - fn data(&self) -> *mut u8; - - /// Extracts a slice of the `ByteArray`'s entire buffer. - /// - /// # Safety - /// - /// Mutation of the `bytearray` invalidates the slice. If it is used afterwards, the behavior is - /// undefined. - /// - /// These mutations may occur in Python code as well as from Rust: - /// - Calling methods like [`PyByteArrayMethods::as_bytes_mut`] and [`PyByteArrayMethods::resize`] will - /// invalidate the slice. - /// - Actions like dropping objects or raising exceptions can invoke `__del__`methods or signal - /// handlers, which may execute arbitrary Python code. This means that if Python code has a - /// reference to the `bytearray` you cannot safely use the vast majority of PyO3's API whilst - /// using the slice. - /// - /// As a result, this slice should only be used for short-lived operations without executing any - /// Python code, such as copying into a Vec. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::exceptions::PyRuntimeError; - /// use pyo3::types::PyByteArray; - /// - /// #[pyfunction] - /// fn a_valid_function(bytes: &Bound<'_, PyByteArray>) -> PyResult<()> { - /// let section = { - /// // SAFETY: We promise to not let the interpreter regain control - /// // or invoke any PyO3 APIs while using the slice. - /// let slice = unsafe { bytes.as_bytes() }; - /// - /// // Copy only a section of `bytes` while avoiding - /// // `to_vec` which copies the entire thing. - /// let section = slice - /// .get(6..11) - /// .ok_or_else(|| PyRuntimeError::new_err("input is not long enough"))?; - /// Vec::from(section) - /// }; - /// - /// // Now we can do things with `section` and call PyO3 APIs again. - /// // ... - /// # assert_eq!(§ion, b"world"); - /// - /// Ok(()) - /// } - /// # fn main() -> PyResult<()> { - /// # Python::with_gil(|py| -> PyResult<()> { - /// # let fun = wrap_pyfunction!(a_valid_function, py)?; - /// # let locals = pyo3::types::PyDict::new(py); - /// # locals.set_item("a_valid_function", fun)?; - /// # - /// # py.run(pyo3::ffi::c_str!( - /// # r#"b = bytearray(b"hello world") - /// # a_valid_function(b) - /// # - /// # try: - /// # a_valid_function(bytearray()) - /// # except RuntimeError as e: - /// # assert str(e) == 'input is not long enough'"#), - /// # None, - /// # Some(&locals), - /// # )?; - /// # - /// # Ok(()) - /// # }) - /// # } - /// ``` - /// - /// # Incorrect usage - /// - /// The following `bug` function is unsound ⚠️ - /// - /// ```rust,no_run - /// # use pyo3::prelude::*; - /// # use pyo3::types::PyByteArray; - /// - /// # #[allow(dead_code)] - /// #[pyfunction] - /// fn bug(py: Python<'_>, bytes: &Bound<'_, PyByteArray>) { - /// let slice = unsafe { bytes.as_bytes() }; - /// - /// // This explicitly yields control back to the Python interpreter... - /// // ...but it's not always this obvious. Many things do this implicitly. - /// py.allow_threads(|| { - /// // Python code could be mutating through its handle to `bytes`, - /// // which makes reading it a data race, which is undefined behavior. - /// println!("{:?}", slice[0]); - /// }); - /// - /// // Python code might have mutated it, so we can not rely on the slice - /// // remaining valid. As such this is also undefined behavior. - /// println!("{:?}", slice[0]); - /// } - /// ``` - unsafe fn as_bytes(&self) -> &[u8]; - - /// Extracts a mutable slice of the `ByteArray`'s entire buffer. - /// - /// # Safety - /// - /// Any other accesses of the `bytearray`'s buffer invalidate the slice. If it is used - /// afterwards, the behavior is undefined. The safety requirements of [`PyByteArrayMethods::as_bytes`] - /// apply to this function as well. - #[allow(clippy::mut_from_ref)] - unsafe fn as_bytes_mut(&self) -> &mut [u8]; - - /// Copies the contents of the bytearray to a Rust vector. - /// - /// # Examples - /// - /// ``` - /// # use pyo3::prelude::*; - /// # use pyo3::types::PyByteArray; - /// # Python::with_gil(|py| { - /// let bytearray = PyByteArray::new(py, b"Hello World."); - /// let mut copied_message = bytearray.to_vec(); - /// assert_eq!(b"Hello World.", copied_message.as_slice()); - /// - /// copied_message[11] = b'!'; - /// assert_eq!(b"Hello World!", copied_message.as_slice()); - /// - /// pyo3::py_run!(py, bytearray, "assert bytearray == b'Hello World.'"); - /// # }); - /// ``` - fn to_vec(&self) -> Vec; - - /// Resizes the bytearray object to the new length `len`. - /// - /// Note that this will invalidate any pointers obtained by [PyByteArrayMethods::data], as well as - /// any (unsafe) slices obtained from [PyByteArrayMethods::as_bytes] and [PyByteArrayMethods::as_bytes_mut]. - fn resize(&self, len: usize) -> PyResult<()>; -} - -impl<'py> PyByteArrayMethods<'py> for Bound<'py, PyByteArray> { - #[inline] - fn len(&self) -> usize { - // non-negative Py_ssize_t should always fit into Rust usize - unsafe { ffi::PyByteArray_Size(self.as_ptr()) as usize } - } - - fn is_empty(&self) -> bool { - self.len() == 0 - } - - fn data(&self) -> *mut u8 { - self.as_borrowed().data() - } - - unsafe fn as_bytes(&self) -> &[u8] { - self.as_borrowed().as_bytes() - } - - #[allow(clippy::mut_from_ref)] - unsafe fn as_bytes_mut(&self) -> &mut [u8] { - self.as_borrowed().as_bytes_mut() - } - - fn to_vec(&self) -> Vec { - unsafe { self.as_bytes() }.to_vec() - } - - fn resize(&self, len: usize) -> PyResult<()> { - unsafe { - let result = ffi::PyByteArray_Resize(self.as_ptr(), len as ffi::Py_ssize_t); - if result == 0 { - Ok(()) - } else { - Err(PyErr::fetch(self.py())) - } - } - } -} - -impl<'a> Borrowed<'a, '_, PyByteArray> { - fn data(&self) -> *mut u8 { - unsafe { ffi::PyByteArray_AsString(self.as_ptr()).cast() } - } - - #[allow(clippy::wrong_self_convention)] - unsafe fn as_bytes(self) -> &'a [u8] { - slice::from_raw_parts(self.data(), self.len()) - } - - #[allow(clippy::wrong_self_convention)] - unsafe fn as_bytes_mut(self) -> &'a mut [u8] { - slice::from_raw_parts_mut(self.data(), self.len()) - } -} - -impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyByteArray> { - type Error = crate::PyErr; - - /// Creates a new Python `bytearray` object from another Python object that - /// implements the buffer protocol. - fn try_from(value: &Bound<'py, PyAny>) -> Result { - PyByteArray::from(value) - } -} - -#[cfg(test)] -mod tests { - use crate::types::{PyAnyMethods, PyByteArray, PyByteArrayMethods}; - use crate::{exceptions, Bound, PyAny, PyObject, Python}; - - #[test] - fn test_len() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - assert_eq!(src.len(), bytearray.len()); - }); - } - - #[test] - fn test_as_bytes() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - - let slice = unsafe { bytearray.as_bytes() }; - assert_eq!(src, slice); - assert_eq!(bytearray.data() as *const _, slice.as_ptr()); - }); - } - - #[test] - fn test_as_bytes_mut() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - - let slice = unsafe { bytearray.as_bytes_mut() }; - assert_eq!(src, slice); - assert_eq!(bytearray.data(), slice.as_mut_ptr()); - - slice[0..5].copy_from_slice(b"Hi..."); - - assert_eq!(bytearray.str().unwrap(), "bytearray(b'Hi... Python')"); - }); - } - - #[test] - fn test_to_vec() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - - let vec = bytearray.to_vec(); - assert_eq!(src, vec.as_slice()); - }); - } - - #[test] - fn test_from() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - - let ba: PyObject = bytearray.into(); - let bytearray = PyByteArray::from(ba.bind(py)).unwrap(); - - assert_eq!(src, unsafe { bytearray.as_bytes() }); - }); - } - - #[test] - fn test_from_err() { - Python::with_gil(|py| { - if let Err(err) = PyByteArray::from(py.None().bind(py)) { - assert!(err.is_instance_of::(py)); - } else { - panic!("error"); - } - }); - } - - #[test] - fn test_try_from() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray: &Bound<'_, PyAny> = &PyByteArray::new(py, src); - let bytearray: Bound<'_, PyByteArray> = TryInto::try_into(bytearray).unwrap(); - - assert_eq!(src, unsafe { bytearray.as_bytes() }); - }); - } - - #[test] - fn test_resize() { - Python::with_gil(|py| { - let src = b"Hello Python"; - let bytearray = PyByteArray::new(py, src); - - bytearray.resize(20).unwrap(); - assert_eq!(20, bytearray.len()); - }); - } - - #[test] - fn test_byte_array_new_with() -> super::PyResult<()> { - Python::with_gil(|py| -> super::PyResult<()> { - let py_bytearray = PyByteArray::new_with(py, 10, |b: &mut [u8]| { - b.copy_from_slice(b"Hello Rust"); - Ok(()) - })?; - let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() }; - assert_eq!(bytearray, b"Hello Rust"); - Ok(()) - }) - } - - #[test] - fn test_byte_array_new_with_zero_initialised() -> super::PyResult<()> { - Python::with_gil(|py| -> super::PyResult<()> { - let py_bytearray = PyByteArray::new_with(py, 10, |_b: &mut [u8]| Ok(()))?; - let bytearray: &[u8] = unsafe { py_bytearray.as_bytes() }; - assert_eq!(bytearray, &[0; 10]); - Ok(()) - }) - } - - #[test] - fn test_byte_array_new_with_error() { - use crate::exceptions::PyValueError; - Python::with_gil(|py| { - let py_bytearray_result = PyByteArray::new_with(py, 10, |_b: &mut [u8]| { - Err(PyValueError::new_err("Hello Crustaceans!")) - }); - assert!(py_bytearray_result.is_err()); - assert!(py_bytearray_result - .err() - .unwrap() - .is_instance_of::(py)); - }) - } -} diff --git a/vendor/pyo3/src/types/bytes.rs b/vendor/pyo3/src/types/bytes.rs deleted file mode 100644 index 77b1d2b735..0000000000 --- a/vendor/pyo3/src/types/bytes.rs +++ /dev/null @@ -1,397 +0,0 @@ -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::{Borrowed, Bound}; -use crate::types::any::PyAnyMethods; -use crate::{ffi, Py, PyAny, PyResult, Python}; -use std::ops::Index; -use std::slice::SliceIndex; -use std::str; - -/// Represents a Python `bytes` object. -/// -/// This type is immutable. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyBytes>`][Bound]. -/// -/// For APIs available on `bytes` objects, see the [`PyBytesMethods`] trait which is implemented for -/// [`Bound<'py, PyBytes>`][Bound]. -/// -/// # Equality -/// -/// For convenience, [`Bound<'py, PyBytes>`][Bound] implements [`PartialEq<[u8]>`][PartialEq] to allow comparing the -/// data in the Python bytes to a Rust `[u8]` byte slice. -/// -/// This is not always the most appropriate way to compare Python bytes, as Python bytes subclasses -/// may have different equality semantics. In situations where subclasses overriding equality might be -/// relevant, use [`PyAnyMethods::eq`], at cost of the additional overhead of a Python method call. -/// -/// ```rust -/// # use pyo3::prelude::*; -/// use pyo3::types::PyBytes; -/// -/// # Python::with_gil(|py| { -/// let py_bytes = PyBytes::new(py, b"foo".as_slice()); -/// // via PartialEq<[u8]> -/// assert_eq!(py_bytes, b"foo".as_slice()); -/// -/// // via Python equality -/// let other = PyBytes::new(py, b"foo".as_slice()); -/// assert!(py_bytes.as_any().eq(other).unwrap()); -/// -/// // Note that `eq` will convert its argument to Python using `IntoPyObject`. -/// // Byte collections are specialized, so that the following slice will indeed -/// // convert into a `bytes` object and not a `list`: -/// assert!(py_bytes.as_any().eq(b"foo".as_slice()).unwrap()); -/// # }); -/// ``` -#[repr(transparent)] -pub struct PyBytes(PyAny); - -pyobject_native_type_core!(PyBytes, pyobject_native_static_type_object!(ffi::PyBytes_Type), #checkfunction=ffi::PyBytes_Check); - -impl PyBytes { - /// Creates a new Python bytestring object. - /// The bytestring is initialized by copying the data from the `&[u8]`. - /// - /// Panics if out of memory. - pub fn new<'p>(py: Python<'p>, s: &[u8]) -> Bound<'p, PyBytes> { - let ptr = s.as_ptr().cast(); - let len = s.len() as ffi::Py_ssize_t; - unsafe { - ffi::PyBytes_FromStringAndSize(ptr, len) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyBytes::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyBytes::new`")] - #[inline] - pub fn new_bound<'p>(py: Python<'p>, s: &[u8]) -> Bound<'p, PyBytes> { - Self::new(py, s) - } - - /// Creates a new Python `bytes` object with an `init` closure to write its contents. - /// Before calling `init` the bytes' contents are zero-initialised. - /// * If Python raises a MemoryError on the allocation, `new_with` will return - /// it inside `Err`. - /// * If `init` returns `Err(e)`, `new_with` will return `Err(e)`. - /// * If `init` returns `Ok(())`, `new_with` will return `Ok(&PyBytes)`. - /// - /// # Examples - /// - /// ``` - /// use pyo3::{prelude::*, types::PyBytes}; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let py_bytes = PyBytes::new_with(py, 10, |bytes: &mut [u8]| { - /// bytes.copy_from_slice(b"Hello Rust"); - /// Ok(()) - /// })?; - /// let bytes: &[u8] = py_bytes.extract()?; - /// assert_eq!(bytes, b"Hello Rust"); - /// Ok(()) - /// }) - /// # } - /// ``` - #[inline] - pub fn new_with(py: Python<'_>, len: usize, init: F) -> PyResult> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - unsafe { - let pyptr = ffi::PyBytes_FromStringAndSize(std::ptr::null(), len as ffi::Py_ssize_t); - // Check for an allocation error and return it - let pybytes = pyptr.assume_owned_or_err(py)?.downcast_into_unchecked(); - let buffer: *mut u8 = ffi::PyBytes_AsString(pyptr).cast(); - debug_assert!(!buffer.is_null()); - // Zero-initialise the uninitialised bytestring - std::ptr::write_bytes(buffer, 0u8, len); - // (Further) Initialise the bytestring in init - // If init returns an Err, pypybytearray will automatically deallocate the buffer - init(std::slice::from_raw_parts_mut(buffer, len)).map(|_| pybytes) - } - } - - /// Deprecated name for [`PyBytes::new_with`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyBytes::new_with`")] - #[inline] - pub fn new_bound_with(py: Python<'_>, len: usize, init: F) -> PyResult> - where - F: FnOnce(&mut [u8]) -> PyResult<()>, - { - Self::new_with(py, len, init) - } - - /// Creates a new Python byte string object from a raw pointer and length. - /// - /// Panics if out of memory. - /// - /// # Safety - /// - /// This function dereferences the raw pointer `ptr` as the - /// leading pointer of a slice of length `len`. [As with - /// `std::slice::from_raw_parts`, this is - /// unsafe](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety). - pub unsafe fn from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> Bound<'_, PyBytes> { - ffi::PyBytes_FromStringAndSize(ptr.cast(), len as isize) - .assume_owned(py) - .downcast_into_unchecked() - } - - /// Deprecated name for [`PyBytes::from_ptr`]. - /// - /// # Safety - /// - /// This function dereferences the raw pointer `ptr` as the - /// leading pointer of a slice of length `len`. [As with - /// `std::slice::from_raw_parts`, this is - /// unsafe](https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html#safety). - #[deprecated(since = "0.23.0", note = "renamed to `PyBytes::from_ptr`")] - #[inline] - pub unsafe fn bound_from_ptr(py: Python<'_>, ptr: *const u8, len: usize) -> Bound<'_, PyBytes> { - Self::from_ptr(py, ptr, len) - } -} - -/// Implementation of functionality for [`PyBytes`]. -/// -/// These methods are defined for the `Bound<'py, PyBytes>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyBytes")] -pub trait PyBytesMethods<'py>: crate::sealed::Sealed { - /// Gets the Python string as a byte slice. - fn as_bytes(&self) -> &[u8]; -} - -impl<'py> PyBytesMethods<'py> for Bound<'py, PyBytes> { - #[inline] - fn as_bytes(&self) -> &[u8] { - self.as_borrowed().as_bytes() - } -} - -impl<'a> Borrowed<'a, '_, PyBytes> { - /// Gets the Python string as a byte slice. - #[allow(clippy::wrong_self_convention)] - pub(crate) fn as_bytes(self) -> &'a [u8] { - unsafe { - let buffer = ffi::PyBytes_AsString(self.as_ptr()) as *const u8; - let length = ffi::PyBytes_Size(self.as_ptr()) as usize; - debug_assert!(!buffer.is_null()); - std::slice::from_raw_parts(buffer, length) - } - } -} - -impl Py { - /// Gets the Python bytes as a byte slice. Because Python bytes are - /// immutable, the result may be used for as long as the reference to - /// `self` is held, including when the GIL is released. - pub fn as_bytes<'a>(&'a self, py: Python<'_>) -> &'a [u8] { - self.bind_borrowed(py).as_bytes() - } -} - -/// This is the same way [Vec] is indexed. -impl> Index for Bound<'_, PyBytes> { - type Output = I::Output; - - fn index(&self, index: I) -> &Self::Output { - &self.as_bytes()[index] - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<[u8]> for Bound<'_, PyBytes> { - #[inline] - fn eq(&self, other: &[u8]) -> bool { - self.as_borrowed() == *other - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<&'_ [u8]> for Bound<'_, PyBytes> { - #[inline] - fn eq(&self, other: &&[u8]) -> bool { - self.as_borrowed() == **other - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq> for [u8] { - #[inline] - fn eq(&self, other: &Bound<'_, PyBytes>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<&'_ Bound<'_, PyBytes>> for [u8] { - #[inline] - fn eq(&self, other: &&Bound<'_, PyBytes>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq> for &'_ [u8] { - #[inline] - fn eq(&self, other: &Bound<'_, PyBytes>) -> bool { - **self == other.as_borrowed() - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<[u8]> for &'_ Bound<'_, PyBytes> { - #[inline] - fn eq(&self, other: &[u8]) -> bool { - self.as_borrowed() == other - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<[u8]> for Borrowed<'_, '_, PyBytes> { - #[inline] - fn eq(&self, other: &[u8]) -> bool { - self.as_bytes() == other - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq<&[u8]> for Borrowed<'_, '_, PyBytes> { - #[inline] - fn eq(&self, other: &&[u8]) -> bool { - *self == **other - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq> for [u8] { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyBytes>) -> bool { - other == self - } -} - -/// Compares whether the Python bytes object is equal to the [u8]. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyBytes`]. -impl PartialEq> for &'_ [u8] { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyBytes>) -> bool { - other == self - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_bytes_index() { - Python::with_gil(|py| { - let bytes = PyBytes::new(py, b"Hello World"); - assert_eq!(bytes[1], b'e'); - }); - } - - #[test] - fn test_bound_bytes_index() { - Python::with_gil(|py| { - let bytes = PyBytes::new(py, b"Hello World"); - assert_eq!(bytes[1], b'e'); - - let bytes = &bytes; - assert_eq!(bytes[1], b'e'); - }); - } - - #[test] - fn test_bytes_new_with() -> super::PyResult<()> { - Python::with_gil(|py| -> super::PyResult<()> { - let py_bytes = PyBytes::new_with(py, 10, |b: &mut [u8]| { - b.copy_from_slice(b"Hello Rust"); - Ok(()) - })?; - let bytes: &[u8] = py_bytes.extract()?; - assert_eq!(bytes, b"Hello Rust"); - Ok(()) - }) - } - - #[test] - fn test_bytes_new_with_zero_initialised() -> super::PyResult<()> { - Python::with_gil(|py| -> super::PyResult<()> { - let py_bytes = PyBytes::new_with(py, 10, |_b: &mut [u8]| Ok(()))?; - let bytes: &[u8] = py_bytes.extract()?; - assert_eq!(bytes, &[0; 10]); - Ok(()) - }) - } - - #[test] - fn test_bytes_new_with_error() { - use crate::exceptions::PyValueError; - Python::with_gil(|py| { - let py_bytes_result = PyBytes::new_with(py, 10, |_b: &mut [u8]| { - Err(PyValueError::new_err("Hello Crustaceans!")) - }); - assert!(py_bytes_result.is_err()); - assert!(py_bytes_result - .err() - .unwrap() - .is_instance_of::(py)); - }); - } - - #[test] - fn test_comparisons() { - Python::with_gil(|py| { - let b = b"hello, world".as_slice(); - let py_bytes = PyBytes::new(py, b); - - assert_eq!(py_bytes, b"hello, world".as_slice()); - - assert_eq!(py_bytes, b); - assert_eq!(&py_bytes, b); - assert_eq!(b, py_bytes); - assert_eq!(b, &py_bytes); - - assert_eq!(py_bytes, *b); - assert_eq!(&py_bytes, *b); - assert_eq!(*b, py_bytes); - assert_eq!(*b, &py_bytes); - - let py_string = py_bytes.as_borrowed(); - - assert_eq!(py_string, b); - assert_eq!(&py_string, b); - assert_eq!(b, py_string); - assert_eq!(b, &py_string); - - assert_eq!(py_string, *b); - assert_eq!(*b, py_string); - }) - } -} diff --git a/vendor/pyo3/src/types/capsule.rs b/vendor/pyo3/src/types/capsule.rs deleted file mode 100644 index 9d9e6e4eb7..0000000000 --- a/vendor/pyo3/src/types/capsule.rs +++ /dev/null @@ -1,528 +0,0 @@ -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::{ffi, PyAny}; -use crate::{Bound, Python}; -use crate::{PyErr, PyResult}; -use std::ffi::{CStr, CString}; -use std::os::raw::{c_char, c_int, c_void}; -/// Represents a Python Capsule -/// as described in [Capsules](https://docs.python.org/3/c-api/capsule.html#capsules): -/// > This subtype of PyObject represents an opaque value, useful for C extension -/// > modules who need to pass an opaque value (as a void* pointer) through Python -/// > code to other C code. It is often used to make a C function pointer defined -/// > in one module available to other modules, so the regular import mechanism can -/// > be used to access C APIs defined in dynamically loaded modules. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyCapsule>`][Bound]. -/// -/// For APIs available on capsule objects, see the [`PyCapsuleMethods`] trait which is implemented for -/// [`Bound<'py, PyCapsule>`][Bound]. -/// -/// # Example -/// ``` -/// use pyo3::{prelude::*, types::PyCapsule}; -/// use std::ffi::CString; -/// -/// #[repr(C)] -/// struct Foo { -/// pub val: u32, -/// } -/// -/// let r = Python::with_gil(|py| -> PyResult<()> { -/// let foo = Foo { val: 123 }; -/// let name = CString::new("builtins.capsule").unwrap(); -/// -/// let capsule = PyCapsule::new(py, foo, Some(name.clone()))?; -/// -/// let module = PyModule::import(py, "builtins")?; -/// module.add("capsule", capsule)?; -/// -/// let cap: &Foo = unsafe { PyCapsule::import(py, name.as_ref())? }; -/// assert_eq!(cap.val, 123); -/// Ok(()) -/// }); -/// assert!(r.is_ok()); -/// ``` -#[repr(transparent)] -pub struct PyCapsule(PyAny); - -pyobject_native_type_core!(PyCapsule, pyobject_native_static_type_object!(ffi::PyCapsule_Type), #checkfunction=ffi::PyCapsule_CheckExact); - -impl PyCapsule { - /// Constructs a new capsule whose contents are `value`, associated with `name`. - /// `name` is the identifier for the capsule; if it is stored as an attribute of a module, - /// the name should be in the format `"modulename.attribute"`. - /// - /// It is checked at compile time that the type T is not zero-sized. Rust function items - /// need to be cast to a function pointer (`fn(args) -> result`) to be put into a capsule. - /// - /// # Example - /// - /// ``` - /// use pyo3::{prelude::*, types::PyCapsule}; - /// use std::ffi::CString; - /// - /// Python::with_gil(|py| { - /// let name = CString::new("foo").unwrap(); - /// let capsule = PyCapsule::new(py, 123_u32, Some(name)).unwrap(); - /// let val = unsafe { capsule.reference::() }; - /// assert_eq!(*val, 123); - /// }); - /// ``` - /// - /// However, attempting to construct a `PyCapsule` with a zero-sized type will not compile: - /// - /// ```compile_fail - /// use pyo3::{prelude::*, types::PyCapsule}; - /// use std::ffi::CString; - /// - /// Python::with_gil(|py| { - /// let capsule = PyCapsule::new(py, (), None).unwrap(); // Oops! `()` is zero sized! - /// }); - /// ``` - pub fn new( - py: Python<'_>, - value: T, - name: Option, - ) -> PyResult> { - Self::new_with_destructor(py, value, name, |_, _| {}) - } - - /// Deprecated name for [`PyCapsule::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyCapsule::new`")] - #[inline] - pub fn new_bound( - py: Python<'_>, - value: T, - name: Option, - ) -> PyResult> { - Self::new(py, value, name) - } - - /// Constructs a new capsule whose contents are `value`, associated with `name`. - /// - /// Also provides a destructor: when the `PyCapsule` is destroyed, it will be passed the original object, - /// as well as a `*mut c_void` which will point to the capsule's context, if any. - /// - /// The `destructor` must be `Send`, because there is no guarantee which thread it will eventually - /// be called from. - pub fn new_with_destructor< - T: 'static + Send + AssertNotZeroSized, - F: FnOnce(T, *mut c_void) + Send, - >( - py: Python<'_>, - value: T, - name: Option, - destructor: F, - ) -> PyResult> { - AssertNotZeroSized::assert_not_zero_sized(&value); - - // Sanity check for capsule layout - debug_assert_eq!(memoffset::offset_of!(CapsuleContents::, value), 0); - - let name_ptr = name.as_ref().map_or(std::ptr::null(), |name| name.as_ptr()); - let val = Box::new(CapsuleContents { - value, - destructor, - name, - }); - - unsafe { - ffi::PyCapsule_New( - Box::into_raw(val).cast(), - name_ptr, - Some(capsule_destructor::), - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyCapsule::new_with_destructor`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyCapsule::new_with_destructor`")] - #[inline] - pub fn new_bound_with_destructor< - T: 'static + Send + AssertNotZeroSized, - F: FnOnce(T, *mut c_void) + Send, - >( - py: Python<'_>, - value: T, - name: Option, - destructor: F, - ) -> PyResult> { - Self::new_with_destructor(py, value, name, destructor) - } - - /// Imports an existing capsule. - /// - /// The `name` should match the path to the module attribute exactly in the form - /// of `"module.attribute"`, which should be the same as the name within the capsule. - /// - /// # Safety - /// - /// It must be known that the capsule imported by `name` contains an item of type `T`. - pub unsafe fn import<'py, T>(py: Python<'py>, name: &CStr) -> PyResult<&'py T> { - let ptr = ffi::PyCapsule_Import(name.as_ptr(), false as c_int); - if ptr.is_null() { - Err(PyErr::fetch(py)) - } else { - Ok(&*ptr.cast::()) - } - } -} - -/// Implementation of functionality for [`PyCapsule`]. -/// -/// These methods are defined for the `Bound<'py, PyCapsule>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyCapsule")] -pub trait PyCapsuleMethods<'py>: crate::sealed::Sealed { - /// Sets the context pointer in the capsule. - /// - /// Returns an error if this capsule is not valid. - /// - /// # Notes - /// - /// The context is treated much like the value of the capsule, but should likely act as - /// a place to store any state management when using the capsule. - /// - /// If you want to store a Rust value as the context, and drop it from the destructor, use - /// `Box::into_raw` to convert it into a pointer, see the example. - /// - /// # Example - /// - /// ``` - /// use std::os::raw::c_void; - /// use std::sync::mpsc::{channel, Sender}; - /// use pyo3::{prelude::*, types::PyCapsule}; - /// - /// let (tx, rx) = channel::(); - /// - /// fn destructor(val: u32, context: *mut c_void) { - /// let ctx = unsafe { *Box::from_raw(context.cast::>()) }; - /// ctx.send("Destructor called!".to_string()).unwrap(); - /// } - /// - /// Python::with_gil(|py| { - /// let capsule = - /// PyCapsule::new_with_destructor(py, 123, None, destructor as fn(u32, *mut c_void)) - /// .unwrap(); - /// let context = Box::new(tx); // `Sender` is our context, box it up and ship it! - /// capsule.set_context(Box::into_raw(context).cast()).unwrap(); - /// // This scope will end, causing our destructor to be called... - /// }); - /// - /// assert_eq!(rx.recv(), Ok("Destructor called!".to_string())); - /// ``` - fn set_context(&self, context: *mut c_void) -> PyResult<()>; - - /// Gets the current context stored in the capsule. If there is no context, the pointer - /// will be null. - /// - /// Returns an error if this capsule is not valid. - fn context(&self) -> PyResult<*mut c_void>; - - /// Obtains a reference to the value of this capsule. - /// - /// # Safety - /// - /// It must be known that this capsule is valid and its pointer is to an item of type `T`. - unsafe fn reference(&self) -> &'py T; - - /// Gets the raw `c_void` pointer to the value in this capsule. - /// - /// Returns null if this capsule is not valid. - fn pointer(&self) -> *mut c_void; - - /// Checks if this is a valid capsule. - /// - /// Returns true if the stored `pointer()` is non-null. - fn is_valid(&self) -> bool; - - /// Retrieves the name of this capsule, if set. - /// - /// Returns an error if this capsule is not valid. - fn name(&self) -> PyResult>; -} - -impl<'py> PyCapsuleMethods<'py> for Bound<'py, PyCapsule> { - #[allow(clippy::not_unsafe_ptr_arg_deref)] - fn set_context(&self, context: *mut c_void) -> PyResult<()> { - let result = unsafe { ffi::PyCapsule_SetContext(self.as_ptr(), context) }; - if result != 0 { - Err(PyErr::fetch(self.py())) - } else { - Ok(()) - } - } - - fn context(&self) -> PyResult<*mut c_void> { - let ctx = unsafe { ffi::PyCapsule_GetContext(self.as_ptr()) }; - if ctx.is_null() { - ensure_no_error(self.py())? - } - Ok(ctx) - } - - unsafe fn reference(&self) -> &'py T { - &*self.pointer().cast() - } - - fn pointer(&self) -> *mut c_void { - unsafe { - let ptr = ffi::PyCapsule_GetPointer(self.as_ptr(), name_ptr_ignore_error(self)); - if ptr.is_null() { - ffi::PyErr_Clear(); - } - ptr - } - } - - fn is_valid(&self) -> bool { - // As well as if the stored pointer is null, PyCapsule_IsValid also returns false if - // self.as_ptr() is null or not a ptr to a PyCapsule object. Both of these are guaranteed - // to not be the case thanks to invariants of this PyCapsule struct. - let r = unsafe { ffi::PyCapsule_IsValid(self.as_ptr(), name_ptr_ignore_error(self)) }; - r != 0 - } - - fn name(&self) -> PyResult> { - unsafe { - let ptr = ffi::PyCapsule_GetName(self.as_ptr()); - if ptr.is_null() { - ensure_no_error(self.py())?; - Ok(None) - } else { - Ok(Some(CStr::from_ptr(ptr))) - } - } - } -} - -// C layout, as PyCapsule::get_reference depends on `T` being first. -#[repr(C)] -struct CapsuleContents { - /// Value of the capsule - value: T, - /// Destructor to be used by the capsule - destructor: D, - /// Name used when creating the capsule - name: Option, -} - -// Wrapping ffi::PyCapsule_Destructor for a user supplied FnOnce(T) for capsule destructor -unsafe extern "C" fn capsule_destructor( - capsule: *mut ffi::PyObject, -) { - let ptr = ffi::PyCapsule_GetPointer(capsule, ffi::PyCapsule_GetName(capsule)); - let ctx = ffi::PyCapsule_GetContext(capsule); - let CapsuleContents { - value, destructor, .. - } = *Box::from_raw(ptr.cast::>()); - destructor(value, ctx) -} - -/// Guarantee `T` is not zero sized at compile time. -// credit: `` -#[doc(hidden)] -pub trait AssertNotZeroSized: Sized { - const _CONDITION: usize = (std::mem::size_of::() == 0) as usize; - const _CHECK: &'static str = - ["PyCapsule value type T must not be zero-sized!"][Self::_CONDITION]; - #[allow(path_statements, clippy::no_effect)] - fn assert_not_zero_sized(&self) { - ::_CHECK; - } -} - -impl AssertNotZeroSized for T {} - -fn ensure_no_error(py: Python<'_>) -> PyResult<()> { - if let Some(err) = PyErr::take(py) { - Err(err) - } else { - Ok(()) - } -} - -fn name_ptr_ignore_error(slf: &Bound<'_, PyCapsule>) -> *const c_char { - let ptr = unsafe { ffi::PyCapsule_GetName(slf.as_ptr()) }; - if ptr.is_null() { - unsafe { ffi::PyErr_Clear() }; - } - ptr -} - -#[cfg(test)] -mod tests { - use crate::prelude::PyModule; - use crate::types::capsule::PyCapsuleMethods; - use crate::types::module::PyModuleMethods; - use crate::{types::PyCapsule, Py, PyResult, Python}; - use std::ffi::CString; - use std::os::raw::c_void; - use std::sync::mpsc::{channel, Sender}; - - #[test] - fn test_pycapsule_struct() -> PyResult<()> { - #[repr(C)] - struct Foo { - pub val: u32, - } - - impl Foo { - fn get_val(&self) -> u32 { - self.val - } - } - - Python::with_gil(|py| -> PyResult<()> { - let foo = Foo { val: 123 }; - let name = CString::new("foo").unwrap(); - - let cap = PyCapsule::new(py, foo, Some(name.clone()))?; - assert!(cap.is_valid()); - - let foo_capi = unsafe { cap.reference::() }; - assert_eq!(foo_capi.val, 123); - assert_eq!(foo_capi.get_val(), 123); - assert_eq!(cap.name().unwrap(), Some(name.as_ref())); - Ok(()) - }) - } - - #[test] - fn test_pycapsule_func() { - fn foo(x: u32) -> u32 { - x - } - - let cap: Py = Python::with_gil(|py| { - let name = CString::new("foo").unwrap(); - let cap = PyCapsule::new(py, foo as fn(u32) -> u32, Some(name)).unwrap(); - cap.into() - }); - - Python::with_gil(move |py| { - let f = unsafe { cap.bind(py).reference:: u32>() }; - assert_eq!(f(123), 123); - }); - } - - #[test] - fn test_pycapsule_context() -> PyResult<()> { - Python::with_gil(|py| { - let name = CString::new("foo").unwrap(); - let cap = PyCapsule::new(py, 0, Some(name))?; - - let c = cap.context()?; - assert!(c.is_null()); - - let ctx = Box::new(123_u32); - cap.set_context(Box::into_raw(ctx).cast())?; - - let ctx_ptr: *mut c_void = cap.context()?; - let ctx = unsafe { *Box::from_raw(ctx_ptr.cast::()) }; - assert_eq!(ctx, 123); - Ok(()) - }) - } - - #[test] - fn test_pycapsule_import() -> PyResult<()> { - #[repr(C)] - struct Foo { - pub val: u32, - } - - Python::with_gil(|py| -> PyResult<()> { - let foo = Foo { val: 123 }; - let name = CString::new("builtins.capsule").unwrap(); - - let capsule = PyCapsule::new(py, foo, Some(name.clone()))?; - - let module = PyModule::import(py, "builtins")?; - module.add("capsule", capsule)?; - - // check error when wrong named passed for capsule. - let wrong_name = CString::new("builtins.non_existant").unwrap(); - let result: PyResult<&Foo> = unsafe { PyCapsule::import(py, wrong_name.as_ref()) }; - assert!(result.is_err()); - - // corret name is okay. - let cap: &Foo = unsafe { PyCapsule::import(py, name.as_ref())? }; - assert_eq!(cap.val, 123); - Ok(()) - }) - } - - #[test] - fn test_vec_storage() { - let cap: Py = Python::with_gil(|py| { - let name = CString::new("foo").unwrap(); - - let stuff: Vec = vec![1, 2, 3, 4]; - let cap = PyCapsule::new(py, stuff, Some(name)).unwrap(); - - cap.into() - }); - - Python::with_gil(move |py| { - let ctx: &Vec = unsafe { cap.bind(py).reference() }; - assert_eq!(ctx, &[1, 2, 3, 4]); - }) - } - - #[test] - fn test_vec_context() { - let context: Vec = vec![1, 2, 3, 4]; - - let cap: Py = Python::with_gil(|py| { - let name = CString::new("foo").unwrap(); - let cap = PyCapsule::new(py, 0, Some(name)).unwrap(); - cap.set_context(Box::into_raw(Box::new(&context)).cast()) - .unwrap(); - - cap.into() - }); - - Python::with_gil(move |py| { - let ctx_ptr: *mut c_void = cap.bind(py).context().unwrap(); - let ctx = unsafe { *Box::from_raw(ctx_ptr.cast::<&Vec>()) }; - assert_eq!(ctx, &vec![1_u8, 2, 3, 4]); - }) - } - - #[test] - fn test_pycapsule_destructor() { - let (tx, rx) = channel::(); - - fn destructor(_val: u32, ctx: *mut c_void) { - assert!(!ctx.is_null()); - let context = unsafe { *Box::from_raw(ctx.cast::>()) }; - context.send(true).unwrap(); - } - - Python::with_gil(move |py| { - let name = CString::new("foo").unwrap(); - let cap = PyCapsule::new_with_destructor(py, 0, Some(name), destructor).unwrap(); - cap.set_context(Box::into_raw(Box::new(tx)).cast()).unwrap(); - }); - - // the destructor was called. - assert_eq!(rx.recv(), Ok(true)); - } - - #[test] - fn test_pycapsule_no_name() { - Python::with_gil(|py| { - let cap = PyCapsule::new(py, 0usize, None).unwrap(); - - assert_eq!(unsafe { cap.reference::() }, &0usize); - assert_eq!(cap.name().unwrap(), None); - assert_eq!(cap.context().unwrap(), std::ptr::null_mut()); - }); - } -} diff --git a/vendor/pyo3/src/types/code.rs b/vendor/pyo3/src/types/code.rs deleted file mode 100644 index 0c1683c75b..0000000000 --- a/vendor/pyo3/src/types/code.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::ffi; -use crate::PyAny; - -/// Represents a Python code object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyCode>`][crate::Bound]. -#[repr(transparent)] -pub struct PyCode(PyAny); - -pyobject_native_type_core!( - PyCode, - pyobject_native_static_type_object!(ffi::PyCode_Type), - #checkfunction=ffi::PyCode_Check -); - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::PyTypeMethods; - use crate::{PyTypeInfo, Python}; - - #[test] - fn test_type_object() { - Python::with_gil(|py| { - assert_eq!(PyCode::type_object(py).name().unwrap(), "code"); - }) - } -} diff --git a/vendor/pyo3/src/types/complex.rs b/vendor/pyo3/src/types/complex.rs deleted file mode 100644 index 58651569b4..0000000000 --- a/vendor/pyo3/src/types/complex.rs +++ /dev/null @@ -1,277 +0,0 @@ -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use crate::py_result_ext::PyResultExt; -use crate::{ffi, types::any::PyAnyMethods, Bound, PyAny, Python}; -use std::os::raw::c_double; - -/// Represents a Python [`complex`](https://docs.python.org/3/library/functions.html#complex) object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyComplex>`][Bound]. -/// -/// For APIs available on `complex` objects, see the [`PyComplexMethods`] trait which is implemented for -/// [`Bound<'py, PyComplex>`][Bound]. -/// -/// Note that `PyComplex` supports only basic operations. For advanced operations -/// consider using [num-complex](https://docs.rs/num-complex)'s [`Complex`] type instead. -/// This optional dependency can be activated with the `num-complex` feature flag. -/// -/// [`Complex`]: https://docs.rs/num-complex/latest/num_complex/struct.Complex.html -#[repr(transparent)] -pub struct PyComplex(PyAny); - -pyobject_subclassable_native_type!(PyComplex, ffi::PyComplexObject); - -pyobject_native_type!( - PyComplex, - ffi::PyComplexObject, - pyobject_native_static_type_object!(ffi::PyComplex_Type), - #checkfunction=ffi::PyComplex_Check -); - -impl PyComplex { - /// Creates a new `PyComplex` from the given real and imaginary values. - pub fn from_doubles(py: Python<'_>, real: c_double, imag: c_double) -> Bound<'_, PyComplex> { - use crate::ffi_ptr_ext::FfiPtrExt; - unsafe { - ffi::PyComplex_FromDoubles(real, imag) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyComplex::from_doubles`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyComplex::from_doubles`")] - #[inline] - pub fn from_doubles_bound( - py: Python<'_>, - real: c_double, - imag: c_double, - ) -> Bound<'_, PyComplex> { - Self::from_doubles(py, real, imag) - } -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -mod not_limited_impls { - use crate::Borrowed; - - use super::*; - use std::ops::{Add, Div, Mul, Neg, Sub}; - - macro_rules! bin_ops { - ($trait:ident, $fn:ident, $op:tt) => { - impl<'py> $trait for Borrowed<'_, 'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn $fn(self, other: Self) -> Self::Output { - PyAnyMethods::$fn(self.as_any(), other) - .downcast_into().expect( - concat!("Complex method ", - stringify!($fn), - " failed.") - ) - } - } - - impl<'py> $trait for &Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn $fn(self, other: &Bound<'py, PyComplex>) -> Bound<'py, PyComplex> { - self.as_borrowed() $op other.as_borrowed() - } - } - - impl<'py> $trait> for &Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn $fn(self, other: Bound<'py, PyComplex>) -> Bound<'py, PyComplex> { - self.as_borrowed() $op other.as_borrowed() - } - } - - impl<'py> $trait for Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn $fn(self, other: Bound<'py, PyComplex>) -> Bound<'py, PyComplex> { - self.as_borrowed() $op other.as_borrowed() - } - } - - impl<'py> $trait<&Self> for Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn $fn(self, other: &Bound<'py, PyComplex>) -> Bound<'py, PyComplex> { - self.as_borrowed() $op other.as_borrowed() - } - } - }; - } - - bin_ops!(Add, add, +); - bin_ops!(Sub, sub, -); - bin_ops!(Mul, mul, *); - bin_ops!(Div, div, /); - - impl<'py> Neg for Borrowed<'_, 'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn neg(self) -> Self::Output { - PyAnyMethods::neg(self.as_any()) - .downcast_into() - .expect("Complex method __neg__ failed.") - } - } - - impl<'py> Neg for &Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn neg(self) -> Bound<'py, PyComplex> { - -self.as_borrowed() - } - } - - impl<'py> Neg for Bound<'py, PyComplex> { - type Output = Bound<'py, PyComplex>; - fn neg(self) -> Bound<'py, PyComplex> { - -self.as_borrowed() - } - } - - #[cfg(test)] - mod tests { - use super::PyComplex; - use crate::{types::complex::PyComplexMethods, Python}; - use assert_approx_eq::assert_approx_eq; - - #[test] - fn test_add() { - Python::with_gil(|py| { - let l = PyComplex::from_doubles(py, 3.0, 1.2); - let r = PyComplex::from_doubles(py, 1.0, 2.6); - let res = l + r; - assert_approx_eq!(res.real(), 4.0); - assert_approx_eq!(res.imag(), 3.8); - }); - } - - #[test] - fn test_sub() { - Python::with_gil(|py| { - let l = PyComplex::from_doubles(py, 3.0, 1.2); - let r = PyComplex::from_doubles(py, 1.0, 2.6); - let res = l - r; - assert_approx_eq!(res.real(), 2.0); - assert_approx_eq!(res.imag(), -1.4); - }); - } - - #[test] - fn test_mul() { - Python::with_gil(|py| { - let l = PyComplex::from_doubles(py, 3.0, 1.2); - let r = PyComplex::from_doubles(py, 1.0, 2.6); - let res = l * r; - assert_approx_eq!(res.real(), -0.12); - assert_approx_eq!(res.imag(), 9.0); - }); - } - - #[test] - fn test_div() { - Python::with_gil(|py| { - let l = PyComplex::from_doubles(py, 3.0, 1.2); - let r = PyComplex::from_doubles(py, 1.0, 2.6); - let res = l / r; - assert_approx_eq!(res.real(), 0.788_659_793_814_432_9); - assert_approx_eq!(res.imag(), -0.850_515_463_917_525_7); - }); - } - - #[test] - fn test_neg() { - Python::with_gil(|py| { - let val = PyComplex::from_doubles(py, 3.0, 1.2); - let res = -val; - assert_approx_eq!(res.real(), -3.0); - assert_approx_eq!(res.imag(), -1.2); - }); - } - - #[test] - fn test_abs() { - Python::with_gil(|py| { - let val = PyComplex::from_doubles(py, 3.0, 1.2); - assert_approx_eq!(val.abs(), 3.231_098_884_280_702_2); - }); - } - - #[test] - fn test_pow() { - Python::with_gil(|py| { - let l = PyComplex::from_doubles(py, 3.0, 1.2); - let r = PyComplex::from_doubles(py, 1.2, 2.6); - let val = l.pow(&r); - assert_approx_eq!(val.real(), -1.419_309_997_016_603_7); - assert_approx_eq!(val.imag(), -0.541_297_466_033_544_6); - }); - } - } -} - -/// Implementation of functionality for [`PyComplex`]. -/// -/// These methods are defined for the `Bound<'py, PyComplex>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyComplex")] -pub trait PyComplexMethods<'py>: crate::sealed::Sealed { - /// Returns the real part of the complex number. - fn real(&self) -> c_double; - /// Returns the imaginary part of the complex number. - fn imag(&self) -> c_double; - /// Returns `|self|`. - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - fn abs(&self) -> c_double; - /// Returns `self` raised to the power of `other`. - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - fn pow(&self, other: &Bound<'py, PyComplex>) -> Bound<'py, PyComplex>; -} - -impl<'py> PyComplexMethods<'py> for Bound<'py, PyComplex> { - fn real(&self) -> c_double { - unsafe { ffi::PyComplex_RealAsDouble(self.as_ptr()) } - } - - fn imag(&self) -> c_double { - unsafe { ffi::PyComplex_ImagAsDouble(self.as_ptr()) } - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - fn abs(&self) -> c_double { - PyAnyMethods::abs(self.as_any()) - .downcast_into() - .expect("Complex method __abs__ failed.") - .extract() - .expect("Failed to extract to c double.") - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - fn pow(&self, other: &Bound<'py, PyComplex>) -> Bound<'py, PyComplex> { - Python::with_gil(|py| { - PyAnyMethods::pow(self.as_any(), other, py.None()) - .downcast_into() - .expect("Complex method __pow__ failed.") - }) - } -} - -#[cfg(test)] -mod tests { - use super::PyComplex; - use crate::{types::complex::PyComplexMethods, Python}; - use assert_approx_eq::assert_approx_eq; - - #[test] - fn test_from_double() { - use assert_approx_eq::assert_approx_eq; - - Python::with_gil(|py| { - let complex = PyComplex::from_doubles(py, 3.0, 1.2); - assert_approx_eq!(complex.real(), 3.0); - assert_approx_eq!(complex.imag(), 1.2); - }); - } -} diff --git a/vendor/pyo3/src/types/datetime.rs b/vendor/pyo3/src/types/datetime.rs deleted file mode 100644 index 8ab512ac46..0000000000 --- a/vendor/pyo3/src/types/datetime.rs +++ /dev/null @@ -1,898 +0,0 @@ -//! Safe Rust wrappers for types defined in the Python `datetime` library -//! -//! For more details about these types, see the [Python -//! documentation](https://docs.python.org/3/library/datetime.html) - -use crate::err::PyResult; -use crate::ffi::{ - self, PyDateTime_CAPI, PyDateTime_FromTimestamp, PyDateTime_IMPORT, PyDate_FromTimestamp, -}; -use crate::ffi::{ - PyDateTime_DATE_GET_FOLD, PyDateTime_DATE_GET_HOUR, PyDateTime_DATE_GET_MICROSECOND, - PyDateTime_DATE_GET_MINUTE, PyDateTime_DATE_GET_SECOND, -}; -#[cfg(GraalPy)] -use crate::ffi::{PyDateTime_DATE_GET_TZINFO, PyDateTime_TIME_GET_TZINFO, Py_IsNone}; -use crate::ffi::{ - PyDateTime_DELTA_GET_DAYS, PyDateTime_DELTA_GET_MICROSECONDS, PyDateTime_DELTA_GET_SECONDS, -}; -use crate::ffi::{PyDateTime_GET_DAY, PyDateTime_GET_MONTH, PyDateTime_GET_YEAR}; -use crate::ffi::{ - PyDateTime_TIME_GET_FOLD, PyDateTime_TIME_GET_HOUR, PyDateTime_TIME_GET_MICROSECOND, - PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND, -}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::types::any::PyAnyMethods; -use crate::types::PyTuple; -use crate::{Bound, IntoPyObject, PyAny, PyErr, Python}; -use std::os::raw::c_int; -#[cfg(feature = "chrono")] -use std::ptr; - -fn ensure_datetime_api(py: Python<'_>) -> PyResult<&'static PyDateTime_CAPI> { - if let Some(api) = unsafe { pyo3_ffi::PyDateTimeAPI().as_ref() } { - Ok(api) - } else { - unsafe { - PyDateTime_IMPORT(); - pyo3_ffi::PyDateTimeAPI().as_ref() - } - .ok_or_else(|| PyErr::fetch(py)) - } -} - -fn expect_datetime_api(py: Python<'_>) -> &'static PyDateTime_CAPI { - ensure_datetime_api(py).expect("failed to import `datetime` C API") -} - -// Type Check macros -// -// These are bindings around the C API typecheck macros, all of them return -// `1` if True and `0` if False. In all type check macros, the argument (`op`) -// must not be `NULL`. The implementations here all call ensure_datetime_api -// to ensure that the PyDateTimeAPI is initialized before use -// -// -// # Safety -// -// These functions must only be called when the GIL is held! - -macro_rules! ffi_fun_with_autoinit { - ($(#[$outer:meta] unsafe fn $name: ident($arg: ident: *mut PyObject) -> $ret: ty;)*) => { - $( - #[$outer] - #[allow(non_snake_case)] - /// # Safety - /// - /// Must only be called while the GIL is held - unsafe fn $name($arg: *mut crate::ffi::PyObject) -> $ret { - - let _ = ensure_datetime_api(Python::assume_gil_acquired()); - crate::ffi::$name($arg) - } - )* - - - }; -} - -ffi_fun_with_autoinit! { - /// Check if `op` is a `PyDateTimeAPI.DateType` or subtype. - unsafe fn PyDate_Check(op: *mut PyObject) -> c_int; - - /// Check if `op` is a `PyDateTimeAPI.DateTimeType` or subtype. - unsafe fn PyDateTime_Check(op: *mut PyObject) -> c_int; - - /// Check if `op` is a `PyDateTimeAPI.TimeType` or subtype. - unsafe fn PyTime_Check(op: *mut PyObject) -> c_int; - - /// Check if `op` is a `PyDateTimeAPI.DetaType` or subtype. - unsafe fn PyDelta_Check(op: *mut PyObject) -> c_int; - - /// Check if `op` is a `PyDateTimeAPI.TZInfoType` or subtype. - unsafe fn PyTZInfo_Check(op: *mut PyObject) -> c_int; -} - -// Access traits - -/// Trait for accessing the date components of a struct containing a date. -pub trait PyDateAccess { - /// Returns the year, as a positive int. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_year(&self) -> i32; - /// Returns the month, as an int from 1 through 12. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_month(&self) -> u8; - /// Returns the day, as an int from 1 through 31. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_day(&self) -> u8; -} - -/// Trait for accessing the components of a struct containing a timedelta. -/// -/// Note: These access the individual components of a (day, second, -/// microsecond) representation of the delta, they are *not* intended as -/// aliases for calculating the total duration in each of these units. -pub trait PyDeltaAccess { - /// Returns the number of days, as an int from -999999999 to 999999999. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_days(&self) -> i32; - /// Returns the number of seconds, as an int from 0 through 86399. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_seconds(&self) -> i32; - /// Returns the number of microseconds, as an int from 0 through 999999. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_microseconds(&self) -> i32; -} - -/// Trait for accessing the time components of a struct containing a time. -pub trait PyTimeAccess { - /// Returns the hour, as an int from 0 through 23. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_hour(&self) -> u8; - /// Returns the minute, as an int from 0 through 59. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_minute(&self) -> u8; - /// Returns the second, as an int from 0 through 59. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_second(&self) -> u8; - /// Returns the microsecond, as an int from 0 through 999999. - /// - /// Implementations should conform to the upstream documentation: - /// - fn get_microsecond(&self) -> u32; - /// Returns whether this date is the later of two moments with the - /// same representation, during a repeated interval. - /// - /// This typically occurs at the end of daylight savings time. Only valid if the - /// represented time is ambiguous. - /// See [PEP 495](https://www.python.org/dev/peps/pep-0495/) for more detail. - fn get_fold(&self) -> bool; -} - -/// Trait for accessing the components of a struct containing a tzinfo. -pub trait PyTzInfoAccess<'py> { - /// Returns the tzinfo (which may be None). - /// - /// Implementations should conform to the upstream documentation: - /// - /// - fn get_tzinfo(&self) -> Option>; - - /// Deprecated name for [`PyTzInfoAccess::get_tzinfo`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTzInfoAccess::get_tzinfo`")] - #[inline] - fn get_tzinfo_bound(&self) -> Option> { - self.get_tzinfo() - } -} - -/// Bindings around `datetime.date`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyDate>`][Bound]. -#[repr(transparent)] -pub struct PyDate(PyAny); -pyobject_native_type!( - PyDate, - crate::ffi::PyDateTime_Date, - |py| expect_datetime_api(py).DateType, - #module=Some("datetime"), - #checkfunction=PyDate_Check -); -pyobject_subclassable_native_type!(PyDate, crate::ffi::PyDateTime_Date); - -impl PyDate { - /// Creates a new `datetime.date`. - pub fn new(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.Date_FromDate)(year, c_int::from(month), c_int::from(day), api.DateType) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDate::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDate::new`")] - #[inline] - pub fn new_bound(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult> { - Self::new(py, year, month, day) - } - - /// Construct a `datetime.date` from a POSIX timestamp - /// - /// This is equivalent to `datetime.date.fromtimestamp` - pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult> { - let time_tuple = PyTuple::new(py, [timestamp])?; - - // safety ensure that the API is loaded - let _api = ensure_datetime_api(py)?; - - unsafe { - PyDate_FromTimestamp(time_tuple.as_ptr()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDate::from_timestamp`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDate::from_timestamp`")] - #[inline] - pub fn from_timestamp_bound(py: Python<'_>, timestamp: i64) -> PyResult> { - Self::from_timestamp(py, timestamp) - } -} - -impl PyDateAccess for Bound<'_, PyDate> { - fn get_year(&self) -> i32 { - unsafe { PyDateTime_GET_YEAR(self.as_ptr()) } - } - - fn get_month(&self) -> u8 { - unsafe { PyDateTime_GET_MONTH(self.as_ptr()) as u8 } - } - - fn get_day(&self) -> u8 { - unsafe { PyDateTime_GET_DAY(self.as_ptr()) as u8 } - } -} - -/// Bindings for `datetime.datetime`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyDateTime>`][Bound]. -#[repr(transparent)] -pub struct PyDateTime(PyAny); -pyobject_native_type!( - PyDateTime, - crate::ffi::PyDateTime_DateTime, - |py| expect_datetime_api(py).DateTimeType, - #module=Some("datetime"), - #checkfunction=PyDateTime_Check -); -pyobject_subclassable_native_type!(PyDateTime, crate::ffi::PyDateTime_DateTime); - -impl PyDateTime { - /// Creates a new `datetime.datetime` object. - #[allow(clippy::too_many_arguments)] - pub fn new<'py>( - py: Python<'py>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.DateTime_FromDateAndTime)( - year, - c_int::from(month), - c_int::from(day), - c_int::from(hour), - c_int::from(minute), - c_int::from(second), - microsecond as c_int, - opt_to_pyobj(tzinfo), - api.DateTimeType, - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDateTime::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDateTime::new`")] - #[inline] - #[allow(clippy::too_many_arguments)] - pub fn new_bound<'py>( - py: Python<'py>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - Self::new( - py, - year, - month, - day, - hour, - minute, - second, - microsecond, - tzinfo, - ) - } - - /// Alternate constructor that takes a `fold` parameter. A `true` value for this parameter - /// signifies this this datetime is the later of two moments with the same representation, - /// during a repeated interval. - /// - /// This typically occurs at the end of daylight savings time. Only valid if the - /// represented time is ambiguous. - /// See [PEP 495](https://www.python.org/dev/peps/pep-0495/) for more detail. - #[allow(clippy::too_many_arguments)] - pub fn new_with_fold<'py>( - py: Python<'py>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - fold: bool, - ) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.DateTime_FromDateAndTimeAndFold)( - year, - c_int::from(month), - c_int::from(day), - c_int::from(hour), - c_int::from(minute), - c_int::from(second), - microsecond as c_int, - opt_to_pyobj(tzinfo), - c_int::from(fold), - api.DateTimeType, - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDateTime::new_with_fold`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDateTime::new_with_fold`")] - #[inline] - #[allow(clippy::too_many_arguments)] - pub fn new_bound_with_fold<'py>( - py: Python<'py>, - year: i32, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - fold: bool, - ) -> PyResult> { - Self::new_with_fold( - py, - year, - month, - day, - hour, - minute, - second, - microsecond, - tzinfo, - fold, - ) - } - - /// Construct a `datetime` object from a POSIX timestamp - /// - /// This is equivalent to `datetime.datetime.fromtimestamp` - pub fn from_timestamp<'py>( - py: Python<'py>, - timestamp: f64, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - let args = (timestamp, tzinfo).into_pyobject(py)?; - - // safety ensure API is loaded - let _api = ensure_datetime_api(py)?; - - unsafe { - PyDateTime_FromTimestamp(args.as_ptr()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDateTime::from_timestamp`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDateTime::from_timestamp`")] - #[inline] - pub fn from_timestamp_bound<'py>( - py: Python<'py>, - timestamp: f64, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - Self::from_timestamp(py, timestamp, tzinfo) - } -} - -impl PyDateAccess for Bound<'_, PyDateTime> { - fn get_year(&self) -> i32 { - unsafe { PyDateTime_GET_YEAR(self.as_ptr()) } - } - - fn get_month(&self) -> u8 { - unsafe { PyDateTime_GET_MONTH(self.as_ptr()) as u8 } - } - - fn get_day(&self) -> u8 { - unsafe { PyDateTime_GET_DAY(self.as_ptr()) as u8 } - } -} - -impl PyTimeAccess for Bound<'_, PyDateTime> { - fn get_hour(&self) -> u8 { - unsafe { PyDateTime_DATE_GET_HOUR(self.as_ptr()) as u8 } - } - - fn get_minute(&self) -> u8 { - unsafe { PyDateTime_DATE_GET_MINUTE(self.as_ptr()) as u8 } - } - - fn get_second(&self) -> u8 { - unsafe { PyDateTime_DATE_GET_SECOND(self.as_ptr()) as u8 } - } - - fn get_microsecond(&self) -> u32 { - unsafe { PyDateTime_DATE_GET_MICROSECOND(self.as_ptr()) as u32 } - } - - fn get_fold(&self) -> bool { - unsafe { PyDateTime_DATE_GET_FOLD(self.as_ptr()) > 0 } - } -} - -impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyDateTime> { - fn get_tzinfo(&self) -> Option> { - let ptr = self.as_ptr() as *mut ffi::PyDateTime_DateTime; - #[cfg(not(GraalPy))] - unsafe { - if (*ptr).hastzinfo != 0 { - Some( - (*ptr) - .tzinfo - .assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked(), - ) - } else { - None - } - } - - #[cfg(GraalPy)] - unsafe { - let res = PyDateTime_DATE_GET_TZINFO(ptr as *mut ffi::PyObject); - if Py_IsNone(res) == 1 { - None - } else { - Some( - res.assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked(), - ) - } - } - } -} - -/// Bindings for `datetime.time`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyTime>`][Bound]. -#[repr(transparent)] -pub struct PyTime(PyAny); -pyobject_native_type!( - PyTime, - crate::ffi::PyDateTime_Time, - |py| expect_datetime_api(py).TimeType, - #module=Some("datetime"), - #checkfunction=PyTime_Check -); -pyobject_subclassable_native_type!(PyTime, crate::ffi::PyDateTime_Time); - -impl PyTime { - /// Creates a new `datetime.time` object. - pub fn new<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.Time_FromTime)( - c_int::from(hour), - c_int::from(minute), - c_int::from(second), - microsecond as c_int, - opt_to_pyobj(tzinfo), - api.TimeType, - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyTime::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTime::new`")] - #[inline] - pub fn new_bound<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - ) -> PyResult> { - Self::new(py, hour, minute, second, microsecond, tzinfo) - } - - /// Alternate constructor that takes a `fold` argument. See [`PyDateTime::new_bound_with_fold`]. - pub fn new_with_fold<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - fold: bool, - ) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.Time_FromTimeAndFold)( - c_int::from(hour), - c_int::from(minute), - c_int::from(second), - microsecond as c_int, - opt_to_pyobj(tzinfo), - fold as c_int, - api.TimeType, - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyTime::new_with_fold`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTime::new_with_fold`")] - #[inline] - pub fn new_bound_with_fold<'py>( - py: Python<'py>, - hour: u8, - minute: u8, - second: u8, - microsecond: u32, - tzinfo: Option<&Bound<'py, PyTzInfo>>, - fold: bool, - ) -> PyResult> { - Self::new_with_fold(py, hour, minute, second, microsecond, tzinfo, fold) - } -} - -impl PyTimeAccess for Bound<'_, PyTime> { - fn get_hour(&self) -> u8 { - unsafe { PyDateTime_TIME_GET_HOUR(self.as_ptr()) as u8 } - } - - fn get_minute(&self) -> u8 { - unsafe { PyDateTime_TIME_GET_MINUTE(self.as_ptr()) as u8 } - } - - fn get_second(&self) -> u8 { - unsafe { PyDateTime_TIME_GET_SECOND(self.as_ptr()) as u8 } - } - - fn get_microsecond(&self) -> u32 { - unsafe { PyDateTime_TIME_GET_MICROSECOND(self.as_ptr()) as u32 } - } - - fn get_fold(&self) -> bool { - unsafe { PyDateTime_TIME_GET_FOLD(self.as_ptr()) != 0 } - } -} - -impl<'py> PyTzInfoAccess<'py> for Bound<'py, PyTime> { - fn get_tzinfo(&self) -> Option> { - let ptr = self.as_ptr() as *mut ffi::PyDateTime_Time; - #[cfg(not(GraalPy))] - unsafe { - if (*ptr).hastzinfo != 0 { - Some( - (*ptr) - .tzinfo - .assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked(), - ) - } else { - None - } - } - - #[cfg(GraalPy)] - unsafe { - let res = PyDateTime_TIME_GET_TZINFO(ptr as *mut ffi::PyObject); - if Py_IsNone(res) == 1 { - None - } else { - Some( - res.assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked(), - ) - } - } - } -} - -/// Bindings for `datetime.tzinfo`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyTzInfo>`][Bound]. -/// -/// This is an abstract base class and cannot be constructed directly. -/// For concrete time zone implementations, see [`timezone_utc_bound`] and -/// the [`zoneinfo` module](https://docs.python.org/3/library/zoneinfo.html). -#[repr(transparent)] -pub struct PyTzInfo(PyAny); -pyobject_native_type!( - PyTzInfo, - crate::ffi::PyObject, - |py| expect_datetime_api(py).TZInfoType, - #module=Some("datetime"), - #checkfunction=PyTZInfo_Check -); -pyobject_subclassable_native_type!(PyTzInfo, crate::ffi::PyObject); - -/// Equivalent to `datetime.timezone.utc` -pub fn timezone_utc(py: Python<'_>) -> Bound<'_, PyTzInfo> { - // TODO: this _could_ have a borrowed form `timezone_utc_borrowed`, but that seems - // like an edge case optimization and we'd prefer in PyO3 0.21 to use `Bound` as - // much as possible - unsafe { - expect_datetime_api(py) - .TimeZone_UTC - .assume_borrowed(py) - .to_owned() - .downcast_into_unchecked() - } -} - -/// Deprecated name for [`timezone_utc`]. -#[deprecated(since = "0.23.0", note = "renamed to `timezone_utc`")] -#[inline] -pub fn timezone_utc_bound(py: Python<'_>) -> Bound<'_, PyTzInfo> { - timezone_utc(py) -} - -/// Equivalent to `datetime.timezone` constructor -/// -/// Only used internally -#[cfg(feature = "chrono")] -pub(crate) fn timezone_from_offset<'py>( - offset: &Bound<'py, PyDelta>, -) -> PyResult> { - let py = offset.py(); - let api = ensure_datetime_api(py)?; - unsafe { - (api.TimeZone_FromTimeZone)(offset.as_ptr(), ptr::null_mut()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } -} - -/// Bindings for `datetime.timedelta`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyDelta>`][Bound]. -#[repr(transparent)] -pub struct PyDelta(PyAny); -pyobject_native_type!( - PyDelta, - crate::ffi::PyDateTime_Delta, - |py| expect_datetime_api(py).DeltaType, - #module=Some("datetime"), - #checkfunction=PyDelta_Check -); -pyobject_subclassable_native_type!(PyDelta, crate::ffi::PyDateTime_Delta); - -impl PyDelta { - /// Creates a new `timedelta`. - pub fn new( - py: Python<'_>, - days: i32, - seconds: i32, - microseconds: i32, - normalize: bool, - ) -> PyResult> { - let api = ensure_datetime_api(py)?; - unsafe { - (api.Delta_FromDelta)( - days as c_int, - seconds as c_int, - microseconds as c_int, - normalize as c_int, - api.DeltaType, - ) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyDelta::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDelta::new`")] - #[inline] - pub fn new_bound( - py: Python<'_>, - days: i32, - seconds: i32, - microseconds: i32, - normalize: bool, - ) -> PyResult> { - Self::new(py, days, seconds, microseconds, normalize) - } -} - -impl PyDeltaAccess for Bound<'_, PyDelta> { - fn get_days(&self) -> i32 { - unsafe { PyDateTime_DELTA_GET_DAYS(self.as_ptr()) } - } - - fn get_seconds(&self) -> i32 { - unsafe { PyDateTime_DELTA_GET_SECONDS(self.as_ptr()) } - } - - fn get_microseconds(&self) -> i32 { - unsafe { PyDateTime_DELTA_GET_MICROSECONDS(self.as_ptr()) } - } -} - -// Utility function which returns a borrowed reference to either -// the underlying tzinfo or None. -fn opt_to_pyobj(opt: Option<&Bound<'_, PyTzInfo>>) -> *mut ffi::PyObject { - match opt { - Some(tzi) => tzi.as_ptr(), - None => unsafe { ffi::Py_None() }, - } -} - -#[cfg(test)] -mod tests { - use super::*; - #[cfg(feature = "macros")] - use crate::py_run; - - #[test] - #[cfg(feature = "macros")] - #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons - fn test_datetime_fromtimestamp() { - Python::with_gil(|py| { - let dt = PyDateTime::from_timestamp(py, 100.0, None).unwrap(); - py_run!( - py, - dt, - "import datetime; assert dt == datetime.datetime.fromtimestamp(100)" - ); - - let dt = PyDateTime::from_timestamp(py, 100.0, Some(&timezone_utc(py))).unwrap(); - py_run!( - py, - dt, - "import datetime; assert dt == datetime.datetime.fromtimestamp(100, datetime.timezone.utc)" - ); - }) - } - - #[test] - #[cfg(feature = "macros")] - #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons - fn test_date_fromtimestamp() { - Python::with_gil(|py| { - let dt = PyDate::from_timestamp(py, 100).unwrap(); - py_run!( - py, - dt, - "import datetime; assert dt == datetime.date.fromtimestamp(100)" - ); - }) - } - - #[test] - #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons - fn test_new_with_fold() { - Python::with_gil(|py| { - let a = PyDateTime::new_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, false); - let b = PyDateTime::new_with_fold(py, 2021, 1, 23, 20, 32, 40, 341516, None, true); - - assert!(!a.unwrap().get_fold()); - assert!(b.unwrap().get_fold()); - }); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons - fn test_get_tzinfo() { - crate::Python::with_gil(|py| { - let utc = timezone_utc(py); - - let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, Some(&utc)).unwrap(); - - assert!(dt.get_tzinfo().unwrap().eq(&utc).unwrap()); - - let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, None).unwrap(); - - assert!(dt.get_tzinfo().is_none()); - - let t = PyTime::new(py, 0, 0, 0, 0, Some(&utc)).unwrap(); - - assert!(t.get_tzinfo().unwrap().eq(utc).unwrap()); - - let t = PyTime::new(py, 0, 0, 0, 0, None).unwrap(); - - assert!(t.get_tzinfo().is_none()); - }); - } - - #[test] - #[cfg(all(feature = "macros", feature = "chrono"))] - #[cfg_attr(target_arch = "wasm32", ignore)] // DateTime import fails on wasm for mysterious reasons - fn test_timezone_from_offset() { - use crate::types::PyNone; - - Python::with_gil(|py| { - assert!( - timezone_from_offset(&PyDelta::new(py, 0, -3600, 0, true).unwrap()) - .unwrap() - .call_method1("utcoffset", (PyNone::get(py),)) - .unwrap() - .downcast_into::() - .unwrap() - .eq(PyDelta::new(py, 0, -3600, 0, true).unwrap()) - .unwrap() - ); - - assert!( - timezone_from_offset(&PyDelta::new(py, 0, 3600, 0, true).unwrap()) - .unwrap() - .call_method1("utcoffset", (PyNone::get(py),)) - .unwrap() - .downcast_into::() - .unwrap() - .eq(PyDelta::new(py, 0, 3600, 0, true).unwrap()) - .unwrap() - ); - - timezone_from_offset(&PyDelta::new(py, 1, 0, 0, true).unwrap()).unwrap_err(); - }) - } -} diff --git a/vendor/pyo3/src/types/dict.rs b/vendor/pyo3/src/types/dict.rs deleted file mode 100644 index 0d2e6ff335..0000000000 --- a/vendor/pyo3/src/types/dict.rs +++ /dev/null @@ -1,1660 +0,0 @@ -use crate::err::{self, PyErr, PyResult}; -use crate::ffi::Py_ssize_t; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::{Borrowed, Bound}; -use crate::py_result_ext::PyResultExt; -use crate::types::{PyAny, PyAnyMethods, PyList, PyMapping}; -use crate::{ffi, BoundObject, IntoPyObject, IntoPyObjectExt, Python}; - -/// Represents a Python `dict`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyDict>`][Bound]. -/// -/// For APIs available on `dict` objects, see the [`PyDictMethods`] trait which is implemented for -/// [`Bound<'py, PyDict>`][Bound]. -#[repr(transparent)] -pub struct PyDict(PyAny); - -pyobject_subclassable_native_type!(PyDict, crate::ffi::PyDictObject); - -pyobject_native_type!( - PyDict, - ffi::PyDictObject, - pyobject_native_static_type_object!(ffi::PyDict_Type), - #checkfunction=ffi::PyDict_Check -); - -/// Represents a Python `dict_keys`. -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(transparent)] -pub struct PyDictKeys(PyAny); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_native_type_core!( - PyDictKeys, - pyobject_native_static_type_object!(ffi::PyDictKeys_Type), - #checkfunction=ffi::PyDictKeys_Check -); - -/// Represents a Python `dict_values`. -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(transparent)] -pub struct PyDictValues(PyAny); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_native_type_core!( - PyDictValues, - pyobject_native_static_type_object!(ffi::PyDictValues_Type), - #checkfunction=ffi::PyDictValues_Check -); - -/// Represents a Python `dict_items`. -#[cfg(not(any(PyPy, GraalPy)))] -#[repr(transparent)] -pub struct PyDictItems(PyAny); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_native_type_core!( - PyDictItems, - pyobject_native_static_type_object!(ffi::PyDictItems_Type), - #checkfunction=ffi::PyDictItems_Check -); - -impl PyDict { - /// Creates a new empty dictionary. - pub fn new(py: Python<'_>) -> Bound<'_, PyDict> { - unsafe { ffi::PyDict_New().assume_owned(py).downcast_into_unchecked() } - } - - /// Deprecated name for [`PyDict::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyDict::new`")] - #[inline] - pub fn new_bound(py: Python<'_>) -> Bound<'_, PyDict> { - Self::new(py) - } - - /// Creates a new dictionary from the sequence given. - /// - /// The sequence must consist of `(PyObject, PyObject)`. This is - /// equivalent to `dict([("a", 1), ("b", 2)])`. - /// - /// Returns an error on invalid input. In the case of key collisions, - /// this keeps the last entry seen. - #[cfg(not(any(PyPy, GraalPy)))] - pub fn from_sequence<'py>(seq: &Bound<'py, PyAny>) -> PyResult> { - let py = seq.py(); - let dict = Self::new(py); - err::error_on_minusone(py, unsafe { - ffi::PyDict_MergeFromSeq2(dict.as_ptr(), seq.as_ptr(), 1) - })?; - Ok(dict) - } - - /// Deprecated name for [`PyDict::from_sequence`]. - #[cfg(not(any(PyPy, GraalPy)))] - #[deprecated(since = "0.23.0", note = "renamed to `PyDict::from_sequence`")] - #[inline] - pub fn from_sequence_bound<'py>(seq: &Bound<'py, PyAny>) -> PyResult> { - Self::from_sequence(seq) - } -} - -/// Implementation of functionality for [`PyDict`]. -/// -/// These methods are defined for the `Bound<'py, PyDict>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyDict")] -pub trait PyDictMethods<'py>: crate::sealed::Sealed { - /// Returns a new dictionary that contains the same key-value pairs as self. - /// - /// This is equivalent to the Python expression `self.copy()`. - fn copy(&self) -> PyResult>; - - /// Empties an existing dictionary of all key-value pairs. - fn clear(&self); - - /// Return the number of items in the dictionary. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> usize; - - /// Checks if the dict is empty, i.e. `len(self) == 0`. - fn is_empty(&self) -> bool; - - /// Determines if the dictionary contains the specified key. - /// - /// This is equivalent to the Python expression `key in self`. - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>; - - /// Gets an item from the dictionary. - /// - /// Returns `None` if the item is not present, or if an error occurs. - /// - /// To get a `KeyError` for non-existing keys, use `PyAny::get_item`. - fn get_item(&self, key: K) -> PyResult>> - where - K: IntoPyObject<'py>; - - /// Sets an item value. - /// - /// This is equivalent to the Python statement `self[key] = value`. - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>; - - /// Deletes an item. - /// - /// This is equivalent to the Python statement `del self[key]`. - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>; - - /// Returns a list of dict keys. - /// - /// This is equivalent to the Python expression `list(dict.keys())`. - fn keys(&self) -> Bound<'py, PyList>; - - /// Returns a list of dict values. - /// - /// This is equivalent to the Python expression `list(dict.values())`. - fn values(&self) -> Bound<'py, PyList>; - - /// Returns a list of dict items. - /// - /// This is equivalent to the Python expression `list(dict.items())`. - fn items(&self) -> Bound<'py, PyList>; - - /// Returns an iterator of `(key, value)` pairs in this dictionary. - /// - /// # Panics - /// - /// If PyO3 detects that the dictionary is mutated during iteration, it will panic. - /// It is allowed to modify values as you iterate over the dictionary, but only - /// so long as the set of keys does not change. - fn iter(&self) -> BoundDictIterator<'py>; - - /// Iterates over the contents of this dictionary while holding a critical section on the dict. - /// This is useful when the GIL is disabled and the dictionary is shared between threads. - /// It is not guaranteed that the dictionary will not be modified during iteration when the - /// closure calls arbitrary Python code that releases the critical section held by the - /// iterator. Otherwise, the dictionary will not be modified during iteration. - /// - /// This method is a small performance optimization over `.iter().try_for_each()` when the - /// nightly feature is not enabled because we cannot implement an optimised version of - /// `iter().try_fold()` on stable yet. If your iteration is infallible then this method has the - /// same performance as `.iter().for_each()`. - fn locked_for_each(&self, closure: F) -> PyResult<()> - where - F: Fn(Bound<'py, PyAny>, Bound<'py, PyAny>) -> PyResult<()>; - - /// Returns `self` cast as a `PyMapping`. - fn as_mapping(&self) -> &Bound<'py, PyMapping>; - - /// Returns `self` cast as a `PyMapping`. - fn into_mapping(self) -> Bound<'py, PyMapping>; - - /// Update this dictionary with the key/value pairs from another. - /// - /// This is equivalent to the Python expression `self.update(other)`. If `other` is a `PyDict`, you may want - /// to use `self.update(other.as_mapping())`, note: `PyDict::as_mapping` is a zero-cost conversion. - fn update(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>; - - /// Add key/value pairs from another dictionary to this one only when they do not exist in this. - /// - /// This is equivalent to the Python expression `self.update({k: v for k, v in other.items() if k not in self})`. - /// If `other` is a `PyDict`, you may want to use `self.update_if_missing(other.as_mapping())`, - /// note: `PyDict::as_mapping` is a zero-cost conversion. - /// - /// This method uses [`PyDict_Merge`](https://docs.python.org/3/c-api/dict.html#c.PyDict_Merge) internally, - /// so should have the same performance as `update`. - fn update_if_missing(&self, other: &Bound<'_, PyMapping>) -> PyResult<()>; -} - -impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> { - fn copy(&self) -> PyResult> { - unsafe { - ffi::PyDict_Copy(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - fn clear(&self) { - unsafe { ffi::PyDict_Clear(self.as_ptr()) } - } - - fn len(&self) -> usize { - dict_len(self) as usize - } - - fn is_empty(&self) -> bool { - self.len() == 0 - } - - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>, - { - fn inner(dict: &Bound<'_, PyDict>, key: Borrowed<'_, '_, PyAny>) -> PyResult { - match unsafe { ffi::PyDict_Contains(dict.as_ptr(), key.as_ptr()) } { - 1 => Ok(true), - 0 => Ok(false), - _ => Err(PyErr::fetch(dict.py())), - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn get_item(&self, key: K) -> PyResult>> - where - K: IntoPyObject<'py>, - { - fn inner<'py>( - dict: &Bound<'py, PyDict>, - key: Borrowed<'_, '_, PyAny>, - ) -> PyResult>> { - let py = dict.py(); - let mut result: *mut ffi::PyObject = std::ptr::null_mut(); - match unsafe { - ffi::compat::PyDict_GetItemRef(dict.as_ptr(), key.as_ptr(), &mut result) - } { - std::os::raw::c_int::MIN..=-1 => Err(PyErr::fetch(py)), - 0 => Ok(None), - 1..=std::os::raw::c_int::MAX => { - // Safety: PyDict_GetItemRef positive return value means the result is a valid - // owned reference - Ok(Some(unsafe { result.assume_owned_unchecked(py) })) - } - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>, - { - fn inner( - dict: &Bound<'_, PyDict>, - key: Borrowed<'_, '_, PyAny>, - value: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - err::error_on_minusone(dict.py(), unsafe { - ffi::PyDict_SetItem(dict.as_ptr(), key.as_ptr(), value.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>, - { - fn inner(dict: &Bound<'_, PyDict>, key: Borrowed<'_, '_, PyAny>) -> PyResult<()> { - err::error_on_minusone(dict.py(), unsafe { - ffi::PyDict_DelItem(dict.as_ptr(), key.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn keys(&self) -> Bound<'py, PyList> { - unsafe { - ffi::PyDict_Keys(self.as_ptr()) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } - - fn values(&self) -> Bound<'py, PyList> { - unsafe { - ffi::PyDict_Values(self.as_ptr()) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } - - fn items(&self) -> Bound<'py, PyList> { - unsafe { - ffi::PyDict_Items(self.as_ptr()) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } - - fn iter(&self) -> BoundDictIterator<'py> { - BoundDictIterator::new(self.clone()) - } - - fn locked_for_each(&self, f: F) -> PyResult<()> - where - F: Fn(Bound<'py, PyAny>, Bound<'py, PyAny>) -> PyResult<()>, - { - #[cfg(feature = "nightly")] - { - // We don't need a critical section when the nightly feature is enabled because - // try_for_each is locked by the implementation of try_fold. - self.iter().try_for_each(|(key, value)| f(key, value)) - } - - #[cfg(not(feature = "nightly"))] - { - crate::sync::with_critical_section(self, || { - self.iter().try_for_each(|(key, value)| f(key, value)) - }) - } - } - - fn as_mapping(&self) -> &Bound<'py, PyMapping> { - unsafe { self.downcast_unchecked() } - } - - fn into_mapping(self) -> Bound<'py, PyMapping> { - unsafe { self.into_any().downcast_into_unchecked() } - } - - fn update(&self, other: &Bound<'_, PyMapping>) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PyDict_Update(self.as_ptr(), other.as_ptr()) - }) - } - - fn update_if_missing(&self, other: &Bound<'_, PyMapping>) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PyDict_Merge(self.as_ptr(), other.as_ptr(), 0) - }) - } -} - -impl<'a, 'py> Borrowed<'a, 'py, PyDict> { - /// Iterates over the contents of this dictionary without incrementing reference counts. - /// - /// # Safety - /// It must be known that this dictionary will not be modified during iteration, - /// for example, when parsing arguments in a keyword arguments dictionary. - pub(crate) unsafe fn iter_borrowed(self) -> BorrowedDictIter<'a, 'py> { - BorrowedDictIter::new(self) - } -} - -fn dict_len(dict: &Bound<'_, PyDict>) -> Py_ssize_t { - #[cfg(any(not(Py_3_8), PyPy, GraalPy, Py_LIMITED_API, Py_GIL_DISABLED))] - unsafe { - ffi::PyDict_Size(dict.as_ptr()) - } - - #[cfg(all( - Py_3_8, - not(PyPy), - not(GraalPy), - not(Py_LIMITED_API), - not(Py_GIL_DISABLED) - ))] - unsafe { - (*dict.as_ptr().cast::()).ma_used - } -} - -/// PyO3 implementation of an iterator for a Python `dict` object. -pub struct BoundDictIterator<'py> { - dict: Bound<'py, PyDict>, - inner: DictIterImpl, -} - -enum DictIterImpl { - DictIter { - ppos: ffi::Py_ssize_t, - di_used: ffi::Py_ssize_t, - remaining: ffi::Py_ssize_t, - }, -} - -impl DictIterImpl { - #[deny(unsafe_op_in_unsafe_fn)] - #[inline] - /// Safety: the dict should be locked with a critical section on the free-threaded build - /// and otherwise not shared between threads in code that releases the GIL. - unsafe fn next_unchecked<'py>( - &mut self, - dict: &Bound<'py, PyDict>, - ) -> Option<(Bound<'py, PyAny>, Bound<'py, PyAny>)> { - match self { - Self::DictIter { - di_used, - remaining, - ppos, - .. - } => { - let ma_used = dict_len(dict); - - // These checks are similar to what CPython does. - // - // If the dimension of the dict changes e.g. key-value pairs are removed - // or added during iteration, this will panic next time when `next` is called - if *di_used != ma_used { - *di_used = -1; - panic!("dictionary changed size during iteration"); - }; - - // If the dict is changed in such a way that the length remains constant - // then this will panic at the end of iteration - similar to this: - // - // d = {"a":1, "b":2, "c": 3} - // - // for k, v in d.items(): - // d[f"{k}_"] = 4 - // del d[k] - // print(k) - // - if *remaining == -1 { - *di_used = -1; - panic!("dictionary keys changed during iteration"); - }; - - let mut key: *mut ffi::PyObject = std::ptr::null_mut(); - let mut value: *mut ffi::PyObject = std::ptr::null_mut(); - - if unsafe { ffi::PyDict_Next(dict.as_ptr(), ppos, &mut key, &mut value) != 0 } { - *remaining -= 1; - let py = dict.py(); - // Safety: - // - PyDict_Next returns borrowed values - // - we have already checked that `PyDict_Next` succeeded, so we can assume these to be non-null - Some(( - unsafe { key.assume_borrowed_unchecked(py).to_owned() }, - unsafe { value.assume_borrowed_unchecked(py).to_owned() }, - )) - } else { - None - } - } - } - } - - #[cfg(Py_GIL_DISABLED)] - #[inline] - fn with_critical_section(&mut self, dict: &Bound<'_, PyDict>, f: F) -> R - where - F: FnOnce(&mut Self) -> R, - { - match self { - Self::DictIter { .. } => crate::sync::with_critical_section(dict, || f(self)), - } - } -} - -impl<'py> Iterator for BoundDictIterator<'py> { - type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>); - - #[inline] - fn next(&mut self) -> Option { - #[cfg(Py_GIL_DISABLED)] - { - self.inner - .with_critical_section(&self.dict, |inner| unsafe { - inner.next_unchecked(&self.dict) - }) - } - #[cfg(not(Py_GIL_DISABLED))] - { - unsafe { self.inner.next_unchecked(&self.dict) } - } - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } - - #[inline] - #[cfg(Py_GIL_DISABLED)] - fn fold(mut self, init: B, mut f: F) -> B - where - Self: Sized, - F: FnMut(B, Self::Item) -> B, - { - self.inner.with_critical_section(&self.dict, |inner| { - let mut accum = init; - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - accum = f(accum, x); - } - accum - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, feature = "nightly"))] - fn try_fold(&mut self, init: B, mut f: F) -> R - where - Self: Sized, - F: FnMut(B, Self::Item) -> R, - R: std::ops::Try, - { - self.inner.with_critical_section(&self.dict, |inner| { - let mut accum = init; - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - accum = f(accum, x)? - } - R::from_output(accum) - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn all(&mut self, mut f: F) -> bool - where - Self: Sized, - F: FnMut(Self::Item) -> bool, - { - self.inner.with_critical_section(&self.dict, |inner| { - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - if !f(x) { - return false; - } - } - true - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn any(&mut self, mut f: F) -> bool - where - Self: Sized, - F: FnMut(Self::Item) -> bool, - { - self.inner.with_critical_section(&self.dict, |inner| { - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - if f(x) { - return true; - } - } - false - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn find

(&mut self, mut predicate: P) -> Option - where - Self: Sized, - P: FnMut(&Self::Item) -> bool, - { - self.inner.with_critical_section(&self.dict, |inner| { - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - if predicate(&x) { - return Some(x); - } - } - None - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn find_map(&mut self, mut f: F) -> Option - where - Self: Sized, - F: FnMut(Self::Item) -> Option, - { - self.inner.with_critical_section(&self.dict, |inner| { - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - if let found @ Some(_) = f(x) { - return found; - } - } - None - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn position

(&mut self, mut predicate: P) -> Option - where - Self: Sized, - P: FnMut(Self::Item) -> bool, - { - self.inner.with_critical_section(&self.dict, |inner| { - let mut acc = 0; - while let Some(x) = unsafe { inner.next_unchecked(&self.dict) } { - if predicate(x) { - return Some(acc); - } - acc += 1; - } - None - }) - } -} - -impl ExactSizeIterator for BoundDictIterator<'_> { - fn len(&self) -> usize { - match self.inner { - DictIterImpl::DictIter { remaining, .. } => remaining as usize, - } - } -} - -impl<'py> BoundDictIterator<'py> { - fn new(dict: Bound<'py, PyDict>) -> Self { - let remaining = dict_len(&dict); - - Self { - dict, - inner: DictIterImpl::DictIter { - ppos: 0, - di_used: remaining, - remaining, - }, - } - } -} - -impl<'py> IntoIterator for Bound<'py, PyDict> { - type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>); - type IntoIter = BoundDictIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - BoundDictIterator::new(self) - } -} - -impl<'py> IntoIterator for &Bound<'py, PyDict> { - type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>); - type IntoIter = BoundDictIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -mod borrowed_iter { - use super::*; - - /// Variant of the above which is used to iterate the items of the dictionary - /// without incrementing reference counts. This is only safe if it's known - /// that the dictionary will not be modified during iteration. - pub struct BorrowedDictIter<'a, 'py> { - dict: Borrowed<'a, 'py, PyDict>, - ppos: ffi::Py_ssize_t, - len: ffi::Py_ssize_t, - } - - impl<'a, 'py> Iterator for BorrowedDictIter<'a, 'py> { - type Item = (Borrowed<'a, 'py, PyAny>, Borrowed<'a, 'py, PyAny>); - - #[inline] - fn next(&mut self) -> Option { - let mut key: *mut ffi::PyObject = std::ptr::null_mut(); - let mut value: *mut ffi::PyObject = std::ptr::null_mut(); - - // Safety: self.dict lives sufficiently long that the pointer is not dangling - if unsafe { ffi::PyDict_Next(self.dict.as_ptr(), &mut self.ppos, &mut key, &mut value) } - != 0 - { - let py = self.dict.py(); - self.len -= 1; - // Safety: - // - PyDict_Next returns borrowed values - // - we have already checked that `PyDict_Next` succeeded, so we can assume these to be non-null - Some(unsafe { (key.assume_borrowed(py), value.assume_borrowed(py)) }) - } else { - None - } - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } - } - - impl ExactSizeIterator for BorrowedDictIter<'_, '_> { - fn len(&self) -> usize { - self.len as usize - } - } - - impl<'a, 'py> BorrowedDictIter<'a, 'py> { - pub(super) fn new(dict: Borrowed<'a, 'py, PyDict>) -> Self { - let len = dict_len(&dict); - BorrowedDictIter { dict, ppos: 0, len } - } - } -} - -pub(crate) use borrowed_iter::BorrowedDictIter; - -/// Conversion trait that allows a sequence of tuples to be converted into `PyDict` -/// Primary use case for this trait is `call` and `call_method` methods as keywords argument. -pub trait IntoPyDict<'py>: Sized { - /// Converts self into a `PyDict` object pointer. Whether pointer owned or borrowed - /// depends on implementation. - fn into_py_dict(self, py: Python<'py>) -> PyResult>; - - /// Deprecated name for [`IntoPyDict::into_py_dict`]. - #[deprecated(since = "0.23.0", note = "renamed to `IntoPyDict::into_py_dict`")] - #[inline] - fn into_py_dict_bound(self, py: Python<'py>) -> Bound<'py, PyDict> { - self.into_py_dict(py).unwrap() - } -} - -impl<'py, T, I> IntoPyDict<'py> for I -where - T: PyDictItem<'py>, - I: IntoIterator, -{ - fn into_py_dict(self, py: Python<'py>) -> PyResult> { - let dict = PyDict::new(py); - self.into_iter().try_for_each(|item| { - let (key, value) = item.unpack(); - dict.set_item(key, value) - })?; - Ok(dict) - } -} - -/// Represents a tuple which can be used as a PyDict item. -trait PyDictItem<'py> { - type K: IntoPyObject<'py>; - type V: IntoPyObject<'py>; - fn unpack(self) -> (Self::K, Self::V); -} - -impl<'py, K, V> PyDictItem<'py> for (K, V) -where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>, -{ - type K = K; - type V = V; - - fn unpack(self) -> (Self::K, Self::V) { - (self.0, self.1) - } -} - -impl<'a, 'py, K, V> PyDictItem<'py> for &'a (K, V) -where - &'a K: IntoPyObject<'py>, - &'a V: IntoPyObject<'py>, -{ - type K = &'a K; - type V = &'a V; - - fn unpack(self) -> (Self::K, Self::V) { - (&self.0, &self.1) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::types::PyTuple; - use std::collections::{BTreeMap, HashMap}; - - #[test] - fn test_new() { - Python::with_gil(|py| { - let dict = [(7, 32)].into_py_dict(py).unwrap(); - assert_eq!( - 32, - dict.get_item(7i32) - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - assert!(dict.get_item(8i32).unwrap().is_none()); - let map: HashMap = [(7, 32)].iter().cloned().collect(); - assert_eq!(map, dict.extract().unwrap()); - let map: BTreeMap = [(7, 32)].iter().cloned().collect(); - assert_eq!(map, dict.extract().unwrap()); - }); - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn test_from_sequence() { - Python::with_gil(|py| { - let items = PyList::new(py, vec![("a", 1), ("b", 2)]).unwrap(); - let dict = PyDict::from_sequence(&items).unwrap(); - assert_eq!( - 1, - dict.get_item("a") - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - assert_eq!( - 2, - dict.get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - let map: HashMap = - [("a".into(), 1), ("b".into(), 2)].into_iter().collect(); - assert_eq!(map, dict.extract().unwrap()); - let map: BTreeMap = - [("a".into(), 1), ("b".into(), 2)].into_iter().collect(); - assert_eq!(map, dict.extract().unwrap()); - }); - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn test_from_sequence_err() { - Python::with_gil(|py| { - let items = PyList::new(py, vec!["a", "b"]).unwrap(); - assert!(PyDict::from_sequence(&items).is_err()); - }); - } - - #[test] - fn test_copy() { - Python::with_gil(|py| { - let dict = [(7, 32)].into_py_dict(py).unwrap(); - - let ndict = dict.copy().unwrap(); - assert_eq!( - 32, - ndict - .get_item(7i32) - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - assert!(ndict.get_item(8i32).unwrap().is_none()); - }); - } - - #[test] - fn test_len() { - Python::with_gil(|py| { - let mut v = HashMap::::new(); - let dict = (&v).into_pyobject(py).unwrap(); - assert_eq!(0, dict.len()); - v.insert(7, 32); - let dict2 = v.into_pyobject(py).unwrap(); - assert_eq!(1, dict2.len()); - }); - } - - #[test] - fn test_contains() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.into_pyobject(py).unwrap(); - assert!(dict.contains(7i32).unwrap()); - assert!(!dict.contains(8i32).unwrap()); - }); - } - - #[test] - fn test_get_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.into_pyobject(py).unwrap(); - assert_eq!( - 32, - dict.get_item(7i32) - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - assert!(dict.get_item(8i32).unwrap().is_none()); - }); - } - - #[cfg(feature = "macros")] - #[test] - fn test_get_item_error_path() { - use crate::exceptions::PyTypeError; - - #[crate::pyclass(crate = "crate")] - struct HashErrors; - - #[crate::pymethods(crate = "crate")] - impl HashErrors { - #[new] - fn new() -> Self { - HashErrors {} - } - - fn __hash__(&self) -> PyResult { - Err(PyTypeError::new_err("Error from __hash__")) - } - } - - Python::with_gil(|py| { - let class = py.get_type::(); - let instance = class.call0().unwrap(); - let d = PyDict::new(py); - match d.get_item(instance) { - Ok(_) => { - panic!("this get_item call should always error") - } - Err(err) => { - assert!(err.is_instance_of::(py)); - assert_eq!(err.value(py).to_string(), "Error from __hash__") - } - } - }) - } - - #[test] - fn test_set_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.into_pyobject(py).unwrap(); - assert!(dict.set_item(7i32, 42i32).is_ok()); // change - assert!(dict.set_item(8i32, 123i32).is_ok()); // insert - assert_eq!( - 42i32, - dict.get_item(7i32) - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - assert_eq!( - 123i32, - dict.get_item(8i32) - .unwrap() - .unwrap() - .extract::() - .unwrap() - ); - }); - } - - #[test] - fn test_set_item_refcnt() { - Python::with_gil(|py| { - let cnt; - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - { - cnt = obj.get_refcnt(); - let _dict = [(10, &obj)].into_py_dict(py); - } - { - assert_eq!(cnt, obj.get_refcnt()); - } - }); - } - - #[test] - fn test_set_item_does_not_update_original_object() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = (&v).into_pyobject(py).unwrap(); - assert!(dict.set_item(7i32, 42i32).is_ok()); // change - assert!(dict.set_item(8i32, 123i32).is_ok()); // insert - assert_eq!(32i32, v[&7i32]); // not updated! - assert_eq!(None, v.get(&8i32)); - }); - } - - #[test] - fn test_del_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.into_pyobject(py).unwrap(); - assert!(dict.del_item(7i32).is_ok()); - assert_eq!(0, dict.len()); - assert!(dict.get_item(7i32).unwrap().is_none()); - }); - } - - #[test] - fn test_del_item_does_not_update_original_object() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = (&v).into_pyobject(py).unwrap(); - assert!(dict.del_item(7i32).is_ok()); // change - assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated! - }); - } - - #[test] - fn test_items() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - let mut value_sum = 0; - for el in dict.items() { - let tuple = el.downcast::().unwrap(); - key_sum += tuple.get_item(0).unwrap().extract::().unwrap(); - value_sum += tuple.get_item(1).unwrap().extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_keys() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - for el in dict.keys() { - key_sum += el.extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - }); - } - - #[test] - fn test_values() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut values_sum = 0; - for el in dict.values() { - values_sum += el.extract::().unwrap(); - } - assert_eq!(32 + 42 + 123, values_sum); - }); - } - - #[test] - fn test_iter() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - let mut key_sum = 0; - let mut value_sum = 0; - for (key, value) in dict { - key_sum += key.extract::().unwrap(); - value_sum += value.extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_iter_bound() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - let mut key_sum = 0; - let mut value_sum = 0; - for (key, value) in dict { - key_sum += key.extract::().unwrap(); - value_sum += value.extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_iter_value_mutated() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - - let dict = (&v).into_pyobject(py).unwrap(); - - for (key, value) in &dict { - dict.set_item(key, value.extract::().unwrap() + 7) - .unwrap(); - } - }); - } - - #[test] - #[should_panic] - fn test_iter_key_mutated() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - for i in 0..10 { - v.insert(i * 2, i * 2); - } - let dict = v.into_pyobject(py).unwrap(); - - for (i, (key, value)) in dict.iter().enumerate() { - let key = key.extract::().unwrap(); - let value = value.extract::().unwrap(); - - dict.set_item(key + 1, value + 1).unwrap(); - - if i > 1000 { - // avoid this test just running out of memory if it fails - break; - }; - } - }); - } - - #[test] - #[should_panic] - fn test_iter_key_mutated_constant_len() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - for i in 0..10 { - v.insert(i * 2, i * 2); - } - let dict = v.into_pyobject(py).unwrap(); - - for (i, (key, value)) in dict.iter().enumerate() { - let key = key.extract::().unwrap(); - let value = value.extract::().unwrap(); - dict.del_item(key).unwrap(); - dict.set_item(key + 1, value + 1).unwrap(); - - if i > 1000 { - // avoid this test just running out of memory if it fails - break; - }; - } - }); - } - - #[test] - fn test_iter_size_hint() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = (&v).into_pyobject(py).unwrap(); - - let mut iter = dict.iter(); - assert_eq!(iter.size_hint(), (v.len(), Some(v.len()))); - iter.next(); - assert_eq!(iter.size_hint(), (v.len() - 1, Some(v.len() - 1))); - - // Exhaust iterator. - for _ in &mut iter {} - - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - - assert_eq!(iter.size_hint(), (0, Some(0))); - }); - } - - #[test] - fn test_into_iter() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_pyobject(py).unwrap(); - let mut key_sum = 0; - let mut value_sum = 0; - for (key, value) in dict { - key_sum += key.extract::().unwrap(); - value_sum += value.extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_hashmap_into_dict() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 1); - assert_eq!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[test] - fn test_btreemap_into_dict() { - Python::with_gil(|py| { - let mut map = BTreeMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 1); - assert_eq!( - py_map - .get_item(1) - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[test] - fn test_vec_into_dict() { - Python::with_gil(|py| { - let vec = vec![("a", 1), ("b", 2), ("c", 3)]; - let py_map = vec.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 3); - assert_eq!( - py_map - .get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - }); - } - - #[test] - fn test_slice_into_dict() { - Python::with_gil(|py| { - let arr = [("a", 1), ("b", 2), ("c", 3)]; - let py_map = arr.into_py_dict(py).unwrap(); - - assert_eq!(py_map.len(), 3); - assert_eq!( - py_map - .get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - }); - } - - #[test] - fn dict_as_mapping() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - assert_eq!(py_map.as_mapping().len().unwrap(), 1); - assert_eq!( - py_map - .as_mapping() - .get_item(1) - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[test] - fn dict_into_mapping() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let py_map = map.into_py_dict(py).unwrap(); - - let py_mapping = py_map.into_mapping(); - assert_eq!(py_mapping.len().unwrap(), 1); - assert_eq!(py_mapping.get_item(1).unwrap().extract::().unwrap(), 1); - }); - } - - #[cfg(not(any(PyPy, GraalPy)))] - fn abc_dict(py: Python<'_>) -> Bound<'_, PyDict> { - let mut map = HashMap::<&'static str, i32>::new(); - map.insert("a", 1); - map.insert("b", 2); - map.insert("c", 3); - map.into_py_dict(py).unwrap() - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn dict_keys_view() { - Python::with_gil(|py| { - let dict = abc_dict(py); - let keys = dict.call_method0("keys").unwrap(); - assert!(keys.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn dict_values_view() { - Python::with_gil(|py| { - let dict = abc_dict(py); - let values = dict.call_method0("values").unwrap(); - assert!(values.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn dict_items_view() { - Python::with_gil(|py| { - let dict = abc_dict(py); - let items = dict.call_method0("items").unwrap(); - assert!(items.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - fn dict_update() { - Python::with_gil(|py| { - let dict = [("a", 1), ("b", 2), ("c", 3)].into_py_dict(py).unwrap(); - let other = [("b", 4), ("c", 5), ("d", 6)].into_py_dict(py).unwrap(); - dict.update(other.as_mapping()).unwrap(); - assert_eq!(dict.len(), 4); - assert_eq!( - dict.get_item("a") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - assert_eq!( - dict.get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 4 - ); - assert_eq!( - dict.get_item("c") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 5 - ); - assert_eq!( - dict.get_item("d") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 6 - ); - - assert_eq!(other.len(), 3); - assert_eq!( - other - .get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 4 - ); - assert_eq!( - other - .get_item("c") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 5 - ); - assert_eq!( - other - .get_item("d") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 6 - ); - }) - } - - #[test] - fn dict_update_if_missing() { - Python::with_gil(|py| { - let dict = [("a", 1), ("b", 2), ("c", 3)].into_py_dict(py).unwrap(); - let other = [("b", 4), ("c", 5), ("d", 6)].into_py_dict(py).unwrap(); - dict.update_if_missing(other.as_mapping()).unwrap(); - assert_eq!(dict.len(), 4); - assert_eq!( - dict.get_item("a") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - assert_eq!( - dict.get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - assert_eq!( - dict.get_item("c") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 3 - ); - assert_eq!( - dict.get_item("d") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 6 - ); - - assert_eq!(other.len(), 3); - assert_eq!( - other - .get_item("b") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 4 - ); - assert_eq!( - other - .get_item("c") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 5 - ); - assert_eq!( - other - .get_item("d") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 6 - ); - }) - } - - #[test] - fn test_iter_all() { - Python::with_gil(|py| { - let dict = [(1, true), (2, true), (3, true)].into_py_dict(py).unwrap(); - assert!(dict.iter().all(|(_, v)| v.extract::().unwrap())); - - let dict = [(1, true), (2, false), (3, true)].into_py_dict(py).unwrap(); - assert!(!dict.iter().all(|(_, v)| v.extract::().unwrap())); - }); - } - - #[test] - fn test_iter_any() { - Python::with_gil(|py| { - let dict = [(1, true), (2, false), (3, false)] - .into_py_dict(py) - .unwrap(); - assert!(dict.iter().any(|(_, v)| v.extract::().unwrap())); - - let dict = [(1, false), (2, false), (3, false)] - .into_py_dict(py) - .unwrap(); - assert!(!dict.iter().any(|(_, v)| v.extract::().unwrap())); - }); - } - - #[test] - #[allow(clippy::search_is_some)] - fn test_iter_find() { - Python::with_gil(|py| { - let dict = [(1, false), (2, true), (3, false)] - .into_py_dict(py) - .unwrap(); - - assert_eq!( - Some((2, true)), - dict.iter() - .find(|(_, v)| v.extract::().unwrap()) - .map(|(k, v)| (k.extract().unwrap(), v.extract().unwrap())) - ); - - let dict = [(1, false), (2, false), (3, false)] - .into_py_dict(py) - .unwrap(); - - assert!(dict - .iter() - .find(|(_, v)| v.extract::().unwrap()) - .is_none()); - }); - } - - #[test] - #[allow(clippy::search_is_some)] - fn test_iter_position() { - Python::with_gil(|py| { - let dict = [(1, false), (2, false), (3, true)] - .into_py_dict(py) - .unwrap(); - assert_eq!( - Some(2), - dict.iter().position(|(_, v)| v.extract::().unwrap()) - ); - - let dict = [(1, false), (2, false), (3, false)] - .into_py_dict(py) - .unwrap(); - assert!(dict - .iter() - .position(|(_, v)| v.extract::().unwrap()) - .is_none()); - }); - } - - #[test] - fn test_iter_fold() { - Python::with_gil(|py| { - let dict = [(1, 1), (2, 2), (3, 3)].into_py_dict(py).unwrap(); - let sum = dict - .iter() - .fold(0, |acc, (_, v)| acc + v.extract::().unwrap()); - assert_eq!(sum, 6); - }); - } - - #[test] - fn test_iter_try_fold() { - Python::with_gil(|py| { - let dict = [(1, 1), (2, 2), (3, 3)].into_py_dict(py).unwrap(); - let sum = dict - .iter() - .try_fold(0, |acc, (_, v)| PyResult::Ok(acc + v.extract::()?)) - .unwrap(); - assert_eq!(sum, 6); - - let dict = [(1, "foo"), (2, "bar")].into_py_dict(py).unwrap(); - assert!(dict - .iter() - .try_fold(0, |acc, (_, v)| PyResult::Ok(acc + v.extract::()?)) - .is_err()); - }); - } -} diff --git a/vendor/pyo3/src/types/ellipsis.rs b/vendor/pyo3/src/types/ellipsis.rs deleted file mode 100644 index ee5898c901..0000000000 --- a/vendor/pyo3/src/types/ellipsis.rs +++ /dev/null @@ -1,80 +0,0 @@ -use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, - Python, -}; - -/// Represents the Python `Ellipsis` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyEllipsis>`][Bound]. -#[repr(transparent)] -pub struct PyEllipsis(PyAny); - -pyobject_native_type_named!(PyEllipsis); - -impl PyEllipsis { - /// Returns the `Ellipsis` object. - #[inline] - pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyEllipsis> { - unsafe { ffi::Py_Ellipsis().assume_borrowed(py).downcast_unchecked() } - } - - /// Deprecated name for [`PyEllipsis::get`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyEllipsis::get`")] - #[inline] - pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyEllipsis> { - Self::get(py) - } -} - -unsafe impl PyTypeInfo for PyEllipsis { - const NAME: &'static str = "ellipsis"; - - const MODULE: Option<&'static str> = None; - - fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject { - unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) } - } - - #[inline] - fn is_type_of(object: &Bound<'_, PyAny>) -> bool { - // ellipsis is not usable as a base type - Self::is_exact_type_of(object) - } - - #[inline] - fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool { - object.is(&**Self::get(object.py())) - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::types::{PyDict, PyEllipsis}; - use crate::{PyTypeInfo, Python}; - - #[test] - fn test_ellipsis_is_itself() { - Python::with_gil(|py| { - assert!(PyEllipsis::get(py).is_instance_of::()); - assert!(PyEllipsis::get(py).is_exact_instance_of::()); - }) - } - - #[test] - fn test_ellipsis_type_object_consistent() { - Python::with_gil(|py| { - assert!(PyEllipsis::get(py) - .get_type() - .is(&PyEllipsis::type_object(py))); - }) - } - - #[test] - fn test_dict_is_not_ellipsis() { - Python::with_gil(|py| { - assert!(PyDict::new(py).downcast::().is_err()); - }) - } -} diff --git a/vendor/pyo3/src/types/float.rs b/vendor/pyo3/src/types/float.rs deleted file mode 100644 index 3c2d6643d1..0000000000 --- a/vendor/pyo3/src/types/float.rs +++ /dev/null @@ -1,396 +0,0 @@ -use super::any::PyAnyMethods; -use crate::conversion::IntoPyObject; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, instance::Bound, Borrowed, FromPyObject, PyAny, PyErr, PyObject, - PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; -use std::convert::Infallible; -use std::os::raw::c_double; - -/// Represents a Python `float` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyFloat>`][Bound]. -/// -/// For APIs available on `float` objects, see the [`PyFloatMethods`] trait which is implemented for -/// [`Bound<'py, PyFloat>`][Bound]. -/// -/// You can usually avoid directly working with this type -/// by using [`ToPyObject`] and [`extract`][PyAnyMethods::extract] -/// with [`f32`]/[`f64`]. -#[repr(transparent)] -pub struct PyFloat(PyAny); - -pyobject_subclassable_native_type!(PyFloat, crate::ffi::PyFloatObject); - -pyobject_native_type!( - PyFloat, - ffi::PyFloatObject, - pyobject_native_static_type_object!(ffi::PyFloat_Type), - #checkfunction=ffi::PyFloat_Check -); - -impl PyFloat { - /// Creates a new Python `float` object. - pub fn new(py: Python<'_>, val: c_double) -> Bound<'_, PyFloat> { - unsafe { - ffi::PyFloat_FromDouble(val) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyFloat::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyFloat::new`")] - #[inline] - pub fn new_bound(py: Python<'_>, val: c_double) -> Bound<'_, PyFloat> { - Self::new(py, val) - } -} - -/// Implementation of functionality for [`PyFloat`]. -/// -/// These methods are defined for the `Bound<'py, PyFloat>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyFloat")] -pub trait PyFloatMethods<'py>: crate::sealed::Sealed { - /// Gets the value of this float. - fn value(&self) -> c_double; -} - -impl<'py> PyFloatMethods<'py> for Bound<'py, PyFloat> { - fn value(&self) -> c_double { - #[cfg(not(Py_LIMITED_API))] - unsafe { - // Safety: self is PyFloat object - ffi::PyFloat_AS_DOUBLE(self.as_ptr()) - } - - #[cfg(Py_LIMITED_API)] - unsafe { - ffi::PyFloat_AsDouble(self.as_ptr()) - } - } -} - -#[allow(deprecated)] -impl ToPyObject for f64 { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for f64 { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for f64 { - type Target = PyFloat; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyFloat::new(py, self)) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("float") - } -} - -impl<'py> IntoPyObject<'py> for &f64 { - type Target = PyFloat; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("float") - } -} - -impl<'py> FromPyObject<'py> for f64 { - // PyFloat_AsDouble returns -1.0 upon failure - #![allow(clippy::float_cmp)] - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - // On non-limited API, .value() uses PyFloat_AS_DOUBLE which - // allows us to have an optimized fast path for the case when - // we have exactly a `float` object (it's not worth going through - // `isinstance` machinery for subclasses). - #[cfg(not(Py_LIMITED_API))] - if let Ok(float) = obj.downcast_exact::() { - return Ok(float.value()); - } - - let v = unsafe { ffi::PyFloat_AsDouble(obj.as_ptr()) }; - - if v == -1.0 { - if let Some(err) = PyErr::take(obj.py()) { - return Err(err); - } - } - - Ok(v) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -#[allow(deprecated)] -impl ToPyObject for f32 { - #[inline] - fn to_object(&self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy for f32 { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - self.into_pyobject(py).unwrap().into_any().unbind() - } -} - -impl<'py> IntoPyObject<'py> for f32 { - type Target = PyFloat; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PyFloat::new(py, self.into())) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("float") - } -} - -impl<'py> IntoPyObject<'py> for &f32 { - type Target = PyFloat; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - #[inline] - fn into_pyobject(self, py: Python<'py>) -> Result { - (*self).into_pyobject(py) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::builtin("float") - } -} - -impl<'py> FromPyObject<'py> for f32 { - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - Ok(obj.extract::()? as f32) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - Self::type_output() - } -} - -macro_rules! impl_partial_eq_for_float { - ($float_type: ty) => { - impl PartialEq<$float_type> for Bound<'_, PyFloat> { - #[inline] - fn eq(&self, other: &$float_type) -> bool { - self.value() as $float_type == *other - } - } - - impl PartialEq<$float_type> for &Bound<'_, PyFloat> { - #[inline] - fn eq(&self, other: &$float_type) -> bool { - self.value() as $float_type == *other - } - } - - impl PartialEq<&$float_type> for Bound<'_, PyFloat> { - #[inline] - fn eq(&self, other: &&$float_type) -> bool { - self.value() as $float_type == **other - } - } - - impl PartialEq> for $float_type { - #[inline] - fn eq(&self, other: &Bound<'_, PyFloat>) -> bool { - other.value() as $float_type == *self - } - } - - impl PartialEq<&'_ Bound<'_, PyFloat>> for $float_type { - #[inline] - fn eq(&self, other: &&'_ Bound<'_, PyFloat>) -> bool { - other.value() as $float_type == *self - } - } - - impl PartialEq> for &'_ $float_type { - #[inline] - fn eq(&self, other: &Bound<'_, PyFloat>) -> bool { - other.value() as $float_type == **self - } - } - - impl PartialEq<$float_type> for Borrowed<'_, '_, PyFloat> { - #[inline] - fn eq(&self, other: &$float_type) -> bool { - self.value() as $float_type == *other - } - } - - impl PartialEq<&$float_type> for Borrowed<'_, '_, PyFloat> { - #[inline] - fn eq(&self, other: &&$float_type) -> bool { - self.value() as $float_type == **other - } - } - - impl PartialEq> for $float_type { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyFloat>) -> bool { - other.value() as $float_type == *self - } - } - - impl PartialEq> for &$float_type { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyFloat>) -> bool { - other.value() as $float_type == **self - } - } - }; -} - -impl_partial_eq_for_float!(f64); -impl_partial_eq_for_float!(f32); - -#[cfg(test)] -mod tests { - use crate::{ - conversion::IntoPyObject, - types::{PyAnyMethods, PyFloat, PyFloatMethods}, - Python, - }; - - macro_rules! num_to_py_object_and_back ( - ($func_name:ident, $t1:ty, $t2:ty) => ( - #[test] - fn $func_name() { - use assert_approx_eq::assert_approx_eq; - - Python::with_gil(|py| { - - let val = 123 as $t1; - let obj = val.into_pyobject(py).unwrap(); - assert_approx_eq!(obj.extract::<$t2>().unwrap(), val as $t2); - }); - } - ) - ); - - num_to_py_object_and_back!(to_from_f64, f64, f64); - num_to_py_object_and_back!(to_from_f32, f32, f32); - num_to_py_object_and_back!(int_to_float, i32, f64); - - #[test] - fn test_float_value() { - use assert_approx_eq::assert_approx_eq; - - Python::with_gil(|py| { - let v = 1.23f64; - let obj = PyFloat::new(py, 1.23); - assert_approx_eq!(v, obj.value()); - }); - } - - #[test] - fn test_pyfloat_comparisons() { - Python::with_gil(|py| { - let f_64 = 1.01f64; - let py_f64 = PyFloat::new(py, 1.01); - let py_f64_ref = &py_f64; - let py_f64_borrowed = py_f64.as_borrowed(); - - // Bound<'_, PyFloat> == f64 and vice versa - assert_eq!(py_f64, f_64); - assert_eq!(f_64, py_f64); - - // Bound<'_, PyFloat> == &f64 and vice versa - assert_eq!(py_f64, &f_64); - assert_eq!(&f_64, py_f64); - - // &Bound<'_, PyFloat> == &f64 and vice versa - assert_eq!(py_f64_ref, f_64); - assert_eq!(f_64, py_f64_ref); - - // &Bound<'_, PyFloat> == &f64 and vice versa - assert_eq!(py_f64_ref, &f_64); - assert_eq!(&f_64, py_f64_ref); - - // Borrowed<'_, '_, PyFloat> == f64 and vice versa - assert_eq!(py_f64_borrowed, f_64); - assert_eq!(f_64, py_f64_borrowed); - - // Borrowed<'_, '_, PyFloat> == &f64 and vice versa - assert_eq!(py_f64_borrowed, &f_64); - assert_eq!(&f_64, py_f64_borrowed); - - let f_32 = 2.02f32; - let py_f32 = PyFloat::new(py, 2.02); - let py_f32_ref = &py_f32; - let py_f32_borrowed = py_f32.as_borrowed(); - - // Bound<'_, PyFloat> == f32 and vice versa - assert_eq!(py_f32, f_32); - assert_eq!(f_32, py_f32); - - // Bound<'_, PyFloat> == &f32 and vice versa - assert_eq!(py_f32, &f_32); - assert_eq!(&f_32, py_f32); - - // &Bound<'_, PyFloat> == &f32 and vice versa - assert_eq!(py_f32_ref, f_32); - assert_eq!(f_32, py_f32_ref); - - // &Bound<'_, PyFloat> == &f32 and vice versa - assert_eq!(py_f32_ref, &f_32); - assert_eq!(&f_32, py_f32_ref); - - // Borrowed<'_, '_, PyFloat> == f32 and vice versa - assert_eq!(py_f32_borrowed, f_32); - assert_eq!(f_32, py_f32_borrowed); - - // Borrowed<'_, '_, PyFloat> == &f32 and vice versa - assert_eq!(py_f32_borrowed, &f_32); - assert_eq!(&f_32, py_f32_borrowed); - }); - } -} diff --git a/vendor/pyo3/src/types/frame.rs b/vendor/pyo3/src/types/frame.rs deleted file mode 100644 index 8d88d4754a..0000000000 --- a/vendor/pyo3/src/types/frame.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::ffi; -use crate::PyAny; - -/// Represents a Python frame. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyFrame>`][crate::Bound]. -#[repr(transparent)] -pub struct PyFrame(PyAny); - -pyobject_native_type_core!( - PyFrame, - pyobject_native_static_type_object!(ffi::PyFrame_Type), - #checkfunction=ffi::PyFrame_Check -); diff --git a/vendor/pyo3/src/types/frozenset.rs b/vendor/pyo3/src/types/frozenset.rs deleted file mode 100644 index 954c49b590..0000000000 --- a/vendor/pyo3/src/types/frozenset.rs +++ /dev/null @@ -1,361 +0,0 @@ -use crate::types::PyIterator; -use crate::{ - err::{self, PyErr, PyResult}, - ffi, - ffi_ptr_ext::FfiPtrExt, - py_result_ext::PyResultExt, - types::any::PyAnyMethods, - Bound, PyAny, Python, -}; -use crate::{Borrowed, BoundObject, IntoPyObject, IntoPyObjectExt}; -use std::ptr; - -/// Allows building a Python `frozenset` one item at a time -pub struct PyFrozenSetBuilder<'py> { - py_frozen_set: Bound<'py, PyFrozenSet>, -} - -impl<'py> PyFrozenSetBuilder<'py> { - /// Create a new `FrozenSetBuilder`. - /// Since this allocates a `PyFrozenSet` internally it may - /// panic when running out of memory. - pub fn new(py: Python<'py>) -> PyResult> { - Ok(PyFrozenSetBuilder { - py_frozen_set: PyFrozenSet::empty(py)?, - }) - } - - /// Adds an element to the set. - pub fn add(&mut self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>, - { - fn inner(frozenset: &Bound<'_, PyFrozenSet>, key: Borrowed<'_, '_, PyAny>) -> PyResult<()> { - err::error_on_minusone(frozenset.py(), unsafe { - ffi::PySet_Add(frozenset.as_ptr(), key.as_ptr()) - }) - } - - inner( - &self.py_frozen_set, - key.into_pyobject(self.py_frozen_set.py()) - .map_err(Into::into)? - .into_any() - .as_borrowed(), - ) - } - - /// Finish building the set and take ownership of its current value - pub fn finalize(self) -> Bound<'py, PyFrozenSet> { - self.py_frozen_set - } - - /// Deprecated name for [`PyFrozenSetBuilder::finalize`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyFrozenSetBuilder::finalize`")] - #[inline] - pub fn finalize_bound(self) -> Bound<'py, PyFrozenSet> { - self.finalize() - } -} - -/// Represents a Python `frozenset`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyFrozenSet>`][Bound]. -/// -/// For APIs available on `frozenset` objects, see the [`PyFrozenSetMethods`] trait which is implemented for -/// [`Bound<'py, PyFrozenSet>`][Bound]. -#[repr(transparent)] -pub struct PyFrozenSet(PyAny); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_subclassable_native_type!(PyFrozenSet, crate::ffi::PySetObject); -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_native_type!( - PyFrozenSet, - ffi::PySetObject, - pyobject_native_static_type_object!(ffi::PyFrozenSet_Type), - #checkfunction=ffi::PyFrozenSet_Check -); - -#[cfg(any(PyPy, GraalPy))] -pyobject_native_type_core!( - PyFrozenSet, - pyobject_native_static_type_object!(ffi::PyFrozenSet_Type), - #checkfunction=ffi::PyFrozenSet_Check -); - -impl PyFrozenSet { - /// Creates a new frozenset. - /// - /// May panic when running out of memory. - #[inline] - pub fn new<'py, T>( - py: Python<'py>, - elements: impl IntoIterator, - ) -> PyResult> - where - T: IntoPyObject<'py>, - { - try_new_from_iter(py, elements) - } - - /// Deprecated name for [`PyFrozenSet::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyFrozenSet::new`")] - #[allow(deprecated)] - #[inline] - pub fn new_bound<'a, 'p, T: crate::ToPyObject + 'a>( - py: Python<'p>, - elements: impl IntoIterator, - ) -> PyResult> { - Self::new(py, elements.into_iter().map(|e| e.to_object(py))) - } - - /// Creates a new empty frozen set - pub fn empty(py: Python<'_>) -> PyResult> { - unsafe { - ffi::PyFrozenSet_New(ptr::null_mut()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyFrozenSet::empty`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyFrozenSet::empty`")] - #[inline] - pub fn empty_bound(py: Python<'_>) -> PyResult> { - Self::empty(py) - } -} - -/// Implementation of functionality for [`PyFrozenSet`]. -/// -/// These methods are defined for the `Bound<'py, PyFrozenSet>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyFrozenSet")] -pub trait PyFrozenSetMethods<'py>: crate::sealed::Sealed { - /// Returns the number of items in the set. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> usize; - - /// Checks if set is empty. - fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Determines if the set contains the specified key. - /// - /// This is equivalent to the Python expression `key in self`. - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>; - - /// Returns an iterator of values in this set. - fn iter(&self) -> BoundFrozenSetIterator<'py>; -} - -impl<'py> PyFrozenSetMethods<'py> for Bound<'py, PyFrozenSet> { - #[inline] - fn len(&self) -> usize { - unsafe { ffi::PySet_Size(self.as_ptr()) as usize } - } - - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>, - { - fn inner( - frozenset: &Bound<'_, PyFrozenSet>, - key: Borrowed<'_, '_, PyAny>, - ) -> PyResult { - match unsafe { ffi::PySet_Contains(frozenset.as_ptr(), key.as_ptr()) } { - 1 => Ok(true), - 0 => Ok(false), - _ => Err(PyErr::fetch(frozenset.py())), - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn iter(&self) -> BoundFrozenSetIterator<'py> { - BoundFrozenSetIterator::new(self.clone()) - } -} - -impl<'py> IntoIterator for Bound<'py, PyFrozenSet> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundFrozenSetIterator<'py>; - - /// Returns an iterator of values in this set. - fn into_iter(self) -> Self::IntoIter { - BoundFrozenSetIterator::new(self) - } -} - -impl<'py> IntoIterator for &Bound<'py, PyFrozenSet> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundFrozenSetIterator<'py>; - - /// Returns an iterator of values in this set. - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -/// PyO3 implementation of an iterator for a Python `frozenset` object. -pub struct BoundFrozenSetIterator<'p> { - it: Bound<'p, PyIterator>, - // Remaining elements in the frozenset - remaining: usize, -} - -impl<'py> BoundFrozenSetIterator<'py> { - pub(super) fn new(set: Bound<'py, PyFrozenSet>) -> Self { - Self { - it: PyIterator::from_object(&set).unwrap(), - remaining: set.len(), - } - } -} - -impl<'py> Iterator for BoundFrozenSetIterator<'py> { - type Item = Bound<'py, super::PyAny>; - - /// Advances the iterator and returns the next value. - fn next(&mut self) -> Option { - self.remaining = self.remaining.saturating_sub(1); - self.it.next().map(Result::unwrap) - } - - fn size_hint(&self) -> (usize, Option) { - (self.remaining, Some(self.remaining)) - } -} - -impl ExactSizeIterator for BoundFrozenSetIterator<'_> { - fn len(&self) -> usize { - self.remaining - } -} - -#[inline] -pub(crate) fn try_new_from_iter<'py, T>( - py: Python<'py>, - elements: impl IntoIterator, -) -> PyResult> -where - T: IntoPyObject<'py>, -{ - let set = unsafe { - // We create the `Py` pointer because its Drop cleans up the set if user code panics. - ffi::PyFrozenSet_New(std::ptr::null_mut()) - .assume_owned_or_err(py)? - .downcast_into_unchecked() - }; - let ptr = set.as_ptr(); - - for e in elements { - let obj = e.into_pyobject_or_pyerr(py)?; - err::error_on_minusone(py, unsafe { ffi::PySet_Add(ptr, obj.as_ptr()) })?; - } - - Ok(set) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_frozenset_new_and_len() { - Python::with_gil(|py| { - let set = PyFrozenSet::new(py, [1]).unwrap(); - assert_eq!(1, set.len()); - - let v = vec![1]; - assert!(PyFrozenSet::new(py, &[v]).is_err()); - }); - } - - #[test] - fn test_frozenset_empty() { - Python::with_gil(|py| { - let set = PyFrozenSet::empty(py).unwrap(); - assert_eq!(0, set.len()); - assert!(set.is_empty()); - }); - } - - #[test] - fn test_frozenset_contains() { - Python::with_gil(|py| { - let set = PyFrozenSet::new(py, [1]).unwrap(); - assert!(set.contains(1).unwrap()); - }); - } - - #[test] - fn test_frozenset_iter() { - Python::with_gil(|py| { - let set = PyFrozenSet::new(py, [1]).unwrap(); - - for el in set { - assert_eq!(1i32, el.extract::().unwrap()); - } - }); - } - - #[test] - fn test_frozenset_iter_bound() { - Python::with_gil(|py| { - let set = PyFrozenSet::new(py, [1]).unwrap(); - - for el in &set { - assert_eq!(1i32, el.extract::().unwrap()); - } - }); - } - - #[test] - fn test_frozenset_iter_size_hint() { - Python::with_gil(|py| { - let set = PyFrozenSet::new(py, [1]).unwrap(); - let mut iter = set.iter(); - - // Exact size - assert_eq!(iter.len(), 1); - assert_eq!(iter.size_hint(), (1, Some(1))); - iter.next(); - assert_eq!(iter.len(), 0); - assert_eq!(iter.size_hint(), (0, Some(0))); - }); - } - - #[test] - fn test_frozenset_builder() { - use super::PyFrozenSetBuilder; - - Python::with_gil(|py| { - let mut builder = PyFrozenSetBuilder::new(py).unwrap(); - - // add an item - builder.add(1).unwrap(); - builder.add(2).unwrap(); - builder.add(2).unwrap(); - - // finalize it - let set = builder.finalize(); - - assert!(set.contains(1).unwrap()); - assert!(set.contains(2).unwrap()); - assert!(!set.contains(3).unwrap()); - }); - } -} diff --git a/vendor/pyo3/src/types/function.rs b/vendor/pyo3/src/types/function.rs deleted file mode 100644 index 039e277454..0000000000 --- a/vendor/pyo3/src/types/function.rs +++ /dev/null @@ -1,229 +0,0 @@ -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::types::capsule::PyCapsuleMethods; -use crate::types::module::PyModuleMethods; -use crate::{ - ffi, - impl_::pymethods::{self, PyMethodDef}, - types::{PyCapsule, PyDict, PyModule, PyString, PyTuple}, -}; -use crate::{Bound, Py, PyAny, PyResult, Python}; -use std::cell::UnsafeCell; -use std::ffi::CStr; - -/// Represents a builtin Python function object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyCFunction>`][Bound]. -#[repr(transparent)] -pub struct PyCFunction(PyAny); - -pyobject_native_type_core!(PyCFunction, pyobject_native_static_type_object!(ffi::PyCFunction_Type), #checkfunction=ffi::PyCFunction_Check); - -impl PyCFunction { - /// Create a new built-in function with keywords (*args and/or **kwargs). - /// - /// To create `name` and `doc` static strings on Rust versions older than 1.77 (which added c"" literals), - /// use the [`c_str!`](crate::ffi::c_str) macro. - pub fn new_with_keywords<'py>( - py: Python<'py>, - fun: ffi::PyCFunctionWithKeywords, - name: &'static CStr, - doc: &'static CStr, - module: Option<&Bound<'py, PyModule>>, - ) -> PyResult> { - Self::internal_new( - py, - &PyMethodDef::cfunction_with_keywords(name, fun, doc), - module, - ) - } - - /// Deprecated name for [`PyCFunction::new_with_keywords`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyCFunction::new_with_keywords`")] - #[inline] - pub fn new_with_keywords_bound<'py>( - py: Python<'py>, - fun: ffi::PyCFunctionWithKeywords, - name: &'static CStr, - doc: &'static CStr, - module: Option<&Bound<'py, PyModule>>, - ) -> PyResult> { - Self::new_with_keywords(py, fun, name, doc, module) - } - - /// Create a new built-in function which takes no arguments. - /// - /// To create `name` and `doc` static strings on Rust versions older than 1.77 (which added c"" literals), - /// use the [`c_str!`](crate::ffi::c_str) macro. - pub fn new<'py>( - py: Python<'py>, - fun: ffi::PyCFunction, - name: &'static CStr, - doc: &'static CStr, - module: Option<&Bound<'py, PyModule>>, - ) -> PyResult> { - Self::internal_new(py, &PyMethodDef::noargs(name, fun, doc), module) - } - - /// Deprecated name for [`PyCFunction::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyCFunction::new`")] - #[inline] - pub fn new_bound<'py>( - py: Python<'py>, - fun: ffi::PyCFunction, - name: &'static CStr, - doc: &'static CStr, - module: Option<&Bound<'py, PyModule>>, - ) -> PyResult> { - Self::new(py, fun, name, doc, module) - } - - /// Create a new function from a closure. - /// - /// # Examples - /// - /// ``` - /// # use pyo3::prelude::*; - /// # use pyo3::{py_run, types::{PyCFunction, PyDict, PyTuple}}; - /// - /// Python::with_gil(|py| { - /// let add_one = |args: &Bound<'_, PyTuple>, _kwargs: Option<&Bound<'_, PyDict>>| -> PyResult<_> { - /// let i = args.extract::<(i64,)>()?.0; - /// Ok(i+1) - /// }; - /// let add_one = PyCFunction::new_closure(py, None, None, add_one).unwrap(); - /// py_run!(py, add_one, "assert add_one(42) == 43"); - /// }); - /// ``` - pub fn new_closure<'py, F, R>( - py: Python<'py>, - name: Option<&'static CStr>, - doc: Option<&'static CStr>, - closure: F, - ) -> PyResult> - where - F: Fn(&Bound<'_, PyTuple>, Option<&Bound<'_, PyDict>>) -> R + Send + 'static, - for<'p> R: crate::impl_::callback::IntoPyCallbackOutput<'p, *mut ffi::PyObject>, - { - let name = name.unwrap_or(ffi::c_str!("pyo3-closure")); - let doc = doc.unwrap_or(ffi::c_str!("")); - let method_def = - pymethods::PyMethodDef::cfunction_with_keywords(name, run_closure::, doc); - let def = method_def.as_method_def(); - - let capsule = PyCapsule::new( - py, - ClosureDestructor:: { - closure, - def: UnsafeCell::new(def), - }, - Some(CLOSURE_CAPSULE_NAME.to_owned()), - )?; - - // Safety: just created the capsule with type ClosureDestructor above - let data = unsafe { capsule.reference::>() }; - - unsafe { - ffi::PyCFunction_NewEx(data.def.get(), capsule.as_ptr(), std::ptr::null_mut()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyCFunction::new_closure`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyCFunction::new_closure`")] - #[inline] - pub fn new_closure_bound<'py, F, R>( - py: Python<'py>, - name: Option<&'static CStr>, - doc: Option<&'static CStr>, - closure: F, - ) -> PyResult> - where - F: Fn(&Bound<'_, PyTuple>, Option<&Bound<'_, PyDict>>) -> R + Send + 'static, - for<'p> R: crate::impl_::callback::IntoPyCallbackOutput<'p, *mut ffi::PyObject>, - { - Self::new_closure(py, name, doc, closure) - } - - #[doc(hidden)] - pub fn internal_new<'py>( - py: Python<'py>, - method_def: &PyMethodDef, - module: Option<&Bound<'py, PyModule>>, - ) -> PyResult> { - let (mod_ptr, module_name): (_, Option>) = if let Some(m) = module { - let mod_ptr = m.as_ptr(); - (mod_ptr, Some(m.name()?.unbind())) - } else { - (std::ptr::null_mut(), None) - }; - let def = method_def.as_method_def(); - - // FIXME: stop leaking the def - let def = Box::into_raw(Box::new(def)); - - let module_name_ptr = module_name - .as_ref() - .map_or(std::ptr::null_mut(), Py::as_ptr); - - unsafe { - ffi::PyCFunction_NewEx(def, mod_ptr, module_name_ptr) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } -} - -static CLOSURE_CAPSULE_NAME: &CStr = ffi::c_str!("pyo3-closure"); - -unsafe extern "C" fn run_closure( - capsule_ptr: *mut ffi::PyObject, - args: *mut ffi::PyObject, - kwargs: *mut ffi::PyObject, -) -> *mut ffi::PyObject -where - F: Fn(&Bound<'_, PyTuple>, Option<&Bound<'_, PyDict>>) -> R + Send + 'static, - for<'py> R: crate::impl_::callback::IntoPyCallbackOutput<'py, *mut ffi::PyObject>, -{ - use crate::types::any::PyAnyMethods; - - crate::impl_::trampoline::cfunction_with_keywords( - capsule_ptr, - args, - kwargs, - |py, capsule_ptr, args, kwargs| { - let boxed_fn: &ClosureDestructor = - &*(ffi::PyCapsule_GetPointer(capsule_ptr, CLOSURE_CAPSULE_NAME.as_ptr()) - as *mut ClosureDestructor); - let args = Bound::ref_from_ptr(py, &args).downcast_unchecked::(); - let kwargs = Bound::ref_from_ptr_or_opt(py, &kwargs) - .as_ref() - .map(|b| b.downcast_unchecked::()); - let result = (boxed_fn.closure)(args, kwargs); - crate::impl_::callback::convert(py, result) - }, - ) -} - -struct ClosureDestructor { - closure: F, - // Wrapped in UnsafeCell because Python C-API wants a *mut pointer - // to this member. - def: UnsafeCell, -} - -// Safety: F is send and none of the fields are ever mutated -unsafe impl Send for ClosureDestructor {} - -/// Represents a Python function object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyFunction>`][Bound]. -#[repr(transparent)] -#[cfg(not(Py_LIMITED_API))] -pub struct PyFunction(PyAny); - -#[cfg(not(Py_LIMITED_API))] -pyobject_native_type_core!(PyFunction, pyobject_native_static_type_object!(ffi::PyFunction_Type), #checkfunction=ffi::PyFunction_Check); diff --git a/vendor/pyo3/src/types/iterator.rs b/vendor/pyo3/src/types/iterator.rs deleted file mode 100644 index 068ab1fce3..0000000000 --- a/vendor/pyo3/src/types/iterator.rs +++ /dev/null @@ -1,330 +0,0 @@ -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Borrowed; -use crate::py_result_ext::PyResultExt; -use crate::{ffi, Bound, PyAny, PyErr, PyResult, PyTypeCheck}; - -/// A Python iterator object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyIterator>`][Bound]. -/// -/// # Examples -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::ffi::c_str; -/// -/// # fn main() -> PyResult<()> { -/// Python::with_gil(|py| -> PyResult<()> { -/// let list = py.eval(c_str!("iter([1, 2, 3, 4])"), None, None)?; -/// let numbers: PyResult> = list -/// .try_iter()? -/// .map(|i| i.and_then(|i|i.extract::())) -/// .collect(); -/// let sum: usize = numbers?.iter().sum(); -/// assert_eq!(sum, 10); -/// Ok(()) -/// }) -/// # } -/// ``` -#[repr(transparent)] -pub struct PyIterator(PyAny); -pyobject_native_type_named!(PyIterator); - -impl PyIterator { - /// Builds an iterator for an iterable Python object; the equivalent of calling `iter(obj)` in Python. - /// - /// Usually it is more convenient to write [`obj.iter()`][crate::types::any::PyAnyMethods::iter], - /// which is a more concise way of calling this function. - pub fn from_object<'py>(obj: &Bound<'py, PyAny>) -> PyResult> { - unsafe { - ffi::PyObject_GetIter(obj.as_ptr()) - .assume_owned_or_err(obj.py()) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyIterator::from_object`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyIterator::from_object`")] - #[inline] - pub fn from_bound_object<'py>(obj: &Bound<'py, PyAny>) -> PyResult> { - Self::from_object(obj) - } -} - -impl<'py> Iterator for Bound<'py, PyIterator> { - type Item = PyResult>; - - /// Retrieves the next item from an iterator. - /// - /// Returns `None` when the iterator is exhausted. - /// If an exception occurs, returns `Some(Err(..))`. - /// Further `next()` calls after an exception occurs are likely - /// to repeatedly result in the same exception. - #[inline] - fn next(&mut self) -> Option { - Borrowed::from(&*self).next() - } - - #[cfg(not(Py_LIMITED_API))] - fn size_hint(&self) -> (usize, Option) { - let hint = unsafe { ffi::PyObject_LengthHint(self.as_ptr(), 0) }; - (hint.max(0) as usize, None) - } -} - -impl<'py> Borrowed<'_, 'py, PyIterator> { - // TODO: this method is on Borrowed so that &'py PyIterator can use this; once that - // implementation is deleted this method should be moved to the `Bound<'py, PyIterator> impl - fn next(self) -> Option>> { - let py = self.py(); - - match unsafe { ffi::PyIter_Next(self.as_ptr()).assume_owned_or_opt(py) } { - Some(obj) => Some(Ok(obj)), - None => PyErr::take(py).map(Err), - } - } -} - -impl<'py> IntoIterator for &Bound<'py, PyIterator> { - type Item = PyResult>; - type IntoIter = Bound<'py, PyIterator>; - - fn into_iter(self) -> Self::IntoIter { - self.clone() - } -} - -impl PyTypeCheck for PyIterator { - const NAME: &'static str = "Iterator"; - - fn type_check(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::PyIter_Check(object.as_ptr()) != 0 } - } -} - -#[cfg(test)] -mod tests { - use super::PyIterator; - use crate::exceptions::PyTypeError; - use crate::types::{PyAnyMethods, PyDict, PyList, PyListMethods}; - use crate::{ffi, IntoPyObject, Python}; - - #[test] - fn vec_iter() { - Python::with_gil(|py| { - let inst = vec![10, 20].into_pyobject(py).unwrap(); - let mut it = inst.try_iter().unwrap(); - assert_eq!( - 10_i32, - it.next().unwrap().unwrap().extract::<'_, i32>().unwrap() - ); - assert_eq!( - 20_i32, - it.next().unwrap().unwrap().extract::<'_, i32>().unwrap() - ); - assert!(it.next().is_none()); - }); - } - - #[test] - fn iter_refcnt() { - let (obj, count) = Python::with_gil(|py| { - let obj = vec![10, 20].into_pyobject(py).unwrap(); - let count = obj.get_refcnt(); - (obj.unbind(), count) - }); - - Python::with_gil(|py| { - let inst = obj.bind(py); - let mut it = inst.try_iter().unwrap(); - - assert_eq!( - 10_i32, - it.next().unwrap().unwrap().extract::<'_, i32>().unwrap() - ); - }); - - Python::with_gil(move |py| { - assert_eq!(count, obj.get_refcnt(py)); - }); - } - - #[test] - fn iter_item_refcnt() { - Python::with_gil(|py| { - let count; - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - let list = { - let list = PyList::empty(py); - list.append(10).unwrap(); - list.append(&obj).unwrap(); - count = obj.get_refcnt(); - list - }; - - { - let mut it = list.iter(); - - assert_eq!(10_i32, it.next().unwrap().extract::<'_, i32>().unwrap()); - assert!(it.next().unwrap().is(&obj)); - assert!(it.next().is_none()); - } - assert_eq!(count, obj.get_refcnt()); - }); - } - - #[test] - fn fibonacci_generator() { - let fibonacci_generator = ffi::c_str!( - r#" -def fibonacci(target): - a = 1 - b = 1 - for _ in range(target): - yield a - a, b = b, a + b -"# - ); - - Python::with_gil(|py| { - let context = PyDict::new(py); - py.run(fibonacci_generator, None, Some(&context)).unwrap(); - - let generator = py - .eval(ffi::c_str!("fibonacci(5)"), None, Some(&context)) - .unwrap(); - for (actual, expected) in generator.try_iter().unwrap().zip(&[1, 1, 2, 3, 5]) { - let actual = actual.unwrap().extract::().unwrap(); - assert_eq!(actual, *expected) - } - }); - } - - #[test] - fn fibonacci_generator_bound() { - use crate::types::any::PyAnyMethods; - use crate::Bound; - - let fibonacci_generator = ffi::c_str!( - r#" -def fibonacci(target): - a = 1 - b = 1 - for _ in range(target): - yield a - a, b = b, a + b -"# - ); - - Python::with_gil(|py| { - let context = PyDict::new(py); - py.run(fibonacci_generator, None, Some(&context)).unwrap(); - - let generator: Bound<'_, PyIterator> = py - .eval(ffi::c_str!("fibonacci(5)"), None, Some(&context)) - .unwrap() - .downcast_into() - .unwrap(); - let mut items = vec![]; - for actual in &generator { - let actual = actual.unwrap().extract::().unwrap(); - items.push(actual); - } - assert_eq!(items, [1, 1, 2, 3, 5]); - }); - } - - #[test] - fn int_not_iterable() { - Python::with_gil(|py| { - let x = 5i32.into_pyobject(py).unwrap(); - let err = PyIterator::from_object(&x).unwrap_err(); - - assert!(err.is_instance_of::(py)); - }); - } - - #[test] - #[cfg(feature = "macros")] - fn python_class_not_iterator() { - use crate::PyErr; - - #[crate::pyclass(crate = "crate")] - struct Downcaster { - failed: Option, - } - - #[crate::pymethods(crate = "crate")] - impl Downcaster { - fn downcast_iterator(&mut self, obj: &crate::Bound<'_, crate::PyAny>) { - self.failed = Some(obj.downcast::().unwrap_err().into()); - } - } - - // Regression test for 2913 - Python::with_gil(|py| { - let downcaster = crate::Py::new(py, Downcaster { failed: None }).unwrap(); - crate::py_run!( - py, - downcaster, - r#" - from collections.abc import Sequence - - class MySequence(Sequence): - def __init__(self): - self._data = [1, 2, 3] - - def __getitem__(self, index): - return self._data[index] - - def __len__(self): - return len(self._data) - - downcaster.downcast_iterator(MySequence()) - "# - ); - - assert_eq!( - downcaster.borrow_mut(py).failed.take().unwrap().to_string(), - "TypeError: 'MySequence' object cannot be converted to 'Iterator'" - ); - }); - } - - #[test] - #[cfg(feature = "macros")] - fn python_class_iterator() { - #[crate::pyfunction(crate = "crate")] - fn assert_iterator(obj: &crate::Bound<'_, crate::PyAny>) { - assert!(obj.downcast::().is_ok()) - } - - // Regression test for 2913 - Python::with_gil(|py| { - let assert_iterator = crate::wrap_pyfunction!(assert_iterator, py).unwrap(); - crate::py_run!( - py, - assert_iterator, - r#" - class MyIter: - def __next__(self): - raise StopIteration - - assert_iterator(MyIter()) - "# - ); - }); - } - - #[test] - #[cfg(not(Py_LIMITED_API))] - fn length_hint_becomes_size_hint_lower_bound() { - Python::with_gil(|py| { - let list = py.eval(ffi::c_str!("[1, 2, 3]"), None, None).unwrap(); - let iter = list.try_iter().unwrap(); - let hint = iter.size_hint(); - assert_eq!(hint, (3, None)); - }); - } -} diff --git a/vendor/pyo3/src/types/list.rs b/vendor/pyo3/src/types/list.rs deleted file mode 100644 index 76da36d00b..0000000000 --- a/vendor/pyo3/src/types/list.rs +++ /dev/null @@ -1,1505 +0,0 @@ -use std::iter::FusedIterator; - -use crate::err::{self, PyResult}; -use crate::ffi::{self, Py_ssize_t}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::internal_tricks::get_ssize_index; -use crate::types::{PySequence, PyTuple}; -use crate::{ - Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyObject, Python, -}; - -use crate::types::any::PyAnyMethods; -use crate::types::sequence::PySequenceMethods; - -/// Represents a Python `list`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyList>`][Bound]. -/// -/// For APIs available on `list` objects, see the [`PyListMethods`] trait which is implemented for -/// [`Bound<'py, PyList>`][Bound]. -#[repr(transparent)] -pub struct PyList(PyAny); - -pyobject_native_type_core!(PyList, pyobject_native_static_type_object!(ffi::PyList_Type), #checkfunction=ffi::PyList_Check); - -#[inline] -#[track_caller] -pub(crate) fn new_from_iter( - py: Python<'_>, - elements: impl ExactSizeIterator, -) -> Bound<'_, PyList> { - try_new_from_iter(py, elements.map(|e| e.into_bound(py)).map(Ok)).unwrap() -} - -#[inline] -#[track_caller] -pub(crate) fn try_new_from_iter<'py>( - py: Python<'py>, - mut elements: impl ExactSizeIterator>>, -) -> PyResult> { - unsafe { - // PyList_New checks for overflow but has a bad error message, so we check ourselves - let len: Py_ssize_t = elements - .len() - .try_into() - .expect("out of range integral type conversion attempted on `elements.len()`"); - - let ptr = ffi::PyList_New(len); - - // We create the `Bound` pointer here for two reasons: - // - panics if the ptr is null - // - its Drop cleans up the list if user code or the asserts panic. - let list = ptr.assume_owned(py).downcast_into_unchecked(); - - let count = (&mut elements) - .take(len as usize) - .try_fold(0, |count, item| { - #[cfg(not(Py_LIMITED_API))] - ffi::PyList_SET_ITEM(ptr, count, item?.into_ptr()); - #[cfg(Py_LIMITED_API)] - ffi::PyList_SetItem(ptr, count, item?.into_ptr()); - Ok::<_, PyErr>(count + 1) - })?; - - assert!(elements.next().is_none(), "Attempted to create PyList but `elements` was larger than reported by its `ExactSizeIterator` implementation."); - assert_eq!(len, count, "Attempted to create PyList but `elements` was smaller than reported by its `ExactSizeIterator` implementation."); - - Ok(list) - } -} - -impl PyList { - /// Constructs a new list with the given elements. - /// - /// If you want to create a [`PyList`] with elements of different or unknown types, or from an - /// iterable that doesn't implement [`ExactSizeIterator`], use [`PyListMethods::append`]. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyList; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let elements: Vec = vec![0, 1, 2, 3, 4, 5]; - /// let list = PyList::new(py, elements)?; - /// assert_eq!(format!("{:?}", list), "[0, 1, 2, 3, 4, 5]"); - /// # Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// - /// This function will panic if `element`'s [`ExactSizeIterator`] implementation is incorrect. - /// All standard library structures implement this trait correctly, if they do, so calling this - /// function with (for example) [`Vec`]`` or `&[T]` will always succeed. - #[track_caller] - pub fn new<'py, T, U>( - py: Python<'py>, - elements: impl IntoIterator, - ) -> PyResult> - where - T: IntoPyObject<'py>, - U: ExactSizeIterator, - { - let iter = elements.into_iter().map(|e| e.into_bound_py_any(py)); - try_new_from_iter(py, iter) - } - - /// Deprecated name for [`PyList::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyList::new`")] - #[allow(deprecated)] - #[inline] - #[track_caller] - pub fn new_bound( - py: Python<'_>, - elements: impl IntoIterator, - ) -> Bound<'_, PyList> - where - T: crate::ToPyObject, - U: ExactSizeIterator, - { - Self::new(py, elements.into_iter().map(|e| e.to_object(py))).unwrap() - } - - /// Constructs a new empty list. - pub fn empty(py: Python<'_>) -> Bound<'_, PyList> { - unsafe { - ffi::PyList_New(0) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyList::empty`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyList::empty`")] - #[inline] - pub fn empty_bound(py: Python<'_>) -> Bound<'_, PyList> { - Self::empty(py) - } -} - -/// Implementation of functionality for [`PyList`]. -/// -/// These methods are defined for the `Bound<'py, PyList>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyList")] -pub trait PyListMethods<'py>: crate::sealed::Sealed { - /// Returns the length of the list. - fn len(&self) -> usize; - - /// Checks if the list is empty. - fn is_empty(&self) -> bool; - - /// Returns `self` cast as a `PySequence`. - fn as_sequence(&self) -> &Bound<'py, PySequence>; - - /// Returns `self` cast as a `PySequence`. - fn into_sequence(self) -> Bound<'py, PySequence>; - - /// Gets the list item at the specified index. - /// # Example - /// ``` - /// use pyo3::{prelude::*, types::PyList}; - /// Python::with_gil(|py| { - /// let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - /// let obj = list.get_item(0); - /// assert_eq!(obj.unwrap().extract::().unwrap(), 2); - /// }); - /// ``` - fn get_item(&self, index: usize) -> PyResult>; - - /// Gets the list item at the specified index. Undefined behavior on bad index. Use with caution. - /// - /// # Safety - /// - /// Caller must verify that the index is within the bounds of the list. - /// On the free-threaded build, caller must verify they have exclusive access to the list - /// via a lock or by holding the innermost critical section on the list. - #[cfg(not(Py_LIMITED_API))] - unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; - - /// Takes the slice `self[low:high]` and returns it as a new list. - /// - /// Indices must be nonnegative, and out-of-range indices are clipped to - /// `self.len()`. - fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList>; - - /// Sets the item at the specified index. - /// - /// Raises `IndexError` if the index is out of range. - fn set_item(&self, index: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>; - - /// Deletes the `index`th element of self. - /// - /// This is equivalent to the Python statement `del self[i]`. - fn del_item(&self, index: usize) -> PyResult<()>; - - /// Assigns the sequence `seq` to the slice of `self` from `low` to `high`. - /// - /// This is equivalent to the Python statement `self[low:high] = v`. - fn set_slice(&self, low: usize, high: usize, seq: &Bound<'_, PyAny>) -> PyResult<()>; - - /// Deletes the slice from `low` to `high` from `self`. - /// - /// This is equivalent to the Python statement `del self[low:high]`. - fn del_slice(&self, low: usize, high: usize) -> PyResult<()>; - - /// Appends an item to the list. - fn append(&self, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>; - - /// Inserts an item at the specified index. - /// - /// If `index >= self.len()`, inserts at the end. - fn insert(&self, index: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>; - - /// Determines if self contains `value`. - /// - /// This is equivalent to the Python expression `value in self`. - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns the first index `i` for which `self[i] == value`. - /// - /// This is equivalent to the Python expression `self.index(value)`. - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns an iterator over this list's items. - fn iter(&self) -> BoundListIterator<'py>; - - /// Iterates over the contents of this list while holding a critical section on the list. - /// This is useful when the GIL is disabled and the list is shared between threads. - /// It is not guaranteed that the list will not be modified during iteration when the - /// closure calls arbitrary Python code that releases the critical section held by the - /// iterator. Otherwise, the list will not be modified during iteration. - /// - /// This is equivalent to for_each if the GIL is enabled. - fn locked_for_each(&self, closure: F) -> PyResult<()> - where - F: Fn(Bound<'py, PyAny>) -> PyResult<()>; - - /// Sorts the list in-place. Equivalent to the Python expression `l.sort()`. - fn sort(&self) -> PyResult<()>; - - /// Reverses the list in-place. Equivalent to the Python expression `l.reverse()`. - fn reverse(&self) -> PyResult<()>; - - /// Return a new tuple containing the contents of the list; equivalent to the Python expression `tuple(list)`. - /// - /// This method is equivalent to `self.as_sequence().to_tuple()` and faster than `PyTuple::new(py, this_list)`. - fn to_tuple(&self) -> Bound<'py, PyTuple>; -} - -impl<'py> PyListMethods<'py> for Bound<'py, PyList> { - /// Returns the length of the list. - fn len(&self) -> usize { - unsafe { - #[cfg(not(Py_LIMITED_API))] - let size = ffi::PyList_GET_SIZE(self.as_ptr()); - #[cfg(Py_LIMITED_API)] - let size = ffi::PyList_Size(self.as_ptr()); - - // non-negative Py_ssize_t should always fit into Rust usize - size as usize - } - } - - /// Checks if the list is empty. - fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns `self` cast as a `PySequence`. - fn as_sequence(&self) -> &Bound<'py, PySequence> { - unsafe { self.downcast_unchecked() } - } - - /// Returns `self` cast as a `PySequence`. - fn into_sequence(self) -> Bound<'py, PySequence> { - unsafe { self.into_any().downcast_into_unchecked() } - } - - /// Gets the list item at the specified index. - /// # Example - /// ``` - /// use pyo3::{prelude::*, types::PyList}; - /// Python::with_gil(|py| { - /// let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - /// let obj = list.get_item(0); - /// assert_eq!(obj.unwrap().extract::().unwrap(), 2); - /// }); - /// ``` - fn get_item(&self, index: usize) -> PyResult> { - unsafe { - ffi::compat::PyList_GetItemRef(self.as_ptr(), index as Py_ssize_t) - .assume_owned_or_err(self.py()) - } - } - - /// Gets the list item at the specified index. Undefined behavior on bad index. Use with caution. - /// - /// # Safety - /// - /// Caller must verify that the index is within the bounds of the list. - #[cfg(not(Py_LIMITED_API))] - unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny> { - // PyList_GET_ITEM return borrowed ptr; must make owned for safety (see #890). - ffi::PyList_GET_ITEM(self.as_ptr(), index as Py_ssize_t) - .assume_borrowed(self.py()) - .to_owned() - } - - /// Takes the slice `self[low:high]` and returns it as a new list. - /// - /// Indices must be nonnegative, and out-of-range indices are clipped to - /// `self.len()`. - fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyList> { - unsafe { - ffi::PyList_GetSlice(self.as_ptr(), get_ssize_index(low), get_ssize_index(high)) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } - - /// Sets the item at the specified index. - /// - /// Raises `IndexError` if the index is out of range. - fn set_item(&self, index: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>, - { - fn inner(list: &Bound<'_, PyList>, index: usize, item: Bound<'_, PyAny>) -> PyResult<()> { - err::error_on_minusone(list.py(), unsafe { - ffi::PyList_SetItem(list.as_ptr(), get_ssize_index(index), item.into_ptr()) - }) - } - - let py = self.py(); - inner(self, index, item.into_bound_py_any(py)?) - } - - /// Deletes the `index`th element of self. - /// - /// This is equivalent to the Python statement `del self[i]`. - #[inline] - fn del_item(&self, index: usize) -> PyResult<()> { - self.as_sequence().del_item(index) - } - - /// Assigns the sequence `seq` to the slice of `self` from `low` to `high`. - /// - /// This is equivalent to the Python statement `self[low:high] = v`. - #[inline] - fn set_slice(&self, low: usize, high: usize, seq: &Bound<'_, PyAny>) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PyList_SetSlice( - self.as_ptr(), - get_ssize_index(low), - get_ssize_index(high), - seq.as_ptr(), - ) - }) - } - - /// Deletes the slice from `low` to `high` from `self`. - /// - /// This is equivalent to the Python statement `del self[low:high]`. - #[inline] - fn del_slice(&self, low: usize, high: usize) -> PyResult<()> { - self.as_sequence().del_slice(low, high) - } - - /// Appends an item to the list. - fn append(&self, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>, - { - fn inner(list: &Bound<'_, PyList>, item: Borrowed<'_, '_, PyAny>) -> PyResult<()> { - err::error_on_minusone(list.py(), unsafe { - ffi::PyList_Append(list.as_ptr(), item.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - item.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - /// Inserts an item at the specified index. - /// - /// If `index >= self.len()`, inserts at the end. - fn insert(&self, index: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>, - { - fn inner( - list: &Bound<'_, PyList>, - index: usize, - item: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - err::error_on_minusone(list.py(), unsafe { - ffi::PyList_Insert(list.as_ptr(), get_ssize_index(index), item.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - index, - item.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - /// Determines if self contains `value`. - /// - /// This is equivalent to the Python expression `value in self`. - #[inline] - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - self.as_sequence().contains(value) - } - - /// Returns the first index `i` for which `self[i] == value`. - /// - /// This is equivalent to the Python expression `self.index(value)`. - #[inline] - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - self.as_sequence().index(value) - } - - /// Returns an iterator over this list's items. - fn iter(&self) -> BoundListIterator<'py> { - BoundListIterator::new(self.clone()) - } - - /// Iterates over a list while holding a critical section, calling a closure on each item - fn locked_for_each(&self, closure: F) -> PyResult<()> - where - F: Fn(Bound<'py, PyAny>) -> PyResult<()>, - { - crate::sync::with_critical_section(self, || self.iter().try_for_each(closure)) - } - - /// Sorts the list in-place. Equivalent to the Python expression `l.sort()`. - fn sort(&self) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { ffi::PyList_Sort(self.as_ptr()) }) - } - - /// Reverses the list in-place. Equivalent to the Python expression `l.reverse()`. - fn reverse(&self) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { ffi::PyList_Reverse(self.as_ptr()) }) - } - - /// Return a new tuple containing the contents of the list; equivalent to the Python expression `tuple(list)`. - /// - /// This method is equivalent to `self.as_sequence().to_tuple()` and faster than `PyTuple::new(py, this_list)`. - fn to_tuple(&self) -> Bound<'py, PyTuple> { - unsafe { - ffi::PyList_AsTuple(self.as_ptr()) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } -} - -// New types for type checking when using BoundListIterator associated methods, like -// BoundListIterator::next_unchecked. -struct Index(usize); -struct Length(usize); - -/// Used by `PyList::iter()`. -pub struct BoundListIterator<'py> { - list: Bound<'py, PyList>, - index: Index, - length: Length, -} - -impl<'py> BoundListIterator<'py> { - fn new(list: Bound<'py, PyList>) -> Self { - Self { - index: Index(0), - length: Length(list.len()), - list, - } - } - - /// # Safety - /// - /// On the free-threaded build, caller must verify they have exclusive - /// access to the list by holding a lock or by holding the innermost - /// critical section on the list. - #[inline] - #[cfg(not(Py_LIMITED_API))] - #[deny(unsafe_op_in_unsafe_fn)] - unsafe fn next_unchecked( - index: &mut Index, - length: &mut Length, - list: &Bound<'py, PyList>, - ) -> Option> { - let length = length.0.min(list.len()); - let my_index = index.0; - - if index.0 < length { - let item = unsafe { list.get_item_unchecked(my_index) }; - index.0 += 1; - Some(item) - } else { - None - } - } - - #[cfg(Py_LIMITED_API)] - fn next( - index: &mut Index, - length: &mut Length, - list: &Bound<'py, PyList>, - ) -> Option> { - let length = length.0.min(list.len()); - let my_index = index.0; - - if index.0 < length { - let item = list.get_item(my_index).expect("get-item failed"); - index.0 += 1; - Some(item) - } else { - None - } - } - - /// # Safety - /// - /// On the free-threaded build, caller must verify they have exclusive - /// access to the list by holding a lock or by holding the innermost - /// critical section on the list. - #[inline] - #[cfg(not(Py_LIMITED_API))] - #[deny(unsafe_op_in_unsafe_fn)] - unsafe fn next_back_unchecked( - index: &mut Index, - length: &mut Length, - list: &Bound<'py, PyList>, - ) -> Option> { - let current_length = length.0.min(list.len()); - - if index.0 < current_length { - let item = unsafe { list.get_item_unchecked(current_length - 1) }; - length.0 = current_length - 1; - Some(item) - } else { - None - } - } - - #[inline] - #[cfg(Py_LIMITED_API)] - fn next_back( - index: &mut Index, - length: &mut Length, - list: &Bound<'py, PyList>, - ) -> Option> { - let current_length = (length.0).min(list.len()); - - if index.0 < current_length { - let item = list.get_item(current_length - 1).expect("get-item failed"); - length.0 = current_length - 1; - Some(item) - } else { - None - } - } - - #[cfg(not(Py_LIMITED_API))] - fn with_critical_section( - &mut self, - f: impl FnOnce(&mut Index, &mut Length, &Bound<'py, PyList>) -> R, - ) -> R { - let Self { - index, - length, - list, - } = self; - crate::sync::with_critical_section(list, || f(index, length, list)) - } -} - -impl<'py> Iterator for BoundListIterator<'py> { - type Item = Bound<'py, PyAny>; - - #[inline] - fn next(&mut self) -> Option { - #[cfg(not(Py_LIMITED_API))] - { - self.with_critical_section(|index, length, list| unsafe { - Self::next_unchecked(index, length, list) - }) - } - #[cfg(Py_LIMITED_API)] - { - let Self { - index, - length, - list, - } = self; - Self::next(index, length, list) - } - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn fold(mut self, init: B, mut f: F) -> B - where - Self: Sized, - F: FnMut(B, Self::Item) -> B, - { - self.with_critical_section(|index, length, list| { - let mut accum = init; - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - accum = f(accum, x); - } - accum - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, feature = "nightly"))] - fn try_fold(&mut self, init: B, mut f: F) -> R - where - Self: Sized, - F: FnMut(B, Self::Item) -> R, - R: std::ops::Try, - { - self.with_critical_section(|index, length, list| { - let mut accum = init; - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - accum = f(accum, x)? - } - R::from_output(accum) - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn all(&mut self, mut f: F) -> bool - where - Self: Sized, - F: FnMut(Self::Item) -> bool, - { - self.with_critical_section(|index, length, list| { - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - if !f(x) { - return false; - } - } - true - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn any(&mut self, mut f: F) -> bool - where - Self: Sized, - F: FnMut(Self::Item) -> bool, - { - self.with_critical_section(|index, length, list| { - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - if f(x) { - return true; - } - } - false - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn find

(&mut self, mut predicate: P) -> Option - where - Self: Sized, - P: FnMut(&Self::Item) -> bool, - { - self.with_critical_section(|index, length, list| { - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - if predicate(&x) { - return Some(x); - } - } - None - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn find_map(&mut self, mut f: F) -> Option - where - Self: Sized, - F: FnMut(Self::Item) -> Option, - { - self.with_critical_section(|index, length, list| { - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - if let found @ Some(_) = f(x) { - return found; - } - } - None - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn position

(&mut self, mut predicate: P) -> Option - where - Self: Sized, - P: FnMut(Self::Item) -> bool, - { - self.with_critical_section(|index, length, list| { - let mut acc = 0; - while let Some(x) = unsafe { Self::next_unchecked(index, length, list) } { - if predicate(x) { - return Some(acc); - } - acc += 1; - } - None - }) - } -} - -impl DoubleEndedIterator for BoundListIterator<'_> { - #[inline] - fn next_back(&mut self) -> Option { - #[cfg(not(Py_LIMITED_API))] - { - self.with_critical_section(|index, length, list| unsafe { - Self::next_back_unchecked(index, length, list) - }) - } - #[cfg(Py_LIMITED_API)] - { - let Self { - index, - length, - list, - } = self; - Self::next_back(index, length, list) - } - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))] - fn rfold(mut self, init: B, mut f: F) -> B - where - Self: Sized, - F: FnMut(B, Self::Item) -> B, - { - self.with_critical_section(|index, length, list| { - let mut accum = init; - while let Some(x) = unsafe { Self::next_back_unchecked(index, length, list) } { - accum = f(accum, x); - } - accum - }) - } - - #[inline] - #[cfg(all(Py_GIL_DISABLED, feature = "nightly"))] - fn try_rfold(&mut self, init: B, mut f: F) -> R - where - Self: Sized, - F: FnMut(B, Self::Item) -> R, - R: std::ops::Try, - { - self.with_critical_section(|index, length, list| { - let mut accum = init; - while let Some(x) = unsafe { Self::next_back_unchecked(index, length, list) } { - accum = f(accum, x)? - } - R::from_output(accum) - }) - } -} - -impl ExactSizeIterator for BoundListIterator<'_> { - fn len(&self) -> usize { - self.length.0.saturating_sub(self.index.0) - } -} - -impl FusedIterator for BoundListIterator<'_> {} - -impl<'py> IntoIterator for Bound<'py, PyList> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundListIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - BoundListIterator::new(self) - } -} - -impl<'py> IntoIterator for &Bound<'py, PyList> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundListIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::types::list::PyListMethods; - use crate::types::sequence::PySequenceMethods; - use crate::types::{PyList, PyTuple}; - use crate::{ffi, IntoPyObject, PyResult, Python}; - - #[test] - fn test_new() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(7, list.get_item(3).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_len() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - assert_eq!(4, list.len()); - }); - } - - #[test] - fn test_get_item() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(7, list.get_item(3).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_get_slice() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let slice = list.get_slice(1, 3); - assert_eq!(2, slice.len()); - let slice = list.get_slice(1, 7); - assert_eq!(3, slice.len()); - }); - } - - #[test] - fn test_set_item() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let val = 42i32.into_pyobject(py).unwrap(); - let val2 = 42i32.into_pyobject(py).unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - list.set_item(0, val).unwrap(); - assert_eq!(42, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.set_item(10, val2).is_err()); - }); - } - - #[test] - fn test_set_item_refcnt() { - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - let cnt; - { - let v = vec![2]; - let ob = v.into_pyobject(py).unwrap(); - let list = ob.downcast::().unwrap(); - cnt = obj.get_refcnt(); - list.set_item(0, &obj).unwrap(); - } - - assert_eq!(cnt, obj.get_refcnt()); - }); - } - - #[test] - fn test_insert() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let val = 42i32.into_pyobject(py).unwrap(); - let val2 = 43i32.into_pyobject(py).unwrap(); - assert_eq!(4, list.len()); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - list.insert(0, val).unwrap(); - list.insert(1000, val2).unwrap(); - assert_eq!(6, list.len()); - assert_eq!(42, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(2, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(43, list.get_item(5).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_insert_refcnt() { - Python::with_gil(|py| { - let cnt; - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - { - let list = PyList::empty(py); - cnt = obj.get_refcnt(); - list.insert(0, &obj).unwrap(); - } - - assert_eq!(cnt, obj.get_refcnt()); - }); - } - - #[test] - fn test_append() { - Python::with_gil(|py| { - let list = PyList::new(py, [2]).unwrap(); - list.append(3).unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_append_refcnt() { - Python::with_gil(|py| { - let cnt; - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - { - let list = PyList::empty(py); - cnt = obj.get_refcnt(); - list.append(&obj).unwrap(); - } - assert_eq!(cnt, obj.get_refcnt()); - }); - } - - #[test] - fn test_iter() { - Python::with_gil(|py| { - let v = vec![2, 3, 5, 7]; - let list = PyList::new(py, &v).unwrap(); - let mut idx = 0; - for el in list { - assert_eq!(v[idx], el.extract::().unwrap()); - idx += 1; - } - assert_eq!(idx, v.len()); - }); - } - - #[test] - fn test_iter_size_hint() { - Python::with_gil(|py| { - let v = vec![2, 3, 5, 7]; - let ob = (&v).into_pyobject(py).unwrap(); - let list = ob.downcast::().unwrap(); - - let mut iter = list.iter(); - assert_eq!(iter.size_hint(), (v.len(), Some(v.len()))); - iter.next(); - assert_eq!(iter.size_hint(), (v.len() - 1, Some(v.len() - 1))); - - // Exhaust iterator. - for _ in &mut iter {} - - assert_eq!(iter.size_hint(), (0, Some(0))); - }); - } - - #[test] - fn test_iter_rev() { - Python::with_gil(|py| { - let v = vec![2, 3, 5, 7]; - let ob = v.into_pyobject(py).unwrap(); - let list = ob.downcast::().unwrap(); - - let mut iter = list.iter().rev(); - - assert_eq!(iter.size_hint(), (4, Some(4))); - - assert_eq!(iter.next().unwrap().extract::().unwrap(), 7); - assert_eq!(iter.size_hint(), (3, Some(3))); - - assert_eq!(iter.next().unwrap().extract::().unwrap(), 5); - assert_eq!(iter.size_hint(), (2, Some(2))); - - assert_eq!(iter.next().unwrap().extract::().unwrap(), 3); - assert_eq!(iter.size_hint(), (1, Some(1))); - - assert_eq!(iter.next().unwrap().extract::().unwrap(), 2); - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - assert!(iter.next().is_none()); - }); - } - - #[test] - fn test_iter_all() { - Python::with_gil(|py| { - let list = PyList::new(py, [true, true, true]).unwrap(); - assert!(list.iter().all(|x| x.extract::().unwrap())); - - let list = PyList::new(py, [true, false, true]).unwrap(); - assert!(!list.iter().all(|x| x.extract::().unwrap())); - }); - } - - #[test] - fn test_iter_any() { - Python::with_gil(|py| { - let list = PyList::new(py, [true, true, true]).unwrap(); - assert!(list.iter().any(|x| x.extract::().unwrap())); - - let list = PyList::new(py, [true, false, true]).unwrap(); - assert!(list.iter().any(|x| x.extract::().unwrap())); - - let list = PyList::new(py, [false, false, false]).unwrap(); - assert!(!list.iter().any(|x| x.extract::().unwrap())); - }); - } - - #[test] - fn test_iter_find() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, ["hello", "world"]).unwrap(); - assert_eq!( - Some("world".to_string()), - list.iter() - .find(|v| v.extract::().unwrap() == "world") - .map(|v| v.extract::().unwrap()) - ); - assert_eq!( - None, - list.iter() - .find(|v| v.extract::().unwrap() == "foobar") - .map(|v| v.extract::().unwrap()) - ); - }); - } - - #[test] - fn test_iter_position() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, ["hello", "world"]).unwrap(); - assert_eq!( - Some(1), - list.iter() - .position(|v| v.extract::().unwrap() == "world") - ); - assert_eq!( - None, - list.iter() - .position(|v| v.extract::().unwrap() == "foobar") - ); - }); - } - - #[test] - fn test_iter_fold() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, [1, 2, 3]).unwrap(); - let sum = list - .iter() - .fold(0, |acc, v| acc + v.extract::().unwrap()); - assert_eq!(sum, 6); - }); - } - - #[test] - fn test_iter_fold_out_of_bounds() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, [1, 2, 3]).unwrap(); - let sum = list.iter().fold(0, |_, _| { - // clear the list to create a pathological fold operation - // that mutates the list as it processes it - for _ in 0..3 { - list.del_item(0).unwrap(); - } - -5 - }); - assert_eq!(sum, -5); - assert!(list.len() == 0); - }); - } - - #[test] - fn test_iter_rfold() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, [1, 2, 3]).unwrap(); - let sum = list - .iter() - .rfold(0, |acc, v| acc + v.extract::().unwrap()); - assert_eq!(sum, 6); - }); - } - - #[test] - fn test_iter_try_fold() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, [1, 2, 3]).unwrap(); - let sum = list - .iter() - .try_fold(0, |acc, v| PyResult::Ok(acc + v.extract::()?)) - .unwrap(); - assert_eq!(sum, 6); - - let list = PyList::new(py, ["foo", "bar"]).unwrap(); - assert!(list - .iter() - .try_fold(0, |acc, v| PyResult::Ok(acc + v.extract::()?)) - .is_err()); - }); - } - - #[test] - fn test_iter_try_rfold() { - Python::with_gil(|py: Python<'_>| { - let list = PyList::new(py, [1, 2, 3]).unwrap(); - let sum = list - .iter() - .try_rfold(0, |acc, v| PyResult::Ok(acc + v.extract::()?)) - .unwrap(); - assert_eq!(sum, 6); - - let list = PyList::new(py, ["foo", "bar"]).unwrap(); - assert!(list - .iter() - .try_rfold(0, |acc, v| PyResult::Ok(acc + v.extract::()?)) - .is_err()); - }); - } - - #[test] - fn test_into_iter() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - for (i, item) in list.iter().enumerate() { - assert_eq!((i + 1) as i32, item.extract::().unwrap()); - } - }); - } - - #[test] - fn test_into_iter_bound() { - use crate::types::any::PyAnyMethods; - - Python::with_gil(|py| { - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - let mut items = vec![]; - for item in &list { - items.push(item.extract::().unwrap()); - } - assert_eq!(items, vec![1, 2, 3, 4]); - }); - } - - #[test] - fn test_as_sequence() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - - assert_eq!(list.as_sequence().len().unwrap(), 4); - assert_eq!( - list.as_sequence() - .get_item(1) - .unwrap() - .extract::() - .unwrap(), - 2 - ); - }); - } - - #[test] - fn test_into_sequence() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 2, 3, 4]).unwrap(); - - let sequence = list.into_sequence(); - - assert_eq!(sequence.len().unwrap(), 4); - assert_eq!(sequence.get_item(1).unwrap().extract::().unwrap(), 2); - }); - } - - #[test] - fn test_extract() { - Python::with_gil(|py| { - let v = vec![2, 3, 5, 7]; - let list = PyList::new(py, &v).unwrap(); - let v2 = list.as_ref().extract::>().unwrap(); - assert_eq!(v, v2); - }); - } - - #[test] - fn test_sort() { - Python::with_gil(|py| { - let v = vec![7, 3, 2, 5]; - let list = PyList::new(py, &v).unwrap(); - assert_eq!(7, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(2, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(3).unwrap().extract::().unwrap()); - list.sort().unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(7, list.get_item(3).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_reverse() { - Python::with_gil(|py| { - let v = vec![2, 3, 5, 7]; - let list = PyList::new(py, &v).unwrap(); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(7, list.get_item(3).unwrap().extract::().unwrap()); - list.reverse().unwrap(); - assert_eq!(7, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(5, list.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(3, list.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(2, list.get_item(3).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_array_into_pyobject() { - Python::with_gil(|py| { - let array = [1, 2].into_pyobject(py).unwrap(); - let list = array.downcast::().unwrap(); - assert_eq!(1, list.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(2, list.get_item(1).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_list_get_item_invalid_index() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let obj = list.get_item(5); - assert!(obj.is_err()); - assert_eq!( - obj.unwrap_err().to_string(), - "IndexError: list index out of range" - ); - }); - } - - #[test] - fn test_list_get_item_sanity() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let obj = list.get_item(0); - assert_eq!(obj.unwrap().extract::().unwrap(), 2); - }); - } - - #[cfg(not(Py_LIMITED_API))] - #[test] - fn test_list_get_item_unchecked_sanity() { - Python::with_gil(|py| { - let list = PyList::new(py, [2, 3, 5, 7]).unwrap(); - let obj = unsafe { list.get_item_unchecked(0) }; - assert_eq!(obj.extract::().unwrap(), 2); - }); - } - - #[test] - fn test_list_del_item() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 1, 2, 3, 5, 8]).unwrap(); - assert!(list.del_item(10).is_err()); - assert_eq!(1, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(1, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(2, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(3, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(5, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(8, list.get_item(0).unwrap().extract::().unwrap()); - assert!(list.del_item(0).is_ok()); - assert_eq!(0, list.len()); - assert!(list.del_item(0).is_err()); - }); - } - - #[test] - fn test_list_set_slice() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 1, 2, 3, 5, 8]).unwrap(); - let ins = PyList::new(py, [7, 4]).unwrap(); - list.set_slice(1, 4, &ins).unwrap(); - assert_eq!([1, 7, 4, 5, 8], list.extract::<[i32; 5]>().unwrap()); - list.set_slice(3, 100, &PyList::empty(py)).unwrap(); - assert_eq!([1, 7, 4], list.extract::<[i32; 3]>().unwrap()); - }); - } - - #[test] - fn test_list_del_slice() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 1, 2, 3, 5, 8]).unwrap(); - list.del_slice(1, 4).unwrap(); - assert_eq!([1, 5, 8], list.extract::<[i32; 3]>().unwrap()); - list.del_slice(1, 100).unwrap(); - assert_eq!([1], list.extract::<[i32; 1]>().unwrap()); - }); - } - - #[test] - fn test_list_contains() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 1, 2, 3, 5, 8]).unwrap(); - assert_eq!(6, list.len()); - - let bad_needle = 7i32.into_pyobject(py).unwrap(); - assert!(!list.contains(&bad_needle).unwrap()); - - let good_needle = 8i32.into_pyobject(py).unwrap(); - assert!(list.contains(&good_needle).unwrap()); - - let type_coerced_needle = 8f32.into_pyobject(py).unwrap(); - assert!(list.contains(&type_coerced_needle).unwrap()); - }); - } - - #[test] - fn test_list_index() { - Python::with_gil(|py| { - let list = PyList::new(py, [1, 1, 2, 3, 5, 8]).unwrap(); - assert_eq!(0, list.index(1i32).unwrap()); - assert_eq!(2, list.index(2i32).unwrap()); - assert_eq!(3, list.index(3i32).unwrap()); - assert_eq!(4, list.index(5i32).unwrap()); - assert_eq!(5, list.index(8i32).unwrap()); - assert!(list.index(42i32).is_err()); - }); - } - - use std::ops::Range; - - // An iterator that lies about its `ExactSizeIterator` implementation. - // See https://github.com/PyO3/pyo3/issues/2118 - struct FaultyIter(Range, usize); - - impl Iterator for FaultyIter { - type Item = usize; - - fn next(&mut self) -> Option { - self.0.next() - } - } - - impl ExactSizeIterator for FaultyIter { - fn len(&self) -> usize { - self.1 - } - } - - #[test] - #[should_panic( - expected = "Attempted to create PyList but `elements` was larger than reported by its `ExactSizeIterator` implementation." - )] - fn too_long_iterator() { - Python::with_gil(|py| { - let iter = FaultyIter(0..usize::MAX, 73); - let _list = PyList::new(py, iter).unwrap(); - }) - } - - #[test] - #[should_panic( - expected = "Attempted to create PyList but `elements` was smaller than reported by its `ExactSizeIterator` implementation." - )] - fn too_short_iterator() { - Python::with_gil(|py| { - let iter = FaultyIter(0..35, 73); - let _list = PyList::new(py, iter).unwrap(); - }) - } - - #[test] - #[should_panic( - expected = "out of range integral type conversion attempted on `elements.len()`" - )] - fn overflowing_size() { - Python::with_gil(|py| { - let iter = FaultyIter(0..0, usize::MAX); - - let _list = PyList::new(py, iter).unwrap(); - }) - } - - #[test] - fn bad_intopyobject_doesnt_cause_leaks() { - use crate::types::PyInt; - use std::convert::Infallible; - use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; - static NEEDS_DESTRUCTING_COUNT: AtomicUsize = AtomicUsize::new(0); - - struct Bad(usize); - - impl Drop for Bad { - fn drop(&mut self) { - NEEDS_DESTRUCTING_COUNT.fetch_sub(1, SeqCst); - } - } - - impl<'py> IntoPyObject<'py> for Bad { - type Target = PyInt; - type Output = crate::Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - // This panic should not lead to a memory leak - assert_ne!(self.0, 42); - self.0.into_pyobject(py) - } - } - - struct FaultyIter(Range, usize); - - impl Iterator for FaultyIter { - type Item = Bad; - - fn next(&mut self) -> Option { - self.0.next().map(|i| { - NEEDS_DESTRUCTING_COUNT.fetch_add(1, SeqCst); - Bad(i) - }) - } - } - - impl ExactSizeIterator for FaultyIter { - fn len(&self) -> usize { - self.1 - } - } - - Python::with_gil(|py| { - std::panic::catch_unwind(|| { - let iter = FaultyIter(0..50, 50); - let _list = PyList::new(py, iter).unwrap(); - }) - .unwrap_err(); - }); - - assert_eq!( - NEEDS_DESTRUCTING_COUNT.load(SeqCst), - 0, - "Some destructors did not run" - ); - } - - #[test] - fn test_list_to_tuple() { - Python::with_gil(|py| { - let list = PyList::new(py, vec![1, 2, 3]).unwrap(); - let tuple = list.to_tuple(); - let tuple_expected = PyTuple::new(py, vec![1, 2, 3]).unwrap(); - assert!(tuple.eq(tuple_expected).unwrap()); - }) - } -} diff --git a/vendor/pyo3/src/types/mapping.rs b/vendor/pyo3/src/types/mapping.rs deleted file mode 100644 index baf3a023ce..0000000000 --- a/vendor/pyo3/src/types/mapping.rs +++ /dev/null @@ -1,338 +0,0 @@ -use crate::conversion::IntoPyObject; -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Bound; -use crate::py_result_ext::PyResultExt; -use crate::sync::GILOnceCell; -use crate::type_object::PyTypeInfo; -use crate::types::any::PyAnyMethods; -use crate::types::{PyAny, PyDict, PyList, PyType}; -use crate::{ffi, Py, PyTypeCheck, Python}; - -/// Represents a reference to a Python object supporting the mapping protocol. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyMapping>`][Bound]. -/// -/// For APIs available on mapping objects, see the [`PyMappingMethods`] trait which is implemented for -/// [`Bound<'py, PyMapping>`][Bound]. -#[repr(transparent)] -pub struct PyMapping(PyAny); -pyobject_native_type_named!(PyMapping); - -impl PyMapping { - /// Register a pyclass as a subclass of `collections.abc.Mapping` (from the Python standard - /// library). This is equivalent to `collections.abc.Mapping.register(T)` in Python. - /// This registration is required for a pyclass to be downcastable from `PyAny` to `PyMapping`. - pub fn register(py: Python<'_>) -> PyResult<()> { - let ty = T::type_object(py); - get_mapping_abc(py)?.call_method1("register", (ty,))?; - Ok(()) - } -} - -/// Implementation of functionality for [`PyMapping`]. -/// -/// These methods are defined for the `Bound<'py, PyMapping>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyMapping")] -pub trait PyMappingMethods<'py>: crate::sealed::Sealed { - /// Returns the number of objects in the mapping. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> PyResult; - - /// Returns whether the mapping is empty. - fn is_empty(&self) -> PyResult; - - /// Determines if the mapping contains the specified key. - /// - /// This is equivalent to the Python expression `key in self`. - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>; - - /// Gets the item in self with key `key`. - /// - /// Returns an `Err` if the item with specified key is not found, usually `KeyError`. - /// - /// This is equivalent to the Python expression `self[key]`. - fn get_item(&self, key: K) -> PyResult> - where - K: IntoPyObject<'py>; - - /// Sets the item in self with key `key`. - /// - /// This is equivalent to the Python expression `self[key] = value`. - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>; - - /// Deletes the item with key `key`. - /// - /// This is equivalent to the Python statement `del self[key]`. - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>; - - /// Returns a list containing all keys in the mapping. - fn keys(&self) -> PyResult>; - - /// Returns a list containing all values in the mapping. - fn values(&self) -> PyResult>; - - /// Returns a list of all (key, value) pairs in the mapping. - fn items(&self) -> PyResult>; -} - -impl<'py> PyMappingMethods<'py> for Bound<'py, PyMapping> { - #[inline] - fn len(&self) -> PyResult { - let v = unsafe { ffi::PyMapping_Size(self.as_ptr()) }; - crate::err::error_on_minusone(self.py(), v)?; - Ok(v as usize) - } - - #[inline] - fn is_empty(&self) -> PyResult { - self.len().map(|l| l == 0) - } - - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>, - { - PyAnyMethods::contains(&**self, key) - } - - #[inline] - fn get_item(&self, key: K) -> PyResult> - where - K: IntoPyObject<'py>, - { - PyAnyMethods::get_item(&**self, key) - } - - #[inline] - fn set_item(&self, key: K, value: V) -> PyResult<()> - where - K: IntoPyObject<'py>, - V: IntoPyObject<'py>, - { - PyAnyMethods::set_item(&**self, key, value) - } - - #[inline] - fn del_item(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>, - { - PyAnyMethods::del_item(&**self, key) - } - - #[inline] - fn keys(&self) -> PyResult> { - unsafe { - ffi::PyMapping_Keys(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn values(&self) -> PyResult> { - unsafe { - ffi::PyMapping_Values(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn items(&self) -> PyResult> { - unsafe { - ffi::PyMapping_Items(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } -} - -fn get_mapping_abc(py: Python<'_>) -> PyResult<&Bound<'_, PyType>> { - static MAPPING_ABC: GILOnceCell> = GILOnceCell::new(); - - MAPPING_ABC.import(py, "collections.abc", "Mapping") -} - -impl PyTypeCheck for PyMapping { - const NAME: &'static str = "Mapping"; - - #[inline] - fn type_check(object: &Bound<'_, PyAny>) -> bool { - // Using `is_instance` for `collections.abc.Mapping` is slow, so provide - // optimized case dict as a well-known mapping - PyDict::is_type_of(object) - || get_mapping_abc(object.py()) - .and_then(|abc| object.is_instance(abc)) - .unwrap_or_else(|err| { - err.write_unraisable(object.py(), Some(object)); - false - }) - } -} - -#[cfg(test)] -mod tests { - use std::collections::HashMap; - - use crate::{exceptions::PyKeyError, types::PyTuple}; - - use super::*; - use crate::conversion::IntoPyObject; - - #[test] - fn test_len() { - Python::with_gil(|py| { - let mut v = HashMap::::new(); - let ob = (&v).into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - assert_eq!(0, mapping.len().unwrap()); - assert!(mapping.is_empty().unwrap()); - - v.insert(7, 32); - let ob = v.into_pyobject(py).unwrap(); - let mapping2 = ob.downcast::().unwrap(); - assert_eq!(1, mapping2.len().unwrap()); - assert!(!mapping2.is_empty().unwrap()); - }); - } - - #[test] - fn test_contains() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert("key0", 1234); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - mapping.set_item("key1", "foo").unwrap(); - - assert!(mapping.contains("key0").unwrap()); - assert!(mapping.contains("key1").unwrap()); - assert!(!mapping.contains("key2").unwrap()); - }); - } - - #[test] - fn test_get_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - assert_eq!( - 32, - mapping.get_item(7i32).unwrap().extract::().unwrap() - ); - assert!(mapping - .get_item(8i32) - .unwrap_err() - .is_instance_of::(py)); - }); - } - - #[test] - fn test_set_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - assert!(mapping.set_item(7i32, 42i32).is_ok()); // change - assert!(mapping.set_item(8i32, 123i32).is_ok()); // insert - assert_eq!( - 42i32, - mapping.get_item(7i32).unwrap().extract::().unwrap() - ); - assert_eq!( - 123i32, - mapping.get_item(8i32).unwrap().extract::().unwrap() - ); - }); - } - - #[test] - fn test_del_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - assert!(mapping.del_item(7i32).is_ok()); - assert_eq!(0, mapping.len().unwrap()); - assert!(mapping - .get_item(7i32) - .unwrap_err() - .is_instance_of::(py)); - }); - } - - #[test] - fn test_items() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - let mut value_sum = 0; - for el in mapping.items().unwrap().try_iter().unwrap() { - let tuple = el.unwrap().downcast_into::().unwrap(); - key_sum += tuple.get_item(0).unwrap().extract::().unwrap(); - value_sum += tuple.get_item(1).unwrap().extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_keys() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - for el in mapping.keys().unwrap().try_iter().unwrap() { - key_sum += el.unwrap().extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - }); - } - - #[test] - fn test_values() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let ob = v.into_pyobject(py).unwrap(); - let mapping = ob.downcast::().unwrap(); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut values_sum = 0; - for el in mapping.values().unwrap().try_iter().unwrap() { - values_sum += el.unwrap().extract::().unwrap(); - } - assert_eq!(32 + 42 + 123, values_sum); - }); - } -} diff --git a/vendor/pyo3/src/types/mappingproxy.rs b/vendor/pyo3/src/types/mappingproxy.rs deleted file mode 100644 index fc28687c56..0000000000 --- a/vendor/pyo3/src/types/mappingproxy.rs +++ /dev/null @@ -1,557 +0,0 @@ -// Copyright (c) 2017-present PyO3 Project and Contributors - -use super::PyMapping; -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Bound; -use crate::types::any::PyAnyMethods; -use crate::types::{PyAny, PyIterator, PyList}; -use crate::{ffi, Python}; - -use std::os::raw::c_int; - -/// Represents a Python `mappingproxy`. -#[repr(transparent)] -pub struct PyMappingProxy(PyAny); - -#[inline] -unsafe fn dict_proxy_check(op: *mut ffi::PyObject) -> c_int { - ffi::Py_IS_TYPE(op, std::ptr::addr_of_mut!(ffi::PyDictProxy_Type)) -} - -pyobject_native_type_core!( - PyMappingProxy, - pyobject_native_static_type_object!(ffi::PyDictProxy_Type), - #checkfunction=dict_proxy_check -); - -impl PyMappingProxy { - /// Creates a mappingproxy from an object. - pub fn new<'py>( - py: Python<'py>, - elements: &Bound<'py, PyMapping>, - ) -> Bound<'py, PyMappingProxy> { - unsafe { - ffi::PyDictProxy_New(elements.as_ptr()) - .assume_owned(py) - .downcast_into_unchecked() - } - } -} - -/// Implementation of functionality for [`PyMappingProxy`]. -/// -/// These methods are defined for the `Bound<'py, PyMappingProxy>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyMappingProxy")] -pub trait PyMappingProxyMethods<'py, 'a>: crate::sealed::Sealed { - /// Checks if the mappingproxy is empty, i.e. `len(self) == 0`. - fn is_empty(&self) -> PyResult; - - /// Returns a list containing all keys in the mapping. - fn keys(&self) -> PyResult>; - - /// Returns a list containing all values in the mapping. - fn values(&self) -> PyResult>; - - /// Returns a list of tuples of all (key, value) pairs in the mapping. - fn items(&self) -> PyResult>; - - /// Returns `self` cast as a `PyMapping`. - fn as_mapping(&self) -> &Bound<'py, PyMapping>; - - /// Takes an object and returns an iterator for it. Returns an error if the object is not - /// iterable. - fn try_iter(&'a self) -> PyResult>; -} - -impl<'py, 'a> PyMappingProxyMethods<'py, 'a> for Bound<'py, PyMappingProxy> { - fn is_empty(&self) -> PyResult { - Ok(self.len()? == 0) - } - - #[inline] - fn keys(&self) -> PyResult> { - unsafe { - Ok(ffi::PyMapping_Keys(self.as_ptr()) - .assume_owned_or_err(self.py())? - .downcast_into_unchecked()) - } - } - - #[inline] - fn values(&self) -> PyResult> { - unsafe { - Ok(ffi::PyMapping_Values(self.as_ptr()) - .assume_owned_or_err(self.py())? - .downcast_into_unchecked()) - } - } - - #[inline] - fn items(&self) -> PyResult> { - unsafe { - Ok(ffi::PyMapping_Items(self.as_ptr()) - .assume_owned_or_err(self.py())? - .downcast_into_unchecked()) - } - } - - fn as_mapping(&self) -> &Bound<'py, PyMapping> { - unsafe { self.downcast_unchecked() } - } - - fn try_iter(&'a self) -> PyResult> { - Ok(BoundMappingProxyIterator { - iterator: PyIterator::from_object(self)?, - mappingproxy: self, - }) - } -} - -pub struct BoundMappingProxyIterator<'py, 'a> { - iterator: Bound<'py, PyIterator>, - mappingproxy: &'a Bound<'py, PyMappingProxy>, -} - -impl<'py> Iterator for BoundMappingProxyIterator<'py, '_> { - type Item = PyResult<(Bound<'py, PyAny>, Bound<'py, PyAny>)>; - - #[inline] - fn next(&mut self) -> Option { - self.iterator.next().map(|key| match key { - Ok(key) => match self.mappingproxy.get_item(&key) { - Ok(value) => Ok((key, value)), - Err(e) => Err(e), - }, - Err(e) => Err(e), - }) - } -} - -#[cfg(test)] -mod tests { - - use super::*; - use crate::types::dict::*; - use crate::Python; - use crate::{ - exceptions::PyKeyError, - types::{PyInt, PyTuple}, - }; - use std::collections::{BTreeMap, HashMap}; - - #[test] - fn test_new() { - Python::with_gil(|py| { - let pydict = [(7, 32)].into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, pydict.as_mapping()); - mappingproxy.get_item(7i32).unwrap(); - assert_eq!( - 32, - mappingproxy - .get_item(7i32) - .unwrap() - .extract::() - .unwrap() - ); - assert!(mappingproxy - .get_item(8i32) - .unwrap_err() - .is_instance_of::(py)); - }); - } - - #[test] - fn test_len() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - let dict = v.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - assert_eq!(mappingproxy.len().unwrap(), 0); - v.insert(7, 32); - let dict2 = v.clone().into_py_dict(py).unwrap(); - let mp2 = PyMappingProxy::new(py, dict2.as_mapping()); - assert_eq!(mp2.len().unwrap(), 1); - }); - } - - #[test] - fn test_contains() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - assert!(mappingproxy.contains(7i32).unwrap()); - assert!(!mappingproxy.contains(8i32).unwrap()); - }); - } - - #[test] - fn test_get_item() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - let dict = v.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - assert_eq!( - 32, - mappingproxy - .get_item(7i32) - .unwrap() - .extract::() - .unwrap() - ); - assert!(mappingproxy - .get_item(8i32) - .unwrap_err() - .is_instance_of::(py)); - }); - } - - #[test] - fn test_set_item_refcnt() { - Python::with_gil(|py| { - let cnt; - { - let none = py.None(); - cnt = none.get_refcnt(py); - let dict = [(10, none)].into_py_dict(py).unwrap(); - let _mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - } - { - assert_eq!(cnt, py.None().get_refcnt(py)); - } - }); - } - - #[test] - fn test_isempty() { - Python::with_gil(|py| { - let map: HashMap = HashMap::new(); - let dict = map.into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - assert!(mappingproxy.is_empty().unwrap()); - }); - } - - #[test] - fn test_keys() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - for el in mappingproxy.keys().unwrap().try_iter().unwrap() { - key_sum += el.unwrap().extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - }); - } - - #[test] - fn test_values() { - Python::with_gil(|py| { - let mut v: HashMap = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut values_sum = 0; - for el in mappingproxy.values().unwrap().try_iter().unwrap() { - values_sum += el.unwrap().extract::().unwrap(); - } - assert_eq!(32 + 42 + 123, values_sum); - }); - } - - #[test] - fn test_items() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - // Can't just compare against a vector of tuples since we don't have a guaranteed ordering. - let mut key_sum = 0; - let mut value_sum = 0; - for res in mappingproxy.items().unwrap().try_iter().unwrap() { - let el = res.unwrap(); - let tuple = el.downcast::().unwrap(); - key_sum += tuple.get_item(0).unwrap().extract::().unwrap(); - value_sum += tuple.get_item(1).unwrap().extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_iter() { - Python::with_gil(|py| { - let mut v = HashMap::new(); - v.insert(7, 32); - v.insert(8, 42); - v.insert(9, 123); - let dict = v.into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - let mut key_sum = 0; - let mut value_sum = 0; - for res in mappingproxy.try_iter().unwrap() { - let (key, value) = res.unwrap(); - key_sum += key.extract::().unwrap(); - value_sum += value.extract::().unwrap(); - } - assert_eq!(7 + 8 + 9, key_sum); - assert_eq!(32 + 42 + 123, value_sum); - }); - } - - #[test] - fn test_hashmap_into_python() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let dict = map.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 1); - assert_eq!(py_map.get_item(1).unwrap().extract::().unwrap(), 1); - }); - } - - #[test] - fn test_hashmap_into_mappingproxy() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let dict = map.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 1); - assert_eq!(py_map.get_item(1).unwrap().extract::().unwrap(), 1); - }); - } - - #[test] - fn test_btreemap_into_py() { - Python::with_gil(|py| { - let mut map = BTreeMap::::new(); - map.insert(1, 1); - - let dict = map.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 1); - assert_eq!(py_map.get_item(1).unwrap().extract::().unwrap(), 1); - }); - } - - #[test] - fn test_btreemap_into_mappingproxy() { - Python::with_gil(|py| { - let mut map = BTreeMap::::new(); - map.insert(1, 1); - - let dict = map.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 1); - assert_eq!(py_map.get_item(1).unwrap().extract::().unwrap(), 1); - }); - } - - #[test] - fn test_vec_into_mappingproxy() { - Python::with_gil(|py| { - let vec = vec![("a", 1), ("b", 2), ("c", 3)]; - let dict = vec.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 3); - assert_eq!(py_map.get_item("b").unwrap().extract::().unwrap(), 2); - }); - } - - #[test] - fn test_slice_into_mappingproxy() { - Python::with_gil(|py| { - let arr = [("a", 1), ("b", 2), ("c", 3)]; - - let dict = arr.into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.len().unwrap(), 3); - assert_eq!(py_map.get_item("b").unwrap().extract::().unwrap(), 2); - }); - } - - #[test] - fn mappingproxy_as_mapping() { - Python::with_gil(|py| { - let mut map = HashMap::::new(); - map.insert(1, 1); - - let dict = map.clone().into_py_dict(py).unwrap(); - let py_map = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!(py_map.as_mapping().len().unwrap(), 1); - assert_eq!( - py_map - .as_mapping() - .get_item(1) - .unwrap() - .extract::() - .unwrap(), - 1 - ); - }); - } - - #[cfg(not(PyPy))] - fn abc_mappingproxy(py: Python<'_>) -> Bound<'_, PyMappingProxy> { - let mut map = HashMap::<&'static str, i32>::new(); - map.insert("a", 1); - map.insert("b", 2); - map.insert("c", 3); - let dict = map.clone().into_py_dict(py).unwrap(); - PyMappingProxy::new(py, dict.as_mapping()) - } - - #[test] - #[cfg(not(PyPy))] - fn mappingproxy_keys_view() { - Python::with_gil(|py| { - let mappingproxy = abc_mappingproxy(py); - let keys = mappingproxy.call_method0("keys").unwrap(); - assert!(keys.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - #[cfg(not(PyPy))] - fn mappingproxy_values_view() { - Python::with_gil(|py| { - let mappingproxy = abc_mappingproxy(py); - let values = mappingproxy.call_method0("values").unwrap(); - assert!(values.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - #[cfg(not(PyPy))] - fn mappingproxy_items_view() { - Python::with_gil(|py| { - let mappingproxy = abc_mappingproxy(py); - let items = mappingproxy.call_method0("items").unwrap(); - assert!(items.is_instance(&py.get_type::()).unwrap()); - }) - } - - #[test] - fn get_value_from_mappingproxy_of_strings() { - Python::with_gil(|py: Python<'_>| { - let mut map = HashMap::new(); - map.insert("first key".to_string(), "first value".to_string()); - map.insert("second key".to_string(), "second value".to_string()); - map.insert("third key".to_string(), "third value".to_string()); - - let dict = map.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!( - map.into_iter().collect::>(), - mappingproxy - .try_iter() - .unwrap() - .map(|object| { - let tuple = object.unwrap(); - ( - tuple.0.extract::().unwrap(), - tuple.1.extract::().unwrap(), - ) - }) - .collect::>() - ); - }) - } - - #[test] - fn get_value_from_mappingproxy_of_integers() { - Python::with_gil(|py: Python<'_>| { - const LEN: usize = 10_000; - let items: Vec<(usize, usize)> = (1..LEN).map(|i| (i, i - 1)).collect(); - - let dict = items.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - - assert_eq!( - items, - mappingproxy - .clone() - .try_iter() - .unwrap() - .map(|object| { - let tuple = object.unwrap(); - ( - tuple - .0 - .downcast::() - .unwrap() - .extract::() - .unwrap(), - tuple - .1 - .downcast::() - .unwrap() - .extract::() - .unwrap(), - ) - }) - .collect::>() - ); - for index in 1..LEN { - assert_eq!( - mappingproxy - .clone() - .get_item(index) - .unwrap() - .extract::() - .unwrap(), - index - 1 - ); - } - }) - } - - #[test] - fn iter_mappingproxy_nosegv() { - Python::with_gil(|py| { - const LEN: usize = 10_000_000; - let items = (0..LEN as u64).map(|i| (i, i * 2)); - - let dict = items.clone().into_py_dict(py).unwrap(); - let mappingproxy = PyMappingProxy::new(py, dict.as_mapping()); - - let mut sum = 0; - for result in mappingproxy.try_iter().unwrap() { - let (k, _v) = result.unwrap(); - let i: u64 = k.extract().unwrap(); - sum += i; - } - assert_eq!(sum, 49_999_995_000_000); - }) - } -} diff --git a/vendor/pyo3/src/types/memoryview.rs b/vendor/pyo3/src/types/memoryview.rs deleted file mode 100644 index 81acc5cbb2..0000000000 --- a/vendor/pyo3/src/types/memoryview.rs +++ /dev/null @@ -1,42 +0,0 @@ -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::{ffi, Bound, PyAny}; - -/// Represents a Python `memoryview`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyMemoryView>`][Bound]. -#[repr(transparent)] -pub struct PyMemoryView(PyAny); - -pyobject_native_type_core!(PyMemoryView, pyobject_native_static_type_object!(ffi::PyMemoryView_Type), #checkfunction=ffi::PyMemoryView_Check); - -impl PyMemoryView { - /// Creates a new Python `memoryview` object from another Python object that - /// implements the buffer protocol. - pub fn from<'py>(src: &Bound<'py, PyAny>) -> PyResult> { - unsafe { - ffi::PyMemoryView_FromObject(src.as_ptr()) - .assume_owned_or_err(src.py()) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyMemoryView::from`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyMemoryView::from`")] - #[inline] - pub fn from_bound<'py>(src: &Bound<'py, PyAny>) -> PyResult> { - Self::from(src) - } -} - -impl<'py> TryFrom<&Bound<'py, PyAny>> for Bound<'py, PyMemoryView> { - type Error = crate::PyErr; - - /// Creates a new Python `memoryview` object from another Python object that - /// implements the buffer protocol. - fn try_from(value: &Bound<'py, PyAny>) -> Result { - PyMemoryView::from(value) - } -} diff --git a/vendor/pyo3/src/types/mod.rs b/vendor/pyo3/src/types/mod.rs deleted file mode 100644 index 39ab3fd501..0000000000 --- a/vendor/pyo3/src/types/mod.rs +++ /dev/null @@ -1,267 +0,0 @@ -//! Various types defined by the Python interpreter such as `int`, `str` and `tuple`. - -pub use self::any::{PyAny, PyAnyMethods}; -pub use self::boolobject::{PyBool, PyBoolMethods}; -pub use self::bytearray::{PyByteArray, PyByteArrayMethods}; -pub use self::bytes::{PyBytes, PyBytesMethods}; -pub use self::capsule::{PyCapsule, PyCapsuleMethods}; -#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))] -pub use self::code::PyCode; -pub use self::complex::{PyComplex, PyComplexMethods}; -#[cfg(not(Py_LIMITED_API))] -#[allow(deprecated)] -pub use self::datetime::{ - timezone_utc, timezone_utc_bound, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, - PyTime, PyTimeAccess, PyTzInfo, PyTzInfoAccess, -}; -pub use self::dict::{IntoPyDict, PyDict, PyDictMethods}; -#[cfg(not(any(PyPy, GraalPy)))] -pub use self::dict::{PyDictItems, PyDictKeys, PyDictValues}; -pub use self::ellipsis::PyEllipsis; -pub use self::float::{PyFloat, PyFloatMethods}; -#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))] -pub use self::frame::PyFrame; -pub use self::frozenset::{PyFrozenSet, PyFrozenSetBuilder, PyFrozenSetMethods}; -pub use self::function::PyCFunction; -#[cfg(all(not(Py_LIMITED_API), not(all(PyPy, not(Py_3_8)))))] -pub use self::function::PyFunction; -pub use self::iterator::PyIterator; -pub use self::list::{PyList, PyListMethods}; -pub use self::mapping::{PyMapping, PyMappingMethods}; -pub use self::mappingproxy::PyMappingProxy; -pub use self::memoryview::PyMemoryView; -pub use self::module::{PyModule, PyModuleMethods}; -pub use self::none::PyNone; -pub use self::notimplemented::PyNotImplemented; -#[allow(deprecated)] -pub use self::num::{PyInt, PyLong}; -#[cfg(not(any(PyPy, GraalPy)))] -pub use self::pysuper::PySuper; -pub use self::sequence::{PySequence, PySequenceMethods}; -pub use self::set::{PySet, PySetMethods}; -pub use self::slice::{PySlice, PySliceIndices, PySliceMethods}; -#[cfg(not(Py_LIMITED_API))] -pub use self::string::PyStringData; -#[allow(deprecated)] -pub use self::string::{PyString, PyStringMethods, PyUnicode}; -pub use self::traceback::{PyTraceback, PyTracebackMethods}; -pub use self::tuple::{PyTuple, PyTupleMethods}; -pub use self::typeobject::{PyType, PyTypeMethods}; -pub use self::weakref::{PyWeakref, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference}; - -/// Iteration over Python collections. -/// -/// When working with a Python collection, one approach is to convert it to a Rust collection such -/// as `Vec` or `HashMap`. However this is a relatively expensive operation. If you just want to -/// visit all their items, consider iterating over the collections directly: -/// -/// # Examples -/// -/// ```rust -/// use pyo3::prelude::*; -/// use pyo3::types::PyDict; -/// use pyo3::ffi::c_str; -/// -/// # pub fn main() -> PyResult<()> { -/// Python::with_gil(|py| { -/// let dict = py.eval(c_str!("{'a':'b', 'c':'d'}"), None, None)?.downcast_into::()?; -/// -/// for (key, value) in &dict { -/// println!("key: {}, value: {}", key, value); -/// } -/// -/// Ok(()) -/// }) -/// # } -/// ``` -/// -/// If PyO3 detects that the collection is mutated during iteration, it will panic. -/// -/// These iterators use Python's C-API directly. However in certain cases, like when compiling for -/// the Limited API and PyPy, the underlying structures are opaque and that may not be possible. -/// In these cases the iterators are implemented by forwarding to [`PyIterator`]. -pub mod iter { - pub use super::dict::BoundDictIterator; - pub use super::frozenset::BoundFrozenSetIterator; - pub use super::list::BoundListIterator; - pub use super::set::BoundSetIterator; - pub use super::tuple::{BorrowedTupleIterator, BoundTupleIterator}; -} - -/// Python objects that have a base type. -/// -/// This marks types that can be upcast into a [`PyAny`] and used in its place. -/// This essentially includes every Python object except [`PyAny`] itself. -/// -/// This is used to provide the [`Deref>`](std::ops::Deref) -/// implementations for [`Bound<'_, T>`](crate::Bound). -/// -/// Users should not need to implement this trait directly. It's implementation -/// is provided by the [`#[pyclass]`](macro@crate::pyclass) attribute. -/// -/// ## Note -/// This is needed because the compiler currently tries to figure out all the -/// types in a deref-chain before starting to look for applicable method calls. -/// So we need to prevent [`Bound<'_, PyAny`](crate::Bound) dereferencing to -/// itself in order to avoid running into the recursion limit. This trait is -/// used to exclude this from our blanket implementation. See [this Rust -/// issue][1] for more details. If the compiler limitation gets resolved, this -/// trait will be removed. -/// -/// [1]: https://github.com/rust-lang/rust/issues/19509 -pub trait DerefToPyAny { - // Empty. -} - -// Implementations core to all native types except for PyAny (because they don't -// make sense on PyAny / have different implementations). -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_type_named ( - ($name:ty $(;$generics:ident)*) => { - impl<$($generics,)*> ::std::convert::AsRef<$crate::PyAny> for $name { - #[inline] - fn as_ref(&self) -> &$crate::PyAny { - &self.0 - } - } - - impl<$($generics,)*> ::std::ops::Deref for $name { - type Target = $crate::PyAny; - - #[inline] - fn deref(&self) -> &$crate::PyAny { - &self.0 - } - } - - impl $crate::types::DerefToPyAny for $name {} - }; -); - -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_static_type_object( - ($typeobject:expr) => { - |_py| { - #[allow(unused_unsafe)] // https://github.com/rust-lang/rust/pull/125834 - unsafe { ::std::ptr::addr_of_mut!($typeobject) } - } - }; -); - -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_type_info( - ($name:ty, $typeobject:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => { - unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name { - const NAME: &'static str = stringify!($name); - const MODULE: ::std::option::Option<&'static str> = $module; - - #[inline] - #[allow(clippy::redundant_closure_call)] - fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject { - $typeobject(py) - } - - $( - #[inline] - fn is_type_of_bound(obj: &$crate::Bound<'_, $crate::PyAny>) -> bool { - #[allow(unused_unsafe)] - unsafe { $checkfunction(obj.as_ptr()) > 0 } - } - )? - } - - impl $name { - #[doc(hidden)] - pub const _PYO3_DEF: $crate::impl_::pymodule::AddTypeToModule = $crate::impl_::pymodule::AddTypeToModule::new(); - } - }; -); - -/// Declares all of the boilerplate for Python types. -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_type_core { - ($name:ty, $typeobject:expr, #module=$module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => { - $crate::pyobject_native_type_named!($name $(;$generics)*); - $crate::pyobject_native_type_info!($name, $typeobject, $module $(, #checkfunction=$checkfunction)? $(;$generics)*); - }; - ($name:ty, $typeobject:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => { - $crate::pyobject_native_type_core!($name, $typeobject, #module=::std::option::Option::Some("builtins") $(, #checkfunction=$checkfunction)? $(;$generics)*); - }; -} - -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_subclassable_native_type { - ($name:ty, $layout:path $(;$generics:ident)*) => { - #[cfg(not(Py_LIMITED_API))] - impl<$($generics,)*> $crate::impl_::pyclass::PyClassBaseType for $name { - type LayoutAsBase = $crate::impl_::pycell::PyClassObjectBase<$layout>; - type BaseNativeType = $name; - type Initializer = $crate::impl_::pyclass_init::PyNativeTypeInitializer; - type PyClassMutability = $crate::pycell::impl_::ImmutableClass; - } - } -} - -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_type_sized { - ($name:ty, $layout:path $(;$generics:ident)*) => { - unsafe impl $crate::type_object::PyLayout<$name> for $layout {} - impl $crate::type_object::PySizedLayout<$name> for $layout {} - }; -} - -/// Declares all of the boilerplate for Python types which can be inherited from (because the exact -/// Python layout is known). -#[doc(hidden)] -#[macro_export] -macro_rules! pyobject_native_type { - ($name:ty, $layout:path, $typeobject:expr $(, #module=$module:expr)? $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => { - $crate::pyobject_native_type_core!($name, $typeobject $(, #module=$module)? $(, #checkfunction=$checkfunction)? $(;$generics)*); - // To prevent inheriting native types with ABI3 - #[cfg(not(Py_LIMITED_API))] - $crate::pyobject_native_type_sized!($name, $layout $(;$generics)*); - }; -} - -pub(crate) mod any; -pub(crate) mod boolobject; -pub(crate) mod bytearray; -pub(crate) mod bytes; -pub(crate) mod capsule; -#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))] -mod code; -pub(crate) mod complex; -#[cfg(not(Py_LIMITED_API))] -pub(crate) mod datetime; -pub(crate) mod dict; -mod ellipsis; -pub(crate) mod float; -#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))] -mod frame; -pub(crate) mod frozenset; -mod function; -pub(crate) mod iterator; -pub(crate) mod list; -pub(crate) mod mapping; -pub(crate) mod mappingproxy; -mod memoryview; -pub(crate) mod module; -mod none; -mod notimplemented; -mod num; -#[cfg(not(any(PyPy, GraalPy)))] -mod pysuper; -pub(crate) mod sequence; -pub(crate) mod set; -pub(crate) mod slice; -pub(crate) mod string; -pub(crate) mod traceback; -pub(crate) mod tuple; -pub(crate) mod typeobject; -pub(crate) mod weakref; diff --git a/vendor/pyo3/src/types/module.rs b/vendor/pyo3/src/types/module.rs deleted file mode 100644 index 99dc1be233..0000000000 --- a/vendor/pyo3/src/types/module.rs +++ /dev/null @@ -1,603 +0,0 @@ -use crate::err::{PyErr, PyResult}; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::impl_::callback::IntoPyCallbackOutput; -use crate::py_result_ext::PyResultExt; -use crate::pyclass::PyClass; -use crate::types::{ - any::PyAnyMethods, list::PyListMethods, PyAny, PyCFunction, PyDict, PyList, PyString, -}; -use crate::{ - exceptions, ffi, Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt, Py, PyObject, - Python, -}; -use std::ffi::{CStr, CString}; -#[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] -use std::os::raw::c_int; -use std::str; - -/// Represents a Python [`module`][1] object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyModule>`][Bound]. -/// -/// For APIs available on `module` objects, see the [`PyModuleMethods`] trait which is implemented for -/// [`Bound<'py, PyModule>`][Bound]. -/// -/// As with all other Python objects, modules are first class citizens. -/// This means they can be passed to or returned from functions, -/// created dynamically, assigned to variables and so forth. -/// -/// [1]: https://docs.python.org/3/tutorial/modules.html -#[repr(transparent)] -pub struct PyModule(PyAny); - -pyobject_native_type_core!(PyModule, pyobject_native_static_type_object!(ffi::PyModule_Type), #checkfunction=ffi::PyModule_Check); - -impl PyModule { - /// Creates a new module object with the `__name__` attribute set to `name`. - /// - /// # Examples - /// - /// ``` rust - /// use pyo3::prelude::*; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let module = PyModule::new(py, "my_module")?; - /// - /// assert_eq!(module.name()?, "my_module"); - /// Ok(()) - /// })?; - /// # Ok(())} - /// ``` - pub fn new<'py>(py: Python<'py>, name: &str) -> PyResult> { - let name = PyString::new(py, name); - unsafe { - ffi::PyModule_NewObject(name.as_ptr()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyModule::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyModule::new`")] - #[inline] - pub fn new_bound<'py>(py: Python<'py>, name: &str) -> PyResult> { - Self::new(py, name) - } - - /// Imports the Python module with the specified name. - /// - /// # Examples - /// - /// ```no_run - /// # fn main() { - /// use pyo3::prelude::*; - /// - /// Python::with_gil(|py| { - /// let module = PyModule::import(py, "antigravity").expect("No flying for you."); - /// }); - /// # } - /// ``` - /// - /// This is equivalent to the following Python expression: - /// ```python - /// import antigravity - /// ``` - /// - /// If you want to import a class, you can store a reference to it with - /// [`GILOnceCell::import`][crate::sync::GILOnceCell#method.import]. - pub fn import<'py, N>(py: Python<'py>, name: N) -> PyResult> - where - N: IntoPyObject<'py, Target = PyString>, - { - let name = name.into_pyobject_or_pyerr(py)?; - unsafe { - ffi::PyImport_Import(name.as_ptr()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyModule::import`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyModule::import`")] - #[allow(deprecated)] - #[inline] - pub fn import_bound(py: Python<'_>, name: N) -> PyResult> - where - N: crate::IntoPy>, - { - Self::import(py, name.into_py(py)) - } - - /// Creates and loads a module named `module_name`, - /// containing the Python code passed to `code` - /// and pretending to live at `file_name`. - /// - ///

- ///
⚠ ️
- ///
-    //
-    ///  Warning: This will compile and execute code. Never pass untrusted code to this function!
-    ///
-    /// 
- /// - /// # Errors - /// - /// Returns `PyErr` if: - /// - `code` is not syntactically correct Python. - /// - Any Python exceptions are raised while initializing the module. - /// - Any of the arguments cannot be converted to [`CString`]s. - /// - /// # Example: bundle in a file at compile time with [`include_str!`][std::include_str]: - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::ffi::c_str; - /// - /// # fn main() -> PyResult<()> { - /// // This path is resolved relative to this file. - /// let code = c_str!(include_str!("../../assets/script.py")); - /// - /// Python::with_gil(|py| -> PyResult<()> { - /// PyModule::from_code(py, code, c_str!("example.py"), c_str!("example"))?; - /// Ok(()) - /// })?; - /// # Ok(()) - /// # } - /// ``` - /// - /// # Example: Load a file at runtime with [`std::fs::read_to_string`]. - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::ffi::c_str; - /// use std::ffi::CString; - /// - /// # fn main() -> PyResult<()> { - /// // This path is resolved by however the platform resolves paths, - /// // which also makes this less portable. Consider using `include_str` - /// // if you just want to bundle a script with your module. - /// let code = std::fs::read_to_string("assets/script.py")?; - /// - /// Python::with_gil(|py| -> PyResult<()> { - /// PyModule::from_code(py, CString::new(code)?.as_c_str(), c_str!("example.py"), c_str!("example"))?; - /// Ok(()) - /// })?; - /// Ok(()) - /// # } - /// ``` - pub fn from_code<'py>( - py: Python<'py>, - code: &CStr, - file_name: &CStr, - module_name: &CStr, - ) -> PyResult> { - unsafe { - let code = ffi::Py_CompileString(code.as_ptr(), file_name.as_ptr(), ffi::Py_file_input) - .assume_owned_or_err(py)?; - - ffi::PyImport_ExecCodeModuleEx(module_name.as_ptr(), code.as_ptr(), file_name.as_ptr()) - .assume_owned_or_err(py) - .downcast_into() - } - } - - /// Deprecated name for [`PyModule::from_code`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyModule::from_code`")] - #[inline] - pub fn from_code_bound<'py>( - py: Python<'py>, - code: &str, - file_name: &str, - module_name: &str, - ) -> PyResult> { - let data = CString::new(code)?; - let filename = CString::new(file_name)?; - let module = CString::new(module_name)?; - - Self::from_code(py, data.as_c_str(), filename.as_c_str(), module.as_c_str()) - } -} - -/// Implementation of functionality for [`PyModule`]. -/// -/// These methods are defined for the `Bound<'py, PyModule>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyModule")] -pub trait PyModuleMethods<'py>: crate::sealed::Sealed { - /// Returns the module's `__dict__` attribute, which contains the module's symbol table. - fn dict(&self) -> Bound<'py, PyDict>; - - /// Returns the index (the `__all__` attribute) of the module, - /// creating one if needed. - /// - /// `__all__` declares the items that will be imported with `from my_module import *`. - fn index(&self) -> PyResult>; - - /// Returns the name (the `__name__` attribute) of the module. - /// - /// May fail if the module does not have a `__name__` attribute. - fn name(&self) -> PyResult>; - - /// Returns the filename (the `__file__` attribute) of the module. - /// - /// May fail if the module does not have a `__file__` attribute. - fn filename(&self) -> PyResult>; - - /// Adds an attribute to the module. - /// - /// For adding classes, functions or modules, prefer to use [`PyModuleMethods::add_class`], - /// [`PyModuleMethods::add_function`] or [`PyModuleMethods::add_submodule`] instead, - /// respectively. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pymodule] - /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> { - /// module.add("c", 299_792_458)?; - /// Ok(()) - /// } - /// ``` - /// - /// Python code can then do the following: - /// - /// ```python - /// from my_module import c - /// - /// print("c is", c) - /// ``` - /// - /// This will result in the following output: - /// - /// ```text - /// c is 299792458 - /// ``` - fn add(&self, name: N, value: V) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - V: IntoPyObject<'py>; - - /// Adds a new class to the module. - /// - /// Notice that this method does not take an argument. - /// Instead, this method is *generic*, and requires us to use the - /// "turbofish" syntax to specify the class we want to add. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pyclass] - /// struct Foo { /* fields omitted */ } - /// - /// #[pymodule] - /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> { - /// module.add_class::()?; - /// Ok(()) - /// } - /// ``` - /// - /// Python code can see this class as such: - /// ```python - /// from my_module import Foo - /// - /// print("Foo is", Foo) - /// ``` - /// - /// This will result in the following output: - /// ```text - /// Foo is - /// ``` - /// - /// Note that as we haven't defined a [constructor][1], Python code can't actually - /// make an *instance* of `Foo` (or *get* one for that matter, as we haven't exported - /// anything that can return instances of `Foo`). - /// - #[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#constructor")] - fn add_class(&self) -> PyResult<()> - where - T: PyClass; - - /// Adds a function or a (sub)module to a module, using the functions name as name. - /// - /// Prefer to use [`PyModuleMethods::add_function`] and/or [`PyModuleMethods::add_submodule`] - /// instead. - fn add_wrapped(&self, wrapper: &impl Fn(Python<'py>) -> T) -> PyResult<()> - where - T: IntoPyCallbackOutput<'py, PyObject>; - - /// Adds a submodule to a module. - /// - /// This is especially useful for creating module hierarchies. - /// - /// Note that this doesn't define a *package*, so this won't allow Python code - /// to directly import submodules by using - /// `from my_module import submodule`. - /// For more information, see [#759][1] and [#1517][2]. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pymodule] - /// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { - /// let submodule = PyModule::new(py, "submodule")?; - /// submodule.add("super_useful_constant", "important")?; - /// - /// module.add_submodule(&submodule)?; - /// Ok(()) - /// } - /// ``` - /// - /// Python code can then do the following: - /// - /// ```python - /// import my_module - /// - /// print("super_useful_constant is", my_module.submodule.super_useful_constant) - /// ``` - /// - /// This will result in the following output: - /// - /// ```text - /// super_useful_constant is important - /// ``` - /// - /// [1]: https://github.com/PyO3/pyo3/issues/759 - /// [2]: https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021 - fn add_submodule(&self, module: &Bound<'_, PyModule>) -> PyResult<()>; - - /// Add a function to a module. - /// - /// Note that this also requires the [`wrap_pyfunction!`][2] macro - /// to wrap a function annotated with [`#[pyfunction]`][1]. - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pyfunction] - /// fn say_hello() { - /// println!("Hello world!") - /// } - /// #[pymodule] - /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> { - /// module.add_function(wrap_pyfunction!(say_hello, module)?) - /// } - /// ``` - /// - /// Python code can then do the following: - /// - /// ```python - /// from my_module import say_hello - /// - /// say_hello() - /// ``` - /// - /// This will result in the following output: - /// - /// ```text - /// Hello world! - /// ``` - /// - /// [1]: crate::prelude::pyfunction - /// [2]: crate::wrap_pyfunction - fn add_function(&self, fun: Bound<'_, PyCFunction>) -> PyResult<()>; - - /// Declare whether or not this module supports running with the GIL disabled - /// - /// If the module does not rely on the GIL for thread safety, you can pass - /// `false` to this function to indicate the module does not rely on the GIL - /// for thread-safety. - /// - /// This function sets the [`Py_MOD_GIL` - /// slot](https://docs.python.org/3/c-api/module.html#c.Py_mod_gil) on the - /// module object. The default is `Py_MOD_GIL_USED`, so passing `true` to - /// this function is a no-op unless you have already set `Py_MOD_GIL` to - /// `Py_MOD_GIL_NOT_USED` elsewhere. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pymodule(gil_used = false)] - /// fn my_module(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> { - /// let submodule = PyModule::new(py, "submodule")?; - /// submodule.gil_used(false)?; - /// module.add_submodule(&submodule)?; - /// Ok(()) - /// } - /// ``` - /// - /// The resulting module will not print a `RuntimeWarning` and re-enable the - /// GIL when Python imports it on the free-threaded build, since all module - /// objects defined in the extension have `Py_MOD_GIL` set to - /// `Py_MOD_GIL_NOT_USED`. - /// - /// This is a no-op on the GIL-enabled build. - fn gil_used(&self, gil_used: bool) -> PyResult<()>; -} - -impl<'py> PyModuleMethods<'py> for Bound<'py, PyModule> { - fn dict(&self) -> Bound<'py, PyDict> { - unsafe { - // PyModule_GetDict returns borrowed ptr; must make owned for safety (see #890). - ffi::PyModule_GetDict(self.as_ptr()) - .assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked() - } - } - - fn index(&self) -> PyResult> { - let __all__ = __all__(self.py()); - match self.getattr(__all__) { - Ok(idx) => idx.downcast_into().map_err(PyErr::from), - Err(err) => { - if err.is_instance_of::(self.py()) { - let l = PyList::empty(self.py()); - self.setattr(__all__, &l)?; - Ok(l) - } else { - Err(err) - } - } - } - } - - fn name(&self) -> PyResult> { - #[cfg(not(PyPy))] - { - unsafe { - ffi::PyModule_GetNameObject(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[cfg(PyPy)] - { - self.dict() - .get_item("__name__") - .map_err(|_| exceptions::PyAttributeError::new_err("__name__"))? - .downcast_into() - .map_err(PyErr::from) - } - } - - fn filename(&self) -> PyResult> { - #[cfg(not(PyPy))] - unsafe { - ffi::PyModule_GetFilenameObject(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - - #[cfg(PyPy)] - { - self.dict() - .get_item("__file__") - .map_err(|_| exceptions::PyAttributeError::new_err("__file__"))? - .downcast_into() - .map_err(PyErr::from) - } - } - - fn add(&self, name: N, value: V) -> PyResult<()> - where - N: IntoPyObject<'py, Target = PyString>, - V: IntoPyObject<'py>, - { - fn inner( - module: &Bound<'_, PyModule>, - name: Borrowed<'_, '_, PyString>, - value: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - module - .index()? - .append(name) - .expect("could not append __name__ to __all__"); - module.setattr(name, value) - } - - let py = self.py(); - inner( - self, - name.into_pyobject_or_pyerr(py)?.as_borrowed(), - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn add_class(&self) -> PyResult<()> - where - T: PyClass, - { - let py = self.py(); - self.add(T::NAME, T::lazy_type_object().get_or_try_init(py)?) - } - - fn add_wrapped(&self, wrapper: &impl Fn(Python<'py>) -> T) -> PyResult<()> - where - T: IntoPyCallbackOutput<'py, PyObject>, - { - fn inner(module: &Bound<'_, PyModule>, object: Bound<'_, PyAny>) -> PyResult<()> { - let name = object.getattr(__name__(module.py()))?; - module.add(name.downcast_into::()?, object) - } - - let py = self.py(); - inner(self, wrapper(py).convert(py)?.into_bound(py)) - } - - fn add_submodule(&self, module: &Bound<'_, PyModule>) -> PyResult<()> { - let name = module.name()?; - self.add(name, module) - } - - fn add_function(&self, fun: Bound<'_, PyCFunction>) -> PyResult<()> { - let name = fun.getattr(__name__(self.py()))?; - self.add(name.downcast_into::()?, fun) - } - - #[cfg_attr(any(Py_LIMITED_API, not(Py_GIL_DISABLED)), allow(unused_variables))] - fn gil_used(&self, gil_used: bool) -> PyResult<()> { - #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))] - { - let gil_used = match gil_used { - true => ffi::Py_MOD_GIL_USED, - false => ffi::Py_MOD_GIL_NOT_USED, - }; - match unsafe { ffi::PyUnstable_Module_SetGIL(self.as_ptr(), gil_used) } { - c_int::MIN..=-1 => Err(PyErr::fetch(self.py())), - 0..=c_int::MAX => Ok(()), - } - } - #[cfg(any(Py_LIMITED_API, not(Py_GIL_DISABLED)))] - Ok(()) - } -} - -fn __all__(py: Python<'_>) -> &Bound<'_, PyString> { - intern!(py, "__all__") -} - -fn __name__(py: Python<'_>) -> &Bound<'_, PyString> { - intern!(py, "__name__") -} - -#[cfg(test)] -mod tests { - use crate::{ - types::{module::PyModuleMethods, PyModule}, - Python, - }; - - #[test] - fn module_import_and_name() { - Python::with_gil(|py| { - let builtins = PyModule::import(py, "builtins").unwrap(); - assert_eq!(builtins.name().unwrap(), "builtins"); - }) - } - - #[test] - fn module_filename() { - use crate::types::string::PyStringMethods; - Python::with_gil(|py| { - let site = PyModule::import(py, "site").unwrap(); - assert!(site - .filename() - .unwrap() - .to_cow() - .unwrap() - .ends_with("site.py")); - }) - } -} diff --git a/vendor/pyo3/src/types/none.rs b/vendor/pyo3/src/types/none.rs deleted file mode 100644 index 1ec12d3f5b..0000000000 --- a/vendor/pyo3/src/types/none.rs +++ /dev/null @@ -1,120 +0,0 @@ -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::{ffi, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyObject, PyTypeInfo, Python}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -/// Represents the Python `None` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyNone>`][Bound]. -#[repr(transparent)] -pub struct PyNone(PyAny); - -pyobject_native_type_named!(PyNone); - -impl PyNone { - /// Returns the `None` object. - #[inline] - pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyNone> { - unsafe { ffi::Py_None().assume_borrowed(py).downcast_unchecked() } - } - - /// Deprecated name for [`PyNone::get`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyNone::get`")] - #[inline] - pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyNone> { - Self::get(py) - } -} - -unsafe impl PyTypeInfo for PyNone { - const NAME: &'static str = "NoneType"; - - const MODULE: Option<&'static str> = None; - - fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject { - unsafe { ffi::Py_TYPE(ffi::Py_None()) } - } - - #[inline] - fn is_type_of(object: &Bound<'_, PyAny>) -> bool { - // NoneType is not usable as a base type - Self::is_exact_type_of(object) - } - - #[inline] - fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool { - object.is(&**Self::get(object.py())) - } -} - -/// `()` is converted to Python `None`. -#[allow(deprecated)] -impl ToPyObject for () { - fn to_object(&self, py: Python<'_>) -> PyObject { - PyNone::get(py).into_py(py) - } -} - -#[allow(deprecated)] -impl IntoPy for () { - #[inline] - fn into_py(self, py: Python<'_>) -> PyObject { - PyNone::get(py).into_py(py) - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::types::{PyDict, PyNone}; - use crate::{PyObject, PyTypeInfo, Python}; - - #[test] - fn test_none_is_itself() { - Python::with_gil(|py| { - assert!(PyNone::get(py).is_instance_of::()); - assert!(PyNone::get(py).is_exact_instance_of::()); - }) - } - - #[test] - fn test_none_type_object_consistent() { - Python::with_gil(|py| { - assert!(PyNone::get(py).get_type().is(&PyNone::type_object(py))); - }) - } - - #[test] - fn test_none_is_none() { - Python::with_gil(|py| { - assert!(PyNone::get(py).downcast::().unwrap().is_none()); - }) - } - - #[test] - #[allow(deprecated)] - fn test_unit_to_object_is_none() { - use crate::ToPyObject; - Python::with_gil(|py| { - assert!(().to_object(py).downcast_bound::(py).is_ok()); - }) - } - - #[test] - #[allow(deprecated)] - fn test_unit_into_py_is_none() { - use crate::IntoPy; - Python::with_gil(|py| { - let obj: PyObject = ().into_py(py); - assert!(obj.downcast_bound::(py).is_ok()); - }) - } - - #[test] - fn test_dict_is_not_none() { - Python::with_gil(|py| { - assert!(PyDict::new(py).downcast::().is_err()); - }) - } -} diff --git a/vendor/pyo3/src/types/notimplemented.rs b/vendor/pyo3/src/types/notimplemented.rs deleted file mode 100644 index d93ab466d2..0000000000 --- a/vendor/pyo3/src/types/notimplemented.rs +++ /dev/null @@ -1,83 +0,0 @@ -use crate::{ - ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo, - Python, -}; - -/// Represents the Python `NotImplemented` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyNotImplemented>`][Bound]. -#[repr(transparent)] -pub struct PyNotImplemented(PyAny); - -pyobject_native_type_named!(PyNotImplemented); - -impl PyNotImplemented { - /// Returns the `NotImplemented` object. - #[inline] - pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyNotImplemented> { - unsafe { - ffi::Py_NotImplemented() - .assume_borrowed(py) - .downcast_unchecked() - } - } - - /// Deprecated name for [`PyNotImplemented::get`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyNotImplemented::get`")] - #[inline] - pub fn get_bound(py: Python<'_>) -> Borrowed<'_, '_, PyNotImplemented> { - Self::get(py) - } -} - -unsafe impl PyTypeInfo for PyNotImplemented { - const NAME: &'static str = "NotImplementedType"; - const MODULE: Option<&'static str> = None; - - fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject { - unsafe { ffi::Py_TYPE(ffi::Py_NotImplemented()) } - } - - #[inline] - fn is_type_of(object: &Bound<'_, PyAny>) -> bool { - // NotImplementedType is not usable as a base type - Self::is_exact_type_of(object) - } - - #[inline] - fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool { - object.is(&**Self::get(object.py())) - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::PyAnyMethods; - use crate::types::{PyDict, PyNotImplemented}; - use crate::{PyTypeInfo, Python}; - - #[test] - fn test_notimplemented_is_itself() { - Python::with_gil(|py| { - assert!(PyNotImplemented::get(py).is_instance_of::()); - assert!(PyNotImplemented::get(py).is_exact_instance_of::()); - }) - } - - #[test] - fn test_notimplemented_type_object_consistent() { - Python::with_gil(|py| { - assert!(PyNotImplemented::get(py) - .get_type() - .is(&PyNotImplemented::type_object(py))); - }) - } - - #[test] - fn test_dict_is_not_notimplemented() { - Python::with_gil(|py| { - assert!(PyDict::new(py).downcast::().is_err()); - }) - } -} diff --git a/vendor/pyo3/src/types/num.rs b/vendor/pyo3/src/types/num.rs deleted file mode 100644 index 0e377f66d4..0000000000 --- a/vendor/pyo3/src/types/num.rs +++ /dev/null @@ -1,126 +0,0 @@ -use super::any::PyAnyMethods; - -use crate::{ffi, instance::Bound, PyAny}; - -/// Represents a Python `int` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyInt>`][crate::Bound]. -/// -/// You can usually avoid directly working with this type -/// by using [`ToPyObject`](crate::conversion::ToPyObject) -/// and [`extract`](super::PyAnyMethods::extract) -/// with the primitive Rust integer types. -#[repr(transparent)] -pub struct PyInt(PyAny); - -pyobject_native_type_core!(PyInt, pyobject_native_static_type_object!(ffi::PyLong_Type), #checkfunction=ffi::PyLong_Check); - -/// Deprecated alias for [`PyInt`]. -#[deprecated(since = "0.23.0", note = "use `PyInt` instead")] -pub type PyLong = PyInt; - -macro_rules! int_compare { - ($rust_type: ty) => { - impl PartialEq<$rust_type> for Bound<'_, PyInt> { - #[inline] - fn eq(&self, other: &$rust_type) -> bool { - if let Ok(value) = self.extract::<$rust_type>() { - value == *other - } else { - false - } - } - } - impl PartialEq> for $rust_type { - #[inline] - fn eq(&self, other: &Bound<'_, PyInt>) -> bool { - if let Ok(value) = other.extract::<$rust_type>() { - value == *self - } else { - false - } - } - } - }; -} - -int_compare!(i8); -int_compare!(u8); -int_compare!(i16); -int_compare!(u16); -int_compare!(i32); -int_compare!(u32); -int_compare!(i64); -int_compare!(u64); -int_compare!(i128); -int_compare!(u128); -int_compare!(isize); -int_compare!(usize); - -#[cfg(test)] -mod tests { - use crate::{IntoPyObject, Python}; - - #[test] - fn test_partial_eq() { - Python::with_gil(|py| { - let v_i8 = 123i8; - let v_u8 = 123i8; - let v_i16 = 123i16; - let v_u16 = 123u16; - let v_i32 = 123i32; - let v_u32 = 123u32; - let v_i64 = 123i64; - let v_u64 = 123u64; - let v_i128 = 123i128; - let v_u128 = 123u128; - let v_isize = 123isize; - let v_usize = 123usize; - let obj = 123_i64.into_pyobject(py).unwrap(); - assert_eq!(v_i8, obj); - assert_eq!(obj, v_i8); - - assert_eq!(v_u8, obj); - assert_eq!(obj, v_u8); - - assert_eq!(v_i16, obj); - assert_eq!(obj, v_i16); - - assert_eq!(v_u16, obj); - assert_eq!(obj, v_u16); - - assert_eq!(v_i32, obj); - assert_eq!(obj, v_i32); - - assert_eq!(v_u32, obj); - assert_eq!(obj, v_u32); - - assert_eq!(v_i64, obj); - assert_eq!(obj, v_i64); - - assert_eq!(v_u64, obj); - assert_eq!(obj, v_u64); - - assert_eq!(v_i128, obj); - assert_eq!(obj, v_i128); - - assert_eq!(v_u128, obj); - assert_eq!(obj, v_u128); - - assert_eq!(v_isize, obj); - assert_eq!(obj, v_isize); - - assert_eq!(v_usize, obj); - assert_eq!(obj, v_usize); - - let big_num = (u8::MAX as u16) + 1; - let big_obj = big_num.into_pyobject(py).unwrap(); - - for x in 0u8..=u8::MAX { - assert_ne!(x, big_obj); - assert_ne!(big_obj, x); - } - }); - } -} diff --git a/vendor/pyo3/src/types/pysuper.rs b/vendor/pyo3/src/types/pysuper.rs deleted file mode 100644 index 81db8cea86..0000000000 --- a/vendor/pyo3/src/types/pysuper.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::instance::Bound; -use crate::types::any::PyAnyMethods; -use crate::types::PyType; -use crate::{ffi, PyTypeInfo}; -use crate::{PyAny, PyResult}; - -/// Represents a Python `super` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PySuper>`][Bound]. -#[repr(transparent)] -pub struct PySuper(PyAny); - -pyobject_native_type_core!( - PySuper, - pyobject_native_static_type_object!(ffi::PySuper_Type) -); - -impl PySuper { - /// Constructs a new super object. More read about super object: [docs](https://docs.python.org/3/library/functions.html#super) - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// - /// #[pyclass(subclass)] - /// struct BaseClass { - /// val1: usize, - /// } - /// - /// #[pymethods] - /// impl BaseClass { - /// #[new] - /// fn new() -> Self { - /// BaseClass { val1: 10 } - /// } - /// - /// pub fn method(&self) -> usize { - /// self.val1 - /// } - /// } - /// - /// #[pyclass(extends=BaseClass)] - /// struct SubClass {} - /// - /// #[pymethods] - /// impl SubClass { - /// #[new] - /// fn new() -> (Self, BaseClass) { - /// (SubClass {}, BaseClass::new()) - /// } - /// - /// fn method<'py>(self_: &Bound<'py, Self>) -> PyResult> { - /// let super_ = self_.py_super()?; - /// super_.call_method("method", (), None) - /// } - /// } - /// ``` - pub fn new<'py>( - ty: &Bound<'py, PyType>, - obj: &Bound<'py, PyAny>, - ) -> PyResult> { - PySuper::type_object(ty.py()).call1((ty, obj)).map(|any| { - // Safety: super() always returns instance of super - unsafe { any.downcast_into_unchecked() } - }) - } - - /// Deprecated name for [`PySuper::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PySuper::new`")] - #[inline] - pub fn new_bound<'py>( - ty: &Bound<'py, PyType>, - obj: &Bound<'py, PyAny>, - ) -> PyResult> { - Self::new(ty, obj) - } -} diff --git a/vendor/pyo3/src/types/sequence.rs b/vendor/pyo3/src/types/sequence.rs deleted file mode 100644 index bc2643dcf8..0000000000 --- a/vendor/pyo3/src/types/sequence.rs +++ /dev/null @@ -1,832 +0,0 @@ -use crate::err::{self, DowncastError, PyErr, PyResult}; -use crate::exceptions::PyTypeError; -use crate::ffi_ptr_ext::FfiPtrExt; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::instance::Bound; -use crate::internal_tricks::get_ssize_index; -use crate::py_result_ext::PyResultExt; -use crate::sync::GILOnceCell; -use crate::type_object::PyTypeInfo; -use crate::types::{any::PyAnyMethods, PyAny, PyList, PyString, PyTuple, PyType}; -use crate::{ - ffi, Borrowed, BoundObject, FromPyObject, IntoPyObject, IntoPyObjectExt, Py, PyTypeCheck, - Python, -}; - -/// Represents a reference to a Python object supporting the sequence protocol. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PySequence>`][Bound]. -/// -/// For APIs available on sequence objects, see the [`PySequenceMethods`] trait which is implemented for -/// [`Bound<'py, PySequence>`][Bound]. -#[repr(transparent)] -pub struct PySequence(PyAny); -pyobject_native_type_named!(PySequence); - -impl PySequence { - /// Register a pyclass as a subclass of `collections.abc.Sequence` (from the Python standard - /// library). This is equivalent to `collections.abc.Sequence.register(T)` in Python. - /// This registration is required for a pyclass to be downcastable from `PyAny` to `PySequence`. - pub fn register(py: Python<'_>) -> PyResult<()> { - let ty = T::type_object(py); - get_sequence_abc(py)?.call_method1("register", (ty,))?; - Ok(()) - } -} - -/// Implementation of functionality for [`PySequence`]. -/// -/// These methods are defined for the `Bound<'py, PySequence>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PySequence")] -pub trait PySequenceMethods<'py>: crate::sealed::Sealed { - /// Returns the number of objects in sequence. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> PyResult; - - /// Returns whether the sequence is empty. - fn is_empty(&self) -> PyResult; - - /// Returns the concatenation of `self` and `other`. - /// - /// This is equivalent to the Python expression `self + other`. - fn concat(&self, other: &Bound<'_, PySequence>) -> PyResult>; - - /// Returns the result of repeating a sequence object `count` times. - /// - /// This is equivalent to the Python expression `self * count`. - fn repeat(&self, count: usize) -> PyResult>; - - /// Concatenates `self` and `other`, in place if possible. - /// - /// This is equivalent to the Python expression `self.__iadd__(other)`. - /// - /// The Python statement `self += other` is syntactic sugar for `self = - /// self.__iadd__(other)`. `__iadd__` should modify and return `self` if - /// possible, but create and return a new object if not. - fn in_place_concat(&self, other: &Bound<'_, PySequence>) -> PyResult>; - - /// Repeats the sequence object `count` times and updates `self`, if possible. - /// - /// This is equivalent to the Python expression `self.__imul__(other)`. - /// - /// The Python statement `self *= other` is syntactic sugar for `self = - /// self.__imul__(other)`. `__imul__` should modify and return `self` if - /// possible, but create and return a new object if not. - fn in_place_repeat(&self, count: usize) -> PyResult>; - - /// Returns the `index`th element of the Sequence. - /// - /// This is equivalent to the Python expression `self[index]` without support of negative indices. - fn get_item(&self, index: usize) -> PyResult>; - - /// Returns the slice of sequence object between `begin` and `end`. - /// - /// This is equivalent to the Python expression `self[begin:end]`. - fn get_slice(&self, begin: usize, end: usize) -> PyResult>; - - /// Assigns object `item` to the `i`th element of self. - /// - /// This is equivalent to the Python statement `self[i] = v`. - fn set_item(&self, i: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>; - - /// Deletes the `i`th element of self. - /// - /// This is equivalent to the Python statement `del self[i]`. - fn del_item(&self, i: usize) -> PyResult<()>; - - /// Assigns the sequence `v` to the slice of `self` from `i1` to `i2`. - /// - /// This is equivalent to the Python statement `self[i1:i2] = v`. - fn set_slice(&self, i1: usize, i2: usize, v: &Bound<'_, PyAny>) -> PyResult<()>; - - /// Deletes the slice from `i1` to `i2` from `self`. - /// - /// This is equivalent to the Python statement `del self[i1:i2]`. - fn del_slice(&self, i1: usize, i2: usize) -> PyResult<()>; - - /// Returns the number of occurrences of `value` in self, that is, return the - /// number of keys for which `self[key] == value`. - #[cfg(not(PyPy))] - fn count(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Determines if self contains `value`. - /// - /// This is equivalent to the Python expression `value in self`. - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns the first index `i` for which `self[i] == value`. - /// - /// This is equivalent to the Python expression `self.index(value)`. - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns a fresh list based on the Sequence. - fn to_list(&self) -> PyResult>; - - /// Returns a fresh tuple based on the Sequence. - fn to_tuple(&self) -> PyResult>; -} - -impl<'py> PySequenceMethods<'py> for Bound<'py, PySequence> { - #[inline] - fn len(&self) -> PyResult { - let v = unsafe { ffi::PySequence_Size(self.as_ptr()) }; - crate::err::error_on_minusone(self.py(), v)?; - Ok(v as usize) - } - - #[inline] - fn is_empty(&self) -> PyResult { - self.len().map(|l| l == 0) - } - - #[inline] - fn concat(&self, other: &Bound<'_, PySequence>) -> PyResult> { - unsafe { - ffi::PySequence_Concat(self.as_ptr(), other.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn repeat(&self, count: usize) -> PyResult> { - unsafe { - ffi::PySequence_Repeat(self.as_ptr(), get_ssize_index(count)) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn in_place_concat(&self, other: &Bound<'_, PySequence>) -> PyResult> { - unsafe { - ffi::PySequence_InPlaceConcat(self.as_ptr(), other.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn in_place_repeat(&self, count: usize) -> PyResult> { - unsafe { - ffi::PySequence_InPlaceRepeat(self.as_ptr(), get_ssize_index(count)) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn get_item(&self, index: usize) -> PyResult> { - unsafe { - ffi::PySequence_GetItem(self.as_ptr(), get_ssize_index(index)) - .assume_owned_or_err(self.py()) - } - } - - #[inline] - fn get_slice(&self, begin: usize, end: usize) -> PyResult> { - unsafe { - ffi::PySequence_GetSlice(self.as_ptr(), get_ssize_index(begin), get_ssize_index(end)) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn set_item(&self, i: usize, item: I) -> PyResult<()> - where - I: IntoPyObject<'py>, - { - fn inner( - seq: &Bound<'_, PySequence>, - i: usize, - item: Borrowed<'_, '_, PyAny>, - ) -> PyResult<()> { - err::error_on_minusone(seq.py(), unsafe { - ffi::PySequence_SetItem(seq.as_ptr(), get_ssize_index(i), item.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - i, - item.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - #[inline] - fn del_item(&self, i: usize) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PySequence_DelItem(self.as_ptr(), get_ssize_index(i)) - }) - } - - #[inline] - fn set_slice(&self, i1: usize, i2: usize, v: &Bound<'_, PyAny>) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PySequence_SetSlice( - self.as_ptr(), - get_ssize_index(i1), - get_ssize_index(i2), - v.as_ptr(), - ) - }) - } - - #[inline] - fn del_slice(&self, i1: usize, i2: usize) -> PyResult<()> { - err::error_on_minusone(self.py(), unsafe { - ffi::PySequence_DelSlice(self.as_ptr(), get_ssize_index(i1), get_ssize_index(i2)) - }) - } - - #[inline] - #[cfg(not(PyPy))] - fn count(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - fn inner(seq: &Bound<'_, PySequence>, value: Borrowed<'_, '_, PyAny>) -> PyResult { - let r = unsafe { ffi::PySequence_Count(seq.as_ptr(), value.as_ptr()) }; - crate::err::error_on_minusone(seq.py(), r)?; - Ok(r as usize) - } - - let py = self.py(); - inner( - self, - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - #[inline] - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - fn inner(seq: &Bound<'_, PySequence>, value: Borrowed<'_, '_, PyAny>) -> PyResult { - let r = unsafe { ffi::PySequence_Contains(seq.as_ptr(), value.as_ptr()) }; - match r { - 0 => Ok(false), - 1 => Ok(true), - _ => Err(PyErr::fetch(seq.py())), - } - } - - let py = self.py(); - inner( - self, - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - #[inline] - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - fn inner(seq: &Bound<'_, PySequence>, value: Borrowed<'_, '_, PyAny>) -> PyResult { - let r = unsafe { ffi::PySequence_Index(seq.as_ptr(), value.as_ptr()) }; - crate::err::error_on_minusone(seq.py(), r)?; - Ok(r as usize) - } - - let py = self.py(); - inner( - self, - value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - #[inline] - fn to_list(&self) -> PyResult> { - unsafe { - ffi::PySequence_List(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } - - #[inline] - fn to_tuple(&self) -> PyResult> { - unsafe { - ffi::PySequence_Tuple(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked() - } - } -} - -impl<'py, T> FromPyObject<'py> for Vec -where - T: FromPyObject<'py>, -{ - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { - if obj.is_instance_of::() { - return Err(PyTypeError::new_err("Can't extract `str` to `Vec`")); - } - extract_sequence(obj) - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::sequence_of(T::type_input()) - } -} - -fn extract_sequence<'py, T>(obj: &Bound<'py, PyAny>) -> PyResult> -where - T: FromPyObject<'py>, -{ - // Types that pass `PySequence_Check` usually implement enough of the sequence protocol - // to support this function and if not, we will only fail extraction safely. - let seq = unsafe { - if ffi::PySequence_Check(obj.as_ptr()) != 0 { - obj.downcast_unchecked::() - } else { - return Err(DowncastError::new(obj, "Sequence").into()); - } - }; - - let mut v = Vec::with_capacity(seq.len().unwrap_or(0)); - for item in seq.try_iter()? { - v.push(item?.extract::()?); - } - Ok(v) -} - -fn get_sequence_abc(py: Python<'_>) -> PyResult<&Bound<'_, PyType>> { - static SEQUENCE_ABC: GILOnceCell> = GILOnceCell::new(); - - SEQUENCE_ABC.import(py, "collections.abc", "Sequence") -} - -impl PyTypeCheck for PySequence { - const NAME: &'static str = "Sequence"; - - #[inline] - fn type_check(object: &Bound<'_, PyAny>) -> bool { - // Using `is_instance` for `collections.abc.Sequence` is slow, so provide - // optimized cases for list and tuples as common well-known sequences - PyList::is_type_of(object) - || PyTuple::is_type_of(object) - || get_sequence_abc(object.py()) - .and_then(|abc| object.is_instance(abc)) - .unwrap_or_else(|err| { - err.write_unraisable(object.py(), Some(object)); - false - }) - } -} - -#[cfg(test)] -mod tests { - use crate::types::{PyAnyMethods, PyList, PySequence, PySequenceMethods, PyTuple}; - use crate::{ffi, IntoPyObject, PyObject, Python}; - - fn get_object() -> PyObject { - // Convenience function for getting a single unique object - Python::with_gil(|py| { - let obj = py.eval(ffi::c_str!("object()"), None, None).unwrap(); - - obj.into_pyobject(py).unwrap().unbind() - }) - } - - #[test] - fn test_numbers_are_not_sequences() { - Python::with_gil(|py| { - let v = 42i32; - assert!(v - .into_pyobject(py) - .unwrap() - .downcast::() - .is_err()); - }); - } - - #[test] - fn test_strings_are_sequences() { - Python::with_gil(|py| { - let v = "London Calling"; - assert!(v - .into_pyobject(py) - .unwrap() - .downcast::() - .is_ok()); - }); - } - - #[test] - fn test_strings_cannot_be_extracted_to_vec() { - Python::with_gil(|py| { - let v = "London Calling"; - let ob = v.into_pyobject(py).unwrap(); - - assert!(ob.extract::>().is_err()); - assert!(ob.extract::>().is_err()); - }); - } - - #[test] - fn test_seq_empty() { - Python::with_gil(|py| { - let v: Vec = vec![]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(0, seq.len().unwrap()); - - let needle = 7i32.into_pyobject(py).unwrap(); - assert!(!seq.contains(&needle).unwrap()); - }); - } - - #[test] - fn test_seq_is_empty() { - Python::with_gil(|py| { - let list = vec![1].into_pyobject(py).unwrap(); - let seq = list.downcast::().unwrap(); - assert!(!seq.is_empty().unwrap()); - let vec: Vec = Vec::new(); - let empty_list = vec.into_pyobject(py).unwrap(); - let empty_seq = empty_list.downcast::().unwrap(); - assert!(empty_seq.is_empty().unwrap()); - }); - } - - #[test] - fn test_seq_contains() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(6, seq.len().unwrap()); - - let bad_needle = 7i32.into_pyobject(py).unwrap(); - assert!(!seq.contains(&bad_needle).unwrap()); - - let good_needle = 8i32.into_pyobject(py).unwrap(); - assert!(seq.contains(&good_needle).unwrap()); - - let type_coerced_needle = 8f32.into_pyobject(py).unwrap(); - assert!(seq.contains(&type_coerced_needle).unwrap()); - }); - } - - #[test] - fn test_seq_get_item() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(1, seq.get_item(0).unwrap().extract::().unwrap()); - assert_eq!(1, seq.get_item(1).unwrap().extract::().unwrap()); - assert_eq!(2, seq.get_item(2).unwrap().extract::().unwrap()); - assert_eq!(3, seq.get_item(3).unwrap().extract::().unwrap()); - assert_eq!(5, seq.get_item(4).unwrap().extract::().unwrap()); - assert_eq!(8, seq.get_item(5).unwrap().extract::().unwrap()); - assert!(seq.get_item(10).is_err()); - }); - } - - #[test] - fn test_seq_del_item() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq.del_item(10).is_err()); - assert_eq!(1, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(1, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(2, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(3, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(5, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(8, seq.get_item(0).unwrap().extract::().unwrap()); - assert!(seq.del_item(0).is_ok()); - assert_eq!(0, seq.len().unwrap()); - assert!(seq.del_item(0).is_err()); - }); - } - - #[test] - fn test_seq_set_item() { - Python::with_gil(|py| { - let v: Vec = vec![1, 2]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(2, seq.get_item(1).unwrap().extract::().unwrap()); - assert!(seq.set_item(1, 10).is_ok()); - assert_eq!(10, seq.get_item(1).unwrap().extract::().unwrap()); - }); - } - - #[test] - fn test_seq_set_item_refcnt() { - let obj = get_object(); - - Python::with_gil(|py| { - let v: Vec = vec![1, 2]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq.set_item(1, &obj).is_ok()); - assert!(seq.get_item(1).unwrap().as_ptr() == obj.as_ptr()); - }); - - Python::with_gil(move |py| { - assert_eq!(1, obj.get_refcnt(py)); - }); - } - - #[test] - fn test_seq_get_slice() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!( - [1, 2, 3], - seq.get_slice(1, 4).unwrap().extract::<[i32; 3]>().unwrap() - ); - assert_eq!( - [3, 5, 8], - seq.get_slice(3, 100) - .unwrap() - .extract::<[i32; 3]>() - .unwrap() - ); - }); - } - - #[test] - fn test_set_slice() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let w: Vec = vec![7, 4]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let ins = w.into_pyobject(py).unwrap(); - seq.set_slice(1, 4, &ins).unwrap(); - assert_eq!([1, 7, 4, 5, 8], seq.extract::<[i32; 5]>().unwrap()); - seq.set_slice(3, 100, &PyList::empty(py)).unwrap(); - assert_eq!([1, 7, 4], seq.extract::<[i32; 3]>().unwrap()); - }); - } - - #[test] - fn test_del_slice() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - seq.del_slice(1, 4).unwrap(); - assert_eq!([1, 5, 8], seq.extract::<[i32; 3]>().unwrap()); - seq.del_slice(1, 100).unwrap(); - assert_eq!([1], seq.extract::<[i32; 1]>().unwrap()); - }); - } - - #[test] - fn test_seq_index() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(0, seq.index(1i32).unwrap()); - assert_eq!(2, seq.index(2i32).unwrap()); - assert_eq!(3, seq.index(3i32).unwrap()); - assert_eq!(4, seq.index(5i32).unwrap()); - assert_eq!(5, seq.index(8i32).unwrap()); - assert!(seq.index(42i32).is_err()); - }); - } - - #[test] - #[cfg(not(any(PyPy, GraalPy)))] - fn test_seq_count() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert_eq!(2, seq.count(1i32).unwrap()); - assert_eq!(1, seq.count(2i32).unwrap()); - assert_eq!(1, seq.count(3i32).unwrap()); - assert_eq!(1, seq.count(5i32).unwrap()); - assert_eq!(1, seq.count(8i32).unwrap()); - assert_eq!(0, seq.count(42i32).unwrap()); - }); - } - - #[test] - fn test_seq_iter() { - Python::with_gil(|py| { - let v: Vec = vec![1, 1, 2, 3, 5, 8]; - let ob = (&v).into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let mut idx = 0; - for el in seq.try_iter().unwrap() { - assert_eq!(v[idx], el.unwrap().extract::().unwrap()); - idx += 1; - } - assert_eq!(idx, v.len()); - }); - } - - #[test] - fn test_seq_strings() { - Python::with_gil(|py| { - let v = vec!["It", "was", "the", "worst", "of", "times"]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - - let bad_needle = "blurst".into_pyobject(py).unwrap(); - assert!(!seq.contains(bad_needle).unwrap()); - - let good_needle = "worst".into_pyobject(py).unwrap(); - assert!(seq.contains(good_needle).unwrap()); - }); - } - - #[test] - fn test_seq_concat() { - Python::with_gil(|py| { - let v: Vec = vec![1, 2, 3]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let concat_seq = seq.concat(seq).unwrap(); - assert_eq!(6, concat_seq.len().unwrap()); - let concat_v: Vec = vec![1, 2, 3, 1, 2, 3]; - for (el, cc) in concat_seq.try_iter().unwrap().zip(concat_v) { - assert_eq!(cc, el.unwrap().extract::().unwrap()); - } - }); - } - - #[test] - fn test_seq_concat_string() { - Python::with_gil(|py| { - let v = "string"; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let concat_seq = seq.concat(seq).unwrap(); - assert_eq!(12, concat_seq.len().unwrap()); - let concat_v = "stringstring".to_owned(); - for (el, cc) in seq.try_iter().unwrap().zip(concat_v.chars()) { - assert_eq!(cc, el.unwrap().extract::().unwrap()); - } - }); - } - - #[test] - fn test_seq_repeat() { - Python::with_gil(|py| { - let v = vec!["foo", "bar"]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let repeat_seq = seq.repeat(3).unwrap(); - assert_eq!(6, repeat_seq.len().unwrap()); - let repeated = ["foo", "bar", "foo", "bar", "foo", "bar"]; - for (el, rpt) in repeat_seq.try_iter().unwrap().zip(repeated.iter()) { - assert_eq!(*rpt, el.unwrap().extract::().unwrap()); - } - }); - } - - #[test] - fn test_seq_inplace() { - Python::with_gil(|py| { - let v = vec!["foo", "bar"]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let rep_seq = seq.in_place_repeat(3).unwrap(); - assert_eq!(6, seq.len().unwrap()); - assert!(seq.is(&rep_seq)); - - let conc_seq = seq.in_place_concat(seq).unwrap(); - assert_eq!(12, seq.len().unwrap()); - assert!(seq.is(&conc_seq)); - }); - } - - #[test] - fn test_list_coercion() { - Python::with_gil(|py| { - let v = vec!["foo", "bar"]; - let ob = (&v).into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq - .to_list() - .unwrap() - .eq(PyList::new(py, &v).unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_strings_coerce_to_lists() { - Python::with_gil(|py| { - let v = "foo"; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq - .to_list() - .unwrap() - .eq(PyList::new(py, ["f", "o", "o"]).unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_tuple_coercion() { - Python::with_gil(|py| { - let v = ("foo", "bar"); - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq - .to_tuple() - .unwrap() - .eq(PyTuple::new(py, ["foo", "bar"]).unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_lists_coerce_to_tuples() { - Python::with_gil(|py| { - let v = vec!["foo", "bar"]; - let ob = (&v).into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - assert!(seq - .to_tuple() - .unwrap() - .eq(PyTuple::new(py, &v).unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_extract_tuple_to_vec() { - Python::with_gil(|py| { - let v: Vec = py - .eval(ffi::c_str!("(1, 2)"), None, None) - .unwrap() - .extract() - .unwrap(); - assert!(v == [1, 2]); - }); - } - - #[test] - fn test_extract_range_to_vec() { - Python::with_gil(|py| { - let v: Vec = py - .eval(ffi::c_str!("range(1, 5)"), None, None) - .unwrap() - .extract() - .unwrap(); - assert!(v == [1, 2, 3, 4]); - }); - } - - #[test] - fn test_extract_bytearray_to_vec() { - Python::with_gil(|py| { - let v: Vec = py - .eval(ffi::c_str!("bytearray(b'abc')"), None, None) - .unwrap() - .extract() - .unwrap(); - assert!(v == b"abc"); - }); - } - - #[test] - fn test_seq_downcast_unchecked() { - Python::with_gil(|py| { - let v = vec!["foo", "bar"]; - let ob = v.into_pyobject(py).unwrap(); - let seq = ob.downcast::().unwrap(); - let type_ptr = seq.as_ref(); - let seq_from = unsafe { type_ptr.downcast_unchecked::() }; - assert!(seq_from.to_list().is_ok()); - }); - } -} diff --git a/vendor/pyo3/src/types/set.rs b/vendor/pyo3/src/types/set.rs deleted file mode 100644 index d5e39ebc83..0000000000 --- a/vendor/pyo3/src/types/set.rs +++ /dev/null @@ -1,485 +0,0 @@ -use crate::types::PyIterator; -#[allow(deprecated)] -use crate::ToPyObject; -use crate::{ - err::{self, PyErr, PyResult}, - ffi_ptr_ext::FfiPtrExt, - instance::Bound, - py_result_ext::PyResultExt, - types::any::PyAnyMethods, -}; -use crate::{ffi, Borrowed, BoundObject, IntoPyObject, IntoPyObjectExt, PyAny, Python}; -use std::ptr; - -/// Represents a Python `set`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PySet>`][Bound]. -/// -/// For APIs available on `set` objects, see the [`PySetMethods`] trait which is implemented for -/// [`Bound<'py, PySet>`][Bound]. -#[repr(transparent)] -pub struct PySet(PyAny); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_subclassable_native_type!(PySet, crate::ffi::PySetObject); - -#[cfg(not(any(PyPy, GraalPy)))] -pyobject_native_type!( - PySet, - ffi::PySetObject, - pyobject_native_static_type_object!(ffi::PySet_Type), - #checkfunction=ffi::PySet_Check -); - -#[cfg(any(PyPy, GraalPy))] -pyobject_native_type_core!( - PySet, - pyobject_native_static_type_object!(ffi::PySet_Type), - #checkfunction=ffi::PySet_Check -); - -impl PySet { - /// Creates a new set with elements from the given slice. - /// - /// Returns an error if some element is not hashable. - #[inline] - pub fn new<'py, T>( - py: Python<'py>, - elements: impl IntoIterator, - ) -> PyResult> - where - T: IntoPyObject<'py>, - { - try_new_from_iter(py, elements) - } - - /// Deprecated name for [`PySet::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PySet::new`")] - #[allow(deprecated)] - #[inline] - pub fn new_bound<'a, 'p, T: ToPyObject + 'a>( - py: Python<'p>, - elements: impl IntoIterator, - ) -> PyResult> { - Self::new(py, elements.into_iter().map(|e| e.to_object(py))) - } - - /// Creates a new empty set. - pub fn empty(py: Python<'_>) -> PyResult> { - unsafe { - ffi::PySet_New(ptr::null_mut()) - .assume_owned_or_err(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PySet::empty`]. - #[deprecated(since = "0.23.0", note = "renamed to `PySet::empty`")] - #[inline] - pub fn empty_bound(py: Python<'_>) -> PyResult> { - Self::empty(py) - } -} - -/// Implementation of functionality for [`PySet`]. -/// -/// These methods are defined for the `Bound<'py, PySet>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PySet")] -pub trait PySetMethods<'py>: crate::sealed::Sealed { - /// Removes all elements from the set. - fn clear(&self); - - /// Returns the number of items in the set. - /// - /// This is equivalent to the Python expression `len(self)`. - fn len(&self) -> usize; - - /// Checks if set is empty. - fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Determines if the set contains the specified key. - /// - /// This is equivalent to the Python expression `key in self`. - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>; - - /// Removes the element from the set if it is present. - /// - /// Returns `true` if the element was present in the set. - fn discard(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>; - - /// Adds an element to the set. - fn add(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>; - - /// Removes and returns an arbitrary element from the set. - fn pop(&self) -> Option>; - - /// Returns an iterator of values in this set. - /// - /// # Panics - /// - /// If PyO3 detects that the set is mutated during iteration, it will panic. - fn iter(&self) -> BoundSetIterator<'py>; -} - -impl<'py> PySetMethods<'py> for Bound<'py, PySet> { - #[inline] - fn clear(&self) { - unsafe { - ffi::PySet_Clear(self.as_ptr()); - } - } - - #[inline] - fn len(&self) -> usize { - unsafe { ffi::PySet_Size(self.as_ptr()) as usize } - } - - fn contains(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>, - { - fn inner(set: &Bound<'_, PySet>, key: Borrowed<'_, '_, PyAny>) -> PyResult { - match unsafe { ffi::PySet_Contains(set.as_ptr(), key.as_ptr()) } { - 1 => Ok(true), - 0 => Ok(false), - _ => Err(PyErr::fetch(set.py())), - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn discard(&self, key: K) -> PyResult - where - K: IntoPyObject<'py>, - { - fn inner(set: &Bound<'_, PySet>, key: Borrowed<'_, '_, PyAny>) -> PyResult { - match unsafe { ffi::PySet_Discard(set.as_ptr(), key.as_ptr()) } { - 1 => Ok(true), - 0 => Ok(false), - _ => Err(PyErr::fetch(set.py())), - } - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn add(&self, key: K) -> PyResult<()> - where - K: IntoPyObject<'py>, - { - fn inner(set: &Bound<'_, PySet>, key: Borrowed<'_, '_, PyAny>) -> PyResult<()> { - err::error_on_minusone(set.py(), unsafe { - ffi::PySet_Add(set.as_ptr(), key.as_ptr()) - }) - } - - let py = self.py(); - inner( - self, - key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(), - ) - } - - fn pop(&self) -> Option> { - let element = unsafe { ffi::PySet_Pop(self.as_ptr()).assume_owned_or_err(self.py()) }; - match element { - Ok(e) => Some(e), - Err(_) => None, - } - } - - fn iter(&self) -> BoundSetIterator<'py> { - BoundSetIterator::new(self.clone()) - } -} - -impl<'py> IntoIterator for Bound<'py, PySet> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundSetIterator<'py>; - - /// Returns an iterator of values in this set. - /// - /// # Panics - /// - /// If PyO3 detects that the set is mutated during iteration, it will panic. - fn into_iter(self) -> Self::IntoIter { - BoundSetIterator::new(self) - } -} - -impl<'py> IntoIterator for &Bound<'py, PySet> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundSetIterator<'py>; - - /// Returns an iterator of values in this set. - /// - /// # Panics - /// - /// If PyO3 detects that the set is mutated during iteration, it will panic. - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -/// PyO3 implementation of an iterator for a Python `set` object. -pub struct BoundSetIterator<'p> { - it: Bound<'p, PyIterator>, - // Remaining elements in the set. This is fine to store because - // Python will error if the set changes size during iteration. - remaining: usize, -} - -impl<'py> BoundSetIterator<'py> { - pub(super) fn new(set: Bound<'py, PySet>) -> Self { - Self { - it: PyIterator::from_object(&set).unwrap(), - remaining: set.len(), - } - } -} - -impl<'py> Iterator for BoundSetIterator<'py> { - type Item = Bound<'py, super::PyAny>; - - /// Advances the iterator and returns the next value. - fn next(&mut self) -> Option { - self.remaining = self.remaining.saturating_sub(1); - self.it.next().map(Result::unwrap) - } - - fn size_hint(&self) -> (usize, Option) { - (self.remaining, Some(self.remaining)) - } -} - -impl ExactSizeIterator for BoundSetIterator<'_> { - fn len(&self) -> usize { - self.remaining - } -} - -#[allow(deprecated)] -#[inline] -pub(crate) fn new_from_iter( - py: Python<'_>, - elements: impl IntoIterator, -) -> PyResult> { - let mut iter = elements.into_iter().map(|e| e.to_object(py)); - try_new_from_iter(py, &mut iter) -} - -#[inline] -pub(crate) fn try_new_from_iter<'py, T>( - py: Python<'py>, - elements: impl IntoIterator, -) -> PyResult> -where - T: IntoPyObject<'py>, -{ - let set = unsafe { - // We create the `Bound` pointer because its Drop cleans up the set if - // user code errors or panics. - ffi::PySet_New(std::ptr::null_mut()) - .assume_owned_or_err(py)? - .downcast_into_unchecked() - }; - let ptr = set.as_ptr(); - - elements.into_iter().try_for_each(|element| { - let obj = element.into_pyobject_or_pyerr(py)?; - err::error_on_minusone(py, unsafe { ffi::PySet_Add(ptr, obj.as_ptr()) }) - })?; - - Ok(set) -} - -#[cfg(test)] -mod tests { - use super::PySet; - use crate::{ - conversion::IntoPyObject, - ffi, - types::{PyAnyMethods, PySetMethods}, - Python, - }; - use std::collections::HashSet; - - #[test] - fn test_set_new() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - assert_eq!(1, set.len()); - - let v = vec![1]; - assert!(PySet::new(py, &[v]).is_err()); - }); - } - - #[test] - fn test_set_empty() { - Python::with_gil(|py| { - let set = PySet::empty(py).unwrap(); - assert_eq!(0, set.len()); - assert!(set.is_empty()); - }); - } - - #[test] - fn test_set_len() { - Python::with_gil(|py| { - let mut v = HashSet::::new(); - let ob = (&v).into_pyobject(py).unwrap(); - let set = ob.downcast::().unwrap(); - assert_eq!(0, set.len()); - v.insert(7); - let ob = v.into_pyobject(py).unwrap(); - let set2 = ob.downcast::().unwrap(); - assert_eq!(1, set2.len()); - }); - } - - #[test] - fn test_set_clear() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - assert_eq!(1, set.len()); - set.clear(); - assert_eq!(0, set.len()); - }); - } - - #[test] - fn test_set_contains() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - assert!(set.contains(1).unwrap()); - }); - } - - #[test] - fn test_set_discard() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - assert!(!set.discard(2).unwrap()); - assert_eq!(1, set.len()); - - assert!(set.discard(1).unwrap()); - assert_eq!(0, set.len()); - assert!(!set.discard(1).unwrap()); - - assert!(set.discard(vec![1, 2]).is_err()); - }); - } - - #[test] - fn test_set_add() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2]).unwrap(); - set.add(1).unwrap(); // Add a dupliated element - assert!(set.contains(1).unwrap()); - }); - } - - #[test] - fn test_set_pop() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - let val = set.pop(); - assert!(val.is_some()); - let val2 = set.pop(); - assert!(val2.is_none()); - assert!(py - .eval( - ffi::c_str!("print('Exception state should not be set.')"), - None, - None - ) - .is_ok()); - }); - } - - #[test] - fn test_set_iter() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - - for el in set { - assert_eq!(1i32, el.extract::<'_, i32>().unwrap()); - } - }); - } - - #[test] - fn test_set_iter_bound() { - use crate::types::any::PyAnyMethods; - - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - - for el in &set { - assert_eq!(1i32, el.extract::().unwrap()); - } - }); - } - - #[test] - #[should_panic] - fn test_set_iter_mutation() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2, 3, 4, 5]).unwrap(); - - for _ in &set { - let _ = set.add(42); - } - }); - } - - #[test] - #[should_panic] - fn test_set_iter_mutation_same_len() { - Python::with_gil(|py| { - let set = PySet::new(py, [1, 2, 3, 4, 5]).unwrap(); - - for item in &set { - let item: i32 = item.extract().unwrap(); - let _ = set.del_item(item); - let _ = set.add(item + 10); - } - }); - } - - #[test] - fn test_set_iter_size_hint() { - Python::with_gil(|py| { - let set = PySet::new(py, [1]).unwrap(); - let mut iter = set.iter(); - - // Exact size - assert_eq!(iter.len(), 1); - assert_eq!(iter.size_hint(), (1, Some(1))); - iter.next(); - assert_eq!(iter.len(), 0); - assert_eq!(iter.size_hint(), (0, Some(0))); - }); - } -} diff --git a/vendor/pyo3/src/types/slice.rs b/vendor/pyo3/src/types/slice.rs deleted file mode 100644 index 9ca2aa4ec4..0000000000 --- a/vendor/pyo3/src/types/slice.rs +++ /dev/null @@ -1,271 +0,0 @@ -use crate::err::{PyErr, PyResult}; -use crate::ffi; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::types::any::PyAnyMethods; -#[allow(deprecated)] -use crate::ToPyObject; -use crate::{Bound, IntoPyObject, PyAny, PyObject, Python}; -use std::convert::Infallible; - -/// Represents a Python `slice`. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PySlice>`][Bound]. -/// -/// For APIs available on `slice` objects, see the [`PySliceMethods`] trait which is implemented for -/// [`Bound<'py, PySlice>`][Bound]. -/// -/// Only `isize` indices supported at the moment by the `PySlice` object. -#[repr(transparent)] -pub struct PySlice(PyAny); - -pyobject_native_type!( - PySlice, - ffi::PySliceObject, - pyobject_native_static_type_object!(ffi::PySlice_Type), - #checkfunction=ffi::PySlice_Check -); - -/// Return value from [`PySliceMethods::indices`]. -#[derive(Debug, Eq, PartialEq)] -pub struct PySliceIndices { - /// Start of the slice - /// - /// It can be -1 when the step is negative, otherwise it's non-negative. - pub start: isize, - /// End of the slice - /// - /// It can be -1 when the step is negative, otherwise it's non-negative. - pub stop: isize, - /// Increment to use when iterating the slice from `start` to `stop`. - pub step: isize, - /// The length of the slice calculated from the original input sequence. - pub slicelength: usize, -} - -impl PySliceIndices { - /// Creates a new `PySliceIndices`. - pub fn new(start: isize, stop: isize, step: isize) -> PySliceIndices { - PySliceIndices { - start, - stop, - step, - slicelength: 0, - } - } -} - -impl PySlice { - /// Constructs a new slice with the given elements. - pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> Bound<'_, PySlice> { - unsafe { - ffi::PySlice_New( - ffi::PyLong_FromSsize_t(start), - ffi::PyLong_FromSsize_t(stop), - ffi::PyLong_FromSsize_t(step), - ) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PySlice::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PySlice::new`")] - #[inline] - pub fn new_bound(py: Python<'_>, start: isize, stop: isize, step: isize) -> Bound<'_, PySlice> { - Self::new(py, start, stop, step) - } - - /// Constructs a new full slice that is equivalent to `::`. - pub fn full(py: Python<'_>) -> Bound<'_, PySlice> { - unsafe { - ffi::PySlice_New(ffi::Py_None(), ffi::Py_None(), ffi::Py_None()) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PySlice::full`]. - #[deprecated(since = "0.23.0", note = "renamed to `PySlice::full`")] - #[inline] - pub fn full_bound(py: Python<'_>) -> Bound<'_, PySlice> { - Self::full(py) - } -} - -/// Implementation of functionality for [`PySlice`]. -/// -/// These methods are defined for the `Bound<'py, PyTuple>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PySlice")] -pub trait PySliceMethods<'py>: crate::sealed::Sealed { - /// Retrieves the start, stop, and step indices from the slice object, - /// assuming a sequence of length `length`, and stores the length of the - /// slice in its `slicelength` member. - fn indices(&self, length: isize) -> PyResult; -} - -impl<'py> PySliceMethods<'py> for Bound<'py, PySlice> { - fn indices(&self, length: isize) -> PyResult { - unsafe { - let mut slicelength: isize = 0; - let mut start: isize = 0; - let mut stop: isize = 0; - let mut step: isize = 0; - let r = ffi::PySlice_GetIndicesEx( - self.as_ptr(), - length, - &mut start, - &mut stop, - &mut step, - &mut slicelength, - ); - if r == 0 { - Ok(PySliceIndices { - start, - stop, - step, - // non-negative isize should always fit into usize - slicelength: slicelength as _, - }) - } else { - Err(PyErr::fetch(self.py())) - } - } - } -} - -#[allow(deprecated)] -impl ToPyObject for PySliceIndices { - fn to_object(&self, py: Python<'_>) -> PyObject { - PySlice::new(py, self.start, self.stop, self.step).into() - } -} - -impl<'py> IntoPyObject<'py> for PySliceIndices { - type Target = PySlice; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PySlice::new(py, self.start, self.stop, self.step)) - } -} - -impl<'py> IntoPyObject<'py> for &PySliceIndices { - type Target = PySlice; - type Output = Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(PySlice::new(py, self.start, self.stop, self.step)) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_py_slice_new() { - Python::with_gil(|py| { - let slice = PySlice::new(py, isize::MIN, isize::MAX, 1); - assert_eq!( - slice.getattr("start").unwrap().extract::().unwrap(), - isize::MIN - ); - assert_eq!( - slice.getattr("stop").unwrap().extract::().unwrap(), - isize::MAX - ); - assert_eq!( - slice.getattr("step").unwrap().extract::().unwrap(), - 1 - ); - }); - } - - #[test] - fn test_py_slice_full() { - Python::with_gil(|py| { - let slice = PySlice::full(py); - assert!(slice.getattr("start").unwrap().is_none(),); - assert!(slice.getattr("stop").unwrap().is_none(),); - assert!(slice.getattr("step").unwrap().is_none(),); - assert_eq!( - slice.indices(0).unwrap(), - PySliceIndices { - start: 0, - stop: 0, - step: 1, - slicelength: 0, - }, - ); - assert_eq!( - slice.indices(42).unwrap(), - PySliceIndices { - start: 0, - stop: 42, - step: 1, - slicelength: 42, - }, - ); - }); - } - - #[test] - fn test_py_slice_indices_new() { - let start = 0; - let stop = 0; - let step = 0; - assert_eq!( - PySliceIndices::new(start, stop, step), - PySliceIndices { - start, - stop, - step, - slicelength: 0 - } - ); - - let start = 0; - let stop = 100; - let step = 10; - assert_eq!( - PySliceIndices::new(start, stop, step), - PySliceIndices { - start, - stop, - step, - slicelength: 0 - } - ); - - let start = 0; - let stop = -10; - let step = -1; - assert_eq!( - PySliceIndices::new(start, stop, step), - PySliceIndices { - start, - stop, - step, - slicelength: 0 - } - ); - - let start = 0; - let stop = -10; - let step = 20; - assert_eq!( - PySliceIndices::new(start, stop, step), - PySliceIndices { - start, - stop, - step, - slicelength: 0 - } - ); - } -} diff --git a/vendor/pyo3/src/types/string.rs b/vendor/pyo3/src/types/string.rs deleted file mode 100644 index 65a9e85fa3..0000000000 --- a/vendor/pyo3/src/types/string.rs +++ /dev/null @@ -1,879 +0,0 @@ -#[cfg(not(Py_LIMITED_API))] -use crate::exceptions::PyUnicodeDecodeError; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::instance::Borrowed; -use crate::py_result_ext::PyResultExt; -use crate::types::any::PyAnyMethods; -use crate::types::bytes::PyBytesMethods; -use crate::types::PyBytes; -#[allow(deprecated)] -use crate::IntoPy; -use crate::{ffi, Bound, Py, PyAny, PyResult, Python}; -use std::borrow::Cow; -use std::str; - -/// Deprecated alias for [`PyString`]. -#[deprecated(since = "0.23.0", note = "use `PyString` instead")] -pub type PyUnicode = PyString; - -/// Represents raw data backing a Python `str`. -/// -/// Python internally stores strings in various representations. This enumeration -/// represents those variations. -#[cfg(not(Py_LIMITED_API))] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum PyStringData<'a> { - /// UCS1 representation. - Ucs1(&'a [u8]), - - /// UCS2 representation. - Ucs2(&'a [u16]), - - /// UCS4 representation. - Ucs4(&'a [u32]), -} - -#[cfg(not(Py_LIMITED_API))] -impl<'a> PyStringData<'a> { - /// Obtain the raw bytes backing this instance as a [u8] slice. - pub fn as_bytes(&self) -> &[u8] { - match self { - Self::Ucs1(s) => s, - Self::Ucs2(s) => unsafe { - std::slice::from_raw_parts(s.as_ptr().cast(), s.len() * self.value_width_bytes()) - }, - Self::Ucs4(s) => unsafe { - std::slice::from_raw_parts(s.as_ptr().cast(), s.len() * self.value_width_bytes()) - }, - } - } - - /// Size in bytes of each value/item in the underlying slice. - #[inline] - pub fn value_width_bytes(&self) -> usize { - match self { - Self::Ucs1(_) => 1, - Self::Ucs2(_) => 2, - Self::Ucs4(_) => 4, - } - } - - /// Convert the raw data to a Rust string. - /// - /// For UCS-1 / UTF-8, returns a borrow into the original slice. For UCS-2 and UCS-4, - /// returns an owned string. - /// - /// Returns [PyUnicodeDecodeError] if the string data isn't valid in its purported - /// storage format. This should only occur for strings that were created via Python - /// C APIs that skip input validation (like `PyUnicode_FromKindAndData`) and should - /// never occur for strings that were created from Python code. - pub fn to_string(self, py: Python<'_>) -> PyResult> { - use std::ffi::CStr; - match self { - Self::Ucs1(data) => match str::from_utf8(data) { - Ok(s) => Ok(Cow::Borrowed(s)), - Err(e) => Err(PyUnicodeDecodeError::new_utf8(py, data, e)?.into()), - }, - Self::Ucs2(data) => match String::from_utf16(data) { - Ok(s) => Ok(Cow::Owned(s)), - Err(e) => { - let mut message = e.to_string().as_bytes().to_vec(); - message.push(0); - - Err(PyUnicodeDecodeError::new( - py, - ffi::c_str!("utf-16"), - self.as_bytes(), - 0..self.as_bytes().len(), - CStr::from_bytes_with_nul(&message).unwrap(), - )? - .into()) - } - }, - Self::Ucs4(data) => match data.iter().map(|&c| std::char::from_u32(c)).collect() { - Some(s) => Ok(Cow::Owned(s)), - None => Err(PyUnicodeDecodeError::new( - py, - ffi::c_str!("utf-32"), - self.as_bytes(), - 0..self.as_bytes().len(), - ffi::c_str!("error converting utf-32"), - )? - .into()), - }, - } - } - - /// Convert the raw data to a Rust string, possibly with data loss. - /// - /// Invalid code points will be replaced with `U+FFFD REPLACEMENT CHARACTER`. - /// - /// Returns a borrow into original data, when possible, or owned data otherwise. - /// - /// The return value of this function should only disagree with [Self::to_string] - /// when that method would error. - pub fn to_string_lossy(self) -> Cow<'a, str> { - match self { - Self::Ucs1(data) => String::from_utf8_lossy(data), - Self::Ucs2(data) => Cow::Owned(String::from_utf16_lossy(data)), - Self::Ucs4(data) => Cow::Owned( - data.iter() - .map(|&c| std::char::from_u32(c).unwrap_or('\u{FFFD}')) - .collect(), - ), - } - } -} - -/// Represents a Python `string` (a Unicode string object). -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyString>`][Bound]. -/// -/// For APIs available on `str` objects, see the [`PyStringMethods`] trait which is implemented for -/// [`Bound<'py, PyString>`][Bound]. -/// -/// # Equality -/// -/// For convenience, [`Bound<'py, PyString>`] implements [`PartialEq`] to allow comparing the -/// data in the Python string to a Rust UTF-8 string slice. -/// -/// This is not always the most appropriate way to compare Python strings, as Python string subclasses -/// may have different equality semantics. In situations where subclasses overriding equality might be -/// relevant, use [`PyAnyMethods::eq`], at cost of the additional overhead of a Python method call. -/// -/// ```rust -/// # use pyo3::prelude::*; -/// use pyo3::types::PyString; -/// -/// # Python::with_gil(|py| { -/// let py_string = PyString::new(py, "foo"); -/// // via PartialEq -/// assert_eq!(py_string, "foo"); -/// -/// // via Python equality -/// assert!(py_string.as_any().eq("foo").unwrap()); -/// # }); -/// ``` -#[repr(transparent)] -pub struct PyString(PyAny); - -pyobject_native_type_core!(PyString, pyobject_native_static_type_object!(ffi::PyUnicode_Type), #checkfunction=ffi::PyUnicode_Check); - -impl PyString { - /// Creates a new Python string object. - /// - /// Panics if out of memory. - pub fn new<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> { - let ptr = s.as_ptr().cast(); - let len = s.len() as ffi::Py_ssize_t; - unsafe { - ffi::PyUnicode_FromStringAndSize(ptr, len) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyString::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyString::new`")] - #[inline] - pub fn new_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> { - Self::new(py, s) - } - - /// Intern the given string - /// - /// This will return a reference to the same Python string object if called repeatedly with the same string. - /// - /// Note that while this is more memory efficient than [`PyString::new_bound`], it unconditionally allocates a - /// temporary Python string object and is thereby slower than [`PyString::new_bound`]. - /// - /// Panics if out of memory. - pub fn intern<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> { - let ptr = s.as_ptr().cast(); - let len = s.len() as ffi::Py_ssize_t; - unsafe { - let mut ob = ffi::PyUnicode_FromStringAndSize(ptr, len); - if !ob.is_null() { - ffi::PyUnicode_InternInPlace(&mut ob); - } - ob.assume_owned(py).downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyString::intern`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyString::intern`")] - #[inline] - pub fn intern_bound<'py>(py: Python<'py>, s: &str) -> Bound<'py, PyString> { - Self::intern(py, s) - } - - /// Attempts to create a Python string from a Python [bytes-like object]. - /// - /// [bytes-like object]: (https://docs.python.org/3/glossary.html#term-bytes-like-object). - pub fn from_object<'py>( - src: &Bound<'py, PyAny>, - encoding: &str, - errors: &str, - ) -> PyResult> { - unsafe { - ffi::PyUnicode_FromEncodedObject( - src.as_ptr(), - encoding.as_ptr().cast(), - errors.as_ptr().cast(), - ) - .assume_owned_or_err(src.py()) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyString::from_object`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyString::from_object`")] - #[inline] - pub fn from_object_bound<'py>( - src: &Bound<'py, PyAny>, - encoding: &str, - errors: &str, - ) -> PyResult> { - Self::from_object(src, encoding, errors) - } -} - -/// Implementation of functionality for [`PyString`]. -/// -/// These methods are defined for the `Bound<'py, PyString>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyString")] -pub trait PyStringMethods<'py>: crate::sealed::Sealed { - /// Gets the Python string as a Rust UTF-8 string slice. - /// - /// Returns a `UnicodeEncodeError` if the input is not valid unicode - /// (containing unpaired surrogates). - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn to_str(&self) -> PyResult<&str>; - - /// Converts the `PyString` into a Rust string, avoiding copying when possible. - /// - /// Returns a `UnicodeEncodeError` if the input is not valid unicode - /// (containing unpaired surrogates). - fn to_cow(&self) -> PyResult>; - - /// Converts the `PyString` into a Rust string. - /// - /// Unpaired surrogates invalid UTF-8 sequences are - /// replaced with `U+FFFD REPLACEMENT CHARACTER`. - fn to_string_lossy(&self) -> Cow<'_, str>; - - /// Encodes this string as a Python `bytes` object, using UTF-8 encoding. - fn encode_utf8(&self) -> PyResult>; - - /// Obtains the raw data backing the Python string. - /// - /// If the Python string object was created through legacy APIs, its internal storage format - /// will be canonicalized before data is returned. - /// - /// # Safety - /// - /// This function implementation relies on manually decoding a C bitfield. In practice, this - /// works well on common little-endian architectures such as x86_64, where the bitfield has a - /// common representation (even if it is not part of the C spec). The PyO3 CI tests this API on - /// x86_64 platforms. - /// - /// By using this API, you accept responsibility for testing that PyStringData behaves as - /// expected on the targets where you plan to distribute your software. - #[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))] - unsafe fn data(&self) -> PyResult>; -} - -impl<'py> PyStringMethods<'py> for Bound<'py, PyString> { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - fn to_str(&self) -> PyResult<&str> { - self.as_borrowed().to_str() - } - - fn to_cow(&self) -> PyResult> { - self.as_borrowed().to_cow() - } - - fn to_string_lossy(&self) -> Cow<'_, str> { - self.as_borrowed().to_string_lossy() - } - - fn encode_utf8(&self) -> PyResult> { - unsafe { - ffi::PyUnicode_AsUTF8String(self.as_ptr()) - .assume_owned_or_err(self.py()) - .downcast_into_unchecked::() - } - } - - #[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))] - unsafe fn data(&self) -> PyResult> { - self.as_borrowed().data() - } -} - -impl<'a> Borrowed<'a, '_, PyString> { - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_str(self) -> PyResult<&'a str> { - // PyUnicode_AsUTF8AndSize only available on limited API starting with 3.10. - let mut size: ffi::Py_ssize_t = 0; - let data: *const u8 = - unsafe { ffi::PyUnicode_AsUTF8AndSize(self.as_ptr(), &mut size).cast() }; - if data.is_null() { - Err(crate::PyErr::fetch(self.py())) - } else { - Ok(unsafe { - std::str::from_utf8_unchecked(std::slice::from_raw_parts(data, size as usize)) - }) - } - } - - #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_cow(self) -> PyResult> { - // TODO: this method can probably be deprecated once Python 3.9 support is dropped, - // because all versions then support the more efficient `to_str`. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - { - self.to_str().map(Cow::Borrowed) - } - - #[cfg(not(any(Py_3_10, not(Py_LIMITED_API))))] - { - let bytes = self.encode_utf8()?; - Ok(Cow::Owned( - unsafe { str::from_utf8_unchecked(bytes.as_bytes()) }.to_owned(), - )) - } - } - - #[allow(clippy::wrong_self_convention)] - fn to_string_lossy(self) -> Cow<'a, str> { - let ptr = self.as_ptr(); - let py = self.py(); - - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - if let Ok(s) = self.to_str() { - return Cow::Borrowed(s); - } - - let bytes = unsafe { - ffi::PyUnicode_AsEncodedString( - ptr, - ffi::c_str!("utf-8").as_ptr(), - ffi::c_str!("surrogatepass").as_ptr(), - ) - .assume_owned(py) - .downcast_into_unchecked::() - }; - Cow::Owned(String::from_utf8_lossy(bytes.as_bytes()).into_owned()) - } - - #[cfg(not(any(Py_LIMITED_API, GraalPy, PyPy)))] - unsafe fn data(self) -> PyResult> { - let ptr = self.as_ptr(); - - #[cfg(not(Py_3_12))] - #[allow(deprecated)] - { - let ready = ffi::PyUnicode_READY(ptr); - if ready != 0 { - // Exception was created on failure. - return Err(crate::PyErr::fetch(self.py())); - } - } - - // The string should be in its canonical form after calling `PyUnicode_READY()`. - // And non-canonical form not possible after Python 3.12. So it should be safe - // to call these APIs. - let length = ffi::PyUnicode_GET_LENGTH(ptr) as usize; - let raw_data = ffi::PyUnicode_DATA(ptr); - let kind = ffi::PyUnicode_KIND(ptr); - - match kind { - ffi::PyUnicode_1BYTE_KIND => Ok(PyStringData::Ucs1(std::slice::from_raw_parts( - raw_data as *const u8, - length, - ))), - ffi::PyUnicode_2BYTE_KIND => Ok(PyStringData::Ucs2(std::slice::from_raw_parts( - raw_data as *const u16, - length, - ))), - ffi::PyUnicode_4BYTE_KIND => Ok(PyStringData::Ucs4(std::slice::from_raw_parts( - raw_data as *const u32, - length, - ))), - _ => unreachable!(), - } - } -} - -impl Py { - /// Gets the Python string as a Rust UTF-8 string slice. - /// - /// Returns a `UnicodeEncodeError` if the input is not valid unicode - /// (containing unpaired surrogates). - /// - /// Because `str` objects are immutable, the returned slice is independent of - /// the GIL lifetime. - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - pub fn to_str<'a>(&'a self, py: Python<'_>) -> PyResult<&'a str> { - self.bind_borrowed(py).to_str() - } - - /// Converts the `PyString` into a Rust string, avoiding copying when possible. - /// - /// Returns a `UnicodeEncodeError` if the input is not valid unicode - /// (containing unpaired surrogates). - /// - /// Because `str` objects are immutable, the returned slice is independent of - /// the GIL lifetime. - pub fn to_cow<'a>(&'a self, py: Python<'_>) -> PyResult> { - self.bind_borrowed(py).to_cow() - } - - /// Converts the `PyString` into a Rust string. - /// - /// Unpaired surrogates invalid UTF-8 sequences are - /// replaced with `U+FFFD REPLACEMENT CHARACTER`. - /// - /// Because `str` objects are immutable, the returned slice is independent of - /// the GIL lifetime. - pub fn to_string_lossy<'a>(&'a self, py: Python<'_>) -> Cow<'a, str> { - self.bind_borrowed(py).to_string_lossy() - } -} - -#[allow(deprecated)] -impl IntoPy> for Bound<'_, PyString> { - fn into_py(self, _py: Python<'_>) -> Py { - self.unbind() - } -} - -#[allow(deprecated)] -impl IntoPy> for &Bound<'_, PyString> { - fn into_py(self, _py: Python<'_>) -> Py { - self.clone().unbind() - } -} - -#[allow(deprecated)] -impl IntoPy> for &'_ Py { - fn into_py(self, py: Python<'_>) -> Py { - self.clone_ref(py) - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq for Bound<'_, PyString> { - #[inline] - fn eq(&self, other: &str) -> bool { - self.as_borrowed() == *other - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq<&'_ str> for Bound<'_, PyString> { - #[inline] - fn eq(&self, other: &&str) -> bool { - self.as_borrowed() == **other - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq> for str { - #[inline] - fn eq(&self, other: &Bound<'_, PyString>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq<&'_ Bound<'_, PyString>> for str { - #[inline] - fn eq(&self, other: &&Bound<'_, PyString>) -> bool { - *self == other.as_borrowed() - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq> for &'_ str { - #[inline] - fn eq(&self, other: &Bound<'_, PyString>) -> bool { - **self == other.as_borrowed() - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq for &'_ Bound<'_, PyString> { - #[inline] - fn eq(&self, other: &str) -> bool { - self.as_borrowed() == other - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq for Borrowed<'_, '_, PyString> { - #[inline] - fn eq(&self, other: &str) -> bool { - #[cfg(not(Py_3_13))] - { - self.to_cow().map_or(false, |s| s == other) - } - - #[cfg(Py_3_13)] - unsafe { - ffi::PyUnicode_EqualToUTF8AndSize( - self.as_ptr(), - other.as_ptr().cast(), - other.len() as _, - ) == 1 - } - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq<&str> for Borrowed<'_, '_, PyString> { - #[inline] - fn eq(&self, other: &&str) -> bool { - *self == **other - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq> for str { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyString>) -> bool { - other == self - } -} - -/// Compares whether the data in the Python string is equal to the given UTF8. -/// -/// In some cases Python equality might be more appropriate; see the note on [`PyString`]. -impl PartialEq> for &'_ str { - #[inline] - fn eq(&self, other: &Borrowed<'_, '_, PyString>) -> bool { - other == self - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{IntoPyObject, PyObject}; - - #[test] - fn test_to_cow_utf8() { - Python::with_gil(|py| { - let s = "ascii 🐈"; - let py_string = PyString::new(py, s); - assert_eq!(s, py_string.to_cow().unwrap()); - }) - } - - #[test] - fn test_to_cow_surrogate() { - Python::with_gil(|py| { - let py_string = py - .eval(ffi::c_str!(r"'\ud800'"), None, None) - .unwrap() - .downcast_into::() - .unwrap(); - assert!(py_string.to_cow().is_err()); - }) - } - - #[test] - fn test_to_cow_unicode() { - Python::with_gil(|py| { - let s = "哈哈🐈"; - let py_string = PyString::new(py, s); - assert_eq!(s, py_string.to_cow().unwrap()); - }) - } - - #[test] - fn test_encode_utf8_unicode() { - Python::with_gil(|py| { - let s = "哈哈🐈"; - let obj = PyString::new(py, s); - assert_eq!(s.as_bytes(), obj.encode_utf8().unwrap().as_bytes()); - }) - } - - #[test] - fn test_encode_utf8_surrogate() { - Python::with_gil(|py| { - let obj: PyObject = py - .eval(ffi::c_str!(r"'\ud800'"), None, None) - .unwrap() - .into(); - assert!(obj - .bind(py) - .downcast::() - .unwrap() - .encode_utf8() - .is_err()); - }) - } - - #[test] - fn test_to_string_lossy() { - Python::with_gil(|py| { - let py_string = py - .eval(ffi::c_str!(r"'🐈 Hello \ud800World'"), None, None) - .unwrap() - .downcast_into::() - .unwrap(); - - assert_eq!(py_string.to_string_lossy(), "🐈 Hello ���World"); - }) - } - - #[test] - fn test_debug_string() { - Python::with_gil(|py| { - let s = "Hello\n".into_pyobject(py).unwrap(); - assert_eq!(format!("{:?}", s), "'Hello\\n'"); - }) - } - - #[test] - fn test_display_string() { - Python::with_gil(|py| { - let s = "Hello\n".into_pyobject(py).unwrap(); - assert_eq!(format!("{}", s), "Hello\n"); - }) - } - - #[test] - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - fn test_string_data_ucs1() { - Python::with_gil(|py| { - let s = PyString::new(py, "hello, world"); - let data = unsafe { s.data().unwrap() }; - - assert_eq!(data, PyStringData::Ucs1(b"hello, world")); - assert_eq!(data.to_string(py).unwrap(), Cow::Borrowed("hello, world")); - assert_eq!(data.to_string_lossy(), Cow::Borrowed("hello, world")); - }) - } - - #[test] - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - fn test_string_data_ucs1_invalid() { - Python::with_gil(|py| { - // 0xfe is not allowed in UTF-8. - let buffer = b"f\xfe\0"; - let ptr = unsafe { - crate::ffi::PyUnicode_FromKindAndData( - crate::ffi::PyUnicode_1BYTE_KIND as _, - buffer.as_ptr().cast(), - 2, - ) - }; - assert!(!ptr.is_null()); - let s = unsafe { ptr.assume_owned(py).downcast_into_unchecked::() }; - let data = unsafe { s.data().unwrap() }; - assert_eq!(data, PyStringData::Ucs1(b"f\xfe")); - let err = data.to_string(py).unwrap_err(); - assert!(err.get_type(py).is(&py.get_type::())); - assert!(err - .to_string() - .contains("'utf-8' codec can't decode byte 0xfe in position 1")); - assert_eq!(data.to_string_lossy(), Cow::Borrowed("f�")); - }); - } - - #[test] - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - fn test_string_data_ucs2() { - Python::with_gil(|py| { - let s = py.eval(ffi::c_str!("'foo\\ud800'"), None, None).unwrap(); - let py_string = s.downcast::().unwrap(); - let data = unsafe { py_string.data().unwrap() }; - - assert_eq!(data, PyStringData::Ucs2(&[102, 111, 111, 0xd800])); - assert_eq!( - data.to_string_lossy(), - Cow::Owned::("foo�".to_string()) - ); - }) - } - - #[test] - #[cfg(all(not(any(Py_LIMITED_API, PyPy)), target_endian = "little"))] - fn test_string_data_ucs2_invalid() { - Python::with_gil(|py| { - // U+FF22 (valid) & U+d800 (never valid) - let buffer = b"\x22\xff\x00\xd8\x00\x00"; - let ptr = unsafe { - crate::ffi::PyUnicode_FromKindAndData( - crate::ffi::PyUnicode_2BYTE_KIND as _, - buffer.as_ptr().cast(), - 2, - ) - }; - assert!(!ptr.is_null()); - let s = unsafe { ptr.assume_owned(py).downcast_into_unchecked::() }; - let data = unsafe { s.data().unwrap() }; - assert_eq!(data, PyStringData::Ucs2(&[0xff22, 0xd800])); - let err = data.to_string(py).unwrap_err(); - assert!(err.get_type(py).is(&py.get_type::())); - assert!(err - .to_string() - .contains("'utf-16' codec can't decode bytes in position 0-3")); - assert_eq!(data.to_string_lossy(), Cow::Owned::("B�".into())); - }); - } - - #[test] - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - fn test_string_data_ucs4() { - Python::with_gil(|py| { - let s = "哈哈🐈"; - let py_string = PyString::new(py, s); - let data = unsafe { py_string.data().unwrap() }; - - assert_eq!(data, PyStringData::Ucs4(&[21704, 21704, 128008])); - assert_eq!(data.to_string_lossy(), Cow::Owned::(s.to_string())); - }) - } - - #[test] - #[cfg(all(not(any(Py_LIMITED_API, PyPy)), target_endian = "little"))] - fn test_string_data_ucs4_invalid() { - Python::with_gil(|py| { - // U+20000 (valid) & U+d800 (never valid) - let buffer = b"\x00\x00\x02\x00\x00\xd8\x00\x00\x00\x00\x00\x00"; - let ptr = unsafe { - crate::ffi::PyUnicode_FromKindAndData( - crate::ffi::PyUnicode_4BYTE_KIND as _, - buffer.as_ptr().cast(), - 2, - ) - }; - assert!(!ptr.is_null()); - let s = unsafe { ptr.assume_owned(py).downcast_into_unchecked::() }; - let data = unsafe { s.data().unwrap() }; - assert_eq!(data, PyStringData::Ucs4(&[0x20000, 0xd800])); - let err = data.to_string(py).unwrap_err(); - assert!(err.get_type(py).is(&py.get_type::())); - assert!(err - .to_string() - .contains("'utf-32' codec can't decode bytes in position 0-7")); - assert_eq!(data.to_string_lossy(), Cow::Owned::("𠀀�".into())); - }); - } - - #[test] - fn test_intern_string() { - Python::with_gil(|py| { - let py_string1 = PyString::intern(py, "foo"); - assert_eq!(py_string1, "foo"); - - let py_string2 = PyString::intern(py, "foo"); - assert_eq!(py_string2, "foo"); - - assert_eq!(py_string1.as_ptr(), py_string2.as_ptr()); - - let py_string3 = PyString::intern(py, "bar"); - assert_eq!(py_string3, "bar"); - - assert_ne!(py_string1.as_ptr(), py_string3.as_ptr()); - }); - } - - #[test] - fn test_py_to_str_utf8() { - Python::with_gil(|py| { - let s = "ascii 🐈"; - let py_string = PyString::new(py, s).unbind(); - - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - assert_eq!(s, py_string.to_str(py).unwrap()); - - assert_eq!(s, py_string.to_cow(py).unwrap()); - }) - } - - #[test] - fn test_py_to_str_surrogate() { - Python::with_gil(|py| { - let py_string: Py = py - .eval(ffi::c_str!(r"'\ud800'"), None, None) - .unwrap() - .extract() - .unwrap(); - - #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - assert!(py_string.to_str(py).is_err()); - - assert!(py_string.to_cow(py).is_err()); - }) - } - - #[test] - fn test_py_to_string_lossy() { - Python::with_gil(|py| { - let py_string: Py = py - .eval(ffi::c_str!(r"'🐈 Hello \ud800World'"), None, None) - .unwrap() - .extract() - .unwrap(); - assert_eq!(py_string.to_string_lossy(py), "🐈 Hello ���World"); - }) - } - - #[test] - fn test_comparisons() { - Python::with_gil(|py| { - let s = "hello, world"; - let py_string = PyString::new(py, s); - - assert_eq!(py_string, "hello, world"); - - assert_eq!(py_string, s); - assert_eq!(&py_string, s); - assert_eq!(s, py_string); - assert_eq!(s, &py_string); - - assert_eq!(py_string, *s); - assert_eq!(&py_string, *s); - assert_eq!(*s, py_string); - assert_eq!(*s, &py_string); - - let py_string = py_string.as_borrowed(); - - assert_eq!(py_string, s); - assert_eq!(&py_string, s); - assert_eq!(s, py_string); - assert_eq!(s, &py_string); - - assert_eq!(py_string, *s); - assert_eq!(*s, py_string); - }) - } -} diff --git a/vendor/pyo3/src/types/traceback.rs b/vendor/pyo3/src/types/traceback.rs deleted file mode 100644 index 885c0f6703..0000000000 --- a/vendor/pyo3/src/types/traceback.rs +++ /dev/null @@ -1,152 +0,0 @@ -use crate::err::{error_on_minusone, PyResult}; -use crate::types::{any::PyAnyMethods, string::PyStringMethods, PyString}; -use crate::{ffi, Bound, PyAny}; - -/// Represents a Python traceback. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyTraceback>`][Bound]. -/// -/// For APIs available on traceback objects, see the [`PyTracebackMethods`] trait which is implemented for -/// [`Bound<'py, PyTraceback>`][Bound]. -#[repr(transparent)] -pub struct PyTraceback(PyAny); - -pyobject_native_type_core!( - PyTraceback, - pyobject_native_static_type_object!(ffi::PyTraceBack_Type), - #checkfunction=ffi::PyTraceBack_Check -); - -/// Implementation of functionality for [`PyTraceback`]. -/// -/// These methods are defined for the `Bound<'py, PyTraceback>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyTraceback")] -pub trait PyTracebackMethods<'py>: crate::sealed::Sealed { - /// Formats the traceback as a string. - /// - /// This does not include the exception type and value. The exception type and value can be - /// formatted using the `Display` implementation for `PyErr`. - /// - /// # Example - /// - /// The following code formats a Python traceback and exception pair from Rust: - /// - /// ```rust - /// # use pyo3::{Python, PyResult, prelude::PyTracebackMethods, ffi::c_str}; - /// # let result: PyResult<()> = - /// Python::with_gil(|py| { - /// let err = py - /// .run(c_str!("raise Exception('banana')"), None, None) - /// .expect_err("raise will create a Python error"); - /// - /// let traceback = err.traceback(py).expect("raised exception will have a traceback"); - /// assert_eq!( - /// format!("{}{}", traceback.format()?, err), - /// "\ - /// Traceback (most recent call last): - /// File \"\", line 1, in - /// Exception: banana\ - /// " - /// ); - /// Ok(()) - /// }) - /// # ; - /// # result.expect("example failed"); - /// ``` - fn format(&self) -> PyResult; -} - -impl<'py> PyTracebackMethods<'py> for Bound<'py, PyTraceback> { - fn format(&self) -> PyResult { - let py = self.py(); - let string_io = py - .import(intern!(py, "io"))? - .getattr(intern!(py, "StringIO"))? - .call0()?; - let result = unsafe { ffi::PyTraceBack_Print(self.as_ptr(), string_io.as_ptr()) }; - error_on_minusone(py, result)?; - let formatted = string_io - .getattr(intern!(py, "getvalue"))? - .call0()? - .downcast::()? - .to_cow()? - .into_owned(); - Ok(formatted) - } -} - -#[cfg(test)] -mod tests { - use crate::IntoPyObject; - use crate::{ - ffi, - types::{any::PyAnyMethods, dict::PyDictMethods, traceback::PyTracebackMethods, PyDict}, - PyErr, Python, - }; - - #[test] - fn format_traceback() { - Python::with_gil(|py| { - let err = py - .run(ffi::c_str!("raise Exception('banana')"), None, None) - .expect_err("raising should have given us an error"); - - assert_eq!( - err.traceback(py).unwrap().format().unwrap(), - "Traceback (most recent call last):\n File \"\", line 1, in \n" - ); - }) - } - - #[test] - fn test_err_from_value() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - // Produce an error from python so that it has a traceback - py.run( - ffi::c_str!( - r" -try: - raise ValueError('raised exception') -except Exception as e: - err = e -" - ), - None, - Some(&locals), - ) - .unwrap(); - let err = PyErr::from_value(locals.get_item("err").unwrap().unwrap()); - let traceback = err.value(py).getattr("__traceback__").unwrap(); - assert!(err.traceback(py).unwrap().is(&traceback)); - }) - } - - #[test] - fn test_err_into_py() { - Python::with_gil(|py| { - let locals = PyDict::new(py); - // Produce an error from python so that it has a traceback - py.run( - ffi::c_str!( - r" -def f(): - raise ValueError('raised exception') -" - ), - None, - Some(&locals), - ) - .unwrap(); - let f = locals.get_item("f").unwrap().unwrap(); - let err = f.call0().unwrap_err(); - let traceback = err.traceback(py).unwrap(); - let err_object = err.clone_ref(py).into_pyobject(py).unwrap(); - - assert!(err_object.getattr("__traceback__").unwrap().is(&traceback)); - }) - } -} diff --git a/vendor/pyo3/src/types/tuple.rs b/vendor/pyo3/src/types/tuple.rs deleted file mode 100644 index 216a376d83..0000000000 --- a/vendor/pyo3/src/types/tuple.rs +++ /dev/null @@ -1,1268 +0,0 @@ -use std::iter::FusedIterator; - -use crate::ffi::{self, Py_ssize_t}; -use crate::ffi_ptr_ext::FfiPtrExt; -#[cfg(feature = "experimental-inspect")] -use crate::inspect::types::TypeInfo; -use crate::instance::Borrowed; -use crate::internal_tricks::get_ssize_index; -use crate::types::{any::PyAnyMethods, sequence::PySequenceMethods, PyList, PySequence}; -use crate::{ - exceptions, Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, Py, PyAny, PyErr, PyObject, - PyResult, Python, -}; -#[allow(deprecated)] -use crate::{IntoPy, ToPyObject}; - -#[inline] -#[track_caller] -fn try_new_from_iter<'py>( - py: Python<'py>, - mut elements: impl ExactSizeIterator>>, -) -> PyResult> { - unsafe { - // PyTuple_New checks for overflow but has a bad error message, so we check ourselves - let len: Py_ssize_t = elements - .len() - .try_into() - .expect("out of range integral type conversion attempted on `elements.len()`"); - - let ptr = ffi::PyTuple_New(len); - - // - Panics if the ptr is null - // - Cleans up the tuple if `convert` or the asserts panic - let tup = ptr.assume_owned(py).downcast_into_unchecked(); - - let mut counter: Py_ssize_t = 0; - - for obj in (&mut elements).take(len as usize) { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - ffi::PyTuple_SET_ITEM(ptr, counter, obj?.into_ptr()); - #[cfg(any(Py_LIMITED_API, PyPy, GraalPy))] - ffi::PyTuple_SetItem(ptr, counter, obj?.into_ptr()); - counter += 1; - } - - assert!(elements.next().is_none(), "Attempted to create PyTuple but `elements` was larger than reported by its `ExactSizeIterator` implementation."); - assert_eq!(len, counter, "Attempted to create PyTuple but `elements` was smaller than reported by its `ExactSizeIterator` implementation."); - - Ok(tup) - } -} - -/// Represents a Python `tuple` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyTuple>`][Bound]. -/// -/// For APIs available on `tuple` objects, see the [`PyTupleMethods`] trait which is implemented for -/// [`Bound<'py, PyTuple>`][Bound]. -#[repr(transparent)] -pub struct PyTuple(PyAny); - -pyobject_native_type_core!(PyTuple, pyobject_native_static_type_object!(ffi::PyTuple_Type), #checkfunction=ffi::PyTuple_Check); - -impl PyTuple { - /// Constructs a new tuple with the given elements. - /// - /// If you want to create a [`PyTuple`] with elements of different or unknown types, or from an - /// iterable that doesn't implement [`ExactSizeIterator`], create a Rust tuple with the given - /// elements and convert it at once using `into_py`. - /// - /// # Examples - /// - /// ```rust - /// use pyo3::prelude::*; - /// use pyo3::types::PyTuple; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let elements: Vec = vec![0, 1, 2, 3, 4, 5]; - /// let tuple = PyTuple::new(py, elements)?; - /// assert_eq!(format!("{:?}", tuple), "(0, 1, 2, 3, 4, 5)"); - /// # Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// - /// This function will panic if `element`'s [`ExactSizeIterator`] implementation is incorrect. - /// All standard library structures implement this trait correctly, if they do, so calling this - /// function using [`Vec`]`` or `&[T]` will always succeed. - #[track_caller] - pub fn new<'py, T, U>( - py: Python<'py>, - elements: impl IntoIterator, - ) -> PyResult> - where - T: IntoPyObject<'py>, - U: ExactSizeIterator, - { - let elements = elements.into_iter().map(|e| e.into_bound_py_any(py)); - try_new_from_iter(py, elements) - } - - /// Deprecated name for [`PyTuple::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTuple::new`")] - #[allow(deprecated)] - #[track_caller] - #[inline] - pub fn new_bound( - py: Python<'_>, - elements: impl IntoIterator, - ) -> Bound<'_, PyTuple> - where - T: ToPyObject, - U: ExactSizeIterator, - { - PyTuple::new(py, elements.into_iter().map(|e| e.to_object(py))).unwrap() - } - - /// Constructs an empty tuple (on the Python side, a singleton object). - pub fn empty(py: Python<'_>) -> Bound<'_, PyTuple> { - unsafe { - ffi::PyTuple_New(0) - .assume_owned(py) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyTuple::empty`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyTuple::empty`")] - #[inline] - pub fn empty_bound(py: Python<'_>) -> Bound<'_, PyTuple> { - PyTuple::empty(py) - } -} - -/// Implementation of functionality for [`PyTuple`]. -/// -/// These methods are defined for the `Bound<'py, PyTuple>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyTuple")] -pub trait PyTupleMethods<'py>: crate::sealed::Sealed { - /// Gets the length of the tuple. - fn len(&self) -> usize; - - /// Checks if the tuple is empty. - fn is_empty(&self) -> bool; - - /// Returns `self` cast as a `PySequence`. - fn as_sequence(&self) -> &Bound<'py, PySequence>; - - /// Returns `self` cast as a `PySequence`. - fn into_sequence(self) -> Bound<'py, PySequence>; - - /// Takes the slice `self[low:high]` and returns it as a new tuple. - /// - /// Indices must be nonnegative, and out-of-range indices are clipped to - /// `self.len()`. - fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyTuple>; - - /// Gets the tuple item at the specified index. - /// # Example - /// ``` - /// use pyo3::prelude::*; - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| -> PyResult<()> { - /// let tuple = (1, 2, 3).into_pyobject(py)?; - /// let obj = tuple.get_item(0); - /// assert_eq!(obj?.extract::()?, 1); - /// Ok(()) - /// }) - /// # } - /// ``` - fn get_item(&self, index: usize) -> PyResult>; - - /// Like [`get_item`][PyTupleMethods::get_item], but returns a borrowed object, which is a slight performance optimization - /// by avoiding a reference count change. - fn get_borrowed_item<'a>(&'a self, index: usize) -> PyResult>; - - /// Gets the tuple item at the specified index. Undefined behavior on bad index. Use with caution. - /// - /// # Safety - /// - /// Caller must verify that the index is within the bounds of the tuple. - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; - - /// Like [`get_item_unchecked`][PyTupleMethods::get_item_unchecked], but returns a borrowed object, - /// which is a slight performance optimization by avoiding a reference count change. - /// - /// # Safety - /// - /// Caller must verify that the index is within the bounds of the tuple. - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe fn get_borrowed_item_unchecked<'a>(&'a self, index: usize) -> Borrowed<'a, 'py, PyAny>; - - /// Returns `self` as a slice of objects. - #[cfg(not(any(Py_LIMITED_API, GraalPy)))] - fn as_slice(&self) -> &[Bound<'py, PyAny>]; - - /// Determines if self contains `value`. - /// - /// This is equivalent to the Python expression `value in self`. - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns the first index `i` for which `self[i] == value`. - /// - /// This is equivalent to the Python expression `self.index(value)`. - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>; - - /// Returns an iterator over the tuple items. - fn iter(&self) -> BoundTupleIterator<'py>; - - /// Like [`iter`][PyTupleMethods::iter], but produces an iterator which returns borrowed objects, - /// which is a slight performance optimization by avoiding a reference count change. - fn iter_borrowed<'a>(&'a self) -> BorrowedTupleIterator<'a, 'py>; - - /// Return a new list containing the contents of this tuple; equivalent to the Python expression `list(tuple)`. - /// - /// This method is equivalent to `self.as_sequence().to_list()` and faster than `PyList::new(py, self)`. - fn to_list(&self) -> Bound<'py, PyList>; -} - -impl<'py> PyTupleMethods<'py> for Bound<'py, PyTuple> { - fn len(&self) -> usize { - unsafe { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - let size = ffi::PyTuple_GET_SIZE(self.as_ptr()); - #[cfg(any(Py_LIMITED_API, PyPy, GraalPy))] - let size = ffi::PyTuple_Size(self.as_ptr()); - // non-negative Py_ssize_t should always fit into Rust uint - size as usize - } - } - - fn is_empty(&self) -> bool { - self.len() == 0 - } - - fn as_sequence(&self) -> &Bound<'py, PySequence> { - unsafe { self.downcast_unchecked() } - } - - fn into_sequence(self) -> Bound<'py, PySequence> { - unsafe { self.into_any().downcast_into_unchecked() } - } - - fn get_slice(&self, low: usize, high: usize) -> Bound<'py, PyTuple> { - unsafe { - ffi::PyTuple_GetSlice(self.as_ptr(), get_ssize_index(low), get_ssize_index(high)) - .assume_owned(self.py()) - .downcast_into_unchecked() - } - } - - fn get_item(&self, index: usize) -> PyResult> { - self.get_borrowed_item(index).map(Borrowed::to_owned) - } - - fn get_borrowed_item<'a>(&'a self, index: usize) -> PyResult> { - self.as_borrowed().get_borrowed_item(index) - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny> { - self.get_borrowed_item_unchecked(index).to_owned() - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe fn get_borrowed_item_unchecked<'a>(&'a self, index: usize) -> Borrowed<'a, 'py, PyAny> { - self.as_borrowed().get_borrowed_item_unchecked(index) - } - - #[cfg(not(any(Py_LIMITED_API, GraalPy)))] - fn as_slice(&self) -> &[Bound<'py, PyAny>] { - // SAFETY: self is known to be a tuple object, and tuples are immutable - let items = unsafe { &(*self.as_ptr().cast::()).ob_item }; - // SAFETY: Bound<'py, PyAny> has the same memory layout as *mut ffi::PyObject - unsafe { std::slice::from_raw_parts(items.as_ptr().cast(), self.len()) } - } - - #[inline] - fn contains(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - self.as_sequence().contains(value) - } - - #[inline] - fn index(&self, value: V) -> PyResult - where - V: IntoPyObject<'py>, - { - self.as_sequence().index(value) - } - - fn iter(&self) -> BoundTupleIterator<'py> { - BoundTupleIterator::new(self.clone()) - } - - fn iter_borrowed<'a>(&'a self) -> BorrowedTupleIterator<'a, 'py> { - self.as_borrowed().iter_borrowed() - } - - fn to_list(&self) -> Bound<'py, PyList> { - self.as_sequence() - .to_list() - .expect("failed to convert tuple to list") - } -} - -impl<'a, 'py> Borrowed<'a, 'py, PyTuple> { - fn get_borrowed_item(self, index: usize) -> PyResult> { - unsafe { - ffi::PyTuple_GetItem(self.as_ptr(), index as Py_ssize_t) - .assume_borrowed_or_err(self.py()) - } - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe fn get_borrowed_item_unchecked(self, index: usize) -> Borrowed<'a, 'py, PyAny> { - ffi::PyTuple_GET_ITEM(self.as_ptr(), index as Py_ssize_t).assume_borrowed(self.py()) - } - - pub(crate) fn iter_borrowed(self) -> BorrowedTupleIterator<'a, 'py> { - BorrowedTupleIterator::new(self) - } -} - -/// Used by `PyTuple::into_iter()`. -pub struct BoundTupleIterator<'py> { - tuple: Bound<'py, PyTuple>, - index: usize, - length: usize, -} - -impl<'py> BoundTupleIterator<'py> { - fn new(tuple: Bound<'py, PyTuple>) -> Self { - let length = tuple.len(); - BoundTupleIterator { - tuple, - index: 0, - length, - } - } -} - -impl<'py> Iterator for BoundTupleIterator<'py> { - type Item = Bound<'py, PyAny>; - - #[inline] - fn next(&mut self) -> Option { - if self.index < self.length { - let item = unsafe { - BorrowedTupleIterator::get_item(self.tuple.as_borrowed(), self.index).to_owned() - }; - self.index += 1; - Some(item) - } else { - None - } - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } -} - -impl DoubleEndedIterator for BoundTupleIterator<'_> { - #[inline] - fn next_back(&mut self) -> Option { - if self.index < self.length { - let item = unsafe { - BorrowedTupleIterator::get_item(self.tuple.as_borrowed(), self.length - 1) - .to_owned() - }; - self.length -= 1; - Some(item) - } else { - None - } - } -} - -impl ExactSizeIterator for BoundTupleIterator<'_> { - fn len(&self) -> usize { - self.length.saturating_sub(self.index) - } -} - -impl FusedIterator for BoundTupleIterator<'_> {} - -impl<'py> IntoIterator for Bound<'py, PyTuple> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundTupleIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - BoundTupleIterator::new(self) - } -} - -impl<'py> IntoIterator for &Bound<'py, PyTuple> { - type Item = Bound<'py, PyAny>; - type IntoIter = BoundTupleIterator<'py>; - - fn into_iter(self) -> Self::IntoIter { - self.iter() - } -} - -/// Used by `PyTuple::iter_borrowed()`. -pub struct BorrowedTupleIterator<'a, 'py> { - tuple: Borrowed<'a, 'py, PyTuple>, - index: usize, - length: usize, -} - -impl<'a, 'py> BorrowedTupleIterator<'a, 'py> { - fn new(tuple: Borrowed<'a, 'py, PyTuple>) -> Self { - let length = tuple.len(); - BorrowedTupleIterator { - tuple, - index: 0, - length, - } - } - - unsafe fn get_item( - tuple: Borrowed<'a, 'py, PyTuple>, - index: usize, - ) -> Borrowed<'a, 'py, PyAny> { - #[cfg(any(Py_LIMITED_API, PyPy, GraalPy))] - let item = tuple.get_borrowed_item(index).expect("tuple.get failed"); - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - let item = tuple.get_borrowed_item_unchecked(index); - item - } -} - -impl<'a, 'py> Iterator for BorrowedTupleIterator<'a, 'py> { - type Item = Borrowed<'a, 'py, PyAny>; - - #[inline] - fn next(&mut self) -> Option { - if self.index < self.length { - let item = unsafe { Self::get_item(self.tuple, self.index) }; - self.index += 1; - Some(item) - } else { - None - } - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - let len = self.len(); - (len, Some(len)) - } -} - -impl DoubleEndedIterator for BorrowedTupleIterator<'_, '_> { - #[inline] - fn next_back(&mut self) -> Option { - if self.index < self.length { - let item = unsafe { Self::get_item(self.tuple, self.length - 1) }; - self.length -= 1; - Some(item) - } else { - None - } - } -} - -impl ExactSizeIterator for BorrowedTupleIterator<'_, '_> { - fn len(&self) -> usize { - self.length.saturating_sub(self.index) - } -} - -impl FusedIterator for BorrowedTupleIterator<'_, '_> {} - -#[allow(deprecated)] -impl IntoPy> for Bound<'_, PyTuple> { - fn into_py(self, _: Python<'_>) -> Py { - self.unbind() - } -} - -#[allow(deprecated)] -impl IntoPy> for &'_ Bound<'_, PyTuple> { - fn into_py(self, _: Python<'_>) -> Py { - self.clone().unbind() - } -} - -#[cold] -fn wrong_tuple_length(t: &Bound<'_, PyTuple>, expected_length: usize) -> PyErr { - let msg = format!( - "expected tuple of length {}, but got tuple of length {}", - expected_length, - t.len() - ); - exceptions::PyValueError::new_err(msg) -} - -macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => { - #[allow(deprecated)] - impl <$($T: ToPyObject),+> ToPyObject for ($($T,)+) { - fn to_object(&self, py: Python<'_>) -> PyObject { - array_into_tuple(py, [$(self.$n.to_object(py).into_bound(py)),+]).into() - } - } - - #[allow(deprecated)] - impl <$($T: IntoPy),+> IntoPy for ($($T,)+) { - fn into_py(self, py: Python<'_>) -> PyObject { - array_into_tuple(py, [$(self.$n.into_py(py).into_bound(py)),+]).into() - } - } - - impl <'py, $($T),+> IntoPyObject<'py> for ($($T,)+) - where - $($T: IntoPyObject<'py>,)+ - { - type Target = PyTuple; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(array_into_tuple(py, [$(self.$n.into_bound_py_any(py)?),+])) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::Tuple(Some(vec![$( $T::type_output() ),+])) - } - } - - impl <'a, 'py, $($T),+> IntoPyObject<'py> for &'a ($($T,)+) - where - $(&'a $T: IntoPyObject<'py>,)+ - $($T: 'a,)+ // MSRV - { - type Target = PyTuple; - type Output = Bound<'py, Self::Target>; - type Error = PyErr; - - fn into_pyobject(self, py: Python<'py>) -> Result { - Ok(array_into_tuple(py, [$(self.$n.into_bound_py_any(py)?),+])) - } - - #[cfg(feature = "experimental-inspect")] - fn type_output() -> TypeInfo { - TypeInfo::Tuple(Some(vec![$( <&$T>::type_output() ),+])) - } - } - - #[allow(deprecated)] - impl <$($T: IntoPy),+> IntoPy> for ($($T,)+) { - fn into_py(self, py: Python<'_>) -> Py { - array_into_tuple(py, [$(self.$n.into_py(py).into_bound(py)),+]).unbind() - } - } - - impl<'py, $($T: FromPyObject<'py>),+> FromPyObject<'py> for ($($T,)+) { - fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult - { - let t = obj.downcast::()?; - if t.len() == $length { - #[cfg(any(Py_LIMITED_API, PyPy, GraalPy))] - return Ok(($(t.get_borrowed_item($n)?.extract::<$T>()?,)+)); - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - unsafe {return Ok(($(t.get_borrowed_item_unchecked($n).extract::<$T>()?,)+));} - } else { - Err(wrong_tuple_length(t, $length)) - } - } - - #[cfg(feature = "experimental-inspect")] - fn type_input() -> TypeInfo { - TypeInfo::Tuple(Some(vec![$( $T::type_input() ),+])) - } - } -}); - -fn array_into_tuple<'py, const N: usize>( - py: Python<'py>, - array: [Bound<'py, PyAny>; N], -) -> Bound<'py, PyTuple> { - unsafe { - let ptr = ffi::PyTuple_New(N.try_into().expect("0 < N <= 12")); - let tup = ptr.assume_owned(py).downcast_into_unchecked(); - for (index, obj) in array.into_iter().enumerate() { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - ffi::PyTuple_SET_ITEM(ptr, index as ffi::Py_ssize_t, obj.into_ptr()); - #[cfg(any(Py_LIMITED_API, PyPy, GraalPy))] - ffi::PyTuple_SetItem(ptr, index as ffi::Py_ssize_t, obj.into_ptr()); - } - tup - } -} - -tuple_conversion!(1, (ref0, 0, T0)); -tuple_conversion!(2, (ref0, 0, T0), (ref1, 1, T1)); -tuple_conversion!(3, (ref0, 0, T0), (ref1, 1, T1), (ref2, 2, T2)); -tuple_conversion!( - 4, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3) -); -tuple_conversion!( - 5, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4) -); -tuple_conversion!( - 6, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5) -); -tuple_conversion!( - 7, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6) -); -tuple_conversion!( - 8, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6), - (ref7, 7, T7) -); -tuple_conversion!( - 9, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6), - (ref7, 7, T7), - (ref8, 8, T8) -); -tuple_conversion!( - 10, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6), - (ref7, 7, T7), - (ref8, 8, T8), - (ref9, 9, T9) -); -tuple_conversion!( - 11, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6), - (ref7, 7, T7), - (ref8, 8, T8), - (ref9, 9, T9), - (ref10, 10, T10) -); - -tuple_conversion!( - 12, - (ref0, 0, T0), - (ref1, 1, T1), - (ref2, 2, T2), - (ref3, 3, T3), - (ref4, 4, T4), - (ref5, 5, T5), - (ref6, 6, T6), - (ref7, 7, T7), - (ref8, 8, T8), - (ref9, 9, T9), - (ref10, 10, T10), - (ref11, 11, T11) -); - -#[cfg(test)] -mod tests { - use crate::types::{any::PyAnyMethods, tuple::PyTupleMethods, PyList, PyTuple}; - use crate::{IntoPyObject, Python}; - use std::collections::HashSet; - use std::ops::Range; - - #[test] - fn test_new() { - Python::with_gil(|py| { - let ob = PyTuple::new(py, [1, 2, 3]).unwrap(); - assert_eq!(3, ob.len()); - let ob = ob.as_any(); - assert_eq!((1, 2, 3), ob.extract().unwrap()); - - let mut map = HashSet::new(); - map.insert(1); - map.insert(2); - PyTuple::new(py, map).unwrap(); - }); - } - - #[test] - fn test_len() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(3, tuple.len()); - assert!(!tuple.is_empty()); - let ob = tuple.as_any(); - assert_eq!((1, 2, 3), ob.extract().unwrap()); - }); - } - - #[test] - fn test_empty() { - Python::with_gil(|py| { - let tuple = PyTuple::empty(py); - assert!(tuple.is_empty()); - assert_eq!(0, tuple.len()); - }); - } - - #[test] - fn test_slice() { - Python::with_gil(|py| { - let tup = PyTuple::new(py, [2, 3, 5, 7]).unwrap(); - let slice = tup.get_slice(1, 3); - assert_eq!(2, slice.len()); - let slice = tup.get_slice(1, 7); - assert_eq!(3, slice.len()); - }); - } - - #[test] - fn test_iter() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(3, tuple.len()); - let mut iter = tuple.iter(); - - assert_eq!(iter.size_hint(), (3, Some(3))); - - assert_eq!(1_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (2, Some(2))); - - assert_eq!(2_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (1, Some(1))); - - assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - assert!(iter.next().is_none()); - }); - } - - #[test] - fn test_iter_rev() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(3, tuple.len()); - let mut iter = tuple.iter().rev(); - - assert_eq!(iter.size_hint(), (3, Some(3))); - - assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (2, Some(2))); - - assert_eq!(2_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (1, Some(1))); - - assert_eq!(1_i32, iter.next().unwrap().extract::<'_, i32>().unwrap()); - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - assert!(iter.next().is_none()); - }); - } - - #[test] - fn test_bound_iter() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, [1, 2, 3]).unwrap(); - assert_eq!(3, tuple.len()); - let mut iter = tuple.iter(); - - assert_eq!(iter.size_hint(), (3, Some(3))); - - assert_eq!(1, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (2, Some(2))); - - assert_eq!(2, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (1, Some(1))); - - assert_eq!(3, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - assert!(iter.next().is_none()); - }); - } - - #[test] - fn test_bound_iter_rev() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, [1, 2, 3]).unwrap(); - assert_eq!(3, tuple.len()); - let mut iter = tuple.iter().rev(); - - assert_eq!(iter.size_hint(), (3, Some(3))); - - assert_eq!(3, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (2, Some(2))); - - assert_eq!(2, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (1, Some(1))); - - assert_eq!(1, iter.next().unwrap().extract::().unwrap()); - assert_eq!(iter.size_hint(), (0, Some(0))); - - assert!(iter.next().is_none()); - assert!(iter.next().is_none()); - }); - } - - #[test] - fn test_into_iter() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(3, tuple.len()); - - for (i, item) in tuple.iter().enumerate() { - assert_eq!(i + 1, item.extract::<'_, usize>().unwrap()); - } - }); - } - - #[test] - fn test_into_iter_bound() { - Python::with_gil(|py| { - let tuple = (1, 2, 3).into_pyobject(py).unwrap(); - assert_eq!(3, tuple.len()); - - let mut items = vec![]; - for item in tuple { - items.push(item.extract::().unwrap()); - } - assert_eq!(items, vec![1, 2, 3]); - }); - } - - #[test] - #[cfg(not(any(Py_LIMITED_API, GraalPy)))] - fn test_as_slice() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - - let slice = tuple.as_slice(); - assert_eq!(3, slice.len()); - assert_eq!(1_i32, slice[0].extract::<'_, i32>().unwrap()); - assert_eq!(2_i32, slice[1].extract::<'_, i32>().unwrap()); - assert_eq!(3_i32, slice[2].extract::<'_, i32>().unwrap()); - }); - } - - #[test] - fn test_tuple_lengths_up_to_12() { - Python::with_gil(|py| { - let t0 = (0,).into_pyobject(py).unwrap(); - let t1 = (0, 1).into_pyobject(py).unwrap(); - let t2 = (0, 1, 2).into_pyobject(py).unwrap(); - let t3 = (0, 1, 2, 3).into_pyobject(py).unwrap(); - let t4 = (0, 1, 2, 3, 4).into_pyobject(py).unwrap(); - let t5 = (0, 1, 2, 3, 4, 5).into_pyobject(py).unwrap(); - let t6 = (0, 1, 2, 3, 4, 5, 6).into_pyobject(py).unwrap(); - let t7 = (0, 1, 2, 3, 4, 5, 6, 7).into_pyobject(py).unwrap(); - let t8 = (0, 1, 2, 3, 4, 5, 6, 7, 8).into_pyobject(py).unwrap(); - let t9 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).into_pyobject(py).unwrap(); - let t10 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) - .into_pyobject(py) - .unwrap(); - let t11 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) - .into_pyobject(py) - .unwrap(); - - assert_eq!(t0.extract::<(i32,)>().unwrap(), (0,)); - assert_eq!(t1.extract::<(i32, i32)>().unwrap(), (0, 1,)); - assert_eq!(t2.extract::<(i32, i32, i32)>().unwrap(), (0, 1, 2,)); - assert_eq!( - t3.extract::<(i32, i32, i32, i32,)>().unwrap(), - (0, 1, 2, 3,) - ); - assert_eq!( - t4.extract::<(i32, i32, i32, i32, i32,)>().unwrap(), - (0, 1, 2, 3, 4,) - ); - assert_eq!( - t5.extract::<(i32, i32, i32, i32, i32, i32,)>().unwrap(), - (0, 1, 2, 3, 4, 5,) - ); - assert_eq!( - t6.extract::<(i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6,) - ); - assert_eq!( - t7.extract::<(i32, i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6, 7,) - ); - assert_eq!( - t8.extract::<(i32, i32, i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6, 7, 8,) - ); - assert_eq!( - t9.extract::<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6, 7, 8, 9,) - ); - assert_eq!( - t10.extract::<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,) - ); - assert_eq!( - t11.extract::<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32,)>() - .unwrap(), - (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,) - ); - }) - } - - #[test] - fn test_tuple_get_item_invalid_index() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - let obj = tuple.get_item(5); - assert!(obj.is_err()); - assert_eq!( - obj.unwrap_err().to_string(), - "IndexError: tuple index out of range" - ); - }); - } - - #[test] - fn test_tuple_get_item_sanity() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - let obj = tuple.get_item(0); - assert_eq!(obj.unwrap().extract::().unwrap(), 1); - }); - } - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - #[test] - fn test_tuple_get_item_unchecked_sanity() { - Python::with_gil(|py| { - let ob = (1, 2, 3).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - let obj = unsafe { tuple.get_item_unchecked(0) }; - assert_eq!(obj.extract::().unwrap(), 1); - }); - } - - #[test] - fn test_tuple_contains() { - Python::with_gil(|py| { - let ob = (1, 1, 2, 3, 5, 8).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(6, tuple.len()); - - let bad_needle = 7i32.into_pyobject(py).unwrap(); - assert!(!tuple.contains(&bad_needle).unwrap()); - - let good_needle = 8i32.into_pyobject(py).unwrap(); - assert!(tuple.contains(&good_needle).unwrap()); - - let type_coerced_needle = 8f32.into_pyobject(py).unwrap(); - assert!(tuple.contains(&type_coerced_needle).unwrap()); - }); - } - - #[test] - fn test_tuple_index() { - Python::with_gil(|py| { - let ob = (1, 1, 2, 3, 5, 8).into_pyobject(py).unwrap(); - let tuple = ob.downcast::().unwrap(); - assert_eq!(0, tuple.index(1i32).unwrap()); - assert_eq!(2, tuple.index(2i32).unwrap()); - assert_eq!(3, tuple.index(3i32).unwrap()); - assert_eq!(4, tuple.index(5i32).unwrap()); - assert_eq!(5, tuple.index(8i32).unwrap()); - assert!(tuple.index(42i32).is_err()); - }); - } - - // An iterator that lies about its `ExactSizeIterator` implementation. - // See https://github.com/PyO3/pyo3/issues/2118 - struct FaultyIter(Range, usize); - - impl Iterator for FaultyIter { - type Item = usize; - - fn next(&mut self) -> Option { - self.0.next() - } - } - - impl ExactSizeIterator for FaultyIter { - fn len(&self) -> usize { - self.1 - } - } - - #[test] - #[should_panic( - expected = "Attempted to create PyTuple but `elements` was larger than reported by its `ExactSizeIterator` implementation." - )] - fn too_long_iterator() { - Python::with_gil(|py| { - let iter = FaultyIter(0..usize::MAX, 73); - let _tuple = PyTuple::new(py, iter); - }) - } - - #[test] - #[should_panic( - expected = "Attempted to create PyTuple but `elements` was smaller than reported by its `ExactSizeIterator` implementation." - )] - fn too_short_iterator() { - Python::with_gil(|py| { - let iter = FaultyIter(0..35, 73); - let _tuple = PyTuple::new(py, iter); - }) - } - - #[test] - #[should_panic( - expected = "out of range integral type conversion attempted on `elements.len()`" - )] - fn overflowing_size() { - Python::with_gil(|py| { - let iter = FaultyIter(0..0, usize::MAX); - - let _tuple = PyTuple::new(py, iter); - }) - } - - #[test] - fn bad_intopyobject_doesnt_cause_leaks() { - use crate::types::PyInt; - use std::convert::Infallible; - use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; - - static NEEDS_DESTRUCTING_COUNT: AtomicUsize = AtomicUsize::new(0); - - struct Bad(usize); - - impl Drop for Bad { - fn drop(&mut self) { - NEEDS_DESTRUCTING_COUNT.fetch_sub(1, SeqCst); - } - } - - impl<'py> IntoPyObject<'py> for Bad { - type Target = PyInt; - type Output = crate::Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - // This panic should not lead to a memory leak - assert_ne!(self.0, 42); - self.0.into_pyobject(py) - } - } - - struct FaultyIter(Range, usize); - - impl Iterator for FaultyIter { - type Item = Bad; - - fn next(&mut self) -> Option { - self.0.next().map(|i| { - NEEDS_DESTRUCTING_COUNT.fetch_add(1, SeqCst); - Bad(i) - }) - } - } - - impl ExactSizeIterator for FaultyIter { - fn len(&self) -> usize { - self.1 - } - } - - Python::with_gil(|py| { - std::panic::catch_unwind(|| { - let iter = FaultyIter(0..50, 50); - let _tuple = PyTuple::new(py, iter); - }) - .unwrap_err(); - }); - - assert_eq!( - NEEDS_DESTRUCTING_COUNT.load(SeqCst), - 0, - "Some destructors did not run" - ); - } - - #[test] - fn bad_intopyobject_doesnt_cause_leaks_2() { - use crate::types::PyInt; - use std::convert::Infallible; - use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; - - static NEEDS_DESTRUCTING_COUNT: AtomicUsize = AtomicUsize::new(0); - - struct Bad(usize); - - impl Drop for Bad { - fn drop(&mut self) { - NEEDS_DESTRUCTING_COUNT.fetch_sub(1, SeqCst); - } - } - - impl<'py> IntoPyObject<'py> for &Bad { - type Target = PyInt; - type Output = crate::Bound<'py, Self::Target>; - type Error = Infallible; - - fn into_pyobject(self, py: Python<'py>) -> Result { - // This panic should not lead to a memory leak - assert_ne!(self.0, 3); - self.0.into_pyobject(py) - } - } - - let s = (Bad(1), Bad(2), Bad(3), Bad(4)); - NEEDS_DESTRUCTING_COUNT.store(4, SeqCst); - Python::with_gil(|py| { - std::panic::catch_unwind(|| { - let _tuple = (&s).into_pyobject(py).unwrap(); - }) - .unwrap_err(); - }); - drop(s); - - assert_eq!( - NEEDS_DESTRUCTING_COUNT.load(SeqCst), - 0, - "Some destructors did not run" - ); - } - - #[test] - fn test_tuple_to_list() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, vec![1, 2, 3]).unwrap(); - let list = tuple.to_list(); - let list_expected = PyList::new(py, vec![1, 2, 3]).unwrap(); - assert!(list.eq(list_expected).unwrap()); - }) - } - - #[test] - fn test_tuple_as_sequence() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, vec![1, 2, 3]).unwrap(); - let sequence = tuple.as_sequence(); - assert!(tuple.get_item(0).unwrap().eq(1).unwrap()); - assert!(sequence.get_item(0).unwrap().eq(1).unwrap()); - - assert_eq!(tuple.len(), 3); - assert_eq!(sequence.len().unwrap(), 3); - }) - } - - #[test] - fn test_tuple_into_sequence() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, vec![1, 2, 3]).unwrap(); - let sequence = tuple.into_sequence(); - assert!(sequence.get_item(0).unwrap().eq(1).unwrap()); - assert_eq!(sequence.len().unwrap(), 3); - }) - } - - #[test] - fn test_bound_tuple_get_item() { - Python::with_gil(|py| { - let tuple = PyTuple::new(py, vec![1, 2, 3, 4]).unwrap(); - - assert_eq!(tuple.len(), 4); - assert_eq!(tuple.get_item(0).unwrap().extract::().unwrap(), 1); - assert_eq!( - tuple - .get_borrowed_item(1) - .unwrap() - .extract::() - .unwrap(), - 2 - ); - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - { - assert_eq!( - unsafe { tuple.get_item_unchecked(2) } - .extract::() - .unwrap(), - 3 - ); - assert_eq!( - unsafe { tuple.get_borrowed_item_unchecked(3) } - .extract::() - .unwrap(), - 4 - ); - } - }) - } -} diff --git a/vendor/pyo3/src/types/typeobject.rs b/vendor/pyo3/src/types/typeobject.rs deleted file mode 100644 index 7a66b7ad0d..0000000000 --- a/vendor/pyo3/src/types/typeobject.rs +++ /dev/null @@ -1,393 +0,0 @@ -use crate::err::{self, PyResult}; -use crate::instance::Borrowed; -#[cfg(not(Py_3_13))] -use crate::pybacked::PyBackedStr; -use crate::types::any::PyAnyMethods; -use crate::types::PyTuple; -use crate::{ffi, Bound, PyAny, PyTypeInfo, Python}; - -use super::PyString; - -/// Represents a reference to a Python `type` object. -/// -/// Values of this type are accessed via PyO3's smart pointers, e.g. as -/// [`Py`][crate::Py] or [`Bound<'py, PyType>`][Bound]. -/// -/// For APIs available on `type` objects, see the [`PyTypeMethods`] trait which is implemented for -/// [`Bound<'py, PyType>`][Bound]. -#[repr(transparent)] -pub struct PyType(PyAny); - -pyobject_native_type_core!(PyType, pyobject_native_static_type_object!(ffi::PyType_Type), #checkfunction=ffi::PyType_Check); - -impl PyType { - /// Creates a new type object. - #[inline] - pub fn new(py: Python<'_>) -> Bound<'_, PyType> { - T::type_object(py) - } - - /// Deprecated name for [`PyType::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyType::new`")] - #[inline] - pub fn new_bound(py: Python<'_>) -> Bound<'_, PyType> { - Self::new::(py) - } - - /// Converts the given FFI pointer into `Bound`, to use in safe code. - /// - /// The function creates a new reference from the given pointer, and returns - /// it as a `Bound`. - /// - /// # Safety - /// - The pointer must be a valid non-null reference to a `PyTypeObject` - #[inline] - pub unsafe fn from_borrowed_type_ptr( - py: Python<'_>, - p: *mut ffi::PyTypeObject, - ) -> Bound<'_, PyType> { - Borrowed::from_ptr_unchecked(py, p.cast()) - .downcast_unchecked() - .to_owned() - } -} - -/// Implementation of functionality for [`PyType`]. -/// -/// These methods are defined for the `Bound<'py, PyType>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyType")] -pub trait PyTypeMethods<'py>: crate::sealed::Sealed { - /// Retrieves the underlying FFI pointer associated with this Python object. - fn as_type_ptr(&self) -> *mut ffi::PyTypeObject; - - /// Gets the name of the `PyType`. Equivalent to `self.__name__` in Python. - fn name(&self) -> PyResult>; - - /// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`. - /// Equivalent to `self.__qualname__` in Python. - fn qualname(&self) -> PyResult>; - - /// Gets the name of the module defining the `PyType`. - fn module(&self) -> PyResult>; - - /// Gets the [fully qualified name](https://peps.python.org/pep-0737/#add-pytype-getfullyqualifiedname-function) of the `PyType`. - fn fully_qualified_name(&self) -> PyResult>; - - /// Checks whether `self` is a subclass of `other`. - /// - /// Equivalent to the Python expression `issubclass(self, other)`. - fn is_subclass(&self, other: &Bound<'_, PyAny>) -> PyResult; - - /// Checks whether `self` is a subclass of type `T`. - /// - /// Equivalent to the Python expression `issubclass(self, T)`, if the type - /// `T` is known at compile time. - fn is_subclass_of(&self) -> PyResult - where - T: PyTypeInfo; - - /// Return the method resolution order for this type. - /// - /// Equivalent to the Python expression `self.__mro__`. - fn mro(&self) -> Bound<'py, PyTuple>; - - /// Return Python bases - /// - /// Equivalent to the Python expression `self.__bases__`. - fn bases(&self) -> Bound<'py, PyTuple>; -} - -impl<'py> PyTypeMethods<'py> for Bound<'py, PyType> { - /// Retrieves the underlying FFI pointer associated with this Python object. - #[inline] - fn as_type_ptr(&self) -> *mut ffi::PyTypeObject { - self.as_ptr() as *mut ffi::PyTypeObject - } - - /// Gets the name of the `PyType`. - fn name(&self) -> PyResult> { - #[cfg(not(Py_3_11))] - let name = self - .getattr(intern!(self.py(), "__name__"))? - .downcast_into()?; - - #[cfg(Py_3_11)] - let name = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - ffi::PyType_GetName(self.as_type_ptr()) - .assume_owned_or_err(self.py())? - // SAFETY: setting `__name__` from Python is required to be a `str` - .downcast_into_unchecked() - }; - - Ok(name) - } - - /// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`. - fn qualname(&self) -> PyResult> { - #[cfg(not(Py_3_11))] - let name = self - .getattr(intern!(self.py(), "__qualname__"))? - .downcast_into()?; - - #[cfg(Py_3_11)] - let name = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - ffi::PyType_GetQualName(self.as_type_ptr()) - .assume_owned_or_err(self.py())? - // SAFETY: setting `__qualname__` from Python is required to be a `str` - .downcast_into_unchecked() - }; - - Ok(name) - } - - /// Gets the name of the module defining the `PyType`. - fn module(&self) -> PyResult> { - #[cfg(not(Py_3_13))] - let name = self.getattr(intern!(self.py(), "__module__"))?; - - #[cfg(Py_3_13)] - let name = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - ffi::PyType_GetModuleName(self.as_type_ptr()).assume_owned_or_err(self.py())? - }; - - // `__module__` is never guaranteed to be a `str` - name.downcast_into().map_err(Into::into) - } - - /// Gets the [fully qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`. - fn fully_qualified_name(&self) -> PyResult> { - #[cfg(not(Py_3_13))] - let name = { - let module = self.getattr(intern!(self.py(), "__module__"))?; - let qualname = self.getattr(intern!(self.py(), "__qualname__"))?; - - let module_str = module.extract::()?; - if module_str == "builtins" || module_str == "__main__" { - qualname.downcast_into()? - } else { - PyString::new(self.py(), &format!("{}.{}", module, qualname)) - } - }; - - #[cfg(Py_3_13)] - let name = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - ffi::PyType_GetFullyQualifiedName(self.as_type_ptr()) - .assume_owned_or_err(self.py())? - .downcast_into_unchecked() - }; - - Ok(name) - } - - /// Checks whether `self` is a subclass of `other`. - /// - /// Equivalent to the Python expression `issubclass(self, other)`. - fn is_subclass(&self, other: &Bound<'_, PyAny>) -> PyResult { - let result = unsafe { ffi::PyObject_IsSubclass(self.as_ptr(), other.as_ptr()) }; - err::error_on_minusone(self.py(), result)?; - Ok(result == 1) - } - - /// Checks whether `self` is a subclass of type `T`. - /// - /// Equivalent to the Python expression `issubclass(self, T)`, if the type - /// `T` is known at compile time. - fn is_subclass_of(&self) -> PyResult - where - T: PyTypeInfo, - { - self.is_subclass(&T::type_object(self.py())) - } - - fn mro(&self) -> Bound<'py, PyTuple> { - #[cfg(any(Py_LIMITED_API, PyPy))] - let mro = self - .getattr(intern!(self.py(), "__mro__")) - .expect("Cannot get `__mro__` from object.") - .extract() - .expect("Unexpected type in `__mro__` attribute."); - - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - let mro = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - (*self.as_type_ptr()) - .tp_mro - .assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked() - }; - - mro - } - - fn bases(&self) -> Bound<'py, PyTuple> { - #[cfg(any(Py_LIMITED_API, PyPy))] - let bases = self - .getattr(intern!(self.py(), "__bases__")) - .expect("Cannot get `__bases__` from object.") - .extract() - .expect("Unexpected type in `__bases__` attribute."); - - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - let bases = unsafe { - use crate::ffi_ptr_ext::FfiPtrExt; - (*self.as_type_ptr()) - .tp_bases - .assume_borrowed(self.py()) - .to_owned() - .downcast_into_unchecked() - }; - - bases - } -} - -#[cfg(test)] -mod tests { - use crate::tests::common::generate_unique_module_name; - use crate::types::{PyAnyMethods, PyBool, PyInt, PyModule, PyTuple, PyType, PyTypeMethods}; - use crate::PyAny; - use crate::Python; - use pyo3_ffi::c_str; - - #[test] - fn test_type_is_subclass() { - Python::with_gil(|py| { - let bool_type = py.get_type::(); - let long_type = py.get_type::(); - assert!(bool_type.is_subclass(&long_type).unwrap()); - }); - } - - #[test] - fn test_type_is_subclass_of() { - Python::with_gil(|py| { - assert!(py.get_type::().is_subclass_of::().unwrap()); - }); - } - - #[test] - fn test_mro() { - Python::with_gil(|py| { - assert!(py - .get_type::() - .mro() - .eq(PyTuple::new( - py, - [ - py.get_type::(), - py.get_type::(), - py.get_type::() - ] - ) - .unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_bases_bool() { - Python::with_gil(|py| { - assert!(py - .get_type::() - .bases() - .eq(PyTuple::new(py, [py.get_type::()]).unwrap()) - .unwrap()); - }); - } - - #[test] - fn test_bases_object() { - Python::with_gil(|py| { - assert!(py - .get_type::() - .bases() - .eq(PyTuple::empty(py)) - .unwrap()); - }); - } - - #[test] - fn test_type_names_standard() { - Python::with_gil(|py| { - let module_name = generate_unique_module_name("test_module"); - let module = PyModule::from_code( - py, - c_str!( - r#" -class MyClass: - pass -"# - ), - c_str!(file!()), - &module_name, - ) - .expect("module create failed"); - - let my_class = module.getattr("MyClass").unwrap(); - let my_class_type = my_class.downcast_into::().unwrap(); - assert_eq!(my_class_type.name().unwrap(), "MyClass"); - assert_eq!(my_class_type.qualname().unwrap(), "MyClass"); - let module_name = module_name.to_str().unwrap(); - let qualname = format!("{module_name}.MyClass"); - assert_eq!(my_class_type.module().unwrap(), module_name); - assert_eq!( - my_class_type.fully_qualified_name().unwrap(), - qualname.as_str() - ); - }); - } - - #[test] - fn test_type_names_builtin() { - Python::with_gil(|py| { - let bool_type = py.get_type::(); - assert_eq!(bool_type.name().unwrap(), "bool"); - assert_eq!(bool_type.qualname().unwrap(), "bool"); - assert_eq!(bool_type.module().unwrap(), "builtins"); - assert_eq!(bool_type.fully_qualified_name().unwrap(), "bool"); - }); - } - - #[test] - fn test_type_names_nested() { - Python::with_gil(|py| { - let module_name = generate_unique_module_name("test_module"); - let module = PyModule::from_code( - py, - c_str!( - r#" -class OuterClass: - class InnerClass: - pass -"# - ), - c_str!(file!()), - &module_name, - ) - .expect("module create failed"); - - let outer_class = module.getattr("OuterClass").unwrap(); - let inner_class = outer_class.getattr("InnerClass").unwrap(); - let inner_class_type = inner_class.downcast_into::().unwrap(); - assert_eq!(inner_class_type.name().unwrap(), "InnerClass"); - assert_eq!( - inner_class_type.qualname().unwrap(), - "OuterClass.InnerClass" - ); - let module_name = module_name.to_str().unwrap(); - let qualname = format!("{module_name}.OuterClass.InnerClass"); - assert_eq!(inner_class_type.module().unwrap(), module_name); - assert_eq!( - inner_class_type.fully_qualified_name().unwrap(), - qualname.as_str() - ); - }); - } -} diff --git a/vendor/pyo3/src/types/weakref/anyref.rs b/vendor/pyo3/src/types/weakref/anyref.rs deleted file mode 100644 index d5af956ac9..0000000000 --- a/vendor/pyo3/src/types/weakref/anyref.rs +++ /dev/null @@ -1,734 +0,0 @@ -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::type_object::{PyTypeCheck, PyTypeInfo}; -use crate::types::{ - any::{PyAny, PyAnyMethods}, - PyNone, -}; -use crate::{ffi, Bound, Python}; - -/// Represents any Python `weakref` reference. -/// -/// In Python this is created by calling `weakref.ref` or `weakref.proxy`. -#[repr(transparent)] -pub struct PyWeakref(PyAny); - -pyobject_native_type_named!(PyWeakref); - -// TODO: We known the layout but this cannot be implemented, due to the lack of public typeobject pointers -// #[cfg(not(Py_LIMITED_API))] -// pyobject_native_type_sized!(PyWeakref, ffi::PyWeakReference); - -impl PyTypeCheck for PyWeakref { - const NAME: &'static str = "weakref"; - - fn type_check(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::PyWeakref_Check(object.as_ptr()) > 0 } - } -} - -/// Implementation of functionality for [`PyWeakref`]. -/// -/// These methods are defined for the `Bound<'py, PyWeakref>` smart pointer, so to use method call -/// syntax these methods are separated into a trait, because stable Rust does not yet support -/// `arbitrary_self_types`. -#[doc(alias = "PyWeakref")] -pub trait PyWeakrefMethods<'py>: crate::sealed::Sealed { - /// Upgrade the weakref to a direct Bound object reference. - /// - /// It is named `upgrade` to be inline with [rust's `Weak::upgrade`](std::rc::Weak::upgrade). - /// In Python it would be equivalent to [`PyWeakref_GetRef`]. - /// - /// # Example - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// #[pymethods] - /// impl Foo { - /// fn get_data(&self) -> (&str, u32) { - /// ("Dave", 10) - /// } - /// } - /// - /// fn parse_data(reference: Borrowed<'_, '_, PyWeakrefReference>) -> PyResult { - /// if let Some(data_src) = reference.upgrade_as::()? { - /// let data = data_src.borrow(); - /// let (name, score) = data.get_data(); - /// Ok(format!("Processing '{}': score = {}", name, score)) - /// } else { - /// Ok("The supplied data reference is nolonger relavent.".to_owned()) - /// } - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let data = Bound::new(py, Foo{})?; - /// let reference = PyWeakrefReference::new(&data)?; - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "Processing 'Dave': score = 10" - /// ); - /// - /// drop(data); - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "The supplied data reference is nolonger relavent." - /// ); - /// - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// This function panics is the current object is invalid. - /// If used propperly this is never the case. (NonNull and actually a weakref type) - /// - /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef - /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType - /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref - fn upgrade_as(&self) -> PyResult>> - where - T: PyTypeCheck, - { - self.upgrade() - .map(Bound::downcast_into::) - .transpose() - .map_err(Into::into) - } - - /// Upgrade the weakref to a direct Bound object reference unchecked. The type of the recovered object is not checked before downcasting, this could lead to unexpected behavior. Use only when absolutely certain the type can be guaranteed. The `weakref` may still return `None`. - /// - /// It is named `upgrade` to be inline with [rust's `Weak::upgrade`](std::rc::Weak::upgrade). - /// In Python it would be equivalent to [`PyWeakref_GetRef`]. - /// - /// # Safety - /// Callers must ensure that the type is valid or risk type confusion. - /// The `weakref` is still allowed to be `None`, if the referenced object has been cleaned up. - /// - /// # Example - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// #[pymethods] - /// impl Foo { - /// fn get_data(&self) -> (&str, u32) { - /// ("Dave", 10) - /// } - /// } - /// - /// fn parse_data(reference: Borrowed<'_, '_, PyWeakrefReference>) -> String { - /// if let Some(data_src) = unsafe { reference.upgrade_as_unchecked::() } { - /// let data = data_src.borrow(); - /// let (name, score) = data.get_data(); - /// format!("Processing '{}': score = {}", name, score) - /// } else { - /// "The supplied data reference is nolonger relavent.".to_owned() - /// } - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let data = Bound::new(py, Foo{})?; - /// let reference = PyWeakrefReference::new(&data)?; - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed()), - /// "Processing 'Dave': score = 10" - /// ); - /// - /// drop(data); - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed()), - /// "The supplied data reference is nolonger relavent." - /// ); - /// - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// This function panics is the current object is invalid. - /// If used propperly this is never the case. (NonNull and actually a weakref type) - /// - /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef - /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType - /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref - unsafe fn upgrade_as_unchecked(&self) -> Option> { - Some(self.upgrade()?.downcast_into_unchecked()) - } - - /// Upgrade the weakref to a exact direct Bound object reference. - /// - /// It is named `upgrade` to be inline with [rust's `Weak::upgrade`](std::rc::Weak::upgrade). - /// In Python it would be equivalent to [`PyWeakref_GetRef`]. - /// - /// # Example - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// #[pymethods] - /// impl Foo { - /// fn get_data(&self) -> (&str, u32) { - /// ("Dave", 10) - /// } - /// } - /// - /// fn parse_data(reference: Borrowed<'_, '_, PyWeakrefReference>) -> PyResult { - /// if let Some(data_src) = reference.upgrade_as_exact::()? { - /// let data = data_src.borrow(); - /// let (name, score) = data.get_data(); - /// Ok(format!("Processing '{}': score = {}", name, score)) - /// } else { - /// Ok("The supplied data reference is nolonger relavent.".to_owned()) - /// } - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let data = Bound::new(py, Foo{})?; - /// let reference = PyWeakrefReference::new(&data)?; - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "Processing 'Dave': score = 10" - /// ); - /// - /// drop(data); - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "The supplied data reference is nolonger relavent." - /// ); - /// - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// This function panics is the current object is invalid. - /// If used propperly this is never the case. (NonNull and actually a weakref type) - /// - /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef - /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType - /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref - fn upgrade_as_exact(&self) -> PyResult>> - where - T: PyTypeInfo, - { - self.upgrade() - .map(Bound::downcast_into_exact) - .transpose() - .map_err(Into::into) - } - - /// Upgrade the weakref to a Bound [`PyAny`] reference to the target object if possible. - /// - /// It is named `upgrade` to be inline with [rust's `Weak::upgrade`](std::rc::Weak::upgrade). - /// This function returns `Some(Bound<'py, PyAny>)` if the reference still exists, otherwise `None` will be returned. - /// - /// This function gets the optional target of this [`weakref.ReferenceType`] (result of calling [`weakref.ref`]). - /// It produces similar results to using [`PyWeakref_GetRef`] in the C api. - /// - /// # Example - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// fn parse_data(reference: Borrowed<'_, '_, PyWeakrefReference>) -> PyResult { - /// if let Some(object) = reference.upgrade() { - /// Ok(format!("The object '{}' refered by this reference still exists.", object.getattr("__class__")?.getattr("__qualname__")?)) - /// } else { - /// Ok("The object, which this reference refered to, no longer exists".to_owned()) - /// } - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let data = Bound::new(py, Foo{})?; - /// let reference = PyWeakrefReference::new(&data)?; - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "The object 'Foo' refered by this reference still exists." - /// ); - /// - /// drop(data); - /// - /// assert_eq!( - /// parse_data(reference.as_borrowed())?, - /// "The object, which this reference refered to, no longer exists" - /// ); - /// - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// This function panics is the current object is invalid. - /// If used properly this is never the case. (NonNull and actually a weakref type) - /// - /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef - /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType - /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref - fn upgrade(&self) -> Option>; - - /// Retrieve to a Bound object pointed to by the weakref. - /// - /// This function returns `Bound<'py, PyAny>`, which is either the object if it still exists, otherwise it will refer to [`PyNone`](crate::types::PyNone). - /// - /// This function gets the optional target of this [`weakref.ReferenceType`] (result of calling [`weakref.ref`]). - /// It produces similar results to using [`PyWeakref_GetRef`] in the C api. - /// - /// # Example - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// #![allow(deprecated)] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// fn get_class(reference: Borrowed<'_, '_, PyWeakrefReference>) -> PyResult { - /// reference - /// .get_object() - /// .getattr("__class__")? - /// .repr() - /// .map(|repr| repr.to_string()) - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let object = Bound::new(py, Foo{})?; - /// let reference = PyWeakrefReference::new(&object)?; - /// - /// assert_eq!( - /// get_class(reference.as_borrowed())?, - /// "" - /// ); - /// - /// drop(object); - /// - /// assert_eq!(get_class(reference.as_borrowed())?, ""); - /// - /// Ok(()) - /// }) - /// # } - /// ``` - /// - /// # Panics - /// This function panics is the current object is invalid. - /// If used propperly this is never the case. (NonNull and actually a weakref type) - /// - /// [`PyWeakref_GetRef`]: https://docs.python.org/3/c-api/weakref.html#c.PyWeakref_GetRef - /// [`weakref.ReferenceType`]: https://docs.python.org/3/library/weakref.html#weakref.ReferenceType - /// [`weakref.ref`]: https://docs.python.org/3/library/weakref.html#weakref.ref - #[deprecated(since = "0.23.0", note = "Use `upgrade` instead")] - fn get_object(&self) -> Bound<'py, PyAny> { - self.upgrade().unwrap_or_else(|| { - // Safety: upgrade() returns `Bound<'py, PyAny>` with a lifetime `'py` if it exists, we - // can safely assume the same lifetime here. - PyNone::get(unsafe { Python::assume_gil_acquired() }) - .to_owned() - .into_any() - }) - } -} - -impl<'py> PyWeakrefMethods<'py> for Bound<'py, PyWeakref> { - fn upgrade(&self) -> Option> { - let mut obj: *mut ffi::PyObject = std::ptr::null_mut(); - match unsafe { ffi::compat::PyWeakref_GetRef(self.as_ptr(), &mut obj) } { - std::os::raw::c_int::MIN..=-1 => panic!("The 'weakref' weak reference instance should be valid (non-null and actually a weakref reference)"), - 0 => None, - 1..=std::os::raw::c_int::MAX => Some(unsafe { obj.assume_owned_unchecked(self.py()) }), - } - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::{PyAny, PyAnyMethods}; - use crate::types::weakref::{PyWeakref, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference}; - use crate::{Bound, PyResult, Python}; - - fn new_reference<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - let reference = PyWeakrefReference::new(object)?; - reference.into_any().downcast_into().map_err(Into::into) - } - - fn new_proxy<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - let reference = PyWeakrefProxy::new(object)?; - reference.into_any().downcast_into().map_err(Into::into) - } - - mod python_class { - use super::*; - use crate::ffi; - use crate::{py_result_ext::PyResultExt, types::PyType}; - - fn get_type(py: Python<'_>) -> PyResult> { - py.run(ffi::c_str!("class A:\n pass\n"), None, None)?; - py.eval(ffi::c_str!("A"), None, None) - .downcast_into::() - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - ) -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = create_reference(&object)?; - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - inner(new_reference)?; - inner(new_proxy) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - ) -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = create_reference(&object)?; - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - inner(new_reference)?; - inner(new_proxy) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - call_retrievable: bool, - ) -> PyResult<()> { - let not_call_retrievable = !call_retrievable; - - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = create_reference(&object)?; - - assert!(not_call_retrievable || reference.call0()?.is(&object)); - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(not_call_retrievable || reference.call0()?.is_none()); - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - inner(new_reference, true)?; - inner(new_proxy, false) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - call_retrievable: bool, - ) -> PyResult<()> { - let not_call_retrievable = !call_retrievable; - - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = create_reference(&object)?; - - assert!(not_call_retrievable || reference.call0()?.is(&object)); - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(not_call_retrievable || reference.call0()?.is(&reference.get_object())); - assert!(not_call_retrievable || reference.call0()?.is_none()); - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - - inner(new_reference, true)?; - inner(new_proxy, false) - } - } - - // under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable. - #[cfg(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))))] - mod pyo3_pyclass { - use super::*; - use crate::{pyclass, Py}; - - #[pyclass(weakref, crate = "crate")] - struct WeakrefablePyClass {} - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - ) -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = create_reference(object.bind(py))?; - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - inner(new_reference)?; - inner(new_proxy) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - ) -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = create_reference(object.bind(py))?; - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - inner(new_reference)?; - inner(new_proxy) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - call_retrievable: bool, - ) -> PyResult<()> { - let not_call_retrievable = !call_retrievable; - - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = create_reference(object.bind(py))?; - - assert!(not_call_retrievable || reference.call0()?.is(&object)); - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(not_call_retrievable || reference.call0()?.is_none()); - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - inner(new_reference, true)?; - inner(new_proxy, false) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - fn inner( - create_reference: impl for<'py> FnOnce( - &Bound<'py, PyAny>, - ) - -> PyResult>, - call_retrievable: bool, - ) -> PyResult<()> { - let not_call_retrievable = !call_retrievable; - - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = create_reference(object.bind(py))?; - - assert!(not_call_retrievable || reference.call0()?.is(&object)); - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(not_call_retrievable || reference.call0()?.is(&reference.get_object())); - assert!(not_call_retrievable || reference.call0()?.is_none()); - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - - inner(new_reference, true)?; - inner(new_proxy, false) - } - } -} diff --git a/vendor/pyo3/src/types/weakref/mod.rs b/vendor/pyo3/src/types/weakref/mod.rs deleted file mode 100644 index 49d4e515b1..0000000000 --- a/vendor/pyo3/src/types/weakref/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -pub use anyref::{PyWeakref, PyWeakrefMethods}; -pub use proxy::PyWeakrefProxy; -pub use reference::PyWeakrefReference; - -pub(crate) mod anyref; -pub(crate) mod proxy; -pub(crate) mod reference; diff --git a/vendor/pyo3/src/types/weakref/proxy.rs b/vendor/pyo3/src/types/weakref/proxy.rs deleted file mode 100644 index 5334f0341f..0000000000 --- a/vendor/pyo3/src/types/weakref/proxy.rs +++ /dev/null @@ -1,935 +0,0 @@ -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::type_object::PyTypeCheck; -use crate::types::any::PyAny; -use crate::{ffi, Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt}; - -use super::PyWeakrefMethods; - -/// Represents any Python `weakref` Proxy type. -/// -/// In Python this is created by calling `weakref.proxy`. -/// This is either a `weakref.ProxyType` or a `weakref.CallableProxyType` (`weakref.ProxyTypes`). -#[repr(transparent)] -pub struct PyWeakrefProxy(PyAny); - -pyobject_native_type_named!(PyWeakrefProxy); - -// TODO: We known the layout but this cannot be implemented, due to the lack of public typeobject pointers. And it is 2 distinct types -// #[cfg(not(Py_LIMITED_API))] -// pyobject_native_type_sized!(PyWeakrefProxy, ffi::PyWeakReference); - -impl PyTypeCheck for PyWeakrefProxy { - const NAME: &'static str = "weakref.ProxyTypes"; - - fn type_check(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::PyWeakref_CheckProxy(object.as_ptr()) > 0 } - } -} - -/// TODO: UPDATE DOCS -impl PyWeakrefProxy { - /// Constructs a new Weak Reference (`weakref.proxy`/`weakref.ProxyType`/`weakref.CallableProxyType`) for the given object. - /// - /// Returns a `TypeError` if `object` is not weak referenceable (Most native types and PyClasses without `weakref` flag). - /// - /// # Examples - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefProxy; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let foo = Bound::new(py, Foo {})?; - /// let weakref = PyWeakrefProxy::new(&foo)?; - /// assert!( - /// // In normal situations where a direct `Bound<'py, Foo>` is required use `upgrade::` - /// weakref.upgrade() - /// .map_or(false, |obj| obj.is(&foo)) - /// ); - /// - /// let weakref2 = PyWeakrefProxy::new(&foo)?; - /// assert!(weakref.is(&weakref2)); - /// - /// drop(foo); - /// - /// assert!(weakref.upgrade().is_none()); - /// Ok(()) - /// }) - /// # } - /// ``` - #[inline] - pub fn new<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - unsafe { - Bound::from_owned_ptr_or_err( - object.py(), - ffi::PyWeakref_NewProxy(object.as_ptr(), ffi::Py_None()), - ) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyWeakrefProxy::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyWeakrefProxy::new`")] - #[inline] - pub fn new_bound<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - Self::new(object) - } - - /// Constructs a new Weak Reference (`weakref.proxy`/`weakref.ProxyType`/`weakref.CallableProxyType`) for the given object with a callback. - /// - /// Returns a `TypeError` if `object` is not weak referenceable (Most native types and PyClasses without `weakref` flag) or if the `callback` is not callable or None. - /// - /// # Examples - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefProxy; - /// use pyo3::ffi::c_str; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// #[pyfunction] - /// fn callback(wref: Bound<'_, PyWeakrefProxy>) -> PyResult<()> { - /// let py = wref.py(); - /// assert!(wref.upgrade_as::()?.is_none()); - /// py.run(c_str!("counter = 1"), None, None) - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// py.run(c_str!("counter = 0"), None, None)?; - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 0); - /// let foo = Bound::new(py, Foo{})?; - /// - /// // This is fine. - /// let weakref = PyWeakrefProxy::new_with(&foo, py.None())?; - /// assert!(weakref.upgrade_as::()?.is_some()); - /// assert!( - /// // In normal situations where a direct `Bound<'py, Foo>` is required use `upgrade::` - /// weakref.upgrade() - /// .map_or(false, |obj| obj.is(&foo)) - /// ); - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 0); - /// - /// let weakref2 = PyWeakrefProxy::new_with(&foo, wrap_pyfunction!(callback, py)?)?; - /// assert!(!weakref.is(&weakref2)); // Not the same weakref - /// assert!(weakref.eq(&weakref2)?); // But Equal, since they point to the same object - /// - /// drop(foo); - /// - /// assert!(weakref.upgrade_as::()?.is_none()); - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 1); - /// Ok(()) - /// }) - /// # } - /// ``` - #[inline] - pub fn new_with<'py, C>( - object: &Bound<'py, PyAny>, - callback: C, - ) -> PyResult> - where - C: IntoPyObject<'py>, - { - fn inner<'py>( - object: &Bound<'py, PyAny>, - callback: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { - Bound::from_owned_ptr_or_err( - object.py(), - ffi::PyWeakref_NewProxy(object.as_ptr(), callback.as_ptr()), - ) - .downcast_into_unchecked() - } - } - - let py = object.py(); - inner( - object, - callback - .into_pyobject_or_pyerr(py)? - .into_any() - .as_borrowed(), - ) - } - - /// Deprecated name for [`PyWeakrefProxy::new_with`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyWeakrefProxy::new_with`")] - #[allow(deprecated)] - #[inline] - pub fn new_bound_with<'py, C>( - object: &Bound<'py, PyAny>, - callback: C, - ) -> PyResult> - where - C: crate::ToPyObject, - { - Self::new_with(object, callback.to_object(object.py())) - } -} - -impl<'py> PyWeakrefMethods<'py> for Bound<'py, PyWeakrefProxy> { - fn upgrade(&self) -> Option> { - let mut obj: *mut ffi::PyObject = std::ptr::null_mut(); - match unsafe { ffi::compat::PyWeakref_GetRef(self.as_ptr(), &mut obj) } { - std::os::raw::c_int::MIN..=-1 => panic!("The 'weakref.ProxyType' (or `weakref.CallableProxyType`) instance should be valid (non-null and actually a weakref reference)"), - 0 => None, - 1..=std::os::raw::c_int::MAX => Some(unsafe { obj.assume_owned_unchecked(self.py()) }), - } - } -} - -#[cfg(test)] -mod tests { - use crate::exceptions::{PyAttributeError, PyReferenceError, PyTypeError}; - use crate::types::any::{PyAny, PyAnyMethods}; - use crate::types::weakref::{PyWeakrefMethods, PyWeakrefProxy}; - use crate::{Bound, PyResult, Python}; - - #[cfg(all(Py_3_13, not(Py_LIMITED_API)))] - const DEADREF_FIX: Option<&str> = None; - #[cfg(all(not(Py_3_13), not(Py_LIMITED_API)))] - const DEADREF_FIX: Option<&str> = Some("NoneType"); - - #[cfg(not(Py_LIMITED_API))] - fn check_repr( - reference: &Bound<'_, PyWeakrefProxy>, - object: &Bound<'_, PyAny>, - class: Option<&str>, - ) -> PyResult<()> { - let repr = reference.repr()?.to_string(); - - #[cfg(Py_3_13)] - let (first_part, second_part) = repr.split_once(';').unwrap(); - #[cfg(not(Py_3_13))] - let (first_part, second_part) = repr.split_once(" to ").unwrap(); - - { - let (msg, addr) = first_part.split_once("0x").unwrap(); - - assert_eq!(msg, ") -> PyResult> { - let globals = PyDict::new(py); - py.run(ffi::c_str!("class A:\n pass\n"), Some(&globals), None)?; - py.eval(ffi::c_str!("A"), Some(&globals), None) - .downcast_into::() - } - - #[test] - fn test_weakref_proxy_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - - #[cfg(not(Py_LIMITED_API))] - assert_eq!( - reference.get_type().to_string(), - format!("", CLASS_NAME) - ); - - assert_eq!(reference.getattr("__class__")?.to_string(), ""); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, &object, Some("A"))?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference.call0().err().map_or(false, |err| { - let result = err.is_instance_of::(py); - #[cfg(not(Py_LIMITED_API))] - let result = result - & (err.value(py).to_string() - == format!("{} object is not callable", CLASS_NAME)); - result - })); - - drop(object); - - assert!(reference.upgrade().is_none()); - assert!(reference - .getattr("__class__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, py.None().bind(py), None)?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference.call0().err().map_or(false, |err| { - let result = err.is_instance_of::(py); - #[cfg(not(Py_LIMITED_API))] - let result = result - & (err.value(py).to_string() - == format!("{} object is not callable", CLASS_NAME)); - result - })); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(reference.upgrade().unwrap().is(&object)); - - drop(object); - - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - } - - // under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable. - #[cfg(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))))] - mod pyo3_pyclass { - use super::*; - use crate::{pyclass, Py}; - - #[pyclass(weakref, crate = "crate")] - struct WeakrefablePyClass {} - - #[test] - fn test_weakref_proxy_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let object: Bound<'_, WeakrefablePyClass> = - Bound::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - #[cfg(not(Py_LIMITED_API))] - assert_eq!( - reference.get_type().to_string(), - format!("", CLASS_NAME) - ); - - assert_eq!( - reference.getattr("__class__")?.to_string(), - "" - ); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, object.as_any(), Some("WeakrefablePyClass"))?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference.call0().err().map_or(false, |err| { - let result = err.is_instance_of::(py); - #[cfg(not(Py_LIMITED_API))] - let result = result - & (err.value(py).to_string() - == format!("{} object is not callable", CLASS_NAME)); - result - })); - - drop(object); - - assert!(reference.upgrade().is_none()); - assert!(reference - .getattr("__class__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, py.None().bind(py), None)?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference.call0().err().map_or(false, |err| { - let result = err.is_instance_of::(py); - #[cfg(not(Py_LIMITED_API))] - let result = result - & (err.value(py).to_string() - == format!("{} object is not callable", CLASS_NAME)); - result - })); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - } - } - - mod callable_proxy { - use super::*; - - #[cfg(all(not(Py_LIMITED_API), Py_3_10))] - const CLASS_NAME: &str = ""; - #[cfg(all(not(Py_LIMITED_API), not(Py_3_10)))] - const CLASS_NAME: &str = ""; - - mod python_class { - use super::*; - use crate::ffi; - use crate::{py_result_ext::PyResultExt, types::PyDict, types::PyType}; - - fn get_type(py: Python<'_>) -> PyResult> { - let globals = PyDict::new(py); - py.run( - ffi::c_str!("class A:\n def __call__(self):\n return 'This class is callable!'\n"), - Some(&globals), - None, - )?; - py.eval(ffi::c_str!("A"), Some(&globals), None) - .downcast_into::() - } - - #[test] - fn test_weakref_proxy_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.get_type().to_string(), CLASS_NAME); - - assert_eq!(reference.getattr("__class__")?.to_string(), ""); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, &object, Some("A"))?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert_eq!(reference.call0()?.to_string(), "This class is callable!"); - - drop(object); - - assert!(reference.upgrade().is_none()); - assert!(reference - .getattr("__class__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, py.None().bind(py), None)?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference - .call0() - .err() - .map_or(false, |err| err.is_instance_of::(py) - & (err.value(py).to_string() - == "weakly-referenced object no longer exists"))); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - } - - // under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable. - #[cfg(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))))] - mod pyo3_pyclass { - use super::*; - use crate::{pyclass, pymethods, Py}; - - #[pyclass(weakref, crate = "crate")] - struct WeakrefablePyClass {} - - #[pymethods(crate = "crate")] - impl WeakrefablePyClass { - fn __call__(&self) -> &str { - "This class is callable!" - } - } - - #[test] - fn test_weakref_proxy_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let object: Bound<'_, WeakrefablePyClass> = - Bound::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.get_type().to_string(), CLASS_NAME); - - assert_eq!( - reference.getattr("__class__")?.to_string(), - "" - ); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, object.as_any(), Some("WeakrefablePyClass"))?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert_eq!(reference.call0()?.to_string(), "This class is callable!"); - - drop(object); - - assert!(reference.upgrade().is_none()); - assert!(reference - .getattr("__class__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, py.None().bind(py), None)?; - - assert!(reference - .getattr("__callback__") - .err() - .map_or(false, |err| err.is_instance_of::(py))); - - assert!(reference - .call0() - .err() - .map_or(false, |err| err.is_instance_of::(py) - & (err.value(py).to_string() - == "weakly-referenced object no longer exists"))); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefProxy::new(object.bind(py))?; - - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - } - } -} diff --git a/vendor/pyo3/src/types/weakref/reference.rs b/vendor/pyo3/src/types/weakref/reference.rs deleted file mode 100644 index edabb6da93..0000000000 --- a/vendor/pyo3/src/types/weakref/reference.rs +++ /dev/null @@ -1,561 +0,0 @@ -use crate::err::PyResult; -use crate::ffi_ptr_ext::FfiPtrExt; -use crate::py_result_ext::PyResultExt; -use crate::types::any::PyAny; -use crate::{ffi, Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt}; - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -use crate::type_object::PyTypeCheck; - -use super::PyWeakrefMethods; - -/// Represents a Python `weakref.ReferenceType`. -/// -/// In Python this is created by calling `weakref.ref`. -#[repr(transparent)] -pub struct PyWeakrefReference(PyAny); - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -pyobject_subclassable_native_type!(PyWeakrefReference, crate::ffi::PyWeakReference); - -#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))] -pyobject_native_type!( - PyWeakrefReference, - ffi::PyWeakReference, - pyobject_native_static_type_object!(ffi::_PyWeakref_RefType), - #module=Some("weakref"), - #checkfunction=ffi::PyWeakref_CheckRefExact -); - -// When targetting alternative or multiple interpreters, it is better to not use the internal API. -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -pyobject_native_type_named!(PyWeakrefReference); - -#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))] -impl PyTypeCheck for PyWeakrefReference { - const NAME: &'static str = "weakref.ReferenceType"; - - fn type_check(object: &Bound<'_, PyAny>) -> bool { - unsafe { ffi::PyWeakref_CheckRef(object.as_ptr()) > 0 } - } -} - -impl PyWeakrefReference { - /// Constructs a new Weak Reference (`weakref.ref`/`weakref.ReferenceType`) for the given object. - /// - /// Returns a `TypeError` if `object` is not weak referenceable (Most native types and PyClasses without `weakref` flag). - /// - /// # Examples - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// let foo = Bound::new(py, Foo {})?; - /// let weakref = PyWeakrefReference::new(&foo)?; - /// assert!( - /// // In normal situations where a direct `Bound<'py, Foo>` is required use `upgrade::` - /// weakref.upgrade() - /// .map_or(false, |obj| obj.is(&foo)) - /// ); - /// - /// let weakref2 = PyWeakrefReference::new(&foo)?; - /// assert!(weakref.is(&weakref2)); - /// - /// drop(foo); - /// - /// assert!(weakref.upgrade().is_none()); - /// Ok(()) - /// }) - /// # } - /// ``` - pub fn new<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - unsafe { - Bound::from_owned_ptr_or_err( - object.py(), - ffi::PyWeakref_NewRef(object.as_ptr(), ffi::Py_None()), - ) - .downcast_into_unchecked() - } - } - - /// Deprecated name for [`PyWeakrefReference::new`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyWeakrefReference::new`")] - #[inline] - pub fn new_bound<'py>(object: &Bound<'py, PyAny>) -> PyResult> { - Self::new(object) - } - - /// Constructs a new Weak Reference (`weakref.ref`/`weakref.ReferenceType`) for the given object with a callback. - /// - /// Returns a `TypeError` if `object` is not weak referenceable (Most native types and PyClasses without `weakref` flag) or if the `callback` is not callable or None. - /// - /// # Examples - #[cfg_attr( - not(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9))))), - doc = "```rust,ignore" - )] - #[cfg_attr( - all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))), - doc = "```rust" - )] - /// use pyo3::prelude::*; - /// use pyo3::types::PyWeakrefReference; - /// use pyo3::ffi::c_str; - /// - /// #[pyclass(weakref)] - /// struct Foo { /* fields omitted */ } - /// - /// #[pyfunction] - /// fn callback(wref: Bound<'_, PyWeakrefReference>) -> PyResult<()> { - /// let py = wref.py(); - /// assert!(wref.upgrade_as::()?.is_none()); - /// py.run(c_str!("counter = 1"), None, None) - /// } - /// - /// # fn main() -> PyResult<()> { - /// Python::with_gil(|py| { - /// py.run(c_str!("counter = 0"), None, None)?; - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 0); - /// let foo = Bound::new(py, Foo{})?; - /// - /// // This is fine. - /// let weakref = PyWeakrefReference::new_with(&foo, py.None())?; - /// assert!(weakref.upgrade_as::()?.is_some()); - /// assert!( - /// // In normal situations where a direct `Bound<'py, Foo>` is required use `upgrade::` - /// weakref.upgrade() - /// .map_or(false, |obj| obj.is(&foo)) - /// ); - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 0); - /// - /// let weakref2 = PyWeakrefReference::new_with(&foo, wrap_pyfunction!(callback, py)?)?; - /// assert!(!weakref.is(&weakref2)); // Not the same weakref - /// assert!(weakref.eq(&weakref2)?); // But Equal, since they point to the same object - /// - /// drop(foo); - /// - /// assert!(weakref.upgrade_as::()?.is_none()); - /// assert_eq!(py.eval(c_str!("counter"), None, None)?.extract::()?, 1); - /// Ok(()) - /// }) - /// # } - /// ``` - pub fn new_with<'py, C>( - object: &Bound<'py, PyAny>, - callback: C, - ) -> PyResult> - where - C: IntoPyObject<'py>, - { - fn inner<'py>( - object: &Bound<'py, PyAny>, - callback: Borrowed<'_, 'py, PyAny>, - ) -> PyResult> { - unsafe { - Bound::from_owned_ptr_or_err( - object.py(), - ffi::PyWeakref_NewRef(object.as_ptr(), callback.as_ptr()), - ) - .downcast_into_unchecked() - } - } - - let py = object.py(); - inner( - object, - callback - .into_pyobject_or_pyerr(py)? - .into_any() - .as_borrowed(), - ) - } - - /// Deprecated name for [`PyWeakrefReference::new_with`]. - #[deprecated(since = "0.23.0", note = "renamed to `PyWeakrefReference::new_with`")] - #[allow(deprecated)] - #[inline] - pub fn new_bound_with<'py, C>( - object: &Bound<'py, PyAny>, - callback: C, - ) -> PyResult> - where - C: crate::ToPyObject, - { - Self::new_with(object, callback.to_object(object.py())) - } -} - -impl<'py> PyWeakrefMethods<'py> for Bound<'py, PyWeakrefReference> { - fn upgrade(&self) -> Option> { - let mut obj: *mut ffi::PyObject = std::ptr::null_mut(); - match unsafe { ffi::compat::PyWeakref_GetRef(self.as_ptr(), &mut obj) } { - std::os::raw::c_int::MIN..=-1 => panic!("The 'weakref.ReferenceType' instance should be valid (non-null and actually a weakref reference)"), - 0 => None, - 1..=std::os::raw::c_int::MAX => Some(unsafe { obj.assume_owned_unchecked(self.py()) }), - } - } -} - -#[cfg(test)] -mod tests { - use crate::types::any::{PyAny, PyAnyMethods}; - use crate::types::weakref::{PyWeakrefMethods, PyWeakrefReference}; - use crate::{Bound, PyResult, Python}; - - #[cfg(all(not(Py_LIMITED_API), Py_3_10))] - const CLASS_NAME: &str = ""; - #[cfg(all(not(Py_LIMITED_API), not(Py_3_10)))] - const CLASS_NAME: &str = ""; - - fn check_repr( - reference: &Bound<'_, PyWeakrefReference>, - object: Option<(&Bound<'_, PyAny>, &str)>, - ) -> PyResult<()> { - let repr = reference.repr()?.to_string(); - let (first_part, second_part) = repr.split_once("; ").unwrap(); - - { - let (msg, addr) = first_part.split_once("0x").unwrap(); - - assert_eq!(msg, " { - let (msg, addr) = second_part.split_once("0x").unwrap(); - - // Avoid testing on reprs directly since they the quoting and full path vs class name tends to be changedi undocumented. - assert!(msg.starts_with("to '")); - assert!(msg.contains(class)); - assert!(msg.ends_with("' at ")); - - assert!(addr - .to_lowercase() - .contains(format!("{:x?}", object.as_ptr()).split_at(2).1)); - } - None => { - assert_eq!(second_part, "dead>") - } - } - - Ok(()) - } - - mod python_class { - use super::*; - use crate::ffi; - use crate::{py_result_ext::PyResultExt, types::PyType}; - - fn get_type(py: Python<'_>) -> PyResult> { - py.run(ffi::c_str!("class A:\n pass\n"), None, None)?; - py.eval(ffi::c_str!("A"), None, None) - .downcast_into::() - } - - #[test] - fn test_weakref_reference_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefReference::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.get_type().to_string(), CLASS_NAME); - - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.getattr("__class__")?.to_string(), CLASS_NAME); - - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, Some((object.as_any(), "A")))?; - - assert!(reference - .getattr("__callback__") - .map_or(false, |result| result.is_none())); - - assert!(reference.call0()?.is(&object)); - - drop(object); - - assert!(reference.upgrade().is_none()); - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.getattr("__class__")?.to_string(), CLASS_NAME); - check_repr(&reference, None)?; - - assert!(reference - .getattr("__callback__") - .map_or(false, |result| result.is_none())); - - assert!(reference.call0()?.is_none()); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefReference::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefReference::new(&object)?; - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr() - && obj.is_exact_instance(&class))); - } - - drop(object); - - { - // This test is a bit weird but ok. - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefReference::new(&object)?; - - assert!(reference.call0()?.is(&object)); - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.call0()?.is_none()); - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let class = get_type(py)?; - let object = class.call0()?; - let reference = PyWeakrefReference::new(&object)?; - - assert!(reference.call0()?.is(&object)); - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(reference.call0()?.is(&reference.get_object())); - assert!(reference.call0()?.is_none()); - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - } - - // under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable. - #[cfg(all(feature = "macros", not(all(Py_LIMITED_API, not(Py_3_9)))))] - mod pyo3_pyclass { - use super::*; - use crate::{pyclass, Py}; - - #[pyclass(weakref, crate = "crate")] - struct WeakrefablePyClass {} - - #[test] - fn test_weakref_reference_behavior() -> PyResult<()> { - Python::with_gil(|py| { - let object: Bound<'_, WeakrefablePyClass> = Bound::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefReference::new(&object)?; - - assert!(!reference.is(&object)); - assert!(reference.upgrade().unwrap().is(&object)); - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.get_type().to_string(), CLASS_NAME); - - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.getattr("__class__")?.to_string(), CLASS_NAME); - #[cfg(not(Py_LIMITED_API))] - check_repr(&reference, Some((object.as_any(), "WeakrefablePyClass")))?; - - assert!(reference - .getattr("__callback__") - .map_or(false, |result| result.is_none())); - - assert!(reference.call0()?.is(&object)); - - drop(object); - - assert!(reference.upgrade().is_none()); - #[cfg(not(Py_LIMITED_API))] - assert_eq!(reference.getattr("__class__")?.to_string(), CLASS_NAME); - check_repr(&reference, None)?; - - assert!(reference - .getattr("__callback__") - .map_or(false, |result| result.is_none())); - - assert!(reference.call0()?.is_none()); - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefReference::new(object.bind(py))?; - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = reference.upgrade_as::(); - - assert!(obj.is_ok()); - let obj = obj.unwrap(); - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade_as_unchecked() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefReference::new(object.bind(py))?; - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_some()); - assert!(obj.map_or(false, |obj| obj.as_ptr() == object.as_ptr())); - } - - drop(object); - - { - let obj = unsafe { reference.upgrade_as_unchecked::() }; - - assert!(obj.is_none()); - } - - Ok(()) - }) - } - - #[test] - fn test_weakref_upgrade() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefReference::new(object.bind(py))?; - - assert!(reference.call0()?.is(&object)); - assert!(reference.upgrade().is_some()); - assert!(reference.upgrade().map_or(false, |obj| obj.is(&object))); - - drop(object); - - assert!(reference.call0()?.is_none()); - assert!(reference.upgrade().is_none()); - - Ok(()) - }) - } - - #[test] - #[allow(deprecated)] - fn test_weakref_get_object() -> PyResult<()> { - Python::with_gil(|py| { - let object = Py::new(py, WeakrefablePyClass {})?; - let reference = PyWeakrefReference::new(object.bind(py))?; - - assert!(reference.call0()?.is(&object)); - assert!(reference.get_object().is(&object)); - - drop(object); - - assert!(reference.call0()?.is(&reference.get_object())); - assert!(reference.call0()?.is_none()); - assert!(reference.get_object().is_none()); - - Ok(()) - }) - } - } -} diff --git a/vendor/pyo3/src/version.rs b/vendor/pyo3/src/version.rs deleted file mode 100644 index e241c02c92..0000000000 --- a/vendor/pyo3/src/version.rs +++ /dev/null @@ -1,143 +0,0 @@ -/// Represents the major, minor, and patch (if any) versions of this interpreter. -/// -/// This struct is usually created with [`Python::version`]. -/// -/// # Examples -/// -/// ```rust -/// # use pyo3::Python; -/// Python::with_gil(|py| { -/// // PyO3 supports Python 3.7 and up. -/// assert!(py.version_info() >= (3, 7)); -/// assert!(py.version_info() >= (3, 7, 0)); -/// }); -/// ``` -/// -/// [`Python::version`]: crate::marker::Python::version -#[derive(Debug)] -pub struct PythonVersionInfo<'a> { - /// Python major version (e.g. `3`). - pub major: u8, - /// Python minor version (e.g. `11`). - pub minor: u8, - /// Python patch version (e.g. `0`). - pub patch: u8, - /// Python version suffix, if applicable (e.g. `a0`). - pub suffix: Option<&'a str>, -} - -impl<'a> PythonVersionInfo<'a> { - /// Parses a hard-coded Python interpreter version string (e.g. 3.9.0a4+). - pub(crate) fn from_str(version_number_str: &'a str) -> Result, &'a str> { - fn split_and_parse_number(version_part: &str) -> (u8, Option<&str>) { - match version_part.find(|c: char| !c.is_ascii_digit()) { - None => (version_part.parse().unwrap(), None), - Some(version_part_suffix_start) => { - let (version_part, version_part_suffix) = - version_part.split_at(version_part_suffix_start); - (version_part.parse().unwrap(), Some(version_part_suffix)) - } - } - } - - let mut parts = version_number_str.split('.'); - let major_str = parts.next().ok_or("Python major version missing")?; - let minor_str = parts.next().ok_or("Python minor version missing")?; - let patch_str = parts.next(); - if parts.next().is_some() { - return Err("Python version string has too many parts"); - }; - - let major = major_str - .parse() - .map_err(|_| "Python major version not an integer")?; - let (minor, suffix) = split_and_parse_number(minor_str); - if suffix.is_some() { - assert!(patch_str.is_none()); - return Ok(PythonVersionInfo { - major, - minor, - patch: 0, - suffix, - }); - } - - let (patch, suffix) = patch_str.map(split_and_parse_number).unwrap_or_default(); - Ok(PythonVersionInfo { - major, - minor, - patch, - suffix, - }) - } -} - -impl PartialEq<(u8, u8)> for PythonVersionInfo<'_> { - fn eq(&self, other: &(u8, u8)) -> bool { - self.major == other.0 && self.minor == other.1 - } -} - -impl PartialEq<(u8, u8, u8)> for PythonVersionInfo<'_> { - fn eq(&self, other: &(u8, u8, u8)) -> bool { - self.major == other.0 && self.minor == other.1 && self.patch == other.2 - } -} - -impl PartialOrd<(u8, u8)> for PythonVersionInfo<'_> { - fn partial_cmp(&self, other: &(u8, u8)) -> Option { - (self.major, self.minor).partial_cmp(other) - } -} - -impl PartialOrd<(u8, u8, u8)> for PythonVersionInfo<'_> { - fn partial_cmp(&self, other: &(u8, u8, u8)) -> Option { - (self.major, self.minor, self.patch).partial_cmp(other) - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::Python; - #[test] - fn test_python_version_info() { - Python::with_gil(|py| { - let version = py.version_info(); - #[cfg(Py_3_7)] - assert!(version >= (3, 7)); - #[cfg(Py_3_7)] - assert!(version >= (3, 7, 0)); - #[cfg(Py_3_8)] - assert!(version >= (3, 8)); - #[cfg(Py_3_8)] - assert!(version >= (3, 8, 0)); - #[cfg(Py_3_9)] - assert!(version >= (3, 9)); - #[cfg(Py_3_9)] - assert!(version >= (3, 9, 0)); - #[cfg(Py_3_10)] - assert!(version >= (3, 10)); - #[cfg(Py_3_10)] - assert!(version >= (3, 10, 0)); - #[cfg(Py_3_11)] - assert!(version >= (3, 11)); - #[cfg(Py_3_11)] - assert!(version >= (3, 11, 0)); - }); - } - - #[test] - fn test_python_version_info_parse() { - assert!(PythonVersionInfo::from_str("3.5.0a1").unwrap() >= (3, 5, 0)); - assert!(PythonVersionInfo::from_str("3.5+").unwrap() >= (3, 5, 0)); - assert!(PythonVersionInfo::from_str("3.5+").unwrap() == (3, 5, 0)); - assert!(PythonVersionInfo::from_str("3.5+").unwrap() != (3, 5, 1)); - assert!(PythonVersionInfo::from_str("3.5.2a1+").unwrap() < (3, 5, 3)); - assert!(PythonVersionInfo::from_str("3.5.2a1+").unwrap() == (3, 5, 2)); - assert!(PythonVersionInfo::from_str("3.5.2a1+").unwrap() == (3, 5)); - assert!(PythonVersionInfo::from_str("3.5+").unwrap() == (3, 5)); - assert!(PythonVersionInfo::from_str("3.5.2a1+").unwrap() < (3, 6)); - assert!(PythonVersionInfo::from_str("3.5.2a1+").unwrap() > (3, 4)); - } -} diff --git a/vendor/pyo3/tests/test_anyhow.rs b/vendor/pyo3/tests/test_anyhow.rs deleted file mode 100644 index faa8a70f9c..0000000000 --- a/vendor/pyo3/tests/test_anyhow.rs +++ /dev/null @@ -1,54 +0,0 @@ -#![cfg(feature = "anyhow")] - -use pyo3::{ffi, wrap_pyfunction}; - -#[test] -fn test_anyhow_py_function_ok_result() { - use pyo3::{py_run, pyfunction, Python}; - - #[pyfunction] - #[allow(clippy::unnecessary_wraps)] - fn produce_ok_result() -> anyhow::Result { - Ok(String::from("OK buddy")) - } - - Python::with_gil(|py| { - let func = wrap_pyfunction!(produce_ok_result)(py).unwrap(); - - py_run!( - py, - func, - r#" - func() - "# - ); - }); -} - -#[test] -fn test_anyhow_py_function_err_result() { - use pyo3::prelude::PyDictMethods; - use pyo3::{pyfunction, types::PyDict, Python}; - - #[pyfunction] - fn produce_err_result() -> anyhow::Result { - anyhow::bail!("error time") - } - - Python::with_gil(|py| { - let func = wrap_pyfunction!(produce_err_result)(py).unwrap(); - let locals = PyDict::new(py); - locals.set_item("func", func).unwrap(); - - py.run( - ffi::c_str!( - r#" - func() - "# - ), - None, - Some(&locals), - ) - .unwrap_err(); - }); -} diff --git a/vendor/pyo3/tests/test_append_to_inittab.rs b/vendor/pyo3/tests/test_append_to_inittab.rs deleted file mode 100644 index fdf14a2aea..0000000000 --- a/vendor/pyo3/tests/test_append_to_inittab.rs +++ /dev/null @@ -1,60 +0,0 @@ -#![cfg(all(feature = "macros", not(PyPy)))] - -use pyo3::prelude::*; - -#[pyfunction] -fn foo() -> usize { - 123 -} - -#[pymodule] -fn module_fn_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(foo, m)?)?; - Ok(()) -} - -#[pymodule] -mod module_mod_with_functions { - #[pymodule_export] - use super::foo; -} - -#[cfg(not(PyPy))] -#[test] -fn test_module_append_to_inittab() { - use pyo3::{append_to_inittab, ffi}; - - append_to_inittab!(module_fn_with_functions); - - append_to_inittab!(module_mod_with_functions); - - Python::with_gil(|py| { - py.run( - ffi::c_str!( - r#" -import module_fn_with_functions -assert module_fn_with_functions.foo() == 123 -"# - ), - None, - None, - ) - .map_err(|e| e.display(py)) - .unwrap(); - }); - - Python::with_gil(|py| { - py.run( - ffi::c_str!( - r#" -import module_mod_with_functions -assert module_mod_with_functions.foo() == 123 -"# - ), - None, - None, - ) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} diff --git a/vendor/pyo3/tests/test_arithmetics.rs b/vendor/pyo3/tests/test_arithmetics.rs deleted file mode 100644 index 21e32d4c13..0000000000 --- a/vendor/pyo3/tests/test_arithmetics.rs +++ /dev/null @@ -1,774 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::class::basic::CompareOp; -use pyo3::py_run; -use pyo3::{prelude::*, BoundObject}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct UnaryArithmetic { - inner: f64, -} - -#[pymethods] -impl UnaryArithmetic { - #[new] - fn new(value: f64) -> Self { - UnaryArithmetic { inner: value } - } - - fn __repr__(&self) -> String { - format!("UA({})", self.inner) - } - - fn __neg__(&self) -> Self { - Self::new(-self.inner) - } - - fn __pos__(&self) -> Self { - Self::new(self.inner) - } - - fn __abs__(&self) -> Self { - Self::new(self.inner.abs()) - } - - fn __invert__(&self) -> Self { - Self::new(self.inner.recip()) - } - - #[pyo3(signature=(_ndigits=None))] - fn __round__(&self, _ndigits: Option) -> Self { - Self::new(self.inner.round()) - } -} - -#[test] -fn unary_arithmetic() { - Python::with_gil(|py| { - let c = Py::new(py, UnaryArithmetic::new(2.7)).unwrap(); - py_run!(py, c, "assert repr(-c) == 'UA(-2.7)'"); - py_run!(py, c, "assert repr(+c) == 'UA(2.7)'"); - py_run!(py, c, "assert repr(abs(c)) == 'UA(2.7)'"); - py_run!(py, c, "assert repr(~c) == 'UA(0.37037037037037035)'"); - py_run!(py, c, "assert repr(round(c)) == 'UA(3)'"); - py_run!(py, c, "assert repr(round(c, 1)) == 'UA(3)'"); - - let c: Bound<'_, PyAny> = c.extract(py).unwrap(); - assert_py_eq!(c.neg().unwrap().repr().unwrap().as_any(), "UA(-2.7)"); - assert_py_eq!(c.pos().unwrap().repr().unwrap().as_any(), "UA(2.7)"); - assert_py_eq!(c.abs().unwrap().repr().unwrap().as_any(), "UA(2.7)"); - assert_py_eq!( - c.bitnot().unwrap().repr().unwrap().as_any(), - "UA(0.37037037037037035)" - ); - }); -} - -#[pyclass] -struct Indexable(i32); - -#[pymethods] -impl Indexable { - fn __index__(&self) -> i32 { - self.0 - } - - fn __int__(&self) -> i32 { - self.0 - } - - fn __float__(&self) -> f64 { - f64::from(self.0) - } - - fn __invert__(&self) -> Self { - Self(!self.0) - } -} - -#[test] -fn indexable() { - Python::with_gil(|py| { - let i = Py::new(py, Indexable(5)).unwrap(); - py_run!(py, i, "assert int(i) == 5"); - py_run!(py, i, "assert [0, 1, 2, 3, 4, 5][i] == 5"); - py_run!(py, i, "assert float(i) == 5.0"); - py_run!(py, i, "assert int(~i) == -6"); - }) -} - -#[pyclass] -struct InPlaceOperations { - value: u32, -} - -#[pymethods] -impl InPlaceOperations { - fn __repr__(&self) -> String { - format!("IPO({:?})", self.value) - } - - fn __iadd__(&mut self, other: u32) { - self.value += other; - } - - fn __isub__(&mut self, other: u32) { - self.value -= other; - } - - fn __imul__(&mut self, other: u32) { - self.value *= other; - } - - fn __ilshift__(&mut self, other: u32) { - self.value <<= other; - } - - fn __irshift__(&mut self, other: u32) { - self.value >>= other; - } - - fn __iand__(&mut self, other: u32) { - self.value &= other; - } - - fn __ixor__(&mut self, other: u32) { - self.value ^= other; - } - - fn __ior__(&mut self, other: u32) { - self.value |= other; - } - - fn __ipow__(&mut self, other: u32, _modulo: Option) { - self.value = self.value.pow(other); - } -} - -#[test] -fn inplace_operations() { - Python::with_gil(|py| { - let init = |value, code| { - let c = Py::new(py, InPlaceOperations { value }).unwrap(); - py_run!(py, c, code); - }; - - init(0, "d = c; c += 1; assert repr(c) == repr(d) == 'IPO(1)'"); - init(10, "d = c; c -= 1; assert repr(c) == repr(d) == 'IPO(9)'"); - init(3, "d = c; c *= 3; assert repr(c) == repr(d) == 'IPO(9)'"); - init(3, "d = c; c <<= 2; assert repr(c) == repr(d) == 'IPO(12)'"); - init(12, "d = c; c >>= 2; assert repr(c) == repr(d) == 'IPO(3)'"); - init(12, "d = c; c &= 10; assert repr(c) == repr(d) == 'IPO(8)'"); - init(12, "d = c; c |= 3; assert repr(c) == repr(d) == 'IPO(15)'"); - init(12, "d = c; c ^= 5; assert repr(c) == repr(d) == 'IPO(9)'"); - init(3, "d = c; c **= 4; assert repr(c) == repr(d) == 'IPO(81)'"); - init( - 3, - "d = c; c.__ipow__(4); assert repr(c) == repr(d) == 'IPO(81)'", - ); - }); -} - -#[pyclass] -struct BinaryArithmetic {} - -#[pymethods] -impl BinaryArithmetic { - fn __repr__(&self) -> &'static str { - "BA" - } - - fn __add__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA + {:?}", rhs) - } - - fn __sub__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA - {:?}", rhs) - } - - fn __mul__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA * {:?}", rhs) - } - - fn __matmul__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA @ {:?}", rhs) - } - - fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA / {:?}", rhs) - } - - fn __floordiv__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA // {:?}", rhs) - } - - fn __mod__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA % {:?}", rhs) - } - - fn __divmod__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("divmod(BA, {:?})", rhs) - } - - fn __lshift__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA << {:?}", rhs) - } - - fn __rshift__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA >> {:?}", rhs) - } - - fn __and__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA & {:?}", rhs) - } - - fn __xor__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA ^ {:?}", rhs) - } - - fn __or__(&self, rhs: &Bound<'_, PyAny>) -> String { - format!("BA | {:?}", rhs) - } - - fn __pow__(&self, rhs: &Bound<'_, PyAny>, mod_: Option) -> String { - format!("BA ** {:?} (mod: {:?})", rhs, mod_) - } -} - -#[test] -fn binary_arithmetic() { - Python::with_gil(|py| { - let c = Py::new(py, BinaryArithmetic {}).unwrap(); - py_run!(py, c, "assert c + c == 'BA + BA'"); - py_run!(py, c, "assert c.__add__(c) == 'BA + BA'"); - py_run!(py, c, "assert c + 1 == 'BA + 1'"); - py_run!(py, c, "assert c - 1 == 'BA - 1'"); - py_run!(py, c, "assert c * 1 == 'BA * 1'"); - py_run!(py, c, "assert c @ 1 == 'BA @ 1'"); - py_run!(py, c, "assert c / 1 == 'BA / 1'"); - py_run!(py, c, "assert c // 1 == 'BA // 1'"); - py_run!(py, c, "assert c % 1 == 'BA % 1'"); - py_run!(py, c, "assert divmod(c, 1) == 'divmod(BA, 1)'"); - py_run!(py, c, "assert c << 1 == 'BA << 1'"); - py_run!(py, c, "assert c >> 1 == 'BA >> 1'"); - py_run!(py, c, "assert c & 1 == 'BA & 1'"); - py_run!(py, c, "assert c ^ 1 == 'BA ^ 1'"); - py_run!(py, c, "assert c | 1 == 'BA | 1'"); - py_run!(py, c, "assert c ** 1 == 'BA ** 1 (mod: None)'"); - - // Class with __add__ only should not allow the reverse op; - // this is consistent with Python classes. - - py_expect_exception!(py, c, "1 + c", PyTypeError); - py_expect_exception!(py, c, "1 - c", PyTypeError); - py_expect_exception!(py, c, "1 * c", PyTypeError); - py_expect_exception!(py, c, "1 @ c", PyTypeError); - py_expect_exception!(py, c, "1 / c", PyTypeError); - py_expect_exception!(py, c, "1 // c", PyTypeError); - py_expect_exception!(py, c, "1 % c", PyTypeError); - py_expect_exception!(py, c, "divmod(1, c)", PyTypeError); - py_expect_exception!(py, c, "1 << c", PyTypeError); - py_expect_exception!(py, c, "1 >> c", PyTypeError); - py_expect_exception!(py, c, "1 & c", PyTypeError); - py_expect_exception!(py, c, "1 ^ c", PyTypeError); - py_expect_exception!(py, c, "1 | c", PyTypeError); - py_expect_exception!(py, c, "1 ** c", PyTypeError); - - py_run!(py, c, "assert pow(c, 1, 100) == 'BA ** 1 (mod: Some(100))'"); - - let c: Bound<'_, PyAny> = c.extract(py).unwrap(); - assert_py_eq!(c.add(&c).unwrap(), "BA + BA"); - assert_py_eq!(c.sub(&c).unwrap(), "BA - BA"); - assert_py_eq!(c.mul(&c).unwrap(), "BA * BA"); - assert_py_eq!(c.matmul(&c).unwrap(), "BA @ BA"); - assert_py_eq!(c.div(&c).unwrap(), "BA / BA"); - assert_py_eq!(c.floor_div(&c).unwrap(), "BA // BA"); - assert_py_eq!(c.rem(&c).unwrap(), "BA % BA"); - assert_py_eq!(c.divmod(&c).unwrap(), "divmod(BA, BA)"); - assert_py_eq!(c.lshift(&c).unwrap(), "BA << BA"); - assert_py_eq!(c.rshift(&c).unwrap(), "BA >> BA"); - assert_py_eq!(c.bitand(&c).unwrap(), "BA & BA"); - assert_py_eq!(c.bitor(&c).unwrap(), "BA | BA"); - assert_py_eq!(c.bitxor(&c).unwrap(), "BA ^ BA"); - assert_py_eq!(c.pow(&c, py.None()).unwrap(), "BA ** BA (mod: None)"); - }); -} - -#[pyclass] -struct RhsArithmetic {} - -#[pymethods] -impl RhsArithmetic { - fn __radd__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} + RA", other) - } - - fn __rsub__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} - RA", other) - } - - fn __rmul__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} * RA", other) - } - - fn __rlshift__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} << RA", other) - } - - fn __rrshift__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} >> RA", other) - } - - fn __rand__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} & RA", other) - } - - fn __rxor__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} ^ RA", other) - } - - fn __ror__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} | RA", other) - } - - fn __rpow__(&self, other: &Bound<'_, PyAny>, _mod: Option<&Bound<'_, PyAny>>) -> String { - format!("{:?} ** RA", other) - } -} - -#[test] -fn rhs_arithmetic() { - Python::with_gil(|py| { - let c = Py::new(py, RhsArithmetic {}).unwrap(); - py_run!(py, c, "assert c.__radd__(1) == '1 + RA'"); - py_run!(py, c, "assert 1 + c == '1 + RA'"); - py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'"); - py_run!(py, c, "assert 1 - c == '1 - RA'"); - py_run!(py, c, "assert c.__rmul__(1) == '1 * RA'"); - py_run!(py, c, "assert 1 * c == '1 * RA'"); - py_run!(py, c, "assert c.__rlshift__(1) == '1 << RA'"); - py_run!(py, c, "assert 1 << c == '1 << RA'"); - py_run!(py, c, "assert c.__rrshift__(1) == '1 >> RA'"); - py_run!(py, c, "assert 1 >> c == '1 >> RA'"); - py_run!(py, c, "assert c.__rand__(1) == '1 & RA'"); - py_run!(py, c, "assert 1 & c == '1 & RA'"); - py_run!(py, c, "assert c.__rxor__(1) == '1 ^ RA'"); - py_run!(py, c, "assert 1 ^ c == '1 ^ RA'"); - py_run!(py, c, "assert c.__ror__(1) == '1 | RA'"); - py_run!(py, c, "assert 1 | c == '1 | RA'"); - py_run!(py, c, "assert c.__rpow__(1) == '1 ** RA'"); - py_run!(py, c, "assert 1 ** c == '1 ** RA'"); - }); -} - -#[pyclass] -struct LhsAndRhs {} - -impl std::fmt::Debug for LhsAndRhs { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "LR") - } -} - -#[pymethods] -impl LhsAndRhs { - // fn __repr__(&self) -> &'static str { - // "BA" - // } - - fn __add__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} + {:?}", lhs, rhs) - } - - fn __sub__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} - {:?}", lhs, rhs) - } - - fn __mul__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} * {:?}", lhs, rhs) - } - - fn __lshift__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} << {:?}", lhs, rhs) - } - - fn __rshift__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} >> {:?}", lhs, rhs) - } - - fn __and__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} & {:?}", lhs, rhs) - } - - fn __xor__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} ^ {:?}", lhs, rhs) - } - - fn __or__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} | {:?}", lhs, rhs) - } - - fn __pow__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>, _mod: Option) -> String { - format!("{:?} ** {:?}", lhs, rhs) - } - - fn __matmul__(lhs: PyRef<'_, Self>, rhs: &Bound<'_, PyAny>) -> String { - format!("{:?} @ {:?}", lhs, rhs) - } - - fn __radd__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} + RA", other) - } - - fn __rsub__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} - RA", other) - } - - fn __rmul__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} * RA", other) - } - - fn __rlshift__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} << RA", other) - } - - fn __rrshift__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} >> RA", other) - } - - fn __rand__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} & RA", other) - } - - fn __rxor__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} ^ RA", other) - } - - fn __ror__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} | RA", other) - } - - fn __rpow__(&self, other: &Bound<'_, PyAny>, _mod: Option<&Bound<'_, PyAny>>) -> String { - format!("{:?} ** RA", other) - } - - fn __rmatmul__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} @ RA", other) - } - - fn __rtruediv__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} / RA", other) - } - - fn __rfloordiv__(&self, other: &Bound<'_, PyAny>) -> String { - format!("{:?} // RA", other) - } -} - -#[test] -fn lhs_fellback_to_rhs() { - Python::with_gil(|py| { - let c = Py::new(py, LhsAndRhs {}).unwrap(); - // If the light hand value is `LhsAndRhs`, LHS is used. - py_run!(py, c, "assert c + 1 == 'LR + 1'"); - py_run!(py, c, "assert c - 1 == 'LR - 1'"); - py_run!(py, c, "assert c * 1 == 'LR * 1'"); - py_run!(py, c, "assert c << 1 == 'LR << 1'"); - py_run!(py, c, "assert c >> 1 == 'LR >> 1'"); - py_run!(py, c, "assert c & 1 == 'LR & 1'"); - py_run!(py, c, "assert c ^ 1 == 'LR ^ 1'"); - py_run!(py, c, "assert c | 1 == 'LR | 1'"); - py_run!(py, c, "assert c ** 1 == 'LR ** 1'"); - py_run!(py, c, "assert c @ 1 == 'LR @ 1'"); - // Fellback to RHS because of type mismatching - py_run!(py, c, "assert 1 + c == '1 + RA'"); - py_run!(py, c, "assert 1 - c == '1 - RA'"); - py_run!(py, c, "assert 1 * c == '1 * RA'"); - py_run!(py, c, "assert 1 << c == '1 << RA'"); - py_run!(py, c, "assert 1 >> c == '1 >> RA'"); - py_run!(py, c, "assert 1 & c == '1 & RA'"); - py_run!(py, c, "assert 1 ^ c == '1 ^ RA'"); - py_run!(py, c, "assert 1 | c == '1 | RA'"); - py_run!(py, c, "assert 1 ** c == '1 ** RA'"); - py_run!(py, c, "assert 1 @ c == '1 @ RA'"); - }); -} - -#[pyclass] -struct RichComparisons {} - -#[pymethods] -impl RichComparisons { - fn __repr__(&self) -> &'static str { - "RC" - } - - fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> String { - match op { - CompareOp::Lt => format!("{} < {:?}", self.__repr__(), other), - CompareOp::Le => format!("{} <= {:?}", self.__repr__(), other), - CompareOp::Eq => format!("{} == {:?}", self.__repr__(), other), - CompareOp::Ne => format!("{} != {:?}", self.__repr__(), other), - CompareOp::Gt => format!("{} > {:?}", self.__repr__(), other), - CompareOp::Ge => format!("{} >= {:?}", self.__repr__(), other), - } - } -} - -#[pyclass] -struct RichComparisons2 {} - -#[pymethods] -impl RichComparisons2 { - fn __repr__(&self) -> &'static str { - "RC2" - } - - fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult { - match op { - CompareOp::Eq => true - .into_pyobject(other.py()) - .map_err(Into::into) - .map(BoundObject::into_any) - .map(BoundObject::unbind), - CompareOp::Ne => false - .into_pyobject(other.py()) - .map_err(Into::into) - .map(BoundObject::into_any) - .map(BoundObject::unbind), - _ => Ok(other.py().NotImplemented()), - } - } -} - -#[test] -fn rich_comparisons() { - Python::with_gil(|py| { - let c = Py::new(py, RichComparisons {}).unwrap(); - py_run!(py, c, "assert (c < c) == 'RC < RC'"); - py_run!(py, c, "assert (c < 1) == 'RC < 1'"); - py_run!(py, c, "assert (1 < c) == 'RC > 1'"); - py_run!(py, c, "assert (c <= c) == 'RC <= RC'"); - py_run!(py, c, "assert (c <= 1) == 'RC <= 1'"); - py_run!(py, c, "assert (1 <= c) == 'RC >= 1'"); - py_run!(py, c, "assert (c == c) == 'RC == RC'"); - py_run!(py, c, "assert (c == 1) == 'RC == 1'"); - py_run!(py, c, "assert (1 == c) == 'RC == 1'"); - py_run!(py, c, "assert (c != c) == 'RC != RC'"); - py_run!(py, c, "assert (c != 1) == 'RC != 1'"); - py_run!(py, c, "assert (1 != c) == 'RC != 1'"); - py_run!(py, c, "assert (c > c) == 'RC > RC'"); - py_run!(py, c, "assert (c > 1) == 'RC > 1'"); - py_run!(py, c, "assert (1 > c) == 'RC < 1'"); - py_run!(py, c, "assert (c >= c) == 'RC >= RC'"); - py_run!(py, c, "assert (c >= 1) == 'RC >= 1'"); - py_run!(py, c, "assert (1 >= c) == 'RC <= 1'"); - }); -} - -#[test] -fn rich_comparisons_python_3_type_error() { - Python::with_gil(|py| { - let c2 = Py::new(py, RichComparisons2 {}).unwrap(); - py_expect_exception!(py, c2, "c2 < c2", PyTypeError); - py_expect_exception!(py, c2, "c2 < 1", PyTypeError); - py_expect_exception!(py, c2, "1 < c2", PyTypeError); - py_expect_exception!(py, c2, "c2 <= c2", PyTypeError); - py_expect_exception!(py, c2, "c2 <= 1", PyTypeError); - py_expect_exception!(py, c2, "1 <= c2", PyTypeError); - py_run!(py, c2, "assert (c2 == c2) == True"); - py_run!(py, c2, "assert (c2 == 1) == True"); - py_run!(py, c2, "assert (1 == c2) == True"); - py_run!(py, c2, "assert (c2 != c2) == False"); - py_run!(py, c2, "assert (c2 != 1) == False"); - py_run!(py, c2, "assert (1 != c2) == False"); - py_expect_exception!(py, c2, "c2 > c2", PyTypeError); - py_expect_exception!(py, c2, "c2 > 1", PyTypeError); - py_expect_exception!(py, c2, "1 > c2", PyTypeError); - py_expect_exception!(py, c2, "c2 >= c2", PyTypeError); - py_expect_exception!(py, c2, "c2 >= 1", PyTypeError); - py_expect_exception!(py, c2, "1 >= c2", PyTypeError); - }); -} - -// Checks that binary operations for which the arguments don't match the -// required type, return NotImplemented. -mod return_not_implemented { - use super::*; - - #[pyclass] - struct RichComparisonToSelf {} - - #[pymethods] - impl RichComparisonToSelf { - fn __repr__(&self) -> &'static str { - "RC_Self" - } - - fn __richcmp__(&self, other: PyRef<'_, Self>, _op: CompareOp) -> PyObject { - other.py().None() - } - - fn __add__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __sub__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __mul__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __matmul__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __truediv__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __floordiv__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __mod__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __pow__(slf: PyRef<'_, Self>, _other: u8, _modulo: Option) -> PyRef<'_, Self> { - slf - } - fn __lshift__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __rshift__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __divmod__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __and__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __or__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - fn __xor__<'p>(slf: PyRef<'p, Self>, _other: PyRef<'p, Self>) -> PyRef<'p, Self> { - slf - } - - // Inplace assignments - fn __iadd__(&mut self, _other: PyRef<'_, Self>) {} - fn __isub__(&mut self, _other: PyRef<'_, Self>) {} - fn __imul__(&mut self, _other: PyRef<'_, Self>) {} - fn __imatmul__(&mut self, _other: PyRef<'_, Self>) {} - fn __itruediv__(&mut self, _other: PyRef<'_, Self>) {} - fn __ifloordiv__(&mut self, _other: PyRef<'_, Self>) {} - fn __imod__(&mut self, _other: PyRef<'_, Self>) {} - fn __ilshift__(&mut self, _other: PyRef<'_, Self>) {} - fn __irshift__(&mut self, _other: PyRef<'_, Self>) {} - fn __iand__(&mut self, _other: PyRef<'_, Self>) {} - fn __ior__(&mut self, _other: PyRef<'_, Self>) {} - fn __ixor__(&mut self, _other: PyRef<'_, Self>) {} - fn __ipow__(&mut self, _other: PyRef<'_, Self>, _modulo: Option) {} - } - - fn _test_binary_dunder(dunder: &str) { - Python::with_gil(|py| { - let c2 = Py::new(py, RichComparisonToSelf {}).unwrap(); - py_run!( - py, - c2, - &format!( - "class Other: pass\nassert c2.__{}__(Other()) is NotImplemented", - dunder - ) - ); - }); - } - - fn _test_binary_operator(operator: &str, dunder: &str) { - _test_binary_dunder(dunder); - - Python::with_gil(|py| { - let c2 = Py::new(py, RichComparisonToSelf {}).unwrap(); - py_expect_exception!( - py, - c2, - format!("class Other: pass\nc2 {} Other()", operator), - PyTypeError - ); - }); - } - - fn _test_inplace_binary_operator(operator: &str, dunder: &str) { - _test_binary_operator(operator, dunder); - } - - #[test] - fn equality() { - _test_binary_dunder("eq"); - _test_binary_dunder("ne"); - } - - #[test] - fn ordering() { - _test_binary_operator("<", "lt"); - _test_binary_operator("<=", "le"); - _test_binary_operator(">", "gt"); - _test_binary_operator(">=", "ge"); - } - - #[test] - fn bitwise() { - _test_binary_operator("&", "and"); - _test_binary_operator("|", "or"); - _test_binary_operator("^", "xor"); - _test_binary_operator("<<", "lshift"); - _test_binary_operator(">>", "rshift"); - } - - #[test] - fn arith() { - _test_binary_operator("+", "add"); - _test_binary_operator("-", "sub"); - _test_binary_operator("*", "mul"); - _test_binary_operator("@", "matmul"); - _test_binary_operator("/", "truediv"); - _test_binary_operator("//", "floordiv"); - _test_binary_operator("%", "mod"); - _test_binary_operator("**", "pow"); - } - - #[test] - fn reverse_arith() { - _test_binary_dunder("radd"); - _test_binary_dunder("rsub"); - _test_binary_dunder("rmul"); - _test_binary_dunder("rmatmul"); - _test_binary_dunder("rtruediv"); - _test_binary_dunder("rfloordiv"); - _test_binary_dunder("rmod"); - _test_binary_dunder("rdivmod"); - _test_binary_dunder("rpow"); - } - - #[test] - fn inplace_bitwise() { - _test_inplace_binary_operator("&=", "iand"); - _test_inplace_binary_operator("|=", "ior"); - _test_inplace_binary_operator("^=", "ixor"); - _test_inplace_binary_operator("<<=", "ilshift"); - _test_inplace_binary_operator(">>=", "irshift"); - } - - #[test] - fn inplace_arith() { - _test_inplace_binary_operator("+=", "iadd"); - _test_inplace_binary_operator("-=", "isub"); - _test_inplace_binary_operator("*=", "imul"); - _test_inplace_binary_operator("@=", "imatmul"); - _test_inplace_binary_operator("/=", "itruediv"); - _test_inplace_binary_operator("//=", "ifloordiv"); - _test_inplace_binary_operator("%=", "imod"); - _test_inplace_binary_operator("**=", "ipow"); - } -} diff --git a/vendor/pyo3/tests/test_buffer.rs b/vendor/pyo3/tests/test_buffer.rs deleted file mode 100644 index 60db80b81c..0000000000 --- a/vendor/pyo3/tests/test_buffer.rs +++ /dev/null @@ -1,140 +0,0 @@ -#![cfg(feature = "macros")] -#![cfg(any(not(Py_LIMITED_API), Py_3_11))] - -use pyo3::{buffer::PyBuffer, exceptions::PyBufferError, ffi, prelude::*}; -use std::{ - os::raw::{c_int, c_void}, - ptr, -}; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -enum TestGetBufferError { - NullShape, - NullStrides, - IncorrectItemSize, - IncorrectFormat, - IncorrectAlignment, -} - -#[pyclass] -struct TestBufferErrors { - buf: Vec, - error: Option, -} - -#[pymethods] -impl TestBufferErrors { - unsafe fn __getbuffer__( - slf: PyRefMut<'_, Self>, - view: *mut ffi::Py_buffer, - flags: c_int, - ) -> PyResult<()> { - if view.is_null() { - return Err(PyBufferError::new_err("View is null")); - } - - if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE { - return Err(PyBufferError::new_err("Object is not writable")); - } - - let bytes = &slf.buf; - - (*view).buf = bytes.as_ptr() as *mut c_void; - (*view).len = bytes.len() as isize; - (*view).readonly = 1; - (*view).itemsize = std::mem::size_of::() as isize; - - let msg = ffi::c_str!("I"); - (*view).format = msg.as_ptr() as *mut _; - - (*view).ndim = 1; - (*view).shape = &mut (*view).len; - - (*view).strides = &mut (*view).itemsize; - - (*view).suboffsets = ptr::null_mut(); - (*view).internal = ptr::null_mut(); - - if let Some(err) = &slf.error { - use TestGetBufferError::*; - match err { - NullShape => { - (*view).shape = std::ptr::null_mut(); - } - NullStrides => { - (*view).strides = std::ptr::null_mut(); - } - IncorrectItemSize => { - (*view).itemsize += 1; - } - IncorrectFormat => { - (*view).format = ffi::c_str!("B").as_ptr() as _; - } - IncorrectAlignment => (*view).buf = (*view).buf.add(1), - } - } - - (*view).obj = slf.into_ptr(); - - Ok(()) - } -} - -#[test] -fn test_get_buffer_errors() { - Python::with_gil(|py| { - let instance = Py::new( - py, - TestBufferErrors { - buf: vec![0, 1, 2, 3], - error: None, - }, - ) - .unwrap(); - - assert!(PyBuffer::::get(instance.bind(py)).is_ok()); - - instance.borrow_mut(py).error = Some(TestGetBufferError::NullShape); - assert_eq!( - PyBuffer::::get(instance.bind(py)) - .unwrap_err() - .to_string(), - "BufferError: shape is null" - ); - - instance.borrow_mut(py).error = Some(TestGetBufferError::NullStrides); - assert_eq!( - PyBuffer::::get(instance.bind(py)) - .unwrap_err() - .to_string(), - "BufferError: strides is null" - ); - - instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectItemSize); - assert_eq!( - PyBuffer::::get(instance.bind(py)) - .unwrap_err() - .to_string(), - "BufferError: buffer contents are not compatible with u32" - ); - - instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectFormat); - assert_eq!( - PyBuffer::::get(instance.bind(py)) - .unwrap_err() - .to_string(), - "BufferError: buffer contents are not compatible with u32" - ); - - instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectAlignment); - assert_eq!( - PyBuffer::::get(instance.bind(py)) - .unwrap_err() - .to_string(), - "BufferError: buffer contents are insufficiently aligned for u32" - ); - }); -} diff --git a/vendor/pyo3/tests/test_buffer_protocol.rs b/vendor/pyo3/tests/test_buffer_protocol.rs deleted file mode 100644 index 1f15e34b38..0000000000 --- a/vendor/pyo3/tests/test_buffer_protocol.rs +++ /dev/null @@ -1,190 +0,0 @@ -#![cfg(feature = "macros")] -#![cfg(any(not(Py_LIMITED_API), Py_3_11))] - -use pyo3::buffer::PyBuffer; -use pyo3::exceptions::PyBufferError; -use pyo3::ffi; -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; -use std::ffi::CString; -use std::os::raw::{c_int, c_void}; -use std::ptr; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct TestBufferClass { - vec: Vec, - drop_called: Arc, -} - -#[pymethods] -impl TestBufferClass { - unsafe fn __getbuffer__( - slf: Bound<'_, Self>, - view: *mut ffi::Py_buffer, - flags: c_int, - ) -> PyResult<()> { - fill_view_from_readonly_data(view, flags, &slf.borrow().vec, slf.into_any()) - } - - unsafe fn __releasebuffer__(&self, view: *mut ffi::Py_buffer) { - // Release memory held by the format string - drop(CString::from_raw((*view).format)); - } -} - -impl Drop for TestBufferClass { - fn drop(&mut self) { - print!("dropped"); - self.drop_called.store(true, Ordering::Relaxed); - } -} - -#[test] -fn test_buffer() { - let drop_called = Arc::new(AtomicBool::new(false)); - - Python::with_gil(|py| { - let instance = Py::new( - py, - TestBufferClass { - vec: vec![b' ', b'2', b'3'], - drop_called: drop_called.clone(), - }, - ) - .unwrap(); - let env = [("ob", instance)].into_py_dict(py).unwrap(); - py_assert!(py, *env, "bytes(ob) == b' 23'"); - }); - - assert!(drop_called.load(Ordering::Relaxed)); -} - -#[test] -fn test_buffer_referenced() { - let drop_called = Arc::new(AtomicBool::new(false)); - - let buf = { - let input = vec![b' ', b'2', b'3']; - Python::with_gil(|py| { - let instance = TestBufferClass { - vec: input.clone(), - drop_called: drop_called.clone(), - } - .into_pyobject(py) - .unwrap(); - - let buf = PyBuffer::::get(&instance).unwrap(); - assert_eq!(buf.to_vec(py).unwrap(), input); - drop(instance); - buf - }) - }; - - assert!(!drop_called.load(Ordering::Relaxed)); - - Python::with_gil(|_| { - drop(buf); - }); - - assert!(drop_called.load(Ordering::Relaxed)); -} - -#[test] -#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] // sys.unraisablehook not available until Python 3.8 -fn test_releasebuffer_unraisable_error() { - use common::UnraisableCapture; - use pyo3::exceptions::PyValueError; - - #[pyclass] - struct ReleaseBufferError {} - - #[pymethods] - impl ReleaseBufferError { - unsafe fn __getbuffer__( - slf: Bound<'_, Self>, - view: *mut ffi::Py_buffer, - flags: c_int, - ) -> PyResult<()> { - static BUF_BYTES: &[u8] = b"hello world"; - fill_view_from_readonly_data(view, flags, BUF_BYTES, slf.into_any()) - } - - unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) -> PyResult<()> { - Err(PyValueError::new_err("oh dear")) - } - } - - Python::with_gil(|py| { - let capture = UnraisableCapture::install(py); - - let instance = Py::new(py, ReleaseBufferError {}).unwrap(); - let env = [("ob", instance.clone_ref(py))].into_py_dict(py).unwrap(); - - assert!(capture.borrow(py).capture.is_none()); - - py_assert!(py, *env, "bytes(ob) == b'hello world'"); - - let (err, object) = capture.borrow_mut(py).capture.take().unwrap(); - assert_eq!(err.to_string(), "ValueError: oh dear"); - assert!(object.is(&instance)); - - capture.borrow_mut(py).uninstall(py); - }); -} - -/// # Safety -/// -/// `view` must be a valid pointer to ffi::Py_buffer, or null -/// `data` must outlive the Python lifetime of `owner` (i.e. data must be owned by owner, or data -/// must be static data) -unsafe fn fill_view_from_readonly_data( - view: *mut ffi::Py_buffer, - flags: c_int, - data: &[u8], - owner: Bound<'_, PyAny>, -) -> PyResult<()> { - if view.is_null() { - return Err(PyBufferError::new_err("View is null")); - } - - if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE { - return Err(PyBufferError::new_err("Object is not writable")); - } - - (*view).obj = owner.into_ptr(); - - (*view).buf = data.as_ptr() as *mut c_void; - (*view).len = data.len() as isize; - (*view).readonly = 1; - (*view).itemsize = 1; - - (*view).format = if (flags & ffi::PyBUF_FORMAT) == ffi::PyBUF_FORMAT { - let msg = CString::new("B").unwrap(); - msg.into_raw() - } else { - ptr::null_mut() - }; - - (*view).ndim = 1; - (*view).shape = if (flags & ffi::PyBUF_ND) == ffi::PyBUF_ND { - &mut (*view).len - } else { - ptr::null_mut() - }; - - (*view).strides = if (flags & ffi::PyBUF_STRIDES) == ffi::PyBUF_STRIDES { - &mut (*view).itemsize - } else { - ptr::null_mut() - }; - - (*view).suboffsets = ptr::null_mut(); - (*view).internal = ptr::null_mut(); - - Ok(()) -} diff --git a/vendor/pyo3/tests/test_bytes.rs b/vendor/pyo3/tests/test_bytes.rs deleted file mode 100644 index 4e58cdc08e..0000000000 --- a/vendor/pyo3/tests/test_bytes.rs +++ /dev/null @@ -1,53 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::types::PyBytes; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyfunction] -fn bytes_pybytes_conversion(bytes: &[u8]) -> &[u8] { - bytes -} - -#[test] -fn test_pybytes_bytes_conversion() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(bytes_pybytes_conversion)(py).unwrap(); - py_assert!(py, f, "f(b'Hello World') == b'Hello World'"); - }); -} - -#[pyfunction] -fn bytes_vec_conversion(py: Python<'_>, bytes: Vec) -> Bound<'_, PyBytes> { - PyBytes::new(py, bytes.as_slice()) -} - -#[test] -fn test_pybytes_vec_conversion() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap(); - py_assert!(py, f, "f(b'Hello World') == b'Hello World'"); - }); -} - -#[test] -fn test_bytearray_vec_conversion() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap(); - py_assert!(py, f, "f(bytearray(b'Hello World')) == b'Hello World'"); - }); -} - -#[test] -fn test_py_as_bytes() { - let pyobj: pyo3::Py = - Python::with_gil(|py| pyo3::types::PyBytes::new(py, b"abc").unbind()); - - let data = Python::with_gil(|py| pyobj.as_bytes(py)); - - assert_eq!(data, b"abc"); - - Python::with_gil(move |_py| drop(pyobj)); -} diff --git a/vendor/pyo3/tests/test_class_attributes.rs b/vendor/pyo3/tests/test_class_attributes.rs deleted file mode 100644 index 3f8eb74df5..0000000000 --- a/vendor/pyo3/tests/test_class_attributes.rs +++ /dev/null @@ -1,288 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct Foo { - #[pyo3(get)] - x: i32, -} - -#[pyclass] -struct Bar { - #[pyo3(get)] - x: i32, -} - -#[pymethods] -impl Foo { - #[classattr] - const MY_CONST: &'static str = "foobar"; - - #[classattr] - #[pyo3(name = "RENAMED_CONST")] - const MY_CONST_2: &'static str = "foobar_2"; - - #[classattr] - fn a() -> i32 { - 5 - } - - #[classattr] - #[pyo3(name = "B")] - fn b() -> String { - "bar".to_string() - } - - #[classattr] - fn bar() -> Bar { - Bar { x: 2 } - } - - #[classattr] - fn a_foo() -> Foo { - Foo { x: 1 } - } - - #[classattr] - fn a_foo_with_py(py: Python<'_>) -> Py { - Py::new(py, Foo { x: 1 }).unwrap() - } -} - -#[test] -fn class_attributes() { - Python::with_gil(|py| { - let foo_obj = py.get_type::(); - py_assert!(py, foo_obj, "foo_obj.MY_CONST == 'foobar'"); - py_assert!(py, foo_obj, "foo_obj.RENAMED_CONST == 'foobar_2'"); - py_assert!(py, foo_obj, "foo_obj.a == 5"); - py_assert!(py, foo_obj, "foo_obj.B == 'bar'"); - py_assert!(py, foo_obj, "foo_obj.a_foo.x == 1"); - py_assert!(py, foo_obj, "foo_obj.a_foo_with_py.x == 1"); - }); -} - -// Ignored because heap types are not immutable: -// https://github.com/python/cpython/blob/master/Objects/typeobject.c#L3399-L3409 -#[test] -#[ignore] -fn class_attributes_are_immutable() { - Python::with_gil(|py| { - let foo_obj = py.get_type::(); - py_expect_exception!(py, foo_obj, "foo_obj.a = 6", PyTypeError); - }); -} - -#[pymethods] -impl Bar { - #[classattr] - fn a_foo() -> Foo { - Foo { x: 3 } - } -} - -#[test] -fn recursive_class_attributes() { - Python::with_gil(|py| { - let foo_obj = py.get_type::(); - let bar_obj = py.get_type::(); - py_assert!(py, foo_obj, "foo_obj.a_foo.x == 1"); - py_assert!(py, foo_obj, "foo_obj.bar.x == 2"); - py_assert!(py, bar_obj, "bar_obj.a_foo.x == 3"); - }); -} - -#[test] -fn test_fallible_class_attribute() { - use pyo3::{exceptions::PyValueError, types::PyString}; - - struct CaptureStdErr<'py> { - oldstderr: Bound<'py, PyAny>, - string_io: Bound<'py, PyAny>, - } - - impl<'py> CaptureStdErr<'py> { - fn new(py: Python<'py>) -> PyResult { - let sys = py.import("sys")?; - let oldstderr = sys.getattr("stderr")?; - let string_io = py.import("io")?.getattr("StringIO")?.call0()?; - sys.setattr("stderr", &string_io)?; - Ok(Self { - oldstderr, - string_io, - }) - } - - fn reset(self) -> PyResult { - let py = self.string_io.py(); - let payload = self - .string_io - .getattr("getvalue")? - .call0()? - .downcast::()? - .to_cow()? - .into_owned(); - let sys = py.import("sys")?; - sys.setattr("stderr", self.oldstderr)?; - Ok(payload) - } - } - - #[pyclass] - struct BrokenClass; - - #[pymethods] - impl BrokenClass { - #[classattr] - fn fails_to_init() -> PyResult { - Err(PyValueError::new_err("failed to create class attribute")) - } - } - - Python::with_gil(|py| { - let stderr = CaptureStdErr::new(py).unwrap(); - assert!(std::panic::catch_unwind(|| py.get_type::()).is_err()); - assert_eq!( - stderr.reset().unwrap().trim(), - "\ -ValueError: failed to create class attribute - -The above exception was the direct cause of the following exception: - -RuntimeError: An error occurred while initializing `BrokenClass.fails_to_init` - -The above exception was the direct cause of the following exception: - -RuntimeError: An error occurred while initializing class BrokenClass" - ) - }); -} - -#[pyclass(get_all, set_all, rename_all = "camelCase")] -struct StructWithRenamedFields { - first_field: bool, - second_field: u8, - #[pyo3(name = "third_field")] - fourth_field: bool, -} - -#[pymethods] -impl StructWithRenamedFields { - #[new] - fn new() -> Self { - Self { - first_field: true, - second_field: 5, - fourth_field: false, - } - } -} - -#[test] -fn test_renaming_all_struct_fields() { - use pyo3::types::PyBool; - - Python::with_gil(|py| { - let struct_class = py.get_type::(); - let struct_obj = struct_class.call0().unwrap(); - assert!(struct_obj - .setattr("firstField", PyBool::new(py, false)) - .is_ok()); - py_assert!(py, struct_obj, "struct_obj.firstField == False"); - py_assert!(py, struct_obj, "struct_obj.secondField == 5"); - assert!(struct_obj - .setattr("third_field", PyBool::new(py, true)) - .is_ok()); - py_assert!(py, struct_obj, "struct_obj.third_field == True"); - }); -} - -macro_rules! test_case { - ($struct_name: ident, $rule: literal, $field_name: ident, $renamed_field_name: literal, $test_name: ident) => { - #[pyclass(get_all, set_all, rename_all = $rule)] - #[allow(non_snake_case)] - struct $struct_name { - $field_name: u8, - } - #[pymethods] - impl $struct_name { - #[new] - fn new() -> Self { - Self { $field_name: 0 } - } - } - #[test] - fn $test_name() { - //use pyo3::types::PyInt; - - Python::with_gil(|py| { - let struct_class = py.get_type::<$struct_name>(); - let struct_obj = struct_class.call0().unwrap(); - assert!(struct_obj.setattr($renamed_field_name, 2).is_ok()); - let attr = struct_obj.getattr($renamed_field_name).unwrap(); - assert_eq!(2, attr.extract::().unwrap()); - }); - } - }; -} - -test_case!( - LowercaseTest, - "lowercase", - fieldOne, - "fieldone", - test_rename_all_lowercase -); -test_case!( - CamelCaseTest, - "camelCase", - field_one, - "fieldOne", - test_rename_all_camel_case -); -test_case!( - KebabCaseTest, - "kebab-case", - field_one, - "field-one", - test_rename_all_kebab_case -); -test_case!( - PascalCaseTest, - "PascalCase", - field_one, - "FieldOne", - test_rename_all_pascal_case -); -test_case!( - ScreamingSnakeCaseTest, - "SCREAMING_SNAKE_CASE", - field_one, - "FIELD_ONE", - test_rename_all_screaming_snake_case -); -test_case!( - ScreamingKebabCaseTest, - "SCREAMING-KEBAB-CASE", - field_one, - "FIELD-ONE", - test_rename_all_screaming_kebab_case -); -test_case!( - SnakeCaseTest, - "snake_case", - fieldOne, - "field_one", - test_rename_all_snake_case -); -test_case!( - UppercaseTest, - "UPPERCASE", - fieldOne, - "FIELDONE", - test_rename_all_uppercase -); diff --git a/vendor/pyo3/tests/test_class_basics.rs b/vendor/pyo3/tests/test_class_basics.rs deleted file mode 100644 index 4a687a89ee..0000000000 --- a/vendor/pyo3/tests/test_class_basics.rs +++ /dev/null @@ -1,716 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::types::PyType; -use pyo3::{py_run, PyClass}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct EmptyClass {} - -#[test] -fn empty_class() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - // By default, don't allow creating instances from python. - assert!(typeobj.call((), None).is_err()); - - py_assert!(py, typeobj, "typeobj.__name__ == 'EmptyClass'"); - }); -} - -#[pyclass] -struct UnitClass; - -#[test] -fn unit_class() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - // By default, don't allow creating instances from python. - assert!(typeobj.call((), None).is_err()); - - py_assert!(py, typeobj, "typeobj.__name__ == 'UnitClass'"); - }); -} - -/// Line1 -///Line2 -/// Line3 -// this is not doc string -#[pyclass] -struct ClassWithDocs { - /// Property field - #[pyo3(get, set)] - value: i32, - - /// Read-only property field - #[pyo3(get)] - readonly: i32, - - /// Write-only property field - #[pyo3(set)] - #[allow(dead_code)] // Rust detects field is never read - writeonly: i32, -} - -#[test] -fn class_with_docstr() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_run!( - py, - typeobj, - "assert typeobj.__doc__ == 'Line1\\nLine2\\n Line3'" - ); - py_run!( - py, - typeobj, - "assert typeobj.value.__doc__ == 'Property field'" - ); - py_run!( - py, - typeobj, - "assert typeobj.readonly.__doc__ == 'Read-only property field'" - ); - py_run!( - py, - typeobj, - "assert typeobj.writeonly.__doc__ == 'Write-only property field'" - ); - }); -} - -#[pyclass(name = "CustomName")] -struct EmptyClass2 {} - -#[pymethods] -impl EmptyClass2 { - #[pyo3(name = "custom_fn")] - fn bar(&self) {} - - #[staticmethod] - #[pyo3(name = "custom_static")] - fn bar_static() {} - - #[getter] - #[pyo3(name = "custom_getter")] - fn foo(&self) -> i32 { - 5 - } -} - -#[test] -fn custom_names() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'"); - py_assert!(py, typeobj, "typeobj.custom_fn.__name__ == 'custom_fn'"); - py_assert!( - py, - typeobj, - "typeobj.custom_static.__name__ == 'custom_static'" - ); - py_assert!( - py, - typeobj, - "typeobj.custom_getter.__name__ == 'custom_getter'" - ); - py_assert!(py, typeobj, "not hasattr(typeobj, 'bar')"); - py_assert!(py, typeobj, "not hasattr(typeobj, 'bar_static')"); - py_assert!(py, typeobj, "not hasattr(typeobj, 'foo')"); - }); -} - -#[pyclass(name = "loop")] -struct ClassRustKeywords { - #[pyo3(name = "unsafe", get, set)] - unsafe_variable: usize, -} - -#[pymethods] -impl ClassRustKeywords { - #[pyo3(name = "struct")] - fn struct_method(&self) {} - - #[staticmethod] - #[pyo3(name = "type")] - fn type_method() {} -} - -#[test] -fn keyword_names() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_assert!(py, typeobj, "typeobj.__name__ == 'loop'"); - py_assert!(py, typeobj, "typeobj.struct.__name__ == 'struct'"); - py_assert!(py, typeobj, "typeobj.type.__name__ == 'type'"); - py_assert!(py, typeobj, "typeobj.unsafe.__name__ == 'unsafe'"); - py_assert!(py, typeobj, "not hasattr(typeobj, 'unsafe_variable')"); - py_assert!(py, typeobj, "not hasattr(typeobj, 'struct_method')"); - py_assert!(py, typeobj, "not hasattr(typeobj, 'type_method')"); - }); -} - -#[pyclass] -struct RawIdents { - #[pyo3(get, set)] - r#type: i64, -} - -#[pymethods] -impl RawIdents { - fn r#fn(&self) {} -} - -#[test] -fn test_raw_idents() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')"); - py_assert!(py, typeobj, "hasattr(typeobj, 'fn')"); - py_assert!(py, typeobj, "hasattr(typeobj, 'type')"); - }); -} - -#[pyclass] -struct EmptyClassInModule {} - -// Ignored because heap types do not show up as being in builtins, instead they -// raise AttributeError: -// https://github.com/python/cpython/blob/v3.11.1/Objects/typeobject.c#L541-L570 -#[test] -#[ignore] -fn empty_class_in_module() { - Python::with_gil(|py| { - let module = PyModule::new(py, "test_module.nested").unwrap(); - module.add_class::().unwrap(); - - let ty = module.getattr("EmptyClassInModule").unwrap(); - assert_eq!( - ty.getattr("__name__").unwrap().extract::().unwrap(), - "EmptyClassInModule" - ); - - let module: String = ty.getattr("__module__").unwrap().extract().unwrap(); - - // Rationale: The class can be added to many modules, but will only be initialized once. - // We currently have no way of determining a canonical module, so builtins is better - // than using whatever calls init first. - assert_eq!(module, "builtins"); - }); -} - -#[pyclass] -struct ClassWithObjectField { - // It used to be that PyObject was not supported with (get, set) - // - this test is just ensuring it compiles. - #[pyo3(get, set)] - value: PyObject, -} - -#[pymethods] -impl ClassWithObjectField { - #[new] - fn new(value: PyObject) -> ClassWithObjectField { - Self { value } - } -} - -#[test] -fn class_with_object_field() { - Python::with_gil(|py| { - let ty = py.get_type::(); - py_assert!(py, ty, "ty(5).value == 5"); - py_assert!(py, ty, "ty(None).value == None"); - }); -} - -#[pyclass(frozen, eq, hash)] -#[derive(PartialEq, Hash)] -struct ClassWithHash { - value: usize, -} - -#[test] -fn class_with_hash() { - Python::with_gil(|py| { - use pyo3::types::IntoPyDict; - let class = ClassWithHash { value: 42 }; - let hash = { - use std::hash::{Hash, Hasher}; - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - class.hash(&mut hasher); - hasher.finish() as isize - }; - - let env = [ - ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), - ] - .into_py_dict(py) - .unwrap(); - - py_assert!(py, *env, "hash(obj) == hsh"); - }); -} - -#[pyclass(unsendable, subclass)] -struct UnsendableBase { - value: std::rc::Rc, -} - -#[pymethods] -impl UnsendableBase { - #[new] - fn new(value: usize) -> UnsendableBase { - Self { - value: std::rc::Rc::new(value), - } - } - - #[getter] - fn value(&self) -> usize { - *self.value - } -} - -#[pyclass(extends=UnsendableBase)] -struct UnsendableChild {} - -#[pymethods] -impl UnsendableChild { - #[new] - fn new(value: usize) -> (UnsendableChild, UnsendableBase) { - (UnsendableChild {}, UnsendableBase::new(value)) - } -} - -fn test_unsendable() -> PyResult<()> { - let (keep_obj_here, obj) = Python::with_gil(|py| -> PyResult<_> { - let obj: Py = PyType::new::(py).call1((5,))?.extract()?; - - // Accessing the value inside this thread should not panic - let caught_panic = - std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> PyResult<_> { - assert_eq!(obj.getattr(py, "value")?.extract::(py)?, 5); - Ok(()) - })) - .is_err(); - - assert!(!caught_panic); - - Ok((obj.clone_ref(py), obj)) - })?; - - let caught_panic = std::thread::spawn(move || { - // This access must panic - Python::with_gil(move |py| { - obj.borrow(py); - }); - }) - .join(); - - Python::with_gil(|_py| drop(keep_obj_here)); - - if let Err(err) = caught_panic { - if let Some(msg) = err.downcast_ref::() { - panic!("{}", msg); - } - } - - Ok(()) -} - -/// If a class is marked as `unsendable`, it panics when accessed by another thread. -#[test] -#[cfg_attr(target_arch = "wasm32", ignore)] -#[should_panic( - expected = "test_class_basics::UnsendableBase is unsendable, but sent to another thread" -)] -fn panic_unsendable_base() { - test_unsendable::().unwrap(); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", ignore)] -#[should_panic( - expected = "test_class_basics::UnsendableBase is unsendable, but sent to another thread" -)] -fn panic_unsendable_child() { - test_unsendable::().unwrap(); -} - -fn get_length(obj: &Bound<'_, PyAny>) -> PyResult { - let length = obj.len()?; - - Ok(length) -} - -fn is_even(obj: &Bound<'_, PyAny>) -> PyResult { - obj.extract::().map(|i| i % 2 == 0) -} - -#[pyclass] -struct ClassWithFromPyWithMethods {} - -#[pymethods] -impl ClassWithFromPyWithMethods { - fn instance_method(&self, #[pyo3(from_py_with = "get_length")] argument: usize) -> usize { - argument - } - #[classmethod] - fn classmethod( - _cls: &Bound<'_, PyType>, - #[pyo3(from_py_with = "Bound::<'_, PyAny>::len")] argument: usize, - ) -> usize { - argument - } - - #[staticmethod] - fn staticmethod(#[pyo3(from_py_with = "get_length")] argument: usize) -> usize { - argument - } - - fn __contains__(&self, #[pyo3(from_py_with = "is_even")] obj: bool) -> bool { - obj - } -} - -#[test] -fn test_pymethods_from_py_with() { - Python::with_gil(|py| { - let instance = Py::new(py, ClassWithFromPyWithMethods {}).unwrap(); - - py_run!( - py, - instance, - r#" - arg = {1: 1, 2: 3} - - assert instance.instance_method(arg) == 2 - assert instance.classmethod(arg) == 2 - assert instance.staticmethod(arg) == 2 - - assert 42 in instance - assert 73 not in instance - "# - ); - }) -} - -#[pyclass] -struct TupleClass(#[pyo3(get, set, name = "value")] i32); - -#[test] -fn test_tuple_struct_class() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - assert!(typeobj.call((), None).is_err()); - - py_assert!(py, typeobj, "typeobj.__name__ == 'TupleClass'"); - - let instance = Py::new(py, TupleClass(5)).unwrap(); - py_run!( - py, - instance, - r#" - assert instance.value == 5; - instance.value = 1234; - assert instance.value == 1234; - "# - ); - - assert_eq!(instance.borrow(py).0, 1234); - }); -} - -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -#[pyclass(dict, subclass)] -struct DunderDictSupport { - // Make sure that dict_offset runs with non-zero sized Self - _pad: [u8; 32], -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn dunder_dict_support() { - Python::with_gil(|py| { - let inst = Py::new( - py, - DunderDictSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ) - .unwrap(); - py_run!( - py, - inst, - r#" - inst.a = 1 - assert inst.a == 1 - "# - ); - }); -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn access_dunder_dict() { - Python::with_gil(|py| { - let inst = Py::new( - py, - DunderDictSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ) - .unwrap(); - py_run!( - py, - inst, - r#" - inst.a = 1 - assert inst.__dict__ == {'a': 1} - "# - ); - }); -} - -// If the base class has dict support, child class also has dict -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -#[pyclass(extends=DunderDictSupport)] -struct InheritDict { - _value: usize, -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn inherited_dict() { - Python::with_gil(|py| { - let inst = Py::new( - py, - ( - InheritDict { _value: 0 }, - DunderDictSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ), - ) - .unwrap(); - py_run!( - py, - inst, - r#" - inst.a = 1 - assert inst.a == 1 - "# - ); - }); -} - -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -#[pyclass(weakref, dict)] -struct WeakRefDunderDictSupport { - // Make sure that weaklist_offset runs with non-zero sized Self - _pad: [u8; 32], -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn weakref_dunder_dict_support() { - Python::with_gil(|py| { - let inst = Py::new( - py, - WeakRefDunderDictSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ) - .unwrap(); - py_run!( - py, - inst, - "import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1" - ); - }); -} - -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -#[pyclass(weakref, subclass)] -struct WeakRefSupport { - _pad: [u8; 32], -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn weakref_support() { - Python::with_gil(|py| { - let inst = Py::new( - py, - WeakRefSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ) - .unwrap(); - py_run!( - py, - inst, - "import weakref; assert weakref.ref(inst)() is inst" - ); - }); -} - -// If the base class has weakref support, child class also has weakref. -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -#[pyclass(extends=WeakRefSupport)] -struct InheritWeakRef { - _value: usize, -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn inherited_weakref() { - Python::with_gil(|py| { - let inst = Py::new( - py, - ( - InheritWeakRef { _value: 0 }, - WeakRefSupport { - _pad: *b"DEADBEEFDEADBEEFDEADBEEFDEADBEEF", - }, - ), - ) - .unwrap(); - py_run!( - py, - inst, - "import weakref; assert weakref.ref(inst)() is inst" - ); - }); -} - -#[test] -fn access_frozen_class_without_gil() { - use std::sync::atomic::{AtomicUsize, Ordering}; - - #[pyclass(frozen)] - struct FrozenCounter { - value: AtomicUsize, - } - - let py_counter: Py = Python::with_gil(|py| { - let counter = FrozenCounter { - value: AtomicUsize::new(0), - }; - - let cell = Bound::new(py, counter).unwrap(); - - cell.get().value.fetch_add(1, Ordering::Relaxed); - - cell.into() - }); - - assert_eq!(py_counter.get().value.load(Ordering::Relaxed), 1); - - Python::with_gil(move |_py| drop(py_counter)); -} - -#[test] -#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] // sys.unraisablehook not available until Python 3.8 -#[cfg_attr(target_arch = "wasm32", ignore)] -fn drop_unsendable_elsewhere() { - use common::UnraisableCapture; - use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }; - use std::thread::spawn; - - #[pyclass(unsendable)] - struct Unsendable { - dropped: Arc, - } - - impl Drop for Unsendable { - fn drop(&mut self) { - self.dropped.store(true, Ordering::SeqCst); - } - } - - Python::with_gil(|py| { - let capture = UnraisableCapture::install(py); - - let dropped = Arc::new(AtomicBool::new(false)); - - let unsendable = Py::new( - py, - Unsendable { - dropped: dropped.clone(), - }, - ) - .unwrap(); - - py.allow_threads(|| { - spawn(move || { - Python::with_gil(move |_py| { - drop(unsendable); - }); - }) - .join() - .unwrap(); - }); - - assert!(!dropped.load(Ordering::SeqCst)); - - let (err, object) = capture.borrow_mut(py).capture.take().unwrap(); - assert_eq!(err.to_string(), "RuntimeError: test_class_basics::drop_unsendable_elsewhere::Unsendable is unsendable, but is being dropped on another thread"); - assert!(object.is_none(py)); - - capture.borrow_mut(py).uninstall(py); - }); -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn test_unsendable_dict() { - #[pyclass(dict, unsendable)] - struct UnsendableDictClass {} - - #[pymethods] - impl UnsendableDictClass { - #[new] - fn new() -> Self { - UnsendableDictClass {} - } - } - - Python::with_gil(|py| { - let inst = Py::new(py, UnsendableDictClass {}).unwrap(); - py_run!(py, inst, "assert inst.__dict__ == {}"); - }); -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn test_unsendable_dict_with_weakref() { - #[pyclass(dict, unsendable, weakref)] - struct UnsendableDictClassWithWeakRef {} - - #[pymethods] - impl UnsendableDictClassWithWeakRef { - #[new] - fn new() -> Self { - Self {} - } - } - - Python::with_gil(|py| { - let inst = Py::new(py, UnsendableDictClassWithWeakRef {}).unwrap(); - py_run!(py, inst, "assert inst.__dict__ == {}"); - py_run!( - py, - inst, - "import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1" - ); - }); -} diff --git a/vendor/pyo3/tests/test_class_comparisons.rs b/vendor/pyo3/tests/test_class_comparisons.rs deleted file mode 100644 index 75bd6251aa..0000000000 --- a/vendor/pyo3/tests/test_class_comparisons.rs +++ /dev/null @@ -1,287 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(eq)] -#[derive(Debug, Clone, PartialEq)] -pub enum MyEnum { - Variant, - OtherVariant, -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)] -pub enum MyEnumOrd { - Variant, - OtherVariant, -} - -#[test] -fn test_enum_eq_enum() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnum::Variant).unwrap(); - let var2 = Py::new(py, MyEnum::Variant).unwrap(); - let other_var = Py::new(py, MyEnum::OtherVariant).unwrap(); - py_assert!(py, var1 var2, "var1 == var2"); - py_assert!(py, var1 other_var, "var1 != other_var"); - py_assert!(py, var1 var2, "(var1 != var2) == False"); - }) -} - -#[test] -fn test_enum_eq_incomparable() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnum::Variant).unwrap(); - py_assert!(py, var1, "(var1 == 'foo') == False"); - py_assert!(py, var1, "(var1 != 'foo') == True"); - }) -} - -#[test] -fn test_enum_ord_comparable_opt_in_only() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnum::Variant).unwrap(); - let var2 = Py::new(py, MyEnum::OtherVariant).unwrap(); - // ordering on simple enums if opt in only, thus raising an error below - py_expect_exception!(py, var1 var2, "(var1 > var2) == False", PyTypeError); - }) -} - -#[test] -fn test_simple_enum_ord_comparable() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnumOrd::Variant).unwrap(); - let var2 = Py::new(py, MyEnumOrd::OtherVariant).unwrap(); - let var3 = Py::new(py, MyEnumOrd::OtherVariant).unwrap(); - py_assert!(py, var1 var2, "(var1 > var2) == False"); - py_assert!(py, var1 var2, "(var1 < var2) == True"); - py_assert!(py, var1 var2, "(var1 >= var2) == False"); - py_assert!(py, var2 var3, "(var3 >= var2) == True"); - py_assert!(py, var1 var2, "(var1 <= var2) == True"); - }) -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)] -pub enum MyComplexEnumOrd { - Variant(i32), - OtherVariant(String), -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)] -pub enum MyComplexEnumOrd2 { - Variant { msg: String, idx: u32 }, - OtherVariant { name: String, idx: u32 }, -} - -#[test] -fn test_complex_enum_ord_comparable() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyComplexEnumOrd::Variant(-2)).unwrap(); - let var2 = Py::new(py, MyComplexEnumOrd::Variant(5)).unwrap(); - let var3 = Py::new(py, MyComplexEnumOrd::OtherVariant("a".to_string())).unwrap(); - let var4 = Py::new(py, MyComplexEnumOrd::OtherVariant("b".to_string())).unwrap(); - py_assert!(py, var1 var2, "(var1 > var2) == False"); - py_assert!(py, var1 var2, "(var1 < var2) == True"); - py_assert!(py, var1 var2, "(var1 >= var2) == False"); - py_assert!(py, var1 var2, "(var1 <= var2) == True"); - - py_assert!(py, var1 var3, "(var1 >= var3) == False"); - py_assert!(py, var1 var3, "(var1 <= var3) == True"); - - py_assert!(py, var3 var4, "(var3 >= var4) == False"); - py_assert!(py, var3 var4, "(var3 <= var4) == True"); - - let var5 = Py::new( - py, - MyComplexEnumOrd2::Variant { - msg: "hello".to_string(), - idx: 1, - }, - ) - .unwrap(); - let var6 = Py::new( - py, - MyComplexEnumOrd2::Variant { - msg: "hello".to_string(), - idx: 1, - }, - ) - .unwrap(); - let var7 = Py::new( - py, - MyComplexEnumOrd2::Variant { - msg: "goodbye".to_string(), - idx: 7, - }, - ) - .unwrap(); - let var8 = Py::new( - py, - MyComplexEnumOrd2::Variant { - msg: "about".to_string(), - idx: 0, - }, - ) - .unwrap(); - let var9 = Py::new( - py, - MyComplexEnumOrd2::OtherVariant { - name: "albert".to_string(), - idx: 1, - }, - ) - .unwrap(); - - py_assert!(py, var5 var6, "(var5 == var6) == True"); - py_assert!(py, var5 var6, "(var5 <= var6) == True"); - py_assert!(py, var6 var7, "(var6 <= var7) == False"); - py_assert!(py, var6 var7, "(var6 >= var7) == True"); - py_assert!(py, var5 var8, "(var5 > var8) == True"); - py_assert!(py, var8 var9, "(var9 > var8) == True"); - }) -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)] -pub struct Point { - x: i32, - y: i32, - z: i32, -} - -#[test] -fn test_struct_numeric_ord_comparable() { - Python::with_gil(|py| { - let var1 = Py::new(py, Point { x: 10, y: 2, z: 3 }).unwrap(); - let var2 = Py::new(py, Point { x: 2, y: 2, z: 3 }).unwrap(); - let var3 = Py::new(py, Point { x: 1, y: 22, z: 4 }).unwrap(); - let var4 = Py::new(py, Point { x: 1, y: 3, z: 4 }).unwrap(); - let var5 = Py::new(py, Point { x: 1, y: 3, z: 4 }).unwrap(); - py_assert!(py, var1 var2, "(var1 > var2) == True"); - py_assert!(py, var1 var2, "(var1 <= var2) == False"); - py_assert!(py, var2 var3, "(var3 < var2) == True"); - py_assert!(py, var3 var4, "(var3 > var4) == True"); - py_assert!(py, var4 var5, "(var4 == var5) == True"); - py_assert!(py, var3 var5, "(var3 != var5) == True"); - }) -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone, PartialOrd)] -pub struct Person { - surname: String, - given_name: String, -} - -#[test] -fn test_struct_string_ord_comparable() { - Python::with_gil(|py| { - let var1 = Py::new( - py, - Person { - surname: "zzz".to_string(), - given_name: "bob".to_string(), - }, - ) - .unwrap(); - let var2 = Py::new( - py, - Person { - surname: "aaa".to_string(), - given_name: "sally".to_string(), - }, - ) - .unwrap(); - let var3 = Py::new( - py, - Person { - surname: "eee".to_string(), - given_name: "qqq".to_string(), - }, - ) - .unwrap(); - let var4 = Py::new( - py, - Person { - surname: "ddd".to_string(), - given_name: "aaa".to_string(), - }, - ) - .unwrap(); - - py_assert!(py, var1 var2, "(var1 > var2) == True"); - py_assert!(py, var1 var2, "(var1 <= var2) == False"); - py_assert!(py, var1 var3, "(var1 >= var3) == True"); - py_assert!(py, var2 var3, "(var2 >= var3) == False"); - py_assert!(py, var3 var4, "(var3 >= var4) == True"); - py_assert!(py, var3 var4, "(var3 != var4) == True"); - }) -} - -#[pyclass(eq, ord)] -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct Record { - name: String, - title: String, - idx: u32, -} - -impl PartialOrd for Record { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.idx.partial_cmp(&other.idx).unwrap()) - } -} - -#[test] -fn test_struct_custom_ord_comparable() { - Python::with_gil(|py| { - let var1 = Py::new( - py, - Record { - name: "zzz".to_string(), - title: "bbb".to_string(), - idx: 9, - }, - ) - .unwrap(); - let var2 = Py::new( - py, - Record { - name: "ddd".to_string(), - title: "aaa".to_string(), - idx: 1, - }, - ) - .unwrap(); - let var3 = Py::new( - py, - Record { - name: "vvv".to_string(), - title: "ggg".to_string(), - idx: 19, - }, - ) - .unwrap(); - let var4 = Py::new( - py, - Record { - name: "vvv".to_string(), - title: "ggg".to_string(), - idx: 19, - }, - ) - .unwrap(); - - py_assert!(py, var1 var2, "(var1 > var2) == True"); - py_assert!(py, var1 var2, "(var1 <= var2) == False"); - py_assert!(py, var1 var3, "(var1 >= var3) == False"); - py_assert!(py, var2 var3, "(var2 >= var3) == False"); - py_assert!(py, var3 var4, "(var3 == var4) == True"); - py_assert!(py, var2 var4, "(var2 != var4) == True"); - }) -} diff --git a/vendor/pyo3/tests/test_class_conversion.rs b/vendor/pyo3/tests/test_class_conversion.rs deleted file mode 100644 index a1e2188e83..0000000000 --- a/vendor/pyo3/tests/test_class_conversion.rs +++ /dev/null @@ -1,156 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -#[derive(Clone, Debug, PartialEq)] -struct Cloneable { - x: i32, -} - -#[test] -fn test_cloneable_pyclass() { - let c = Cloneable { x: 10 }; - - Python::with_gil(|py| { - let py_c = Py::new(py, c.clone()).unwrap(); - - let c2: Cloneable = py_c.extract(py).unwrap(); - assert_eq!(c, c2); - { - let rc: PyRef<'_, Cloneable> = py_c.extract(py).unwrap(); - assert_eq!(&c, &*rc); - // Drops PyRef before taking PyRefMut - } - let mrc: PyRefMut<'_, Cloneable> = py_c.extract(py).unwrap(); - assert_eq!(&c, &*mrc); - }); -} - -#[pyclass(subclass)] -#[derive(Default)] -struct BaseClass { - value: i32, -} - -#[pymethods] -impl BaseClass { - fn foo(&self) -> &'static str { - "BaseClass" - } -} - -#[pyclass(extends=BaseClass)] -struct SubClass {} - -#[pymethods] -impl SubClass { - fn foo(&self) -> &'static str { - "SubClass" - } -} - -#[pyclass] -struct PolymorphicContainer { - #[pyo3(get, set)] - inner: Py, -} - -#[test] -fn test_polymorphic_container_stores_base_class() { - Python::with_gil(|py| { - let p = Py::new( - py, - PolymorphicContainer { - inner: Py::new(py, BaseClass::default()).unwrap(), - }, - ) - .unwrap(); - - py_assert!(py, p, "p.inner.foo() == 'BaseClass'"); - }); -} - -#[test] -fn test_polymorphic_container_stores_sub_class() { - Python::with_gil(|py| { - let p = Py::new( - py, - PolymorphicContainer { - inner: Py::new(py, BaseClass::default()).unwrap(), - }, - ) - .unwrap(); - - p.bind(py) - .setattr( - "inner", - Py::new( - py, - PyClassInitializer::from(BaseClass::default()).add_subclass(SubClass {}), - ) - .unwrap(), - ) - .unwrap(); - - py_assert!(py, p, "p.inner.foo() == 'SubClass'"); - }); -} - -#[test] -fn test_polymorphic_container_does_not_accept_other_types() { - Python::with_gil(|py| { - let p = Py::new( - py, - PolymorphicContainer { - inner: Py::new(py, BaseClass::default()).unwrap(), - }, - ) - .unwrap(); - - let setattr = |value: Bound<'_, PyAny>| p.bind(py).setattr("inner", value); - - assert!(setattr(1i32.into_pyobject(py).unwrap().into_any()).is_err()); - assert!(setattr(py.None().into_bound(py)).is_err()); - assert!(setattr((1i32, 2i32).into_pyobject(py).unwrap().into_any()).is_err()); - }); -} - -#[test] -fn test_pyref_as_base() { - Python::with_gil(|py| { - let cell = Bound::new(py, (SubClass {}, BaseClass { value: 120 })).unwrap(); - - // First try PyRefMut - let sub: PyRefMut<'_, SubClass> = cell.borrow_mut(); - let mut base: PyRefMut<'_, BaseClass> = sub.into_super(); - assert_eq!(120, base.value); - base.value = 999; - assert_eq!(999, base.value); - drop(base); - - // Repeat for PyRef - let sub: PyRef<'_, SubClass> = cell.borrow(); - let base: PyRef<'_, BaseClass> = sub.into_super(); - assert_eq!(999, base.value); - }); -} - -#[test] -fn test_pycell_deref() { - Python::with_gil(|py| { - let obj = Bound::new(py, (SubClass {}, BaseClass { value: 120 })).unwrap(); - - // Should be able to deref as PyAny - assert_eq!( - obj.call_method0("foo") - .and_then(|e| e.extract::()) - .unwrap(), - "SubClass" - ); - }); -} diff --git a/vendor/pyo3/tests/test_class_formatting.rs b/vendor/pyo3/tests/test_class_formatting.rs deleted file mode 100644 index 10f760b9c4..0000000000 --- a/vendor/pyo3/tests/test_class_formatting.rs +++ /dev/null @@ -1,179 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::py_run; -use std::fmt::{Display, Formatter}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(eq, str)] -#[derive(Debug, PartialEq)] -pub enum MyEnum2 { - Variant, - OtherVariant, -} - -impl Display for MyEnum2 { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} - -#[pyclass(eq, str)] -#[derive(Debug, PartialEq)] -pub enum MyEnum3 { - #[pyo3(name = "AwesomeVariant")] - Variant, - OtherVariant, -} - -impl Display for MyEnum3 { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let variant = match self { - MyEnum3::Variant => "AwesomeVariant", - MyEnum3::OtherVariant => "OtherVariant", - }; - write!(f, "MyEnum.{}", variant) - } -} - -#[test] -fn test_enum_class_fmt() { - Python::with_gil(|py| { - let var2 = Py::new(py, MyEnum2::Variant).unwrap(); - let var3 = Py::new(py, MyEnum3::Variant).unwrap(); - let var4 = Py::new(py, MyEnum3::OtherVariant).unwrap(); - py_assert!(py, var2, "str(var2) == 'Variant'"); - py_assert!(py, var3, "str(var3) == 'MyEnum.AwesomeVariant'"); - py_assert!(py, var4, "str(var4) == 'MyEnum.OtherVariant'"); - }) -} - -#[pyclass(str = "X: {x}, Y: {y}, Z: {z}")] -#[derive(PartialEq, Eq, Clone, PartialOrd)] -pub struct Point { - x: i32, - y: i32, - z: i32, -} - -#[test] -fn test_custom_struct_custom_str() { - Python::with_gil(|py| { - let var1 = Py::new(py, Point { x: 1, y: 2, z: 3 }).unwrap(); - py_assert!(py, var1, "str(var1) == 'X: 1, Y: 2, Z: 3'"); - }) -} - -#[pyclass(str)] -#[derive(PartialEq, Eq, Clone, PartialOrd)] -pub struct Point2 { - x: i32, - y: i32, - z: i32, -} - -impl Display for Point2 { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "({}, {}, {})", self.x, self.y, self.z) - } -} - -#[test] -fn test_struct_str() { - Python::with_gil(|py| { - let var1 = Py::new(py, Point2 { x: 1, y: 2, z: 3 }).unwrap(); - py_assert!(py, var1, "str(var1) == '(1, 2, 3)'"); - }) -} - -#[pyclass(str)] -#[derive(PartialEq, Debug)] -enum ComplexEnumWithStr { - A(u32), - B { msg: String }, -} - -impl Display for ComplexEnumWithStr { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} - -#[test] -fn test_custom_complex_enum_str() { - Python::with_gil(|py| { - let var1 = Py::new(py, ComplexEnumWithStr::A(45)).unwrap(); - let var2 = Py::new( - py, - ComplexEnumWithStr::B { - msg: "Hello".to_string(), - }, - ) - .unwrap(); - py_assert!(py, var1, "str(var1) == 'A(45)'"); - py_assert!(py, var2, "str(var2) == 'B { msg: \"Hello\" }'"); - }) -} - -#[pyclass(str = "{0}, {1}, {2}")] -#[derive(PartialEq)] -struct Coord(u32, u32, u32); - -#[pyclass(str = "{{{0}, {1}, {2}}}")] -#[derive(PartialEq)] -struct Coord2(u32, u32, u32); - -#[test] -fn test_str_representation_by_position() { - Python::with_gil(|py| { - let var1 = Py::new(py, Coord(1, 2, 3)).unwrap(); - let var2 = Py::new(py, Coord2(1, 2, 3)).unwrap(); - py_assert!(py, var1, "str(var1) == '1, 2, 3'"); - py_assert!(py, var2, "str(var2) == '{1, 2, 3}'"); - }) -} - -#[pyclass(str = "name: {name}: {name}, idn: {idn:03} with message: {msg}")] -#[derive(PartialEq, Debug)] -struct Point4 { - name: String, - msg: String, - idn: u32, -} - -#[test] -fn test_mixed_and_repeated_str_formats() { - Python::with_gil(|py| { - let var1 = Py::new( - py, - Point4 { - name: "aaa".to_string(), - msg: "hello".to_string(), - idn: 1, - }, - ) - .unwrap(); - py_run!( - py, - var1, - r#" - assert str(var1) == 'name: aaa: aaa, idn: 001 with message: hello' - "# - ); - }) -} - -#[pyclass(str = "type: {r#type}")] -struct Foo { - r#type: u32, -} - -#[test] -fn test_raw_identifier_struct_custom_str() { - Python::with_gil(|py| { - let var1 = Py::new(py, Foo { r#type: 3 }).unwrap(); - py_assert!(py, var1, "str(var1) == 'type: 3'"); - }) -} diff --git a/vendor/pyo3/tests/test_class_new.rs b/vendor/pyo3/tests/test_class_new.rs deleted file mode 100644 index 75070bd274..0000000000 --- a/vendor/pyo3/tests/test_class_new.rs +++ /dev/null @@ -1,325 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::exceptions::PyValueError; -use pyo3::prelude::*; -use pyo3::sync::GILOnceCell; -use pyo3::types::IntoPyDict; - -#[pyclass] -struct EmptyClassWithNew {} - -#[pymethods] -impl EmptyClassWithNew { - #[new] - fn new() -> EmptyClassWithNew { - EmptyClassWithNew {} - } -} - -#[test] -fn empty_class_with_new() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - assert!(typeobj - .call((), None) - .unwrap() - .downcast::() - .is_ok()); - - // Calling with arbitrary args or kwargs is not ok - assert!(typeobj.call(("some", "args"), None).is_err()); - assert!(typeobj - .call((), Some(&[("some", "kwarg")].into_py_dict(py).unwrap())) - .is_err()); - }); -} - -#[pyclass] -struct UnitClassWithNew; - -#[pymethods] -impl UnitClassWithNew { - #[new] - fn new() -> Self { - Self - } -} - -#[test] -fn unit_class_with_new() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - assert!(typeobj - .call((), None) - .unwrap() - .downcast::() - .is_ok()); - }); -} - -#[pyclass] -struct TupleClassWithNew(i32); - -#[pymethods] -impl TupleClassWithNew { - #[new] - fn new(arg: i32) -> Self { - Self(arg) - } -} - -#[test] -fn tuple_class_with_new() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let wrp = typeobj.call((42,), None).unwrap(); - let obj = wrp.downcast::().unwrap(); - let obj_ref = obj.borrow(); - assert_eq!(obj_ref.0, 42); - }); -} - -#[pyclass] -#[derive(Debug)] -struct NewWithOneArg { - data: i32, -} - -#[pymethods] -impl NewWithOneArg { - #[new] - fn new(arg: i32) -> NewWithOneArg { - NewWithOneArg { data: arg } - } -} - -#[test] -fn new_with_one_arg() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let wrp = typeobj.call((42,), None).unwrap(); - let obj = wrp.downcast::().unwrap(); - let obj_ref = obj.borrow(); - assert_eq!(obj_ref.data, 42); - }); -} - -#[pyclass] -struct NewWithTwoArgs { - data1: i32, - data2: i32, -} - -#[pymethods] -impl NewWithTwoArgs { - #[new] - fn new(arg1: i32, arg2: i32) -> Self { - NewWithTwoArgs { - data1: arg1, - data2: arg2, - } - } -} - -#[test] -fn new_with_two_args() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let wrp = typeobj - .call((10, 20), None) - .map_err(|e| e.display(py)) - .unwrap(); - let obj = wrp.downcast::().unwrap(); - let obj_ref = obj.borrow(); - assert_eq!(obj_ref.data1, 10); - assert_eq!(obj_ref.data2, 20); - }); -} - -#[pyclass(subclass)] -struct SuperClass { - #[pyo3(get)] - from_rust: bool, -} - -#[pymethods] -impl SuperClass { - #[new] - fn new() -> Self { - SuperClass { from_rust: true } - } -} - -/// Checks that `subclass.__new__` works correctly. -/// See https://github.com/PyO3/pyo3/issues/947 for the corresponding bug. -#[test] -fn subclass_new() { - Python::with_gil(|py| { - let super_cls = py.get_type::(); - let source = pyo3_ffi::c_str!(pyo3::indoc::indoc!( - r#" -class Class(SuperClass): - def __new__(cls): - return super().__new__(cls) # This should return an instance of Class - - @property - def from_rust(self): - return False -c = Class() -assert c.from_rust is False -"# - )); - let globals = PyModule::import(py, "__main__").unwrap().dict(); - globals.set_item("SuperClass", super_cls).unwrap(); - py.run(source, Some(&globals), None) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} - -#[pyclass] -#[derive(Debug)] -struct NewWithCustomError {} - -struct CustomError; - -impl From for PyErr { - fn from(_error: CustomError) -> PyErr { - PyValueError::new_err("custom error") - } -} - -#[pymethods] -impl NewWithCustomError { - #[new] - fn new() -> Result { - Err(CustomError) - } -} - -#[test] -fn new_with_custom_error() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let err = typeobj.call0().unwrap_err(); - assert_eq!(err.to_string(), "ValueError: custom error"); - }); -} - -#[pyclass] -struct NewExisting { - #[pyo3(get)] - num: usize, -} - -#[pymethods] -impl NewExisting { - #[new] - fn new(py: pyo3::Python<'_>, val: usize) -> pyo3::Py { - static PRE_BUILT: GILOnceCell<[pyo3::Py; 2]> = GILOnceCell::new(); - let existing = PRE_BUILT.get_or_init(py, || { - [ - pyo3::Py::new(py, NewExisting { num: 0 }).unwrap(), - pyo3::Py::new(py, NewExisting { num: 1 }).unwrap(), - ] - }); - - if val < existing.len() { - return existing[val].clone_ref(py); - } - - pyo3::Py::new(py, NewExisting { num: val }).unwrap() - } -} - -#[test] -fn test_new_existing() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - let obj1 = typeobj.call1((0,)).unwrap(); - let obj2 = typeobj.call1((0,)).unwrap(); - let obj3 = typeobj.call1((1,)).unwrap(); - let obj4 = typeobj.call1((1,)).unwrap(); - let obj5 = typeobj.call1((2,)).unwrap(); - let obj6 = typeobj.call1((2,)).unwrap(); - - assert!(obj1.getattr("num").unwrap().extract::().unwrap() == 0); - assert!(obj2.getattr("num").unwrap().extract::().unwrap() == 0); - assert!(obj3.getattr("num").unwrap().extract::().unwrap() == 1); - assert!(obj4.getattr("num").unwrap().extract::().unwrap() == 1); - assert!(obj5.getattr("num").unwrap().extract::().unwrap() == 2); - assert!(obj6.getattr("num").unwrap().extract::().unwrap() == 2); - - assert!(obj1.is(&obj2)); - assert!(obj3.is(&obj4)); - assert!(!obj1.is(&obj3)); - assert!(!obj1.is(&obj5)); - assert!(!obj5.is(&obj6)); - }); -} - -#[pyclass] -struct NewReturnsPy; - -#[pymethods] -impl NewReturnsPy { - #[new] - fn new(py: Python<'_>) -> PyResult> { - Py::new(py, NewReturnsPy) - } -} - -#[test] -fn test_new_returns_py() { - Python::with_gil(|py| { - let type_ = py.get_type::(); - let obj = type_.call0().unwrap(); - assert!(obj.is_exact_instance_of::()); - }) -} - -#[pyclass] -struct NewReturnsBound; - -#[pymethods] -impl NewReturnsBound { - #[new] - fn new(py: Python<'_>) -> PyResult> { - Bound::new(py, NewReturnsBound) - } -} - -#[test] -fn test_new_returns_bound() { - Python::with_gil(|py| { - let type_ = py.get_type::(); - let obj = type_.call0().unwrap(); - assert!(obj.is_exact_instance_of::()); - }) -} - -#[pyo3::pyclass] -struct NewClassMethod { - #[pyo3(get)] - cls: pyo3::PyObject, -} - -#[pyo3::pymethods] -impl NewClassMethod { - #[new] - #[classmethod] - fn new(cls: &pyo3::Bound<'_, pyo3::types::PyType>) -> Self { - Self { - cls: cls.clone().into_any().unbind(), - } - } -} - -#[test] -fn test_new_class_method() { - pyo3::Python::with_gil(|py| { - let cls = py.get_type::(); - pyo3::py_run!(py, cls, "assert cls().cls is cls"); - }); -} diff --git a/vendor/pyo3/tests/test_compile_error.rs b/vendor/pyo3/tests/test_compile_error.rs deleted file mode 100644 index 4aba1d4723..0000000000 --- a/vendor/pyo3/tests/test_compile_error.rs +++ /dev/null @@ -1,72 +0,0 @@ -#![cfg(feature = "macros")] - -#[cfg(not(target_arch = "wasm32"))] // Not possible to invoke compiler from wasm -#[test] -fn test_compile_errors() { - let t = trybuild::TestCases::new(); - - t.compile_fail("tests/ui/invalid_property_args.rs"); - t.compile_fail("tests/ui/invalid_proto_pymethods.rs"); - t.compile_fail("tests/ui/invalid_pyclass_args.rs"); - t.compile_fail("tests/ui/invalid_pyclass_enum.rs"); - t.compile_fail("tests/ui/invalid_pyclass_item.rs"); - t.compile_fail("tests/ui/invalid_pyfunction_signatures.rs"); - t.compile_fail("tests/ui/invalid_pyfunction_definition.rs"); - #[cfg(any(not(Py_LIMITED_API), Py_3_11))] - t.compile_fail("tests/ui/invalid_pymethods_buffer.rs"); - // The output is not stable across abi3 / not abi3 and features - #[cfg(all(not(Py_LIMITED_API), feature = "full"))] - t.compile_fail("tests/ui/invalid_pymethods_duplicates.rs"); - t.compile_fail("tests/ui/invalid_pymethod_enum.rs"); - t.compile_fail("tests/ui/invalid_pymethod_names.rs"); - t.compile_fail("tests/ui/invalid_pymodule_args.rs"); - t.compile_fail("tests/ui/reject_generics.rs"); - t.compile_fail("tests/ui/deprecations.rs"); - t.compile_fail("tests/ui/invalid_closure.rs"); - t.compile_fail("tests/ui/pyclass_send.rs"); - t.compile_fail("tests/ui/invalid_argument_attributes.rs"); - t.compile_fail("tests/ui/invalid_intopy_derive.rs"); - t.compile_fail("tests/ui/invalid_frompy_derive.rs"); - t.compile_fail("tests/ui/static_ref.rs"); - t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs"); - t.compile_fail("tests/ui/invalid_pyfunctions.rs"); - #[cfg(not(any(feature = "hashbrown", feature = "indexmap")))] - t.compile_fail("tests/ui/invalid_pymethods.rs"); - // output changes with async feature - #[cfg(all(Py_LIMITED_API, feature = "experimental-async"))] - t.compile_fail("tests/ui/abi3_nativetype_inheritance.rs"); - t.compile_fail("tests/ui/invalid_intern_arg.rs"); - t.compile_fail("tests/ui/invalid_frozen_pyclass_borrow.rs"); - #[cfg(not(any(feature = "hashbrown", feature = "indexmap")))] - t.compile_fail("tests/ui/invalid_pymethod_receiver.rs"); - t.compile_fail("tests/ui/missing_intopy.rs"); - // adding extra error conversion impls changes the output - #[cfg(not(any(windows, feature = "eyre", feature = "anyhow", Py_LIMITED_API)))] - t.compile_fail("tests/ui/invalid_result_conversion.rs"); - t.compile_fail("tests/ui/not_send.rs"); - t.compile_fail("tests/ui/not_send2.rs"); - t.compile_fail("tests/ui/get_set_all.rs"); - t.compile_fail("tests/ui/traverse.rs"); - t.compile_fail("tests/ui/invalid_pymodule_in_root.rs"); - t.compile_fail("tests/ui/invalid_pymodule_glob.rs"); - t.compile_fail("tests/ui/invalid_pymodule_trait.rs"); - t.compile_fail("tests/ui/invalid_pymodule_two_pymodule_init.rs"); - #[cfg(feature = "experimental-async")] - #[cfg(any(not(Py_LIMITED_API), Py_3_10))] // to avoid PyFunctionArgument for &str - t.compile_fail("tests/ui/invalid_cancel_handle.rs"); - t.pass("tests/ui/pymodule_missing_docs.rs"); - #[cfg(not(Py_LIMITED_API))] - t.pass("tests/ui/forbid_unsafe.rs"); - #[cfg(all(Py_LIMITED_API, not(feature = "experimental-async")))] - // output changes with async feature - t.compile_fail("tests/ui/abi3_inheritance.rs"); - #[cfg(all(Py_LIMITED_API, not(Py_3_9)))] - t.compile_fail("tests/ui/abi3_weakref.rs"); - #[cfg(all(Py_LIMITED_API, not(Py_3_9)))] - t.compile_fail("tests/ui/abi3_dict.rs"); - t.compile_fail("tests/ui/duplicate_pymodule_submodule.rs"); - #[cfg(all(not(Py_LIMITED_API), Py_3_11))] - t.compile_fail("tests/ui/invalid_base_class.rs"); - t.pass("tests/ui/ambiguous_associated_items.rs"); - t.pass("tests/ui/pyclass_probe.rs"); -} diff --git a/vendor/pyo3/tests/test_coroutine.rs b/vendor/pyo3/tests/test_coroutine.rs deleted file mode 100644 index 89ab3d64a4..0000000000 --- a/vendor/pyo3/tests/test_coroutine.rs +++ /dev/null @@ -1,359 +0,0 @@ -#![cfg(feature = "experimental-async")] -#![cfg(not(target_arch = "wasm32"))] -use std::{ffi::CString, task::Poll, thread, time::Duration}; - -use futures::{channel::oneshot, future::poll_fn, FutureExt}; -#[cfg(not(target_has_atomic = "64"))] -use portable_atomic::{AtomicBool, Ordering}; -use pyo3::{ - coroutine::CancelHandle, - prelude::*, - py_run, - types::{IntoPyDict, PyType}, -}; -#[cfg(target_has_atomic = "64")] -use std::sync::atomic::{AtomicBool, Ordering}; - -#[path = "../src/tests/common.rs"] -mod common; - -fn handle_windows(test: &str) -> String { - let set_event_loop_policy = r#" - import asyncio, sys - if sys.platform == "win32": - asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) - "#; - pyo3::unindent::unindent(set_event_loop_policy) + &pyo3::unindent::unindent(test) -} - -#[test] -fn noop_coroutine() { - #[pyfunction] - async fn noop() -> usize { - 42 - } - Python::with_gil(|gil| { - let noop = wrap_pyfunction!(noop, gil).unwrap(); - let test = "import asyncio; assert asyncio.run(noop()) == 42"; - py_run!(gil, noop, &handle_windows(test)); - }) -} - -#[test] -fn test_coroutine_qualname() { - #[pyfunction] - async fn my_fn() {} - #[pyclass] - struct MyClass; - #[pymethods] - impl MyClass { - #[new] - fn new() -> Self { - Self - } - // TODO use &self when possible - async fn my_method(_self: Py) {} - #[classmethod] - async fn my_classmethod(_cls: Py) {} - #[staticmethod] - async fn my_staticmethod() {} - } - Python::with_gil(|gil| { - let test = r#" - for coro, name, qualname in [ - (my_fn(), "my_fn", "my_fn"), - (MyClass().my_method(), "my_method", "MyClass.my_method"), - #(MyClass().my_classmethod(), "my_classmethod", "MyClass.my_classmethod"), - (MyClass.my_staticmethod(), "my_staticmethod", "MyClass.my_staticmethod"), - ]: - assert coro.__name__ == name and coro.__qualname__ == qualname - "#; - let locals = [ - ("my_fn", wrap_pyfunction!(my_fn, gil).unwrap().as_any()), - ("MyClass", gil.get_type::().as_any()), - ] - .into_py_dict(gil) - .unwrap(); - py_run!(gil, *locals, &handle_windows(test)); - }) -} - -#[test] -fn sleep_0_like_coroutine() { - #[pyfunction] - async fn sleep_0() -> usize { - let mut waken = false; - poll_fn(|cx| { - if !waken { - cx.waker().wake_by_ref(); - waken = true; - return Poll::Pending; - } - Poll::Ready(42) - }) - .await - } - Python::with_gil(|gil| { - let sleep_0 = wrap_pyfunction!(sleep_0, gil).unwrap(); - let test = "import asyncio; assert asyncio.run(sleep_0()) == 42"; - py_run!(gil, sleep_0, &handle_windows(test)); - }) -} - -#[pyfunction] -async fn sleep(seconds: f64) -> usize { - let (tx, rx) = oneshot::channel(); - thread::spawn(move || { - thread::sleep(Duration::from_secs_f64(seconds)); - tx.send(42).unwrap(); - }); - rx.await.unwrap() -} - -#[test] -fn sleep_coroutine() { - Python::with_gil(|gil| { - let sleep = wrap_pyfunction!(sleep, gil).unwrap(); - let test = r#"import asyncio; assert asyncio.run(sleep(0.1)) == 42"#; - py_run!(gil, sleep, &handle_windows(test)); - }) -} - -#[pyfunction] -async fn return_tuple() -> (usize, usize) { - (42, 43) -} - -#[test] -fn tuple_coroutine() { - Python::with_gil(|gil| { - let func = wrap_pyfunction!(return_tuple, gil).unwrap(); - let test = r#"import asyncio; assert asyncio.run(func()) == (42, 43)"#; - py_run!(gil, func, &handle_windows(test)); - }) -} - -#[test] -fn cancelled_coroutine() { - Python::with_gil(|gil| { - let sleep = wrap_pyfunction!(sleep, gil).unwrap(); - let test = r#" - import asyncio - async def main(): - task = asyncio.create_task(sleep(999)) - await asyncio.sleep(0) - task.cancel() - await task - asyncio.run(main()) - "#; - let globals = gil.import("__main__").unwrap().dict(); - globals.set_item("sleep", sleep).unwrap(); - let err = gil - .run( - &CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(), - Some(&globals), - None, - ) - .unwrap_err(); - assert_eq!( - err.value(gil).get_type().qualname().unwrap(), - "CancelledError" - ); - }) -} - -#[test] -fn coroutine_cancel_handle() { - #[pyfunction] - async fn cancellable_sleep( - seconds: f64, - #[pyo3(cancel_handle)] mut cancel: CancelHandle, - ) -> usize { - futures::select! { - _ = sleep(seconds).fuse() => 42, - _ = cancel.cancelled().fuse() => 0, - } - } - Python::with_gil(|gil| { - let cancellable_sleep = wrap_pyfunction!(cancellable_sleep, gil).unwrap(); - let test = r#" - import asyncio; - async def main(): - task = asyncio.create_task(cancellable_sleep(999)) - await asyncio.sleep(0) - task.cancel() - return await task - assert asyncio.run(main()) == 0 - "#; - let globals = gil.import("__main__").unwrap().dict(); - globals - .set_item("cancellable_sleep", cancellable_sleep) - .unwrap(); - gil.run( - &CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(), - Some(&globals), - None, - ) - .unwrap(); - }) -} - -#[test] -fn coroutine_is_cancelled() { - #[pyfunction] - async fn sleep_loop(#[pyo3(cancel_handle)] cancel: CancelHandle) { - while !cancel.is_cancelled() { - sleep(0.001).await; - } - } - Python::with_gil(|gil| { - let sleep_loop = wrap_pyfunction!(sleep_loop, gil).unwrap(); - let test = r#" - import asyncio; - async def main(): - task = asyncio.create_task(sleep_loop()) - await asyncio.sleep(0) - task.cancel() - await task - asyncio.run(main()) - "#; - let globals = gil.import("__main__").unwrap().dict(); - globals.set_item("sleep_loop", sleep_loop).unwrap(); - gil.run( - &CString::new(pyo3::unindent::unindent(&handle_windows(test))).unwrap(), - Some(&globals), - None, - ) - .unwrap(); - }) -} - -#[test] -fn coroutine_panic() { - #[pyfunction] - async fn panic() { - panic!("test panic"); - } - Python::with_gil(|gil| { - let panic = wrap_pyfunction!(panic, gil).unwrap(); - let test = r#" - import asyncio - coro = panic() - try: - asyncio.run(coro) - except BaseException as err: - assert type(err).__name__ == "PanicException" - assert str(err) == "test panic" - else: - assert False - try: - coro.send(None) - except RuntimeError as err: - assert str(err) == "cannot reuse already awaited coroutine" - else: - assert False; - "#; - py_run!(gil, panic, &handle_windows(test)); - }) -} - -#[test] -fn test_async_method_receiver() { - #[pyclass] - struct Counter(usize); - #[pymethods] - impl Counter { - #[new] - fn new() -> Self { - Self(0) - } - async fn get(&self) -> usize { - self.0 - } - async fn incr(&mut self) -> usize { - self.0 += 1; - self.0 - } - } - - static IS_DROPPED: AtomicBool = AtomicBool::new(false); - - impl Drop for Counter { - fn drop(&mut self) { - IS_DROPPED.store(true, Ordering::SeqCst); - } - } - - Python::with_gil(|gil| { - let test = r#" - import asyncio - - obj = Counter() - coro1 = obj.get() - coro2 = obj.get() - try: - obj.incr() # borrow checking should fail - except RuntimeError as err: - pass - else: - assert False - assert asyncio.run(coro1) == 0 - coro2.close() - coro3 = obj.incr() - try: - obj.incr() # borrow checking should fail - except RuntimeError as err: - pass - else: - assert False - try: - obj.get() # borrow checking should fail - except RuntimeError as err: - pass - else: - assert False - assert asyncio.run(coro3) == 1 - "#; - let locals = [("Counter", gil.get_type::())] - .into_py_dict(gil) - .unwrap(); - py_run!(gil, *locals, test); - }); - - assert!(IS_DROPPED.load(Ordering::SeqCst)); -} - -#[test] -fn test_async_method_receiver_with_other_args() { - #[pyclass] - struct Value(i32); - #[pymethods] - impl Value { - #[new] - fn new() -> Self { - Self(0) - } - async fn get_value_plus_with(&self, v1: i32, v2: i32) -> i32 { - self.0 + v1 + v2 - } - async fn set_value(&mut self, new_value: i32) -> i32 { - self.0 = new_value; - self.0 - } - } - - Python::with_gil(|gil| { - let test = r#" - import asyncio - - v = Value() - assert asyncio.run(v.get_value_plus_with(3, 0)) == 3 - assert asyncio.run(v.set_value(10)) == 10 - assert asyncio.run(v.get_value_plus_with(1, 1)) == 12 - "#; - let locals = [("Value", gil.get_type::())] - .into_py_dict(gil) - .unwrap(); - py_run!(gil, *locals, test); - }); -} diff --git a/vendor/pyo3/tests/test_datetime.rs b/vendor/pyo3/tests/test_datetime.rs deleted file mode 100644 index b1fad408dc..0000000000 --- a/vendor/pyo3/tests/test_datetime.rs +++ /dev/null @@ -1,228 +0,0 @@ -#![cfg(not(Py_LIMITED_API))] - -use pyo3::types::{timezone_utc, IntoPyDict, PyDate, PyDateTime, PyTime}; -use pyo3::{ffi, prelude::*}; -use pyo3_ffi::PyDateTime_IMPORT; -use std::ffi::CString; - -fn _get_subclasses<'py>( - py: Python<'py>, - py_type: &str, - args: &str, -) -> PyResult<(Bound<'py, PyAny>, Bound<'py, PyAny>, Bound<'py, PyAny>)> { - // Import the class from Python and create some subclasses - let datetime = py.import("datetime")?; - - let locals = [(py_type, datetime.getattr(py_type)?)] - .into_py_dict(py) - .unwrap(); - - let make_subclass_py = CString::new(format!("class Subklass({}):\n pass", py_type))?; - - let make_sub_subclass_py = ffi::c_str!("class SubSubklass(Subklass):\n pass"); - - py.run(&make_subclass_py, None, Some(&locals))?; - py.run(make_sub_subclass_py, None, Some(&locals))?; - - // Construct an instance of the base class - let obj = py.eval( - &CString::new(format!("{}({})", py_type, args))?, - None, - Some(&locals), - )?; - - // Construct an instance of the subclass - let sub_obj = py.eval( - &CString::new(format!("Subklass({})", args))?, - None, - Some(&locals), - )?; - - // Construct an instance of the sub-subclass - let sub_sub_obj = py.eval( - &CString::new(format!("SubSubklass({})", args))?, - None, - Some(&locals), - )?; - - Ok((obj, sub_obj, sub_sub_obj)) -} - -macro_rules! assert_check_exact { - ($check_func:ident, $check_func_exact:ident, $obj: expr) => { - unsafe { - use pyo3::ffi::*; - assert!($check_func(($obj).as_ptr()) != 0); - assert!($check_func_exact(($obj).as_ptr()) != 0); - } - }; -} - -macro_rules! assert_check_only { - ($check_func:ident, $check_func_exact:ident, $obj: expr) => { - unsafe { - use pyo3::ffi::*; - assert!($check_func(($obj).as_ptr()) != 0); - assert!($check_func_exact(($obj).as_ptr()) == 0); - } - }; -} - -#[test] -fn test_date_check() { - Python::with_gil(|py| { - let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "date", "2018, 1, 1").unwrap(); - unsafe { PyDateTime_IMPORT() } - assert_check_exact!(PyDate_Check, PyDate_CheckExact, obj); - assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_obj); - assert_check_only!(PyDate_Check, PyDate_CheckExact, sub_sub_obj); - assert!(obj.is_instance_of::()); - assert!(!obj.is_instance_of::()); - assert!(!obj.is_instance_of::()); - }); -} - -#[test] -fn test_time_check() { - Python::with_gil(|py| { - let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "time", "12, 30, 15").unwrap(); - unsafe { PyDateTime_IMPORT() } - - assert_check_exact!(PyTime_Check, PyTime_CheckExact, obj); - assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_obj); - assert_check_only!(PyTime_Check, PyTime_CheckExact, sub_sub_obj); - assert!(!obj.is_instance_of::()); - assert!(obj.is_instance_of::()); - assert!(!obj.is_instance_of::()); - }); -} - -#[test] -fn test_datetime_check() { - Python::with_gil(|py| { - let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "datetime", "2018, 1, 1, 13, 30, 15") - .map_err(|e| e.display(py)) - .unwrap(); - unsafe { PyDateTime_IMPORT() } - - assert_check_only!(PyDate_Check, PyDate_CheckExact, obj); - assert_check_exact!(PyDateTime_Check, PyDateTime_CheckExact, obj); - assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_obj); - assert_check_only!(PyDateTime_Check, PyDateTime_CheckExact, sub_sub_obj); - assert!(obj.is_instance_of::()); - assert!(!obj.is_instance_of::()); - assert!(obj.is_instance_of::()); - }); -} - -#[test] -fn test_delta_check() { - Python::with_gil(|py| { - let (obj, sub_obj, sub_sub_obj) = _get_subclasses(py, "timedelta", "1, -3").unwrap(); - unsafe { PyDateTime_IMPORT() } - - assert_check_exact!(PyDelta_Check, PyDelta_CheckExact, obj); - assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_obj); - assert_check_only!(PyDelta_Check, PyDelta_CheckExact, sub_sub_obj); - }); -} - -#[test] -fn test_datetime_utc() { - use assert_approx_eq::assert_approx_eq; - use pyo3::types::PyDateTime; - - Python::with_gil(|py| { - let utc = timezone_utc(py); - - let dt = PyDateTime::new(py, 2018, 1, 1, 0, 0, 0, 0, Some(&utc)).unwrap(); - - let locals = [("dt", dt)].into_py_dict(py).unwrap(); - - let offset: f32 = py - .eval( - ffi::c_str!("dt.utcoffset().total_seconds()"), - None, - Some(&locals), - ) - .unwrap() - .extract() - .unwrap(); - assert_approx_eq!(offset, 0f32); - }); -} - -static INVALID_DATES: &[(i32, u8, u8)] = &[ - (-1, 1, 1), - (0, 1, 1), - (10000, 1, 1), - (2 << 30, 1, 1), - (2018, 0, 1), - (2018, 13, 1), - (2018, 1, 0), - (2017, 2, 29), - (2018, 1, 32), -]; - -static INVALID_TIMES: &[(u8, u8, u8, u32)] = - &[(25, 0, 0, 0), (255, 0, 0, 0), (0, 60, 0, 0), (0, 0, 61, 0)]; - -#[test] -fn test_pydate_out_of_bounds() { - use pyo3::types::PyDate; - - Python::with_gil(|py| { - for val in INVALID_DATES { - let (year, month, day) = val; - let dt = PyDate::new(py, *year, *month, *day); - dt.unwrap_err(); - } - }); -} - -#[test] -fn test_pytime_out_of_bounds() { - use pyo3::types::PyTime; - - Python::with_gil(|py| { - for val in INVALID_TIMES { - let (hour, minute, second, microsecond) = val; - let dt = PyTime::new(py, *hour, *minute, *second, *microsecond, None); - dt.unwrap_err(); - } - }); -} - -#[test] -fn test_pydatetime_out_of_bounds() { - use pyo3::types::PyDateTime; - use std::iter; - - Python::with_gil(|py| { - let valid_time = (0, 0, 0, 0); - let valid_date = (2018, 1, 1); - - let invalid_dates = INVALID_DATES.iter().zip(iter::repeat(&valid_time)); - let invalid_times = iter::repeat(&valid_date).zip(INVALID_TIMES.iter()); - - let vals = invalid_dates.chain(invalid_times); - - for val in vals { - let (date, time) = val; - let (year, month, day) = date; - let (hour, minute, second, microsecond) = time; - let dt = PyDateTime::new( - py, - *year, - *month, - *day, - *hour, - *minute, - *second, - *microsecond, - None, - ); - dt.unwrap_err(); - } - }); -} diff --git a/vendor/pyo3/tests/test_datetime_import.rs b/vendor/pyo3/tests/test_datetime_import.rs deleted file mode 100644 index cac4908bba..0000000000 --- a/vendor/pyo3/tests/test_datetime_import.rs +++ /dev/null @@ -1,28 +0,0 @@ -#![cfg(not(Py_LIMITED_API))] - -use pyo3::{prelude::*, types::PyDate}; -use tempfile::Builder; - -#[test] -#[should_panic(expected = "module 'datetime' has no attribute 'datetime_CAPI'")] -fn test_bad_datetime_module_panic() { - // Create an empty temporary directory - // with an empty "datetime" module which we'll put on the sys.path - let tmpdir = Builder::new() - .prefix("pyo3_test_data_check") - .tempdir() - .unwrap(); - std::fs::File::create(tmpdir.path().join("datetime.py")).unwrap(); - - Python::with_gil(|py: Python<'_>| { - let sys = py.import("sys").unwrap(); - sys.getattr("path") - .unwrap() - .call_method1("insert", (0, tmpdir.path())) - .unwrap(); - - // This should panic because the "datetime" module is empty - PyDate::new(py, 2018, 1, 1).unwrap(); - }); - tmpdir.close().unwrap(); -} diff --git a/vendor/pyo3/tests/test_declarative_module.rs b/vendor/pyo3/tests/test_declarative_module.rs deleted file mode 100644 index 93e0e1366f..0000000000 --- a/vendor/pyo3/tests/test_declarative_module.rs +++ /dev/null @@ -1,241 +0,0 @@ -#![cfg(feature = "macros")] - -use std::sync::Once; - -use pyo3::create_exception; -use pyo3::exceptions::PyException; -use pyo3::prelude::*; -use pyo3::sync::{GILOnceCell, OnceExt}; - -#[path = "../src/tests/common.rs"] -mod common; - -mod some_module { - use pyo3::create_exception; - use pyo3::exceptions::PyException; - use pyo3::prelude::*; - - #[pyclass] - pub struct SomePyClass; - - create_exception!(some_module, SomeException, PyException); -} - -#[pyclass] -struct ValueClass { - value: usize, -} - -#[pymethods] -impl ValueClass { - #[new] - fn new(value: usize) -> Self { - Self { value } - } -} - -#[pyclass(module = "module")] -pub struct LocatedClass {} - -#[pyfunction] -fn double(x: usize) -> usize { - x * 2 -} - -create_exception!( - declarative_module, - MyError, - PyException, - "Some description." -); - -#[pymodule(submodule)] -mod external_submodule {} - -/// A module written using declarative syntax. -#[pymodule] -mod declarative_module { - #[pymodule_export] - use super::declarative_submodule; - #[pymodule_export] - // This is not a real constraint but to test cfg attribute support - #[cfg(not(Py_LIMITED_API))] - use super::LocatedClass; - use super::*; - #[pymodule_export] - use super::{declarative_module2, double, MyError, ValueClass as Value}; - - // test for #4036 - #[pymodule_export] - use super::some_module::SomePyClass; - - // test for #4036 - #[pymodule_export] - use super::some_module::SomeException; - - #[pymodule_export] - use super::external_submodule; - - #[pymodule] - mod inner { - use super::*; - - #[pyfunction] - fn triple(x: usize) -> usize { - x * 3 - } - - #[pyclass(name = "Struct")] - struct Struct; - - #[pymethods] - impl Struct { - #[new] - fn new() -> Self { - Self - } - } - - #[pyclass(module = "foo")] - struct StructInCustomModule; - - #[pyclass(eq, eq_int, name = "Enum")] - #[derive(PartialEq)] - enum Enum { - A, - B, - } - - #[pyclass(eq, eq_int, module = "foo")] - #[derive(PartialEq)] - enum EnumInCustomModule { - A, - B, - } - } - - #[pymodule] - #[pyo3(module = "custom_root")] - mod inner_custom_root { - use super::*; - - #[pyclass] - struct Struct; - } - - #[pyo3::prelude::pymodule] - mod full_path_inner {} - - #[pymodule_init] - fn init(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add("double2", m.getattr("double")?) - } -} - -#[pyfunction] -fn double_value(v: &ValueClass) -> usize { - v.value * 2 -} - -#[pymodule] -mod declarative_submodule { - #[pymodule_export] - use super::{double, double_value}; -} - -#[pymodule(name = "declarative_module_renamed")] -mod declarative_module2 { - #[pymodule_export] - use super::double; -} - -fn declarative_module(py: Python<'_>) -> &Bound<'_, PyModule> { - static MODULE: GILOnceCell> = GILOnceCell::new(); - static ONCE: Once = Once::new(); - - // Guarantee that the module is only ever initialized once; GILOnceCell can race. - // TODO: use OnceLock when MSRV >= 1.70 - ONCE.call_once_py_attached(py, || { - MODULE - .set(py, pyo3::wrap_pymodule!(declarative_module)(py)) - .expect("only ever set once"); - }); - - MODULE.get(py).expect("once is completed").bind(py) -} - -#[test] -fn test_declarative_module() { - Python::with_gil(|py| { - let m = declarative_module(py); - py_assert!( - py, - m, - "m.__doc__ == 'A module written using declarative syntax.'" - ); - - py_assert!(py, m, "m.double(2) == 4"); - py_assert!(py, m, "m.inner.triple(3) == 9"); - py_assert!(py, m, "m.declarative_submodule.double(4) == 8"); - py_assert!( - py, - m, - "m.declarative_submodule.double_value(m.ValueClass(1)) == 2" - ); - py_assert!(py, m, "str(m.MyError('foo')) == 'foo'"); - py_assert!(py, m, "m.declarative_module_renamed.double(2) == 4"); - #[cfg(Py_LIMITED_API)] - py_assert!(py, m, "not hasattr(m, 'LocatedClass')"); - #[cfg(not(Py_LIMITED_API))] - py_assert!(py, m, "hasattr(m, 'LocatedClass')"); - py_assert!(py, m, "isinstance(m.inner.Struct(), m.inner.Struct)"); - py_assert!(py, m, "isinstance(m.inner.Enum.A, m.inner.Enum)"); - py_assert!(py, m, "hasattr(m, 'external_submodule')") - }) -} - -#[pymodule] -mod r#type { - #[pymodule_export] - use super::double; -} - -#[test] -fn test_raw_ident_module() { - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(r#type)(py).into_bound(py); - py_assert!(py, m, "m.double(2) == 4"); - }) -} - -#[test] -fn test_module_names() { - Python::with_gil(|py| { - let m = declarative_module(py); - py_assert!( - py, - m, - "m.inner.Struct.__module__ == 'declarative_module.inner'" - ); - py_assert!(py, m, "m.inner.StructInCustomModule.__module__ == 'foo'"); - py_assert!( - py, - m, - "m.inner.Enum.__module__ == 'declarative_module.inner'" - ); - py_assert!(py, m, "m.inner.EnumInCustomModule.__module__ == 'foo'"); - py_assert!( - py, - m, - "m.inner_custom_root.Struct.__module__ == 'custom_root.inner_custom_root'" - ); - }) -} - -#[test] -fn test_inner_module_full_path() { - Python::with_gil(|py| { - let m = declarative_module(py); - py_assert!(py, m, "m.full_path_inner"); - }) -} diff --git a/vendor/pyo3/tests/test_default_impls.rs b/vendor/pyo3/tests/test_default_impls.rs deleted file mode 100644 index 670772369f..0000000000 --- a/vendor/pyo3/tests/test_default_impls.rs +++ /dev/null @@ -1,46 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[path = "../src/tests/common.rs"] -mod common; - -// Test default generated __repr__. -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum TestDefaultRepr { - Var, -} - -#[test] -fn test_default_slot_exists() { - Python::with_gil(|py| { - let test_object = Py::new(py, TestDefaultRepr::Var).unwrap(); - py_assert!( - py, - test_object, - "repr(test_object) == 'TestDefaultRepr.Var'" - ); - }) -} - -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum OverrideSlot { - Var, -} - -#[pymethods] -impl OverrideSlot { - fn __repr__(&self) -> &str { - "overridden" - } -} - -#[test] -fn test_override_slot() { - Python::with_gil(|py| { - let test_object = Py::new(py, OverrideSlot::Var).unwrap(); - py_assert!(py, test_object, "repr(test_object) == 'overridden'"); - }) -} diff --git a/vendor/pyo3/tests/test_enum.rs b/vendor/pyo3/tests/test_enum.rs deleted file mode 100644 index 40c5f4681a..0000000000 --- a/vendor/pyo3/tests/test_enum.rs +++ /dev/null @@ -1,406 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::py_run; -use pyo3::types::PyString; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum MyEnum { - Variant, - OtherVariant, -} - -#[test] -fn test_enum_class_attr() { - Python::with_gil(|py| { - let my_enum = py.get_type::(); - let var = Py::new(py, MyEnum::Variant).unwrap(); - py_assert!(py, my_enum var, "my_enum.Variant == var"); - }) -} - -#[pyfunction] -fn return_enum() -> MyEnum { - MyEnum::Variant -} - -#[test] -fn test_return_enum() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(return_enum)(py).unwrap(); - let mynum = py.get_type::(); - - py_run!(py, f mynum, "assert f() == mynum.Variant") - }); -} - -#[pyfunction] -fn enum_arg(e: MyEnum) { - assert_eq!(MyEnum::OtherVariant, e) -} - -#[test] -fn test_enum_arg() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(enum_arg)(py).unwrap(); - let mynum = py.get_type::(); - - py_run!(py, f mynum, "f(mynum.OtherVariant)") - }) -} - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -enum CustomDiscriminant { - One = 1, - Two = 2, -} - -#[test] -fn test_custom_discriminant() { - Python::with_gil(|py| { - #[allow(non_snake_case)] - let CustomDiscriminant = py.get_type::(); - let one = Py::new(py, CustomDiscriminant::One).unwrap(); - let two = Py::new(py, CustomDiscriminant::Two).unwrap(); - py_run!(py, CustomDiscriminant one two, r#" - assert CustomDiscriminant.One == one - assert CustomDiscriminant.Two == two - assert one != two - "#); - }) -} - -#[test] -fn test_enum_to_int() { - Python::with_gil(|py| { - let one = Py::new(py, CustomDiscriminant::One).unwrap(); - py_assert!(py, one, "int(one) == 1"); - let v = Py::new(py, MyEnum::Variant).unwrap(); - let v_value = MyEnum::Variant as isize; - py_run!(py, v v_value, "int(v) == v_value"); - }) -} - -#[test] -fn test_enum_compare_int() { - Python::with_gil(|py| { - let one = Py::new(py, CustomDiscriminant::One).unwrap(); - py_run!( - py, - one, - r#" - assert one == 1 - assert 1 == one - assert one != 2 - "# - ) - }) -} - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -#[repr(u8)] -enum SmallEnum { - V = 1, -} - -#[test] -fn test_enum_compare_int_no_throw_when_overflow() { - Python::with_gil(|py| { - let v = Py::new(py, SmallEnum::V).unwrap(); - py_assert!(py, v, "v != 1<<30") - }) -} - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -#[repr(usize)] -#[allow(clippy::enum_clike_unportable_variant)] -enum BigEnum { - V = usize::MAX, -} - -#[test] -fn test_big_enum_no_overflow() { - Python::with_gil(|py| { - let usize_max = usize::MAX; - let v = Py::new(py, BigEnum::V).unwrap(); - - py_assert!(py, usize_max v, "v == usize_max"); - py_assert!(py, usize_max v, "int(v) == usize_max"); - }) -} - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -#[repr(u16, align(8))] -enum TestReprParse { - V, -} - -#[test] -fn test_repr_parse() { - assert_eq!(std::mem::align_of::(), 8); -} - -#[pyclass(eq, eq_int, name = "MyEnum")] -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum RenameEnum { - Variant, -} - -#[test] -fn test_rename_enum_repr_correct() { - Python::with_gil(|py| { - let var1 = Py::new(py, RenameEnum::Variant).unwrap(); - py_assert!(py, var1, "repr(var1) == 'MyEnum.Variant'"); - }) -} - -#[pyclass(eq, eq_int)] -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum RenameVariantEnum { - #[pyo3(name = "VARIANT")] - Variant, -} - -#[test] -fn test_rename_variant_repr_correct() { - Python::with_gil(|py| { - let var1 = Py::new(py, RenameVariantEnum::Variant).unwrap(); - py_assert!(py, var1, "repr(var1) == 'RenameVariantEnum.VARIANT'"); - }) -} - -#[pyclass(eq, eq_int, rename_all = "SCREAMING_SNAKE_CASE")] -#[derive(Debug, PartialEq, Eq, Clone)] -#[allow(clippy::enum_variant_names)] -enum RenameAllVariantsEnum { - VariantOne, - VariantTwo, - #[pyo3(name = "VariantThree")] - VariantFour, -} - -#[test] -fn test_renaming_all_enum_variants() { - Python::with_gil(|py| { - let enum_obj = py.get_type::(); - py_assert!(py, enum_obj, "enum_obj.VARIANT_ONE == enum_obj.VARIANT_ONE"); - py_assert!(py, enum_obj, "enum_obj.VARIANT_TWO == enum_obj.VARIANT_TWO"); - py_assert!( - py, - enum_obj, - "enum_obj.VariantThree == enum_obj.VariantThree" - ); - }); -} - -#[pyclass(module = "custom_module")] -#[derive(Debug)] -enum CustomModuleComplexEnum { - Variant(), - Py(Py), -} - -#[test] -fn test_custom_module() { - Python::with_gil(|py| { - let enum_obj = py.get_type::(); - py_assert!( - py, - enum_obj, - "enum_obj.Variant.__module__ == 'custom_module'" - ); - }); -} - -#[pyclass(eq)] -#[derive(Debug, Clone, PartialEq)] -pub enum EqOnly { - VariantA, - VariantB, -} - -#[test] -fn test_simple_enum_eq_only() { - Python::with_gil(|py| { - let var1 = Py::new(py, EqOnly::VariantA).unwrap(); - let var2 = Py::new(py, EqOnly::VariantA).unwrap(); - let var3 = Py::new(py, EqOnly::VariantB).unwrap(); - py_assert!(py, var1 var2, "var1 == var2"); - py_assert!(py, var1 var3, "var1 != var3"); - }) -} - -#[pyclass(frozen, eq, eq_int, hash)] -#[derive(PartialEq, Hash)] -enum SimpleEnumWithHash { - A, - B, -} - -#[test] -fn test_simple_enum_with_hash() { - Python::with_gil(|py| { - use pyo3::types::IntoPyDict; - let class = SimpleEnumWithHash::A; - let hash = { - use std::hash::{Hash, Hasher}; - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - class.hash(&mut hasher); - hasher.finish() as isize - }; - - let env = [ - ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), - ] - .into_py_dict(py) - .unwrap(); - - py_assert!(py, *env, "hash(obj) == hsh"); - }); -} - -#[pyclass(eq, hash)] -#[derive(PartialEq, Hash)] -enum ComplexEnumWithHash { - A(u32), - B { msg: String }, -} - -#[test] -fn test_complex_enum_with_hash() { - Python::with_gil(|py| { - use pyo3::types::IntoPyDict; - let class = ComplexEnumWithHash::B { - msg: String::from("Hello"), - }; - let hash = { - use std::hash::{Hash, Hasher}; - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - class.hash(&mut hasher); - hasher.finish() as isize - }; - - let env = [ - ("obj", Py::new(py, class).unwrap().into_any()), - ("hsh", hash.into_pyobject(py).unwrap().into_any().unbind()), - ] - .into_py_dict(py) - .unwrap(); - - py_assert!(py, *env, "hash(obj) == hsh"); - }); -} - -#[allow(deprecated)] -mod deprecated { - use crate::py_assert; - use pyo3::prelude::*; - use pyo3::py_run; - - #[pyclass] - #[derive(Debug, PartialEq, Eq, Clone)] - pub enum MyEnum { - Variant, - OtherVariant, - } - - #[test] - fn test_enum_eq_enum() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnum::Variant).unwrap(); - let var2 = Py::new(py, MyEnum::Variant).unwrap(); - let other_var = Py::new(py, MyEnum::OtherVariant).unwrap(); - py_assert!(py, var1 var2, "var1 == var2"); - py_assert!(py, var1 other_var, "var1 != other_var"); - py_assert!(py, var1 var2, "(var1 != var2) == False"); - }) - } - - #[test] - fn test_enum_eq_incomparable() { - Python::with_gil(|py| { - let var1 = Py::new(py, MyEnum::Variant).unwrap(); - py_assert!(py, var1, "(var1 == 'foo') == False"); - py_assert!(py, var1, "(var1 != 'foo') == True"); - }) - } - - #[pyclass] - enum CustomDiscriminant { - One = 1, - Two = 2, - } - - #[test] - fn test_custom_discriminant() { - Python::with_gil(|py| { - #[allow(non_snake_case)] - let CustomDiscriminant = py.get_type::(); - let one = Py::new(py, CustomDiscriminant::One).unwrap(); - let two = Py::new(py, CustomDiscriminant::Two).unwrap(); - py_run!(py, CustomDiscriminant one two, r#" - assert CustomDiscriminant.One == one - assert CustomDiscriminant.Two == two - assert CustomDiscriminant.One == 1 - assert CustomDiscriminant.Two == 2 - assert one != two - assert CustomDiscriminant.One != 2 - assert CustomDiscriminant.Two != 1 - "#); - }) - } -} - -#[test] -fn custom_eq() { - #[pyclass(frozen)] - #[derive(PartialEq)] - pub enum CustomPyEq { - A, - B, - } - - #[pymethods] - impl CustomPyEq { - fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool { - if let Ok(rhs) = other.downcast::() { - rhs.to_cow().map_or(false, |rhs| self.__str__() == rhs) - } else if let Ok(rhs) = other.downcast::() { - self == rhs.get() - } else { - false - } - } - - fn __str__(&self) -> String { - match self { - CustomPyEq::A => "A".to_string(), - CustomPyEq::B => "B".to_string(), - } - } - } - - Python::with_gil(|py| { - let a = Bound::new(py, CustomPyEq::A).unwrap(); - let b = Bound::new(py, CustomPyEq::B).unwrap(); - - assert!(a.as_any().eq(&a).unwrap()); - assert!(a.as_any().eq("A").unwrap()); - assert!(a.as_any().ne(&b).unwrap()); - assert!(a.as_any().ne("B").unwrap()); - - assert!(b.as_any().eq(&b).unwrap()); - assert!(b.as_any().eq("B").unwrap()); - assert!(b.as_any().ne(&a).unwrap()); - assert!(b.as_any().ne("A").unwrap()); - }) -} diff --git a/vendor/pyo3/tests/test_exceptions.rs b/vendor/pyo3/tests/test_exceptions.rs deleted file mode 100644 index 777c2aa22d..0000000000 --- a/vendor/pyo3/tests/test_exceptions.rs +++ /dev/null @@ -1,128 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::{exceptions, py_run}; -use std::error::Error; -use std::fmt; -#[cfg(not(target_os = "windows"))] -use std::fs::File; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyfunction] -#[cfg(not(target_os = "windows"))] -fn fail_to_open_file() -> PyResult<()> { - File::open("not_there.txt")?; - Ok(()) -} - -#[test] -#[cfg_attr(target_arch = "wasm32", ignore)] // Not sure why this fails. -#[cfg(not(target_os = "windows"))] -fn test_filenotfounderror() { - Python::with_gil(|py| { - let fail_to_open_file = wrap_pyfunction!(fail_to_open_file)(py).unwrap(); - - py_run!( - py, - fail_to_open_file, - r#" - try: - fail_to_open_file() - except FileNotFoundError as e: - assert str(e) == "No such file or directory (os error 2)" - "# - ); - }); -} - -#[derive(Debug)] -struct CustomError; - -impl Error for CustomError {} - -impl fmt::Display for CustomError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Oh no!") - } -} - -impl std::convert::From for PyErr { - fn from(err: CustomError) -> PyErr { - exceptions::PyOSError::new_err(err.to_string()) - } -} - -fn fail_with_custom_error() -> Result<(), CustomError> { - Err(CustomError) -} - -#[pyfunction] -fn call_fail_with_custom_error() -> PyResult<()> { - fail_with_custom_error()?; - Ok(()) -} - -#[test] -fn test_custom_error() { - Python::with_gil(|py| { - let call_fail_with_custom_error = - wrap_pyfunction!(call_fail_with_custom_error)(py).unwrap(); - - py_run!( - py, - call_fail_with_custom_error, - r#" - try: - call_fail_with_custom_error() - except OSError as e: - assert str(e) == "Oh no!" - "# - ); - }); -} - -#[test] -fn test_exception_nosegfault() { - use std::net::TcpListener; - fn io_err() -> PyResult<()> { - TcpListener::bind("no:address")?; - Ok(()) - } - fn parse_int() -> PyResult<()> { - "@_@".parse::()?; - Ok(()) - } - assert!(io_err().is_err()); - assert!(parse_int().is_err()); -} - -#[test] -#[cfg(all(Py_3_8, not(Py_GIL_DISABLED)))] -fn test_write_unraisable() { - use common::UnraisableCapture; - use pyo3::{exceptions::PyRuntimeError, ffi, types::PyNotImplemented}; - - Python::with_gil(|py| { - let capture = UnraisableCapture::install(py); - - assert!(capture.borrow(py).capture.is_none()); - - let err = PyRuntimeError::new_err("foo"); - err.write_unraisable(py, None); - - let (err, object) = capture.borrow_mut(py).capture.take().unwrap(); - assert_eq!(err.to_string(), "RuntimeError: foo"); - assert!(object.is_none(py)); - - let err = PyRuntimeError::new_err("bar"); - err.write_unraisable(py, Some(&PyNotImplemented::get(py))); - - let (err, object) = capture.borrow_mut(py).capture.take().unwrap(); - assert_eq!(err.to_string(), "RuntimeError: bar"); - assert!(object.as_ptr() == unsafe { ffi::Py_NotImplemented() }); - - capture.borrow_mut(py).uninstall(py); - }); -} diff --git a/vendor/pyo3/tests/test_field_cfg.rs b/vendor/pyo3/tests/test_field_cfg.rs deleted file mode 100644 index 1766cadb3d..0000000000 --- a/vendor/pyo3/tests/test_field_cfg.rs +++ /dev/null @@ -1,53 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[pyclass] -struct CfgClass { - #[pyo3(get, set)] - #[cfg(any())] - pub a: u32, - #[pyo3(get, set)] - // This is always true - #[cfg(any( - target_family = "unix", - target_family = "windows", - target_family = "wasm" - ))] - pub b: u32, -} - -#[pyclass(eq, eq_int)] -#[derive(PartialEq)] -enum CfgSimpleEnum { - #[cfg(any())] - DisabledVariant, - #[cfg(not(any()))] - EnabledVariant, -} - -#[test] -fn test_cfg() { - Python::with_gil(|py| { - let cfg = CfgClass { b: 3 }; - let py_cfg = Py::new(py, cfg).unwrap(); - assert!(py_cfg.bind(py).getattr("a").is_err()); - let b: u32 = py_cfg.bind(py).getattr("b").unwrap().extract().unwrap(); - assert_eq!(b, 3); - }); -} - -#[test] -fn test_cfg_simple_enum() { - Python::with_gil(|py| { - let simple = py.get_type::(); - pyo3::py_run!( - py, - simple, - r#" - assert hasattr(simple, "EnabledVariant") - assert not hasattr(simple, "DisabledVariant") - "# - ); - }) -} diff --git a/vendor/pyo3/tests/test_frompy_intopy_roundtrip.rs b/vendor/pyo3/tests/test_frompy_intopy_roundtrip.rs deleted file mode 100644 index b17320fa43..0000000000 --- a/vendor/pyo3/tests/test_frompy_intopy_roundtrip.rs +++ /dev/null @@ -1,255 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::types::{PyDict, PyString}; -use pyo3::{prelude::*, IntoPyObject, IntoPyObjectRef}; -use std::collections::HashMap; -use std::hash::Hash; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -#[derive(Debug, Clone, IntoPyObject, IntoPyObjectRef, FromPyObject)] -pub struct A<'py> { - #[pyo3(item)] - s: String, - #[pyo3(item)] - t: Bound<'py, PyString>, - #[pyo3(item("foo"))] - p: Bound<'py, PyAny>, -} - -#[test] -fn test_named_fields_struct() { - Python::with_gil(|py| { - let a = A { - s: "Hello".into(), - t: PyString::new(py, "World"), - p: 42i32.into_pyobject(py).unwrap().into_any(), - }; - let pya = (&a).into_pyobject(py).unwrap(); - let new_a = pya.extract::>().unwrap(); - - assert_eq!(a.s, new_a.s); - assert_eq!(a.t.to_cow().unwrap(), new_a.t.to_cow().unwrap()); - assert_eq!( - a.p.extract::().unwrap(), - new_a.p.extract::().unwrap() - ); - - let pya = a.clone().into_pyobject(py).unwrap(); - let new_a = pya.extract::>().unwrap(); - - assert_eq!(a.s, new_a.s); - assert_eq!(a.t.to_cow().unwrap(), new_a.t.to_cow().unwrap()); - assert_eq!( - a.p.extract::().unwrap(), - new_a.p.extract::().unwrap() - ); - }); -} - -#[derive(Debug, Clone, PartialEq, IntoPyObject, IntoPyObjectRef, FromPyObject)] -#[pyo3(transparent)] -pub struct B { - test: String, -} - -#[test] -fn test_transparent_named_field_struct() { - Python::with_gil(|py| { - let b = B { - test: "test".into(), - }; - let pyb = (&b).into_pyobject(py).unwrap(); - let new_b = pyb.extract::().unwrap(); - assert_eq!(b, new_b); - - let pyb = b.clone().into_pyobject(py).unwrap(); - let new_b = pyb.extract::().unwrap(); - assert_eq!(b, new_b); - }); -} - -#[derive(Debug, Clone, PartialEq, IntoPyObject, IntoPyObjectRef, FromPyObject)] -#[pyo3(transparent)] -pub struct D { - test: T, -} - -#[test] -fn test_generic_transparent_named_field_struct() { - Python::with_gil(|py| { - let d = D { - test: String::from("test"), - }; - let pyd = (&d).into_pyobject(py).unwrap(); - let new_d = pyd.extract::>().unwrap(); - assert_eq!(d, new_d); - - let d = D { test: 1usize }; - let pyd = (&d).into_pyobject(py).unwrap(); - let new_d = pyd.extract::>().unwrap(); - assert_eq!(d, new_d); - - let d = D { - test: String::from("test"), - }; - let pyd = d.clone().into_pyobject(py).unwrap(); - let new_d = pyd.extract::>().unwrap(); - assert_eq!(d, new_d); - - let d = D { test: 1usize }; - let pyd = d.clone().into_pyobject(py).unwrap(); - let new_d = pyd.extract::>().unwrap(); - assert_eq!(d, new_d); - }); -} - -#[derive(Debug, IntoPyObject, IntoPyObjectRef, FromPyObject)] -pub struct GenericWithBound(HashMap); - -#[test] -fn test_generic_with_bound() { - Python::with_gil(|py| { - let mut hash_map = HashMap::::new(); - hash_map.insert("1".into(), 1); - hash_map.insert("2".into(), 2); - let map = GenericWithBound(hash_map); - let py_map = (&map).into_pyobject(py).unwrap(); - assert_eq!(py_map.len(), 2); - assert_eq!( - py_map - .get_item("1") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - assert_eq!( - py_map - .get_item("2") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - assert!(py_map.get_item("3").unwrap().is_none()); - - let py_map = map.into_pyobject(py).unwrap(); - assert_eq!(py_map.len(), 2); - assert_eq!( - py_map - .get_item("1") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - assert_eq!( - py_map - .get_item("2") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - assert!(py_map.get_item("3").unwrap().is_none()); - }); -} - -#[derive(Debug, Clone, PartialEq, IntoPyObject, IntoPyObjectRef, FromPyObject)] -pub struct Tuple(String, usize); - -#[test] -fn test_tuple_struct() { - Python::with_gil(|py| { - let tup = Tuple(String::from("test"), 1); - let tuple = (&tup).into_pyobject(py).unwrap(); - let new_tup = tuple.extract::().unwrap(); - assert_eq!(tup, new_tup); - - let tuple = tup.clone().into_pyobject(py).unwrap(); - let new_tup = tuple.extract::().unwrap(); - assert_eq!(tup, new_tup); - }); -} - -#[derive(Debug, Clone, PartialEq, IntoPyObject, IntoPyObjectRef, FromPyObject)] -pub struct TransparentTuple(String); - -#[test] -fn test_transparent_tuple_struct() { - Python::with_gil(|py| { - let tup = TransparentTuple(String::from("test")); - let tuple = (&tup).into_pyobject(py).unwrap(); - let new_tup = tuple.extract::().unwrap(); - assert_eq!(tup, new_tup); - - let tuple = tup.clone().into_pyobject(py).unwrap(); - let new_tup = tuple.extract::().unwrap(); - assert_eq!(tup, new_tup); - }); -} - -#[derive(Debug, Clone, PartialEq, IntoPyObject, IntoPyObjectRef, FromPyObject)] -pub enum Foo { - TupleVar(usize, String), - StructVar { - #[pyo3(item)] - test: char, - }, - #[pyo3(transparent)] - TransparentTuple(usize), - #[pyo3(transparent)] - TransparentStructVar { - a: Option, - }, -} - -#[test] -fn test_enum() { - Python::with_gil(|py| { - let tuple_var = Foo::TupleVar(1, "test".into()); - let foo = (&tuple_var).into_pyobject(py).unwrap(); - assert_eq!(tuple_var, foo.extract::().unwrap()); - - let foo = tuple_var.clone().into_pyobject(py).unwrap(); - assert_eq!(tuple_var, foo.extract::().unwrap()); - - let struct_var = Foo::StructVar { test: 'b' }; - let foo = (&struct_var) - .into_pyobject(py) - .unwrap() - .downcast_into::() - .unwrap(); - assert_eq!(struct_var, foo.extract::().unwrap()); - - let foo = struct_var - .clone() - .into_pyobject(py) - .unwrap() - .downcast_into::() - .unwrap(); - - assert_eq!(struct_var, foo.extract::().unwrap()); - - let transparent_tuple = Foo::TransparentTuple(1); - let foo = (&transparent_tuple).into_pyobject(py).unwrap(); - assert_eq!(transparent_tuple, foo.extract::().unwrap()); - - let foo = transparent_tuple.clone().into_pyobject(py).unwrap(); - assert_eq!(transparent_tuple, foo.extract::().unwrap()); - - let transparent_struct_var = Foo::TransparentStructVar { a: None }; - let foo = (&transparent_struct_var).into_pyobject(py).unwrap(); - assert_eq!(transparent_struct_var, foo.extract::().unwrap()); - - let foo = transparent_struct_var.clone().into_pyobject(py).unwrap(); - assert_eq!(transparent_struct_var, foo.extract::().unwrap()); - }); -} diff --git a/vendor/pyo3/tests/test_frompyobject.rs b/vendor/pyo3/tests/test_frompyobject.rs deleted file mode 100644 index 2192caf1f7..0000000000 --- a/vendor/pyo3/tests/test_frompyobject.rs +++ /dev/null @@ -1,688 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::exceptions::PyValueError; -use pyo3::prelude::*; -use pyo3::types::{IntoPyDict, PyDict, PyList, PyString, PyTuple}; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -/// Helper function that concatenates the error message from -/// each error in the traceback into a single string that can -/// be tested. -fn extract_traceback(py: Python<'_>, mut error: PyErr) -> String { - let mut error_msg = error.to_string(); - while let Some(cause) = error.cause(py) { - error_msg.push_str(": "); - error_msg.push_str(&cause.to_string()); - error = cause - } - error_msg -} - -#[derive(Debug, FromPyObject)] -pub struct A<'py> { - #[pyo3(attribute)] - s: String, - #[pyo3(item)] - t: Bound<'py, PyString>, - #[pyo3(attribute("foo"))] - p: Bound<'py, PyAny>, -} - -#[pyclass] -pub struct PyA { - #[pyo3(get)] - s: String, - #[pyo3(get)] - foo: Option, -} - -#[pymethods] -impl PyA { - fn __getitem__(&self, key: String) -> pyo3::PyResult { - if key == "t" { - Ok("bar".into()) - } else { - Err(PyValueError::new_err("Failed")) - } - } -} - -#[test] -fn test_named_fields_struct() { - Python::with_gil(|py| { - let pya = PyA { - s: "foo".into(), - foo: None, - }; - let py_c = Py::new(py, pya).unwrap(); - let a = py_c - .extract::>(py) - .expect("Failed to extract A from PyA"); - assert_eq!(a.s, "foo"); - assert_eq!(a.t.to_string_lossy(), "bar"); - assert!(a.p.is_none()); - }); -} - -#[derive(Debug, FromPyObject)] -#[pyo3(transparent)] -pub struct B { - test: String, -} - -#[test] -fn test_transparent_named_field_struct() { - Python::with_gil(|py| { - let test = "test".into_pyobject(py).unwrap(); - let b = test - .extract::() - .expect("Failed to extract B from String"); - assert_eq!(b.test, "test"); - let test = 1i32.into_pyobject(py).unwrap(); - let b = test.extract::(); - assert!(b.is_err()); - }); -} - -#[derive(Debug, FromPyObject)] -#[pyo3(transparent)] -pub struct D { - test: T, -} - -#[test] -fn test_generic_transparent_named_field_struct() { - Python::with_gil(|py| { - let test = "test".into_pyobject(py).unwrap(); - let d = test - .extract::>() - .expect("Failed to extract D from String"); - assert_eq!(d.test, "test"); - let test = 1usize.into_pyobject(py).unwrap(); - let d = test - .extract::>() - .expect("Failed to extract D from String"); - assert_eq!(d.test, 1); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct GenericWithBound(std::collections::HashMap); - -#[test] -fn test_generic_with_bound() { - Python::with_gil(|py| { - let dict = [("1", 1), ("2", 2)].into_py_dict(py).unwrap(); - let map = dict.extract::>().unwrap().0; - assert_eq!(map.len(), 2); - assert_eq!(map["1"], 1); - assert_eq!(map["2"], 2); - assert!(!map.contains_key("3")); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct E { - test: T, - test2: T2, -} - -#[pyclass] -#[derive(Clone)] -pub struct PyE { - #[pyo3(get)] - test: String, - #[pyo3(get)] - test2: usize, -} - -#[test] -fn test_generic_named_fields_struct() { - Python::with_gil(|py| { - let pye = PyE { - test: "test".into(), - test2: 2, - } - .into_pyobject(py) - .unwrap(); - - let e = pye - .extract::>() - .expect("Failed to extract E from PyE"); - assert_eq!(e.test, "test"); - assert_eq!(e.test2, 2); - let e = pye.extract::>(); - assert!(e.is_err()); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct C { - #[pyo3(attribute("test"))] - test: String, -} - -#[test] -fn test_named_field_with_ext_fn() { - Python::with_gil(|py| { - let pyc = PyE { - test: "foo".into(), - test2: 0, - } - .into_pyobject(py) - .unwrap(); - let c = pyc.extract::().expect("Failed to extract C from PyE"); - assert_eq!(c.test, "foo"); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct Tuple(String, usize); - -#[test] -fn test_tuple_struct() { - Python::with_gil(|py| { - let tup = PyTuple::new( - py, - &[ - 1i32.into_pyobject(py).unwrap().into_any(), - "test".into_pyobject(py).unwrap().into_any(), - ], - ) - .unwrap(); - let tup = tup.extract::(); - assert!(tup.is_err()); - let tup = PyTuple::new( - py, - &[ - "test".into_pyobject(py).unwrap().into_any(), - 1i32.into_pyobject(py).unwrap().into_any(), - ], - ) - .unwrap(); - let tup = tup - .extract::() - .expect("Failed to extract Tuple from PyTuple"); - assert_eq!(tup.0, "test"); - assert_eq!(tup.1, 1); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct TransparentTuple(String); - -#[test] -fn test_transparent_tuple_struct() { - Python::with_gil(|py| { - let tup = 1i32.into_pyobject(py).unwrap(); - let tup = tup.extract::(); - assert!(tup.is_err()); - let test = "test".into_pyobject(py).unwrap(); - let tup = test - .extract::() - .expect("Failed to extract TransparentTuple from PyTuple"); - assert_eq!(tup.0, "test"); - }); -} - -#[pyclass] -struct PyBaz { - #[pyo3(get)] - tup: (String, String), - #[pyo3(get)] - e: PyE, -} - -#[derive(Debug, FromPyObject)] -#[allow(dead_code)] -struct Baz { - e: E, - tup: Tuple, -} - -#[test] -fn test_struct_nested_type_errors() { - Python::with_gil(|py| { - let pybaz = PyBaz { - tup: ("test".into(), "test".into()), - e: PyE { - test: "foo".into(), - test2: 0, - }, - } - .into_pyobject(py) - .unwrap(); - - let test = pybaz.extract::>(); - assert!(test.is_err()); - assert_eq!( - extract_traceback(py,test.unwrap_err()), - "TypeError: failed to extract field Baz.tup: TypeError: failed to extract field Tuple.1: \ - TypeError: \'str\' object cannot be interpreted as an integer" - ); - }); -} - -#[test] -fn test_struct_nested_type_errors_with_generics() { - Python::with_gil(|py| { - let pybaz = PyBaz { - tup: ("test".into(), "test".into()), - e: PyE { - test: "foo".into(), - test2: 0, - }, - } - .into_pyobject(py) - .unwrap(); - - let test = pybaz.extract::>(); - assert!(test.is_err()); - assert_eq!( - extract_traceback(py, test.unwrap_err()), - "TypeError: failed to extract field Baz.e: TypeError: failed to extract field E.test: \ - TypeError: \'str\' object cannot be interpreted as an integer", - ); - }); -} - -#[test] -fn test_transparent_struct_error_message() { - Python::with_gil(|py| { - let tup = 1i32.into_pyobject(py).unwrap(); - let tup = tup.extract::(); - assert!(tup.is_err()); - assert_eq!( - extract_traceback(py,tup.unwrap_err()), - "TypeError: failed to extract field B.test: TypeError: \'int\' object cannot be converted \ - to \'PyString\'" - ); - }); -} - -#[test] -fn test_tuple_struct_error_message() { - Python::with_gil(|py| { - let tup = (1, "test").into_pyobject(py).unwrap(); - let tup = tup.extract::(); - assert!(tup.is_err()); - assert_eq!( - extract_traceback(py, tup.unwrap_err()), - "TypeError: failed to extract field Tuple.0: TypeError: \'int\' object cannot be \ - converted to \'PyString\'" - ); - }); -} - -#[test] -fn test_transparent_tuple_error_message() { - Python::with_gil(|py| { - let tup = 1i32.into_pyobject(py).unwrap(); - let tup = tup.extract::(); - assert!(tup.is_err()); - assert_eq!( - extract_traceback(py, tup.unwrap_err()), - "TypeError: failed to extract field TransparentTuple.0: TypeError: 'int' object \ - cannot be converted to 'PyString'", - ); - }); -} - -#[derive(Debug, FromPyObject)] -pub enum Foo<'py> { - TupleVar(usize, String), - StructVar { - test: Bound<'py, PyString>, - }, - #[pyo3(transparent)] - TransparentTuple(usize), - #[pyo3(transparent)] - TransparentStructVar { - a: Option, - }, - StructVarGetAttrArg { - #[pyo3(attribute("bla"))] - a: bool, - }, - StructWithGetItem { - #[pyo3(item)] - a: String, - }, - StructWithGetItemArg { - #[pyo3(item("foo"))] - a: String, - }, -} - -#[pyclass] -pub struct PyBool { - #[pyo3(get)] - bla: bool, -} - -#[test] -fn test_enum() { - Python::with_gil(|py| { - let tup = PyTuple::new( - py, - &[ - 1i32.into_pyobject(py).unwrap().into_any(), - "test".into_pyobject(py).unwrap().into_any(), - ], - ) - .unwrap(); - let f = tup - .extract::>() - .expect("Failed to extract Foo from tuple"); - match f { - Foo::TupleVar(test, test2) => { - assert_eq!(test, 1); - assert_eq!(test2, "test"); - } - _ => panic!("Expected extracting Foo::TupleVar, got {:?}", f), - } - - let pye = PyE { - test: "foo".into(), - test2: 0, - } - .into_pyobject(py) - .unwrap(); - let f = pye - .extract::>() - .expect("Failed to extract Foo from PyE"); - match f { - Foo::StructVar { test } => assert_eq!(test.to_string_lossy(), "foo"), - _ => panic!("Expected extracting Foo::StructVar, got {:?}", f), - } - - let int = 1i32.into_pyobject(py).unwrap(); - let f = int - .extract::>() - .expect("Failed to extract Foo from int"); - match f { - Foo::TransparentTuple(test) => assert_eq!(test, 1), - _ => panic!("Expected extracting Foo::TransparentTuple, got {:?}", f), - } - let none = py.None(); - let f = none - .extract::>(py) - .expect("Failed to extract Foo from int"); - match f { - Foo::TransparentStructVar { a } => assert!(a.is_none()), - _ => panic!("Expected extracting Foo::TransparentStructVar, got {:?}", f), - } - - let pybool = PyBool { bla: true }.into_pyobject(py).unwrap(); - let f = pybool - .extract::>() - .expect("Failed to extract Foo from PyBool"); - match f { - Foo::StructVarGetAttrArg { a } => assert!(a), - _ => panic!("Expected extracting Foo::StructVarGetAttrArg, got {:?}", f), - } - - let dict = PyDict::new(py); - dict.set_item("a", "test").expect("Failed to set item"); - let f = dict - .extract::>() - .expect("Failed to extract Foo from dict"); - match f { - Foo::StructWithGetItem { a } => assert_eq!(a, "test"), - _ => panic!("Expected extracting Foo::StructWithGetItem, got {:?}", f), - } - - let dict = PyDict::new(py); - dict.set_item("foo", "test").expect("Failed to set item"); - let f = dict - .extract::>() - .expect("Failed to extract Foo from dict"); - match f { - Foo::StructWithGetItemArg { a } => assert_eq!(a, "test"), - _ => panic!("Expected extracting Foo::StructWithGetItemArg, got {:?}", f), - } - }); -} - -#[test] -fn test_enum_error() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - let err = dict.extract::>().unwrap_err(); - assert_eq!( - err.to_string(), - "\ -TypeError: failed to extract enum Foo ('TupleVar | StructVar | TransparentTuple | TransparentStructVar | StructVarGetAttrArg | StructWithGetItem | StructWithGetItemArg') -- variant TupleVar (TupleVar): TypeError: 'dict' object cannot be converted to 'PyTuple' -- variant StructVar (StructVar): AttributeError: 'dict' object has no attribute 'test' -- variant TransparentTuple (TransparentTuple): TypeError: failed to extract field Foo::TransparentTuple.0, caused by TypeError: 'dict' object cannot be interpreted as an integer -- variant TransparentStructVar (TransparentStructVar): TypeError: failed to extract field Foo::TransparentStructVar.a, caused by TypeError: 'dict' object cannot be converted to 'PyString' -- variant StructVarGetAttrArg (StructVarGetAttrArg): AttributeError: 'dict' object has no attribute 'bla' -- variant StructWithGetItem (StructWithGetItem): KeyError: 'a' -- variant StructWithGetItemArg (StructWithGetItemArg): KeyError: 'foo'" - ); - - let tup = PyTuple::empty(py); - let err = tup.extract::>().unwrap_err(); - assert_eq!( - err.to_string(), - "\ -TypeError: failed to extract enum Foo ('TupleVar | StructVar | TransparentTuple | TransparentStructVar | StructVarGetAttrArg | StructWithGetItem | StructWithGetItemArg') -- variant TupleVar (TupleVar): ValueError: expected tuple of length 2, but got tuple of length 0 -- variant StructVar (StructVar): AttributeError: 'tuple' object has no attribute 'test' -- variant TransparentTuple (TransparentTuple): TypeError: failed to extract field Foo::TransparentTuple.0, caused by TypeError: 'tuple' object cannot be interpreted as an integer -- variant TransparentStructVar (TransparentStructVar): TypeError: failed to extract field Foo::TransparentStructVar.a, caused by TypeError: 'tuple' object cannot be converted to 'PyString' -- variant StructVarGetAttrArg (StructVarGetAttrArg): AttributeError: 'tuple' object has no attribute 'bla' -- variant StructWithGetItem (StructWithGetItem): TypeError: tuple indices must be integers or slices, not str -- variant StructWithGetItemArg (StructWithGetItemArg): TypeError: tuple indices must be integers or slices, not str" - ); - }); -} - -#[derive(Debug, FromPyObject)] -enum EnumWithCatchAll<'py> { - #[allow(dead_code)] - #[pyo3(transparent)] - Foo(Foo<'py>), - #[pyo3(transparent)] - CatchAll(Bound<'py, PyAny>), -} - -#[test] -fn test_enum_catch_all() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - let f = dict - .extract::>() - .expect("Failed to extract EnumWithCatchAll from dict"); - match f { - EnumWithCatchAll::CatchAll(any) => { - let d = any.extract::>().expect("Expected pydict"); - assert!(d.is_empty()); - } - _ => panic!( - "Expected extracting EnumWithCatchAll::CatchAll, got {:?}", - f - ), - } - }); -} - -#[derive(Debug, FromPyObject)] -pub enum Bar { - #[pyo3(annotation = "str")] - A(String), - #[pyo3(annotation = "uint")] - B(usize), - #[pyo3(annotation = "int", transparent)] - C(isize), -} - -#[test] -fn test_err_rename() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - let f = dict.extract::(); - assert!(f.is_err()); - assert_eq!( - f.unwrap_err().to_string(), - "\ -TypeError: failed to extract enum Bar ('str | uint | int') -- variant A (str): TypeError: failed to extract field Bar::A.0, caused by TypeError: 'dict' object cannot be converted to 'PyString' -- variant B (uint): TypeError: failed to extract field Bar::B.0, caused by TypeError: 'dict' object cannot be interpreted as an integer -- variant C (int): TypeError: failed to extract field Bar::C.0, caused by TypeError: 'dict' object cannot be interpreted as an integer" - ); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct Zap { - #[pyo3(item)] - name: String, - - #[pyo3(from_py_with = "Bound::<'_, PyAny>::len", item("my_object"))] - some_object_length: usize, -} - -#[test] -fn test_from_py_with() { - Python::with_gil(|py| { - let py_zap = py - .eval( - pyo3_ffi::c_str!(r#"{"name": "whatever", "my_object": [1, 2, 3]}"#), - None, - None, - ) - .expect("failed to create dict"); - - let zap = py_zap.extract::().unwrap(); - - assert_eq!(zap.name, "whatever"); - assert_eq!(zap.some_object_length, 3usize); - }); -} - -#[derive(Debug, FromPyObject)] -pub struct ZapTuple( - String, - #[pyo3(from_py_with = "Bound::<'_, PyAny>::len")] usize, -); - -#[test] -fn test_from_py_with_tuple_struct() { - Python::with_gil(|py| { - let py_zap = py - .eval(pyo3_ffi::c_str!(r#"("whatever", [1, 2, 3])"#), None, None) - .expect("failed to create tuple"); - - let zap = py_zap.extract::().unwrap(); - - assert_eq!(zap.0, "whatever"); - assert_eq!(zap.1, 3usize); - }); -} - -#[test] -fn test_from_py_with_tuple_struct_error() { - Python::with_gil(|py| { - let py_zap = py - .eval( - pyo3_ffi::c_str!(r#"("whatever", [1, 2, 3], "third")"#), - None, - None, - ) - .expect("failed to create tuple"); - - let f = py_zap.extract::(); - - assert!(f.is_err()); - assert_eq!( - f.unwrap_err().to_string(), - "ValueError: expected tuple of length 2, but got tuple of length 3" - ); - }); -} - -#[derive(Debug, FromPyObject, PartialEq, Eq)] -pub enum ZapEnum { - Zip(#[pyo3(from_py_with = "Bound::<'_, PyAny>::len")] usize), - Zap( - String, - #[pyo3(from_py_with = "Bound::<'_, PyAny>::len")] usize, - ), -} - -#[test] -fn test_from_py_with_enum() { - Python::with_gil(|py| { - let py_zap = py - .eval(pyo3_ffi::c_str!(r#"("whatever", [1, 2, 3])"#), None, None) - .expect("failed to create tuple"); - - let zap = py_zap.extract::().unwrap(); - let expected_zap = ZapEnum::Zip(2); - - assert_eq!(zap, expected_zap); - }); -} - -#[derive(Debug, FromPyObject, PartialEq, Eq)] -#[pyo3(transparent)] -pub struct TransparentFromPyWith { - #[pyo3(from_py_with = "Bound::<'_, PyAny>::len")] - len: usize, -} - -#[test] -fn test_transparent_from_py_with() { - Python::with_gil(|py| { - let result = PyList::new(py, [1, 2, 3]) - .unwrap() - .extract::() - .unwrap(); - let expected = TransparentFromPyWith { len: 3 }; - - assert_eq!(result, expected); - }); -} - -#[derive(Debug, FromPyObject, PartialEq, Eq)] -pub struct WithKeywordAttr { - r#box: usize, -} - -#[pyclass] -pub struct WithKeywordAttrC { - #[pyo3(get)] - r#box: usize, -} - -#[test] -fn test_with_keyword_attr() { - Python::with_gil(|py| { - let cls = WithKeywordAttrC { r#box: 3 }.into_pyobject(py).unwrap(); - let result = cls.extract::().unwrap(); - let expected = WithKeywordAttr { r#box: 3 }; - assert_eq!(result, expected); - }); -} - -#[derive(Debug, FromPyObject, PartialEq, Eq)] -pub struct WithKeywordItem { - #[pyo3(item)] - r#box: usize, -} - -#[test] -fn test_with_keyword_item() { - Python::with_gil(|py| { - let dict = PyDict::new(py); - dict.set_item("box", 3).unwrap(); - let result = dict.extract::().unwrap(); - let expected = WithKeywordItem { r#box: 3 }; - assert_eq!(result, expected); - }); -} diff --git a/vendor/pyo3/tests/test_gc.rs b/vendor/pyo3/tests/test_gc.rs deleted file mode 100644 index 4b293449b3..0000000000 --- a/vendor/pyo3/tests/test_gc.rs +++ /dev/null @@ -1,728 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::class::PyTraverseError; -use pyo3::class::PyVisit; -use pyo3::ffi; -use pyo3::prelude::*; -use pyo3::py_run; -#[cfg(not(target_arch = "wasm32"))] -use std::cell::Cell; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Once; -use std::sync::{Arc, Mutex}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(freelist = 2)] -struct ClassWithFreelist {} - -#[test] -fn class_with_freelist() { - let ptr = Python::with_gil(|py| { - let inst = Py::new(py, ClassWithFreelist {}).unwrap(); - let _inst2 = Py::new(py, ClassWithFreelist {}).unwrap(); - let ptr = inst.as_ptr(); - drop(inst); - ptr - }); - - Python::with_gil(|py| { - let inst3 = Py::new(py, ClassWithFreelist {}).unwrap(); - assert_eq!(ptr, inst3.as_ptr()); - - let inst4 = Py::new(py, ClassWithFreelist {}).unwrap(); - assert_ne!(ptr, inst4.as_ptr()) - }); -} - -/// Helper function to create a pair of objects that can be used to test drops; -/// the first object is a guard that records when it has been dropped, the second -/// object is a check that can be used to assert that the guard has been dropped. -fn drop_check() -> (DropGuard, DropCheck) { - let flag = Arc::new(Once::new()); - (DropGuard(flag.clone()), DropCheck(flag)) -} - -/// Helper structure that records when it has been dropped in the cor -struct DropGuard(Arc); -impl Drop for DropGuard { - fn drop(&mut self) { - self.0.call_once(|| ()); - } -} - -struct DropCheck(Arc); -impl DropCheck { - #[track_caller] - fn assert_not_dropped(&self) { - assert!(!self.0.is_completed()); - } - - #[track_caller] - fn assert_dropped(&self) { - assert!(self.0.is_completed()); - } - - #[track_caller] - fn assert_drops_with_gc(&self, object: *mut pyo3::ffi::PyObject) { - // running the GC might take a few cycles to collect an object - for _ in 0..100 { - if self.0.is_completed() { - return; - } - - Python::with_gil(|py| { - py.run(ffi::c_str!("import gc; gc.collect()"), None, None) - .unwrap(); - }); - #[cfg(Py_GIL_DISABLED)] - { - // on the free-threaded build, the GC might be running in a separate thread, allow - // some time for this - std::thread::sleep(std::time::Duration::from_millis(5)); - } - } - - panic!( - "Object was not dropped after 100 GC cycles, refcount is {}", - // this could be garbage, but it's in a test and we're just printing the value - unsafe { ffi::Py_REFCNT(object) } - ); - } -} - -#[test] -fn data_is_dropped() { - #[pyclass] - struct DataIsDropped { - _guard1: DropGuard, - _guard2: DropGuard, - } - - let (guard1, check1) = drop_check(); - let (guard2, check2) = drop_check(); - - Python::with_gil(|py| { - let data_is_dropped = DataIsDropped { - _guard1: guard1, - _guard2: guard2, - }; - let inst = Py::new(py, data_is_dropped).unwrap(); - check1.assert_not_dropped(); - check2.assert_not_dropped(); - drop(inst); - }); - - check1.assert_dropped(); - check2.assert_dropped(); -} - -#[pyclass(subclass)] -struct CycleWithClear { - cycle: Option, - _guard: DropGuard, -} - -#[pymethods] -impl CycleWithClear { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - visit.call(&self.cycle) - } - - fn __clear__(slf: &Bound<'_, Self>) { - println!("clear run, refcount before {}", slf.get_refcnt()); - slf.borrow_mut().cycle = None; - println!("clear run, refcount after {}", slf.get_refcnt()); - } -} - -#[test] -fn test_cycle_clear() { - let (guard, check) = drop_check(); - - let ptr = Python::with_gil(|py| { - let inst = Bound::new( - py, - CycleWithClear { - cycle: None, - _guard: guard, - }, - ) - .unwrap(); - - inst.borrow_mut().cycle = Some(inst.clone().into_any().unbind()); - - py_run!(py, inst, "import gc; assert inst in gc.get_objects()"); - check.assert_not_dropped(); - inst.as_ptr() - }); - - check.assert_drops_with_gc(ptr); -} - -/// Test that traversing `None` of `Option>` does not cause a segfault -#[test] -fn gc_null_traversal() { - #[pyclass] - struct GcNullTraversal { - cycle: Option>, - null: Option>, - } - - #[pymethods] - impl GcNullTraversal { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - visit.call(&self.cycle)?; - visit.call(&self.null)?; // Should not segfault - Ok(()) - } - - fn __clear__(&mut self) { - self.cycle = None; - self.null = None; - } - } - - Python::with_gil(|py| { - let obj = Py::new( - py, - GcNullTraversal { - cycle: None, - null: None, - }, - ) - .unwrap(); - obj.borrow_mut(py).cycle = Some(obj.clone_ref(py)); - - // the object doesn't have to be cleaned up, it just needs to be traversed. - py.run(ffi::c_str!("import gc; gc.collect()"), None, None) - .unwrap(); - }); -} - -#[test] -fn inheritance_with_new_methods_with_drop() { - #[pyclass(subclass)] - struct BaseClassWithDrop { - guard: Option, - } - - #[pymethods] - impl BaseClassWithDrop { - #[new] - fn new() -> BaseClassWithDrop { - BaseClassWithDrop { guard: None } - } - } - - #[pyclass(extends = BaseClassWithDrop)] - struct SubClassWithDrop { - guard: Option, - } - - #[pymethods] - impl SubClassWithDrop { - #[new] - fn new() -> (Self, BaseClassWithDrop) { - ( - SubClassWithDrop { guard: None }, - BaseClassWithDrop { guard: None }, - ) - } - } - - let (guard_base, check_base) = drop_check(); - let (guard_sub, check_sub) = drop_check(); - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let inst = typeobj - .call((), None) - .unwrap() - .downcast_into::() - .unwrap(); - - inst.as_super().borrow_mut().guard = Some(guard_base); - inst.borrow_mut().guard = Some(guard_sub); - - check_base.assert_not_dropped(); - check_sub.assert_not_dropped(); - }); - - check_base.assert_dropped(); - check_sub.assert_dropped(); -} - -#[test] -fn gc_during_borrow() { - #[pyclass] - struct TraversableClass { - traversed: AtomicBool, - } - - impl TraversableClass { - fn new() -> Self { - Self { - traversed: AtomicBool::new(false), - } - } - } - - #[pymethods] - impl TraversableClass { - fn __clear__(&mut self) {} - - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - self.traversed.store(true, Ordering::Relaxed); - Ok(()) - } - } - - Python::with_gil(|py| { - // get the traverse function - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - // create an object and check that traversing it works normally - // when it's not borrowed - let cell = Bound::new(py, TraversableClass::new()).unwrap(); - assert!(!cell.borrow().traversed.load(Ordering::Relaxed)); - unsafe { traverse(cell.as_ptr(), novisit, std::ptr::null_mut()) }; - assert!(cell.borrow().traversed.load(Ordering::Relaxed)); - - // create an object and check that it is not traversed if the GC - // is invoked while it is already borrowed mutably - let cell2 = Bound::new(py, TraversableClass::new()).unwrap(); - let guard = cell2.borrow_mut(); - assert!(!guard.traversed.load(Ordering::Relaxed)); - unsafe { traverse(cell2.as_ptr(), novisit, std::ptr::null_mut()) }; - assert!(!guard.traversed.load(Ordering::Relaxed)); - drop(guard); - }); -} - -#[test] -fn traverse_partial() { - #[pyclass] - struct PartialTraverse { - member: PyObject, - } - - impl PartialTraverse { - fn new(py: Python<'_>) -> Self { - Self { member: py.None() } - } - } - - #[pymethods] - impl PartialTraverse { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - visit.call(&self.member)?; - // In the test, we expect this to never be hit - unreachable!() - } - } - - Python::with_gil(|py| { - // get the traverse function - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - // confirm that traversing errors - let obj = Py::new(py, PartialTraverse::new(py)).unwrap(); - assert_eq!( - unsafe { traverse(obj.as_ptr(), visit_error, std::ptr::null_mut()) }, - -1 - ); - }) -} - -#[test] -fn traverse_panic() { - #[pyclass] - struct PanickyTraverse { - member: PyObject, - } - - impl PanickyTraverse { - fn new(py: Python<'_>) -> Self { - Self { member: py.None() } - } - } - - #[pymethods] - impl PanickyTraverse { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - visit.call(&self.member)?; - panic!("at the disco"); - } - } - - Python::with_gil(|py| { - // get the traverse function - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - // confirm that traversing errors - let obj = Py::new(py, PanickyTraverse::new(py)).unwrap(); - assert_eq!( - unsafe { traverse(obj.as_ptr(), novisit, std::ptr::null_mut()) }, - -1 - ); - }) -} - -#[test] -fn tries_gil_in_traverse() { - #[pyclass] - struct TriesGILInTraverse {} - - #[pymethods] - impl TriesGILInTraverse { - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - Python::with_gil(|_py| Ok(())) - } - } - - Python::with_gil(|py| { - // get the traverse function - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - // confirm that traversing panicks - let obj = Py::new(py, TriesGILInTraverse {}).unwrap(); - assert_eq!( - unsafe { traverse(obj.as_ptr(), novisit, std::ptr::null_mut()) }, - -1 - ); - }) -} - -#[test] -fn traverse_cannot_be_hijacked() { - #[pyclass] - struct HijackedTraverse { - traversed: AtomicBool, - hijacked: AtomicBool, - } - - impl HijackedTraverse { - fn new() -> Self { - Self { - traversed: AtomicBool::new(false), - hijacked: AtomicBool::new(false), - } - } - - fn traversed_and_hijacked(&self) -> (bool, bool) { - ( - self.traversed.load(Ordering::Acquire), - self.hijacked.load(Ordering::Acquire), - ) - } - } - - #[pymethods] - impl HijackedTraverse { - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - self.traversed.store(true, Ordering::Release); - Ok(()) - } - } - - #[allow(dead_code)] - trait Traversable { - fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>; - } - - impl Traversable for PyRef<'_, HijackedTraverse> { - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - self.hijacked.store(true, Ordering::Release); - Ok(()) - } - } - - Python::with_gil(|py| { - // get the traverse function - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - let cell = Bound::new(py, HijackedTraverse::new()).unwrap(); - assert_eq!(cell.borrow().traversed_and_hijacked(), (false, false)); - unsafe { traverse(cell.as_ptr(), novisit, std::ptr::null_mut()) }; - assert_eq!(cell.borrow().traversed_and_hijacked(), (true, false)); - }) -} - -#[pyclass] -struct DropDuringTraversal { - cycle: Mutex>>, - _guard: DropGuard, -} - -#[pymethods] -impl DropDuringTraversal { - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - let mut cycle_ref = self.cycle.lock().unwrap(); - *cycle_ref = None; - Ok(()) - } -} - -#[cfg(not(pyo3_disable_reference_pool))] -#[test] -fn drop_during_traversal_with_gil() { - let (guard, check) = drop_check(); - - let ptr = Python::with_gil(|py| { - let cycle = Mutex::new(None); - let inst = Py::new( - py, - DropDuringTraversal { - cycle, - _guard: guard, - }, - ) - .unwrap(); - - *inst.borrow_mut(py).cycle.lock().unwrap() = Some(inst.clone_ref(py)); - - check.assert_not_dropped(); - let ptr = inst.as_ptr(); - drop(inst); // drop the object while holding the GIL - - #[cfg(not(Py_GIL_DISABLED))] - { - // other thread might have caused GC on free-threaded build - check.assert_not_dropped(); - } - - ptr - }); - - check.assert_drops_with_gc(ptr); -} - -#[cfg(not(pyo3_disable_reference_pool))] -#[test] -fn drop_during_traversal_without_gil() { - let (guard, check) = drop_check(); - - let inst = Python::with_gil(|py| { - let cycle = Mutex::new(None); - let inst = Py::new( - py, - DropDuringTraversal { - cycle, - _guard: guard, - }, - ) - .unwrap(); - - *inst.borrow_mut(py).cycle.lock().unwrap() = Some(inst.clone_ref(py)); - - check.assert_not_dropped(); - inst - }); - - let ptr = inst.as_ptr(); - drop(inst); // drop the object without holding the GIL - - check.assert_drops_with_gc(ptr); -} - -#[test] -#[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled -fn unsendable_are_not_traversed_on_foreign_thread() { - #[pyclass(unsendable)] - struct UnsendableTraversal { - traversed: Cell, - } - - #[pymethods] - impl UnsendableTraversal { - fn __clear__(&mut self) {} - - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - self.traversed.set(true); - Ok(()) - } - } - - #[derive(Clone, Copy)] - struct SendablePtr(*mut pyo3::ffi::PyObject); - - unsafe impl Send for SendablePtr {} - - Python::with_gil(|py| { - let ty = py.get_type::(); - let traverse = unsafe { get_type_traverse(ty.as_type_ptr()).unwrap() }; - - let obj = Bound::new( - py, - UnsendableTraversal { - traversed: Cell::new(false), - }, - ) - .unwrap(); - - let ptr = SendablePtr(obj.as_ptr()); - - std::thread::spawn(move || { - // traversal on foreign thread is a no-op - assert_eq!( - unsafe { traverse({ ptr }.0, novisit, std::ptr::null_mut()) }, - 0 - ); - }) - .join() - .unwrap(); - - assert!(!obj.borrow().traversed.get()); - - // traversal on home thread still works - assert_eq!( - unsafe { traverse({ ptr }.0, novisit, std::ptr::null_mut()) }, - 0 - ); - - assert!(obj.borrow().traversed.get()); - }); -} - -#[test] -fn test_traverse_subclass() { - #[pyclass(extends = CycleWithClear)] - struct SubOverrideTraverse {} - - #[pymethods] - impl SubOverrideTraverse { - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - // subclass traverse overrides the base class traverse - Ok(()) - } - } - - let (guard, check) = drop_check(); - - Python::with_gil(|py| { - let base = CycleWithClear { - cycle: None, - _guard: guard, - }; - let obj = Bound::new( - py, - PyClassInitializer::from(base).add_subclass(SubOverrideTraverse {}), - ) - .unwrap(); - obj.borrow_mut().as_super().cycle = Some(obj.clone().into_any().unbind()); - - check.assert_not_dropped(); - let ptr = obj.as_ptr(); - drop(obj); - #[cfg(not(Py_GIL_DISABLED))] - { - // other thread might have caused GC on free-threaded build - check.assert_not_dropped(); - } - - #[cfg(not(Py_GIL_DISABLED))] - { - // FIXME: seems like a bug that this is flaky on the free-threaded build - // https://github.com/PyO3/pyo3/issues/4627 - check.assert_drops_with_gc(ptr); - } - - #[cfg(Py_GIL_DISABLED)] - { - // silence unused ptr warning - let _ = ptr; - } - }); -} - -#[test] -fn test_traverse_subclass_override_clear() { - #[pyclass(extends = CycleWithClear)] - struct SubOverrideClear {} - - #[pymethods] - impl SubOverrideClear { - #[allow(clippy::unnecessary_wraps)] - fn __traverse__(&self, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - // subclass traverse overrides the base class traverse, necessary for - // the sub clear to be called - // FIXME: should this really need to be the case? - Ok(()) - } - - fn __clear__(&self) { - // subclass clear overrides the base class clear - } - } - - let (guard, check) = drop_check(); - - Python::with_gil(|py| { - let base = CycleWithClear { - cycle: None, - _guard: guard, - }; - let obj = Bound::new( - py, - PyClassInitializer::from(base).add_subclass(SubOverrideClear {}), - ) - .unwrap(); - obj.borrow_mut().as_super().cycle = Some(obj.clone().into_any().unbind()); - - check.assert_not_dropped(); - let ptr = obj.as_ptr(); - drop(obj); - #[cfg(not(Py_GIL_DISABLED))] - { - // other thread might have caused GC on free-threaded build - check.assert_not_dropped(); - } - - #[cfg(not(Py_GIL_DISABLED))] - { - // FIXME: seems like a bug that this is flaky on the free-threaded build - // https://github.com/PyO3/pyo3/issues/4627 - check.assert_drops_with_gc(ptr); - } - - #[cfg(Py_GIL_DISABLED)] - { - // silence unused ptr warning - let _ = ptr; - } - }); -} - -// Manual traversal utilities - -unsafe fn get_type_traverse(tp: *mut pyo3::ffi::PyTypeObject) -> Option { - std::mem::transmute(pyo3::ffi::PyType_GetSlot(tp, pyo3::ffi::Py_tp_traverse)) -} - -// a dummy visitor function -extern "C" fn novisit( - _object: *mut pyo3::ffi::PyObject, - _arg: *mut core::ffi::c_void, -) -> std::os::raw::c_int { - 0 -} - -// a visitor function which errors (returns nonzero code) -extern "C" fn visit_error( - _object: *mut pyo3::ffi::PyObject, - _arg: *mut core::ffi::c_void, -) -> std::os::raw::c_int { - -1 -} diff --git a/vendor/pyo3/tests/test_getter_setter.rs b/vendor/pyo3/tests/test_getter_setter.rs deleted file mode 100644 index cdc8136bed..0000000000 --- a/vendor/pyo3/tests/test_getter_setter.rs +++ /dev/null @@ -1,320 +0,0 @@ -#![cfg(feature = "macros")] - -use std::cell::Cell; - -use pyo3::prelude::*; -use pyo3::py_run; -use pyo3::types::PyString; -use pyo3::types::{IntoPyDict, PyList}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct ClassWithProperties { - num: i32, -} - -#[pymethods] -impl ClassWithProperties { - fn get_num(&self) -> i32 { - self.num - } - - #[getter(DATA)] - /// a getter for data - fn get_data(&self) -> i32 { - self.num - } - #[setter(DATA)] - fn set_data(&mut self, value: i32) { - self.num = value; - } - - #[getter] - /// a getter with a type un-wrapped by PyResult - fn get_unwrapped(&self) -> i32 { - self.num - } - - #[setter] - fn set_unwrapped(&mut self, value: i32) { - self.num = value; - } - - #[setter] - fn set_from_len(&mut self, #[pyo3(from_py_with = "extract_len")] value: i32) { - self.num = value; - } - - #[setter] - fn set_from_any(&mut self, value: &Bound<'_, PyAny>) -> PyResult<()> { - self.num = value.extract()?; - Ok(()) - } - - #[getter] - fn get_data_list<'py>(&self, py: Python<'py>) -> PyResult> { - PyList::new(py, [self.num]) - } -} - -fn extract_len(any: &Bound<'_, PyAny>) -> PyResult { - any.len().map(|len| len as i32) -} - -#[test] -fn class_with_properties() { - Python::with_gil(|py| { - let inst = Py::new(py, ClassWithProperties { num: 10 }).unwrap(); - - py_run!(py, inst, "assert inst.get_num() == 10"); - py_run!(py, inst, "assert inst.get_num() == inst.DATA"); - py_run!(py, inst, "inst.DATA = 20"); - py_run!(py, inst, "assert inst.get_num() == 20 == inst.DATA"); - - py_expect_exception!(py, inst, "del inst.DATA", PyAttributeError); - - py_run!(py, inst, "assert inst.get_num() == inst.unwrapped == 20"); - py_run!(py, inst, "inst.unwrapped = 42"); - py_run!(py, inst, "assert inst.get_num() == inst.unwrapped == 42"); - py_run!(py, inst, "assert inst.data_list == [42]"); - - py_run!(py, inst, "inst.from_len = [0, 0, 0]"); - py_run!(py, inst, "assert inst.get_num() == 3"); - - py_run!(py, inst, "inst.from_any = 15"); - py_run!(py, inst, "assert inst.get_num() == 15"); - - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!(py, *d, "C.DATA.__doc__ == 'a getter for data'"); - }); -} - -#[pyclass] -struct GetterSetter { - #[pyo3(get, set)] - num: i32, - #[pyo3(get, set)] - text: String, -} - -#[pymethods] -impl GetterSetter { - fn get_num2(&self) -> i32 { - self.num - } -} - -#[test] -fn getter_setter_autogen() { - Python::with_gil(|py| { - let inst = Py::new( - py, - GetterSetter { - num: 10, - text: "Hello".to_string(), - }, - ) - .unwrap(); - - py_run!(py, inst, "assert inst.num == 10"); - py_run!(py, inst, "inst.num = 20; assert inst.num == 20"); - py_run!( - py, - inst, - "assert inst.text == 'Hello'; inst.text = 'There'; assert inst.text == 'There'" - ); - }); -} - -#[pyclass] -struct RefGetterSetter { - num: i32, -} - -#[pymethods] -impl RefGetterSetter { - #[getter] - fn get_num(slf: PyRef<'_, Self>) -> i32 { - slf.num - } - - #[setter] - fn set_num(mut slf: PyRefMut<'_, Self>, value: i32) { - slf.num = value; - } -} - -#[test] -fn ref_getter_setter() { - // Regression test for #837 - Python::with_gil(|py| { - let inst = Py::new(py, RefGetterSetter { num: 10 }).unwrap(); - - py_run!(py, inst, "assert inst.num == 10"); - py_run!(py, inst, "inst.num = 20; assert inst.num == 20"); - }); -} - -#[pyclass] -struct TupleClassGetterSetter(i32); - -#[pymethods] -impl TupleClassGetterSetter { - #[getter(num)] - fn get_num(&self) -> i32 { - self.0 - } - - #[setter(num)] - fn set_num(&mut self, value: i32) { - self.0 = value; - } -} - -#[test] -fn tuple_struct_getter_setter() { - Python::with_gil(|py| { - let inst = Py::new(py, TupleClassGetterSetter(10)).unwrap(); - - py_assert!(py, inst, "inst.num == 10"); - py_run!(py, inst, "inst.num = 20"); - py_assert!(py, inst, "inst.num == 20"); - }); -} - -#[pyclass(get_all, set_all)] -struct All { - num: i32, -} - -#[test] -fn get_set_all() { - Python::with_gil(|py| { - let inst = Py::new(py, All { num: 10 }).unwrap(); - - py_run!(py, inst, "assert inst.num == 10"); - py_run!(py, inst, "inst.num = 20; assert inst.num == 20"); - }); -} - -#[pyclass(get_all)] -struct All2 { - #[pyo3(set)] - num: i32, -} - -#[test] -fn get_all_and_set() { - Python::with_gil(|py| { - let inst = Py::new(py, All2 { num: 10 }).unwrap(); - - py_run!(py, inst, "assert inst.num == 10"); - py_run!(py, inst, "inst.num = 20; assert inst.num == 20"); - }); -} - -#[pyclass(unsendable)] -struct CellGetterSetter { - #[pyo3(get, set)] - cell_inner: Cell, -} - -#[test] -fn cell_getter_setter() { - let c = CellGetterSetter { - cell_inner: Cell::new(10), - }; - Python::with_gil(|py| { - let inst = Py::new(py, c).unwrap(); - let cell = Cell::new(20i32).into_pyobject(py).unwrap(); - - py_run!(py, cell, "assert cell == 20"); - py_run!(py, inst, "assert inst.cell_inner == 10"); - py_run!( - py, - inst, - "inst.cell_inner = 20; assert inst.cell_inner == 20" - ); - }); -} - -#[test] -fn borrowed_value_with_lifetime_of_self() { - #[pyclass] - struct BorrowedValue {} - - #[pymethods] - impl BorrowedValue { - #[getter] - fn value(&self) -> &str { - "value" - } - } - - Python::with_gil(|py| { - let inst = Py::new(py, BorrowedValue {}).unwrap(); - - py_run!(py, inst, "assert inst.value == 'value'"); - }); -} - -#[test] -fn frozen_py_field_get() { - #[pyclass(frozen)] - struct FrozenPyField { - #[pyo3(get)] - value: Py, - } - - Python::with_gil(|py| { - let inst = Py::new( - py, - FrozenPyField { - value: "value".into_pyobject(py).unwrap().unbind(), - }, - ) - .unwrap(); - - py_run!(py, inst, "assert inst.value == 'value'"); - }); -} - -#[test] -fn test_optional_setter() { - #[pyclass] - struct SimpleClass { - field: Option, - } - - #[pymethods] - impl SimpleClass { - #[getter] - fn get_field(&self) -> Option { - self.field - } - - #[setter] - fn set_field(&mut self, field: Option) { - self.field = field; - } - } - - Python::with_gil(|py| { - let instance = Py::new(py, SimpleClass { field: None }).unwrap(); - py_run!(py, instance, "assert instance.field is None"); - py_run!( - py, - instance, - "instance.field = 42; assert instance.field == 42" - ); - py_run!( - py, - instance, - "instance.field = None; assert instance.field is None" - ); - }) -} diff --git a/vendor/pyo3/tests/test_inheritance.rs b/vendor/pyo3/tests/test_inheritance.rs deleted file mode 100644 index 7190dd4955..0000000000 --- a/vendor/pyo3/tests/test_inheritance.rs +++ /dev/null @@ -1,349 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::py_run; - -use pyo3::ffi; -use pyo3::types::IntoPyDict; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(subclass)] -struct BaseClass { - #[pyo3(get)] - val1: usize, -} - -#[pyclass(subclass)] -struct SubclassAble {} - -#[test] -fn subclass() { - Python::with_gil(|py| { - let d = [("SubclassAble", py.get_type::())] - .into_py_dict(py) - .unwrap(); - - py.run( - ffi::c_str!("class A(SubclassAble): pass\nassert issubclass(A, SubclassAble)"), - None, - Some(&d), - ) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} - -#[pymethods] -impl BaseClass { - #[new] - fn new() -> Self { - BaseClass { val1: 10 } - } - fn base_method(&self, x: usize) -> usize { - x * self.val1 - } - fn base_set(&mut self, fn_: &Bound<'_, PyAny>) -> PyResult<()> { - let value: usize = fn_.call0()?.extract()?; - self.val1 = value; - Ok(()) - } -} - -#[pyclass(extends=BaseClass)] -struct SubClass { - #[pyo3(get)] - val2: usize, -} - -#[pymethods] -impl SubClass { - #[new] - fn new() -> (Self, BaseClass) { - (SubClass { val2: 5 }, BaseClass { val1: 10 }) - } - fn sub_method(&self, x: usize) -> usize { - x * self.val2 - } - fn sub_set_and_ret(&mut self, x: usize) -> usize { - self.val2 = x; - x - } -} - -#[test] -fn inheritance_with_new_methods() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - let inst = typeobj.call((), None).unwrap(); - py_run!(py, inst, "assert inst.val1 == 10; assert inst.val2 == 5"); - }); -} - -#[test] -fn call_base_and_sub_methods() { - Python::with_gil(|py| { - let obj = Py::new(py, SubClass::new()).unwrap(); - py_run!( - py, - obj, - r#" - assert obj.base_method(10) == 100 - assert obj.sub_method(10) == 50 -"# - ); - }); -} - -#[test] -fn mutation_fails() { - Python::with_gil(|py| { - let obj = Py::new(py, SubClass::new()).unwrap(); - let global = [("obj", obj)].into_py_dict(py).unwrap(); - let e = py - .run( - ffi::c_str!("obj.base_set(lambda: obj.sub_set_and_ret(1))"), - Some(&global), - None, - ) - .unwrap_err(); - assert_eq!(&e.to_string(), "RuntimeError: Already borrowed"); - }); -} - -#[test] -fn is_subclass_and_is_instance() { - Python::with_gil(|py| { - let sub_ty = py.get_type::(); - let base_ty = py.get_type::(); - assert!(sub_ty.is_subclass_of::().unwrap()); - assert!(sub_ty.is_subclass(&base_ty).unwrap()); - - let obj = Bound::new(py, SubClass::new()).unwrap().into_any(); - assert!(obj.is_instance_of::()); - assert!(obj.is_instance_of::()); - assert!(obj.is_instance(&sub_ty).unwrap()); - assert!(obj.is_instance(&base_ty).unwrap()); - }); -} - -#[pyclass(subclass)] -struct BaseClassWithResult { - _val: usize, -} - -#[pymethods] -impl BaseClassWithResult { - #[new] - fn new(value: isize) -> PyResult { - Ok(Self { - _val: std::convert::TryFrom::try_from(value)?, - }) - } -} - -#[pyclass(extends=BaseClassWithResult)] -struct SubClass2 {} - -#[pymethods] -impl SubClass2 { - #[new] - fn new(value: isize) -> PyResult<(Self, BaseClassWithResult)> { - let base = BaseClassWithResult::new(value)?; - Ok((Self {}, base)) - } -} - -#[test] -fn handle_result_in_new() { - Python::with_gil(|py| { - let subclass = py.get_type::(); - py_run!( - py, - subclass, - r#" -try: - subclass(-10) - assert Fals -except ValueError as e: - pass -except Exception as e: - raise e -"# - ); - }); -} - -// Subclassing builtin types is not allowed in the LIMITED API. -#[cfg(not(Py_LIMITED_API))] -mod inheriting_native_type { - use super::*; - use pyo3::exceptions::PyException; - use pyo3::types::PyDict; - - #[cfg(not(PyPy))] - #[test] - fn inherit_set() { - use pyo3::types::PySet; - - #[cfg(not(PyPy))] - #[pyclass(extends=PySet)] - #[derive(Debug)] - struct SetWithName { - #[pyo3(get, name = "name")] - _name: &'static str, - } - - #[cfg(not(PyPy))] - #[pymethods] - impl SetWithName { - #[new] - fn new() -> Self { - SetWithName { _name: "Hello :)" } - } - } - - Python::with_gil(|py| { - let set_sub = pyo3::Py::new(py, SetWithName::new()).unwrap(); - py_run!( - py, - set_sub, - r#"set_sub.add(10); assert list(set_sub) == [10]; assert set_sub.name == "Hello :)""# - ); - }); - } - - #[pyclass(extends=PyDict)] - #[derive(Debug)] - struct DictWithName { - #[pyo3(get, name = "name")] - _name: &'static str, - } - - #[pymethods] - impl DictWithName { - #[new] - fn new() -> Self { - DictWithName { _name: "Hello :)" } - } - } - - #[test] - fn inherit_dict() { - Python::with_gil(|py| { - let dict_sub = pyo3::Py::new(py, DictWithName::new()).unwrap(); - py_run!( - py, - dict_sub, - r#"dict_sub[0] = 1; assert dict_sub[0] == 1; assert dict_sub.name == "Hello :)""# - ); - }); - } - - #[test] - fn inherit_dict_drop() { - Python::with_gil(|py| { - let dict_sub = pyo3::Py::new(py, DictWithName::new()).unwrap(); - assert_eq!(dict_sub.get_refcnt(py), 1); - - let item = &py.eval(ffi::c_str!("object()"), None, None).unwrap(); - assert_eq!(item.get_refcnt(), 1); - - dict_sub.bind(py).set_item("foo", item).unwrap(); - assert_eq!(item.get_refcnt(), 2); - - drop(dict_sub); - assert_eq!(item.get_refcnt(), 1); - }) - } - - #[pyclass(extends=PyException)] - struct CustomException { - #[pyo3(get)] - context: &'static str, - } - - #[pymethods] - impl CustomException { - #[new] - fn new(_exc_arg: &Bound<'_, PyAny>) -> Self { - CustomException { - context: "Hello :)", - } - } - } - - #[test] - fn custom_exception() { - Python::with_gil(|py| { - let cls = py.get_type::(); - let dict = [("cls", &cls)].into_py_dict(py).unwrap(); - let res = py.run( - ffi::c_str!("e = cls('hello'); assert str(e) == 'hello'; assert e.context == 'Hello :)'; raise e"), - None, - Some(&dict) - ); - let err = res.unwrap_err(); - assert!(err.matches(py, &cls).unwrap(), "{}", err); - - // catching the exception in Python also works: - py_run!( - py, - cls, - r#" - try: - raise cls("foo") - except cls: - pass - "# - ) - }) - } -} - -#[pyclass(subclass)] -struct SimpleClass {} - -#[pymethods] -impl SimpleClass { - #[new] - fn new() -> Self { - Self {} - } -} - -#[test] -fn test_subclass_ref_counts() { - // regression test for issue #1363 - Python::with_gil(|py| { - #[allow(non_snake_case)] - let SimpleClass = py.get_type::(); - py_run!( - py, - SimpleClass, - r#" - import gc - import sys - - class SubClass(SimpleClass): - pass - - gc.collect() - count = sys.getrefcount(SubClass) - - for i in range(1000): - c = SubClass() - del c - - gc.collect() - after = sys.getrefcount(SubClass) - # depending on Python's GC the count may be either identical or exactly 1000 higher, - # both are expected values that are not representative of the issue. - # - # (With issue #1363 the count will be decreased.) - assert after == count or (after == count + 1000), f"{after} vs {count}" - "# - ); - }) -} diff --git a/vendor/pyo3/tests/test_intopyobject.rs b/vendor/pyo3/tests/test_intopyobject.rs deleted file mode 100644 index 971663b05d..0000000000 --- a/vendor/pyo3/tests/test_intopyobject.rs +++ /dev/null @@ -1,201 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::types::{PyDict, PyString}; -use pyo3::{prelude::*, IntoPyObject}; -use std::collections::HashMap; -use std::hash::Hash; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -#[derive(Debug, IntoPyObject)] -pub struct A<'py> { - s: String, - t: Bound<'py, PyString>, - p: Bound<'py, PyAny>, -} - -#[test] -fn test_named_fields_struct() { - Python::with_gil(|py| { - let a = A { - s: "Hello".into(), - t: PyString::new(py, "World"), - p: 42i32.into_pyobject(py).unwrap().into_any(), - }; - let pya = a.into_pyobject(py).unwrap(); - assert_eq!( - pya.get_item("s") - .unwrap() - .unwrap() - .downcast::() - .unwrap(), - "Hello" - ); - assert_eq!( - pya.get_item("t") - .unwrap() - .unwrap() - .downcast::() - .unwrap(), - "World" - ); - assert_eq!( - pya.get_item("p") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 42 - ); - }); -} - -#[derive(Debug, IntoPyObject)] -#[pyo3(transparent)] -pub struct B<'a> { - test: &'a str, -} - -#[test] -fn test_transparent_named_field_struct() { - Python::with_gil(|py| { - let pyb = B { test: "test" }.into_pyobject(py).unwrap(); - let b = pyb.extract::().unwrap(); - assert_eq!(b, "test"); - }); -} - -#[derive(Debug, IntoPyObject)] -#[pyo3(transparent)] -pub struct D { - test: T, -} - -#[test] -fn test_generic_transparent_named_field_struct() { - Python::with_gil(|py| { - let pyd = D { - test: String::from("test"), - } - .into_pyobject(py) - .unwrap(); - let d = pyd.extract::().unwrap(); - assert_eq!(d, "test"); - - let pyd = D { test: 1usize }.into_pyobject(py).unwrap(); - let d = pyd.extract::().unwrap(); - assert_eq!(d, 1); - }); -} - -#[derive(Debug, IntoPyObject)] -pub struct GenericWithBound(HashMap); - -#[test] -fn test_generic_with_bound() { - Python::with_gil(|py| { - let mut hash_map = HashMap::::new(); - hash_map.insert("1".into(), 1); - hash_map.insert("2".into(), 2); - let map = GenericWithBound(hash_map).into_pyobject(py).unwrap(); - assert_eq!(map.len(), 2); - assert_eq!( - map.get_item("1") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 1 - ); - assert_eq!( - map.get_item("2") - .unwrap() - .unwrap() - .extract::() - .unwrap(), - 2 - ); - assert!(map.get_item("3").unwrap().is_none()); - }); -} - -#[derive(Debug, IntoPyObject)] -pub struct Tuple(String, usize); - -#[test] -fn test_tuple_struct() { - Python::with_gil(|py| { - let tup = Tuple(String::from("test"), 1).into_pyobject(py).unwrap(); - assert!(tup.extract::<(usize, String)>().is_err()); - let tup = tup.extract::<(String, usize)>().unwrap(); - assert_eq!(tup.0, "test"); - assert_eq!(tup.1, 1); - }); -} - -#[derive(Debug, IntoPyObject)] -pub struct TransparentTuple(String); - -#[test] -fn test_transparent_tuple_struct() { - Python::with_gil(|py| { - let tup = TransparentTuple(String::from("test")) - .into_pyobject(py) - .unwrap(); - assert!(tup.extract::<(String,)>().is_err()); - let tup = tup.extract::().unwrap(); - assert_eq!(tup, "test"); - }); -} - -#[derive(Debug, IntoPyObject)] -pub enum Foo<'py> { - TupleVar(usize, String), - StructVar { - test: Bound<'py, PyString>, - }, - #[pyo3(transparent)] - TransparentTuple(usize), - #[pyo3(transparent)] - TransparentStructVar { - a: Option, - }, -} - -#[test] -fn test_enum() { - Python::with_gil(|py| { - let foo = Foo::TupleVar(1, "test".into()).into_pyobject(py).unwrap(); - assert_eq!( - foo.extract::<(usize, String)>().unwrap(), - (1, String::from("test")) - ); - - let foo = Foo::StructVar { - test: PyString::new(py, "test"), - } - .into_pyobject(py) - .unwrap() - .downcast_into::() - .unwrap(); - - assert_eq!( - foo.get_item("test") - .unwrap() - .unwrap() - .downcast_into::() - .unwrap(), - "test" - ); - - let foo = Foo::TransparentTuple(1).into_pyobject(py).unwrap(); - assert_eq!(foo.extract::().unwrap(), 1); - - let foo = Foo::TransparentStructVar { a: None } - .into_pyobject(py) - .unwrap(); - assert!(foo.is_none()); - }); -} diff --git a/vendor/pyo3/tests/test_macro_docs.rs b/vendor/pyo3/tests/test_macro_docs.rs deleted file mode 100644 index 42159be9d3..0000000000 --- a/vendor/pyo3/tests/test_macro_docs.rs +++ /dev/null @@ -1,40 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -/// The MacroDocs class. -#[doc = concat!("Some macro ", "class ", "docs.")] -/// A very interesting type! -struct MacroDocs {} - -#[pymethods] -impl MacroDocs { - #[doc = concat!("A macro ", "example.")] - /// With mixed doc types. - fn macro_doc(&self) {} -} - -#[test] -fn meth_doc() { - Python::with_gil(|py| { - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!( - py, - *d, - "C.__doc__ == 'The MacroDocs class.\\nSome macro class docs.\\nA very interesting type!'" - ); - py_assert!( - py, - *d, - "C.macro_doc.__doc__ == 'A macro example.\\nWith mixed doc types.'" - ); - }); -} diff --git a/vendor/pyo3/tests/test_macros.rs b/vendor/pyo3/tests/test_macros.rs deleted file mode 100644 index 4b5feddf3f..0000000000 --- a/vendor/pyo3/tests/test_macros.rs +++ /dev/null @@ -1,94 +0,0 @@ -#![cfg(feature = "macros")] - -//! Ensure that pyo3 macros can be used inside macro_rules! - -use pyo3::prelude::*; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -macro_rules! make_struct_using_macro { - // Ensure that one doesn't need to fall back on the escape type: tt - // in order to macro create pyclass. - ($class_name:ident, $py_name:literal) => { - #[pyclass(name=$py_name, subclass)] - struct $class_name {} - }; -} - -make_struct_using_macro!(MyBaseClass, "MyClass"); - -macro_rules! set_extends_via_macro { - ($class_name:ident, $base_class:path) => { - // Try and pass a variable into the extends parameter - #[allow(dead_code)] - #[pyclass(extends=$base_class)] - struct $class_name {} - }; -} - -set_extends_via_macro!(MyClass2, MyBaseClass); - -// -// Check that pyfunctiona nd text_signature can be called with macro arguments. -// - -macro_rules! fn_macro { - ($sig:literal, $a_exp:expr, $b_exp:expr, $c_exp: expr) => { - // Try and pass a variable into the signature parameter - #[pyfunction(signature = ($a_exp, $b_exp, *, $c_exp))] - #[pyo3(text_signature = $sig)] - fn my_function_in_macro(a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - }; -} - -fn_macro!("(a, b=None, *, c=42)", a, b = None, c = 42); - -macro_rules! property_rename_via_macro { - ($prop_name:ident) => { - #[pyclass] - struct ClassWithProperty { - member: u64, - } - - #[pymethods] - impl ClassWithProperty { - #[getter($prop_name)] - fn get_member(&self) -> u64 { - self.member - } - - #[setter($prop_name)] - fn set_member(&mut self, member: u64) { - self.member = member; - } - } - }; -} - -property_rename_via_macro!(my_new_property_name); - -#[test] -fn test_macro_rules_interactions() { - Python::with_gil(|py| { - let my_base = py.get_type::(); - py_assert!(py, my_base, "my_base.__name__ == 'MyClass'"); - - let my_func = wrap_pyfunction!(my_function_in_macro, py).unwrap(); - py_assert!( - py, - my_func, - "my_func.__text_signature__ == '(a, b=None, *, c=42)'" - ); - - let renamed_prop = py.get_type::(); - py_assert!( - py, - renamed_prop, - "hasattr(renamed_prop, 'my_new_property_name')" - ); - }); -} diff --git a/vendor/pyo3/tests/test_mapping.rs b/vendor/pyo3/tests/test_mapping.rs deleted file mode 100644 index ba65c64722..0000000000 --- a/vendor/pyo3/tests/test_mapping.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![cfg(feature = "macros")] - -use std::collections::HashMap; - -use pyo3::exceptions::PyKeyError; -use pyo3::prelude::*; -use pyo3::py_run; -use pyo3::types::IntoPyDict; -use pyo3::types::PyList; -use pyo3::types::PyMapping; -use pyo3::types::PySequence; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass(mapping)] -struct Mapping { - index: HashMap, -} - -#[pymethods] -impl Mapping { - #[new] - #[pyo3(signature=(elements=None))] - fn new(elements: Option<&Bound<'_, PyList>>) -> PyResult { - if let Some(pylist) = elements { - let mut elems = HashMap::with_capacity(pylist.len()); - for (i, pyelem) in pylist.into_iter().enumerate() { - let elem = pyelem.extract()?; - elems.insert(elem, i); - } - Ok(Self { index: elems }) - } else { - Ok(Self { - index: HashMap::new(), - }) - } - } - - fn __len__(&self) -> usize { - self.index.len() - } - - fn __getitem__(&self, query: String) -> PyResult { - self.index - .get(&query) - .copied() - .ok_or_else(|| PyKeyError::new_err("unknown key")) - } - - fn __setitem__(&mut self, key: String, value: usize) { - self.index.insert(key, value); - } - - fn __delitem__(&mut self, key: String) -> PyResult<()> { - if self.index.remove(&key).is_none() { - Err(PyKeyError::new_err("unknown key")) - } else { - Ok(()) - } - } - - #[pyo3(signature=(key, default=None))] - fn get( - &self, - py: Python<'_>, - key: &str, - default: Option, - ) -> PyResult> { - match self.index.get(key) { - Some(value) => Ok(Some(value.into_pyobject(py)?.into_any().unbind())), - None => Ok(default), - } - } -} - -/// Return a dict with `m = Mapping(['1', '2', '3'])`. -fn map_dict(py: Python<'_>) -> Bound<'_, pyo3::types::PyDict> { - let d = [("Mapping", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_run!(py, *d, "m = Mapping(['1', '2', '3'])"); - d -} - -#[test] -fn test_getitem() { - Python::with_gil(|py| { - let d = map_dict(py); - - py_assert!(py, *d, "m['1'] == 0"); - py_assert!(py, *d, "m['2'] == 1"); - py_assert!(py, *d, "m['3'] == 2"); - py_expect_exception!(py, *d, "print(m['4'])", PyKeyError); - }); -} - -#[test] -fn test_setitem() { - Python::with_gil(|py| { - let d = map_dict(py); - - py_run!(py, *d, "m['1'] = 4; assert m['1'] == 4"); - py_run!(py, *d, "m['0'] = 0; assert m['0'] == 0"); - py_assert!(py, *d, "len(m) == 4"); - py_expect_exception!(py, *d, "m[0] = 'hello'", PyTypeError); - py_expect_exception!(py, *d, "m[0] = -1", PyTypeError); - }); -} - -#[test] -fn test_delitem() { - Python::with_gil(|py| { - let d = map_dict(py); - py_run!( - py, - *d, - "del m['1']; assert len(m) == 2 and m['2'] == 1 and m['3'] == 2" - ); - py_expect_exception!(py, *d, "del m[-1]", PyTypeError); - py_expect_exception!(py, *d, "del m['4']", PyKeyError); - }); -} - -#[test] -fn mapping_is_not_sequence() { - Python::with_gil(|py| { - let mut index = HashMap::new(); - index.insert("Foo".into(), 1); - index.insert("Bar".into(), 2); - let m = Py::new(py, Mapping { index }).unwrap(); - - PyMapping::register::(py).unwrap(); - - assert!(m.bind(py).downcast::().is_ok()); - assert!(m.bind(py).downcast::().is_err()); - }); -} diff --git a/vendor/pyo3/tests/test_methods.rs b/vendor/pyo3/tests/test_methods.rs deleted file mode 100644 index 743fa6e6b4..0000000000 --- a/vendor/pyo3/tests/test_methods.rs +++ /dev/null @@ -1,1204 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::py_run; -use pyo3::types::PySequence; -use pyo3::types::{IntoPyDict, PyDict, PyList, PySet, PyString, PyTuple, PyType}; -use pyo3::BoundObject; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct InstanceMethod { - member: i32, -} - -#[pymethods] -impl InstanceMethod { - /// Test method - fn method(&self) -> i32 { - self.member - } - - // Checks that &Self works - fn add_other(&self, other: &Self) -> i32 { - self.member + other.member - } -} - -#[test] -fn instance_method() { - Python::with_gil(|py| { - let obj = Bound::new(py, InstanceMethod { member: 42 }).unwrap(); - let obj_ref = obj.borrow(); - assert_eq!(obj_ref.method(), 42); - py_assert!(py, obj, "obj.method() == 42"); - py_assert!(py, obj, "obj.add_other(obj) == 84"); - py_assert!(py, obj, "obj.method.__doc__ == 'Test method'"); - }); -} - -#[pyclass] -struct InstanceMethodWithArgs { - member: i32, -} - -#[pymethods] -impl InstanceMethodWithArgs { - fn method(&self, multiplier: i32) -> i32 { - self.member * multiplier - } -} - -#[test] -fn instance_method_with_args() { - Python::with_gil(|py| { - let obj = Bound::new(py, InstanceMethodWithArgs { member: 7 }).unwrap(); - let obj_ref = obj.borrow(); - assert_eq!(obj_ref.method(6), 42); - py_assert!(py, obj, "obj.method(3) == 21"); - py_assert!(py, obj, "obj.method(multiplier=6) == 42"); - }); -} - -#[pyclass] -struct ClassMethod {} - -#[pymethods] -impl ClassMethod { - #[new] - fn new() -> Self { - ClassMethod {} - } - - #[classmethod] - /// Test class method. - fn method(cls: &Bound<'_, PyType>) -> PyResult { - Ok(format!("{}.method()!", cls.qualname()?)) - } - - #[classmethod] - fn method_owned(cls: Py, py: Python<'_>) -> PyResult { - Ok(format!("{}.method_owned()!", cls.bind(py).qualname()?)) - } -} - -#[test] -fn class_method() { - Python::with_gil(|py| { - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!(py, *d, "C.method() == 'ClassMethod.method()!'"); - py_assert!(py, *d, "C().method() == 'ClassMethod.method()!'"); - py_assert!( - py, - *d, - "C().method_owned() == 'ClassMethod.method_owned()!'" - ); - py_assert!(py, *d, "C.method.__doc__ == 'Test class method.'"); - py_assert!(py, *d, "C().method.__doc__ == 'Test class method.'"); - }); -} - -#[pyclass] -struct ClassMethodWithArgs {} - -#[pymethods] -impl ClassMethodWithArgs { - #[classmethod] - fn method(cls: &Bound<'_, PyType>, input: &Bound<'_, PyString>) -> PyResult { - Ok(format!("{}.method({})", cls.qualname()?, input)) - } -} - -#[test] -fn class_method_with_args() { - Python::with_gil(|py| { - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!( - py, - *d, - "C.method('abc') == 'ClassMethodWithArgs.method(abc)'" - ); - }); -} - -#[pyclass] -struct StaticMethod {} - -#[pymethods] -impl StaticMethod { - #[new] - fn new() -> Self { - StaticMethod {} - } - - #[staticmethod] - /// Test static method. - fn method(_py: Python<'_>) -> &'static str { - "StaticMethod.method()!" - } -} - -#[test] -fn static_method() { - Python::with_gil(|py| { - assert_eq!(StaticMethod::method(py), "StaticMethod.method()!"); - - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!(py, *d, "C.method() == 'StaticMethod.method()!'"); - py_assert!(py, *d, "C().method() == 'StaticMethod.method()!'"); - py_assert!(py, *d, "C.method.__doc__ == 'Test static method.'"); - py_assert!(py, *d, "C().method.__doc__ == 'Test static method.'"); - }); -} - -#[pyclass] -struct StaticMethodWithArgs {} - -#[pymethods] -impl StaticMethodWithArgs { - #[staticmethod] - fn method(_py: Python<'_>, input: i32) -> String { - format!("0x{:x}", input) - } -} - -#[test] -fn static_method_with_args() { - Python::with_gil(|py| { - assert_eq!(StaticMethodWithArgs::method(py, 1234), "0x4d2"); - - let d = [("C", py.get_type::())] - .into_py_dict(py) - .unwrap(); - py_assert!(py, *d, "C.method(1337) == '0x539'"); - }); -} - -#[pyclass] -struct MethSignature {} - -#[pymethods] -impl MethSignature { - #[pyo3(signature = (test = None))] - fn get_optional(&self, test: Option) -> i32 { - test.unwrap_or(10) - } - #[pyo3(signature = (test = None))] - fn get_optional2(&self, test: Option) -> Option { - test - } - #[pyo3(signature=(_t1 = None, t2 = None, _t3 = None))] - fn get_optional_positional( - &self, - _t1: Option, - t2: Option, - _t3: Option, - ) -> Option { - t2 - } - - #[pyo3(signature = (test = 10))] - fn get_default(&self, test: i32) -> i32 { - test - } - #[pyo3(signature = (*, test = 10))] - fn get_kwarg(&self, test: i32) -> i32 { - test - } - #[pyo3(signature = (*args, **kwargs))] - fn get_kwargs<'py>( - &self, - py: Python<'py>, - args: &Bound<'py, PyTuple>, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - [ - args.as_any().clone(), - kwargs.into_pyobject(py)?.into_any().into_bound(), - ] - .into_pyobject(py) - } - - #[pyo3(signature = (a, *args, **kwargs))] - fn get_pos_arg_kw<'py>( - &self, - py: Python<'py>, - a: i32, - args: &Bound<'py, PyTuple>, - kwargs: Option<&Bound<'py, PyDict>>, - ) -> PyResult> { - [ - a.into_pyobject(py)?.into_any().into_bound(), - args.as_any().clone(), - kwargs.into_pyobject(py)?.into_any().into_bound(), - ] - .into_pyobject(py) - } - - #[pyo3(signature = (a, b, /))] - fn get_pos_only(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (a, /, b))] - fn get_pos_only_and_pos(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (a, /, b, c = 5))] - fn get_pos_only_and_pos_and_kw(&self, a: i32, b: i32, c: i32) -> i32 { - a + b + c - } - - #[pyo3(signature = (a, /, *, b))] - fn get_pos_only_and_kw_only(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (a, /, *, b = 3))] - fn get_pos_only_and_kw_only_with_default(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (a, /, b, *, c, d = 5))] - fn get_all_arg_types_together(&self, a: i32, b: i32, c: i32, d: i32) -> i32 { - a + b + c + d - } - - #[pyo3(signature = (a, /, *args))] - fn get_pos_only_with_varargs(&self, a: i32, args: Vec) -> i32 { - a + args.iter().sum::() - } - - #[pyo3(signature = (a, /, **kwargs))] - fn get_pos_only_with_kwargs( - &self, - py: Python<'_>, - a: i32, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult { - [ - a.into_pyobject(py)?.into_any().into_bound(), - kwargs.into_pyobject(py)?.into_any().into_bound(), - ] - .into_pyobject(py) - .map(Bound::unbind) - } - - #[pyo3(signature = (a=0, /, **kwargs))] - fn get_optional_pos_only_with_kwargs( - &self, - py: Python<'_>, - a: i32, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult { - [ - a.into_pyobject(py)?.into_any().into_bound(), - kwargs.into_pyobject(py)?.into_any().into_bound(), - ] - .into_pyobject(py) - .map(Bound::unbind) - } - - #[pyo3(signature = (*, a = 2, b = 3))] - fn get_kwargs_only_with_defaults(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (*, a, b))] - fn get_kwargs_only(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (*, a = 1, b))] - fn get_kwargs_only_with_some_default(&self, a: i32, b: i32) -> i32 { - a + b - } - - #[pyo3(signature = (*args, a))] - fn get_args_and_required_keyword( - &self, - py: Python<'_>, - args: &Bound<'_, PyTuple>, - a: i32, - ) -> PyResult { - (args, a) - .into_pyobject(py) - .map(BoundObject::into_any) - .map(Bound::unbind) - } - - #[pyo3(signature = (a, b = 2, *, c = 3))] - fn get_pos_arg_kw_sep1(&self, a: i32, b: i32, c: i32) -> i32 { - a + b + c - } - - #[pyo3(signature = (a, *, b = 2, c = 3))] - fn get_pos_arg_kw_sep2(&self, a: i32, b: i32, c: i32) -> i32 { - a + b + c - } - - #[pyo3(signature = (a, **kwargs))] - fn get_pos_kw( - &self, - py: Python<'_>, - a: i32, - kwargs: Option<&Bound<'_, PyDict>>, - ) -> PyResult { - [ - a.into_pyobject(py)?.into_any().into_bound(), - kwargs.into_pyobject(py)?.into_any().into_bound(), - ] - .into_pyobject(py) - .map(Bound::unbind) - } - - // "args" can be anything that can be extracted from PyTuple - #[pyo3(signature = (*args))] - fn args_as_vec(&self, args: Vec) -> i32 { - args.iter().sum() - } -} - -#[test] -fn meth_signature() { - Python::with_gil(|py| { - let inst = Py::new(py, MethSignature {}).unwrap(); - - py_run!(py, inst, "assert inst.get_optional() == 10"); - py_run!(py, inst, "assert inst.get_optional(100) == 100"); - py_run!(py, inst, "assert inst.get_optional2() == None"); - py_run!(py, inst, "assert inst.get_optional2(100) == 100"); - py_run!( - py, - inst, - "assert inst.get_optional_positional(1, 2, 3) == 2" - ); - py_run!(py, inst, "assert inst.get_optional_positional(1) == None"); - py_run!(py, inst, "assert inst.get_default() == 10"); - py_run!(py, inst, "assert inst.get_default(100) == 100"); - py_run!(py, inst, "assert inst.get_kwarg() == 10"); - py_expect_exception!(py, inst, "inst.get_kwarg(100)", PyTypeError); - py_run!(py, inst, "assert inst.get_kwarg(test=100) == 100"); - py_run!(py, inst, "assert inst.get_kwargs() == [(), None]"); - py_run!(py, inst, "assert inst.get_kwargs(1,2,3) == [(1,2,3), None]"); - py_run!( - py, - inst, - "assert inst.get_kwargs(t=1,n=2) == [(), {'t': 1, 'n': 2}]" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs(1,2,3,t=1,n=2) == [(1,2,3), {'t': 1, 'n': 2}]" - ); - - py_run!(py, inst, "assert inst.get_pos_arg_kw(1) == [1, (), None]"); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw(1, 2, 3) == [1, (2, 3), None]" - ); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw(1, b=2) == [1, (), {'b': 2}]" - ); - py_run!(py, inst, "assert inst.get_pos_arg_kw(a=1) == [1, (), None]"); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw()", PyTypeError); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw(1, a=1)", PyTypeError); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw(b=2)", PyTypeError); - - py_run!(py, inst, "assert inst.get_pos_only(10, 11) == 21"); - py_expect_exception!(py, inst, "inst.get_pos_only(10, b = 11)", PyTypeError); - py_expect_exception!(py, inst, "inst.get_pos_only(a = 10, b = 11)", PyTypeError); - - py_run!(py, inst, "assert inst.get_pos_only_and_pos(10, 11) == 21"); - py_run!( - py, - inst, - "assert inst.get_pos_only_and_pos(10, b = 11) == 21" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_pos(a = 10, b = 11)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_pos_only_and_pos_and_kw(10, 11) == 26" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_and_pos_and_kw(10, b = 11) == 26" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_and_pos_and_kw(10, 11, c = 0) == 21" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_and_pos_and_kw(10, b = 11, c = 0) == 21" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_pos_and_kw(a = 10, b = 11)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_pos_only_and_kw_only(10, b = 11) == 21" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_kw_only(10, 11)", - PyTypeError - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_kw_only(a = 10, b = 11)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_pos_only_and_kw_only_with_default(10) == 13" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_and_kw_only_with_default(10, b = 11) == 21" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_kw_only_with_default(10, 11)", - PyTypeError - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_and_kw_only_with_default(a = 10, b = 11)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_all_arg_types_together(10, 10, c = 10) == 35" - ); - py_run!( - py, - inst, - "assert inst.get_all_arg_types_together(10, 10, c = 10, d = 10) == 40" - ); - py_run!( - py, - inst, - "assert inst.get_all_arg_types_together(10, b = 10, c = 10, d = 10) == 40" - ); - py_expect_exception!( - py, - inst, - "inst.get_all_arg_types_together(10, 10, 10)", - PyTypeError - ); - py_expect_exception!( - py, - inst, - "inst.get_all_arg_types_together(a = 10, b = 10, c = 10)", - PyTypeError - ); - - py_run!(py, inst, "assert inst.get_pos_only_with_varargs(10) == 10"); - py_run!( - py, - inst, - "assert inst.get_pos_only_with_varargs(10, 10) == 20" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_with_varargs(10, 10, 10, 10, 10) == 50" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_with_varargs(a = 10)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_pos_only_with_kwargs(10) == [10, None]" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_with_kwargs(10, b = 10) == [10, {'b': 10}]" - ); - py_run!( - py, - inst, - "assert inst.get_pos_only_with_kwargs(10, b = 10, c = 10, d = 10, e = 10) == [10, {'b': 10, 'c': 10, 'd': 10, 'e': 10}]" - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_with_kwargs(a = 10)", - PyTypeError - ); - py_expect_exception!( - py, - inst, - "inst.get_pos_only_with_kwargs(a = 10, b = 10)", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_optional_pos_only_with_kwargs() == [0, None]" - ); - py_run!( - py, - inst, - "assert inst.get_optional_pos_only_with_kwargs(10) == [10, None]" - ); - py_run!( - py, - inst, - "assert inst.get_optional_pos_only_with_kwargs(a=10) == [0, {'a': 10}]" - ); - - py_run!(py, inst, "assert inst.get_kwargs_only_with_defaults() == 5"); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_defaults(a = 8) == 11" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_defaults(b = 8) == 10" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_defaults(a = 1, b = 1) == 2" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_defaults(b = 1, a = 1) == 2" - ); - - py_run!(py, inst, "assert inst.get_kwargs_only(a = 1, b = 1) == 2"); - py_run!(py, inst, "assert inst.get_kwargs_only(b = 1, a = 1) == 2"); - - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_some_default(a = 2, b = 1) == 3" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_some_default(b = 1) == 2" - ); - py_run!( - py, - inst, - "assert inst.get_kwargs_only_with_some_default(b = 1, a = 2) == 3" - ); - py_expect_exception!( - py, - inst, - "inst.get_kwargs_only_with_some_default()", - PyTypeError - ); - - py_run!( - py, - inst, - "assert inst.get_args_and_required_keyword(1, 2, a=3) == ((1, 2), 3)" - ); - py_run!( - py, - inst, - "assert inst.get_args_and_required_keyword(a=1) == ((), 1)" - ); - py_expect_exception!( - py, - inst, - "inst.get_args_and_required_keyword()", - PyTypeError - ); - - py_run!(py, inst, "assert inst.get_pos_arg_kw_sep1(1) == 6"); - py_run!(py, inst, "assert inst.get_pos_arg_kw_sep1(1, 2) == 6"); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw_sep1(1, 2, c=13) == 16" - ); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw_sep1(a=1, b=2, c=13) == 16" - ); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw_sep1(b=2, c=13, a=1) == 16" - ); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw_sep1(c=13, b=2, a=1) == 16" - ); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep1(1, 2, 3)", PyTypeError); - - py_run!(py, inst, "assert inst.get_pos_arg_kw_sep2(1) == 6"); - py_run!( - py, - inst, - "assert inst.get_pos_arg_kw_sep2(1, b=12, c=13) == 26" - ); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep2(1, 2)", PyTypeError); - - py_run!(py, inst, "assert inst.get_pos_kw(1, b=2) == [1, {'b': 2}]"); - py_expect_exception!(py, inst, "inst.get_pos_kw(1,2)", PyTypeError); - - py_run!(py, inst, "assert inst.args_as_vec(1,2,3) == 6"); - }); -} - -#[pyclass] -/// A class with "documentation". -struct MethDocs { - x: i32, -} - -#[pymethods] -impl MethDocs { - /// A method with "documentation" as well. - fn method(&self) -> i32 { - 0 - } - - #[getter] - /// `int`: a very "important" member of 'this' instance. - fn get_x(&self) -> i32 { - self.x - } -} - -#[test] -fn meth_doc() { - Python::with_gil(|py| { - let d = [("C", py.get_type::())].into_py_dict(py).unwrap(); - py_assert!(py, *d, "C.__doc__ == 'A class with \"documentation\".'"); - py_assert!( - py, - *d, - "C.method.__doc__ == 'A method with \"documentation\" as well.'" - ); - py_assert!( - py, - *d, - "C.x.__doc__ == '`int`: a very \"important\" member of \\'this\\' instance.'" - ); - }); -} - -#[pyclass] -struct MethodWithLifeTime {} - -#[pymethods] -impl MethodWithLifeTime { - fn set_to_list<'py>(&self, set: &Bound<'py, PySet>) -> PyResult> { - let py = set.py(); - let mut items = vec![]; - for _ in 0..set.len() { - items.push(set.pop().unwrap()); - } - let list = PyList::new(py, items)?; - list.sort()?; - Ok(list) - } -} - -#[test] -fn method_with_lifetime() { - Python::with_gil(|py| { - let obj = Py::new(py, MethodWithLifeTime {}).unwrap(); - py_run!( - py, - obj, - "assert obj.set_to_list(set((1, 2, 3))) == [1, 2, 3]" - ); - }); -} - -#[pyclass] -struct MethodWithPyClassArg { - #[pyo3(get)] - value: i64, -} - -#[pymethods] -impl MethodWithPyClassArg { - fn add(&self, other: &MethodWithPyClassArg) -> MethodWithPyClassArg { - MethodWithPyClassArg { - value: self.value + other.value, - } - } - fn add_pyref(&self, other: PyRef<'_, MethodWithPyClassArg>) -> MethodWithPyClassArg { - MethodWithPyClassArg { - value: self.value + other.value, - } - } - fn inplace_add(&self, other: &mut MethodWithPyClassArg) { - other.value += self.value; - } - fn inplace_add_pyref(&self, mut other: PyRefMut<'_, MethodWithPyClassArg>) { - other.value += self.value; - } - #[pyo3(signature=(other = None))] - fn optional_add(&self, other: Option<&MethodWithPyClassArg>) -> MethodWithPyClassArg { - MethodWithPyClassArg { - value: self.value + other.map(|o| o.value).unwrap_or(10), - } - } - #[pyo3(signature=(other = None))] - fn optional_inplace_add(&self, other: Option<&mut MethodWithPyClassArg>) { - if let Some(other) = other { - other.value += self.value; - } - } -} - -#[test] -fn method_with_pyclassarg() { - Python::with_gil(|py| { - let obj1 = Py::new(py, MethodWithPyClassArg { value: 10 }).unwrap(); - let obj2 = Py::new(py, MethodWithPyClassArg { value: 10 }).unwrap(); - let d = [("obj1", obj1), ("obj2", obj2)].into_py_dict(py).unwrap(); - py_run!(py, *d, "obj = obj1.add(obj2); assert obj.value == 20"); - py_run!(py, *d, "obj = obj1.add_pyref(obj2); assert obj.value == 20"); - py_run!(py, *d, "obj = obj1.optional_add(); assert obj.value == 20"); - py_run!( - py, - *d, - "obj = obj1.optional_add(obj2); assert obj.value == 20" - ); - py_run!(py, *d, "obj1.inplace_add(obj2); assert obj.value == 20"); - py_run!( - py, - *d, - "obj1.inplace_add_pyref(obj2); assert obj2.value == 30" - ); - py_run!( - py, - *d, - "obj1.optional_inplace_add(); assert obj2.value == 30" - ); - py_run!( - py, - *d, - "obj1.optional_inplace_add(obj2); assert obj2.value == 40" - ); - }); -} - -#[pyclass] -#[cfg(unix)] -struct CfgStruct {} - -#[pyclass] -#[cfg(not(unix))] -struct CfgStruct {} - -#[pymethods] -#[cfg(unix)] -impl CfgStruct { - fn unix_method(&self) -> &str { - "unix" - } - - #[cfg(not(unix))] - fn never_compiled_method(&self) {} -} - -#[pymethods] -#[cfg(not(unix))] -impl CfgStruct { - fn not_unix_method(&self) -> &str { - "not unix" - } - - #[cfg(unix)] - fn never_compiled_method(&self) {} -} - -#[test] -fn test_cfg_attrs() { - Python::with_gil(|py| { - let inst = Py::new(py, CfgStruct {}).unwrap(); - - #[cfg(unix)] - { - py_assert!(py, inst, "inst.unix_method() == 'unix'"); - py_assert!(py, inst, "not hasattr(inst, 'not_unix_method')"); - } - - #[cfg(not(unix))] - { - py_assert!(py, inst, "not hasattr(inst, 'unix_method')"); - py_assert!(py, inst, "inst.not_unix_method() == 'not unix'"); - } - - py_assert!(py, inst, "not hasattr(inst, 'never_compiled_method')"); - }); -} - -#[pyclass] -#[derive(Default)] -struct FromSequence { - #[pyo3(get)] - numbers: Vec, -} - -#[pymethods] -impl FromSequence { - #[new] - #[pyo3(signature=(seq = None))] - fn new(seq: Option<&Bound<'_, PySequence>>) -> PyResult { - if let Some(seq) = seq { - Ok(FromSequence { - numbers: seq.as_ref().extract::>()?, - }) - } else { - Ok(FromSequence::default()) - } - } -} - -#[test] -fn test_from_sequence() { - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_assert!(py, typeobj, "typeobj(range(0, 4)).numbers == [0, 1, 2, 3]"); - }); -} - -#[pyclass] -struct r#RawIdents { - #[pyo3(get, set)] - r#type: PyObject, - r#subtype: PyObject, - r#subsubtype: PyObject, -} - -#[pymethods] -impl r#RawIdents { - #[new] - pub fn r#new( - r#_py: Python<'_>, - r#type: PyObject, - r#subtype: PyObject, - r#subsubtype: PyObject, - ) -> Self { - Self { - r#type, - r#subtype, - r#subsubtype, - } - } - - #[getter(r#subtype)] - pub fn r#get_subtype(&self, py: Python<'_>) -> PyObject { - self.r#subtype.clone_ref(py) - } - - #[setter(r#subtype)] - pub fn r#set_subtype(&mut self, r#subtype: PyObject) { - self.r#subtype = r#subtype; - } - - #[getter] - pub fn r#get_subsubtype(&self, py: Python<'_>) -> PyObject { - self.r#subsubtype.clone_ref(py) - } - - #[setter] - pub fn r#set_subsubtype(&mut self, r#subsubtype: PyObject) { - self.r#subsubtype = r#subsubtype; - } - - pub fn r#__call__(&mut self, r#type: PyObject) { - self.r#type = r#type; - } - - #[staticmethod] - pub fn r#static_method(r#type: PyObject) -> PyObject { - r#type - } - - #[classmethod] - pub fn r#class_method(_: &Bound<'_, PyType>, r#type: PyObject) -> PyObject { - r#type - } - - #[classattr] - pub fn r#class_attr_fn() -> i32 { - 5 - } - - #[classattr] - const r#CLASS_ATTR_CONST: i32 = 6; - - #[pyo3(signature = (r#struct = "foo"))] - fn method_with_keyword<'a>(&self, r#struct: &'a str) -> &'a str { - r#struct - } -} - -#[test] -fn test_raw_idents() { - Python::with_gil(|py| { - let raw_idents_type = py.get_type::(); - assert_eq!(raw_idents_type.qualname().unwrap(), "RawIdents"); - py_run!( - py, - raw_idents_type, - r#" - instance = raw_idents_type(type=None, subtype=5, subsubtype="foo") - - assert instance.type is None - assert instance.subtype == 5 - assert instance.subsubtype == "foo" - - instance.type = 1 - instance.subtype = 2 - instance.subsubtype = 3 - - assert instance.type == 1 - assert instance.subtype == 2 - assert instance.subsubtype == 3 - - assert raw_idents_type.static_method(type=30) == 30 - assert instance.class_method(type=40) == 40 - - instance(type=50) - assert instance.type == 50 - - assert raw_idents_type.class_attr_fn == 5 - assert raw_idents_type.CLASS_ATTR_CONST == 6 - - assert instance.method_with_keyword() == "foo" - assert instance.method_with_keyword("bar") == "bar" - assert instance.method_with_keyword(struct="baz") == "baz" - "# - ); - }) -} - -// Regression test for issue 1505 - Python argument not detected correctly when inside a macro. - -#[pyclass] -struct Issue1505 {} - -macro_rules! pymethods { - ( - #[pymethods] - impl $ty: ty { - fn $fn:ident (&self, $arg:ident : $arg_ty:ty) {} - } - ) => { - #[pymethods] - impl $ty { - fn $fn(&self, $arg: $arg_ty) {} - } - }; -} - -pymethods!( - #[pymethods] - impl Issue1505 { - fn issue_1505(&self, _py: Python<'_>) {} - } -); - -// Regression test for issue 1506 - incorrect macro hygiene. -// By applying the `#[pymethods]` attribute inside a macro_rules! macro, this separates the macro -// call scope from the scope of the impl block. For this to work our macros must be careful to not -// cheat hygiene! - -#[pyclass] -struct Issue1506 {} - -macro_rules! issue_1506 { - (#[pymethods] $($body:tt)*) => { - #[pymethods] - $($body)* - }; -} - -issue_1506!( - #[pymethods] - impl Issue1506 { - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506( - &self, - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_mut( - &mut self, - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_custom_receiver( - _slf: Py, - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_custom_receiver_explicit( - _slf: Py, - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - - #[new] - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_new( - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) -> Self { - Issue1506 {} - } - - #[getter("foo")] - fn issue_1506_getter(&self, _py: Python<'_>) -> i32 { - 5 - } - - #[setter("foo")] - fn issue_1506_setter(&self, _py: Python<'_>, _value: i32) {} - - #[staticmethod] - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_static( - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - - #[classmethod] - #[pyo3(signature = (_arg, _args, _kwargs=None))] - fn issue_1506_class( - _cls: &Bound<'_, PyType>, - _py: Python<'_>, - _arg: &Bound<'_, PyAny>, - _args: &Bound<'_, PyTuple>, - _kwargs: Option<&Bound<'_, PyDict>>, - ) { - } - } -); - -#[pyclass] -struct Issue1696 {} - -pymethods!( - #[pymethods] - impl Issue1696 { - fn issue_1696(&self, _x: &InstanceMethod) {} - } -); - -#[test] -fn test_option_pyclass_arg() { - // Option<&PyClass> argument with a default set in a signature regressed to a compile - // error in PyO3 0.17.0 - this test it continues to be accepted. - - #[pyclass] - struct SomePyClass {} - - #[pyfunction(signature = (arg=None))] - fn option_class_arg(arg: Option<&SomePyClass>) -> Option { - arg.map(|_| SomePyClass {}) - } - - Python::with_gil(|py| { - let f = wrap_pyfunction!(option_class_arg, py).unwrap(); - assert!(f.call0().unwrap().is_none()); - let obj = Py::new(py, SomePyClass {}).unwrap(); - assert!(f - .call1((obj,)) - .unwrap() - .extract::>() - .is_ok()); - }) -} - -#[test] -fn test_issue_2988() { - #[pyfunction] - #[pyo3(signature = ( - _data = vec![], - _data2 = vec![], - ))] - pub fn _foo( - _data: Vec, - // The from_py_with here looks a little odd, we just need some way - // to encourage the macro to expand the from_py_with default path too - #[pyo3(from_py_with = " as PyAnyMethods>::extract")] _data2: Vec, - ) { - } -} diff --git a/vendor/pyo3/tests/test_module.rs b/vendor/pyo3/tests/test_module.rs deleted file mode 100644 index 35b7d35433..0000000000 --- a/vendor/pyo3/tests/test_module.rs +++ /dev/null @@ -1,492 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -use pyo3::py_run; -use pyo3::types::PyString; -use pyo3::types::{IntoPyDict, PyDict, PyTuple}; -use pyo3::BoundObject; -use pyo3_ffi::c_str; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct AnonClass {} - -#[pyclass] -struct ValueClass { - value: usize, -} - -#[pymethods] -impl ValueClass { - #[new] - fn new(value: usize) -> ValueClass { - ValueClass { value } - } -} - -#[pyclass(module = "module")] -struct LocatedClass {} - -#[pyfunction] -/// Doubles the given value -fn double(x: usize) -> usize { - x * 2 -} - -/// This module is implemented in Rust. -#[pymodule(gil_used = false)] -fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> { - #[pyfn(m)] - #[pyo3(name = "no_parameters")] - fn function_with_name() -> usize { - 42 - } - - #[pyfn(m)] - #[pyo3(pass_module)] - fn with_module<'py>(module: &Bound<'py, PyModule>) -> PyResult> { - module.name() - } - - #[pyfn(m)] - fn double_value(v: &ValueClass) -> usize { - v.value * 2 - } - - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - m.add("foo", "bar")?; - - m.add_function(wrap_pyfunction!(double, m)?)?; - m.add("also_double", wrap_pyfunction!(double, m)?)?; - - Ok(()) -} - -#[test] -fn test_module_with_functions() { - use pyo3::wrap_pymodule; - - Python::with_gil(|py| { - let d = [( - "module_with_functions", - wrap_pymodule!(module_with_functions)(py), - )] - .into_py_dict(py) - .unwrap(); - - py_assert!( - py, - *d, - "module_with_functions.__doc__ == 'This module is implemented in Rust.'" - ); - py_assert!(py, *d, "module_with_functions.no_parameters() == 42"); - py_assert!(py, *d, "module_with_functions.foo == 'bar'"); - py_assert!(py, *d, "module_with_functions.AnonClass != None"); - py_assert!(py, *d, "module_with_functions.LocatedClass != None"); - py_assert!( - py, - *d, - "module_with_functions.LocatedClass.__module__ == 'module'" - ); - py_assert!(py, *d, "module_with_functions.double(3) == 6"); - py_assert!( - py, - *d, - "module_with_functions.double.__doc__ == 'Doubles the given value'" - ); - py_assert!(py, *d, "module_with_functions.also_double(3) == 6"); - py_assert!( - py, - *d, - "module_with_functions.also_double.__doc__ == 'Doubles the given value'" - ); - py_assert!( - py, - *d, - "module_with_functions.double_value(module_with_functions.ValueClass(1)) == 2" - ); - py_assert!( - py, - *d, - "module_with_functions.with_module() == 'module_with_functions'" - ); - }); -} - -/// This module uses a legacy two-argument module function. -#[pymodule] -fn module_with_explicit_py_arg(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; - Ok(()) -} - -#[test] -fn test_module_with_explicit_py_arg() { - use pyo3::wrap_pymodule; - - Python::with_gil(|py| { - let d = [( - "module_with_explicit_py_arg", - wrap_pymodule!(module_with_explicit_py_arg)(py), - )] - .into_py_dict(py) - .unwrap(); - - py_assert!(py, *d, "module_with_explicit_py_arg.double(3) == 6"); - }); -} - -#[pymodule(name = "other_name")] -fn some_name(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add("other_name", "other_name")?; - Ok(()) -} - -#[test] -fn test_module_renaming() { - use pyo3::wrap_pymodule; - - Python::with_gil(|py| { - let d = [("different_name", wrap_pymodule!(some_name)(py))] - .into_py_dict(py) - .unwrap(); - - py_run!(py, *d, "assert different_name.__name__ == 'other_name'"); - }); -} - -#[test] -fn test_module_from_code_bound() { - Python::with_gil(|py| { - let adder_mod = PyModule::from_code( - py, - c_str!("def add(a,b):\n\treturn a+b"), - c_str!("adder_mod.py"), - &common::generate_unique_module_name("adder_mod"), - ) - .expect("Module code should be loaded"); - - let add_func = adder_mod - .getattr("add") - .expect("Add function should be in the module"); - - let ret_value: i32 = add_func - .call1((1, 2)) - .expect("A value should be returned") - .extract() - .expect("The value should be able to be converted to an i32"); - - adder_mod.gil_used(false).expect("Disabling the GIL failed"); - - assert_eq!(ret_value, 3); - }); -} - -#[pyfunction] -fn r#move() -> usize { - 42 -} - -#[pymodule] -fn raw_ident_module(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_function(wrap_pyfunction!(r#move, module)?) -} - -#[test] -fn test_raw_idents() { - use pyo3::wrap_pymodule; - - Python::with_gil(|py| { - let module = wrap_pymodule!(raw_ident_module)(py); - - py_assert!(py, module, "module.move() == 42"); - }); -} - -#[pyfunction] -#[pyo3(name = "foobar")] -fn custom_named_fn() -> usize { - 42 -} - -#[test] -fn test_custom_names() { - #[pymodule] - fn custom_names(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?; - Ok(()) - } - - Python::with_gil(|py| { - let module = pyo3::wrap_pymodule!(custom_names)(py); - - py_assert!(py, module, "not hasattr(module, 'custom_named_fn')"); - py_assert!(py, module, "module.foobar() == 42"); - }); -} - -#[test] -fn test_module_dict() { - #[pymodule] - fn module_dict(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.dict().set_item("yay", "me")?; - Ok(()) - } - - Python::with_gil(|py| { - let module = pyo3::wrap_pymodule!(module_dict)(py); - - py_assert!(py, module, "module.yay == 'me'"); - }); -} - -#[test] -fn test_module_dunder_all() { - Python::with_gil(|py| { - #[pymodule] - fn dunder_all(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.dict().set_item("yay", "me")?; - m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?; - Ok(()) - } - - let module = pyo3::wrap_pymodule!(dunder_all)(py); - - py_assert!(py, module, "module.__all__ == ['foobar']"); - }); -} - -#[pyfunction] -fn subfunction() -> String { - "Subfunction".to_string() -} - -fn submodule(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_function(wrap_pyfunction!(subfunction, module)?)?; - Ok(()) -} - -#[pymodule] -fn submodule_with_init_fn(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_function(wrap_pyfunction!(subfunction, module)?)?; - Ok(()) -} - -#[pyfunction] -fn superfunction() -> String { - "Superfunction".to_string() -} - -#[pymodule] -fn supermodule(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_function(wrap_pyfunction!(superfunction, module)?)?; - let module_to_add = PyModule::new(module.py(), "submodule")?; - submodule(&module_to_add)?; - module.add_submodule(&module_to_add)?; - let module_to_add = PyModule::new(module.py(), "submodule_with_init_fn")?; - submodule_with_init_fn(&module_to_add)?; - module.add_submodule(&module_to_add)?; - Ok(()) -} - -#[test] -fn test_module_nesting() { - use pyo3::wrap_pymodule; - - Python::with_gil(|py| { - let supermodule = wrap_pymodule!(supermodule)(py); - - py_assert!( - py, - supermodule, - "supermodule.superfunction() == 'Superfunction'" - ); - py_assert!( - py, - supermodule, - "supermodule.submodule.subfunction() == 'Subfunction'" - ); - py_assert!( - py, - supermodule, - "supermodule.submodule_with_init_fn.subfunction() == 'Subfunction'" - ); - }); -} - -// Test that argument parsing specification works for pyfunctions - -#[pyfunction(signature = (a=5, *args))] -fn ext_vararg_fn(py: Python<'_>, a: i32, args: &Bound<'_, PyTuple>) -> PyResult { - [ - a.into_pyobject(py)?.into_any().into_bound(), - args.as_any().clone(), - ] - .into_pyobject(py) - .map(Bound::unbind) -} - -#[pymodule] -fn vararg_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - #[pyfn(m, signature = (a=5, *args))] - fn int_vararg_fn(py: Python<'_>, a: i32, args: &Bound<'_, PyTuple>) -> PyResult { - ext_vararg_fn(py, a, args) - } - - m.add_function(wrap_pyfunction!(ext_vararg_fn, m)?).unwrap(); - Ok(()) -} - -#[test] -fn test_vararg_module() { - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(vararg_module)(py); - - py_assert!(py, m, "m.ext_vararg_fn() == [5, ()]"); - py_assert!(py, m, "m.ext_vararg_fn(1, 2) == [1, (2,)]"); - - py_assert!(py, m, "m.int_vararg_fn() == [5, ()]"); - py_assert!(py, m, "m.int_vararg_fn(1, 2) == [1, (2,)]"); - }); -} - -#[test] -fn test_module_with_constant() { - // Regression test for #1102 - - #[pymodule] - fn module_with_constant(m: &Bound<'_, PyModule>) -> PyResult<()> { - const ANON: AnonClass = AnonClass {}; - - m.add("ANON", ANON)?; - m.add_class::()?; - - Ok(()) - } - - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(module_with_constant)(py); - py_assert!(py, m, "isinstance(m.ANON, m.AnonClass)"); - }); -} - -#[pyfunction] -#[pyo3(pass_module)] -fn pyfunction_with_module<'py>(module: &Bound<'py, PyModule>) -> PyResult> { - module.name() -} - -#[pyfunction] -#[pyo3(pass_module)] -fn pyfunction_with_module_owned( - module: Py, - py: Python<'_>, -) -> PyResult> { - module.bind(py).name() -} - -#[pyfunction] -#[pyo3(pass_module)] -fn pyfunction_with_module_and_py<'py>( - module: &Bound<'py, PyModule>, - _python: Python<'py>, -) -> PyResult> { - module.name() -} - -#[pyfunction] -#[pyo3(pass_module)] -fn pyfunction_with_module_and_arg<'py>( - module: &Bound<'py, PyModule>, - string: String, -) -> PyResult<(Bound<'py, PyString>, String)> { - module.name().map(|s| (s, string)) -} - -#[pyfunction(signature = (string="foo"))] -#[pyo3(pass_module)] -fn pyfunction_with_module_and_default_arg<'py>( - module: &Bound<'py, PyModule>, - string: &str, -) -> PyResult<(Bound<'py, PyString>, String)> { - module.name().map(|s| (s, string.into())) -} - -#[pyfunction(signature = (*args, **kwargs))] -#[pyo3(pass_module)] -fn pyfunction_with_module_and_args_kwargs<'py>( - module: &Bound<'py, PyModule>, - args: &Bound<'py, PyTuple>, - kwargs: Option<&Bound<'py, PyDict>>, -) -> PyResult<(Bound<'py, PyString>, usize, Option)> { - module - .name() - .map(|s| (s, args.len(), kwargs.map(|d| d.len()))) -} - -#[pymodule] -fn module_with_functions_with_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module_owned, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module_and_py, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module_and_arg, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module_and_default_arg, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module_and_args_kwargs, m)?)?; - m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)?; - Ok(()) -} - -#[test] -fn test_module_functions_with_module() { - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(module_with_functions_with_module)(py); - py_assert!( - py, - m, - "m.pyfunction_with_module() == 'module_with_functions_with_module'" - ); - py_assert!( - py, - m, - "m.pyfunction_with_module_owned() == 'module_with_functions_with_module'" - ); - py_assert!( - py, - m, - "m.pyfunction_with_module_and_py() == 'module_with_functions_with_module'" - ); - py_assert!( - py, - m, - "m.pyfunction_with_module_and_default_arg() \ - == ('module_with_functions_with_module', 'foo')" - ); - py_assert!( - py, - m, - "m.pyfunction_with_module_and_args_kwargs(1, x=1, y=2) \ - == ('module_with_functions_with_module', 1, 2)" - ); - }); -} - -#[test] -fn test_module_doc_hidden() { - #[doc(hidden)] - #[allow(clippy::unnecessary_wraps)] - #[pymodule] - fn my_module(_m: &Bound<'_, PyModule>) -> PyResult<()> { - Ok(()) - } - - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(my_module)(py); - py_assert!(py, m, "m.__doc__ == ''"); - }) -} diff --git a/vendor/pyo3/tests/test_multiple_pymethods.rs b/vendor/pyo3/tests/test_multiple_pymethods.rs deleted file mode 100644 index 13baeed381..0000000000 --- a/vendor/pyo3/tests/test_multiple_pymethods.rs +++ /dev/null @@ -1,76 +0,0 @@ -#![cfg(feature = "multiple-pymethods")] - -use pyo3::prelude::*; -use pyo3::types::PyType; - -#[macro_use] -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct PyClassWithMultiplePyMethods {} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - #[new] - fn new() -> Self { - Self {} - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - fn __call__(&self) -> &'static str { - "call" - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - fn method(&self) -> &'static str { - "method" - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - #[classmethod] - fn classmethod(_ty: &Bound<'_, PyType>) -> &'static str { - "classmethod" - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - #[staticmethod] - fn staticmethod() -> &'static str { - "staticmethod" - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - #[classattr] - fn class_attribute() -> &'static str { - "class_attribute" - } -} - -#[pymethods] -impl PyClassWithMultiplePyMethods { - #[classattr] - const CLASS_ATTRIBUTE: &'static str = "CLASS_ATTRIBUTE"; -} - -#[test] -fn test_class_with_multiple_pymethods() { - Python::with_gil(|py| { - let cls = py.get_type::(); - py_assert!(py, cls, "cls()() == 'call'"); - py_assert!(py, cls, "cls().method() == 'method'"); - py_assert!(py, cls, "cls.classmethod() == 'classmethod'"); - py_assert!(py, cls, "cls.staticmethod() == 'staticmethod'"); - py_assert!(py, cls, "cls.class_attribute == 'class_attribute'"); - py_assert!(py, cls, "cls.CLASS_ATTRIBUTE == 'CLASS_ATTRIBUTE'"); - }) -} diff --git a/vendor/pyo3/tests/test_proto_methods.rs b/vendor/pyo3/tests/test_proto_methods.rs deleted file mode 100644 index 23f7f6cf21..0000000000 --- a/vendor/pyo3/tests/test_proto_methods.rs +++ /dev/null @@ -1,894 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::exceptions::{PyAttributeError, PyIndexError, PyValueError}; -use pyo3::types::{PyDict, PyList, PyMapping, PySequence, PySlice, PyType}; -use pyo3::{prelude::*, py_run}; -use std::iter; -use std::sync::Mutex; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct EmptyClass; - -#[pyclass] -struct ExampleClass { - #[pyo3(get, set)] - value: i32, - custom_attr: Option, -} - -#[pymethods] -impl ExampleClass { - fn __getattr__(&self, py: Python<'_>, attr: &str) -> PyResult { - if attr == "special_custom_attr" { - Ok(self.custom_attr.into_pyobject(py)?.into_any().unbind()) - } else { - Err(PyAttributeError::new_err(attr.to_string())) - } - } - - fn __setattr__(&mut self, attr: &str, value: &Bound<'_, PyAny>) -> PyResult<()> { - if attr == "special_custom_attr" { - self.custom_attr = Some(value.extract()?); - Ok(()) - } else { - Err(PyAttributeError::new_err(attr.to_string())) - } - } - - fn __delattr__(&mut self, attr: &str) -> PyResult<()> { - if attr == "special_custom_attr" { - self.custom_attr = None; - Ok(()) - } else { - Err(PyAttributeError::new_err(attr.to_string())) - } - } - - fn __str__(&self) -> String { - self.value.to_string() - } - - fn __repr__(&self) -> String { - format!("ExampleClass(value={})", self.value) - } - - fn __hash__(&self) -> u64 { - let i64_value: i64 = self.value.into(); - i64_value as u64 - } - - fn __bool__(&self) -> bool { - self.value != 0 - } -} - -fn make_example(py: Python<'_>) -> Bound<'_, ExampleClass> { - Bound::new( - py, - ExampleClass { - value: 5, - custom_attr: Some(20), - }, - ) - .unwrap() -} - -#[test] -fn test_getattr() { - Python::with_gil(|py| { - let example_py = make_example(py); - assert_eq!( - example_py - .getattr("value") - .unwrap() - .extract::() - .unwrap(), - 5, - ); - assert_eq!( - example_py - .getattr("special_custom_attr") - .unwrap() - .extract::() - .unwrap(), - 20, - ); - assert!(example_py - .getattr("other_attr") - .unwrap_err() - .is_instance_of::(py)); - }) -} - -#[test] -fn test_setattr() { - Python::with_gil(|py| { - let example_py = make_example(py); - example_py.setattr("special_custom_attr", 15).unwrap(); - assert_eq!( - example_py - .getattr("special_custom_attr") - .unwrap() - .extract::() - .unwrap(), - 15, - ); - }) -} - -#[test] -fn test_delattr() { - Python::with_gil(|py| { - let example_py = make_example(py); - example_py.delattr("special_custom_attr").unwrap(); - assert!(example_py.getattr("special_custom_attr").unwrap().is_none()); - }) -} - -#[test] -fn test_str() { - Python::with_gil(|py| { - let example_py = make_example(py); - assert_eq!(example_py.str().unwrap(), "5"); - }) -} - -#[test] -fn test_repr() { - Python::with_gil(|py| { - let example_py = make_example(py); - assert_eq!(example_py.repr().unwrap(), "ExampleClass(value=5)"); - }) -} - -#[test] -fn test_hash() { - Python::with_gil(|py| { - let example_py = make_example(py); - assert_eq!(example_py.hash().unwrap(), 5); - }) -} - -#[test] -fn test_bool() { - Python::with_gil(|py| { - let example_py = make_example(py); - assert!(example_py.is_truthy().unwrap()); - example_py.borrow_mut().value = 0; - assert!(!example_py.is_truthy().unwrap()); - }) -} - -#[pyclass] -pub struct LenOverflow; - -#[pymethods] -impl LenOverflow { - fn __len__(&self) -> usize { - (isize::MAX as usize) + 1 - } -} - -#[test] -fn len_overflow() { - Python::with_gil(|py| { - let inst = Py::new(py, LenOverflow).unwrap(); - py_expect_exception!(py, inst, "len(inst)", PyOverflowError); - }); -} - -#[pyclass] -pub struct Mapping { - values: Py, -} - -#[pymethods] -impl Mapping { - fn __len__(&self, py: Python<'_>) -> usize { - self.values.bind(py).len() - } - - fn __getitem__<'py>(&self, key: &Bound<'py, PyAny>) -> PyResult> { - let any: &Bound<'py, PyAny> = self.values.bind(key.py()); - any.get_item(key) - } - - fn __setitem__<'py>(&self, key: &Bound<'py, PyAny>, value: &Bound<'py, PyAny>) -> PyResult<()> { - self.values.bind(key.py()).set_item(key, value) - } - - fn __delitem__(&self, key: &Bound<'_, PyAny>) -> PyResult<()> { - self.values.bind(key.py()).del_item(key) - } -} - -#[test] -fn mapping() { - Python::with_gil(|py| { - PyMapping::register::(py).unwrap(); - - let inst = Py::new( - py, - Mapping { - values: PyDict::new(py).into(), - }, - ) - .unwrap(); - - let mapping: &Bound<'_, PyMapping> = inst.bind(py).downcast().unwrap(); - - py_assert!(py, inst, "len(inst) == 0"); - - py_run!(py, inst, "inst['foo'] = 'foo'"); - py_assert!(py, inst, "inst['foo'] == 'foo'"); - py_run!(py, inst, "del inst['foo']"); - py_expect_exception!(py, inst, "inst['foo']", PyKeyError); - - // Default iteration will call __getitem__ with integer indices - // which fails with a KeyError - py_expect_exception!(py, inst, "[*inst] == []", PyKeyError, "0"); - - // check mapping protocol - assert_eq!(mapping.len().unwrap(), 0); - - mapping.set_item(0, 5).unwrap(); - assert_eq!(mapping.len().unwrap(), 1); - - assert_eq!(mapping.get_item(0).unwrap().extract::().unwrap(), 5); - - mapping.del_item(0).unwrap(); - assert_eq!(mapping.len().unwrap(), 0); - }); -} - -#[derive(FromPyObject)] -enum SequenceIndex<'py> { - Integer(isize), - Slice(Bound<'py, PySlice>), -} - -#[pyclass] -pub struct Sequence { - values: Vec, -} - -#[pymethods] -impl Sequence { - fn __len__(&self) -> usize { - self.values.len() - } - - fn __getitem__(&self, index: SequenceIndex<'_>, py: Python<'_>) -> PyResult { - match index { - SequenceIndex::Integer(index) => { - let uindex = self.usize_index(index)?; - self.values - .get(uindex) - .map(|o| o.clone_ref(py)) - .ok_or_else(|| PyIndexError::new_err(index)) - } - // Just to prove that slicing can be implemented - SequenceIndex::Slice(s) => Ok(s.into()), - } - } - - fn __setitem__(&mut self, index: isize, value: PyObject) -> PyResult<()> { - let uindex = self.usize_index(index)?; - self.values - .get_mut(uindex) - .map(|place| *place = value) - .ok_or_else(|| PyIndexError::new_err(index)) - } - - fn __delitem__(&mut self, index: isize) -> PyResult<()> { - let uindex = self.usize_index(index)?; - if uindex >= self.values.len() { - Err(PyIndexError::new_err(index)) - } else { - self.values.remove(uindex); - Ok(()) - } - } - - fn append(&mut self, value: PyObject) { - self.values.push(value); - } -} - -impl Sequence { - fn usize_index(&self, index: isize) -> PyResult { - if index < 0 { - let corrected_index = index + self.values.len() as isize; - if corrected_index < 0 { - Err(PyIndexError::new_err(index)) - } else { - Ok(corrected_index as usize) - } - } else { - Ok(index as usize) - } - } -} - -#[test] -fn sequence() { - Python::with_gil(|py| { - PySequence::register::(py).unwrap(); - - let inst = Py::new(py, Sequence { values: vec![] }).unwrap(); - - let sequence: &Bound<'_, PySequence> = inst.bind(py).downcast().unwrap(); - - py_assert!(py, inst, "len(inst) == 0"); - - py_expect_exception!(py, inst, "inst[0]", PyIndexError); - py_run!(py, inst, "inst.append('foo')"); - - py_assert!(py, inst, "inst[0] == 'foo'"); - py_assert!(py, inst, "inst[-1] == 'foo'"); - - py_expect_exception!(py, inst, "inst[1]", PyIndexError); - py_expect_exception!(py, inst, "inst[-2]", PyIndexError); - - py_assert!(py, inst, "[*inst] == ['foo']"); - - py_run!(py, inst, "del inst[0]"); - - py_expect_exception!(py, inst, "inst['foo']", PyTypeError); - - py_assert!(py, inst, "inst[0:2] == slice(0, 2)"); - - // check sequence protocol - - // we don't implement sequence length so that CPython doesn't attempt to correct negative - // indices. - assert!(sequence.len().is_err()); - // however regular python len() works thanks to mp_len slot - assert_eq!(inst.bind(py).len().unwrap(), 0); - - py_run!(py, inst, "inst.append(0)"); - sequence.set_item(0, 5).unwrap(); - assert_eq!(inst.bind(py).len().unwrap(), 1); - - assert_eq!(sequence.get_item(0).unwrap().extract::().unwrap(), 5); - sequence.del_item(0).unwrap(); - - assert_eq!(inst.bind(py).len().unwrap(), 0); - }); -} - -#[pyclass] -struct Iterator { - iter: Mutex + Send>>, -} - -#[pymethods] -impl Iterator { - fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __next__(slf: PyRefMut<'_, Self>) -> Option { - slf.iter.lock().unwrap().next() - } -} - -#[test] -fn iterator() { - Python::with_gil(|py| { - let inst = Py::new( - py, - Iterator { - iter: Mutex::new(Box::new(5..8)), - }, - ) - .unwrap(); - py_assert!(py, inst, "iter(inst) is inst"); - py_assert!(py, inst, "list(inst) == [5, 6, 7]"); - }); -} - -#[pyclass] -struct Callable; - -#[pymethods] -impl Callable { - fn __call__(&self, arg: i32) -> i32 { - arg * 6 - } -} - -#[pyclass] -struct NotCallable; - -#[test] -fn callable() { - Python::with_gil(|py| { - let c = Py::new(py, Callable).unwrap(); - py_assert!(py, c, "callable(c)"); - py_assert!(py, c, "c(7) == 42"); - - let nc = Py::new(py, NotCallable).unwrap(); - py_assert!(py, nc, "not callable(nc)"); - }); -} - -#[pyclass] -#[derive(Debug)] -struct SetItem { - key: i32, - val: i32, -} - -#[pymethods] -impl SetItem { - fn __setitem__(&mut self, key: i32, val: i32) { - self.key = key; - self.val = val; - } -} - -#[test] -fn setitem() { - Python::with_gil(|py| { - let c = Bound::new(py, SetItem { key: 0, val: 0 }).unwrap(); - py_run!(py, c, "c[1] = 2"); - { - let c = c.borrow(); - assert_eq!(c.key, 1); - assert_eq!(c.val, 2); - } - py_expect_exception!(py, c, "del c[1]", PyNotImplementedError); - }); -} - -#[pyclass] -struct DelItem { - key: i32, -} - -#[pymethods] -impl DelItem { - fn __delitem__(&mut self, key: i32) { - self.key = key; - } -} - -#[test] -fn delitem() { - Python::with_gil(|py| { - let c = Bound::new(py, DelItem { key: 0 }).unwrap(); - py_run!(py, c, "del c[1]"); - { - let c = c.borrow(); - assert_eq!(c.key, 1); - } - py_expect_exception!(py, c, "c[1] = 2", PyNotImplementedError); - }); -} - -#[pyclass] -struct SetDelItem { - val: Option, -} - -#[pymethods] -impl SetDelItem { - fn __setitem__(&mut self, _key: i32, val: i32) { - self.val = Some(val); - } - - fn __delitem__(&mut self, _key: i32) { - self.val = None; - } -} - -#[test] -fn setdelitem() { - Python::with_gil(|py| { - let c = Bound::new(py, SetDelItem { val: None }).unwrap(); - py_run!(py, c, "c[1] = 2"); - { - let c = c.borrow(); - assert_eq!(c.val, Some(2)); - } - py_run!(py, c, "del c[1]"); - let c = c.borrow(); - assert_eq!(c.val, None); - }); -} - -#[pyclass] -struct Contains {} - -#[pymethods] -impl Contains { - fn __contains__(&self, item: i32) -> bool { - item >= 0 - } -} - -#[test] -fn contains() { - Python::with_gil(|py| { - let c = Py::new(py, Contains {}).unwrap(); - py_run!(py, c, "assert 1 in c"); - py_run!(py, c, "assert -1 not in c"); - py_expect_exception!(py, c, "assert 'wrong type' not in c", PyTypeError); - }); -} - -#[pyclass] -struct GetItem {} - -#[pymethods] -impl GetItem { - fn __getitem__(&self, idx: &Bound<'_, PyAny>) -> PyResult<&'static str> { - if let Ok(slice) = idx.downcast::() { - let indices = slice.indices(1000)?; - if indices.start == 100 && indices.stop == 200 && indices.step == 1 { - return Ok("slice"); - } - } else if let Ok(idx) = idx.extract::() { - if idx == 1 { - return Ok("int"); - } - } - Err(PyValueError::new_err("error")) - } -} - -#[test] -fn test_getitem() { - Python::with_gil(|py| { - let ob = Py::new(py, GetItem {}).unwrap(); - - py_assert!(py, ob, "ob[1] == 'int'"); - py_assert!(py, ob, "ob[100:200:1] == 'slice'"); - }); -} - -#[pyclass] -struct ClassWithGetAttr { - #[pyo3(get, set)] - data: u32, -} - -#[pymethods] -impl ClassWithGetAttr { - fn __getattr__(&self, _name: &str) -> u32 { - self.data * 2 - } -} - -#[test] -fn getattr_doesnt_override_member() { - Python::with_gil(|py| { - let inst = Py::new(py, ClassWithGetAttr { data: 4 }).unwrap(); - py_assert!(py, inst, "inst.data == 4"); - py_assert!(py, inst, "inst.a == 8"); - }); -} - -#[pyclass] -struct ClassWithGetAttribute { - #[pyo3(get, set)] - data: u32, -} - -#[pymethods] -impl ClassWithGetAttribute { - fn __getattribute__(&self, _name: &str) -> u32 { - self.data * 2 - } -} - -#[test] -fn getattribute_overrides_member() { - Python::with_gil(|py| { - let inst = Py::new(py, ClassWithGetAttribute { data: 4 }).unwrap(); - py_assert!(py, inst, "inst.data == 8"); - py_assert!(py, inst, "inst.y == 8"); - }); -} - -#[pyclass] -struct ClassWithGetAttrAndGetAttribute; - -#[pymethods] -impl ClassWithGetAttrAndGetAttribute { - fn __getattribute__(&self, name: &str) -> PyResult { - if name == "exists" { - Ok(42) - } else if name == "error" { - Err(PyValueError::new_err("bad")) - } else { - Err(PyAttributeError::new_err("fallback")) - } - } - - fn __getattr__(&self, name: &str) -> PyResult { - if name == "lucky" { - Ok(57) - } else { - Err(PyAttributeError::new_err("no chance")) - } - } -} - -#[test] -fn getattr_and_getattribute() { - Python::with_gil(|py| { - let inst = Py::new(py, ClassWithGetAttrAndGetAttribute).unwrap(); - py_assert!(py, inst, "inst.exists == 42"); - py_assert!(py, inst, "inst.lucky == 57"); - py_expect_exception!(py, inst, "inst.error", PyValueError); - py_expect_exception!(py, inst, "inst.unlucky", PyAttributeError); - }); -} - -/// Wraps a Python future and yield it once. -#[pyclass] -#[derive(Debug)] -struct OnceFuture { - future: PyObject, - polled: bool, -} - -#[pymethods] -impl OnceFuture { - #[new] - fn new(future: PyObject) -> Self { - OnceFuture { - future, - polled: false, - } - } - - fn __await__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - fn __next__<'py>(&mut self, py: Python<'py>) -> Option<&Bound<'py, PyAny>> { - if !self.polled { - self.polled = true; - Some(self.future.bind(py)) - } else { - None - } - } -} - -#[test] -#[cfg(not(target_arch = "wasm32"))] // Won't work without wasm32 event loop (e.g., Pyodide has WebLoop) -fn test_await() { - Python::with_gil(|py| { - let once = py.get_type::(); - let source = pyo3_ffi::c_str!( - r#" -import asyncio -import sys - -async def main(): - res = await Once(await asyncio.sleep(0.1)) - assert res is None - -# For an odd error similar to https://bugs.python.org/issue38563 -if sys.platform == "win32" and sys.version_info >= (3, 8, 0): - asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) - -asyncio.run(main()) -"# - ); - let globals = PyModule::import(py, "__main__").unwrap().dict(); - globals.set_item("Once", once).unwrap(); - py.run(source, Some(&globals), None) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} - -#[pyclass] -struct AsyncIterator { - future: Option>, -} - -#[pymethods] -impl AsyncIterator { - #[new] - fn new(future: Py) -> Self { - Self { - future: Some(future), - } - } - - fn __aiter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __anext__(&mut self) -> Option> { - self.future.take() - } -} - -#[test] -#[cfg(not(target_arch = "wasm32"))] // Won't work without wasm32 event loop (e.g., Pyodide has WebLoop) -fn test_anext_aiter() { - Python::with_gil(|py| { - let once = py.get_type::(); - let source = pyo3_ffi::c_str!( - r#" -import asyncio -import sys - -async def main(): - count = 0 - async for result in AsyncIterator(Once(await asyncio.sleep(0.1))): - # The Once is awaited as part of the `async for` and produces None - assert result is None - count +=1 - assert count == 1 - -# For an odd error similar to https://bugs.python.org/issue38563 -if sys.platform == "win32" and sys.version_info >= (3, 8, 0): - asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) - -asyncio.run(main()) -"# - ); - let globals = PyModule::import(py, "__main__").unwrap().dict(); - globals.set_item("Once", once).unwrap(); - globals - .set_item("AsyncIterator", py.get_type::()) - .unwrap(); - py.run(source, Some(&globals), None) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} - -/// Increment the count when `__get__` is called. -#[pyclass] -struct DescrCounter { - #[pyo3(get)] - count: usize, -} - -#[pymethods] -impl DescrCounter { - #[new] - fn new() -> Self { - DescrCounter { count: 0 } - } - /// Each access will increase the count - fn __get__<'a>( - mut slf: PyRefMut<'a, Self>, - _instance: &Bound<'_, PyAny>, - _owner: Option<&Bound<'_, PyType>>, - ) -> PyRefMut<'a, Self> { - slf.count += 1; - slf - } - /// Allow assigning a new counter to the descriptor, copying the count across - fn __set__(&self, _instance: &Bound<'_, PyAny>, new_value: &mut Self) { - new_value.count = self.count; - } - /// Delete to reset the counter - fn __delete__(&mut self, _instance: &Bound<'_, PyAny>) { - self.count = 0; - } -} - -#[test] -fn descr_getset() { - Python::with_gil(|py| { - let counter = py.get_type::(); - let source = pyo3_ffi::c_str!(pyo3::indoc::indoc!( - r#" -class Class: - counter = Counter() - -# access via type -counter = Class.counter -assert counter.count == 1 - -# access with instance directly -assert Counter.__get__(counter, Class()).count == 2 - -# access via instance -c = Class() -assert c.counter.count == 3 - -# __set__ -c.counter = Counter() -assert c.counter.count == 4 - -# __delete__ -del c.counter -assert c.counter.count == 1 -"# - )); - let globals = PyModule::import(py, "__main__").unwrap().dict(); - globals.set_item("Counter", counter).unwrap(); - py.run(source, Some(&globals), None) - .map_err(|e| e.display(py)) - .unwrap(); - }); -} - -#[pyclass] -struct NotHashable; - -#[pymethods] -impl NotHashable { - #[classattr] - const __hash__: Option = None; -} - -#[test] -fn test_hash_opt_out() { - // By default Python provides a hash implementation, which can be disabled by setting __hash__ - // to None. - Python::with_gil(|py| { - let empty = Py::new(py, EmptyClass).unwrap(); - py_assert!(py, empty, "hash(empty) is not None"); - - let not_hashable = Py::new(py, NotHashable).unwrap(); - py_expect_exception!(py, not_hashable, "hash(not_hashable)", PyTypeError); - }) -} - -/// Class with __iter__ gets default contains from CPython. -#[pyclass] -struct DefaultedContains; - -#[pymethods] -impl DefaultedContains { - fn __iter__(&self, py: Python<'_>) -> PyObject { - PyList::new(py, ["a", "b", "c"]) - .unwrap() - .as_ref() - .try_iter() - .unwrap() - .into() - } -} - -#[pyclass] -struct NoContains; - -#[pymethods] -impl NoContains { - fn __iter__(&self, py: Python<'_>) -> PyObject { - PyList::new(py, ["a", "b", "c"]) - .unwrap() - .as_ref() - .try_iter() - .unwrap() - .into() - } - - // Equivalent to the opt-out const form in NotHashable above, just more verbose, to confirm this - // also works. - #[classattr] - fn __contains__() -> Option { - None - } -} - -#[test] -fn test_contains_opt_out() { - Python::with_gil(|py| { - let defaulted_contains = Py::new(py, DefaultedContains).unwrap(); - py_assert!(py, defaulted_contains, "'a' in defaulted_contains"); - - let no_contains = Py::new(py, NoContains).unwrap(); - py_expect_exception!(py, no_contains, "'a' in no_contains", PyTypeError); - }) -} diff --git a/vendor/pyo3/tests/test_pyfunction.rs b/vendor/pyo3/tests/test_pyfunction.rs deleted file mode 100644 index 13ba5405ed..0000000000 --- a/vendor/pyo3/tests/test_pyfunction.rs +++ /dev/null @@ -1,610 +0,0 @@ -#![cfg(feature = "macros")] - -use std::collections::HashMap; - -#[cfg(not(Py_LIMITED_API))] -use pyo3::buffer::PyBuffer; -use pyo3::ffi::c_str; -use pyo3::prelude::*; -#[cfg(not(Py_LIMITED_API))] -use pyo3::types::PyDateTime; -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -use pyo3::types::PyFunction; -use pyo3::types::{self, PyCFunction}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyfunction(name = "struct")] -fn struct_function() {} - -#[test] -fn test_rust_keyword_name() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(struct_function)(py).unwrap(); - - py_assert!(py, f, "f.__name__ == 'struct'"); - }); -} - -#[pyfunction(signature = (arg = true))] -fn optional_bool(arg: Option) -> String { - format!("{:?}", arg) -} - -#[test] -fn test_optional_bool() { - // Regression test for issue #932 - Python::with_gil(|py| { - let f = wrap_pyfunction!(optional_bool)(py).unwrap(); - - py_assert!(py, f, "f() == 'Some(true)'"); - py_assert!(py, f, "f(True) == 'Some(true)'"); - py_assert!(py, f, "f(False) == 'Some(false)'"); - py_assert!(py, f, "f(None) == 'None'"); - }); -} - -#[cfg(not(Py_LIMITED_API))] -#[pyfunction] -fn buffer_inplace_add(py: Python<'_>, x: PyBuffer, y: PyBuffer) { - let x = x.as_mut_slice(py).unwrap(); - let y = y.as_slice(py).unwrap(); - for (xi, yi) in x.iter().zip(y) { - let xi_plus_yi = xi.get() + yi.get(); - xi.set(xi_plus_yi); - } -} - -#[cfg(not(Py_LIMITED_API))] -#[test] -fn test_buffer_add() { - Python::with_gil(|py| { - let f = wrap_pyfunction!(buffer_inplace_add)(py).unwrap(); - - py_expect_exception!( - py, - f, - r#" -import array -a = array.array("i", [0, 1, 2, 3]) -b = array.array("I", [0, 1, 2, 3]) -f(a, b) -"#, - PyBufferError - ); - - pyo3::py_run!( - py, - f, - r#" -import array -a = array.array("i", [0, 1, 2, 3]) -b = array.array("i", [2, 3, 4, 5]) -f(a, b) -assert a, array.array("i", [2, 4, 6, 8]) -"# - ); - }); -} - -#[cfg(not(any(Py_LIMITED_API, PyPy)))] -#[pyfunction] -fn function_with_pyfunction_arg<'py>(fun: &Bound<'py, PyFunction>) -> PyResult> { - fun.call((), None) -} - -#[pyfunction] -fn function_with_pycfunction_arg<'py>( - fun: &Bound<'py, PyCFunction>, -) -> PyResult> { - fun.call((), None) -} - -#[test] -fn test_functions_with_function_args() { - Python::with_gil(|py| { - let py_cfunc_arg = wrap_pyfunction!(function_with_pycfunction_arg)(py).unwrap(); - let bool_to_string = wrap_pyfunction!(optional_bool)(py).unwrap(); - - pyo3::py_run!( - py, - py_cfunc_arg - bool_to_string, - r#" - assert py_cfunc_arg(bool_to_string) == "Some(true)" - "# - ); - - #[cfg(not(any(Py_LIMITED_API, PyPy)))] - { - let py_func_arg = wrap_pyfunction!(function_with_pyfunction_arg)(py).unwrap(); - - pyo3::py_run!( - py, - py_func_arg, - r#" - def foo(): return "bar" - assert py_func_arg(foo) == "bar" - "# - ); - } - }); -} - -#[cfg(not(Py_LIMITED_API))] -fn datetime_to_timestamp(dt: &Bound<'_, PyAny>) -> PyResult { - let dt = dt.downcast::()?; - let ts: f64 = dt.call_method0("timestamp")?.extract()?; - - Ok(ts as i64) -} - -#[cfg(not(Py_LIMITED_API))] -#[pyfunction] -fn function_with_custom_conversion( - #[pyo3(from_py_with = "datetime_to_timestamp")] timestamp: i64, -) -> i64 { - timestamp -} - -#[cfg(not(Py_LIMITED_API))] -#[test] -fn test_function_with_custom_conversion() { - Python::with_gil(|py| { - let custom_conv_func = wrap_pyfunction!(function_with_custom_conversion)(py).unwrap(); - - pyo3::py_run!( - py, - custom_conv_func, - r#" - import datetime - - dt = datetime.datetime.fromtimestamp(1612040400) - assert custom_conv_func(dt) == 1612040400 - "# - ) - }); -} - -#[cfg(not(Py_LIMITED_API))] -#[test] -fn test_function_with_custom_conversion_error() { - Python::with_gil(|py| { - let custom_conv_func = wrap_pyfunction!(function_with_custom_conversion)(py).unwrap(); - - py_expect_exception!( - py, - custom_conv_func, - "custom_conv_func(['a'])", - PyTypeError, - "argument 'timestamp': 'list' object cannot be converted to 'PyDateTime'" - ); - }); -} - -#[test] -fn test_from_py_with_defaults() { - fn optional_int(x: &Bound<'_, PyAny>) -> PyResult> { - if x.is_none() { - Ok(None) - } else { - Some(x.extract()).transpose() - } - } - - // issue 2280 combination of from_py_with and Option did not compile - #[pyfunction] - #[pyo3(signature = (int=None))] - fn from_py_with_option(#[pyo3(from_py_with = "optional_int")] int: Option) -> i32 { - int.unwrap_or(0) - } - - #[pyfunction(signature = (len=0))] - fn from_py_with_default( - #[pyo3(from_py_with = " as PyAnyMethods>::len")] len: usize, - ) -> usize { - len - } - - Python::with_gil(|py| { - let f = wrap_pyfunction!(from_py_with_option)(py).unwrap(); - - assert_eq!(f.call0().unwrap().extract::().unwrap(), 0); - assert_eq!(f.call1((123,)).unwrap().extract::().unwrap(), 123); - assert_eq!(f.call1((999,)).unwrap().extract::().unwrap(), 999); - - let f2 = wrap_pyfunction!(from_py_with_default)(py).unwrap(); - - assert_eq!(f2.call0().unwrap().extract::().unwrap(), 0); - assert_eq!(f2.call1(("123",)).unwrap().extract::().unwrap(), 3); - assert_eq!(f2.call1(("1234",)).unwrap().extract::().unwrap(), 4); - }); -} - -#[pyclass] -#[derive(Debug, FromPyObject)] -struct ValueClass { - #[pyo3(get)] - value: usize, -} - -#[pyfunction] -#[pyo3(signature=(str_arg, int_arg, tuple_arg, option_arg = None, struct_arg = None))] -fn conversion_error( - str_arg: &str, - int_arg: i64, - tuple_arg: (String, f64), - option_arg: Option, - struct_arg: Option, -) { - println!( - "{:?} {:?} {:?} {:?} {:?}", - str_arg, int_arg, tuple_arg, option_arg, struct_arg - ); -} - -#[test] -fn test_conversion_error() { - Python::with_gil(|py| { - let conversion_error = wrap_pyfunction!(conversion_error)(py).unwrap(); - py_expect_exception!( - py, - conversion_error, - "conversion_error(None, None, None, None, None)", - PyTypeError, - "argument 'str_arg': 'NoneType' object cannot be converted to 'PyString'" - ); - py_expect_exception!( - py, - conversion_error, - "conversion_error(100, None, None, None, None)", - PyTypeError, - "argument 'str_arg': 'int' object cannot be converted to 'PyString'" - ); - py_expect_exception!( - py, - conversion_error, - "conversion_error('string1', 'string2', None, None, None)", - PyTypeError, - "argument 'int_arg': 'str' object cannot be interpreted as an integer" - ); - py_expect_exception!( - py, - conversion_error, - "conversion_error('string1', -100, 'string2', None, None)", - PyTypeError, - "argument 'tuple_arg': 'str' object cannot be converted to 'PyTuple'" - ); - py_expect_exception!( - py, - conversion_error, - "conversion_error('string1', -100, ('string2', 10.), 'string3', None)", - PyTypeError, - "argument 'option_arg': 'str' object cannot be interpreted as an integer" - ); - let exception = py_expect_exception!( - py, - conversion_error, - " -class ValueClass: - def __init__(self, value): - self.value = value -conversion_error('string1', -100, ('string2', 10.), None, ValueClass(\"no_expected_type\"))", - PyTypeError - ); - assert_eq!( - extract_traceback(py, exception), - "TypeError: argument 'struct_arg': failed to \ - extract field ValueClass.value: TypeError: 'str' object cannot be interpreted as an integer" - ); - - let exception = py_expect_exception!( - py, - conversion_error, - " -class ValueClass: - def __init__(self, value): - self.value = value -conversion_error('string1', -100, ('string2', 10.), None, ValueClass(-5))", - PyTypeError - ); - assert_eq!( - extract_traceback(py, exception), - "TypeError: argument 'struct_arg': failed to \ - extract field ValueClass.value: OverflowError: can't convert negative int to unsigned" - ); - }); -} - -/// Helper function that concatenates the error message from -/// each error in the traceback into a single string that can -/// be tested. -fn extract_traceback(py: Python<'_>, mut error: PyErr) -> String { - let mut error_msg = error.to_string(); - while let Some(cause) = error.cause(py) { - error_msg.push_str(": "); - error_msg.push_str(&cause.to_string()); - error = cause - } - error_msg -} - -#[test] -fn test_pycfunction_new() { - use pyo3::ffi; - - Python::with_gil(|py| { - unsafe extern "C" fn c_fn( - _self: *mut ffi::PyObject, - _args: *mut ffi::PyObject, - ) -> *mut ffi::PyObject { - ffi::PyLong_FromLong(4200) - } - - let py_fn = PyCFunction::new( - py, - c_fn, - c_str!("py_fn"), - c_str!("py_fn for test (this is the docstring)"), - None, - ) - .unwrap(); - - py_assert!(py, py_fn, "py_fn() == 4200"); - py_assert!( - py, - py_fn, - "py_fn.__doc__ == 'py_fn for test (this is the docstring)'" - ); - }); -} - -#[test] -fn test_pycfunction_new_with_keywords() { - use pyo3::ffi; - use std::os::raw::c_long; - use std::ptr; - - Python::with_gil(|py| { - unsafe extern "C" fn c_fn( - _self: *mut ffi::PyObject, - args: *mut ffi::PyObject, - kwds: *mut ffi::PyObject, - ) -> *mut ffi::PyObject { - let mut foo: c_long = 0; - let mut bar: c_long = 0; - - #[cfg(not(Py_3_13))] - let foo_name = std::ffi::CString::new("foo").unwrap(); - #[cfg(not(Py_3_13))] - let kw_bar_name = std::ffi::CString::new("kw_bar").unwrap(); - #[cfg(not(Py_3_13))] - let mut args_names = [foo_name.into_raw(), kw_bar_name.into_raw(), ptr::null_mut()]; - - #[cfg(Py_3_13)] - let args_names = [ - c_str!("foo").as_ptr(), - c_str!("kw_bar").as_ptr(), - ptr::null_mut(), - ]; - - ffi::PyArg_ParseTupleAndKeywords( - args, - kwds, - c_str!("l|l").as_ptr(), - #[cfg(Py_3_13)] - args_names.as_ptr(), - #[cfg(not(Py_3_13))] - args_names.as_mut_ptr(), - &mut foo, - &mut bar, - ); - - #[cfg(not(Py_3_13))] - drop(std::ffi::CString::from_raw(args_names[0])); - #[cfg(not(Py_3_13))] - drop(std::ffi::CString::from_raw(args_names[1])); - - ffi::PyLong_FromLong(foo * bar) - } - - let py_fn = PyCFunction::new_with_keywords( - py, - c_fn, - c_str!("py_fn"), - c_str!("py_fn for test (this is the docstring)"), - None, - ) - .unwrap(); - - py_assert!(py, py_fn, "py_fn(42, kw_bar=100) == 4200"); - py_assert!(py, py_fn, "py_fn(foo=42, kw_bar=100) == 4200"); - py_assert!( - py, - py_fn, - "py_fn.__doc__ == 'py_fn for test (this is the docstring)'" - ); - }); -} - -#[test] -fn test_closure() { - Python::with_gil(|py| { - let f = |args: &Bound<'_, types::PyTuple>, - _kwargs: Option<&Bound<'_, types::PyDict>>| - -> PyResult<_> { - Python::with_gil(|py| { - let res: PyResult> = args - .iter() - .map(|elem| { - if let Ok(i) = elem.extract::() { - Ok((i + 1).into_pyobject(py)?.into_any().unbind()) - } else if let Ok(f) = elem.extract::() { - Ok((2. * f).into_pyobject(py)?.into_any().unbind()) - } else if let Ok(mut s) = elem.extract::() { - s.push_str("-py"); - Ok(s.into_pyobject(py)?.into_any().unbind()) - } else { - panic!("unexpected argument type for {:?}", elem) - } - }) - .collect(); - res - }) - }; - let closure_py = - PyCFunction::new_closure(py, Some(c_str!("test_fn")), Some(c_str!("test_fn doc")), f) - .unwrap(); - - py_assert!(py, closure_py, "closure_py(42) == [43]"); - py_assert!(py, closure_py, "closure_py.__name__ == 'test_fn'"); - py_assert!(py, closure_py, "closure_py.__doc__ == 'test_fn doc'"); - py_assert!( - py, - closure_py, - "closure_py(42, 3.14, 'foo') == [43, 6.28, 'foo-py']" - ); - }); -} - -#[test] -fn test_closure_counter() { - Python::with_gil(|py| { - let counter = std::cell::RefCell::new(0); - let counter_fn = move |_args: &Bound<'_, types::PyTuple>, - _kwargs: Option<&Bound<'_, types::PyDict>>| - -> PyResult { - let mut counter = counter.borrow_mut(); - *counter += 1; - Ok(*counter) - }; - let counter_py = PyCFunction::new_closure(py, None, None, counter_fn).unwrap(); - - py_assert!(py, counter_py, "counter_py() == 1"); - py_assert!(py, counter_py, "counter_py() == 2"); - py_assert!(py, counter_py, "counter_py() == 3"); - }); -} - -#[test] -fn use_pyfunction() { - mod function_in_module { - use pyo3::prelude::*; - - #[pyfunction] - pub fn foo(x: i32) -> i32 { - x - } - } - - Python::with_gil(|py| { - use function_in_module::foo; - - // check imported name can be wrapped - let f = wrap_pyfunction!(foo, py).unwrap(); - assert_eq!(f.call1((5,)).unwrap().extract::().unwrap(), 5); - assert_eq!(f.call1((42,)).unwrap().extract::().unwrap(), 42); - - // check path import can be wrapped - let f2 = wrap_pyfunction!(function_in_module::foo, py).unwrap(); - assert_eq!(f2.call1((5,)).unwrap().extract::().unwrap(), 5); - assert_eq!(f2.call1((42,)).unwrap().extract::().unwrap(), 42); - }) -} - -#[pyclass] -struct Key(String); - -#[pyclass] -struct Value(i32); - -#[pyfunction] -fn return_value_borrows_from_arguments<'py>( - py: Python<'py>, - key: &'py Key, - value: &'py Value, -) -> HashMap<&'py str, i32> { - py.allow_threads(move || { - let mut map = HashMap::new(); - map.insert(key.0.as_str(), value.0); - map - }) -} - -#[test] -fn test_return_value_borrows_from_arguments() { - Python::with_gil(|py| { - let function = wrap_pyfunction!(return_value_borrows_from_arguments, py).unwrap(); - - let key = Py::new(py, Key("key".to_owned())).unwrap(); - let value = Py::new(py, Value(42)).unwrap(); - - py_assert!(py, function key value, "function(key, value) == { \"key\": 42 }"); - }); -} - -#[test] -fn test_some_wrap_arguments() { - // https://github.com/PyO3/pyo3/issues/3460 - const NONE: Option = None; - #[pyfunction(signature = (a = 1, b = Some(2), c = None, d = NONE))] - fn some_wrap_arguments( - a: Option, - b: Option, - c: Option, - d: Option, - ) -> [Option; 4] { - [a, b, c, d] - } - - Python::with_gil(|py| { - let function = wrap_pyfunction!(some_wrap_arguments, py).unwrap(); - py_assert!(py, function, "function() == [1, 2, None, None]"); - }) -} - -#[test] -fn test_reference_to_bound_arguments() { - #[pyfunction] - #[pyo3(signature = (x, y = None))] - fn reference_args<'py>( - x: &Bound<'py, PyAny>, - y: Option<&Bound<'py, PyAny>>, - ) -> PyResult> { - y.map_or_else(|| Ok(x.clone()), |y| y.add(x)) - } - - Python::with_gil(|py| { - let function = wrap_pyfunction!(reference_args, py).unwrap(); - py_assert!(py, function, "function(1) == 1"); - py_assert!(py, function, "function(1, 2) == 3"); - }) -} - -#[test] -fn test_pyfunction_raw_ident() { - #[pyfunction] - fn r#struct() -> bool { - true - } - - #[pyfunction] - #[pyo3(name = "r#enum")] - fn raw_ident() -> bool { - true - } - - #[pymodule] - fn m(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(r#struct, m)?)?; - m.add_function(wrap_pyfunction!(raw_ident, m)?)?; - Ok(()) - } - - Python::with_gil(|py| { - let m = pyo3::wrap_pymodule!(m)(py); - py_assert!(py, m, "m.struct()"); - py_assert!(py, m, "m.enum()"); - }) -} diff --git a/vendor/pyo3/tests/test_pyself.rs b/vendor/pyo3/tests/test_pyself.rs deleted file mode 100644 index c4f3e6d6fa..0000000000 --- a/vendor/pyo3/tests/test_pyself.rs +++ /dev/null @@ -1,126 +0,0 @@ -#![cfg(feature = "macros")] - -//! Test slf: PyRef/PyMutRef(especially, slf.into::) works -use pyo3::prelude::*; -use pyo3::types::{PyBytes, PyString}; -use std::collections::HashMap; - -#[path = "../src/tests/common.rs"] -mod common; - -/// Assumes it's a file reader or so. -/// Inspired by https://github.com/jothan/cordoba, thanks. -#[pyclass] -#[derive(Clone, Debug)] -struct Reader { - inner: HashMap, -} - -#[pymethods] -impl Reader { - fn clone_ref<'a, 'py>(slf: &'a Bound<'py, Self>) -> &'a Bound<'py, Self> { - slf - } - fn clone_ref_with_py<'a, 'py>( - slf: &'a Bound<'py, Self>, - _py: Python<'py>, - ) -> &'a Bound<'py, Self> { - slf - } - fn get_iter(slf: &Bound<'_, Self>, keys: Py) -> Iter { - Iter { - reader: slf.clone().unbind(), - keys, - idx: 0, - } - } - fn get_iter_and_reset( - mut slf: PyRefMut<'_, Self>, - keys: Py, - py: Python<'_>, - ) -> PyResult { - let reader = Py::new(py, slf.clone())?; - slf.inner.clear(); - Ok(Iter { - reader, - keys, - idx: 0, - }) - } -} - -#[pyclass] -#[derive(Debug)] -struct Iter { - reader: Py, - keys: Py, - idx: usize, -} - -#[pymethods] -impl Iter { - #[allow(clippy::self_named_constructors)] - fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { - slf - } - - fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult> { - let bytes = slf.keys.bind(slf.py()).as_bytes(); - match bytes.get(slf.idx) { - Some(&b) => { - slf.idx += 1; - let py = slf.py(); - let reader = slf.reader.bind(py); - let reader_ref = reader.try_borrow()?; - let res = reader_ref - .inner - .get(&b) - .map(|s| PyString::new(py, s).into()); - Ok(res) - } - None => Ok(None), - } - } -} - -fn reader() -> Reader { - let reader = [(1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e")]; - Reader { - inner: reader.iter().map(|(k, v)| (*k, (*v).to_string())).collect(), - } -} - -#[test] -fn test_nested_iter() { - Python::with_gil(|py| { - let reader = reader().into_pyobject(py).unwrap(); - py_assert!( - py, - reader, - "list(reader.get_iter(bytes([3, 5, 2]))) == ['c', 'e', 'b']" - ); - }); -} - -#[test] -fn test_clone_ref() { - Python::with_gil(|py| { - let reader = reader().into_pyobject(py).unwrap(); - py_assert!(py, reader, "reader == reader.clone_ref()"); - py_assert!(py, reader, "reader == reader.clone_ref_with_py()"); - }); -} - -#[test] -fn test_nested_iter_reset() { - Python::with_gil(|py| { - let reader = Bound::new(py, reader()).unwrap(); - py_assert!( - py, - reader, - "list(reader.get_iter_and_reset(bytes([3, 5, 2]))) == ['c', 'e', 'b']" - ); - let reader_ref = reader.borrow(); - assert!(reader_ref.inner.is_empty()); - }); -} diff --git a/vendor/pyo3/tests/test_sequence.rs b/vendor/pyo3/tests/test_sequence.rs deleted file mode 100644 index e8f61e80e4..0000000000 --- a/vendor/pyo3/tests/test_sequence.rs +++ /dev/null @@ -1,369 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::exceptions::{PyIndexError, PyValueError}; -use pyo3::types::{IntoPyDict, PyList, PyMapping, PySequence}; -use pyo3::{ffi, prelude::*}; - -use pyo3::py_run; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct ByteSequence { - elements: Vec, -} - -#[pymethods] -impl ByteSequence { - #[new] - #[pyo3(signature=(elements = None))] - fn new(elements: Option<&Bound<'_, PyList>>) -> PyResult { - if let Some(pylist) = elements { - let mut elems = Vec::with_capacity(pylist.len()); - for pyelem in pylist { - let elem = pyelem.extract()?; - elems.push(elem); - } - Ok(Self { elements: elems }) - } else { - Ok(Self { - elements: Vec::new(), - }) - } - } - - fn __len__(&self) -> usize { - self.elements.len() - } - - fn __getitem__(&self, idx: isize) -> PyResult { - self.elements - .get(idx as usize) - .copied() - .ok_or_else(|| PyIndexError::new_err("list index out of range")) - } - - fn __setitem__(&mut self, idx: isize, value: u8) { - self.elements[idx as usize] = value; - } - - fn __delitem__(&mut self, mut idx: isize) -> PyResult<()> { - let self_len = self.elements.len() as isize; - if idx < 0 { - idx += self_len; - } - if (idx < self_len) && (idx >= 0) { - self.elements.remove(idx as usize); - Ok(()) - } else { - Err(PyIndexError::new_err("list index out of range")) - } - } - - fn __contains__(&self, other: &Bound<'_, PyAny>) -> bool { - match other.extract::() { - Ok(x) => self.elements.contains(&x), - Err(_) => false, - } - } - - fn __concat__(&self, other: &Self) -> Self { - let mut elements = self.elements.clone(); - elements.extend_from_slice(&other.elements); - Self { elements } - } - - fn __inplace_concat__(mut slf: PyRefMut<'_, Self>, other: &Self) -> Py { - slf.elements.extend_from_slice(&other.elements); - slf.into() - } - - fn __repeat__(&self, count: isize) -> PyResult { - if count >= 0 { - let mut elements = Vec::with_capacity(self.elements.len() * count as usize); - for _ in 0..count { - elements.extend(&self.elements); - } - Ok(Self { elements }) - } else { - Err(PyValueError::new_err("invalid repeat count")) - } - } - - fn __inplace_repeat__(mut slf: PyRefMut<'_, Self>, count: isize) -> PyResult> { - if count >= 0 { - let mut elements = Vec::with_capacity(slf.elements.len() * count as usize); - for _ in 0..count { - elements.extend(&slf.elements); - } - slf.elements = elements; - Ok(slf.into()) - } else { - Err(PyValueError::new_err("invalid repeat count")) - } - } -} - -/// Return a dict with `s = ByteSequence([1, 2, 3])`. -fn seq_dict(py: Python<'_>) -> Bound<'_, pyo3::types::PyDict> { - let d = [("ByteSequence", py.get_type::())] - .into_py_dict(py) - .unwrap(); - // Though we can construct `s` in Rust, let's test `__new__` works. - py_run!(py, *d, "s = ByteSequence([1, 2, 3])"); - d -} - -#[test] -fn test_getitem() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_assert!(py, *d, "s[0] == 1"); - py_assert!(py, *d, "s[1] == 2"); - py_assert!(py, *d, "s[2] == 3"); - py_expect_exception!(py, *d, "print(s[-4])", PyIndexError); - py_expect_exception!(py, *d, "print(s[4])", PyIndexError); - }); -} - -#[test] -fn test_setitem() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_run!(py, *d, "s[0] = 4; assert list(s) == [4, 2, 3]"); - py_expect_exception!(py, *d, "s[0] = 'hello'", PyTypeError); - }); -} - -#[test] -fn test_delitem() { - Python::with_gil(|py| { - let d = [("ByteSequence", py.get_type::())] - .into_py_dict(py) - .unwrap(); - - py_run!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[0]; assert list(s) == [2, 3]" - ); - py_run!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[1]; assert list(s) == [1, 3]" - ); - py_run!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[-1]; assert list(s) == [1, 2]" - ); - py_run!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[-2]; assert list(s) == [1, 3]" - ); - py_expect_exception!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[-4]; print(list(s))", - PyIndexError - ); - py_expect_exception!( - py, - *d, - "s = ByteSequence([1, 2, 3]); del s[4]", - PyIndexError - ); - }); -} - -#[test] -fn test_contains() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_assert!(py, *d, "1 in s"); - py_assert!(py, *d, "2 in s"); - py_assert!(py, *d, "3 in s"); - py_assert!(py, *d, "4 not in s"); - py_assert!(py, *d, "'hello' not in s"); - }); -} - -#[test] -fn test_concat() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_run!( - py, - *d, - "s1 = ByteSequence([1, 2]); s2 = ByteSequence([3, 4]); assert list(s1 + s2) == [1, 2, 3, 4]" - ); - py_expect_exception!( - py, - *d, - "s1 = ByteSequence([1, 2]); s2 = 'hello'; s1 + s2", - PyTypeError - ); - }); -} - -#[test] -fn test_inplace_concat() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_run!( - py, - *d, - "s += ByteSequence([4, 5]); assert list(s) == [1, 2, 3, 4, 5]" - ); - py_expect_exception!(py, *d, "s += 'hello'", PyTypeError); - }); -} - -#[test] -fn test_repeat() { - Python::with_gil(|py| { - let d = seq_dict(py); - - py_run!(py, *d, "s2 = s * 2; assert list(s2) == [1, 2, 3, 1, 2, 3]"); - py_expect_exception!(py, *d, "s2 = s * -1", PyValueError); - }); -} - -#[test] -fn test_inplace_repeat() { - Python::with_gil(|py| { - let d = [("ByteSequence", py.get_type::())] - .into_py_dict(py) - .unwrap(); - - py_run!( - py, - *d, - "s = ByteSequence([1, 2]); s *= 3; assert list(s) == [1, 2, 1, 2, 1, 2]" - ); - py_expect_exception!(py, *d, "s = ByteSequence([1, 2]); s *= -1", PyValueError); - }); -} - -// Check that #[pyo3(get, set)] works correctly for Vec - -#[pyclass] -struct GenericList { - #[pyo3(get, set)] - items: Vec, -} - -#[test] -fn test_generic_list_get() { - Python::with_gil(|py| { - let list = GenericList { - items: [1i32, 2, 3] - .iter() - .map(|i| i.into_pyobject(py).unwrap().into_any().unbind()) - .collect(), - } - .into_pyobject(py) - .unwrap(); - - py_assert!(py, list, "list.items == [1, 2, 3]"); - }); -} - -#[test] -fn test_generic_list_set() { - Python::with_gil(|py| { - let list = Bound::new(py, GenericList { items: vec![] }).unwrap(); - - py_run!(py, list, "list.items = [1, 2, 3]"); - assert!(list - .borrow() - .items - .iter() - .zip(&[1u32, 2, 3]) - .all(|(a, b)| a.bind(py).eq(b.into_pyobject(py).unwrap()).unwrap())); - }); -} - -#[pyclass(sequence)] -struct OptionList { - #[pyo3(get, set)] - items: Vec>, -} - -#[pymethods] -impl OptionList { - fn __len__(&self) -> usize { - self.items.len() - } - - fn __getitem__(&self, idx: isize) -> PyResult> { - match self.items.get(idx as usize) { - Some(x) => Ok(*x), - None => Err(PyIndexError::new_err("Index out of bounds")), - } - } -} - -#[test] -fn test_option_list_get() { - // Regression test for #798 - Python::with_gil(|py| { - let list = Py::new( - py, - OptionList { - items: vec![Some(1), None], - }, - ) - .unwrap(); - - py_assert!(py, list, "list[0] == 1"); - py_assert!(py, list, "list[1] == None"); - py_expect_exception!(py, list, "list[2]", PyIndexError); - }); -} - -#[test] -fn sequence_is_not_mapping() { - Python::with_gil(|py| { - let list = Bound::new( - py, - OptionList { - items: vec![Some(1), None], - }, - ) - .unwrap() - .into_any(); - - PySequence::register::(py).unwrap(); - - assert!(list.downcast::().is_err()); - assert!(list.downcast::().is_ok()); - }) -} - -#[test] -fn sequence_length() { - Python::with_gil(|py| { - let list = Bound::new( - py, - OptionList { - items: vec![Some(1), None], - }, - ) - .unwrap() - .into_any(); - - assert_eq!(list.len().unwrap(), 2); - assert_eq!(unsafe { ffi::PySequence_Length(list.as_ptr()) }, 2); - - assert_eq!(unsafe { ffi::PyMapping_Length(list.as_ptr()) }, -1); - unsafe { ffi::PyErr_Clear() }; - }) -} diff --git a/vendor/pyo3/tests/test_serde.rs b/vendor/pyo3/tests/test_serde.rs deleted file mode 100644 index 9e97946b5f..0000000000 --- a/vendor/pyo3/tests/test_serde.rs +++ /dev/null @@ -1,80 +0,0 @@ -#[cfg(feature = "serde")] -mod test_serde { - use pyo3::prelude::*; - - use serde::{Deserialize, Serialize}; - - #[pyclass] - #[derive(Debug, Serialize, Deserialize)] - struct Group { - name: String, - } - - #[pyclass] - #[derive(Debug, Serialize, Deserialize)] - struct User { - username: String, - group: Option>, - friends: Vec>, - } - - #[test] - fn test_serialize() { - let friend1 = User { - username: "friend 1".into(), - group: None, - friends: vec![], - }; - let friend2 = User { - username: "friend 2".into(), - group: None, - friends: vec![], - }; - - let user = Python::with_gil(|py| { - let py_friend1 = Py::new(py, friend1).expect("failed to create friend 1"); - let py_friend2 = Py::new(py, friend2).expect("failed to create friend 2"); - - let friends = vec![py_friend1, py_friend2]; - let py_group = Py::new( - py, - Group { - name: "group name".into(), - }, - ) - .unwrap(); - - User { - username: "danya".into(), - group: Some(py_group), - friends, - } - }); - - let serialized = serde_json::to_string(&user).expect("failed to serialize"); - assert_eq!( - serialized, - r#"{"username":"danya","group":{"name":"group name"},"friends":[{"username":"friend 1","group":null,"friends":[]},{"username":"friend 2","group":null,"friends":[]}]}"# - ); - } - - #[test] - fn test_deserialize() { - let serialized = r#"{"username": "danya", "friends": - [{"username": "friend", "group": {"name": "danya's friends"}, "friends": []}]}"#; - let user: User = serde_json::from_str(serialized).expect("failed to deserialize"); - - assert_eq!(user.username, "danya"); - assert!(user.group.is_none()); - assert_eq!(user.friends.len(), 1usize); - let friend = user.friends.first().unwrap(); - - Python::with_gil(|py| { - assert_eq!(friend.borrow(py).username, "friend"); - assert_eq!( - friend.borrow(py).group.as_ref().unwrap().borrow(py).name, - "danya's friends" - ) - }); - } -} diff --git a/vendor/pyo3/tests/test_static_slots.rs b/vendor/pyo3/tests/test_static_slots.rs deleted file mode 100644 index 48d80a4385..0000000000 --- a/vendor/pyo3/tests/test_static_slots.rs +++ /dev/null @@ -1,74 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::exceptions::PyIndexError; -use pyo3::prelude::*; -use pyo3::types::IntoPyDict; - -use pyo3::py_run; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct Count5(); - -#[pymethods] -impl Count5 { - #[new] - fn new() -> Self { - Self() - } - - #[staticmethod] - fn __len__() -> usize { - 5 - } - - #[staticmethod] - fn __getitem__(idx: isize) -> PyResult { - if idx < 0 { - Err(PyIndexError::new_err("Count5 cannot count backwards")) - } else if idx > 4 { - Err(PyIndexError::new_err("Count5 cannot count higher than 5")) - } else { - Ok(idx as f64 + 1.0) - } - } -} - -/// Return a dict with `s = Count5()`. -fn test_dict(py: Python<'_>) -> Bound<'_, pyo3::types::PyDict> { - let d = [("Count5", py.get_type::())] - .into_py_dict(py) - .unwrap(); - // Though we can construct `s` in Rust, let's test `__new__` works. - py_run!(py, *d, "s = Count5()"); - d -} - -#[test] -fn test_len() { - Python::with_gil(|py| { - let d = test_dict(py); - - py_assert!(py, *d, "len(s) == 5"); - }); -} - -#[test] -fn test_getitem() { - Python::with_gil(|py| { - let d = test_dict(py); - - py_assert!(py, *d, "s[4] == 5.0"); - }); -} - -#[test] -fn test_list() { - Python::with_gil(|py| { - let d = test_dict(py); - - py_assert!(py, *d, "list(s) == [1.0, 2.0, 3.0, 4.0, 5.0]"); - }); -} diff --git a/vendor/pyo3/tests/test_string.rs b/vendor/pyo3/tests/test_string.rs deleted file mode 100644 index 02bf2ecd4d..0000000000 --- a/vendor/pyo3/tests/test_string.rs +++ /dev/null @@ -1,23 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyfunction] -fn take_str(_s: &str) {} - -#[test] -fn test_unicode_encode_error() { - Python::with_gil(|py| { - let take_str = wrap_pyfunction!(take_str)(py).unwrap(); - py_expect_exception!( - py, - take_str, - "take_str('\\ud800')", - PyUnicodeEncodeError, - "'utf-8' codec can't encode character '\\ud800' in position 0: surrogates not allowed" - ); - }); -} diff --git a/vendor/pyo3/tests/test_super.rs b/vendor/pyo3/tests/test_super.rs deleted file mode 100644 index 4ff049824b..0000000000 --- a/vendor/pyo3/tests/test_super.rs +++ /dev/null @@ -1,57 +0,0 @@ -#![cfg(all(feature = "macros", not(PyPy)))] - -use pyo3::{prelude::*, types::PySuper}; - -#[pyclass(subclass)] -struct BaseClass { - val1: usize, -} - -#[pymethods] -impl BaseClass { - #[new] - fn new() -> Self { - BaseClass { val1: 10 } - } - - pub fn method(&self) -> usize { - self.val1 - } -} - -#[pyclass(extends=BaseClass)] -struct SubClass {} - -#[pymethods] -impl SubClass { - #[new] - fn new() -> (Self, BaseClass) { - (SubClass {}, BaseClass::new()) - } - - fn method<'py>(self_: &Bound<'py, Self>) -> PyResult> { - let super_ = self_.py_super()?; - super_.call_method("method", (), None) - } - - fn method_super_new<'py>(self_: &Bound<'py, Self>) -> PyResult> { - let super_ = PySuper::new(&self_.get_type(), self_)?; - super_.call_method("method", (), None) - } -} - -#[test] -fn test_call_super_method() { - Python::with_gil(|py| { - let cls = py.get_type::(); - pyo3::py_run!( - py, - cls, - r#" - obj = cls() - assert obj.method() == 10 - assert obj.method_super_new() == 10 - "# - ) - }); -} diff --git a/vendor/pyo3/tests/test_text_signature.rs b/vendor/pyo3/tests/test_text_signature.rs deleted file mode 100644 index 7aceedd44c..0000000000 --- a/vendor/pyo3/tests/test_text_signature.rs +++ /dev/null @@ -1,438 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; -use pyo3::{types::PyType, wrap_pymodule}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[test] -fn class_without_docs_or_signature() { - #[pyclass] - struct MyClass {} - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - py_assert!(py, typeobj, "typeobj.__doc__ is None"); - py_assert!(py, typeobj, "typeobj.__text_signature__ is None"); - }); -} - -#[test] -fn class_with_docs() { - /// docs line1 - #[pyclass] - /// docs line2 - struct MyClass {} - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - py_assert!(py, typeobj, "typeobj.__doc__ == 'docs line1\\ndocs line2'"); - py_assert!(py, typeobj, "typeobj.__text_signature__ is None"); - }); -} - -#[test] -#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)] -fn class_with_signature_no_doc() { - #[pyclass] - struct MyClass {} - - #[pymethods] - impl MyClass { - #[new] - #[pyo3(signature = (a, b=None, *, c=42), text_signature = "(a, b=None, *, c=42)")] - fn __new__(a: i32, b: Option, c: i32) -> Self { - let _ = (a, b, c); - Self {} - } - } - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - py_assert!(py, typeobj, "typeobj.__doc__ == ''"); - py_assert!( - py, - typeobj, - "typeobj.__text_signature__ == '(a, b=None, *, c=42)'" - ); - }); -} - -#[test] -#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)] -fn class_with_docs_and_signature() { - /// docs line1 - #[pyclass] - /// docs line2 - struct MyClass {} - - #[pymethods] - impl MyClass { - #[new] - #[pyo3(signature = (a, b=None, *, c=42), text_signature = "(a, b=None, *, c=42)")] - fn __new__(a: i32, b: Option, c: i32) -> Self { - let _ = (a, b, c); - Self {} - } - } - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - py_assert!(py, typeobj, "typeobj.__doc__ == 'docs line1\\ndocs line2'"); - py_assert!( - py, - typeobj, - "typeobj.__text_signature__ == '(a, b=None, *, c=42)'" - ); - }); -} - -#[test] -fn test_function() { - #[pyfunction(signature = (a, b=None, *, c=42))] - #[pyo3(text_signature = "(a, b=None, *, c=42)")] - fn my_function(a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - - Python::with_gil(|py| { - let f = wrap_pyfunction!(my_function)(py).unwrap(); - - py_assert!(py, f, "f.__text_signature__ == '(a, b=None, *, c=42)'"); - }); -} - -#[test] -fn test_auto_test_signature_function() { - #[pyfunction] - fn my_function(a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[pyfunction(pass_module)] - fn my_function_2(module: &Bound<'_, PyModule>, a: i32, b: i32, c: i32) { - let _ = (module, a, b, c); - } - - #[pyfunction(signature = (a, /, b = None, *, c = 5))] - fn my_function_3(a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - - #[pyfunction(signature = (a, /, b = None, *args, c, d=5, **kwargs))] - fn my_function_4( - a: i32, - b: Option, - args: &Bound<'_, PyTuple>, - c: i32, - d: i32, - kwargs: Option<&Bound<'_, PyDict>>, - ) { - let _ = (a, b, args, c, d, kwargs); - } - - #[pyfunction(signature = (a = 1, /, b = None, c = 1.5, d=5, e = "pyo3", f = 'f', h = true))] - fn my_function_5(a: i32, b: Option, c: f32, d: i32, e: &str, f: char, h: bool) { - let _ = (a, b, c, d, e, f, h); - } - - #[pyfunction] - #[pyo3(signature=(a, b=None, c=None))] - fn my_function_6(a: i32, b: Option, c: Option) { - let _ = (a, b, c); - } - - Python::with_gil(|py| { - let f = wrap_pyfunction!(my_function)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '(a, b, c)', f.__text_signature__" - ); - - let f = wrap_pyfunction!(my_function_2)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '($module, a, b, c)', f.__text_signature__" - ); - - let f = wrap_pyfunction!(my_function_3)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '(a, /, b=None, *, c=5)', f.__text_signature__" - ); - - let f = wrap_pyfunction!(my_function_4)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '(a, /, b=None, *args, c, d=5, **kwargs)', f.__text_signature__" - ); - - let f = wrap_pyfunction!(my_function_5)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '(a=1, /, b=None, c=1.5, d=5, e=\"pyo3\", f=\\'f\\', h=True)', f.__text_signature__" - ); - - let f = wrap_pyfunction!(my_function_6)(py).unwrap(); - py_assert!( - py, - f, - "f.__text_signature__ == '(a, b=None, c=None)', f.__text_signature__" - ); - }); -} - -#[test] -fn test_auto_test_signature_method() { - #[pyclass] - struct MyClass {} - - #[pymethods] - impl MyClass { - #[new] - fn new(a: i32, b: i32, c: i32) -> Self { - let _ = (a, b, c); - Self {} - } - - fn method(&self, a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[pyo3(signature = (a, /, b = None, *, c = 5))] - fn method_2(&self, a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - - #[pyo3(signature = (a, /, b = None, *args, c, d=5, **kwargs))] - fn method_3( - &self, - a: i32, - b: Option, - args: &Bound<'_, PyTuple>, - c: i32, - d: i32, - kwargs: Option<&Bound<'_, PyDict>>, - ) { - let _ = (a, b, args, c, d, kwargs); - } - - #[staticmethod] - fn staticmethod(a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[classmethod] - fn classmethod(cls: &Bound<'_, PyType>, a: i32, b: i32, c: i32) { - let _ = (cls, a, b, c); - } - } - - Python::with_gil(|py| { - let cls = py.get_type::(); - #[cfg(any(not(Py_LIMITED_API), Py_3_10))] - py_assert!(py, cls, "cls.__text_signature__ == '(a, b, c)'"); - py_assert!( - py, - cls, - "cls.method.__text_signature__ == '($self, a, b, c)'" - ); - py_assert!( - py, - cls, - "cls.method_2.__text_signature__ == '($self, a, /, b=None, *, c=5)'" - ); - py_assert!( - py, - cls, - "cls.method_3.__text_signature__ == '($self, a, /, b=None, *args, c, d=5, **kwargs)'" - ); - py_assert!( - py, - cls, - "cls.staticmethod.__text_signature__ == '(a, b, c)'" - ); - py_assert!( - py, - cls, - "cls.classmethod.__text_signature__ == '($cls, a, b, c)'" - ); - }); -} - -#[test] -fn test_auto_test_signature_opt_out() { - #[pyfunction(text_signature = None)] - fn my_function(a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[pyfunction(signature = (a, /, b = None, *, c = 5), text_signature = None)] - fn my_function_2(a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - - #[pyclass] - struct MyClass {} - - #[pymethods] - impl MyClass { - #[new] - #[pyo3(text_signature = None)] - fn new(a: i32, b: i32, c: i32) -> Self { - let _ = (a, b, c); - Self {} - } - - #[pyo3(text_signature = None)] - fn method(&self, a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[pyo3(signature = (a, /, b = None, *, c = 5), text_signature = None)] - fn method_2(&self, a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - - #[staticmethod] - #[pyo3(text_signature = None)] - fn staticmethod(a: i32, b: i32, c: i32) { - let _ = (a, b, c); - } - - #[classmethod] - #[pyo3(text_signature = None)] - fn classmethod(cls: &Bound<'_, PyType>, a: i32, b: i32, c: i32) { - let _ = (cls, a, b, c); - } - } - - Python::with_gil(|py| { - let f = wrap_pyfunction!(my_function)(py).unwrap(); - py_assert!(py, f, "f.__text_signature__ == None"); - - let f = wrap_pyfunction!(my_function_2)(py).unwrap(); - py_assert!(py, f, "f.__text_signature__ == None"); - - let cls = py.get_type::(); - py_assert!(py, cls, "cls.__text_signature__ == None"); - py_assert!(py, cls, "cls.method.__text_signature__ == None"); - py_assert!(py, cls, "cls.method_2.__text_signature__ == None"); - py_assert!(py, cls, "cls.staticmethod.__text_signature__ == None"); - py_assert!(py, cls, "cls.classmethod.__text_signature__ == None"); - }); -} - -#[test] -fn test_pyfn() { - #[pymodule] - fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> { - #[pyfn(m, signature = (a, b=None, *, c=42))] - #[pyo3(text_signature = "(a, b=None, *, c=42)")] - fn my_function(a: i32, b: Option, c: i32) { - let _ = (a, b, c); - } - Ok(()) - } - - Python::with_gil(|py| { - let m = wrap_pymodule!(my_module)(py); - - py_assert!( - py, - m, - "m.my_function.__text_signature__ == '(a, b=None, *, c=42)'" - ); - }); -} - -#[test] -fn test_methods() { - #[pyclass] - struct MyClass {} - - #[pymethods] - impl MyClass { - #[pyo3(text_signature = "($self, a)")] - fn method(&self, a: i32) { - let _ = a; - } - #[pyo3(text_signature = "($self, b)")] - fn pyself_method(_this: &Bound<'_, Self>, b: i32) { - let _ = b; - } - #[classmethod] - #[pyo3(text_signature = "($cls, c)")] - fn class_method(_cls: &Bound<'_, PyType>, c: i32) { - let _ = c; - } - #[staticmethod] - #[pyo3(text_signature = "(d)")] - fn static_method(d: i32) { - let _ = d; - } - } - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - py_assert!( - py, - typeobj, - "typeobj.method.__text_signature__ == '($self, a)'" - ); - py_assert!( - py, - typeobj, - "typeobj.pyself_method.__text_signature__ == '($self, b)'" - ); - py_assert!( - py, - typeobj, - "typeobj.class_method.__text_signature__ == '($cls, c)'" - ); - py_assert!( - py, - typeobj, - "typeobj.static_method.__text_signature__ == '(d)'" - ); - }); -} - -#[test] -#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)] -fn test_raw_identifiers() { - #[pyclass] - struct r#MyClass {} - - #[pymethods] - impl MyClass { - #[new] - fn new() -> MyClass { - MyClass {} - } - fn r#method(&self) {} - } - - Python::with_gil(|py| { - let typeobj = py.get_type::(); - - py_assert!(py, typeobj, "typeobj.__text_signature__ == '()'"); - - py_assert!( - py, - typeobj, - "typeobj.method.__text_signature__ == '($self)'" - ); - }); -} diff --git a/vendor/pyo3/tests/test_variable_arguments.rs b/vendor/pyo3/tests/test_variable_arguments.rs deleted file mode 100644 index d8d9c79e31..0000000000 --- a/vendor/pyo3/tests/test_variable_arguments.rs +++ /dev/null @@ -1,49 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[staticmethod] - #[pyo3(signature = (*args))] - fn test_args(args: Bound<'_, PyTuple>) -> Bound<'_, PyTuple> { - args - } - - #[staticmethod] - #[pyo3(signature = (**kwargs))] - fn test_kwargs(kwargs: Option>) -> Option> { - kwargs - } -} - -#[test] -fn variable_args() { - Python::with_gil(|py| { - let my_obj = py.get_type::(); - py_assert!(py, my_obj, "my_obj.test_args() == ()"); - py_assert!(py, my_obj, "my_obj.test_args(1) == (1,)"); - py_assert!(py, my_obj, "my_obj.test_args(1, 2) == (1, 2)"); - }); -} - -#[test] -fn variable_kwargs() { - Python::with_gil(|py| { - let my_obj = py.get_type::(); - py_assert!(py, my_obj, "my_obj.test_kwargs() == None"); - py_assert!(py, my_obj, "my_obj.test_kwargs(test=1) == {'test': 1}"); - py_assert!( - py, - my_obj, - "my_obj.test_kwargs(test1=1, test2=2) == {'test1':1, 'test2':2}" - ); - }); -} diff --git a/vendor/pyo3/tests/test_various.rs b/vendor/pyo3/tests/test_various.rs deleted file mode 100644 index a8aa6b7a71..0000000000 --- a/vendor/pyo3/tests/test_various.rs +++ /dev/null @@ -1,209 +0,0 @@ -#![cfg(feature = "macros")] - -use pyo3::prelude::*; -use pyo3::py_run; -use pyo3::types::PyTuple; - -use std::fmt; - -#[path = "../src/tests/common.rs"] -mod common; - -#[pyclass] -struct MutRefArg { - n: i32, -} - -#[pymethods] -impl MutRefArg { - fn get(&self) -> i32 { - self.n - } - fn set_other(&self, mut other: PyRefMut<'_, MutRefArg>) { - other.n = 100; - } -} - -#[test] -fn mut_ref_arg() { - Python::with_gil(|py| { - let inst1 = Py::new(py, MutRefArg { n: 0 }).unwrap(); - let inst2 = Py::new(py, MutRefArg { n: 0 }).unwrap(); - - py_run!(py, inst1 inst2, "inst1.set_other(inst2)"); - let inst2 = inst2.bind(py).borrow(); - assert_eq!(inst2.n, 100); - }); -} - -#[pyclass] -struct PyUsize { - #[pyo3(get)] - pub value: usize, -} - -#[pyfunction] -fn get_zero() -> PyUsize { - PyUsize { value: 0 } -} - -#[test] -/// Checks that we can use return a custom class in arbitrary function and use those functions -/// both in rust and python -fn return_custom_class() { - Python::with_gil(|py| { - // Using from rust - assert_eq!(get_zero().value, 0); - - // Using from python - let get_zero = wrap_pyfunction!(get_zero)(py).unwrap(); - py_assert!(py, get_zero, "get_zero().value == 0"); - }); -} - -#[test] -fn intopytuple_primitive() { - Python::with_gil(|py| { - let tup = (1, 2, "foo"); - py_assert!(py, tup, "tup == (1, 2, 'foo')"); - py_assert!(py, tup, "tup[0] == 1"); - py_assert!(py, tup, "tup[1] == 2"); - py_assert!(py, tup, "tup[2] == 'foo'"); - }); -} - -#[pyclass] -struct SimplePyClass {} - -#[test] -fn intopytuple_pyclass() { - Python::with_gil(|py| { - let tup = ( - Py::new(py, SimplePyClass {}).unwrap(), - Py::new(py, SimplePyClass {}).unwrap(), - ); - py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'"); - py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[1]).__name__"); - py_assert!(py, tup, "tup[0] != tup[1]"); - }); -} - -#[test] -fn pytuple_primitive_iter() { - Python::with_gil(|py| { - let tup = PyTuple::new(py, [1u32, 2, 3].iter()).unwrap(); - py_assert!(py, tup, "tup == (1, 2, 3)"); - }); -} - -#[test] -fn pytuple_pyclass_iter() { - Python::with_gil(|py| { - let tup = PyTuple::new( - py, - [ - Py::new(py, SimplePyClass {}).unwrap(), - Py::new(py, SimplePyClass {}).unwrap(), - ] - .iter(), - ) - .unwrap(); - py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'"); - py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[0]).__name__"); - py_assert!(py, tup, "tup[0] != tup[1]"); - }); -} - -#[test] -#[cfg(any(Py_3_9, not(Py_LIMITED_API)))] -fn test_pickle() { - use pyo3::types::PyDict; - - #[pyclass(dict, module = "test_module")] - struct PickleSupport {} - - #[pymethods] - impl PickleSupport { - #[new] - fn new() -> PickleSupport { - PickleSupport {} - } - - pub fn __reduce__<'py>( - slf: &Bound<'py, Self>, - py: Python<'py>, - ) -> PyResult<(Bound<'py, PyAny>, Bound<'py, PyTuple>, Bound<'py, PyAny>)> { - let cls = slf.getattr("__class__")?; - let dict = slf.getattr("__dict__")?; - Ok((cls, PyTuple::empty(py), dict)) - } - } - - fn add_module(module: Bound<'_, PyModule>) -> PyResult<()> { - PyModule::import(module.py(), "sys")? - .dict() - .get_item("modules") - .unwrap() - .unwrap() - .downcast::()? - .set_item(module.name()?, module) - } - - Python::with_gil(|py| { - let module = PyModule::new(py, "test_module").unwrap(); - module.add_class::().unwrap(); - add_module(module).unwrap(); - let inst = Py::new(py, PickleSupport {}).unwrap(); - py_run!( - py, - inst, - r#" - inst.a = 1 - assert inst.__dict__ == {'a': 1} - - import pickle - inst2 = pickle.loads(pickle.dumps(inst)) - - assert inst2.__dict__ == {'a': 1} - "# - ); - }); -} - -/// Testing https://github.com/PyO3/pyo3/issues/1106. A result type that -/// implements `From for PyErr` should be automatically converted -/// when using `#[pyfunction]`. -/// -/// This only makes sure that valid `Result` types do work. For an invalid -/// enum type, see `ui/invalid_result_conversion.py`. -#[derive(Debug)] -struct MyError { - pub descr: &'static str, -} - -impl fmt::Display for MyError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "My error message: {}", self.descr) - } -} - -/// Important for the automatic conversion to `PyErr`. -impl From for PyErr { - fn from(err: MyError) -> pyo3::PyErr { - pyo3::exceptions::PyOSError::new_err(err.to_string()) - } -} - -#[pyfunction] -fn result_conversion_function() -> Result<(), MyError> { - Err(MyError { - descr: "something went wrong", - }) -} - -#[test] -fn test_result_conversion() { - Python::with_gil(|py| { - wrap_pyfunction!(result_conversion_function)(py).unwrap(); - }); -} diff --git a/vendor/pyo3/tests/ui/abi3_dict.rs b/vendor/pyo3/tests/ui/abi3_dict.rs deleted file mode 100644 index 764a4d415a..0000000000 --- a/vendor/pyo3/tests/ui/abi3_dict.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! With abi3, dict not supported until python 3.9 or greater -use pyo3::prelude::*; - -#[pyclass(dict)] -struct TestClass {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/abi3_dict.stderr b/vendor/pyo3/tests/ui/abi3_dict.stderr deleted file mode 100644 index 5887d5aa84..0000000000 --- a/vendor/pyo3/tests/ui/abi3_dict.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: `dict` requires Python >= 3.9 when using the `abi3` feature - --> tests/ui/abi3_dict.rs:4:11 - | -4 | #[pyclass(dict)] - | ^^^^ diff --git a/vendor/pyo3/tests/ui/abi3_inheritance.rs b/vendor/pyo3/tests/ui/abi3_inheritance.rs deleted file mode 100644 index 60972e4cf7..0000000000 --- a/vendor/pyo3/tests/ui/abi3_inheritance.rs +++ /dev/null @@ -1,10 +0,0 @@ -use pyo3::exceptions::PyException; -use pyo3::prelude::*; - -#[pyclass(extends=PyException)] -#[derive(Clone)] -struct MyException { - code: u32, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/abi3_inheritance.stderr b/vendor/pyo3/tests/ui/abi3_inheritance.stderr deleted file mode 100644 index 309b67a633..0000000000 --- a/vendor/pyo3/tests/ui/abi3_inheritance.stderr +++ /dev/null @@ -1,27 +0,0 @@ -error[E0277]: pyclass `PyException` cannot be subclassed - --> tests/ui/abi3_inheritance.rs:4:19 - | -4 | #[pyclass(extends=PyException)] - | ^^^^^^^^^^^ required for `#[pyclass(extends=PyException)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyException` - = note: `PyException` must have `#[pyclass(subclass)]` to be eligible for subclassing - = note: with the `abi3` feature enabled, PyO3 does not support subclassing native types - = help: the trait `PyClassBaseType` is implemented for `PyAny` -note: required by a bound in `PyClassImpl::BaseType` - --> src/impl_/pyclass.rs - | - | type BaseType: PyTypeInfo + PyClassBaseType; - | ^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::BaseType` - -error[E0277]: pyclass `PyException` cannot be subclassed - --> tests/ui/abi3_inheritance.rs:4:1 - | -4 | #[pyclass(extends=PyException)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required for `#[pyclass(extends=PyException)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyException` - = note: `PyException` must have `#[pyclass(subclass)]` to be eligible for subclassing - = note: with the `abi3` feature enabled, PyO3 does not support subclassing native types - = help: the trait `PyClassBaseType` is implemented for `PyAny` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.rs b/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.rs deleted file mode 100644 index 80faff1b73..0000000000 --- a/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! With abi3, we cannot inherit native types. -use pyo3::prelude::*; -use pyo3::types::PyDict; - -#[pyclass(extends=PyDict)] -struct TestClass {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.stderr b/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.stderr deleted file mode 100644 index 872de60b24..0000000000 --- a/vendor/pyo3/tests/ui/abi3_nativetype_inheritance.stderr +++ /dev/null @@ -1,27 +0,0 @@ -error[E0277]: pyclass `PyDict` cannot be subclassed - --> tests/ui/abi3_nativetype_inheritance.rs:5:19 - | -5 | #[pyclass(extends=PyDict)] - | ^^^^^^ required for `#[pyclass(extends=PyDict)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyDict` - = note: `PyDict` must have `#[pyclass(subclass)]` to be eligible for subclassing - = note: with the `abi3` feature enabled, PyO3 does not support subclassing native types - = help: the trait `PyClassBaseType` is implemented for `PyAny` -note: required by a bound in `PyClassImpl::BaseType` - --> src/impl_/pyclass.rs - | - | type BaseType: PyTypeInfo + PyClassBaseType; - | ^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::BaseType` - -error[E0277]: pyclass `PyDict` cannot be subclassed - --> tests/ui/abi3_nativetype_inheritance.rs:5:1 - | -5 | #[pyclass(extends=PyDict)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required for `#[pyclass(extends=PyDict)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyDict` - = note: `PyDict` must have `#[pyclass(subclass)]` to be eligible for subclassing - = note: with the `abi3` feature enabled, PyO3 does not support subclassing native types - = help: the trait `PyClassBaseType` is implemented for `PyAny` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/abi3_weakref.rs b/vendor/pyo3/tests/ui/abi3_weakref.rs deleted file mode 100644 index f45b2258c9..0000000000 --- a/vendor/pyo3/tests/ui/abi3_weakref.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! With abi3, weakref not supported until python 3.9 or greater -use pyo3::prelude::*; - -#[pyclass(weakref)] -struct TestClass {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/abi3_weakref.stderr b/vendor/pyo3/tests/ui/abi3_weakref.stderr deleted file mode 100644 index b8ef3936cd..0000000000 --- a/vendor/pyo3/tests/ui/abi3_weakref.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: `weakref` requires Python >= 3.9 when using the `abi3` feature - --> tests/ui/abi3_weakref.rs:4:11 - | -4 | #[pyclass(weakref)] - | ^^^^^^^ diff --git a/vendor/pyo3/tests/ui/ambiguous_associated_items.rs b/vendor/pyo3/tests/ui/ambiguous_associated_items.rs deleted file mode 100644 index f553ba1f33..0000000000 --- a/vendor/pyo3/tests/ui/ambiguous_associated_items.rs +++ /dev/null @@ -1,25 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass(eq)] -#[derive(PartialEq)] -pub enum SimpleItems { - Error, - Output, - Target, -} - -#[pyclass] -pub enum ComplexItems { - Error(PyObject), - Output(PyObject), - Target(PyObject), -} - -#[derive(IntoPyObject)] -enum DeriveItems { - Error(PyObject), - Output(PyObject), - Target(PyObject), -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/deprecations.rs b/vendor/pyo3/tests/ui/deprecations.rs deleted file mode 100644 index da78a826ca..0000000000 --- a/vendor/pyo3/tests/ui/deprecations.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![deny(deprecated)] -#![allow(dead_code)] - -use pyo3::prelude::*; - -fn extract_options(obj: &Bound<'_, PyAny>) -> PyResult> { - obj.extract() -} - -#[pyfunction] -#[pyo3(signature = (_i, _any=None))] -fn pyfunction_option_1(_i: u32, _any: Option) {} - -#[pyfunction] -fn pyfunction_option_2(_i: u32, _any: Option) {} - -#[pyfunction] -fn pyfunction_option_3(_i: u32, _any: Option, _foo: Option) {} - -#[pyfunction] -fn pyfunction_option_4( - _i: u32, - #[pyo3(from_py_with = "extract_options")] _any: Option, - _foo: Option, -) { -} - -#[pyclass] -pub enum SimpleEnumWithoutEq { - VariamtA, - VariantB, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/deprecations.stderr b/vendor/pyo3/tests/ui/deprecations.stderr deleted file mode 100644 index 6236dc5563..0000000000 --- a/vendor/pyo3/tests/ui/deprecations.stderr +++ /dev/null @@ -1,37 +0,0 @@ -error: use of deprecated constant `__pyfunction_pyfunction_option_2::SIGNATURE`: this function has implicit defaults for the trailing `Option` arguments - = note: these implicit defaults are being phased out - = help: add `#[pyo3(signature = (_i, _any=None))]` to this function to silence this warning and keep the current behavior - --> tests/ui/deprecations.rs:15:4 - | -15 | fn pyfunction_option_2(_i: u32, _any: Option) {} - | ^^^^^^^^^^^^^^^^^^^ - | -note: the lint level is defined here - --> tests/ui/deprecations.rs:1:9 - | -1 | #![deny(deprecated)] - | ^^^^^^^^^^ - -error: use of deprecated constant `__pyfunction_pyfunction_option_3::SIGNATURE`: this function has implicit defaults for the trailing `Option` arguments - = note: these implicit defaults are being phased out - = help: add `#[pyo3(signature = (_i, _any=None, _foo=None))]` to this function to silence this warning and keep the current behavior - --> tests/ui/deprecations.rs:18:4 - | -18 | fn pyfunction_option_3(_i: u32, _any: Option, _foo: Option) {} - | ^^^^^^^^^^^^^^^^^^^ - -error: use of deprecated constant `__pyfunction_pyfunction_option_4::SIGNATURE`: this function has implicit defaults for the trailing `Option` arguments - = note: these implicit defaults are being phased out - = help: add `#[pyo3(signature = (_i, _any=None, _foo=None))]` to this function to silence this warning and keep the current behavior - --> tests/ui/deprecations.rs:21:4 - | -21 | fn pyfunction_option_4( - | ^^^^^^^^^^^^^^^^^^^ - -error: use of deprecated method `pyo3::impl_::pyclass::Deprecation::autogenerated_equality`: Implicit equality for simple enums is deprecated. Use `#[pyclass(eq, eq_int)]` to keep the current behavior. - --> tests/ui/deprecations.rs:28:1 - | -28 | #[pyclass] - | ^^^^^^^^^^ - | - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.rs b/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.rs deleted file mode 100644 index 774d381964..0000000000 --- a/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[pyo3::pymodule] -mod mymodule { - #[pyo3::pymodule(submodule)] - mod submod {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.stderr b/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.stderr deleted file mode 100644 index a2141b0dcd..0000000000 --- a/vendor/pyo3/tests/ui/duplicate_pymodule_submodule.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error: `submodule` may only be specified once (it is implicitly always specified for nested modules) - --> tests/ui/duplicate_pymodule_submodule.rs:4:2 - | -4 | mod submod {} - | ^^^ - -error[E0425]: cannot find value `_PYO3_DEF` in module `submod` - --> tests/ui/duplicate_pymodule_submodule.rs:1:1 - | -1 | #[pyo3::pymodule] - | ^^^^^^^^^^^^^^^^^ not found in `submod` - | - = note: this error originates in the attribute macro `pyo3::pymodule` (in Nightly builds, run with -Z macro-backtrace for more info) -help: consider importing this static - | -3 + use crate::mymodule::_PYO3_DEF; - | diff --git a/vendor/pyo3/tests/ui/empty.rs b/vendor/pyo3/tests/ui/empty.rs deleted file mode 100644 index be89c63642..0000000000 --- a/vendor/pyo3/tests/ui/empty.rs +++ /dev/null @@ -1 +0,0 @@ -// see invalid_pymodule_in_root.rs diff --git a/vendor/pyo3/tests/ui/forbid_unsafe.rs b/vendor/pyo3/tests/ui/forbid_unsafe.rs deleted file mode 100644 index 4ab7639d65..0000000000 --- a/vendor/pyo3/tests/ui/forbid_unsafe.rs +++ /dev/null @@ -1,43 +0,0 @@ -#![forbid(unsafe_code)] -#![forbid(unsafe_op_in_unsafe_fn)] - -use pyo3::*; - -#[allow(unexpected_cfgs)] -#[path = "../../src/tests/hygiene/mod.rs"] -mod hygiene; - -mod gh_4394 { - use pyo3::prelude::*; - - #[derive(Eq, Ord, PartialEq, PartialOrd, Clone)] - #[pyclass(get_all)] - pub struct VersionSpecifier { - pub(crate) operator: Operator, - pub(crate) version: Version, - } - - #[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash, Clone, Copy)] - #[pyo3::pyclass(eq, eq_int)] - pub enum Operator { - Equal, - } - - #[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] - #[pyclass] - pub struct Version; -} - -mod from_py_with { - use pyo3::prelude::*; - use pyo3::types::PyBytes; - - fn bytes_from_py(bytes: &Bound<'_, PyAny>) -> PyResult> { - Ok(bytes.downcast::()?.as_bytes().to_vec()) - } - - #[pyfunction] - fn f(#[pyo3(from_py_with = "bytes_from_py")] _bytes: Vec) {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/get_set_all.rs b/vendor/pyo3/tests/ui/get_set_all.rs deleted file mode 100644 index 20a5eca07b..0000000000 --- a/vendor/pyo3/tests/ui/get_set_all.rs +++ /dev/null @@ -1,21 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass(set_all)] -struct Foo; - -#[pyclass(set_all)] -struct Foo2{ - #[pyo3(set)] - field: u8, -} - -#[pyclass(get_all)] -struct Foo3; - -#[pyclass(get_all)] -struct Foo4{ - #[pyo3(get)] - field: u8, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/get_set_all.stderr b/vendor/pyo3/tests/ui/get_set_all.stderr deleted file mode 100644 index 607d5f9659..0000000000 --- a/vendor/pyo3/tests/ui/get_set_all.stderr +++ /dev/null @@ -1,23 +0,0 @@ -error: `set_all` on an unit struct does nothing, because unit structs have no fields - --> tests/ui/get_set_all.rs:3:11 - | -3 | #[pyclass(set_all)] - | ^^^^^^^ - -error: useless `set` - the struct is already annotated with `set_all` - --> tests/ui/get_set_all.rs:8:12 - | -8 | #[pyo3(set)] - | ^^^ - -error: `get_all` on an unit struct does nothing, because unit structs have no fields - --> tests/ui/get_set_all.rs:12:11 - | -12 | #[pyclass(get_all)] - | ^^^^^^^ - -error: useless `get` - the struct is already annotated with `get_all` - --> tests/ui/get_set_all.rs:17:12 - | -17 | #[pyo3(get)] - | ^^^ diff --git a/vendor/pyo3/tests/ui/invalid_argument_attributes.rs b/vendor/pyo3/tests/ui/invalid_argument_attributes.rs deleted file mode 100644 index 6797642d77..0000000000 --- a/vendor/pyo3/tests/ui/invalid_argument_attributes.rs +++ /dev/null @@ -1,18 +0,0 @@ -use pyo3::prelude::*; - -#[pyfunction] -fn invalid_attribute(#[pyo3(get)] _param: String) {} - -#[pyfunction] -fn from_py_with_no_value(#[pyo3(from_py_with)] _param: String) {} - -#[pyfunction] -fn from_py_with_string(#[pyo3("from_py_with")] _param: String) {} - -#[pyfunction] -fn from_py_with_value_not_a_string(#[pyo3(from_py_with = func)] _param: String) {} - -#[pyfunction] -fn from_py_with_repeated(#[pyo3(from_py_with = "func", from_py_with = "func")] _param: String) {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_argument_attributes.stderr b/vendor/pyo3/tests/ui/invalid_argument_attributes.stderr deleted file mode 100644 index e6c42f82a8..0000000000 --- a/vendor/pyo3/tests/ui/invalid_argument_attributes.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error: expected `cancel_handle` or `from_py_with` - --> tests/ui/invalid_argument_attributes.rs:4:29 - | -4 | fn invalid_attribute(#[pyo3(get)] _param: String) {} - | ^^^ - -error: expected `=` - --> tests/ui/invalid_argument_attributes.rs:7:45 - | -7 | fn from_py_with_no_value(#[pyo3(from_py_with)] _param: String) {} - | ^ - -error: expected `cancel_handle` or `from_py_with` - --> tests/ui/invalid_argument_attributes.rs:10:31 - | -10 | fn from_py_with_string(#[pyo3("from_py_with")] _param: String) {} - | ^^^^^^^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_argument_attributes.rs:13:58 - | -13 | fn from_py_with_value_not_a_string(#[pyo3(from_py_with = func)] _param: String) {} - | ^^^^ - -error: `from_py_with` may only be specified once per argument - --> tests/ui/invalid_argument_attributes.rs:16:56 - | -16 | fn from_py_with_repeated(#[pyo3(from_py_with = "func", from_py_with = "func")] _param: String) {} - | ^^^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_base_class.rs b/vendor/pyo3/tests/ui/invalid_base_class.rs deleted file mode 100644 index 7433dcb2b9..0000000000 --- a/vendor/pyo3/tests/ui/invalid_base_class.rs +++ /dev/null @@ -1,7 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyBool; - -#[pyclass(extends=PyBool)] -struct ExtendsBool; - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_base_class.stderr b/vendor/pyo3/tests/ui/invalid_base_class.stderr deleted file mode 100644 index c40bed9eaa..0000000000 --- a/vendor/pyo3/tests/ui/invalid_base_class.stderr +++ /dev/null @@ -1,43 +0,0 @@ -error[E0277]: pyclass `PyBool` cannot be subclassed - --> tests/ui/invalid_base_class.rs:4:19 - | -4 | #[pyclass(extends=PyBool)] - | ^^^^^^ required for `#[pyclass(extends=PyBool)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyBool` - = note: `PyBool` must have `#[pyclass(subclass)]` to be eligible for subclassing - = help: the following other types implement trait `PyClassBaseType`: - PyAny - PyArithmeticError - PyAssertionError - PyAttributeError - PyBaseException - PyBaseExceptionGroup - PyBlockingIOError - PyBrokenPipeError - and $N others -note: required by a bound in `PyClassImpl::BaseType` - --> src/impl_/pyclass.rs - | - | type BaseType: PyTypeInfo + PyClassBaseType; - | ^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::BaseType` - -error[E0277]: pyclass `PyBool` cannot be subclassed - --> tests/ui/invalid_base_class.rs:4:1 - | -4 | #[pyclass(extends=PyBool)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required for `#[pyclass(extends=PyBool)]` - | - = help: the trait `PyClassBaseType` is not implemented for `PyBool` - = note: `PyBool` must have `#[pyclass(subclass)]` to be eligible for subclassing - = help: the following other types implement trait `PyClassBaseType`: - PyAny - PyArithmeticError - PyAssertionError - PyAttributeError - PyBaseException - PyBaseExceptionGroup - PyBlockingIOError - PyBrokenPipeError - and $N others - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/invalid_cancel_handle.rs b/vendor/pyo3/tests/ui/invalid_cancel_handle.rs deleted file mode 100644 index cff6c5dcba..0000000000 --- a/vendor/pyo3/tests/ui/invalid_cancel_handle.rs +++ /dev/null @@ -1,28 +0,0 @@ -use pyo3::prelude::*; - -#[pyfunction] -async fn cancel_handle_repeated(#[pyo3(cancel_handle, cancel_handle)] _param: String) {} - -#[pyfunction] -async fn cancel_handle_repeated2( - #[pyo3(cancel_handle)] _param: String, - #[pyo3(cancel_handle)] _param2: String, -) { -} - -#[pyfunction] -fn cancel_handle_synchronous(#[pyo3(cancel_handle)] _param: String) {} - -#[pyfunction] -async fn cancel_handle_wrong_type(#[pyo3(cancel_handle)] _param: String) {} - -#[pyfunction] -async fn missing_cancel_handle_attribute(_param: pyo3::coroutine::CancelHandle) {} - -#[pyfunction] -async fn cancel_handle_and_from_py_with( - #[pyo3(cancel_handle, from_py_with = "cancel_handle")] _param: pyo3::coroutine::CancelHandle, -) { -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_cancel_handle.stderr b/vendor/pyo3/tests/ui/invalid_cancel_handle.stderr deleted file mode 100644 index 90a8caa422..0000000000 --- a/vendor/pyo3/tests/ui/invalid_cancel_handle.stderr +++ /dev/null @@ -1,81 +0,0 @@ -error: `cancel_handle` may only be specified once per argument - --> tests/ui/invalid_cancel_handle.rs:4:55 - | -4 | async fn cancel_handle_repeated(#[pyo3(cancel_handle, cancel_handle)] _param: String) {} - | ^^^^^^^^^^^^^ - -error: `cancel_handle` may only be specified once - --> tests/ui/invalid_cancel_handle.rs:9:28 - | -9 | #[pyo3(cancel_handle)] _param2: String, - | ^^^^^^^ - -error: `cancel_handle` attribute can only be used with `async fn` - --> tests/ui/invalid_cancel_handle.rs:14:53 - | -14 | fn cancel_handle_synchronous(#[pyo3(cancel_handle)] _param: String) {} - | ^^^^^^ - -error: `from_py_with` and `cancel_handle` cannot be specified together - --> tests/ui/invalid_cancel_handle.rs:24:12 - | -24 | #[pyo3(cancel_handle, from_py_with = "cancel_handle")] _param: pyo3::coroutine::CancelHandle, - | ^^^^^^^^^^^^^ - -error[E0308]: mismatched types - --> tests/ui/invalid_cancel_handle.rs:16:1 - | -16 | #[pyfunction] - | ^^^^^^^^^^^^^ - | | - | expected `String`, found `CancelHandle` - | arguments to this function are incorrect - | -note: function defined here - --> tests/ui/invalid_cancel_handle.rs:17:10 - | -17 | async fn cancel_handle_wrong_type(#[pyo3(cancel_handle)] _param: String) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ -------------- - = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0277]: the trait bound `CancelHandle: PyFunctionArgument<'_, '_>` is not satisfied - --> tests/ui/invalid_cancel_handle.rs:20:50 - | -20 | async fn missing_cancel_handle_attribute(_param: pyo3::coroutine::CancelHandle) {} - | ^^^^ the trait `PyClass` is not implemented for `CancelHandle` - | - = help: the trait `PyClass` is implemented for `pyo3::coroutine::Coroutine` - = note: required for `CancelHandle` to implement `FromPyObject<'_>` - = note: required for `CancelHandle` to implement `FromPyObjectBound<'_, '_>` - = note: required for `CancelHandle` to implement `PyFunctionArgument<'_, '_>` -note: required by a bound in `extract_argument` - --> src/impl_/extract_argument.rs - | - | pub fn extract_argument<'a, 'py, T>( - | ---------------- required by a bound in this function -... - | T: PyFunctionArgument<'a, 'py>, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `extract_argument` - -error[E0277]: the trait bound `CancelHandle: PyFunctionArgument<'_, '_>` is not satisfied - --> tests/ui/invalid_cancel_handle.rs:20:50 - | -20 | async fn missing_cancel_handle_attribute(_param: pyo3::coroutine::CancelHandle) {} - | ^^^^ the trait `Clone` is not implemented for `CancelHandle` - | - = help: the following other types implement trait `PyFunctionArgument<'a, 'py>`: - &'a mut pyo3::coroutine::Coroutine - &'a pyo3::Bound<'py, T> - &'a pyo3::coroutine::Coroutine - Option<&'a pyo3::Bound<'py, T>> - = note: required for `CancelHandle` to implement `FromPyObject<'_>` - = note: required for `CancelHandle` to implement `FromPyObjectBound<'_, '_>` - = note: required for `CancelHandle` to implement `PyFunctionArgument<'_, '_>` -note: required by a bound in `extract_argument` - --> src/impl_/extract_argument.rs - | - | pub fn extract_argument<'a, 'py, T>( - | ---------------- required by a bound in this function -... - | T: PyFunctionArgument<'a, 'py>, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `extract_argument` diff --git a/vendor/pyo3/tests/ui/invalid_closure.rs b/vendor/pyo3/tests/ui/invalid_closure.rs deleted file mode 100644 index cb30237593..0000000000 --- a/vendor/pyo3/tests/ui/invalid_closure.rs +++ /dev/null @@ -1,22 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::{PyCFunction, PyDict, PyTuple}; - -fn main() { - let fun: Py = Python::with_gil(|py| { - let local_data = vec![0, 1, 2, 3, 4]; - let ref_: &[u8] = &local_data; - - let closure_fn = - |_args: &Bound<'_, PyTuple>, _kwargs: Option<&Bound<'_, PyDict>>| -> PyResult<()> { - println!("This is five: {:?}", ref_.len()); - Ok(()) - }; - PyCFunction::new_closure(py, None, None, closure_fn) - .unwrap() - .into() - }); - - Python::with_gil(|py| { - fun.call0(py).unwrap(); - }); -} diff --git a/vendor/pyo3/tests/ui/invalid_closure.stderr b/vendor/pyo3/tests/ui/invalid_closure.stderr deleted file mode 100644 index 4791954f3e..0000000000 --- a/vendor/pyo3/tests/ui/invalid_closure.stderr +++ /dev/null @@ -1,13 +0,0 @@ -error[E0597]: `local_data` does not live long enough - --> tests/ui/invalid_closure.rs:7:27 - | -6 | let local_data = vec![0, 1, 2, 3, 4]; - | ---------- binding `local_data` declared here -7 | let ref_: &[u8] = &local_data; - | ^^^^^^^^^^^ borrowed value does not live long enough -... -14 | PyCFunction::new_closure(py, None, None, closure_fn) - | ---------------------------------------------------- argument requires that `local_data` is borrowed for `'static` -... -17 | }); - | - `local_data` dropped here while still borrowed diff --git a/vendor/pyo3/tests/ui/invalid_frompy_derive.rs b/vendor/pyo3/tests/ui/invalid_frompy_derive.rs deleted file mode 100644 index f123b149fb..0000000000 --- a/vendor/pyo3/tests/ui/invalid_frompy_derive.rs +++ /dev/null @@ -1,216 +0,0 @@ -use pyo3::FromPyObject; - -#[derive(FromPyObject)] -struct Foo(); - -#[derive(FromPyObject)] -struct Foo2 {} - -#[derive(FromPyObject)] -enum EmptyEnum {} - -#[derive(FromPyObject)] -enum EnumWithEmptyTupleVar { - EmptyTuple(), - Valid(String), -} - -#[derive(FromPyObject)] -enum EnumWithEmptyStructVar { - EmptyStruct {}, - Valid(String), -} - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct EmptyTransparentTup(); - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct EmptyTransparentStruct {} - -#[derive(FromPyObject)] -enum EnumWithTransparentEmptyTupleVar { - #[pyo3(transparent)] - EmptyTuple(), - Valid(String), -} - -#[derive(FromPyObject)] -enum EnumWithTransparentEmptyStructVar { - #[pyo3(transparent)] - EmptyStruct {}, - Valid(String), -} - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct TransparentTupTooManyFields(String, String); - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct TransparentStructTooManyFields { - foo: String, - bar: String, -} - -#[derive(FromPyObject)] -enum EnumWithTransparentTupleTooMany { - #[pyo3(transparent)] - EmptyTuple(String, String), - Valid(String), -} - -#[derive(FromPyObject)] -enum EnumWithTransparentStructTooMany { - #[pyo3(transparent)] - EmptyStruct { - foo: String, - bar: String, - }, - Valid(String), -} - -#[derive(FromPyObject)] -struct UnknownAttribute { - #[pyo3(attr)] - a: String, -} - -#[derive(FromPyObject)] -struct InvalidAttributeArg { - #[pyo3(attribute(1))] - a: String, -} - -#[derive(FromPyObject)] -struct TooManyAttributeArgs { - #[pyo3(attribute("a", "b"))] - a: String, -} - -#[derive(FromPyObject)] -struct EmptyAttributeArg { - #[pyo3(attribute(""))] - a: String, -} - -#[derive(FromPyObject)] -struct NoAttributeArg { - #[pyo3(attribute())] - a: String, -} - -#[derive(FromPyObject)] -struct TooManyitemArgs { - #[pyo3(item("a", "b"))] - a: String, -} - -#[derive(FromPyObject)] -struct NoItemArg { - #[pyo3(item())] - a: String, -} - -#[derive(FromPyObject)] -struct ItemAndAttribute { - #[pyo3(item, attribute)] - a: String, -} - -#[derive(FromPyObject)] -#[pyo3(unknown = "should not work")] -struct UnknownContainerAttr { - a: String, -} - -#[derive(FromPyObject)] -#[pyo3(annotation = "should not work")] -struct AnnotationOnStruct { - a: String, -} - -#[derive(FromPyObject)] -enum InvalidAnnotatedEnum { - #[pyo3(annotation = 1)] - Foo(String), -} - -#[derive(FromPyObject)] -enum TooManyLifetimes<'a, 'b> { - Foo(&'a str), - Bar(&'b str), -} - -#[derive(FromPyObject)] -union Union { - a: usize, -} - -#[derive(FromPyObject)] -enum UnitEnum { - Unit, -} - -#[derive(FromPyObject)] -struct InvalidFromPyWith { - #[pyo3(from_py_with)] - field: String, -} - -#[derive(FromPyObject)] -struct InvalidFromPyWithLiteral { - #[pyo3(from_py_with = func)] - field: String, -} - -#[derive(FromPyObject)] -struct InvalidTupleGetter(#[pyo3(item("foo"))] String); - -#[derive(FromPyObject)] -#[pyo3(transparent)] -struct InvalidTransparentWithGetter { - #[pyo3(item("foo"))] - field: String, -} - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -struct FromItemAllOnTuple(String); - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -#[pyo3(transparent)] -struct FromItemAllWithTransparent { - field: String, -} - -#[derive(FromPyObject)] -#[pyo3(from_item_all, from_item_all)] -struct MultipleFromItemAll { - field: String, -} - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -struct UselessItemAttr { - #[pyo3(item)] - field: String, -} - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -struct FromItemAllConflictAttr { - #[pyo3(attribute)] - field: String, -} - -#[derive(FromPyObject)] -#[pyo3(from_item_all)] -struct FromItemAllConflictAttrWithArgs { - #[pyo3(attribute("f"))] - field: String, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_frompy_derive.stderr b/vendor/pyo3/tests/ui/invalid_frompy_derive.stderr deleted file mode 100644 index 8ed03caafb..0000000000 --- a/vendor/pyo3/tests/ui/invalid_frompy_derive.stderr +++ /dev/null @@ -1,225 +0,0 @@ -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:4:11 - | -4 | struct Foo(); - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:7:13 - | -7 | struct Foo2 {} - | ^^ - -error: cannot derive FromPyObject for empty enum - --> tests/ui/invalid_frompy_derive.rs:10:6 - | -10 | enum EmptyEnum {} - | ^^^^^^^^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:14:15 - | -14 | EmptyTuple(), - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:20:17 - | -20 | EmptyStruct {}, - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:26:27 - | -26 | struct EmptyTransparentTup(); - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:30:31 - | -30 | struct EmptyTransparentStruct {} - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:35:15 - | -35 | EmptyTuple(), - | ^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:42:17 - | -42 | EmptyStruct {}, - | ^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_frompy_derive.rs:48:35 - | -48 | struct TransparentTupTooManyFields(String, String); - | ^^^^^^^^^^^^^^^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_frompy_derive.rs:52:39 - | -52 | struct TransparentStructTooManyFields { - | _______________________________________^ -53 | | foo: String, -54 | | bar: String, -55 | | } - | |_^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_frompy_derive.rs:60:15 - | -60 | EmptyTuple(String, String), - | ^^^^^^^^^^^^^^^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_frompy_derive.rs:67:17 - | -67 | EmptyStruct { - | _________________^ -68 | | foo: String, -69 | | bar: String, -70 | | }, - | |_____^ - -error: expected one of: `attribute`, `item`, `from_py_with` - --> tests/ui/invalid_frompy_derive.rs:76:12 - | -76 | #[pyo3(attr)] - | ^^^^ - -error: expected string literal - --> tests/ui/invalid_frompy_derive.rs:82:22 - | -82 | #[pyo3(attribute(1))] - | ^ - -error: expected at most one argument: `attribute` or `attribute("name")` - --> tests/ui/invalid_frompy_derive.rs:88:25 - | -88 | #[pyo3(attribute("a", "b"))] - | ^ - -error: attribute name cannot be empty - --> tests/ui/invalid_frompy_derive.rs:94:22 - | -94 | #[pyo3(attribute(""))] - | ^^ - -error: unexpected end of input, expected string literal - --> tests/ui/invalid_frompy_derive.rs:100:22 - | -100 | #[pyo3(attribute())] - | ^ - -error: expected at most one argument: `item` or `item(key)` - --> tests/ui/invalid_frompy_derive.rs:106:20 - | -106 | #[pyo3(item("a", "b"))] - | ^ - -error: unexpected end of input, expected literal - --> tests/ui/invalid_frompy_derive.rs:112:17 - | -112 | #[pyo3(item())] - | ^ - -error: only one of `attribute` or `item` can be provided - --> tests/ui/invalid_frompy_derive.rs:118:5 - | -118 | #[pyo3(item, attribute)] - | ^ - -error: expected one of: `transparent`, `from_item_all`, `annotation`, `crate` - --> tests/ui/invalid_frompy_derive.rs:123:8 - | -123 | #[pyo3(unknown = "should not work")] - | ^^^^^^^ - -error: `annotation` is unsupported for structs - --> tests/ui/invalid_frompy_derive.rs:129:21 - | -129 | #[pyo3(annotation = "should not work")] - | ^^^^^^^^^^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_frompy_derive.rs:136:25 - | -136 | #[pyo3(annotation = 1)] - | ^ - -error: FromPyObject can be derived with at most one lifetime parameter - --> tests/ui/invalid_frompy_derive.rs:141:22 - | -141 | enum TooManyLifetimes<'a, 'b> { - | ^ - -error: #[derive(FromPyObject)] is not supported for unions - --> tests/ui/invalid_frompy_derive.rs:147:1 - | -147 | union Union { - | ^^^^^ - -error: cannot derive FromPyObject for empty structs and variants - --> tests/ui/invalid_frompy_derive.rs:151:10 - | -151 | #[derive(FromPyObject)] - | ^^^^^^^^^^^^ - | - = note: this error originates in the derive macro `FromPyObject` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: expected `=` - --> tests/ui/invalid_frompy_derive.rs:158:24 - | -158 | #[pyo3(from_py_with)] - | ^ - -error: expected string literal - --> tests/ui/invalid_frompy_derive.rs:164:27 - | -164 | #[pyo3(from_py_with = func)] - | ^^^^ - -error: `getter` is not permitted on tuple struct elements. - --> tests/ui/invalid_frompy_derive.rs:169:27 - | -169 | struct InvalidTupleGetter(#[pyo3(item("foo"))] String); - | ^ - -error: `transparent` structs may not have a `getter` for the inner field - --> tests/ui/invalid_frompy_derive.rs:175:5 - | -175 | field: String, - | ^^^^^ - -error: `transparent` structs may not have a `getter` for the inner field - --> tests/ui/invalid_frompy_derive.rs:186:5 - | -186 | field: String, - | ^^^^^ - -error: `from_item_all` may only be provided once - --> tests/ui/invalid_frompy_derive.rs:190:23 - | -190 | #[pyo3(from_item_all, from_item_all)] - | ^^^^^^^^^^^^^ - -error: Useless `item` - the struct is already annotated with `from_item_all` - --> tests/ui/invalid_frompy_derive.rs:196:8 - | -196 | #[pyo3(from_item_all)] - | ^^^^^^^^^^^^^ - -error: The struct is already annotated with `from_item_all`, `attribute` is not allowed - --> tests/ui/invalid_frompy_derive.rs:203:8 - | -203 | #[pyo3(from_item_all)] - | ^^^^^^^^^^^^^ - -error: The struct is already annotated with `from_item_all`, `attribute` is not allowed - --> tests/ui/invalid_frompy_derive.rs:210:8 - | -210 | #[pyo3(from_item_all)] - | ^^^^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.rs b/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.rs deleted file mode 100644 index 6379a8707c..0000000000 --- a/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass(frozen)] -pub struct Foo { - #[pyo3(get)] - field: u32, -} - -#[pymethods] -impl Foo { - fn mut_method(&mut self) {} -} - -fn borrow_mut_fails(foo: Py, py: Python) { - let borrow = foo.bind(py).borrow_mut(); -} - -#[pyclass(subclass)] -struct MutableBase; - -#[pyclass(frozen, extends = MutableBase)] -struct ImmutableChild; - -fn borrow_mut_of_child_fails(child: Py, py: Python) { - let borrow = child.bind(py).borrow_mut(); -} - -fn py_get_of_mutable_class_fails(class: Py) { - class.get(); -} - -fn pyclass_get_of_mutable_class_fails(class: &Bound<'_, MutableBase>) { - class.get(); -} - -#[pyclass(frozen)] -pub struct SetOnFrozenClass { - #[pyo3(set)] - field: u32, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.stderr b/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.stderr deleted file mode 100644 index 52a0623f28..0000000000 --- a/vendor/pyo3/tests/ui/invalid_frozen_pyclass_borrow.stderr +++ /dev/null @@ -1,90 +0,0 @@ -error: cannot use `#[pyo3(set)]` on a `frozen` class - --> tests/ui/invalid_frozen_pyclass_borrow.rs:38:12 - | -38 | #[pyo3(set)] - | ^^^ - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:11:19 - | -11 | fn mut_method(&mut self) {} - | ^ expected `False`, found `True` - | -note: required by a bound in `extract_pyclass_ref_mut` - --> src/impl_/extract_argument.rs - | - | pub fn extract_pyclass_ref_mut<'a, 'py: 'a, T: PyClass>( - | ^^^^^^^^^^^^^^ required by this bound in `extract_pyclass_ref_mut` - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:9:1 - | -9 | #[pymethods] - | ^^^^^^^^^^^^ expected `False`, found `True` - | -note: required by a bound in `PyRefMut` - --> src/pycell.rs - | - | pub struct PyRefMut<'p, T: PyClass> { - | ^^^^^^^^^^^^^^ required by this bound in `PyRefMut` - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:15:31 - | -15 | let borrow = foo.bind(py).borrow_mut(); - | ^^^^^^^^^^ expected `False`, found `True` - | -note: required by a bound in `pyo3::Bound::<'py, T>::borrow_mut` - --> src/instance.rs - | - | pub fn borrow_mut(&self) -> PyRefMut<'py, T> - | ---------- required by a bound in this associated function - | where - | T: PyClass, - | ^^^^^^^^^^^^^^ required by this bound in `Bound::<'py, T>::borrow_mut` - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:25:33 - | -25 | let borrow = child.bind(py).borrow_mut(); - | ^^^^^^^^^^ expected `False`, found `True` - | -note: required by a bound in `pyo3::Bound::<'py, T>::borrow_mut` - --> src/instance.rs - | - | pub fn borrow_mut(&self) -> PyRefMut<'py, T> - | ---------- required by a bound in this associated function - | where - | T: PyClass, - | ^^^^^^^^^^^^^^ required by this bound in `Bound::<'py, T>::borrow_mut` - -error[E0271]: type mismatch resolving `::Frozen == True` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:29:11 - | -29 | class.get(); - | ^^^ expected `True`, found `False` - | -note: required by a bound in `pyo3::Py::::get` - --> src/instance.rs - | - | pub fn get(&self) -> &T - | --- required by a bound in this associated function - | where - | T: PyClass + Sync, - | ^^^^^^^^^^^^^ required by this bound in `Py::::get` - -error[E0271]: type mismatch resolving `::Frozen == True` - --> tests/ui/invalid_frozen_pyclass_borrow.rs:33:11 - | -33 | class.get(); - | ^^^ expected `True`, found `False` - | -note: required by a bound in `pyo3::Bound::<'py, T>::get` - --> src/instance.rs - | - | pub fn get(&self) -> &T - | --- required by a bound in this associated function - | where - | T: PyClass + Sync, - | ^^^^^^^^^^^^^ required by this bound in `Bound::<'py, T>::get` diff --git a/vendor/pyo3/tests/ui/invalid_intern_arg.rs b/vendor/pyo3/tests/ui/invalid_intern_arg.rs deleted file mode 100644 index ac82fed97d..0000000000 --- a/vendor/pyo3/tests/ui/invalid_intern_arg.rs +++ /dev/null @@ -1,6 +0,0 @@ -use pyo3::Python; - -fn main() { - let _foo = if true { "foo" } else { "bar" }; - Python::with_gil(|py| py.import(pyo3::intern!(py, _foo)).unwrap()); -} diff --git a/vendor/pyo3/tests/ui/invalid_intern_arg.stderr b/vendor/pyo3/tests/ui/invalid_intern_arg.stderr deleted file mode 100644 index ab4268310a..0000000000 --- a/vendor/pyo3/tests/ui/invalid_intern_arg.stderr +++ /dev/null @@ -1,20 +0,0 @@ -error[E0435]: attempt to use a non-constant value in a constant - --> tests/ui/invalid_intern_arg.rs:5:55 - | -5 | Python::with_gil(|py| py.import(pyo3::intern!(py, _foo)).unwrap()); - | ^^^^ non-constant value - | -help: consider using `let` instead of `static` - --> src/sync.rs - | - | let INTERNED: $crate::sync::Interned = $crate::sync::Interned::new($text); - | ~~~ - -error: lifetime may not live long enough - --> tests/ui/invalid_intern_arg.rs:5:27 - | -5 | Python::with_gil(|py| py.import(pyo3::intern!(py, _foo)).unwrap()); - | --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` - | | | - | | return type of closure is pyo3::Bound<'2, PyModule> - | has type `Python<'1>` diff --git a/vendor/pyo3/tests/ui/invalid_intopy_derive.rs b/vendor/pyo3/tests/ui/invalid_intopy_derive.rs deleted file mode 100644 index c65d44ff1b..0000000000 --- a/vendor/pyo3/tests/ui/invalid_intopy_derive.rs +++ /dev/null @@ -1,109 +0,0 @@ -use pyo3::{IntoPyObject, IntoPyObjectRef}; - -#[derive(IntoPyObject, IntoPyObjectRef)] -struct Foo(); - -#[derive(IntoPyObject, IntoPyObjectRef)] -struct Foo2 {} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EmptyEnum {} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithEmptyTupleVar { - EmptyTuple(), - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithEmptyStructVar { - EmptyStruct {}, - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(transparent)] -struct EmptyTransparentTup(); - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(transparent)] -struct EmptyTransparentStruct {} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithTransparentEmptyTupleVar { - #[pyo3(transparent)] - EmptyTuple(), - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithTransparentEmptyStructVar { - #[pyo3(transparent)] - EmptyStruct {}, - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(transparent)] -struct TransparentTupTooManyFields(String, String); - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(transparent)] -struct TransparentStructTooManyFields { - foo: String, - bar: String, -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithTransparentTupleTooMany { - #[pyo3(transparent)] - EmptyTuple(String, String), - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum EnumWithTransparentStructTooMany { - #[pyo3(transparent)] - EmptyStruct { - foo: String, - bar: String, - }, - Valid(String), -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(unknown = "should not work")] -struct UnknownContainerAttr { - a: String, -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -union Union { - a: usize, -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -enum UnitEnum { - Unit, -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -struct TupleAttribute(#[pyo3(attribute)] String, usize); - -#[derive(IntoPyObject, IntoPyObjectRef)] -struct TupleItem(#[pyo3(item)] String, usize); - -#[derive(IntoPyObject, IntoPyObjectRef)] -struct StructAttribute { - #[pyo3(attribute)] - foo: String, -} - -#[derive(IntoPyObject, IntoPyObjectRef)] -#[pyo3(transparent)] -struct StructTransparentItem { - #[pyo3(item)] - foo: String, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_intopy_derive.stderr b/vendor/pyo3/tests/ui/invalid_intopy_derive.stderr deleted file mode 100644 index cf125d9c07..0000000000 --- a/vendor/pyo3/tests/ui/invalid_intopy_derive.stderr +++ /dev/null @@ -1,127 +0,0 @@ -error: cannot derive `IntoPyObject` for empty structs - --> tests/ui/invalid_intopy_derive.rs:4:11 - | -4 | struct Foo(); - | ^^ - -error: cannot derive `IntoPyObject` for empty structs - --> tests/ui/invalid_intopy_derive.rs:7:13 - | -7 | struct Foo2 {} - | ^^ - -error: cannot derive `IntoPyObject` for empty enum - --> tests/ui/invalid_intopy_derive.rs:10:6 - | -10 | enum EmptyEnum {} - | ^^^^^^^^^ - -error: cannot derive `IntoPyObject` for empty variants - --> tests/ui/invalid_intopy_derive.rs:14:5 - | -14 | EmptyTuple(), - | ^^^^^^^^^^ - -error: cannot derive `IntoPyObject` for empty variants - --> tests/ui/invalid_intopy_derive.rs:20:5 - | -20 | EmptyStruct {}, - | ^^^^^^^^^^^ - -error: cannot derive `IntoPyObject` for empty structs - --> tests/ui/invalid_intopy_derive.rs:26:27 - | -26 | struct EmptyTransparentTup(); - | ^^ - -error: cannot derive `IntoPyObject` for empty structs - --> tests/ui/invalid_intopy_derive.rs:30:31 - | -30 | struct EmptyTransparentStruct {} - | ^^ - -error: cannot derive `IntoPyObject` for empty variants - --> tests/ui/invalid_intopy_derive.rs:35:5 - | -35 | EmptyTuple(), - | ^^^^^^^^^^ - -error: cannot derive `IntoPyObject` for empty variants - --> tests/ui/invalid_intopy_derive.rs:42:5 - | -42 | EmptyStruct {}, - | ^^^^^^^^^^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_intopy_derive.rs:48:35 - | -48 | struct TransparentTupTooManyFields(String, String); - | ^^^^^^^^^^^^^^^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_intopy_derive.rs:52:39 - | -52 | struct TransparentStructTooManyFields { - | _______________________________________^ -53 | | foo: String, -54 | | bar: String, -55 | | } - | |_^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_intopy_derive.rs:60:15 - | -60 | EmptyTuple(String, String), - | ^^^^^^^^^^^^^^^^ - -error: transparent structs and variants can only have 1 field - --> tests/ui/invalid_intopy_derive.rs:67:17 - | -67 | EmptyStruct { - | _________________^ -68 | | foo: String, -69 | | bar: String, -70 | | }, - | |_____^ - -error: expected `transparent` or `crate` - --> tests/ui/invalid_intopy_derive.rs:75:8 - | -75 | #[pyo3(unknown = "should not work")] - | ^^^^^^^ - -error: #[derive(`IntoPyObject`)] is not supported for unions - --> tests/ui/invalid_intopy_derive.rs:81:1 - | -81 | union Union { - | ^^^^^ - -error: cannot derive `IntoPyObject` for empty variants - --> tests/ui/invalid_intopy_derive.rs:87:5 - | -87 | Unit, - | ^^^^ - -error: `attribute` is not supported by `IntoPyObject` - --> tests/ui/invalid_intopy_derive.rs:91:30 - | -91 | struct TupleAttribute(#[pyo3(attribute)] String, usize); - | ^^^^^^^^^ - -error: `item` is not permitted on tuple struct elements. - --> tests/ui/invalid_intopy_derive.rs:94:25 - | -94 | struct TupleItem(#[pyo3(item)] String, usize); - | ^^^^ - -error: `attribute` is not supported by `IntoPyObject` - --> tests/ui/invalid_intopy_derive.rs:98:12 - | -98 | #[pyo3(attribute)] - | ^^^^^^^^^ - -error: `transparent` structs may not have `item` for the inner field - --> tests/ui/invalid_intopy_derive.rs:105:12 - | -105 | #[pyo3(item)] - | ^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_property_args.rs b/vendor/pyo3/tests/ui/invalid_property_args.rs deleted file mode 100644 index f35367df7a..0000000000 --- a/vendor/pyo3/tests/ui/invalid_property_args.rs +++ /dev/null @@ -1,48 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct ClassWithGetter {} - -#[pymethods] -impl ClassWithGetter { - #[getter] - fn getter_with_arg(&self, _py: Python<'_>, _index: u32) {} -} - -#[pyclass] -struct ClassWithSetter {} - -#[pymethods] -impl ClassWithSetter { - #[setter] - fn setter_with_no_arg(&mut self, _py: Python<'_>) {} -} - -#[pymethods] -impl ClassWithSetter { - #[setter] - fn setter_with_too_many_args(&mut self, _py: Python<'_>, _foo: u32, _bar: u32) {} -} - -#[pyclass] -struct TupleGetterSetterNoName(#[pyo3(get, set)] i32); - -#[pyclass] -struct MultipleGet(#[pyo3(get, get)] i32); - -#[pyclass] -struct MultipleSet(#[pyo3(set, set)] i32); - -#[pyclass] -struct MultipleName(#[pyo3(name = "foo", name = "bar")] i32); - -#[pyclass] -struct NameWithoutGetSet(#[pyo3(name = "value")] i32); - -#[pyclass] -struct InvalidGetterType { - #[pyo3(get)] - value: ::std::marker::PhantomData, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_property_args.stderr b/vendor/pyo3/tests/ui/invalid_property_args.stderr deleted file mode 100644 index 5ef01fa210..0000000000 --- a/vendor/pyo3/tests/ui/invalid_property_args.stderr +++ /dev/null @@ -1,75 +0,0 @@ -error: getter function can only have one argument (of type pyo3::Python) - --> tests/ui/invalid_property_args.rs:9:56 - | -9 | fn getter_with_arg(&self, _py: Python<'_>, _index: u32) {} - | ^^^ - -error: setter function expected to have one argument - --> tests/ui/invalid_property_args.rs:18:8 - | -18 | fn setter_with_no_arg(&mut self, _py: Python<'_>) {} - | ^^^^^^^^^^^^^^^^^^ - -error: setter function can have at most two arguments ([pyo3::Python,] and value) - --> tests/ui/invalid_property_args.rs:24:79 - | -24 | fn setter_with_too_many_args(&mut self, _py: Python<'_>, _foo: u32, _bar: u32) {} - | ^^^ - -error: `get` and `set` with tuple struct fields require `name` - --> tests/ui/invalid_property_args.rs:28:50 - | -28 | struct TupleGetterSetterNoName(#[pyo3(get, set)] i32); - | ^^^ - -error: `get` may only be specified once - --> tests/ui/invalid_property_args.rs:31:32 - | -31 | struct MultipleGet(#[pyo3(get, get)] i32); - | ^^^ - -error: `set` may only be specified once - --> tests/ui/invalid_property_args.rs:34:32 - | -34 | struct MultipleSet(#[pyo3(set, set)] i32); - | ^^^ - -error: `name` may only be specified once - --> tests/ui/invalid_property_args.rs:37:42 - | -37 | struct MultipleName(#[pyo3(name = "foo", name = "bar")] i32); - | ^^^^ - -error: `name` is useless without `get` or `set` - --> tests/ui/invalid_property_args.rs:40:33 - | -40 | struct NameWithoutGetSet(#[pyo3(name = "value")] i32); - | ^^^^^^^^^^^^^^ - -error[E0277]: `PhantomData` cannot be converted to a Python object - --> tests/ui/invalid_property_args.rs:45:12 - | -45 | value: ::std::marker::PhantomData, - | ^ required by `#[pyo3(get)]` to create a readable property from a field of type `PhantomData` - | - = help: the trait `IntoPyObject<'_>` is not implemented for `PhantomData` - = note: implement `IntoPyObject` for `&PhantomData` or `IntoPyObject + Clone` for `PhantomData` to define the conversion - = help: the following other types implement trait `IntoPyObject<'py>`: - &&'a T - &&OsStr - &&Path - &&str - &'a (T0, T1) - &'a (T0, T1, T2) - &'a (T0, T1, T2, T3) - &'a (T0, T1, T2, T3, T4) - and $N others - = note: required for `PhantomData` to implement `for<'py> PyO3GetField<'py>` -note: required by a bound in `PyClassGetterGenerator::::generate` - --> src/impl_/pyclass.rs - | - | pub const fn generate(&self, _name: &'static CStr, _doc: &'static CStr) -> PyMethodDefType - | -------- required by a bound in this associated function -... - | for<'py> FieldT: PyO3GetField<'py>, - | ^^^^^^^^^^^^^^^^^ required by this bound in `PyClassGetterGenerator::::generate` diff --git a/vendor/pyo3/tests/ui/invalid_proto_pymethods.rs b/vendor/pyo3/tests/ui/invalid_proto_pymethods.rs deleted file mode 100644 index c40790c316..0000000000 --- a/vendor/pyo3/tests/ui/invalid_proto_pymethods.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! Check that some magic methods edge cases error as expected. -//! -//! For convenience use #[pyo3(name = "__some_dunder__")] to create the methods, -//! so that the function names can describe the edge case to be rejected. - -use pyo3::prelude::*; -use pyo3::pyclass::CompareOp; - -#[pyclass] -struct MyClass {} - -// -// Argument counts -// - -#[pymethods] -impl MyClass { - #[pyo3(name = "__truediv__")] - fn truediv_expects_one_argument(&self) -> PyResult<()> { - Ok(()) - } -} - -#[pymethods] -impl MyClass { - #[pyo3(name = "__truediv__")] - fn truediv_expects_one_argument_py(&self, _py: Python<'_>) -> PyResult<()> { - Ok(()) - } -} - -// -// Forbidden attributes -// - -#[pymethods] -impl MyClass { - #[pyo3(name = "__bool__", signature = ())] - fn signature_is_forbidden(&self) -> bool { - true - } -} - -#[pymethods] -impl MyClass { - #[pyo3(name = "__bool__", text_signature = "")] - fn text_signature_is_forbidden(&self) -> bool { - true - } -} - -#[pyclass] -struct EqAndRichcmp; - -#[pymethods] -impl EqAndRichcmp { - fn __eq__(&self, _other: &Self) -> bool { - true - } - - fn __richcmp__(&self, _other: &Self, _op: CompareOp) -> bool { - true - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_proto_pymethods.stderr b/vendor/pyo3/tests/ui/invalid_proto_pymethods.stderr deleted file mode 100644 index 18c9611329..0000000000 --- a/vendor/pyo3/tests/ui/invalid_proto_pymethods.stderr +++ /dev/null @@ -1,81 +0,0 @@ -error: Expected 1 arguments, got 0 - --> tests/ui/invalid_proto_pymethods.rs:19:8 - | -19 | fn truediv_expects_one_argument(&self) -> PyResult<()> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: Expected 1 arguments, got 0 - --> tests/ui/invalid_proto_pymethods.rs:27:8 - | -27 | fn truediv_expects_one_argument_py(&self, _py: Python<'_>) -> PyResult<()> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: `signature` cannot be used with magic method `__bool__` - --> tests/ui/invalid_proto_pymethods.rs:38:31 - | -38 | #[pyo3(name = "__bool__", signature = ())] - | ^^^^^^^^^ - -error: `text_signature` cannot be used with magic method `__bool__` - --> tests/ui/invalid_proto_pymethods.rs:46:31 - | -46 | #[pyo3(name = "__bool__", text_signature = "")] - | ^^^^^^^^^^^^^^ - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ multiple `__pymethod___richcmp____` found - | -note: candidate #1 is defined in an impl for the type `EqAndRichcmp` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `EqAndRichcmp` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the macro `::pyo3::impl_::pyclass::generate_pyclass_richcompare_slot` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0119]: conflicting implementations of trait `HasCustomRichCmp` for type `EqAndRichcmp` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ - | | - | first implementation here - | conflicting implementation for `EqAndRichcmp` - | - = note: this error originates in the macro `::pyo3::impl_::pyclass::generate_pyclass_richcompare_slot` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod___richcmp____` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ - | | - | duplicate definitions for `__pymethod___richcmp____` - | other definition for `__pymethod___richcmp____` - | - = note: this error originates in the macro `::pyo3::impl_::pyclass::generate_pyclass_richcompare_slot` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ multiple `__pymethod___richcmp____` found - | -note: candidate #1 is defined in an impl for the type `EqAndRichcmp` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `EqAndRichcmp` - --> tests/ui/invalid_proto_pymethods.rs:55:1 - | -55 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_args.rs b/vendor/pyo3/tests/ui/invalid_pyclass_args.rs deleted file mode 100644 index 221fc6ca35..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_args.rs +++ /dev/null @@ -1,184 +0,0 @@ -use pyo3::prelude::*; -use std::fmt::{Display, Formatter}; - -#[pyclass(extend=pyo3::types::PyDict)] -struct TypoIntheKey {} - -#[pyclass(extends = "PyDict")] -struct InvalidExtends {} - -#[pyclass(name = m::MyClass)] -struct InvalidName {} - -#[pyclass(name = "Custom Name")] -struct InvalidName2 {} - -#[pyclass(name = CustomName)] -struct DeprecatedName {} - -#[pyclass(rename_all = camelCase)] -struct InvalidRenamingRule {} - -#[pyclass(rename_all = "Camel-Case")] -struct InvalidRenamingRule2 {} - -#[pyclass(module = my_module)] -struct InvalidModule {} - -#[pyclass(weakrev)] -struct InvalidArg {} - -#[pyclass(mapping, sequence)] -struct CannotBeMappingAndSequence {} - -#[pyclass(eq)] -struct EqOptRequiresEq {} - -#[pyclass(eq)] -#[derive(PartialEq)] -struct EqOptAndManualRichCmp {} - -#[pymethods] -impl EqOptAndManualRichCmp { - fn __richcmp__( - &self, - _py: Python, - _other: Bound<'_, PyAny>, - _op: pyo3::pyclass::CompareOp, - ) -> PyResult { - todo!() - } -} - -#[pyclass(eq_int)] -struct NoEqInt {} - -#[pyclass(frozen, eq, hash)] -#[derive(PartialEq)] -struct HashOptRequiresHash; - -#[pyclass(hash)] -#[derive(Hash)] -struct HashWithoutFrozenAndEq; - -#[pyclass(frozen, eq, hash)] -#[derive(PartialEq, Hash)] -struct HashOptAndManualHash {} - -#[pymethods] -impl HashOptAndManualHash { - fn __hash__(&self) -> u64 { - todo!() - } -} - -#[pyclass(ord)] -struct InvalidOrderedStruct { - inner: i32, -} - -#[pyclass] -struct MultipleErrors { - #[pyo3(foo)] - #[pyo3(blah)] - x: i32, - #[pyo3(pop)] - y: i32, -} - -#[pyclass(str)] -struct StrOptAndManualStr {} - -impl Display for StrOptAndManualStr { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self) - } -} - -#[pymethods] -impl StrOptAndManualStr { - fn __str__(&self) -> String { - todo!() - } -} - -#[pyclass(str = "{")] -#[derive(PartialEq)] -struct Coord(u32, u32, u32); - -#[pyclass(str = "{$}")] -#[derive(PartialEq)] -struct Coord2(u32, u32, u32); - -#[pyclass(str = "X: {aaaa}, Y: {y}, Z: {z}")] -#[derive(PartialEq, Eq, Clone, PartialOrd)] -pub struct Point { - x: i32, - y: i32, - z: i32, -} - -#[pyclass(str = "X: {x}, Y: {y}}}, Z: {zzz}")] -#[derive(PartialEq, Eq, Clone, PartialOrd)] -pub struct Point2 { - x: i32, - y: i32, - z: i32, -} - -#[pyclass(str = "{0}, {162543}, {2}")] -#[derive(PartialEq)] -struct Coord3(u32, u32, u32); - -#[pyclass(name = "aaa", str = "unsafe: {unsafe_variable}")] -struct StructRenamingWithStrFormatter { - #[pyo3(name = "unsafe", get, set)] - unsafe_variable: usize, -} - -#[pyclass(name = "aaa", str = "unsafe: {unsafe_variable}")] -struct StructRenamingWithStrFormatter2 { - unsafe_variable: usize, -} - -#[pyclass(str = "unsafe: {unsafe_variable}")] -struct StructRenamingWithStrFormatter3 { - #[pyo3(name = "unsafe", get, set)] - unsafe_variable: usize, -} - -#[pyclass(rename_all = "SCREAMING_SNAKE_CASE", str = "{a_a}, {b_b}, {c_d_e}")] -struct RenameAllVariantsStruct { - a_a: u32, - b_b: u32, - c_d_e: String, -} - -#[pyclass(str = "{:?}")] -#[derive(Debug)] -struct StructWithNoMember { - a: String, - b: String, -} - -#[pyclass(str = "{}")] -#[derive(Debug)] -struct StructWithNoMember2 { - a: String, - b: String, -} - -#[pyclass(eq, str = "Stuff...")] -#[derive(Debug, PartialEq)] -pub enum MyEnumInvalidStrFmt { - Variant, - OtherVariant, -} - -impl Display for MyEnumInvalidStrFmt { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_args.stderr b/vendor/pyo3/tests/ui/invalid_pyclass_args.stderr deleted file mode 100644 index e4d415ce17..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_args.stderr +++ /dev/null @@ -1,385 +0,0 @@ -error: expected one of: `crate`, `dict`, `eq`, `eq_int`, `extends`, `freelist`, `frozen`, `get_all`, `hash`, `mapping`, `module`, `name`, `ord`, `rename_all`, `sequence`, `set_all`, `str`, `subclass`, `unsendable`, `weakref` - --> tests/ui/invalid_pyclass_args.rs:4:11 - | -4 | #[pyclass(extend=pyo3::types::PyDict)] - | ^^^^^^ - -error: expected identifier - --> tests/ui/invalid_pyclass_args.rs:7:21 - | -7 | #[pyclass(extends = "PyDict")] - | ^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_pyclass_args.rs:10:18 - | -10 | #[pyclass(name = m::MyClass)] - | ^ - -error: expected a single identifier in double quotes - --> tests/ui/invalid_pyclass_args.rs:13:18 - | -13 | #[pyclass(name = "Custom Name")] - | ^^^^^^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_pyclass_args.rs:16:18 - | -16 | #[pyclass(name = CustomName)] - | ^^^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_pyclass_args.rs:19:24 - | -19 | #[pyclass(rename_all = camelCase)] - | ^^^^^^^^^ - -error: expected a valid renaming rule, possible values are: "camelCase", "kebab-case", "lowercase", "PascalCase", "SCREAMING-KEBAB-CASE", "SCREAMING_SNAKE_CASE", "snake_case", "UPPERCASE" - --> tests/ui/invalid_pyclass_args.rs:22:24 - | -22 | #[pyclass(rename_all = "Camel-Case")] - | ^^^^^^^^^^^^ - -error: expected string literal - --> tests/ui/invalid_pyclass_args.rs:25:20 - | -25 | #[pyclass(module = my_module)] - | ^^^^^^^^^ - -error: expected one of: `crate`, `dict`, `eq`, `eq_int`, `extends`, `freelist`, `frozen`, `get_all`, `hash`, `mapping`, `module`, `name`, `ord`, `rename_all`, `sequence`, `set_all`, `str`, `subclass`, `unsendable`, `weakref` - --> tests/ui/invalid_pyclass_args.rs:28:11 - | -28 | #[pyclass(weakrev)] - | ^^^^^^^ - -error: a `#[pyclass]` cannot be both a `mapping` and a `sequence` - --> tests/ui/invalid_pyclass_args.rs:32:8 - | -32 | struct CannotBeMappingAndSequence {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: `eq_int` can only be used on simple enums. - --> tests/ui/invalid_pyclass_args.rs:53:11 - | -53 | #[pyclass(eq_int)] - | ^^^^^^ - -error: The `hash` option requires the `frozen` option. - --> tests/ui/invalid_pyclass_args.rs:60:11 - | -60 | #[pyclass(hash)] - | ^^^^ - -error: The `hash` option requires the `eq` option. - --> tests/ui/invalid_pyclass_args.rs:60:11 - | -60 | #[pyclass(hash)] - | ^^^^ - -error: The `ord` option requires the `eq` option. - --> tests/ui/invalid_pyclass_args.rs:75:11 - | -75 | #[pyclass(ord)] - | ^^^ - -error: expected one of: `get`, `set`, `name` - --> tests/ui/invalid_pyclass_args.rs:82:12 - | -82 | #[pyo3(foo)] - | ^^^ - -error: expected one of: `get`, `set`, `name` - --> tests/ui/invalid_pyclass_args.rs:83:12 - | -83 | #[pyo3(blah)] - | ^^^^ - -error: expected one of: `get`, `set`, `name` - --> tests/ui/invalid_pyclass_args.rs:85:12 - | -85 | #[pyo3(pop)] - | ^^^ - -error: invalid format string: expected `}` but string was terminated - --> tests/ui/invalid_pyclass_args.rs:105:19 - | -105 | #[pyclass(str = "{")] - | -^ expected `}` in format string - | | - | because of this opening brace - | - = note: if you intended to print `{`, you can escape it using `{{` - -error: invalid format string: expected `}`, found `$` - --> tests/ui/invalid_pyclass_args.rs:109:19 - | -109 | #[pyclass(str = "{$}")] - | -^ expected `}` in format string - | | - | because of this opening brace - | - = note: if you intended to print `{`, you can escape it using `{{` - -error: The format string syntax is incompatible with any renaming via `name` or `rename_all` - --> tests/ui/invalid_pyclass_args.rs:133:31 - | -133 | #[pyclass(name = "aaa", str = "unsafe: {unsafe_variable}")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: The format string syntax is incompatible with any renaming via `name` or `rename_all` - --> tests/ui/invalid_pyclass_args.rs:139:31 - | -139 | #[pyclass(name = "aaa", str = "unsafe: {unsafe_variable}")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: The format string syntax is incompatible with any renaming via `name` or `rename_all` - --> tests/ui/invalid_pyclass_args.rs:144:17 - | -144 | #[pyclass(str = "unsafe: {unsafe_variable}")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: The format string syntax is incompatible with any renaming via `name` or `rename_all` - --> tests/ui/invalid_pyclass_args.rs:150:54 - | -150 | #[pyclass(rename_all = "SCREAMING_SNAKE_CASE", str = "{a_a}, {b_b}, {c_d_e}")] - | ^^^^^^^^^^^^^^^^^^^^^^^ - -error: No member found, you must provide a named or positionally specified member. - --> tests/ui/invalid_pyclass_args.rs:157:17 - | -157 | #[pyclass(str = "{:?}")] - | ^^^^^^ - -error: No member found, you must provide a named or positionally specified member. - --> tests/ui/invalid_pyclass_args.rs:164:17 - | -164 | #[pyclass(str = "{}")] - | ^^^^ - -error: The format string syntax cannot be used with enums - --> tests/ui/invalid_pyclass_args.rs:171:21 - | -171 | #[pyclass(eq, str = "Stuff...")] - | ^^^^^^^^^^ - -error[E0119]: conflicting implementations of trait `HasCustomRichCmp` for type `EqOptAndManualRichCmp` - --> tests/ui/invalid_pyclass_args.rs:41:1 - | -37 | #[pyclass(eq)] - | -------------- first implementation here -... -41 | #[pymethods] - | ^^^^^^^^^^^^ conflicting implementation for `EqOptAndManualRichCmp` - | - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod___richcmp____` - --> tests/ui/invalid_pyclass_args.rs:37:1 - | -37 | #[pyclass(eq)] - | ^^^^^^^^^^^^^^ duplicate definitions for `__pymethod___richcmp____` -... -41 | #[pymethods] - | ------------ other definition for `__pymethod___richcmp____` - | - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod___hash____` - --> tests/ui/invalid_pyclass_args.rs:64:1 - | -64 | #[pyclass(frozen, eq, hash)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ duplicate definitions for `__pymethod___hash____` -... -68 | #[pymethods] - | ------------ other definition for `__pymethod___hash____` - | - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod___str____` - --> tests/ui/invalid_pyclass_args.rs:89:1 - | -89 | #[pyclass(str)] - | ^^^^^^^^^^^^^^^ duplicate definitions for `__pymethod___str____` -... -98 | #[pymethods] - | ------------ other definition for `__pymethod___str____` - | - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0369]: binary operation `==` cannot be applied to type `&EqOptRequiresEq` - --> tests/ui/invalid_pyclass_args.rs:34:11 - | -34 | #[pyclass(eq)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `EqOptRequiresEq` - --> tests/ui/invalid_pyclass_args.rs:35:1 - | -35 | struct EqOptRequiresEq {} - | ^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `EqOptRequiresEq` with `#[derive(PartialEq)]` - | -35 + #[derive(PartialEq)] -36 | struct EqOptRequiresEq {} - | - -error[E0369]: binary operation `!=` cannot be applied to type `&EqOptRequiresEq` - --> tests/ui/invalid_pyclass_args.rs:34:11 - | -34 | #[pyclass(eq)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `EqOptRequiresEq` - --> tests/ui/invalid_pyclass_args.rs:35:1 - | -35 | struct EqOptRequiresEq {} - | ^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `EqOptRequiresEq` with `#[derive(PartialEq)]` - | -35 + #[derive(PartialEq)] -36 | struct EqOptRequiresEq {} - | - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:37:1 - | -37 | #[pyclass(eq)] - | ^^^^^^^^^^^^^^ multiple `__pymethod___richcmp____` found - | -note: candidate #1 is defined in an impl for the type `EqOptAndManualRichCmp` - --> tests/ui/invalid_pyclass_args.rs:37:1 - | -37 | #[pyclass(eq)] - | ^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `EqOptAndManualRichCmp` - --> tests/ui/invalid_pyclass_args.rs:41:1 - | -41 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pyclass` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:41:1 - | -41 | #[pymethods] - | ^^^^^^^^^^^^ multiple `__pymethod___richcmp____` found - | -note: candidate #1 is defined in an impl for the type `EqOptAndManualRichCmp` - --> tests/ui/invalid_pyclass_args.rs:37:1 - | -37 | #[pyclass(eq)] - | ^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `EqOptAndManualRichCmp` - --> tests/ui/invalid_pyclass_args.rs:41:1 - | -41 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0277]: the trait bound `HashOptRequiresHash: Hash` is not satisfied - --> tests/ui/invalid_pyclass_args.rs:56:23 - | -56 | #[pyclass(frozen, eq, hash)] - | ^^^^ the trait `Hash` is not implemented for `HashOptRequiresHash` - | -help: consider annotating `HashOptRequiresHash` with `#[derive(Hash)]` - | -58 + #[derive(Hash)] -59 | struct HashOptRequiresHash; - | - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:64:1 - | -64 | #[pyclass(frozen, eq, hash)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ multiple `__pymethod___hash____` found - | -note: candidate #1 is defined in an impl for the type `HashOptAndManualHash` - --> tests/ui/invalid_pyclass_args.rs:64:1 - | -64 | #[pyclass(frozen, eq, hash)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `HashOptAndManualHash` - --> tests/ui/invalid_pyclass_args.rs:68:1 - | -68 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pyclass` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:68:1 - | -68 | #[pymethods] - | ^^^^^^^^^^^^ multiple `__pymethod___hash____` found - | -note: candidate #1 is defined in an impl for the type `HashOptAndManualHash` - --> tests/ui/invalid_pyclass_args.rs:64:1 - | -64 | #[pyclass(frozen, eq, hash)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `HashOptAndManualHash` - --> tests/ui/invalid_pyclass_args.rs:68:1 - | -68 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:89:1 - | -89 | #[pyclass(str)] - | ^^^^^^^^^^^^^^^ multiple `__pymethod___str____` found - | -note: candidate #1 is defined in an impl for the type `StrOptAndManualStr` - --> tests/ui/invalid_pyclass_args.rs:89:1 - | -89 | #[pyclass(str)] - | ^^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `StrOptAndManualStr` - --> tests/ui/invalid_pyclass_args.rs:98:1 - | -98 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pyclass` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0034]: multiple applicable items in scope - --> tests/ui/invalid_pyclass_args.rs:98:1 - | -98 | #[pymethods] - | ^^^^^^^^^^^^ multiple `__pymethod___str____` found - | -note: candidate #1 is defined in an impl for the type `StrOptAndManualStr` - --> tests/ui/invalid_pyclass_args.rs:89:1 - | -89 | #[pyclass(str)] - | ^^^^^^^^^^^^^^^ -note: candidate #2 is defined in an impl for the type `StrOptAndManualStr` - --> tests/ui/invalid_pyclass_args.rs:98:1 - | -98 | #[pymethods] - | ^^^^^^^^^^^^ - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0609]: no field `aaaa` on type `&Point` - --> tests/ui/invalid_pyclass_args.rs:113:17 - | -113 | #[pyclass(str = "X: {aaaa}, Y: {y}, Z: {z}")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unknown field - | - = note: available fields are: `x`, `y`, `z` - -error[E0609]: no field `zzz` on type `&Point2` - --> tests/ui/invalid_pyclass_args.rs:121:17 - | -121 | #[pyclass(str = "X: {x}, Y: {y}}}, Z: {zzz}")] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unknown field - | - = note: available fields are: `x`, `y`, `z` - -error[E0609]: no field `162543` on type `&Coord3` - --> tests/ui/invalid_pyclass_args.rs:129:17 - | -129 | #[pyclass(str = "{0}, {162543}, {2}")] - | ^^^^^^^^^^^^^^^^^^^^ unknown field - | - = note: available fields are: `0`, `1`, `2` diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_enum.rs b/vendor/pyo3/tests/ui/invalid_pyclass_enum.rs deleted file mode 100644 index 102659e822..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_enum.rs +++ /dev/null @@ -1,105 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass(subclass)] -enum NotBaseClass { - X, - Y, -} - -#[pyclass(extends = PyList)] -enum NotDrivedClass { - X, - Y, -} - -#[pyclass] -enum NoEmptyEnum {} - -#[pyclass] -enum NoUnitVariants { - StructVariant { field: i32 }, - UnitVariant, -} - -#[pyclass] -enum SimpleNoSignature { - #[pyo3(constructor = (a, b))] - A, - B, -} - -#[pyclass(eq, eq_int)] -enum SimpleEqOptRequiresPartialEq { - A, - B, -} - -#[pyclass(eq)] -enum ComplexEqOptRequiresPartialEq { - A(i32), - B { msg: String }, -} - -#[pyclass(eq_int)] -enum SimpleEqIntWithoutEq { - A, - B, -} - -#[pyclass(eq_int)] -enum NoEqInt { - A(i32), - B { msg: String }, -} - -#[pyclass(frozen, eq, eq_int, hash)] -#[derive(PartialEq)] -enum SimpleHashOptRequiresHash { - A, - B, -} - -#[pyclass(frozen, eq, hash)] -#[derive(PartialEq)] -enum ComplexHashOptRequiresHash { - A(i32), - B { msg: String }, -} - -#[pyclass(hash)] -#[derive(Hash)] -enum SimpleHashOptRequiresFrozenAndEq { - A, - B, -} - -#[pyclass(hash)] -#[derive(Hash)] -enum ComplexHashOptRequiresEq { - A(i32), - B { msg: String }, -} - -#[pyclass(ord)] -enum InvalidOrderedComplexEnum { - VariantA (i32), - VariantB { msg: String } -} - -#[pyclass(eq,ord)] -#[derive(PartialEq)] -enum InvalidOrderedComplexEnum2 { - VariantA (i32), - VariantB { msg: String } -} - -#[pyclass(eq)] -#[derive(PartialEq)] -enum AllEnumVariantsDisabled { - #[cfg(any())] - DisabledA, - #[cfg(not(all()))] - DisabledB, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_enum.stderr b/vendor/pyo3/tests/ui/invalid_pyclass_enum.stderr deleted file mode 100644 index 80dc953974..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_enum.stderr +++ /dev/null @@ -1,233 +0,0 @@ -error: enums can't be inherited by other classes - --> tests/ui/invalid_pyclass_enum.rs:3:11 - | -3 | #[pyclass(subclass)] - | ^^^^^^^^ - -error: enums can't extend from other classes - --> tests/ui/invalid_pyclass_enum.rs:9:11 - | -9 | #[pyclass(extends = PyList)] - | ^^^^^^^ - -error: #[pyclass] can't be used on enums without any variants - --> tests/ui/invalid_pyclass_enum.rs:16:18 - | -16 | enum NoEmptyEnum {} - | ^^ - -error: Unit variant `UnitVariant` is not yet supported in a complex enum - = help: change to an empty tuple variant instead: `UnitVariant()` - = note: the enum is complex because of non-unit variant `StructVariant` - --> tests/ui/invalid_pyclass_enum.rs:21:5 - | -21 | UnitVariant, - | ^^^^^^^^^^^ - -error: `constructor` can't be used on a simple enum variant - --> tests/ui/invalid_pyclass_enum.rs:26:12 - | -26 | #[pyo3(constructor = (a, b))] - | ^^^^^^^^^^^ - -error: The `eq_int` option requires the `eq` option. - --> tests/ui/invalid_pyclass_enum.rs:43:11 - | -43 | #[pyclass(eq_int)] - | ^^^^^^ - -error: `eq_int` can only be used on simple enums. - --> tests/ui/invalid_pyclass_enum.rs:49:11 - | -49 | #[pyclass(eq_int)] - | ^^^^^^ - -error: The `hash` option requires the `frozen` option. - --> tests/ui/invalid_pyclass_enum.rs:69:11 - | -69 | #[pyclass(hash)] - | ^^^^ - -error: The `hash` option requires the `eq` option. - --> tests/ui/invalid_pyclass_enum.rs:69:11 - | -69 | #[pyclass(hash)] - | ^^^^ - -error: The `hash` option requires the `eq` option. - --> tests/ui/invalid_pyclass_enum.rs:76:11 - | -76 | #[pyclass(hash)] - | ^^^^ - -error: The `ord` option requires the `eq` option. - --> tests/ui/invalid_pyclass_enum.rs:83:11 - | -83 | #[pyclass(ord)] - | ^^^ - -error: #[pyclass] can't be used on enums without any variants - all variants of enum `AllEnumVariantsDisabled` have been configured out by cfg attributes - --> tests/ui/invalid_pyclass_enum.rs:98:6 - | -98 | enum AllEnumVariantsDisabled { - | ^^^^^^^^^^^^^^^^^^^^^^^ - -error[E0369]: binary operation `==` cannot be applied to type `&SimpleEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:31:11 - | -31 | #[pyclass(eq, eq_int)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `SimpleEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:32:1 - | -32 | enum SimpleEqOptRequiresPartialEq { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `SimpleEqOptRequiresPartialEq` with `#[derive(PartialEq)]` - | -32 + #[derive(PartialEq)] -33 | enum SimpleEqOptRequiresPartialEq { - | - -error[E0369]: binary operation `!=` cannot be applied to type `&SimpleEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:31:11 - | -31 | #[pyclass(eq, eq_int)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `SimpleEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:32:1 - | -32 | enum SimpleEqOptRequiresPartialEq { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `SimpleEqOptRequiresPartialEq` with `#[derive(PartialEq)]` - | -32 + #[derive(PartialEq)] -33 | enum SimpleEqOptRequiresPartialEq { - | - -error[E0369]: binary operation `==` cannot be applied to type `&ComplexEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:37:11 - | -37 | #[pyclass(eq)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `ComplexEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:38:1 - | -38 | enum ComplexEqOptRequiresPartialEq { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `ComplexEqOptRequiresPartialEq` with `#[derive(PartialEq)]` - | -38 + #[derive(PartialEq)] -39 | enum ComplexEqOptRequiresPartialEq { - | - -error[E0369]: binary operation `!=` cannot be applied to type `&ComplexEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:37:11 - | -37 | #[pyclass(eq)] - | ^^ - | -note: an implementation of `PartialEq` might be missing for `ComplexEqOptRequiresPartialEq` - --> tests/ui/invalid_pyclass_enum.rs:38:1 - | -38 | enum ComplexEqOptRequiresPartialEq { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialEq` -help: consider annotating `ComplexEqOptRequiresPartialEq` with `#[derive(PartialEq)]` - | -38 + #[derive(PartialEq)] -39 | enum ComplexEqOptRequiresPartialEq { - | - -error[E0277]: the trait bound `SimpleHashOptRequiresHash: Hash` is not satisfied - --> tests/ui/invalid_pyclass_enum.rs:55:31 - | -55 | #[pyclass(frozen, eq, eq_int, hash)] - | ^^^^ the trait `Hash` is not implemented for `SimpleHashOptRequiresHash` - | -help: consider annotating `SimpleHashOptRequiresHash` with `#[derive(Hash)]` - | -57 + #[derive(Hash)] -58 | enum SimpleHashOptRequiresHash { - | - -error[E0277]: the trait bound `ComplexHashOptRequiresHash: Hash` is not satisfied - --> tests/ui/invalid_pyclass_enum.rs:62:23 - | -62 | #[pyclass(frozen, eq, hash)] - | ^^^^ the trait `Hash` is not implemented for `ComplexHashOptRequiresHash` - | -help: consider annotating `ComplexHashOptRequiresHash` with `#[derive(Hash)]` - | -64 + #[derive(Hash)] -65 | enum ComplexHashOptRequiresHash { - | - -error[E0369]: binary operation `>` cannot be applied to type `&InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:89:14 - | -89 | #[pyclass(eq,ord)] - | ^^^ - | -note: an implementation of `PartialOrd` might be missing for `InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:91:1 - | -91 | enum InvalidOrderedComplexEnum2 { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialOrd` -help: consider annotating `InvalidOrderedComplexEnum2` with `#[derive(PartialEq, PartialOrd)]` - | -91 + #[derive(PartialEq, PartialOrd)] -92 | enum InvalidOrderedComplexEnum2 { - | - -error[E0369]: binary operation `<` cannot be applied to type `&InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:89:14 - | -89 | #[pyclass(eq,ord)] - | ^^^ - | -note: an implementation of `PartialOrd` might be missing for `InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:91:1 - | -91 | enum InvalidOrderedComplexEnum2 { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialOrd` -help: consider annotating `InvalidOrderedComplexEnum2` with `#[derive(PartialEq, PartialOrd)]` - | -91 + #[derive(PartialEq, PartialOrd)] -92 | enum InvalidOrderedComplexEnum2 { - | - -error[E0369]: binary operation `<=` cannot be applied to type `&InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:89:14 - | -89 | #[pyclass(eq,ord)] - | ^^^ - | -note: an implementation of `PartialOrd` might be missing for `InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:91:1 - | -91 | enum InvalidOrderedComplexEnum2 { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialOrd` -help: consider annotating `InvalidOrderedComplexEnum2` with `#[derive(PartialEq, PartialOrd)]` - | -91 + #[derive(PartialEq, PartialOrd)] -92 | enum InvalidOrderedComplexEnum2 { - | - -error[E0369]: binary operation `>=` cannot be applied to type `&InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:89:14 - | -89 | #[pyclass(eq,ord)] - | ^^^ - | -note: an implementation of `PartialOrd` might be missing for `InvalidOrderedComplexEnum2` - --> tests/ui/invalid_pyclass_enum.rs:91:1 - | -91 | enum InvalidOrderedComplexEnum2 { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ must implement `PartialOrd` -help: consider annotating `InvalidOrderedComplexEnum2` with `#[derive(PartialEq, PartialOrd)]` - | -91 + #[derive(PartialEq, PartialOrd)] -92 | enum InvalidOrderedComplexEnum2 { - | diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_item.rs b/vendor/pyo3/tests/ui/invalid_pyclass_item.rs deleted file mode 100644 index 4b34854060..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_item.rs +++ /dev/null @@ -1,6 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -fn foo() {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyclass_item.stderr b/vendor/pyo3/tests/ui/invalid_pyclass_item.stderr deleted file mode 100644 index f29756dfae..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyclass_item.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: #[pyclass] only supports structs and enums. - --> tests/ui/invalid_pyclass_item.rs:4:1 - | -4 | fn foo() {} - | ^^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pyfunction_definition.rs b/vendor/pyo3/tests/ui/invalid_pyfunction_definition.rs deleted file mode 100644 index 2f08ff421b..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunction_definition.rs +++ /dev/null @@ -1,15 +0,0 @@ -#[pyo3::pymodule] -mod pyo3_scratch { - use pyo3::prelude::*; - - #[pyclass] - struct Foo {} - - #[pymethods] - impl Foo { - #[pyfunction] - fn bug() {} - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyfunction_definition.stderr b/vendor/pyo3/tests/ui/invalid_pyfunction_definition.stderr deleted file mode 100644 index 9c7cac1f0f..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunction_definition.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: functions inside #[pymethods] do not need to be annotated with #[pyfunction] - --> tests/ui/invalid_pyfunction_definition.rs:11:9 - | -11 | fn bug() {} - | ^^ diff --git a/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.rs b/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.rs deleted file mode 100644 index 86033aa12e..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.rs +++ /dev/null @@ -1,65 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyTuple}; - -#[pyfunction] -#[pyo3(signature = ())] -fn function_with_one_argument_empty_signature(_x: i32) {} - -#[pyfunction] -#[pyo3(signature = (x))] -fn function_with_one_entry_signature_no_args() {} - -#[pyfunction] -#[pyo3(signature = (x))] -fn function_with_incorrect_argument_names(y: i32) { - let _ = y; -} - -#[pyfunction(x)] -#[pyo3(signature = (x))] -fn function_with_both_args_and_signature(x: i32) { - let _ = x; -} - -#[pyfunction] -#[pyo3(signature = (*, *args))] -fn function_with_args_after_args_sep(args: &PyTuple) { - let _ = args; -} - -#[pyfunction] -#[pyo3(signature = (*, *))] -fn function_with_args_sep_after_args_sep() {} - -#[pyfunction] -#[pyo3(signature = (**kwargs, *args))] -fn function_with_args_after_kwargs(kwargs: Option<&PyDict>, args: &PyTuple) { - let _ = args; - let _ = kwargs; -} - -#[pyfunction] -#[pyo3(signature = (**kwargs_a, **kwargs_b))] -fn function_with_kwargs_after_kwargs(kwargs_a: Option<&PyDict>, kwargs_b: Option<&PyDict>) { - let _ = kwargs_a; - let _ = kwargs_b; -} - -#[pyfunction(signature = (py))] -fn signature_contains_py(py: Python<'_>) { - let _ = py; -} - -#[pyclass] -struct MyClass; - -#[pymethods] -impl MyClass { - #[args(x)] - #[pyo3(signature = (x))] - fn method_with_both_args_and_signature(&self, x: i32) { - let _ = x; - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.stderr b/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.stderr deleted file mode 100644 index 97d0fd3b4a..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunction_signatures.stderr +++ /dev/null @@ -1,59 +0,0 @@ -error: missing signature entry for argument `_x` - --> tests/ui/invalid_pyfunction_signatures.rs:5:8 - | -5 | #[pyo3(signature = ())] - | ^^^^^^^^^ - -error: signature entry does not have a corresponding function argument - --> tests/ui/invalid_pyfunction_signatures.rs:9:21 - | -9 | #[pyo3(signature = (x))] - | ^ - -error: expected argument from function definition `y` but got argument `x` - --> tests/ui/invalid_pyfunction_signatures.rs:13:21 - | -13 | #[pyo3(signature = (x))] - | ^ - -error: expected one of: `name`, `pass_module`, `signature`, `text_signature`, `crate` - --> tests/ui/invalid_pyfunction_signatures.rs:18:14 - | -18 | #[pyfunction(x)] - | ^ - -error: `*args` not allowed after `*` - --> tests/ui/invalid_pyfunction_signatures.rs:25:24 - | -25 | #[pyo3(signature = (*, *args))] - | ^ - -error: `*` not allowed after `*` - --> tests/ui/invalid_pyfunction_signatures.rs:31:24 - | -31 | #[pyo3(signature = (*, *))] - | ^ - -error: `*args` not allowed after `**kwargs` - --> tests/ui/invalid_pyfunction_signatures.rs:35:31 - | -35 | #[pyo3(signature = (**kwargs, *args))] - | ^ - -error: `**kwargs_b` not allowed after `**kwargs_a` - --> tests/ui/invalid_pyfunction_signatures.rs:42:33 - | -42 | #[pyo3(signature = (**kwargs_a, **kwargs_b))] - | ^ - -error: arguments of type `Python` must not be part of the signature - --> tests/ui/invalid_pyfunction_signatures.rs:48:27 - | -48 | #[pyfunction(signature = (py))] - | ^^ - -error: cannot find attribute `args` in this scope - --> tests/ui/invalid_pyfunction_signatures.rs:58:7 - | -58 | #[args(x)] - | ^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pyfunctions.rs b/vendor/pyo3/tests/ui/invalid_pyfunctions.rs deleted file mode 100644 index 1c0c45d6b9..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunctions.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::{PyDict, PyString, PyTuple}; - -#[pyfunction] -fn generic_function(_value: T) {} - -#[pyfunction] -fn impl_trait_function(_impl_trait: impl AsRef) {} - -#[pyfunction] -fn wildcard_argument(_: i32) {} - -#[pyfunction] -fn destructured_argument((_a, _b): (i32, i32)) {} - -#[pyfunction] -fn function_with_required_after_option(_opt: Option, _x: i32) {} - -#[pyfunction] -#[pyo3(signature=(*args))] -fn function_with_optional_args(args: Option>) { - let _ = args; -} - -#[pyfunction] -#[pyo3(signature=(**kwargs))] -fn function_with_required_kwargs(kwargs: Bound<'_, PyDict>) { - let _ = kwargs; -} - -#[pyfunction(pass_module)] -fn pass_module_but_no_arguments<'py>() {} - -#[pyfunction(pass_module)] -fn first_argument_not_module<'a, 'py>( - _string: &str, - module: &'a Bound<'py, PyModule>, -) -> PyResult> { - module.name() -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pyfunctions.stderr b/vendor/pyo3/tests/ui/invalid_pyfunctions.stderr deleted file mode 100644 index 4e537ae1ea..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pyfunctions.stderr +++ /dev/null @@ -1,63 +0,0 @@ -error: Python functions cannot have generic type parameters - --> tests/ui/invalid_pyfunctions.rs:5:21 - | -5 | fn generic_function(_value: T) {} - | ^ - -error: Python functions cannot have `impl Trait` arguments - --> tests/ui/invalid_pyfunctions.rs:8:37 - | -8 | fn impl_trait_function(_impl_trait: impl AsRef) {} - | ^^^^ - -error: wildcard argument names are not supported - --> tests/ui/invalid_pyfunctions.rs:11:22 - | -11 | fn wildcard_argument(_: i32) {} - | ^ - -error: destructuring in arguments is not supported - --> tests/ui/invalid_pyfunctions.rs:14:26 - | -14 | fn destructured_argument((_a, _b): (i32, i32)) {} - | ^^^^^^^^ - -error: required arguments after an `Option<_>` argument are ambiguous - = help: add a `#[pyo3(signature)]` annotation on this function to unambiguously specify the default values for all optional parameters - --> tests/ui/invalid_pyfunctions.rs:17:63 - | -17 | fn function_with_required_after_option(_opt: Option, _x: i32) {} - | ^^^ - -error: args cannot be optional - --> tests/ui/invalid_pyfunctions.rs:21:32 - | -21 | fn function_with_optional_args(args: Option>) { - | ^^^^ - -error: kwargs must be Option<_> - --> tests/ui/invalid_pyfunctions.rs:27:34 - | -27 | fn function_with_required_kwargs(kwargs: Bound<'_, PyDict>) { - | ^^^^^^ - -error: expected `&PyModule` or `Py` as first argument with `pass_module` - --> tests/ui/invalid_pyfunctions.rs:32:37 - | -32 | fn pass_module_but_no_arguments<'py>() {} - | ^^ - -error[E0277]: the trait bound `&str: From>` is not satisfied - --> tests/ui/invalid_pyfunctions.rs:36:14 - | -36 | _string: &str, - | ^ the trait `From>` is not implemented for `&str` - | - = help: the following other types implement trait `From`: - `String` implements `From<&String>` - `String` implements `From<&mut str>` - `String` implements `From<&str>` - `String` implements `From>` - `String` implements `From>` - `String` implements `From` - = note: required for `BoundRef<'_, '_, pyo3::types::PyModule>` to implement `Into<&str>` diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_enum.rs b/vendor/pyo3/tests/ui/invalid_pymethod_enum.rs deleted file mode 100644 index 5c41d19d4e..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_enum.rs +++ /dev/null @@ -1,35 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -enum ComplexEnum { - Int { int: i32 }, - Str { string: String }, -} - -#[pymethods] -impl ComplexEnum { - fn mutate_in_place(&mut self) { - *self = match self { - ComplexEnum::Int { int } => ComplexEnum::Str { string: int.to_string() }, - ComplexEnum::Str { string } => ComplexEnum::Int { int: string.len() as i32 }, - } - } -} - -#[pyclass] -enum TupleEnum { - Int(i32), - Str(String), -} - -#[pymethods] -impl TupleEnum { - fn mutate_in_place(&mut self) { - *self = match self { - TupleEnum::Int(int) => TupleEnum::Str(int.to_string()), - TupleEnum::Str(string) => TupleEnum::Int(string.len() as i32), - } - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_enum.stderr b/vendor/pyo3/tests/ui/invalid_pymethod_enum.stderr deleted file mode 100644 index bc377d2a05..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_enum.stderr +++ /dev/null @@ -1,49 +0,0 @@ -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_pymethod_enum.rs:11:24 - | -11 | fn mutate_in_place(&mut self) { - | ^ expected `False`, found `True` - | -note: required by a bound in `extract_pyclass_ref_mut` - --> src/impl_/extract_argument.rs - | - | pub fn extract_pyclass_ref_mut<'a, 'py: 'a, T: PyClass>( - | ^^^^^^^^^^^^^^ required by this bound in `extract_pyclass_ref_mut` - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_pymethod_enum.rs:9:1 - | -9 | #[pymethods] - | ^^^^^^^^^^^^ expected `False`, found `True` - | -note: required by a bound in `PyRefMut` - --> src/pycell.rs - | - | pub struct PyRefMut<'p, T: PyClass> { - | ^^^^^^^^^^^^^^ required by this bound in `PyRefMut` - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_pymethod_enum.rs:27:24 - | -27 | fn mutate_in_place(&mut self) { - | ^ expected `False`, found `True` - | -note: required by a bound in `extract_pyclass_ref_mut` - --> src/impl_/extract_argument.rs - | - | pub fn extract_pyclass_ref_mut<'a, 'py: 'a, T: PyClass>( - | ^^^^^^^^^^^^^^ required by this bound in `extract_pyclass_ref_mut` - -error[E0271]: type mismatch resolving `::Frozen == False` - --> tests/ui/invalid_pymethod_enum.rs:25:1 - | -25 | #[pymethods] - | ^^^^^^^^^^^^ expected `False`, found `True` - | -note: required by a bound in `PyRefMut` - --> src/pycell.rs - | - | pub struct PyRefMut<'p, T: PyClass> { - | ^^^^^^^^^^^^^^ required by this bound in `PyRefMut` - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_names.rs b/vendor/pyo3/tests/ui/invalid_pymethod_names.rs deleted file mode 100644 index 1334bcabde..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_names.rs +++ /dev/null @@ -1,42 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct TestClass { - num: u32, -} - -#[pymethods] -impl TestClass { - #[pyo3(name = "num")] - #[getter(number)] - fn get_num(&self) -> u32 { self.num } -} - -#[pymethods] -impl TestClass { - #[pyo3(name = "foo")] - #[pyo3(name = "bar")] - fn qux(&self) -> u32 { self.num } -} - -#[pymethods] -impl TestClass { - #[pyo3(name = "makenew")] - #[new] - fn new(&self) -> Self { Self { num: 0 } } -} - -#[pymethods] -impl TestClass { - #[getter(1)] - fn get_one(&self) -> Self { Self { num: 0 } } -} - -#[pymethods] -impl TestClass { - #[getter = 1] - fn get_two(&self) -> Self { Self { num: 0 } } -} - - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_names.stderr b/vendor/pyo3/tests/ui/invalid_pymethod_names.stderr deleted file mode 100644 index 1e7a6f4406..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_names.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error: `name` may only be specified once - --> tests/ui/invalid_pymethod_names.rs:11:14 - | -11 | #[getter(number)] - | ^^^^^^ - -error: `name` may only be specified once - --> tests/ui/invalid_pymethod_names.rs:18:12 - | -18 | #[pyo3(name = "bar")] - | ^^^^ - -error: `name` not allowed with `#[new]` - --> tests/ui/invalid_pymethod_names.rs:24:19 - | -24 | #[pyo3(name = "makenew")] - | ^^^^^^^^^ - -error: expected ident or string literal for property name - --> tests/ui/invalid_pymethod_names.rs:31:14 - | -31 | #[getter(1)] - | ^ - -error: expected `#[getter(name)]` to set the name - --> tests/ui/invalid_pymethod_names.rs:37:14 - | -37 | #[getter = 1] - | ^ diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_receiver.rs b/vendor/pyo3/tests/ui/invalid_pymethod_receiver.rs deleted file mode 100644 index 4949ff9602..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_receiver.rs +++ /dev/null @@ -1,11 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - fn method_with_invalid_self_type(_slf: i32, _py: Python<'_>, _index: u32) {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethod_receiver.stderr b/vendor/pyo3/tests/ui/invalid_pymethod_receiver.stderr deleted file mode 100644 index 7d81c0fae9..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethod_receiver.stderr +++ /dev/null @@ -1,14 +0,0 @@ -error[E0277]: the trait bound `i32: TryFrom>` is not satisfied - --> tests/ui/invalid_pymethod_receiver.rs:8:44 - | -8 | fn method_with_invalid_self_type(_slf: i32, _py: Python<'_>, _index: u32) {} - | ^^^ the trait `From>` is not implemented for `i32` - | - = help: the following other types implement trait `From`: - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - = note: required for `BoundRef<'_, '_, MyClass>` to implement `Into` - = note: required for `i32` to implement `TryFrom>` diff --git a/vendor/pyo3/tests/ui/invalid_pymethods.rs b/vendor/pyo3/tests/ui/invalid_pymethods.rs deleted file mode 100644 index 41786cd789..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods.rs +++ /dev/null @@ -1,200 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[classattr] - fn class_attr_with_args(_foo: i32) {} -} - -#[pymethods] -impl MyClass { - #[classattr(foobar)] - const CLASS_ATTR_WITH_ATTRIBUTE_ARG: i32 = 3; -} - -#[pymethods] -impl MyClass { - fn staticmethod_without_attribute() {} -} - -#[pymethods] -impl MyClass { - #[staticmethod] - fn staticmethod_with_receiver(&self) {} -} - -#[pymethods] -impl MyClass { - #[classmethod] - fn classmethod_with_receiver(&self) {} -} - -#[pymethods] -impl MyClass { - #[classmethod] - fn classmethod_missing_argument() -> Self { - Self {} - } -} - -#[pymethods] -impl MyClass { - #[classmethod] - fn classmethod_wrong_first_argument(_x: i32) -> Self { - Self {} - } -} - -#[pymethods] -impl MyClass { - #[getter(x)] - fn getter_without_receiver() {} -} - -#[pymethods] -impl MyClass { - #[setter(x)] - fn setter_without_receiver() {} -} - -#[pymethods] -impl MyClass { - #[pyo3(name = "__call__", text_signature = "()")] - fn text_signature_on_call() {} -} - -#[pymethods] -impl MyClass { - #[getter(x)] - #[pyo3(text_signature = "()")] - fn text_signature_on_getter(&self) {} -} - -#[pymethods] -impl MyClass { - #[setter(x)] - #[pyo3(text_signature = "()")] - fn text_signature_on_setter(&self) {} -} - -#[pymethods] -impl MyClass { - #[classattr] - #[pyo3(text_signature = "()")] - fn text_signature_on_classattr() {} -} - -#[pymethods] -impl MyClass { - #[pyo3(text_signature = 1)] - fn invalid_text_signature() {} -} - -#[pymethods] -impl MyClass { - #[pyo3(text_signature = "()")] - #[pyo3(text_signature = None)] - fn duplicate_text_signature() {} -} - -#[pymethods] -impl MyClass { - #[getter(x)] - #[pyo3(signature = ())] - fn signature_on_getter(&self) {} -} - -#[pymethods] -impl MyClass { - #[setter(x)] - #[pyo3(signature = ())] - fn signature_on_setter(&self) {} -} - -#[pymethods] -impl MyClass { - #[classattr] - #[pyo3(signature = ())] - fn signature_on_classattr() {} -} - -#[pymethods] -impl MyClass { - #[new] - #[classmethod] - #[staticmethod] - #[classattr] - #[getter(x)] - #[setter(x)] - fn multiple_method_types() {} -} - -#[pymethods] -impl MyClass { - #[new(signature = ())] - fn new_takes_no_arguments(&self) {} -} - -#[pymethods] -impl MyClass { - #[new = ()] // in this form there's no suggestion to move arguments to `#[pyo3()]` attribute - fn new_takes_no_arguments_nv(&self) {} -} - -#[pymethods] -impl MyClass { - #[classmethod(signature = ())] - fn classmethod_takes_no_arguments(&self) {} -} - -#[pymethods] -impl MyClass { - #[staticmethod(signature = ())] - fn staticmethod_takes_no_arguments(&self) {} -} - -#[pymethods] -impl MyClass { - #[classattr(signature = ())] - fn classattr_takes_no_arguments(&self) {} -} - -#[pymethods] -impl MyClass { - fn generic_method(_value: T) {} -} - -#[pymethods] -impl MyClass { - fn impl_trait_method_first_arg(_impl_trait: impl AsRef) {} -} - -#[pymethods] -impl MyClass { - fn impl_trait_method_second_arg(&self, _impl_trait: impl AsRef) {} -} - -#[pymethods] -impl MyClass { - #[pyo3(pass_module)] - fn method_cannot_pass_module(&self, _m: &PyModule) {} -} - -#[pymethods] -impl MyClass { - fn method_self_by_value(self) {} -} - -macro_rules! macro_invocation { - () => {}; -} - -#[pymethods] -impl MyClass { - macro_invocation!(); -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethods.stderr b/vendor/pyo3/tests/ui/invalid_pymethods.stderr deleted file mode 100644 index eb8f429c93..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods.stderr +++ /dev/null @@ -1,194 +0,0 @@ -error: #[classattr] can only have one argument (of type pyo3::Python) - --> tests/ui/invalid_pymethods.rs:9:35 - | -9 | fn class_attr_with_args(_foo: i32) {} - | ^^^ - -error: `#[classattr]` does not take any arguments - --> tests/ui/invalid_pymethods.rs:14:5 - | -14 | #[classattr(foobar)] - | ^ - -error: static method needs #[staticmethod] attribute - --> tests/ui/invalid_pymethods.rs:20:5 - | -20 | fn staticmethod_without_attribute() {} - | ^^ - -error: unexpected receiver - --> tests/ui/invalid_pymethods.rs:26:35 - | -26 | fn staticmethod_with_receiver(&self) {} - | ^ - -error: Expected `&Bound` or `Py` as the first argument to `#[classmethod]` - --> tests/ui/invalid_pymethods.rs:32:33 - | -32 | fn classmethod_with_receiver(&self) {} - | ^^^^^^^ - -error: Expected `&Bound` or `Py` as the first argument to `#[classmethod]` - --> tests/ui/invalid_pymethods.rs:38:36 - | -38 | fn classmethod_missing_argument() -> Self { - | ^^ - -error: expected receiver for `#[getter]` - --> tests/ui/invalid_pymethods.rs:54:5 - | -54 | fn getter_without_receiver() {} - | ^^ - -error: expected receiver for `#[setter]` - --> tests/ui/invalid_pymethods.rs:60:5 - | -60 | fn setter_without_receiver() {} - | ^^ - -error: static method needs #[staticmethod] attribute - --> tests/ui/invalid_pymethods.rs:66:5 - | -66 | fn text_signature_on_call() {} - | ^^ - -error: `text_signature` not allowed with `getter` - --> tests/ui/invalid_pymethods.rs:72:12 - | -72 | #[pyo3(text_signature = "()")] - | ^^^^^^^^^^^^^^ - -error: `text_signature` not allowed with `setter` - --> tests/ui/invalid_pymethods.rs:79:12 - | -79 | #[pyo3(text_signature = "()")] - | ^^^^^^^^^^^^^^ - -error: `text_signature` not allowed with `classattr` - --> tests/ui/invalid_pymethods.rs:86:12 - | -86 | #[pyo3(text_signature = "()")] - | ^^^^^^^^^^^^^^ - -error: expected a string literal or `None` - --> tests/ui/invalid_pymethods.rs:92:30 - | -92 | #[pyo3(text_signature = 1)] - | ^ - -error: `text_signature` may only be specified once - --> tests/ui/invalid_pymethods.rs:99:12 - | -99 | #[pyo3(text_signature = None)] - | ^^^^^^^^^^^^^^ - -error: `signature` not allowed with `getter` - --> tests/ui/invalid_pymethods.rs:106:12 - | -106 | #[pyo3(signature = ())] - | ^^^^^^^^^ - -error: `signature` not allowed with `setter` - --> tests/ui/invalid_pymethods.rs:113:12 - | -113 | #[pyo3(signature = ())] - | ^^^^^^^^^ - -error: `signature` not allowed with `classattr` - --> tests/ui/invalid_pymethods.rs:120:12 - | -120 | #[pyo3(signature = ())] - | ^^^^^^^^^ - -error: `#[new]` may not be combined with `#[classmethod]` `#[staticmethod]`, `#[classattr]`, `#[getter]`, and `#[setter]` - --> tests/ui/invalid_pymethods.rs:126:7 - | -126 | #[new] - | ^^^ - -error: `#[new]` does not take any arguments - = help: did you mean `#[new] #[pyo3(signature = ())]`? - --> tests/ui/invalid_pymethods.rs:137:7 - | -137 | #[new(signature = ())] - | ^^^ - -error: `#[new]` does not take any arguments - = note: this was previously accepted and ignored - --> tests/ui/invalid_pymethods.rs:143:11 - | -143 | #[new = ()] // in this form there's no suggestion to move arguments to `#[pyo3()]` attribute - | ^ - -error: `#[classmethod]` does not take any arguments - = help: did you mean `#[classmethod] #[pyo3(signature = ())]`? - --> tests/ui/invalid_pymethods.rs:149:7 - | -149 | #[classmethod(signature = ())] - | ^^^^^^^^^^^ - -error: `#[staticmethod]` does not take any arguments - = help: did you mean `#[staticmethod] #[pyo3(signature = ())]`? - --> tests/ui/invalid_pymethods.rs:155:7 - | -155 | #[staticmethod(signature = ())] - | ^^^^^^^^^^^^ - -error: `#[classattr]` does not take any arguments - = help: did you mean `#[classattr] #[pyo3(signature = ())]`? - --> tests/ui/invalid_pymethods.rs:161:7 - | -161 | #[classattr(signature = ())] - | ^^^^^^^^^ - -error: Python functions cannot have generic type parameters - --> tests/ui/invalid_pymethods.rs:167:23 - | -167 | fn generic_method(_value: T) {} - | ^ - -error: Python functions cannot have `impl Trait` arguments - --> tests/ui/invalid_pymethods.rs:172:49 - | -172 | fn impl_trait_method_first_arg(_impl_trait: impl AsRef) {} - | ^^^^ - -error: Python functions cannot have `impl Trait` arguments - --> tests/ui/invalid_pymethods.rs:177:57 - | -177 | fn impl_trait_method_second_arg(&self, _impl_trait: impl AsRef) {} - | ^^^^ - -error: `pass_module` cannot be used on Python methods - --> tests/ui/invalid_pymethods.rs:182:12 - | -182 | #[pyo3(pass_module)] - | ^^^^^^^^^^^ - -error: Python objects are shared, so 'self' cannot be moved out of the Python interpreter. - Try `&self`, `&mut self, `slf: PyRef<'_, Self>` or `slf: PyRefMut<'_, Self>`. - --> tests/ui/invalid_pymethods.rs:188:29 - | -188 | fn method_self_by_value(self) {} - | ^^^^ - -error: macros cannot be used as items in `#[pymethods]` impl blocks - = note: this was previously accepted and ignored - --> tests/ui/invalid_pymethods.rs:197:5 - | -197 | macro_invocation!(); - | ^^^^^^^^^^^^^^^^ - -error[E0277]: the trait bound `i32: From>` is not satisfied - --> tests/ui/invalid_pymethods.rs:46:45 - | -46 | fn classmethod_wrong_first_argument(_x: i32) -> Self { - | ^^^ the trait `From>` is not implemented for `i32` - | - = help: the following other types implement trait `From`: - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - `i32` implements `From` - = note: required for `BoundRef<'_, '_, PyType>` to implement `Into` diff --git a/vendor/pyo3/tests/ui/invalid_pymethods_buffer.rs b/vendor/pyo3/tests/ui/invalid_pymethods_buffer.rs deleted file mode 100644 index 4eb7c28e09..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods_buffer.rs +++ /dev/null @@ -1,18 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct MyClass {} - -#[pymethods] -impl MyClass { - #[pyo3(name = "__getbuffer__")] - fn getbuffer_must_be_unsafe(&self, _view: *mut pyo3::ffi::Py_buffer, _flags: std::os::raw::c_int) {} -} - -#[pymethods] -impl MyClass { - #[pyo3(name = "__releasebuffer__")] - fn releasebuffer_must_be_unsafe(&self, _view: *mut pyo3::ffi::Py_buffer) {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethods_buffer.stderr b/vendor/pyo3/tests/ui/invalid_pymethods_buffer.stderr deleted file mode 100644 index 3480848f3c..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods_buffer.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: `__getbuffer__` must be `unsafe fn` - --> tests/ui/invalid_pymethods_buffer.rs:9:8 - | -9 | fn getbuffer_must_be_unsafe(&self, _view: *mut pyo3::ffi::Py_buffer, _flags: std::os::raw::c_int) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ - -error: `__releasebuffer__` must be `unsafe fn` - --> tests/ui/invalid_pymethods_buffer.rs:15:8 - | -15 | fn releasebuffer_must_be_unsafe(&self, _view: *mut pyo3::ffi::Py_buffer) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.rs b/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.rs deleted file mode 100644 index 04435dcb64..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! These tests are located in a separate file because they cause conflicting implementation -//! errors, which means other errors such as typechecking errors are not reported. - -use pyo3::prelude::*; - -#[pyclass] -struct TwoNew {} - -#[pymethods] -impl TwoNew { - #[new] - fn new_1() -> Self { - Self {} - } - - #[new] - fn new_2() -> Self { - Self {} - } -} - -#[pyclass] -struct DuplicateMethod {} - -#[pymethods] -impl DuplicateMethod { - #[pyo3(name = "func")] - fn func_a(&self) {} - - #[pyo3(name = "func")] - fn func_b(&self) {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.stderr b/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.stderr deleted file mode 100644 index a68d01cf5f..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymethods_duplicates.stderr +++ /dev/null @@ -1,32 +0,0 @@ -error[E0119]: conflicting implementations of trait `pyo3::impl_::pyclass::PyClassNewTextSignature` for type `pyo3::impl_::pyclass::PyClassImplCollector` - --> tests/ui/invalid_pymethods_duplicates.rs:9:1 - | -9 | #[pymethods] - | ^^^^^^^^^^^^ - | | - | first implementation here - | conflicting implementation for `pyo3::impl_::pyclass::PyClassImplCollector` - | - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod___new____` - --> tests/ui/invalid_pymethods_duplicates.rs:9:1 - | -9 | #[pymethods] - | ^^^^^^^^^^^^ - | | - | duplicate definitions for `__pymethod___new____` - | other definition for `__pymethod___new____` - | - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0592]: duplicate definitions with name `__pymethod_func__` - --> tests/ui/invalid_pymethods_duplicates.rs:25:1 - | -25 | #[pymethods] - | ^^^^^^^^^^^^ - | | - | duplicate definitions for `__pymethod_func__` - | other definition for `__pymethod_func__` - | - = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_args.rs b/vendor/pyo3/tests/ui/invalid_pymodule_args.rs deleted file mode 100644 index 37e53960fd..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_args.rs +++ /dev/null @@ -1,8 +0,0 @@ -use pyo3::prelude::*; - -#[pymodule(some_arg)] -fn module(m: &Bound<'_, PyModule>) -> PyResult<()> { - Ok(()) -} - -fn main(){} diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_args.stderr b/vendor/pyo3/tests/ui/invalid_pymodule_args.stderr deleted file mode 100644 index 23a5109b4c..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_args.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: expected one of: `name`, `crate`, `module`, `submodule`, `gil_used` - --> tests/ui/invalid_pymodule_args.rs:3:12 - | -3 | #[pymodule(some_arg)] - | ^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_glob.rs b/vendor/pyo3/tests/ui/invalid_pymodule_glob.rs deleted file mode 100644 index 853493b535..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_glob.rs +++ /dev/null @@ -1,16 +0,0 @@ -#![allow(unused_imports)] - -use pyo3::prelude::*; - -#[pyfunction] -fn foo() -> usize { - 0 -} - -#[pymodule] -mod module { - #[pymodule_export] - use super::*; -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_glob.stderr b/vendor/pyo3/tests/ui/invalid_pymodule_glob.stderr deleted file mode 100644 index 1c033083e0..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_glob.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: #[pymodule] cannot import glob statements - --> tests/ui/invalid_pymodule_glob.rs:13:16 - | -13 | use super::*; - | ^ diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_in_root.rs b/vendor/pyo3/tests/ui/invalid_pymodule_in_root.rs deleted file mode 100644 index 76ced6c3fb..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_in_root.rs +++ /dev/null @@ -1,7 +0,0 @@ -use pyo3::prelude::*; - -#[pymodule] -#[path = "empty.rs"] // to silence error related to missing file -mod invalid_pymodule_in_root_module; - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_in_root.stderr b/vendor/pyo3/tests/ui/invalid_pymodule_in_root.stderr deleted file mode 100644 index 06152161e5..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_in_root.stderr +++ /dev/null @@ -1,13 +0,0 @@ -error[E0658]: non-inline modules in proc macro input are unstable - --> tests/ui/invalid_pymodule_in_root.rs:5:1 - | -5 | mod invalid_pymodule_in_root_module; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #54727 for more information - -error: `#[pymodule]` can only be used on inline modules - --> tests/ui/invalid_pymodule_in_root.rs:5:1 - | -5 | mod invalid_pymodule_in_root_module; - | ^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_trait.rs b/vendor/pyo3/tests/ui/invalid_pymodule_trait.rs deleted file mode 100644 index 6649a3547a..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_trait.rs +++ /dev/null @@ -1,9 +0,0 @@ -use pyo3::prelude::*; - -#[pymodule] -mod module { - #[pymodule_export] - trait Foo {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_trait.stderr b/vendor/pyo3/tests/ui/invalid_pymodule_trait.stderr deleted file mode 100644 index 0b0d46da93..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_trait.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: `#[pymodule_export]` may only be used on `use` statements - --> tests/ui/invalid_pymodule_trait.rs:5:5 - | -5 | #[pymodule_export] - | ^ - -error: cannot find attribute `pymodule_export` in this scope - --> tests/ui/invalid_pymodule_trait.rs:5:7 - | -5 | #[pymodule_export] - | ^^^^^^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.rs b/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.rs deleted file mode 100644 index ed72cbb723..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.rs +++ /dev/null @@ -1,18 +0,0 @@ -use pyo3::prelude::*; - -#[pymodule] -mod module { - use pyo3::prelude::*; - - #[pymodule_init] - fn init(_m: &Bound<'_, PyModule>) -> PyResult<()> { - Ok(()) - } - - #[pymodule_init] - fn init2(_m: &Bound<'_, PyModule>) -> PyResult<()> { - Ok(()) - } -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.stderr b/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.stderr deleted file mode 100644 index 8fbd12f2e4..0000000000 --- a/vendor/pyo3/tests/ui/invalid_pymodule_two_pymodule_init.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: only one `#[pymodule_init]` may be specified - --> tests/ui/invalid_pymodule_two_pymodule_init.rs:13:5 - | -13 | fn init2(_m: &Bound<'_, PyModule>) -> PyResult<()> { - | ^^ diff --git a/vendor/pyo3/tests/ui/invalid_result_conversion.rs b/vendor/pyo3/tests/ui/invalid_result_conversion.rs deleted file mode 100644 index 2679f3afda..0000000000 --- a/vendor/pyo3/tests/ui/invalid_result_conversion.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Testing https://github.com/PyO3/pyo3/issues/1106. A result type that -//! *doesn't* implement `From for PyErr` won't be automatically -//! converted when using `#[pyfunction]`. -use pyo3::prelude::*; - -use std::fmt; - -/// A basic error type for the tests. It's missing `From for PyErr`, -/// though, so it shouldn't work. -#[derive(Debug)] -struct MyError { - pub descr: &'static str, -} - -impl fmt::Display for MyError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "My error message: {}", self.descr) - } -} - -#[pyfunction] -fn should_not_work() -> Result<(), MyError> { - Err(MyError { - descr: "something went wrong", - }) -} - -fn main() { - Python::with_gil(|py| { - wrap_pyfunction!(should_not_work)(py); - }); -} diff --git a/vendor/pyo3/tests/ui/invalid_result_conversion.stderr b/vendor/pyo3/tests/ui/invalid_result_conversion.stderr deleted file mode 100644 index f782e00828..0000000000 --- a/vendor/pyo3/tests/ui/invalid_result_conversion.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error[E0277]: the trait bound `PyErr: From` is not satisfied - --> tests/ui/invalid_result_conversion.rs:22:25 - | -22 | fn should_not_work() -> Result<(), MyError> { - | ^^^^^^ the trait `From` is not implemented for `PyErr` - | - = help: the following other types implement trait `From`: - `PyErr` implements `From` - `PyErr` implements `From` - `PyErr` implements `From>` - `PyErr` implements `From>` - `PyErr` implements `From` - `PyErr` implements `From` - `PyErr` implements `From` - `PyErr` implements `From>` - and $N others - = note: required for `MyError` to implement `Into` diff --git a/vendor/pyo3/tests/ui/missing_intopy.rs b/vendor/pyo3/tests/ui/missing_intopy.rs deleted file mode 100644 index 36de7bdee7..0000000000 --- a/vendor/pyo3/tests/ui/missing_intopy.rs +++ /dev/null @@ -1,8 +0,0 @@ -struct Blah; - -#[pyo3::pyfunction] -fn blah() -> Blah { - Blah -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/missing_intopy.stderr b/vendor/pyo3/tests/ui/missing_intopy.stderr deleted file mode 100644 index afa8d9e48a..0000000000 --- a/vendor/pyo3/tests/ui/missing_intopy.stderr +++ /dev/null @@ -1,36 +0,0 @@ -error[E0277]: `Blah` cannot be converted to a Python object - --> tests/ui/missing_intopy.rs:4:14 - | -4 | fn blah() -> Blah { - | ^^^^ the trait `IntoPyObject<'_>` is not implemented for `Blah` - | - = note: `IntoPyObject` is automatically implemented by the `#[pyclass]` macro - = note: if you do not wish to have a corresponding Python type, implement it manually - = note: if you do not own `Blah` you can perform a manual conversion to one of the types in `pyo3::types::*` - = help: the following other types implement trait `IntoPyObject<'py>`: - &&'a T - &&OsStr - &&Path - &&str - &'a (T0, T1) - &'a (T0, T1, T2) - &'a (T0, T1, T2, T3) - &'a (T0, T1, T2, T3, T4) - and $N others -note: required by a bound in `UnknownReturnType::::wrap` - --> src/impl_/wrap.rs - | - | pub fn wrap<'py>(&self, _: T) -> T - | ---- required by a bound in this associated function - | where - | T: IntoPyObject<'py>, - | ^^^^^^^^^^^^^^^^^ required by this bound in `UnknownReturnType::::wrap` - -error[E0599]: no method named `map_err` found for struct `Blah` in the current scope - --> tests/ui/missing_intopy.rs:4:14 - | -1 | struct Blah; - | ----------- method `map_err` not found for this struct -... -4 | fn blah() -> Blah { - | ^^^^ method not found in `Blah` diff --git a/vendor/pyo3/tests/ui/not_send.rs b/vendor/pyo3/tests/ui/not_send.rs deleted file mode 100644 index 6566f2d7de..0000000000 --- a/vendor/pyo3/tests/ui/not_send.rs +++ /dev/null @@ -1,11 +0,0 @@ -use pyo3::prelude::*; - -fn test_not_send_allow_threads(py: Python<'_>) { - py.allow_threads(|| { drop(py); }); -} - -fn main() { - Python::with_gil(|py| { - test_not_send_allow_threads(py); - }) -} diff --git a/vendor/pyo3/tests/ui/not_send.stderr b/vendor/pyo3/tests/ui/not_send.stderr deleted file mode 100644 index 5d05b3de05..0000000000 --- a/vendor/pyo3/tests/ui/not_send.stderr +++ /dev/null @@ -1,45 +0,0 @@ -error[E0277]: `*mut pyo3::Python<'static>` cannot be shared between threads safely - --> tests/ui/not_send.rs:4:22 - | -4 | py.allow_threads(|| { drop(py); }); - | ------------- ^^^^^^^^^^^^^^^^ `*mut pyo3::Python<'static>` cannot be shared between threads safely - | | - | required by a bound introduced by this call - | - = help: within `pyo3::Python<'_>`, the trait `Sync` is not implemented for `*mut pyo3::Python<'static>` -note: required because it appears within the type `PhantomData<*mut pyo3::Python<'static>>` - --> $RUST/core/src/marker.rs - | - | pub struct PhantomData; - | ^^^^^^^^^^^ -note: required because it appears within the type `impl_::not_send::NotSend` - --> src/impl_/not_send.rs - | - | pub(crate) struct NotSend(PhantomData<*mut Python<'static>>); - | ^^^^^^^ - = note: required because it appears within the type `(&pyo3::gil::GILGuard, impl_::not_send::NotSend)` -note: required because it appears within the type `PhantomData<(&pyo3::gil::GILGuard, impl_::not_send::NotSend)>` - --> $RUST/core/src/marker.rs - | - | pub struct PhantomData; - | ^^^^^^^^^^^ -note: required because it appears within the type `pyo3::Python<'_>` - --> src/marker.rs - | - | pub struct Python<'py>(PhantomData<(&'py GILGuard, NotSend)>); - | ^^^^^^ - = note: required for `&pyo3::Python<'_>` to implement `Send` -note: required because it's used within this closure - --> tests/ui/not_send.rs:4:22 - | -4 | py.allow_threads(|| { drop(py); }); - | ^^ - = note: required for `{closure@$DIR/tests/ui/not_send.rs:4:22: 4:24}` to implement `Ungil` -note: required by a bound in `pyo3::Python::<'py>::allow_threads` - --> src/marker.rs - | - | pub fn allow_threads(self, f: F) -> T - | ------------- required by a bound in this associated function - | where - | F: Ungil + FnOnce() -> T, - | ^^^^^ required by this bound in `Python::<'py>::allow_threads` diff --git a/vendor/pyo3/tests/ui/not_send2.rs b/vendor/pyo3/tests/ui/not_send2.rs deleted file mode 100644 index 32d95c1a23..0000000000 --- a/vendor/pyo3/tests/ui/not_send2.rs +++ /dev/null @@ -1,12 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyString; - -fn main() { - Python::with_gil(|py| { - let string = PyString::new(py, "foo"); - - py.allow_threads(|| { - println!("{:?}", string); - }); - }); -} diff --git a/vendor/pyo3/tests/ui/not_send2.stderr b/vendor/pyo3/tests/ui/not_send2.stderr deleted file mode 100644 index eec2b644e9..0000000000 --- a/vendor/pyo3/tests/ui/not_send2.stderr +++ /dev/null @@ -1,53 +0,0 @@ -error[E0277]: `*mut pyo3::Python<'static>` cannot be shared between threads safely - --> tests/ui/not_send2.rs:8:26 - | -8 | py.allow_threads(|| { - | ____________-------------_^ - | | | - | | required by a bound introduced by this call -9 | | println!("{:?}", string); -10 | | }); - | |_________^ `*mut pyo3::Python<'static>` cannot be shared between threads safely - | - = help: within `pyo3::Bound<'_, PyString>`, the trait `Sync` is not implemented for `*mut pyo3::Python<'static>` -note: required because it appears within the type `PhantomData<*mut pyo3::Python<'static>>` - --> $RUST/core/src/marker.rs - | - | pub struct PhantomData; - | ^^^^^^^^^^^ -note: required because it appears within the type `impl_::not_send::NotSend` - --> src/impl_/not_send.rs - | - | pub(crate) struct NotSend(PhantomData<*mut Python<'static>>); - | ^^^^^^^ - = note: required because it appears within the type `(&pyo3::gil::GILGuard, impl_::not_send::NotSend)` -note: required because it appears within the type `PhantomData<(&pyo3::gil::GILGuard, impl_::not_send::NotSend)>` - --> $RUST/core/src/marker.rs - | - | pub struct PhantomData; - | ^^^^^^^^^^^ -note: required because it appears within the type `pyo3::Python<'_>` - --> src/marker.rs - | - | pub struct Python<'py>(PhantomData<(&'py GILGuard, NotSend)>); - | ^^^^^^ -note: required because it appears within the type `pyo3::Bound<'_, PyString>` - --> src/instance.rs - | - | pub struct Bound<'py, T>(Python<'py>, ManuallyDrop>); - | ^^^^^ - = note: required for `&pyo3::Bound<'_, PyString>` to implement `Send` -note: required because it's used within this closure - --> tests/ui/not_send2.rs:8:26 - | -8 | py.allow_threads(|| { - | ^^ - = note: required for `{closure@$DIR/tests/ui/not_send2.rs:8:26: 8:28}` to implement `Ungil` -note: required by a bound in `pyo3::Python::<'py>::allow_threads` - --> src/marker.rs - | - | pub fn allow_threads(self, f: F) -> T - | ------------- required by a bound in this associated function - | where - | F: Ungil + FnOnce() -> T, - | ^^^^^ required by this bound in `Python::<'py>::allow_threads` diff --git a/vendor/pyo3/tests/ui/pyclass_probe.rs b/vendor/pyo3/tests/ui/pyclass_probe.rs deleted file mode 100644 index 590af194f6..0000000000 --- a/vendor/pyo3/tests/ui/pyclass_probe.rs +++ /dev/null @@ -1,6 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -pub struct Probe {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/pyclass_send.rs b/vendor/pyo3/tests/ui/pyclass_send.rs deleted file mode 100644 index 5faded9874..0000000000 --- a/vendor/pyo3/tests/ui/pyclass_send.rs +++ /dev/null @@ -1,28 +0,0 @@ -use pyo3::prelude::*; -use std::os::raw::c_void; - -#[pyclass] -struct NotSyncNotSend(*mut c_void); - -#[pyclass] -struct SendNotSync(*mut c_void); -unsafe impl Send for SendNotSync {} - -#[pyclass] -struct SyncNotSend(*mut c_void); -unsafe impl Sync for SyncNotSend {} - -// None of the `unsendable` forms below should fail to compile - -#[pyclass(unsendable)] -struct NotSyncNotSendUnsendable(*mut c_void); - -#[pyclass(unsendable)] -struct SendNotSyncUnsendable(*mut c_void); -unsafe impl Send for SendNotSyncUnsendable {} - -#[pyclass(unsendable)] -struct SyncNotSendUnsendable(*mut c_void); -unsafe impl Sync for SyncNotSendUnsendable {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/pyclass_send.stderr b/vendor/pyo3/tests/ui/pyclass_send.stderr deleted file mode 100644 index 1623a5b218..0000000000 --- a/vendor/pyo3/tests/ui/pyclass_send.stderr +++ /dev/null @@ -1,121 +0,0 @@ -error[E0277]: `*mut c_void` cannot be shared between threads safely - --> tests/ui/pyclass_send.rs:5:8 - | -5 | struct NotSyncNotSend(*mut c_void); - | ^^^^^^^^^^^^^^ `*mut c_void` cannot be shared between threads safely - | - = help: within `NotSyncNotSend`, the trait `Sync` is not implemented for `*mut c_void` -note: required because it appears within the type `NotSyncNotSend` - --> tests/ui/pyclass_send.rs:5:8 - | -5 | struct NotSyncNotSend(*mut c_void); - | ^^^^^^^^^^^^^^ -note: required by a bound in `assert_pyclass_sync` - --> src/impl_/pyclass/assertions.rs - | - | pub const fn assert_pyclass_sync() - | ------------------- required by a bound in this function - | where - | T: PyClassSync + Sync, - | ^^^^ required by this bound in `assert_pyclass_sync` - -error[E0277]: `*mut c_void` cannot be sent between threads safely - --> tests/ui/pyclass_send.rs:4:1 - | -4 | #[pyclass] - | ^^^^^^^^^^ `*mut c_void` cannot be sent between threads safely - | - = help: within `NotSyncNotSend`, the trait `Send` is not implemented for `*mut c_void` - = help: the trait `pyo3::impl_::pyclass::PyClassThreadChecker` is implemented for `SendablePyClass` -note: required because it appears within the type `NotSyncNotSend` - --> tests/ui/pyclass_send.rs:5:8 - | -5 | struct NotSyncNotSend(*mut c_void); - | ^^^^^^^^^^^^^^ - = note: required for `SendablePyClass` to implement `pyo3::impl_::pyclass::PyClassThreadChecker` -note: required by a bound in `PyClassImpl::ThreadChecker` - --> src/impl_/pyclass.rs - | - | type ThreadChecker: PyClassThreadChecker; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::ThreadChecker` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0277]: `*mut c_void` cannot be shared between threads safely - --> tests/ui/pyclass_send.rs:8:8 - | -8 | struct SendNotSync(*mut c_void); - | ^^^^^^^^^^^ `*mut c_void` cannot be shared between threads safely - | - = help: within `SendNotSync`, the trait `Sync` is not implemented for `*mut c_void` -note: required because it appears within the type `SendNotSync` - --> tests/ui/pyclass_send.rs:8:8 - | -8 | struct SendNotSync(*mut c_void); - | ^^^^^^^^^^^ -note: required by a bound in `assert_pyclass_sync` - --> src/impl_/pyclass/assertions.rs - | - | pub const fn assert_pyclass_sync() - | ------------------- required by a bound in this function - | where - | T: PyClassSync + Sync, - | ^^^^ required by this bound in `assert_pyclass_sync` - -error[E0277]: `*mut c_void` cannot be sent between threads safely - --> tests/ui/pyclass_send.rs:11:1 - | -11 | #[pyclass] - | ^^^^^^^^^^ `*mut c_void` cannot be sent between threads safely - | - = help: within `SyncNotSend`, the trait `Send` is not implemented for `*mut c_void` - = help: the trait `pyo3::impl_::pyclass::PyClassThreadChecker` is implemented for `SendablePyClass` -note: required because it appears within the type `SyncNotSend` - --> tests/ui/pyclass_send.rs:12:8 - | -12 | struct SyncNotSend(*mut c_void); - | ^^^^^^^^^^^ - = note: required for `SendablePyClass` to implement `pyo3::impl_::pyclass::PyClassThreadChecker` -note: required by a bound in `PyClassImpl::ThreadChecker` - --> src/impl_/pyclass.rs - | - | type ThreadChecker: PyClassThreadChecker; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::ThreadChecker` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0277]: `*mut c_void` cannot be sent between threads safely - --> tests/ui/pyclass_send.rs:4:1 - | -4 | #[pyclass] - | ^^^^^^^^^^ `*mut c_void` cannot be sent between threads safely - | - = help: within `NotSyncNotSend`, the trait `Send` is not implemented for `*mut c_void` -note: required because it appears within the type `NotSyncNotSend` - --> tests/ui/pyclass_send.rs:5:8 - | -5 | struct NotSyncNotSend(*mut c_void); - | ^^^^^^^^^^^^^^ -note: required by a bound in `SendablePyClass` - --> src/impl_/pyclass.rs - | - | pub struct SendablePyClass(PhantomData); - | ^^^^ required by this bound in `SendablePyClass` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0277]: `*mut c_void` cannot be sent between threads safely - --> tests/ui/pyclass_send.rs:11:1 - | -11 | #[pyclass] - | ^^^^^^^^^^ `*mut c_void` cannot be sent between threads safely - | - = help: within `SyncNotSend`, the trait `Send` is not implemented for `*mut c_void` -note: required because it appears within the type `SyncNotSend` - --> tests/ui/pyclass_send.rs:12:8 - | -12 | struct SyncNotSend(*mut c_void); - | ^^^^^^^^^^^ -note: required by a bound in `SendablePyClass` - --> src/impl_/pyclass.rs - | - | pub struct SendablePyClass(PhantomData); - | ^^^^ required by this bound in `SendablePyClass` - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/pymodule_missing_docs.rs b/vendor/pyo3/tests/ui/pymodule_missing_docs.rs deleted file mode 100644 index d8bf2eb4d2..0000000000 --- a/vendor/pyo3/tests/ui/pymodule_missing_docs.rs +++ /dev/null @@ -1,16 +0,0 @@ -#![deny(missing_docs)] -//! Some crate docs - -use pyo3::prelude::*; - -/// Some module documentation -#[pymodule] -pub fn python_module(_m: &Bound<'_, PyModule>) -> PyResult<()> { - Ok(()) -} - -/// Some module documentation -#[pymodule] -pub mod declarative_python_module {} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/reject_generics.rs b/vendor/pyo3/tests/ui/reject_generics.rs deleted file mode 100644 index 55142ee0e3..0000000000 --- a/vendor/pyo3/tests/ui/reject_generics.rs +++ /dev/null @@ -1,13 +0,0 @@ -use pyo3::prelude::*; - -#[pyclass] -struct ClassWithGenerics
{ - a: A, -} - -#[pyclass] -struct ClassWithLifetimes<'a> { - a: &'a str, -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/reject_generics.stderr b/vendor/pyo3/tests/ui/reject_generics.stderr deleted file mode 100644 index fa5359eeca..0000000000 --- a/vendor/pyo3/tests/ui/reject_generics.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: #[pyclass] cannot have generic parameters. For an explanation, see https://pyo3.rs/v0.23.4/class.html#no-generic-parameters - --> tests/ui/reject_generics.rs:4:25 - | -4 | struct ClassWithGenerics { - | ^ - -error: #[pyclass] cannot have lifetime parameters. For an explanation, see https://pyo3.rs/v0.23.4/class.html#no-lifetime-parameters - --> tests/ui/reject_generics.rs:9:27 - | -9 | struct ClassWithLifetimes<'a> { - | ^^ diff --git a/vendor/pyo3/tests/ui/static_ref.rs b/vendor/pyo3/tests/ui/static_ref.rs deleted file mode 100644 index e8015db2f6..0000000000 --- a/vendor/pyo3/tests/ui/static_ref.rs +++ /dev/null @@ -1,14 +0,0 @@ -use pyo3::prelude::*; -use pyo3::types::PyList; - -#[pyfunction] -fn static_ref(list: &'static Bound<'_, PyList>) -> usize { - list.len() -} - -#[pyfunction] -fn static_py(list: &Bound<'static, PyList>) -> usize { - list.len() -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/static_ref.stderr b/vendor/pyo3/tests/ui/static_ref.stderr deleted file mode 100644 index 9fe3735598..0000000000 --- a/vendor/pyo3/tests/ui/static_ref.stderr +++ /dev/null @@ -1,47 +0,0 @@ -error: lifetime may not live long enough - --> tests/ui/static_ref.rs:4:1 - | -4 | #[pyfunction] - | ^^^^^^^^^^^^^ - | | - | lifetime `'py` defined here - | coercion requires that `'py` must outlive `'static` - | - = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0597]: `output[_]` does not live long enough - --> tests/ui/static_ref.rs:4:1 - | -4 | #[pyfunction] - | ^^^^^^^^^^^^- - | | | - | | `output[_]` dropped here while still borrowed - | borrowed value does not live long enough - | argument requires that `output[_]` is borrowed for `'static` - | - = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0597]: `holder_0` does not live long enough - --> tests/ui/static_ref.rs:5:15 - | -4 | #[pyfunction] - | ------------- - | | | - | | `holder_0` dropped here while still borrowed - | binding `holder_0` declared here -5 | fn static_ref(list: &'static Bound<'_, PyList>) -> usize { - | ^^^^^^- - | | | - | | argument requires that `holder_0` is borrowed for `'static` - | borrowed value does not live long enough - -error: lifetime may not live long enough - --> tests/ui/static_ref.rs:9:1 - | -9 | #[pyfunction] - | ^^^^^^^^^^^^^ - | | - | lifetime `'py` defined here - | coercion requires that `'py` must outlive `'static` - | - = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/vendor/pyo3/tests/ui/traverse.rs b/vendor/pyo3/tests/ui/traverse.rs deleted file mode 100644 index faa7b5c041..0000000000 --- a/vendor/pyo3/tests/ui/traverse.rs +++ /dev/null @@ -1,67 +0,0 @@ -use pyo3::prelude::*; -use pyo3::PyTraverseError; -use pyo3::PyVisit; - -#[pyclass] -struct TraverseTriesToTakePyRef {} - -#[pymethods] -impl TraverseTriesToTakePyRef { - fn __traverse__(_slf: PyRef, _visit: PyVisit) -> Result<(), PyTraverseError> { - Ok(()) - } -} - -#[pyclass] -struct TraverseTriesToTakePyRefMut {} - -#[pymethods] -impl TraverseTriesToTakePyRefMut { - fn __traverse__(_slf: PyRefMut, _visit: PyVisit) -> Result<(), PyTraverseError> { - Ok(()) - } -} - -#[pyclass] -struct TraverseTriesToTakeBound {} - -#[pymethods] -impl TraverseTriesToTakeBound { - fn __traverse__(_slf: Bound<'_, Self>, _visit: PyVisit) -> Result<(), PyTraverseError> { - Ok(()) - } -} - -#[pyclass] -struct TraverseTriesToTakeMutSelf {} - -#[pymethods] -impl TraverseTriesToTakeMutSelf { - fn __traverse__(&mut self, _visit: PyVisit) -> Result<(), PyTraverseError> { - Ok(()) - } -} - -#[pyclass] -struct TraverseTriesToTakeSelf {} - -#[pymethods] -impl TraverseTriesToTakeSelf { - fn __traverse__(&self, _visit: PyVisit) -> Result<(), PyTraverseError> { - Ok(()) - } -} - -#[pyclass] -struct Class; - -#[pymethods] -impl Class { - fn __traverse__(&self, _py: Python<'_>, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - Ok(()) - } - - fn __clear__(&mut self) {} -} - -fn main() {} diff --git a/vendor/pyo3/tests/ui/traverse.stderr b/vendor/pyo3/tests/ui/traverse.stderr deleted file mode 100644 index 504a6dfa67..0000000000 --- a/vendor/pyo3/tests/ui/traverse.stderr +++ /dev/null @@ -1,29 +0,0 @@ -error: __traverse__ may not take a receiver other than `&self`. Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic. - --> tests/ui/traverse.rs:10:27 - | -10 | fn __traverse__(_slf: PyRef, _visit: PyVisit) -> Result<(), PyTraverseError> { - | ^^^^^ - -error: __traverse__ may not take a receiver other than `&self`. Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic. - --> tests/ui/traverse.rs:20:27 - | -20 | fn __traverse__(_slf: PyRefMut, _visit: PyVisit) -> Result<(), PyTraverseError> { - | ^^^^^^^^ - -error: __traverse__ may not take a receiver other than `&self`. Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic. - --> tests/ui/traverse.rs:30:27 - | -30 | fn __traverse__(_slf: Bound<'_, Self>, _visit: PyVisit) -> Result<(), PyTraverseError> { - | ^^^^^ - -error: __traverse__ may not take a receiver other than `&self`. Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic. - --> tests/ui/traverse.rs:40:21 - | -40 | fn __traverse__(&mut self, _visit: PyVisit) -> Result<(), PyTraverseError> { - | ^ - -error: __traverse__ may not take `Python`. Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic. - --> tests/ui/traverse.rs:60:33 - | -60 | fn __traverse__(&self, _py: Python<'_>, _visit: PyVisit<'_>) -> Result<(), PyTraverseError> { - | ^^^^^^^^^^ diff --git a/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.rs b/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.rs deleted file mode 100644 index d547e9e86f..0000000000 --- a/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.rs +++ /dev/null @@ -1,10 +0,0 @@ -use pyo3::{types::PyDict, Bound, Py, Python}; - -fn main() { - let dict: Py = Python::with_gil(|py| PyDict::new(py).unbind()); - - // Should not be able to get access to Py contents outside of with_gil. - let dict: &Bound<'_, PyDict> = Python::with_gil(|py| dict.bind(py)); - - let _py: Python = dict.py(); // Obtain a Python<'p> without GIL. -} diff --git a/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.stderr b/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.stderr deleted file mode 100644 index f2f43d99f2..0000000000 --- a/vendor/pyo3/tests/ui/wrong_aspyref_lifetimes.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: lifetime may not live long enough - --> tests/ui/wrong_aspyref_lifetimes.rs:7:58 - | -7 | let dict: &Bound<'_, PyDict> = Python::with_gil(|py| dict.bind(py)); - | --- ^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` - | | | - | | return type of closure is &'2 pyo3::Bound<'_, PyDict> - | has type `Python<'1>`
- - - -