Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13)

Delta Between Two Patch Sets: Modules/_tkinter.c

Issue 16840: Tkinter doesn't support large integers
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 4 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Modules/tkinter.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*********************************************************** 1 /***********************************************************
2 Copyright (C) 1994 Steen Lumholt. 2 Copyright (C) 1994 Steen Lumholt.
3 3
4 All Rights Reserved 4 All Rights Reserved
5 5
6 ******************************************************************/ 6 ******************************************************************/
7 7
8 /* _tkinter.c -- Interface to libtk.a and libtcl.a. */ 8 /* _tkinter.c -- Interface to libtk.a and libtcl.a. */
9 9
10 /* TCL/TK VERSION INFO: 10 /* TCL/TK VERSION INFO:
(...skipping 15 matching lines...) Expand all
26 #include <ctype.h> 26 #include <ctype.h>
27 27
28 #ifdef WITH_THREAD 28 #ifdef WITH_THREAD
29 #include "pythread.h" 29 #include "pythread.h"
30 #endif 30 #endif
31 31
32 #ifdef MS_WINDOWS 32 #ifdef MS_WINDOWS
33 #include <windows.h> 33 #include <windows.h>
34 #endif 34 #endif
35 35
36 /* Allow using this code in Python 2.[12] */
37 #ifndef PyDoc_STRVAR
38 #define PyDoc_STRVAR(name,str) static char name[] = str
39 #endif
40
41 #ifndef PyMODINIT_FUNC
42 #define PyMODINIT_FUNC void
43 #endif
44
45 #ifndef PyBool_Check
46 #define PyBool_Check(o) 0
47 #define PyBool_FromLong PyInt_FromLong
48 #endif
49
36 #define CHECK_SIZE(size, elemsize) \ 50 #define CHECK_SIZE(size, elemsize) \
37 ((size_t)(size) <= Py_MAX((size_t)INT_MAX, UINT_MAX / (size_t)(elemsize))) 51 ((size_t)(size) <= (size_t)INT_MAX && \
52 (size_t)(size) <= UINT_MAX / (size_t)(elemsize))
38 53
39 /* Starting with Tcl 8.4, many APIs offer const-correctness. Unfortunately, 54 /* Starting with Tcl 8.4, many APIs offer const-correctness. Unfortunately,
40 making _tkinter correct for this API means to break earlier 55 making _tkinter correct for this API means to break earlier
41 versions. USE_COMPAT_CONST allows to make _tkinter work with both 8.4 and 56 versions. USE_COMPAT_CONST allows to make _tkinter work with both 8.4 and
42 earlier versions. Once Tcl releases before 8.4 don't need to be supported 57 earlier versions. Once Tcl releases before 8.4 don't need to be supported
43 anymore, this should go. */ 58 anymore, this should go. */
44 #define USE_COMPAT_CONST 59 #define USE_COMPAT_CONST
45 60
46 /* If Tcl is compiled for threads, we must also define TCL_THREAD. We define 61 /* If Tcl is compiled for threads, we must also define TCL_THREAD. We define
47 it always; if Tcl is not threaded, the thread functions in 62 it always; if Tcl is not threaded, the thread functions in
(...skipping 10 matching lines...) Expand all
58 73
59 #include "tkinter.h" 74 #include "tkinter.h"
60 75
61 /* For Tcl 8.2 and 8.3, CONST* is not defined (except on Cygwin). */ 76 /* For Tcl 8.2 and 8.3, CONST* is not defined (except on Cygwin). */
62 #ifndef CONST84_RETURN 77 #ifndef CONST84_RETURN
63 #define CONST84_RETURN 78 #define CONST84_RETURN
64 #undef CONST 79 #undef CONST
65 #define CONST 80 #define CONST
66 #endif 81 #endif
67 82
68 #if TK_VERSION_HEX < 0x08030102 83 #if TK_HEX_VERSION < 0x08030201
69 #error "Tk older than 8.3.1 not supported" 84 #error "Tk older than 8.3.1 not supported"
70 #endif 85 #endif
71 86
72 #if TK_VERSION_HEX >= 0x08050000 87 /* Unicode conversion assumes that Tcl_UniChar is two bytes.
88 We cannot test this directly, so we test UTF-8 size instead,
89 expecting that TCL_UTF_MAX is changed if Tcl ever supports
90 either UTF-16 or UCS-4.
91 Redhat 8 sets TCL_UTF_MAX to 6, and uses wchar_t for
92 Tcl_Unichar. This is also ok as long as Python uses UCS-4,
93 as well.
94 */
95 #if TCL_UTF_MAX != 3 && !(defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==6)
96 #error "unsupported Tcl configuration"
97 #endif
98
99 #if TK_HEX_VERSION >= 0x08050208 && TK_HEX_VERSION < 0x08060000 || \
100 TK_HEX_VERSION >= 0x08060200
73 #define HAVE_LIBTOMMAMTH 101 #define HAVE_LIBTOMMAMTH
74 #include <tclTomMath.h> 102 #include <tclTomMath.h>
75 #endif 103 #endif
76 104
77 #if !(defined(MS_WINDOWS) || defined(__CYGWIN__)) 105 #if !(defined(MS_WINDOWS) || defined(__CYGWIN__))
78 #define HAVE_CREATEFILEHANDLER 106 #define HAVE_CREATEFILEHANDLER
79 #endif 107 #endif
80 108
81 #ifdef HAVE_CREATEFILEHANDLER 109 #ifdef HAVE_CREATEFILEHANDLER
82 110
(...skipping 29 matching lines...) Expand all
112 140
113 #ifdef MS_WINDOWS 141 #ifdef MS_WINDOWS
114 #include <conio.h> 142 #include <conio.h>
115 #define WAIT_FOR_STDIN 143 #define WAIT_FOR_STDIN
116 #endif 144 #endif
117 145
118 #ifdef WITH_THREAD 146 #ifdef WITH_THREAD
119 147
120 /* The threading situation is complicated. Tcl is not thread-safe, except 148 /* The threading situation is complicated. Tcl is not thread-safe, except
121 when configured with --enable-threads. 149 when configured with --enable-threads.
122 150 So we need to use a lock around all uses of Tcl. Previously, the Python
123 So we need to use a lock around all uses of Tcl. Previously, the 151 interpreter lock was used for this. However, this causes problems when
124 Python interpreter lock was used for this. However, this causes 152 other Python threads need to run while Tcl is blocked waiting for events.
125 problems when other Python threads need to run while Tcl is blocked 153
126 waiting for events. 154 To solve this problem, a separate lock for Tcl is introduced. Holding it
127 155 is incompatible with holding Python's interpreter lock. The following four
128 To solve this problem, a separate lock for Tcl is introduced. 156 macros manipulate both locks together.
129 Holding it is incompatible with holding Python's interpreter lock. 157
130 The following four macros manipulate both locks together. 158 ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and
131 159 Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made
132 ENTER_TCL and LEAVE_TCL are brackets, just like 160 that could call an event handler, or otherwise affect the state of a Tcl
133 Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. They should be 161 interpreter. These assume that the surrounding code has the Python
134 used whenever a call into Tcl is made that could call an event 162 interpreter lock; inside the brackets, the Python interpreter lock has been
135 handler, or otherwise affect the state of a Tcl interpreter. These 163 released and the lock for Tcl has been acquired.
136 assume that the surrounding code has the Python interpreter lock; 164
137 inside the brackets, the Python interpreter lock has been released 165 Sometimes, it is necessary to have both the Python lock and the Tcl lock.
138 and the lock for Tcl has been acquired. 166 (For example, when transferring data from the Tcl interpreter result to a
139 167 Python string object.) This can be done by using different macros to close
140 Sometimes, it is necessary to have both the Python lock and the Tcl 168 the ENTER_TCL block: ENTER_OVERLAP reacquires the Python lock (and restores
141 lock. (For example, when transferring data from the Tcl 169 the thread state) but doesn't release the Tcl lock; LEAVE_OVERLAP_TCL
142 interpreter result to a Python string object.) This can be done by 170 releases the Tcl lock.
143 using different macros to close the ENTER_TCL block: ENTER_OVERLAP
144 reacquires the Python lock (and restores the thread state) but
145 doesn't release the Tcl lock; LEAVE_OVERLAP_TCL releases the Tcl
146 lock.
147 171
148 By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event 172 By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event
149 handlers when the handler needs to use Python. Such event handlers 173 handlers when the handler needs to use Python. Such event handlers are
150 are entered while the lock for Tcl is held; the event handler 174 entered while the lock for Tcl is held; the event handler presumably needs
151 presumably needs to use Python. ENTER_PYTHON releases the lock for 175 to use Python. ENTER_PYTHON releases the lock for Tcl and acquires
152 Tcl and acquires the Python interpreter lock, restoring the 176 the Python interpreter lock, restoring the appropriate thread state, and
153 appropriate thread state, and LEAVE_PYTHON releases the Python 177 LEAVE_PYTHON releases the Python interpreter lock and re-acquires the lock
154 interpreter lock and re-acquires the lock for Tcl. It is okay for 178 for Tcl. It is okay for ENTER_TCL/LEAVE_TCL pairs to be contained inside
155 ENTER_TCL/LEAVE_TCL pairs to be contained inside the code between 179 the code between ENTER_PYTHON and LEAVE_PYTHON.
156 ENTER_PYTHON and LEAVE_PYTHON. 180
157 181 These locks expand to several statements and brackets; they should not be
158 These locks expand to several statements and brackets; they should 182 used in branches of if statements and the like.
159 not be used in branches of if statements and the like. 183
160 184 If Tcl is threaded, this approach won't work anymore. The Tcl interpreter is
161 If Tcl is threaded, this approach won't work anymore. The Tcl 185 only valid in the thread that created it, and all Tk activity must happen in this
162 interpreter is only valid in the thread that created it, and all Tk 186 thread, also. That means that the mainloop must be invoked in the thread that
163 activity must happen in this thread, also. That means that the 187 created the interpreter. Invoking commands from other threads is possible;
164 mainloop must be invoked in the thread that created the 188 _tkinter will queue an event for the interpreter thread, which will then
165 interpreter. Invoking commands from other threads is possible; 189 execute the command and pass back the result. If the main thread is not in th e
166 _tkinter will queue an event for the interpreter thread, which will 190 mainloop, and invoking commands causes an exception; if the main loop is runn ing
167 then execute the command and pass back the result. If the main 191 but not processing events, the command invocation will block.
168 thread is not in the mainloop, and invoking commands causes an 192
169 exception; if the main loop is running but not processing events, 193 In addition, for a threaded Tcl, a single global tcl_tstate won't be sufficie nt
170 the command invocation will block. 194 anymore, since multiple Tcl interpreters may simultaneously dispatch in diffe rent
171 195 threads. So we use the Tcl TLS API.
172 In addition, for a threaded Tcl, a single global tcl_tstate won't
173 be sufficient anymore, since multiple Tcl interpreters may
174 simultaneously dispatch in different threads. So we use the Tcl TLS
175 API.
176 196
177 */ 197 */
178 198
179 static PyThread_type_lock tcl_lock = 0; 199 static PyThread_type_lock tcl_lock = 0;
180 200
181 #ifdef TCL_THREADS 201 #ifdef TCL_THREADS
182 static Tcl_ThreadDataKey state_key; 202 static Tcl_ThreadDataKey state_key;
183 typedef PyThreadState *ThreadSpecificData; 203 typedef PyThreadState *ThreadSpecificData;
184 #define tcl_tstate \ 204 #define tcl_tstate (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThr eadState*)))
185 (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*)))
186 #else 205 #else
187 static PyThreadState *tcl_tstate = NULL; 206 static PyThreadState *tcl_tstate = NULL;
188 #endif 207 #endif
189 208
190 #define ENTER_TCL \ 209 #define ENTER_TCL \
191 { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \ 210 { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \
192 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; 211 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate;
193 212
194 #define LEAVE_TCL \ 213 #define LEAVE_TCL \
195 tcl_tstate = NULL; \ 214 tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW _THREADS}
196 if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS}
197 215
198 #define ENTER_OVERLAP \ 216 #define ENTER_OVERLAP \
199 Py_END_ALLOW_THREADS 217 Py_END_ALLOW_THREADS
200 218
201 #define LEAVE_OVERLAP_TCL \ 219 #define LEAVE_OVERLAP_TCL \
202 tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); } 220 tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); }
203 221
204 #define ENTER_PYTHON \ 222 #define ENTER_PYTHON \
205 { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ 223 { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \
206 if(tcl_lock) \ 224 if(tcl_lock)PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstat e)); }
207 PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); }
208 225
209 #define LEAVE_PYTHON \ 226 #define LEAVE_PYTHON \
210 { PyThreadState *tstate = PyEval_SaveThread(); \ 227 { PyThreadState *tstate = PyEval_SaveThread(); \
211 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; } 228 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; }
212 229
213 #define CHECK_TCL_APPARTMENT \ 230 #define CHECK_TCL_APPARTMENT \
214 if (((TkappObject *)self)->threaded && \ 231 if (((TkappObject *)self)->threaded && \
215 ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \ 232 ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \
216 PyErr_SetString(PyExc_RuntimeError, \ 233 PyErr_SetString(PyExc_RuntimeError, "Calling Tcl from different appartme nt"); \
217 "Calling Tcl from different appartment"); \
218 return 0; \ 234 return 0; \
219 } 235 }
220 236
221 #else 237 #else
222 238
223 #define ENTER_TCL 239 #define ENTER_TCL
224 #define LEAVE_TCL 240 #define LEAVE_TCL
225 #define ENTER_OVERLAP 241 #define ENTER_OVERLAP
226 #define LEAVE_OVERLAP_TCL 242 #define LEAVE_OVERLAP_TCL
227 #define ENTER_PYTHON 243 #define ENTER_PYTHON
228 #define LEAVE_PYTHON 244 #define LEAVE_PYTHON
229 #define CHECK_TCL_APPARTMENT 245 #define CHECK_TCL_APPARTMENT
230 246
231 #endif 247 #endif
232 248
233 #ifndef FREECAST 249 #ifndef FREECAST
234 #define FREECAST (char *) 250 #define FREECAST (char *)
235 #endif 251 #endif
236 252
237 /**** Tkapp Object Declaration ****/ 253 /**** Tkapp Object Declaration ****/
238 254
239 static PyObject *Tkapp_Type; 255 static PyTypeObject Tkapp_Type;
240 256
241 typedef struct { 257 typedef struct {
242 PyObject_HEAD 258 PyObject_HEAD
243 Tcl_Interp *interp; 259 Tcl_Interp *interp;
244 int wantobjects; 260 int wantobjects;
245 int threaded; /* True if tcl_platform[threaded] */ 261 int threaded; /* True if tcl_platform[threaded] */
246 Tcl_ThreadId thread_id; 262 Tcl_ThreadId thread_id;
247 int dispatching; 263 int dispatching;
248 /* We cannot include tclInt.h, as this is internal. 264 /* We cannot include tclInt.h, as this is internal.
249 So we cache interesting types here. */ 265 So we cache interesting types here. */
250 Tcl_ObjType *BooleanType; 266 const Tcl_ObjType *OldBooleanType;
251 Tcl_ObjType *ByteArrayType; 267 const Tcl_ObjType *BooleanType;
252 Tcl_ObjType *DoubleType; 268 const Tcl_ObjType *ByteArrayType;
253 Tcl_ObjType *IntType; 269 const Tcl_ObjType *DoubleType;
254 Tcl_ObjType *WideIntType; 270 const Tcl_ObjType *IntType;
255 Tcl_ObjType *BignumType; 271 const Tcl_ObjType *WideIntType;
256 Tcl_ObjType *ListType; 272 const Tcl_ObjType *BignumType;
257 Tcl_ObjType *ProcBodyType; 273 const Tcl_ObjType *ListType;
258 Tcl_ObjType *StringType; 274 const Tcl_ObjType *ProcBodyType;
275 const Tcl_ObjType *StringType;
259 } TkappObject; 276 } TkappObject;
260 277
278 #define Tkapp_Check(v) (Py_TYPE(v) == &Tkapp_Type)
261 #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) 279 #define Tkapp_Interp(v) (((TkappObject *) (v))->interp)
262 #define Tkapp_Result(v) Tcl_GetStringResult(Tkapp_Interp(v)) 280 #define Tkapp_Result(v) Tcl_GetStringResult(Tkapp_Interp(v))
263 281
264 #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \ 282 #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \
265 (void *) v, Py_REFCNT(v))) 283 (void *) v, Py_REFCNT(v)))
266 284
267 285
268 286
269 /**** Error Handling ****/ 287 /**** Error Handling ****/
270 288
271 static PyObject *Tkinter_TclError; 289 static PyObject *Tkinter_TclError;
272 static int quitMainLoop = 0; 290 static int quitMainLoop = 0;
273 static int errorInCmd = 0; 291 static int errorInCmd = 0;
274 static PyObject *excInCmd; 292 static PyObject *excInCmd;
275 static PyObject *valInCmd; 293 static PyObject *valInCmd;
276 static PyObject *trbInCmd; 294 static PyObject *trbInCmd;
277 295
278 #ifdef TKINTER_PROTECT_LOADTK 296 #ifdef TKINTER_PROTECT_LOADTK
279 static int tk_load_failed = 0; 297 static int tk_load_failed;
280 #endif 298 #endif
281 299
282 300
283 static PyObject * 301 static PyObject *
284 Tkinter_Error(PyObject *v) 302 Tkinter_Error(PyObject *v)
285 { 303 {
286 PyErr_SetString(Tkinter_TclError, Tkapp_Result(v)); 304 PyErr_SetString(Tkinter_TclError, Tkapp_Result(v));
287 return NULL; 305 return NULL;
288 } 306 }
289 307
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 Py_END_ALLOW_THREADS 341 Py_END_ALLOW_THREADS
324 } 342 }
325 if (self->dispatching) 343 if (self->dispatching)
326 return 1; 344 return 1;
327 PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop"); 345 PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop");
328 return 0; 346 return 0;
329 } 347 }
330 #endif /* WITH_THREAD */ 348 #endif /* WITH_THREAD */
331 349
332 350
351 static char *
352 AsString(PyObject *value, PyObject *tmp)
353 {
354 if (PyString_Check(value))
355 return PyString_AsString(value);
356 #ifdef Py_USING_UNICODE
357 else if (PyUnicode_Check(value)) {
358 PyObject *v = PyUnicode_AsUTF8String(value);
359 if (v == NULL)
360 return NULL;
361 if (PyList_Append(tmp, v) != 0) {
362 Py_DECREF(v);
363 return NULL;
364 }
365 Py_DECREF(v);
366 return PyString_AsString(v);
367 }
368 #endif
369 else {
370 PyObject *v = PyObject_Str(value);
371 if (v == NULL)
372 return NULL;
373 if (PyList_Append(tmp, v) != 0) {
374 Py_DECREF(v);
375 return NULL;
376 }
377 Py_DECREF(v);
378 return PyString_AsString(v);
379 }
380 }
381
382
333 383
334 #define ARGSZ 64 384 #define ARGSZ 64
335 385
336 386 static char *
337 387 Merge(PyObject *args)
338 static PyObject * 388 {
339 unicodeFromTclStringAndSize(const char *s, Py_ssize_t size) 389 PyObject *tmp = NULL;
390 char *argvStore[ARGSZ];
391 char **argv = NULL;
392 int fvStore[ARGSZ];
393 int *fv = NULL;
394 Py_ssize_t argc = 0, fvc = 0, i;
395 char *res = NULL;
396
397 if (!(tmp = PyList_New(0)))
398 return NULL;
399
400 argv = argvStore;
401 fv = fvStore;
402
403 if (args == NULL)
404 argc = 0;
405
406 else if (!PyTuple_Check(args)) {
407 argc = 1;
408 fv[0] = 0;
409 if (!(argv[0] = AsString(args, tmp)))
410 goto finally;
411 }
412 else {
413 argc = PyTuple_Size(args);
414
415 if (argc > ARGSZ) {
416 if (!CHECK_SIZE(argc, sizeof(char *))) {
417 PyErr_SetString(PyExc_OverflowError, "tuple is too long");
418 goto finally;
419 }
420 argv = (char **)attemptckalloc((size_t)argc * sizeof(char *));
421 fv = (int *)attemptckalloc((size_t)argc * sizeof(int));
422 if (argv == NULL || fv == NULL) {
423 PyErr_NoMemory();
424 goto finally;
425 }
426 }
427
428 for (i = 0; i < argc; i++) {
429 PyObject *v = PyTuple_GetItem(args, i);
430 if (PyTuple_Check(v)) {
431 fv[i] = 1;
432 if (!(argv[i] = Merge(v)))
433 goto finally;
434 fvc++;
435 }
436 else if (v == Py_None) {
437 argc = i;
438 break;
439 }
440 else {
441 fv[i] = 0;
442 if (!(argv[i] = AsString(v, tmp)))
443 goto finally;
444 fvc++;
445 }
446 }
447 }
448 res = Tcl_Merge(argc, argv);
449 if (res == NULL)
450 PyErr_SetString(Tkinter_TclError, "merge failed");
451
452 finally:
453 for (i = 0; i < fvc; i++)
454 if (fv[i]) {
455 ckfree(argv[i]);
456 }
457 if (argv != argvStore)
458 ckfree(FREECAST argv);
459 if (fv != fvStore)
460 ckfree(FREECAST fv);
461
462 Py_DECREF(tmp);
463 return res;
464 }
465
466
467
468 #ifdef Py_USING_UNICODE
469 static PyObject *
470 unicode_FromTclStringAndSize(const char *s, Py_ssize_t size)
340 { 471 {
341 PyObject *r = PyUnicode_DecodeUTF8(s, size, NULL); 472 PyObject *r = PyUnicode_DecodeUTF8(s, size, NULL);
342 if (!r && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { 473 if (!r && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
343 /* Tcl encodes null character as \xc0\x80 */ 474 /* Tcl encodes null character as \xc0\x80 */
344 if (memchr(s, '\xc0', size)) { 475 if (memchr(s, '\xc0', size)) {
345 char *buf, *q; 476 char *buf, *q;
346 const char *e = s + size; 477 const char *e = s + size;
347 PyErr_Clear(); 478 PyErr_Clear();
348 q = buf = (char *)PyMem_Malloc(size); 479 q = buf = (char *)PyMem_Malloc(size);
349 if (buf == NULL) 480 if (buf == NULL) {
481 PyErr_NoMemory();
350 return NULL; 482 return NULL;
483 }
351 while (s != e) { 484 while (s != e) {
352 if (s + 1 != e && s[0] == '\xc0' && s[1] == '\x80') { 485 if (s + 1 != e && s[0] == '\xc0' && s[1] == '\x80') {
353 *q++ = '\0'; 486 *q++ = '\0';
354 s += 2; 487 s += 2;
355 } 488 }
356 else 489 else
357 *q++ = *s++; 490 *q++ = *s++;
358 } 491 }
359 s = buf; 492 s = buf;
360 size = q - s; 493 size = q - s;
361 r = PyUnicode_DecodeUTF8(s, size, NULL); 494 r = PyUnicode_DecodeUTF8(s, size, NULL);
362 PyMem_Free(buf); 495 PyMem_Free(buf);
363 } 496 }
364 } 497 }
365 return r; 498 return r;
366 } 499 }
367 500 #endif
368 static PyObject * 501
369 unicodeFromTclString(const char *s) 502 static PyObject *
370 { 503 fromTclStringAndSize(const char *s, Py_ssize_t size)
371 return unicodeFromTclStringAndSize(s, strlen(s)); 504 {
372 } 505 PyObject *r;
373 506 #ifdef Py_USING_UNICODE
374 static PyObject * 507 Py_ssize_t i;
375 unicodeFromTclObj(Tcl_Obj *value) 508 /* If Tcl string contains any bytes with the top bit set,
376 { 509 it's UTF-8 and we should decode it to Unicode */
377 int len; 510 for (i = 0; i < size; i++)
378 char *s = Tcl_GetStringFromObj(value, &len); 511 if (s[i] & 0x80)
379 return unicodeFromTclStringAndSize(s, len); 512 break;
513 if (i != size) {
514 /* It isn't an ASCII string. */
515 r = unicode_FromTclStringAndSize(s, size);
516 if (r)
517 return r;
518 PyErr_Clear();
519 }
520 #endif
521 r = PyString_FromStringAndSize(s, size);
522 return r;
523 }
524
525 static PyObject *
526 fromTclString(const char *s)
527 {
528 return fromTclStringAndSize(s, strlen(s));
380 } 529 }
381 530
382 531
383 static PyObject * 532 static PyObject *
384 Split(char *list) 533 Split(char *list)
385 { 534 {
386 int argc; 535 int argc;
387 char **argv; 536 char **argv;
388 PyObject *v; 537 PyObject *v;
389 538
390 if (list == NULL) { 539 if (list == NULL) {
391 Py_RETURN_NONE; 540 Py_INCREF(Py_None);
541 return Py_None;
392 } 542 }
393 543
394 if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { 544 if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
395 /* Not a list. 545 /* Not a list.
396 * Could be a quoted string containing funnies, e.g. {"}. 546 * Could be a quoted string containing funnies, e.g. {"}.
397 * Return the string itself. 547 * Return the string itself.
398 */ 548 */
399 return unicodeFromTclString(list); 549 return PyString_FromString(list);
400 } 550 }
401 551
402 if (argc == 0) 552 if (argc == 0)
403 v = PyUnicode_FromString(""); 553 v = PyString_FromString("");
404 else if (argc == 1) 554 else if (argc == 1)
405 v = unicodeFromTclString(argv[0]); 555 v = PyString_FromString(argv[0]);
406 else if ((v = PyTuple_New(argc)) != NULL) { 556 else if ((v = PyTuple_New(argc)) != NULL) {
407 int i; 557 int i;
408 PyObject *w; 558 PyObject *w;
409 559
410 for (i = 0; i < argc; i++) { 560 for (i = 0; i < argc; i++) {
411 if ((w = Split(argv[i])) == NULL) { 561 if ((w = Split(argv[i])) == NULL) {
412 Py_DECREF(v); 562 Py_DECREF(v);
413 v = NULL; 563 v = NULL;
414 break; 564 break;
415 } 565 }
416 PyTuple_SetItem(v, i, w); 566 PyTuple_SetItem(v, i, w);
417 } 567 }
418 } 568 }
419 Tcl_Free(FREECAST argv); 569 Tcl_Free(FREECAST argv);
420 return v; 570 return v;
421 } 571 }
422 572
423 /* In some cases, Tcl will still return strings that are supposed to 573 /* In some cases, Tcl will still return strings that are supposed to be
424 be lists. SplitObj walks through a nested tuple, finding string 574 lists. SplitObj walks through a nested tuple, finding string objects that
425 objects that need to be split. */ 575 need to be split. */
426 576
427 static PyObject * 577 static PyObject *
428 SplitObj(PyObject *arg) 578 SplitObj(PyObject *arg)
429 { 579 {
430 if (PyTuple_Check(arg)) { 580 if (PyTuple_Check(arg)) {
431 int i, size; 581 int i, size;
432 PyObject *elem, *newelem, *result; 582 PyObject *elem, *newelem, *result;
433 583
434 size = PyTuple_Size(arg); 584 size = PyTuple_Size(arg);
435 result = NULL; 585 result = NULL;
(...skipping 21 matching lines...) Expand all
457 Py_INCREF(elem); 607 Py_INCREF(elem);
458 PyTuple_SetItem(result, k, elem); 608 PyTuple_SetItem(result, k, elem);
459 } 609 }
460 } 610 }
461 PyTuple_SetItem(result, i, newelem); 611 PyTuple_SetItem(result, i, newelem);
462 } 612 }
463 if (result) 613 if (result)
464 return result; 614 return result;
465 /* Fall through, returning arg. */ 615 /* Fall through, returning arg. */
466 } 616 }
617 else if (PyString_Check(arg)) {
618 int argc;
619 char **argv;
620 char *list = PyString_AsString(arg);
621
622 if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
623 Py_INCREF(arg);
624 return arg;
625 }
626 Tcl_Free(FREECAST argv);
627 if (argc > 1)
628 return Split(PyString_AsString(arg));
629 /* Fall through, returning arg. */
630 }
467 else if (PyUnicode_Check(arg)) { 631 else if (PyUnicode_Check(arg)) {
468 int argc; 632 int argc;
469 char **argv; 633 char **argv;
470 char *list = PyUnicode_AsUTF8(arg); 634 char *list;
635 PyObject *s = PyUnicode_AsUTF8String(arg);
636
637 if (s == NULL) {
638 Py_INCREF(arg);
639 return arg;
640 }
641 list = PyString_AsString(s);
471 642
472 if (list == NULL || 643 if (list == NULL ||
473 Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { 644 Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
645 Py_DECREF(s);
474 Py_INCREF(arg); 646 Py_INCREF(arg);
475 return arg; 647 return arg;
476 } 648 }
477 Tcl_Free(FREECAST argv); 649 Tcl_Free(FREECAST argv);
478 if (argc > 1) 650 if (argc > 1) {
479 return Split(list); 651 PyObject *v = Split(list);
480 /* Fall through, returning arg. */ 652 Py_DECREF(s);
481 } 653 return v;
482 else if (PyBytes_Check(arg)) { 654 }
483 int argc; 655 Py_DECREF(s);
484 char **argv;
485 char *list = PyBytes_AsString(arg);
486
487 if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
488 Py_INCREF(arg);
489 return arg;
490 }
491 Tcl_Free(FREECAST argv);
492 if (argc > 1)
493 return Split(PyBytes_AsString(arg));
494 /* Fall through, returning arg. */ 656 /* Fall through, returning arg. */
495 } 657 }
496 Py_INCREF(arg); 658 Py_INCREF(arg);
497 return arg; 659 return arg;
498 } 660 }
499 661
500 662
501 /**** Tkapp Object ****/ 663 /**** Tkapp Object ****/
502 664
503 #ifndef WITH_APPINIT 665 #ifndef WITH_APPINIT
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 703
542 704
543 /* Initialize the Tk application; see the `main' function in 705 /* Initialize the Tk application; see the `main' function in
544 * `tkMain.c'. 706 * `tkMain.c'.
545 */ 707 */
546 708
547 static void EnableEventHook(void); /* Forward */ 709 static void EnableEventHook(void); /* Forward */
548 static void DisableEventHook(void); /* Forward */ 710 static void DisableEventHook(void); /* Forward */
549 711
550 static TkappObject * 712 static TkappObject *
551 Tkapp_New(char *screenName, char *className, 713 Tkapp_New(char *screenName, char *baseName, char *className,
552 int interactive, int wantobjects, int wantTk, int sync, char *use) 714 int interactive, int wantobjects, int wantTk, int sync, char *use)
553 { 715 {
554 TkappObject *v; 716 TkappObject *v;
555 char *argv0; 717 char *argv0;
556 718
557 v = PyObject_New(TkappObject, (PyTypeObject *) Tkapp_Type); 719 v = PyObject_New(TkappObject, &Tkapp_Type);
558 if (v == NULL) 720 if (v == NULL)
559 return NULL; 721 return NULL;
560 Py_INCREF(Tkapp_Type);
561 722
562 v->interp = Tcl_CreateInterp(); 723 v->interp = Tcl_CreateInterp();
563 v->wantobjects = wantobjects; 724 v->wantobjects = wantobjects;
564 v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded", 725 v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded",
565 TCL_GLOBAL_ONLY) != NULL; 726 TCL_GLOBAL_ONLY) != NULL;
566 v->thread_id = Tcl_GetCurrentThread(); 727 v->thread_id = Tcl_GetCurrentThread();
567 v->dispatching = 0; 728 v->dispatching = 0;
568 729
569 #ifndef TCL_THREADS 730 #ifndef TCL_THREADS
570 if (v->threaded) { 731 if (v->threaded) {
571 PyErr_SetString(PyExc_RuntimeError, 732 PyErr_SetString(PyExc_RuntimeError, "Tcl is threaded but _tkinter is not ");
572 "Tcl is threaded but _tkinter is not");
573 Py_DECREF(v); 733 Py_DECREF(v);
574 return 0; 734 return 0;
575 } 735 }
576 #endif 736 #endif
577 #ifdef WITH_THREAD 737 #ifdef WITH_THREAD
578 if (v->threaded && tcl_lock) { 738 if (v->threaded && tcl_lock) {
579 /* If Tcl is threaded, we don't need the lock. */ 739 /* If Tcl is threaded, we don't need the lock. */
580 PyThread_free_lock(tcl_lock); 740 PyThread_free_lock(tcl_lock);
581 tcl_lock = NULL; 741 tcl_lock = NULL;
582 } 742 }
583 #endif 743 #endif
584 744
585 v->BooleanType = Tcl_GetObjType("boolean"); 745 v->OldBooleanType = Tcl_GetObjType("boolean");
746 v->BooleanType = Tcl_GetObjType("booleanString");
586 v->ByteArrayType = Tcl_GetObjType("bytearray"); 747 v->ByteArrayType = Tcl_GetObjType("bytearray");
587 v->DoubleType = Tcl_GetObjType("double"); 748 v->DoubleType = Tcl_GetObjType("double");
588 v->IntType = Tcl_GetObjType("int"); 749 v->IntType = Tcl_GetObjType("int");
589 v->WideIntType = Tcl_GetObjType("wideInt"); 750 v->WideIntType = Tcl_GetObjType("wideInt");
590 v->BignumType = Tcl_GetObjType("bignum"); 751 v->BignumType = Tcl_GetObjType("bignum");
591 v->ListType = Tcl_GetObjType("list"); 752 v->ListType = Tcl_GetObjType("list");
592 v->ProcBodyType = Tcl_GetObjType("procbody"); 753 v->ProcBodyType = Tcl_GetObjType("procbody");
593 v->StringType = Tcl_GetObjType("string"); 754 v->StringType = Tcl_GetObjType("string");
594 755
595 /* Delete the 'exit' command, which can screw things up */ 756 /* Delete the 'exit' command, which can screw things up */
596 Tcl_DeleteCommand(v->interp, "exit"); 757 Tcl_DeleteCommand(v->interp, "exit");
597 758
598 if (screenName != NULL) 759 if (screenName != NULL)
599 Tcl_SetVar2(v->interp, "env", "DISPLAY", 760 Tcl_SetVar2(v->interp, "env", "DISPLAY",
600 screenName, TCL_GLOBAL_ONLY); 761 screenName, TCL_GLOBAL_ONLY);
601 762
602 if (interactive) 763 if (interactive)
603 Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); 764 Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
604 else 765 else
605 Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); 766 Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
606 767
607 /* This is used to get the application class for Tk 4.1 and up */ 768 /* This is used to get the application class for Tk 4.1 and up */
608 argv0 = (char*)ckalloc(strlen(className) + 1); 769 argv0 = (char*)attemptckalloc(strlen(className) + 1);
609 if (!argv0) { 770 if (!argv0) {
610 PyErr_NoMemory(); 771 PyErr_NoMemory();
611 Py_DECREF(v); 772 Py_DECREF(v);
612 return NULL; 773 return NULL;
613 } 774 }
614 775
615 strcpy(argv0, className); 776 strcpy(argv0, className);
616 if (Py_ISUPPER(Py_CHARMASK(argv0[0]))) 777 if (Py_ISUPPER(Py_CHARMASK(argv0[0])))
617 argv0[0] = Py_TOLOWER(Py_CHARMASK(argv0[0])); 778 argv0[0] = Py_TOLOWER(Py_CHARMASK(argv0[0]));
618 Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); 779 Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
(...skipping 13 matching lines...) Expand all
632 /* some initial arguments need to be in argv */ 793 /* some initial arguments need to be in argv */
633 if (sync || use) { 794 if (sync || use) {
634 char *args; 795 char *args;
635 int len = 0; 796 int len = 0;
636 797
637 if (sync) 798 if (sync)
638 len += sizeof "-sync"; 799 len += sizeof "-sync";
639 if (use) 800 if (use)
640 len += strlen(use) + sizeof "-use "; 801 len += strlen(use) + sizeof "-use ";
641 802
642 args = (char*)ckalloc(len); 803 args = (char*)attemptckalloc(len);
643 if (!args) { 804 if (!args) {
644 PyErr_NoMemory(); 805 PyErr_NoMemory();
645 Py_DECREF(v); 806 Py_DECREF(v);
646 return NULL; 807 return NULL;
647 } 808 }
648 809
649 args[0] = '\0'; 810 args[0] = '\0';
650 if (sync) 811 if (sync)
651 strcat(args, "-sync"); 812 strcat(args, "-sync");
652 if (use) { 813 if (use) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 862
702 863
703 /** Tcl Eval **/ 864 /** Tcl Eval **/
704 865
705 typedef struct { 866 typedef struct {
706 PyObject_HEAD 867 PyObject_HEAD
707 Tcl_Obj *value; 868 Tcl_Obj *value;
708 PyObject *string; /* This cannot cause cycles. */ 869 PyObject *string; /* This cannot cause cycles. */
709 } PyTclObject; 870 } PyTclObject;
710 871
711 static PyObject *PyTclObject_Type; 872 staticforward PyTypeObject PyTclObject_Type;
712 #define PyTclObject_Check(v) ((v)->ob_type == (PyTypeObject *) PyTclObject_Type) 873 #define PyTclObject_Check(v) ((v)->ob_type == &PyTclObject_Type)
713 874
714 static PyObject * 875 static PyObject *
715 newPyTclObject(Tcl_Obj *arg) 876 newPyTclObject(Tcl_Obj *arg)
716 { 877 {
717 PyTclObject *self; 878 PyTclObject *self;
718 self = PyObject_New(PyTclObject, (PyTypeObject *) PyTclObject_Type); 879 self = PyObject_New(PyTclObject, &PyTclObject_Type);
719 if (self == NULL) 880 if (self == NULL)
720 return NULL; 881 return NULL;
721 Py_INCREF(PyTclObject_Type);
722 Tcl_IncrRefCount(arg); 882 Tcl_IncrRefCount(arg);
723 self->value = arg; 883 self->value = arg;
724 self->string = NULL; 884 self->string = NULL;
725 return (PyObject*)self; 885 return (PyObject*)self;
726 } 886 }
727 887
728 static void 888 static void
729 PyTclObject_dealloc(PyTclObject *self) 889 PyTclObject_dealloc(PyTclObject *self)
730 { 890 {
731 PyObject *tp = (PyObject *) Py_TYPE(self);
732 Tcl_DecrRefCount(self->value); 891 Tcl_DecrRefCount(self->value);
733 Py_XDECREF(self->string); 892 Py_XDECREF(self->string);
734 PyObject_Del(self); 893 PyObject_Del(self);
735 Py_DECREF(tp); 894 }
895
896 static PyObject *
897 PyTclObject_str(PyTclObject *self)
898 {
899 if (self->string && PyString_Check(self->string)) {
900 Py_INCREF(self->string);
901 return self->string;
902 }
903 /* XXX Could cache value if it is an ASCII string. */
904 return PyString_FromString(Tcl_GetString(self->value));
736 } 905 }
737 906
738 static char* 907 static char*
739 PyTclObject_TclString(PyObject *self) 908 PyTclObject_TclString(PyObject *self)
740 { 909 {
741 return Tcl_GetString(((PyTclObject*)self)->value); 910 return Tcl_GetString(((PyTclObject*)self)->value);
742 } 911 }
743 912
744 /* Like _str, but create Unicode if necessary. */ 913 /* Like _str, but create Unicode if necessary. */
745 PyDoc_STRVAR(PyTclObject_string__doc__, 914 PyDoc_STRVAR(PyTclObject_string__doc__,
746 "the string representation of this object, either as str or bytes"); 915 "the string representation of this object, either as string or Unicode");
747 916
748 static PyObject * 917 static PyObject *
749 PyTclObject_string(PyTclObject *self, void *ignored) 918 PyTclObject_string(PyTclObject *self, void *ignored)
750 { 919 {
751 if (!self->string) { 920 if (!self->string) {
752 self->string = unicodeFromTclObj(self->value); 921 int len;
922 char *s = Tcl_GetStringFromObj(self->value, &len);
923 self->string = fromTclStringAndSize(s, len);
753 if (!self->string) 924 if (!self->string)
754 return NULL; 925 return NULL;
755 } 926 }
756 Py_INCREF(self->string); 927 Py_INCREF(self->string);
757 return self->string; 928 return self->string;
758 } 929 }
759 930
760 static PyObject * 931 #ifdef Py_USING_UNICODE
761 PyTclObject_str(PyTclObject *self, void *ignored) 932 PyDoc_STRVAR(PyTclObject_unicode__doc__, "convert argument to unicode");
762 { 933
763 if (self->string) { 934 static PyObject *
935 PyTclObject_unicode(PyTclObject *self, void *ignored)
936 {
937 char *s;
938 int len;
939 if (self->string && PyUnicode_Check(self->string)) {
764 Py_INCREF(self->string); 940 Py_INCREF(self->string);
765 return self->string; 941 return self->string;
766 } 942 }
767 /* XXX Could chache result if it is non-ASCII. */ 943 /* XXX Could chache result if it is non-ASCII. */
768 return unicodeFromTclObj(self->value); 944 s = Tcl_GetStringFromObj(self->value, &len);
769 } 945 return unicode_FromTclStringAndSize(s, len);
946 }
947 #endif
770 948
771 static PyObject * 949 static PyObject *
772 PyTclObject_repr(PyTclObject *self) 950 PyTclObject_repr(PyTclObject *self)
773 { 951 {
774 PyObject *repr, *str = PyTclObject_str(self, NULL); 952 char buf[50];
775 if (str == NULL) 953 PyOS_snprintf(buf, 50, "<%s object at %p>",
776 return NULL; 954 self->value->typePtr->name, self->value);
777 repr = PyUnicode_FromFormat("<%s object: %R>", 955 return PyString_FromString(buf);
778 self->value->typePtr->name, str); 956 }
779 Py_DECREF(str); 957
780 return repr; 958 static int
781 } 959 PyTclObject_cmp(PyTclObject *self, PyTclObject *other)
782 960 {
783 #define TEST_COND(cond) ((cond) ? Py_True : Py_False) 961 int res;
784 962 res = strcmp(Tcl_GetString(self->value),
785 static PyObject * 963 Tcl_GetString(other->value));
786 PyTclObject_richcompare(PyObject *self, PyObject *other, int op) 964 if (res < 0) return -1;
787 { 965 if (res > 0) return 1;
788 int result; 966 return 0;
789 PyObject *v;
790
791 /* neither argument should be NULL, unless something's gone wrong */
792 if (self == NULL || other == NULL) {
793 PyErr_BadInternalCall();
794 return NULL;
795 }
796
797 /* both arguments should be instances of PyTclObject */
798 if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
799 v = Py_NotImplemented;
800 goto finished;
801 }
802
803 if (self == other)
804 /* fast path when self and other are identical */
805 result = 0;
806 else
807 result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
808 Tcl_GetString(((PyTclObject *)other)->value));
809 /* Convert return value to a Boolean */
810 switch (op) {
811 case Py_EQ:
812 v = TEST_COND(result == 0);
813 break;
814 case Py_NE:
815 v = TEST_COND(result != 0);
816 break;
817 case Py_LE:
818 v = TEST_COND(result <= 0);
819 break;
820 case Py_GE:
821 v = TEST_COND(result >= 0);
822 break;
823 case Py_LT:
824 v = TEST_COND(result < 0);
825 break;
826 case Py_GT:
827 v = TEST_COND(result > 0);
828 break;
829 default:
830 PyErr_BadArgument();
831 return NULL;
832 }
833 finished:
834 Py_INCREF(v);
835 return v;
836 } 967 }
837 968
838 PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type"); 969 PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
839 970
840 static PyObject* 971 static PyObject*
841 get_typename(PyTclObject* obj, void* ignored) 972 get_typename(PyTclObject* obj, void* ignored)
842 { 973 {
843 return unicodeFromTclString(obj->value->typePtr->name); 974 return PyString_FromString(obj->value->typePtr->name);
844 } 975 }
845 976
846 977
847 static PyGetSetDef PyTclObject_getsetlist[] = { 978 static PyGetSetDef PyTclObject_getsetlist[] = {
848 {"typename", (getter)get_typename, NULL, get_typename__doc__}, 979 {"typename", (getter)get_typename, NULL, get_typename__doc__},
849 {"string", (getter)PyTclObject_string, NULL, 980 {"string", (getter)PyTclObject_string, NULL,
850 PyTclObject_string__doc__}, 981 PyTclObject_string__doc__},
851 {0}, 982 {0},
852 }; 983 };
853 984
854 static PyType_Slot PyTclObject_Type_slots[] = { 985 static PyMethodDef PyTclObject_methods[] = {
855 {Py_tp_dealloc, (destructor)PyTclObject_dealloc}, 986 #ifdef Py_USING_UNICODE
856 {Py_tp_repr, (reprfunc)PyTclObject_repr}, 987 {"__unicode__", (PyCFunction)PyTclObject_unicode, METH_NOARGS,
857 {Py_tp_str, (reprfunc)PyTclObject_str}, 988 PyTclObject_unicode__doc__},
858 {Py_tp_getattro, PyObject_GenericGetAttr}, 989 #endif
859 {Py_tp_richcompare, PyTclObject_richcompare}, 990 {0}
860 {Py_tp_getset, PyTclObject_getsetlist},
861 {0, 0}
862 }; 991 };
863 992
864 static PyType_Spec PyTclObject_Type_spec = { 993 statichere PyTypeObject PyTclObject_Type = {
865 "_tkinter.Tcl_Obj", 994 PyObject_HEAD_INIT(NULL)
866 sizeof(PyTclObject), 995 0, /*ob_size*/
867 0, 996 "_tkinter.Tcl_Obj", /*tp_name*/
868 Py_TPFLAGS_DEFAULT, 997 sizeof(PyTclObject), /*tp_basicsize*/
869 PyTclObject_Type_slots, 998 0, /*tp_itemsize*/
999 /* methods */
1000 (destructor)PyTclObject_dealloc, /*tp_dealloc*/
1001 0, /*tp_print*/
1002 0, /*tp_getattr*/
1003 0, /*tp_setattr*/
1004 (cmpfunc)PyTclObject_cmp, /*tp_compare*/
1005 (reprfunc)PyTclObject_repr, /*tp_repr*/
1006 0, /*tp_as_number*/
1007 0, /*tp_as_sequence*/
1008 0, /*tp_as_mapping*/
1009 0, /*tp_hash*/
1010 0, /*tp_call*/
1011 (reprfunc)PyTclObject_str, /*tp_str*/
1012 PyObject_GenericGetAttr,/*tp_getattro*/
1013 0, /*tp_setattro*/
1014 0, /*tp_as_buffer*/
1015 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1016 0, /*tp_doc*/
1017 0, /*tp_traverse*/
1018 0, /*tp_clear*/
1019 0, /*tp_richcompare*/
1020 0, /*tp_weaklistoffset*/
1021 0, /*tp_iter*/
1022 0, /*tp_iternext*/
1023 PyTclObject_methods, /*tp_methods*/
1024 0, /*tp_members*/
1025 PyTclObject_getsetlist, /*tp_getset*/
1026 0, /*tp_base*/
1027 0, /*tp_dict*/
1028 0, /*tp_descr_get*/
1029 0, /*tp_descr_set*/
1030 0, /*tp_dictoffset*/
1031 0, /*tp_init*/
1032 0, /*tp_alloc*/
1033 0, /*tp_new*/
1034 0, /*tp_free*/
1035 0, /*tp_is_gc*/
870 }; 1036 };
871 1037
1038 #if PY_SIZE_MAX > INT_MAX
1039 #define CHECK_STRING_LENGTH(s) do { \
1040 if (s != NULL && strlen(s) >= INT_MAX) { \
1041 PyErr_SetString(PyExc_OverflowError, "string is too long"); \
1042 return NULL; \
1043 } } while(0)
1044 #else
1045 #define CHECK_STRING_LENGTH(s)
1046 #endif
1047
1048 #ifdef HAVE_LIBTOMMAMTH
1049 static Tcl_Obj*
1050 asBignumObj(PyObject *value)
1051 {
1052 Tcl_Obj *result;
1053 int neg;
1054 PyObject *hexstr;
1055 char *hexchars;
1056 mp_int bigValue;
1057
1058 neg = Py_SIZE(value) < 0;
1059 hexstr = _PyLong_Format(value, 16, 0, 1);
1060 if (hexstr == NULL)
1061 return NULL;
1062 hexchars = PyString_AsString(hexstr);
1063 if (hexchars == NULL) {
1064 Py_DECREF(hexstr);
1065 return NULL;
1066 }
1067 hexchars += neg + 2; /* skip sign and "0x" */
1068 mp_init(&bigValue);
1069 if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) {
1070 mp_clear(&bigValue);
1071 Py_DECREF(hexstr);
1072 PyErr_NoMemory();
1073 return NULL;
1074 }
1075 Py_DECREF(hexstr);
1076 bigValue.sign = neg ? MP_NEG : MP_ZPOS;
1077 result = Tcl_NewBignumObj(&bigValue);
1078 mp_clear(&bigValue);
1079 if (result == NULL) {
1080 PyErr_NoMemory();
1081 return NULL;
1082 }
1083 return result;
1084 }
1085 #endif
872 1086
873 static Tcl_Obj* 1087 static Tcl_Obj*
874 AsObj(PyObject *value) 1088 AsObj(PyObject *value)
875 { 1089 {
876 Tcl_Obj *result; 1090 Tcl_Obj *result;
877 1091
878 if (PyBytes_Check(value)) 1092 if (PyString_Check(value))
879 return Tcl_NewStringObj(PyBytes_AS_STRING(value), 1093 return Tcl_NewStringObj(PyString_AS_STRING(value),
880 PyBytes_GET_SIZE(value)); 1094 PyString_GET_SIZE(value));
1095
881 if (PyBool_Check(value)) 1096 if (PyBool_Check(value))
882 return Tcl_NewBooleanObj(PyObject_IsTrue(value)); 1097 return Tcl_NewBooleanObj(PyObject_IsTrue(value));
1098
1099 if (PyInt_Check(value))
1100 return Tcl_NewLongObj(PyInt_AS_LONG(value));
883 1101
884 if (PyLong_CheckExact(value)) { 1102 if (PyLong_CheckExact(value)) {
885 int overflow; 1103 int overflow;
886 long longValue; 1104 long longValue;
887 #ifdef HAVE_LONG_LONG 1105 #ifdef TCL_WIDE_INT_TYPE
888 PY_LONG_LONG wideValue; 1106 Tcl_WideInt wideValue;
889 #endif 1107 #endif
890 #ifdef HAVE_LIBTOMMAMTH
891 int neg;
892 PyObject *hexstr;
893 char *hexchars;
894 mp_int bigValue;
895 #endif
896
897 longValue = PyLong_AsLongAndOverflow(value, &overflow); 1108 longValue = PyLong_AsLongAndOverflow(value, &overflow);
898 if (!overflow) { 1109 if (!overflow) {
899 /* If there is an overflow in the long conversion,
900 fall through to wideInt handling. */
901 return Tcl_NewLongObj(longValue); 1110 return Tcl_NewLongObj(longValue);
902 } 1111 }
903 1112 /* If there is an overflow in the long conversion,
904 #ifdef HAVE_LONG_LONG 1113 fall through to wideInt handling. */
905 wideValue = PyLong_AsLongLongAndOverflow(value, &overflow); 1114 #ifdef TCL_WIDE_INT_TYPE
906 if (!overflow) { 1115 if (_PyLong_AsByteArray((PyLongObject *)value,
907 /* If there is an overflow in the wideInt conversion, 1116 (unsigned char *)(void *)&wideValue,
908 fall through to bignum handling. */ 1117 sizeof(wideValue),
1118 #ifdef WORDS_BIGENDIAN
1119 0,
1120 #else
1121 1,
1122 #endif
1123 /* signed */ 1) == 0) {
909 return Tcl_NewWideIntObj(wideValue); 1124 return Tcl_NewWideIntObj(wideValue);
910 } 1125 }
911 #endif 1126 PyErr_Clear();
912 1127 #endif
1128 /* If there is an overflow in the wideInt conversion,
1129 fall through to bignum handling. */
913 #ifdef HAVE_LIBTOMMAMTH 1130 #ifdef HAVE_LIBTOMMAMTH
914 neg = Py_SIZE(value) < 0; 1131 return asBignumObj(value);
915 hexstr = _PyLong_Format(value, 16); 1132 #endif
916 if (hexstr == NULL) 1133 /* If there is no wideInt or bignum support,
917 return NULL; 1134 fall through to default object handling. */
918 hexchars = PyUnicode_AsUTF8(hexstr);
919 if (hexchars == NULL) {
920 Py_DECREF(hexstr);
921 return NULL;
922 }
923 hexchars += neg + 2; /* skip sign and "0x" */
924 mp_init(&bigValue);
925 if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) {
926 mp_clear(&bigValue);
927 Py_DECREF(hexstr);
928 PyErr_NoMemory();
929 return NULL;
930 }
931 Py_DECREF(hexstr);
932 bigValue.sign = neg ? MP_NEG : MP_ZPOS;
933 result = Tcl_NewBignumObj(&bigValue);
934 mp_clear(&bigValue);
935 if (result == NULL) {
936 PyErr_NoMemory();
937 return NULL;
938 }
939 return result;
940 #endif
941 } 1135 }
942 1136
943 if (PyFloat_Check(value)) 1137 if (PyFloat_Check(value))
944 return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); 1138 return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value));
1139
945 if (PyTuple_Check(value)) { 1140 if (PyTuple_Check(value)) {
946 Tcl_Obj **argv; 1141 Tcl_Obj **argv;
947 Py_ssize_t size, i; 1142 Py_ssize_t size, i;
948 1143
949 size = PyTuple_Size(value); 1144 size = PyTuple_Size(value);
1145 if (size == 0)
1146 return Tcl_NewListObj(0, NULL);
950 if (!CHECK_SIZE(size, sizeof(Tcl_Obj *))) { 1147 if (!CHECK_SIZE(size, sizeof(Tcl_Obj *))) {
951 PyErr_SetString(PyExc_OverflowError, "tuple is too long"); 1148 PyErr_SetString(PyExc_OverflowError, "tuple is too long");
952 return NULL; 1149 return NULL;
953 } 1150 }
954 argv = (Tcl_Obj **) ckalloc(((size_t)size) * sizeof(Tcl_Obj *)); 1151 argv = (Tcl_Obj **) attemptckalloc(((size_t)size) * sizeof(Tcl_Obj *));
955 if(!argv) 1152 if(!argv)
956 return 0; 1153 return 0;
957 for (i = 0; i < size; i++) 1154 for (i = 0; i < size; i++)
958 argv[i] = AsObj(PyTuple_GetItem(value,i)); 1155 argv[i] = AsObj(PyTuple_GetItem(value,i));
959 result = Tcl_NewListObj(PyTuple_Size(value), argv); 1156 result = Tcl_NewListObj(PyTuple_Size(value), argv);
960 ckfree(FREECAST argv); 1157 ckfree(FREECAST argv);
961 return result; 1158 return result;
962 } 1159 }
1160
1161 #ifdef Py_USING_UNICODE
963 if (PyUnicode_Check(value)) { 1162 if (PyUnicode_Check(value)) {
964 void *inbuf; 1163 Py_UNICODE *inbuf = PyUnicode_AS_UNICODE(value);
965 Py_ssize_t size; 1164 Py_ssize_t size = PyUnicode_GET_SIZE(value);
966 int kind; 1165 /* This #ifdef assumes that Tcl uses UCS-2.
1166 See TCL_UTF_MAX test above. */
1167 #if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX == 3
967 Tcl_UniChar *outbuf = NULL; 1168 Tcl_UniChar *outbuf = NULL;
968 Py_ssize_t i; 1169 Py_ssize_t i;
969 size_t allocsize; 1170 size_t allocsize;
970 1171 if (size == 0)
971 if (PyUnicode_READY(value) == -1) 1172 return Tcl_NewUnicodeObj((const void *)"", 0);
972 return NULL;
973
974 inbuf = PyUnicode_DATA(value);
975 size = PyUnicode_GET_LENGTH(value);
976 if (!CHECK_SIZE(size, sizeof(Tcl_UniChar))) { 1173 if (!CHECK_SIZE(size, sizeof(Tcl_UniChar))) {
977 PyErr_SetString(PyExc_OverflowError, "string is too long"); 1174 PyErr_SetString(PyExc_OverflowError, "string is too long");
978 return NULL; 1175 return NULL;
979 } 1176 }
980 kind = PyUnicode_KIND(value); 1177 if (sizeof(Py_UNICODE) == sizeof(Tcl_UniChar))
981 if (kind == sizeof(Tcl_UniChar))
982 return Tcl_NewUnicodeObj(inbuf, size); 1178 return Tcl_NewUnicodeObj(inbuf, size);
983 allocsize = ((size_t)size) * sizeof(Tcl_UniChar); 1179 allocsize = ((size_t)size) * sizeof(Tcl_UniChar);
984 outbuf = (Tcl_UniChar*)ckalloc(allocsize); 1180 if (allocsize >= size)
1181 outbuf = (Tcl_UniChar*)attemptckalloc(allocsize);
985 /* Else overflow occurred, and we take the next exit */ 1182 /* Else overflow occurred, and we take the next exit */
986 if (!outbuf) { 1183 if (!outbuf) {
987 PyErr_NoMemory(); 1184 PyErr_NoMemory();
988 return NULL; 1185 return NULL;
989 } 1186 }
990 for (i = 0; i < size; i++) { 1187 for (i = 0; i < size; i++) {
991 Py_UCS4 ch = PyUnicode_READ(kind, inbuf, i); 1188 if (inbuf[i] >= 0x10000) {
992 /* We cannot test for sizeof(Tcl_UniChar) directly,
993 so we test for UTF-8 size instead. */
994 #if TCL_UTF_MAX == 3
995 if (ch >= 0x10000) {
996 /* Tcl doesn't do UTF-16, yet. */ 1189 /* Tcl doesn't do UTF-16, yet. */
997 PyErr_Format(Tkinter_TclError, 1190 PyErr_Format(Tkinter_TclError,
998 "character U+%x is above the range " 1191 "character U+%x is above the range "
999 "(U+0000-U+FFFF) allowed by Tcl", 1192 "(U+0000-U+FFFF) allowed by Tcl",
1000 ch); 1193 (int)inbuf[i]);
1001 ckfree(FREECAST outbuf); 1194 ckfree(FREECAST outbuf);
1002 return NULL; 1195 return NULL;
1003 } 1196 }
1004 #endif 1197 outbuf[i] = inbuf[i];
1005 outbuf[i] = ch;
1006 } 1198 }
1007 result = Tcl_NewUnicodeObj(outbuf, size); 1199 result = Tcl_NewUnicodeObj(outbuf, size);
1008 ckfree(FREECAST outbuf); 1200 ckfree(FREECAST outbuf);
1009 return result; 1201 return result;
1010 } 1202 #else
1203 return Tcl_NewUnicodeObj(inbuf, size);
1204 #endif
1205 }
1206 #endif
1207
1011 if(PyTclObject_Check(value)) { 1208 if(PyTclObject_Check(value)) {
1012 Tcl_Obj *v = ((PyTclObject*)value)->value; 1209 Tcl_Obj *v = ((PyTclObject*)value)->value;
1013 Tcl_IncrRefCount(v); 1210 Tcl_IncrRefCount(v);
1014 return v; 1211 return v;
1015 } 1212 }
1213
1016 { 1214 {
1017 PyObject *v = PyObject_Str(value); 1215 PyObject *v = PyObject_Str(value);
1018 if (!v) 1216 if (!v)
1019 return 0; 1217 return 0;
1020 result = AsObj(v); 1218 result = AsObj(v);
1021 Py_DECREF(v); 1219 Py_DECREF(v);
1022 return result; 1220 return result;
1023 } 1221 }
1024 } 1222 }
1025 1223
1224 static PyObject *
1225 fromBoolean(PyObject* tkapp, Tcl_Obj *value)
1226 {
1227 int boolValue;
1228 if (Tcl_GetBooleanFromObj(Tkapp_Interp(tkapp), value, &boolValue) == TCL_ERR OR)
1229 return Tkinter_Error(tkapp);
1230 return PyBool_FromLong(boolValue);
1231 }
1232
1233 #ifdef TCL_WIDE_INT_TYPE
1234 static PyObject*
1235 fromWideIntObj(PyObject* tkapp, Tcl_Obj *value)
1236 {
1237 Tcl_WideInt wideValue;
1238 if (Tcl_GetWideIntFromObj(Tkapp_Interp(tkapp), value, &wideValue) == TCL _OK) {
1239 #ifdef HAVE_LONG_LONG
1240 if (sizeof(wideValue) <= SIZEOF_LONG_LONG)
1241 return PyLong_FromLongLong(wideValue);
1242 #endif
1243 return _PyLong_FromByteArray((unsigned char *)(void *)&wideValue,
1244 sizeof(wideValue),
1245 #ifdef WORDS_BIGENDIAN
1246 0,
1247 #else
1248 1,
1249 #endif
1250 /* signed */ 1);
1251 }
1252 return NULL;
1253 }
1254 #endif
1255
1256 #ifdef HAVE_LIBTOMMAMTH
1257 static PyObject*
1258 fromBignumObj(PyObject* tkapp, Tcl_Obj *value)
1259 {
1260 mp_int bigValue;
1261 unsigned long numBytes;
1262 unsigned char *bytes;
1263 PyObject *res;
1264
1265 if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK)
1266 return Tkinter_Error(tkapp);
1267 numBytes = mp_unsigned_bin_size(&bigValue);
1268 bytes = PyMem_Malloc(numBytes);
1269 if (bytes == NULL) {
1270 mp_clear(&bigValue);
1271 return PyErr_NoMemory();
1272 }
1273 if (mp_to_unsigned_bin_n(&bigValue, bytes,
1274 &numBytes) != MP_OKAY) {
1275 mp_clear(&bigValue);
1276 PyMem_Free(bytes);
1277 return PyErr_NoMemory();
1278 }
1279 res = _PyLong_FromByteArray(bytes, numBytes,
1280 /* big-endian */ 0,
1281 /* unsigned */ 0);
1282 PyMem_Free(bytes);
1283 if (res != NULL && bigValue.sign == MP_NEG) {
1284 PyObject *res2 = PyNumber_Negative(res);
1285 Py_DECREF(res);
1286 res = res2;
1287 }
1288 mp_clear(&bigValue);
1289 return res;
1290 }
1291 #endif
1292
1026 static PyObject* 1293 static PyObject*
1027 FromObj(PyObject* tkapp, Tcl_Obj *value) 1294 FromObj(PyObject* tkapp, Tcl_Obj *value)
1028 { 1295 {
1029 PyObject *result = NULL; 1296 PyObject *result = NULL;
1030 TkappObject *app = (TkappObject*)tkapp; 1297 TkappObject *app = (TkappObject*)tkapp;
1031 Tcl_Interp *interp = Tkapp_Interp(tkapp); 1298 Tcl_Interp *interp = Tkapp_Interp(tkapp);
1032 1299
1033 if (value->typePtr == NULL) { 1300 if (value->typePtr == NULL) {
1034 return unicodeFromTclStringAndSize(value->bytes, value->length); 1301 result = fromTclStringAndSize(value->bytes, value->length);
1035 }
1036
1037 if (value->typePtr == app->BooleanType) {
1038 result = value->internalRep.longValue ? Py_True : Py_False;
1039 Py_INCREF(result);
1040 return result; 1302 return result;
1303 }
1304
1305 if (value->typePtr == app->BooleanType ||
1306 value->typePtr == app->OldBooleanType) {
1307 return fromBoolean(tkapp, value);
1041 } 1308 }
1042 1309
1043 if (value->typePtr == app->ByteArrayType) { 1310 if (value->typePtr == app->ByteArrayType) {
1044 int size; 1311 int size;
1045 char *data = (char*)Tcl_GetByteArrayFromObj(value, &size); 1312 char *data = (char*)Tcl_GetByteArrayFromObj(value, &size);
1046 return PyBytes_FromStringAndSize(data, size); 1313 return PyString_FromStringAndSize(data, size);
1047 } 1314 }
1048 1315
1049 if (value->typePtr == app->DoubleType) { 1316 if (value->typePtr == app->DoubleType) {
1050 return PyFloat_FromDouble(value->internalRep.doubleValue); 1317 return PyFloat_FromDouble(value->internalRep.doubleValue);
1051 } 1318 }
1052 1319
1053 if (value->typePtr == app->IntType) { 1320 if (value->typePtr == app->IntType) {
1054 long longValue; 1321 long longValue;
1055 if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK) 1322 if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK)
1056 return PyLong_FromLong(longValue); 1323 return PyInt_FromLong(longValue);
1057 /* If there is an error in the long conversion, 1324 /* If there is an error in the long conversion,
1058 fall through to wideInt handling. */ 1325 fall through to wideInt handling. */
1059 } 1326 }
1060 1327
1061 #ifdef HAVE_LONG_LONG 1328 #ifdef TCL_WIDE_INT_TYPE
1062 if (value->typePtr == app->IntType || 1329 if (value->typePtr == app->IntType ||
1063 value->typePtr == app->WideIntType) { 1330 value->typePtr == app->WideIntType) {
1064 Tcl_WideInt wideValue; 1331 result = fromWideIntObj(tkapp, value);
1065 if (Tcl_GetWideIntFromObj(interp, value, &wideValue) == TCL_OK) 1332 if (result != NULL || PyErr_Occurred())
1066 return PyLong_FromLongLong(wideValue); 1333 return result;
1334 Tcl_ResetResult(interp);
1067 /* If there is an error in the wideInt conversion, 1335 /* If there is an error in the wideInt conversion,
1068 fall through to bignum handling. */ 1336 fall through to bignum handling. */
1069 } 1337 }
1070 #endif 1338 #endif
1071 1339
1072 #ifdef HAVE_LIBTOMMAMTH 1340 #ifdef HAVE_LIBTOMMAMTH
1073 if (value->typePtr == app->IntType || 1341 if (value->typePtr == app->IntType ||
1074 value->typePtr == app->WideIntType || 1342 value->typePtr == app->WideIntType ||
1075 value->typePtr == app->BignumType || 1343 value->typePtr == app->BignumType) {
1076 (app->BignumType == NULL && 1344 return fromBignumObj(tkapp, value);
1077 strcmp(value->typePtr->name, "bignum") == 0)) {
1078 mp_int bigValue;
1079 /* bignum type is not registered in Tcl */
1080 if (app->BignumType == NULL &&
1081 strcmp(value->typePtr->name, "bignum") == 0) {
1082 app->BignumType = value->typePtr;
1083 }
1084 if (Tcl_GetBignumFromObj(interp, value, &bigValue) == TCL_OK) {
1085 unsigned long numBytes = mp_unsigned_bin_size(&bigValue);
1086 unsigned char *bytes = PyMem_Malloc(numBytes);
1087 PyObject *res;
1088 if (bytes == NULL) {
1089 mp_clear(&bigValue);
1090 return NULL;
1091 }
1092 if (mp_to_unsigned_bin_n(&bigValue, bytes,
1093 &numBytes) != MP_OKAY) {
1094 mp_clear(&bigValue);
1095 PyMem_Free(bytes);
1096 return PyErr_NoMemory();
1097 }
1098 res = _PyLong_FromByteArray(bytes, numBytes,
1099 /* big-endian */ 0,
1100 /* unsigned */ 0);
1101 PyMem_Free(bytes);
1102 if (res != NULL && bigValue.sign == MP_NEG) {
1103 PyObject *res2 = PyNumber_Negative(res);
1104 Py_DECREF(res);
1105 res = res2;
1106 }
1107 mp_clear(&bigValue);
1108 return res;
1109 }
1110 return Tkinter_Error(tkapp);
1111 } 1345 }
1112 #endif 1346 #endif
1113 1347
1114 if (value->typePtr == app->ListType) { 1348 if (value->typePtr == app->ListType) {
1115 int size; 1349 int size;
1116 int i, status; 1350 int i, status;
1117 PyObject *elem; 1351 PyObject *elem;
1118 Tcl_Obj *tcl_elem; 1352 Tcl_Obj *tcl_elem;
1119 1353
1120 status = Tcl_ListObjLength(interp, value, &size); 1354 status = Tcl_ListObjLength(interp, value, &size);
(...skipping 16 matching lines...) Expand all
1137 PyTuple_SetItem(result, i, elem); 1371 PyTuple_SetItem(result, i, elem);
1138 } 1372 }
1139 return result; 1373 return result;
1140 } 1374 }
1141 1375
1142 if (value->typePtr == app->ProcBodyType) { 1376 if (value->typePtr == app->ProcBodyType) {
1143 /* fall through: return tcl object. */ 1377 /* fall through: return tcl object. */
1144 } 1378 }
1145 1379
1146 if (value->typePtr == app->StringType) { 1380 if (value->typePtr == app->StringType) {
1147 return PyUnicode_FromKindAndData( 1381 #ifdef Py_USING_UNICODE
1148 sizeof(Tcl_UniChar), Tcl_GetUnicode(value), 1382 #if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==3
1149 Tcl_GetCharLength(value)); 1383 PyObject *result;
1150 } 1384 int size;
1385 Tcl_UniChar *input;
1386 Py_UNICODE *output;
1387
1388 size = Tcl_GetCharLength(value);
1389 result = PyUnicode_FromUnicode(NULL, size);
1390 if (!result)
1391 return NULL;
1392 input = Tcl_GetUnicode(value);
1393 output = PyUnicode_AS_UNICODE(result);
1394 while (size--)
1395 *output++ = *input++;
1396 return result;
1397 #else
1398 return PyUnicode_FromUnicode(Tcl_GetUnicode(value),
1399 Tcl_GetCharLength(value));
1400 #endif
1401 #else
1402 int size;
1403 char *c;
1404 c = Tcl_GetStringFromObj(value, &size);
1405 return PyString_FromStringAndSize(c, size);
1406 #endif
1407 }
1408
1409 #if TK_HEX_VERSION >= 0x08050000
1410 if (app->BooleanType == NULL &&
1411 strcmp(value->typePtr->name, "booleanString") == 0) {
1412 /* booleanString type is not registered in Tcl */
1413 app->BooleanType = value->typePtr;
1414 return fromBoolean(tkapp, value);
1415 }
1416 #endif
1417
1418 #ifdef HAVE_LIBTOMMAMTH
1419 if (app->BignumType == NULL &&
1420 strcmp(value->typePtr->name, "bignum") == 0) {
1421 /* bignum type is not registered in Tcl */
1422 app->BignumType = value->typePtr;
1423 return fromBignumObj(tkapp, value);
1424 }
1425 #endif
1151 1426
1152 return newPyTclObject(value); 1427 return newPyTclObject(value);
1153 } 1428 }
1154 1429
1155 #ifdef WITH_THREAD 1430 #ifdef WITH_THREAD
1156 /* This mutex synchronizes inter-thread command calls. */ 1431 /* This mutex synchronizes inter-thread command calls. */
1157 TCL_DECLARE_MUTEX(call_mutex) 1432 TCL_DECLARE_MUTEX(call_mutex)
1158 1433
1159 typedef struct Tkapp_CallEvent { 1434 typedef struct Tkapp_CallEvent {
1160 Tcl_Event ev; /* Must be first */ 1435 Tcl_Event ev; /* Must be first */
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 Tcl_IncrRefCount(objv[0]); 1471 Tcl_IncrRefCount(objv[0]);
1197 } 1472 }
1198 else { 1473 else {
1199 objc = PyTuple_Size(args); 1474 objc = PyTuple_Size(args);
1200 1475
1201 if (objc > ARGSZ) { 1476 if (objc > ARGSZ) {
1202 if (!CHECK_SIZE(objc, sizeof(Tcl_Obj *))) { 1477 if (!CHECK_SIZE(objc, sizeof(Tcl_Obj *))) {
1203 PyErr_SetString(PyExc_OverflowError, "tuple is too long"); 1478 PyErr_SetString(PyExc_OverflowError, "tuple is too long");
1204 return NULL; 1479 return NULL;
1205 } 1480 }
1206 objv = (Tcl_Obj **)ckalloc(((size_t)objc) * sizeof(Tcl_Obj *)); 1481 objv = (Tcl_Obj **)attemptckalloc(((size_t)objc) * sizeof(Tcl_Obj *) );
1207 if (objv == NULL) { 1482 if (objv == NULL) {
1208 PyErr_NoMemory(); 1483 PyErr_NoMemory();
1209 objc = 0; 1484 objc = 0;
1210 goto finally; 1485 goto finally;
1211 } 1486 }
1212 } 1487 }
1213 1488
1214 for (i = 0; i < objc; i++) { 1489 for (i = 0; i < objc; i++) {
1215 PyObject *v = PyTuple_GetItem(args, i); 1490 PyObject *v = PyTuple_GetItem(args, i);
1216 if (v == Py_None) { 1491 if (v == Py_None) {
(...skipping 25 matching lines...) Expand all
1242 PyObject *res = NULL; 1517 PyObject *res = NULL;
1243 Tcl_Obj *value = Tcl_GetObjResult(self->interp); 1518 Tcl_Obj *value = Tcl_GetObjResult(self->interp);
1244 if(self->wantobjects) { 1519 if(self->wantobjects) {
1245 /* Not sure whether the IncrRef is necessary, but something 1520 /* Not sure whether the IncrRef is necessary, but something
1246 may overwrite the interpreter result while we are 1521 may overwrite the interpreter result while we are
1247 converting it. */ 1522 converting it. */
1248 Tcl_IncrRefCount(value); 1523 Tcl_IncrRefCount(value);
1249 res = FromObj((PyObject*)self, value); 1524 res = FromObj((PyObject*)self, value);
1250 Tcl_DecrRefCount(value); 1525 Tcl_DecrRefCount(value);
1251 } else { 1526 } else {
1252 res = unicodeFromTclObj(value); 1527 int len;
1528 const char *s = Tcl_GetStringFromObj(value, &len);
1529 res = fromTclStringAndSize(s, len);
1253 } 1530 }
1254 return res; 1531 return res;
1255 } 1532 }
1256 1533
1257 #ifdef WITH_THREAD 1534 #ifdef WITH_THREAD
1258 1535
1259 /* Tkapp_CallProc is the event procedure that is executed in the context of 1536 /* Tkapp_CallProc is the event procedure that is executed in the context of
1260 the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't 1537 the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't
1261 hold the Python lock. */ 1538 hold the Python lock. */
1262 1539
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 } 1609 }
1333 #ifdef WITH_THREAD 1610 #ifdef WITH_THREAD
1334 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { 1611 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
1335 /* We cannot call the command directly. Instead, we must 1612 /* We cannot call the command directly. Instead, we must
1336 marshal the parameters to the interpreter thread. */ 1613 marshal the parameters to the interpreter thread. */
1337 Tkapp_CallEvent *ev; 1614 Tkapp_CallEvent *ev;
1338 Tcl_Condition cond = NULL; 1615 Tcl_Condition cond = NULL;
1339 PyObject *exc_type, *exc_value, *exc_tb; 1616 PyObject *exc_type, *exc_value, *exc_tb;
1340 if (!WaitForMainloop(self)) 1617 if (!WaitForMainloop(self))
1341 return NULL; 1618 return NULL;
1342 ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent)); 1619 ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent));
1620 if (ev == NULL) {
1621 PyErr_NoMemory();
1622 return NULL;
1623 }
1343 ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; 1624 ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
1344 ev->self = self; 1625 ev->self = self;
1345 ev->args = args; 1626 ev->args = args;
1346 ev->res = &res; 1627 ev->res = &res;
1347 ev->exc_type = &exc_type; 1628 ev->exc_type = &exc_type;
1348 ev->exc_value = &exc_value; 1629 ev->exc_value = &exc_value;
1349 ev->exc_tb = &exc_tb; 1630 ev->exc_tb = &exc_tb;
1350 ev->done = &cond; 1631 ev->done = &cond;
1351 1632
1352 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex); 1633 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex);
(...skipping 27 matching lines...) Expand all
1380 1661
1381 LEAVE_OVERLAP_TCL 1662 LEAVE_OVERLAP_TCL
1382 1663
1383 Tkapp_CallDeallocArgs(objv, objStore, objc); 1664 Tkapp_CallDeallocArgs(objv, objStore, objc);
1384 } 1665 }
1385 return res; 1666 return res;
1386 } 1667 }
1387 1668
1388 1669
1389 static PyObject * 1670 static PyObject *
1671 Tkapp_GlobalCall(PyObject *self, PyObject *args)
1672 {
1673 /* Could do the same here as for Tkapp_Call(), but this is not used
1674 much, so I can't be bothered. Unfortunately Tcl doesn't export a
1675 way for the user to do what all its Global* variants do (save and
1676 reset the scope pointer, call the local version, restore the saved
1677 scope pointer). */
1678
1679 char *cmd;
1680 PyObject *res = NULL;
1681
1682 CHECK_TCL_APPARTMENT;
1683
1684 cmd = Merge(args);
1685 if (cmd) {
1686 int err;
1687 ENTER_TCL
1688 err = Tcl_GlobalEval(Tkapp_Interp(self), cmd);
1689 ENTER_OVERLAP
1690 if (err == TCL_ERROR)
1691 res = Tkinter_Error(self);
1692 else
1693 res = PyString_FromString(Tkapp_Result(self));
1694 LEAVE_OVERLAP_TCL
1695 ckfree(cmd);
1696 }
1697
1698 return res;
1699 }
1700
1701 static PyObject *
1390 Tkapp_Eval(PyObject *self, PyObject *args) 1702 Tkapp_Eval(PyObject *self, PyObject *args)
1391 { 1703 {
1392 char *script; 1704 char *script;
1393 PyObject *res = NULL; 1705 PyObject *res = NULL;
1394 int err; 1706 int err;
1395 1707
1396 if (!PyArg_ParseTuple(args, "s:eval", &script)) 1708 if (!PyArg_ParseTuple(args, "s:eval", &script))
1397 return NULL; 1709 return NULL;
1398 1710
1711 CHECK_STRING_LENGTH(script);
1399 CHECK_TCL_APPARTMENT; 1712 CHECK_TCL_APPARTMENT;
1400 1713
1401 ENTER_TCL 1714 ENTER_TCL
1402 err = Tcl_Eval(Tkapp_Interp(self), script); 1715 err = Tcl_Eval(Tkapp_Interp(self), script);
1403 ENTER_OVERLAP 1716 ENTER_OVERLAP
1404 if (err == TCL_ERROR) 1717 if (err == TCL_ERROR)
1405 res = Tkinter_Error(self); 1718 res = Tkinter_Error(self);
1406 else 1719 else
1407 res = unicodeFromTclString(Tkapp_Result(self)); 1720 res = PyString_FromString(Tkapp_Result(self));
1721 LEAVE_OVERLAP_TCL
1722 return res;
1723 }
1724
1725 static PyObject *
1726 Tkapp_GlobalEval(PyObject *self, PyObject *args)
1727 {
1728 char *script;
1729 PyObject *res = NULL;
1730 int err;
1731
1732 if (!PyArg_ParseTuple(args, "s:globaleval", &script))
1733 return NULL;
1734
1735 CHECK_TCL_APPARTMENT;
1736
1737 ENTER_TCL
1738 err = Tcl_GlobalEval(Tkapp_Interp(self), script);
1739 ENTER_OVERLAP
1740 if (err == TCL_ERROR)
1741 res = Tkinter_Error(self);
1742 else
1743 res = PyString_FromString(Tkapp_Result(self));
1408 LEAVE_OVERLAP_TCL 1744 LEAVE_OVERLAP_TCL
1409 return res; 1745 return res;
1410 } 1746 }
1411 1747
1412 static PyObject * 1748 static PyObject *
1413 Tkapp_EvalFile(PyObject *self, PyObject *args) 1749 Tkapp_EvalFile(PyObject *self, PyObject *args)
1414 { 1750 {
1415 char *fileName; 1751 char *fileName;
1416 PyObject *res = NULL; 1752 PyObject *res = NULL;
1417 int err; 1753 int err;
1418 1754
1419 if (!PyArg_ParseTuple(args, "s:evalfile", &fileName)) 1755 if (!PyArg_ParseTuple(args, "s:evalfile", &fileName))
1420 return NULL; 1756 return NULL;
1421 1757
1758 CHECK_STRING_LENGTH(fileName);
1422 CHECK_TCL_APPARTMENT; 1759 CHECK_TCL_APPARTMENT;
1423 1760
1424 ENTER_TCL 1761 ENTER_TCL
1425 err = Tcl_EvalFile(Tkapp_Interp(self), fileName); 1762 err = Tcl_EvalFile(Tkapp_Interp(self), fileName);
1426 ENTER_OVERLAP 1763 ENTER_OVERLAP
1427 if (err == TCL_ERROR) 1764 if (err == TCL_ERROR)
1428 res = Tkinter_Error(self); 1765 res = Tkinter_Error(self);
1766
1429 else 1767 else
1430 res = unicodeFromTclString(Tkapp_Result(self)); 1768 res = PyString_FromString(Tkapp_Result(self));
1431 LEAVE_OVERLAP_TCL 1769 LEAVE_OVERLAP_TCL
1432 return res; 1770 return res;
1433 } 1771 }
1434 1772
1435 static PyObject * 1773 static PyObject *
1436 Tkapp_Record(PyObject *self, PyObject *args) 1774 Tkapp_Record(PyObject *self, PyObject *args)
1437 { 1775 {
1438 char *script; 1776 char *script;
1439 PyObject *res = NULL; 1777 PyObject *res = NULL;
1440 int err; 1778 int err;
1441 1779
1442 if (!PyArg_ParseTuple(args, "s", &script)) 1780 if (!PyArg_ParseTuple(args, "s:record", &script))
1443 return NULL; 1781 return NULL;
1444 1782
1783 CHECK_STRING_LENGTH(script);
1445 CHECK_TCL_APPARTMENT; 1784 CHECK_TCL_APPARTMENT;
1446 1785
1447 ENTER_TCL 1786 ENTER_TCL
1448 err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); 1787 err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL);
1449 ENTER_OVERLAP 1788 ENTER_OVERLAP
1450 if (err == TCL_ERROR) 1789 if (err == TCL_ERROR)
1451 res = Tkinter_Error(self); 1790 res = Tkinter_Error(self);
1452 else 1791 else
1453 res = unicodeFromTclString(Tkapp_Result(self)); 1792 res = PyString_FromString(Tkapp_Result(self));
1454 LEAVE_OVERLAP_TCL 1793 LEAVE_OVERLAP_TCL
1455 return res; 1794 return res;
1456 } 1795 }
1457 1796
1458 static PyObject * 1797 static PyObject *
1459 Tkapp_AddErrorInfo(PyObject *self, PyObject *args) 1798 Tkapp_AddErrorInfo(PyObject *self, PyObject *args)
1460 { 1799 {
1461 char *msg; 1800 char *msg;
1462 1801
1463 if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg)) 1802 if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg))
1464 return NULL; 1803 return NULL;
1804 CHECK_STRING_LENGTH(msg);
1465 CHECK_TCL_APPARTMENT; 1805 CHECK_TCL_APPARTMENT;
1466 1806
1467 ENTER_TCL 1807 ENTER_TCL
1468 Tcl_AddErrorInfo(Tkapp_Interp(self), msg); 1808 Tcl_AddErrorInfo(Tkapp_Interp(self), msg);
1469 LEAVE_TCL 1809 LEAVE_TCL
1470 1810
1471 Py_RETURN_NONE; 1811 Py_INCREF(Py_None);
1812 return Py_None;
1472 } 1813 }
1473 1814
1474 1815
1475 1816
1476 /** Tcl Variable **/ 1817 /** Tcl Variable **/
1477 1818
1478 typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags); 1819 typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags);
1479 1820
1480 #ifdef WITH_THREAD 1821 #ifdef WITH_THREAD
1481 TCL_DECLARE_MUTEX(var_mutex) 1822 TCL_DECLARE_MUTEX(var_mutex)
1482 1823
1483 typedef struct VarEvent { 1824 typedef struct VarEvent {
1484 Tcl_Event ev; /* must be first */ 1825 Tcl_Event ev; /* must be first */
1485 PyObject *self; 1826 PyObject *self;
1486 PyObject *args; 1827 PyObject *args;
1487 int flags; 1828 int flags;
1488 EventFunc func; 1829 EventFunc func;
1489 PyObject **res; 1830 PyObject **res;
1490 PyObject **exc_type; 1831 PyObject **exc_type;
1491 PyObject **exc_val; 1832 PyObject **exc_val;
1492 Tcl_Condition *cond; 1833 Tcl_Condition *cond;
1493 } VarEvent; 1834 } VarEvent;
1494 #endif 1835 #endif
1495 1836
1496 static int 1837 static int
1497 varname_converter(PyObject *in, void *_out) 1838 varname_converter(PyObject *in, void *_out)
1498 { 1839 {
1499 char *s; 1840 char *s;
1500 char **out = (char**)_out; 1841 char **out = (char**)_out;
1501 if (PyBytes_Check(in)) { 1842 if (PyString_Check(in)) {
1502 if (PyBytes_Size(in) > INT_MAX) { 1843 if (PyString_Size(in) > INT_MAX) {
1503 PyErr_SetString(PyExc_OverflowError, "bytes object is too long");
1504 return 0;
1505 }
1506 s = PyBytes_AsString(in);
1507 if (strlen(s) != PyBytes_Size(in)) {
1508 PyErr_SetString(PyExc_ValueError, "null byte in bytes object");
1509 return 0;
1510 }
1511 *out = s;
1512 return 1;
1513 }
1514 if (PyUnicode_Check(in)) {
1515 Py_ssize_t size;
1516 s = PyUnicode_AsUTF8AndSize(in, &size);
1517 if (s == NULL) {
1518 return 0;
1519 }
1520 if (size > INT_MAX) {
1521 PyErr_SetString(PyExc_OverflowError, "string is too long"); 1844 PyErr_SetString(PyExc_OverflowError, "string is too long");
1522 return 0; 1845 return 0;
1523 } 1846 }
1524 if (strlen(s) != size) { 1847 s = PyString_AsString(in);
1848 if (strlen(s) != PyString_Size(in)) {
1525 PyErr_SetString(PyExc_ValueError, "null character in string"); 1849 PyErr_SetString(PyExc_ValueError, "null character in string");
1526 return 0; 1850 return 0;
1527 } 1851 }
1528 *out = s; 1852 *out = s;
1529 return 1; 1853 return 1;
1530 } 1854 }
1531 if (PyTclObject_Check(in)) { 1855 if (PyTclObject_Check(in)) {
1532 *out = PyTclObject_TclString(in); 1856 *out = PyTclObject_TclString(in);
1533 return 1; 1857 return 1;
1534 } 1858 }
1535 PyErr_Format(PyExc_TypeError, 1859 PyErr_Format(PyExc_TypeError,
1536 "must be str, bytes or Tcl_Obj, not %.50s", 1860 "must be str or Tcl_Obj, not %.50s",
1537 in->ob_type->tp_name); 1861 in->ob_type->tp_name);
1538 return 0; 1862 return 0;
1539 } 1863 }
1540 1864
1541 #ifdef WITH_THREAD 1865 #ifdef WITH_THREAD
1542 1866
1543 static void 1867 static void
1544 var_perform(VarEvent *ev) 1868 var_perform(VarEvent *ev)
1545 { 1869 {
1546 *(ev->res) = ev->func(ev->self, ev->args, ev->flags); 1870 *(ev->res) = ev->func(ev->self, ev->args, ev->flags);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1579 VarEvent *ev; 1903 VarEvent *ev;
1580 PyObject *res, *exc_type, *exc_val; 1904 PyObject *res, *exc_type, *exc_val;
1581 Tcl_Condition cond = NULL; 1905 Tcl_Condition cond = NULL;
1582 1906
1583 /* The current thread is not the interpreter thread. Marshal 1907 /* The current thread is not the interpreter thread. Marshal
1584 the call to the interpreter thread, then wait for 1908 the call to the interpreter thread, then wait for
1585 completion. */ 1909 completion. */
1586 if (!WaitForMainloop(self)) 1910 if (!WaitForMainloop(self))
1587 return NULL; 1911 return NULL;
1588 1912
1589 ev = (VarEvent*)ckalloc(sizeof(VarEvent)); 1913 ev = (VarEvent*)attemptckalloc(sizeof(VarEvent));
1590 1914 if (ev == NULL) {
1915 PyErr_NoMemory();
1916 return NULL;
1917 }
1591 ev->self = selfptr; 1918 ev->self = selfptr;
1592 ev->args = args; 1919 ev->args = args;
1593 ev->flags = flags; 1920 ev->flags = flags;
1594 ev->func = func; 1921 ev->func = func;
1595 ev->res = &res; 1922 ev->res = &res;
1596 ev->exc_type = &exc_type; 1923 ev->exc_type = &exc_type;
1597 ev->exc_val = &exc_val; 1924 ev->exc_val = &exc_val;
1598 ev->cond = &cond; 1925 ev->cond = &cond;
1599 ev->ev.proc = (Tcl_EventProc*)var_proc; 1926 ev->ev.proc = (Tcl_EventProc*)var_proc;
1600 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex); 1927 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1638 else { 1965 else {
1639 res = Py_None; 1966 res = Py_None;
1640 Py_INCREF(res); 1967 Py_INCREF(res);
1641 } 1968 }
1642 LEAVE_OVERLAP_TCL 1969 LEAVE_OVERLAP_TCL
1643 break; 1970 break;
1644 case 3: 1971 case 3:
1645 if (!PyArg_ParseTuple(args, "ssO:setvar", 1972 if (!PyArg_ParseTuple(args, "ssO:setvar",
1646 &name1, &name2, &newValue)) 1973 &name1, &name2, &newValue))
1647 return NULL; 1974 return NULL;
1975 CHECK_STRING_LENGTH(name1);
1976 CHECK_STRING_LENGTH(name2);
1648 /* XXX must hold tcl lock already??? */ 1977 /* XXX must hold tcl lock already??? */
1649 newval = AsObj(newValue); 1978 newval = AsObj(newValue);
1650 ENTER_TCL 1979 ENTER_TCL
1651 ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags); 1980 ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags);
1652 ENTER_OVERLAP 1981 ENTER_OVERLAP
1653 if (!ok) 1982 if (!ok)
1654 Tkinter_Error(self); 1983 Tkinter_Error(self);
1655 else { 1984 else {
1656 res = Py_None; 1985 res = Py_None;
1657 Py_INCREF(res); 1986 Py_INCREF(res);
(...skipping 25 matching lines...) Expand all
1683 GetVar(PyObject *self, PyObject *args, int flags) 2012 GetVar(PyObject *self, PyObject *args, int flags)
1684 { 2013 {
1685 char *name1, *name2=NULL; 2014 char *name1, *name2=NULL;
1686 PyObject *res = NULL; 2015 PyObject *res = NULL;
1687 Tcl_Obj *tres; 2016 Tcl_Obj *tres;
1688 2017
1689 if (!PyArg_ParseTuple(args, "O&|s:getvar", 2018 if (!PyArg_ParseTuple(args, "O&|s:getvar",
1690 varname_converter, &name1, &name2)) 2019 varname_converter, &name1, &name2))
1691 return NULL; 2020 return NULL;
1692 2021
2022 CHECK_STRING_LENGTH(name2);
1693 ENTER_TCL 2023 ENTER_TCL
1694 tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags); 2024 tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags);
1695 ENTER_OVERLAP 2025 ENTER_OVERLAP
1696 if (tres == NULL) { 2026 if (tres == NULL) {
1697 PyErr_SetString(Tkinter_TclError, 2027 PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self) ));
1698 Tcl_GetStringResult(Tkapp_Interp(self)));
1699 } else { 2028 } else {
1700 if (((TkappObject*)self)->wantobjects) { 2029 if (((TkappObject*)self)->wantobjects) {
1701 res = FromObj(self, tres); 2030 res = FromObj(self, tres);
1702 } 2031 }
1703 else { 2032 else {
1704 res = unicodeFromTclObj(tres); 2033 int len;
2034 char *s = Tcl_GetStringFromObj(tres, &len);
2035 res = PyString_FromStringAndSize(s, len);
1705 } 2036 }
1706 } 2037 }
1707 LEAVE_OVERLAP_TCL 2038 LEAVE_OVERLAP_TCL
1708 return res; 2039 return res;
1709 } 2040 }
1710 2041
1711 static PyObject * 2042 static PyObject *
1712 Tkapp_GetVar(PyObject *self, PyObject *args) 2043 Tkapp_GetVar(PyObject *self, PyObject *args)
1713 { 2044 {
1714 return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG); 2045 return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG);
(...skipping 10 matching lines...) Expand all
1725 static PyObject * 2056 static PyObject *
1726 UnsetVar(PyObject *self, PyObject *args, int flags) 2057 UnsetVar(PyObject *self, PyObject *args, int flags)
1727 { 2058 {
1728 char *name1, *name2=NULL; 2059 char *name1, *name2=NULL;
1729 int code; 2060 int code;
1730 PyObject *res = NULL; 2061 PyObject *res = NULL;
1731 2062
1732 if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2)) 2063 if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2))
1733 return NULL; 2064 return NULL;
1734 2065
2066 CHECK_STRING_LENGTH(name1);
2067 CHECK_STRING_LENGTH(name2);
1735 ENTER_TCL 2068 ENTER_TCL
1736 code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); 2069 code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags);
1737 ENTER_OVERLAP 2070 ENTER_OVERLAP
1738 if (code == TCL_ERROR) 2071 if (code == TCL_ERROR)
1739 res = Tkinter_Error(self); 2072 res = Tkinter_Error(self);
1740 else { 2073 else {
1741 Py_INCREF(Py_None); 2074 Py_INCREF(Py_None);
1742 res = Py_None; 2075 res = Py_None;
1743 } 2076 }
1744 LEAVE_OVERLAP_TCL 2077 LEAVE_OVERLAP_TCL
1745 return res; 2078 return res;
1746 } 2079 }
1747 2080
1748 static PyObject * 2081 static PyObject *
1749 Tkapp_UnsetVar(PyObject *self, PyObject *args) 2082 Tkapp_UnsetVar(PyObject *self, PyObject *args)
1750 { 2083 {
1751 return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG); 2084 return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG);
1752 } 2085 }
1753 2086
1754 static PyObject * 2087 static PyObject *
1755 Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args) 2088 Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args)
1756 { 2089 {
1757 return var_invoke(UnsetVar, self, args, 2090 return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY) ;
1758 TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
1759 } 2091 }
1760 2092
1761 2093
1762 2094
1763 /** Tcl to Python **/ 2095 /** Tcl to Python **/
1764 2096
1765 static PyObject * 2097 static PyObject *
1766 Tkapp_GetInt(PyObject *self, PyObject *args) 2098 Tkapp_GetInt(PyObject *self, PyObject *args)
1767 { 2099 {
1768 char *s; 2100 char *s;
1769 int v; 2101 #if defined(TCL_WIDE_INT_TYPE) || defined(HAVE_LIBTOMMAMTH)
2102 Tcl_Obj *value;
2103 PyObject *result;
2104 #else
2105 int intValue;
2106 #endif
1770 2107
1771 if (PyTuple_Size(args) == 1) { 2108 if (PyTuple_Size(args) == 1) {
1772 PyObject* o = PyTuple_GetItem(args, 0); 2109 PyObject* o = PyTuple_GetItem(args, 0);
1773 if (PyLong_Check(o)) { 2110 if (PyInt_Check(o) || PyLong_Check(o)) {
1774 Py_INCREF(o); 2111 Py_INCREF(o);
1775 return o; 2112 return o;
1776 } 2113 }
1777 } 2114 }
1778 if (!PyArg_ParseTuple(args, "s:getint", &s)) 2115 if (!PyArg_ParseTuple(args, "s:getint", &s))
1779 return NULL; 2116 return NULL;
1780 if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) 2117 CHECK_STRING_LENGTH(s);
2118 #if defined(TCL_WIDE_INT_TYPE) || defined(HAVE_LIBTOMMAMTH)
2119 value = Tcl_NewStringObj(s, -1);
2120 if (value == NULL)
1781 return Tkinter_Error(self); 2121 return Tkinter_Error(self);
1782 return Py_BuildValue("i", v); 2122 /* Don't use Tcl_GetInt() because it returns ambiguous result for value
2123 in ranges -2**32..-2**31-1 and 2**31..2**32-1 (on 32-bit platform).
2124
2125 Prefer bignum because Tcl_GetWideIntFromObj returns ambiguous result for
2126 value in ranges -2**64..-2**63-1 and 2**63..2**64-1 (on 32-bit platform).
2127 */
2128 #ifdef HAVE_LIBTOMMAMTH
2129 result = fromBignumObj(self, value);
2130 #else
2131 result = fromWideIntObj(self, value);
2132 #endif
2133 Tcl_DecrRefCount(value);
2134 if (result != NULL)
2135 return PyNumber_Int(result);
2136 if (PyErr_Occurred())
2137 return NULL;
2138 #else
2139 if (Tcl_GetInt(Tkapp_Interp(self), s, &intValue) == TCL_OK)
2140 return PyInt_FromLong(intValue);
2141 #endif
2142 return Tkinter_Error(self);
1783 } 2143 }
1784 2144
1785 static PyObject * 2145 static PyObject *
1786 Tkapp_GetDouble(PyObject *self, PyObject *args) 2146 Tkapp_GetDouble(PyObject *self, PyObject *args)
1787 { 2147 {
1788 char *s; 2148 char *s;
1789 double v; 2149 double v;
1790 2150
1791 if (PyTuple_Size(args) == 1) { 2151 if (PyTuple_Size(args) == 1) {
1792 PyObject *o = PyTuple_GetItem(args, 0); 2152 PyObject *o = PyTuple_GetItem(args, 0);
1793 if (PyFloat_Check(o)) { 2153 if (PyFloat_Check(o)) {
1794 Py_INCREF(o); 2154 Py_INCREF(o);
1795 return o; 2155 return o;
1796 } 2156 }
1797 } 2157 }
1798 if (!PyArg_ParseTuple(args, "s:getdouble", &s)) 2158 if (!PyArg_ParseTuple(args, "s:getdouble", &s))
1799 return NULL; 2159 return NULL;
2160 CHECK_STRING_LENGTH(s);
1800 if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) 2161 if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR)
1801 return Tkinter_Error(self); 2162 return Tkinter_Error(self);
1802 return Py_BuildValue("d", v); 2163 return Py_BuildValue("d", v);
1803 } 2164 }
1804 2165
1805 static PyObject * 2166 static PyObject *
1806 Tkapp_GetBoolean(PyObject *self, PyObject *args) 2167 Tkapp_GetBoolean(PyObject *self, PyObject *arg)
1807 { 2168 {
1808 char *s; 2169 char *s;
1809 int v; 2170 int v;
1810 2171
1811 if (PyTuple_Size(args) == 1) { 2172 if (PyInt_Check(arg)) /* int or bool */
1812 PyObject *o = PyTuple_GetItem(args, 0); 2173 return PyBool_FromLong(PyInt_AS_LONG(arg));
1813 if (PyLong_Check(o)) { 2174
1814 Py_INCREF(o); 2175 if (PyLong_Check(arg))
1815 return o; 2176 return PyBool_FromLong(Py_SIZE(arg) != 0);
1816 } 2177
1817 } 2178 if (PyTclObject_Check(arg)) {
1818 if (!PyArg_ParseTuple(args, "s:getboolean", &s)) 2179 if (Tcl_GetBooleanFromObj(Tkapp_Interp(self),
1819 return NULL; 2180 ((PyTclObject*)arg)->value,
2181 &v) == TCL_ERROR)
2182 return Tkinter_Error(self);
2183 return PyBool_FromLong(v);
2184 }
2185
2186 if (!PyArg_Parse(arg, "s:getboolean", &s))
2187 return NULL;
2188 CHECK_STRING_LENGTH(s);
1820 if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) 2189 if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR)
1821 return Tkinter_Error(self); 2190 return Tkinter_Error(self);
1822 return PyBool_FromLong(v); 2191 return PyBool_FromLong(v);
1823 } 2192 }
1824 2193
1825 static PyObject * 2194 static PyObject *
1826 Tkapp_ExprString(PyObject *self, PyObject *args) 2195 Tkapp_ExprString(PyObject *self, PyObject *args)
1827 { 2196 {
1828 char *s; 2197 char *s;
1829 PyObject *res = NULL; 2198 PyObject *res = NULL;
1830 int retval; 2199 int retval;
1831 2200
1832 if (!PyArg_ParseTuple(args, "s:exprstring", &s)) 2201 if (!PyArg_ParseTuple(args, "s:exprstring", &s))
1833 return NULL; 2202 return NULL;
1834 2203
2204 CHECK_STRING_LENGTH(s);
1835 CHECK_TCL_APPARTMENT; 2205 CHECK_TCL_APPARTMENT;
1836 2206
1837 ENTER_TCL 2207 ENTER_TCL
1838 retval = Tcl_ExprString(Tkapp_Interp(self), s); 2208 retval = Tcl_ExprString(Tkapp_Interp(self), s);
1839 ENTER_OVERLAP 2209 ENTER_OVERLAP
1840 if (retval == TCL_ERROR) 2210 if (retval == TCL_ERROR)
1841 res = Tkinter_Error(self); 2211 res = Tkinter_Error(self);
1842 else 2212 else
1843 res = unicodeFromTclString(Tkapp_Result(self)); 2213 res = PyString_FromString(Tkapp_Result(self));
1844 LEAVE_OVERLAP_TCL 2214 LEAVE_OVERLAP_TCL
1845 return res; 2215 return res;
1846 } 2216 }
1847 2217
1848 static PyObject * 2218 static PyObject *
1849 Tkapp_ExprLong(PyObject *self, PyObject *args) 2219 Tkapp_ExprLong(PyObject *self, PyObject *args)
1850 { 2220 {
1851 char *s; 2221 char *s;
1852 PyObject *res = NULL; 2222 PyObject *res = NULL;
1853 int retval; 2223 int retval;
1854 long v; 2224 long v;
1855 2225
1856 if (!PyArg_ParseTuple(args, "s:exprlong", &s)) 2226 if (!PyArg_ParseTuple(args, "s:exprlong", &s))
1857 return NULL; 2227 return NULL;
1858 2228
2229 CHECK_STRING_LENGTH(s);
1859 CHECK_TCL_APPARTMENT; 2230 CHECK_TCL_APPARTMENT;
1860 2231
1861 ENTER_TCL 2232 ENTER_TCL
1862 retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); 2233 retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v);
1863 ENTER_OVERLAP 2234 ENTER_OVERLAP
1864 if (retval == TCL_ERROR) 2235 if (retval == TCL_ERROR)
1865 res = Tkinter_Error(self); 2236 res = Tkinter_Error(self);
1866 else 2237 else
1867 res = Py_BuildValue("l", v); 2238 res = Py_BuildValue("l", v);
1868 LEAVE_OVERLAP_TCL 2239 LEAVE_OVERLAP_TCL
1869 return res; 2240 return res;
1870 } 2241 }
1871 2242
1872 static PyObject * 2243 static PyObject *
1873 Tkapp_ExprDouble(PyObject *self, PyObject *args) 2244 Tkapp_ExprDouble(PyObject *self, PyObject *args)
1874 { 2245 {
1875 char *s; 2246 char *s;
1876 PyObject *res = NULL; 2247 PyObject *res = NULL;
1877 double v; 2248 double v;
1878 int retval; 2249 int retval;
1879 2250
1880 if (!PyArg_ParseTuple(args, "s:exprdouble", &s)) 2251 if (!PyArg_ParseTuple(args, "s:exprdouble", &s))
1881 return NULL; 2252 return NULL;
2253 CHECK_STRING_LENGTH(s);
1882 CHECK_TCL_APPARTMENT; 2254 CHECK_TCL_APPARTMENT;
1883 PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) 2255 PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0)
1884 ENTER_TCL 2256 ENTER_TCL
1885 retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); 2257 retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v);
1886 ENTER_OVERLAP 2258 ENTER_OVERLAP
1887 PyFPE_END_PROTECT(retval) 2259 PyFPE_END_PROTECT(retval)
1888 if (retval == TCL_ERROR) 2260 if (retval == TCL_ERROR)
1889 res = Tkinter_Error(self); 2261 res = Tkinter_Error(self);
1890 else 2262 else
1891 res = Py_BuildValue("d", v); 2263 res = Py_BuildValue("d", v);
1892 LEAVE_OVERLAP_TCL 2264 LEAVE_OVERLAP_TCL
1893 return res; 2265 return res;
1894 } 2266 }
1895 2267
1896 static PyObject * 2268 static PyObject *
1897 Tkapp_ExprBoolean(PyObject *self, PyObject *args) 2269 Tkapp_ExprBoolean(PyObject *self, PyObject *args)
1898 { 2270 {
1899 char *s; 2271 char *s;
1900 PyObject *res = NULL; 2272 PyObject *res = NULL;
1901 int retval; 2273 int retval;
1902 int v; 2274 int v;
1903 2275
1904 if (!PyArg_ParseTuple(args, "s:exprboolean", &s)) 2276 if (!PyArg_ParseTuple(args, "s:exprboolean", &s))
1905 return NULL; 2277 return NULL;
2278 CHECK_STRING_LENGTH(s);
1906 CHECK_TCL_APPARTMENT; 2279 CHECK_TCL_APPARTMENT;
1907 ENTER_TCL 2280 ENTER_TCL
1908 retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); 2281 retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v);
1909 ENTER_OVERLAP 2282 ENTER_OVERLAP
1910 if (retval == TCL_ERROR) 2283 if (retval == TCL_ERROR)
1911 res = Tkinter_Error(self); 2284 res = Tkinter_Error(self);
1912 else 2285 else
1913 res = Py_BuildValue("i", v); 2286 res = Py_BuildValue("i", v);
1914 LEAVE_OVERLAP_TCL 2287 LEAVE_OVERLAP_TCL
1915 return res; 2288 return res;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 return v; 2321 return v;
1949 } 2322 }
1950 if (PyTuple_Check(arg)) { 2323 if (PyTuple_Check(arg)) {
1951 Py_INCREF(arg); 2324 Py_INCREF(arg);
1952 return arg; 2325 return arg;
1953 } 2326 }
1954 2327
1955 if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list)) 2328 if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list))
1956 return NULL; 2329 return NULL;
1957 2330
2331 CHECK_STRING_LENGTH(list);
1958 if (Tcl_SplitList(Tkapp_Interp(self), list, 2332 if (Tcl_SplitList(Tkapp_Interp(self), list,
1959 &argc, &argv) == TCL_ERROR) { 2333 &argc, &argv) == TCL_ERROR) {
1960 PyMem_Free(list); 2334 PyMem_Free(list);
1961 return Tkinter_Error(self); 2335 return Tkinter_Error(self);
1962 } 2336 }
1963 2337
1964 if (!(v = PyTuple_New(argc))) 2338 if (!(v = PyTuple_New(argc)))
1965 goto finally; 2339 goto finally;
1966 2340
1967 for (i = 0; i < argc; i++) { 2341 for (i = 0; i < argc; i++) {
1968 PyObject *s = unicodeFromTclString(argv[i]); 2342 PyObject *s = PyString_FromString(argv[i]);
1969 if (!s || PyTuple_SetItem(v, i, s)) { 2343 if (!s || PyTuple_SetItem(v, i, s)) {
1970 Py_DECREF(v); 2344 Py_DECREF(v);
1971 v = NULL; 2345 v = NULL;
1972 goto finally; 2346 goto finally;
1973 } 2347 }
1974 } 2348 }
1975 2349
1976 finally: 2350 finally:
1977 ckfree(FREECAST argv); 2351 ckfree(FREECAST argv);
1978 PyMem_Free(list); 2352 PyMem_Free(list);
(...skipping 11 matching lines...) Expand all
1990 if (PyTclObject_Check(arg)) { 2364 if (PyTclObject_Check(arg)) {
1991 Tcl_Obj *value = ((PyTclObject*)arg)->value; 2365 Tcl_Obj *value = ((PyTclObject*)arg)->value;
1992 int objc; 2366 int objc;
1993 Tcl_Obj **objv; 2367 Tcl_Obj **objv;
1994 int i; 2368 int i;
1995 if (Tcl_ListObjGetElements(Tkapp_Interp(self), value, 2369 if (Tcl_ListObjGetElements(Tkapp_Interp(self), value,
1996 &objc, &objv) == TCL_ERROR) { 2370 &objc, &objv) == TCL_ERROR) {
1997 return FromObj(self, value); 2371 return FromObj(self, value);
1998 } 2372 }
1999 if (objc == 0) 2373 if (objc == 0)
2000 return PyUnicode_FromString(""); 2374 return PyString_FromString("");
2001 if (objc == 1) 2375 if (objc == 1)
2002 return FromObj(self, objv[0]); 2376 return FromObj(self, objv[0]);
2003 if (!(v = PyTuple_New(objc))) 2377 if (!(v = PyTuple_New(objc)))
2004 return NULL; 2378 return NULL;
2005 for (i = 0; i < objc; i++) { 2379 for (i = 0; i < objc; i++) {
2006 PyObject *s = FromObj(self, objv[i]); 2380 PyObject *s = FromObj(self, objv[i]);
2007 if (!s || PyTuple_SetItem(v, i, s)) { 2381 if (!s || PyTuple_SetItem(v, i, s)) {
2008 Py_DECREF(v); 2382 Py_DECREF(v);
2009 return NULL; 2383 return NULL;
2010 } 2384 }
2011 } 2385 }
2012 return v; 2386 return v;
2013 } 2387 }
2014 if (PyTuple_Check(arg)) 2388 if (PyTuple_Check(arg))
2015 return SplitObj(arg); 2389 return SplitObj(arg);
2016 2390
2017 if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list)) 2391 if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list))
2018 return NULL; 2392 return NULL;
2393 CHECK_STRING_LENGTH(list);
2019 v = Split(list); 2394 v = Split(list);
2020 PyMem_Free(list); 2395 PyMem_Free(list);
2021 return v; 2396 return v;
2397 }
2398
2399 static PyObject *
2400 Tkapp_Merge(PyObject *self, PyObject *args)
2401 {
2402 char *s = Merge(args);
2403 PyObject *res = NULL;
2404
2405 if (s) {
2406 res = PyString_FromString(s);
2407 ckfree(s);
2408 }
2409
2410 return res;
2022 } 2411 }
2023 2412
2024 2413
2025 2414
2026 /** Tcl Command **/ 2415 /** Tcl Command **/
2027 2416
2028 /* Client data struct */ 2417 /* Client data struct */
2029 typedef struct { 2418 typedef struct {
2030 PyObject *self; 2419 PyObject *self;
2031 PyObject *func; 2420 PyObject *func;
(...skipping 24 matching lines...) Expand all
2056 /* TBD: no error checking here since we know, via the 2445 /* TBD: no error checking here since we know, via the
2057 * Tkapp_CreateCommand() that the client data is a two-tuple 2446 * Tkapp_CreateCommand() that the client data is a two-tuple
2058 */ 2447 */
2059 func = data->func; 2448 func = data->func;
2060 2449
2061 /* Create argument list (argv1, ..., argvN) */ 2450 /* Create argument list (argv1, ..., argvN) */
2062 if (!(arg = PyTuple_New(argc - 1))) 2451 if (!(arg = PyTuple_New(argc - 1)))
2063 return PythonCmd_Error(interp); 2452 return PythonCmd_Error(interp);
2064 2453
2065 for (i = 0; i < (argc - 1); i++) { 2454 for (i = 0; i < (argc - 1); i++) {
2066 PyObject *s = unicodeFromTclString(argv[i + 1]); 2455 PyObject *s = fromTclString(argv[i + 1]);
2067 if (!s || PyTuple_SetItem(arg, i, s)) { 2456 if (!s || PyTuple_SetItem(arg, i, s)) {
2068 Py_DECREF(arg); 2457 Py_DECREF(arg);
2069 return PythonCmd_Error(interp); 2458 return PythonCmd_Error(interp);
2070 } 2459 }
2071 } 2460 }
2072 res = PyEval_CallObject(func, arg); 2461 res = PyEval_CallObject(func, arg);
2073 Py_DECREF(arg); 2462 Py_DECREF(arg);
2074 2463
2075 if (res == NULL) 2464 if (res == NULL)
2076 return PythonCmd_Error(interp); 2465 return PythonCmd_Error(interp);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2140 Tkapp_CreateCommand(PyObject *selfptr, PyObject *args) 2529 Tkapp_CreateCommand(PyObject *selfptr, PyObject *args)
2141 { 2530 {
2142 TkappObject *self = (TkappObject*)selfptr; 2531 TkappObject *self = (TkappObject*)selfptr;
2143 PythonCmd_ClientData *data; 2532 PythonCmd_ClientData *data;
2144 char *cmdName; 2533 char *cmdName;
2145 PyObject *func; 2534 PyObject *func;
2146 int err; 2535 int err;
2147 2536
2148 if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func)) 2537 if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func))
2149 return NULL; 2538 return NULL;
2539 CHECK_STRING_LENGTH(cmdName);
2150 if (!PyCallable_Check(func)) { 2540 if (!PyCallable_Check(func)) {
2151 PyErr_SetString(PyExc_TypeError, "command not callable"); 2541 PyErr_SetString(PyExc_TypeError, "command not callable");
2152 return NULL; 2542 return NULL;
2153 } 2543 }
2154 2544
2155 #ifdef WITH_THREAD 2545 #ifdef WITH_THREAD
2156 if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && 2546 if (self->threaded && self->thread_id != Tcl_GetCurrentThread() &&
2157 !WaitForMainloop(self)) 2547 !WaitForMainloop(self))
2158 return NULL; 2548 return NULL;
2159 #endif 2549 #endif
2160 2550
2161 data = PyMem_NEW(PythonCmd_ClientData, 1); 2551 data = PyMem_NEW(PythonCmd_ClientData, 1);
2162 if (!data) 2552 if (!data)
2163 return PyErr_NoMemory(); 2553 return PyErr_NoMemory();
2164 Py_INCREF(self); 2554 Py_INCREF(self);
2165 Py_INCREF(func); 2555 Py_INCREF(func);
2166 data->self = selfptr; 2556 data->self = selfptr;
2167 data->func = func; 2557 data->func = func;
2558
2168 #ifdef WITH_THREAD 2559 #ifdef WITH_THREAD
2169 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { 2560 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
2170 Tcl_Condition cond = NULL; 2561 Tcl_Condition cond = NULL;
2171 CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); 2562 CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
2563 if (ev == NULL) {
2564 PyErr_NoMemory();
2565 PyMem_DEL(data);
2566 return NULL;
2567 }
2172 ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; 2568 ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2173 ev->interp = self->interp; 2569 ev->interp = self->interp;
2174 ev->create = 1; 2570 ev->create = 1;
2175 ev->name = cmdName; 2571 ev->name = cmdName;
2176 ev->data = (ClientData)data; 2572 ev->data = (ClientData)data;
2177 ev->status = &err; 2573 ev->status = &err;
2178 ev->done = &cond; 2574 ev->done = &cond;
2179 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex); 2575 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex);
2180 Tcl_ConditionFinalize(&cond); 2576 Tcl_ConditionFinalize(&cond);
2181 } 2577 }
2182 else 2578 else
2183 #endif 2579 #endif
2184 { 2580 {
2185 ENTER_TCL 2581 ENTER_TCL
2186 err = Tcl_CreateCommand( 2582 err = Tcl_CreateCommand(
2187 Tkapp_Interp(self), cmdName, PythonCmd, 2583 Tkapp_Interp(self), cmdName, PythonCmd,
2188 (ClientData)data, PythonCmdDelete) == NULL; 2584 (ClientData)data, PythonCmdDelete) == NULL;
2189 LEAVE_TCL 2585 LEAVE_TCL
2190 } 2586 }
2191 if (err) { 2587 if (err) {
2192 PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); 2588 PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
2193 PyMem_DEL(data); 2589 PyMem_DEL(data);
2194 return NULL; 2590 return NULL;
2195 } 2591 }
2196 2592
2197 Py_RETURN_NONE; 2593 Py_INCREF(Py_None);
2594 return Py_None;
2198 } 2595 }
2199 2596
2200 2597
2201 2598
2202 static PyObject * 2599 static PyObject *
2203 Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args) 2600 Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args)
2204 { 2601 {
2205 TkappObject *self = (TkappObject*)selfptr; 2602 TkappObject *self = (TkappObject*)selfptr;
2206 char *cmdName; 2603 char *cmdName;
2207 int err; 2604 int err;
2208 2605
2209 if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName)) 2606 if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName))
2210 return NULL; 2607 return NULL;
2608 CHECK_STRING_LENGTH(cmdName);
2211 2609
2212 #ifdef WITH_THREAD 2610 #ifdef WITH_THREAD
2213 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { 2611 if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
2214 Tcl_Condition cond = NULL; 2612 Tcl_Condition cond = NULL;
2215 CommandEvent *ev; 2613 CommandEvent *ev;
2216 ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); 2614 ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
2615 if (ev == NULL) {
2616 PyErr_NoMemory();
2617 return NULL;
2618 }
2217 ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; 2619 ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2218 ev->interp = self->interp; 2620 ev->interp = self->interp;
2219 ev->create = 0; 2621 ev->create = 0;
2220 ev->name = cmdName; 2622 ev->name = cmdName;
2221 ev->status = &err; 2623 ev->status = &err;
2222 ev->done = &cond; 2624 ev->done = &cond;
2223 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, 2625 Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond,
2224 &command_mutex); 2626 &command_mutex);
2225 Tcl_ConditionFinalize(&cond); 2627 Tcl_ConditionFinalize(&cond);
2226 } 2628 }
2227 else 2629 else
2228 #endif 2630 #endif
2229 { 2631 {
2230 ENTER_TCL 2632 ENTER_TCL
2231 err = Tcl_DeleteCommand(self->interp, cmdName); 2633 err = Tcl_DeleteCommand(self->interp, cmdName);
2232 LEAVE_TCL 2634 LEAVE_TCL
2233 } 2635 }
2234 if (err == -1) { 2636 if (err == -1) {
2235 PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); 2637 PyErr_SetString(Tkinter_TclError, "can't delete Tcl command");
2236 return NULL; 2638 return NULL;
2237 } 2639 }
2238 Py_RETURN_NONE; 2640 Py_INCREF(Py_None);
2641 return Py_None;
2239 } 2642 }
2240 2643
2241 2644
2242 2645
2243 #ifdef HAVE_CREATEFILEHANDLER 2646 #ifdef HAVE_CREATEFILEHANDLER
2244 /** File Handler **/ 2647 /** File Handler **/
2245 2648
2246 typedef struct _fhcdata { 2649 typedef struct _fhcdata {
2247 PyObject *func; 2650 PyObject *func;
2248 PyObject *file; 2651 PyObject *file;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2310 } 2713 }
2311 2714
2312 static PyObject * 2715 static PyObject *
2313 Tkapp_CreateFileHandler(PyObject *self, PyObject *args) 2716 Tkapp_CreateFileHandler(PyObject *self, PyObject *args)
2314 /* args is (file, mask, func) */ 2717 /* args is (file, mask, func) */
2315 { 2718 {
2316 FileHandler_ClientData *data; 2719 FileHandler_ClientData *data;
2317 PyObject *file, *func; 2720 PyObject *file, *func;
2318 int mask, tfile; 2721 int mask, tfile;
2319 2722
2723 if (!self && Py_Py3kWarningFlag) {
2724 if (PyErr_Warn(PyExc_DeprecationWarning,
2725 "_tkinter.createfilehandler is gone in 3.x") < 0 )
2726 return NULL;
2727 }
2728
2320 if (!PyArg_ParseTuple(args, "OiO:createfilehandler", 2729 if (!PyArg_ParseTuple(args, "OiO:createfilehandler",
2321 &file, &mask, &func)) 2730 &file, &mask, &func))
2322 return NULL; 2731 return NULL;
2323 2732
2324 CHECK_TCL_APPARTMENT; 2733 #ifdef WITH_THREAD
2734 if (!self && !tcl_lock) {
2735 /* We don't have the Tcl lock since Tcl is threaded. */
2736 PyErr_SetString(PyExc_RuntimeError,
2737 "_tkinter.createfilehandler not supported "
2738 "for threaded Tcl");
2739 return NULL;
2740 }
2741 #endif
2742
2743 if (self) {
2744 CHECK_TCL_APPARTMENT;
2745 }
2325 2746
2326 tfile = PyObject_AsFileDescriptor(file); 2747 tfile = PyObject_AsFileDescriptor(file);
2327 if (tfile < 0) 2748 if (tfile < 0)
2328 return NULL; 2749 return NULL;
2329 if (!PyCallable_Check(func)) { 2750 if (!PyCallable_Check(func)) {
2330 PyErr_SetString(PyExc_TypeError, "bad argument list"); 2751 PyErr_SetString(PyExc_TypeError, "bad argument list");
2331 return NULL; 2752 return NULL;
2332 } 2753 }
2333 2754
2334 data = NewFHCD(func, file, tfile); 2755 data = NewFHCD(func, file, tfile);
2335 if (data == NULL) 2756 if (data == NULL)
2336 return NULL; 2757 return NULL;
2337 2758
2338 /* Ought to check for null Tcl_File object... */ 2759 /* Ought to check for null Tcl_File object... */
2339 ENTER_TCL 2760 ENTER_TCL
2340 Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); 2761 Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data);
2341 LEAVE_TCL 2762 LEAVE_TCL
2342 Py_RETURN_NONE; 2763 Py_INCREF(Py_None);
2764 return Py_None;
2343 } 2765 }
2344 2766
2345 static PyObject * 2767 static PyObject *
2346 Tkapp_DeleteFileHandler(PyObject *self, PyObject *args) 2768 Tkapp_DeleteFileHandler(PyObject *self, PyObject *args)
2347 { 2769 {
2348 PyObject *file; 2770 PyObject *file;
2349 int tfile; 2771 int tfile;
2350 2772
2773 if (!self && Py_Py3kWarningFlag) {
2774 if (PyErr_Warn(PyExc_DeprecationWarning,
2775 "_tkinter.deletefilehandler is gone in 3.x") < 0 )
2776 return NULL;
2777 }
2778
2351 if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file)) 2779 if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file))
2352 return NULL; 2780 return NULL;
2353 2781
2354 CHECK_TCL_APPARTMENT; 2782 #ifdef WITH_THREAD
2783 if (!self && !tcl_lock) {
2784 /* We don't have the Tcl lock since Tcl is threaded. */
2785 PyErr_SetString(PyExc_RuntimeError,
2786 "_tkinter.deletefilehandler not supported "
2787 "for threaded Tcl");
2788 return NULL;
2789 }
2790 #endif
2791
2792 if (self) {
2793 CHECK_TCL_APPARTMENT;
2794 }
2355 2795
2356 tfile = PyObject_AsFileDescriptor(file); 2796 tfile = PyObject_AsFileDescriptor(file);
2357 if (tfile < 0) 2797 if (tfile < 0)
2358 return NULL; 2798 return NULL;
2359 2799
2360 DeleteFHCD(tfile); 2800 DeleteFHCD(tfile);
2361 2801
2362 /* Ought to check for null Tcl_File object... */ 2802 /* Ought to check for null Tcl_File object... */
2363 ENTER_TCL 2803 ENTER_TCL
2364 Tcl_DeleteFileHandler(tfile); 2804 Tcl_DeleteFileHandler(tfile);
2365 LEAVE_TCL 2805 LEAVE_TCL
2366 Py_RETURN_NONE; 2806 Py_INCREF(Py_None);
2807 return Py_None;
2367 } 2808 }
2368 #endif /* HAVE_CREATEFILEHANDLER */ 2809 #endif /* HAVE_CREATEFILEHANDLER */
2369 2810
2370 2811
2371 /**** Tktt Object (timer token) ****/ 2812 /**** Tktt Object (timer token) ****/
2372 2813
2373 static PyObject *Tktt_Type; 2814 static PyTypeObject Tktt_Type;
2374 2815
2375 typedef struct { 2816 typedef struct {
2376 PyObject_HEAD 2817 PyObject_HEAD
2377 Tcl_TimerToken token; 2818 Tcl_TimerToken token;
2378 PyObject *func; 2819 PyObject *func;
2379 } TkttObject; 2820 } TkttObject;
2380 2821
2381 static PyObject * 2822 static PyObject *
2382 Tktt_DeleteTimerHandler(PyObject *self, PyObject *args) 2823 Tktt_DeleteTimerHandler(PyObject *self, PyObject *args)
2383 { 2824 {
2384 TkttObject *v = (TkttObject *)self; 2825 TkttObject *v = (TkttObject *)self;
2385 PyObject *func = v->func; 2826 PyObject *func = v->func;
2386 2827
2387 if (!PyArg_ParseTuple(args, ":deletetimerhandler")) 2828 if (!PyArg_ParseTuple(args, ":deletetimerhandler"))
2388 return NULL; 2829 return NULL;
2389 if (v->token != NULL) { 2830 if (v->token != NULL) {
2390 Tcl_DeleteTimerHandler(v->token); 2831 Tcl_DeleteTimerHandler(v->token);
2391 v->token = NULL; 2832 v->token = NULL;
2392 } 2833 }
2393 if (func != NULL) { 2834 if (func != NULL) {
2394 v->func = NULL; 2835 v->func = NULL;
2395 Py_DECREF(func); 2836 Py_DECREF(func);
2396 Py_DECREF(v); /* See Tktt_New() */ 2837 Py_DECREF(v); /* See Tktt_New() */
2397 } 2838 }
2398 Py_RETURN_NONE; 2839 Py_INCREF(Py_None);
2840 return Py_None;
2399 } 2841 }
2400 2842
2401 static PyMethodDef Tktt_methods[] = 2843 static PyMethodDef Tktt_methods[] =
2402 { 2844 {
2403 {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS}, 2845 {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS},
2404 {NULL, NULL} 2846 {NULL, NULL}
2405 }; 2847 };
2406 2848
2407 static TkttObject * 2849 static TkttObject *
2408 Tktt_New(PyObject *func) 2850 Tktt_New(PyObject *func)
2409 { 2851 {
2410 TkttObject *v; 2852 TkttObject *v;
2411 2853
2412 v = PyObject_New(TkttObject, (PyTypeObject *) Tktt_Type); 2854 v = PyObject_New(TkttObject, &Tktt_Type);
2413 if (v == NULL) 2855 if (v == NULL)
2414 return NULL; 2856 return NULL;
2415 Py_INCREF(Tktt_Type);
2416 2857
2417 Py_INCREF(func); 2858 Py_INCREF(func);
2418 v->token = NULL; 2859 v->token = NULL;
2419 v->func = func; 2860 v->func = func;
2420 2861
2421 /* Extra reference, deleted when called or when handler is deleted */ 2862 /* Extra reference, deleted when called or when handler is deleted */
2422 Py_INCREF(v); 2863 Py_INCREF(v);
2423 return v; 2864 return v;
2424 } 2865 }
2425 2866
2426 static void 2867 static void
2427 Tktt_Dealloc(PyObject *self) 2868 Tktt_Dealloc(PyObject *self)
2428 { 2869 {
2429 TkttObject *v = (TkttObject *)self; 2870 TkttObject *v = (TkttObject *)self;
2430 PyObject *func = v->func; 2871 PyObject *func = v->func;
2431 PyObject *tp = (PyObject *) Py_TYPE(self);
2432 2872
2433 Py_XDECREF(func); 2873 Py_XDECREF(func);
2434 2874
2435 PyObject_Del(self); 2875 PyObject_Del(self);
2436 Py_DECREF(tp);
2437 } 2876 }
2438 2877
2439 static PyObject * 2878 static PyObject *
2440 Tktt_Repr(PyObject *self) 2879 Tktt_Repr(PyObject *self)
2441 { 2880 {
2442 TkttObject *v = (TkttObject *)self; 2881 TkttObject *v = (TkttObject *)self;
2443 return PyUnicode_FromFormat("<tktimertoken at %p%s>", 2882 char buf[100];
2444 v, 2883
2445 v->func == NULL ? ", handler deleted" : ""); 2884 PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v,
2446 } 2885 v->func == NULL ? ", handler deleted" : "");
2447 2886 return PyString_FromString(buf);
2448 static PyType_Slot Tktt_Type_slots[] = { 2887 }
2449 {Py_tp_dealloc, Tktt_Dealloc}, 2888
2450 {Py_tp_repr, Tktt_Repr}, 2889 static PyObject *
2451 {Py_tp_methods, Tktt_methods}, 2890 Tktt_GetAttr(PyObject *self, char *name)
2452 {0, 0} 2891 {
2892 return Py_FindMethod(Tktt_methods, self, name);
2893 }
2894
2895 static PyTypeObject Tktt_Type =
2896 {
2897 PyVarObject_HEAD_INIT(NULL, 0)
2898 "tktimertoken", /*tp_name */
2899 sizeof(TkttObject), /*tp_basicsize */
2900 0, /*tp_itemsize */
2901 Tktt_Dealloc, /*tp_dealloc */
2902 0, /*tp_print */
2903 Tktt_GetAttr, /*tp_getattr */
2904 0, /*tp_setattr */
2905 0, /*tp_compare */
2906 Tktt_Repr, /*tp_repr */
2907 0, /*tp_as_number */
2908 0, /*tp_as_sequence */
2909 0, /*tp_as_mapping */
2910 0, /*tp_hash */
2453 }; 2911 };
2454 2912
2455 static PyType_Spec Tktt_Type_spec = {
2456 "tktimertoken",
2457 sizeof(TkttObject),
2458 0,
2459 Py_TPFLAGS_DEFAULT,
2460 Tktt_Type_slots,
2461 };
2462 2913
2463 2914
2464 /** Timer Handler **/ 2915 /** Timer Handler **/
2465 2916
2466 static void 2917 static void
2467 TimerHandler(ClientData clientData) 2918 TimerHandler(ClientData clientData)
2468 { 2919 {
2469 TkttObject *v = (TkttObject *)clientData; 2920 TkttObject *v = (TkttObject *)clientData;
2470 PyObject *func = v->func; 2921 PyObject *func = v->func;
2471 PyObject *res; 2922 PyObject *res;
(...skipping 19 matching lines...) Expand all
2491 LEAVE_PYTHON 2942 LEAVE_PYTHON
2492 } 2943 }
2493 2944
2494 static PyObject * 2945 static PyObject *
2495 Tkapp_CreateTimerHandler(PyObject *self, PyObject *args) 2946 Tkapp_CreateTimerHandler(PyObject *self, PyObject *args)
2496 { 2947 {
2497 int milliseconds; 2948 int milliseconds;
2498 PyObject *func; 2949 PyObject *func;
2499 TkttObject *v; 2950 TkttObject *v;
2500 2951
2952 if (!self && Py_Py3kWarningFlag) {
2953 if (PyErr_Warn(PyExc_DeprecationWarning,
2954 "_tkinter.createtimerhandler is gone in 3.x") < 0)
2955 return NULL;
2956 }
2957
2501 if (!PyArg_ParseTuple(args, "iO:createtimerhandler", 2958 if (!PyArg_ParseTuple(args, "iO:createtimerhandler",
2502 &milliseconds, &func)) 2959 &milliseconds, &func))
2503 return NULL; 2960 return NULL;
2504 if (!PyCallable_Check(func)) { 2961 if (!PyCallable_Check(func)) {
2505 PyErr_SetString(PyExc_TypeError, "bad argument list"); 2962 PyErr_SetString(PyExc_TypeError, "bad argument list");
2506 return NULL; 2963 return NULL;
2507 } 2964 }
2508 2965
2509 CHECK_TCL_APPARTMENT; 2966 #ifdef WITH_THREAD
2967 if (!self && !tcl_lock) {
2968 /* We don't have the Tcl lock since Tcl is threaded. */
2969 PyErr_SetString(PyExc_RuntimeError,
2970 "_tkinter.createtimerhandler not supported "
2971 "for threaded Tcl");
2972 return NULL;
2973 }
2974 #endif
2975
2976 if (self) {
2977 CHECK_TCL_APPARTMENT;
2978 }
2510 2979
2511 v = Tktt_New(func); 2980 v = Tktt_New(func);
2512 if (v) { 2981 if (v) {
2513 v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, 2982 v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler,
2514 (ClientData)v); 2983 (ClientData)v);
2515 } 2984 }
2516 2985
2517 return (PyObject *) v; 2986 return (PyObject *) v;
2518 } 2987 }
2519 2988
2520 2989
2521 /** Event Loop **/ 2990 /** Event Loop **/
2522 2991
2523 static PyObject * 2992 static PyObject *
2524 Tkapp_MainLoop(PyObject *selfptr, PyObject *args) 2993 Tkapp_MainLoop(PyObject *selfptr, PyObject *args)
2525 { 2994 {
2526 int threshold = 0; 2995 int threshold = 0;
2527 TkappObject *self = (TkappObject*)selfptr; 2996 TkappObject *self = (TkappObject*)selfptr;
2528 #ifdef WITH_THREAD 2997 #ifdef WITH_THREAD
2529 PyThreadState *tstate = PyThreadState_Get(); 2998 PyThreadState *tstate = PyThreadState_Get();
2530 #endif 2999 #endif
2531 3000
3001 if (!self && Py_Py3kWarningFlag) {
3002 if (PyErr_Warn(PyExc_DeprecationWarning,
3003 "_tkinter.mainloop is gone in 3.x") < 0)
3004 return NULL;
3005 }
3006
2532 if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold)) 3007 if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold))
2533 return NULL; 3008 return NULL;
2534 3009
2535 CHECK_TCL_APPARTMENT; 3010 #ifdef WITH_THREAD
2536 self->dispatching = 1; 3011 if (!self && !tcl_lock) {
3012 /* We don't have the Tcl lock since Tcl is threaded. */
3013 PyErr_SetString(PyExc_RuntimeError,
3014 "_tkinter.mainloop not supported "
3015 "for threaded Tcl");
3016 return NULL;
3017 }
3018 #endif
3019
3020 if (self) {
3021 CHECK_TCL_APPARTMENT;
3022 self->dispatching = 1;
3023 }
2537 3024
2538 quitMainLoop = 0; 3025 quitMainLoop = 0;
2539 while (Tk_GetNumMainWindows() > threshold && 3026 while (Tk_GetNumMainWindows() > threshold &&
2540 !quitMainLoop && 3027 !quitMainLoop &&
2541 !errorInCmd) 3028 !errorInCmd)
2542 { 3029 {
2543 int result; 3030 int result;
2544 3031
2545 #ifdef WITH_THREAD 3032 #ifdef WITH_THREAD
2546 if (self->threaded) { 3033 if (self && self->threaded) {
2547 /* Allow other Python threads to run. */ 3034 /* Allow other Python threads to run. */
2548 ENTER_TCL 3035 ENTER_TCL
2549 result = Tcl_DoOneEvent(0); 3036 result = Tcl_DoOneEvent(0);
2550 LEAVE_TCL 3037 LEAVE_TCL
2551 } 3038 }
2552 else { 3039 else {
2553 Py_BEGIN_ALLOW_THREADS 3040 Py_BEGIN_ALLOW_THREADS
2554 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); 3041 if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
2555 tcl_tstate = tstate; 3042 tcl_tstate = tstate;
2556 result = Tcl_DoOneEvent(TCL_DONT_WAIT); 3043 result = Tcl_DoOneEvent(TCL_DONT_WAIT);
2557 tcl_tstate = NULL; 3044 tcl_tstate = NULL;
2558 if(tcl_lock)PyThread_release_lock(tcl_lock); 3045 if(tcl_lock)PyThread_release_lock(tcl_lock);
2559 if (result == 0) 3046 if (result == 0)
2560 Sleep(Tkinter_busywaitinterval); 3047 Sleep(Tkinter_busywaitinterval);
2561 Py_END_ALLOW_THREADS 3048 Py_END_ALLOW_THREADS
2562 } 3049 }
2563 #else 3050 #else
2564 result = Tcl_DoOneEvent(0); 3051 result = Tcl_DoOneEvent(0);
2565 #endif 3052 #endif
2566 3053
2567 if (PyErr_CheckSignals() != 0) { 3054 if (PyErr_CheckSignals() != 0) {
2568 self->dispatching = 0; 3055 if (self)
3056 self->dispatching = 0;
2569 return NULL; 3057 return NULL;
2570 } 3058 }
2571 if (result < 0) 3059 if (result < 0)
2572 break; 3060 break;
2573 } 3061 }
2574 self->dispatching = 0; 3062 if (self)
3063 self->dispatching = 0;
2575 quitMainLoop = 0; 3064 quitMainLoop = 0;
2576 3065
2577 if (errorInCmd) { 3066 if (errorInCmd) {
2578 errorInCmd = 0; 3067 errorInCmd = 0;
2579 PyErr_Restore(excInCmd, valInCmd, trbInCmd); 3068 PyErr_Restore(excInCmd, valInCmd, trbInCmd);
2580 excInCmd = valInCmd = trbInCmd = NULL; 3069 excInCmd = valInCmd = trbInCmd = NULL;
2581 return NULL; 3070 return NULL;
2582 } 3071 }
2583 Py_RETURN_NONE; 3072 Py_INCREF(Py_None);
3073 return Py_None;
2584 } 3074 }
2585 3075
2586 static PyObject * 3076 static PyObject *
2587 Tkapp_DoOneEvent(PyObject *self, PyObject *args) 3077 Tkapp_DoOneEvent(PyObject *self, PyObject *args)
2588 { 3078 {
2589 int flags = 0; 3079 int flags = 0;
2590 int rv; 3080 int rv;
3081
3082 if (!self && Py_Py3kWarningFlag) {
3083 if (PyErr_Warn(PyExc_DeprecationWarning,
3084 "_tkinter.dooneevent is gone in 3.x") < 0)
3085 return NULL;
3086 }
2591 3087
2592 if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags)) 3088 if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags))
2593 return NULL; 3089 return NULL;
2594 3090
2595 ENTER_TCL 3091 ENTER_TCL
2596 rv = Tcl_DoOneEvent(flags); 3092 rv = Tcl_DoOneEvent(flags);
2597 LEAVE_TCL 3093 LEAVE_TCL
2598 return Py_BuildValue("i", rv); 3094 return Py_BuildValue("i", rv);
2599 } 3095 }
2600 3096
2601 static PyObject * 3097 static PyObject *
2602 Tkapp_Quit(PyObject *self, PyObject *args) 3098 Tkapp_Quit(PyObject *self, PyObject *args)
2603 { 3099 {
2604 3100
3101 if (!self && Py_Py3kWarningFlag) {
3102 if (PyErr_Warn(PyExc_DeprecationWarning,
3103 "_tkinter.quit is gone in 3.x") < 0)
3104 return NULL;
3105 }
3106
2605 if (!PyArg_ParseTuple(args, ":quit")) 3107 if (!PyArg_ParseTuple(args, ":quit"))
2606 return NULL; 3108 return NULL;
2607 3109
2608 quitMainLoop = 1; 3110 quitMainLoop = 1;
2609 Py_RETURN_NONE; 3111 Py_INCREF(Py_None);
3112 return Py_None;
2610 } 3113 }
2611 3114
2612 static PyObject * 3115 static PyObject *
2613 Tkapp_InterpAddr(PyObject *self, PyObject *args) 3116 Tkapp_InterpAddr(PyObject *self, PyObject *args)
2614 { 3117 {
2615 3118
2616 if (!PyArg_ParseTuple(args, ":interpaddr")) 3119 if (!PyArg_ParseTuple(args, ":interpaddr"))
2617 return NULL; 3120 return NULL;
2618 3121
2619 return PyLong_FromVoidPtr(Tkapp_Interp(self)); 3122 return PyLong_FromVoidPtr(Tkapp_Interp(self));
(...skipping 29 matching lines...) Expand all
2649 Tkinter_Error(self); 3152 Tkinter_Error(self);
2650 } else { 3153 } else {
2651 _tk_exists = Tkapp_Result(self); 3154 _tk_exists = Tkapp_Result(self);
2652 } 3155 }
2653 LEAVE_OVERLAP_TCL 3156 LEAVE_OVERLAP_TCL
2654 if (err == TCL_ERROR) { 3157 if (err == TCL_ERROR) {
2655 return NULL; 3158 return NULL;
2656 } 3159 }
2657 if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) { 3160 if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) {
2658 if (Tk_Init(interp) == TCL_ERROR) { 3161 if (Tk_Init(interp) == TCL_ERROR) {
2659 PyErr_SetString(Tkinter_TclError, 3162 PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(s elf)));
2660 Tcl_GetStringResult(Tkapp_Interp(self)));
2661 #ifdef TKINTER_PROTECT_LOADTK 3163 #ifdef TKINTER_PROTECT_LOADTK
2662 tk_load_failed = 1; 3164 tk_load_failed = 1;
2663 #endif 3165 #endif
2664 return NULL; 3166 return NULL;
2665 } 3167 }
2666 } 3168 }
2667 Py_RETURN_NONE; 3169 Py_INCREF(Py_None);
3170 return Py_None;
2668 } 3171 }
2669 3172
2670 static PyObject * 3173 static PyObject *
2671 Tkapp_WantObjects(PyObject *self, PyObject *args) 3174 Tkapp_WantObjects(PyObject *self, PyObject *args)
2672 { 3175 {
2673 3176
2674 int wantobjects = -1; 3177 int wantobjects = -1;
2675 if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects)) 3178 if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects))
2676 return NULL; 3179 return NULL;
2677 if (wantobjects == -1) 3180 if (wantobjects == -1)
2678 return PyBool_FromLong(((TkappObject*)self)->wantobjects); 3181 return PyBool_FromLong(((TkappObject*)self)->wantobjects);
2679 ((TkappObject*)self)->wantobjects = wantobjects; 3182 ((TkappObject*)self)->wantobjects = wantobjects;
2680 3183
2681 Py_RETURN_NONE; 3184 Py_INCREF(Py_None);
3185 return Py_None;
2682 } 3186 }
2683 3187
2684 static PyObject * 3188 static PyObject *
2685 Tkapp_WillDispatch(PyObject *self, PyObject *args) 3189 Tkapp_WillDispatch(PyObject *self, PyObject *args)
2686 { 3190 {
2687 3191
2688 ((TkappObject*)self)->dispatching = 1; 3192 ((TkappObject*)self)->dispatching = 1;
2689 3193
2690 Py_RETURN_NONE; 3194 Py_INCREF(Py_None);
3195 return Py_None;
3196 }
3197
3198 /* Convert Python string or any buffer compatible object to Tcl byte-array
3199 * object. Use it to pass binary data (e.g. image's data) to Tcl/Tk commands.
3200 */
3201 static PyObject *
3202 Tkapp_CreateByteArray(PyObject *self, PyObject *args)
3203 {
3204 Py_buffer view;
3205 Tcl_Obj* obj;
3206 PyObject *res = NULL;
3207
3208 if (!PyArg_ParseTuple(args, "s*:_createbytearray", &view))
3209 return NULL;
3210
3211 if (view.len >= INT_MAX) {
3212 PyErr_SetString(PyExc_OverflowError, "string is too long");
3213 return NULL;
3214 }
3215 obj = Tcl_NewByteArrayObj(view.buf, (int)view.len);
3216 if (obj == NULL) {
3217 PyBuffer_Release(&view);
3218 return Tkinter_Error(self);
3219 }
3220 res = newPyTclObject(obj);
3221 PyBuffer_Release(&view);
3222 return res;
2691 } 3223 }
2692 3224
2693 3225
2694 /**** Tkapp Method List ****/ 3226 /**** Tkapp Method List ****/
2695 3227
2696 static PyMethodDef Tkapp_methods[] = 3228 static PyMethodDef Tkapp_methods[] =
2697 { 3229 {
2698 {"willdispatch", Tkapp_WillDispatch, METH_NOARGS}, 3230 {"willdispatch", Tkapp_WillDispatch, METH_NOARGS},
2699 {"wantobjects", Tkapp_WantObjects, METH_VARARGS}, 3231 {"wantobjects", Tkapp_WantObjects, METH_VARARGS},
2700 {"call", Tkapp_Call, METH_VARARGS}, 3232 {"call", Tkapp_Call, METH_VARARGS},
3233 {"globalcall", Tkapp_GlobalCall, METH_VARARGS},
2701 {"eval", Tkapp_Eval, METH_VARARGS}, 3234 {"eval", Tkapp_Eval, METH_VARARGS},
3235 {"globaleval", Tkapp_GlobalEval, METH_VARARGS},
2702 {"evalfile", Tkapp_EvalFile, METH_VARARGS}, 3236 {"evalfile", Tkapp_EvalFile, METH_VARARGS},
2703 {"record", Tkapp_Record, METH_VARARGS}, 3237 {"record", Tkapp_Record, METH_VARARGS},
2704 {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS}, 3238 {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS},
2705 {"setvar", Tkapp_SetVar, METH_VARARGS}, 3239 {"setvar", Tkapp_SetVar, METH_VARARGS},
2706 {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS}, 3240 {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS},
2707 {"getvar", Tkapp_GetVar, METH_VARARGS}, 3241 {"getvar", Tkapp_GetVar, METH_VARARGS},
2708 {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS}, 3242 {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS},
2709 {"unsetvar", Tkapp_UnsetVar, METH_VARARGS}, 3243 {"unsetvar", Tkapp_UnsetVar, METH_VARARGS},
2710 {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS}, 3244 {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS},
2711 {"getint", Tkapp_GetInt, METH_VARARGS}, 3245 {"getint", Tkapp_GetInt, METH_VARARGS},
2712 {"getdouble", Tkapp_GetDouble, METH_VARARGS}, 3246 {"getdouble", Tkapp_GetDouble, METH_VARARGS},
2713 {"getboolean", Tkapp_GetBoolean, METH_VARARGS}, 3247 {"getboolean", Tkapp_GetBoolean, METH_O},
2714 {"exprstring", Tkapp_ExprString, METH_VARARGS}, 3248 {"exprstring", Tkapp_ExprString, METH_VARARGS},
2715 {"exprlong", Tkapp_ExprLong, METH_VARARGS}, 3249 {"exprlong", Tkapp_ExprLong, METH_VARARGS},
2716 {"exprdouble", Tkapp_ExprDouble, METH_VARARGS}, 3250 {"exprdouble", Tkapp_ExprDouble, METH_VARARGS},
2717 {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS}, 3251 {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS},
2718 {"splitlist", Tkapp_SplitList, METH_VARARGS}, 3252 {"splitlist", Tkapp_SplitList, METH_VARARGS},
2719 {"split", Tkapp_Split, METH_VARARGS}, 3253 {"split", Tkapp_Split, METH_VARARGS},
3254 {"merge", Tkapp_Merge, METH_VARARGS},
2720 {"createcommand", Tkapp_CreateCommand, METH_VARARGS}, 3255 {"createcommand", Tkapp_CreateCommand, METH_VARARGS},
2721 {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS}, 3256 {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS},
2722 #ifdef HAVE_CREATEFILEHANDLER 3257 #ifdef HAVE_CREATEFILEHANDLER
2723 {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS}, 3258 {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS},
2724 {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS}, 3259 {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS},
2725 #endif 3260 #endif
2726 {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS}, 3261 {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS},
2727 {"mainloop", Tkapp_MainLoop, METH_VARARGS}, 3262 {"mainloop", Tkapp_MainLoop, METH_VARARGS},
2728 {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS}, 3263 {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS},
2729 {"quit", Tkapp_Quit, METH_VARARGS}, 3264 {"quit", Tkapp_Quit, METH_VARARGS},
2730 {"interpaddr", Tkapp_InterpAddr, METH_VARARGS}, 3265 {"interpaddr", Tkapp_InterpAddr, METH_VARARGS},
2731 {"loadtk", Tkapp_TkInit, METH_NOARGS}, 3266 {"loadtk", Tkapp_TkInit, METH_NOARGS},
3267 {"_createbytearray", Tkapp_CreateByteArray, METH_VARARGS},
2732 {NULL, NULL} 3268 {NULL, NULL}
2733 }; 3269 };
2734 3270
2735 3271
2736 3272
2737 /**** Tkapp Type Methods ****/ 3273 /**** Tkapp Type Methods ****/
2738 3274
2739 static void 3275 static void
2740 Tkapp_Dealloc(PyObject *self) 3276 Tkapp_Dealloc(PyObject *self)
2741 { 3277 {
2742 PyObject *tp = (PyObject *) Py_TYPE(self);
2743 /*CHECK_TCL_APPARTMENT;*/ 3278 /*CHECK_TCL_APPARTMENT;*/
2744 ENTER_TCL 3279 ENTER_TCL
2745 Tcl_DeleteInterp(Tkapp_Interp(self)); 3280 Tcl_DeleteInterp(Tkapp_Interp(self));
2746 LEAVE_TCL 3281 LEAVE_TCL
2747 PyObject_Del(self); 3282 PyObject_Del(self);
2748 Py_DECREF(tp);
2749 DisableEventHook(); 3283 DisableEventHook();
2750 } 3284 }
2751 3285
2752 static PyType_Slot Tkapp_Type_slots[] = { 3286 static PyObject *
2753 {Py_tp_dealloc, Tkapp_Dealloc}, 3287 Tkapp_GetAttr(PyObject *self, char *name)
2754 {Py_tp_methods, Tkapp_methods}, 3288 {
2755 {0, 0} 3289 return Py_FindMethod(Tkapp_methods, self, name);
2756 }; 3290 }
2757 3291
2758 3292 static PyTypeObject Tkapp_Type =
2759 static PyType_Spec Tkapp_Type_spec = { 3293 {
2760 "tkapp", 3294 PyVarObject_HEAD_INIT(NULL, 0)
2761 sizeof(TkappObject), 3295 "tkapp", /*tp_name */
2762 0, 3296 sizeof(TkappObject), /*tp_basicsize */
2763 Py_TPFLAGS_DEFAULT, 3297 0, /*tp_itemsize */
2764 Tkapp_Type_slots, 3298 Tkapp_Dealloc, /*tp_dealloc */
3299 0, /*tp_print */
3300 Tkapp_GetAttr, /*tp_getattr */
3301 0, /*tp_setattr */
3302 0, /*tp_compare */
3303 0, /*tp_repr */
3304 0, /*tp_as_number */
3305 0, /*tp_as_sequence */
3306 0, /*tp_as_mapping */
3307 0, /*tp_hash */
2765 }; 3308 };
2766 3309
2767 3310
2768 3311
2769 /**** Tkinter Module ****/ 3312 /**** Tkinter Module ****/
2770 3313
2771 typedef struct { 3314 typedef struct {
2772 PyObject* tuple; 3315 PyObject* tuple;
2773 int size; /* current size */ 3316 int size; /* current size */
2774 int maxsize; /* allocated size */ 3317 int maxsize; /* allocated size */
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
2876 if (_PyTuple_Resize(&context.tuple, context.size)) 3419 if (_PyTuple_Resize(&context.tuple, context.size))
2877 return NULL; 3420 return NULL;
2878 3421
2879 return context.tuple; 3422 return context.tuple;
2880 } 3423 }
2881 3424
2882 static PyObject * 3425 static PyObject *
2883 Tkinter_Create(PyObject *self, PyObject *args) 3426 Tkinter_Create(PyObject *self, PyObject *args)
2884 { 3427 {
2885 char *screenName = NULL; 3428 char *screenName = NULL;
2886 char *baseName = NULL; /* XXX this is not used anymore; 3429 char *baseName = NULL;
2887 try getting rid of it. */
2888 char *className = NULL; 3430 char *className = NULL;
2889 int interactive = 0; 3431 int interactive = 0;
2890 int wantobjects = 0; 3432 int wantobjects = 0;
2891 int wantTk = 1; /* If false, then Tk_Init() doesn't get called */ 3433 int wantTk = 1; /* If false, then Tk_Init() doesn't get called */
2892 int sync = 0; /* pass -sync to wish */ 3434 int sync = 0; /* pass -sync to wish */
2893 char *use = NULL; /* pass -use to wish */ 3435 char *use = NULL; /* pass -use to wish */
2894 3436
3437 baseName = strrchr(Py_GetProgramName(), '/');
3438 if (baseName != NULL)
3439 baseName++;
3440 else
3441 baseName = Py_GetProgramName();
2895 className = "Tk"; 3442 className = "Tk";
2896 3443
2897 if (!PyArg_ParseTuple(args, "|zssiiiiz:create", 3444 if (!PyArg_ParseTuple(args, "|zssiiiiz:create",
2898 &screenName, &baseName, &className, 3445 &screenName, &baseName, &className,
2899 &interactive, &wantobjects, &wantTk, 3446 &interactive, &wantobjects, &wantTk,
2900 &sync, &use)) 3447 &sync, &use))
2901 return NULL; 3448 return NULL;
2902 3449 CHECK_STRING_LENGTH(screenName);
2903 return (PyObject *) Tkapp_New(screenName, className, 3450 CHECK_STRING_LENGTH(baseName);
2904 interactive, wantobjects, wantTk, 3451 CHECK_STRING_LENGTH(className);
3452 CHECK_STRING_LENGTH(use);
3453
3454 return (PyObject *) Tkapp_New(screenName, baseName, className,
3455 interactive, wantobjects, wantTk,
2905 sync, use); 3456 sync, use);
2906 } 3457 }
2907 3458
2908 static PyObject * 3459 static PyObject *
2909 Tkinter_setbusywaitinterval(PyObject *self, PyObject *args) 3460 Tkinter_setbusywaitinterval(PyObject *self, PyObject *args)
2910 { 3461 {
2911 int new_val; 3462 int new_val;
2912 if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val)) 3463 if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val))
2913 return NULL; 3464 return NULL;
2914 if (new_val < 0) { 3465 if (new_val < 0) {
2915 PyErr_SetString(PyExc_ValueError, 3466 PyErr_SetString(PyExc_ValueError,
2916 "busywaitinterval must be >= 0"); 3467 "busywaitinterval must be >= 0");
2917 return NULL; 3468 return NULL;
2918 } 3469 }
2919 Tkinter_busywaitinterval = new_val; 3470 Tkinter_busywaitinterval = new_val;
2920 Py_RETURN_NONE; 3471 Py_INCREF(Py_None);
3472 return Py_None;
2921 } 3473 }
2922 3474
2923 static char setbusywaitinterval_doc[] = 3475 static char setbusywaitinterval_doc[] =
2924 "setbusywaitinterval(n) -> None\n\ 3476 "setbusywaitinterval(n) -> None\n\
2925 \n\ 3477 \n\
2926 Set the busy-wait interval in milliseconds between successive\n\ 3478 Set the busy-wait interval in milliseconds between successive\n\
2927 calls to Tcl_DoOneEvent in a threaded Python interpreter.\n\ 3479 calls to Tcl_DoOneEvent in a threaded Python interpreter.\n\
2928 It should be set to a divisor of the maximum time between\n\ 3480 It should be set to a divisor of the maximum time between\n\
2929 frames in an animation."; 3481 frames in an animation.";
2930 3482
2931 static PyObject * 3483 static PyObject *
2932 Tkinter_getbusywaitinterval(PyObject *self, PyObject *args) 3484 Tkinter_getbusywaitinterval(PyObject *self, PyObject *args)
2933 { 3485 {
2934 return PyLong_FromLong(Tkinter_busywaitinterval); 3486 return PyInt_FromLong(Tkinter_busywaitinterval);
2935 } 3487 }
2936 3488
2937 static char getbusywaitinterval_doc[] = 3489 static char getbusywaitinterval_doc[] =
2938 "getbusywaitinterval() -> int\n\ 3490 "getbusywaitinterval() -> int\n\
2939 \n\ 3491 \n\
2940 Return the current busy-wait interval between successive\n\ 3492 Return the current busy-wait interval between successive\n\
2941 calls to Tcl_DoOneEvent in a threaded Python interpreter."; 3493 calls to Tcl_DoOneEvent in a threaded Python interpreter.";
2942 3494
2943 static PyMethodDef moduleMethods[] = 3495 static PyMethodDef moduleMethods[] =
2944 { 3496 {
2945 {"_flatten", Tkinter_Flatten, METH_VARARGS}, 3497 {"_flatten", Tkinter_Flatten, METH_VARARGS},
2946 {"create", Tkinter_Create, METH_VARARGS}, 3498 {"create", Tkinter_Create, METH_VARARGS},
3499 #ifdef HAVE_CREATEFILEHANDLER
3500 {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS},
3501 {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS},
3502 #endif
3503 {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS},
3504 {"mainloop", Tkapp_MainLoop, METH_VARARGS},
3505 {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS},
3506 {"quit", Tkapp_Quit, METH_VARARGS},
2947 {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS, 3507 {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS,
2948 setbusywaitinterval_doc}, 3508 setbusywaitinterval_doc},
2949 {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval, 3509 {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval,
2950 METH_NOARGS, getbusywaitinterval_doc}, 3510 METH_NOARGS, getbusywaitinterval_doc},
2951 {NULL, NULL} 3511 {NULL, NULL}
2952 }; 3512 };
2953 3513
2954 #ifdef WAIT_FOR_STDIN 3514 #ifdef WAIT_FOR_STDIN
2955 3515
2956 static int stdin_ready = 0; 3516 static int stdin_ready = 0;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
3043 DisableEventHook(void) 3603 DisableEventHook(void)
3044 { 3604 {
3045 #ifdef WAIT_FOR_STDIN 3605 #ifdef WAIT_FOR_STDIN
3046 if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { 3606 if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) {
3047 PyOS_InputHook = NULL; 3607 PyOS_InputHook = NULL;
3048 } 3608 }
3049 #endif 3609 #endif
3050 } 3610 }
3051 3611
3052 3612
3053 static struct PyModuleDef _tkintermodule = { 3613 /* all errors will be checked in one fell swoop in init_tkinter() */
3054 PyModuleDef_HEAD_INIT, 3614 static void
3055 "_tkinter", 3615 ins_long(PyObject *d, char *name, long val)
3056 NULL, 3616 {
3057 -1, 3617 PyObject *v = PyInt_FromLong(val);
3058 moduleMethods, 3618 if (v) {
3059 NULL, 3619 PyDict_SetItemString(d, name, v);
3060 NULL, 3620 Py_DECREF(v);
3061 NULL, 3621 }
3062 NULL 3622 }
3063 }; 3623 static void
3624 ins_string(PyObject *d, char *name, char *val)
3625 {
3626 PyObject *v = PyString_FromString(val);
3627 if (v) {
3628 PyDict_SetItemString(d, name, v);
3629 Py_DECREF(v);
3630 }
3631 }
3632
3064 3633
3065 PyMODINIT_FUNC 3634 PyMODINIT_FUNC
3066 PyInit__tkinter(void) 3635 init_tkinter(void)
3067 { 3636 {
3068 PyObject *m, *uexe, *cexe, *o; 3637 PyObject *m, *d;
3638
3639 Py_TYPE(&Tkapp_Type) = &PyType_Type;
3069 3640
3070 #ifdef WITH_THREAD 3641 #ifdef WITH_THREAD
3071 tcl_lock = PyThread_allocate_lock(); 3642 tcl_lock = PyThread_allocate_lock();
3072 if (tcl_lock == NULL) 3643 #endif
3073 return NULL; 3644
3074 #endif 3645 m = Py_InitModule("_tkinter", moduleMethods);
3075
3076 m = PyModule_Create(&_tkintermodule);
3077 if (m == NULL) 3646 if (m == NULL)
3078 return NULL; 3647 return;
3079 3648
3080 o = PyErr_NewException("_tkinter.TclError", NULL, NULL); 3649 d = PyModule_GetDict(m);
3081 if (o == NULL) { 3650 Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL);
3082 Py_DECREF(m); 3651 PyDict_SetItemString(d, "TclError", Tkinter_TclError);
3083 return NULL; 3652
3084 } 3653 ins_long(d, "READABLE", TCL_READABLE);
3085 Py_INCREF(o); 3654 ins_long(d, "WRITABLE", TCL_WRITABLE);
3086 if (PyModule_AddObject(m, "TclError", o)) { 3655 ins_long(d, "EXCEPTION", TCL_EXCEPTION);
3087 Py_DECREF(o); 3656 ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS);
3088 Py_DECREF(m); 3657 ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS);
3089 return NULL; 3658 ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS);
3090 } 3659 ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS);
3091 Tkinter_TclError = o; 3660 ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS);
3092 3661 ins_long(d, "DONT_WAIT", TCL_DONT_WAIT);
3093 if (PyModule_AddIntConstant(m, "READABLE", TCL_READABLE)) { 3662 ins_string(d, "TK_VERSION", TK_VERSION);
3094 Py_DECREF(m); 3663 ins_string(d, "TCL_VERSION", TCL_VERSION);
3095 return NULL; 3664
3096 } 3665 PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type);
3097 if (PyModule_AddIntConstant(m, "WRITABLE", TCL_WRITABLE)) { 3666
3098 Py_DECREF(m); 3667 Py_TYPE(&Tktt_Type) = &PyType_Type;
3099 return NULL; 3668 PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type);
3100 } 3669
3101 if (PyModule_AddIntConstant(m, "EXCEPTION", TCL_EXCEPTION)) { 3670 Py_TYPE(&PyTclObject_Type) = &PyType_Type;
3102 Py_DECREF(m); 3671 PyDict_SetItemString(d, "Tcl_Obj", (PyObject *)&PyTclObject_Type);
3103 return NULL;
3104 }
3105 if (PyModule_AddIntConstant(m, "WINDOW_EVENTS", TCL_WINDOW_EVENTS)) {
3106 Py_DECREF(m);
3107 return NULL;
3108 }
3109 if (PyModule_AddIntConstant(m, "FILE_EVENTS", TCL_FILE_EVENTS)) {
3110 Py_DECREF(m);
3111 return NULL;
3112 }
3113 if (PyModule_AddIntConstant(m, "TIMER_EVENTS", TCL_TIMER_EVENTS)) {
3114 Py_DECREF(m);
3115 return NULL;
3116 }
3117 if (PyModule_AddIntConstant(m, "IDLE_EVENTS", TCL_IDLE_EVENTS)) {
3118 Py_DECREF(m);
3119 return NULL;
3120 }
3121 if (PyModule_AddIntConstant(m, "ALL_EVENTS", TCL_ALL_EVENTS)) {
3122 Py_DECREF(m);
3123 return NULL;
3124 }
3125 if (PyModule_AddIntConstant(m, "DONT_WAIT", TCL_DONT_WAIT)) {
3126 Py_DECREF(m);
3127 return NULL;
3128 }
3129 if (PyModule_AddStringConstant(m, "TK_VERSION", TK_VERSION)) {
3130 Py_DECREF(m);
3131 return NULL;
3132 }
3133 if (PyModule_AddStringConstant(m, "TCL_VERSION", TCL_VERSION)) {
3134 Py_DECREF(m);
3135 return NULL;
3136 }
3137
3138 o = PyType_FromSpec(&Tkapp_Type_spec);
3139 if (o == NULL) {
3140 Py_DECREF(m);
3141 return NULL;
3142 }
3143 if (PyModule_AddObject(m, "TkappType", o)) {
3144 Py_DECREF(o);
3145 Py_DECREF(m);
3146 return NULL;
3147 }
3148 Tkapp_Type = o;
3149
3150 o = PyType_FromSpec(&Tktt_Type_spec);
3151 if (o == NULL) {
3152 Py_DECREF(m);
3153 return NULL;
3154 }
3155 if (PyModule_AddObject(m, "TkttType", o)) {
3156 Py_DECREF(o);
3157 Py_DECREF(m);
3158 return NULL;
3159 }
3160 Tktt_Type = o;
3161
3162 o = PyType_FromSpec(&PyTclObject_Type_spec);
3163 if (o == NULL) {
3164 Py_DECREF(m);
3165 return NULL;
3166 }
3167 if (PyModule_AddObject(m, "Tcl_Obj", o)) {
3168 Py_DECREF(o);
3169 Py_DECREF(m);
3170 return NULL;
3171 }
3172 PyTclObject_Type = o;
3173 3672
3174 #ifdef TK_AQUA 3673 #ifdef TK_AQUA
3175 /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems 3674 /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems
3176 * start waking up. Note that Tcl_FindExecutable will do this, this 3675 * start waking up. Note that Tcl_FindExecutable will do this, this
3177 * code must be above it! The original warning from 3676 * code must be above it! The original warning from
3178 * tkMacOSXAppInit.c is copied below. 3677 * tkMacOSXAppInit.c is copied below.
3179 * 3678 *
3180 * NB - You have to swap in the Tk Notifier BEFORE you start up the 3679 * NB - You have to swap in the Tk Notifier BEFORE you start up the
3181 * Tcl interpreter for now. It probably should work to do this 3680 * Tcl interpreter for now. It probably should work to do this
3182 * in the other order, but for now it doesn't seem to. 3681 * in the other order, but for now it doesn't seem to.
3183 * 3682 *
3184 */ 3683 */
3185 Tk_MacOSXSetupTkNotifier(); 3684 Tk_MacOSXSetupTkNotifier();
3186 #endif 3685 #endif
3187 3686
3188 3687
3189 /* This helps the dynamic loader; in Unicode aware Tcl versions 3688 /* This helps the dynamic loader; in Unicode aware Tcl versions
3190 it also helps Tcl find its encodings. */ 3689 it also helps Tcl find its encodings. */
3191 uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1); 3690 Tcl_FindExecutable(Py_GetProgramName());
3192 if (uexe) { 3691
3193 cexe = PyUnicode_EncodeFSDefault(uexe); 3692 if (PyErr_Occurred())
3194 if (cexe) 3693 return;
3195 Tcl_FindExecutable(PyBytes_AsString(cexe));
3196 Py_XDECREF(cexe);
3197 Py_DECREF(uexe);
3198 }
3199
3200 if (PyErr_Occurred()) {
3201 Py_DECREF(m);
3202 return NULL;
3203 }
3204 3694
3205 #if 0 3695 #if 0
3206 /* This was not a good idea; through <Destroy> bindings, 3696 /* This was not a good idea; through <Destroy> bindings,
3207 Tcl_Finalize() may invoke Python code but at that point the 3697 Tcl_Finalize() may invoke Python code but at that point the
3208 interpreter and thread state have already been destroyed! */ 3698 interpreter and thread state have already been destroyed! */
3209 Py_AtExit(Tcl_Finalize); 3699 Py_AtExit(Tcl_Finalize);
3210 #endif 3700 #endif
3211 return m; 3701
3212 } 3702 }
LEFTRIGHT
« no previous file | Modules/tkinter.h » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+