A third-party library (written in C) that I use in my python code is issuing warnings. I want to be able to use the try
except
syntax to properly handle these warnings. Is there a way to do this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
If you just want you script to fail on warnings you can use:
Here's a variation that makes it clearer how to work with only your custom warnings.
In some cases, you need use ctypes to turn warnings into errors. For example:
To quote from the python handbook (27.6.4. Testing Warnings):
To handle warnings as errors simply use this:
After this you will be able to catch warnings same as errors, e.g. this will work:
P.S. Added this answer because the best answer in comments contains misspelling:
filterwarnigns
instead offilterwarnings
.