diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-05-28.gh-issue-124442.e9EOeZ.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-05-28.gh-issue-124442.e9EOeZ.rst new file mode 100644 index 000000000000000..ad9a21f25f28280 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-09-25-11-05-28.gh-issue-124442.e9EOeZ.rst @@ -0,0 +1,10 @@ +Make `compile` builtin function deterministic by sorting +`__static_attributes__` before dumping to the bytecode #.. section: Library +#.. section: Documentation #.. section: Tests #.. section: Build #.. +section: Windows #.. section: macOS #.. section: IDLE #.. section: +Tools/Demos #.. section: C API + +# Write your Misc/NEWS.d entry below. It should be a simple ReST paragraph. +# Don't start with "- Issue #: " or "- gh-issue-: " or that sort of +stuff. +########################################################################### diff --git a/Python/compile.c b/Python/compile.c index 7b3e6f336e44b18..1d63183a1e04c35 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -911,7 +911,14 @@ PyObject * _PyCompile_StaticAttributesAsTuple(compiler *c) { assert(c->u->u_static_attributes); - return PySequence_Tuple(c->u->u_static_attributes); + PyObject *static_attributes_unsorted = PySequence_List(c->u->u_static_attributes); + if (static_attributes_unsorted == NULL) { + return NULL; + } + PyList_Sort(static_attributes_unsorted); + PyObject *static_attributes = PySequence_Tuple(static_attributes_unsorted); + Py_DECREF(static_attributes_unsorted); + return static_attributes; } int