python / python/cpython

IDLE - there is no known way to trigger atexit functions

Open
#98,318 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-IDLE type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Hello!

So this has been an issue that has probably been bugging many people for a while now. This has bitten me back when I was a newbie as well, learning python using IDLE. This issue does not personally affect me anymore since I've mostly moved on from IDLE, but for the sake of others new to the language, I would like to formalise this issue on the tracker.

Here is a pure python MRE that demonstrates what I'm saying

import atexit
import time

@atexit.register
def done():
    print("Exiting application!")

time.sleep(1)

When I run this on the command-line with no flags, I see the text printed after the program runs for a second and exits. On IDLE, this just runs for a second and exits without printing anything.

So I later realised that IDLE runs programs with the -i flag. But on the commandline I can do a ctrl+z and get the interpreter to actually stop and handle atexit. This is not apparent in IDLE shell, where there does not seem to be any distinction between the "regular interactive state", and the "previous module run interactive state". And neither does ctrl+c/z/d work. A hard shell restart also does not lead to anything being printed.

I've glanced through the issue tracker with a quick search and I didn't find a duplicate of this issue, but incase I'm mistaken, my apologies; this issue can be closed.

Your environment

  • CPython versions tested on: 3.10 (but from my memory, it happened on all previous versions too)
  • Operating system and architecture: Windows 11 64-bit (In my understanding it also happens on other architectures and OSes)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied pure-Python atexit reproducer in IDLE and from the command line, then compare the IDLE shell's regular and post-module-run states. Done means IDLE provides a clear way to reach the interpreter state that runs registered atexit functions, with the reported output appearing after the module exits.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.