Asserts are not removed with PYTHONOPTIMIZE

Issue #2929 new
Pavel Ivashkov
created an issue

When optimization flag set, assert should be removed:

-O     : skip assert statements; also PYTHONOPTIMIZE=x
-OO    : remove docstrings when importing modules in addition to -O

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.

Actual result:

$ pypy3 -O
Python 3.5.3 (fdd60ed87e941677e8ea11acf9f1819466521bf2, Jul 30 2018, 08:59:51)
[PyPy 6.0.0 with GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> __debug__
False
>>>> assert False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

What was expected:

$ python3 -O
Python 3.7.0 (default, Jun 28 2018, 05:55:06)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __debug__
False
>>> assert False
>>>

Comments (3)

  1. Armin Rigo

    It's because the PyPy2 logic was not copied to PyPy3. In PyPy3 we make an attempt to be closer to CPython, but fail in this case. In PyPy2, we don't try to generate different versions of the bytecode but instead generate just one, which does basically if __debug__ and not condition: raise AssertionError. As a result, PyPy2 only writes .pyc files and not .pyo. On PyPy3 we produce __pycache__/foo.pypy3-60.opt-1.pyc, and I didn't find so far where the opt-1 is added (still looking).

    ...ah, found it in lib-python/3/importlib/_bootstrap_external:cache_from_source

  2. Armin Rigo

    Of course, if I were to fix this issue I'd make PyPy3 behave like PyPy2. Maybe I shouldn't do that until we get some discussion about whether that is a good idea.

  3. Log in to comment