[issue40403] pdb does not drop into debugger upon SyntaxError caused by ast.literal_eval

Rémi Lapeyre report at bugs.python.org
Mon Jun 1 14:34:50 EDT 2020


Rémi Lapeyre <remi.lapeyre at henki.fr> added the comment:

I've looked into this, in Bdb both the part where the code is compiled and the one where the code is run are in the run() method (https://github.com/python/cpython/blob/master/Lib/bdb.py#L565-L585):


    def run(self, cmd, globals=None, locals=None):
        """Debug a statement executed via the exec() function.
        globals defaults to __main__.dict; locals defaults to globals.
        """
        if globals is None:
            import __main__
            globals = __main__.__dict__
        if locals is None:
            locals = globals
        self.reset()
        if isinstance(cmd, str):
            cmd = compile(cmd, "<string>", "exec")
        sys.settrace(self.trace_dispatch)
        try:
            exec(cmd, globals, locals)
        except BdbQuit:
            pass
        finally:
            self.quitting = True
            sys.settrace(None)


This is an issue as SyntaxError may come from two lines

 - the call to compile() which means the code being run is not valid Python, in this case the current behaviour of PDB to exit is correct as there is nothing to debug
 - the call to exec() in which case a SyntaxError can happen like in the report, and PDB should go in post mortem debug mode.


One way to fix the issue would be to catch the error in compile() and wrap it in a BdbSyntaxError so that PDB can differentiate between the two, another to keep BDB as it is and change PDB so that it compiles the code first, and call run() in a second step. I think the last one is better and will start writing a PR for this.

----------
nosy: +remi.lapeyre
versions: +Python 3.10, Python 3.7, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40403>
_______________________________________


More information about the Python-bugs-list mailing list