From b00ecf8dcf786edf1b1dcf539da33e59d0c19288 Mon Sep 17 00:00:00 2001 From: crazygolem Date: Thu, 5 Jun 2025 00:50:42 +0200 Subject: [PATCH] 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 --- src/_bcrypt/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/_bcrypt/src/lib.rs b/src/_bcrypt/src/lib.rs index 11cdba3..a32797d 100644 --- a/src/_bcrypt/src/lib.rs +++ b/src/_bcrypt/src/lib.rs @@ -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, + rounds: u16, prefix: Option<&[u8]>, ) -> pyo3::PyResult> { - let rounds = rounds.unwrap_or(12); let prefix = prefix.unwrap_or(b"2b"); if prefix != b"2a" && prefix != b"2b" {