From bd71f6e173c39710a2e5c36b51197198deec2b88 Mon Sep 17 00:00:00 2001 From: Kira Date: Sun, 29 Sep 2024 01:15:43 +0300 Subject: [PATCH 1/2] [3.13] gh-124442: make `__static_attributes__` deterministic by sorting (GH-124492) (cherry picked from commit 04c837d9d8a474777ef9c1412fbba14f0682366c) Co-authored-by: Kira Signed-off-by: kp2pml30 Co-authored-by: Jelle Zijlstra Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/test/test_compile.py | 17 +++++++++++++++++ ...24-09-25-11-53-22.gh-issue-124442.EXC1Ve.rst | 2 ++ Python/compile.c | 11 ++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-53-22.gh-issue-124442.EXC1Ve.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index b7e4d847822571..9d8c2bb74cdd00 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1,6 +1,7 @@ import contextlib import dis import io +import itertools import math import opcode import os @@ -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() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-53-22.gh-issue-124442.EXC1Ve.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-53-22.gh-issue-124442.EXC1Ve.rst new file mode 100644 index 00000000000000..58e79f22ac0f90 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-53-22.gh-issue-124442.EXC1Ve.rst @@ -0,0 +1,2 @@ +Fix nondeterminism in compilation by sorting the value of +:attr:`~type.__static_attributes__`. Patch by kp2pml30. diff --git a/Python/compile.c b/Python/compile.c index 7752a6885cc4e2..78d107ea198e0e 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; From c2ae6e3ca26c88ea43ce19bce5bc5d2e3d2b14c0 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 30 Sep 2024 08:06:52 -0700 Subject: [PATCH 2/2] Apply suggestions from code review --- Python/compile.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/compile.c b/Python/compile.c index 78d107ea198e0e..7d93f2a05f66d9 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2546,9 +2546,11 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno) assert(c->u->u_static_attributes); PyObject *static_attributes_unsorted = PySequence_List(c->u->u_static_attributes); if (static_attributes_unsorted == NULL) { + compiler_exit_scope(c); return ERROR; } if (PyList_Sort(static_attributes_unsorted) != 0) { + compiler_exit_scope(c); Py_DECREF(static_attributes_unsorted); return ERROR; }