Skip to content

Commit d888c7e

Browse files
authored
Adjust atexit callback order for consistency
Reversed the order of callbacks in atexit to maintain FIFO consistency with earlier versions, while noting changes in Python 3.14.
1 parent 21a96fe commit d888c7e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/sage/cpython/atexit.pyx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ def _get_exithandlers():
220220
return exithandlers
221221
# callbacks is a list of tuples: [(func, args, kwargs), ...]
222222
# Normalize kwargs to ensure it's always a dict (not None)
223-
for item in callbacks_list:
223+
# Note: In Python 3.14+, atexit stores callbacks in LIFO order
224+
# (most recently registered first), but we return them in FIFO
225+
# order (registration order) for consistency with earlier versions
226+
for item in reversed(callbacks_list):
224227
func, args, kwargs = item
225228
if kwargs is None:
226229
kwargs = {}
@@ -251,6 +254,9 @@ def _set_exithandlers(exithandlers):
251254

252255
# We could do this more efficiently by directly rebuilding the array
253256
# of atexit_callbacks, but this is much simpler
257+
# Note: exithandlers is in registration order (FIFO).
258+
# In Python 3.14+, atexit.register prepends to the list (LIFO),
259+
# so registering in forward order gives us the correct execution order.
254260
for callback in exithandlers:
255261
atexit.register(callback[0], *callback[1], **callback[2])
256262

0 commit comments

Comments
 (0)