如果我使用这样一个简单的测试脚本,它会导致ResourceWarning:
import asyncio
import aiohttp
async def main():
client = aiohttp.ClientSession()
await client.get("https://google.com")
asyncio.run(main())
用python -bb -We -X dev test.py
运行它。
我预计ResourceWarning
会出错并导致非0退出代码。
实际结果:
Exception ignored in: <function ClientSession.__del__ at 0x7f397e04beb0>
Traceback (most recent call last):
File "/home/s/.local/lib/python3.8/site-packages/aiohttp/client.py", line 342, in __del__
_warnings.warn(
ResourceWarning: Unclosed client session <aiohttp.client.ClientSession object at 0x7f397dfce4b0>
Exception ignored in: <function BaseConnector.__del__ at 0x7f397e0b6eb0>
Traceback (most recent call last):
File "/home/s/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 281, in __del__
_warnings.warn(f"Unclosed connector {self!r}", ResourceWarning, **kwargs)
ResourceWarning: Unclosed connector <aiohttp.connector.TCPConnector object at 0x7f397dfce690>
Exception ignored in: <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.18', 45750), raddr=('172.217.169.4', 443)>
ResourceWarning: unclosed <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.18', 45750), raddr=('172.217.169.4', 443)>
Exception ignored in: <function _SelectorTransport.__del__ at 0x7f397e8264b0>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/selector_events.py", line 696, in __del__
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
ResourceWarning: unclosed transport <_SelectorSocketTransport fd=7>
Exception ignored in: <function _SelectorTransport.__del__ at 0x7f397e8264b0>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/selector_events.py", line 696, in __del__
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
ResourceWarning: unclosed transport <_SelectorSocketTransport fd=6>
Exception ignored in: <socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.18', 42986), raddr=('142.250.187.206', 443)>
ResourceWarning: unclosed <socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.18', 42986), raddr=('142.250.187.206', 443)>
0退出代码表示成功。
如何确保程序在出现警告时确实失败?
1条答案
按热度按时间7cwmlq891#
-We
使解释器引发异常而不是警告。但是,在__del__
函数中不能引发异常。相反,此类异常将打印到stderr,并继续执行。此行为可通过sys.unraisablehook
覆盖。下面是代码的修改版本,它将返回非零退出代码:
请注意,我使用
os._exit()
而不是sys.exit()
,这是因为sys.exit()
会引发SystemExit异常,这在我们的示例中是一个坏主意。