@@ -141,7 +141,20 @@ def encoded_tuple_values(self) -> list[str]:
141141 return self ._encode_collection_values (self .tuple_literals )
142142
143143 def encoded_frozenset_values (self ) -> list [str ]:
144- return self ._encode_collection_values (self .frozenset_literals )
144+ def sort_frozenset_recursive (frozen : frozenset [object ]) -> list [object ]:
145+ # even though frozensets are not sorted in python, we need to sort the items here
146+ # to improve the determinism of the generated C file, making it easier to compare
147+ # differences in the C files generated by different versions of your code.
148+ sort_helper = {repr (v ) + type (v ).__name__ : v for v in value }
149+ items = []
150+ for key in sorted (sort_helper ):
151+ v = sort_helper [key ]
152+ if isinstance (v , frozenset ):
153+ v = sort_frozenset_items (v )
154+ items .append (v )
155+ return items
156+
157+ return self ._encode_collection_values ({sort_frozenset_items (fr ): i for fr , i in self .frozenset_literals .items ()})
145158
146159 def _encode_collection_values (
147160 self , values : dict [tuple [object , ...], int ] | dict [frozenset [object ], int ]
@@ -165,15 +178,7 @@ def _encode_collection_values(
165178 for i in range (count ):
166179 value = value_by_index [i ]
167180 result .append (str (len (value )))
168- if isinstance (value , frozenset ):
169- # even though frozensets are not sorted in python, we need to sort the items here
170- # to improve the determinism of the generated C file, making it easier to compare
171- # differences in the C files generated by different versions of your code.
172- sort_helper = {str (v ) + type (v ).__name__ : v for v in value }
173- items = tuple (sort_helper [key ] for key in sorted (sort_helper ))
174- else :
175- items = value
176- for item in items :
181+ for item in value :
177182 assert _is_literal_value (item )
178183 index = self .literal_index (item )
179184 result .append (str (index ))
0 commit comments