Fix incorrect python signature of gensalt (#1031)

It seems that pyo3 does not support specifying a python `bytes` as default
value: the `signature` attribute accepts rust literals but interprets them as
python values; `b"2b"` is interpreted as a rust `[u8, 2]` and not a python
`bytes` object.

To work around this, the `prefix` argument is handled in code, and the python
signature is overridden via pyo3's `text_signature` attribute, which makes the
desired signature visible to python tooling (such as `inspect.signature`).

Fixes #1030
This commit is contained in:
crazygolem 2025-06-05 00:50:42 +02:00 committed by GitHub
parent 6e297f29dd
commit b00ecf8dcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,13 +26,12 @@ pub const BASE64_ENGINE: base64::engine::GeneralPurpose = base64::engine::Genera
);
#[pyo3::pyfunction]
#[pyo3(signature = (rounds=None, prefix=None))]
#[pyo3(signature = (rounds=12, prefix=None), text_signature = "(rounds=12, prefix=b'2b')")]
fn gensalt<'p>(
py: pyo3::Python<'p>,
rounds: Option<u16>,
rounds: u16,
prefix: Option<&[u8]>,
) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
let rounds = rounds.unwrap_or(12);
let prefix = prefix.unwrap_or(b"2b");
if prefix != b"2a" && prefix != b"2b" {