This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.last_* not set for SyntaxErrors with IDLE
Type: behavior Stage: test needed
Components: IDLE Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: aroberge, terry.reedy
Priority: normal Keywords:

Created on 2021-10-21 21:31 by aroberge, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg404669 - (view) Author: Andre Roberge (aroberge) * Date: 2021-10-21 21:31
As stated in the documentation, sys.last_type, sys.last_value and sys.last_traceback ... are set when an exception is not handled and the interpreter prints an error message and a stack traceback.

This is true whether the exception is a SyntaxError or some runtime error when a standard Python interpreter is used.

However, when the IDLE shell is used and a SyntaxError occurs, these are not set.  If it is not possible to change this behaviour in IDLE, perhaps the documentation of the sys module should be amended to include this information.
msg404711 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-22 00:17
This is most likely an oversight.  It affects both normal and -n mode.  The three sys attributes can be written.  Given the error instance e caught in the IDLE process, we need to execute, *in the user namespace*,
  sys.last_type, sys.last_value, sys.traceback = type(e), e, None

The syntax error handle should call run_source (or maybe run_code).  I will make a PR if it seem to work.
msg404712 - (view) Author: Andre Roberge (aroberge) * Date: 2021-10-22 00:40
If this can be implemented, then I believe that https://bugs.python.org/issue43476 could be closed as well.
msg404713 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-22 01:16
The method to run just code in userspace, without involving the debugger or history list, is runcommand.  It needs a code string.  The hard part is the code needed to recreate the exception instance in the user space.  It will be different for SyntaxError versus the other error possible when compiling.  The SE details tuple has a mix of strs and ints.  I believe I know in principle how to do it.
msg404716 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-22 01:32
#43476 is clearly an enhancement request.  It proposes to stash info in the GUI process in a way that lets it be into from the other process by non-standard means.

This is closer to a bugfix request in that successive lines have a different response in REPL and Shell.  Having sys attributes different in the two environments is not a bug in itself: for instance, sys.stdxxx *must* be different in the two environments because the standard streams *are* different.  Having sys.last_type, for instance, being either None or wrong (the last non-syntax error) instead of SyntaxError is something different.  I fix here will be harder, but I like it better.
msg411840 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2022-01-27 05:46
If this is not fixed, perhaps it should be documented along with other IDLE differences in Running User code.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89723
2022-01-27 05:46:40terry.reedysetmessages: + msg411840
2021-10-22 01:32:00terry.reedysetmessages: + msg404716
2021-10-22 01:16:03terry.reedysetmessages: + msg404713
2021-10-22 00:40:58arobergesetmessages: + msg404712
2021-10-22 00:17:38terry.reedysettype: behavior
stage: test needed
messages: + msg404711
versions: - Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10
2021-10-21 21:31:52arobergecreate