Branch data Line data Source code
1 : : /***********************************************************
2 : : Copyright (C) 1994 Steen Lumholt.
3 : :
4 : : All Rights Reserved
5 : :
6 : : ******************************************************************/
7 : :
8 : : /* _tkinter.c -- Interface to libtk.a and libtcl.a. */
9 : :
10 : : /* TCL/TK VERSION INFO:
11 : :
12 : : Only Tcl/Tk 8.5.12 and later are supported. Older versions are not
13 : : supported. Use Python 3.10 or older if you cannot upgrade your
14 : : Tcl/Tk libraries.
15 : : */
16 : :
17 : : /* XXX Further speed-up ideas, involving Tcl 8.0 features:
18 : :
19 : : - Register a new Tcl type, "Python callable", which can be called more
20 : : efficiently and passed to Tcl_EvalObj() directly (if this is possible).
21 : :
22 : : */
23 : :
24 : : #define PY_SSIZE_T_CLEAN
25 : : #ifndef Py_BUILD_CORE_BUILTIN
26 : : # define Py_BUILD_CORE_MODULE 1
27 : : #endif
28 : :
29 : : #include "Python.h"
30 : : #include <ctype.h>
31 : : #ifdef MS_WINDOWS
32 : : # include "pycore_fileutils.h" // _Py_stat()
33 : : #endif
34 : :
35 : : #ifdef MS_WINDOWS
36 : : #include <windows.h>
37 : : #endif
38 : :
39 : : #define CHECK_SIZE(size, elemsize) \
40 : : ((size_t)(size) <= Py_MIN((size_t)INT_MAX, UINT_MAX / (size_t)(elemsize)))
41 : :
42 : : /* If Tcl is compiled for threads, we must also define TCL_THREAD. We define
43 : : it always; if Tcl is not threaded, the thread functions in
44 : : Tcl are empty. */
45 : : #define TCL_THREADS
46 : :
47 : : #ifdef TK_FRAMEWORK
48 : : #include <Tcl/tcl.h>
49 : : #include <Tk/tk.h>
50 : : #else
51 : : #include <tcl.h>
52 : : #include <tk.h>
53 : : #endif
54 : :
55 : : #include "tkinter.h"
56 : :
57 : : #if TK_HEX_VERSION < 0x0805020c
58 : : #error "Tk older than 8.5.12 not supported"
59 : : #endif
60 : :
61 : : #include <tclTomMath.h>
62 : :
63 : : #if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
64 : : #define HAVE_CREATEFILEHANDLER
65 : : #endif
66 : :
67 : : #ifdef HAVE_CREATEFILEHANDLER
68 : :
69 : : /* This bit is to ensure that TCL_UNIX_FD is defined and doesn't interfere
70 : : with the proper calculation of FHANDLETYPE == TCL_UNIX_FD below. */
71 : : #ifndef TCL_UNIX_FD
72 : : # ifdef TCL_WIN_SOCKET
73 : : # define TCL_UNIX_FD (! TCL_WIN_SOCKET)
74 : : # else
75 : : # define TCL_UNIX_FD 1
76 : : # endif
77 : : #endif
78 : :
79 : : /* Tcl_CreateFileHandler() changed several times; these macros deal with the
80 : : messiness. In Tcl 8.0 and later, it is not available on Windows (and on
81 : : Unix, only because Jack added it back); when available on Windows, it only
82 : : applies to sockets. */
83 : :
84 : : #ifdef MS_WINDOWS
85 : : #define FHANDLETYPE TCL_WIN_SOCKET
86 : : #else
87 : : #define FHANDLETYPE TCL_UNIX_FD
88 : : #endif
89 : :
90 : : /* If Tcl can wait for a Unix file descriptor, define the EventHook() routine
91 : : which uses this to handle Tcl events while the user is typing commands. */
92 : :
93 : : #if FHANDLETYPE == TCL_UNIX_FD
94 : : #define WAIT_FOR_STDIN
95 : : #endif
96 : :
97 : : #endif /* HAVE_CREATEFILEHANDLER */
98 : :
99 : : /* Use OS native encoding for converting between Python strings and
100 : : Tcl objects.
101 : : On Windows use UTF-16 (or UTF-32 for 32-bit Tcl_UniChar) with the
102 : : "surrogatepass" error handler for converting to/from Tcl Unicode objects.
103 : : On Linux use UTF-8 with the "surrogateescape" error handler for converting
104 : : to/from Tcl String objects. */
105 : : #ifdef MS_WINDOWS
106 : : #define USE_TCL_UNICODE 1
107 : : #else
108 : : #define USE_TCL_UNICODE 0
109 : : #endif
110 : :
111 : : #if PY_LITTLE_ENDIAN
112 : : #define NATIVE_BYTEORDER -1
113 : : #else
114 : : #define NATIVE_BYTEORDER 1
115 : : #endif
116 : :
117 : : #ifdef MS_WINDOWS
118 : : #include <conio.h>
119 : : #define WAIT_FOR_STDIN
120 : :
121 : : static PyObject *
122 : : _get_tcl_lib_path()
123 : : {
124 : : static PyObject *tcl_library_path = NULL;
125 : : static int already_checked = 0;
126 : :
127 : : if (already_checked == 0) {
128 : : PyObject *prefix;
129 : : struct stat stat_buf;
130 : : int stat_return_value;
131 : :
132 : : prefix = PyUnicode_FromWideChar(Py_GetPrefix(), -1);
133 : : if (prefix == NULL) {
134 : : return NULL;
135 : : }
136 : :
137 : : /* Check expected location for an installed Python first */
138 : : tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
139 : : if (tcl_library_path == NULL) {
140 : : return NULL;
141 : : }
142 : : tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path);
143 : : if (tcl_library_path == NULL) {
144 : : return NULL;
145 : : }
146 : : stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
147 : : if (stat_return_value == -2) {
148 : : return NULL;
149 : : }
150 : : if (stat_return_value == -1) {
151 : : /* install location doesn't exist, reset errno and see if
152 : : we're a repository build */
153 : : errno = 0;
154 : : #ifdef Py_TCLTK_DIR
155 : : tcl_library_path = PyUnicode_FromString(
156 : : Py_TCLTK_DIR "\\lib\\tcl" TCL_VERSION);
157 : : if (tcl_library_path == NULL) {
158 : : return NULL;
159 : : }
160 : : stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
161 : : if (stat_return_value == -2) {
162 : : return NULL;
163 : : }
164 : : if (stat_return_value == -1) {
165 : : /* tcltkDir for a repository build doesn't exist either,
166 : : reset errno and leave Tcl to its own devices */
167 : : errno = 0;
168 : : tcl_library_path = NULL;
169 : : }
170 : : #else
171 : : tcl_library_path = NULL;
172 : : #endif
173 : : }
174 : : already_checked = 1;
175 : : }
176 : : return tcl_library_path;
177 : : }
178 : : #endif /* MS_WINDOWS */
179 : :
180 : : /* The threading situation is complicated. Tcl is not thread-safe, except
181 : : when configured with --enable-threads.
182 : :
183 : : So we need to use a lock around all uses of Tcl. Previously, the
184 : : Python interpreter lock was used for this. However, this causes
185 : : problems when other Python threads need to run while Tcl is blocked
186 : : waiting for events.
187 : :
188 : : To solve this problem, a separate lock for Tcl is introduced.
189 : : Holding it is incompatible with holding Python's interpreter lock.
190 : : The following four macros manipulate both locks together.
191 : :
192 : : ENTER_TCL and LEAVE_TCL are brackets, just like
193 : : Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. They should be
194 : : used whenever a call into Tcl is made that could call an event
195 : : handler, or otherwise affect the state of a Tcl interpreter. These
196 : : assume that the surrounding code has the Python interpreter lock;
197 : : inside the brackets, the Python interpreter lock has been released
198 : : and the lock for Tcl has been acquired.
199 : :
200 : : Sometimes, it is necessary to have both the Python lock and the Tcl
201 : : lock. (For example, when transferring data from the Tcl
202 : : interpreter result to a Python string object.) This can be done by
203 : : using different macros to close the ENTER_TCL block: ENTER_OVERLAP
204 : : reacquires the Python lock (and restores the thread state) but
205 : : doesn't release the Tcl lock; LEAVE_OVERLAP_TCL releases the Tcl
206 : : lock.
207 : :
208 : : By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event
209 : : handlers when the handler needs to use Python. Such event handlers
210 : : are entered while the lock for Tcl is held; the event handler
211 : : presumably needs to use Python. ENTER_PYTHON releases the lock for
212 : : Tcl and acquires the Python interpreter lock, restoring the
213 : : appropriate thread state, and LEAVE_PYTHON releases the Python
214 : : interpreter lock and re-acquires the lock for Tcl. It is okay for
215 : : ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between
216 : : ENTER_PYTHON and LEAVE_PYTHON.
217 : :
218 : : These locks expand to several statements and brackets; they should
219 : : not be used in branches of if statements and the like.
220 : :
221 : : If Tcl is threaded, this approach won't work anymore. The Tcl
222 : : interpreter is only valid in the thread that created it, and all Tk
223 : : activity must happen in this thread, also. That means that the
224 : : mainloop must be invoked in the thread that created the
225 : : interpreter. Invoking commands from other threads is possible;
226 : : _tkinter will queue an event for the interpreter thread, which will
227 : : then execute the command and pass back the result. If the main
228 : : thread is not in the mainloop, and invoking commands causes an
229 : : exception; if the main loop is running but not processing events,
230 : : the command invocation will block.
231 : :
232 : : In addition, for a threaded Tcl, a single global tcl_tstate won't
233 : : be sufficient anymore, since multiple Tcl interpreters may
234 : : simultaneously dispatch in different threads. So we use the Tcl TLS
235 : : API.
236 : :
237 : : */
238 : :
239 : : static PyThread_type_lock tcl_lock = 0;
240 : :
241 : : #ifdef TCL_THREADS
242 : : static Tcl_ThreadDataKey state_key;
243 : : typedef PyThreadState *ThreadSpecificData;
244 : : #define tcl_tstate \
245 : : (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*)))
246 : : #else
247 : : static PyThreadState *tcl_tstate = NULL;
248 : : #endif
249 : :
250 : : #define ENTER_TCL \
251 : : { PyThreadState *tstate = PyThreadState_Get(); \
252 : : Py_BEGIN_ALLOW_THREADS \
253 : : if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \
254 : : tcl_tstate = tstate;
255 : :
256 : : #define LEAVE_TCL \
257 : : tcl_tstate = NULL; \
258 : : if(tcl_lock)PyThread_release_lock(tcl_lock); \
259 : : Py_END_ALLOW_THREADS}
260 : :
261 : : #define ENTER_OVERLAP \
262 : : Py_END_ALLOW_THREADS
263 : :
264 : : #define LEAVE_OVERLAP_TCL \
265 : : tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
266 : :
267 : : #define ENTER_PYTHON \
268 : : { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
269 : : if(tcl_lock) \
270 : : PyThread_release_lock(tcl_lock); \
271 : : PyEval_RestoreThread((tstate)); }
272 : :
273 : : #define LEAVE_PYTHON \
274 : : { PyThreadState *tstate = PyEval_SaveThread(); \
275 : : if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); \
276 : : tcl_tstate = tstate; }
277 : :
278 : : #define CHECK_TCL_APPARTMENT \
279 : : if (((TkappObject *)self)->threaded && \
280 : : ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \
281 : : PyErr_SetString(PyExc_RuntimeError, \
282 : : "Calling Tcl from different apartment"); \
283 : : return 0; \
284 : : }
285 : :
286 : : #ifndef FREECAST
287 : : #define FREECAST (char *)
288 : : #endif
289 : :
290 : : /**** Tkapp Object Declaration ****/
291 : :
292 : : static PyObject *Tkapp_Type;
293 : :
294 : : typedef struct {
295 : : PyObject_HEAD
296 : : Tcl_Interp *interp;
297 : : int wantobjects;
298 : : int threaded; /* True if tcl_platform[threaded] */
299 : : Tcl_ThreadId thread_id;
300 : : int dispatching;
301 : : /* We cannot include tclInt.h, as this is internal.
302 : : So we cache interesting types here. */
303 : : const Tcl_ObjType *OldBooleanType;
304 : : const Tcl_ObjType *BooleanType;
305 : : const Tcl_ObjType *ByteArrayType;
306 : : const Tcl_ObjType *DoubleType;
307 : : const Tcl_ObjType *IntType;
308 : : const Tcl_ObjType *WideIntType;
309 : : const Tcl_ObjType *BignumType;
310 : : const Tcl_ObjType *ListType;
311 : : const Tcl_ObjType *ProcBodyType;
312 : : const Tcl_ObjType *StringType;
313 : : } TkappObject;
314 : :
315 : : #define Tkapp_Interp(v) (((TkappObject *) (v))->interp)
316 : :
317 : :
318 : : /**** Error Handling ****/
319 : :
320 : : static PyObject *Tkinter_TclError;
321 : : static int quitMainLoop = 0;
322 : : static int errorInCmd = 0;
323 : : static PyObject *excInCmd;
324 : :
325 : : #ifdef TKINTER_PROTECT_LOADTK
326 : : static int tk_load_failed = 0;
327 : : #endif
328 : :
329 : :
330 : : static PyObject *Tkapp_UnicodeResult(TkappObject *);
331 : :
332 : : static PyObject *
333 : 0 : Tkinter_Error(TkappObject *self)
334 : : {
335 : 0 : PyObject *res = Tkapp_UnicodeResult(self);
336 [ # # ]: 0 : if (res != NULL) {
337 : 0 : PyErr_SetObject(Tkinter_TclError, res);
338 : 0 : Py_DECREF(res);
339 : : }
340 : 0 : return NULL;
341 : : }
342 : :
343 : :
344 : :
345 : : /**** Utils ****/
346 : :
347 : : static int Tkinter_busywaitinterval = 20;
348 : :
349 : : #ifndef MS_WINDOWS
350 : :
351 : : /* Millisecond sleep() for Unix platforms. */
352 : :
353 : : static void
354 : 0 : Sleep(int milli)
355 : : {
356 : : /* XXX Too bad if you don't have select(). */
357 : : struct timeval t;
358 : 0 : t.tv_sec = milli/1000;
359 : 0 : t.tv_usec = (milli%1000) * 1000;
360 : 0 : select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
361 : 0 : }
362 : : #endif /* MS_WINDOWS */
363 : :
364 : : /* Wait up to 1s for the mainloop to come up. */
365 : :
366 : : static int
367 : 0 : WaitForMainloop(TkappObject* self)
368 : : {
369 : : int i;
370 [ # # ]: 0 : for (i = 0; i < 10; i++) {
371 [ # # ]: 0 : if (self->dispatching)
372 : 0 : return 1;
373 : 0 : Py_BEGIN_ALLOW_THREADS
374 : 0 : Sleep(100);
375 : 0 : Py_END_ALLOW_THREADS
376 : : }
377 [ # # ]: 0 : if (self->dispatching)
378 : 0 : return 1;
379 : 0 : PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop");
380 : 0 : return 0;
381 : : }
382 : :
383 : :
384 : :
385 : : #define ARGSZ 64
386 : :
387 : :
388 : :
389 : : static PyObject *
390 : 0 : unicodeFromTclStringAndSize(const char *s, Py_ssize_t size)
391 : : {
392 : 0 : PyObject *r = PyUnicode_DecodeUTF8(s, size, NULL);
393 [ # # # # ]: 0 : if (r != NULL || !PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
394 : 0 : return r;
395 : : }
396 : :
397 : 0 : char *buf = NULL;
398 : 0 : PyErr_Clear();
399 : : /* Tcl encodes null character as \xc0\x80.
400 : : https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 */
401 [ # # ]: 0 : if (memchr(s, '\xc0', size)) {
402 : : char *q;
403 : 0 : const char *e = s + size;
404 : 0 : q = buf = (char *)PyMem_Malloc(size);
405 [ # # ]: 0 : if (buf == NULL) {
406 : 0 : PyErr_NoMemory();
407 : 0 : return NULL;
408 : : }
409 [ # # ]: 0 : while (s != e) {
410 [ # # # # : 0 : if (s + 1 != e && s[0] == '\xc0' && s[1] == '\x80') {
# # ]
411 : 0 : *q++ = '\0';
412 : 0 : s += 2;
413 : : }
414 : : else
415 : 0 : *q++ = *s++;
416 : : }
417 : 0 : s = buf;
418 : 0 : size = q - s;
419 : : }
420 : 0 : r = PyUnicode_DecodeUTF8(s, size, "surrogateescape");
421 [ # # ]: 0 : if (buf != NULL) {
422 : 0 : PyMem_Free(buf);
423 : : }
424 [ # # # # ]: 0 : if (r == NULL || PyUnicode_KIND(r) == PyUnicode_1BYTE_KIND) {
425 : 0 : return r;
426 : : }
427 : :
428 : : /* In CESU-8 non-BMP characters are represented as a surrogate pair,
429 : : like in UTF-16, and then each surrogate code point is encoded in UTF-8.
430 : : https://en.wikipedia.org/wiki/CESU-8 */
431 : 0 : Py_ssize_t len = PyUnicode_GET_LENGTH(r);
432 : : Py_ssize_t i, j;
433 : : /* All encoded surrogate characters start with \xED. */
434 : 0 : i = PyUnicode_FindChar(r, 0xdcED, 0, len, 1);
435 [ # # ]: 0 : if (i == -2) {
436 : 0 : Py_DECREF(r);
437 : 0 : return NULL;
438 : : }
439 [ # # ]: 0 : if (i == -1) {
440 : 0 : return r;
441 : : }
442 : 0 : Py_UCS4 *u = PyUnicode_AsUCS4Copy(r);
443 : 0 : Py_DECREF(r);
444 [ # # ]: 0 : if (u == NULL) {
445 : 0 : return NULL;
446 : : }
447 : : Py_UCS4 ch;
448 [ # # ]: 0 : for (j = i; i < len; i++, u[j++] = ch) {
449 : : Py_UCS4 ch1, ch2, ch3, high, low;
450 : : /* Low surrogates U+D800 - U+DBFF are encoded as
451 : : \xED\xA0\x80 - \xED\xAF\xBF. */
452 : 0 : ch1 = ch = u[i];
453 [ # # ]: 0 : if (ch1 != 0xdcED) continue;
454 : 0 : ch2 = u[i + 1];
455 [ # # # # ]: 0 : if (!(0xdcA0 <= ch2 && ch2 <= 0xdcAF)) continue;
456 : 0 : ch3 = u[i + 2];
457 [ # # # # ]: 0 : if (!(0xdc80 <= ch3 && ch3 <= 0xdcBF)) continue;
458 : 0 : high = 0xD000 | ((ch2 & 0x3F) << 6) | (ch3 & 0x3F);
459 : : assert(Py_UNICODE_IS_HIGH_SURROGATE(high));
460 : : /* High surrogates U+DC00 - U+DFFF are encoded as
461 : : \xED\xB0\x80 - \xED\xBF\xBF. */
462 : 0 : ch1 = u[i + 3];
463 [ # # ]: 0 : if (ch1 != 0xdcED) continue;
464 : 0 : ch2 = u[i + 4];
465 [ # # # # ]: 0 : if (!(0xdcB0 <= ch2 && ch2 <= 0xdcBF)) continue;
466 : 0 : ch3 = u[i + 5];
467 [ # # # # ]: 0 : if (!(0xdc80 <= ch3 && ch3 <= 0xdcBF)) continue;
468 : 0 : low = 0xD000 | ((ch2 & 0x3F) << 6) | (ch3 & 0x3F);
469 : : assert(Py_UNICODE_IS_HIGH_SURROGATE(high));
470 : 0 : ch = Py_UNICODE_JOIN_SURROGATES(high, low);
471 : 0 : i += 5;
472 : : }
473 : 0 : r = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, u, j);
474 : 0 : PyMem_Free(u);
475 : 0 : return r;
476 : : }
477 : :
478 : : static PyObject *
479 : 0 : unicodeFromTclString(const char *s)
480 : : {
481 : 0 : return unicodeFromTclStringAndSize(s, strlen(s));
482 : : }
483 : :
484 : : static PyObject *
485 : 0 : unicodeFromTclObj(Tcl_Obj *value)
486 : : {
487 : : int len;
488 : : #if USE_TCL_UNICODE
489 : : int byteorder = NATIVE_BYTEORDER;
490 : : const Tcl_UniChar *u = Tcl_GetUnicodeFromObj(value, &len);
491 : : if (sizeof(Tcl_UniChar) == 2)
492 : : return PyUnicode_DecodeUTF16((const char *)u, len * 2,
493 : : "surrogatepass", &byteorder);
494 : : else if (sizeof(Tcl_UniChar) == 4)
495 : : return PyUnicode_DecodeUTF32((const char *)u, len * 4,
496 : : "surrogatepass", &byteorder);
497 : : else
498 : : Py_UNREACHABLE();
499 : : #else
500 : 0 : const char *s = Tcl_GetStringFromObj(value, &len);
501 : 0 : return unicodeFromTclStringAndSize(s, len);
502 : : #endif
503 : : }
504 : :
505 : : /*[clinic input]
506 : : module _tkinter
507 : : class _tkinter.tkapp "TkappObject *" "&Tkapp_Type_spec"
508 : : class _tkinter.Tcl_Obj "PyTclObject *" "&PyTclObject_Type_spec"
509 : : class _tkinter.tktimertoken "TkttObject *" "&Tktt_Type_spec"
510 : : [clinic start generated code]*/
511 : : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b1ebf15c162ee229]*/
512 : :
513 : : /**** Tkapp Object ****/
514 : :
515 : : #ifndef WITH_APPINIT
516 : : int
517 : : Tcl_AppInit(Tcl_Interp *interp)
518 : : {
519 : : const char * _tkinter_skip_tk_init;
520 : :
521 : : if (Tcl_Init(interp) == TCL_ERROR) {
522 : : PySys_WriteStderr("Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
523 : : return TCL_ERROR;
524 : : }
525 : :
526 : : _tkinter_skip_tk_init = Tcl_GetVar(interp,
527 : : "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
528 : : if (_tkinter_skip_tk_init != NULL &&
529 : : strcmp(_tkinter_skip_tk_init, "1") == 0) {
530 : : return TCL_OK;
531 : : }
532 : :
533 : : #ifdef TKINTER_PROTECT_LOADTK
534 : : if (tk_load_failed) {
535 : : PySys_WriteStderr("Tk_Init error: %s\n", TKINTER_LOADTK_ERRMSG);
536 : : return TCL_ERROR;
537 : : }
538 : : #endif
539 : :
540 : : if (Tk_Init(interp) == TCL_ERROR) {
541 : : #ifdef TKINTER_PROTECT_LOADTK
542 : : tk_load_failed = 1;
543 : : #endif
544 : : PySys_WriteStderr("Tk_Init error: %s\n", Tcl_GetStringResult(interp));
545 : : return TCL_ERROR;
546 : : }
547 : :
548 : : return TCL_OK;
549 : : }
550 : : #endif /* !WITH_APPINIT */
551 : :
552 : :
553 : :
554 : :
555 : : /* Initialize the Tk application; see the `main' function in
556 : : * `tkMain.c'.
557 : : */
558 : :
559 : : static void EnableEventHook(void); /* Forward */
560 : : static void DisableEventHook(void); /* Forward */
561 : :
562 : : static TkappObject *
563 : 0 : Tkapp_New(const char *screenName, const char *className,
564 : : int interactive, int wantobjects, int wantTk, int sync,
565 : : const char *use)
566 : : {
567 : : TkappObject *v;
568 : : char *argv0;
569 : :
570 : 0 : v = PyObject_New(TkappObject, (PyTypeObject *) Tkapp_Type);
571 [ # # ]: 0 : if (v == NULL)
572 : 0 : return NULL;
573 : :
574 : 0 : v->interp = Tcl_CreateInterp();
575 : 0 : v->wantobjects = wantobjects;
576 : 0 : v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded",
577 : 0 : TCL_GLOBAL_ONLY) != NULL;
578 : 0 : v->thread_id = Tcl_GetCurrentThread();
579 : 0 : v->dispatching = 0;
580 : :
581 : : #ifndef TCL_THREADS
582 : : if (v->threaded) {
583 : : PyErr_SetString(PyExc_RuntimeError,
584 : : "Tcl is threaded but _tkinter is not");
585 : : Py_DECREF(v);
586 : : return 0;
587 : : }
588 : : #endif
589 [ # # # # ]: 0 : if (v->threaded && tcl_lock) {
590 : : /* If Tcl is threaded, we don't need the lock. */
591 : 0 : PyThread_free_lock(tcl_lock);
592 : 0 : tcl_lock = NULL;
593 : : }
594 : :
595 : 0 : v->OldBooleanType = Tcl_GetObjType("boolean");
596 : 0 : v->BooleanType = Tcl_GetObjType("booleanString");
597 : 0 : v->ByteArrayType = Tcl_GetObjType("bytearray");
598 : 0 : v->DoubleType = Tcl_GetObjType("double");
599 : 0 : v->IntType = Tcl_GetObjType("int");
600 : 0 : v->WideIntType = Tcl_GetObjType("wideInt");
601 : 0 : v->BignumType = Tcl_GetObjType("bignum");
602 : 0 : v->ListType = Tcl_GetObjType("list");
603 : 0 : v->ProcBodyType = Tcl_GetObjType("procbody");
604 : 0 : v->StringType = Tcl_GetObjType("string");
605 : :
606 : : /* Delete the 'exit' command, which can screw things up */
607 : 0 : Tcl_DeleteCommand(v->interp, "exit");
608 : :
609 [ # # ]: 0 : if (screenName != NULL)
610 : 0 : Tcl_SetVar2(v->interp, "env", "DISPLAY",
611 : : screenName, TCL_GLOBAL_ONLY);
612 : :
613 [ # # ]: 0 : if (interactive)
614 : 0 : Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
615 : : else
616 : 0 : Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
617 : :
618 : : /* This is used to get the application class for Tk 4.1 and up */
619 : 0 : argv0 = (char*)PyMem_Malloc(strlen(className) + 1);
620 [ # # ]: 0 : if (!argv0) {
621 : 0 : PyErr_NoMemory();
622 : 0 : Py_DECREF(v);
623 : 0 : return NULL;
624 : : }
625 : :
626 : 0 : strcpy(argv0, className);
627 [ # # ]: 0 : if (Py_ISUPPER(argv0[0]))
628 : 0 : argv0[0] = Py_TOLOWER(argv0[0]);
629 : 0 : Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
630 : 0 : PyMem_Free(argv0);
631 : :
632 [ # # ]: 0 : if (! wantTk) {
633 : 0 : Tcl_SetVar(v->interp,
634 : : "_tkinter_skip_tk_init", "1", TCL_GLOBAL_ONLY);
635 : : }
636 : : #ifdef TKINTER_PROTECT_LOADTK
637 : : else if (tk_load_failed) {
638 : : Tcl_SetVar(v->interp,
639 : : "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
640 : : }
641 : : #endif
642 : :
643 : : /* some initial arguments need to be in argv */
644 [ # # # # ]: 0 : if (sync || use) {
645 : : char *args;
646 : 0 : Py_ssize_t len = 0;
647 : :
648 [ # # ]: 0 : if (sync)
649 : 0 : len += sizeof "-sync";
650 [ # # ]: 0 : if (use)
651 : 0 : len += strlen(use) + sizeof "-use "; /* never overflows */
652 : :
653 : 0 : args = (char*)PyMem_Malloc(len);
654 [ # # ]: 0 : if (!args) {
655 : 0 : PyErr_NoMemory();
656 : 0 : Py_DECREF(v);
657 : 0 : return NULL;
658 : : }
659 : :
660 : 0 : args[0] = '\0';
661 [ # # ]: 0 : if (sync)
662 : 0 : strcat(args, "-sync");
663 [ # # ]: 0 : if (use) {
664 [ # # ]: 0 : if (sync)
665 : 0 : strcat(args, " ");
666 : 0 : strcat(args, "-use ");
667 : 0 : strcat(args, use);
668 : : }
669 : :
670 : 0 : Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY);
671 : 0 : PyMem_Free(args);
672 : : }
673 : :
674 : : #ifdef MS_WINDOWS
675 : : {
676 : : PyObject *str_path;
677 : : PyObject *utf8_path;
678 : : DWORD ret;
679 : :
680 : : ret = GetEnvironmentVariableW(L"TCL_LIBRARY", NULL, 0);
681 : : if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
682 : : str_path = _get_tcl_lib_path();
683 : : if (str_path == NULL && PyErr_Occurred()) {
684 : : return NULL;
685 : : }
686 : : if (str_path != NULL) {
687 : : utf8_path = PyUnicode_AsUTF8String(str_path);
688 : : if (utf8_path == NULL) {
689 : : return NULL;
690 : : }
691 : : Tcl_SetVar(v->interp,
692 : : "tcl_library",
693 : : PyBytes_AS_STRING(utf8_path),
694 : : TCL_GLOBAL_ONLY);
695 : : Py_DECREF(utf8_path);
696 : : }
697 : : }
698 : : }
699 : : #endif
700 : :
701 [ # # ]: 0 : if (Tcl_AppInit(v->interp) != TCL_OK) {
702 : 0 : PyObject *result = Tkinter_Error(v);
703 : : #ifdef TKINTER_PROTECT_LOADTK
704 : : if (wantTk) {
705 : : const char *_tkinter_tk_failed;
706 : : _tkinter_tk_failed = Tcl_GetVar(v->interp,
707 : : "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
708 : :
709 : : if ( _tkinter_tk_failed != NULL &&
710 : : strcmp(_tkinter_tk_failed, "1") == 0) {
711 : : tk_load_failed = 1;
712 : : }
713 : : }
714 : : #endif
715 : 0 : Py_DECREF((PyObject *)v);
716 : 0 : return (TkappObject *)result;
717 : : }
718 : :
719 : 0 : EnableEventHook();
720 : :
721 : 0 : return v;
722 : : }
723 : :
724 : :
725 : : static void
726 : 0 : Tkapp_ThreadSend(TkappObject *self, Tcl_Event *ev,
727 : : Tcl_Condition *cond, Tcl_Mutex *mutex)
728 : : {
729 : 0 : Py_BEGIN_ALLOW_THREADS;
730 : 0 : Tcl_MutexLock(mutex);
731 : 0 : Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL);
732 : 0 : Tcl_ThreadAlert(self->thread_id);
733 : 0 : Tcl_ConditionWait(cond, mutex, NULL);
734 : 0 : Tcl_MutexUnlock(mutex);
735 : 0 : Py_END_ALLOW_THREADS
736 : 0 : }
737 : :
738 : :
739 : : /** Tcl Eval **/
740 : :
741 : : typedef struct {
742 : : PyObject_HEAD
743 : : Tcl_Obj *value;
744 : : PyObject *string; /* This cannot cause cycles. */
745 : : } PyTclObject;
746 : :
747 : : static PyObject *PyTclObject_Type;
748 : : #define PyTclObject_Check(v) Py_IS_TYPE(v, (PyTypeObject *) PyTclObject_Type)
749 : :
750 : : static PyObject *
751 : 0 : newPyTclObject(Tcl_Obj *arg)
752 : : {
753 : : PyTclObject *self;
754 : 0 : self = PyObject_New(PyTclObject, (PyTypeObject *) PyTclObject_Type);
755 [ # # ]: 0 : if (self == NULL)
756 : 0 : return NULL;
757 : 0 : Tcl_IncrRefCount(arg);
758 : 0 : self->value = arg;
759 : 0 : self->string = NULL;
760 : 0 : return (PyObject*)self;
761 : : }
762 : :
763 : : static void
764 : 0 : PyTclObject_dealloc(PyTclObject *self)
765 : : {
766 : 0 : PyObject *tp = (PyObject *) Py_TYPE(self);
767 [ # # ]: 0 : Tcl_DecrRefCount(self->value);
768 : 0 : Py_XDECREF(self->string);
769 : 0 : PyObject_Free(self);
770 : 0 : Py_DECREF(tp);
771 : 0 : }
772 : :
773 : : /* Like _str, but create Unicode if necessary. */
774 : : PyDoc_STRVAR(PyTclObject_string__doc__,
775 : : "the string representation of this object, either as str or bytes");
776 : :
777 : : static PyObject *
778 : 0 : PyTclObject_string(PyTclObject *self, void *ignored)
779 : : {
780 [ # # ]: 0 : if (!self->string) {
781 : 0 : self->string = unicodeFromTclObj(self->value);
782 [ # # ]: 0 : if (!self->string)
783 : 0 : return NULL;
784 : : }
785 : 0 : return Py_NewRef(self->string);
786 : : }
787 : :
788 : : static PyObject *
789 : 0 : PyTclObject_str(PyTclObject *self)
790 : : {
791 [ # # ]: 0 : if (self->string) {
792 : 0 : return Py_NewRef(self->string);
793 : : }
794 : : /* XXX Could cache result if it is non-ASCII. */
795 : 0 : return unicodeFromTclObj(self->value);
796 : : }
797 : :
798 : : static PyObject *
799 : 0 : PyTclObject_repr(PyTclObject *self)
800 : : {
801 : 0 : PyObject *repr, *str = PyTclObject_str(self);
802 [ # # ]: 0 : if (str == NULL)
803 : 0 : return NULL;
804 : 0 : repr = PyUnicode_FromFormat("<%s object: %R>",
805 : 0 : self->value->typePtr->name, str);
806 : 0 : Py_DECREF(str);
807 : 0 : return repr;
808 : : }
809 : :
810 : : static PyObject *
811 : 0 : PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
812 : : {
813 : : int result;
814 : :
815 : : /* neither argument should be NULL, unless something's gone wrong */
816 [ # # # # ]: 0 : if (self == NULL || other == NULL) {
817 : 0 : PyErr_BadInternalCall();
818 : 0 : return NULL;
819 : : }
820 : :
821 : : /* both arguments should be instances of PyTclObject */
822 [ # # # # ]: 0 : if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
823 : 0 : Py_RETURN_NOTIMPLEMENTED;
824 : : }
825 : :
826 [ # # ]: 0 : if (self == other)
827 : : /* fast path when self and other are identical */
828 : 0 : result = 0;
829 : : else
830 : 0 : result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
831 : 0 : Tcl_GetString(((PyTclObject *)other)->value));
832 [ # # # # : 0 : Py_RETURN_RICHCOMPARE(result, 0, op);
# # # # #
# # # # #
# # # #
# ]
833 : : }
834 : :
835 : : PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
836 : :
837 : : static PyObject*
838 : 0 : get_typename(PyTclObject* obj, void* ignored)
839 : : {
840 : 0 : return unicodeFromTclString(obj->value->typePtr->name);
841 : : }
842 : :
843 : :
844 : : static PyGetSetDef PyTclObject_getsetlist[] = {
845 : : {"typename", (getter)get_typename, NULL, get_typename__doc__},
846 : : {"string", (getter)PyTclObject_string, NULL,
847 : : PyTclObject_string__doc__},
848 : : {0},
849 : : };
850 : :
851 : : static PyType_Slot PyTclObject_Type_slots[] = {
852 : : {Py_tp_dealloc, (destructor)PyTclObject_dealloc},
853 : : {Py_tp_repr, (reprfunc)PyTclObject_repr},
854 : : {Py_tp_str, (reprfunc)PyTclObject_str},
855 : : {Py_tp_getattro, PyObject_GenericGetAttr},
856 : : {Py_tp_richcompare, PyTclObject_richcompare},
857 : : {Py_tp_getset, PyTclObject_getsetlist},
858 : : {0, 0}
859 : : };
860 : :
861 : : static PyType_Spec PyTclObject_Type_spec = {
862 : : "_tkinter.Tcl_Obj",
863 : : sizeof(PyTclObject),
864 : : 0,
865 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
866 : : PyTclObject_Type_slots,
867 : : };
868 : :
869 : :
870 : : #if SIZE_MAX > INT_MAX
871 : : #define CHECK_STRING_LENGTH(s) do { \
872 : : if (s != NULL && strlen(s) >= INT_MAX) { \
873 : : PyErr_SetString(PyExc_OverflowError, "string is too long"); \
874 : : return NULL; \
875 : : } } while(0)
876 : : #else
877 : : #define CHECK_STRING_LENGTH(s)
878 : : #endif
879 : :
880 : : static Tcl_Obj*
881 : 0 : asBignumObj(PyObject *value)
882 : : {
883 : : Tcl_Obj *result;
884 : : int neg;
885 : : PyObject *hexstr;
886 : : const char *hexchars;
887 : : mp_int bigValue;
888 : :
889 : 0 : neg = Py_SIZE(value) < 0;
890 : 0 : hexstr = _PyLong_Format(value, 16);
891 [ # # ]: 0 : if (hexstr == NULL)
892 : 0 : return NULL;
893 : 0 : hexchars = PyUnicode_AsUTF8(hexstr);
894 [ # # ]: 0 : if (hexchars == NULL) {
895 : 0 : Py_DECREF(hexstr);
896 : 0 : return NULL;
897 : : }
898 : 0 : hexchars += neg + 2; /* skip sign and "0x" */
899 : 0 : mp_init(&bigValue);
900 [ # # ]: 0 : if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) {
901 : 0 : mp_clear(&bigValue);
902 : 0 : Py_DECREF(hexstr);
903 : 0 : PyErr_NoMemory();
904 : 0 : return NULL;
905 : : }
906 : 0 : Py_DECREF(hexstr);
907 : 0 : bigValue.sign = neg ? MP_NEG : MP_ZPOS;
908 : 0 : result = Tcl_NewBignumObj(&bigValue);
909 : 0 : mp_clear(&bigValue);
910 [ # # ]: 0 : if (result == NULL) {
911 : 0 : PyErr_NoMemory();
912 : 0 : return NULL;
913 : : }
914 : 0 : return result;
915 : : }
916 : :
917 : : static Tcl_Obj*
918 : 0 : AsObj(PyObject *value)
919 : : {
920 : : Tcl_Obj *result;
921 : :
922 [ # # ]: 0 : if (PyBytes_Check(value)) {
923 [ # # ]: 0 : if (PyBytes_GET_SIZE(value) >= INT_MAX) {
924 : 0 : PyErr_SetString(PyExc_OverflowError, "bytes object is too long");
925 : 0 : return NULL;
926 : : }
927 : 0 : return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value),
928 : 0 : (int)PyBytes_GET_SIZE(value));
929 : : }
930 : :
931 [ # # ]: 0 : if (PyBool_Check(value))
932 : 0 : return Tcl_NewBooleanObj(PyObject_IsTrue(value));
933 : :
934 [ # # ]: 0 : if (PyLong_CheckExact(value)) {
935 : : int overflow;
936 : : long longValue;
937 : : #ifdef TCL_WIDE_INT_TYPE
938 : : Tcl_WideInt wideValue;
939 : : #endif
940 : 0 : longValue = PyLong_AsLongAndOverflow(value, &overflow);
941 [ # # ]: 0 : if (!overflow) {
942 : 0 : return Tcl_NewLongObj(longValue);
943 : : }
944 : : /* If there is an overflow in the long conversion,
945 : : fall through to wideInt handling. */
946 : : #ifdef TCL_WIDE_INT_TYPE
947 [ # # ]: 0 : if (_PyLong_AsByteArray((PyLongObject *)value,
948 : : (unsigned char *)(void *)&wideValue,
949 : : sizeof(wideValue),
950 : : PY_LITTLE_ENDIAN,
951 : : /* signed */ 1) == 0) {
952 : 0 : return Tcl_NewWideIntObj(wideValue);
953 : : }
954 : 0 : PyErr_Clear();
955 : : #endif
956 : : /* If there is an overflow in the wideInt conversion,
957 : : fall through to bignum handling. */
958 : 0 : return asBignumObj(value);
959 : : /* If there is no wideInt or bignum support,
960 : : fall through to default object handling. */
961 : : }
962 : :
963 [ # # ]: 0 : if (PyFloat_Check(value))
964 : 0 : return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
965 : :
966 [ # # # # ]: 0 : if (PyTuple_Check(value) || PyList_Check(value)) {
967 : : Tcl_Obj **argv;
968 : : Py_ssize_t size, i;
969 : :
970 [ # # ]: 0 : size = PySequence_Fast_GET_SIZE(value);
971 [ # # ]: 0 : if (size == 0)
972 : 0 : return Tcl_NewListObj(0, NULL);
973 [ # # ]: 0 : if (!CHECK_SIZE(size, sizeof(Tcl_Obj *))) {
974 [ # # ]: 0 : PyErr_SetString(PyExc_OverflowError,
975 : 0 : PyTuple_Check(value) ? "tuple is too long" :
976 : : "list is too long");
977 : 0 : return NULL;
978 : : }
979 : 0 : argv = (Tcl_Obj **) PyMem_Malloc(((size_t)size) * sizeof(Tcl_Obj *));
980 [ # # ]: 0 : if (!argv) {
981 : 0 : PyErr_NoMemory();
982 : 0 : return NULL;
983 : : }
984 [ # # ]: 0 : for (i = 0; i < size; i++)
985 [ # # ]: 0 : argv[i] = AsObj(PySequence_Fast_GET_ITEM(value,i));
986 : 0 : result = Tcl_NewListObj((int)size, argv);
987 : 0 : PyMem_Free(argv);
988 : 0 : return result;
989 : : }
990 : :
991 [ # # ]: 0 : if (PyUnicode_Check(value)) {
992 [ # # ]: 0 : if (PyUnicode_READY(value) == -1)
993 : 0 : return NULL;
994 : :
995 : 0 : Py_ssize_t size = PyUnicode_GET_LENGTH(value);
996 [ # # ]: 0 : if (size == 0) {
997 : 0 : return Tcl_NewStringObj("", 0);
998 : : }
999 [ # # ]: 0 : if (!CHECK_SIZE(size, sizeof(Tcl_UniChar))) {
1000 : 0 : PyErr_SetString(PyExc_OverflowError, "string is too long");
1001 : 0 : return NULL;
1002 : : }
1003 [ # # ]: 0 : if (PyUnicode_IS_ASCII(value)) {
1004 : 0 : return Tcl_NewStringObj((const char *)PyUnicode_DATA(value),
1005 : : (int)size);
1006 : : }
1007 : :
1008 : : PyObject *encoded;
1009 : : #if USE_TCL_UNICODE
1010 : : if (sizeof(Tcl_UniChar) == 2)
1011 : : encoded = _PyUnicode_EncodeUTF16(value,
1012 : : "surrogatepass", NATIVE_BYTEORDER);
1013 : : else if (sizeof(Tcl_UniChar) == 4)
1014 : : encoded = _PyUnicode_EncodeUTF32(value,
1015 : : "surrogatepass", NATIVE_BYTEORDER);
1016 : : else
1017 : : Py_UNREACHABLE();
1018 : : #else
1019 : 0 : encoded = _PyUnicode_AsUTF8String(value, "surrogateescape");
1020 : : #endif
1021 [ # # ]: 0 : if (!encoded) {
1022 : 0 : return NULL;
1023 : : }
1024 : 0 : size = PyBytes_GET_SIZE(encoded);
1025 [ # # ]: 0 : if (size > INT_MAX) {
1026 : 0 : Py_DECREF(encoded);
1027 : 0 : PyErr_SetString(PyExc_OverflowError, "string is too long");
1028 : 0 : return NULL;
1029 : : }
1030 : : #if USE_TCL_UNICODE
1031 : : result = Tcl_NewUnicodeObj((const Tcl_UniChar *)PyBytes_AS_STRING(encoded),
1032 : : (int)(size / sizeof(Tcl_UniChar)));
1033 : : #else
1034 : 0 : result = Tcl_NewStringObj(PyBytes_AS_STRING(encoded), (int)size);
1035 : : #endif
1036 : 0 : Py_DECREF(encoded);
1037 : 0 : return result;
1038 : : }
1039 : :
1040 [ # # ]: 0 : if (PyTclObject_Check(value)) {
1041 : 0 : return ((PyTclObject*)value)->value;
1042 : : }
1043 : :
1044 : : {
1045 : 0 : PyObject *v = PyObject_Str(value);
1046 [ # # ]: 0 : if (!v)
1047 : 0 : return 0;
1048 : 0 : result = AsObj(v);
1049 : 0 : Py_DECREF(v);
1050 : 0 : return result;
1051 : : }
1052 : : }
1053 : :
1054 : : static PyObject *
1055 : 0 : fromBoolean(TkappObject *tkapp, Tcl_Obj *value)
1056 : : {
1057 : : int boolValue;
1058 [ # # ]: 0 : if (Tcl_GetBooleanFromObj(Tkapp_Interp(tkapp), value, &boolValue) == TCL_ERROR)
1059 : 0 : return Tkinter_Error(tkapp);
1060 : 0 : return PyBool_FromLong(boolValue);
1061 : : }
1062 : :
1063 : : static PyObject*
1064 : 0 : fromWideIntObj(TkappObject *tkapp, Tcl_Obj *value)
1065 : : {
1066 : : Tcl_WideInt wideValue;
1067 [ # # ]: 0 : if (Tcl_GetWideIntFromObj(Tkapp_Interp(tkapp), value, &wideValue) == TCL_OK) {
1068 : : if (sizeof(wideValue) <= SIZEOF_LONG_LONG)
1069 : 0 : return PyLong_FromLongLong(wideValue);
1070 : : return _PyLong_FromByteArray((unsigned char *)(void *)&wideValue,
1071 : : sizeof(wideValue),
1072 : : PY_LITTLE_ENDIAN,
1073 : : /* signed */ 1);
1074 : : }
1075 : 0 : return NULL;
1076 : : }
1077 : :
1078 : : static PyObject*
1079 : 0 : fromBignumObj(TkappObject *tkapp, Tcl_Obj *value)
1080 : : {
1081 : : mp_int bigValue;
1082 : : unsigned long numBytes;
1083 : : unsigned char *bytes;
1084 : : PyObject *res;
1085 : :
1086 [ # # ]: 0 : if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK)
1087 : 0 : return Tkinter_Error(tkapp);
1088 : 0 : numBytes = mp_unsigned_bin_size(&bigValue);
1089 : 0 : bytes = PyMem_Malloc(numBytes);
1090 [ # # ]: 0 : if (bytes == NULL) {
1091 : 0 : mp_clear(&bigValue);
1092 : 0 : return PyErr_NoMemory();
1093 : : }
1094 [ # # ]: 0 : if (mp_to_unsigned_bin_n(&bigValue, bytes,
1095 : : &numBytes) != MP_OKAY) {
1096 : 0 : mp_clear(&bigValue);
1097 : 0 : PyMem_Free(bytes);
1098 : 0 : return PyErr_NoMemory();
1099 : : }
1100 : 0 : res = _PyLong_FromByteArray(bytes, numBytes,
1101 : : /* big-endian */ 0,
1102 : : /* unsigned */ 0);
1103 : 0 : PyMem_Free(bytes);
1104 [ # # # # ]: 0 : if (res != NULL && bigValue.sign == MP_NEG) {
1105 : 0 : PyObject *res2 = PyNumber_Negative(res);
1106 : 0 : Py_SETREF(res, res2);
1107 : : }
1108 : 0 : mp_clear(&bigValue);
1109 : 0 : return res;
1110 : : }
1111 : :
1112 : : static PyObject*
1113 : 0 : FromObj(TkappObject *tkapp, Tcl_Obj *value)
1114 : : {
1115 : 0 : PyObject *result = NULL;
1116 : 0 : Tcl_Interp *interp = Tkapp_Interp(tkapp);
1117 : :
1118 [ # # ]: 0 : if (value->typePtr == NULL) {
1119 : 0 : return unicodeFromTclObj(value);
1120 : : }
1121 : :
1122 [ # # ]: 0 : if (value->typePtr == tkapp->BooleanType ||
1123 [ # # ]: 0 : value->typePtr == tkapp->OldBooleanType) {
1124 : 0 : return fromBoolean(tkapp, value);
1125 : : }
1126 : :
1127 [ # # ]: 0 : if (value->typePtr == tkapp->ByteArrayType) {
1128 : : int size;
1129 : 0 : char *data = (char*)Tcl_GetByteArrayFromObj(value, &size);
1130 : 0 : return PyBytes_FromStringAndSize(data, size);
1131 : : }
1132 : :
1133 [ # # ]: 0 : if (value->typePtr == tkapp->DoubleType) {
1134 : 0 : return PyFloat_FromDouble(value->internalRep.doubleValue);
1135 : : }
1136 : :
1137 [ # # ]: 0 : if (value->typePtr == tkapp->IntType) {
1138 : : long longValue;
1139 [ # # ]: 0 : if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK)
1140 : 0 : return PyLong_FromLong(longValue);
1141 : : /* If there is an error in the long conversion,
1142 : : fall through to wideInt handling. */
1143 : : }
1144 : :
1145 [ # # ]: 0 : if (value->typePtr == tkapp->IntType ||
1146 [ # # ]: 0 : value->typePtr == tkapp->WideIntType) {
1147 : 0 : result = fromWideIntObj(tkapp, value);
1148 [ # # # # ]: 0 : if (result != NULL || PyErr_Occurred())
1149 : 0 : return result;
1150 : 0 : Tcl_ResetResult(interp);
1151 : : /* If there is an error in the wideInt conversion,
1152 : : fall through to bignum handling. */
1153 : : }
1154 : :
1155 [ # # ]: 0 : if (value->typePtr == tkapp->IntType ||
1156 [ # # ]: 0 : value->typePtr == tkapp->WideIntType ||
1157 [ # # ]: 0 : value->typePtr == tkapp->BignumType) {
1158 : 0 : return fromBignumObj(tkapp, value);
1159 : : }
1160 : :
1161 [ # # ]: 0 : if (value->typePtr == tkapp->ListType) {
1162 : : int size;
1163 : : int i, status;
1164 : : PyObject *elem;
1165 : : Tcl_Obj *tcl_elem;
1166 : :
1167 : 0 : status = Tcl_ListObjLength(interp, value, &size);
1168 [ # # ]: 0 : if (status == TCL_ERROR)
1169 : 0 : return Tkinter_Error(tkapp);
1170 : 0 : result = PyTuple_New(size);
1171 [ # # ]: 0 : if (!result)
1172 : 0 : return NULL;
1173 [ # # ]: 0 : for (i = 0; i < size; i++) {
1174 : 0 : status = Tcl_ListObjIndex(interp, value, i, &tcl_elem);
1175 [ # # ]: 0 : if (status == TCL_ERROR) {
1176 : 0 : Py_DECREF(result);
1177 : 0 : return Tkinter_Error(tkapp);
1178 : : }
1179 : 0 : elem = FromObj(tkapp, tcl_elem);
1180 [ # # ]: 0 : if (!elem) {
1181 : 0 : Py_DECREF(result);
1182 : 0 : return NULL;
1183 : : }
1184 : 0 : PyTuple_SET_ITEM(result, i, elem);
1185 : : }
1186 : 0 : return result;
1187 : : }
1188 : :
1189 : 0 : if (value->typePtr == tkapp->ProcBodyType) {
1190 : : /* fall through: return tcl object. */
1191 : : }
1192 : :
1193 [ # # ]: 0 : if (value->typePtr == tkapp->StringType) {
1194 : 0 : return unicodeFromTclObj(value);
1195 : : }
1196 : :
1197 [ # # ]: 0 : if (tkapp->BooleanType == NULL &&
1198 [ # # ]: 0 : strcmp(value->typePtr->name, "booleanString") == 0) {
1199 : : /* booleanString type is not registered in Tcl */
1200 : 0 : tkapp->BooleanType = value->typePtr;
1201 : 0 : return fromBoolean(tkapp, value);
1202 : : }
1203 : :
1204 [ # # ]: 0 : if (tkapp->BignumType == NULL &&
1205 [ # # ]: 0 : strcmp(value->typePtr->name, "bignum") == 0) {
1206 : : /* bignum type is not registered in Tcl */
1207 : 0 : tkapp->BignumType = value->typePtr;
1208 : 0 : return fromBignumObj(tkapp, value);
1209 : : }
1210 : :
1211 : 0 : return newPyTclObject(value);
1212 : : }
1213 : :
1214 : : /* This mutex synchronizes inter-thread command calls. */
1215 : : TCL_DECLARE_MUTEX(call_mutex)
1216 : :
1217 : : typedef struct Tkapp_CallEvent {
1218 : : Tcl_Event ev; /* Must be first */
1219 : : TkappObject *self;
1220 : : PyObject *args;
1221 : : int flags;
1222 : : PyObject **res;
1223 : : PyObject **exc;
1224 : : Tcl_Condition *done;
1225 : : } Tkapp_CallEvent;
1226 : :
1227 : : void
1228 : 0 : Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc)
1229 : : {
1230 : : int i;
1231 [ # # ]: 0 : for (i = 0; i < objc; i++)
1232 [ # # ]: 0 : Tcl_DecrRefCount(objv[i]);
1233 [ # # ]: 0 : if (objv != objStore)
1234 : 0 : PyMem_Free(objv);
1235 : 0 : }
1236 : :
1237 : : /* Convert Python objects to Tcl objects. This must happen in the
1238 : : interpreter thread, which may or may not be the calling thread. */
1239 : :
1240 : : static Tcl_Obj**
1241 : 0 : Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc)
1242 : : {
1243 : 0 : Tcl_Obj **objv = objStore;
1244 : 0 : Py_ssize_t objc = 0, i;
1245 [ # # ]: 0 : if (args == NULL)
1246 : : /* do nothing */;
1247 : :
1248 [ # # # # ]: 0 : else if (!(PyTuple_Check(args) || PyList_Check(args))) {
1249 : 0 : objv[0] = AsObj(args);
1250 [ # # ]: 0 : if (objv[0] == NULL)
1251 : 0 : goto finally;
1252 : 0 : objc = 1;
1253 : 0 : Tcl_IncrRefCount(objv[0]);
1254 : : }
1255 : : else {
1256 [ # # ]: 0 : objc = PySequence_Fast_GET_SIZE(args);
1257 : :
1258 [ # # ]: 0 : if (objc > ARGSZ) {
1259 [ # # ]: 0 : if (!CHECK_SIZE(objc, sizeof(Tcl_Obj *))) {
1260 [ # # ]: 0 : PyErr_SetString(PyExc_OverflowError,
1261 : 0 : PyTuple_Check(args) ? "tuple is too long" :
1262 : : "list is too long");
1263 : 0 : return NULL;
1264 : : }
1265 : 0 : objv = (Tcl_Obj **)PyMem_Malloc(((size_t)objc) * sizeof(Tcl_Obj *));
1266 [ # # ]: 0 : if (objv == NULL) {
1267 : 0 : PyErr_NoMemory();
1268 : 0 : objc = 0;
1269 : 0 : goto finally;
1270 : : }
1271 : : }
1272 : :
1273 [ # # ]: 0 : for (i = 0; i < objc; i++) {
1274 [ # # ]: 0 : PyObject *v = PySequence_Fast_GET_ITEM(args, i);
1275 [ # # ]: 0 : if (v == Py_None) {
1276 : 0 : objc = i;
1277 : 0 : break;
1278 : : }
1279 : 0 : objv[i] = AsObj(v);
1280 [ # # ]: 0 : if (!objv[i]) {
1281 : : /* Reset objc, so it attempts to clear
1282 : : objects only up to i. */
1283 : 0 : objc = i;
1284 : 0 : goto finally;
1285 : : }
1286 : 0 : Tcl_IncrRefCount(objv[i]);
1287 : : }
1288 : : }
1289 : 0 : *pobjc = (int)objc;
1290 : 0 : return objv;
1291 : 0 : finally:
1292 : 0 : Tkapp_CallDeallocArgs(objv, objStore, (int)objc);
1293 : 0 : return NULL;
1294 : : }
1295 : :
1296 : : /* Convert the results of a command call into a Python string. */
1297 : :
1298 : : static PyObject *
1299 : 0 : Tkapp_UnicodeResult(TkappObject *self)
1300 : : {
1301 : 0 : return unicodeFromTclObj(Tcl_GetObjResult(self->interp));
1302 : : }
1303 : :
1304 : :
1305 : : /* Convert the results of a command call into a Python objects. */
1306 : :
1307 : : static PyObject *
1308 : 0 : Tkapp_ObjectResult(TkappObject *self)
1309 : : {
1310 : 0 : PyObject *res = NULL;
1311 : 0 : Tcl_Obj *value = Tcl_GetObjResult(self->interp);
1312 [ # # ]: 0 : if (self->wantobjects) {
1313 : : /* Not sure whether the IncrRef is necessary, but something
1314 : : may overwrite the interpreter result while we are
1315 : : converting it. */
1316 : 0 : Tcl_IncrRefCount(value);
1317 : 0 : res = FromObj(self, value);
1318 [ # # ]: 0 : Tcl_DecrRefCount(value);
1319 : : } else {
1320 : 0 : res = unicodeFromTclObj(value);
1321 : : }
1322 : 0 : return res;
1323 : : }
1324 : :
1325 : :
1326 : : /* Tkapp_CallProc is the event procedure that is executed in the context of
1327 : : the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't
1328 : : hold the Python lock. */
1329 : :
1330 : : static int
1331 : 0 : Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
1332 : : {
1333 : : Tcl_Obj *objStore[ARGSZ];
1334 : : Tcl_Obj **objv;
1335 : : int objc;
1336 : : int i;
1337 [ # # ]: 0 : ENTER_PYTHON
1338 : 0 : objv = Tkapp_CallArgs(e->args, objStore, &objc);
1339 [ # # ]: 0 : if (!objv) {
1340 : 0 : *(e->exc) = PyErr_GetRaisedException();
1341 : 0 : *(e->res) = NULL;
1342 : : }
1343 [ # # ]: 0 : LEAVE_PYTHON
1344 [ # # ]: 0 : if (!objv)
1345 : 0 : goto done;
1346 : 0 : i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags);
1347 [ # # ]: 0 : ENTER_PYTHON
1348 [ # # ]: 0 : if (i == TCL_ERROR) {
1349 : 0 : *(e->res) = Tkinter_Error(e->self);
1350 : : }
1351 : : else {
1352 : 0 : *(e->res) = Tkapp_ObjectResult(e->self);
1353 : : }
1354 [ # # ]: 0 : if (*(e->res) == NULL) {
1355 : 0 : *(e->exc) = PyErr_GetRaisedException();
1356 : : }
1357 [ # # ]: 0 : LEAVE_PYTHON
1358 : :
1359 : 0 : Tkapp_CallDeallocArgs(objv, objStore, objc);
1360 : 0 : done:
1361 : : /* Wake up calling thread. */
1362 : 0 : Tcl_MutexLock(&call_mutex);
1363 : 0 : Tcl_ConditionNotify(e->done);
1364 : 0 : Tcl_MutexUnlock(&call_mutex);
1365 : 0 : return 1;
1366 : : }
1367 : :
1368 : :
1369 : : /* This is the main entry point for calling a Tcl command.
1370 : : It supports three cases, with regard to threading:
1371 : : 1. Tcl is not threaded: Must have the Tcl lock, then can invoke command in
1372 : : the context of the calling thread.
1373 : : 2. Tcl is threaded, caller of the command is in the interpreter thread:
1374 : : Execute the command in the calling thread. Since the Tcl lock will
1375 : : not be used, we can merge that with case 1.
1376 : : 3. Tcl is threaded, caller is in a different thread: Must queue an event to
1377 : : the interpreter thread. Allocation of Tcl objects needs to occur in the
1378 : : interpreter thread, so we ship the PyObject* args to the target thread,
1379 : : and perform processing there. */
1380 : :
1381 : : static PyObject *
1382 : 0 : Tkapp_Call(PyObject *selfptr, PyObject *args)
1383 : : {
1384 : : Tcl_Obj *objStore[ARGSZ];
1385 : 0 : Tcl_Obj **objv = NULL;
1386 : : int objc, i;
1387 : 0 : PyObject *res = NULL;
1388 : 0 : TkappObject *self = (TkappObject*)selfptr;
1389 : 0 : int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL;
1390 : :
1391 : : /* If args is a single tuple, replace with contents of tuple */
1392 [ # # ]: 0 : if (PyTuple_GET_SIZE(args) == 1) {
1393 : 0 : PyObject *item = PyTuple_GET_ITEM(args, 0);
1394 [ # # ]: 0 : if (PyTuple_Check(item))
1395 : 0 : args = item;
1396 : : }
1397 [ # # # # ]: 0 : if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
1398 : : /* We cannot call the command directly. Instead, we must
1399 : : marshal the parameters to the interpreter thread. */
1400 : : Tkapp_CallEvent *ev;
1401 : 0 : Tcl_Condition cond = NULL;
1402 : : PyObject *exc;
1403 [ # # ]: 0 : if (!WaitForMainloop(self))
1404 : 0 : return NULL;
1405 : 0 : ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent));
1406 [ # # ]: 0 : if (ev == NULL) {
1407 : 0 : PyErr_NoMemory();
1408 : 0 : return NULL;
1409 : : }
1410 : 0 : ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
1411 : 0 : ev->self = self;
1412 : 0 : ev->args = args;
1413 : 0 : ev->res = &res;
1414 : 0 : ev->exc = &exc;
1415 : 0 : ev->done = &cond;
1416 : :
1417 : 0 : Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
1418 : :
1419 [ # # ]: 0 : if (res == NULL) {
1420 [ # # ]: 0 : if (exc) {
1421 : 0 : PyErr_SetRaisedException(exc);
1422 : : }
1423 : : else {
1424 : 0 : PyErr_SetObject(Tkinter_TclError, exc);
1425 : : }
1426 : : }
1427 : 0 : Tcl_ConditionFinalize(&cond);
1428 : : }
1429 : : else
1430 : : {
1431 : :
1432 : 0 : objv = Tkapp_CallArgs(args, objStore, &objc);
1433 [ # # ]: 0 : if (!objv)
1434 : 0 : return NULL;
1435 : :
1436 [ # # ]: 0 : ENTER_TCL
1437 : :
1438 : 0 : i = Tcl_EvalObjv(self->interp, objc, objv, flags);
1439 : :
1440 : 0 : ENTER_OVERLAP
1441 : :
1442 [ # # ]: 0 : if (i == TCL_ERROR)
1443 : 0 : Tkinter_Error(self);
1444 : : else
1445 : 0 : res = Tkapp_ObjectResult(self);
1446 : :
1447 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1448 : :
1449 : 0 : Tkapp_CallDeallocArgs(objv, objStore, objc);
1450 : : }
1451 : 0 : return res;
1452 : : }
1453 : :
1454 : :
1455 : : /*[clinic input]
1456 : : _tkinter.tkapp.eval
1457 : :
1458 : : script: str
1459 : : /
1460 : :
1461 : : [clinic start generated code]*/
1462 : :
1463 : : static PyObject *
1464 : 0 : _tkinter_tkapp_eval_impl(TkappObject *self, const char *script)
1465 : : /*[clinic end generated code: output=24b79831f700dea0 input=481484123a455f22]*/
1466 : : {
1467 : 0 : PyObject *res = NULL;
1468 : : int err;
1469 : :
1470 [ # # # # ]: 0 : CHECK_STRING_LENGTH(script);
1471 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
1472 : :
1473 [ # # ]: 0 : ENTER_TCL
1474 : 0 : err = Tcl_Eval(Tkapp_Interp(self), script);
1475 : 0 : ENTER_OVERLAP
1476 [ # # ]: 0 : if (err == TCL_ERROR)
1477 : 0 : res = Tkinter_Error(self);
1478 : : else
1479 : 0 : res = Tkapp_UnicodeResult(self);
1480 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1481 : 0 : return res;
1482 : : }
1483 : :
1484 : : /*[clinic input]
1485 : : _tkinter.tkapp.evalfile
1486 : :
1487 : : fileName: str
1488 : : /
1489 : :
1490 : : [clinic start generated code]*/
1491 : :
1492 : : static PyObject *
1493 : 0 : _tkinter_tkapp_evalfile_impl(TkappObject *self, const char *fileName)
1494 : : /*[clinic end generated code: output=63be88dcee4f11d3 input=873ab707e5e947e1]*/
1495 : : {
1496 : 0 : PyObject *res = NULL;
1497 : : int err;
1498 : :
1499 [ # # # # ]: 0 : CHECK_STRING_LENGTH(fileName);
1500 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
1501 : :
1502 [ # # ]: 0 : ENTER_TCL
1503 : 0 : err = Tcl_EvalFile(Tkapp_Interp(self), fileName);
1504 : 0 : ENTER_OVERLAP
1505 [ # # ]: 0 : if (err == TCL_ERROR)
1506 : 0 : res = Tkinter_Error(self);
1507 : : else
1508 : 0 : res = Tkapp_UnicodeResult(self);
1509 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1510 : 0 : return res;
1511 : : }
1512 : :
1513 : : /*[clinic input]
1514 : : _tkinter.tkapp.record
1515 : :
1516 : : script: str
1517 : : /
1518 : :
1519 : : [clinic start generated code]*/
1520 : :
1521 : : static PyObject *
1522 : 0 : _tkinter_tkapp_record_impl(TkappObject *self, const char *script)
1523 : : /*[clinic end generated code: output=0ffe08a0061730df input=c0b0db5a21412cac]*/
1524 : : {
1525 : 0 : PyObject *res = NULL;
1526 : : int err;
1527 : :
1528 [ # # # # ]: 0 : CHECK_STRING_LENGTH(script);
1529 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
1530 : :
1531 [ # # ]: 0 : ENTER_TCL
1532 : 0 : err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL);
1533 : 0 : ENTER_OVERLAP
1534 [ # # ]: 0 : if (err == TCL_ERROR)
1535 : 0 : res = Tkinter_Error(self);
1536 : : else
1537 : 0 : res = Tkapp_UnicodeResult(self);
1538 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1539 : 0 : return res;
1540 : : }
1541 : :
1542 : : /*[clinic input]
1543 : : _tkinter.tkapp.adderrorinfo
1544 : :
1545 : : msg: str
1546 : : /
1547 : :
1548 : : [clinic start generated code]*/
1549 : :
1550 : : static PyObject *
1551 : 0 : _tkinter_tkapp_adderrorinfo_impl(TkappObject *self, const char *msg)
1552 : : /*[clinic end generated code: output=52162eaca2ee53cb input=f4b37aec7c7e8c77]*/
1553 : : {
1554 [ # # # # ]: 0 : CHECK_STRING_LENGTH(msg);
1555 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
1556 : :
1557 [ # # ]: 0 : ENTER_TCL
1558 : 0 : Tcl_AddErrorInfo(Tkapp_Interp(self), msg);
1559 [ # # ]: 0 : LEAVE_TCL
1560 : :
1561 : 0 : Py_RETURN_NONE;
1562 : : }
1563 : :
1564 : :
1565 : :
1566 : : /** Tcl Variable **/
1567 : :
1568 : : typedef PyObject* (*EventFunc)(TkappObject *, PyObject *, int);
1569 : :
1570 : : TCL_DECLARE_MUTEX(var_mutex)
1571 : :
1572 : : typedef struct VarEvent {
1573 : : Tcl_Event ev; /* must be first */
1574 : : TkappObject *self;
1575 : : PyObject *args;
1576 : : int flags;
1577 : : EventFunc func;
1578 : : PyObject **res;
1579 : : PyObject **exc;
1580 : : Tcl_Condition *cond;
1581 : : } VarEvent;
1582 : :
1583 : : /*[python]
1584 : :
1585 : : class varname_converter(CConverter):
1586 : : type = 'const char *'
1587 : : converter = 'varname_converter'
1588 : :
1589 : : [python]*/
1590 : : /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
1591 : :
1592 : : static int
1593 : 0 : varname_converter(PyObject *in, void *_out)
1594 : : {
1595 : : const char *s;
1596 : 0 : const char **out = (const char**)_out;
1597 [ # # ]: 0 : if (PyBytes_Check(in)) {
1598 [ # # ]: 0 : if (PyBytes_GET_SIZE(in) > INT_MAX) {
1599 : 0 : PyErr_SetString(PyExc_OverflowError, "bytes object is too long");
1600 : 0 : return 0;
1601 : : }
1602 : 0 : s = PyBytes_AS_STRING(in);
1603 [ # # ]: 0 : if (strlen(s) != (size_t)PyBytes_GET_SIZE(in)) {
1604 : 0 : PyErr_SetString(PyExc_ValueError, "embedded null byte");
1605 : 0 : return 0;
1606 : : }
1607 : 0 : *out = s;
1608 : 0 : return 1;
1609 : : }
1610 [ # # ]: 0 : if (PyUnicode_Check(in)) {
1611 : : Py_ssize_t size;
1612 : 0 : s = PyUnicode_AsUTF8AndSize(in, &size);
1613 [ # # ]: 0 : if (s == NULL) {
1614 : 0 : return 0;
1615 : : }
1616 [ # # ]: 0 : if (size > INT_MAX) {
1617 : 0 : PyErr_SetString(PyExc_OverflowError, "string is too long");
1618 : 0 : return 0;
1619 : : }
1620 [ # # ]: 0 : if (strlen(s) != (size_t)size) {
1621 : 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
1622 : 0 : return 0;
1623 : : }
1624 : 0 : *out = s;
1625 : 0 : return 1;
1626 : : }
1627 [ # # ]: 0 : if (PyTclObject_Check(in)) {
1628 : 0 : *out = Tcl_GetString(((PyTclObject *)in)->value);
1629 : 0 : return 1;
1630 : : }
1631 : 0 : PyErr_Format(PyExc_TypeError,
1632 : : "must be str, bytes or Tcl_Obj, not %.50s",
1633 : 0 : Py_TYPE(in)->tp_name);
1634 : 0 : return 0;
1635 : : }
1636 : :
1637 : :
1638 : : static void
1639 : 0 : var_perform(VarEvent *ev)
1640 : : {
1641 : 0 : *(ev->res) = ev->func(ev->self, ev->args, ev->flags);
1642 [ # # ]: 0 : if (!*(ev->res)) {
1643 : 0 : *(ev->exc) = PyErr_GetRaisedException();;
1644 : : }
1645 : :
1646 : 0 : }
1647 : :
1648 : : static int
1649 : 0 : var_proc(VarEvent* ev, int flags)
1650 : : {
1651 [ # # ]: 0 : ENTER_PYTHON
1652 : 0 : var_perform(ev);
1653 : 0 : Tcl_MutexLock(&var_mutex);
1654 : 0 : Tcl_ConditionNotify(ev->cond);
1655 : 0 : Tcl_MutexUnlock(&var_mutex);
1656 [ # # ]: 0 : LEAVE_PYTHON
1657 : 0 : return 1;
1658 : : }
1659 : :
1660 : :
1661 : : static PyObject*
1662 : 0 : var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
1663 : : {
1664 : 0 : TkappObject *self = (TkappObject*)selfptr;
1665 [ # # # # ]: 0 : if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
1666 : : VarEvent *ev;
1667 : : PyObject *res, *exc;
1668 : 0 : Tcl_Condition cond = NULL;
1669 : :
1670 : : /* The current thread is not the interpreter thread. Marshal
1671 : : the call to the interpreter thread, then wait for
1672 : : completion. */
1673 [ # # ]: 0 : if (!WaitForMainloop(self))
1674 : 0 : return NULL;
1675 : :
1676 : 0 : ev = (VarEvent*)attemptckalloc(sizeof(VarEvent));
1677 [ # # ]: 0 : if (ev == NULL) {
1678 : 0 : PyErr_NoMemory();
1679 : 0 : return NULL;
1680 : : }
1681 : 0 : ev->self = self;
1682 : 0 : ev->args = args;
1683 : 0 : ev->flags = flags;
1684 : 0 : ev->func = func;
1685 : 0 : ev->res = &res;
1686 : 0 : ev->exc = &exc;
1687 : 0 : ev->cond = &cond;
1688 : 0 : ev->ev.proc = (Tcl_EventProc*)var_proc;
1689 : 0 : Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
1690 : 0 : Tcl_ConditionFinalize(&cond);
1691 [ # # ]: 0 : if (!res) {
1692 : 0 : PyErr_SetObject((PyObject*)Py_TYPE(exc), exc);
1693 : 0 : Py_DECREF(exc);
1694 : 0 : return NULL;
1695 : : }
1696 : 0 : return res;
1697 : : }
1698 : : /* Tcl is not threaded, or this is the interpreter thread. */
1699 : 0 : return func(self, args, flags);
1700 : : }
1701 : :
1702 : : static PyObject *
1703 : 0 : SetVar(TkappObject *self, PyObject *args, int flags)
1704 : : {
1705 : : const char *name1, *name2;
1706 : : PyObject *newValue;
1707 : 0 : PyObject *res = NULL;
1708 : : Tcl_Obj *newval, *ok;
1709 : :
1710 [ # # # ]: 0 : switch (PyTuple_GET_SIZE(args)) {
1711 : 0 : case 2:
1712 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "O&O:setvar",
1713 : : varname_converter, &name1, &newValue))
1714 : 0 : return NULL;
1715 : : /* XXX Acquire tcl lock??? */
1716 : 0 : newval = AsObj(newValue);
1717 [ # # ]: 0 : if (newval == NULL)
1718 : 0 : return NULL;
1719 [ # # ]: 0 : ENTER_TCL
1720 : 0 : ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL,
1721 : : newval, flags);
1722 : 0 : ENTER_OVERLAP
1723 [ # # ]: 0 : if (!ok)
1724 : 0 : Tkinter_Error(self);
1725 : : else {
1726 : 0 : res = Py_NewRef(Py_None);
1727 : : }
1728 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1729 : 0 : break;
1730 : 0 : case 3:
1731 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "ssO:setvar",
1732 : : &name1, &name2, &newValue))
1733 : 0 : return NULL;
1734 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name1);
1735 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name2);
1736 : : /* XXX must hold tcl lock already??? */
1737 : 0 : newval = AsObj(newValue);
1738 [ # # ]: 0 : ENTER_TCL
1739 : 0 : ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags);
1740 : 0 : ENTER_OVERLAP
1741 [ # # ]: 0 : if (!ok)
1742 : 0 : Tkinter_Error(self);
1743 : : else {
1744 : 0 : res = Py_NewRef(Py_None);
1745 : : }
1746 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1747 : 0 : break;
1748 : 0 : default:
1749 : 0 : PyErr_SetString(PyExc_TypeError, "setvar requires 2 to 3 arguments");
1750 : 0 : return NULL;
1751 : : }
1752 : 0 : return res;
1753 : : }
1754 : :
1755 : : static PyObject *
1756 : 0 : Tkapp_SetVar(PyObject *self, PyObject *args)
1757 : : {
1758 : 0 : return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG);
1759 : : }
1760 : :
1761 : : static PyObject *
1762 : 0 : Tkapp_GlobalSetVar(PyObject *self, PyObject *args)
1763 : : {
1764 : 0 : return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
1765 : : }
1766 : :
1767 : :
1768 : :
1769 : : static PyObject *
1770 : 0 : GetVar(TkappObject *self, PyObject *args, int flags)
1771 : : {
1772 : 0 : const char *name1, *name2=NULL;
1773 : 0 : PyObject *res = NULL;
1774 : : Tcl_Obj *tres;
1775 : :
1776 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "O&|s:getvar",
1777 : : varname_converter, &name1, &name2))
1778 : 0 : return NULL;
1779 : :
1780 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name2);
1781 [ # # ]: 0 : ENTER_TCL
1782 : 0 : tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags);
1783 : 0 : ENTER_OVERLAP
1784 [ # # ]: 0 : if (tres == NULL) {
1785 : 0 : Tkinter_Error(self);
1786 : : } else {
1787 [ # # ]: 0 : if (self->wantobjects) {
1788 : 0 : res = FromObj(self, tres);
1789 : : }
1790 : : else {
1791 : 0 : res = unicodeFromTclObj(tres);
1792 : : }
1793 : : }
1794 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1795 : 0 : return res;
1796 : : }
1797 : :
1798 : : static PyObject *
1799 : 0 : Tkapp_GetVar(PyObject *self, PyObject *args)
1800 : : {
1801 : 0 : return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG);
1802 : : }
1803 : :
1804 : : static PyObject *
1805 : 0 : Tkapp_GlobalGetVar(PyObject *self, PyObject *args)
1806 : : {
1807 : 0 : return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
1808 : : }
1809 : :
1810 : :
1811 : :
1812 : : static PyObject *
1813 : 0 : UnsetVar(TkappObject *self, PyObject *args, int flags)
1814 : : {
1815 : 0 : char *name1, *name2=NULL;
1816 : : int code;
1817 : 0 : PyObject *res = NULL;
1818 : :
1819 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2))
1820 : 0 : return NULL;
1821 : :
1822 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name1);
1823 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name2);
1824 [ # # ]: 0 : ENTER_TCL
1825 : 0 : code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags);
1826 : 0 : ENTER_OVERLAP
1827 [ # # ]: 0 : if (code == TCL_ERROR)
1828 : 0 : res = Tkinter_Error(self);
1829 : : else {
1830 : 0 : res = Py_NewRef(Py_None);
1831 : : }
1832 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1833 : 0 : return res;
1834 : : }
1835 : :
1836 : : static PyObject *
1837 : 0 : Tkapp_UnsetVar(PyObject *self, PyObject *args)
1838 : : {
1839 : 0 : return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG);
1840 : : }
1841 : :
1842 : : static PyObject *
1843 : 0 : Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args)
1844 : : {
1845 : 0 : return var_invoke(UnsetVar, self, args,
1846 : : TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
1847 : : }
1848 : :
1849 : :
1850 : :
1851 : : /** Tcl to Python **/
1852 : :
1853 : : /*[clinic input]
1854 : : _tkinter.tkapp.getint
1855 : :
1856 : : arg: object
1857 : : /
1858 : :
1859 : : [clinic start generated code]*/
1860 : :
1861 : : static PyObject *
1862 : 0 : _tkinter_tkapp_getint(TkappObject *self, PyObject *arg)
1863 : : /*[clinic end generated code: output=88cf293fae307cfe input=034026997c5b91f8]*/
1864 : : {
1865 : : char *s;
1866 : : Tcl_Obj *value;
1867 : : PyObject *result;
1868 : :
1869 [ # # ]: 0 : if (PyLong_Check(arg)) {
1870 : 0 : return Py_NewRef(arg);
1871 : : }
1872 : :
1873 [ # # ]: 0 : if (PyTclObject_Check(arg)) {
1874 : 0 : value = ((PyTclObject*)arg)->value;
1875 : 0 : Tcl_IncrRefCount(value);
1876 : : }
1877 : : else {
1878 [ # # ]: 0 : if (!PyArg_Parse(arg, "s:getint", &s))
1879 : 0 : return NULL;
1880 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
1881 : 0 : value = Tcl_NewStringObj(s, -1);
1882 [ # # ]: 0 : if (value == NULL)
1883 : 0 : return Tkinter_Error(self);
1884 : : }
1885 : : /* Don't use Tcl_GetInt() because it returns ambiguous result for value
1886 : : in ranges -2**32..-2**31-1 and 2**31..2**32-1 (on 32-bit platform).
1887 : :
1888 : : Prefer bignum because Tcl_GetWideIntFromObj returns ambiguous result for
1889 : : value in ranges -2**64..-2**63-1 and 2**63..2**64-1 (on 32-bit platform).
1890 : : */
1891 : 0 : result = fromBignumObj(self, value);
1892 [ # # ]: 0 : Tcl_DecrRefCount(value);
1893 [ # # # # ]: 0 : if (result != NULL || PyErr_Occurred())
1894 : 0 : return result;
1895 : 0 : return Tkinter_Error(self);
1896 : : }
1897 : :
1898 : : /*[clinic input]
1899 : : _tkinter.tkapp.getdouble
1900 : :
1901 : : arg: object
1902 : : /
1903 : :
1904 : : [clinic start generated code]*/
1905 : :
1906 : : static PyObject *
1907 : 0 : _tkinter_tkapp_getdouble(TkappObject *self, PyObject *arg)
1908 : : /*[clinic end generated code: output=c52b138bd8b956b9 input=22015729ce9ef7f8]*/
1909 : : {
1910 : : char *s;
1911 : : double v;
1912 : :
1913 [ # # ]: 0 : if (PyFloat_Check(arg)) {
1914 : 0 : return Py_NewRef(arg);
1915 : : }
1916 : :
1917 [ # # ]: 0 : if (PyNumber_Check(arg)) {
1918 : 0 : return PyNumber_Float(arg);
1919 : : }
1920 : :
1921 [ # # ]: 0 : if (PyTclObject_Check(arg)) {
1922 [ # # ]: 0 : if (Tcl_GetDoubleFromObj(Tkapp_Interp(self),
1923 : : ((PyTclObject*)arg)->value,
1924 : : &v) == TCL_ERROR)
1925 : 0 : return Tkinter_Error(self);
1926 : 0 : return PyFloat_FromDouble(v);
1927 : : }
1928 : :
1929 [ # # ]: 0 : if (!PyArg_Parse(arg, "s:getdouble", &s))
1930 : 0 : return NULL;
1931 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
1932 [ # # ]: 0 : if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR)
1933 : 0 : return Tkinter_Error(self);
1934 : 0 : return PyFloat_FromDouble(v);
1935 : : }
1936 : :
1937 : : /*[clinic input]
1938 : : _tkinter.tkapp.getboolean
1939 : :
1940 : : arg: object
1941 : : /
1942 : :
1943 : : [clinic start generated code]*/
1944 : :
1945 : : static PyObject *
1946 : 0 : _tkinter_tkapp_getboolean(TkappObject *self, PyObject *arg)
1947 : : /*[clinic end generated code: output=726a9ae445821d91 input=7f11248ef8f8776e]*/
1948 : : {
1949 : : char *s;
1950 : : int v;
1951 : :
1952 [ # # ]: 0 : if (PyLong_Check(arg)) { /* int or bool */
1953 : 0 : return PyBool_FromLong(Py_SIZE(arg) != 0);
1954 : : }
1955 : :
1956 [ # # ]: 0 : if (PyTclObject_Check(arg)) {
1957 [ # # ]: 0 : if (Tcl_GetBooleanFromObj(Tkapp_Interp(self),
1958 : : ((PyTclObject*)arg)->value,
1959 : : &v) == TCL_ERROR)
1960 : 0 : return Tkinter_Error(self);
1961 : 0 : return PyBool_FromLong(v);
1962 : : }
1963 : :
1964 [ # # ]: 0 : if (!PyArg_Parse(arg, "s:getboolean", &s))
1965 : 0 : return NULL;
1966 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
1967 [ # # ]: 0 : if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR)
1968 : 0 : return Tkinter_Error(self);
1969 : 0 : return PyBool_FromLong(v);
1970 : : }
1971 : :
1972 : : /*[clinic input]
1973 : : _tkinter.tkapp.exprstring
1974 : :
1975 : : s: str
1976 : : /
1977 : :
1978 : : [clinic start generated code]*/
1979 : :
1980 : : static PyObject *
1981 : 0 : _tkinter_tkapp_exprstring_impl(TkappObject *self, const char *s)
1982 : : /*[clinic end generated code: output=beda323d3ed0abb1 input=fa78f751afb2f21b]*/
1983 : : {
1984 : 0 : PyObject *res = NULL;
1985 : : int retval;
1986 : :
1987 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
1988 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
1989 : :
1990 [ # # ]: 0 : ENTER_TCL
1991 : 0 : retval = Tcl_ExprString(Tkapp_Interp(self), s);
1992 : 0 : ENTER_OVERLAP
1993 [ # # ]: 0 : if (retval == TCL_ERROR)
1994 : 0 : res = Tkinter_Error(self);
1995 : : else
1996 : 0 : res = Tkapp_UnicodeResult(self);
1997 [ # # ]: 0 : LEAVE_OVERLAP_TCL
1998 : 0 : return res;
1999 : : }
2000 : :
2001 : : /*[clinic input]
2002 : : _tkinter.tkapp.exprlong
2003 : :
2004 : : s: str
2005 : : /
2006 : :
2007 : : [clinic start generated code]*/
2008 : :
2009 : : static PyObject *
2010 : 0 : _tkinter_tkapp_exprlong_impl(TkappObject *self, const char *s)
2011 : : /*[clinic end generated code: output=5d6a46b63c6ebcf9 input=11bd7eee0c57b4dc]*/
2012 : : {
2013 : 0 : PyObject *res = NULL;
2014 : : int retval;
2015 : : long v;
2016 : :
2017 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
2018 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2019 : :
2020 [ # # ]: 0 : ENTER_TCL
2021 : 0 : retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v);
2022 : 0 : ENTER_OVERLAP
2023 [ # # ]: 0 : if (retval == TCL_ERROR)
2024 : 0 : res = Tkinter_Error(self);
2025 : : else
2026 : 0 : res = PyLong_FromLong(v);
2027 [ # # ]: 0 : LEAVE_OVERLAP_TCL
2028 : 0 : return res;
2029 : : }
2030 : :
2031 : : /*[clinic input]
2032 : : _tkinter.tkapp.exprdouble
2033 : :
2034 : : s: str
2035 : : /
2036 : :
2037 : : [clinic start generated code]*/
2038 : :
2039 : : static PyObject *
2040 : 0 : _tkinter_tkapp_exprdouble_impl(TkappObject *self, const char *s)
2041 : : /*[clinic end generated code: output=ff78df1081ea4158 input=ff02bc11798832d5]*/
2042 : : {
2043 : 0 : PyObject *res = NULL;
2044 : : double v;
2045 : : int retval;
2046 : :
2047 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
2048 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2049 [ # # ]: 0 : ENTER_TCL
2050 : 0 : retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v);
2051 : 0 : ENTER_OVERLAP
2052 [ # # ]: 0 : if (retval == TCL_ERROR)
2053 : 0 : res = Tkinter_Error(self);
2054 : : else
2055 : 0 : res = PyFloat_FromDouble(v);
2056 [ # # ]: 0 : LEAVE_OVERLAP_TCL
2057 : 0 : return res;
2058 : : }
2059 : :
2060 : : /*[clinic input]
2061 : : _tkinter.tkapp.exprboolean
2062 : :
2063 : : s: str
2064 : : /
2065 : :
2066 : : [clinic start generated code]*/
2067 : :
2068 : : static PyObject *
2069 : 0 : _tkinter_tkapp_exprboolean_impl(TkappObject *self, const char *s)
2070 : : /*[clinic end generated code: output=8b28038c22887311 input=c8c66022bdb8d5d3]*/
2071 : : {
2072 : 0 : PyObject *res = NULL;
2073 : : int retval;
2074 : : int v;
2075 : :
2076 [ # # # # ]: 0 : CHECK_STRING_LENGTH(s);
2077 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2078 [ # # ]: 0 : ENTER_TCL
2079 : 0 : retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v);
2080 : 0 : ENTER_OVERLAP
2081 [ # # ]: 0 : if (retval == TCL_ERROR)
2082 : 0 : res = Tkinter_Error(self);
2083 : : else
2084 : 0 : res = PyLong_FromLong(v);
2085 [ # # ]: 0 : LEAVE_OVERLAP_TCL
2086 : 0 : return res;
2087 : : }
2088 : :
2089 : :
2090 : :
2091 : : /*[clinic input]
2092 : : _tkinter.tkapp.splitlist
2093 : :
2094 : : arg: object
2095 : : /
2096 : :
2097 : : [clinic start generated code]*/
2098 : :
2099 : : static PyObject *
2100 : 0 : _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
2101 : : /*[clinic end generated code: output=13b51d34386d36fb input=2b2e13351e3c0b53]*/
2102 : : {
2103 : : char *list;
2104 : : int argc;
2105 : : const char **argv;
2106 : : PyObject *v;
2107 : : int i;
2108 : :
2109 [ # # ]: 0 : if (PyTclObject_Check(arg)) {
2110 : : int objc;
2111 : : Tcl_Obj **objv;
2112 [ # # ]: 0 : if (Tcl_ListObjGetElements(Tkapp_Interp(self),
2113 : : ((PyTclObject*)arg)->value,
2114 : : &objc, &objv) == TCL_ERROR) {
2115 : 0 : return Tkinter_Error(self);
2116 : : }
2117 [ # # ]: 0 : if (!(v = PyTuple_New(objc)))
2118 : 0 : return NULL;
2119 [ # # ]: 0 : for (i = 0; i < objc; i++) {
2120 : 0 : PyObject *s = FromObj(self, objv[i]);
2121 [ # # ]: 0 : if (!s) {
2122 : 0 : Py_DECREF(v);
2123 : 0 : return NULL;
2124 : : }
2125 : 0 : PyTuple_SET_ITEM(v, i, s);
2126 : : }
2127 : 0 : return v;
2128 : : }
2129 [ # # ]: 0 : if (PyTuple_Check(arg)) {
2130 : 0 : return Py_NewRef(arg);
2131 : : }
2132 [ # # ]: 0 : if (PyList_Check(arg)) {
2133 : 0 : return PySequence_Tuple(arg);
2134 : : }
2135 : :
2136 [ # # ]: 0 : if (!PyArg_Parse(arg, "et:splitlist", "utf-8", &list))
2137 : 0 : return NULL;
2138 : :
2139 [ # # ]: 0 : if (strlen(list) >= INT_MAX) {
2140 : 0 : PyErr_SetString(PyExc_OverflowError, "string is too long");
2141 : 0 : PyMem_Free(list);
2142 : 0 : return NULL;
2143 : : }
2144 [ # # ]: 0 : if (Tcl_SplitList(Tkapp_Interp(self), list,
2145 : : &argc, &argv) == TCL_ERROR) {
2146 : 0 : PyMem_Free(list);
2147 : 0 : return Tkinter_Error(self);
2148 : : }
2149 : :
2150 [ # # ]: 0 : if (!(v = PyTuple_New(argc)))
2151 : 0 : goto finally;
2152 : :
2153 [ # # ]: 0 : for (i = 0; i < argc; i++) {
2154 : 0 : PyObject *s = unicodeFromTclString(argv[i]);
2155 [ # # ]: 0 : if (!s) {
2156 : 0 : Py_SETREF(v, NULL);
2157 : 0 : goto finally;
2158 : : }
2159 : 0 : PyTuple_SET_ITEM(v, i, s);
2160 : : }
2161 : :
2162 : 0 : finally:
2163 : 0 : ckfree(FREECAST argv);
2164 : 0 : PyMem_Free(list);
2165 : 0 : return v;
2166 : : }
2167 : :
2168 : :
2169 : : /** Tcl Command **/
2170 : :
2171 : : /* Client data struct */
2172 : : typedef struct {
2173 : : PyObject *self;
2174 : : PyObject *func;
2175 : : } PythonCmd_ClientData;
2176 : :
2177 : : static int
2178 : 0 : PythonCmd_Error(Tcl_Interp *interp)
2179 : : {
2180 : 0 : errorInCmd = 1;
2181 : 0 : excInCmd = PyErr_GetRaisedException();
2182 [ # # ]: 0 : LEAVE_PYTHON
2183 : 0 : return TCL_ERROR;
2184 : : }
2185 : :
2186 : : /* This is the Tcl command that acts as a wrapper for Python
2187 : : * function or method.
2188 : : */
2189 : : static int
2190 : 0 : PythonCmd(ClientData clientData, Tcl_Interp *interp,
2191 : : int objc, Tcl_Obj *const objv[])
2192 : : {
2193 : 0 : PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
2194 : : PyObject *args, *res;
2195 : : int i;
2196 : : Tcl_Obj *obj_res;
2197 : :
2198 [ # # ]: 0 : ENTER_PYTHON
2199 : :
2200 : : /* Create argument tuple (objv1, ..., objvN) */
2201 [ # # ]: 0 : if (!(args = PyTuple_New(objc - 1)))
2202 : 0 : return PythonCmd_Error(interp);
2203 : :
2204 [ # # ]: 0 : for (i = 0; i < (objc - 1); i++) {
2205 : 0 : PyObject *s = unicodeFromTclObj(objv[i + 1]);
2206 [ # # ]: 0 : if (!s) {
2207 : 0 : Py_DECREF(args);
2208 : 0 : return PythonCmd_Error(interp);
2209 : : }
2210 : 0 : PyTuple_SET_ITEM(args, i, s);
2211 : : }
2212 : :
2213 : 0 : res = PyObject_Call(data->func, args, NULL);
2214 : 0 : Py_DECREF(args);
2215 : :
2216 [ # # ]: 0 : if (res == NULL)
2217 : 0 : return PythonCmd_Error(interp);
2218 : :
2219 : 0 : obj_res = AsObj(res);
2220 [ # # ]: 0 : if (obj_res == NULL) {
2221 : 0 : Py_DECREF(res);
2222 : 0 : return PythonCmd_Error(interp);
2223 : : }
2224 : 0 : Tcl_SetObjResult(interp, obj_res);
2225 : 0 : Py_DECREF(res);
2226 : :
2227 [ # # ]: 0 : LEAVE_PYTHON
2228 : :
2229 : 0 : return TCL_OK;
2230 : : }
2231 : :
2232 : :
2233 : : static void
2234 : 0 : PythonCmdDelete(ClientData clientData)
2235 : : {
2236 : 0 : PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;
2237 : :
2238 [ # # ]: 0 : ENTER_PYTHON
2239 : 0 : Py_XDECREF(data->self);
2240 : 0 : Py_XDECREF(data->func);
2241 : 0 : PyMem_Free(data);
2242 [ # # ]: 0 : LEAVE_PYTHON
2243 : 0 : }
2244 : :
2245 : :
2246 : :
2247 : :
2248 : : TCL_DECLARE_MUTEX(command_mutex)
2249 : :
2250 : : typedef struct CommandEvent{
2251 : : Tcl_Event ev;
2252 : : Tcl_Interp* interp;
2253 : : const char *name;
2254 : : int create;
2255 : : int *status;
2256 : : ClientData *data;
2257 : : Tcl_Condition *done;
2258 : : } CommandEvent;
2259 : :
2260 : : static int
2261 : 0 : Tkapp_CommandProc(CommandEvent *ev, int flags)
2262 : : {
2263 [ # # ]: 0 : if (ev->create)
2264 : 0 : *ev->status = Tcl_CreateObjCommand(
2265 : : ev->interp, ev->name, PythonCmd,
2266 : 0 : ev->data, PythonCmdDelete) == NULL;
2267 : : else
2268 : 0 : *ev->status = Tcl_DeleteCommand(ev->interp, ev->name);
2269 : 0 : Tcl_MutexLock(&command_mutex);
2270 : 0 : Tcl_ConditionNotify(ev->done);
2271 : 0 : Tcl_MutexUnlock(&command_mutex);
2272 : 0 : return 1;
2273 : : }
2274 : :
2275 : : /*[clinic input]
2276 : : _tkinter.tkapp.createcommand
2277 : :
2278 : : name: str
2279 : : func: object
2280 : : /
2281 : :
2282 : : [clinic start generated code]*/
2283 : :
2284 : : static PyObject *
2285 : 0 : _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
2286 : : PyObject *func)
2287 : : /*[clinic end generated code: output=2a1c79a4ee2af410 input=255785cb70edc6a0]*/
2288 : : {
2289 : : PythonCmd_ClientData *data;
2290 : : int err;
2291 : :
2292 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name);
2293 [ # # ]: 0 : if (!PyCallable_Check(func)) {
2294 : 0 : PyErr_SetString(PyExc_TypeError, "command not callable");
2295 : 0 : return NULL;
2296 : : }
2297 : :
2298 [ # # # # : 0 : if (self->threaded && self->thread_id != Tcl_GetCurrentThread() &&
# # ]
2299 : 0 : !WaitForMainloop(self))
2300 : 0 : return NULL;
2301 : :
2302 : 0 : data = PyMem_NEW(PythonCmd_ClientData, 1);
2303 [ # # ]: 0 : if (!data)
2304 : 0 : return PyErr_NoMemory();
2305 : 0 : data->self = Py_NewRef(self);
2306 : 0 : data->func = Py_NewRef(func);
2307 [ # # # # ]: 0 : if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
2308 : 0 : Tcl_Condition cond = NULL;
2309 : 0 : CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
2310 [ # # ]: 0 : if (ev == NULL) {
2311 : 0 : PyErr_NoMemory();
2312 : 0 : PyMem_Free(data);
2313 : 0 : return NULL;
2314 : : }
2315 : 0 : ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2316 : 0 : ev->interp = self->interp;
2317 : 0 : ev->create = 1;
2318 : 0 : ev->name = name;
2319 : 0 : ev->data = (ClientData)data;
2320 : 0 : ev->status = &err;
2321 : 0 : ev->done = &cond;
2322 : 0 : Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex);
2323 : 0 : Tcl_ConditionFinalize(&cond);
2324 : : }
2325 : : else
2326 : : {
2327 [ # # ]: 0 : ENTER_TCL
2328 : 0 : err = Tcl_CreateObjCommand(
2329 : : Tkapp_Interp(self), name, PythonCmd,
2330 : 0 : (ClientData)data, PythonCmdDelete) == NULL;
2331 [ # # ]: 0 : LEAVE_TCL
2332 : : }
2333 [ # # ]: 0 : if (err) {
2334 : 0 : PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
2335 : 0 : PyMem_Free(data);
2336 : 0 : return NULL;
2337 : : }
2338 : :
2339 : 0 : Py_RETURN_NONE;
2340 : : }
2341 : :
2342 : :
2343 : :
2344 : : /*[clinic input]
2345 : : _tkinter.tkapp.deletecommand
2346 : :
2347 : : name: str
2348 : : /
2349 : :
2350 : : [clinic start generated code]*/
2351 : :
2352 : : static PyObject *
2353 : 0 : _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
2354 : : /*[clinic end generated code: output=a67e8cb5845e0d2d input=53e9952eae1f85f5]*/
2355 : : {
2356 : : int err;
2357 : :
2358 [ # # # # ]: 0 : CHECK_STRING_LENGTH(name);
2359 : :
2360 [ # # # # ]: 0 : if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
2361 : 0 : Tcl_Condition cond = NULL;
2362 : : CommandEvent *ev;
2363 : 0 : ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
2364 [ # # ]: 0 : if (ev == NULL) {
2365 : 0 : PyErr_NoMemory();
2366 : 0 : return NULL;
2367 : : }
2368 : 0 : ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2369 : 0 : ev->interp = self->interp;
2370 : 0 : ev->create = 0;
2371 : 0 : ev->name = name;
2372 : 0 : ev->status = &err;
2373 : 0 : ev->done = &cond;
2374 : 0 : Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond,
2375 : : &command_mutex);
2376 : 0 : Tcl_ConditionFinalize(&cond);
2377 : : }
2378 : : else
2379 : : {
2380 [ # # ]: 0 : ENTER_TCL
2381 : 0 : err = Tcl_DeleteCommand(self->interp, name);
2382 [ # # ]: 0 : LEAVE_TCL
2383 : : }
2384 [ # # ]: 0 : if (err == -1) {
2385 : 0 : PyErr_SetString(Tkinter_TclError, "can't delete Tcl command");
2386 : 0 : return NULL;
2387 : : }
2388 : 0 : Py_RETURN_NONE;
2389 : : }
2390 : :
2391 : :
2392 : :
2393 : : #ifdef HAVE_CREATEFILEHANDLER
2394 : : /** File Handler **/
2395 : :
2396 : : typedef struct _fhcdata {
2397 : : PyObject *func;
2398 : : PyObject *file;
2399 : : int id;
2400 : : struct _fhcdata *next;
2401 : : } FileHandler_ClientData;
2402 : :
2403 : : static FileHandler_ClientData *HeadFHCD;
2404 : :
2405 : : static FileHandler_ClientData *
2406 : 0 : NewFHCD(PyObject *func, PyObject *file, int id)
2407 : : {
2408 : : FileHandler_ClientData *p;
2409 : 0 : p = PyMem_NEW(FileHandler_ClientData, 1);
2410 [ # # ]: 0 : if (p != NULL) {
2411 : 0 : p->func = Py_XNewRef(func);
2412 : 0 : p->file = Py_XNewRef(file);
2413 : 0 : p->id = id;
2414 : 0 : p->next = HeadFHCD;
2415 : 0 : HeadFHCD = p;
2416 : : }
2417 : 0 : return p;
2418 : : }
2419 : :
2420 : : static void
2421 : 0 : DeleteFHCD(int id)
2422 : : {
2423 : : FileHandler_ClientData *p, **pp;
2424 : :
2425 : 0 : pp = &HeadFHCD;
2426 [ # # ]: 0 : while ((p = *pp) != NULL) {
2427 [ # # ]: 0 : if (p->id == id) {
2428 : 0 : *pp = p->next;
2429 : 0 : Py_XDECREF(p->func);
2430 : 0 : Py_XDECREF(p->file);
2431 : 0 : PyMem_Free(p);
2432 : : }
2433 : : else
2434 : 0 : pp = &p->next;
2435 : : }
2436 : 0 : }
2437 : :
2438 : : static void
2439 : 0 : FileHandler(ClientData clientData, int mask)
2440 : : {
2441 : 0 : FileHandler_ClientData *data = (FileHandler_ClientData *)clientData;
2442 : : PyObject *func, *file, *res;
2443 : :
2444 [ # # ]: 0 : ENTER_PYTHON
2445 : 0 : func = data->func;
2446 : 0 : file = data->file;
2447 : :
2448 : 0 : res = PyObject_CallFunction(func, "Oi", file, mask);
2449 [ # # ]: 0 : if (res == NULL) {
2450 : 0 : errorInCmd = 1;
2451 : 0 : excInCmd = PyErr_GetRaisedException();
2452 : : }
2453 : 0 : Py_XDECREF(res);
2454 [ # # ]: 0 : LEAVE_PYTHON
2455 : 0 : }
2456 : :
2457 : : /*[clinic input]
2458 : : _tkinter.tkapp.createfilehandler
2459 : :
2460 : : file: object
2461 : : mask: int
2462 : : func: object
2463 : : /
2464 : :
2465 : : [clinic start generated code]*/
2466 : :
2467 : : static PyObject *
2468 : 0 : _tkinter_tkapp_createfilehandler_impl(TkappObject *self, PyObject *file,
2469 : : int mask, PyObject *func)
2470 : : /*[clinic end generated code: output=f73ce82de801c353 input=84943a5286e47947]*/
2471 : : {
2472 : : FileHandler_ClientData *data;
2473 : : int tfile;
2474 : :
2475 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2476 : :
2477 : 0 : tfile = PyObject_AsFileDescriptor(file);
2478 [ # # ]: 0 : if (tfile < 0)
2479 : 0 : return NULL;
2480 [ # # ]: 0 : if (!PyCallable_Check(func)) {
2481 : 0 : PyErr_SetString(PyExc_TypeError, "bad argument list");
2482 : 0 : return NULL;
2483 : : }
2484 : :
2485 : 0 : data = NewFHCD(func, file, tfile);
2486 [ # # ]: 0 : if (data == NULL)
2487 : 0 : return NULL;
2488 : :
2489 : : /* Ought to check for null Tcl_File object... */
2490 [ # # ]: 0 : ENTER_TCL
2491 : 0 : Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data);
2492 [ # # ]: 0 : LEAVE_TCL
2493 : 0 : Py_RETURN_NONE;
2494 : : }
2495 : :
2496 : : /*[clinic input]
2497 : : _tkinter.tkapp.deletefilehandler
2498 : :
2499 : : file: object
2500 : : /
2501 : :
2502 : : [clinic start generated code]*/
2503 : :
2504 : : static PyObject *
2505 : 0 : _tkinter_tkapp_deletefilehandler(TkappObject *self, PyObject *file)
2506 : : /*[clinic end generated code: output=b53cc96ebf9476fd input=abbec19d66312e2a]*/
2507 : : {
2508 : : int tfile;
2509 : :
2510 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2511 : :
2512 : 0 : tfile = PyObject_AsFileDescriptor(file);
2513 [ # # ]: 0 : if (tfile < 0)
2514 : 0 : return NULL;
2515 : :
2516 : 0 : DeleteFHCD(tfile);
2517 : :
2518 : : /* Ought to check for null Tcl_File object... */
2519 [ # # ]: 0 : ENTER_TCL
2520 : 0 : Tcl_DeleteFileHandler(tfile);
2521 [ # # ]: 0 : LEAVE_TCL
2522 : 0 : Py_RETURN_NONE;
2523 : : }
2524 : : #endif /* HAVE_CREATEFILEHANDLER */
2525 : :
2526 : :
2527 : : /**** Tktt Object (timer token) ****/
2528 : :
2529 : : static PyObject *Tktt_Type;
2530 : :
2531 : : typedef struct {
2532 : : PyObject_HEAD
2533 : : Tcl_TimerToken token;
2534 : : PyObject *func;
2535 : : } TkttObject;
2536 : :
2537 : : /*[clinic input]
2538 : : _tkinter.tktimertoken.deletetimerhandler
2539 : :
2540 : : [clinic start generated code]*/
2541 : :
2542 : : static PyObject *
2543 : 0 : _tkinter_tktimertoken_deletetimerhandler_impl(TkttObject *self)
2544 : : /*[clinic end generated code: output=bd7fe17f328cfa55 input=40bd070ff85f5cf3]*/
2545 : : {
2546 : 0 : TkttObject *v = self;
2547 : 0 : PyObject *func = v->func;
2548 : :
2549 [ # # ]: 0 : if (v->token != NULL) {
2550 : 0 : Tcl_DeleteTimerHandler(v->token);
2551 : 0 : v->token = NULL;
2552 : : }
2553 [ # # ]: 0 : if (func != NULL) {
2554 : 0 : v->func = NULL;
2555 : 0 : Py_DECREF(func);
2556 : 0 : Py_DECREF(v); /* See Tktt_New() */
2557 : : }
2558 : 0 : Py_RETURN_NONE;
2559 : : }
2560 : :
2561 : : static TkttObject *
2562 : 0 : Tktt_New(PyObject *func)
2563 : : {
2564 : : TkttObject *v;
2565 : :
2566 : 0 : v = PyObject_New(TkttObject, (PyTypeObject *) Tktt_Type);
2567 [ # # ]: 0 : if (v == NULL)
2568 : 0 : return NULL;
2569 : :
2570 : 0 : v->token = NULL;
2571 : 0 : v->func = Py_NewRef(func);
2572 : :
2573 : : /* Extra reference, deleted when called or when handler is deleted */
2574 : 0 : return (TkttObject*)Py_NewRef(v);
2575 : : }
2576 : :
2577 : : static void
2578 : 0 : Tktt_Dealloc(PyObject *self)
2579 : : {
2580 : 0 : TkttObject *v = (TkttObject *)self;
2581 : 0 : PyObject *func = v->func;
2582 : 0 : PyObject *tp = (PyObject *) Py_TYPE(self);
2583 : :
2584 : 0 : Py_XDECREF(func);
2585 : :
2586 : 0 : PyObject_Free(self);
2587 : 0 : Py_DECREF(tp);
2588 : 0 : }
2589 : :
2590 : : static PyObject *
2591 : 0 : Tktt_Repr(PyObject *self)
2592 : : {
2593 : 0 : TkttObject *v = (TkttObject *)self;
2594 : 0 : return PyUnicode_FromFormat("<tktimertoken at %p%s>",
2595 : : v,
2596 [ # # ]: 0 : v->func == NULL ? ", handler deleted" : "");
2597 : : }
2598 : :
2599 : : /** Timer Handler **/
2600 : :
2601 : : static void
2602 : 0 : TimerHandler(ClientData clientData)
2603 : : {
2604 : 0 : TkttObject *v = (TkttObject *)clientData;
2605 : 0 : PyObject *func = v->func;
2606 : : PyObject *res;
2607 : :
2608 [ # # ]: 0 : if (func == NULL)
2609 : 0 : return;
2610 : :
2611 : 0 : v->func = NULL;
2612 : :
2613 [ # # ]: 0 : ENTER_PYTHON
2614 : :
2615 : 0 : res = PyObject_CallNoArgs(func);
2616 : 0 : Py_DECREF(func);
2617 : 0 : Py_DECREF(v); /* See Tktt_New() */
2618 : :
2619 [ # # ]: 0 : if (res == NULL) {
2620 : 0 : errorInCmd = 1;
2621 : 0 : excInCmd = PyErr_GetRaisedException();
2622 : : }
2623 : : else
2624 : 0 : Py_DECREF(res);
2625 : :
2626 [ # # ]: 0 : LEAVE_PYTHON
2627 : : }
2628 : :
2629 : : /*[clinic input]
2630 : : _tkinter.tkapp.createtimerhandler
2631 : :
2632 : : milliseconds: int
2633 : : func: object
2634 : : /
2635 : :
2636 : : [clinic start generated code]*/
2637 : :
2638 : : static PyObject *
2639 : 0 : _tkinter_tkapp_createtimerhandler_impl(TkappObject *self, int milliseconds,
2640 : : PyObject *func)
2641 : : /*[clinic end generated code: output=2da5959b9d031911 input=ba6729f32f0277a5]*/
2642 : : {
2643 : : TkttObject *v;
2644 : :
2645 [ # # ]: 0 : if (!PyCallable_Check(func)) {
2646 : 0 : PyErr_SetString(PyExc_TypeError, "bad argument list");
2647 : 0 : return NULL;
2648 : : }
2649 : :
2650 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2651 : :
2652 : 0 : v = Tktt_New(func);
2653 [ # # ]: 0 : if (v) {
2654 : 0 : v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler,
2655 : : (ClientData)v);
2656 : : }
2657 : :
2658 : 0 : return (PyObject *) v;
2659 : : }
2660 : :
2661 : :
2662 : : /** Event Loop **/
2663 : :
2664 : : /*[clinic input]
2665 : : _tkinter.tkapp.mainloop
2666 : :
2667 : : threshold: int = 0
2668 : : /
2669 : :
2670 : : [clinic start generated code]*/
2671 : :
2672 : : static PyObject *
2673 : 0 : _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
2674 : : /*[clinic end generated code: output=0ba8eabbe57841b0 input=036bcdcf03d5eca0]*/
2675 : : {
2676 : 0 : PyThreadState *tstate = PyThreadState_Get();
2677 : :
2678 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2679 : 0 : self->dispatching = 1;
2680 : :
2681 : 0 : quitMainLoop = 0;
2682 [ # # ]: 0 : while (Tk_GetNumMainWindows() > threshold &&
2683 [ # # ]: 0 : !quitMainLoop &&
2684 [ # # ]: 0 : !errorInCmd)
2685 : : {
2686 : : int result;
2687 : :
2688 [ # # ]: 0 : if (self->threaded) {
2689 : : /* Allow other Python threads to run. */
2690 [ # # ]: 0 : ENTER_TCL
2691 : 0 : result = Tcl_DoOneEvent(0);
2692 [ # # ]: 0 : LEAVE_TCL
2693 : : }
2694 : : else {
2695 : 0 : Py_BEGIN_ALLOW_THREADS
2696 [ # # ]: 0 : if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
2697 : 0 : tcl_tstate = tstate;
2698 : 0 : result = Tcl_DoOneEvent(TCL_DONT_WAIT);
2699 : 0 : tcl_tstate = NULL;
2700 [ # # ]: 0 : if(tcl_lock)PyThread_release_lock(tcl_lock);
2701 [ # # ]: 0 : if (result == 0)
2702 : 0 : Sleep(Tkinter_busywaitinterval);
2703 : 0 : Py_END_ALLOW_THREADS
2704 : : }
2705 : :
2706 [ # # ]: 0 : if (PyErr_CheckSignals() != 0) {
2707 : 0 : self->dispatching = 0;
2708 : 0 : return NULL;
2709 : : }
2710 [ # # ]: 0 : if (result < 0)
2711 : 0 : break;
2712 : : }
2713 : 0 : self->dispatching = 0;
2714 : 0 : quitMainLoop = 0;
2715 : :
2716 [ # # ]: 0 : if (errorInCmd) {
2717 : 0 : errorInCmd = 0;
2718 : 0 : PyErr_SetRaisedException(excInCmd);
2719 : 0 : excInCmd = NULL;
2720 : 0 : return NULL;
2721 : : }
2722 : 0 : Py_RETURN_NONE;
2723 : : }
2724 : :
2725 : : /*[clinic input]
2726 : : _tkinter.tkapp.dooneevent
2727 : :
2728 : : flags: int = 0
2729 : : /
2730 : :
2731 : : [clinic start generated code]*/
2732 : :
2733 : : static PyObject *
2734 : 0 : _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags)
2735 : : /*[clinic end generated code: output=27c6b2aa464cac29 input=6542b928e364b793]*/
2736 : : {
2737 : : int rv;
2738 : :
2739 [ # # ]: 0 : ENTER_TCL
2740 : 0 : rv = Tcl_DoOneEvent(flags);
2741 [ # # ]: 0 : LEAVE_TCL
2742 : 0 : return PyLong_FromLong(rv);
2743 : : }
2744 : :
2745 : : /*[clinic input]
2746 : : _tkinter.tkapp.quit
2747 : : [clinic start generated code]*/
2748 : :
2749 : : static PyObject *
2750 : 0 : _tkinter_tkapp_quit_impl(TkappObject *self)
2751 : : /*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/
2752 : : {
2753 : 0 : quitMainLoop = 1;
2754 : 0 : Py_RETURN_NONE;
2755 : : }
2756 : :
2757 : : /*[clinic input]
2758 : : _tkinter.tkapp.interpaddr
2759 : : [clinic start generated code]*/
2760 : :
2761 : : static PyObject *
2762 : 0 : _tkinter_tkapp_interpaddr_impl(TkappObject *self)
2763 : : /*[clinic end generated code: output=6caaae3273b3c95a input=2dd32cbddb55a111]*/
2764 : : {
2765 : 0 : return PyLong_FromVoidPtr(Tkapp_Interp(self));
2766 : : }
2767 : :
2768 : : /*[clinic input]
2769 : : _tkinter.tkapp.loadtk
2770 : : [clinic start generated code]*/
2771 : :
2772 : : static PyObject *
2773 : 0 : _tkinter_tkapp_loadtk_impl(TkappObject *self)
2774 : : /*[clinic end generated code: output=e9e10a954ce46d2a input=b5e82afedd6354f0]*/
2775 : : {
2776 : 0 : Tcl_Interp *interp = Tkapp_Interp(self);
2777 : 0 : const char * _tk_exists = NULL;
2778 : : int err;
2779 : :
2780 : : #ifdef TKINTER_PROTECT_LOADTK
2781 : : /* Up to Tk 8.4.13, Tk_Init deadlocks on the second call when the
2782 : : * first call failed.
2783 : : * To avoid the deadlock, we just refuse the second call through
2784 : : * a static variable.
2785 : : */
2786 : : if (tk_load_failed) {
2787 : : PyErr_SetString(Tkinter_TclError, TKINTER_LOADTK_ERRMSG);
2788 : : return NULL;
2789 : : }
2790 : : #endif
2791 : :
2792 : : /* We want to guard against calling Tk_Init() multiple times */
2793 [ # # # # ]: 0 : CHECK_TCL_APPARTMENT;
2794 [ # # ]: 0 : ENTER_TCL
2795 : 0 : err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version");
2796 : 0 : ENTER_OVERLAP
2797 [ # # ]: 0 : if (err == TCL_ERROR) {
2798 : : /* This sets an exception, but we cannot return right
2799 : : away because we need to exit the overlap first. */
2800 : 0 : Tkinter_Error(self);
2801 : : } else {
2802 : 0 : _tk_exists = Tcl_GetStringResult(Tkapp_Interp(self));
2803 : : }
2804 [ # # ]: 0 : LEAVE_OVERLAP_TCL
2805 [ # # ]: 0 : if (err == TCL_ERROR) {
2806 : 0 : return NULL;
2807 : : }
2808 [ # # # # ]: 0 : if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) {
2809 [ # # ]: 0 : if (Tk_Init(interp) == TCL_ERROR) {
2810 : 0 : Tkinter_Error(self);
2811 : : #ifdef TKINTER_PROTECT_LOADTK
2812 : : tk_load_failed = 1;
2813 : : #endif
2814 : 0 : return NULL;
2815 : : }
2816 : : }
2817 : 0 : Py_RETURN_NONE;
2818 : : }
2819 : :
2820 : : static PyObject *
2821 : 0 : Tkapp_WantObjects(PyObject *self, PyObject *args)
2822 : : {
2823 : :
2824 : 0 : int wantobjects = -1;
2825 [ # # ]: 0 : if (!PyArg_ParseTuple(args, "|p:wantobjects", &wantobjects))
2826 : 0 : return NULL;
2827 [ # # ]: 0 : if (wantobjects == -1)
2828 : 0 : return PyBool_FromLong(((TkappObject*)self)->wantobjects);
2829 : 0 : ((TkappObject*)self)->wantobjects = wantobjects;
2830 : :
2831 : 0 : Py_RETURN_NONE;
2832 : : }
2833 : :
2834 : : /*[clinic input]
2835 : : _tkinter.tkapp.willdispatch
2836 : :
2837 : : [clinic start generated code]*/
2838 : :
2839 : : static PyObject *
2840 : 0 : _tkinter_tkapp_willdispatch_impl(TkappObject *self)
2841 : : /*[clinic end generated code: output=0e3f46d244642155 input=d88f5970843d6dab]*/
2842 : : {
2843 : 0 : self->dispatching = 1;
2844 : :
2845 : 0 : Py_RETURN_NONE;
2846 : : }
2847 : :
2848 : :
2849 : : /**** Tkapp Type Methods ****/
2850 : :
2851 : : static void
2852 : 0 : Tkapp_Dealloc(PyObject *self)
2853 : : {
2854 : 0 : PyObject *tp = (PyObject *) Py_TYPE(self);
2855 : : /*CHECK_TCL_APPARTMENT;*/
2856 [ # # ]: 0 : ENTER_TCL
2857 : 0 : Tcl_DeleteInterp(Tkapp_Interp(self));
2858 [ # # ]: 0 : LEAVE_TCL
2859 : 0 : PyObject_Free(self);
2860 : 0 : Py_DECREF(tp);
2861 : 0 : DisableEventHook();
2862 : 0 : }
2863 : :
2864 : :
2865 : :
2866 : : /**** Tkinter Module ****/
2867 : :
2868 : : typedef struct {
2869 : : PyObject* tuple;
2870 : : Py_ssize_t size; /* current size */
2871 : : Py_ssize_t maxsize; /* allocated size */
2872 : : } FlattenContext;
2873 : :
2874 : : static int
2875 : 0 : _bump(FlattenContext* context, Py_ssize_t size)
2876 : : {
2877 : : /* expand tuple to hold (at least) size new items.
2878 : : return true if successful, false if an exception was raised */
2879 : :
2880 : 0 : Py_ssize_t maxsize = context->maxsize * 2; /* never overflows */
2881 : :
2882 [ # # ]: 0 : if (maxsize < context->size + size)
2883 : 0 : maxsize = context->size + size; /* never overflows */
2884 : :
2885 : 0 : context->maxsize = maxsize;
2886 : :
2887 : 0 : return _PyTuple_Resize(&context->tuple, maxsize) >= 0;
2888 : : }
2889 : :
2890 : : static int
2891 : 0 : _flatten1(FlattenContext* context, PyObject* item, int depth)
2892 : : {
2893 : : /* add tuple or list to argument tuple (recursively) */
2894 : :
2895 : : Py_ssize_t i, size;
2896 : :
2897 [ # # ]: 0 : if (depth > 1000) {
2898 : 0 : PyErr_SetString(PyExc_ValueError,
2899 : : "nesting too deep in _flatten");
2900 : 0 : return 0;
2901 [ # # # # ]: 0 : } else if (PyTuple_Check(item) || PyList_Check(item)) {
2902 [ # # ]: 0 : size = PySequence_Fast_GET_SIZE(item);
2903 : : /* preallocate (assume no nesting) */
2904 [ # # # # ]: 0 : if (context->size + size > context->maxsize &&
2905 : 0 : !_bump(context, size))
2906 : 0 : return 0;
2907 : : /* copy items to output tuple */
2908 [ # # ]: 0 : for (i = 0; i < size; i++) {
2909 [ # # ]: 0 : PyObject *o = PySequence_Fast_GET_ITEM(item, i);
2910 [ # # # # ]: 0 : if (PyList_Check(o) || PyTuple_Check(o)) {
2911 [ # # ]: 0 : if (!_flatten1(context, o, depth + 1))
2912 : 0 : return 0;
2913 [ # # ]: 0 : } else if (o != Py_None) {
2914 [ # # # # ]: 0 : if (context->size + 1 > context->maxsize &&
2915 : 0 : !_bump(context, 1))
2916 : 0 : return 0;
2917 : 0 : PyTuple_SET_ITEM(context->tuple,
2918 : : context->size++, Py_NewRef(o));
2919 : : }
2920 : : }
2921 : : } else {
2922 : 0 : PyErr_SetString(PyExc_TypeError, "argument must be sequence");
2923 : 0 : return 0;
2924 : : }
2925 : 0 : return 1;
2926 : : }
2927 : :
2928 : : /*[clinic input]
2929 : : _tkinter._flatten
2930 : :
2931 : : item: object
2932 : : /
2933 : :
2934 : : [clinic start generated code]*/
2935 : :
2936 : : static PyObject *
2937 : 0 : _tkinter__flatten(PyObject *module, PyObject *item)
2938 : : /*[clinic end generated code: output=cad02a3f97f29862 input=6b9c12260aa1157f]*/
2939 : : {
2940 : : FlattenContext context;
2941 : :
2942 : 0 : context.maxsize = PySequence_Size(item);
2943 [ # # ]: 0 : if (context.maxsize < 0)
2944 : 0 : return NULL;
2945 [ # # ]: 0 : if (context.maxsize == 0)
2946 : 0 : return PyTuple_New(0);
2947 : :
2948 : 0 : context.tuple = PyTuple_New(context.maxsize);
2949 [ # # ]: 0 : if (!context.tuple)
2950 : 0 : return NULL;
2951 : :
2952 : 0 : context.size = 0;
2953 : :
2954 [ # # ]: 0 : if (!_flatten1(&context, item, 0)) {
2955 : 0 : Py_XDECREF(context.tuple);
2956 : 0 : return NULL;
2957 : : }
2958 : :
2959 [ # # ]: 0 : if (_PyTuple_Resize(&context.tuple, context.size))
2960 : 0 : return NULL;
2961 : :
2962 : 0 : return context.tuple;
2963 : : }
2964 : :
2965 : : /*[clinic input]
2966 : : _tkinter.create
2967 : :
2968 : : screenName: str(accept={str, NoneType}) = None
2969 : : baseName: str = ""
2970 : : className: str = "Tk"
2971 : : interactive: bool = False
2972 : : wantobjects: bool = False
2973 : : wantTk: bool = True
2974 : : if false, then Tk_Init() doesn't get called
2975 : : sync: bool = False
2976 : : if true, then pass -sync to wish
2977 : : use: str(accept={str, NoneType}) = None
2978 : : if not None, then pass -use to wish
2979 : : /
2980 : :
2981 : : [clinic start generated code]*/
2982 : :
2983 : : static PyObject *
2984 : 0 : _tkinter_create_impl(PyObject *module, const char *screenName,
2985 : : const char *baseName, const char *className,
2986 : : int interactive, int wantobjects, int wantTk, int sync,
2987 : : const char *use)
2988 : : /*[clinic end generated code: output=e3315607648e6bb4 input=09afef9adea70a19]*/
2989 : : {
2990 : : /* XXX baseName is not used anymore;
2991 : : * try getting rid of it. */
2992 [ # # # # ]: 0 : CHECK_STRING_LENGTH(screenName);
2993 [ # # # # ]: 0 : CHECK_STRING_LENGTH(baseName);
2994 [ # # # # ]: 0 : CHECK_STRING_LENGTH(className);
2995 [ # # # # ]: 0 : CHECK_STRING_LENGTH(use);
2996 : :
2997 : 0 : return (PyObject *) Tkapp_New(screenName, className,
2998 : : interactive, wantobjects, wantTk,
2999 : : sync, use);
3000 : : }
3001 : :
3002 : : /*[clinic input]
3003 : : _tkinter.setbusywaitinterval
3004 : :
3005 : : new_val: int
3006 : : /
3007 : :
3008 : : Set the busy-wait interval in milliseconds between successive calls to Tcl_DoOneEvent in a threaded Python interpreter.
3009 : :
3010 : : It should be set to a divisor of the maximum time between frames in an animation.
3011 : : [clinic start generated code]*/
3012 : :
3013 : : static PyObject *
3014 : 0 : _tkinter_setbusywaitinterval_impl(PyObject *module, int new_val)
3015 : : /*[clinic end generated code: output=42bf7757dc2d0ab6 input=deca1d6f9e6dae47]*/
3016 : : {
3017 [ # # ]: 0 : if (new_val < 0) {
3018 : 0 : PyErr_SetString(PyExc_ValueError,
3019 : : "busywaitinterval must be >= 0");
3020 : 0 : return NULL;
3021 : : }
3022 : 0 : Tkinter_busywaitinterval = new_val;
3023 : 0 : Py_RETURN_NONE;
3024 : : }
3025 : :
3026 : : /*[clinic input]
3027 : : _tkinter.getbusywaitinterval -> int
3028 : :
3029 : : Return the current busy-wait interval between successive calls to Tcl_DoOneEvent in a threaded Python interpreter.
3030 : : [clinic start generated code]*/
3031 : :
3032 : : static int
3033 : 0 : _tkinter_getbusywaitinterval_impl(PyObject *module)
3034 : : /*[clinic end generated code: output=23b72d552001f5c7 input=a695878d2d576a84]*/
3035 : : {
3036 : 0 : return Tkinter_busywaitinterval;
3037 : : }
3038 : :
3039 : : #include "clinic/_tkinter.c.h"
3040 : :
3041 : : static PyMethodDef Tktt_methods[] =
3042 : : {
3043 : : _TKINTER_TKTIMERTOKEN_DELETETIMERHANDLER_METHODDEF
3044 : : {NULL, NULL}
3045 : : };
3046 : :
3047 : : static PyType_Slot Tktt_Type_slots[] = {
3048 : : {Py_tp_dealloc, Tktt_Dealloc},
3049 : : {Py_tp_repr, Tktt_Repr},
3050 : : {Py_tp_methods, Tktt_methods},
3051 : : {0, 0}
3052 : : };
3053 : :
3054 : : static PyType_Spec Tktt_Type_spec = {
3055 : : "_tkinter.tktimertoken",
3056 : : sizeof(TkttObject),
3057 : : 0,
3058 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
3059 : : Tktt_Type_slots,
3060 : : };
3061 : :
3062 : :
3063 : : /**** Tkapp Method List ****/
3064 : :
3065 : : static PyMethodDef Tkapp_methods[] =
3066 : : {
3067 : : _TKINTER_TKAPP_WILLDISPATCH_METHODDEF
3068 : : {"wantobjects", Tkapp_WantObjects, METH_VARARGS},
3069 : : {"call", Tkapp_Call, METH_VARARGS},
3070 : : _TKINTER_TKAPP_EVAL_METHODDEF
3071 : : _TKINTER_TKAPP_EVALFILE_METHODDEF
3072 : : _TKINTER_TKAPP_RECORD_METHODDEF
3073 : : _TKINTER_TKAPP_ADDERRORINFO_METHODDEF
3074 : : {"setvar", Tkapp_SetVar, METH_VARARGS},
3075 : : {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS},
3076 : : {"getvar", Tkapp_GetVar, METH_VARARGS},
3077 : : {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS},
3078 : : {"unsetvar", Tkapp_UnsetVar, METH_VARARGS},
3079 : : {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS},
3080 : : _TKINTER_TKAPP_GETINT_METHODDEF
3081 : : _TKINTER_TKAPP_GETDOUBLE_METHODDEF
3082 : : _TKINTER_TKAPP_GETBOOLEAN_METHODDEF
3083 : : _TKINTER_TKAPP_EXPRSTRING_METHODDEF
3084 : : _TKINTER_TKAPP_EXPRLONG_METHODDEF
3085 : : _TKINTER_TKAPP_EXPRDOUBLE_METHODDEF
3086 : : _TKINTER_TKAPP_EXPRBOOLEAN_METHODDEF
3087 : : _TKINTER_TKAPP_SPLITLIST_METHODDEF
3088 : : _TKINTER_TKAPP_CREATECOMMAND_METHODDEF
3089 : : _TKINTER_TKAPP_DELETECOMMAND_METHODDEF
3090 : : _TKINTER_TKAPP_CREATEFILEHANDLER_METHODDEF
3091 : : _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF
3092 : : _TKINTER_TKAPP_CREATETIMERHANDLER_METHODDEF
3093 : : _TKINTER_TKAPP_MAINLOOP_METHODDEF
3094 : : _TKINTER_TKAPP_DOONEEVENT_METHODDEF
3095 : : _TKINTER_TKAPP_QUIT_METHODDEF
3096 : : _TKINTER_TKAPP_INTERPADDR_METHODDEF
3097 : : _TKINTER_TKAPP_LOADTK_METHODDEF
3098 : : {NULL, NULL}
3099 : : };
3100 : :
3101 : : static PyType_Slot Tkapp_Type_slots[] = {
3102 : : {Py_tp_dealloc, Tkapp_Dealloc},
3103 : : {Py_tp_methods, Tkapp_methods},
3104 : : {0, 0}
3105 : : };
3106 : :
3107 : :
3108 : : static PyType_Spec Tkapp_Type_spec = {
3109 : : "_tkinter.tkapp",
3110 : : sizeof(TkappObject),
3111 : : 0,
3112 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
3113 : : Tkapp_Type_slots,
3114 : : };
3115 : :
3116 : : static PyMethodDef moduleMethods[] =
3117 : : {
3118 : : _TKINTER__FLATTEN_METHODDEF
3119 : : _TKINTER_CREATE_METHODDEF
3120 : : _TKINTER_SETBUSYWAITINTERVAL_METHODDEF
3121 : : _TKINTER_GETBUSYWAITINTERVAL_METHODDEF
3122 : : {NULL, NULL}
3123 : : };
3124 : :
3125 : : #ifdef WAIT_FOR_STDIN
3126 : :
3127 : : static int stdin_ready = 0;
3128 : :
3129 : : #ifndef MS_WINDOWS
3130 : : static void
3131 : 0 : MyFileProc(void *clientData, int mask)
3132 : : {
3133 : 0 : stdin_ready = 1;
3134 : 0 : }
3135 : : #endif
3136 : :
3137 : : static PyThreadState *event_tstate = NULL;
3138 : :
3139 : : static int
3140 : 0 : EventHook(void)
3141 : : {
3142 : : #ifndef MS_WINDOWS
3143 : : int tfile;
3144 : : #endif
3145 : 0 : PyEval_RestoreThread(event_tstate);
3146 : 0 : stdin_ready = 0;
3147 : 0 : errorInCmd = 0;
3148 : : #ifndef MS_WINDOWS
3149 : 0 : tfile = fileno(stdin);
3150 : 0 : Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL);
3151 : : #endif
3152 [ # # # # ]: 0 : while (!errorInCmd && !stdin_ready) {
3153 : : int result;
3154 : : #ifdef MS_WINDOWS
3155 : : if (_kbhit()) {
3156 : : stdin_ready = 1;
3157 : : break;
3158 : : }
3159 : : #endif
3160 : 0 : Py_BEGIN_ALLOW_THREADS
3161 [ # # ]: 0 : if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3162 : 0 : tcl_tstate = event_tstate;
3163 : :
3164 : 0 : result = Tcl_DoOneEvent(TCL_DONT_WAIT);
3165 : :
3166 : 0 : tcl_tstate = NULL;
3167 [ # # ]: 0 : if(tcl_lock)PyThread_release_lock(tcl_lock);
3168 [ # # ]: 0 : if (result == 0)
3169 : 0 : Sleep(Tkinter_busywaitinterval);
3170 : 0 : Py_END_ALLOW_THREADS
3171 : :
3172 [ # # ]: 0 : if (result < 0)
3173 : 0 : break;
3174 : : }
3175 : : #ifndef MS_WINDOWS
3176 : 0 : Tcl_DeleteFileHandler(tfile);
3177 : : #endif
3178 [ # # ]: 0 : if (errorInCmd) {
3179 : 0 : errorInCmd = 0;
3180 : 0 : PyErr_SetRaisedException(excInCmd);
3181 : 0 : excInCmd = NULL;
3182 : 0 : PyErr_Print();
3183 : : }
3184 : 0 : PyEval_SaveThread();
3185 : 0 : return 0;
3186 : : }
3187 : :
3188 : : #endif
3189 : :
3190 : : static void
3191 : 0 : EnableEventHook(void)
3192 : : {
3193 : : #ifdef WAIT_FOR_STDIN
3194 [ # # ]: 0 : if (PyOS_InputHook == NULL) {
3195 : 0 : event_tstate = PyThreadState_Get();
3196 : 0 : PyOS_InputHook = EventHook;
3197 : : }
3198 : : #endif
3199 : 0 : }
3200 : :
3201 : : static void
3202 : 0 : DisableEventHook(void)
3203 : : {
3204 : : #ifdef WAIT_FOR_STDIN
3205 [ # # # # ]: 0 : if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
3206 : 0 : PyOS_InputHook = NULL;
3207 : : }
3208 : : #endif
3209 : 0 : }
3210 : :
3211 : :
3212 : : static struct PyModuleDef _tkintermodule = {
3213 : : PyModuleDef_HEAD_INIT,
3214 : : "_tkinter",
3215 : : NULL,
3216 : : -1,
3217 : : moduleMethods,
3218 : : NULL,
3219 : : NULL,
3220 : : NULL,
3221 : : NULL
3222 : : };
3223 : :
3224 : : PyMODINIT_FUNC
3225 : 1 : PyInit__tkinter(void)
3226 : : {
3227 : : PyObject *m, *uexe, *cexe, *o;
3228 : :
3229 : 1 : tcl_lock = PyThread_allocate_lock();
3230 [ - + ]: 1 : if (tcl_lock == NULL)
3231 : 0 : return NULL;
3232 : :
3233 : 1 : m = PyModule_Create(&_tkintermodule);
3234 [ - + ]: 1 : if (m == NULL)
3235 : 0 : return NULL;
3236 : :
3237 : 1 : o = PyErr_NewException("_tkinter.TclError", NULL, NULL);
3238 [ - + ]: 1 : if (o == NULL) {
3239 : 0 : Py_DECREF(m);
3240 : 0 : return NULL;
3241 : : }
3242 [ - + ]: 1 : if (PyModule_AddObject(m, "TclError", Py_NewRef(o))) {
3243 : 0 : Py_DECREF(o);
3244 : 0 : Py_DECREF(m);
3245 : 0 : return NULL;
3246 : : }
3247 : 1 : Tkinter_TclError = o;
3248 : :
3249 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "READABLE", TCL_READABLE)) {
3250 : 0 : Py_DECREF(m);
3251 : 0 : return NULL;
3252 : : }
3253 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "WRITABLE", TCL_WRITABLE)) {
3254 : 0 : Py_DECREF(m);
3255 : 0 : return NULL;
3256 : : }
3257 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "EXCEPTION", TCL_EXCEPTION)) {
3258 : 0 : Py_DECREF(m);
3259 : 0 : return NULL;
3260 : : }
3261 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "WINDOW_EVENTS", TCL_WINDOW_EVENTS)) {
3262 : 0 : Py_DECREF(m);
3263 : 0 : return NULL;
3264 : : }
3265 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "FILE_EVENTS", TCL_FILE_EVENTS)) {
3266 : 0 : Py_DECREF(m);
3267 : 0 : return NULL;
3268 : : }
3269 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "TIMER_EVENTS", TCL_TIMER_EVENTS)) {
3270 : 0 : Py_DECREF(m);
3271 : 0 : return NULL;
3272 : : }
3273 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "IDLE_EVENTS", TCL_IDLE_EVENTS)) {
3274 : 0 : Py_DECREF(m);
3275 : 0 : return NULL;
3276 : : }
3277 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "ALL_EVENTS", TCL_ALL_EVENTS)) {
3278 : 0 : Py_DECREF(m);
3279 : 0 : return NULL;
3280 : : }
3281 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "DONT_WAIT", TCL_DONT_WAIT)) {
3282 : 0 : Py_DECREF(m);
3283 : 0 : return NULL;
3284 : : }
3285 [ - + ]: 1 : if (PyModule_AddStringConstant(m, "TK_VERSION", TK_VERSION)) {
3286 : 0 : Py_DECREF(m);
3287 : 0 : return NULL;
3288 : : }
3289 [ - + ]: 1 : if (PyModule_AddStringConstant(m, "TCL_VERSION", TCL_VERSION)) {
3290 : 0 : Py_DECREF(m);
3291 : 0 : return NULL;
3292 : : }
3293 : :
3294 : 1 : o = PyType_FromSpec(&Tkapp_Type_spec);
3295 [ - + ]: 1 : if (o == NULL) {
3296 : 0 : Py_DECREF(m);
3297 : 0 : return NULL;
3298 : : }
3299 [ - + ]: 1 : if (PyModule_AddObject(m, "TkappType", o)) {
3300 : 0 : Py_DECREF(o);
3301 : 0 : Py_DECREF(m);
3302 : 0 : return NULL;
3303 : : }
3304 : 1 : Tkapp_Type = o;
3305 : :
3306 : 1 : o = PyType_FromSpec(&Tktt_Type_spec);
3307 [ - + ]: 1 : if (o == NULL) {
3308 : 0 : Py_DECREF(m);
3309 : 0 : return NULL;
3310 : : }
3311 [ - + ]: 1 : if (PyModule_AddObject(m, "TkttType", o)) {
3312 : 0 : Py_DECREF(o);
3313 : 0 : Py_DECREF(m);
3314 : 0 : return NULL;
3315 : : }
3316 : 1 : Tktt_Type = o;
3317 : :
3318 : 1 : o = PyType_FromSpec(&PyTclObject_Type_spec);
3319 [ - + ]: 1 : if (o == NULL) {
3320 : 0 : Py_DECREF(m);
3321 : 0 : return NULL;
3322 : : }
3323 [ - + ]: 1 : if (PyModule_AddObject(m, "Tcl_Obj", o)) {
3324 : 0 : Py_DECREF(o);
3325 : 0 : Py_DECREF(m);
3326 : 0 : return NULL;
3327 : : }
3328 : 1 : PyTclObject_Type = o;
3329 : :
3330 : : #ifdef TK_AQUA
3331 : : /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems
3332 : : * start waking up. Note that Tcl_FindExecutable will do this, this
3333 : : * code must be above it! The original warning from
3334 : : * tkMacOSXAppInit.c is copied below.
3335 : : *
3336 : : * NB - You have to swap in the Tk Notifier BEFORE you start up the
3337 : : * Tcl interpreter for now. It probably should work to do this
3338 : : * in the other order, but for now it doesn't seem to.
3339 : : *
3340 : : */
3341 : : Tk_MacOSXSetupTkNotifier();
3342 : : #endif
3343 : :
3344 : :
3345 : : /* This helps the dynamic loader; in Unicode aware Tcl versions
3346 : : it also helps Tcl find its encodings. */
3347 : 1 : uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
3348 [ + - ]: 1 : if (uexe) {
3349 : 1 : cexe = PyUnicode_EncodeFSDefault(uexe);
3350 [ + - ]: 1 : if (cexe) {
3351 : : #ifdef MS_WINDOWS
3352 : : int set_var = 0;
3353 : : PyObject *str_path;
3354 : : wchar_t *wcs_path;
3355 : : DWORD ret;
3356 : :
3357 : : ret = GetEnvironmentVariableW(L"TCL_LIBRARY", NULL, 0);
3358 : :
3359 : : if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
3360 : : str_path = _get_tcl_lib_path();
3361 : : if (str_path == NULL && PyErr_Occurred()) {
3362 : : Py_DECREF(m);
3363 : : return NULL;
3364 : : }
3365 : : if (str_path != NULL) {
3366 : : wcs_path = PyUnicode_AsWideCharString(str_path, NULL);
3367 : : if (wcs_path == NULL) {
3368 : : Py_DECREF(m);
3369 : : return NULL;
3370 : : }
3371 : : SetEnvironmentVariableW(L"TCL_LIBRARY", wcs_path);
3372 : : set_var = 1;
3373 : : }
3374 : : }
3375 : :
3376 : : Tcl_FindExecutable(PyBytes_AS_STRING(cexe));
3377 : :
3378 : : if (set_var) {
3379 : : SetEnvironmentVariableW(L"TCL_LIBRARY", NULL);
3380 : : PyMem_Free(wcs_path);
3381 : : }
3382 : : #else
3383 : 1 : Tcl_FindExecutable(PyBytes_AS_STRING(cexe));
3384 : : #endif /* MS_WINDOWS */
3385 : : }
3386 : 1 : Py_XDECREF(cexe);
3387 : 1 : Py_DECREF(uexe);
3388 : : }
3389 : :
3390 [ - + ]: 1 : if (PyErr_Occurred()) {
3391 : 0 : Py_DECREF(m);
3392 : 0 : return NULL;
3393 : : }
3394 : :
3395 : 1 : return m;
3396 : : }
|