import asyncio import sys # PyFuncObject (obj.__code__) def func(): return # PyTracebackObject (obj.tb_frame) def get_traceback(): try: raise Exception except Exception as e: return e.__traceback__ # PyGenObject (obj.gi_code) def gen(): yield # PyCoroObject (obj.cr_code) async def coro(): return # PyAsyncGenObject (obj.ag_code) async def async_gen(): yield def hook(event, args): if event == 'object.__getattr__': print(args[1], 'AUDIT', args) async def main(): sys.addaudithook(hook) # PyFuncObject (obj.__code__) print('__code__') print(func.__code__) print() # PyFrameObject (frame.f_code) print('f_code') print(sys._getframe().f_code) print() # PyTracebackObject (obj.tb_frame) print('tb_frame') print(get_traceback().tb_frame) print() # PyGenObject (obj.gi_code) print('gi_code') print(gen().gi_code) print() # PyCoroObject (obj.cr_code) print('cr_code') cr = coro() print(cr.cr_code) await cr print() # PyAsyncGenObject (obj.ag_code) print('ag_code') print(async_gen().ag_code) print() asyncio.run(main())