Skip to content

Commit e5469b8

Browse files
committed
fix(ooniauth-py): make extension module optional.
Today, the README gives some information for how to link the python interpreter and expects the user to find their way. This commit only enables PyO3 extension-module for maturin builds, but disable it for plain cargo builds. This way, Rust test binaries link libpython correctly. Along the way, we remove some unused configuration flags in build.rs, and add wheels/ to .gitignore.
1 parent 509a86a commit e5469b8

6 files changed

Lines changed: 28 additions & 77 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ ios/**/.DS_Store
44
ios/**/*.xcuserstate
55
ios/**/xcuserdata/
66
ios/OoniAuthBindings/OoniAuthFFI.xcframework/
7+
ooniauth-py/wheels/

ooniauth-py/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ edition = "2021"
88
name = "ooniauth_py"
99
crate-type = ["cdylib", "rlib"]
1010

11+
[features]
12+
default = []
13+
extension-module = ["pyo3/extension-module"]
14+
1115
[dependencies]
1216
cmz = { workspace = true }
1317
ooniauth-core = {path = "../ooniauth-core"}
14-
pyo3 = {version = "0.26.0", features = ["extension-module", "abi3-py310"]}
18+
pyo3 = {version = "0.26.0", features = ["abi3-py310"]}
1519
pyo3-stub-gen = "0.14.1"
1620
rand = {workspace = true}
1721
bincode = {workspace = true}

ooniauth-py/README.md

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,16 @@ the resulting Python package during development, see more details
2525

2626
### Testing installation
2727

