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: Add sys.debug_build public variable to check if Python was compiled in debug mode
Type: enhancement Stage: commit review
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: alex, berker.peksag, brett.cannon, ezio.melotti, matrixise, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2015-09-28 13:15 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
is_debug_build.patch vstinner, 2015-09-28 13:14 review
debug_build-2.patch vstinner, 2015-09-30 23:16 review
Messages (14)
msg251765 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-09-28 13:14
Attached patch adds the sys.is_debug_build() public function and replaces hasattr(sys, xxx) tests to check for debug mode with sys.is_debug_build().

+.. function:: is_debug_build()
+
+   Return :const:`True` if the Python executable was compiled in debug mode,
+   return :const:`False` if it was compiled in release mode.
+
+   The debug mode is enabled with ``#define Py_DEBUG``, or with
+   ``./configure --with-pydebug``.
+
+   .. versionadded:: 3.6

I would like to add an obvious way to check if Python was compiled in debug mode, instead of having hacks/tips to check it.

For example, 3 different checks are proposed on StackOverflow and only one looks portable:

http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter

I don't think that we need to mark the function as an implementation detail or specific to CPython. Other implementations of Python would probably benefit from such flag. If they don't care, they can simply return False.

Alternative: Add a new sys.implementation.debug_build flag.

Note: I chose the "is_debug_build" name using the existing sysconfig.is_python_build(). There is a sys.flags.debug flag, so "is_debug()" can be confusing. I prefer to attach the "build" suffix.
msg251766 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-09-28 13:18
I wrote this patch while working on new tests for Lib/test/regrtest.py: issue #25220. The Windows scripts PCbuild/rt.bat (and Tools/buildbot/test.bat) requires a "-d" command line option if the Python was compiled in debug mode. The flag is used to choose the name of the Python executable: python.exe or python_d.exe. In my patch, I used:

Py_DEBUG = hasattr(sys, 'getobjects')
msg251767 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-09-28 13:28
In your Python unittest, could you change the comment, it's ambiguous.

Thanks
msg251769 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-09-28 13:46
Victor, I have tested your patch with the default branch (3.6), works fine on OSX Yosemite. In release and debug modes.
msg251978 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-09-30 23:16
Updated patch:

* replace sys.is_debug_build() function with sys.debug_build variable
* use also sys.debug_build in test_regrtest.py
* remove the paragraph on how the debug module can be enabled when compiling CPython: it's too specific to CPython, and not really interesting in the (sys) library doc.
* mention the new function in What's New in Python 3.6
msg252023 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-10-01 12:36
LGTM

> Alternative: Add a new sys.implementation.debug_build flag.

According to the sys.implementation documentation and PEP 421, we can only add a private attribute without writing a PEP. But I find sys.implementation._debug_build too long and ``from sys import implementation; implementation._debug_build``(or ``from sys import implementation as i; i._debug_build``) is also not easy to write. So I'm +1 to sys.debug_build.

I left two trivial review comments on Rietveld.
msg252047 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-10-01 19:54
I don't like this. The sys module is one of most used module, but it has too many members, and adding yet one makes the situation worse.

>>> len(dir(sys))
81

Checking for debug mode is not often needed, and mainly in tests. Current way ``hasattr(sys, 'gettotalrefcount')`` works good. You also can check ``'d' in sys.abiflags`` if it looks cleaner to you. Or add a member to test.support.
msg252055 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-10-01 20:15
> I don't like this. The sys module is one of most used module, but it has too many members, and adding yet one makes the situation worse.

Hum, what is the problem of adding a symbol? How does it make the module less usable?

> Checking for debug mode is not often needed, and mainly in tests.

My patch changes distutils, sysconfig and warnings modules, I agree that other changes are only in tests.

>  Current way ``hasattr(sys, 'gettotalrefcount')`` works good.

For me it looks more like an hack than a reliable check.

> You also can check ``'d' in sys.abiflags`` if it looks cleaner to you.

For me, it doesn't look correct to have various ways to check if python was compiled in debug mode. It doesn't look portable neither. I prefer to use a flag which works on any version of Python (>= 3.6) and any implementation of Python.

I don't think that PyPy wants to implement sys.gettotalrefcount() for example, but PyPy may want to mimick CPython when it's compiled in debug mode. For example, display warnings by default in debug mode.
msg252069 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-10-01 22:27
FYI the sys module has 5 more symbols when CPython is compiled in debug mode:

* getobjects()
* gettotalrefcount()
* last_traceback, last_type, last_value
msg252070 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-10-01 22:33
>> You also can check ``'d' in sys.abiflags`` if it looks cleaner to you.

> For me, it doesn't look correct to have various ways to check if python was compiled in debug mode. It doesn't look portable neither. (...)

Oops, I didn't notice that sys.abiflags is not available on Windows!

It's the same issue with 2 solutions to this question:

http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter

* Checking for '_d.pyd' in imp.get_suffixes() => specific to Windows
* sys.executable.endswith("_d.exe") => again, specific to Windows :-(

That's part of my rationale in my first message, we need a portable and reliable flag to check if Python was compiled in debug mode.

By the way, the StackOverflow question comes from an user who is probably not writing a test, but an application. It means that the flag is also helpful to final users.
msg257273 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-01 05:11
If there is consensus about adding this to sys, then the latest patch LGTM (module a couple of unaddressed comments on Rietveld).
If not, you should probably bring this up to python-dev.
msg257280 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-01 07:59
Implementing sys.abiflags on Windows looks more general solution, and it doesn't increase the complexity of the stdlib.
msg257287 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-01 12:48
The consensus was to add a new flag to sys.implementation.
msg262465 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-03-25 23:33
I lost interest in this issue. It really looks like a corner case, so I prefer to keep the current code. I'm not interested to work on the abiflags on Windows.
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69443
2016-03-25 23:33:09vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg262465
2016-01-01 12:48:50vstinnersetmessages: + msg257287
2016-01-01 07:59:34serhiy.storchakasetmessages: + msg257280
2016-01-01 05:11:22ezio.melottisetmessages: + msg257273
2015-10-01 22:33:26vstinnersetmessages: + msg252070
2015-10-01 22:27:05vstinnersetmessages: + msg252069
2015-10-01 20:15:05vstinnersetmessages: + msg252055
2015-10-01 19:54:18serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg252047
2015-10-01 12:36:49berker.peksagsetnosy: + berker.peksag

messages: + msg252023
stage: patch review -> commit review
2015-09-30 23:16:13vstinnersetfiles: + debug_build-2.patch

messages: + msg251978
title: Add sys.is_debug_build() public function to check if Python was compiled in debug mode -> Add sys.debug_build public variable to check if Python was compiled in debug mode
2015-09-28 14:16:51ezio.melottisetnosy: + ezio.melotti

type: enhancement
stage: patch review
2015-09-28 13:46:44matrixisesetmessages: + msg251769
2015-09-28 13:28:08matrixisesetnosy: + matrixise
messages: + msg251767
2015-09-28 13:18:30vstinnersetmessages: + msg251766
2015-09-28 13:15:41vstinnersetnosy: + brett.cannon, alex
2015-09-28 13:15:00vstinnercreate