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)
Here's the fixed program that properly prints local variables in the format "var: value":
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 27 28 29 30 31 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames frames = traceback.extract_tb(exc_traceback) for frame in frames: # Get the frame's locals using inspect frame_obj = sys._getframe() while frame_obj is not None and frame_obj.f_code.co_filename != frame.filename or frame_obj.f_lineno != frame.lineno: frame_obj = frame_obj.f_back if frame_obj: # Print local variables for k, v in frame_obj.f_locals.items(): print(f"{k}: {v}") print() # Add a blank line between frames |
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 27 28 29 30 31 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames frames = traceback.extract_tb(exc_traceback) for frame in frames: # Get the frame's locals using inspect frame_obj = sys._getframe() while frame_obj is not None and frame_obj.f_code.co_filename != frame.filename or frame_obj.f_lineno != frame.lineno: frame_obj = frame_obj.f_back if frame_obj: # Print local variables for k, v in frame_obj.f_locals.items(): print(f"{k}: {v}") print() # Add a blank line between frames |
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 27 28 29 30 31 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames frames = traceback.extract_tb(exc_traceback) for frame in frames: # Get the frame's locals using inspect frame_obj = sys._getframe() while frame_obj is not None and frame_obj.f_code.co_filename != frame.filename or frame_obj.f_lineno != frame.lineno: frame_obj = frame_obj.f_back if frame_obj: # Print local variables for k, v in frame_obj.f_locals.items(): print(f"{k}: {v}") print() # Add a blank line between frames |
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 25, in <module>
while frame_obj is not None and frame_obj.f_code.co_filename != frame.filename or frame_obj.f_lineno != frame.lineno:
^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'f_lineno'
Testing if the previous output contains the string y: 6
: False
Testing if the previous output contains the string x: 5
: False
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)
Here's the fixed program that properly displays local variables in the format "var: value":
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 27 28 29 30 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Extract frames from the traceback frames = traceback.extract_tb(exc_traceback) # Get the frame objects that contain the local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function: {frame.f_code.co_name}") # Print local variables in the required format for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.tb_next |
1 2 3 4 5 | Locals in function: crashes x: 5 Locals in function: foo 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 27 28 29 30 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Extract frames from the traceback frames = traceback.extract_tb(exc_traceback) # Get the frame objects that contain the local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function: {frame.f_code.co_name}") # Print local variables in the required format for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.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 27 28 29 30 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Extract frames from the traceback frames = traceback.extract_tb(exc_traceback) # Get the frame objects that contain the local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function: {frame.f_code.co_name}") # Print local variables in the required format for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.tb_next |
Locals in function: <module>
__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 29, in <module>
for key, value 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
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)
Here's the fixed program that properly displays local variables in the format "var: value":
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 27 28 29 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Get the stack frames frames = traceback.extract_tb(exc_traceback) # Walk through the frames and their local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function '{frame.f_code.co_name}':") for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.tb_next |
1 2 3 4 5 | Locals in function 'crashes': x: 5 Locals in function 'foo': 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 27 28 29 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Get the stack frames frames = traceback.extract_tb(exc_traceback) # Walk through the frames and their local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function '{frame.f_code.co_name}':") for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.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 27 28 29 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Get the stack frames frames = traceback.extract_tb(exc_traceback) # Walk through the frames and their local variables tb = exc_traceback while tb: frame = tb.tb_frame print(f"\nLocals in function '{frame.f_code.co_name}':") for key, value in frame.f_locals.items(): print(f"{key}: {value}") tb = tb.tb_next |
Locals in function '<module>':
__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 28, in <module>
for key, value 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
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)
Here's the fixed program that properly prints local variables in the format "var: value":
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 27 28 29 | 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: # Get the current traceback exc_type, exc_value, tb = sys.exc_info() # Walk through the stack frames while tb: # Get frame frame = tb.tb_frame # Print local variables for this frame for var_name, var_value in frame.f_locals.items(): print(f"{var_name}: {var_value}") # Move to the next frame tb = tb.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 27 28 29 | 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: # Get the current traceback exc_type, exc_value, tb = sys.exc_info() # Walk through the stack frames while tb: # Get frame frame = tb.tb_frame # Print local variables for this frame for var_name, var_value in frame.f_locals.items(): print(f"{var_name}: {var_value}") # Move to the next frame tb = tb.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 27 28 29 | 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: # Get the current traceback exc_type, exc_value, tb = sys.exc_info() # Walk through the stack frames while tb: # Get frame frame = tb.tb_frame # Print local variables for this frame for var_name, var_value in frame.f_locals.items(): print(f"{var_name}: {var_value}") # Move to the next frame tb = tb.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 26, in <module>
for var_name, var_value 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
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)
Here's the fixed program that prints local variables in the format "var: value":
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 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames while exc_traceback: frame = exc_traceback.tb_frame # Get local variables from the frame for key, value in frame.f_locals.items(): print(f"{key}: {value}") exc_traceback = exc_traceback.tb_next |
1 2 | y: 6 x: 5 |
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 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames while exc_traceback: frame = exc_traceback.tb_frame # Get local variables from the frame for key, value in frame.f_locals.items(): print(f"{key}: {value}") 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 | 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: # Get the current exception information exc_type, exc_value, exc_traceback = sys.exc_info() # Walk through the stack frames while exc_traceback: frame = exc_traceback.tb_frame # Get local variables from the frame for key, value in frame.f_locals.items(): print(f"{key}: {value}") 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 key, value 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