28-
If you try to run the tests as you usually would with `cargo test`, you will get linking errors. This
29-
happens because Maturin provides a build configuration with all the linking flags required to build
30-
the library. However, it does not provide a `maturing test` command that could help you with this.
31-
32-
A possible solution is to manually specify the linking flags to the compiler, but in order to do this
33-
you will probably need to download the specific Python version (3.10). A good way to do this
34-
is using [Pyenv](https://github.com/pyenv/pyenv):
35-
36-
1. [Install Pyenv](https://github.com/pyenv/pyenv?tab=readme-ov-file#installation)
37-
2. Install Python 3.10.0 with pyenv: `pyenv install 3.10.0`
38-
39-
With the Python version installed, you can create a `.cargo/config.toml`
40-
with the linking flags. Create the file in `userauth/.cargo/config.toml`
41-
and fill the following template:
42-
43-
```toml
44-
[target.'cfg(all())']
45-
rustflags = [
46-
"-C", "link-arg=-Wl,-rpath,<YOUR PYENV PATH HERE>/.pyenv/versions/3.10.0/lib",
47-
"-C", "link-arg=-L<YOUR PYENV PATH HERE>/.pyenv/versions/3.10.0/lib",
48-
"-C", "link-arg=-lpython3.10",
49-
]
50-
```
28+
Run the Rust tests with:
5129

52-
Example result:
53-
```toml
54-
[target.'cfg(all())']
55-
rustflags = [
56-
"-C", "link-arg=-Wl,-rpath,/home/ooni/.pyenv/versions/3.10.0/lib",
57-
"-C", "link-arg=-L/home/ooni/.pyenv/versions/3.10.0/lib",
58-
"-C", "link-arg=-lpython3.10",
59-
]
30+
```bash
31+
cargo test
6032
```
6133

62-
**Note**: Make sure to create this file in `userauth/.cargo/config.toml` and not in `userauth/ooniauth-py/.cargo.toml`
34+
The Python extension-module linker mode is enabled only for Maturin builds.
35+
If you need to test against a specific Python interpreter, activate a virtualenv
36+
or set `PYO3_PYTHON` before running the tests. The virtualenv must point to a
37+
base Python installation which provides an embeddable shared library.
6338

6439
### Usage
6540

ooniauth-py/build.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,4 @@ fn main() {
1010
println!("cargo:rerun-if-env-changed=CFLAGS");
1111
println!("cargo:rerun-if-env-changed=LDFLAGS");
1212
println!("cargo:rerun-if-env-changed=RUSTFLAGS");
13-
println!("cargo:rustc-check-cfg=cfg(Py_3_10)");
14-
println!("cargo:rustc-check-cfg=cfg(Py_3_11)");
15-
println!("cargo:rustc-check-cfg=cfg(Py_3_12)");
16-
println!("cargo:rustc-check-cfg=cfg(Py_3_13)");
17-
println!("cargo:rustc-check-cfg=cfg(Py_3_14)");
18-
println!("cargo:rustc-check-cfg=cfg(Py_3_15)");
1913
}

ooniauth-py/ooniauth_py.pyi

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,40 @@ import builtins
55
import typing
66

77
__version__: builtins.str
8-
98
class CredentialError(builtins.Exception):
109
r"""
1110
An authentication error
1211
"""
13-
1412
...
1513

1614
class DeserializationFailed(builtins.Exception):
1715
r"""
1816
An error trying to deserialize a base64-encoded payload
1917
"""
20-
2118
...
2219

2320
class ProtocolError(builtins.Exception):
2421
r"""
2522
An error performing the protocol
2623
"""
27-
2824
...
2925

3026
class ServerState:
3127
def __new__(cls) -> ServerState: ...
3228
@staticmethod
33-
def from_creds(public_parameters: str, secret_key: str) -> ServerState:
29+
def from_creds(public_parameters:str, secret_key:str) -> ServerState:
3430
r"""
3531
Create a new server state from base64-encoded keys
3632
This is meant to be used by the server, so it can store the keys somewhere and recreate the
3733
state when needed
3834
"""
39-
4035
def get_secret_key(self) -> str: ...
4136
def get_public_parameters(self) -> str: ...
42-
def handle_registration_request(self, registration_request: str) -> str: ...
37+
def handle_registration_request(self, registration_request:str) -> str: ...
4338
@staticmethod
4439
def today() -> builtins.int: ...
45-
def handle_submit_request(
46-
self,
47-
nym: str,
48-
request: str,
49-
probe_cc: str,
50-
probe_asn: str,
51-
age_range: tuple[builtins.int, builtins.int],
52-
min_measurement_count: builtins.int,
53-
) -> str: ...
54-
def handle_update_request(
55-
self, req: str, old_public_params: str, old_secret_key: str
56-
) -> str: ...
40+
def handle_submit_request(self, nym:str, request:str, probe_cc:str, probe_asn:str, age_range:tuple[builtins.int, builtins.int], min_measurement_count:builtins.int) -> str: ...
41+
def handle_update_request(self, req:str, old_public_params:str, old_secret_key:str) -> str: ...
5742

5843
class SubmitRequest:
5944
@property
@@ -62,46 +47,38 @@ class SubmitRequest:
6247
def request(self) -> str: ...
6348

6449
class UserState:
65-
def __new__(cls, public_params: str) -> UserState: ...
50+
def __new__(cls, public_params:str) -> UserState: ...
6651
def get_credential(self) -> typing.Optional[str]: ...
67-
def set_public_params(self, new_public_params: str) -> None: ...
52+
def set_public_params(self, new_public_params:str) -> None: ...
6853
def make_registration_request(self) -> str: ...
69-
def handle_registration_response(self, resp: str) -> None:
54+
def handle_registration_response(self, resp:str) -> None:
7055
r"""
7156
Handle a registration response sent by the server, updating your credentials
72-
57+
7358
Note that this function will only work if you previously called
7459
`make_registration_request`
7560
"""
76-
77-
def make_submit_request(
78-
self,
79-
probe_cc: str,
80-
probe_asn: str,
81-
age_range: tuple[builtins.int, builtins.int],
82-
min_measurement_count: builtins.int,
83-
) -> SubmitRequest: ...
84-
def handle_submit_response(self, response: str) -> None:
61+
def make_submit_request(self, probe_cc:str, probe_asn:str, age_range:tuple[builtins.int, builtins.int], min_measurement_count:builtins.int) -> SubmitRequest: ...
62+
def handle_submit_response(self, response:str) -> None:
8563
r"""
8664
Handle a submit response sent by the server, updating your credentials
87-
65+
8866
Note that this function will only work if you previously called
8967
`make_submit_request`
9068
"""
91-
9269
def make_credential_update_request(self) -> str:
9370
r"""
9471
Creates a credential update request to be sent to the server.
9572
"""
96-
97-
def handle_credential_update_response(self, resp: str) -> None:
73+
def handle_credential_update_response(self, resp:str) -> None:
9874
r"""
9975
Handles the credential update response sent by the server, updating your credentials.
100-
76+
10177
This function only works if you previosly called `make_credential_update_request`
10278
"""
10379

10480
def get_protocol_version() -> builtins.str:
10581
r"""
10682
Returns the version of the `ooniauth-core`, the actual protocol implementation.
10783
"""
84+

ooniauth-py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Homepage = "https://github.com/ooni/userauth"
2121
Issues = "https://github.com/ooni/userauth/issues"
2222

2323
[tool.maturin]
24-
features = ["pyo3/extension-module"]
24+
features = ["extension-module"]

0 commit comments

Comments
 (0)