Why is `pdb` claiming something unrelated and misleading?

My Python script tells where it goes wrong ("line 122" in myscript.py) when I run it in a shell:

$ toc2others.py -i toc -p pg
Traceback (most recent call last):
  File "~/myscript.py", line 122, in <module>
    p = re.match(keywords[index+1][0], inlines[n+1], re.IGNORECASE)
IndexError: list index out of range

      

This is because it is keywords[index+1]

out of the range of indices keywords

.

However, when I run it under pdb

, it doesn't tell you where it is going wrong, but says something unrelated (the bug is reported in import re

).

$ pdb ~/myscript.py -i toc -p pg
> /myscript.py(3)<module>()
-> import re
(Pdb) c
Traceback (most recent call last):
  File "/usr/lib/python2.7/pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
    self.run(statement)
  File "/usr/lib/python2.7/bdb.py", line 387, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "~/myscript.py", line 3, in <module>
    import re
IndexError: list index out of range
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program

      

I wonder why is pdb

indicating something unrelated and misleading?

Can anyone pdb

point out where it really goes wrong?

Thank.

+3


source to share


1 answer


This is actually a mistake. See Questions:

This only happens if an exception is thrown at the module level of the executable file, i.e. not inside any function. So if you just put your code in a function main()

it will fix it. Or you can use ipython, which is much more interesting for debugging:



ipython ~/myscript.py --pdb -- -i toc -p pg

      

This will run the script and only stop if there is an error, and it won't suffer from the above error either.

+2


source







All Articles