-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sys.debug_build public variable to check if Python was compiled in debug mode #69443
Comments
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() 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. |
I wrote this patch while working on new tests for Lib/test/regrtest.py: issue bpo-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') |
In your Python unittest, could you change the comment, it's ambiguous. Thanks |
Victor, I have tested your patch with the default branch (3.6), works fine on OSX Yosemite. In release and debug modes. |
Updated patch:
|
LGTM
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 I left two trivial review comments on Rietveld. |
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 |
Hum, what is the problem of adding a symbol? How does it make the module less usable?
My patch changes distutils, sysconfig and warnings modules, I agree that other changes are only in tests.
For me it looks more like an hack than a reliable check.
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. |
FYI the sys module has 5 more symbols when CPython is compiled in debug mode:
|
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
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. |
If there is consensus about adding this to sys, then the latest patch LGTM (module a couple of unaddressed comments on Rietveld). |
Implementing sys.abiflags on Windows looks more general solution, and it doesn't increase the complexity of the stdlib. |
The consensus was to add a new flag to sys.implementation. |
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. |
I pushed an uncontroversial change instead: 5185956 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: