Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f86e24

Browse files
authored
Merge pull request #23054 from anntzer/alwaystcl
MNT: Slightly simplify tcl/tk load in extension.
2 parents 65e9204 + e1fcc90 commit 3f86e24

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

src/_tkagg.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ static int convert_voidptr(PyObject *obj, void *p)
5151
// extension module or loaded Tk libraries at run-time.
5252
static Tk_FindPhoto_t TK_FIND_PHOTO;
5353
static 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.
5756
static Tcl_SetVar_t TCL_SETVAR;
58-
#endif
5957

6058
static 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

227225
template <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
}
283273
exit:
@@ -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;

src/_tkmini.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ typedef int (*Tk_PhotoPutBlock_t) (Tcl_Interp *interp, Tk_PhotoHandle handle,
100100
Tk_PhotoImageBlock *blockPtr, int x, int y,
101101
int width, int height, int compRule);
102102

103-
#ifdef WIN32_DLL
104103
/* Typedefs derived from function signatures in Tcl header */
104+
/* Tcl_SetVar typedef */
105105
typedef const char *(*Tcl_SetVar_t)(Tcl_Interp *interp, const char *varName,
106106
const char *newValue, int flags);
107-
#endif
108107

109108
#ifdef __cplusplus
110109
}

0 commit comments

Comments
 (0)