Skip to main content
added 200 characters in body
Source Link
vaultah
  • 47k
  • 13
  • 121
  • 146

You could use functions from the traceback module. For example, extract_tbextract_tb returns a list of tuples (named tuples in Python 3.5 and newer) representing the stack trace entries. Each tuple contains a line number as well as the source text line (if available).

import traceback

try:
    assert 1
    assert None
except AssertionError as e:
    for x in traceback.extract_tb(e.__traceback__, limit=-1):
        print(x.lineno, repr(x.line)) # Prints 5 'assert None'

You could use functions from the traceback module. For example extract_tb returns a list of tuples representing the stack trace entries.

import traceback

try:
    assert 1
    assert None
except AssertionError as e:
    for x in traceback.extract_tb(e.__traceback__, limit=-1):
        print(x.lineno, repr(x.line)) # Prints 5 'assert None'

You could use functions from the traceback module. For example, extract_tb returns a list of tuples (named tuples in Python 3.5 and newer) representing the stack trace entries. Each tuple contains a line number as well as the source text line (if available).

import traceback

try:
    assert 1
    assert None
except AssertionError as e:
    for x in traceback.extract_tb(e.__traceback__, limit=-1):
        print(x.lineno, repr(x.line)) # Prints 5 'assert None'
Source Link
vaultah
  • 47k
  • 13
  • 121
  • 146

You could use functions from the traceback module. For example extract_tb returns a list of tuples representing the stack trace entries.

import traceback

try:
    assert 1
    assert None
except AssertionError as e:
    for x in traceback.extract_tb(e.__traceback__, limit=-1):
        print(x.lineno, repr(x.line)) # Prints 5 'assert None'