bpo-31657: add test coverage for the __debug__ case (optimization levels) by dianaclarke · Pull Request #3450 · python/cpython

The optimization levels in this test don't control what __debug__ evaluates to.

For example, note that the following modified test passes (I added the __debug__ value along side the debug_enabled value in a tuple).

$ git diff
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 87dcda7b43..32c9c9b044 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -328,9 +328,9 @@ class BuiltinTest(unittest.TestCase):
 
         codestr = '''def f():
         """doc"""
-        debug_enabled = False
+        debug_enabled = (False, __debug__)
         if __debug__:
-            debug_enabled = True
+            debug_enabled = (True, __debug__)
         try:
             assert False
         except AssertionError:
@@ -339,10 +339,10 @@ class BuiltinTest(unittest.TestCase):
             return (False, f.__doc__, debug_enabled)
         '''
         def f(): """doc"""
-        values = [(-1, __debug__, f.__doc__, __debug__),
-                  (0, True, 'doc', True),
-                  (1, False, 'doc', False),
-                  (2, False, None, False)]
+        values = [(-1, __debug__, f.__doc__, (__debug__, __debug__)),
+                  (0, True, 'doc', (True, True)),
+                  (1, False, 'doc', (False, True)),
+                  (2, False, None, (False, True))]
         for optval, assertval, docstring, debugval in values:
             # test both direct compilation and compilation via AST
             codeobjs = []