Skip to content

Commit f0f93ba

Browse files
authored
gh-131942: Use the Python-specific Py_DEBUG macro rather than _DEBUG in Windows-related C code (GH-131944)
1 parent e15bbfa commit f0f93ba

File tree

13 files changed

+32
-28
lines changed

13 files changed

+32
-28
lines changed

Doc/c-api/intro.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,14 +826,17 @@ frequently used builds will be described in the remainder of this section.
826826
827827
Compiling the interpreter with the :c:macro:`!Py_DEBUG` macro defined produces
828828
what is generally meant by :ref:`a debug build of Python <debug-build>`.
829-
:c:macro:`!Py_DEBUG` is enabled in the Unix build by adding
830-
:option:`--with-pydebug` to the :file:`./configure` command.
831-
It is also implied by the presence of the
832-
not-Python-specific :c:macro:`!_DEBUG` macro. When :c:macro:`!Py_DEBUG` is enabled
833-
in the Unix build, compiler optimization is disabled.
829+
830+
On Unix, :c:macro:`!Py_DEBUG` can be enabled by adding :option:`--with-pydebug`
831+
to the :file:`./configure` command. This will also disable compiler optimization.
832+
833+
On Windows, selecting a debug build (e.g., by passing the :option:`-d` option to
834+
:file:`PCbuild/build.bat`) automatically enables :c:macro:`!Py_DEBUG`.
835+
Additionally, the presence of the not-Python-specific :c:macro:`!_DEBUG` macro,
836+
when defined by the compiler, will also implicitly enable :c:macro:`!Py_DEBUG`.
834837

835838
In addition to the reference count debugging described below, extra checks are
836-
performed, see :ref:`Python Debug Build <debug-build>`.
839+
performed. See :ref:`Python Debug Build <debug-build>` for more details.
837840

838841
Defining :c:macro:`Py_TRACE_REFS` enables reference tracing
839842
(see the :option:`configure --with-trace-refs option <--with-trace-refs>`).

Include/internal/pycore_importdl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extern int _PyImport_RunModInitFunc(
107107
#include <windows.h>
108108
typedef FARPROC dl_funcptr;
109109

110-
#ifdef _DEBUG
110+
#ifdef Py_DEBUG
111111
# define PYD_DEBUG_SUFFIX "_d"
112112
#else
113113
# define PYD_DEBUG_SUFFIX ""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use the Python-specific :c:macro:`Py_DEBUG` macro rather than :c:macro:`!_DEBUG` in Windows-related C code. Patch by Xuehai Pan.

Modules/_ctypes/callproc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module _ctypes
9292
#include <sanitizer/msan_interface.h>
9393
#endif
9494

95-
#if defined(_DEBUG) || defined(__MINGW32__)
95+
#if defined(Py_DEBUG) || defined(__MINGW32__)
9696
/* Don't use structured exception handling on Windows if this is defined.
9797
MingW, AFAIK, doesn't support it.
9898
*/

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4427,7 +4427,7 @@ _ssl__SSLContext_load_dh_params_impl(PySSLContext *self, PyObject *filepath)
44274427
FILE *f;
44284428
DH *dh;
44294429

4430-
#if defined(MS_WINDOWS) && defined(_DEBUG)
4430+
#if defined(MS_WINDOWS) && defined(Py_DEBUG)
44314431
PyErr_SetString(PyExc_NotImplementedError,
44324432
"load_dh_params: unavailable on Windows debug build");
44334433
return NULL;

Modules/_ssl/debughelpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ _PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg,
175175
PySSLContext *self = PySSLContext_CAST(op);
176176
FILE *fp;
177177

178-
#if defined(MS_WINDOWS) && defined(_DEBUG)
178+
#if defined(MS_WINDOWS) && defined(Py_DEBUG)
179179
PyErr_SetString(PyExc_NotImplementedError,
180180
"set_keylog_filename: unavailable on Windows debug build");
181181
return -1;

PC/launcher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static wchar_t * get_env(wchar_t * key)
140140
return buf;
141141
}
142142

143-
#if defined(_DEBUG)
143+
#if defined(Py_DEBUG)
144144
/* Do not define EXECUTABLEPATH_VALUE in debug builds as it'll
145145
never point to the debug build. */
146146
#if defined(_WINDOWS)

PC/pyconfig.h.in

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ WIN32 is still required for the locale module.
9494
#endif
9595
#endif /* Py_BUILD_CORE || Py_BUILD_CORE_BUILTIN || Py_BUILD_CORE_MODULE */
9696

