python / python/cpython

Build Windows debug extensions without debug Python

Offen
#142,760 8 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

build OS-windows type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature or enhancement

Proposal:

Summary of a problem

On Windows when users are building a debug version of anything, including Python extensions, they typically provide /MDd/MTd flag to link against the debug version of the C runtime library.

Which automatically sets _DEBUG, which, historically, sets Py_DEBUG macro.

Py_DEBUG macro makes it impossible to link against release version of Python library:

  • because of implicit linking against _d.lib by # pragma comment(lib,"python315_d.lib").
    This restriction was overcomed in Python 3.14 by introduction of Py_NO_LINK_LIB macro.

  • Py_DEBUG also automatically sets Py_REF_DEBUG, which in turn enables additional symbols (namely Py_NegativeRefcount, Py_INCREF_IncRefTotal, Py_DECREF_DecRefTotal), which of course are not present in Release library, so linking Debug build against it is still impossible.
    But if we disable Py_REF_DEBUG when Py_DEBUG is set, then linking succeeds, so there are no more obstacles
    to build debug extensions against release Python.

Typically, users don't want to use debug Python for their extensions, they just want debug version of their own extension, as debug Python binaries are not widely available and even if they are, after build user now would also need debug versions of the entire extensions ecosystem they were using.

So it would be great to provide an option to finally figure this out after all those years.


Changes

There are two approaches to consider (PR currently implements both of them just to showcase the options, but we need to pick one):

  • Py_NO_PY_REF_DEBUG - macro to avoid setting Py_REF_DEBUG based on Py_DEBUG.
    It's a minimal change change to overcome the current problem.
    It seems currently there are no implications when set Py_DEBUG would conflict with unset Py_REF_DEBUG, but Python developers will need to keep it in mind in future that Py_REF_DEBUG might be unset even in debug builds.

  • Py_WIN_NO_PY_DEBUG - that would make _DEBUG not imply Py_DEBUG.
    Honestly, I would prefer this option, as it seems to be a fix for the root cause of the problem and makes flags more consistent between Windows and other platforms - making Py_DEBUG deliberate choice on both Windows and Unix systems.
    _DEBUG implying Py_DEBUG seems to be mostly historical thing introduced awhile ago (https://github.com/python/cpython/commit/9f9fa6d0c937a2cbc51feb3c92212688b09235f0) and they're not closely related.
    Adding Py_WIN_NO_PY_DEBUG also opens an opportunity in the future to completely deprecate _DEBUG setting Py_DEBUG.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/building-debug-version-of-extensions-on-windows-without-debug-binaries

Linked PRs
  • gh-142761

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften PR gh-142761 und der Discourse-Diskussion und verfolge dann die hier beschriebenen Py_DEBUG-, Py_REF_DEBUG-, Py_NO_LINK_LIB-, Py_NO_PY_REF_DEBUG- und Py_WIN_NO_PY_DEBUG-Einstiegspunkte. Als abgeschlossen gilt, wenn ein vorgeschlagener Makro-Ansatz ausgewählt und validiert wurde, dass Windows-Debug-Erweiterungen gegen Release-Python gelinkt werden können, ohne Debug-only-Symbole zu benötigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
build-system
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.