Skip to content
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

Closed
vstinner opened this issue Sep 28, 2015 · 15 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

BPO 25256
Nosy @brettcannon, @vstinner, @ezio-melotti, @alex, @berkerpeksag, @serhiy-storchaka, @matrixise
Files
  • is_debug_build.patch
  • debug_build-2.patch
  • 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:

    assignee = None
    closed_at = <Date 2016-03-25.23:33:09.313>
    created_at = <Date 2015-09-28.13:15:00.655>
    labels = ['type-feature', 'library']
    title = 'Add sys.debug_build public variable to check if Python was compiled in debug mode'
    updated_at = <Date 2016-03-25.23:33:09.312>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2016-03-25.23:33:09.312>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-03-25.23:33:09.313>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2015-09-28.13:15:00.655>
    creator = 'vstinner'
    dependencies = []
    files = ['40610', '40633']
    hgrepos = []
    issue_num = 25256
    keywords = ['patch']
    message_count = 14.0
    messages = ['251765', '251766', '251767', '251769', '251978', '252023', '252047', '252055', '252069', '252070', '257273', '257280', '257287', '262465']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'vstinner', 'ezio.melotti', 'alex', 'berker.peksag', 'serhiy.storchaka', 'matrixise']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25256'
    versions = ['Python 3.6']

    @vstinner
    Copy link
    Member Author

    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.

    @vstinner vstinner added the stdlib Python modules in the Lib dir label Sep 28, 2015
    @vstinner
    Copy link
    Member Author

    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')

    @matrixise
    Copy link
    Member

    In your Python unittest, could you change the comment, it's ambiguous.

    Thanks

    @matrixise
    Copy link
    Member

    Victor, I have tested your patch with the default branch (3.6), works fine on OSX Yosemite. In release and debug modes.

    @ezio-melotti ezio-melotti added the type-feature A feature request or enhancement label Sep 28, 2015
    @vstinner
    Copy link
    Member Author

    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

    @vstinner vstinner changed the 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 Sep 30, 2015
    @berkerpeksag
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @vstinner
    Copy link
    Member Author

    vstinner commented Oct 1, 2015

    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.

    @vstinner
    Copy link
    Member Author

    vstinner commented Oct 1, 2015

    FYI the sys module has 5 more symbols when CPython is compiled in debug mode:

    • getobjects()
    • gettotalrefcount()
    • last_traceback, last_type, last_value

    @vstinner
    Copy link
    Member Author

    vstinner commented Oct 1, 2015

    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.

    @ezio-melotti
    Copy link
    Member

    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.

    @serhiy-storchaka
    Copy link
    Member

    Implementing sys.abiflags on Windows looks more general solution, and it doesn't increase the complexity of the stdlib.

    @vstinner
    Copy link
    Member Author

    vstinner commented Jan 1, 2016

    The consensus was to add a new flag to sys.implementation.

    @vstinner
    Copy link
    Member Author

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @vstinner
    Copy link
    Member Author

    I pushed an uncontroversial change instead: 5185956

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants