此代码发出四个基本相同的警告:
import warnings
warnings.warn("1") # warning at this line 3
warnings.warn("2",
stacklevel=1) # warning at this line 5
warnings.warn("3", # warning at this line 7
)
warnings.warn(
"4",) # warning at this line 10
warnings.warn(
"5", # warning at this line 13
)
我注意到的是报告的位置略有不同:
c:\Src\bug.py:3: UserWarning: 1
warnings.warn("1") # warning at this line 3
c:\Src\bug.py:5: UserWarning: 2
stacklevel=1) # warning at this line 5
c:\Src\bug.py:7: UserWarning: 3
warnings.warn("3", # warning at this line 7
c:\Src\bug.py:10: UserWarning: 4
"4",) # warning at this line 10
c:\Src\bug.py:13: UserWarning: 5
"5", # warning at this line 13
尤其是,警告2
和4
指向命令的结束行,而警告3
和5
则没有(基本上,尾随{{1 }}被忽略)。这是故意的吗?理想情况下,我希望在控制台上显示完整的命令,而不仅仅是一行。