Skip to content

Instantly share code, notes, and snippets.

@nvictus
Last active June 20, 2017 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvictus/a1de6faf25e76efd3c33cc76ae7180fa to your computer and use it in GitHub Desktop.
Save nvictus/a1de6faf25e76efd3c33cc76ae7180fa to your computer and use it in GitHub Desktop.
print traceback in warnings
import traceback
import warnings
import sys
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = warn_with_traceback
@nvictus
Copy link
Author

nvictus commented Jun 20, 2017

Thanks to user mgab at https://stackoverflow.com/a/22376126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment