From 7fe551f82dca56018cd2133d43c3cb3cb1ff270b Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 15 Nov 2024 12:10:20 -0800 Subject: [PATCH] Backport PR #29144: Use both TCL_SETVAR and TCL_SETVAR2 for tcl 9 support --- src/_tkagg.cpp | 20 ++++++++++++++++---- src/_tkmini.h | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/_tkagg.cpp b/src/_tkagg.cpp index 04ca3d4d1b84..bfb19cc476ea 100644 --- a/src/_tkagg.cpp +++ b/src/_tkagg.cpp @@ -92,6 +92,7 @@ static Tk_PhotoPutBlock_t TK_PHOTO_PUT_BLOCK; // Global vars for Tcl functions. We load these symbols from the tkinter // extension module or loaded Tcl libraries at run-time. static Tcl_SetVar_t TCL_SETVAR; +static Tcl_SetVar2_t TCL_SETVAR2; static void mpl_tk_blit(py::object interp_obj, const char *photo_name, @@ -173,7 +174,15 @@ DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, std::string dpi = std::to_string(LOWORD(wParam)); Tcl_Interp* interp = (Tcl_Interp*)dwRefData; - TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0); + if (TCL_SETVAR) { + TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0); + } else if (TCL_SETVAR2) { + TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0); + } else { + // This should be prevented at import time, and therefore unreachable. + // But defensively throw just in case. + throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2"); + } } return 0; case WM_NCDESTROY: @@ -246,13 +255,16 @@ bool load_tcl_tk(T lib) if (auto ptr = dlsym(lib, "Tcl_SetVar")) { TCL_SETVAR = (Tcl_SetVar_t)ptr; } + if (auto ptr = dlsym(lib, "Tcl_SetVar2")) { + TCL_SETVAR2 = (Tcl_SetVar2_t)ptr; + } if (auto ptr = dlsym(lib, "Tk_FindPhoto")) { TK_FIND_PHOTO = (Tk_FindPhoto_t)ptr; } if (auto ptr = dlsym(lib, "Tk_PhotoPutBlock")) { TK_PHOTO_PUT_BLOCK = (Tk_PhotoPutBlock_t)ptr; } - return TCL_SETVAR && TK_FIND_PHOTO && TK_PHOTO_PUT_BLOCK; + return (TCL_SETVAR || TCL_SETVAR2) && TK_FIND_PHOTO && TK_PHOTO_PUT_BLOCK; } #ifdef WIN32_DLL @@ -343,8 +355,8 @@ PYBIND11_MODULE(_tkagg, m, py::mod_gil_not_used()) throw py::error_already_set(); } - if (!TCL_SETVAR) { - throw py::import_error("Failed to load Tcl_SetVar"); + if (!(TCL_SETVAR || TCL_SETVAR2)) { + throw py::import_error("Failed to load Tcl_SetVar or Tcl_SetVar2"); } else if (!TK_FIND_PHOTO) { throw py::import_error("Failed to load Tk_FindPhoto"); } else if (!TK_PHOTO_PUT_BLOCK) { diff --git a/src/_tkmini.h b/src/_tkmini.h index 85f245815e4c..1c74cf9720f8 100644 --- a/src/_tkmini.h +++ b/src/_tkmini.h @@ -104,6 +104,9 @@ typedef int (*Tk_PhotoPutBlock_t) (Tcl_Interp *interp, Tk_PhotoHandle handle, /* Tcl_SetVar typedef */ typedef const char *(*Tcl_SetVar_t)(Tcl_Interp *interp, const char *varName, const char *newValue, int flags); +/* Tcl_SetVar2 typedef */ +typedef const char *(*Tcl_SetVar2_t)(Tcl_Interp *interp, const char *part1, const char *part2, + const char *newValue, int flags); #ifdef __cplusplus }