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