Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacktrace for UserWarning

I see a warning like this in my logs:

py.warnings.__init__: WARNING .../bs4/__init__.py:219: UserWarning: "foo" 
  looks like a filename, not markup. You should probably open this file 
  and pass the filehandle into Beautiful Soup

This message does not help very much.

I would like to see the stacktrace where this happens.

Please don't look into the content of this warning. This question is not about Beautiful Soup :-)

An easy solution would be to modify the third party code (bs4/__init__.py at line 219) and add something like this:

import traceback
logger.warn('Exc at ...\n%s' % ''.join(traceback.format_stack()))

But I would like to avoid this. Reasons:

  • This is a warning from a production system. I don't want to change the source.
  • The next time a warning like this occurs, I would like to see the stacktrace immediately

Is there a flag or setting for python which I can change, to see not only one line, but the while stacktrace? I need the upper frames to debug this.

In this environment Python 2.7 gets used.

like image 463
guettli Avatar asked Aug 08 '17 08:08

guettli


1 Answers

You would need to do the following:

  1. Create if USER_SITE does not exists: issue python -c "import site; site._script()", see USER_SITE variable contents
  2. Place a file usercustomize.py in that directory with the following code:

    import traceback
    import warnings
    
    
    _old_warn = warnings.warn
    def warn(*args, **kwargs):
        tb = traceback.extract_stack()
        _old_warn(*args, **kwargs)
        print("".join(traceback.format_list(tb)[:-1]))
    warnings.warn = warn
    

    Credits to this answer for the code.

Run the code as usual. My test code:

import warnings

def f():
    warnings.warn("foz")

f()

Before the procedure:

$ python test_warn.py
test_warn.py:4: UserWarning: foz
  warnings.warn("foz")

After:

$ python test_warn.py
<USER_SITE_REDACTED>/usercustomize.py:6: UserWarning: foz
  _old_warn(*args, **kwargs)
  File "test_warn.py", line 6, in <module>
    f()
  File "test_warn.py", line 4, in f
    warnings.warn("foz")
like image 132
RebelWithoutAPulse Avatar answered Oct 13 '22 12:10

RebelWithoutAPulse