Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

Disabling Python 3.2 ResourceWarning

Python 3.2 introduced ResourceWarning for unclosed system resources (network sockets, files):

Though the code runs clean in production, I am getting a lot of following warnings when running unit tests due to use of third party libraries where the warning occurs. I could fix the library, but on the other hand it were much simpler just to ignore it during the test run.

 block_io-python/block_io/__init__.py:182: ResourceWarning: unclosed <ssl.SSLSocket fd=11, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=6, laddr=('x', 58395), raddr=('x, 443)>

What is the way to disable these warnings? I tried the following but no effect:

 warnings.filterwarnings("ignore", category=ResourceWarning)

(Run during unit test import time).

question from:https://stackoverflow.com/questions/26563711/disabling-python-3-2-resourcewarning

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the culprit. You say you set your filter during import time. However, since Python 3.2 the unittest module has been updated to set the warning filter to default. See Section 29.5.5. Basically, unittest overwrites your warning filter preferences after it has finished importing your modules.

For example.

my_tests.py

import socket
import unittest
import warnings

warnings.simplefilter("ignore", ResourceWarning)

def abusesocket():
    s = socket.socket()
    s.connect(("www.google.com", 80))

class Test(unittest.TestCase):

    def test1(self):
        print("test1")
        abusesocket()
        print("module import warning filter nixed")

    def test2(self):
        print("test2")
        warnings.simplefilter("ignore", ResourceWarning)
        abusesocket()
        print("higher warning filter okay")

Gives the following output

$ python3 -m unittest  my_tests.py 
test1
/home/user/my_tests.py:15: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('x.x.x.x', 52332), raddr=('31.55.166.217', 80)>
  abusesocket()
module import warning filter nixed
.test2
higher warning filter okay
.
----------------------------------------------------------------------
Ran 2 tests in 0.347s

OK

Solution

unittest appears to reset the warning filter after each test. So you'll have clear the filter at the start of each test. Probably best to use a decorator to wrap your test functions.

def ignore_warnings(test_func):
    def do_test(self, *args, **kwargs):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", ResourceWarning)
            test_func(self, *args, **kwargs)
    return do_test

class Test(unittest.TestCase):

    @ignore_warnings
    def test1(self):
        abusesocket()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...