-
Notifications
You must be signed in to change notification settings - Fork 9
/
mpl-tkagg-1.5.0.patch
431 lines (403 loc) · 15.1 KB
/
mpl-tkagg-1.5.0.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
diff --git a/setup.py b/setup.py
index 4856e1c..4ce4d59 100644
--- a/setup.py
+++ b/setup.py
@@ -52,9 +52,7 @@ from distutils.dist import Distribution
import setupext
from setupext import print_line, print_raw, print_message, print_status
-# Get the version from versioneer
-import versioneer
-__version__ = versioneer.get_version()
+__version__ = "1.5.0"
# These are the packages in the order we want to display them. This
@@ -235,7 +233,7 @@ class NoseTestCommand(TestCommand):
argv=['nosetests'] + self.test_args,
exit=True)
-cmdclass = versioneer.get_cmdclass()
+cmdclass = {}
cmdclass['test'] = NoseTestCommand
# One doesn't normally see `if __name__ == '__main__'` blocks in a setup.py,
diff --git a/setupext.py b/setupext.py
index dd872fd..c942d17 100755
--- a/setupext.py
+++ b/setupext.py
@@ -1499,11 +1499,10 @@ class BackendTkAgg(OptionalBackendPackage):
major, minor1, minor2, s, tmp = sys.version_info
if sys.version_info[0:2] < (3, 4):
ext.include_dirs.extend(['win32_static/include/tcl85'])
- ext.libraries.extend(['tk85', 'tcl85'])
else:
ext.include_dirs.extend(['win32_static/include/tcl86'])
- ext.libraries.extend(['tk86t', 'tcl86t'])
- ext.library_dirs.extend([os.path.join(sys.prefix, 'dlls')])
+ # PSAPI library needed for finding TCL / Tk at run time
+ ext.libraries.extend(['psapi'])
elif sys.platform == 'darwin':
# this config section lifted directly from Imaging - thanks to
@@ -1548,10 +1547,7 @@ class BackendTkAgg(OptionalBackendPackage):
# not found...
# tk_include_dirs.append('/usr/X11R6/include')
- frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
ext.include_dirs.extend(tk_include_dirs)
- ext.extra_link_args.extend(frameworks)
- ext.extra_compile_args.extend(frameworks)
# you're still here? ok we'll try it this way...
else:
@@ -1584,8 +1580,6 @@ class BackendTkAgg(OptionalBackendPackage):
(tcl_lib_dir, tcl_inc_dir, tcl_lib,
tk_lib_dir, tk_inc_dir, tk_lib) = result
ext.include_dirs.extend([tcl_inc_dir, tk_inc_dir])
- ext.library_dirs.extend([tcl_lib_dir, tk_lib_dir])
- ext.libraries.extend([tcl_lib, tk_lib])
class BackendGtk(OptionalBackendPackage):
diff --git a/src/_tkagg.cpp b/src/_tkagg.cpp
index a193410..e98c1d6 100644
--- a/src/_tkagg.cpp
+++ b/src/_tkagg.cpp
@@ -44,6 +44,26 @@ typedef struct
Tcl_Interp *interp;
} TkappObject;
+// Load TCL / Tk symbols from tkinter extension module at run-time.
+// Typedefs, global vars for TCL / Tk library functions.
+typedef Tcl_Command (*tcl_cc)(Tcl_Interp *interp,
+ const char *cmdName, Tcl_CmdProc *proc,
+ ClientData clientData,
+ Tcl_CmdDeleteProc *deleteProc);
+static tcl_cc TCL_CREATE_COMMAND;
+typedef void (*tcl_app_res) (Tcl_Interp *interp, ...);
+static tcl_app_res TCL_APPEND_RESULT;
+typedef Tk_Window (*tk_mw) (Tcl_Interp *interp);
+static tk_mw TK_MAIN_WINDOW;
+typedef Tk_PhotoHandle (*tk_fp) (Tcl_Interp *interp, const char *imageName);
+static tk_fp TK_FIND_PHOTO;
+typedef void (*tk_ppb_nc) (Tk_PhotoHandle handle,
+ Tk_PhotoImageBlock *blockPtr, int x, int y,
+ int width, int height);
+static tk_ppb_nc TK_PHOTO_PUTBLOCK;
+typedef void (*tk_pb) (Tk_PhotoHandle handle);
+static tk_pb TK_PHOTO_BLANK;
+
static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc, char **argv)
{
Tk_PhotoHandle photo;
@@ -61,25 +81,25 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
long mode;
long nval;
- if (Tk_MainWindow(interp) == NULL) {
+ if (TK_MAIN_WINDOW(interp) == NULL) {
// Will throw a _tkinter.TclError with "this isn't a Tk application"
return TCL_ERROR;
}
if (argc != 5) {
- Tcl_AppendResult(interp, "usage: ", argv[0], " destPhoto srcImage", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "usage: ", argv[0], " destPhoto srcImage", (char *)NULL);
return TCL_ERROR;
}
/* get Tcl PhotoImage handle */
- photo = Tk_FindPhoto(interp, argv[1]);
+ photo = TK_FIND_PHOTO(interp, argv[1]);
if (photo == NULL) {
- Tcl_AppendResult(interp, "destination photo must exist", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "destination photo must exist", (char *)NULL);
return TCL_ERROR;
}
/* get array (or object that can be converted to array) pointer */
if (sscanf(argv[2], SIZE_T_FORMAT, &aggl) != 1) {
- Tcl_AppendResult(interp, "error casting pointer", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL);
return TCL_ERROR;
}
bufferobj = (PyObject *)aggl;
@@ -88,7 +108,7 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
try {
buffer = numpy::array_view<uint8_t, 3>(bufferobj);
} catch (...) {
- Tcl_AppendResult(interp, "buffer is of wrong type", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "buffer is of wrong type", (char *)NULL);
PyErr_Clear();
return TCL_ERROR;
}
@@ -99,13 +119,13 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
/* get array mode (0=mono, 1=rgb, 2=rgba) */
mode = atol(argv[3]);
if ((mode != 0) && (mode != 1) && (mode != 2)) {
- Tcl_AppendResult(interp, "illegal image mode", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "illegal image mode", (char *)NULL);
return TCL_ERROR;
}
/* check for bbox/blitting */
if (sscanf(argv[4], SIZE_T_FORMAT, &bboxl) != 1) {
- Tcl_AppendResult(interp, "error casting pointer", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL);
return TCL_ERROR;
}
bboxo = (PyObject *)bboxl;
@@ -126,7 +146,7 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
destbuffer = new agg::int8u[deststride * destheight];
if (destbuffer == NULL) {
- Tcl_AppendResult(interp, "could not allocate memory", (char *)NULL);
+ TCL_APPEND_RESULT(interp, "could not allocate memory", (char *)NULL);
return TCL_ERROR;
}
@@ -167,7 +187,7 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
block.pitch = deststride;
block.pixelPtr = destbuffer;
- Tk_PhotoPutBlock(photo, &block, destx, desty, destwidth, destheight);
+ TK_PHOTO_PUTBLOCK(photo, &block, destx, desty, destwidth, destheight);
delete[] destbuffer;
} else {
@@ -177,9 +197,9 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
block.pixelPtr = buffer.data();
/* Clear current contents */
- Tk_PhotoBlank(photo);
+ TK_PHOTO_BLANK(photo);
/* Copy opaque block to photo image, and leave the rest to TK */
- Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height);
+ TK_PHOTO_PUTBLOCK(photo, &block, 0, 0, block.width, block.height);
}
return TCL_OK;
@@ -216,11 +236,11 @@ static PyObject *_tkinit(PyObject *self, PyObject *args)
/* This will bomb if interp is invalid... */
- Tcl_CreateCommand(interp,
- "PyAggImagePhoto",
- (Tcl_CmdProc *)PyAggImagePhoto,
- (ClientData)0,
- (Tcl_CmdDeleteProc *)NULL);
+ TCL_CREATE_COMMAND(interp,
+ "PyAggImagePhoto",
+ (Tcl_CmdProc *)PyAggImagePhoto,
+ (ClientData)0,
+ (Tcl_CmdDeleteProc *)NULL);
Py_INCREF(Py_None);
return Py_None;
@@ -232,6 +252,217 @@ static PyMethodDef functions[] = {
{ NULL, NULL } /* sentinel */
};
+// Functions to fill global TCL / Tk function pointers by dynamic loading
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+
+/*
+ * On Windows, we can't load the tkinter module to get the TCL or Tk symbols,
+ * because Windows does not load symbols into the library name-space of
+ * importing modules. So, knowing that tkinter has already been imported by
+ * Python, we scan all modules in the running process for the TCL and Tk
+ * function names.
+ */
+#include <windows.h>
+#define PSAPI_VERSION 1
+#include <psapi.h>
+// Must be linked with 'psapi' library
+
+FARPROC _dfunc(HMODULE lib_handle, const char *func_name)
+{
+ // Load function `func_name` from `lib_handle`.
+ // Set Python exception if we can't find `func_name` in `lib_handle`.
+ // Returns function pointer or NULL if not present.
+
+ char message[100];
+
+ FARPROC func = GetProcAddress(lib_handle, func_name);
+ if (func == NULL) {
+ sprintf(message, "Cannot load function %s", func_name);
+ PyErr_SetString(PyExc_RuntimeError, message);
+ }
+ return func;
+}
+
+int get_tcl(HMODULE hMod)
+{
+ // Try to fill TCL global vars with function pointers. Return 0 for no
+ // functions found, 1 for all functions found, -1 for some but not all
+ // functions found.
+ TCL_CREATE_COMMAND = (tcl_cc) GetProcAddress(hMod, "Tcl_CreateCommand");
+ if (TCL_CREATE_COMMAND == NULL) { // Maybe not TCL module
+ return 0;
+ }
+ TCL_APPEND_RESULT = (tcl_app_res) _dfunc(hMod, "Tcl_AppendResult");
+ return (TCL_APPEND_RESULT == NULL) ? -1 : 1;
+}
+
+int get_tk(HMODULE hMod)
+{
+ // Try to fill Tk global vars with function pointers. Return 0 for no
+ // functions found, 1 for all functions found, -1 for some but not all
+ // functions found.
+ TK_MAIN_WINDOW = (tk_mw) GetProcAddress(hMod, "Tk_MainWindow");
+ if (TK_MAIN_WINDOW == NULL) { // Maybe not Tk module
+ return 0;
+ }
+ TK_FIND_PHOTO = (tk_fp) _dfunc(hMod, "Tk_FindPhoto");
+ TK_PHOTO_PUTBLOCK = (tk_ppb_nc) _dfunc(hMod,
+ "Tk_PhotoPutBlock_NoComposite");
+ TK_PHOTO_BLANK = (tk_pb) _dfunc(hMod, "Tk_PhotoBlank");
+ return ((TK_FIND_PHOTO == NULL) ||
+ (TK_PHOTO_PUTBLOCK == NULL) ||
+ (TK_PHOTO_BLANK == NULL)) ? -1 : 1;
+}
+
+int load_tkinter_funcs(void)
+{
+ // Load TCL and Tk functions by searching all modules in current process.
+ // Return 0 for success, non-zero for failure.
+
+ HMODULE hMods[1024];
+ HANDLE hProcess;
+ DWORD cbNeeded;
+ unsigned int i;
+ int found_tcl = 0;
+ int found_tk = 0;
+
+ // Returns pseudo-handle that does not need to be closed
+ hProcess = GetCurrentProcess();
+
+ // Iterate through modules in this process looking for TCL / Tk names
+ if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) {
+ for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
+ if (!found_tcl) {
+ found_tcl = get_tcl(hMods[i]);
+ if (found_tcl == -1) {
+ return 1;
+ }
+ }
+ if (!found_tk) {
+ found_tk = get_tk(hMods[i]);
+ if (found_tk == -1) {
+ return 1;
+ }
+ }
+ if (found_tcl && found_tk) {
+ return 0;
+ }
+ }
+ }
+
+ if (found_tcl == 0) {
+ PyErr_SetString(PyExc_RuntimeError, "Could not find TCL routines");
+ } else {
+ PyErr_SetString(PyExc_RuntimeError, "Could not find Tk routines");
+ }
+ return 1;
+}
+
+#else // not Windows
+
+/*
+ * On Unix, we can get the TCL and Tk synbols from the tkinter module, because
+ * tkinter uses these symbols, and the symbols are therefore visible in the
+ * tkinter dynamic library (module).
+ */
+#if PY3K
+#define TKINTER_PKG "tkinter"
+#define TKINTER_MOD "_tkinter"
+// From module __file__ attribute to char *string for dlopen.
+char *fname2char(PyObject *fname)
+{
+ PyObject *bytes = PyUnicode_EncodeFSDefault(fname);
+ if (bytes == NULL) {
+ return NULL;
+ }
+ return PyBytes_AsString(bytes);
+}
+#else
+#define TKINTER_PKG "Tkinter"
+#define TKINTER_MOD "tkinter"
+// From module __file__ attribute to char *string for dlopen
+#define fname2char(s) (PyString_AsString(s))
+#endif
+
+#include <dlfcn.h>
+
+void *_dfunc(void *lib_handle, const char *func_name)
+{
+ // Load function `func_name` from `lib_handle`.
+ // Set Python exception if we can't find `func_name` in `lib_handle`.
+ // Returns function pointer or NULL if not present.
+
+ // Reset errors.
+ dlerror();
+ void *func = dlsym(lib_handle, func_name);
+ if (func == NULL) {
+ const char *error = dlerror();
+ PyErr_SetString(PyExc_RuntimeError, error);
+ }
+ return func;
+}
+
+int _func_loader(void *lib)
+{
+ // Fill global function pointers from dynamic lib.
+ // Return 1 if any pointer is NULL, 0 otherwise.
+ return (
+ ((TCL_CREATE_COMMAND = (tcl_cc)
+ _dfunc(lib, "Tcl_CreateCommand")) == NULL) ||
+ ((TCL_APPEND_RESULT = (tcl_app_res)
+ _dfunc(lib, "Tcl_AppendResult")) == NULL) ||
+ ((TK_MAIN_WINDOW = (tk_mw)
+ _dfunc(lib, "Tk_MainWindow")) == NULL) ||
+ ((TK_FIND_PHOTO = (tk_fp)
+ _dfunc(lib, "Tk_FindPhoto")) == NULL) ||
+ ((TK_PHOTO_PUTBLOCK = (tk_ppb_nc)
+ _dfunc(lib, "Tk_PhotoPutBlock_NoComposite")) == NULL) ||
+ ((TK_PHOTO_BLANK = (tk_pb)
+ _dfunc(lib, "Tk_PhotoBlank")) == NULL));
+}
+
+int load_tkinter_funcs(void)
+{
+ // Load tkinter global funcs from tkinter compiled module.
+ // Return 0 for success, non-zero for failure.
+ int ret = -1;
+ void *tkinter_lib;
+ char *tkinter_libname;
+ PyObject *pModule = NULL, *pSubmodule = NULL, *pString = NULL;
+
+ pModule = PyImport_ImportModule(TKINTER_PKG);
+ if (pModule == NULL) {
+ goto exit;
+ }
+ pSubmodule = PyObject_GetAttrString(pModule, TKINTER_MOD);
+ if (pSubmodule == NULL) {
+ goto exit;
+ }
+ pString = PyObject_GetAttrString(pSubmodule, "__file__");
+ if (pString == NULL) {
+ goto exit;
+ }
+ tkinter_libname = fname2char(pString);
+ if (tkinter_libname == NULL) {
+ goto exit;
+ }
+ tkinter_lib = dlopen(tkinter_libname, RTLD_LAZY);
+ if (tkinter_lib == NULL) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "Cannot dlopen tkinter module file");
+ goto exit;
+ }
+ ret = _func_loader(tkinter_lib);
+ // dlclose probably safe because tkinter has been imported.
+ dlclose(tkinter_lib);
+exit:
+ Py_XDECREF(pModule);
+ Py_XDECREF(pSubmodule);
+ Py_XDECREF(pString);
+ return ret;
+}
+#endif // end not Windows
+
#if PY3K
static PyModuleDef _tkagg_module = { PyModuleDef_HEAD_INIT, "_tkagg", "", -1, functions,
NULL, NULL, NULL, NULL };
@@ -244,7 +475,7 @@ PyMODINIT_FUNC PyInit__tkagg(void)
import_array();
- return m;
+ return (load_tkinter_funcs() == 0) ? m : NULL;
}
#else
PyMODINIT_FUNC init_tkagg(void)
@@ -252,5 +483,7 @@ PyMODINIT_FUNC init_tkagg(void)
import_array();
Py_InitModule("_tkagg", functions);
+
+ load_tkinter_funcs();
}
#endif