This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unit test for optimization levels does not cover __debug__ case
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mariatta, diana
Priority: normal Keywords: patch

Created on 2017-10-01 15:08 by diana, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3450 closed diana, 2017-10-01 15:21
Messages (3)
msg303465 - (view) Author: diana (diana) * Date: 2017-10-01 15:08
There are currently three supported optimization levels (0, 1, and 2). Briefly summarized, they do the following.

    0: no optimizations
    1: remove assert statements and __debug__ blocks
    2: remove docstrings, assert statements, and __debug__ blocks

The current compile() tests for optimization levels in Lib/test/test_builtin.py covers the assert and docstring cases, but it doesn't test that __debug__ code blocks are included or excluded based on the optimization level.

For example, if you change Python/compile.c to always include __debug__ blocks regardless of the optimization level, the existing compile() tests will continue to pass.

$ git diff Python/compile.c
diff --git a/Python/compile.c b/Python/compile.c
index 280ddc39e3..d65df098bb 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4143,7 +4143,7 @@ expr_constant(struct compiler *c, expr_ty e)
         /* optimize away names that can't be reassigned */
         id = PyUnicode_AsUTF8(e->v.Name.id);
         if (id && strcmp(id, "__debug__") == 0)
-            return !c->c_optimize;
+            return 1;
         return -1;
     case NameConstant_kind: {
         PyObject *o = e->v.NameConstant.value;
msg303630 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-10-03 16:46
New changeset 543386b7f077d210ea0722079d68beb6c066730a by Mariatta (diana) in branch 'master':
bpo-31657: Add test coverage for the __debug__ case (GH-3450)
https://github.com/python/cpython/commit/543386b7f077d210ea0722079d68beb6c066730a
msg303724 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-10-04 18:25
Thanks for this enhancement, Diana.

Since PR has been merged, and backport is not needed, I think this can be closed now. :)
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75838
2017-10-04 18:25:01Mariattasetstatus: open -> closed
versions: + Python 3.7
messages: + msg303724

resolution: fixed
stage: patch review -> resolved
2017-10-03 16:46:58Mariattasetnosy: + Mariatta
messages: + msg303630
2017-10-01 15:21:22dianasetkeywords: + patch
stage: patch review
pull_requests: + pull_request3827
2017-10-01 15:08:33dianacreate