mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
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:
@ -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",
|
||||
|
@ -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
|
||||
|
19
torch/csrc/dynamo/stackref_bridge.c
Normal file
19
torch/csrc/dynamo/stackref_bridge.c
Normal 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 MSVC’s “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);
|
||||
}
|
17
torch/csrc/dynamo/stackref_bridge.h
Normal file
17
torch/csrc/dynamo/stackref_bridge.h
Normal 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
|
Reference in New Issue
Block a user