forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_tkinter.c
3564 lines (3017 loc) · 91.3 KB
/
_tkinter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************
Copyright (C) 1994 Steen Lumholt.
All Rights Reserved
******************************************************************/
/* _tkinter.c -- Interface to libtk.a and libtcl.a. */
/* TCL/TK VERSION INFO:
Only Tcl/Tk 8.4 and later are supported. Older versions are not
supported. Use Python 3.4 or older if you cannot upgrade your
Tcl/Tk libraries.
*/
/* XXX Further speed-up ideas, involving Tcl 8.0 features:
- Register a new Tcl type, "Python callable", which can be called more
efficiently and passed to Tcl_EvalObj() directly (if this is possible).
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include <ctype.h>
#include "pythread.h"
#ifdef MS_WINDOWS
#include <windows.h>
#endif
#define CHECK_SIZE(size, elemsize) \
((size_t)(size) <= Py_MIN((size_t)INT_MAX, UINT_MAX / (size_t)(elemsize)))
/* If Tcl is compiled for threads, we must also define TCL_THREAD. We define
it always; if Tcl is not threaded, the thread functions in
Tcl are empty. */
#define TCL_THREADS
#ifdef TK_FRAMEWORK
#include <Tcl/tcl.h>
#include <Tk/tk.h>
#else
#include <tcl.h>
#include <tk.h>
#endif
#include "tkinter.h"
#if TK_HEX_VERSION < 0x08040200
#error "Tk older than 8.4 not supported"
#endif
#if TK_HEX_VERSION >= 0x08050208 && TK_HEX_VERSION < 0x08060000 || \
TK_HEX_VERSION >= 0x08060200
#define HAVE_LIBTOMMAMTH
#include <tclTomMath.h>
#endif
#if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
#define HAVE_CREATEFILEHANDLER
#endif
#ifdef HAVE_CREATEFILEHANDLER
/* This bit is to ensure that TCL_UNIX_FD is defined and doesn't interfere
with the proper calculation of FHANDLETYPE == TCL_UNIX_FD below. */
#ifndef TCL_UNIX_FD
# ifdef TCL_WIN_SOCKET
# define TCL_UNIX_FD (! TCL_WIN_SOCKET)
# else
# define TCL_UNIX_FD 1
# endif
#endif
/* Tcl_CreateFileHandler() changed several times; these macros deal with the
messiness. In Tcl 8.0 and later, it is not available on Windows (and on
Unix, only because Jack added it back); when available on Windows, it only
applies to sockets. */
#ifdef MS_WINDOWS
#define FHANDLETYPE TCL_WIN_SOCKET
#else
#define FHANDLETYPE TCL_UNIX_FD
#endif
/* If Tcl can wait for a Unix file descriptor, define the EventHook() routine
which uses this to handle Tcl events while the user is typing commands. */
#if FHANDLETYPE == TCL_UNIX_FD
#define WAIT_FOR_STDIN
#endif
#endif /* HAVE_CREATEFILEHANDLER */
#ifdef MS_WINDOWS
#include <conio.h>
#define WAIT_FOR_STDIN
static PyObject *
_get_tcl_lib_path()
{
static PyObject *tcl_library_path = NULL;
static int already_checked = 0;
if (already_checked == 0) {
PyObject *prefix;
struct stat stat_buf;
int stat_return_value;
prefix = PyUnicode_FromWideChar(Py_GetPrefix(), -1);
if (prefix == NULL) {
return NULL;
}
/* Check expected location for an installed Python first */
tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
if (tcl_library_path == NULL) {
return NULL;
}
tcl_library_path = PyUnicode_Concat(prefix, tcl_library_path);
if (tcl_library_path == NULL) {
return NULL;
}
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
if (stat_return_value == -2) {
return NULL;
}
if (stat_return_value == -1) {
/* install location doesn't exist, reset errno and see if
we're a repository build */
errno = 0;
#ifdef Py_TCLTK_DIR
tcl_library_path = PyUnicode_FromString(
Py_TCLTK_DIR "\\lib\\tcl" TCL_VERSION);
if (tcl_library_path == NULL) {
return NULL;
}
stat_return_value = _Py_stat(tcl_library_path, &stat_buf);
if (stat_return_value == -2) {
return NULL;
}
if (stat_return_value == -1) {
/* tcltkDir for a repository build doesn't exist either,
reset errno and leave Tcl to its own devices */
errno = 0;
tcl_library_path = NULL;
}
#else
tcl_library_path = NULL;
#endif
}
already_checked = 1;
}
return tcl_library_path;
}
#endif /* MS_WINDOWS */
/* The threading situation is complicated. Tcl is not thread-safe, except
when configured with --enable-threads.
So we need to use a lock around all uses of Tcl. Previously, the
Python interpreter lock was used for this. However, this causes
problems when other Python threads need to run while Tcl is blocked
waiting for events.
To solve this problem, a separate lock for Tcl is introduced.
Holding it is incompatible with holding Python's interpreter lock.
The following four macros manipulate both locks together.
ENTER_TCL and LEAVE_TCL are brackets, just like
Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. They should be
used whenever a call into Tcl is made that could call an event
handler, or otherwise affect the state of a Tcl interpreter. These
assume that the surrounding code has the Python interpreter lock;
inside the brackets, the Python interpreter lock has been released
and the lock for Tcl has been acquired.
Sometimes, it is necessary to have both the Python lock and the Tcl
lock. (For example, when transferring data from the Tcl
interpreter result to a Python string object.) This can be done by
using different macros to close the ENTER_TCL block: ENTER_OVERLAP
reacquires the Python lock (and restores the thread state) but
doesn't release the Tcl lock; LEAVE_OVERLAP_TCL releases the Tcl
lock.
By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event
handlers when the handler needs to use Python. Such event handlers
are entered while the lock for Tcl is held; the event handler
presumably needs to use Python. ENTER_PYTHON releases the lock for
Tcl and acquires the Python interpreter lock, restoring the
appropriate thread state, and LEAVE_PYTHON releases the Python
interpreter lock and re-acquires the lock for Tcl. It is okay for
ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between
ENTER_PYTHON and LEAVE_PYTHON.
These locks expand to several statements and brackets; they should
not be used in branches of if statements and the like.
If Tcl is threaded, this approach won't work anymore. The Tcl
interpreter is only valid in the thread that created it, and all Tk
activity must happen in this thread, also. That means that the
mainloop must be invoked in the thread that created the
interpreter. Invoking commands from other threads is possible;
_tkinter will queue an event for the interpreter thread, which will
then execute the command and pass back the result. If the main
thread is not in the mainloop, and invoking commands causes an
exception; if the main loop is running but not processing events,
the command invocation will block.
In addition, for a threaded Tcl, a single global tcl_tstate won't
be sufficient anymore, since multiple Tcl interpreters may
simultaneously dispatch in different threads. So we use the Tcl TLS
API.
*/
static PyThread_type_lock tcl_lock = 0;
#ifdef TCL_THREADS
static Tcl_ThreadDataKey state_key;
typedef PyThreadState *ThreadSpecificData;
#define tcl_tstate \
(*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*)))
#else
static PyThreadState *tcl_tstate = NULL;
#endif
#define ENTER_TCL \
{ PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate;
#define LEAVE_TCL \
tcl_tstate = NULL; \
if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS}
#define ENTER_OVERLAP \
Py_END_ALLOW_THREADS
#define LEAVE_OVERLAP_TCL \
tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
#define ENTER_PYTHON \
{ PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
if(tcl_lock) \
PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }
#define LEAVE_PYTHON \
{ PyThreadState *tstate = PyEval_SaveThread(); \
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; }
#define CHECK_TCL_APPARTMENT \
if (((TkappObject *)self)->threaded && \
((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \
PyErr_SetString(PyExc_RuntimeError, \
"Calling Tcl from different apartment"); \
return 0; \
}
#ifndef FREECAST
#define FREECAST (char *)
#endif
/**** Tkapp Object Declaration ****/
static PyObject *Tkapp_Type;
typedef struct {
PyObject_HEAD
Tcl_Interp *interp;
int wantobjects;
int threaded; /* True if tcl_platform[threaded] */
Tcl_ThreadId thread_id;
int dispatching;
/* We cannot include tclInt.h, as this is internal.
So we cache interesting types here. */
const Tcl_ObjType *OldBooleanType;
const Tcl_ObjType *BooleanType;
const Tcl_ObjType *ByteArrayType;
const Tcl_ObjType *DoubleType;
const Tcl_ObjType *IntType;
const Tcl_ObjType *WideIntType;
const Tcl_ObjType *BignumType;
const Tcl_ObjType *ListType;
const Tcl_ObjType *ProcBodyType;
const Tcl_ObjType *StringType;
} TkappObject;
#define Tkapp_Interp(v) (((TkappObject *) (v))->interp)
#define Tkapp_Result(v) Tcl_GetStringResult(Tkapp_Interp(v))
#define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \
(void *) v, Py_REFCNT(v)))
/**** Error Handling ****/
static PyObject *Tkinter_TclError;
static int quitMainLoop = 0;
static int errorInCmd = 0;
static PyObject *excInCmd;
static PyObject *valInCmd;
static PyObject *trbInCmd;
#ifdef TKINTER_PROTECT_LOADTK
static int tk_load_failed = 0;
#endif
static PyObject *
Tkinter_Error(PyObject *v)
{
PyErr_SetString(Tkinter_TclError, Tkapp_Result(v));
return NULL;
}
/**** Utils ****/
static int Tkinter_busywaitinterval = 20;
#ifndef MS_WINDOWS
/* Millisecond sleep() for Unix platforms. */
static void
Sleep(int milli)
{
/* XXX Too bad if you don't have select(). */
struct timeval t;
t.tv_sec = milli/1000;
t.tv_usec = (milli%1000) * 1000;
select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
}
#endif /* MS_WINDOWS */
/* Wait up to 1s for the mainloop to come up. */
static int
WaitForMainloop(TkappObject* self)
{
int i;
for (i = 0; i < 10; i++) {
if (self->dispatching)
return 1;
Py_BEGIN_ALLOW_THREADS
Sleep(100);
Py_END_ALLOW_THREADS
}
if (self->dispatching)
return 1;
PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop");
return 0;
}
#define ARGSZ 64
static PyObject *
unicodeFromTclStringAndSize(const char *s, Py_ssize_t size)
{
PyObject *r = PyUnicode_DecodeUTF8(s, size, NULL);
if (!r && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
/* Tcl encodes null character as \xc0\x80 */
if (memchr(s, '\xc0', size)) {
char *buf, *q;
const char *e = s + size;
PyErr_Clear();
q = buf = (char *)PyMem_Malloc(size);
if (buf == NULL) {
PyErr_NoMemory();
return NULL;
}
while (s != e) {
if (s + 1 != e && s[0] == '\xc0' && s[1] == '\x80') {
*q++ = '\0';
s += 2;
}
else
*q++ = *s++;
}
s = buf;
size = q - s;
r = PyUnicode_DecodeUTF8(s, size, NULL);
PyMem_Free(buf);
}
}
return r;
}
static PyObject *
unicodeFromTclString(const char *s)
{
return unicodeFromTclStringAndSize(s, strlen(s));
}
static PyObject *
unicodeFromTclObj(Tcl_Obj *value)
{
int len;
char *s = Tcl_GetStringFromObj(value, &len);
return unicodeFromTclStringAndSize(s, len);
}
static PyObject *
Split(const char *list)
{
int argc;
const char **argv;
PyObject *v;
if (list == NULL) {
Py_RETURN_NONE;
}
if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
/* Not a list.
* Could be a quoted string containing funnies, e.g. {"}.
* Return the string itself.
*/
return unicodeFromTclString(list);
}
if (argc == 0)
v = PyUnicode_FromString("");
else if (argc == 1)
v = unicodeFromTclString(argv[0]);
else if ((v = PyTuple_New(argc)) != NULL) {
int i;
PyObject *w;
for (i = 0; i < argc; i++) {
if ((w = Split(argv[i])) == NULL) {
Py_DECREF(v);
v = NULL;
break;
}
PyTuple_SET_ITEM(v, i, w);
}
}
Tcl_Free(FREECAST argv);
return v;
}
/* In some cases, Tcl will still return strings that are supposed to
be lists. SplitObj walks through a nested tuple, finding string
objects that need to be split. */
static PyObject *
SplitObj(PyObject *arg)
{
if (PyTuple_Check(arg)) {
Py_ssize_t i, size;
PyObject *elem, *newelem, *result;
size = PyTuple_GET_SIZE(arg);
result = NULL;
/* Recursively invoke SplitObj for all tuple items.
If this does not return a new object, no action is
needed. */
for(i = 0; i < size; i++) {
elem = PyTuple_GET_ITEM(arg, i);
newelem = SplitObj(elem);
if (!newelem) {
Py_XDECREF(result);
return NULL;
}
if (!result) {
Py_ssize_t k;
if (newelem == elem) {
Py_DECREF(newelem);
continue;
}
result = PyTuple_New(size);
if (!result)
return NULL;
for(k = 0; k < i; k++) {
elem = PyTuple_GET_ITEM(arg, k);
Py_INCREF(elem);
PyTuple_SET_ITEM(result, k, elem);
}
}
PyTuple_SET_ITEM(result, i, newelem);
}
if (result)
return result;
/* Fall through, returning arg. */
}
else if (PyList_Check(arg)) {
Py_ssize_t i, size;
PyObject *elem, *newelem, *result;
size = PyList_GET_SIZE(arg);
result = PyTuple_New(size);
if (!result)
return NULL;
/* Recursively invoke SplitObj for all list items. */
for(i = 0; i < size; i++) {
elem = PyList_GET_ITEM(arg, i);
newelem = SplitObj(elem);
if (!newelem) {
Py_XDECREF(result);
return NULL;
}
PyTuple_SET_ITEM(result, i, newelem);
}
return result;
}
else if (PyUnicode_Check(arg)) {
int argc;
const char **argv;
const char *list = PyUnicode_AsUTF8(arg);
if (list == NULL ||
Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
Py_INCREF(arg);
return arg;
}
Tcl_Free(FREECAST argv);
if (argc > 1)
return Split(list);
/* Fall through, returning arg. */
}
else if (PyBytes_Check(arg)) {
int argc;
const char **argv;
char *list = PyBytes_AS_STRING(arg);
if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
Py_INCREF(arg);
return arg;
}
Tcl_Free(FREECAST argv);
if (argc > 1)
return Split(PyBytes_AS_STRING(arg));
/* Fall through, returning arg. */
}
Py_INCREF(arg);
return arg;
}
/*[clinic input]
module _tkinter
class _tkinter.tkapp "TkappObject *" "&Tkapp_Type_spec"
class _tkinter.Tcl_Obj "PyTclObject *" "&PyTclObject_Type_spec"
class _tkinter.tktimertoken "TkttObject *" "&Tktt_Type_spec"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b1ebf15c162ee229]*/
/**** Tkapp Object ****/
#ifndef WITH_APPINIT
int
Tcl_AppInit(Tcl_Interp *interp)
{
const char * _tkinter_skip_tk_init;
if (Tcl_Init(interp) == TCL_ERROR) {
PySys_WriteStderr("Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
return TCL_ERROR;
}
_tkinter_skip_tk_init = Tcl_GetVar(interp,
"_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
if (_tkinter_skip_tk_init != NULL &&
strcmp(_tkinter_skip_tk_init, "1") == 0) {
return TCL_OK;
}
#ifdef TKINTER_PROTECT_LOADTK
if (tk_load_failed) {
PySys_WriteStderr("Tk_Init error: %s\n", TKINTER_LOADTK_ERRMSG);
return TCL_ERROR;
}
#endif
if (Tk_Init(interp) == TCL_ERROR) {
#ifdef TKINTER_PROTECT_LOADTK
tk_load_failed = 1;
#endif
PySys_WriteStderr("Tk_Init error: %s\n", Tcl_GetStringResult(interp));
return TCL_ERROR;
}
return TCL_OK;
}
#endif /* !WITH_APPINIT */
/* Initialize the Tk application; see the `main' function in
* `tkMain.c'.
*/
static void EnableEventHook(void); /* Forward */
static void DisableEventHook(void); /* Forward */
static TkappObject *
Tkapp_New(const char *screenName, const char *className,
int interactive, int wantobjects, int wantTk, int sync,
const char *use)
{
TkappObject *v;
char *argv0;
v = PyObject_New(TkappObject, (PyTypeObject *) Tkapp_Type);
if (v == NULL)
return NULL;
Py_INCREF(Tkapp_Type);
v->interp = Tcl_CreateInterp();
v->wantobjects = wantobjects;
v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded",
TCL_GLOBAL_ONLY) != NULL;
v->thread_id = Tcl_GetCurrentThread();
v->dispatching = 0;
#ifndef TCL_THREADS
if (v->threaded) {
PyErr_SetString(PyExc_RuntimeError,
"Tcl is threaded but _tkinter is not");
Py_DECREF(v);
return 0;
}
#endif
if (v->threaded && tcl_lock) {
/* If Tcl is threaded, we don't need the lock. */
PyThread_free_lock(tcl_lock);
tcl_lock = NULL;
}
v->OldBooleanType = Tcl_GetObjType("boolean");
v->BooleanType = Tcl_GetObjType("booleanString");
v->ByteArrayType = Tcl_GetObjType("bytearray");
v->DoubleType = Tcl_GetObjType("double");
v->IntType = Tcl_GetObjType("int");
v->WideIntType = Tcl_GetObjType("wideInt");
v->BignumType = Tcl_GetObjType("bignum");
v->ListType = Tcl_GetObjType("list");
v->ProcBodyType = Tcl_GetObjType("procbody");
v->StringType = Tcl_GetObjType("string");
/* Delete the 'exit' command, which can screw things up */
Tcl_DeleteCommand(v->interp, "exit");
if (screenName != NULL)
Tcl_SetVar2(v->interp, "env", "DISPLAY",
screenName, TCL_GLOBAL_ONLY);
if (interactive)
Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
else
Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
/* This is used to get the application class for Tk 4.1 and up */
argv0 = (char*)PyMem_Malloc(strlen(className) + 1);
if (!argv0) {
PyErr_NoMemory();
Py_DECREF(v);
return NULL;
}
strcpy(argv0, className);
if (Py_ISUPPER(Py_CHARMASK(argv0[0])))
argv0[0] = Py_TOLOWER(Py_CHARMASK(argv0[0]));
Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
PyMem_Free(argv0);
if (! wantTk) {
Tcl_SetVar(v->interp,
"_tkinter_skip_tk_init", "1", TCL_GLOBAL_ONLY);
}
#ifdef TKINTER_PROTECT_LOADTK
else if (tk_load_failed) {
Tcl_SetVar(v->interp,
"_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
}
#endif
/* some initial arguments need to be in argv */
if (sync || use) {
char *args;
Py_ssize_t len = 0;
if (sync)
len += sizeof "-sync";
if (use)
len += strlen(use) + sizeof "-use "; /* never overflows */
args = (char*)PyMem_Malloc(len);
if (!args) {
PyErr_NoMemory();
Py_DECREF(v);
return NULL;
}
args[0] = '\0';
if (sync)
strcat(args, "-sync");
if (use) {
if (sync)
strcat(args, " ");
strcat(args, "-use ");
strcat(args, use);
}
Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY);
PyMem_Free(args);
}
#ifdef MS_WINDOWS
{
PyObject *str_path;
PyObject *utf8_path;
DWORD ret;
ret = GetEnvironmentVariableW(L"TCL_LIBRARY", NULL, 0);
if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
str_path = _get_tcl_lib_path();
if (str_path == NULL && PyErr_Occurred()) {
return NULL;
}
if (str_path != NULL) {
utf8_path = PyUnicode_AsUTF8String(str_path);
if (utf8_path == NULL) {
return NULL;
}
Tcl_SetVar(v->interp,
"tcl_library",
PyBytes_AS_STRING(utf8_path),
TCL_GLOBAL_ONLY);
Py_DECREF(utf8_path);
}
}
}
#endif
if (Tcl_AppInit(v->interp) != TCL_OK) {
PyObject *result = Tkinter_Error((PyObject *)v);
#ifdef TKINTER_PROTECT_LOADTK
if (wantTk) {
const char *_tkinter_tk_failed;
_tkinter_tk_failed = Tcl_GetVar(v->interp,
"_tkinter_tk_failed", TCL_GLOBAL_ONLY);
if ( _tkinter_tk_failed != NULL &&
strcmp(_tkinter_tk_failed, "1") == 0) {
tk_load_failed = 1;
}
}
#endif
Py_DECREF((PyObject *)v);
return (TkappObject *)result;
}
EnableEventHook();
return v;
}
static void
Tkapp_ThreadSend(TkappObject *self, Tcl_Event *ev,
Tcl_Condition *cond, Tcl_Mutex *mutex)
{
Py_BEGIN_ALLOW_THREADS;
Tcl_MutexLock(mutex);
Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL);
Tcl_ThreadAlert(self->thread_id);
Tcl_ConditionWait(cond, mutex, NULL);
Tcl_MutexUnlock(mutex);
Py_END_ALLOW_THREADS
}
/** Tcl Eval **/
typedef struct {
PyObject_HEAD
Tcl_Obj *value;
PyObject *string; /* This cannot cause cycles. */
} PyTclObject;
static PyObject *PyTclObject_Type;
#define PyTclObject_Check(v) ((v)->ob_type == (PyTypeObject *) PyTclObject_Type)
static PyObject *
newPyTclObject(Tcl_Obj *arg)
{
PyTclObject *self;
self = PyObject_New(PyTclObject, (PyTypeObject *) PyTclObject_Type);
if (self == NULL)
return NULL;
Py_INCREF(PyTclObject_Type);
Tcl_IncrRefCount(arg);
self->value = arg;
self->string = NULL;
return (PyObject*)self;
}
static void
PyTclObject_dealloc(PyTclObject *self)
{
PyObject *tp = (PyObject *) Py_TYPE(self);
Tcl_DecrRefCount(self->value);
Py_XDECREF(self->string);
PyObject_Del(self);
Py_DECREF(tp);
}
static const char *
PyTclObject_TclString(PyObject *self)
{
return Tcl_GetString(((PyTclObject*)self)->value);
}
/* Like _str, but create Unicode if necessary. */
PyDoc_STRVAR(PyTclObject_string__doc__,
"the string representation of this object, either as str or bytes");
static PyObject *
PyTclObject_string(PyTclObject *self, void *ignored)
{
if (!self->string) {
self->string = unicodeFromTclObj(self->value);
if (!self->string)
return NULL;
}
Py_INCREF(self->string);
return self->string;
}
static PyObject *
PyTclObject_str(PyTclObject *self)
{
if (self->string) {
Py_INCREF(self->string);
return self->string;
}
/* XXX Could chache result if it is non-ASCII. */
return unicodeFromTclObj(self->value);
}
static PyObject *
PyTclObject_repr(PyTclObject *self)
{
PyObject *repr, *str = PyTclObject_str(self);
if (str == NULL)
return NULL;
repr = PyUnicode_FromFormat("<%s object: %R>",
self->value->typePtr->name, str);
Py_DECREF(str);
return repr;
}
static PyObject *
PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
{
int result;
/* neither argument should be NULL, unless something's gone wrong */
if (self == NULL || other == NULL) {
PyErr_BadInternalCall();
return NULL;
}
/* both arguments should be instances of PyTclObject */
if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
Py_RETURN_NOTIMPLEMENTED;
}
if (self == other)
/* fast path when self and other are identical */
result = 0;
else
result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
Tcl_GetString(((PyTclObject *)other)->value));
Py_RETURN_RICHCOMPARE(result, 0, op);
}
PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
static PyObject*
get_typename(PyTclObject* obj, void* ignored)
{
return unicodeFromTclString(obj->value->typePtr->name);
}
static PyGetSetDef PyTclObject_getsetlist[] = {
{"typename", (getter)get_typename, NULL, get_typename__doc__},
{"string", (getter)PyTclObject_string, NULL,
PyTclObject_string__doc__},
{0},
};
static PyType_Slot PyTclObject_Type_slots[] = {
{Py_tp_dealloc, (destructor)PyTclObject_dealloc},
{Py_tp_repr, (reprfunc)PyTclObject_repr},
{Py_tp_str, (reprfunc)PyTclObject_str},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_richcompare, PyTclObject_richcompare},
{Py_tp_getset, PyTclObject_getsetlist},
{0, 0}
};
static PyType_Spec PyTclObject_Type_spec = {
"_tkinter.Tcl_Obj",
sizeof(PyTclObject),
0,
Py_TPFLAGS_DEFAULT,
PyTclObject_Type_slots,
};
#if SIZE_MAX > INT_MAX
#define CHECK_STRING_LENGTH(s) do { \
if (s != NULL && strlen(s) >= INT_MAX) { \
PyErr_SetString(PyExc_OverflowError, "string is too long"); \
return NULL; \
} } while(0)
#else
#define CHECK_STRING_LENGTH(s)
#endif
#ifdef HAVE_LIBTOMMAMTH
static Tcl_Obj*
asBignumObj(PyObject *value)
{
Tcl_Obj *result;
int neg;
PyObject *hexstr;
const char *hexchars;
mp_int bigValue;
neg = Py_SIZE(value) < 0;
hexstr = _PyLong_Format(value, 16);
if (hexstr == NULL)
return NULL;
hexchars = PyUnicode_AsUTF8(hexstr);
if (hexchars == NULL) {
Py_DECREF(hexstr);
return NULL;
}
hexchars += neg + 2; /* skip sign and "0x" */
mp_init(&bigValue);
if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) {
mp_clear(&bigValue);
Py_DECREF(hexstr);
PyErr_NoMemory();
return NULL;
}
Py_DECREF(hexstr);
bigValue.sign = neg ? MP_NEG : MP_ZPOS;
result = Tcl_NewBignumObj(&bigValue);
mp_clear(&bigValue);
if (result == NULL) {
PyErr_NoMemory();
return NULL;
}
return result;
}
#endif
static Tcl_Obj*
AsObj(PyObject *value)
{
Tcl_Obj *result;
if (PyBytes_Check(value)) {
if (PyBytes_GET_SIZE(value) >= INT_MAX) {
PyErr_SetString(PyExc_OverflowError, "bytes object is too long");
return NULL;
}
return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value),
(int)PyBytes_GET_SIZE(value));
}
if (PyBool_Check(value))
return Tcl_NewBooleanObj(PyObject_IsTrue(value));
if (PyLong_CheckExact(value)) {
int overflow;
long longValue;
#ifdef TCL_WIDE_INT_TYPE
Tcl_WideInt wideValue;
#endif
longValue = PyLong_AsLongAndOverflow(value, &overflow);
if (!overflow) {