Skip to content

Commit 0a457ea

Browse files
committed
fix: use Python 3.13 API if on 3.13
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 6851255 commit 0a457ea

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

include/pybind11/pybind11.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,8 +1344,13 @@ using module = module_;
13441344
/// Return a dictionary representing the global variables in the current execution frame,
13451345
/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
13461346
inline dict globals() {
1347+
#if PY_VERSION_HEX >= 0x030d00000
1348+
PyObject *p = PyEval_GetFrameGlobals();
1349+
return p ? reinterpret_steal<dict>(p) : reinterpret_borrow<dict>(module_::import("__main__").attr("__dict__").ptr()));
1350+
#else
13471351
PyObject *p = PyEval_GetGlobals();
13481352
return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
1353+
#endif
13491354
}
13501355

13511356
template <typename... Args, typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>>
@@ -2770,7 +2775,12 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
27702775
PyCodeObject *f_code = PyFrame_GetCode(frame);
27712776
// f_code is guaranteed to not be NULL
27722777
if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) {
2778+
# if PY_VERSION_HEX >= 0x030d0000
2779+
PyObject *locals = PyEval_GetFrameLocals();
2780+
# else
27732781
PyObject *locals = PyEval_GetLocals();
2782+
Py_INCREF(locals);
2783+
# endif
27742784
if (locals != nullptr) {
27752785
# if PY_VERSION_HEX >= 0x030b0000
27762786
PyObject *co_varnames = PyCode_GetVarnames(f_code);
@@ -2780,6 +2790,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
27802790
PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0);
27812791
Py_DECREF(co_varnames);
27822792
PyObject *self_caller = dict_getitem(locals, self_arg);
2793+
Py_DECREF(locals);
27832794
if (self_caller == self.ptr()) {
27842795
Py_DECREF(f_code);
27852796
Py_DECREF(frame);

0 commit comments

Comments
 (0)