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 version of the program, maintaining the "var: value" format:
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 32 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_lineno == line_number: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information print(f"Exception: {exc_type.__name__}: {exc_value}") |
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 32 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_lineno == line_number: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information print(f"Exception: {exc_type.__name__}: {exc_value}") |
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 32 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_lineno == line_number: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information print(f"Exception: {exc_type.__name__}: {exc_value}") |
Exception: Exception: This is a test exception
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 version of the program, maintaining the "var: value" format:
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 | 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: # extract stack frames _, _, tb = sys.exc_info() stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame in stack: filename, line_number, func_name, _ = frame frame_locals = traceback.extract_tb(tb, limit=1)[0].frame.f_locals print(f"Frame: {func_name} in {filename} at line {line_number}") for k, v in frame_locals.items(): print(f"{k}: {v}") print() # Add a blank line between frames |
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 | 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: # extract stack frames _, _, tb = sys.exc_info() stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame in stack: filename, line_number, func_name, _ = frame frame_locals = traceback.extract_tb(tb, limit=1)[0].frame.f_locals print(f"Frame: {func_name} in {filename} at line {line_number}") for k, v in frame_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 | 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: # extract stack frames _, _, tb = sys.exc_info() stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame in stack: filename, line_number, func_name, _ = frame frame_locals = traceback.extract_tb(tb, limit=1)[0].frame.f_locals print(f"Frame: {func_name} in {filename} at line {line_number}") for k, v in frame_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 24, in <module>
frame_locals = traceback.extract_tb(tb, limit=1)[0].frame.f_locals
AttributeError: 'FrameSummary' object has no attribute 'frame'. Did you mean: 'name'?
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 version of the program, maintaining the "var: value" format:
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 | 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: # extract stack trace exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") # print the exception message print(f"Exception: {exc_value}") |
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 | 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: # extract stack trace exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") # print the exception message print(f"Exception: {exc_value}") |
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 | 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: # extract stack trace exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") # print the exception message print(f"Exception: {exc_value}") |
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 23, in <module>
local_vars = frame.frame.f_locals
AttributeError: 'FrameSummary' object has no attribute 'frame'. Did you mean: 'name'?
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 version of the program, maintaining the "var: value" format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") |
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 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") |
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 | 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: # extract stack frames exc_type, exc_value, exc_traceback = sys.exc_info() stack = traceback.extract_tb(exc_traceback) # print all locals in each stack frame for frame in stack: local_vars = frame.frame.f_locals for k, v in local_vars.items(): print(f"{k}: {v}") |
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 23, in <module>
local_vars = frame.frame.f_locals
AttributeError: 'FrameSummary' object has no attribute 'frame'. Did you mean: 'name'?
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 maintains the "var: value" format for printing local variables:
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 32 33 | 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: # extract stack frames tb = sys.exc_info()[2] stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_code.co_name == function_name: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information exc_type, exc_value, exc_traceback = sys.exc_info() print(f"Exception: {exc_type.__name__}: {exc_value}") |
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 32 33 | 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: # extract stack frames tb = sys.exc_info()[2] stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_code.co_name == function_name: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information exc_type, exc_value, exc_traceback = sys.exc_info() print(f"Exception: {exc_type.__name__}: {exc_value}") |
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 32 33 | 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: # extract stack frames tb = sys.exc_info()[2] stack = traceback.extract_tb(tb) # print all locals in each stack frame for frame_info in stack: filename, line_number, function_name, text = frame_info frame = sys._getframe(0) while frame: if frame.f_code.co_filename == filename and frame.f_code.co_name == function_name: for k, v in frame.f_locals.items(): print(f"{k}: {v}") break frame = frame.f_back # Print the exception information exc_type, exc_value, exc_traceback = sys.exc_info() print(f"Exception: {exc_type.__name__}: {exc_value}") |
__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 27, 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