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

Delta Between Two Patch Sets: Modules/_tkinter.c

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