Note. Stefano M is the right answer because it is perfect for StackOverflow standards
For those who are not familiar with working with Frame / traceback objects, I have provided a working Tracker class built from the Stefano M MyTracer template :
import sys,traceback def printf(*stuff,sep=" ",end="\n",file=sys.stdout): file.write(sep.join(stuff)+end)#for backward compatability with python2 class Tracker: def __init__(self,out_file = sys.stdout): assert out_file.writable(),"need to open a file for writing" self.out = out_file self.last_frame = None def __enter__(self): self.old_trace = sys.gettrace() sys.settrace(self.newscope) def __exit__(self,etype,value,tb): sys.settrace(self.old_trace) self.finish_previous() if tb: traceback.printf_exception(etype,value,tb,file=self.out) else: printf("exit status normal",file=self.out) def newscope(self,f,e,ar): self.finish_previous(f) global_vars = f.f_globals for name,module in sys.modules.items(): if global_vars is vars(module): file_name = name break else: file_name = "??" module = None printf(self._format_module(file_name,f.f_lineno),file=self.out) if module: printf(self._read_line_of_file(module,f.f_lineno),file=self.out) @staticmethod def _format_module(file_name,line): return "{}, line:{}".format(file_name,line) @staticmethod def _read_line_of_file(module,line): try: with open(module.__file__,"r") as FILE: line = list(FILE)[line] return line except Exception as e: return "ERROR WHEN READING FILE: %r"%e @staticmethod def _local_vars(frame): return "locals:\n "+"\n ".join("{} = {!r}".format(*pair) for pair in frame.f_locals.items()) def finish_previous(self,new_frame=None): if self.last_frame: printf(self._local_vars(self.last_frame),end="\n\n\n",file=self.out) self.last_frame = new_frame
Then it can be used to map the output to standard output:
with Tracker(): response=self.admin_client.post(url, post) self.assertEqual(200, response.status_code, trace)
or send the output to a file, you can do this:
with open("log.txt","w") as f, Tracker(f): response=self.admin_client.post(url, post) self.assertEqual(200, response.status_code, trace)
I hope you can get immediate results, but it will still be much better for you to implement the Stefano M template yourself, since the information you are looking for is most likely different from what I would like to display.