Fix MSCV C++ compilation error of pycore_stackref.h header

Wraps the header in a C file and compile it using a C compiler, which should support designated initializers

Fix issue #160647
This commit is contained in:
Guilherme Leobas
2025-10-16 18:14:23 -03:00
parent 35e51893bd
commit 8b302de0c5
4 changed files with 39 additions and 1 deletions

View File

@ -926,6 +926,7 @@ libtorch_python_core_sources = [
"torch/csrc/dynamo/guards.cpp",
"torch/csrc/dynamo/utils.cpp",
"torch/csrc/dynamo/init.cpp",
"torch/csrc/dynamo/stackref_bridge.c",
"torch/csrc/functorch/init.cpp",
"torch/csrc/fx/node.cpp",
"torch/csrc/mps/Module.cpp",

View File

@ -28,6 +28,7 @@
#include <internal/pycore_stackref.h>
#elif IS_PYTHON_3_14_PLUS && defined(_WIN32)
#include <internal/pycore_interpframe_structs.h> // _PyInterpreterFrame
#include "stackref_bridge.h"
#endif
#endif
@ -45,7 +46,7 @@ extern "C" {
#elif IS_PYTHON_3_14_PLUS && defined(_WIN32)
#define F_CODE(x) ((PyCodeObject*)((x)->f_executable.bits))
#define F_CODE(x) ((PyCodeObject*)Torch_PyStackRef_AsPyObjectBorrow(&x->f_executable))
#define PREV_INSTR(x) (x)->instr_ptr
#else

View File

@ -0,0 +1,19 @@
// Compile this file as C, not C++.
// Wrap inclusion of pycore_stackref.h inside a pure C file and ensure this file
// is compiled as C (not C++). This avoids MSVCs “designated initializers
// require /std:c++20” error, since pycore_stackref.h uses C99-style designated
// initializers that are not supported in older C++ standards, but is supported
// in C.
#define Py_BUILD_CORE
#include <Python.h>
#include <internal/pycore_stackref.h>
#include "stackref_bridge.h"
#undef Py_BUILD_CORE
PyObject* Torch_PyStackRef_AsPyObjectBorrow(void* stackref) {
_PyStackRef *sr = (_PyStackRef*)stackref;
return PyStackRef_AsPyObjectBorrow(*sr);
}

View File

@ -0,0 +1,17 @@
#pragma once
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// Use a void* to avoid exposing the internal _PyStackRef union on this
// translation unit
PyObject* Torch_PyStackRef_AsPyObjectBorrow(void* stackref);
#ifdef __cplusplus
}
#endif // #ifdef __cplusplus
#endif // #ifdef _WIN32