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.

Author terry.reedy
Recipients aroberge, terry.reedy
Date 2021-06-21.03:45:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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.
Date User Action Args
2021-06-21 03:45:57terry.reedysetrecipients: + terry.reedy, aroberge
2021-06-21 03:45:57terry.reedysetmessageid: <>
2021-06-21 03:45:57terry.reedylinkissue43476 messages
2021-06-21 03:45:56terry.reedycreate