python -We资源警告未出错

pprl5pva  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(215)

如果我使用这样一个简单的测试脚本,它会导致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退出代码表示成功。
如何确保程序在出现警告时确实失败?

7cwmlq89

7cwmlq891#

-We使解释器引发异常而不是警告。但是,在__del__函数中不能引发异常。相反,此类异常将打印到stderr,并继续执行。此行为可通过sys.unraisablehook覆盖。
下面是代码的修改版本,它将返回非零退出代码:

import aiohttp
import asyncio
import os
import sys

def custom_hook(args):
    os._exit(7)

async def main():
    client = aiohttp.ClientSession()
    await client.get("https://google.com")

sys.unraisablehook = custom_hook
asyncio.run(main())

请注意,我使用os._exit()而不是sys.exit(),这是因为sys.exit()会引发SystemExit异常,这在我们的示例中是一个坏主意。

相关问题