Skip to content

Commit c33657e

Browse files
committed
WIP: try Windows support for TCL dynamic loading
Try adding defines etc for using LoadLibrary on Windows to get the TCL / Tk routines.
1 parent 64f01dd commit c33657e

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

setupext.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1783,8 +1783,8 @@ def add_flags(self, ext):
17831783
# tk_include_dirs.append('/usr/X11R6/include')
17841784
frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
17851785
ext.include_dirs.extend(tk_include_dirs)
1786-
ext.extra_link_args.extend(frameworks)
1787-
ext.extra_compile_args.extend(frameworks)
1786+
# ext.extra_link_args.extend(frameworks)
1787+
# ext.extra_compile_args.extend(frameworks)
17881788

17891789
# you're still here? ok we'll try it this way...
17901790
else:

src/_tkagg.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ static PyMethodDef functions[] = {
267267
#ifdef DYNAMIC_TKINTER
268268
// Functions to fill global TCL / Tk function pointers from tkinter module.
269269

270-
#include <dlfcn.h>
271-
272270
#if PY3K
273271
#define TKINTER_PKG "tkinter"
274272
#define TKINTER_MOD "_tkinter"
@@ -288,6 +286,31 @@ char *fname2char(PyObject *fname)
288286
#define fname2char(s) (PyString_AsString(s))
289287
#endif
290288

289+
#if defined(_MSC_VER)
290+
#include <windows.h>
291+
#define LIB_PTR_TYPE HMODULE
292+
#define LOAD_LIB(name) LoadLibrary(name)
293+
#define CLOSE_LIB(name) FreeLibrary(name)
294+
FARPROC _dfunc(LIB_PTR_TYPE lib_handle, const char *func_name)
295+
{
296+
// Load function `func_name` from `lib_handle`.
297+
// Set Python exception if we can't find `func_name` in `lib_handle`.
298+
// Returns function pointer or NULL if not present.
299+
300+
char message[100];
301+
302+
FARPROC func = GetProcAddress(lib_handle, func_name);
303+
if (func == NULL) {
304+
sprintf(message, "Cannot load function %s", func_name);
305+
PyErr_SetString(PyExc_RuntimeError, message);
306+
}
307+
return func;
308+
}
309+
#else
310+
#include <dlfcn.h>
311+
#define LIB_PTR_TYPE void*
312+
#define LOAD_LIB(name) dlopen(name, RTLD_LAZY)
313+
#define CLOSE_LIB(name) dlclose(name)
291314
void *_dfunc(void *lib_handle, const char *func_name)
292315
{
293316
// Load function `func_name` from `lib_handle`.
@@ -303,8 +326,9 @@ void *_dfunc(void *lib_handle, const char *func_name)
303326
}
304327
return func;
305328
}
329+
#endif
306330

307-
int _func_loader(void *lib)
331+
int _func_loader(LIB_PTR_TYPE lib)
308332
{
309333
// Fill global function pointers from dynamic lib.
310334
// Return 1 if any pointer is NULL, 0 otherwise.
@@ -328,7 +352,7 @@ int load_tkinter_funcs(void)
328352
// Load tkinter global funcs from tkinter compiled module.
329353
// Return 0 for success, non-zero for failure.
330354
int ret = -1;
331-
void *tkinter_lib;
355+
LIB_PTR_TYPE tkinter_lib;
332356
char *tkinter_libname;
333357
PyObject *pModule = NULL, *pSubmodule = NULL, *pString = NULL;
334358

@@ -348,15 +372,15 @@ int load_tkinter_funcs(void)
348372
if (tkinter_libname == NULL) {
349373
goto exit;
350374
}
351-
tkinter_lib = dlopen(tkinter_libname, RTLD_LAZY);
375+
tkinter_lib = LOAD_LIB(tkinter_libname);
352376
if (tkinter_lib == NULL) {
353377
PyErr_SetString(PyExc_RuntimeError,
354378
"Cannot dlopen tkinter module file");
355379
goto exit;
356380
}
357381
ret = _func_loader(tkinter_lib);
358382
// dlclose probably safe because tkinter has been imported.
359-
dlclose(tkinter_lib);
383+
CLOSE_LIB(tkinter_lib);
360384
exit:
361385
Py_XDECREF(pModule);
362386
Py_XDECREF(pSubmodule);

0 commit comments

Comments
 (0)