97+
/* _DEBUG implies Py_DEBUG */
98+
#ifdef _DEBUG
99+
# define Py_DEBUG 1
100+
#endif
101+
97102
/* Define to 1 if you want to disable the GIL */
98103
/* Uncomment the definition for free-threaded builds, or define it manually
99104
* when compiling extension modules. Note that we test with #ifdef, so
@@ -319,21 +324,21 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
319324
This is relevant when using build-system generator (e.g CMake) where
320325
the linking is explicitly handled */
321326
# if defined(Py_GIL_DISABLED)
322-
# if defined(_DEBUG)
327+
# if defined(Py_DEBUG)
323328
# pragma comment(lib,"python315t_d.lib")
324329
# elif defined(Py_LIMITED_API)
325330
# pragma comment(lib,"python3t.lib")
326331
# else
327332
# pragma comment(lib,"python315t.lib")
328-
# endif /* _DEBUG */
333+
# endif /* Py_DEBUG */
329334
# else /* Py_GIL_DISABLED */
330-
# if defined(_DEBUG)
335+
# if defined(Py_DEBUG)
331336
# pragma comment(lib,"python315_d.lib")
332337
# elif defined(Py_LIMITED_API)
333338
# pragma comment(lib,"python3.lib")
334339
# else
335340
# pragma comment(lib,"python315.lib")
336-
# endif /* _DEBUG */
341+
# endif /* Py_DEBUG */
337342
# endif /* Py_GIL_DISABLED */
338343
# endif /* _MSC_VER && !Py_NO_LINK_LIB */
339344
# endif /* Py_BUILD_CORE */
@@ -376,11 +381,6 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
376381
# define ALIGNOF_MAX_ALIGN_T 8
377382
#endif
378383

379-
#ifdef _DEBUG
380-
# define Py_DEBUG
381-
#endif
382-
383-
384384
#ifdef MS_WIN32
385385

386386
#define SIZEOF_SHORT 2

PC/python_uwp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#include <winrt\Windows.Storage.h>
2020

2121
#ifdef PYTHONW
22-
#ifdef _DEBUG
22+
#ifdef Py_DEBUG
2323
const wchar_t *PROGNAME = L"pythonw_d.exe";
2424
#else
2525
const wchar_t *PROGNAME = L"pythonw.exe";
2626
#endif
2727
#else
28-
#ifdef _DEBUG
28+
#ifdef Py_DEBUG
2929
const wchar_t *PROGNAME = L"python_d.exe";
3030
#else
3131
const wchar_t *PROGNAME = L"python.exe";

PC/python_ver_rc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define MS_WINDOWS
1111
#include "modsupport.h"
1212
#include "patchlevel.h"
13-
#ifdef _DEBUG
13+
#ifdef Py_DEBUG
1414
# define PYTHON_DEBUG_EXT "_d"
1515
#else
1616
# define PYTHON_DEBUG_EXT

Python/dynload_win.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static char *GetPythonImport (HINSTANCE hModule)
108108
char *pch;
109109

110110
/* Don't claim that python3.dll is a Python DLL. */
111-
#ifdef _DEBUG
111+
#ifdef Py_DEBUG
112112
if (strcmp(import_name, "python3_d.dll") == 0) {
113113
#else
114114
if (strcmp(import_name, "python3.dll") == 0) {
@@ -120,7 +120,7 @@ static char *GetPythonImport (HINSTANCE hModule)
120120
/* Ensure python prefix is followed only
121121
by numbers to the end of the basename */
122122
pch = import_name + 6;
123-
#ifdef _DEBUG
123+
#ifdef Py_DEBUG
124124
while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
125125
#else
126126
while (*pch && *pch != '.') {
@@ -300,7 +300,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
300300
char buffer[256];
301301

302302
PyOS_snprintf(buffer, sizeof(buffer),
303-
#ifdef _DEBUG
303+
#ifdef Py_DEBUG
304304
"python%d%d_d.dll",
305305
#else
306306
"python%d%d.dll",

Python/marshal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module marshal
3838
* On Windows PGO builds, the r_object function overallocates its stack and
3939
* can cause a stack overflow. We reduce the maximum depth for all Windows
4040
* releases to protect against this.
41-
* #if defined(MS_WINDOWS) && defined(_DEBUG)
41+
* #if defined(MS_WINDOWS) && defined(Py_DEBUG)
4242
*/
4343
#if defined(MS_WINDOWS)
4444
# define MAX_MARSHAL_STACK_DEPTH 1000

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ static inline void _Py_NO_RETURN
31443144
fatal_error_exit(int status)
31453145
{
31463146
if (status < 0) {
3147-
#if defined(MS_WINDOWS) && defined(_DEBUG)
3147+
#if defined(MS_WINDOWS) && defined(Py_DEBUG)
31483148
DebugBreak();
31493149
#endif
31503150
abort();

0 commit comments

Comments
 (0)