10 examples of 'python print stack trace without exception' in Python

Every line of 'python print stack trace without exception' 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

200def print_exception():
201 import linecache
202 linecache.checkcache()
203 flush_stdout()
204 efile = sys.stderr
205 typ, val, tb = excinfo = sys.exc_info()
206 sys.last_type, sys.last_value, sys.last_traceback = excinfo
207 seen = set()
208
209 def print_exc(typ, exc, tb):
210 seen.add(id(exc))
211 context = exc.__context__
212 cause = exc.__cause__
213 if cause is not None and id(cause) not in seen:
214 print_exc(type(cause), cause, cause.__traceback__)
215 print("\nThe above exception was the direct cause "
216 "of the following exception:\n", file=efile)
217 elif (context is not None and
218 not exc.__suppress_context__ and
219 id(context) not in seen):
220 print_exc(type(context), context, context.__traceback__)
221 print("\nDuring handling of the above exception, "
222 "another exception occurred:\n", file=efile)
223 if tb:
224 tbe = traceback.extract_tb(tb)
225 print('Traceback (most recent call last):', file=efile)
226 exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
227 "debugger_r.py", "bdb.py")
228 cleanup_traceback(tbe, exclude)
229 traceback.print_list(tbe, file=efile)
230 lines = traceback.format_exception_only(typ, exc)
231 for line in lines:
232 print(line, end='', file=efile)
233
234 print_exc(typ, val, tb)
89@contextlib.contextmanager
90def _dump_locals_on_exception():
91 try:
92 yield
93 except Exception as e:
94 trace = inspect.trace()
95 if len(trace) >= 2:
96 name = _dump_stack_and_locals(trace[1:], exc = e)
97 logger.fatal(f'Dumped stack and locals to {name}')
98 raise
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)
244def format_exc(self):
245 """
246 Format the latest exception's traceback.
247 """
248 return self._format_tb_string_with_locals(*sys.exc_info())
137def traceback_format(self):
138 return [line.traceback_format() for line in self.lines]
126def print_exception():
127 flush_stdout()
128 efile = sys.stderr
129 typ, val, tb = excinfo = sys.exc_info()
130 sys.last_type, sys.last_value, sys.last_traceback = excinfo
131 tbe = traceback.extract_tb(tb)
132 print>>efile, '\nTraceback (most recent call last):'
133 exclude = ("run.py", "rpc.py", "threading.py", "Queue.py",
134 "RemoteDebugger.py", "bdb.py")
135 cleanup_traceback(tbe, exclude)
136 traceback.print_list(tbe, file=efile)
137 lines = traceback.format_exception_only(typ, val)
138 for line in lines:
139 print>>efile, line,
51def format_traceback(skip=0):
52 """Return a description of the current exception as a string.
53
54 skip -- number of traceback entries to omit from the top of the list
55
56 """
57 exception_type, value, tb = sys.exc_info()
58 return format_traceback_from_info(exception_type, value, tb, skip)
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
17def _print_traceback(e):
18 exc_info = (type(e), e, e.__traceback__)
19 traceback.print_exception(*exc_info)

Related snippets