How to inspect variables after Traceback?

Colonel Panic

My Python script is crashing. To debug it, I ran it in interactive mode python -i example.py

Traceback (most recent call last):
  File "example.py", line 5, in <module>
    main()
  File "example.py", line 3, in main
    message[20]
IndexError: string index out of range

At this point, I would like to inspect the variable message. I tried

>>> message
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'message' is not defined

Alas message is not in scope (though main is). That's frustrating. How can I inspect the variable? Is there a more useful version of python -i that keeps what's in scope at the crash (rather than the top level)?


Code used for example.py above. Needless to say, this is a simplification.

def main():
    message = "hello world"
    message[20]

main()
Colonel Panic

According to the Python docs https://docs.python.org/3.4/library/pdb.html

pdb.py can also be invoked as a script to debug other scripts. For example: python -m pdb myscript.py. When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally.

This isn't entirely accurate. It actually enters debugging at the first line.

$ python -m pdb example.py
> example.py(1)<module>()
-> def main():

However if you type c it will then continue to the crash

(Pdb) c
Traceback (most recent call last):
  File "C:\Python34\lib\pdb.py", line 1661, in main
    pdb._runscript(mainpyfile)
  File "C:\Python34\lib\pdb.py", line 1542, in _runscript
    self.run(statement)
  File "C:\Python34\lib\bdb.py", line 431, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "example.py", line 1, in <module>
    def main():
  File "example.py", line 3, in main
    message[20]
IndexError: string index out of range
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> example.py(3)main()

At this point you can type message to inspect the variable.

-> message[20]
(Pdb) message
'hello world'

Wahey!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I inspect Pelican variables

From Dev

How to inspect the variables of user space functions in systemtap?

From Dev

How can I inspect cmake internal variables?

From Dev

How do you inspect CSS variables in the browser?

From Dev

PDB: How to inspect local variables of functions in nested stack frames?

From Dev

Inspect WebdriverIO spec variables

From Dev

How to create a traceback object

From Dev

Using gdb to inspect environment variables

From Dev

CSS works only after inspect

From Dev

How do I print variables in Rust and have it show everything about that variable, like Ruby's .inspect?

From Dev

How to solve this traceback error in pdfquery

From Dev

How to handle the "Traceback" error in python?

From Dev

how to inspect haskell bytecode

From Java

How to inspect FormData?

From Dev

How to inspect om objects?

From Dev

How to inspect DSL router?

From Dev

How to inspect disk image

From Dev

Reflect / Inspect closed-over variables in Python

From Dev

Get the inspect element variables from a website - Java

From Dev

Get the inspect element variables from a website - Java

From Dev

Element only available after inspect element in selenium

From Dev

How to inspect openshift build log?

From Dev

How to inspect the payload of a guzzle request

From Dev

How to inspect a running Docker container

From Dev

How to inspect JQuery UI tooltip?

From Dev

How to inspect parsed Aeson Value?

From Dev

How to parse ruby BigDecimal inspect?

From Dev

How to inspect a Firefox WebExtension popup?

From Dev

How to inspect a JavaScript Bound Function