Disabling python's assert() without -0 flag

前端 未结 3 2169
悲哀的现实
悲哀的现实 2020-12-15 09:26

I\'m running a python script from inside a different software (it provides a python interface to manipulate its data structures).

I\'m optimizing my code for speed

相关标签:
3条回答
  • 2020-12-15 09:44

    The docs say,

    The value for the built-in variable [__debug__] is determined when the interpreter starts.

    So, if you can not control how the python interpreter is started, then it looks like you can not disable assert.

    Here then are some other options:

    1. The safest way is to manually remove all the assert statements.
    2. If all your assert statements occur on lines by themselves, then perhaps you could remove them with

      sed -i 's/assert /pass #assert /g' script.py
      

      Note that this will mangle your code if other code comes after the assert. For example, the sed command above would comment-out the return in a line like this:

      assert x; return True
      

      which would change the logic of your program.

      If you have code like this, it would probably be best to manually remove the asserts.

    3. There might be a way to remove them programmatically by parsing your script with the tokenize module, but writing such a program to remove asserts may take more time than it would take to manually remove the asserts, especially if this is a one-time job.

    4. If the other piece of software accepts .pyc files, then there is a dirty trick which seems to work on my machine, though note a Python core developer warns against this (See Éric Araujo's comment on 2011-09-17). Suppose your script is called script.py.

      • Make a temporary script called, say, temp.py:

        import script
        
      • Run python -O temp.py. This creates script.pyo.
      • Move script.py and script.pyc (if it exists) out of your PYTHONPATH or whatever directory the other software is reading to find your script.
      • Rename script.pyo --> script.pyc.

      Now when the other software tries to import your script, it will only find the pyc file, which has the asserts removed.

      For example, if script.py looks like this:

      assert False
      print('Got here')
      

      then running python temp.py will now print Got here instead of raising an AssertionError.

    0 讨论(0)
  • 2020-12-15 09:51

    As @unutbu describes, there is no official way of doing this. However, a simple strategy is to define a flag like _test somewhere (for example, as keyword argument to a function, or as a global variable in a module), then include this in your assert statements as follows:

    def f(x, _test=True):
        assert not _test or x > 0
        ...
    

    Then you can disable asserts in that function if needed.

    f(x, _test=False)
    
    0 讨论(0)
  • 2020-12-15 09:52

    You may be able to do this with an environment variable, as described in this other answer. Setting PYTHONOPTIMIZE=1 is equivalent to starting Python with the -O option. As an example, this works in Blender 2.78, which embeds Python 3.5:

    blender --python-expr 'assert False; print("foo")'
    
    PYTHONOPTIMIZE=1 blender --python-expr 'assert False; print("foo")'
    

    The first command prints a traceback, while the second just prints "foo".

    0 讨论(0)
提交回复
热议问题