Last active
April 10, 2021 12:33
-
-
Save internetimagery/031b6f3e106661c28d21a85a003ffdfe to your computer and use it in GitHub Desktop.
Traceback Generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate tracebacks from arbitrary information | |
import types, sys | |
def create_traceback(stack, _code=compile("__temp__()", "", "exec").co_code): | |
""" Create a traceback from given information, or None if provided stack is empty. """ | |
func = None | |
for frame in stack: | |
func = types.FunctionType( | |
types.CodeType( | |
0, 0, 0, 0, 1, 64, _code, (None,), ("__temp__",), (), | |
frame[0], frame[2], frame[1], b"", (), ()), | |
dict(frame[3], __temp__=func), frame[2]) | |
try: | |
func() | |
except TypeError: | |
return sys.exc_info()[2].tb_next | |
if __name__ == "__main__": | |
raise RuntimeError("Here").with_traceback(create_traceback(( | |
("/some/file1.py", 123, "abc", {}), | |
("/some/file2.py", 456, "def", {}), | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment