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