You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pdb does not drop into debugger upon SyntaxError caused by ast.literal_eval #84583
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2020-04-27.03:07:35.981>labels= ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library']
title='pdb does not drop into debugger upon SyntaxError caused by ast.literal_eval'updated_at=<Date2020-06-03.09:03:33.251>user='https://bugs.python.org/KerrickStaley'
Summary:
When you call ast.literal_eval on a string that does not contain valid Python code, it raises a SyntaxError. This causes pdb to exit instead of stopping execution at the point that the SyntaxError was raised.
To reproduce:
Create a Python file foo.py with these contents:
importastast.literal_eval('')
Run python -m pdb foo.py
Type 'c' and hit enter.
Expected behavior:
pdb drops into a shell in ast.py at the point where the SyntaxError occurred.
Actual behavior:
The program exits, and a SyntaxError traceback is displayed.
defrun(self, cmd, globals=None, locals=None):
"""Debug a statement executed via the exec() function. globals defaults to __main__.dict; locals defaults to globals. """ifglobalsisNone:
import__main__globals=__main__.__dict__iflocalsisNone:
locals=globalsself.reset()
ifisinstance(cmd, str):
cmd=compile(cmd, "<string>", "exec")
sys.settrace(self.trace_dispatch)
try:
exec(cmd, globals, locals)
exceptBdbQuit:
passfinally:
self.quitting=Truesys.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.
This is related to bpo-16180, it may be possible to improve the situation by trying to determine whether the SyntaxError is in the file or came during its execution by looking at the filename but it's probably very brittle:
# In most cases SystemExit does not warrant a post-mortem session.
print("The program exited via sys.exit(). Exit status:", end=' ')
print(sys.exc_info()[1])
- except SyntaxError:- traceback.print_exc()- sys.exit(1)- except:+ except Exception as e:+ if (type(e) is SyntaxError and+ e.filename == os.path.abspath(mainpyfile)):+ traceback.print_exc()+ sys.exit(1)
traceback.print_exc()
print("Uncaught exception. Entering post mortem debugging")
print("Running 'cont' or 'step' will restart the program")
xdegaye commented on Jun 2, 2020
xdegayemannequin
Mannequin
More actions
In Kerrick's example ast.literal_eval('') could be ast.literal_eval(some_code) instead where some_code is a string containing dynamically generated Python code. pdb post-mortem debugging must allow finding the syntax error in this code. The patch proposed in bpo-16180 by Terry may fix this problem (and bpo-16180).
remilapeyre commented on Jun 2, 2020
remilapeyremannequin
Mannequin
More actions
Yes, the patch by Terry Reedy fixes this issue while still breaking the loop from def f: pass.
It will start the debugger once for def f: pass which may be weird as in this case no user code has been executed and it will be in bdb which may confuse users:
Traceback (most recent call last):
File "/Users/remi/src/cpython/Lib/pdb.py", line 1703, in main
pdb._runscript(mainpyfile)
File "/Users/remi/src/cpython/Lib/pdb.py", line 1572, in _runscriptself.run(statement)
File "/Users/remi/src/cpython/Lib/bdb.py", line 580, in runexec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/Users/remi/src/cpython/tests.py", line 1def f: pass^SyntaxError: invalid syntax
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> <string>(1)<module>()
(Pdb) bt
/Users/remi/src/cpython/Lib/pdb.py(1703)main()
-> pdb._runscript(mainpyfile)
/Users/remi/src/cpython/Lib/pdb.py(1572)_runscript()
-> self.run(statement)
/Users/remi/src/cpython/Lib/bdb.py(580)run()
-> exec(cmd, globals, locals)
> <string>(1)<module>()
Perhaps we should should test whether the exception happened there and not drop in the debugger in that case?
xdegaye commented on Jun 3, 2020
xdegayemannequin
Mannequin
More actions
Perhaps we should should test whether the exception happened there and not drop in the debugger in that case?
The same kind of problem occurs for any post-mortem debugging raised by an uncaught exception in the user code: the backtrace displayed by the 'bt' command shows frames that are owned by the pdb and bdb modules; and worse, the 'up' command allows to move to these frames. See for example below what happens when debugging foo.py that contains only the line "1/0". IMO this problem should be handled in another issue and in that case, in your example, 'bt' output would be empty.
$ python -m pdb foo.py
> /tmp/foo.py(1)<module>()
-> 1/0
(Pdb) c
Traceback (most recent call last):
File "/usr/lib/python3.8/pdb.py", line 1703, in main
pdb._runscript(mainpyfile)
File "/usr/lib/python3.8/pdb.py", line 1572, in _runscriptself.run(statement)
File "/usr/lib/python3.8/bdb.py", line 580, in runexec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/tmp/foo.py", line 1, in <module>1/0ZeroDivisionError: division by zero
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /tmp/foo.py(1)<module>()
-> 1/0
(Pdb) bt
/usr/lib/python3.8/pdb.py(1703)main()
-> pdb._runscript(mainpyfile)
/usr/lib/python3.8/pdb.py(1572)_runscript()
-> self.run(statement)
/usr/lib/python3.8/bdb.py(580)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
> /tmp/foo.py(1)<module>()
-> 1/0
(Pdb)
Activity
KerrickStaley commentedon Apr 27, 2020
Summary:
When you call ast.literal_eval on a string that does not contain valid Python code, it raises a SyntaxError. This causes pdb to exit instead of stopping execution at the point that the SyntaxError was raised.
To reproduce:
Expected behavior:
pdb drops into a shell in ast.py at the point where the SyntaxError occurred.
Actual behavior:
The program exits, and a SyntaxError traceback is displayed.
System configuration:
Python 3.8.2 on Arch Linux.
remilapeyre commentedon Jun 1, 2020
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):
This is an issue as SyntaxError may come from two lines
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.
remilapeyre commentedon Jun 1, 2020
This is related to bpo-16180, it may be possible to improve the situation by trying to determine whether the SyntaxError is in the file or came during its execution by looking at the filename but it's probably very brittle:
# In most cases SystemExit does not warrant a post-mortem session. print("The program exited via sys.exit(). Exit status:", end=' ') print(sys.exc_info()[1]) - except SyntaxError: - traceback.print_exc() - sys.exit(1) - except: + except Exception as e: + if (type(e) is SyntaxError and + e.filename == os.path.abspath(mainpyfile)): + traceback.print_exc() + sys.exit(1) traceback.print_exc() print("Uncaught exception. Entering post mortem debugging") print("Running 'cont' or 'step' will restart the program")xdegaye commentedon Jun 2, 2020
In Kerrick's example ast.literal_eval('') could be ast.literal_eval(some_code) instead where some_code is a string containing dynamically generated Python code. pdb post-mortem debugging must allow finding the syntax error in this code. The patch proposed in bpo-16180 by Terry may fix this problem (and bpo-16180).
remilapeyre commentedon Jun 2, 2020
Yes, the patch by Terry Reedy fixes this issue while still breaking the loop from
def f: pass.It will start the debugger once for
def f: passwhich may be weird as in this case no user code has been executed and it will be in bdb which may confuse users:Perhaps we should should test whether the exception happened there and not drop in the debugger in that case?
xdegaye commentedon Jun 3, 2020
The same kind of problem occurs for any post-mortem debugging raised by an uncaught exception in the user code: the backtrace displayed by the 'bt' command shows frames that are owned by the pdb and bdb modules; and worse, the 'up' command allows to move to these frames. See for example below what happens when debugging foo.py that contains only the line "1/0". IMO this problem should be handled in another issue and in that case, in your example, 'bt' output would be empty.
gh-84583: Make pdb enter post-mortem mode even for SyntaxError (#110883)
gaogaotiantian commentedon Oct 15, 2023
Fixed in #110883
pythongh-84583: Make pdb enter post-mortem mode even for SyntaxError (p…
pythongh-84583: Make pdb enter post-mortem mode even for SyntaxError (p…