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