zoukankan      html  css  js  c++  java
  • python assert 在正式产品里禁用的手法 直接-O即可

    How do I disable assertions in Python?

    There are multiple approaches that affect a single process, the environment, or a single line of code.

    I demonstrate each.

    For the whole process

    Using the -O flag (capital O) disables all assert statements in a process.

    For example:

    $ python -Oc "assert False"
    
    $ python -c "assert False"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    AssertionError
    

    Note that by disable I mean it also does not execute the expression that follows it:

    $ python -Oc "assert 1/0"
    
    $ python -c "assert 1/0"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ZeroDivisionError: integer division or modulo by zero
    

    For the environment

    You can use an environment variable to set this flag as well.

    This will affect every process that uses or inherits the environment.

    E.g., in Windows, setting and then clearing the environment variable:

    C:>python -c "assert False"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    AssertionError
    C:>SET PYTHONOPTIMIZE=TRUE
    
    C:>python -c "assert False"
    
    C:>SET PYTHONOPTIMIZE=
    
    C:>python -c "assert False"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    AssertionError
    

    Same in Unix (using set and unset for respective functionality)

    Single point in code

    You continue your question:

    if an assertion fails, I don't want it to throw an AssertionError, but to keep going.

    If you want the code that fails to be exercised, you can catch an assertion error:

    try:
        assert False, "we know this fails"
    except AssertionError as e:
        print(repr(e))
    

    which prints:

    AssertionError('we know this fails',)
    

    and you'll keep going from the point you handled the AssertionError.

    References

    From the assert documentation:

    An assert statement like this:

    assert expression #, optional_message
    

    Is equivalent to

    if __debug__:
        if not expression: raise AssertionError #(optional_message)
    

    And,

    the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O).

    From the usage docs:

    -O

    Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. See also PYTHONOPTIMIZE.

    and

    PYTHONOPTIMIZE

    If this is set to a non-empty string it is equivalent to specifying the -O option. If set to an integer, it is equivalent to specifying -O multiple times.

  • 相关阅读:
    SQL中的字符串字段根据某字段实现自增
    SQL中的字符串字段实现自增
    ECS Windows服务器IIS FTP登陆提示“530 valid hostname is expected”
    HTML中动态生成内容的事件绑定问题
    JavaScript对JSON数据进行排序和搜索
    IIS站点下多应用程序 C#获取根目录方法
    asp.net中form表单多个按钮submit提交到后台的实例
    C#从一个SqlCommand对象生成可执行的SQL语句
    传递参数
    js创建jsonArray传输至后台及后台解析
  • 原文地址:https://www.cnblogs.com/bonelee/p/11413905.html
Copyright © 2011-2022 走看看