Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Traceback of warnings

In numpy we can do np.seterr(invalid='raise') to get a traceback for warnings raising an error instead (see this post).

  • Is there a general way for tracing warnings?
  • Can I make python to give a traceback, when a warning is raised?
like image 838
embert Avatar asked Mar 13 '14 09:03

embert


People also ask

How do you use traceback in Python?

In Python, A traceback is a report containing the function calls made in your code at a specific point i.e when you get an error it is recommended that you should trace it backward(traceback). Whenever the code gets an exception, the traceback will give the information about what went wrong in the code.

What is DeprecationWarning in Python?

Throwing deprecation warnings To warn about deprecation, you need to set Python's builtin DeprecationWarning as category. To let the warning refer to the caller, so you know exactly where you use deprecated code, you have to set stacklevel=2 .

What is Django traceback?

This module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace.


3 Answers

You can get what you want by assigning to warnings.showwarning. The warnings module documentation itself recommends that you do that, so it's not that you're being tempted by the dark side of the source. :)

You may replace this function with an alternative implementation by assigning to warnings.showwarning.

You can define a new function that does what warning.showwarning normaly does and additionally it prints the stack. Then you place it instead of the original:

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

After this, every warning will print the stack trace as well as the warning message. Take into account, however, that if the warning is ignored because it is not the first one, nothing will happen, so you still need to execute:

warnings.simplefilter("always")

You can get a similar control that the one numpy.seterr gives through the warning module's filters

If what you want is python to report every a warning every time it is triggered and not only the first time, you can include something like:

import warnings
warnings.simplefilter("always")

You can get other behaviours by passing different strings as arguments. Using the same function you can also specify different behaviours for warnings depending on the module that raised them, the message they provide, the warning class, the line of code that is causing it and so on...

You can check the list in the module documentation

As an example, you can set all the warnings to raise exceptions, except the DeprecationWarnings that should be ignored completely:

import warnings
warnings.simplefilter("error")
warnings.simplefilter("ignore", DeprecationWarning)

This way you get the full traceback for each warning raised as error (only the first one, since execution will stop... but you can address them one by one, and create a filter to ignore the ones you don't want to hear about again...

like image 194
mgab Avatar answered Oct 21 '22 14:10

mgab


Run your program like

python -W error myprogram.py

This makes all warnings fatal, see here for more information

like image 40
Jakob Bowyer Avatar answered Oct 21 '22 14:10

Jakob Bowyer


You can use warnings.filterwarnings() to turn selected warnings into exceptions and get a traceback as follows:

import warnings
warnings.filterwarnings(
    'error', 'DateTimeField .* received a naive datetime',
    RuntimeWarning, 'django.db.models.fields'
)
like image 4
mrts Avatar answered Oct 21 '22 15:10

mrts