忽略来自`PYTHONWARNINGS ="错误::警告`的Python语法错误?

时间:2018-03-07 04:20:51

标签: python warnings deprecation-warning

我使用PYTHONWARNINGS="error::Warning"来引发警告异常,但我想忽略某些此类异常,这通常很好。如果结果为SyntaxError,我有时会忽略这一点,但不知道如何。

详细示例:

# has_deprecated_syntax.py
"""this becomes invalid: \* """

然后:

`$PYTHONWARNINGS="error::Warning" python3.6 -c "import     has_deprecated_syntax"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/davidchudzicki/temp/so/has_deprecated_syntax.py", line 1
    """this becomes invalid: \* """
    ^
SyntaxError: invalid escape sequence \*

这些忽视它的尝试都让我失败了:

PYTHONWARNINGS="error::Warning,default::Warning:has_deprecated_syntax" python3.6 -c "import has_deprecated_syntax" 

PYTHONWARNINGS="error::Warning,default::SyntaxError" python3.6 -c "import has_deprecated_syntax"

作为参考,这里有什么让我觉得这是正确的环境设置,一般忽略来自特定模块的警告:

使用:

# has_warning.py
import warnings
warnings.warn("hi")

......我明白了:

$PYTHONWARNINGS="error::Warning" python -c "import has_warning"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "has_warning.py", line 3, in <module>
    warnings.warn("hi")
UserWarning: hi

......我可以忽略:

$PYTHONWARNINGS="error::Warning,default::Warning:has_warning" python -c 
"import has_warning"
has_warning.py:3: UserWarning: hi
  warnings.warn("hi")

但它对SyntaxError不起作用。有没有办法忽略特定模块的那些?

(注意:我已经为这个问题构建了一个小例子,但我关心的真正原因是我希望在我帮助的包的持续集成中出现警告失败,但我得到了{{1}当我这样做时,从我们的一个依赖项中获取。)

1 个答案:

答案 0 :(得分:0)

您不能忽略SyntaxError。这不是警告,这是一个错误。