@@ -51,11 +51,9 @@ static int convert_voidptr(PyObject *obj, void *p)
5151// extension module or loaded Tk libraries at run-time.
5252static Tk_FindPhoto_t TK_FIND_PHOTO;
5353static Tk_PhotoPutBlock_t TK_PHOTO_PUT_BLOCK;
54- #ifdef WIN32_DLL
5554// Global vars for Tcl functions. We load these symbols from the tkinter
5655// extension module or loaded Tcl libraries at run-time.
5756static Tcl_SetVar_t TCL_SETVAR;
58- #endif
5957
6058static PyObject *mpl_tk_blit (PyObject *self, PyObject *args)
6159{
@@ -225,28 +223,24 @@ static PyMethodDef functions[] = {
225223// Functions to fill global Tcl/Tk function pointers by dynamic loading.
226224
227225template <class T >
228- int load_tk (T lib)
226+ bool load_tcl_tk (T lib)
229227{
230- // Try to fill Tk global vars with function pointers. Return the number of
231- // functions found.
232- return
233- !!(TK_FIND_PHOTO =
234- (Tk_FindPhoto_t)dlsym (lib, " Tk_FindPhoto" )) +
235- !!(TK_PHOTO_PUT_BLOCK =
236- (Tk_PhotoPutBlock_t)dlsym (lib, " Tk_PhotoPutBlock" ));
228+ // Try to fill Tcl/Tk global vars with function pointers. Return whether
229+ // all of them have been filled.
230+ if (void * ptr = dlsym (lib, " Tcl_SetVar" )) {
231+ TCL_SETVAR = (Tcl_SetVar_t)ptr;
232+ }
233+ if (void * ptr = dlsym (lib, " Tk_FindPhoto" )) {
234+ TK_FIND_PHOTO = (Tk_FindPhoto_t)ptr;
235+ }
236+ if (void * ptr = dlsym (lib, " Tk_PhotoPutBlock" )) {
237+ TK_PHOTO_PUT_BLOCK = (Tk_PhotoPutBlock_t)ptr;
238+ }
239+ return TCL_SETVAR && TK_FIND_PHOTO && TK_PHOTO_PUT_BLOCK;
237240}
238241
239242#ifdef WIN32_DLL
240243
241- template <class T >
242- int load_tcl (T lib)
243- {
244- // Try to fill Tcl global vars with function pointers. Return the number of
245- // functions found.
246- return
247- !!(TCL_SETVAR = (Tcl_SetVar_t)dlsym (lib, " Tcl_SetVar" ));
248- }
249-
250244/* On Windows, we can't load the tkinter module to get the Tcl/Tk symbols,
251245 * because Windows does not load symbols into the library name-space of
252246 * importing modules. So, knowing that tkinter has already been imported by
@@ -259,7 +253,6 @@ void load_tkinter_funcs(void)
259253 HANDLE process = GetCurrentProcess (); // Pseudo-handle, doesn't need closing.
260254 HMODULE* modules = NULL ;
261255 DWORD size;
262- bool tcl_ok = false , tk_ok = false ;
263256 if (!EnumProcessModules (process, NULL , 0 , &size)) {
264257 PyErr_SetFromWindowsErr (0 );
265258 goto exit;
@@ -273,11 +266,8 @@ void load_tkinter_funcs(void)
273266 goto exit;
274267 }
275268 for (unsigned i = 0 ; i < size / sizeof (HMODULE); ++i) {
276- if (!tcl_ok) {
277- tcl_ok = load_tcl (modules[i]);
278- }
279- if (!tk_ok) {
280- tk_ok = load_tk (modules[i]);
269+ if (load_tcl_tk (modules[i])) {
270+ return ;
281271 }
282272 }
283273exit:
@@ -301,7 +291,7 @@ void load_tkinter_funcs(void)
301291
302292 // Try loading from the main program namespace first.
303293 main_program = dlopen (NULL , RTLD_LAZY);
304- if (load_tk (main_program)) {
294+ if (load_tcl_tk (main_program)) {
305295 goto exit;
306296 }
307297 // Clear exception triggered when we didn't find symbols above.
@@ -324,7 +314,7 @@ void load_tkinter_funcs(void)
324314 PyErr_SetString (PyExc_RuntimeError, dlerror ());
325315 goto exit;
326316 }
327- if (load_tk (tkinter_lib)) {
317+ if (load_tcl_tk (tkinter_lib)) {
328318 goto exit;
329319 }
330320
@@ -353,11 +343,9 @@ PyMODINIT_FUNC PyInit__tkagg(void)
353343 load_tkinter_funcs ();
354344 if (PyErr_Occurred ()) {
355345 return NULL ;
356- #ifdef WIN32_DLL
357346 } else if (!TCL_SETVAR) {
358347 PyErr_SetString (PyExc_RuntimeError, " Failed to load Tcl_SetVar" );
359348 return NULL ;
360- #endif
361349 } else if (!TK_FIND_PHOTO) {
362350 PyErr_SetString (PyExc_RuntimeError, " Failed to load Tk_FindPhoto" );
363351 return NULL ;
0 commit comments