Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright (C) 1994 Steen Lumholt. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 3 | |
| 4 | All Rights Reserved |
| 5 | |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 6 | ******************************************************************/ |
| 7 | |
| 8 | /* _tkinter.c -- Interface to libtk.a and libtcl.a. */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 10 | /* TCL/TK VERSION INFO: |
| 11 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 12 | Only Tcl/Tk 8.2 and later are supported. Older versions are not |
| 13 | supported. (Use Python 2.2 if you cannot upgrade your Tcl/Tk |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 14 | libraries.) |
| 15 | */ |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 17 | /* XXX Further speed-up ideas, involving Tcl 8.0 features: |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 19 | - Register a new Tcl type, "Python callable", which can be called more |
| 20 | efficiently and passed to Tcl_EvalObj() directly (if this is possible). |
| 21 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 25 | #include "Python.h" |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 28 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 29 | #include "pythread.h" |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 32 | #ifdef MS_WINDOWS |
| 33 | #include <windows.h> |
| 34 | #endif |
| 35 | |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 36 | /* Allow using this code in Python 2.[12] */ |
| 37 | #ifndef PyDoc_STRVAR |
Martin v. Löwis | cd9a8b6 | 2003-01-21 21:52:57 +0000 | [diff] [blame] | 38 | #define PyDoc_STRVAR(name,str) static char name[] = str |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #ifndef PyMODINIT_FUNC |
Martin v. Löwis | 3a57d9d | 2003-01-04 08:54:59 +0000 | [diff] [blame] | 42 | #define PyMODINIT_FUNC void |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Martin v. Löwis | 52ae6f6 | 2003-03-30 08:26:04 +0000 | [diff] [blame] | 45 | #ifndef PyBool_Check |
| 46 | #define PyBool_Check(o) 0 |
| 47 | #define PyBool_FromLong PyInt_FromLong |
| 48 | #endif |
| 49 | |
Martin v. Löwis | 71e25a0 | 2002-10-01 18:08:06 +0000 | [diff] [blame] | 50 | /* Starting with Tcl 8.4, many APIs offer const-correctness. Unfortunately, |
| 51 | making _tkinter correct for this API means to break earlier |
| 52 | versions. USE_COMPAT_CONST allows to make _tkinter work with both 8.4 and |
| 53 | earlier versions. Once Tcl releases before 8.4 don't need to be supported |
| 54 | anymore, this should go. */ |
| 55 | #define USE_COMPAT_CONST |
| 56 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 57 | /* If Tcl is compiled for threads, we must also define TCL_THREAD. We define |
| 58 | it always; if Tcl is not threaded, the thread functions in |
| 59 | Tcl are empty. */ |
| 60 | #define TCL_THREADS |
| 61 | |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 62 | #ifdef TK_FRAMEWORK |
| 63 | #include <Tcl/tcl.h> |
| 64 | #include <Tk/tk.h> |
| 65 | #else |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 66 | #include <tcl.h> |
| 67 | #include <tk.h> |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 68 | #endif |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 69 | |
Jason Tishler | bbe8961 | 2002-12-31 20:30:46 +0000 | [diff] [blame] | 70 | /* For Tcl 8.2 and 8.3, CONST* is not defined (except on Cygwin). */ |
Martin v. Löwis | 5b177f1 | 2002-12-30 18:14:15 +0000 | [diff] [blame] | 71 | #ifndef CONST84_RETURN |
| 72 | #define CONST84_RETURN |
Jason Tishler | bbe8961 | 2002-12-31 20:30:46 +0000 | [diff] [blame] | 73 | #undef CONST |
Martin v. Löwis | 5b177f1 | 2002-12-30 18:14:15 +0000 | [diff] [blame] | 74 | #define CONST |
| 75 | #endif |
| 76 | |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 77 | #define TKMAJORMINOR (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) |
| 78 | |
Martin v. Löwis | 6bfa2e6 | 2002-10-01 17:48:31 +0000 | [diff] [blame] | 79 | #if TKMAJORMINOR < 8002 |
| 80 | #error "Tk older than 8.2 not supported" |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 83 | /* Unicode conversion assumes that Tcl_UniChar is two bytes. |
| 84 | We cannot test this directly, so we test UTF-8 size instead, |
| 85 | expecting that TCL_UTF_MAX is changed if Tcl ever supports |
Martin v. Löwis | 6f29ff3 | 2003-04-16 20:34:55 +0000 | [diff] [blame] | 86 | either UTF-16 or UCS-4. |
| 87 | Redhat 8 sets TCL_UTF_MAX to 6, and uses wchar_t for |
| 88 | Tcl_Unichar. This is also ok as long as Python uses UCS-4, |
| 89 | as well. |
| 90 | */ |
| 91 | #if TCL_UTF_MAX != 3 && !(defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==6) |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 92 | #error "unsupported Tcl configuration" |
| 93 | #endif |
| 94 | |
Jack Jansen | eddc144 | 2003-11-20 01:44:59 +0000 | [diff] [blame] | 95 | #if !(defined(MS_WINDOWS) || defined(__CYGWIN__)) |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 96 | #define HAVE_CREATEFILEHANDLER |
| 97 | #endif |
| 98 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 99 | #ifdef HAVE_CREATEFILEHANDLER |
| 100 | |
Neal Norwitz | d948a43 | 2006-01-08 01:08:55 +0000 | [diff] [blame] | 101 | /* This bit is to ensure that TCL_UNIX_FD is defined and doesn't interfere |
| 102 | with the proper calculation of FHANDLETYPE == TCL_UNIX_FD below. */ |
| 103 | #ifndef TCL_UNIX_FD |
| 104 | # ifdef TCL_WIN_SOCKET |
| 105 | # define TCL_UNIX_FD (! TCL_WIN_SOCKET) |
| 106 | # else |
| 107 | # define TCL_UNIX_FD 1 |
| 108 | # endif |
| 109 | #endif |
| 110 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 111 | /* Tcl_CreateFileHandler() changed several times; these macros deal with the |
| 112 | messiness. In Tcl 8.0 and later, it is not available on Windows (and on |
| 113 | Unix, only because Jack added it back); when available on Windows, it only |
| 114 | applies to sockets. */ |
| 115 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 116 | #ifdef MS_WINDOWS |
| 117 | #define FHANDLETYPE TCL_WIN_SOCKET |
| 118 | #else |
| 119 | #define FHANDLETYPE TCL_UNIX_FD |
| 120 | #endif |
| 121 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 122 | /* If Tcl can wait for a Unix file descriptor, define the EventHook() routine |
| 123 | which uses this to handle Tcl events while the user is typing commands. */ |
| 124 | |
| 125 | #if FHANDLETYPE == TCL_UNIX_FD |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 126 | #define WAIT_FOR_STDIN |
| 127 | #endif |
| 128 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 129 | #endif /* HAVE_CREATEFILEHANDLER */ |
| 130 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 131 | #ifdef MS_WINDOWS |
| 132 | #include <conio.h> |
| 133 | #define WAIT_FOR_STDIN |
| 134 | #endif |
| 135 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 136 | #ifdef WITH_THREAD |
| 137 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 138 | /* The threading situation is complicated. Tcl is not thread-safe, except |
| 139 | when configured with --enable-threads. |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 140 | So we need to use a lock around all uses of Tcl. Previously, the Python |
| 141 | interpreter lock was used for this. However, this causes problems when |
| 142 | other Python threads need to run while Tcl is blocked waiting for events. |
| 143 | |
| 144 | To solve this problem, a separate lock for Tcl is introduced. Holding it |
| 145 | is incompatible with holding Python's interpreter lock. The following four |
| 146 | macros manipulate both locks together. |
| 147 | |
| 148 | ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and |
| 149 | Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made |
| 150 | that could call an event handler, or otherwise affect the state of a Tcl |
| 151 | interpreter. These assume that the surrounding code has the Python |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 152 | interpreter lock; inside the brackets, the Python interpreter lock has been |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 153 | released and the lock for Tcl has been acquired. |
| 154 | |
Guido van Rossum | 5e97783 | 1998-06-15 14:03:52 +0000 | [diff] [blame] | 155 | Sometimes, it is necessary to have both the Python lock and the Tcl lock. |
| 156 | (For example, when transferring data from the Tcl interpreter result to a |
| 157 | Python string object.) This can be done by using different macros to close |
| 158 | the ENTER_TCL block: ENTER_OVERLAP reacquires the Python lock (and restores |
| 159 | the thread state) but doesn't release the Tcl lock; LEAVE_OVERLAP_TCL |
| 160 | releases the Tcl lock. |
| 161 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 162 | By contrast, ENTER_PYTHON and LEAVE_PYTHON are used in Tcl event |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 163 | handlers when the handler needs to use Python. Such event handlers are |
| 164 | entered while the lock for Tcl is held; the event handler presumably needs |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 165 | to use Python. ENTER_PYTHON releases the lock for Tcl and acquires |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 166 | the Python interpreter lock, restoring the appropriate thread state, and |
| 167 | LEAVE_PYTHON releases the Python interpreter lock and re-acquires the lock |
| 168 | for Tcl. It is okay for ENTER_TCL/LEAVE_TCL pairs to be contained inside |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 169 | the code between ENTER_PYTHON and LEAVE_PYTHON. |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 170 | |
| 171 | These locks expand to several statements and brackets; they should not be |
| 172 | used in branches of if statements and the like. |
| 173 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 174 | If Tcl is threaded, this approach won't work anymore. The Tcl interpreter is |
| 175 | only valid in the thread that created it, and all Tk activity must happen in this |
| 176 | thread, also. That means that the mainloop must be invoked in the thread that |
| 177 | created the interpreter. Invoking commands from other threads is possible; |
| 178 | _tkinter will queue an event for the interpreter thread, which will then |
| 179 | execute the command and pass back the result. If the main thread is not in the |
| 180 | mainloop, and invoking commands causes an exception; if the main loop is running |
| 181 | but not processing events, the command invocation will block. |
| 182 | |
| 183 | In addition, for a threaded Tcl, a single global tcl_tstate won't be sufficient |
| 184 | anymore, since multiple Tcl interpreters may simultaneously dispatch in different |
| 185 | threads. So we use the Tcl TLS API. |
| 186 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 187 | */ |
| 188 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 189 | static PyThread_type_lock tcl_lock = 0; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 190 | |
| 191 | #ifdef TCL_THREADS |
| 192 | static Tcl_ThreadDataKey state_key; |
| 193 | typedef PyThreadState *ThreadSpecificData; |
| 194 | #define tcl_tstate (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*))) |
| 195 | #else |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 196 | static PyThreadState *tcl_tstate = NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 197 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 198 | |
| 199 | #define ENTER_TCL \ |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 200 | { PyThreadState *tstate = PyThreadState_Get(); Py_BEGIN_ALLOW_THREADS \ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 201 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 202 | |
| 203 | #define LEAVE_TCL \ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 204 | tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); Py_END_ALLOW_THREADS} |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 206 | #define ENTER_OVERLAP \ |
| 207 | Py_END_ALLOW_THREADS |
| 208 | |
| 209 | #define LEAVE_OVERLAP_TCL \ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 210 | tcl_tstate = NULL; if(tcl_lock)PyThread_release_lock(tcl_lock); } |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 212 | #define ENTER_PYTHON \ |
| 213 | { PyThreadState *tstate = tcl_tstate; tcl_tstate = NULL; \ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 214 | if(tcl_lock)PyThread_release_lock(tcl_lock); PyEval_RestoreThread((tstate)); } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 215 | |
| 216 | #define LEAVE_PYTHON \ |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 217 | { PyThreadState *tstate = PyEval_SaveThread(); \ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 218 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); tcl_tstate = tstate; } |
| 219 | |
| 220 | #define CHECK_TCL_APPARTMENT \ |
| 221 | if (((TkappObject *)self)->threaded && \ |
| 222 | ((TkappObject *)self)->thread_id != Tcl_GetCurrentThread()) { \ |
| 223 | PyErr_SetString(PyExc_RuntimeError, "Calling Tcl from different appartment"); \ |
| 224 | return 0; \ |
| 225 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 226 | |
| 227 | #else |
| 228 | |
| 229 | #define ENTER_TCL |
| 230 | #define LEAVE_TCL |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 231 | #define ENTER_OVERLAP |
| 232 | #define LEAVE_OVERLAP_TCL |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 233 | #define ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 234 | #define LEAVE_PYTHON |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 235 | #define CHECK_TCL_APPARTMENT |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 236 | |
| 237 | #endif |
| 238 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 239 | #ifndef FREECAST |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 240 | #define FREECAST (char *) |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 241 | #endif |
| 242 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 243 | /**** Tkapp Object Declaration ****/ |
| 244 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 245 | static PyTypeObject Tkapp_Type; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 247 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 248 | PyObject_HEAD |
| 249 | Tcl_Interp *interp; |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 250 | int wantobjects; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 251 | int threaded; /* True if tcl_platform[threaded] */ |
| 252 | Tcl_ThreadId thread_id; |
| 253 | int dispatching; |
| 254 | /* We cannot include tclInt.h, as this is internal. |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 255 | So we cache interesting types here. */ |
| 256 | Tcl_ObjType *BooleanType; |
| 257 | Tcl_ObjType *ByteArrayType; |
| 258 | Tcl_ObjType *DoubleType; |
| 259 | Tcl_ObjType *IntType; |
| 260 | Tcl_ObjType *ListType; |
| 261 | Tcl_ObjType *ProcBodyType; |
| 262 | Tcl_ObjType *StringType; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 263 | } TkappObject; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 264 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 265 | #define Tkapp_Check(v) (Py_Type(v) == &Tkapp_Type) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 266 | #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) |
Guido van Rossum | ada6d87 | 2000-10-12 17:14:46 +0000 | [diff] [blame] | 267 | #define Tkapp_Result(v) Tcl_GetStringResult(Tkapp_Interp(v)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 269 | #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 270 | (void *) v, Py_Refcnt(v))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 271 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 272 | |
| 273 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 274 | /**** Error Handling ****/ |
| 275 | |
| 276 | static PyObject *Tkinter_TclError; |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 277 | static int quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 278 | static int errorInCmd = 0; |
| 279 | static PyObject *excInCmd; |
| 280 | static PyObject *valInCmd; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 281 | static PyObject *trbInCmd; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 282 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 283 | |
| 284 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 285 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 286 | Tkinter_Error(PyObject *v) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 287 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 288 | PyErr_SetString(Tkinter_TclError, Tkapp_Result(v)); |
| 289 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 292 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 294 | /**** Utils ****/ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 295 | |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 296 | static int Tkinter_busywaitinterval = 20; |
| 297 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 298 | #ifdef WITH_THREAD |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 299 | #ifndef MS_WINDOWS |
Guido van Rossum | 541f241 | 1998-08-13 13:29:22 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 301 | /* Millisecond sleep() for Unix platforms. */ |
| 302 | |
| 303 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 304 | Sleep(int milli) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 305 | { |
| 306 | /* XXX Too bad if you don't have select(). */ |
| 307 | struct timeval t; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 308 | t.tv_sec = milli/1000; |
| 309 | t.tv_usec = (milli%1000) * 1000; |
| 310 | select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 311 | } |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 312 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 313 | |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 314 | /* Wait up to 1s for the mainloop to come up. */ |
| 315 | |
| 316 | static int |
| 317 | WaitForMainloop(TkappObject* self) |
| 318 | { |
| 319 | int i; |
| 320 | for (i = 0; i < 10; i++) { |
| 321 | if (self->dispatching) |
| 322 | return 1; |
| 323 | Py_BEGIN_ALLOW_THREADS |
| 324 | Sleep(100); |
| 325 | Py_END_ALLOW_THREADS |
| 326 | } |
| 327 | if (self->dispatching) |
| 328 | return 1; |
| 329 | PyErr_SetString(PyExc_RuntimeError, "main thread is not in main loop"); |
| 330 | return 0; |
| 331 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 332 | #endif /* WITH_THREAD */ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 335 | static char * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 336 | AsString(PyObject *value, PyObject *tmp) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 338 | if (PyString_Check(value)) |
| 339 | return PyString_AsString(value); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 340 | else if (PyUnicode_Check(value)) { |
| 341 | PyObject *v = PyUnicode_AsUTF8String(value); |
| 342 | if (v == NULL) |
| 343 | return NULL; |
| 344 | if (PyList_Append(tmp, v) != 0) { |
| 345 | Py_DECREF(v); |
| 346 | return NULL; |
| 347 | } |
| 348 | Py_DECREF(v); |
| 349 | return PyString_AsString(v); |
| 350 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 351 | else { |
| 352 | PyObject *v = PyObject_Str(value); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 353 | if (v == NULL) |
| 354 | return NULL; |
| 355 | if (PyList_Append(tmp, v) != 0) { |
| 356 | Py_DECREF(v); |
| 357 | return NULL; |
| 358 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 359 | Py_DECREF(v); |
| 360 | return PyString_AsString(v); |
| 361 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 364 | |
| 365 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 366 | #define ARGSZ 64 |
| 367 | |
| 368 | static char * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 369 | Merge(PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 370 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 371 | PyObject *tmp = NULL; |
| 372 | char *argvStore[ARGSZ]; |
| 373 | char **argv = NULL; |
| 374 | int fvStore[ARGSZ]; |
| 375 | int *fv = NULL; |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 376 | int argc = 0, fvc = 0, i; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 377 | char *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 378 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 379 | if (!(tmp = PyList_New(0))) |
| 380 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 381 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 382 | argv = argvStore; |
| 383 | fv = fvStore; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 384 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 385 | if (args == NULL) |
| 386 | argc = 0; |
| 387 | |
| 388 | else if (!PyTuple_Check(args)) { |
| 389 | argc = 1; |
| 390 | fv[0] = 0; |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 391 | if (!(argv[0] = AsString(args, tmp))) |
| 392 | goto finally; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 393 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 394 | else { |
| 395 | argc = PyTuple_Size(args); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 396 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 397 | if (argc > ARGSZ) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 398 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 399 | fv = (int *)ckalloc(argc * sizeof(int)); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 400 | if (argv == NULL || fv == NULL) { |
| 401 | PyErr_NoMemory(); |
| 402 | goto finally; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < argc; i++) { |
| 407 | PyObject *v = PyTuple_GetItem(args, i); |
| 408 | if (PyTuple_Check(v)) { |
| 409 | fv[i] = 1; |
| 410 | if (!(argv[i] = Merge(v))) |
| 411 | goto finally; |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 412 | fvc++; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 413 | } |
| 414 | else if (v == Py_None) { |
| 415 | argc = i; |
| 416 | break; |
| 417 | } |
| 418 | else { |
| 419 | fv[i] = 0; |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 420 | if (!(argv[i] = AsString(v, tmp))) |
| 421 | goto finally; |
| 422 | fvc++; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 425 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 426 | res = Tcl_Merge(argc, argv); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 427 | if (res == NULL) |
| 428 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 429 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 430 | finally: |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 431 | for (i = 0; i < fvc; i++) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 432 | if (fv[i]) { |
| 433 | ckfree(argv[i]); |
| 434 | } |
| 435 | if (argv != argvStore) |
| 436 | ckfree(FREECAST argv); |
| 437 | if (fv != fvStore) |
| 438 | ckfree(FREECAST fv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 439 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 440 | Py_DECREF(tmp); |
| 441 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 444 | |
| 445 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 446 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 447 | Split(char *list) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 448 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 449 | int argc; |
| 450 | char **argv; |
| 451 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 452 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 453 | if (list == NULL) { |
| 454 | Py_INCREF(Py_None); |
| 455 | return Py_None; |
| 456 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 457 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 458 | if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 459 | /* Not a list. |
| 460 | * Could be a quoted string containing funnies, e.g. {"}. |
| 461 | * Return the string itself. |
| 462 | */ |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 463 | return PyUnicode_FromString(list); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 464 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 465 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 466 | if (argc == 0) |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 467 | v = PyUnicode_FromString(""); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 468 | else if (argc == 1) |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 469 | v = PyUnicode_FromString(argv[0]); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 470 | else if ((v = PyTuple_New(argc)) != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 471 | int i; |
| 472 | PyObject *w; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 473 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 474 | for (i = 0; i < argc; i++) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 475 | if ((w = Split(argv[i])) == NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 476 | Py_DECREF(v); |
| 477 | v = NULL; |
| 478 | break; |
| 479 | } |
| 480 | PyTuple_SetItem(v, i, w); |
| 481 | } |
| 482 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 483 | Tcl_Free(FREECAST argv); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 484 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 487 | /* In some cases, Tcl will still return strings that are supposed to be |
| 488 | lists. SplitObj walks through a nested tuple, finding string objects that |
| 489 | need to be split. */ |
| 490 | |
| 491 | PyObject * |
| 492 | SplitObj(PyObject *arg) |
| 493 | { |
| 494 | if (PyTuple_Check(arg)) { |
| 495 | int i, size; |
| 496 | PyObject *elem, *newelem, *result; |
| 497 | |
| 498 | size = PyTuple_Size(arg); |
| 499 | result = NULL; |
| 500 | /* Recursively invoke SplitObj for all tuple items. |
| 501 | If this does not return a new object, no action is |
| 502 | needed. */ |
| 503 | for(i = 0; i < size; i++) { |
| 504 | elem = PyTuple_GetItem(arg, i); |
| 505 | newelem = SplitObj(elem); |
| 506 | if (!newelem) { |
| 507 | Py_XDECREF(result); |
| 508 | return NULL; |
| 509 | } |
| 510 | if (!result) { |
| 511 | int k; |
| 512 | if (newelem == elem) { |
| 513 | Py_DECREF(newelem); |
| 514 | continue; |
| 515 | } |
| 516 | result = PyTuple_New(size); |
| 517 | if (!result) |
| 518 | return NULL; |
| 519 | for(k = 0; k < i; k++) { |
| 520 | elem = PyTuple_GetItem(arg, k); |
| 521 | Py_INCREF(elem); |
| 522 | PyTuple_SetItem(result, k, elem); |
| 523 | } |
| 524 | } |
| 525 | PyTuple_SetItem(result, i, newelem); |
| 526 | } |
| 527 | if (result) |
| 528 | return result; |
| 529 | /* Fall through, returning arg. */ |
| 530 | } |
| 531 | else if (PyString_Check(arg)) { |
| 532 | int argc; |
| 533 | char **argv; |
| 534 | char *list = PyString_AsString(arg); |
| 535 | |
| 536 | if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { |
| 537 | Py_INCREF(arg); |
| 538 | return arg; |
| 539 | } |
| 540 | Tcl_Free(FREECAST argv); |
| 541 | if (argc > 1) |
| 542 | return Split(PyString_AsString(arg)); |
| 543 | /* Fall through, returning arg. */ |
| 544 | } |
| 545 | Py_INCREF(arg); |
| 546 | return arg; |
| 547 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 548 | |
| 549 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 550 | /**** Tkapp Object ****/ |
| 551 | |
| 552 | #ifndef WITH_APPINIT |
| 553 | int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 554 | Tcl_AppInit(Tcl_Interp *interp) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 555 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 556 | Tk_Window main; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 557 | const char * _tkinter_skip_tk_init; |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 558 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 559 | if (Tcl_Init(interp) == TCL_ERROR) { |
Guido van Rossum | ada6d87 | 2000-10-12 17:14:46 +0000 | [diff] [blame] | 560 | PySys_WriteStderr("Tcl_Init error: %s\n", Tcl_GetStringResult(interp)); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 561 | return TCL_ERROR; |
| 562 | } |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 563 | _tkinter_skip_tk_init = Tcl_GetVar(interp, "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY); |
| 564 | if (_tkinter_skip_tk_init == NULL || strcmp(_tkinter_skip_tk_init, "1") != 0) { |
| 565 | main = Tk_MainWindow(interp); |
| 566 | if (Tk_Init(interp) == TCL_ERROR) { |
| 567 | PySys_WriteStderr("Tk_Init error: %s\n", Tcl_GetStringResult(interp)); |
| 568 | return TCL_ERROR; |
| 569 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 570 | } |
| 571 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 572 | } |
| 573 | #endif /* !WITH_APPINIT */ |
| 574 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 575 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 576 | |
| 577 | |
| 578 | /* Initialize the Tk application; see the `main' function in |
| 579 | * `tkMain.c'. |
| 580 | */ |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 581 | |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 582 | static void EnableEventHook(void); /* Forward */ |
| 583 | static void DisableEventHook(void); /* Forward */ |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 584 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 585 | static TkappObject * |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 586 | Tkapp_New(char *screenName, char *baseName, char *className, |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 587 | int interactive, int wantobjects, int wantTk, int sync, char *use) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 588 | { |
| 589 | TkappObject *v; |
| 590 | char *argv0; |
Tim Peters | 51fa3b7 | 2004-08-04 02:16:48 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 592 | v = PyObject_New(TkappObject, &Tkapp_Type); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 593 | if (v == NULL) |
| 594 | return NULL; |
| 595 | |
| 596 | v->interp = Tcl_CreateInterp(); |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 597 | v->wantobjects = wantobjects; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 598 | v->threaded = Tcl_GetVar2Ex(v->interp, "tcl_platform", "threaded", |
| 599 | TCL_GLOBAL_ONLY) != NULL; |
| 600 | v->thread_id = Tcl_GetCurrentThread(); |
| 601 | v->dispatching = 0; |
| 602 | |
| 603 | #ifndef TCL_THREADS |
| 604 | if (v->threaded) { |
| 605 | PyErr_SetString(PyExc_RuntimeError, "Tcl is threaded but _tkinter is not"); |
| 606 | Py_DECREF(v); |
| 607 | return 0; |
| 608 | } |
| 609 | #endif |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 610 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 611 | if (v->threaded && tcl_lock) { |
| 612 | /* If Tcl is threaded, we don't need the lock. */ |
| 613 | PyThread_free_lock(tcl_lock); |
| 614 | tcl_lock = NULL; |
| 615 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 616 | #endif |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 617 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 618 | v->BooleanType = Tcl_GetObjType("boolean"); |
| 619 | v->ByteArrayType = Tcl_GetObjType("bytearray"); |
| 620 | v->DoubleType = Tcl_GetObjType("double"); |
| 621 | v->IntType = Tcl_GetObjType("int"); |
| 622 | v->ListType = Tcl_GetObjType("list"); |
| 623 | v->ProcBodyType = Tcl_GetObjType("procbody"); |
| 624 | v->StringType = Tcl_GetObjType("string"); |
| 625 | |
Guido van Rossum | 1c0d315 | 1998-02-19 21:28:49 +0000 | [diff] [blame] | 626 | /* Delete the 'exit' command, which can screw things up */ |
| 627 | Tcl_DeleteCommand(v->interp, "exit"); |
| 628 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 629 | if (screenName != NULL) |
| 630 | Tcl_SetVar2(v->interp, "env", "DISPLAY", |
| 631 | screenName, TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 632 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 633 | if (interactive) |
| 634 | Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
| 635 | else |
| 636 | Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 637 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 638 | /* This is used to get the application class for Tk 4.1 and up */ |
| 639 | argv0 = (char*)ckalloc(strlen(className) + 1); |
| 640 | if (!argv0) { |
| 641 | PyErr_NoMemory(); |
| 642 | Py_DECREF(v); |
| 643 | return NULL; |
| 644 | } |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 645 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 646 | strcpy(argv0, className); |
Neal Norwitz | 9dbc7dd | 2005-12-19 06:08:59 +0000 | [diff] [blame] | 647 | if (isupper(Py_CHARMASK(argv0[0]))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 648 | argv0[0] = tolower(Py_CHARMASK(argv0[0])); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 649 | Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); |
| 650 | ckfree(argv0); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 651 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 652 | if (! wantTk) { |
| 653 | Tcl_SetVar(v->interp, "_tkinter_skip_tk_init", "1", TCL_GLOBAL_ONLY); |
| 654 | } |
| 655 | |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 656 | /* some initial arguments need to be in argv */ |
| 657 | if (sync || use) { |
Tim Peters | 51fa3b7 | 2004-08-04 02:16:48 +0000 | [diff] [blame] | 658 | char *args; |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 659 | int len = 0; |
Tim Peters | 51fa3b7 | 2004-08-04 02:16:48 +0000 | [diff] [blame] | 660 | |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 661 | if (sync) |
| 662 | len += sizeof "-sync"; |
| 663 | if (use) |
| 664 | len += strlen(use) + sizeof "-use "; |
| 665 | |
Tim Peters | 51fa3b7 | 2004-08-04 02:16:48 +0000 | [diff] [blame] | 666 | args = (char*)ckalloc(len); |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 667 | if (!args) { |
| 668 | PyErr_NoMemory(); |
| 669 | Py_DECREF(v); |
| 670 | return NULL; |
| 671 | } |
| 672 | |
| 673 | args[0] = '\0'; |
| 674 | if (sync) |
| 675 | strcat(args, "-sync"); |
| 676 | if (use) { |
| 677 | if (sync) |
| 678 | strcat(args, " "); |
| 679 | strcat(args, "-use "); |
| 680 | strcat(args, use); |
| 681 | } |
| 682 | |
| 683 | Tcl_SetVar(v->interp, "argv", args, TCL_GLOBAL_ONLY); |
| 684 | ckfree(args); |
| 685 | } |
| 686 | |
Thomas Wouters | a74a84d | 2006-03-07 14:04:31 +0000 | [diff] [blame] | 687 | if (Tcl_AppInit(v->interp) != TCL_OK) { |
| 688 | PyObject *result = Tkinter_Error((PyObject *)v); |
| 689 | Py_DECREF((PyObject *)v); |
| 690 | return (TkappObject *)result; |
| 691 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 693 | EnableEventHook(); |
| 694 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 695 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 698 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 699 | static void |
| 700 | Tkapp_ThreadSend(TkappObject *self, Tcl_Event *ev, |
| 701 | Tcl_Condition *cond, Tcl_Mutex *mutex) |
| 702 | { |
| 703 | Py_BEGIN_ALLOW_THREADS; |
| 704 | Tcl_MutexLock(mutex); |
| 705 | Tcl_ThreadQueueEvent(self->thread_id, ev, TCL_QUEUE_TAIL); |
| 706 | Tcl_ThreadAlert(self->thread_id); |
| 707 | Tcl_ConditionWait(cond, mutex, NULL); |
| 708 | Tcl_MutexUnlock(mutex); |
| 709 | Py_END_ALLOW_THREADS |
| 710 | } |
| 711 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 713 | /** Tcl Eval **/ |
| 714 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 715 | typedef struct { |
| 716 | PyObject_HEAD |
| 717 | Tcl_Obj *value; |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 718 | PyObject *string; /* This cannot cause cycles. */ |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 719 | } PyTclObject; |
| 720 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 721 | static PyTypeObject PyTclObject_Type; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 722 | #define PyTclObject_Check(v) ((v)->ob_type == &PyTclObject_Type) |
| 723 | |
| 724 | static PyObject * |
| 725 | newPyTclObject(Tcl_Obj *arg) |
| 726 | { |
| 727 | PyTclObject *self; |
| 728 | self = PyObject_New(PyTclObject, &PyTclObject_Type); |
| 729 | if (self == NULL) |
| 730 | return NULL; |
| 731 | Tcl_IncrRefCount(arg); |
| 732 | self->value = arg; |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 733 | self->string = NULL; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 734 | return (PyObject*)self; |
| 735 | } |
| 736 | |
| 737 | static void |
| 738 | PyTclObject_dealloc(PyTclObject *self) |
| 739 | { |
| 740 | Tcl_DecrRefCount(self->value); |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 741 | Py_XDECREF(self->string); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 742 | PyObject_Del(self); |
| 743 | } |
| 744 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 745 | static char* |
| 746 | PyTclObject_TclString(PyObject *self) |
| 747 | { |
| 748 | return Tcl_GetString(((PyTclObject*)self)->value); |
| 749 | } |
| 750 | |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 751 | /* Like _str, but create Unicode if necessary. */ |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 752 | PyDoc_STRVAR(PyTclObject_string__doc__, |
Walter Dörwald | a188466 | 2007-07-12 12:16:02 +0000 | [diff] [blame] | 753 | "the string representation of this object, either as str8 or str"); |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 754 | |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 755 | static PyObject * |
| 756 | PyTclObject_string(PyTclObject *self, void *ignored) |
| 757 | { |
| 758 | char *s; |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 759 | int len; |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 760 | if (!self->string) { |
| 761 | s = Tcl_GetStringFromObj(self->value, &len); |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 762 | self->string = PyUnicode_FromStringAndSize(s, len); |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 763 | if (!self->string) |
| 764 | return NULL; |
| 765 | } |
| 766 | Py_INCREF(self->string); |
| 767 | return self->string; |
| 768 | } |
| 769 | |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 770 | static PyObject * |
Walter Dörwald | 6720d91 | 2007-07-12 12:12:25 +0000 | [diff] [blame] | 771 | PyTclObject_str(PyTclObject *self, void *ignored) |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 772 | { |
| 773 | char *s; |
| 774 | int len; |
| 775 | if (self->string && PyUnicode_Check(self->string)) { |
| 776 | Py_INCREF(self->string); |
| 777 | return self->string; |
| 778 | } |
| 779 | /* XXX Could chache result if it is non-ASCII. */ |
| 780 | s = Tcl_GetStringFromObj(self->value, &len); |
| 781 | return PyUnicode_DecodeUTF8(s, len, "strict"); |
| 782 | } |
Martin v. Löwis | 25c7b50 | 2003-01-04 00:08:09 +0000 | [diff] [blame] | 783 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 784 | static PyObject * |
| 785 | PyTclObject_repr(PyTclObject *self) |
| 786 | { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 787 | return PyUnicode_FromFormat("<%s object at %p>", |
| 788 | self->value->typePtr->name, self->value); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Martin v. Löwis | dd6cd65 | 2003-05-03 09:45:12 +0000 | [diff] [blame] | 791 | static int |
| 792 | PyTclObject_cmp(PyTclObject *self, PyTclObject *other) |
| 793 | { |
| 794 | int res; |
| 795 | res = strcmp(Tcl_GetString(self->value), |
| 796 | Tcl_GetString(other->value)); |
| 797 | if (res < 0) return -1; |
| 798 | if (res > 0) return 1; |
| 799 | return 0; |
| 800 | } |
| 801 | |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 802 | PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type"); |
| 803 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 804 | static PyObject* |
| 805 | get_typename(PyTclObject* obj, void* ignored) |
| 806 | { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 807 | return PyUnicode_FromString(obj->value->typePtr->name); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 810 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 811 | static PyGetSetDef PyTclObject_getsetlist[] = { |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 812 | {"typename", (getter)get_typename, NULL, get_typename__doc__}, |
| 813 | {"string", (getter)PyTclObject_string, NULL, |
| 814 | PyTclObject_string__doc__}, |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 815 | {0}, |
| 816 | }; |
| 817 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 818 | static PyTypeObject PyTclObject_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 819 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 820 | "_tkinter.Tcl_Obj", /*tp_name*/ |
| 821 | sizeof(PyTclObject), /*tp_basicsize*/ |
| 822 | 0, /*tp_itemsize*/ |
| 823 | /* methods */ |
| 824 | (destructor)PyTclObject_dealloc, /*tp_dealloc*/ |
| 825 | 0, /*tp_print*/ |
| 826 | 0, /*tp_getattr*/ |
| 827 | 0, /*tp_setattr*/ |
Martin v. Löwis | dd6cd65 | 2003-05-03 09:45:12 +0000 | [diff] [blame] | 828 | (cmpfunc)PyTclObject_cmp, /*tp_compare*/ |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 829 | (reprfunc)PyTclObject_repr, /*tp_repr*/ |
| 830 | 0, /*tp_as_number*/ |
| 831 | 0, /*tp_as_sequence*/ |
| 832 | 0, /*tp_as_mapping*/ |
| 833 | 0, /*tp_hash*/ |
| 834 | 0, /*tp_call*/ |
| 835 | (reprfunc)PyTclObject_str, /*tp_str*/ |
Jason Tishler | fb8595d | 2003-01-06 12:41:26 +0000 | [diff] [blame] | 836 | PyObject_GenericGetAttr,/*tp_getattro*/ |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 837 | 0, /*tp_setattro*/ |
| 838 | 0, /*tp_as_buffer*/ |
| 839 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 840 | 0, /*tp_doc*/ |
| 841 | 0, /*tp_traverse*/ |
| 842 | 0, /*tp_clear*/ |
| 843 | 0, /*tp_richcompare*/ |
| 844 | 0, /*tp_weaklistoffset*/ |
| 845 | 0, /*tp_iter*/ |
| 846 | 0, /*tp_iternext*/ |
Walter Dörwald | 6720d91 | 2007-07-12 12:12:25 +0000 | [diff] [blame] | 847 | 0, /*tp_methods*/ |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 848 | 0, /*tp_members*/ |
| 849 | PyTclObject_getsetlist, /*tp_getset*/ |
| 850 | 0, /*tp_base*/ |
| 851 | 0, /*tp_dict*/ |
| 852 | 0, /*tp_descr_get*/ |
| 853 | 0, /*tp_descr_set*/ |
| 854 | 0, /*tp_dictoffset*/ |
| 855 | 0, /*tp_init*/ |
| 856 | 0, /*tp_alloc*/ |
| 857 | 0, /*tp_new*/ |
| 858 | 0, /*tp_free*/ |
| 859 | 0, /*tp_is_gc*/ |
| 860 | }; |
| 861 | |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 862 | static Tcl_Obj* |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 863 | AsObj(PyObject *value) |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 864 | { |
| 865 | Tcl_Obj *result; |
| 866 | |
| 867 | if (PyString_Check(value)) |
| 868 | return Tcl_NewStringObj(PyString_AS_STRING(value), |
| 869 | PyString_GET_SIZE(value)); |
Martin v. Löwis | 70c3dda | 2003-01-22 09:17:38 +0000 | [diff] [blame] | 870 | else if (PyBool_Check(value)) |
| 871 | return Tcl_NewBooleanObj(PyObject_IsTrue(value)); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 872 | else if (PyInt_CheckExact(value)) |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 873 | return Tcl_NewLongObj(PyInt_AS_LONG(value)); |
| 874 | else if (PyFloat_Check(value)) |
| 875 | return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); |
| 876 | else if (PyTuple_Check(value)) { |
| 877 | Tcl_Obj **argv = (Tcl_Obj**) |
| 878 | ckalloc(PyTuple_Size(value)*sizeof(Tcl_Obj*)); |
| 879 | int i; |
| 880 | if(!argv) |
| 881 | return 0; |
| 882 | for(i=0;i<PyTuple_Size(value);i++) |
| 883 | argv[i] = AsObj(PyTuple_GetItem(value,i)); |
| 884 | result = Tcl_NewListObj(PyTuple_Size(value), argv); |
| 885 | ckfree(FREECAST argv); |
| 886 | return result; |
| 887 | } |
| 888 | else if (PyUnicode_Check(value)) { |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 889 | Py_UNICODE *inbuf = PyUnicode_AS_UNICODE(value); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 890 | Py_ssize_t size = PyUnicode_GET_SIZE(value); |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 891 | /* This #ifdef assumes that Tcl uses UCS-2. |
| 892 | See TCL_UTF_MAX test above. */ |
Martin v. Löwis | 6f29ff3 | 2003-04-16 20:34:55 +0000 | [diff] [blame] | 893 | #if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX == 3 |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 894 | Tcl_UniChar *outbuf; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 895 | Py_ssize_t i; |
| 896 | assert(size < size * sizeof(Tcl_UniChar)); |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 897 | outbuf = (Tcl_UniChar*)ckalloc(size * sizeof(Tcl_UniChar)); |
| 898 | if (!outbuf) { |
| 899 | PyErr_NoMemory(); |
| 900 | return NULL; |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 901 | } |
Martin v. Löwis | 3c6d6f2 | 2002-10-01 18:50:56 +0000 | [diff] [blame] | 902 | for (i = 0; i < size; i++) { |
| 903 | if (inbuf[i] >= 0x10000) { |
| 904 | /* Tcl doesn't do UTF-16, yet. */ |
| 905 | PyErr_SetString(PyExc_ValueError, |
| 906 | "unsupported character"); |
| 907 | ckfree(FREECAST outbuf); |
| 908 | return NULL; |
| 909 | } |
| 910 | outbuf[i] = inbuf[i]; |
| 911 | } |
| 912 | result = Tcl_NewUnicodeObj(outbuf, size); |
| 913 | ckfree(FREECAST outbuf); |
| 914 | return result; |
| 915 | #else |
| 916 | return Tcl_NewUnicodeObj(inbuf, size); |
| 917 | #endif |
| 918 | |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 919 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 920 | else if(PyTclObject_Check(value)) { |
| 921 | Tcl_Obj *v = ((PyTclObject*)value)->value; |
| 922 | Tcl_IncrRefCount(v); |
| 923 | return v; |
| 924 | } |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 925 | else { |
| 926 | PyObject *v = PyObject_Str(value); |
| 927 | if (!v) |
| 928 | return 0; |
| 929 | result = AsObj(v); |
| 930 | Py_DECREF(v); |
| 931 | return result; |
| 932 | } |
| 933 | } |
| 934 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 935 | static PyObject* |
| 936 | FromObj(PyObject* tkapp, Tcl_Obj *value) |
| 937 | { |
| 938 | PyObject *result = NULL; |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 939 | TkappObject *app = (TkappObject*)tkapp; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 940 | |
Martin v. Löwis | e07e18d | 2002-12-04 19:54:36 +0000 | [diff] [blame] | 941 | if (value->typePtr == NULL) { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 942 | return PyUnicode_FromStringAndSize(value->bytes, |
| 943 | value->length); |
Martin v. Löwis | e07e18d | 2002-12-04 19:54:36 +0000 | [diff] [blame] | 944 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 945 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 946 | if (value->typePtr == app->BooleanType) { |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 947 | result = value->internalRep.longValue ? Py_True : Py_False; |
| 948 | Py_INCREF(result); |
| 949 | return result; |
| 950 | } |
| 951 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 952 | if (value->typePtr == app->ByteArrayType) { |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 953 | int size; |
Martin v. Löwis | 33ec3ba | 2002-12-07 14:57:11 +0000 | [diff] [blame] | 954 | char *data = (char*)Tcl_GetByteArrayFromObj(value, &size); |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 955 | return PyBytes_FromStringAndSize(data, size); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 958 | if (value->typePtr == app->DoubleType) { |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 959 | return PyFloat_FromDouble(value->internalRep.doubleValue); |
| 960 | } |
| 961 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 962 | if (value->typePtr == app->IntType) { |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 963 | return PyInt_FromLong(value->internalRep.longValue); |
| 964 | } |
| 965 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 966 | if (value->typePtr == app->ListType) { |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 967 | int size; |
| 968 | int i, status; |
| 969 | PyObject *elem; |
| 970 | Tcl_Obj *tcl_elem; |
| 971 | |
| 972 | status = Tcl_ListObjLength(Tkapp_Interp(tkapp), value, &size); |
| 973 | if (status == TCL_ERROR) |
| 974 | return Tkinter_Error(tkapp); |
| 975 | result = PyTuple_New(size); |
| 976 | if (!result) |
| 977 | return NULL; |
| 978 | for (i = 0; i < size; i++) { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 979 | status = Tcl_ListObjIndex(Tkapp_Interp(tkapp), |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 980 | value, i, &tcl_elem); |
| 981 | if (status == TCL_ERROR) { |
| 982 | Py_DECREF(result); |
| 983 | return Tkinter_Error(tkapp); |
| 984 | } |
| 985 | elem = FromObj(tkapp, tcl_elem); |
| 986 | if (!elem) { |
| 987 | Py_DECREF(result); |
| 988 | return NULL; |
| 989 | } |
| 990 | PyTuple_SetItem(result, i, elem); |
| 991 | } |
| 992 | return result; |
| 993 | } |
| 994 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 995 | if (value->typePtr == app->ProcBodyType) { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 996 | /* fall through: return tcl object. */ |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Martin v. Löwis | 4ec2e70 | 2002-11-26 22:12:12 +0000 | [diff] [blame] | 999 | if (value->typePtr == app->StringType) { |
Martin v. Löwis | 6f29ff3 | 2003-04-16 20:34:55 +0000 | [diff] [blame] | 1000 | #if defined(Py_UNICODE_WIDE) && TCL_UTF_MAX==3 |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1001 | PyObject *result; |
| 1002 | int size; |
| 1003 | Tcl_UniChar *input; |
| 1004 | Py_UNICODE *output; |
| 1005 | |
| 1006 | size = Tcl_GetCharLength(value); |
| 1007 | result = PyUnicode_FromUnicode(NULL, size); |
| 1008 | if (!result) |
| 1009 | return NULL; |
| 1010 | input = Tcl_GetUnicode(value); |
| 1011 | output = PyUnicode_AS_UNICODE(result); |
| 1012 | while (size--) |
| 1013 | *output++ = *input++; |
| 1014 | return result; |
| 1015 | #else |
| 1016 | return PyUnicode_FromUnicode(Tcl_GetUnicode(value), |
| 1017 | Tcl_GetCharLength(value)); |
| 1018 | #endif |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | return newPyTclObject(value); |
| 1022 | } |
| 1023 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1024 | /* This mutex synchronizes inter-thread command calls. */ |
| 1025 | |
| 1026 | TCL_DECLARE_MUTEX(call_mutex) |
| 1027 | |
| 1028 | typedef struct Tkapp_CallEvent { |
| 1029 | Tcl_Event ev; /* Must be first */ |
| 1030 | TkappObject *self; |
| 1031 | PyObject *args; |
| 1032 | int flags; |
| 1033 | PyObject **res; |
| 1034 | PyObject **exc_type, **exc_value, **exc_tb; |
| 1035 | Tcl_Condition done; |
| 1036 | } Tkapp_CallEvent; |
| 1037 | |
| 1038 | void |
| 1039 | Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1040 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1041 | int i; |
| 1042 | for (i = 0; i < objc; i++) |
| 1043 | Tcl_DecrRefCount(objv[i]); |
| 1044 | if (objv != objStore) |
| 1045 | ckfree(FREECAST objv); |
| 1046 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1047 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1048 | /* Convert Python objects to Tcl objects. This must happen in the |
| 1049 | interpreter thread, which may or may not be the calling thread. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1050 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1051 | static Tcl_Obj** |
| 1052 | Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc) |
| 1053 | { |
| 1054 | Tcl_Obj **objv = objStore; |
Martin v. Löwis | 7d13495 | 2002-12-12 19:05:48 +0000 | [diff] [blame] | 1055 | int objc = 0, i; |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1056 | if (args == NULL) |
Martin v. Löwis | 0295601 | 2000-10-29 00:44:43 +0000 | [diff] [blame] | 1057 | /* do nothing */; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1059 | else if (!PyTuple_Check(args)) { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1060 | objv[0] = AsObj(args); |
| 1061 | if (objv[0] == 0) |
| 1062 | goto finally; |
Martin v. Löwis | 0295601 | 2000-10-29 00:44:43 +0000 | [diff] [blame] | 1063 | objc = 1; |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1064 | Tcl_IncrRefCount(objv[0]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1065 | } |
| 1066 | else { |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1067 | objc = PyTuple_Size(args); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1069 | if (objc > ARGSZ) { |
| 1070 | objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *)); |
| 1071 | if (objv == NULL) { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1072 | PyErr_NoMemory(); |
Martin v. Löwis | 0295601 | 2000-10-29 00:44:43 +0000 | [diff] [blame] | 1073 | objc = 0; |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1074 | goto finally; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1078 | for (i = 0; i < objc; i++) { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1079 | PyObject *v = PyTuple_GetItem(args, i); |
Guido van Rossum | 64231e5 | 2000-03-31 03:29:39 +0000 | [diff] [blame] | 1080 | if (v == Py_None) { |
| 1081 | objc = i; |
| 1082 | break; |
| 1083 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1084 | objv[i] = AsObj(v); |
Martin v. Löwis | 0295601 | 2000-10-29 00:44:43 +0000 | [diff] [blame] | 1085 | if (!objv[i]) { |
| 1086 | /* Reset objc, so it attempts to clear |
| 1087 | objects only up to i. */ |
| 1088 | objc = i; |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1089 | goto finally; |
Martin v. Löwis | 0295601 | 2000-10-29 00:44:43 +0000 | [diff] [blame] | 1090 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1091 | Tcl_IncrRefCount(objv[i]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1094 | *pobjc = objc; |
| 1095 | return objv; |
| 1096 | finally: |
| 1097 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
| 1098 | return NULL; |
| 1099 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1100 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1101 | /* Convert the results of a command call into a Python objects. */ |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1102 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1103 | static PyObject* |
| 1104 | Tkapp_CallResult(TkappObject *self) |
| 1105 | { |
| 1106 | PyObject *res = NULL; |
| 1107 | if(self->wantobjects) { |
| 1108 | Tcl_Obj *value = Tcl_GetObjResult(self->interp); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1109 | /* Not sure whether the IncrRef is necessary, but something |
| 1110 | may overwrite the interpreter result while we are |
| 1111 | converting it. */ |
| 1112 | Tcl_IncrRefCount(value); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1113 | res = FromObj((PyObject*)self, value); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1114 | Tcl_DecrRefCount(value); |
| 1115 | } else { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1116 | const char *s = Tcl_GetStringResult(self->interp); |
Martin v. Löwis | 71e25a0 | 2002-10-01 18:08:06 +0000 | [diff] [blame] | 1117 | const char *p = s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1118 | |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1119 | res = PyUnicode_FromStringAndSize(s, (int)(p-s)); |
Guido van Rossum | 990f5c6 | 2000-05-04 15:07:16 +0000 | [diff] [blame] | 1120 | } |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1121 | return res; |
| 1122 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1123 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1124 | /* Tkapp_CallProc is the event procedure that is executed in the context of |
| 1125 | the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't |
| 1126 | hold the Python lock. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1127 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1128 | static int |
| 1129 | Tkapp_CallProc(Tkapp_CallEvent *e, int flags) |
| 1130 | { |
| 1131 | Tcl_Obj *objStore[ARGSZ]; |
| 1132 | Tcl_Obj **objv; |
| 1133 | int objc; |
| 1134 | int i; |
| 1135 | ENTER_PYTHON |
| 1136 | objv = Tkapp_CallArgs(e->args, objStore, &objc); |
| 1137 | if (!objv) { |
| 1138 | PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb); |
| 1139 | *(e->res) = NULL; |
| 1140 | } |
| 1141 | LEAVE_PYTHON |
| 1142 | if (!objv) |
| 1143 | goto done; |
| 1144 | i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags); |
| 1145 | ENTER_PYTHON |
| 1146 | if (i == TCL_ERROR) { |
| 1147 | *(e->res) = NULL; |
| 1148 | *(e->exc_type) = NULL; |
| 1149 | *(e->exc_tb) = NULL; |
| 1150 | *(e->exc_value) = PyObject_CallFunction( |
| 1151 | Tkinter_TclError, "s", |
| 1152 | Tcl_GetStringResult(e->self->interp)); |
| 1153 | } |
| 1154 | else { |
| 1155 | *(e->res) = Tkapp_CallResult(e->self); |
| 1156 | } |
| 1157 | LEAVE_PYTHON |
| 1158 | done: |
| 1159 | /* Wake up calling thread. */ |
| 1160 | Tcl_MutexLock(&call_mutex); |
| 1161 | Tcl_ConditionNotify(&e->done); |
| 1162 | Tcl_MutexUnlock(&call_mutex); |
| 1163 | return 1; |
| 1164 | } |
| 1165 | |
| 1166 | /* This is the main entry point for calling a Tcl command. |
| 1167 | It supports three cases, with regard to threading: |
| 1168 | 1. Tcl is not threaded: Must have the Tcl lock, then can invoke command in |
| 1169 | the context of the calling thread. |
| 1170 | 2. Tcl is threaded, caller of the command is in the interpreter thread: |
| 1171 | Execute the command in the calling thread. Since the Tcl lock will |
| 1172 | not be used, we can merge that with case 1. |
| 1173 | 3. Tcl is threaded, caller is in a different thread: Must queue an event to |
| 1174 | the interpreter thread. Allocation of Tcl objects needs to occur in the |
| 1175 | interpreter thread, so we ship the PyObject* args to the target thread, |
| 1176 | and perform processing there. */ |
| 1177 | |
| 1178 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1179 | Tkapp_Call(PyObject *selfptr, PyObject *args) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1180 | { |
| 1181 | Tcl_Obj *objStore[ARGSZ]; |
| 1182 | Tcl_Obj **objv = NULL; |
| 1183 | int objc, i; |
| 1184 | PyObject *res = NULL; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1185 | TkappObject *self = (TkappObject*)selfptr; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1186 | /* Could add TCL_EVAL_GLOBAL if wrapped by GlobalCall... */ |
| 1187 | int flags = TCL_EVAL_DIRECT; |
| 1188 | |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1189 | /* If args is a single tuple, replace with contents of tuple */ |
| 1190 | if (1 == PyTuple_Size(args)){ |
| 1191 | PyObject* item = PyTuple_GetItem(args, 0); |
| 1192 | if (PyTuple_Check(item)) |
| 1193 | args = item; |
| 1194 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1195 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1196 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 1197 | /* We cannot call the command directly. Instead, we must |
| 1198 | marshal the parameters to the interpreter thread. */ |
| 1199 | Tkapp_CallEvent *ev; |
| 1200 | PyObject *exc_type, *exc_value, *exc_tb; |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 1201 | if (!WaitForMainloop(self)) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1202 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1203 | ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent)); |
| 1204 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; |
| 1205 | ev->self = self; |
| 1206 | ev->args = args; |
| 1207 | ev->res = &res; |
| 1208 | ev->exc_type = &exc_type; |
| 1209 | ev->exc_value = &exc_value; |
| 1210 | ev->exc_tb = &exc_tb; |
| 1211 | ev->done = (Tcl_Condition)0; |
| 1212 | |
| 1213 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &ev->done, &call_mutex); |
| 1214 | |
| 1215 | if (res == NULL) { |
| 1216 | if (exc_type) |
| 1217 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 1218 | else |
| 1219 | PyErr_SetObject(Tkinter_TclError, exc_value); |
| 1220 | } |
| 1221 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1222 | else |
| 1223 | #endif |
| 1224 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1225 | |
| 1226 | objv = Tkapp_CallArgs(args, objStore, &objc); |
| 1227 | if (!objv) |
| 1228 | return NULL; |
| 1229 | |
| 1230 | ENTER_TCL |
| 1231 | |
| 1232 | i = Tcl_EvalObjv(self->interp, objc, objv, flags); |
| 1233 | |
| 1234 | ENTER_OVERLAP |
| 1235 | |
| 1236 | if (i == TCL_ERROR) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1237 | Tkinter_Error(selfptr); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1238 | else |
| 1239 | res = Tkapp_CallResult(self); |
| 1240 | |
| 1241 | LEAVE_OVERLAP_TCL |
| 1242 | |
| 1243 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
| 1244 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1245 | return res; |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1250 | Tkapp_GlobalCall(PyObject *self, PyObject *args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1251 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1252 | /* Could do the same here as for Tkapp_Call(), but this is not used |
| 1253 | much, so I can't be bothered. Unfortunately Tcl doesn't export a |
| 1254 | way for the user to do what all its Global* variants do (save and |
| 1255 | reset the scope pointer, call the local version, restore the saved |
| 1256 | scope pointer). */ |
| 1257 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1258 | char *cmd; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1259 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1260 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1261 | CHECK_TCL_APPARTMENT; |
| 1262 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1263 | cmd = Merge(args); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1264 | if (cmd) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1265 | int err; |
| 1266 | ENTER_TCL |
| 1267 | err = Tcl_GlobalEval(Tkapp_Interp(self), cmd); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1268 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1269 | if (err == TCL_ERROR) |
| 1270 | res = Tkinter_Error(self); |
| 1271 | else |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1272 | res = PyUnicode_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1273 | LEAVE_OVERLAP_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1274 | ckfree(cmd); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1275 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1276 | |
| 1277 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1281 | Tkapp_Eval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1282 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1283 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1284 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1285 | int err; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1286 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1287 | if (!PyArg_ParseTuple(args, "s:eval", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1288 | return NULL; |
| 1289 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1290 | CHECK_TCL_APPARTMENT; |
| 1291 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1292 | ENTER_TCL |
| 1293 | err = Tcl_Eval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1294 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1295 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1296 | res = Tkinter_Error(self); |
| 1297 | else |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1298 | res = PyUnicode_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1299 | LEAVE_OVERLAP_TCL |
| 1300 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1304 | Tkapp_GlobalEval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1305 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1306 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1307 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1308 | int err; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1309 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1310 | if (!PyArg_ParseTuple(args, "s:globaleval", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1311 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1312 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1313 | CHECK_TCL_APPARTMENT; |
| 1314 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1315 | ENTER_TCL |
| 1316 | err = Tcl_GlobalEval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1317 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1318 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1319 | res = Tkinter_Error(self); |
| 1320 | else |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1321 | res = PyUnicode_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1322 | LEAVE_OVERLAP_TCL |
| 1323 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1327 | Tkapp_EvalFile(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1328 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1329 | char *fileName; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1330 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1331 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1332 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1333 | if (!PyArg_ParseTuple(args, "s:evalfile", &fileName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1334 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1335 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1336 | CHECK_TCL_APPARTMENT; |
| 1337 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1338 | ENTER_TCL |
| 1339 | err = Tcl_EvalFile(Tkapp_Interp(self), fileName); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1340 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1341 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1342 | res = Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1343 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1344 | else |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1345 | res = PyUnicode_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1346 | LEAVE_OVERLAP_TCL |
| 1347 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1351 | Tkapp_Record(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1352 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1353 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1354 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1355 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1356 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1357 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1358 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1359 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1360 | CHECK_TCL_APPARTMENT; |
| 1361 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1362 | ENTER_TCL |
| 1363 | err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1364 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1365 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1366 | res = Tkinter_Error(self); |
| 1367 | else |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1368 | res = PyUnicode_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1369 | LEAVE_OVERLAP_TCL |
| 1370 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1374 | Tkapp_AddErrorInfo(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1375 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1376 | char *msg; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1377 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1378 | if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1379 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1380 | CHECK_TCL_APPARTMENT; |
| 1381 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1382 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1383 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1384 | LEAVE_TCL |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1385 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1386 | Py_INCREF(Py_None); |
| 1387 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1390 | |
| 1391 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1392 | /** Tcl Variable **/ |
| 1393 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1394 | TCL_DECLARE_MUTEX(var_mutex) |
| 1395 | |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1396 | typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1397 | typedef struct VarEvent { |
| 1398 | Tcl_Event ev; /* must be first */ |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1399 | PyObject *self; |
| 1400 | PyObject *args; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1401 | int flags; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1402 | EventFunc func; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1403 | PyObject **res; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1404 | PyObject **exc_type; |
| 1405 | PyObject **exc_val; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1406 | Tcl_Condition cond; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1407 | } VarEvent; |
| 1408 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1409 | static int |
| 1410 | varname_converter(PyObject *in, void *_out) |
| 1411 | { |
| 1412 | char **out = (char**)_out; |
| 1413 | if (PyString_Check(in)) { |
| 1414 | *out = PyString_AsString(in); |
| 1415 | return 1; |
| 1416 | } |
Guido van Rossum | f761e10 | 2007-07-23 18:34:37 +0000 | [diff] [blame] | 1417 | if (PyUnicode_Check(in)) { |
| 1418 | *out = PyUnicode_AsString(in); |
| 1419 | return 1; |
| 1420 | } |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1421 | if (PyTclObject_Check(in)) { |
| 1422 | *out = PyTclObject_TclString(in); |
| 1423 | return 1; |
| 1424 | } |
| 1425 | /* XXX: Should give diagnostics. */ |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1429 | void |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1430 | var_perform(VarEvent *ev) |
| 1431 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1432 | *(ev->res) = ev->func(ev->self, ev->args, ev->flags); |
| 1433 | if (!*(ev->res)) { |
| 1434 | PyObject *exc, *val, *tb; |
| 1435 | PyErr_Fetch(&exc, &val, &tb); |
| 1436 | PyErr_NormalizeException(&exc, &val, &tb); |
| 1437 | *(ev->exc_type) = exc; |
| 1438 | *(ev->exc_val) = val; |
| 1439 | Py_DECREF(tb); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1440 | } |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1441 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int |
| 1445 | var_proc(VarEvent* ev, int flags) |
| 1446 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1447 | ENTER_PYTHON |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1448 | var_perform(ev); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1449 | Tcl_MutexLock(&var_mutex); |
| 1450 | Tcl_ConditionNotify(&ev->cond); |
| 1451 | Tcl_MutexUnlock(&var_mutex); |
| 1452 | LEAVE_PYTHON |
| 1453 | return 1; |
| 1454 | } |
| 1455 | |
| 1456 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1457 | var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1458 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1459 | TkappObject *self = (TkappObject*)selfptr; |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1460 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1461 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1462 | TkappObject *self = (TkappObject*)selfptr; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1463 | VarEvent *ev; |
| 1464 | PyObject *res, *exc_type, *exc_val; |
| 1465 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1466 | /* The current thread is not the interpreter thread. Marshal |
| 1467 | the call to the interpreter thread, then wait for |
| 1468 | completion. */ |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 1469 | if (!WaitForMainloop(self)) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1470 | return NULL; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1471 | |
| 1472 | ev = (VarEvent*)ckalloc(sizeof(VarEvent)); |
| 1473 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1474 | ev->self = selfptr; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1475 | ev->args = args; |
| 1476 | ev->flags = flags; |
| 1477 | ev->func = func; |
| 1478 | ev->res = &res; |
| 1479 | ev->exc_type = &exc_type; |
| 1480 | ev->exc_val = &exc_val; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1481 | ev->cond = NULL; |
| 1482 | ev->ev.proc = (Tcl_EventProc*)var_proc; |
| 1483 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &ev->cond, &var_mutex); |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1484 | if (!res) { |
| 1485 | PyErr_SetObject(exc_type, exc_val); |
| 1486 | Py_DECREF(exc_type); |
| 1487 | Py_DECREF(exc_val); |
| 1488 | return NULL; |
| 1489 | } |
| 1490 | return res; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1491 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1492 | #endif |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1493 | /* Tcl is not threaded, or this is the interpreter thread. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1494 | return func(selfptr, args, flags); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1497 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1498 | SetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1499 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1500 | char *name1, *name2; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1501 | PyObject *newValue; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1502 | PyObject *res = NULL; |
| 1503 | Tcl_Obj *newval, *ok; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1504 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1505 | if (PyArg_ParseTuple(args, "O&O:setvar", |
| 1506 | varname_converter, &name1, &newValue)) { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1507 | /* XXX Acquire tcl lock??? */ |
| 1508 | newval = AsObj(newValue); |
| 1509 | if (newval == NULL) |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1510 | return NULL; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1511 | ENTER_TCL |
| 1512 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL, |
| 1513 | newval, flags); |
| 1514 | ENTER_OVERLAP |
| 1515 | if (!ok) |
| 1516 | Tkinter_Error(self); |
| 1517 | else { |
| 1518 | res = Py_None; |
| 1519 | Py_INCREF(res); |
| 1520 | } |
| 1521 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1522 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1523 | else { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1524 | PyErr_Clear(); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1525 | if (PyArg_ParseTuple(args, "ssO:setvar", |
| 1526 | &name1, &name2, &newValue)) { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1527 | /* XXX must hold tcl lock already??? */ |
| 1528 | newval = AsObj(newValue); |
| 1529 | ENTER_TCL |
| 1530 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags); |
| 1531 | ENTER_OVERLAP |
| 1532 | if (!ok) |
| 1533 | Tkinter_Error(self); |
| 1534 | else { |
| 1535 | res = Py_None; |
| 1536 | Py_INCREF(res); |
| 1537 | } |
| 1538 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1539 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1540 | else { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1541 | return NULL; |
| 1542 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1543 | } |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1544 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1548 | Tkapp_SetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1549 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1550 | return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1554 | Tkapp_GlobalSetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1555 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1556 | return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1559 | |
| 1560 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1561 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1562 | GetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1563 | { |
Martin v. Löwis | 71e25a0 | 2002-10-01 18:08:06 +0000 | [diff] [blame] | 1564 | char *name1, *name2=NULL; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1565 | PyObject *res = NULL; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1566 | Tcl_Obj *tres; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1567 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1568 | if (!PyArg_ParseTuple(args, "O&|s:getvar", |
| 1569 | varname_converter, &name1, &name2)) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1570 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1571 | |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1572 | ENTER_TCL |
| 1573 | tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags); |
| 1574 | ENTER_OVERLAP |
Martin v. Löwis | d46e684 | 2003-10-03 17:12:26 +0000 | [diff] [blame] | 1575 | if (tres == NULL) { |
| 1576 | PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self))); |
| 1577 | } else { |
| 1578 | if (((TkappObject*)self)->wantobjects) { |
| 1579 | res = FromObj(self, tres); |
| 1580 | } |
| 1581 | else { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1582 | res = PyUnicode_FromString(Tcl_GetString(tres)); |
Martin v. Löwis | d46e684 | 2003-10-03 17:12:26 +0000 | [diff] [blame] | 1583 | } |
Martin v. Löwis | 8fd86cc | 2003-05-19 19:57:42 +0000 | [diff] [blame] | 1584 | } |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1585 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1586 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1590 | Tkapp_GetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1591 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1592 | return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1596 | Tkapp_GlobalGetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1597 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1598 | return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1601 | |
| 1602 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1603 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1604 | UnsetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1605 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1606 | char *name1, *name2=NULL; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1607 | int code; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1608 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1609 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1610 | if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1611 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1612 | |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1613 | ENTER_TCL |
| 1614 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
| 1615 | ENTER_OVERLAP |
| 1616 | if (code == TCL_ERROR) |
| 1617 | res = Tkinter_Error(self); |
| 1618 | else { |
| 1619 | Py_INCREF(Py_None); |
| 1620 | res = Py_None; |
| 1621 | } |
| 1622 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1623 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1627 | Tkapp_UnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1628 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1629 | return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1633 | Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1634 | { |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1635 | return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1638 | |
| 1639 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1640 | /** Tcl to Python **/ |
| 1641 | |
| 1642 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1643 | Tkapp_GetInt(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1644 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1645 | char *s; |
| 1646 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1647 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1648 | if (PyTuple_Size(args) == 1) { |
| 1649 | PyObject* o = PyTuple_GetItem(args, 0); |
| 1650 | if (PyInt_Check(o)) { |
| 1651 | Py_INCREF(o); |
| 1652 | return o; |
| 1653 | } |
| 1654 | } |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1655 | if (!PyArg_ParseTuple(args, "s:getint", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1656 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1657 | if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1658 | return Tkinter_Error(self); |
| 1659 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1663 | Tkapp_GetDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1664 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1665 | char *s; |
| 1666 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1667 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1668 | if (PyTuple_Size(args) == 1) { |
| 1669 | PyObject *o = PyTuple_GetItem(args, 0); |
| 1670 | if (PyFloat_Check(o)) { |
| 1671 | Py_INCREF(o); |
| 1672 | return o; |
| 1673 | } |
| 1674 | } |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1675 | if (!PyArg_ParseTuple(args, "s:getdouble", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1676 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1677 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1678 | return Tkinter_Error(self); |
| 1679 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1683 | Tkapp_GetBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1684 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1685 | char *s; |
| 1686 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1687 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1688 | if (PyTuple_Size(args) == 1) { |
| 1689 | PyObject *o = PyTuple_GetItem(args, 0); |
| 1690 | if (PyInt_Check(o)) { |
| 1691 | Py_INCREF(o); |
| 1692 | return o; |
| 1693 | } |
| 1694 | } |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1695 | if (!PyArg_ParseTuple(args, "s:getboolean", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1696 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1697 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 1698 | return Tkinter_Error(self); |
Martin v. Löwis | 70c3dda | 2003-01-22 09:17:38 +0000 | [diff] [blame] | 1699 | return PyBool_FromLong(v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1703 | Tkapp_ExprString(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1704 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1705 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1706 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1707 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1708 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1709 | if (!PyArg_ParseTuple(args, "s:exprstring", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1710 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1711 | |
| 1712 | CHECK_TCL_APPARTMENT; |
| 1713 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1714 | ENTER_TCL |
| 1715 | retval = Tcl_ExprString(Tkapp_Interp(self), s); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1716 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1717 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1718 | res = Tkinter_Error(self); |
| 1719 | else |
| 1720 | res = Py_BuildValue("s", Tkapp_Result(self)); |
| 1721 | LEAVE_OVERLAP_TCL |
| 1722 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1726 | Tkapp_ExprLong(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1727 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1728 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1729 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1730 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1731 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1732 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1733 | if (!PyArg_ParseTuple(args, "s:exprlong", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1734 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1735 | |
| 1736 | CHECK_TCL_APPARTMENT; |
| 1737 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1738 | ENTER_TCL |
| 1739 | retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1740 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1741 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1742 | res = Tkinter_Error(self); |
| 1743 | else |
| 1744 | res = Py_BuildValue("l", v); |
| 1745 | LEAVE_OVERLAP_TCL |
| 1746 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1750 | Tkapp_ExprDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1751 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1752 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1753 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1754 | double v; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1755 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1756 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1757 | if (!PyArg_ParseTuple(args, "s:exprdouble", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1758 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1759 | CHECK_TCL_APPARTMENT; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1760 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1761 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1762 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1763 | ENTER_OVERLAP |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1764 | PyFPE_END_PROTECT(retval) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1765 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1766 | res = Tkinter_Error(self); |
| 1767 | else |
| 1768 | res = Py_BuildValue("d", v); |
| 1769 | LEAVE_OVERLAP_TCL |
| 1770 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1774 | Tkapp_ExprBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1775 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1776 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1777 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1778 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1779 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1780 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1781 | if (!PyArg_ParseTuple(args, "s:exprboolean", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1782 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1783 | CHECK_TCL_APPARTMENT; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1784 | ENTER_TCL |
| 1785 | retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1786 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1787 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1788 | res = Tkinter_Error(self); |
| 1789 | else |
| 1790 | res = Py_BuildValue("i", v); |
| 1791 | LEAVE_OVERLAP_TCL |
| 1792 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1795 | |
| 1796 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1797 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1798 | Tkapp_SplitList(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1799 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1800 | char *list; |
| 1801 | int argc; |
| 1802 | char **argv; |
| 1803 | PyObject *v; |
| 1804 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1805 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1806 | if (PyTuple_Size(args) == 1) { |
| 1807 | v = PyTuple_GetItem(args, 0); |
| 1808 | if (PyTuple_Check(v)) { |
| 1809 | Py_INCREF(v); |
| 1810 | return v; |
| 1811 | } |
| 1812 | } |
Martin v. Löwis | 84432eb | 2002-01-26 20:21:50 +0000 | [diff] [blame] | 1813 | if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1814 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1815 | |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1816 | if (Tcl_SplitList(Tkapp_Interp(self), list, |
| 1817 | &argc, &argv) == TCL_ERROR) { |
| 1818 | PyMem_Free(list); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1819 | return Tkinter_Error(self); |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1820 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1821 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1822 | if (!(v = PyTuple_New(argc))) |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1823 | goto finally; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1824 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1825 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1826 | PyObject *s = PyUnicode_FromString(argv[i]); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1827 | if (!s || PyTuple_SetItem(v, i, s)) { |
| 1828 | Py_DECREF(v); |
| 1829 | v = NULL; |
| 1830 | goto finally; |
| 1831 | } |
| 1832 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1833 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1834 | finally: |
| 1835 | ckfree(FREECAST argv); |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1836 | PyMem_Free(list); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1837 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1841 | Tkapp_Split(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1842 | { |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1843 | PyObject *v; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1844 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1845 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1846 | if (PyTuple_Size(args) == 1) { |
| 1847 | PyObject* o = PyTuple_GetItem(args, 0); |
| 1848 | if (PyTuple_Check(o)) { |
| 1849 | o = SplitObj(o); |
| 1850 | return o; |
| 1851 | } |
| 1852 | } |
Martin v. Löwis | 84432eb | 2002-01-26 20:21:50 +0000 | [diff] [blame] | 1853 | if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1854 | return NULL; |
Neal Norwitz | d1c5510 | 2003-05-29 00:17:03 +0000 | [diff] [blame] | 1855 | v = Split(list); |
| 1856 | PyMem_Free(list); |
| 1857 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1861 | Tkapp_Merge(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1862 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1863 | char *s = Merge(args); |
| 1864 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1865 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1866 | if (s) { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1867 | res = PyUnicode_FromString(s); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1868 | ckfree(s); |
| 1869 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1870 | |
| 1871 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1874 | |
| 1875 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1876 | /** Tcl Command **/ |
| 1877 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1878 | /* Client data struct */ |
| 1879 | typedef struct { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1880 | PyObject *self; |
| 1881 | PyObject *func; |
| 1882 | } PythonCmd_ClientData; |
| 1883 | |
| 1884 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1885 | PythonCmd_Error(Tcl_Interp *interp) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1886 | { |
| 1887 | errorInCmd = 1; |
| 1888 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1889 | LEAVE_PYTHON |
| 1890 | return TCL_ERROR; |
| 1891 | } |
| 1892 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1893 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1894 | * function or method. |
| 1895 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1896 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1897 | PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1898 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1899 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Martin v. Löwis | 50fb866 | 2007-08-13 05:41:41 +0000 | [diff] [blame] | 1900 | PyObject *self, *func, *arg, *res; |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1901 | int i, rv; |
Martin v. Löwis | 50fb866 | 2007-08-13 05:41:41 +0000 | [diff] [blame] | 1902 | Tcl_Obj *tres; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1903 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1904 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1905 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1906 | /* TBD: no error checking here since we know, via the |
| 1907 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 1908 | */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1909 | self = data->self; |
| 1910 | func = data->func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1911 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1912 | /* Create argument list (argv1, ..., argvN) */ |
| 1913 | if (!(arg = PyTuple_New(argc - 1))) |
| 1914 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1915 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1916 | for (i = 0; i < (argc - 1); i++) { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 1917 | PyObject *s = PyUnicode_FromString(argv[i + 1]); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1918 | if (!s || PyTuple_SetItem(arg, i, s)) { |
| 1919 | Py_DECREF(arg); |
Guido van Rossum | f766e23 | 1998-06-19 04:28:10 +0000 | [diff] [blame] | 1920 | return PythonCmd_Error(interp); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | res = PyEval_CallObject(func, arg); |
| 1924 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1925 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1926 | if (res == NULL) |
| 1927 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1928 | |
Martin v. Löwis | 50fb866 | 2007-08-13 05:41:41 +0000 | [diff] [blame] | 1929 | tres = AsObj(res); |
| 1930 | if (tres == NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1931 | Py_DECREF(res); |
| 1932 | return PythonCmd_Error(interp); |
| 1933 | } |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1934 | else { |
Martin v. Löwis | 50fb866 | 2007-08-13 05:41:41 +0000 | [diff] [blame] | 1935 | Tcl_SetObjResult(Tkapp_Interp(self), tres); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1936 | rv = TCL_OK; |
| 1937 | } |
| 1938 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1939 | Py_DECREF(res); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1940 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1941 | LEAVE_PYTHON |
| 1942 | |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 1943 | return rv; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1947 | PythonCmdDelete(ClientData clientData) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1948 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1949 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
| 1950 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 1951 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1952 | Py_XDECREF(data->self); |
| 1953 | Py_XDECREF(data->func); |
| 1954 | PyMem_DEL(data); |
| 1955 | LEAVE_PYTHON |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1958 | |
| 1959 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1960 | |
| 1961 | TCL_DECLARE_MUTEX(command_mutex) |
| 1962 | |
| 1963 | typedef struct CommandEvent{ |
| 1964 | Tcl_Event ev; |
| 1965 | Tcl_Interp* interp; |
| 1966 | char *name; |
| 1967 | int create; |
| 1968 | int *status; |
| 1969 | ClientData *data; |
| 1970 | Tcl_Condition done; |
| 1971 | } CommandEvent; |
| 1972 | |
| 1973 | static int |
| 1974 | Tkapp_CommandProc(CommandEvent *ev, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1975 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1976 | if (ev->create) |
| 1977 | *ev->status = Tcl_CreateCommand( |
| 1978 | ev->interp, ev->name, PythonCmd, |
| 1979 | ev->data, PythonCmdDelete) == NULL; |
| 1980 | else |
| 1981 | *ev->status = Tcl_DeleteCommand(ev->interp, ev->name); |
| 1982 | Tcl_MutexLock(&command_mutex); |
| 1983 | Tcl_ConditionNotify(&ev->done); |
| 1984 | Tcl_MutexUnlock(&command_mutex); |
| 1985 | return 1; |
| 1986 | } |
| 1987 | |
| 1988 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1989 | Tkapp_CreateCommand(PyObject *selfptr, PyObject *args) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1990 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1991 | TkappObject *self = (TkappObject*)selfptr; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1992 | PythonCmd_ClientData *data; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1993 | char *cmdName; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1994 | PyObject *func; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1995 | int err; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1996 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1997 | if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func)) |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1998 | return NULL; |
| 1999 | if (!PyCallable_Check(func)) { |
| 2000 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2001 | return NULL; |
| 2002 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2003 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2004 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2005 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2006 | !WaitForMainloop(self)) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2007 | return NULL; |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2008 | #endif |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2009 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2010 | data = PyMem_NEW(PythonCmd_ClientData, 1); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2011 | if (!data) |
Neal Norwitz | ec74f2f | 2003-02-11 23:05:40 +0000 | [diff] [blame] | 2012 | return PyErr_NoMemory(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2013 | Py_INCREF(self); |
| 2014 | Py_INCREF(func); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2015 | data->self = selfptr; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2016 | data->func = func; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2019 | CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); |
| 2020 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2021 | ev->interp = self->interp; |
| 2022 | ev->create = 1; |
| 2023 | ev->name = cmdName; |
| 2024 | ev->data = (ClientData)data; |
| 2025 | ev->status = &err; |
| 2026 | ev->done = NULL; |
| 2027 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &ev->done, &command_mutex); |
| 2028 | } |
| 2029 | else { |
| 2030 | ENTER_TCL |
| 2031 | err = Tcl_CreateCommand( |
| 2032 | Tkapp_Interp(self), cmdName, PythonCmd, |
| 2033 | (ClientData)data, PythonCmdDelete) == NULL; |
| 2034 | LEAVE_TCL |
| 2035 | } |
| 2036 | if (err) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2037 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2038 | PyMem_DEL(data); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2039 | return NULL; |
| 2040 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2041 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2042 | Py_INCREF(Py_None); |
| 2043 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2046 | |
| 2047 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2048 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2049 | Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2050 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2051 | TkappObject *self = (TkappObject*)selfptr; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2052 | char *cmdName; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2053 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2054 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2055 | if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2056 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2057 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2058 | CommandEvent *ev; |
| 2059 | ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); |
| 2060 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2061 | ev->interp = self->interp; |
| 2062 | ev->create = 0; |
| 2063 | ev->name = cmdName; |
| 2064 | ev->status = &err; |
| 2065 | ev->done = NULL; |
| 2066 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &ev->done, |
| 2067 | &command_mutex); |
| 2068 | } |
| 2069 | else { |
| 2070 | ENTER_TCL |
| 2071 | err = Tcl_DeleteCommand(self->interp, cmdName); |
| 2072 | LEAVE_TCL |
| 2073 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2074 | if (err == -1) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2075 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 2076 | return NULL; |
| 2077 | } |
| 2078 | Py_INCREF(Py_None); |
| 2079 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2082 | |
| 2083 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2084 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2085 | /** File Handler **/ |
| 2086 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2087 | typedef struct _fhcdata { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2088 | PyObject *func; |
| 2089 | PyObject *file; |
| 2090 | int id; |
| 2091 | struct _fhcdata *next; |
| 2092 | } FileHandler_ClientData; |
| 2093 | |
| 2094 | static FileHandler_ClientData *HeadFHCD; |
| 2095 | |
| 2096 | static FileHandler_ClientData * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2097 | NewFHCD(PyObject *func, PyObject *file, int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2098 | { |
| 2099 | FileHandler_ClientData *p; |
| 2100 | p = PyMem_NEW(FileHandler_ClientData, 1); |
| 2101 | if (p != NULL) { |
| 2102 | Py_XINCREF(func); |
| 2103 | Py_XINCREF(file); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2104 | p->func = func; |
| 2105 | p->file = file; |
| 2106 | p->id = id; |
| 2107 | p->next = HeadFHCD; |
| 2108 | HeadFHCD = p; |
| 2109 | } |
| 2110 | return p; |
| 2111 | } |
| 2112 | |
| 2113 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2114 | DeleteFHCD(int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2115 | { |
| 2116 | FileHandler_ClientData *p, **pp; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2117 | |
| 2118 | pp = &HeadFHCD; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2119 | while ((p = *pp) != NULL) { |
| 2120 | if (p->id == id) { |
| 2121 | *pp = p->next; |
| 2122 | Py_XDECREF(p->func); |
| 2123 | Py_XDECREF(p->file); |
| 2124 | PyMem_DEL(p); |
| 2125 | } |
| 2126 | else |
| 2127 | pp = &p->next; |
| 2128 | } |
| 2129 | } |
| 2130 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 2131 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2132 | FileHandler(ClientData clientData, int mask) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2133 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2134 | FileHandler_ClientData *data = (FileHandler_ClientData *)clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2135 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2136 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2137 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2138 | func = data->func; |
| 2139 | file = data->file; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2140 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2141 | arg = Py_BuildValue("(Oi)", file, (long) mask); |
| 2142 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2143 | Py_DECREF(arg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2144 | |
| 2145 | if (res == NULL) { |
| 2146 | errorInCmd = 1; |
| 2147 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2148 | } |
| 2149 | Py_XDECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2150 | LEAVE_PYTHON |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2153 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2154 | Tkapp_CreateFileHandler(PyObject *self, PyObject *args) |
| 2155 | /* args is (file, mask, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2156 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2157 | FileHandler_ClientData *data; |
| 2158 | PyObject *file, *func; |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2159 | int mask, tfile; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2160 | |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2161 | if (!PyArg_ParseTuple(args, "OiO:createfilehandler", |
| 2162 | &file, &mask, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2163 | return NULL; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2164 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2165 | #ifdef WITH_THREAD |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2166 | if (!self && !tcl_lock) { |
| 2167 | /* We don't have the Tcl lock since Tcl is threaded. */ |
| 2168 | PyErr_SetString(PyExc_RuntimeError, |
| 2169 | "_tkinter.createfilehandler not supported " |
| 2170 | "for threaded Tcl"); |
| 2171 | return NULL; |
| 2172 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2173 | #endif |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2174 | |
| 2175 | if (self) { |
| 2176 | CHECK_TCL_APPARTMENT; |
| 2177 | } |
| 2178 | |
Andrew M. Kuchling | 9f28a03 | 2000-07-13 23:59:35 +0000 | [diff] [blame] | 2179 | tfile = PyObject_AsFileDescriptor(file); |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2180 | if (tfile < 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2181 | return NULL; |
| 2182 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2183 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2184 | return NULL; |
| 2185 | } |
| 2186 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2187 | data = NewFHCD(func, file, tfile); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2188 | if (data == NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2189 | return NULL; |
| 2190 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2191 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2192 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2193 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2194 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2195 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2196 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2200 | Tkapp_DeleteFileHandler(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2201 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2202 | PyObject *file; |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2203 | int tfile; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2205 | if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2206 | return NULL; |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2207 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2208 | #ifdef WITH_THREAD |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2209 | if (!self && !tcl_lock) { |
| 2210 | /* We don't have the Tcl lock since Tcl is threaded. */ |
| 2211 | PyErr_SetString(PyExc_RuntimeError, |
| 2212 | "_tkinter.deletefilehandler not supported " |
| 2213 | "for threaded Tcl"); |
| 2214 | return NULL; |
| 2215 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2216 | #endif |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (self) { |
| 2219 | CHECK_TCL_APPARTMENT; |
| 2220 | } |
| 2221 | |
Andrew M. Kuchling | 9f28a03 | 2000-07-13 23:59:35 +0000 | [diff] [blame] | 2222 | tfile = PyObject_AsFileDescriptor(file); |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2223 | if (tfile < 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2224 | return NULL; |
| 2225 | |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2226 | DeleteFHCD(tfile); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2227 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2228 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2229 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2230 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2231 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2232 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2233 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2234 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2235 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2236 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2237 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2238 | /**** Tktt Object (timer token) ****/ |
| 2239 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2240 | static PyTypeObject Tktt_Type; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2241 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2242 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2243 | PyObject_HEAD |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2244 | Tcl_TimerToken token; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2245 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2246 | } TkttObject; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2247 | |
| 2248 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2249 | Tktt_DeleteTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2250 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2251 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2252 | PyObject *func = v->func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2253 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2254 | if (!PyArg_ParseTuple(args, ":deletetimerhandler")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2255 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2256 | if (v->token != NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2257 | Tcl_DeleteTimerHandler(v->token); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2258 | v->token = NULL; |
| 2259 | } |
| 2260 | if (func != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2261 | v->func = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2262 | Py_DECREF(func); |
| 2263 | Py_DECREF(v); /* See Tktt_New() */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2264 | } |
| 2265 | Py_INCREF(Py_None); |
| 2266 | return Py_None; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | static PyMethodDef Tktt_methods[] = |
| 2270 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2271 | {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2272 | {NULL, NULL} |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2273 | }; |
| 2274 | |
| 2275 | static TkttObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2276 | Tktt_New(PyObject *func) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2277 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2278 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2279 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2280 | v = PyObject_New(TkttObject, &Tktt_Type); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2281 | if (v == NULL) |
| 2282 | return NULL; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2284 | Py_INCREF(func); |
| 2285 | v->token = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2286 | v->func = func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2287 | |
| 2288 | /* Extra reference, deleted when called or when handler is deleted */ |
| 2289 | Py_INCREF(v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2290 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2294 | Tktt_Dealloc(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2295 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2296 | TkttObject *v = (TkttObject *)self; |
| 2297 | PyObject *func = v->func; |
| 2298 | |
| 2299 | Py_XDECREF(func); |
| 2300 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2301 | PyObject_Del(self); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2304 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2305 | Tktt_Repr(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2306 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2307 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2308 | char buf[100]; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2309 | |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 2310 | PyOS_snprintf(buf, sizeof(buf), "<tktimertoken at %p%s>", v, |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 2311 | v->func == NULL ? ", handler deleted" : ""); |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 2312 | return PyUnicode_FromString(buf); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | static PyObject * |
Martin v. Löwis | 1d519e4 | 2006-02-27 23:10:11 +0000 | [diff] [blame] | 2316 | Tktt_GetAttr(PyObject *self, char *name) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2317 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2318 | return Py_FindMethod(Tktt_methods, self, name); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | static PyTypeObject Tktt_Type = |
| 2322 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2323 | PyVarObject_HEAD_INIT(NULL, 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2324 | "tktimertoken", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2325 | sizeof(TkttObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2326 | 0, /*tp_itemsize */ |
| 2327 | Tktt_Dealloc, /*tp_dealloc */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2328 | 0, /*tp_print */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2329 | Tktt_GetAttr, /*tp_getattr */ |
| 2330 | 0, /*tp_setattr */ |
| 2331 | 0, /*tp_compare */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2332 | Tktt_Repr, /*tp_repr */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2333 | 0, /*tp_as_number */ |
| 2334 | 0, /*tp_as_sequence */ |
| 2335 | 0, /*tp_as_mapping */ |
| 2336 | 0, /*tp_hash */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2337 | }; |
| 2338 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2339 | |
| 2340 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2341 | /** Timer Handler **/ |
| 2342 | |
| 2343 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2344 | TimerHandler(ClientData clientData) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2345 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2346 | TkttObject *v = (TkttObject *)clientData; |
| 2347 | PyObject *func = v->func; |
| 2348 | PyObject *res; |
| 2349 | |
| 2350 | if (func == NULL) |
| 2351 | return; |
| 2352 | |
| 2353 | v->func = NULL; |
| 2354 | |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2355 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2356 | |
| 2357 | res = PyEval_CallObject(func, NULL); |
| 2358 | Py_DECREF(func); |
| 2359 | Py_DECREF(v); /* See Tktt_New() */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2360 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2361 | if (res == NULL) { |
| 2362 | errorInCmd = 1; |
| 2363 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2364 | } |
| 2365 | else |
| 2366 | Py_DECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2367 | |
| 2368 | LEAVE_PYTHON |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2372 | Tkapp_CreateTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2373 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2374 | int milliseconds; |
| 2375 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2376 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2377 | |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2378 | if (!PyArg_ParseTuple(args, "iO:createtimerhandler", |
| 2379 | &milliseconds, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2380 | return NULL; |
| 2381 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2382 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2383 | return NULL; |
| 2384 | } |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2385 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2386 | #ifdef WITH_THREAD |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2387 | if (!self && !tcl_lock) { |
| 2388 | /* We don't have the Tcl lock since Tcl is threaded. */ |
| 2389 | PyErr_SetString(PyExc_RuntimeError, |
| 2390 | "_tkinter.createtimerhandler not supported " |
| 2391 | "for threaded Tcl"); |
| 2392 | return NULL; |
| 2393 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2394 | #endif |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2395 | |
| 2396 | if (self) { |
| 2397 | CHECK_TCL_APPARTMENT; |
| 2398 | } |
| 2399 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2400 | v = Tktt_New(func); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2401 | if (v) { |
| 2402 | v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 2403 | (ClientData)v); |
| 2404 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2405 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2406 | return (PyObject *) v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2409 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2410 | /** Event Loop **/ |
| 2411 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2412 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2413 | Tkapp_MainLoop(PyObject *selfptr, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2414 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2415 | int threshold = 0; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2416 | TkappObject *self = (TkappObject*)selfptr; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2417 | #ifdef WITH_THREAD |
| 2418 | PyThreadState *tstate = PyThreadState_Get(); |
| 2419 | #endif |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2420 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2421 | if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2422 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2423 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2424 | #ifdef WITH_THREAD |
Martin v. Löwis | 6a759d9 | 2003-01-04 08:36:57 +0000 | [diff] [blame] | 2425 | if (!self && !tcl_lock) { |
| 2426 | /* We don't have the Tcl lock since Tcl is threaded. */ |
| 2427 | PyErr_SetString(PyExc_RuntimeError, |
| 2428 | "_tkinter.mainloop not supported " |
| 2429 | "for threaded Tcl"); |
| 2430 | return NULL; |
| 2431 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2432 | #endif |
Martin v. Löwis | 6a759d9 | 2003-01-04 08:36:57 +0000 | [diff] [blame] | 2433 | |
| 2434 | if (self) { |
| 2435 | CHECK_TCL_APPARTMENT; |
| 2436 | self->dispatching = 1; |
| 2437 | } |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2438 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2439 | quitMainLoop = 0; |
| 2440 | while (Tk_GetNumMainWindows() > threshold && |
| 2441 | !quitMainLoop && |
| 2442 | !errorInCmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2443 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2444 | int result; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2445 | |
| 2446 | #ifdef WITH_THREAD |
Martin v. Löwis | 6a759d9 | 2003-01-04 08:36:57 +0000 | [diff] [blame] | 2447 | if (self && self->threaded) { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2448 | /* Allow other Python threads to run. */ |
| 2449 | ENTER_TCL |
| 2450 | result = Tcl_DoOneEvent(0); |
| 2451 | LEAVE_TCL |
| 2452 | } |
| 2453 | else { |
| 2454 | Py_BEGIN_ALLOW_THREADS |
| 2455 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
| 2456 | tcl_tstate = tstate; |
| 2457 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 2458 | tcl_tstate = NULL; |
| 2459 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
| 2460 | if (result == 0) |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2461 | Sleep(Tkinter_busywaitinterval); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2462 | Py_END_ALLOW_THREADS |
| 2463 | } |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 2464 | #else |
| 2465 | result = Tcl_DoOneEvent(0); |
| 2466 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2467 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2468 | if (PyErr_CheckSignals() != 0) { |
Martin v. Löwis | 6a759d9 | 2003-01-04 08:36:57 +0000 | [diff] [blame] | 2469 | if (self) |
| 2470 | self->dispatching = 0; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2471 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2472 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2473 | if (result < 0) |
| 2474 | break; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2475 | } |
Martin v. Löwis | 6a759d9 | 2003-01-04 08:36:57 +0000 | [diff] [blame] | 2476 | if (self) |
| 2477 | self->dispatching = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2478 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2479 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2480 | if (errorInCmd) { |
| 2481 | errorInCmd = 0; |
| 2482 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 2483 | excInCmd = valInCmd = trbInCmd = NULL; |
| 2484 | return NULL; |
| 2485 | } |
| 2486 | Py_INCREF(Py_None); |
| 2487 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2491 | Tkapp_DoOneEvent(PyObject *self, PyObject *args) |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2492 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2493 | int flags = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2494 | int rv; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2495 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2496 | if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2497 | return NULL; |
| 2498 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2499 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2500 | rv = Tcl_DoOneEvent(flags); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2501 | LEAVE_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2502 | return Py_BuildValue("i", rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2506 | Tkapp_Quit(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2507 | { |
| 2508 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2509 | if (!PyArg_ParseTuple(args, ":quit")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2510 | return NULL; |
| 2511 | |
| 2512 | quitMainLoop = 1; |
| 2513 | Py_INCREF(Py_None); |
| 2514 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2517 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2518 | Tkapp_InterpAddr(PyObject *self, PyObject *args) |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2519 | { |
| 2520 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2521 | if (!PyArg_ParseTuple(args, ":interpaddr")) |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2522 | return NULL; |
| 2523 | |
| 2524 | return PyInt_FromLong((long)Tkapp_Interp(self)); |
| 2525 | } |
| 2526 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2527 | static PyObject * |
| 2528 | Tkapp_TkInit(PyObject *self, PyObject *args) |
| 2529 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2530 | static int has_failed; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2531 | Tcl_Interp *interp = Tkapp_Interp(self); |
Neal Norwitz | 44dbae8 | 2004-02-19 02:44:22 +0000 | [diff] [blame] | 2532 | Tk_Window main_window; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2533 | const char * _tk_exists = NULL; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2534 | int err; |
Neal Norwitz | 44dbae8 | 2004-02-19 02:44:22 +0000 | [diff] [blame] | 2535 | main_window = Tk_MainWindow(interp); |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2536 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2537 | /* In all current versions of Tk (including 8.4.13), Tk_Init |
| 2538 | deadlocks on the second call when the first call failed. |
| 2539 | To avoid the deadlock, we just refuse the second call through |
| 2540 | a static variable. */ |
| 2541 | if (has_failed) { |
| 2542 | PyErr_SetString(Tkinter_TclError, |
| 2543 | "Calling Tk_Init again after a previous call failed might deadlock"); |
| 2544 | return NULL; |
| 2545 | } |
| 2546 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2547 | /* We want to guard against calling Tk_Init() multiple times */ |
| 2548 | CHECK_TCL_APPARTMENT; |
| 2549 | ENTER_TCL |
| 2550 | err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version"); |
| 2551 | ENTER_OVERLAP |
| 2552 | if (err == TCL_ERROR) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2553 | /* This sets an exception, but we cannot return right |
| 2554 | away because we need to exit the overlap first. */ |
| 2555 | Tkinter_Error(self); |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2556 | } else { |
| 2557 | _tk_exists = Tkapp_Result(self); |
| 2558 | } |
| 2559 | LEAVE_OVERLAP_TCL |
| 2560 | if (err == TCL_ERROR) { |
| 2561 | return NULL; |
| 2562 | } |
| 2563 | if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) { |
| 2564 | if (Tk_Init(interp) == TCL_ERROR) { |
| 2565 | PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self))); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2566 | has_failed = 1; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2567 | return NULL; |
| 2568 | } |
| 2569 | } |
| 2570 | Py_INCREF(Py_None); |
| 2571 | return Py_None; |
| 2572 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2573 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2574 | static PyObject * |
| 2575 | Tkapp_WantObjects(PyObject *self, PyObject *args) |
| 2576 | { |
| 2577 | |
Martin v. Löwis | d6efae5 | 2003-06-14 21:34:32 +0000 | [diff] [blame] | 2578 | int wantobjects = -1; |
| 2579 | if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects)) |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2580 | return NULL; |
Martin v. Löwis | d6efae5 | 2003-06-14 21:34:32 +0000 | [diff] [blame] | 2581 | if (wantobjects == -1) |
| 2582 | return PyBool_FromLong(((TkappObject*)self)->wantobjects); |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 2583 | ((TkappObject*)self)->wantobjects = wantobjects; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2584 | |
| 2585 | Py_INCREF(Py_None); |
| 2586 | return Py_None; |
| 2587 | } |
| 2588 | |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2589 | static PyObject * |
| 2590 | Tkapp_WillDispatch(PyObject *self, PyObject *args) |
| 2591 | { |
| 2592 | |
| 2593 | ((TkappObject*)self)->dispatching = 1; |
| 2594 | |
| 2595 | Py_INCREF(Py_None); |
| 2596 | return Py_None; |
| 2597 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2598 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2599 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2600 | /**** Tkapp Method List ****/ |
| 2601 | |
| 2602 | static PyMethodDef Tkapp_methods[] = |
| 2603 | { |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2604 | {"willdispatch", Tkapp_WillDispatch, METH_NOARGS}, |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2605 | {"wantobjects", Tkapp_WantObjects, METH_VARARGS}, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2606 | {"call", Tkapp_Call, METH_VARARGS}, |
| 2607 | {"globalcall", Tkapp_GlobalCall, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2608 | {"eval", Tkapp_Eval, METH_VARARGS}, |
| 2609 | {"globaleval", Tkapp_GlobalEval, METH_VARARGS}, |
| 2610 | {"evalfile", Tkapp_EvalFile, METH_VARARGS}, |
| 2611 | {"record", Tkapp_Record, METH_VARARGS}, |
| 2612 | {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS}, |
| 2613 | {"setvar", Tkapp_SetVar, METH_VARARGS}, |
| 2614 | {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS}, |
| 2615 | {"getvar", Tkapp_GetVar, METH_VARARGS}, |
| 2616 | {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS}, |
| 2617 | {"unsetvar", Tkapp_UnsetVar, METH_VARARGS}, |
| 2618 | {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS}, |
| 2619 | {"getint", Tkapp_GetInt, METH_VARARGS}, |
| 2620 | {"getdouble", Tkapp_GetDouble, METH_VARARGS}, |
| 2621 | {"getboolean", Tkapp_GetBoolean, METH_VARARGS}, |
| 2622 | {"exprstring", Tkapp_ExprString, METH_VARARGS}, |
| 2623 | {"exprlong", Tkapp_ExprLong, METH_VARARGS}, |
| 2624 | {"exprdouble", Tkapp_ExprDouble, METH_VARARGS}, |
| 2625 | {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS}, |
| 2626 | {"splitlist", Tkapp_SplitList, METH_VARARGS}, |
| 2627 | {"split", Tkapp_Split, METH_VARARGS}, |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2628 | {"merge", Tkapp_Merge, METH_VARARGS}, |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2629 | {"createcommand", Tkapp_CreateCommand, METH_VARARGS}, |
| 2630 | {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2631 | #ifdef HAVE_CREATEFILEHANDLER |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2632 | {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS}, |
| 2633 | {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 2634 | #endif |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2635 | {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS}, |
| 2636 | {"mainloop", Tkapp_MainLoop, METH_VARARGS}, |
| 2637 | {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS}, |
| 2638 | {"quit", Tkapp_Quit, METH_VARARGS}, |
| 2639 | {"interpaddr", Tkapp_InterpAddr, METH_VARARGS}, |
Neal Norwitz | 44dbae8 | 2004-02-19 02:44:22 +0000 | [diff] [blame] | 2640 | {"loadtk", Tkapp_TkInit, METH_NOARGS}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2641 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2642 | }; |
| 2643 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2644 | |
| 2645 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2646 | /**** Tkapp Type Methods ****/ |
| 2647 | |
| 2648 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2649 | Tkapp_Dealloc(PyObject *self) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2650 | { |
Martin v. Löwis | ba2f875 | 2002-12-31 17:34:30 +0000 | [diff] [blame] | 2651 | /*CHECK_TCL_APPARTMENT;*/ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2652 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2653 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2654 | LEAVE_TCL |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2655 | PyObject_Del(self); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2656 | DisableEventHook(); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static PyObject * |
Martin v. Löwis | 1d519e4 | 2006-02-27 23:10:11 +0000 | [diff] [blame] | 2660 | Tkapp_GetAttr(PyObject *self, char *name) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2661 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2662 | return Py_FindMethod(Tkapp_methods, self, name); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | static PyTypeObject Tkapp_Type = |
| 2666 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2667 | PyVarObject_HEAD_INIT(NULL, 0) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2668 | "tkapp", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2669 | sizeof(TkappObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2670 | 0, /*tp_itemsize */ |
| 2671 | Tkapp_Dealloc, /*tp_dealloc */ |
| 2672 | 0, /*tp_print */ |
| 2673 | Tkapp_GetAttr, /*tp_getattr */ |
| 2674 | 0, /*tp_setattr */ |
| 2675 | 0, /*tp_compare */ |
| 2676 | 0, /*tp_repr */ |
| 2677 | 0, /*tp_as_number */ |
| 2678 | 0, /*tp_as_sequence */ |
| 2679 | 0, /*tp_as_mapping */ |
| 2680 | 0, /*tp_hash */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2681 | }; |
| 2682 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2683 | |
| 2684 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2685 | /**** Tkinter Module ****/ |
| 2686 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2687 | typedef struct { |
| 2688 | PyObject* tuple; |
| 2689 | int size; /* current size */ |
| 2690 | int maxsize; /* allocated size */ |
| 2691 | } FlattenContext; |
| 2692 | |
| 2693 | static int |
| 2694 | _bump(FlattenContext* context, int size) |
| 2695 | { |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2696 | /* expand tuple to hold (at least) size new items. |
| 2697 | return true if successful, false if an exception was raised */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2698 | |
| 2699 | int maxsize = context->maxsize * 2; |
| 2700 | |
| 2701 | if (maxsize < context->size + size) |
| 2702 | maxsize = context->size + size; |
| 2703 | |
| 2704 | context->maxsize = maxsize; |
| 2705 | |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 2706 | return _PyTuple_Resize(&context->tuple, maxsize) >= 0; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | static int |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2710 | _flatten1(FlattenContext* context, PyObject* item, int depth) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2711 | { |
| 2712 | /* add tuple or list to argument tuple (recursively) */ |
| 2713 | |
| 2714 | int i, size; |
| 2715 | |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2716 | if (depth > 1000) { |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2717 | PyErr_SetString(PyExc_ValueError, |
| 2718 | "nesting too deep in _flatten"); |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2719 | return 0; |
| 2720 | } else if (PyList_Check(item)) { |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2721 | size = PyList_GET_SIZE(item); |
| 2722 | /* preallocate (assume no nesting) */ |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2723 | if (context->size + size > context->maxsize && |
| 2724 | !_bump(context, size)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2725 | return 0; |
| 2726 | /* copy items to output tuple */ |
| 2727 | for (i = 0; i < size; i++) { |
| 2728 | PyObject *o = PyList_GET_ITEM(item, i); |
| 2729 | if (PyList_Check(o) || PyTuple_Check(o)) { |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2730 | if (!_flatten1(context, o, depth + 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2731 | return 0; |
| 2732 | } else if (o != Py_None) { |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2733 | if (context->size + 1 > context->maxsize && |
| 2734 | !_bump(context, 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2735 | return 0; |
| 2736 | Py_INCREF(o); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2737 | PyTuple_SET_ITEM(context->tuple, |
| 2738 | context->size++, o); |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | } else if (PyTuple_Check(item)) { |
| 2742 | /* same, for tuples */ |
| 2743 | size = PyTuple_GET_SIZE(item); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2744 | if (context->size + size > context->maxsize && |
| 2745 | !_bump(context, size)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2746 | return 0; |
| 2747 | for (i = 0; i < size; i++) { |
| 2748 | PyObject *o = PyTuple_GET_ITEM(item, i); |
| 2749 | if (PyList_Check(o) || PyTuple_Check(o)) { |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2750 | if (!_flatten1(context, o, depth + 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2751 | return 0; |
| 2752 | } else if (o != Py_None) { |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2753 | if (context->size + 1 > context->maxsize && |
| 2754 | !_bump(context, 1)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2755 | return 0; |
| 2756 | Py_INCREF(o); |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2757 | PyTuple_SET_ITEM(context->tuple, |
| 2758 | context->size++, o); |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | } else { |
| 2762 | PyErr_SetString(PyExc_TypeError, "argument must be sequence"); |
| 2763 | return 0; |
| 2764 | } |
| 2765 | return 1; |
| 2766 | } |
| 2767 | |
| 2768 | static PyObject * |
| 2769 | Tkinter_Flatten(PyObject* self, PyObject* args) |
| 2770 | { |
| 2771 | FlattenContext context; |
| 2772 | PyObject* item; |
| 2773 | |
| 2774 | if (!PyArg_ParseTuple(args, "O:_flatten", &item)) |
| 2775 | return NULL; |
| 2776 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 2777 | context.maxsize = PySequence_Size(item); |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2778 | if (context.maxsize <= 0) |
| 2779 | return PyTuple_New(0); |
| 2780 | |
| 2781 | context.tuple = PyTuple_New(context.maxsize); |
| 2782 | if (!context.tuple) |
| 2783 | return NULL; |
| 2784 | |
| 2785 | context.size = 0; |
| 2786 | |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2787 | if (!_flatten1(&context, item,0)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2788 | return NULL; |
| 2789 | |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 2790 | if (_PyTuple_Resize(&context.tuple, context.size)) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2791 | return NULL; |
| 2792 | |
| 2793 | return context.tuple; |
| 2794 | } |
| 2795 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2796 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2797 | Tkinter_Create(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2798 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2799 | char *screenName = NULL; |
| 2800 | char *baseName = NULL; |
| 2801 | char *className = NULL; |
| 2802 | int interactive = 0; |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 2803 | int wantobjects = 0; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2804 | int wantTk = 1; /* If false, then Tk_Init() doesn't get called */ |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 2805 | int sync = 0; /* pass -sync to wish */ |
| 2806 | char *use = NULL; /* pass -use to wish */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2807 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2808 | baseName = strrchr(Py_GetProgramName(), '/'); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2809 | if (baseName != NULL) |
| 2810 | baseName++; |
| 2811 | else |
| 2812 | baseName = Py_GetProgramName(); |
| 2813 | className = "Tk"; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2814 | |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 2815 | if (!PyArg_ParseTuple(args, "|zssiiiiz:create", |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2816 | &screenName, &baseName, &className, |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 2817 | &interactive, &wantobjects, &wantTk, |
| 2818 | &sync, &use)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2819 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2820 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2821 | return (PyObject *) Tkapp_New(screenName, baseName, className, |
Martin v. Löwis | 1fa649f | 2004-08-03 18:45:31 +0000 | [diff] [blame] | 2822 | interactive, wantobjects, wantTk, |
| 2823 | sync, use); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2826 | static PyObject * |
| 2827 | Tkinter_setbusywaitinterval(PyObject *self, PyObject *args) |
| 2828 | { |
| 2829 | int new_val; |
| 2830 | if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val)) |
| 2831 | return NULL; |
| 2832 | if (new_val < 0) { |
| 2833 | PyErr_SetString(PyExc_ValueError, |
| 2834 | "busywaitinterval must be >= 0"); |
| 2835 | return NULL; |
| 2836 | } |
| 2837 | Tkinter_busywaitinterval = new_val; |
| 2838 | Py_INCREF(Py_None); |
| 2839 | return Py_None; |
| 2840 | } |
| 2841 | |
| 2842 | static char setbusywaitinterval_doc[] = |
| 2843 | "setbusywaitinterval(n) -> None\n\ |
| 2844 | \n\ |
| 2845 | Set the busy-wait interval in milliseconds between successive\n\ |
| 2846 | calls to Tcl_DoOneEvent in a threaded Python interpreter.\n\ |
| 2847 | It should be set to a divisor of the maximum time between\n\ |
| 2848 | frames in an animation."; |
| 2849 | |
| 2850 | static PyObject * |
| 2851 | Tkinter_getbusywaitinterval(PyObject *self, PyObject *args) |
| 2852 | { |
| 2853 | return PyInt_FromLong(Tkinter_busywaitinterval); |
| 2854 | } |
| 2855 | |
| 2856 | static char getbusywaitinterval_doc[] = |
| 2857 | "getbusywaitinterval() -> int\n\ |
| 2858 | \n\ |
| 2859 | Return the current busy-wait interval between successive\n\ |
| 2860 | calls to Tcl_DoOneEvent in a threaded Python interpreter."; |
| 2861 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2862 | static PyMethodDef moduleMethods[] = |
| 2863 | { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2864 | {"_flatten", Tkinter_Flatten, METH_VARARGS}, |
| 2865 | {"create", Tkinter_Create, METH_VARARGS}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2866 | #ifdef HAVE_CREATEFILEHANDLER |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2867 | {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS}, |
| 2868 | {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 2869 | #endif |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 2870 | {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS}, |
| 2871 | {"mainloop", Tkapp_MainLoop, METH_VARARGS}, |
| 2872 | {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS}, |
| 2873 | {"quit", Tkapp_Quit, METH_VARARGS}, |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2874 | {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS, |
| 2875 | setbusywaitinterval_doc}, |
| 2876 | {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval, |
| 2877 | METH_NOARGS, getbusywaitinterval_doc}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2878 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2879 | }; |
| 2880 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2881 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2882 | |
| 2883 | static int stdin_ready = 0; |
| 2884 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2885 | #ifndef MS_WINDOWS |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2886 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2887 | MyFileProc(void *clientData, int mask) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2888 | { |
| 2889 | stdin_ready = 1; |
| 2890 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2891 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2892 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2893 | #ifdef WITH_THREAD |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2894 | static PyThreadState *event_tstate = NULL; |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2895 | #endif |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 2896 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2897 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2898 | EventHook(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2899 | { |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2900 | #ifndef MS_WINDOWS |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2901 | int tfile; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2902 | #endif |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2903 | #ifdef WITH_THREAD |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2904 | PyEval_RestoreThread(event_tstate); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2905 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2906 | stdin_ready = 0; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2907 | errorInCmd = 0; |
| 2908 | #ifndef MS_WINDOWS |
Guido van Rossum | a80649b | 2000-03-28 20:07:05 +0000 | [diff] [blame] | 2909 | tfile = fileno(stdin); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2910 | Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2911 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2912 | while (!errorInCmd && !stdin_ready) { |
| 2913 | int result; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2914 | #ifdef MS_WINDOWS |
| 2915 | if (_kbhit()) { |
| 2916 | stdin_ready = 1; |
| 2917 | break; |
| 2918 | } |
| 2919 | #endif |
| 2920 | #if defined(WITH_THREAD) || defined(MS_WINDOWS) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 2921 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2922 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
Guido van Rossum | 41f0a98 | 1998-10-12 16:26:22 +0000 | [diff] [blame] | 2923 | tcl_tstate = event_tstate; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2924 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2925 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2926 | |
| 2927 | tcl_tstate = NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2928 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2929 | if (result == 0) |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2930 | Sleep(Tkinter_busywaitinterval); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2931 | Py_END_ALLOW_THREADS |
| 2932 | #else |
| 2933 | result = Tcl_DoOneEvent(0); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2934 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2935 | |
| 2936 | if (result < 0) |
| 2937 | break; |
| 2938 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2939 | #ifndef MS_WINDOWS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2940 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2941 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2942 | if (errorInCmd) { |
| 2943 | errorInCmd = 0; |
| 2944 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 2945 | excInCmd = valInCmd = trbInCmd = NULL; |
| 2946 | PyErr_Print(); |
| 2947 | } |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2948 | #ifdef WITH_THREAD |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2949 | PyEval_SaveThread(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2950 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2951 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2952 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2953 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2954 | #endif |
| 2955 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2956 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2957 | EnableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2958 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2959 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2960 | if (PyOS_InputHook == NULL) { |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2961 | #ifdef WITH_THREAD |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2962 | event_tstate = PyThreadState_Get(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2963 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2964 | PyOS_InputHook = EventHook; |
| 2965 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2966 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2970 | DisableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2971 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2972 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2973 | if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { |
| 2974 | PyOS_InputHook = NULL; |
| 2975 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2976 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2979 | |
| 2980 | /* all errors will be checked in one fell swoop in init_tkinter() */ |
| 2981 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2982 | ins_long(PyObject *d, char *name, long val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2983 | { |
| 2984 | PyObject *v = PyInt_FromLong(val); |
| 2985 | if (v) { |
| 2986 | PyDict_SetItemString(d, name, v); |
| 2987 | Py_DECREF(v); |
| 2988 | } |
| 2989 | } |
| 2990 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2991 | ins_string(PyObject *d, char *name, char *val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2992 | { |
Martin v. Löwis | 4040fb8 | 2007-08-13 06:01:43 +0000 | [diff] [blame] | 2993 | PyObject *v = PyUnicode_FromString(val); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2994 | if (v) { |
| 2995 | PyDict_SetItemString(d, name, v); |
| 2996 | Py_DECREF(v); |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3001 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3002 | init_tkinter(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3003 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3004 | PyObject *m, *d; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3005 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3006 | Py_Type(&Tkapp_Type) = &PyType_Type; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3007 | |
| 3008 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 3009 | tcl_lock = PyThread_allocate_lock(); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3010 | #endif |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 3011 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3012 | m = Py_InitModule("_tkinter", moduleMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 3013 | if (m == NULL) |
| 3014 | return; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3015 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3016 | d = PyModule_GetDict(m); |
Neal Norwitz | b5b5a26 | 2002-06-04 17:14:07 +0000 | [diff] [blame] | 3017 | Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3018 | PyDict_SetItemString(d, "TclError", Tkinter_TclError); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3019 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 3020 | ins_long(d, "READABLE", TCL_READABLE); |
| 3021 | ins_long(d, "WRITABLE", TCL_WRITABLE); |
| 3022 | ins_long(d, "EXCEPTION", TCL_EXCEPTION); |
| 3023 | ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS); |
| 3024 | ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS); |
| 3025 | ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS); |
| 3026 | ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS); |
| 3027 | ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS); |
| 3028 | ins_long(d, "DONT_WAIT", TCL_DONT_WAIT); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3029 | ins_string(d, "TK_VERSION", TK_VERSION); |
| 3030 | ins_string(d, "TCL_VERSION", TCL_VERSION); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3031 | |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 3032 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3033 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3034 | Py_Type(&Tktt_Type) = &PyType_Type; |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 3035 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); |
| 3036 | |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3037 | Py_Type(&PyTclObject_Type) = &PyType_Type; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 3038 | PyDict_SetItemString(d, "Tcl_Obj", (PyObject *)&PyTclObject_Type); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 3039 | |
| 3040 | #ifdef TK_AQUA |
| 3041 | /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems |
| 3042 | * start waking up. Note that Tcl_FindExecutable will do this, this |
| 3043 | * code must be above it! The original warning from |
| 3044 | * tkMacOSXAppInit.c is copied below. |
| 3045 | * |
| 3046 | * NB - You have to swap in the Tk Notifier BEFORE you start up the |
| 3047 | * Tcl interpreter for now. It probably should work to do this |
| 3048 | * in the other order, but for now it doesn't seem to. |
| 3049 | * |
| 3050 | */ |
| 3051 | Tk_MacOSXSetupTkNotifier(); |
| 3052 | #endif |
| 3053 | |
| 3054 | |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 3055 | /* This helps the dynamic loader; in Unicode aware Tcl versions |
| 3056 | it also helps Tcl find its encodings. */ |
| 3057 | Tcl_FindExecutable(Py_GetProgramName()); |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 3058 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3059 | if (PyErr_Occurred()) |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 3060 | return; |
| 3061 | |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 3062 | #if 0 |
| 3063 | /* This was not a good idea; through <Destroy> bindings, |
| 3064 | Tcl_Finalize() may invoke Python code but at that point the |
| 3065 | interpreter and thread state have already been destroyed! */ |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 3066 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 3067 | #endif |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 3068 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3069 | } |