Issue #25256: Add sys.debug_build?

Hi, I created the issue "Add sys.debug_build public variable to check if Python was compiled in debug mode": http://bugs.python.org/issue25256 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. On the Internet, I found various recipes to check if Python is compiled is debug mode. Sadly, some of them are not portable. For example, 3 different checks are proposed on StackOverflow but 2 of them are specific to Windows: http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpr... Even if the exact impact of a debug build depends on the Python implementation and the Python version, we can use it to have the same behaviour on all Python implementations. For example, the warnings module shows warnings by default if Python is compiled in debug mode: Extract of my patch: - if hasattr(sys, 'gettotalrefcount'): + if sys.debug_build: resource_action = "always" else: resource_action = "ignore" Alternative: Add a new sys.implementation.debug_build flag. Problem: extending sys.implementation requires a new PEP, and I don't think that debug_build fits into this object. Berker Peksag likes the idea. Serhiy Storchaka dislike the new flag: "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." (sys has 81 symbols) "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." The name "debug_build" comes from the existing sysconfig.is_python_build() function. There is a sys.flags.debug flag, so "sys.debug" can be confusing. I prefer to attach the "build" suffix. First I proposed a function sys.is_debug_build(), but a flag is simpler than a function. There is not need to compute a version it's known at build time. What do you think? Should we add sys.debug_build? Victor

2015-10-02 9:37 GMT+02:00 Nick Coghlan <ncoghlan@gmail.com>:
Spell it as "sys.implementation.debug_build" and I'm in favour.
Oh, in fact, I don't have no preference between sys.debug_flag and sys.implementation.debug_flag. If I understood correctly, Serhiy would prefer sys.implementation.debug_flag because he doesn't want to add yet another symbol to the sys namespace. But Berker Peksag wrote: "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." Should I write a PEP for a new field in sys.implementation? Victor

On Oct 02, 2015, at 11:46 AM, Victor Stinner wrote:
Should I write a PEP for a new field in sys.implementation?
Specifically PEP 421 says that a PEP is needed if the new sys.implementation attribute is required to be defined in all implementations, i.e. it's a new required attribute. Will debug_build be required of all implementations? The PEP can be short. https://www.python.org/dev/peps/pep-0421/#id30 If it's only relevant for CPython then an underscore-prefix symbol in sys.implementation is the right place for it, and no PEP is needed. Just open an issue on the tracker. Cheers, -Barry

Whats wrong with:
sysconfig.get_config_var('Py_DEBUG') 0
Nir On Fri, Oct 2, 2015 at 10:18 AM, Victor Stinner <victor.stinner@gmail.com> wrote:
Hi,
I created the issue "Add sys.debug_build public variable to check if Python was compiled in debug mode": http://bugs.python.org/issue25256
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. On the Internet, I found various recipes to check if Python is compiled is debug mode. Sadly, some of them are not portable. For example, 3 different checks are proposed on StackOverflow but 2 of them are specific to Windows:
http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpr...
Even if the exact impact of a debug build depends on the Python implementation and the Python version, we can use it to have the same behaviour on all Python implementations. For example, the warnings module shows warnings by default if Python is compiled in debug mode: Extract of my patch:
- if hasattr(sys, 'gettotalrefcount'): + if sys.debug_build: resource_action = "always" else: resource_action = "ignore"
Alternative: Add a new sys.implementation.debug_build flag. Problem: extending sys.implementation requires a new PEP, and I don't think that debug_build fits into this object.
Berker Peksag likes the idea.
Serhiy Storchaka dislike the new flag: "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." (sys has 81 symbols) "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."
The name "debug_build" comes from the existing sysconfig.is_python_build() function. There is a sys.flags.debug flag, so "sys.debug" can be confusing. I prefer to attach the "build" suffix.
First I proposed a function sys.is_debug_build(), but a flag is simpler than a function. There is not need to compute a version it's known at build time.
What do you think? Should we add sys.debug_build?
Victor _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/nirsof%40gmail.com

2015-10-02 13:16 GMT+02:00 Nir Soffer <nirsof@gmail.com>:
Whats wrong with:
sysconfig.get_config_var('Py_DEBUG') 0
Again, refer to my first message "On the Internet, I found various recipes to check if Python is compiled is debug mode. Sadly, some of them are not portable." I don't think that sysconfig.get_config_var('Py_DEBUG') will work on other Python implementations. On Windows, there is no such file like "Makefile" used to fill syscofig.get_config_vars() :-( sysconfig._init_non_posix() only fills a few variables like BINDIR or INCLUDEPY, but not Py_DEBUG. Victor

Speaking of other python implementations - why would you even care? (the pypy debug build has very different properties and does very different stuff for example). I would be very happy to have this clearly marked as implementation-dependent and that's why it would be cool to not be in sys (there are already 5 symbols there for this reason, so hasattr totalrefcount is cool enough) On Fri, Oct 2, 2015 at 2:19 PM, Victor Stinner <victor.stinner@gmail.com> wrote:
2015-10-02 13:16 GMT+02:00 Nir Soffer <nirsof@gmail.com>:
Whats wrong with:
sysconfig.get_config_var('Py_DEBUG') 0
Again, refer to my first message "On the Internet, I found various recipes to check if Python is compiled is debug mode. Sadly, some of them are not portable."
I don't think that sysconfig.get_config_var('Py_DEBUG') will work on other Python implementations.
On Windows, there is no such file like "Makefile" used to fill syscofig.get_config_vars() :-( sysconfig._init_non_posix() only fills a few variables like BINDIR or INCLUDEPY, but not Py_DEBUG.
Victor _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/fijall%40gmail.com

On 02.10.15 10:18, Victor Stinner wrote:
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. On the Internet, I found various recipes to check if Python is compiled is debug mode. Sadly, some of them are not portable.
I agree with Maciej. Why do you need to check if Python is compiled in debug mode? Because either you need to use some feature that is available only in debug mode (such as sys.gettotalrefcount), or to distinguish debug and non-debug binaries (in sysconfig and distutils), or make the behaviour different in debug mode (in warnings), or handle other behaviour differences (such as additional asserts or different stack consumption). In the first case you should just explicitly check the existence of related function. In the second case I suggest to use sys.abiflags as a suffix (likely you want to distinguish pymalloc from non-pymalloc build), or at least make a suffix depended on sys.abiflags. In the third case perhaps we have to set default warning level depending on sys.flags.debug, or sys.flags.verbose, or other dynamic flags. In the fourth case there is no good solution, but in any case this behaviour is implementation specific, and other implementation can have different consistency checks and different limits.
participants (6)
-
Barry Warsaw
-
Maciej Fijalkowski
-
Nick Coghlan
-
Nir Soffer
-
Serhiy Storchaka
-
Victor Stinner