Skip to content

Commit

Permalink
[3.13] pythongh-124442: make __static_attributes__ deterministic by…
Browse files Browse the repository at this point in the history
… sorting (pythonGH-124492)

(cherry picked from commit 04c837d)

Co-authored-by: Kira <[email protected]>
Signed-off-by: kp2pml30 <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
  • Loading branch information
3 people committed Sep 28, 2024
1 parent 80de976 commit bd71f6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import dis
import io
import itertools
import math
import opcode
import os
Expand Down Expand Up @@ -2593,6 +2594,22 @@ def test_nested(self):
self.compare_instructions(seq, [('LOAD_CONST', 1, 1, 0, 0, 0)])
self.compare_instructions(seq.get_nested()[0], [('LOAD_CONST', 2, 2, 0, 0, 0)])

def test_static_attributes_are_sorted(self):
code = (
'class T:\n'
' def __init__(self):\n'
' self.{V1} = 10\n'
' self.{V2} = 10\n'
' def foo(self):\n'
' self.{V3} = 10\n'
)
attributes = ("a", "b", "c")
for perm in itertools.permutations(attributes):
var_names = {f'V{i + 1}': name for i, name in enumerate(perm)}
ns = run_code(code.format(**var_names))
t = ns['T']
self.assertEqual(t.__static_attributes__, attributes)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix nondeterminism in compilation by sorting the value of
:attr:`~type.__static_attributes__`. Patch by kp2pml30.
11 changes: 10 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,16 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
return ERROR;
}
assert(c->u->u_static_attributes);
PyObject *static_attributes = PySequence_Tuple(c->u->u_static_attributes);
PyObject *static_attributes_unsorted = PySequence_List(c->u->u_static_attributes);
if (static_attributes_unsorted == NULL) {
return ERROR;
}
if (PyList_Sort(static_attributes_unsorted) != 0) {
Py_DECREF(static_attributes_unsorted);
return ERROR;
}
PyObject *static_attributes = PySequence_Tuple(static_attributes_unsorted);
Py_DECREF(static_attributes_unsorted);
if (static_attributes == NULL) {
compiler_exit_scope(c);
return ERROR;
Expand Down

0 comments on commit bd71f6e

Please sign in to comment.