diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst index 76d7d5793428f8..9634e053527054 100644 --- a/Doc/c-api/intro.rst +++ b/Doc/c-api/intro.rst @@ -832,6 +832,13 @@ It is also implied by the presence of the not-Python-specific :c:macro:`!_DEBUG` macro. When :c:macro:`!Py_DEBUG` is enabled in the Unix build, compiler optimization is disabled. +.. note:: + On Windows, the :c:macro:`!Py_DEBUG` macro is not defined by default. + + The :option:`--with-pydebug` option is not available on Windows. Instead, the + not-Python-specific :c:macro:`!_DEBUG` macro can be defined by the compiler + options and that will enable the :c:macro:`!Py_DEBUG` macro. + In addition to the reference count debugging described below, extra checks are performed, see :ref:`Python Debug Build `. diff --git a/Include/internal/pycore_importdl.h b/Include/internal/pycore_importdl.h index 525a16f6b97274..3ba9229cc21378 100644 --- a/Include/internal/pycore_importdl.h +++ b/Include/internal/pycore_importdl.h @@ -107,7 +107,7 @@ extern int _PyImport_RunModInitFunc( #include typedef FARPROC dl_funcptr; -#ifdef _DEBUG +#ifdef Py_DEBUG # define PYD_DEBUG_SUFFIX "_d" #else # define PYD_DEBUG_SUFFIX "" diff --git a/Misc/NEWS.d/next/Windows/2025-03-31-15-37-57.gh-issue-131942.jip_aL.rst b/Misc/NEWS.d/next/Windows/2025-03-31-15-37-57.gh-issue-131942.jip_aL.rst new file mode 100644 index 00000000000000..837f7265bba246 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-03-31-15-37-57.gh-issue-131942.jip_aL.rst @@ -0,0 +1 @@ +Use the Python-specific :c:macro:`Py_DEBUG` macro rather than :c:macro:`!_DEBUG` in Windows-related C code. Patch by Xuehai Pan. diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 158422fb9b0471..b316706be84481 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -92,7 +92,7 @@ module _ctypes #include #endif -#if defined(_DEBUG) || defined(__MINGW32__) +#if defined(Py_DEBUG) || defined(__MINGW32__) /* Don't use structured exception handling on Windows if this is defined. MingW, AFAIK, doesn't support it. */ diff --git a/Modules/_ssl.c b/Modules/_ssl.c index e6bfbe46add9e9..75f97ecd6b1954 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -4427,7 +4427,7 @@ _ssl__SSLContext_load_dh_params_impl(PySSLContext *self, PyObject *filepath) FILE *f; DH *dh; -#if defined(MS_WINDOWS) && defined(_DEBUG) +#if defined(MS_WINDOWS) && defined(Py_DEBUG) PyErr_SetString(PyExc_NotImplementedError, "load_dh_params: unavailable on Windows debug build"); return NULL; diff --git a/Modules/_ssl/debughelpers.c b/Modules/_ssl/debughelpers.c index 7c0b4876f4353a..f0a0a1674f32bd 100644 --- a/Modules/_ssl/debughelpers.c +++ b/Modules/_ssl/debughelpers.c @@ -175,7 +175,7 @@ _PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg, PySSLContext *self = PySSLContext_CAST(op); FILE *fp; -#if defined(MS_WINDOWS) && defined(_DEBUG) +#if defined(MS_WINDOWS) && defined(Py_DEBUG) PyErr_SetString(PyExc_NotImplementedError, "set_keylog_filename: unavailable on Windows debug build"); return -1; diff --git a/PC/clinic/msvcrtmodule.c.h b/PC/clinic/msvcrtmodule.c.h index a77d0855af293f..8cb1a5b5c6efa9 100644 --- a/PC/clinic/msvcrtmodule.c.h +++ b/PC/clinic/msvcrtmodule.c.h @@ -517,7 +517,7 @@ msvcrt_ungetwch(PyObject *module, PyObject *arg) #endif /* defined(MS_WINDOWS_DESKTOP) */ -#if defined(_DEBUG) +#if defined(Py_DEBUG) PyDoc_STRVAR(msvcrt_CrtSetReportFile__doc__, "CrtSetReportFile($module, type, file, /)\n" @@ -562,9 +562,9 @@ msvcrt_CrtSetReportFile(PyObject *module, PyObject *const *args, Py_ssize_t narg return return_value; } -#endif /* defined(_DEBUG) */ +#endif /* defined(Py_DEBUG) */ -#if defined(_DEBUG) +#if defined(Py_DEBUG) PyDoc_STRVAR(msvcrt_CrtSetReportMode__doc__, "CrtSetReportMode($module, type, mode, /)\n" @@ -609,9 +609,9 @@ msvcrt_CrtSetReportMode(PyObject *module, PyObject *const *args, Py_ssize_t narg return return_value; } -#endif /* defined(_DEBUG) */ +#endif /* defined(Py_DEBUG) */ -#if defined(_DEBUG) +#if defined(Py_DEBUG) PyDoc_STRVAR(msvcrt_set_error_mode__doc__, "set_error_mode($module, mode, /)\n" @@ -648,7 +648,7 @@ msvcrt_set_error_mode(PyObject *module, PyObject *arg) return return_value; } -#endif /* defined(_DEBUG) */ +#endif /* defined(Py_DEBUG) */ #if (defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM)) @@ -731,4 +731,4 @@ msvcrt_SetErrorMode(PyObject *module, PyObject *arg) #ifndef MSVCRT_GETERRORMODE_METHODDEF #define MSVCRT_GETERRORMODE_METHODDEF #endif /* !defined(MSVCRT_GETERRORMODE_METHODDEF) */ -/*[clinic end generated code: output=692c6f52bb9193ce input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5cbbc70157dcfaed input=a9049054013a1b77]*/ diff --git a/PC/launcher.c b/PC/launcher.c index 47fafbc3bf6bad..40e6630b82e17a 100644 --- a/PC/launcher.c +++ b/PC/launcher.c @@ -140,7 +140,7 @@ static wchar_t * get_env(wchar_t * key) return buf; } -#if defined(_DEBUG) +#if defined(Py_DEBUG) /* Do not define EXECUTABLEPATH_VALUE in debug builds as it'll never point to the debug build. */ #if defined(_WINDOWS) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index b170e06b47dd59..60b53cb39aaf4d 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -415,7 +415,7 @@ msvcrt_ungetwch_impl(PyObject *module, int unicode_char) #endif /* MS_WINDOWS_DESKTOP */ -#ifdef _DEBUG +#ifdef Py_DEBUG /*[clinic input] msvcrt.CrtSetReportFile -> HANDLE @@ -490,7 +490,7 @@ msvcrt_set_error_mode_impl(PyObject *module, int mode) return res; } -#endif /* _DEBUG */ +#endif /* Py_DEBUG */ #if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_APP) || defined(MS_WINDOWS_SYSTEM) @@ -604,7 +604,7 @@ exec_module(PyObject* m) INSERTINT(m, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX); INSERTINT(m, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX); #endif -#ifdef _DEBUG +#ifdef Py_DEBUG INSERTINT(m, "CRT_WARN", _CRT_WARN); INSERTINT(m, "CRT_ERROR", _CRT_ERROR); INSERTINT(m, "CRT_ASSERT", _CRT_ASSERT); diff --git a/PC/pyconfig.h.in b/PC/pyconfig.h.in index 9e70303868e5de..dae2a22c3ec49c 100644 --- a/PC/pyconfig.h.in +++ b/PC/pyconfig.h.in @@ -94,6 +94,11 @@ WIN32 is still required for the locale module. #endif #endif /* Py_BUILD_CORE || Py_BUILD_CORE_BUILTIN || Py_BUILD_CORE_MODULE */ +/* _DEBUG implies Py_DEBUG */ +#ifdef _DEBUG +# define Py_DEBUG 1 +#endif + /* Define to 1 if you want to disable the GIL */ /* Uncomment the definition for free-threaded builds, or define it manually * 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 */ This is relevant when using build-system generator (e.g CMake) where the linking is explicitly handled */ # if defined(Py_GIL_DISABLED) -# if defined(_DEBUG) +# if defined(Py_DEBUG) # pragma comment(lib,"python314t_d.lib") # elif defined(Py_LIMITED_API) # pragma comment(lib,"python3t.lib") # else # pragma comment(lib,"python314t.lib") -# endif /* _DEBUG */ +# endif /* Py_DEBUG */ # else /* Py_GIL_DISABLED */ -# if defined(_DEBUG) +# if defined(Py_DEBUG) # pragma comment(lib,"python314_d.lib") # elif defined(Py_LIMITED_API) # pragma comment(lib,"python3.lib") # else # pragma comment(lib,"python314.lib") -# endif /* _DEBUG */ +# endif /* Py_DEBUG */ # endif /* Py_GIL_DISABLED */ # endif /* _MSC_VER && !Py_NO_LINK_LIB */ # endif /* Py_BUILD_CORE */ @@ -376,11 +381,6 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ # define ALIGNOF_MAX_ALIGN_T 8 #endif -#ifdef _DEBUG -# define Py_DEBUG -#endif - - #ifdef MS_WIN32 #define SIZEOF_SHORT 2 diff --git a/PC/python_uwp.cpp b/PC/python_uwp.cpp index b9c408a580c999..8cdb8d722cdb9a 100644 --- a/PC/python_uwp.cpp +++ b/PC/python_uwp.cpp @@ -19,13 +19,13 @@ #include #ifdef PYTHONW -#ifdef _DEBUG +#ifdef Py_DEBUG const wchar_t *PROGNAME = L"pythonw_d.exe"; #else const wchar_t *PROGNAME = L"pythonw.exe"; #endif #else -#ifdef _DEBUG +#ifdef Py_DEBUG const wchar_t *PROGNAME = L"python_d.exe"; #else const wchar_t *PROGNAME = L"python.exe"; diff --git a/PC/python_ver_rc.h b/PC/python_ver_rc.h index ee867fe41224c3..bb98144cd03f15 100644 --- a/PC/python_ver_rc.h +++ b/PC/python_ver_rc.h @@ -10,7 +10,7 @@ #define MS_WINDOWS #include "modsupport.h" #include "patchlevel.h" -#ifdef _DEBUG +#ifdef Py_DEBUG # define PYTHON_DEBUG_EXT "_d" #else # define PYTHON_DEBUG_EXT diff --git a/Python/dynload_win.c b/Python/dynload_win.c index 6324063401e51f..de9b0a77817a63 100644 --- a/Python/dynload_win.c +++ b/Python/dynload_win.c @@ -108,7 +108,7 @@ static char *GetPythonImport (HINSTANCE hModule) char *pch; /* Don't claim that python3.dll is a Python DLL. */ -#ifdef _DEBUG +#ifdef Py_DEBUG if (strcmp(import_name, "python3_d.dll") == 0) { #else if (strcmp(import_name, "python3.dll") == 0) { @@ -120,7 +120,7 @@ static char *GetPythonImport (HINSTANCE hModule) /* Ensure python prefix is followed only by numbers to the end of the basename */ pch = import_name + 6; -#ifdef _DEBUG +#ifdef Py_DEBUG while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') { #else while (*pch && *pch != '.') { @@ -300,7 +300,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, char buffer[256]; PyOS_snprintf(buffer, sizeof(buffer), -#ifdef _DEBUG +#ifdef Py_DEBUG "python%d%d_d.dll", #else "python%d%d.dll", diff --git a/Python/marshal.c b/Python/marshal.c index b39c1a5b1ade50..afbef6ee6796d9 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -38,7 +38,7 @@ module marshal * On Windows PGO builds, the r_object function overallocates its stack and * can cause a stack overflow. We reduce the maximum depth for all Windows * releases to protect against this. - * #if defined(MS_WINDOWS) && defined(_DEBUG) + * #if defined(MS_WINDOWS) && defined(Py_DEBUG) */ #if defined(MS_WINDOWS) # define MAX_MARSHAL_STACK_DEPTH 1000 diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 934614e73b56f9..59c261333103e6 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -3134,7 +3134,7 @@ static inline void _Py_NO_RETURN fatal_error_exit(int status) { if (status < 0) { -#if defined(MS_WINDOWS) && defined(_DEBUG) +#if defined(MS_WINDOWS) && defined(Py_DEBUG) DebugBreak(); #endif abort();