Fix this program. Make sure to keep the format "var: value". import traceback def foo(): y = 6 raise Exception("This is a test exception") def crashes(): x = 5 foo() if __name__ == "__main__": try: crashes() except: # extract stack frames stack = traceback.extract_stack() # print all locals in each stack frame for frame in stack: for k,v in frame.locals: print(k + ": " + v)
Fix this program. Make sure to keep the format "var: value". import traceback def foo(): y = 6 raise Exception("This is a test exception") def crashes(): x = 5 foo() if __name__ == "__main__": try: crashes() except: # extract stack frames stack = traceback.extract_stack() # print all locals in each stack frame for frame in stack: for k,v in frame.locals: print(k + ": " + v)
The program you provided has a few issues that need to be addressed: 1. The `traceback.extract_stack()` function does not provide access to local variables. Instead, you should use `sys._getframe()` to access the current stack frames and their local variables. 2. The `frame.locals` attribute does not exist. Instead, you should use `frame.f_locals` to access the local variables of a frame. 3. The `for k, v in frame.locals` loop is incorrect because `frame.locals` is a dictionary, not a list of tuples. You should iterate over `frame.f_locals.items()` instead. Here’s the corrected version of the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import traceback import sys def foo(): y = 6 raise Exception("This is a test exception") def crashes(): x = 5 foo() if __name__ == "__main__": try: crashes() except Exception: # Get the current exception's traceback exc_type, exc_value, exc_traceback = sys.exc_info() # Iterate through the traceback frames while exc_traceback is not None: frame = exc_traceback.tb_frame # Print all locals in the current frame for k, v in frame.f_locals.items(): print(f"{k}: {v}") # Move to the next frame exc_traceback = exc_traceback.tb_next |
1 2 | x: 5 y: 6 |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import traceback import sys def foo(): y = 6 raise Exception("This is a test exception") def crashes(): x = 5 foo() if __name__ == "__main__": try: crashes() except Exception: # Get the current exception's traceback exc_type, exc_value, exc_traceback = sys.exc_info() # Iterate through the traceback frames while exc_traceback is not None: frame = exc_traceback.tb_frame # Print all locals in the current frame for k, v in frame.f_locals.items(): print(f"{k}: {v}") # Move to the next frame exc_traceback = exc_traceback.tb_next |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import traceback import sys def foo(): y = 6 raise Exception("This is a test exception") def crashes(): x = 5 foo() if __name__ == "__main__": try: crashes() except Exception: # Get the current exception's traceback exc_type, exc_value, exc_traceback = sys.exc_info() # Iterate through the traceback frames while exc_traceback is not None: frame = exc_traceback.tb_frame # Print all locals in the current frame for k, v in frame.f_locals.items(): print(f"{k}: {v}") # Move to the next frame exc_traceback = exc_traceback.tb_next |
__name__: __main__
Traceback (most recent call last):
File "/usr/src/app/main.py", line 15, in <module>
crashes()
File "/usr/src/app/main.py", line 11, in crashes
foo()
File "/usr/src/app/main.py", line 7, in foo
raise Exception("This is a test exception")
Exception: This is a test exception
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/app/main.py", line 24, in <module>
for k, v in frame.f_locals.items():
RuntimeError: dictionary changed size during iteration
Testing if the previous output contains the string y: 6
: False
Testing if the previous output contains the string x: 5
: False