[issue43476] Enabling access to showsyntaxerror for IDLE's shell
Terry J. Reedy
report at bugs.python.org
Sun Jun 20 23:45:57 EDT 2021
Terry J. Reedy <tjreedy at udel.edu> added the comment:
What I am proposing that pseudofile <SyntaxError> have one line representing a tuple with all the exception information, *including the filename* for the code with the error. In Shell, the filename will usually be another pseudofile name, <pyshell#xx>.
The latter are set with
def stuffsource(self, source):
"Stuff source in the filename cache"
filename = "<pyshell#%d>" % self.gid
self.gid = self.gid + 1
lines = source.split("\n")
linecache.cache[filename] = len(source)+1, 0, lines, filename
return filename
I think the +1 is for a '\n' that will be appended. The linecache line is otherwise our model.
The following is how I created a line <SyntaxError> while testing.
try: compile('a b', '<pyshell#33>', 'single')
except SyntaxError as e:
err = str((type(e).__name__, e.args[0], *e.args[1]))+'\n'
err will be the single line for the file:
"('SyntaxError', 'invalid syntax. Perhaps you forgot a comma?', '<pyshell#33>', 1, 1, 'a b', 1, 4)"
For the patch, err would can be calculated a little differently further down in showsyntaxerror (which needs updating).
Then set the cache with
linecache.cache["<SyntaxError>"] = (len(err), 0, [err], "<SyntaxError>")
---
In friendly, retrieve the lines and unpack the evaluated tuple (\n at the end is ok).
exception, message, filename, line, col, text, line_end, col_end = eval(lines[0])
Use filename to retrieve the error code lines as you wish.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43476>
_______________________________________
More information about the Python-bugs-list
mailing list