gpt4 book ai didi

python-3.x - Pillow+numpy+unittest 给出 ResourceWarning : does this indicate a leak?

转载 作者:行者123 更新时间:2023-12-03 17:08:06 26 4
gpt4 key购买 nike

当我在 Python3 上使用 Pillow(3.3.0 版,通过 pip 安装)将图像数据加载到 numpy 数组中时,我的单元测试报告了 ResourceWarning。例如,当我运行以下脚本时会出现警告:

#! /usr/bin/env python3

import unittest
import numpy as np
import PIL.Image

def load_image():
with PIL.Image.open('test.tif') as im:
return np.array(im)

class TestData(unittest.TestCase):

def test_PIL(self):
im = load_image()
print(im.shape)

unittest.main()

输出是

./err.py:14: ResourceWarning: unclosed file <_io.BufferedReader name='test.tif'>
im = load_image()
(420, 580)
.
----------------------------------------------------------------------
Ran 1 test in 0.012s

OK

(如果我不将图像包装在 numpy 数组中,警告就会消失。)

此资源警告是否表示我的代码中存在泄漏(例如,除了使用 with 语句之外,我是否需要以某种方式“关闭”图像文件)?或者,如果警告是虚假的,我该如何禁用它?

最佳答案

TL;DR,我认为这是一个错误。这应该按预期关闭文件句柄:

def load_image():
with open('test.tif', 'rb') as im_handle:
im = PIL.Image.open(im_handle)
return np.array(im)

好吧,让我们看看到底发生了什么:

为此我们添加了一个记录器:

#! /usr/bin/env python3

import logging
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('hi from the logger!')

import unittest
import numpy as np
import PIL.Image

def load_image():
with PIL.Image.open('test.tif') as im:
return np.array(im)

class TestData(unittest.TestCase):

def test_PIL(self):
im = load_image()
print(im.shape)

unittest.main()

这是我们得到的:

DEBUG:root:hi from the logger!
/Users/ch/miniconda/envs/sci34/lib/python3.4/site-packages/PIL/Image.py:678: ResourceWarning: unclosed file <_io.BufferedReader name='test.tif'>
self.load()
DEBUG:PIL.Image:Error closing: 'NoneType' object has no attribute 'close'
(225, 300, 3)
.
----------------------------------------------------------------------
Ran 1 test in 0.022s

OK

DEBUG:PIL.Image:Error closing: 'NoneType' object has no attribute 'close'Image 类的关闭方法中引发:

def close(self):
"""
Closes the file pointer, if possible.

This operation will destroy the image core and release its memory.
The image data will be unusable afterward.

This function is only required to close images that have not
had their file read and closed by the
:py:meth:`~PIL.Image.Image.load` method.
"""
try:
self.fp.close()
except Exception as msg:
logger.debug("Error closing: %s", msg)

# Instead of simply setting to None, we're setting up a
# deferred error that will better explain that the core image
# object is gone.
self.im = deferred_error(ValueError("Operation on closed image"))

用语言表达:

with block 的末尾,调用了 close 方法。它会尝试关闭存储在 self.fp 中的打开文件句柄。但在您的情况下 self.fp 不是文件句柄,因此无法关闭。关闭无声地失败。

所以当你离开 with block 时,文件句柄没有关闭,nose 的错误消息是合法的。

关于python-3.x - Pillow+numpy+unittest 给出 ResourceWarning : does this indicate a leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com