Files
pytorch/torch/csrc/dynamo/cpython_includes.h
albanD 058fb1790f Fix compilation and "import torch" issues for cpython 3.14 (#158184)
Beginning of process for 3.14 bringup.

State of things from this PR:
- Nothing too scary looking from the Dynamo CPython side, nothing we heavily rely on seems to be missing @williamwen42
- The existing check that makes torch.compile() nicely fail is working as expected. So all these empty functions shouldn't cause any weirdness.
- The `__module__` update changes look suspicious, we should investigate what is the reason and impact of that, in particular for our public API checking @jbschlosser
- Leaving the weakref.py thread safety change as a follow up to keep this a bit simpler. I vendored the whole struct in the meantime FYI @ezyang

EDIT: The `__module__` change is even more cursed than I though due to changes to Union and Optional type where the `__module__` field cannot be changed anymore. See https://github.com/python/cpython/issues/132139 for details.
For now, I'm just skipping the `__module__` setting for 3.14 which will trip the public API checks. Will revisit once I have a final answer on the cpython issue.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/158184
Approved by: https://github.com/msaroufim
2025-07-15 05:06:55 +00:00

67 lines
1.3 KiB
C

#pragma once
#include <torch/csrc/utils/python_compat.h>
// Problem in CPython includes when mixing core and non-core build
// The fix was not backported to 3.12 so this is needed here
// https://github.com/python/cpython/issues/105268
#if IS_PYTHON_3_12_PLUS
#undef _PyGC_FINALIZED
#endif
// see https://bugs.python.org/issue35886
#if PY_VERSION_HEX >= 0x03080000
#define Py_BUILD_CORE
#ifndef __cplusplus
// C-only headers
#include <internal/pycore_pystate.h>
#endif // __cplusplus
#if IS_PYTHON_3_11_PLUS
#include <internal/pycore_frame.h>
#if IS_PYTHON_3_14_PLUS
#include <internal/pycore_interpframe_structs.h>
#include <internal/pycore_stackref.h>
#endif
#endif
#if IS_PYTHON_3_14_PLUS
#include <internal/pycore_code.h>
#endif
#undef Py_BUILD_CORE
#endif // PY_VERSION_HEX >= 0x03080000
#ifdef __cplusplus
extern "C" {
#endif
#if IS_PYTHON_3_14_PLUS
#define F_CODE(x) (PyCodeObject*)PyStackRef_AsPyObjectBorrow(x->f_executable)
#define PREV_INSTR(x) (x)->instr_ptr
#else
#if IS_PYTHON_3_13_PLUS
#define F_CODE(x) ((PyCodeObject*)(x)->f_executable)
#define PREV_INSTR(x) (x)->instr_ptr
#else
#define F_CODE(x) ((PyCodeObject*)(x)->f_code)
#define PREV_INSTR(x) (x)->prev_instr
#endif
#endif // IS_PYTHON_3_14_PLUS
#if IS_PYTHON_3_12_PLUS
#define FUNC(x) ((x)->f_funcobj)
#else
#define FUNC(x) ((x)->f_func)
#endif
#ifdef __cplusplus
} // extern "C"
#endif