Title: IDLE should print exit message or status if one is provided
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Random832, miss-islington, terry.reedy
Priority: normal Keywords: patch

Created on 2019-05-18 19:38 by Random832, last changed 2019-05-20 07:18 by terry.reedy. This issue is now closed.

File name Uploaded Description Edit Random832, 2019-05-18 19:38
Pull Requests
URL Status Linked Edit
PR 13435 merged terry.reedy, 2019-05-20 02:05
PR 13437 merged miss-islington, 2019-05-20 03:11
Messages (5)
msg342809 - (view) Author: (Random832) Date: 2019-05-18 19:38
IDLE currently just returns to the interactive prompt when a script exits, even if SystemExit has arguments. This can be confusing to new users if they are using sys.exit to print a message.
msg342823 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-05-18 23:09 says: skip traceback, convert default None to 0, print any other non-int and convert to 1, and pass unprinted int to C exit().

I agree that IDLE should also print non-ints.  The IDLE doc should mention its non-exit behavior.

[ adds more about int return codes and that exit() only exits the process in the main thread.  Neither is relevant here.]

I believe the relevant code is the following, from run.Executive.runcode:

        except SystemExit:
            # Scripts that raise SystemExit should just
            # return to the interactive prompt
            self.usr_exc_info = sys.exc_info()
            if quitting:
            # even print a user code SystemExit exception, continue
            jit = self.rpchandler.console.getvar("<<toggle-jit-stack-viewer>>")
            if jit:

The bare except clause, including the comment, is from 2003.  The 'except SystemExit' clause was added 2013 June 11 in #18196, a follow-up of comments in #5492.  The obsoleted comment should have been deleted.  The behavior suppressed was always printing traceback + message.  What should have been retained was printing non-int messages, but I don't think that either Roger or I were aware of that behavior.

One could argue that SystemExit in user code should trigger an exit from the user execution process, which would trigger a Shell Restart.  This would be closer to the standard behavior.  But it does not hurt, and may be better, to keep the process and let the user trigger a restart when wanted.  In the case that inspired #18196, the SystemExit came from, not user code, and definitely should not cause a restart.

I will try something like the following:

        except SystemExit as e:
            ob = e.args[0]
            if not isinstance(ob, (type(None), int)):
                print('SystemExit: ' + str(ob), file=sys.stderr)

Since the message will be followed by a normal prompt rather than an exit, I want to 'enhance' the message with the prefix and error color.
msg342886 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-05-20 02:52
New changeset 6d965b39b7a486dd9e96a60b19ee92382d668299 by Terry Jan Reedy in branch 'master':
bpo-36958: In IDLE, print exit message (GH-13435)
msg342889 - (view) Author: miss-islington (miss-islington) Date: 2019-05-20 07:17
New changeset 2d94d4f1a5f54f73450d2982eab54a6301741a32 by Miss Islington (bot) in branch '3.7':
bpo-36958: In IDLE, print exit message (GH-13435)
msg342890 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-05-20 07:18
Thanks for the report.
Date User Action Args
2019-05-20 07:18:14terry.reedysetstatus: open -> closed
resolution: fixed
messages: + msg342890

stage: patch review -> resolved
2019-05-20 07:17:08miss-islingtonsetnosy: + miss-islington
messages: + msg342889
2019-05-20 03:11:58miss-islingtonsetpull_requests: + pull_request13346
2019-05-20 02:52:25terry.reedysetmessages: + msg342886
2019-05-20 02:05:41terry.reedysetstage: needs patch -> patch review
pull_requests: + pull_request13345
2019-05-18 23:09:19terry.reedysettype: behavior
stage: needs patch
messages: + msg342823
versions: + Python 3.8
2019-05-18 19:38:04Random832create