| 1 |
commit ac1c36584abcfe71b92fb7eb2b74c039d591b552 |
| 2 |
Author: Jelmer Vernooij <[email protected]> |
| 3 |
Date: Tue May 26 22:24:35 2026 +0100 |
| 4 |
|
| 5 |
fix: make introspection JSON output deterministic |
| 6 |
|
| 7 |
The introspection proc macros (used by the `experimental-inspect` |
| 8 |
feature) accumulated each JSON object's fields in a `HashMap` before |
| 9 |
serializing them in iteration order. Because `std`'s default hasher is |
| 10 |
seeded randomly per process, the emitted JSON key order varied between |
| 11 |
otherwise identical builds, leaving the embedded introspection chunks |
| 12 |
non-reproducible. |
| 13 |
|
| 14 |
Switch to `BTreeMap` so the keys come out in a stable (alphabetical) |
| 15 |
order. JSON objects are unordered by spec, so downstream parsers are |
| 16 |
unaffected. |
| 17 |
|
| 18 |
Caught via Debian's reproducible-builds rebuild of pysequoia 0.1.34-2, |
| 19 |
which fails with diffs like: |
| 20 |
{"name","parent","type","annotation"} |
| 21 |
vs |
| 22 |
{"annotation","parent","type","name"} |
| 23 |
|
| 24 |
index aad7eb1f2..c6b89b192 100644 |
| 25 |
Index: pyo3-macros-backend/src/introspection.rs |
| 26 |
=================================================================== |
| 27 |
--- pyo3-macros-backend.orig/src/introspection.rs |
| 28 |
+++ pyo3-macros-backend/src/introspection.rs |
| 29 |
@@ -16,7 +16,7 @@ use proc_macro2::{Span, TokenStream}; |
| 30 |
use quote::{format_ident, quote, ToTokens}; |
| 31 |
use std::borrow::Cow; |
| 32 |
use std::collections::hash_map::DefaultHasher; |
| 33 |
-use std::collections::HashMap; |
| 34 |
+use std::collections::BTreeMap; |
| 35 |
use std::hash::{Hash, Hasher}; |
| 36 |
use std::mem::take; |
| 37 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 38 |
@@ -64,7 +64,7 @@ pub fn class_introspection_code( |
| 39 |
is_final: bool, |
| 40 |
parent: Option<&Type>, |
| 41 |
) -> TokenStream { |
| 42 |
- let mut desc = HashMap::from([ |
| 43 |
+ let mut desc = BTreeMap::from([ |
| 44 |
("type", IntrospectionNode::String("class".into())), |
| 45 |
( |
| 46 |
"id", |
| 47 |
@@ -102,7 +102,7 @@ pub fn function_introspection_code( |
| 48 |
is_async: bool, |
| 49 |
parent: Option<&Type>, |
| 50 |
) -> TokenStream { |
| 51 |
- let mut desc = HashMap::from([ |
| 52 |
+ let mut desc = BTreeMap::from([ |
| 53 |
("type", IntrospectionNode::String("function".into())), |
| 54 |
("name", IntrospectionNode::String(name.into())), |
| 55 |
( |
| 56 |
@@ -156,7 +156,7 @@ pub fn attribute_introspection_code( |
| 57 |
rust_type: Type, |
| 58 |
is_final: bool, |
| 59 |
) -> TokenStream { |
| 60 |
- let mut desc = HashMap::from([ |
| 61 |
+ let mut desc = BTreeMap::from([ |
| 62 |
("type", IntrospectionNode::String("attribute".into())), |
| 63 |
("name", IntrospectionNode::String(name.into())), |
| 64 |
( |
| 65 |
@@ -246,7 +246,7 @@ fn arguments_introspection_data<'a>( |
| 66 |
let Some(FnArg::VarArgs(arg_desc)) = argument_desc.next() else { |
| 67 |
panic!("Fewer arguments than in python signature"); |
| 68 |
}; |
| 69 |
- let mut params = HashMap::from([("name", IntrospectionNode::String(param.into()))]); |
| 70 |
+ let mut params = BTreeMap::from([("name", IntrospectionNode::String(param.into()))]); |
| 71 |
if let Some(annotation) = &arg_desc.annotation { |
| 72 |
params.insert("annotation", annotation.clone().into()); |
| 73 |
} |
| 74 |
@@ -264,14 +264,14 @@ fn arguments_introspection_data<'a>( |
| 75 |
let Some(FnArg::KwArgs(arg_desc)) = argument_desc.next() else { |
| 76 |
panic!("Less arguments than in python signature"); |
| 77 |
}; |
| 78 |
- let mut params = HashMap::from([("name", IntrospectionNode::String(param.into()))]); |
| 79 |
+ let mut params = BTreeMap::from([("name", IntrospectionNode::String(param.into()))]); |
| 80 |
if let Some(annotation) = &arg_desc.annotation { |
| 81 |
params.insert("annotation", annotation.clone().into()); |
| 82 |
} |
| 83 |
kwarg = Some(IntrospectionNode::Map(params)); |
| 84 |
} |
| 85 |
|
| 86 |
- let mut map = HashMap::new(); |
| 87 |
+ let mut map = BTreeMap::new(); |
| 88 |
if !posonlyargs.is_empty() { |
| 89 |
map.insert("posonlyargs", IntrospectionNode::List(posonlyargs)); |
| 90 |
} |
| 91 |
@@ -295,7 +295,7 @@ fn argument_introspection_data<'a>( |
| 92 |
desc: &'a RegularArg<'_>, |
| 93 |
class_type: Option<&Type>, |
| 94 |
) -> AttributedIntrospectionNode<'a> { |
| 95 |
- let mut params: HashMap<_, _> = [("name", IntrospectionNode::String(name.into()))].into(); |
| 96 |
+ let mut params: BTreeMap<_, _> = [("name", IntrospectionNode::String(name.into()))].into(); |
| 97 |
if let Some(expr) = &desc.default_value { |
| 98 |
params.insert("default", PyExpr::constant_from_expression(expr).into()); |
| 99 |
} |
| 100 |
@@ -317,7 +317,7 @@ enum IntrospectionNode<'a> { |
| 101 |
Bool(bool), |
| 102 |
IntrospectionId(Option<Cow<'a, Type>>), |
| 103 |
TypeHint(Cow<'a, PyExpr>), |
| 104 |
- Map(HashMap<&'static str, IntrospectionNode<'a>>), |
| 105 |
+ Map(BTreeMap<&'static str, IntrospectionNode<'a>>), |
| 106 |
List(Vec<AttributedIntrospectionNode<'a>>), |
| 107 |
} |
| 108 |
|