禁用 Python 3.2 ResourceWarning [英] Disabling Python 3.2 ResourceWarning

查看:89
本文介绍了禁用 Python 3.2 ResourceWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

推荐答案

我找到了罪魁祸首.您说您在导入期间设置了过滤器.但是,自 Python 3.2 以来,unittest 模块已更新为将警告过滤器设置为默认值.请参阅第 29.5.5 节.基本上,unittest 在完成导入模块后会覆盖您的警告过滤器首选项.

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.

例如.

my_tests.py

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")

给出以下输出

$ 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

解决方案

unittest 似乎在每次测试后重置警告过滤器.因此,您将在每次测试开始时清除过滤器.可能最好使用装饰器来包装您的测试函数.

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()

这篇关于禁用 Python 3.2 ResourceWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