10 examples of 'python print stack trace' in Python

Every line of 'python print stack trace' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
201def print_stack(f=None, limit=None, file=None):
202 """This function prints a stack trace from its invocation point.
203 The optional 'f' argument can be used to specify an alternate stack
204 frame at which to start. The optional 'limit' and 'file' arguments
205 have the same meaning as for print_exception()."""
206 if f is None:
207 try:
208 raise ZeroDivisionError
209 except ZeroDivisionError:
210 f = sys.exc_info()[2].tb_frame.f_back
211 print_list(extract_stack(f, limit), file)
280def print_stack(f=None, limit=None, file=None):
281 """Print a stack trace from its invocation point.
282
283 The optional 'f' argument can be used to specify an alternate
284 stack frame at which to start. The optional 'limit' and 'file'
285 arguments have the same meaning as for print_exception().
286 """
287 if f is None:
288 try:
289 raise ZeroDivisionError
290 except ZeroDivisionError:
291 f = sys.exc_info()[2].tb_frame.f_back
292
293 print_list(extract_stack(f, limit), file)
294 return
8def trace():
9 """
10 trace finds the line, the filename
11 and error message and returns it
12 to the user
13 """
14 import traceback, inspect
15 tb = sys.exc_info()[2]
16 tbinfo = traceback.format_tb(tb)[0]
17 filename = inspect.getfile(inspect.currentframe())
18 # script name + line number
19 line = tbinfo.split(", ")[1]
20 # Get Python syntax error
21 #
22 synerror = traceback.format_exc().splitlines()[-1]
23 return line, filename, synerror
821def print_stacktrace(cont, stack):
822 st = []
823 st.append(stacktrace_for_cont(cont))
824 while stack:
825 st.append(stacktrace_for_cont(stack._cont))
826 stack = stack._parent
827 st.reverse()
828 for line in st:
829 print line
86def print_stack_trace(self):
87 try:
88 for frame_lineno in self.stack:
89 if not is_skipped_frame(self, frame_lineno[0]):
90 self.print_stack_entry(frame_lineno)
91 except KeyboardInterrupt:
92 pass
774def print_stacktrace(thread, string_buffer=False):
775 """Prints a simple stack trace of this thread."""
776
777 output = StringIO.StringIO() if string_buffer else sys.stdout
778 target = thread.GetProcess().GetTarget()
779
780 depth = thread.GetNumFrames()
781
782 mods = get_module_names(thread)
783 funcs = get_function_names(thread)
784 symbols = get_symbol_names(thread)
785 files = get_filenames(thread)
786 lines = get_line_numbers(thread)
787 addrs = get_pc_addresses(thread)
788
789 if thread.GetStopReason() != lldb.eStopReasonInvalid:
790 desc = "stop reason=" + stop_reason_to_str(thread.GetStopReason())
791 else:
792 desc = ""
793 print >> output, "Stack trace for thread id={0:#x} name={1} queue={2} ".format(
794 thread.GetThreadID(), thread.GetName(), thread.GetQueueName()) + desc
795
796 for i in range(depth):
797 frame = thread.GetFrameAtIndex(i)
798 function = frame.GetFunction()
799
800 load_addr = addrs[i].GetLoadAddress(target)
801 if not function:
802 file_addr = addrs[i].GetFileAddress()
803 start_addr = frame.GetSymbol().GetStartAddress().GetFileAddress()
804 symbol_offset = file_addr - start_addr
805 print >> output, " frame #{num}: {addr:#016x} {mod}`{symbol} + {offset}".format(
806 num=i, addr=load_addr, mod=mods[i], symbol=symbols[i], offset=symbol_offset)
807 else:
808 print >> output, " frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line} {args}".format(
809 num=i, addr=load_addr, mod=mods[i], func='%s [inlined]' %
810 funcs[i] if frame.IsInlined() else funcs[i], file=files[i], line=lines[i], args=get_args_as_string(
811 frame, showFuncName=False) if not frame.IsInlined() else '()')
812
813 if string_buffer:
814 return output.getvalue()
137def traceback_format(self):
138 return [line.traceback_format() for line in self.lines]
272def format_stack(f=None, limit=None):
273 """Shorthand for 'format_list(extract_stack(f, limit))'."""
274 if f is None:
275 try:
276 raise ZeroDivisionError
277 except ZeroDivisionError:
278 f = sys.exc_info()[2].tb_frame.f_back
279 return format_list(extract_stack(f, limit))
130def repr_stack(stack, sp):
131 for line in stack.splitlines():
132 addr, values = line.split(':')
133 values = values.split()
134 if addr == sp:
135 top = values[0]
136 rest = ' '.join(values[1:])
137 line = '==> {} {} {}'.format(
138 red(addr), red(top, back='green'),
139 yellow(rest))
140 else:
141 line = ' {} {}'.format(
142 red(addr), cyan(' '.join(values)))
143 print(line)
282def stacktraces():
283 code = []
284
285 for threadId, stack in sys._current_frames().items():
286 code.append("\n>>> # ThreadID: %s" % threadId)
287
288 for filename, lineno, name, line in traceback.extract_stack(stack):
289 code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
290 if line:
291 code.append(" %s" % (line.strip()))
292
293 code.append("\n<<<")
294
295 print "\n".join(code)

Related snippets