From f0474c664d2afc8fddd918a1043dadbf5654747e Mon Sep 17 00:00:00 2001
From: Alexey Pavlov <alexpux@gmail.com>
Date: Wed, 3 Sep 2025 15:14:22 +0300
Subject: [PATCH 155/N] Try fix building C++ modules for Python. Fixes for
 WMI module

---
 Makefile.pre.in   |  3 +++
 Modules/makesetup | 16 ++++++++--------
 PC/_wmimodule.cpp |  8 ++++++--
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 1ea9558..62668fd 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -104,6 +104,7 @@ CONFIGURE_LDFLAGS=	@LDFLAGS@
 # values.
 PY_CFLAGS=	$(BASECFLAGS) $(OPT) $(CONFIGURE_CFLAGS) $(CFLAGS) $(EXTRA_CFLAGS)
 PY_CFLAGS_NODIST=$(CONFIGURE_CFLAGS_NODIST) $(CFLAGS_NODIST) -I$(srcdir)/Include/internal -I$(srcdir)/Include/internal/mimalloc
+PY_CXXFLAGS_NODIST=$(filter-out -std=c11,$(PY_CFLAGS_NODIST)) -std=c++17
 # Both CPPFLAGS and LDFLAGS need to contain the shell's value for setup.py to
 # be able to build extension modules using the directories specified in the
 # environment variables
@@ -121,7 +122,9 @@ ARFLAGS=	@ARFLAGS@
 CFLAGSFORSHARED=@CFLAGSFORSHARED@
 # C flags used for building the interpreter object files
 PY_STDMODULE_CFLAGS= $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED)
+PY_STDMODULE_CXXFLAGS= $(PY_CFLAGS) $(PY_CXXFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED)
 PY_BUILTIN_MODULE_CFLAGS= $(PY_STDMODULE_CFLAGS) -DPy_BUILD_CORE_BUILTIN
+PY_BUILTIN_MODULE_CXXFLAGS= $(PY_STDMODULE_CXXFLAGS) -DPy_BUILD_CORE_BUILTIN
 PY_CORE_CFLAGS=	$(PY_STDMODULE_CFLAGS) -DPy_BUILD_CORE
 # Linker flags used for building the interpreter object files
 PY_CORE_LDFLAGS=$(PY_LDFLAGS) $(PY_LDFLAGS_NODIST)
diff --git a/Modules/makesetup b/Modules/makesetup
index 7f58fb0..544d1d2 100755
--- a/Modules/makesetup
+++ b/Modules/makesetup
@@ -228,13 +228,13 @@ sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
 		for src in $srcs
 		do
 			case $src in
-			*.c)   obj=`basename $src .c`.o; cc='$(CC)'; ccld='$(BLDSHARED)';;
-			*.cc)  obj=`basename $src .cc`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)';;
-			*.c++) obj=`basename $src .c++`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)';;
-			*.C)   obj=`basename $src .C`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)';;
-			*.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)';;
-			*.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)';;
-			*.m)   obj=`basename $src .m`.o; cc='$(CC)'; ccld='$(BLDSHARED)';; # Obj-C
+			*.c)   obj=`basename $src .c`.o; cc='$(CC)'; ccld='$(BLDSHARED)'; ccflags='$(PY_STDMODULE_CFLAGS)';;
+			*.cc)  obj=`basename $src .cc`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)'; ccflags='$(PY_STDMODULE_CXXFLAGS)';;
+			*.c++) obj=`basename $src .c++`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)'; ccflags='$(PY_STDMODULE_CXXFLAGS)';;
+			*.C)   obj=`basename $src .C`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)'; ccflags='$(PY_STDMODULE_CXXFLAGS)';;
+			*.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)'; ccflags='$(PY_STDMODULE_CXXFLAGS)';;
+			*.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)'; ccld='$(LDCXXSHARED)'; ccflags='$(PY_STDMODULE_CXXFLAGS)';;
+			*.m)   obj=`basename $src .m`.o; cc='$(CC)'; ccld='$(BLDSHARED)'; ccflags='$(PY_STDMODULE_CFLAGS)';; # Obj-C
 			*)     continue;;
 			esac
 			case $src in
@@ -251,7 +251,7 @@ sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
 			# custom flags first, PY_STDMODULE_CFLAGS may contain -I with system libmpdec
 			case $doconfig in
 			no)
-				cc="$cc $cpps \$(PY_STDMODULE_CFLAGS) \$(CCSHARED)"
+				cc="$cc $cpps $ccflags \$(CCSHARED)"
 				rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(MODULE_DEPS_SHARED) \$(PYTHON_HEADERS); $cc -c $src -o $obj"
 				;;
 			*)
diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp
index 58cb9a8..e05037d 100644
--- a/PC/_wmimodule.cpp
+++ b/PC/_wmimodule.cpp
@@ -100,10 +100,12 @@ _query_thread(LPVOID param)
         hr = HRESULT_FROM_WIN32(GetLastError());
     }
     if (SUCCEEDED(hr)) {
+        BSTR bstr_wql = SysAllocString(L"ROOT\\CIMV2");
         hr = locator->ConnectServer(
-            bstr_t(L"ROOT\\CIMV2"),
+            bstr_wql,
             NULL, NULL, 0, NULL, 0, 0, &services
         );
+        SysFreeString(bstr_wql);
     }
     if (SUCCEEDED(hr) && !SetEvent(data.connectEvent)) {
         hr = HRESULT_FROM_WIN32(GetLastError());
@@ -116,13 +118,15 @@ _query_thread(LPVOID param)
         );
     }
     if (SUCCEEDED(hr)) {
+        BSTR bstr_wql = SysAllocString(L"WQL");
         hr = services->ExecQuery(
-            bstr_t("WQL"),
+            bstr_wql,
             bstrQuery,
             WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
             NULL,
             &enumerator
         );
+        SysFreeString(bstr_wql);
     }
 
     // Okay, after all that, at this stage we should have an enumerator
