【发布时间】:2021-03-16 17:08:17
【问题描述】:
当明确告诉 Python 将 RuntimeWarning 视为异常时,Warning 成功变为 Exception。但是现在我得到了一个Exception ignored,并且程序仍然没有在它应该失败的地方失败。最小可重现示例:
import asyncio
import warnings
async def main():
asyncio.sleep(1)
print("I don't want this printed.")
warnings.filterwarnings("error", category=RuntimeWarning)
asyncio.run(main())
Exception ignored in: <coroutine object sleep at 0x7f1e4f3d1b40>
Traceback (most recent call last):
File "/home/teresaejunior/.asdf/installs/python/3.9.2/lib/python3.9/warnings.py", line 506, in _warn_unawaited_coroutine
warn(msg, category=RuntimeWarning, stacklevel=2, source=coro)
RuntimeWarning: coroutine 'sleep' was never awaited
I don't want this printed.
如何阻止 Python 忽略 warnings 模块引发的异常?
【问题讨论】:
-
“请问如何阻止 Python 忽略这一点?”始终等待致电
asyncio.sleep -
@ranisalt 我会尝试重写我的问题以避免这种情况,但我在第一段中确实提到我有时会忘记包含等待。我希望 Python 失败,不要忽略这些错误,继续不睡觉......
-
@TeresaeJunior 你可能选择了 wrooooong 语言。它怎么知道这是错误的?这有点像责怪你的车不是卡车。你选的车……
-
@JaredSmith 该程序应该在设置
warnings.filterwarnings("error", category=RuntimeWarning)时终止。它只是没有。
标签: python asynchronous async-await python-asyncio