From a6cbf32f701eb1c0fa9ff1ae029adcfa04772c73 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Fri, 15 Nov 2024 12:20:20 -0600 Subject: [PATCH 1/2] Use both TCL_SETVAR and TCL_SETVAR2 for tcl 9 support Co-authored-by: Antony Lee --- 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..1afe19ab417f 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 and 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 } From a6a442d3f67e92893e7bd8fa0187b9bf755bb401 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 15 Nov 2024 14:11:34 -0500 Subject: [PATCH 2/2] DOC: word choice in error message --- src/_tkagg.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_tkagg.cpp b/src/_tkagg.cpp index 1afe19ab417f..bfb19cc476ea 100644 --- a/src/_tkagg.cpp +++ b/src/_tkagg.cpp @@ -356,7 +356,7 @@ PYBIND11_MODULE(_tkagg, m, py::mod_gil_not_used()) } if (!(TCL_SETVAR || TCL_SETVAR2)) { - throw py::import_error("Failed to load Tcl_SetVar and 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) {