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