pyo3 0.17.3

Bindings to Python interpreter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
name: CI

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
      - run: pip install nox
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          components: rustfmt
      - name: Check python formatting (black)
        run: nox -s fmt-py
      - name: Check rust formatting (rustfmt)
        run: nox -s fmt-rust

  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
      - run: pip install nox
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          components: clippy
      - run: nox -s clippy

  check-target:
    needs: [fmt]
    runs-on: ubuntu-latest
    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:
        target: [powerpc64le-unknown-linux-gnu, s390x-unknown-linux-gnu, wasm32-wasi]
    name: check-${{ matrix.target }}
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          profile: minimal
          default: true

      - name: Run cargo checks
        run: |
          set -x
          VERSIONS=("3.7" "3.8" "3.9" "3.10")
          for VERSION in ${VERSIONS[@]}; do
            echo "version=$VERSION" > config.txt
            echo "suppress_build_script_link_lines=true" >> config.txt
            PYO3_BUILD_CONFIG=$(pwd)/config.txt cargo check --all-targets --no-default-features
            PYO3_BUILD_CONFIG=$(pwd)/config.txt cargo check --all-targets --no-default-features --features "abi3"
            PYO3_BUILD_CONFIG=$(pwd)/config.txt cargo check --all-targets --features "full multiple-pymethods"
            PYO3_BUILD_CONFIG=$(pwd)/config.txt cargo check --all-targets --features "abi3 full multiple-pymethods"
          done

  build:
    needs: [fmt] # don't wait for clippy as fails rarely and takes longer
    name: python${{ matrix.python-version }}-${{ matrix.platform.python-architecture }} ${{ matrix.platform.os }} rust-${{ matrix.rust }}
    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:
        extra_features: ["multiple-pymethods"]  # Because MSRV doesn't support this
        rust: [stable]
        python-version: [
          "3.7",
          "3.8",
          "3.9",
          "3.10",
          "3.11",
          "pypy-3.7",
          "pypy-3.8",
          "pypy-3.9"
        ]
        platform:
          [
            {
              os: "macos-latest",
              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",
            },
          ]
        include:
          # Test minimal supported Rust version
          - rust: 1.48.0
            python-version: "3.10"
            platform:
              {
                os: "ubuntu-latest",
                python-architecture: "x64",
                rust-target: "x86_64-unknown-linux-gnu",
              }
            msrv: "MSRV"
            extra_features: ""

          # Test the `nightly` feature
          - rust: nightly
            python-version: "3.10"
            platform:
              {
                os: "ubuntu-latest",
                python-architecture: "x64",
                rust-target: "x86_64-unknown-linux-gnu",
              }
            extra_features: "nightly multiple-pymethods"

          # Test 32-bit Windows only with the latest Python version
          - rust: stable
            python-version: "3.10"
            platform:
              {
                os: "windows-latest",
                python-architecture: "x86",
                rust-target: "i686-pc-windows-msvc",
              }
            extra_features: "multiple-pymethods"
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          architecture: ${{ matrix.platform.python-architecture }}

      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          target: ${{ matrix.platform.rust-target }}
          profile: minimal
          default: true
          # needed to correctly format errors, see #1865
          components: rust-src

      - uses: Swatinem/rust-cache@v1
        with:
          key: cargo-${{ matrix.platform.python-architecture }}-${{ matrix.platform.os }}-${{ matrix.msrv }}
        continue-on-error: true

      - if: matrix.platform.os == 'ubuntu-latest'
        name: Prepare LD_LIBRARY_PATH (Ubuntu only)
        run: echo LD_LIBRARY_PATH=${pythonLocation}/lib >> $GITHUB_ENV

      - if: matrix.msrv == 'MSRV'
        name: Prepare minimal package versions (MSRV only)
        run: |
          set -x
          cargo update -p indexmap --precise 1.6.2
          cargo update -p hashbrown:0.12.3 --precise 0.9.1
          PROJECTS=("." "examples/decorator" "examples/maturin-starter" "examples/setuptools-rust-starter" "examples/word-count")
          for PROJ in ${PROJECTS[@]}; do
            cargo update --manifest-path "$PROJ/Cargo.toml" -p parking_lot --precise 0.11.0
            cargo update --manifest-path "$PROJ/Cargo.toml" -p once_cell --precise 1.14.0
          done
          cargo update -p plotters --precise 0.3.1
          cargo update -p plotters-svg --precise 0.3.1
          cargo update -p plotters-backend --precise 0.3.2
          cargo update -p bumpalo --precise 3.10.0
          cargo update -p once_cell --precise 1.14.0

      - name: Build docs
        run: cargo doc --no-deps --no-default-features --features "full ${{ matrix.extra_features }}"

      - name: Build (no features)
        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(matrix.python-version, 'pypy') }}
        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)
        run: cargo build --lib --tests --no-default-features --features "full ${{ matrix.extra_features }}"

      - if: ${{ startsWith(matrix.python-version, 'pypy') }}
        name: Build PyPy (abi3-py37)
        run: cargo build --lib --tests --no-default-features --features "abi3-py37 full ${{ matrix.extra_features }}"

      # Run tests (except on PyPy, because no embedding API).
      - if: ${{ !startsWith(matrix.python-version, 'pypy') }}
        name: Test
        run: cargo test --no-default-features --features "full ${{ matrix.extra_features }}"

      # Run tests again, but in abi3 mode
      - if: ${{ !startsWith(matrix.python-version, 'pypy') }}
        name: Test (abi3)
        run: cargo test --no-default-features --features "abi3 full ${{ matrix.extra_features }}"

      # Run tests again, for abi3-py37 (the minimal Python version)
      - if: ${{ (!startsWith(matrix.python-version, 'pypy')) && (matrix.python-version != '3.7') }}
        name: Test (abi3-py37)
        run: cargo test --no-default-features --features "abi3-py37 full ${{ matrix.extra_features }}"

      - 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: |
          python -m pip install -U pip nox
          nox -s test-py
        env:
          CARGO_TARGET_DIR: ${{ github.workspace }}/target

      - name: Test cross compilation
        if: ${{ matrix.platform.os == 'ubuntu-latest' && matrix.python-version == '3.9' }}
        uses: messense/maturin-action@v1
        env:
          PYO3_CROSS_LIB_DIR: /opt/python/cp39-cp39/lib
        with:
          target: aarch64-unknown-linux-gnu
          manylinux: auto
          args: --release -i python3.9 -m examples/maturin-starter/Cargo.toml

      - run: sudo rm -rf examples/maturin-starter/target
        if: ${{ matrix.platform.os == 'ubuntu-latest' && matrix.python-version == '3.9' }}
      - name: Test cross compile to same architecture
        if: ${{ matrix.platform.os == 'ubuntu-latest' && matrix.python-version == '3.9' }}
        uses: messense/maturin-action@v1
        env:
          PYO3_CROSS_LIB_DIR: /opt/python/cp39-cp39/lib
        with:
          target: x86_64-unknown-linux-gnu
          manylinux: auto
          args: --release -i python3.9 -m examples/maturin-starter/Cargo.toml

      - name: Test cross compilation
        if: ${{ matrix.platform.os == 'macos-latest' && matrix.python-version == '3.9' }}
        uses: messense/maturin-action@v1
        with:
          target: aarch64-apple-darwin
          args: --release -i python3.9 -m examples/maturin-starter/Cargo.toml

      - name: Test cross compile to Windows
        if: ${{ matrix.platform.os == 'ubuntu-latest' && matrix.python-version == '3.8' }}
        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
          python -m 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.9
          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
      - name: Test cross compile to Windows with maturin
        if: ${{ matrix.platform.os == 'ubuntu-latest' && matrix.python-version == '3.8' }}
        uses: messense/maturin-action@v1
        with:
          target: x86_64-pc-windows-gnu
          args: -i python3.8 -m examples/maturin-starter/Cargo.toml --features abi3

    env:
      CARGO_TERM_VERBOSE: true
      CARGO_BUILD_TARGET: ${{ matrix.platform.rust-target }}
      RUST_BACKTRACE: 1
      RUSTFLAGS: "-D warnings"
      RUSTDOCFLAGS: "-D warnings"
      # TODO: this is a hack to workaround compile_error! warnings about auto-initialize on PyPy
      # Once cargo's `resolver = "2"` is stable (~ MSRV Rust 1.52), remove this.
      PYO3_CI: 1
      # This is a hack to make CARGO_PRIMARY_PACKAGE always set even for the
      # msrv job. MSRV is currently 1.48, but CARGO_PRIMARY_PACKAGE only came in
      # 1.49.
      CARGO_PRIMARY_PACKAGE: 1

  valgrind:
    needs: [fmt]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
      - uses: Swatinem/rust-cache@v1
        with:
          key: cargo-valgrind
        continue-on-error: true
      - uses: actions-rs/toolchain@v1
        with:
          # FIXME(adamreichold): Switch to stable when Valgrind understands DWARF5 as generated by LLVM 14,
          # c.f. https://bugs.kde.org/show_bug.cgi?id=452758#c35
          toolchain: 1.61.0
          override: true
          profile: minimal
      - uses: taiki-e/install-action@valgrind
      - run: python -m pip install -U pip 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:
    needs: [fmt]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
      - uses: Swatinem/rust-cache@v1
        with:
          key: cargo-careful
        continue-on-error: true
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
          profile: minimal
          components: rust-src
      - run: cargo install cargo-careful
      - run: python -m pip install -U pip nox
      - run: nox -s test-rust -- careful skip-full
    env:
      RUST_BACKTRACE: 1
      TRYBUILD: overwrite

  coverage:
    needs: [fmt]
    name: coverage-${{ matrix.os }}
    strategy:
      matrix:
        os: ["windows", "macos", "ubuntu"]
    runs-on: ${{ matrix.os }}-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
      - uses: Swatinem/rust-cache@v1
        with:
          key: coverage-cargo-${{ matrix.os }}
        continue-on-error: true
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
          components: llvm-tools-preview
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - run: python -m pip install -U pip nox
      - run: nox -s coverage
      - uses: codecov/codecov-action@v3
        with:
          file: coverage.lcov
          name: ${{ matrix.os }}

  emscripten:
    name: emscripten
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v4
        id: setup-python
        with:
          python-version: 3.11.0-beta.4
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: wasm32-unknown-emscripten
      - uses: actions/setup-node@v3
        with:
          node-version: 14
      - run: pip install nox
      - uses: actions/cache@v3
        id: cache
        with:
          path: |
            .nox/emscripten
          key: ${{ hashFiles('emscripten/*') }} - ${{ hashFiles('noxfile.py') }} - ${{ steps.setup-python.outputs.python-path }}
      - uses: Swatinem/rust-cache@v1
        with:
          key: cargo-emscripten-wasm32
      - name: Build
        if: steps.cache.outputs.cache-hit != 'true'
        run: nox -s build-emscripten
      - name: Test
        run: nox -s test-emscripten