Skip to content

Commit 091c896

Browse files
[mypyc] feat: make frozenset literal compilation deterministic
1 parent 053c054 commit 091c896

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

mypyc/codegen/literals.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ def _encode_collection_values(
165165
for i in range(count):
166166
value = value_by_index[i]
167167
result.append(str(len(value)))
168-
for item in 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 between compilation units.
172+
sort_keys_to_values = {str(v): v for v in value}
173+
items = tuple(sort_keys_to-values[sort_key] for sort_key in sorted(sort_keys_to_values))
174+
else:
175+
items = value
176+
for item in items:
169177
assert _is_literal_value(item)
170178
index = self.literal_index(item)
171179
result.append(str(index))

0 commit comments

Comments
 (0)