@@ -51,11 +51,9 @@ static int convert_voidptr(PyObject *obj, void *p)
51
51
// extension module or loaded Tk libraries at run-time.
52
52
static Tk_FindPhoto_t TK_FIND_PHOTO;
53
53
static Tk_PhotoPutBlock_t TK_PHOTO_PUT_BLOCK;
54
- #ifdef WIN32_DLL
55
54
// Global vars for Tcl functions. We load these symbols from the tkinter
56
55
// extension module or loaded Tcl libraries at run-time.
57
56
static Tcl_SetVar_t TCL_SETVAR;
58
- #endif
59
57
60
58
static PyObject *mpl_tk_blit (PyObject *self, PyObject *args)
61
59
{
@@ -225,28 +223,24 @@ static PyMethodDef functions[] = {
225
223
// Functions to fill global Tcl/Tk function pointers by dynamic loading.
226
224
227
225
template <class T >
228
- int load_tk (T lib)
226
+ bool load_tcl_tk (T lib)
229
227
{
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;
237
240
}
238
241
239
242
#ifdef WIN32_DLL
240
243
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
-
250
244
/* On Windows, we can't load the tkinter module to get the Tcl/Tk symbols,
251
245
* because Windows does not load symbols into the library name-space of
252
246
* importing modules. So, knowing that tkinter has already been imported by
@@ -259,7 +253,6 @@ void load_tkinter_funcs(void)
259
253
HANDLE process = GetCurrentProcess (); // Pseudo-handle, doesn't need closing.
260
254
HMODULE* modules = NULL ;
261
255
DWORD size;
262
- bool tcl_ok = false , tk_ok = false ;
263
256
if (!EnumProcessModules (process, NULL , 0 , &size)) {
264
257
PyErr_SetFromWindowsErr (0 );
265
258
goto exit ;
@@ -273,11 +266,8 @@ void load_tkinter_funcs(void)
273
266
goto exit ;
274
267
}
275
268
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 ;
281
271
}
282
272
}
283
273
exit :
@@ -301,7 +291,7 @@ void load_tkinter_funcs(void)
301
291
302
292
// Try loading from the main program namespace first.
303
293
main_program = dlopen (NULL , RTLD_LAZY);
304
- if (load_tk (main_program)) {
294
+ if (load_tcl_tk (main_program)) {
305
295
goto exit ;
306
296
}
307
297
// Clear exception triggered when we didn't find symbols above.
@@ -324,7 +314,7 @@ void load_tkinter_funcs(void)
324
314
PyErr_SetString (PyExc_RuntimeError, dlerror ());
325
315
goto exit ;
326
316
}
327
- if (load_tk (tkinter_lib)) {
317
+ if (load_tcl_tk (tkinter_lib)) {
328
318
goto exit ;
329
319
}
330
320
@@ -353,11 +343,9 @@ PyMODINIT_FUNC PyInit__tkagg(void)
353
343
load_tkinter_funcs ();
354
344
if (PyErr_Occurred ()) {
355
345
return NULL ;
356
- #ifdef WIN32_DLL
357
346
} else if (!TCL_SETVAR) {
358
347
PyErr_SetString (PyExc_RuntimeError, " Failed to load Tcl_SetVar" );
359
348
return NULL ;
360
- #endif
361
349
} else if (!TK_FIND_PHOTO) {
362
350
PyErr_SetString (PyExc_RuntimeError, " Failed to load Tk_FindPhoto" );
363
351
return NULL ;
0 commit comments