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

Delta Between Two Patch Sets: Modules/_tkinter.c

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