Skip to content

Commit 2e32e7a

Browse files
committed
pyo3 bindings
Advantages ========== - Can do the entire loading in one shot in pure rust from a python iterable. - Work using rust semantics. - Really just works. - Only requires a pyi for type declarations (?). Drawbacks ========= - Likely slower than cffi for pypy, but unlikely to be slower than the slog of pure python. - Graal don't work in tox.
1 parent 40e44d1 commit 2e32e7a

File tree

14 files changed

+769
-9
lines changed

14 files changed

+769
-9
lines changed

.github/workflows/pychecks.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: py checks
2+
3+
on: [pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
checks:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout working copy
13+
uses: actions/checkout@v4
14+
- name: ruff check
15+
uses: chartboost/ruff-action@v1
16+
with:
17+
args: check
18+
- name: ruff format
19+
if: always()
20+
uses: chartboost/ruff-action@v1
21+
with:
22+
args: format --diff
23+
- name: Set up Python
24+
id: setup_python
25+
if: always()
26+
uses: actions/setup-python@v5
27+
with:
28+
python-version: "3.x"
29+
- name: Install mypy
30+
id: install_mypy
31+
if: ${{ always() && steps.setup_python.conclusion == 'success' }}
32+
run: |
33+
python -mpip install --upgrade pip
34+
python -mpip install mypy pytest types-PyYaml
35+
- name: mypy
36+
if: ${{ always() && steps.install_mypy.conclusion == 'success' }}
37+
run: mypy --strict .

.github/workflows/pyo3-wheels.yml

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# This file is autogenerated by maturin v1.7.4
2+
# To update, run
3+
#
4+
# maturin generate-ci --zig github
5+
#
6+
name: build wheels
7+
8+
on:
9+
push:
10+
branches:
11+
- main
12+
- master
13+
tags:
14+
- '*'
15+
pull_request:
16+
workflow_dispatch:
17+
18+
permissions:
19+
contents: read
20+
21+
jobs:
22+
linux:
23+
runs-on: ${{ matrix.platform.runner }}
24+
strategy:
25+
matrix:
26+
platform:
27+
- runner: ubuntu-latest
28+
target: x86_64
29+
- runner: ubuntu-latest
30+
target: x86
31+
- runner: ubuntu-latest
32+
target: aarch64
33+
- runner: ubuntu-latest
34+
target: armv7
35+
steps:
36+
- uses: actions/checkout@v4
37+
- uses: actions/setup-python@v5
38+
with:
39+
python-version: 3.x
40+
- name: Build wheels
41+
uses: PyO3/maturin-action@v1
42+
with:
43+
target: ${{ matrix.platform.target }}
44+
args: --release --out dist --zig
45+
sccache: 'true'
46+
manylinux: auto
47+
- name: Upload wheels
48+
uses: actions/upload-artifact@v4
49+
with:
50+
name: wheels-linux-${{ matrix.platform.target }}
51+
path: dist
52+
53+
musllinux:
54+
runs-on: ${{ matrix.platform.runner }}
55+
strategy:
56+
matrix:
57+
platform:
58+
- runner: ubuntu-latest
59+
target: x86_64
60+
- runner: ubuntu-latest
61+
target: x86
62+
- runner: ubuntu-latest
63+
target: aarch64
64+
- runner: ubuntu-latest
65+
target: armv7
66+
steps:
67+
- uses: actions/checkout@v4
68+
- uses: actions/setup-python@v5
69+
with:
70+
python-version: 3.x
71+
- name: Build wheels
72+
uses: PyO3/maturin-action@v1
73+
with:
74+
target: ${{ matrix.platform.target }}
75+
args: --release --out dist
76+
sccache: 'true'
77+
manylinux: musllinux_1_2
78+
- name: Upload wheels
79+
uses: actions/upload-artifact@v4
80+
with:
81+
name: wheels-musllinux-${{ matrix.platform.target }}
82+
path: dist
83+
84+
windows:
85+
runs-on: ${{ matrix.platform.runner }}
86+
strategy:
87+
matrix:
88+
platform:
89+
- runner: windows-latest
90+
target: x64
91+
- runner: windows-latest
92+
target: x86
93+
steps:
94+
- uses: actions/checkout@v4
95+
- uses: actions/setup-python@v5
96+
with:
97+
python-version: 3.x
98+
architecture: ${{ matrix.platform.target }}
99+
- name: Build wheels
100+
uses: PyO3/maturin-action@v1
101+
with:
102+
target: ${{ matrix.platform.target }}
103+
args: --release --out dist
104+
sccache: 'true'
105+
- name: Upload wheels
106+
uses: actions/upload-artifact@v4
107+
with:
108+
name: wheels-windows-${{ matrix.platform.target }}
109+
path: dist
110+
111+
macos:
112+
runs-on: ${{ matrix.platform.runner }}
113+
strategy:
114+
matrix:
115+
platform:
116+
- runner: macos-12
117+
target: x86_64
118+
- runner: macos-14
119+
target: aarch64
120+
steps:
121+
- uses: actions/checkout@v4
122+
- uses: actions/setup-python@v5
123+
with:
124+
python-version: 3.x
125+
- name: Build wheels
126+
uses: PyO3/maturin-action@v1
127+
with:
128+
target: ${{ matrix.platform.target }}
129+
args: --release --out dist
130+
sccache: 'true'
131+
- name: Upload wheels
132+
uses: actions/upload-artifact@v4
133+
with:
134+
name: wheels-macos-${{ matrix.platform.target }}
135+
path: dist
136+
137+
sdist:
138+
runs-on: ubuntu-latest
139+
steps:
140+
- uses: actions/checkout@v4
141+
- name: Build sdist
142+
uses: PyO3/maturin-action@v1
143+
with:
144+
command: sdist
145+
args: --out dist
146+
- name: Upload sdist
147+
uses: actions/upload-artifact@v4
148+
with:
149+
name: wheels-sdist
150+
path: dist
151+
152+
release:
153+
name: Release
154+
runs-on: ubuntu-latest
155+
if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }}
156+
needs: [linux, musllinux, windows, macos, sdist]
157+
permissions:
158+
# Use to sign the release artifacts
159+
id-token: write
160+
# Used to upload release artifacts
161+
contents: write
162+
# Used to generate artifact attestation
163+
attestations: write
164+
steps:
165+
- uses: actions/download-artifact@v4
166+
- name: Generate artifact attestation
167+
uses: actions/attest-build-provenance@v1
168+
with:
169+
subject-path: 'wheels-*/*'
170+
- name: Publish to PyPI
171+
if: "startsWith(github.ref, 'refs/tags/')"
172+
uses: PyO3/maturin-action@v1
173+
env:
174+
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
175+
with:
176+
command: upload
177+
args: --non-interactive --skip-existing wheels-*/*

.github/workflows/pytests.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: py tests
2+
3+
on: [pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
tests:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
python-version:
15+
- "3.8"
16+
- "3.9"
17+
- "3.10"
18+
- "3.11"
19+
- "3.12"
20+
- "3.13"
21+
- "pypy-3.8"
22+
- "pypy-3.9"
23+
- "pypy-3.10"
24+
- "graalpy-24"
25+
steps:
26+
- name: Checkout working copy
27+
uses: actions/checkout@v4
28+
with:
29+
submodules: true
30+
- name: Set up Python ${{ matrix.python-version }}
31+
uses: actions/setup-python@v5
32+
with:
33+
python-version: ${{ matrix.python-version }}
34+
allow-prereleases: true
35+
- name: Install test dependencies
36+
run: |
37+
pwd
38+
python -mpip install --upgrade pip
39+
# cyaml is outright broken on pypy
40+
if ! ${{ startsWith(matrix.python-version, 'pypy-') }}; then
41+
# if binary wheels are not available for the current
42+
# package install libyaml-dev so we can install pyyaml
43+
# from source
44+
if ! pip download --only-binary pyyaml > /dev/null 2>&1; then
45+
sudo apt install libyaml-dev
46+
fi
47+
fi
48+
python -mpip install pytest pyyaml
49+
- name: install package
50+
run: pip install ./ua-parser-py
51+
- name: run tests
52+
run: pytest -v -Werror -ra ./ua-parser-py

.github/workflows/rust.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
name: Rust
22

3-
on:
4-
push:
5-
branches: [ "main" ]
6-
pull_request:
7-
branches: [ "main" ]
3+
on: [push, pull_request]
84

95
env:
106
CARGO_TERM_COLOR: always
117

128
jobs:
139
checks:
14-
1510
runs-on: ubuntu-latest
1611

1712
steps:
1813
- uses: actions/checkout@v4
1914
with:
2015
submodules: true
21-
- name: Build
22-
run: cargo build --verbose
16+
- name: Check
17+
run: cargo check
2318
- name: Format
2419
run: cargo fmt --check
2520
- name: clippy

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Cargo.lock
44
*.dSYM/
55
regex-filtered/re2/flake.lock
66
regex-filtered/re2/bench
7+
.tox/

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["regex-filtered", "ua-parser"]
2+
members = ["regex-filtered", "ua-parser", "ua-parser-py"]
33
resolver = "2"
44

55
[profile.release]

ua-parser-py/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "ua_parser_rs"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
name = "ua_parser_rs"
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
pyo3 = { version = "0.20.0", features = ["extension-module", "abi3", "abi3-py38"] }
13+
ua-parser = { version = "0.2.0", path = "../ua-parser" }

ua-parser-py/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[build-system]
2+
requires = ["maturin>=1.5,<2.0"]
3+
build-backend = "maturin"
4+
5+
[project]
6+
name = "ua_parser_rs"
7+
requires-python = ">=3.8"
8+
classifiers = [
9+
"Programming Language :: Rust",
10+
"Programming Language :: Python :: Implementation :: CPython",
11+
"Programming Language :: Python :: Implementation :: PyPy",
12+
]
13+
dynamic = ["version"]
14+
[tool.maturin]
15+
features = ["pyo3/extension-module"]

0 commit comments

Comments
 (0)