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); |
Antoine Pitrou | ed8ba14 | 2011-10-04 13:50:21 +0200 | [diff] [blame] | 652 | if (Py_ISUPPER(Py_CHARMASK(argv0[0]))) |
| 653 | argv0[0] = Py_TOLOWER(Py_CHARMASK(argv0[0])); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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. */ |
Victor Stinner | 7ab4192 | 2011-11-04 00:36:46 +0100 | [diff] [blame] | 993 | PyErr_Format(PyExc_ValueError, |
| 994 | "character U+%x is above the range " |
| 995 | "(U+0000-U+FFFF) allowed by Tcl", |
| 996 | inbuf[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | ckfree(FREECAST outbuf); |
| 998 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 999 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1001 | outbuf[i] = ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | } |
| 1003 | result = Tcl_NewUnicodeObj(outbuf, size); |
| 1004 | ckfree(FREECAST outbuf); |
| 1005 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | } |
| 1007 | else if(PyTclObject_Check(value)) { |
| 1008 | Tcl_Obj *v = ((PyTclObject*)value)->value; |
| 1009 | Tcl_IncrRefCount(v); |
| 1010 | return v; |
| 1011 | } |
| 1012 | else { |
| 1013 | PyObject *v = PyObject_Str(value); |
| 1014 | if (!v) |
| 1015 | return 0; |
| 1016 | result = AsObj(v); |
| 1017 | Py_DECREF(v); |
| 1018 | return result; |
| 1019 | } |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1022 | static PyObject* |
| 1023 | FromObj(PyObject* tkapp, Tcl_Obj *value) |
| 1024 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | PyObject *result = NULL; |
| 1026 | TkappObject *app = (TkappObject*)tkapp; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | if (value->typePtr == NULL) { |
| 1029 | return PyUnicode_FromStringAndSize(value->bytes, |
| 1030 | value->length); |
| 1031 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | if (value->typePtr == app->BooleanType) { |
| 1034 | result = value->internalRep.longValue ? Py_True : Py_False; |
| 1035 | Py_INCREF(result); |
| 1036 | return result; |
| 1037 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | if (value->typePtr == app->ByteArrayType) { |
| 1040 | int size; |
| 1041 | char *data = (char*)Tcl_GetByteArrayFromObj(value, &size); |
| 1042 | return PyBytes_FromStringAndSize(data, size); |
| 1043 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | if (value->typePtr == app->DoubleType) { |
| 1046 | return PyFloat_FromDouble(value->internalRep.doubleValue); |
| 1047 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | if (value->typePtr == app->IntType) { |
| 1050 | return PyLong_FromLong(value->internalRep.longValue); |
| 1051 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | if (value->typePtr == app->ListType) { |
| 1054 | int size; |
| 1055 | int i, status; |
| 1056 | PyObject *elem; |
| 1057 | Tcl_Obj *tcl_elem; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | status = Tcl_ListObjLength(Tkapp_Interp(tkapp), value, &size); |
| 1060 | if (status == TCL_ERROR) |
| 1061 | return Tkinter_Error(tkapp); |
| 1062 | result = PyTuple_New(size); |
| 1063 | if (!result) |
| 1064 | return NULL; |
| 1065 | for (i = 0; i < size; i++) { |
| 1066 | status = Tcl_ListObjIndex(Tkapp_Interp(tkapp), |
| 1067 | value, i, &tcl_elem); |
| 1068 | if (status == TCL_ERROR) { |
| 1069 | Py_DECREF(result); |
| 1070 | return Tkinter_Error(tkapp); |
| 1071 | } |
| 1072 | elem = FromObj(tkapp, tcl_elem); |
| 1073 | if (!elem) { |
| 1074 | Py_DECREF(result); |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | PyTuple_SetItem(result, i, elem); |
| 1078 | } |
| 1079 | return result; |
| 1080 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | if (value->typePtr == app->ProcBodyType) { |
| 1083 | /* fall through: return tcl object. */ |
| 1084 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | if (value->typePtr == app->StringType) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1087 | #if TCL_UTF_MAX==3 |
| 1088 | return PyUnicode_FromKindAndData( |
| 1089 | PyUnicode_2BYTE_KIND, Tcl_GetUnicode(value), |
| 1090 | Tcl_GetCharLength(value)); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1091 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1092 | return PyUnicode_FromKindAndData( |
| 1093 | PyUnicode_4BYTE_KIND, Tcl_GetUnicode(value), |
| 1094 | Tcl_GetCharLength(value)); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1095 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | return newPyTclObject(value); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1101 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1102 | /* This mutex synchronizes inter-thread command calls. */ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1103 | TCL_DECLARE_MUTEX(call_mutex) |
| 1104 | |
| 1105 | typedef struct Tkapp_CallEvent { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | Tcl_Event ev; /* Must be first */ |
| 1107 | TkappObject *self; |
| 1108 | PyObject *args; |
| 1109 | int flags; |
| 1110 | PyObject **res; |
| 1111 | PyObject **exc_type, **exc_value, **exc_tb; |
| 1112 | Tcl_Condition *done; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1113 | } Tkapp_CallEvent; |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1114 | #endif |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1115 | |
| 1116 | void |
| 1117 | Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1118 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | int i; |
| 1120 | for (i = 0; i < objc; i++) |
| 1121 | Tcl_DecrRefCount(objv[i]); |
| 1122 | if (objv != objStore) |
| 1123 | ckfree(FREECAST objv); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1124 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1125 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1126 | /* Convert Python objects to Tcl objects. This must happen in the |
| 1127 | interpreter thread, which may or may not be the calling thread. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1128 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1129 | static Tcl_Obj** |
| 1130 | Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc) |
| 1131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | Tcl_Obj **objv = objStore; |
| 1133 | int objc = 0, i; |
| 1134 | if (args == NULL) |
| 1135 | /* do nothing */; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | else if (!PyTuple_Check(args)) { |
| 1138 | objv[0] = AsObj(args); |
| 1139 | if (objv[0] == 0) |
| 1140 | goto finally; |
| 1141 | objc = 1; |
| 1142 | Tcl_IncrRefCount(objv[0]); |
| 1143 | } |
| 1144 | else { |
| 1145 | objc = PyTuple_Size(args); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (objc > ARGSZ) { |
| 1148 | objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *)); |
| 1149 | if (objv == NULL) { |
| 1150 | PyErr_NoMemory(); |
| 1151 | objc = 0; |
| 1152 | goto finally; |
| 1153 | } |
| 1154 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | for (i = 0; i < objc; i++) { |
| 1157 | PyObject *v = PyTuple_GetItem(args, i); |
| 1158 | if (v == Py_None) { |
| 1159 | objc = i; |
| 1160 | break; |
| 1161 | } |
| 1162 | objv[i] = AsObj(v); |
| 1163 | if (!objv[i]) { |
| 1164 | /* Reset objc, so it attempts to clear |
| 1165 | objects only up to i. */ |
| 1166 | objc = i; |
| 1167 | goto finally; |
| 1168 | } |
| 1169 | Tcl_IncrRefCount(objv[i]); |
| 1170 | } |
| 1171 | } |
| 1172 | *pobjc = objc; |
| 1173 | return objv; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1174 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
| 1176 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1177 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1178 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1179 | /* Convert the results of a command call into a Python objects. */ |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1180 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1181 | static PyObject* |
| 1182 | Tkapp_CallResult(TkappObject *self) |
| 1183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | PyObject *res = NULL; |
| 1185 | if(self->wantobjects) { |
| 1186 | Tcl_Obj *value = Tcl_GetObjResult(self->interp); |
| 1187 | /* Not sure whether the IncrRef is necessary, but something |
| 1188 | may overwrite the interpreter result while we are |
| 1189 | converting it. */ |
| 1190 | Tcl_IncrRefCount(value); |
| 1191 | res = FromObj((PyObject*)self, value); |
| 1192 | Tcl_DecrRefCount(value); |
| 1193 | } else { |
| 1194 | const char *s = Tcl_GetStringResult(self->interp); |
| 1195 | const char *p = s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | res = PyUnicode_FromStringAndSize(s, (int)(p-s)); |
| 1198 | } |
| 1199 | return res; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1200 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1201 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1202 | #ifdef WITH_THREAD |
| 1203 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1204 | /* Tkapp_CallProc is the event procedure that is executed in the context of |
| 1205 | the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't |
| 1206 | hold the Python lock. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1207 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1208 | static int |
| 1209 | Tkapp_CallProc(Tkapp_CallEvent *e, int flags) |
| 1210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | Tcl_Obj *objStore[ARGSZ]; |
| 1212 | Tcl_Obj **objv; |
| 1213 | int objc; |
| 1214 | int i; |
| 1215 | ENTER_PYTHON |
| 1216 | objv = Tkapp_CallArgs(e->args, objStore, &objc); |
| 1217 | if (!objv) { |
| 1218 | PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb); |
| 1219 | *(e->res) = NULL; |
| 1220 | } |
| 1221 | LEAVE_PYTHON |
| 1222 | if (!objv) |
| 1223 | goto done; |
| 1224 | i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags); |
| 1225 | ENTER_PYTHON |
| 1226 | if (i == TCL_ERROR) { |
| 1227 | *(e->res) = NULL; |
| 1228 | *(e->exc_type) = NULL; |
| 1229 | *(e->exc_tb) = NULL; |
| 1230 | *(e->exc_value) = PyObject_CallFunction( |
| 1231 | Tkinter_TclError, "s", |
| 1232 | Tcl_GetStringResult(e->self->interp)); |
| 1233 | } |
| 1234 | else { |
| 1235 | *(e->res) = Tkapp_CallResult(e->self); |
| 1236 | } |
| 1237 | LEAVE_PYTHON |
Guilherme Polo | 491aee2 | 2009-02-06 23:16:11 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
Guilherme Polo | 491aee2 | 2009-02-06 23:16:11 +0000 | [diff] [blame] | 1240 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | /* Wake up calling thread. */ |
| 1242 | Tcl_MutexLock(&call_mutex); |
| 1243 | Tcl_ConditionNotify(e->done); |
| 1244 | Tcl_MutexUnlock(&call_mutex); |
| 1245 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1248 | #endif |
| 1249 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1250 | /* This is the main entry point for calling a Tcl command. |
| 1251 | It supports three cases, with regard to threading: |
| 1252 | 1. Tcl is not threaded: Must have the Tcl lock, then can invoke command in |
| 1253 | the context of the calling thread. |
| 1254 | 2. Tcl is threaded, caller of the command is in the interpreter thread: |
| 1255 | Execute the command in the calling thread. Since the Tcl lock will |
| 1256 | not be used, we can merge that with case 1. |
| 1257 | 3. Tcl is threaded, caller is in a different thread: Must queue an event to |
| 1258 | the interpreter thread. Allocation of Tcl objects needs to occur in the |
| 1259 | interpreter thread, so we ship the PyObject* args to the target thread, |
| 1260 | and perform processing there. */ |
| 1261 | |
| 1262 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1263 | Tkapp_Call(PyObject *selfptr, PyObject *args) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | Tcl_Obj *objStore[ARGSZ]; |
| 1266 | Tcl_Obj **objv = NULL; |
| 1267 | int objc, i; |
| 1268 | PyObject *res = NULL; |
| 1269 | TkappObject *self = (TkappObject*)selfptr; |
| 1270 | int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | /* If args is a single tuple, replace with contents of tuple */ |
| 1273 | if (1 == PyTuple_Size(args)){ |
| 1274 | PyObject* item = PyTuple_GetItem(args, 0); |
| 1275 | if (PyTuple_Check(item)) |
| 1276 | args = item; |
| 1277 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1278 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 1280 | /* We cannot call the command directly. Instead, we must |
| 1281 | marshal the parameters to the interpreter thread. */ |
| 1282 | Tkapp_CallEvent *ev; |
| 1283 | Tcl_Condition cond = NULL; |
| 1284 | PyObject *exc_type, *exc_value, *exc_tb; |
| 1285 | if (!WaitForMainloop(self)) |
| 1286 | return NULL; |
| 1287 | ev = (Tkapp_CallEvent*)ckalloc(sizeof(Tkapp_CallEvent)); |
| 1288 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; |
| 1289 | ev->self = self; |
| 1290 | ev->args = args; |
| 1291 | ev->res = &res; |
| 1292 | ev->exc_type = &exc_type; |
| 1293 | ev->exc_value = &exc_value; |
| 1294 | ev->exc_tb = &exc_tb; |
| 1295 | ev->done = &cond; |
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 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | if (res == NULL) { |
| 1300 | if (exc_type) |
| 1301 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 1302 | else |
| 1303 | PyErr_SetObject(Tkinter_TclError, exc_value); |
| 1304 | } |
| 1305 | Tcl_ConditionFinalize(&cond); |
| 1306 | } |
| 1307 | else |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1308 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | objv = Tkapp_CallArgs(args, objStore, &objc); |
| 1312 | if (!objv) |
| 1313 | return NULL; |
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 | ENTER_TCL |
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 | i = Tcl_EvalObjv(self->interp, objc, objv, flags); |
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 | ENTER_OVERLAP |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | if (i == TCL_ERROR) |
| 1322 | Tkinter_Error(selfptr); |
| 1323 | else |
| 1324 | res = Tkapp_CallResult(self); |
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 | LEAVE_OVERLAP_TCL |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
| 1329 | } |
| 1330 | return res; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1335 | Tkapp_GlobalCall(PyObject *self, PyObject *args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | /* Could do the same here as for Tkapp_Call(), but this is not used |
| 1338 | much, so I can't be bothered. Unfortunately Tcl doesn't export a |
| 1339 | way for the user to do what all its Global* variants do (save and |
| 1340 | reset the scope pointer, call the local version, restore the saved |
| 1341 | scope pointer). */ |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | char *cmd; |
| 1344 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | cmd = Merge(args); |
| 1349 | if (cmd) { |
| 1350 | int err; |
| 1351 | ENTER_TCL |
| 1352 | err = Tcl_GlobalEval(Tkapp_Interp(self), cmd); |
| 1353 | ENTER_OVERLAP |
| 1354 | if (err == TCL_ERROR) |
| 1355 | res = Tkinter_Error(self); |
| 1356 | else |
| 1357 | res = PyUnicode_FromString(Tkapp_Result(self)); |
| 1358 | LEAVE_OVERLAP_TCL |
| 1359 | ckfree(cmd); |
| 1360 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1366 | Tkapp_Eval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | char *script; |
| 1369 | PyObject *res = NULL; |
| 1370 | int err; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | if (!PyArg_ParseTuple(args, "s:eval", &script)) |
| 1373 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | ENTER_TCL |
| 1378 | err = Tcl_Eval(Tkapp_Interp(self), script); |
| 1379 | ENTER_OVERLAP |
| 1380 | if (err == TCL_ERROR) |
| 1381 | res = Tkinter_Error(self); |
| 1382 | else |
| 1383 | res = PyUnicode_FromString(Tkapp_Result(self)); |
| 1384 | LEAVE_OVERLAP_TCL |
| 1385 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1389 | Tkapp_GlobalEval(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | char *script; |
| 1392 | PyObject *res = NULL; |
| 1393 | int err; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | if (!PyArg_ParseTuple(args, "s:globaleval", &script)) |
| 1396 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | ENTER_TCL |
| 1401 | err = Tcl_GlobalEval(Tkapp_Interp(self), script); |
| 1402 | ENTER_OVERLAP |
| 1403 | if (err == TCL_ERROR) |
| 1404 | res = Tkinter_Error(self); |
| 1405 | else |
| 1406 | res = PyUnicode_FromString(Tkapp_Result(self)); |
| 1407 | LEAVE_OVERLAP_TCL |
| 1408 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1412 | Tkapp_EvalFile(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | char *fileName; |
| 1415 | PyObject *res = NULL; |
| 1416 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | if (!PyArg_ParseTuple(args, "s:evalfile", &fileName)) |
| 1419 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | ENTER_TCL |
| 1424 | err = Tcl_EvalFile(Tkapp_Interp(self), fileName); |
| 1425 | ENTER_OVERLAP |
| 1426 | if (err == TCL_ERROR) |
| 1427 | res = Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | else |
| 1430 | res = PyUnicode_FromString(Tkapp_Result(self)); |
| 1431 | LEAVE_OVERLAP_TCL |
| 1432 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1436 | Tkapp_Record(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | char *script; |
| 1439 | PyObject *res = NULL; |
| 1440 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (!PyArg_ParseTuple(args, "s", &script)) |
| 1443 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | ENTER_TCL |
| 1448 | err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); |
| 1449 | ENTER_OVERLAP |
| 1450 | if (err == TCL_ERROR) |
| 1451 | res = Tkinter_Error(self); |
| 1452 | else |
| 1453 | res = PyUnicode_FromString(Tkapp_Result(self)); |
| 1454 | LEAVE_OVERLAP_TCL |
| 1455 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1459 | Tkapp_AddErrorInfo(PyObject *self, PyObject *args) |
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 | char *msg; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | if (!PyArg_ParseTuple(args, "s:adderrorinfo", &msg)) |
| 1464 | return NULL; |
| 1465 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | ENTER_TCL |
| 1468 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
| 1469 | LEAVE_TCL |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | Py_INCREF(Py_None); |
| 1472 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1477 | /** Tcl Variable **/ |
| 1478 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1479 | typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags); |
| 1480 | |
| 1481 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1482 | TCL_DECLARE_MUTEX(var_mutex) |
| 1483 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1484 | typedef struct VarEvent { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | Tcl_Event ev; /* must be first */ |
| 1486 | PyObject *self; |
| 1487 | PyObject *args; |
| 1488 | int flags; |
| 1489 | EventFunc func; |
| 1490 | PyObject **res; |
| 1491 | PyObject **exc_type; |
| 1492 | PyObject **exc_val; |
| 1493 | Tcl_Condition *cond; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1494 | } VarEvent; |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1495 | #endif |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1496 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1497 | static int |
| 1498 | varname_converter(PyObject *in, void *_out) |
| 1499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | char **out = (char**)_out; |
| 1501 | if (PyBytes_Check(in)) { |
| 1502 | *out = PyBytes_AsString(in); |
| 1503 | return 1; |
| 1504 | } |
| 1505 | if (PyUnicode_Check(in)) { |
| 1506 | *out = _PyUnicode_AsString(in); |
| 1507 | return 1; |
| 1508 | } |
| 1509 | if (PyTclObject_Check(in)) { |
| 1510 | *out = PyTclObject_TclString(in); |
| 1511 | return 1; |
| 1512 | } |
| 1513 | /* XXX: Should give diagnostics. */ |
| 1514 | return 0; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1515 | } |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1516 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1517 | #ifdef WITH_THREAD |
| 1518 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1519 | static void |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1520 | var_perform(VarEvent *ev) |
| 1521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | *(ev->res) = ev->func(ev->self, ev->args, ev->flags); |
| 1523 | if (!*(ev->res)) { |
| 1524 | PyObject *exc, *val, *tb; |
| 1525 | PyErr_Fetch(&exc, &val, &tb); |
| 1526 | PyErr_NormalizeException(&exc, &val, &tb); |
| 1527 | *(ev->exc_type) = exc; |
| 1528 | *(ev->exc_val) = val; |
| 1529 | Py_DECREF(tb); |
| 1530 | } |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1531 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | static int |
| 1535 | var_proc(VarEvent* ev, int flags) |
| 1536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | ENTER_PYTHON |
| 1538 | var_perform(ev); |
| 1539 | Tcl_MutexLock(&var_mutex); |
| 1540 | Tcl_ConditionNotify(ev->cond); |
| 1541 | Tcl_MutexUnlock(&var_mutex); |
| 1542 | LEAVE_PYTHON |
| 1543 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1546 | #endif |
| 1547 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1548 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1549 | var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1550 | { |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1551 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | TkappObject *self = (TkappObject*)selfptr; |
| 1553 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 1554 | TkappObject *self = (TkappObject*)selfptr; |
| 1555 | VarEvent *ev; |
| 1556 | PyObject *res, *exc_type, *exc_val; |
| 1557 | Tcl_Condition cond = NULL; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | /* The current thread is not the interpreter thread. Marshal |
| 1560 | the call to the interpreter thread, then wait for |
| 1561 | completion. */ |
| 1562 | if (!WaitForMainloop(self)) |
| 1563 | return NULL; |
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 = (VarEvent*)ckalloc(sizeof(VarEvent)); |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | ev->self = selfptr; |
| 1568 | ev->args = args; |
| 1569 | ev->flags = flags; |
| 1570 | ev->func = func; |
| 1571 | ev->res = &res; |
| 1572 | ev->exc_type = &exc_type; |
| 1573 | ev->exc_val = &exc_val; |
| 1574 | ev->cond = &cond; |
| 1575 | ev->ev.proc = (Tcl_EventProc*)var_proc; |
| 1576 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex); |
| 1577 | Tcl_ConditionFinalize(&cond); |
| 1578 | if (!res) { |
| 1579 | PyErr_SetObject(exc_type, exc_val); |
| 1580 | Py_DECREF(exc_type); |
| 1581 | Py_DECREF(exc_val); |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | return res; |
| 1585 | } |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 1586 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | /* Tcl is not threaded, or this is the interpreter thread. */ |
| 1588 | return func(selfptr, args, flags); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1591 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1592 | SetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | char *name1, *name2; |
| 1595 | PyObject *newValue; |
| 1596 | PyObject *res = NULL; |
| 1597 | Tcl_Obj *newval, *ok; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | if (PyArg_ParseTuple(args, "O&O:setvar", |
| 1600 | varname_converter, &name1, &newValue)) { |
| 1601 | /* XXX Acquire tcl lock??? */ |
| 1602 | newval = AsObj(newValue); |
| 1603 | if (newval == NULL) |
| 1604 | return NULL; |
| 1605 | ENTER_TCL |
| 1606 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL, |
| 1607 | newval, flags); |
| 1608 | ENTER_OVERLAP |
| 1609 | if (!ok) |
| 1610 | Tkinter_Error(self); |
| 1611 | else { |
| 1612 | res = Py_None; |
| 1613 | Py_INCREF(res); |
| 1614 | } |
| 1615 | LEAVE_OVERLAP_TCL |
| 1616 | } |
| 1617 | else { |
| 1618 | PyErr_Clear(); |
| 1619 | if (PyArg_ParseTuple(args, "ssO:setvar", |
| 1620 | &name1, &name2, &newValue)) { |
| 1621 | /* XXX must hold tcl lock already??? */ |
| 1622 | newval = AsObj(newValue); |
| 1623 | ENTER_TCL |
| 1624 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags); |
| 1625 | ENTER_OVERLAP |
| 1626 | if (!ok) |
| 1627 | Tkinter_Error(self); |
| 1628 | else { |
| 1629 | res = Py_None; |
| 1630 | Py_INCREF(res); |
| 1631 | } |
| 1632 | LEAVE_OVERLAP_TCL |
| 1633 | } |
| 1634 | else { |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | } |
| 1638 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1642 | Tkapp_SetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1648 | Tkapp_GlobalSetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | 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] | 1651 | } |
| 1652 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1655 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1656 | GetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | char *name1, *name2=NULL; |
| 1659 | PyObject *res = NULL; |
| 1660 | Tcl_Obj *tres; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | if (!PyArg_ParseTuple(args, "O&|s:getvar", |
| 1663 | varname_converter, &name1, &name2)) |
| 1664 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | ENTER_TCL |
| 1667 | tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags); |
| 1668 | ENTER_OVERLAP |
| 1669 | if (tres == NULL) { |
| 1670 | PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self))); |
| 1671 | } else { |
| 1672 | if (((TkappObject*)self)->wantobjects) { |
| 1673 | res = FromObj(self, tres); |
| 1674 | } |
| 1675 | else { |
| 1676 | res = PyUnicode_FromString(Tcl_GetString(tres)); |
| 1677 | } |
| 1678 | } |
| 1679 | LEAVE_OVERLAP_TCL |
| 1680 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1684 | Tkapp_GetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1690 | Tkapp_GlobalGetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | 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] | 1693 | } |
| 1694 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1697 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1698 | UnsetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | char *name1, *name2=NULL; |
| 1701 | int code; |
| 1702 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2)) |
| 1705 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | ENTER_TCL |
| 1708 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
| 1709 | ENTER_OVERLAP |
| 1710 | if (code == TCL_ERROR) |
| 1711 | res = Tkinter_Error(self); |
| 1712 | else { |
| 1713 | Py_INCREF(Py_None); |
| 1714 | res = Py_None; |
| 1715 | } |
| 1716 | LEAVE_OVERLAP_TCL |
| 1717 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1721 | Tkapp_UnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1727 | Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | 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] | 1730 | } |
| 1731 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1734 | /** Tcl to Python **/ |
| 1735 | |
| 1736 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1737 | Tkapp_GetInt(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | char *s; |
| 1740 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | if (PyTuple_Size(args) == 1) { |
| 1743 | PyObject* o = PyTuple_GetItem(args, 0); |
| 1744 | if (PyLong_Check(o)) { |
| 1745 | Py_INCREF(o); |
| 1746 | return o; |
| 1747 | } |
| 1748 | } |
| 1749 | if (!PyArg_ParseTuple(args, "s:getint", &s)) |
| 1750 | return NULL; |
| 1751 | if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 1752 | return Tkinter_Error(self); |
| 1753 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1757 | Tkapp_GetDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | char *s; |
| 1760 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | if (PyTuple_Size(args) == 1) { |
| 1763 | PyObject *o = PyTuple_GetItem(args, 0); |
| 1764 | if (PyFloat_Check(o)) { |
| 1765 | Py_INCREF(o); |
| 1766 | return o; |
| 1767 | } |
| 1768 | } |
| 1769 | if (!PyArg_ParseTuple(args, "s:getdouble", &s)) |
| 1770 | return NULL; |
| 1771 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 1772 | return Tkinter_Error(self); |
| 1773 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1777 | Tkapp_GetBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | char *s; |
| 1780 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | if (PyTuple_Size(args) == 1) { |
| 1783 | PyObject *o = PyTuple_GetItem(args, 0); |
| 1784 | if (PyLong_Check(o)) { |
| 1785 | Py_INCREF(o); |
| 1786 | return o; |
| 1787 | } |
| 1788 | } |
| 1789 | if (!PyArg_ParseTuple(args, "s:getboolean", &s)) |
| 1790 | return NULL; |
| 1791 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 1792 | return Tkinter_Error(self); |
| 1793 | return PyBool_FromLong(v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1797 | Tkapp_ExprString(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | char *s; |
| 1800 | PyObject *res = NULL; |
| 1801 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | if (!PyArg_ParseTuple(args, "s:exprstring", &s)) |
| 1804 | return NULL; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | ENTER_TCL |
| 1809 | retval = Tcl_ExprString(Tkapp_Interp(self), s); |
| 1810 | ENTER_OVERLAP |
| 1811 | if (retval == TCL_ERROR) |
| 1812 | res = Tkinter_Error(self); |
| 1813 | else |
| 1814 | res = Py_BuildValue("s", Tkapp_Result(self)); |
| 1815 | LEAVE_OVERLAP_TCL |
| 1816 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1820 | Tkapp_ExprLong(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | char *s; |
| 1823 | PyObject *res = NULL; |
| 1824 | int retval; |
| 1825 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | if (!PyArg_ParseTuple(args, "s:exprlong", &s)) |
| 1828 | return NULL; |
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 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | ENTER_TCL |
| 1833 | retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); |
| 1834 | ENTER_OVERLAP |
| 1835 | if (retval == TCL_ERROR) |
| 1836 | res = Tkinter_Error(self); |
| 1837 | else |
| 1838 | res = Py_BuildValue("l", v); |
| 1839 | LEAVE_OVERLAP_TCL |
| 1840 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1844 | Tkapp_ExprDouble(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | char *s; |
| 1847 | PyObject *res = NULL; |
| 1848 | double v; |
| 1849 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | if (!PyArg_ParseTuple(args, "s:exprdouble", &s)) |
| 1852 | return NULL; |
| 1853 | CHECK_TCL_APPARTMENT; |
| 1854 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
| 1855 | ENTER_TCL |
| 1856 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
| 1857 | ENTER_OVERLAP |
| 1858 | PyFPE_END_PROTECT(retval) |
| 1859 | if (retval == TCL_ERROR) |
| 1860 | res = Tkinter_Error(self); |
| 1861 | else |
| 1862 | res = Py_BuildValue("d", v); |
| 1863 | LEAVE_OVERLAP_TCL |
| 1864 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1868 | Tkapp_ExprBoolean(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | char *s; |
| 1871 | PyObject *res = NULL; |
| 1872 | int retval; |
| 1873 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | if (!PyArg_ParseTuple(args, "s:exprboolean", &s)) |
| 1876 | return NULL; |
| 1877 | CHECK_TCL_APPARTMENT; |
| 1878 | ENTER_TCL |
| 1879 | retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); |
| 1880 | ENTER_OVERLAP |
| 1881 | if (retval == TCL_ERROR) |
| 1882 | res = Tkinter_Error(self); |
| 1883 | else |
| 1884 | res = Py_BuildValue("i", v); |
| 1885 | LEAVE_OVERLAP_TCL |
| 1886 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1891 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1892 | Tkapp_SplitList(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | char *list; |
| 1895 | int argc; |
| 1896 | char **argv; |
| 1897 | PyObject *v; |
| 1898 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | if (PyTuple_Size(args) == 1) { |
| 1901 | v = PyTuple_GetItem(args, 0); |
| 1902 | if (PyTuple_Check(v)) { |
| 1903 | Py_INCREF(v); |
| 1904 | return v; |
| 1905 | } |
| 1906 | } |
| 1907 | if (!PyArg_ParseTuple(args, "et:splitlist", "utf-8", &list)) |
| 1908 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | if (Tcl_SplitList(Tkapp_Interp(self), list, |
| 1911 | &argc, &argv) == TCL_ERROR) { |
| 1912 | PyMem_Free(list); |
| 1913 | return Tkinter_Error(self); |
| 1914 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | if (!(v = PyTuple_New(argc))) |
| 1917 | goto finally; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | for (i = 0; i < argc; i++) { |
| 1920 | PyObject *s = PyUnicode_FromString(argv[i]); |
| 1921 | if (!s || PyTuple_SetItem(v, i, s)) { |
| 1922 | Py_DECREF(v); |
| 1923 | v = NULL; |
| 1924 | goto finally; |
| 1925 | } |
| 1926 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1927 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1928 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | ckfree(FREECAST argv); |
| 1930 | PyMem_Free(list); |
| 1931 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1935 | Tkapp_Split(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | PyObject *v; |
| 1938 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | if (PyTuple_Size(args) == 1) { |
| 1941 | PyObject* o = PyTuple_GetItem(args, 0); |
| 1942 | if (PyTuple_Check(o)) { |
| 1943 | o = SplitObj(o); |
| 1944 | return o; |
| 1945 | } |
| 1946 | } |
| 1947 | if (!PyArg_ParseTuple(args, "et:split", "utf-8", &list)) |
| 1948 | return NULL; |
| 1949 | v = Split(list); |
| 1950 | PyMem_Free(list); |
| 1951 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1955 | Tkapp_Merge(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | char *s = Merge(args); |
| 1958 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | if (s) { |
| 1961 | res = PyUnicode_FromString(s); |
| 1962 | ckfree(s); |
| 1963 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1970 | /** Tcl Command **/ |
| 1971 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1972 | /* Client data struct */ |
| 1973 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | PyObject *self; |
| 1975 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1976 | } PythonCmd_ClientData; |
| 1977 | |
| 1978 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1979 | PythonCmd_Error(Tcl_Interp *interp) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | errorInCmd = 1; |
| 1982 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1983 | LEAVE_PYTHON |
| 1984 | return TCL_ERROR; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1987 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1988 | * function or method. |
| 1989 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1990 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1991 | PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1994 | PyObject *func, *arg, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | int i, rv; |
| 1996 | Tcl_Obj *obj_res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | /* TBD: no error checking here since we know, via the |
| 2001 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 2002 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | func = data->func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | /* Create argument list (argv1, ..., argvN) */ |
| 2006 | if (!(arg = PyTuple_New(argc - 1))) |
| 2007 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | for (i = 0; i < (argc - 1); i++) { |
| 2010 | PyObject *s = PyUnicode_FromString(argv[i + 1]); |
Kurt B. Kaiser | e147806 | 2011-05-11 12:24:17 -0400 | [diff] [blame] | 2011 | if (!s) { |
| 2012 | /* Is Tk leaking 0xC080 in %A - a "modified" utf-8 null? */ |
| 2013 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError) && |
| 2014 | !strcmp(argv[i + 1], "\xC0\x80")) { |
| 2015 | PyErr_Clear(); |
| 2016 | /* Convert to "strict" utf-8 null */ |
| 2017 | s = PyUnicode_FromString("\0"); |
| 2018 | } else { |
| 2019 | Py_DECREF(arg); |
| 2020 | return PythonCmd_Error(interp); |
| 2021 | } |
| 2022 | } |
| 2023 | if (PyTuple_SetItem(arg, i, s)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | Py_DECREF(arg); |
| 2025 | return PythonCmd_Error(interp); |
| 2026 | } |
| 2027 | } |
| 2028 | res = PyEval_CallObject(func, arg); |
| 2029 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | if (res == NULL) |
| 2032 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | obj_res = AsObj(res); |
| 2035 | if (obj_res == NULL) { |
| 2036 | Py_DECREF(res); |
| 2037 | return PythonCmd_Error(interp); |
| 2038 | } |
| 2039 | else { |
| 2040 | Tcl_SetObjResult(interp, obj_res); |
| 2041 | rv = TCL_OK; |
| 2042 | } |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | Py_DECREF(res); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | LEAVE_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | return rv; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2052 | PythonCmdDelete(ClientData clientData) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | ENTER_PYTHON |
| 2057 | Py_XDECREF(data->self); |
| 2058 | Py_XDECREF(data->func); |
| 2059 | PyMem_DEL(data); |
| 2060 | LEAVE_PYTHON |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2065 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2066 | #ifdef WITH_THREAD |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2067 | TCL_DECLARE_MUTEX(command_mutex) |
| 2068 | |
| 2069 | typedef struct CommandEvent{ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | Tcl_Event ev; |
| 2071 | Tcl_Interp* interp; |
| 2072 | char *name; |
| 2073 | int create; |
| 2074 | int *status; |
| 2075 | ClientData *data; |
| 2076 | Tcl_Condition *done; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2077 | } CommandEvent; |
| 2078 | |
| 2079 | static int |
| 2080 | Tkapp_CommandProc(CommandEvent *ev, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | if (ev->create) |
| 2083 | *ev->status = Tcl_CreateCommand( |
| 2084 | ev->interp, ev->name, PythonCmd, |
| 2085 | ev->data, PythonCmdDelete) == NULL; |
| 2086 | else |
| 2087 | *ev->status = Tcl_DeleteCommand(ev->interp, ev->name); |
| 2088 | Tcl_MutexLock(&command_mutex); |
| 2089 | Tcl_ConditionNotify(ev->done); |
| 2090 | Tcl_MutexUnlock(&command_mutex); |
| 2091 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2092 | } |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2093 | #endif |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2094 | |
| 2095 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2096 | Tkapp_CreateCommand(PyObject *selfptr, PyObject *args) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2097 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | TkappObject *self = (TkappObject*)selfptr; |
| 2099 | PythonCmd_ClientData *data; |
| 2100 | char *cmdName; |
| 2101 | PyObject *func; |
| 2102 | int err; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | if (!PyArg_ParseTuple(args, "sO:createcommand", &cmdName, &func)) |
| 2105 | return NULL; |
| 2106 | if (!PyCallable_Check(func)) { |
| 2107 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
| 2108 | return NULL; |
| 2109 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2110 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2111 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && |
| 2113 | !WaitForMainloop(self)) |
| 2114 | return NULL; |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2115 | #endif |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | data = PyMem_NEW(PythonCmd_ClientData, 1); |
| 2118 | if (!data) |
| 2119 | return PyErr_NoMemory(); |
| 2120 | Py_INCREF(self); |
| 2121 | Py_INCREF(func); |
| 2122 | data->self = selfptr; |
| 2123 | data->func = func; |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2124 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2126 | Tcl_Condition cond = NULL; |
| 2127 | CommandEvent *ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); |
| 2128 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2129 | ev->interp = self->interp; |
| 2130 | ev->create = 1; |
| 2131 | ev->name = cmdName; |
| 2132 | ev->data = (ClientData)data; |
| 2133 | ev->status = &err; |
| 2134 | ev->done = &cond; |
| 2135 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex); |
| 2136 | Tcl_ConditionFinalize(&cond); |
| 2137 | } |
| 2138 | else |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2139 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | { |
| 2141 | ENTER_TCL |
| 2142 | err = Tcl_CreateCommand( |
| 2143 | Tkapp_Interp(self), cmdName, PythonCmd, |
| 2144 | (ClientData)data, PythonCmdDelete) == NULL; |
| 2145 | LEAVE_TCL |
| 2146 | } |
| 2147 | if (err) { |
| 2148 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
| 2149 | PyMem_DEL(data); |
| 2150 | return NULL; |
| 2151 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | Py_INCREF(Py_None); |
| 2154 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2159 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2160 | Tkapp_DeleteCommand(PyObject *selfptr, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | TkappObject *self = (TkappObject*)selfptr; |
| 2163 | char *cmdName; |
| 2164 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | if (!PyArg_ParseTuple(args, "s:deletecommand", &cmdName)) |
| 2167 | return NULL; |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2168 | |
| 2169 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2171 | Tcl_Condition cond = NULL; |
| 2172 | CommandEvent *ev; |
| 2173 | ev = (CommandEvent*)ckalloc(sizeof(CommandEvent)); |
| 2174 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2175 | ev->interp = self->interp; |
| 2176 | ev->create = 0; |
| 2177 | ev->name = cmdName; |
| 2178 | ev->status = &err; |
| 2179 | ev->done = &cond; |
| 2180 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, |
| 2181 | &command_mutex); |
| 2182 | Tcl_ConditionFinalize(&cond); |
| 2183 | } |
| 2184 | else |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2185 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2186 | { |
| 2187 | ENTER_TCL |
| 2188 | err = Tcl_DeleteCommand(self->interp, cmdName); |
| 2189 | LEAVE_TCL |
| 2190 | } |
| 2191 | if (err == -1) { |
| 2192 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | Py_INCREF(Py_None); |
| 2196 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2201 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2202 | /** File Handler **/ |
| 2203 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2204 | typedef struct _fhcdata { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | PyObject *func; |
| 2206 | PyObject *file; |
| 2207 | int id; |
| 2208 | struct _fhcdata *next; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2209 | } FileHandler_ClientData; |
| 2210 | |
| 2211 | static FileHandler_ClientData *HeadFHCD; |
| 2212 | |
| 2213 | static FileHandler_ClientData * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2214 | NewFHCD(PyObject *func, PyObject *file, int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2215 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | FileHandler_ClientData *p; |
| 2217 | p = PyMem_NEW(FileHandler_ClientData, 1); |
| 2218 | if (p != NULL) { |
| 2219 | Py_XINCREF(func); |
| 2220 | Py_XINCREF(file); |
| 2221 | p->func = func; |
| 2222 | p->file = file; |
| 2223 | p->id = id; |
| 2224 | p->next = HeadFHCD; |
| 2225 | HeadFHCD = p; |
| 2226 | } |
| 2227 | return p; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2231 | DeleteFHCD(int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | FileHandler_ClientData *p, **pp; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2235 | pp = &HeadFHCD; |
| 2236 | while ((p = *pp) != NULL) { |
| 2237 | if (p->id == id) { |
| 2238 | *pp = p->next; |
| 2239 | Py_XDECREF(p->func); |
| 2240 | Py_XDECREF(p->file); |
| 2241 | PyMem_DEL(p); |
| 2242 | } |
| 2243 | else |
| 2244 | pp = &p->next; |
| 2245 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 2248 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2249 | FileHandler(ClientData clientData, int mask) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | FileHandler_ClientData *data = (FileHandler_ClientData *)clientData; |
| 2252 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | ENTER_PYTHON |
| 2255 | func = data->func; |
| 2256 | file = data->file; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | arg = Py_BuildValue("(Oi)", file, (long) mask); |
| 2259 | res = PyEval_CallObject(func, arg); |
| 2260 | Py_DECREF(arg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | if (res == NULL) { |
| 2263 | errorInCmd = 1; |
| 2264 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2265 | } |
| 2266 | Py_XDECREF(res); |
| 2267 | LEAVE_PYTHON |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2270 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2271 | Tkapp_CreateFileHandler(PyObject *self, PyObject *args) |
| 2272 | /* args is (file, mask, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | FileHandler_ClientData *data; |
| 2275 | PyObject *file, *func; |
| 2276 | int mask, tfile; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | if (!PyArg_ParseTuple(args, "OiO:createfilehandler", |
| 2279 | &file, &mask, &func)) |
| 2280 | return NULL; |
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 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | tfile = PyObject_AsFileDescriptor(file); |
| 2285 | if (tfile < 0) |
| 2286 | return NULL; |
| 2287 | if (!PyCallable_Check(func)) { |
| 2288 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
| 2289 | return NULL; |
| 2290 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | data = NewFHCD(func, file, tfile); |
| 2293 | if (data == NULL) |
| 2294 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | /* Ought to check for null Tcl_File object... */ |
| 2297 | ENTER_TCL |
| 2298 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
| 2299 | LEAVE_TCL |
| 2300 | Py_INCREF(Py_None); |
| 2301 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2305 | Tkapp_DeleteFileHandler(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | PyObject *file; |
| 2308 | int tfile; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | if (!PyArg_ParseTuple(args, "O:deletefilehandler", &file)) |
| 2311 | return NULL; |
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 | CHECK_TCL_APPARTMENT; |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | tfile = PyObject_AsFileDescriptor(file); |
| 2316 | if (tfile < 0) |
| 2317 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | DeleteFHCD(tfile); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | /* Ought to check for null Tcl_File object... */ |
| 2322 | ENTER_TCL |
| 2323 | Tcl_DeleteFileHandler(tfile); |
| 2324 | LEAVE_TCL |
| 2325 | Py_INCREF(Py_None); |
| 2326 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2327 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2328 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2331 | /**** Tktt Object (timer token) ****/ |
| 2332 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2333 | static PyTypeObject Tktt_Type; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2334 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2335 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | PyObject_HEAD |
| 2337 | Tcl_TimerToken token; |
| 2338 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2339 | } TkttObject; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2340 | |
| 2341 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2342 | Tktt_DeleteTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | TkttObject *v = (TkttObject *)self; |
| 2345 | PyObject *func = v->func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | if (!PyArg_ParseTuple(args, ":deletetimerhandler")) |
| 2348 | return NULL; |
| 2349 | if (v->token != NULL) { |
| 2350 | Tcl_DeleteTimerHandler(v->token); |
| 2351 | v->token = NULL; |
| 2352 | } |
| 2353 | if (func != NULL) { |
| 2354 | v->func = NULL; |
| 2355 | Py_DECREF(func); |
| 2356 | Py_DECREF(v); /* See Tktt_New() */ |
| 2357 | } |
| 2358 | Py_INCREF(Py_None); |
| 2359 | return Py_None; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | static PyMethodDef Tktt_methods[] = |
| 2363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | {"deletetimerhandler", Tktt_DeleteTimerHandler, METH_VARARGS}, |
| 2365 | {NULL, NULL} |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2366 | }; |
| 2367 | |
| 2368 | static TkttObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2369 | Tktt_New(PyObject *func) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2370 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | TkttObject *v; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | v = PyObject_New(TkttObject, &Tktt_Type); |
| 2374 | if (v == NULL) |
| 2375 | return NULL; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | Py_INCREF(func); |
| 2378 | v->token = NULL; |
| 2379 | v->func = func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | /* Extra reference, deleted when called or when handler is deleted */ |
| 2382 | Py_INCREF(v); |
| 2383 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2387 | Tktt_Dealloc(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2389 | TkttObject *v = (TkttObject *)self; |
| 2390 | PyObject *func = v->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 | Py_XDECREF(func); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | PyObject_Del(self); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2397 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2398 | Tktt_Repr(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | TkttObject *v = (TkttObject *)self; |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 2401 | return PyUnicode_FromFormat("<tktimertoken at %p%s>", |
| 2402 | v, |
| 2403 | v->func == NULL ? ", handler deleted" : ""); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2406 | static PyTypeObject Tktt_Type = |
| 2407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2409 | "tktimertoken", /*tp_name */ |
| 2410 | sizeof(TkttObject), /*tp_basicsize */ |
| 2411 | 0, /*tp_itemsize */ |
| 2412 | Tktt_Dealloc, /*tp_dealloc */ |
| 2413 | 0, /*tp_print */ |
| 2414 | 0, /*tp_getattr */ |
| 2415 | 0, /*tp_setattr */ |
| 2416 | 0, /*tp_reserved */ |
| 2417 | Tktt_Repr, /*tp_repr */ |
| 2418 | 0, /*tp_as_number */ |
| 2419 | 0, /*tp_as_sequence */ |
| 2420 | 0, /*tp_as_mapping */ |
| 2421 | 0, /*tp_hash */ |
| 2422 | 0, /*tp_call*/ |
| 2423 | 0, /*tp_str*/ |
| 2424 | 0, /*tp_getattro*/ |
| 2425 | 0, /*tp_setattro*/ |
| 2426 | 0, /*tp_as_buffer*/ |
| 2427 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2428 | 0, /*tp_doc*/ |
| 2429 | 0, /*tp_traverse*/ |
| 2430 | 0, /*tp_clear*/ |
| 2431 | 0, /*tp_richcompare*/ |
| 2432 | 0, /*tp_weaklistoffset*/ |
| 2433 | 0, /*tp_iter*/ |
| 2434 | 0, /*tp_iternext*/ |
| 2435 | Tktt_methods, /*tp_methods*/ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2436 | }; |
| 2437 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2440 | /** Timer Handler **/ |
| 2441 | |
| 2442 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2443 | TimerHandler(ClientData clientData) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | TkttObject *v = (TkttObject *)clientData; |
| 2446 | PyObject *func = v->func; |
| 2447 | PyObject *res; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | if (func == NULL) |
| 2450 | return; |
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 | v->func = NULL; |
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 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | res = PyEval_CallObject(func, NULL); |
| 2457 | Py_DECREF(func); |
| 2458 | Py_DECREF(v); /* See Tktt_New() */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | if (res == NULL) { |
| 2461 | errorInCmd = 1; |
| 2462 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2463 | } |
| 2464 | else |
| 2465 | Py_DECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2467 | LEAVE_PYTHON |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2471 | Tkapp_CreateTimerHandler(PyObject *self, PyObject *args) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | int milliseconds; |
| 2474 | PyObject *func; |
| 2475 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | if (!PyArg_ParseTuple(args, "iO:createtimerhandler", |
| 2478 | &milliseconds, &func)) |
| 2479 | return NULL; |
| 2480 | if (!PyCallable_Check(func)) { |
| 2481 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
| 2482 | return NULL; |
| 2483 | } |
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 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | v = Tktt_New(func); |
| 2488 | if (v) { |
| 2489 | v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 2490 | (ClientData)v); |
| 2491 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2493 | return (PyObject *) v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2497 | /** Event Loop **/ |
| 2498 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2499 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2500 | Tkapp_MainLoop(PyObject *selfptr, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | int threshold = 0; |
| 2503 | TkappObject *self = (TkappObject*)selfptr; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2504 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 | PyThreadState *tstate = PyThreadState_Get(); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2506 | #endif |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | if (!PyArg_ParseTuple(args, "|i:mainloop", &threshold)) |
| 2509 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | CHECK_TCL_APPARTMENT; |
| 2512 | self->dispatching = 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | quitMainLoop = 0; |
| 2515 | while (Tk_GetNumMainWindows() > threshold && |
| 2516 | !quitMainLoop && |
| 2517 | !errorInCmd) |
| 2518 | { |
| 2519 | int result; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2520 | |
| 2521 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | if (self->threaded) { |
| 2523 | /* Allow other Python threads to run. */ |
| 2524 | ENTER_TCL |
| 2525 | result = Tcl_DoOneEvent(0); |
| 2526 | LEAVE_TCL |
| 2527 | } |
| 2528 | else { |
| 2529 | Py_BEGIN_ALLOW_THREADS |
| 2530 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
| 2531 | tcl_tstate = tstate; |
| 2532 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 2533 | tcl_tstate = NULL; |
| 2534 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
| 2535 | if (result == 0) |
| 2536 | Sleep(Tkinter_busywaitinterval); |
| 2537 | Py_END_ALLOW_THREADS |
| 2538 | } |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 2539 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | result = Tcl_DoOneEvent(0); |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 2541 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | if (PyErr_CheckSignals() != 0) { |
| 2544 | self->dispatching = 0; |
| 2545 | return NULL; |
| 2546 | } |
| 2547 | if (result < 0) |
| 2548 | break; |
| 2549 | } |
| 2550 | self->dispatching = 0; |
| 2551 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | if (errorInCmd) { |
| 2554 | errorInCmd = 0; |
| 2555 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 2556 | excInCmd = valInCmd = trbInCmd = NULL; |
| 2557 | return NULL; |
| 2558 | } |
| 2559 | Py_INCREF(Py_None); |
| 2560 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2564 | Tkapp_DoOneEvent(PyObject *self, PyObject *args) |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | int flags = 0; |
| 2567 | int rv; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | if (!PyArg_ParseTuple(args, "|i:dooneevent", &flags)) |
| 2570 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2572 | ENTER_TCL |
| 2573 | rv = Tcl_DoOneEvent(flags); |
| 2574 | LEAVE_TCL |
| 2575 | return Py_BuildValue("i", rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2579 | Tkapp_Quit(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2580 | { |
| 2581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | if (!PyArg_ParseTuple(args, ":quit")) |
| 2583 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2585 | quitMainLoop = 1; |
| 2586 | Py_INCREF(Py_None); |
| 2587 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2590 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2591 | Tkapp_InterpAddr(PyObject *self, PyObject *args) |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2592 | { |
| 2593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | if (!PyArg_ParseTuple(args, ":interpaddr")) |
| 2595 | return NULL; |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | return PyLong_FromLong((long)Tkapp_Interp(self)); |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | static PyObject * |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2601 | Tkapp_TkInit(PyObject *self, PyObject *args) |
| 2602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | Tcl_Interp *interp = Tkapp_Interp(self); |
| 2604 | const char * _tk_exists = NULL; |
| 2605 | int err; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2606 | |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2607 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | /* Up to Tk 8.4.13, Tk_Init deadlocks on the second call when the |
| 2609 | * first call failed. |
| 2610 | * To avoid the deadlock, we just refuse the second call through |
| 2611 | * a static variable. |
| 2612 | */ |
| 2613 | if (tk_load_failed) { |
| 2614 | PyErr_SetString(Tkinter_TclError, TKINTER_LOADTK_ERRMSG); |
| 2615 | return NULL; |
| 2616 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2617 | #endif |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2619 | /* We want to guard against calling Tk_Init() multiple times */ |
| 2620 | CHECK_TCL_APPARTMENT; |
| 2621 | ENTER_TCL |
| 2622 | err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version"); |
| 2623 | ENTER_OVERLAP |
| 2624 | if (err == TCL_ERROR) { |
| 2625 | /* This sets an exception, but we cannot return right |
| 2626 | away because we need to exit the overlap first. */ |
| 2627 | Tkinter_Error(self); |
| 2628 | } else { |
| 2629 | _tk_exists = Tkapp_Result(self); |
| 2630 | } |
| 2631 | LEAVE_OVERLAP_TCL |
| 2632 | if (err == TCL_ERROR) { |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) { |
| 2636 | if (Tk_Init(interp) == TCL_ERROR) { |
| 2637 | PyErr_SetString(Tkinter_TclError, Tcl_GetStringResult(Tkapp_Interp(self))); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2638 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2639 | tk_load_failed = 1; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2640 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | return NULL; |
| 2642 | } |
| 2643 | } |
| 2644 | Py_INCREF(Py_None); |
| 2645 | return Py_None; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2646 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2647 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2648 | static PyObject * |
| 2649 | Tkapp_WantObjects(PyObject *self, PyObject *args) |
| 2650 | { |
| 2651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | int wantobjects = -1; |
| 2653 | if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects)) |
| 2654 | return NULL; |
| 2655 | if (wantobjects == -1) |
| 2656 | return PyBool_FromLong(((TkappObject*)self)->wantobjects); |
| 2657 | ((TkappObject*)self)->wantobjects = wantobjects; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | Py_INCREF(Py_None); |
| 2660 | return Py_None; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2663 | static PyObject * |
| 2664 | Tkapp_WillDispatch(PyObject *self, PyObject *args) |
| 2665 | { |
| 2666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | ((TkappObject*)self)->dispatching = 1; |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | Py_INCREF(Py_None); |
| 2670 | return Py_None; |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2671 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2674 | /**** Tkapp Method List ****/ |
| 2675 | |
| 2676 | static PyMethodDef Tkapp_methods[] = |
| 2677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2678 | {"willdispatch", Tkapp_WillDispatch, METH_NOARGS}, |
| 2679 | {"wantobjects", Tkapp_WantObjects, METH_VARARGS}, |
| 2680 | {"call", Tkapp_Call, METH_VARARGS}, |
| 2681 | {"globalcall", Tkapp_GlobalCall, METH_VARARGS}, |
| 2682 | {"eval", Tkapp_Eval, METH_VARARGS}, |
| 2683 | {"globaleval", Tkapp_GlobalEval, METH_VARARGS}, |
| 2684 | {"evalfile", Tkapp_EvalFile, METH_VARARGS}, |
| 2685 | {"record", Tkapp_Record, METH_VARARGS}, |
| 2686 | {"adderrorinfo", Tkapp_AddErrorInfo, METH_VARARGS}, |
| 2687 | {"setvar", Tkapp_SetVar, METH_VARARGS}, |
| 2688 | {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS}, |
| 2689 | {"getvar", Tkapp_GetVar, METH_VARARGS}, |
| 2690 | {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS}, |
| 2691 | {"unsetvar", Tkapp_UnsetVar, METH_VARARGS}, |
| 2692 | {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS}, |
| 2693 | {"getint", Tkapp_GetInt, METH_VARARGS}, |
| 2694 | {"getdouble", Tkapp_GetDouble, METH_VARARGS}, |
| 2695 | {"getboolean", Tkapp_GetBoolean, METH_VARARGS}, |
| 2696 | {"exprstring", Tkapp_ExprString, METH_VARARGS}, |
| 2697 | {"exprlong", Tkapp_ExprLong, METH_VARARGS}, |
| 2698 | {"exprdouble", Tkapp_ExprDouble, METH_VARARGS}, |
| 2699 | {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS}, |
| 2700 | {"splitlist", Tkapp_SplitList, METH_VARARGS}, |
| 2701 | {"split", Tkapp_Split, METH_VARARGS}, |
| 2702 | {"merge", Tkapp_Merge, METH_VARARGS}, |
| 2703 | {"createcommand", Tkapp_CreateCommand, METH_VARARGS}, |
| 2704 | {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2705 | #ifdef HAVE_CREATEFILEHANDLER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | {"createfilehandler", Tkapp_CreateFileHandler, METH_VARARGS}, |
| 2707 | {"deletefilehandler", Tkapp_DeleteFileHandler, METH_VARARGS}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 2708 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | {"createtimerhandler", Tkapp_CreateTimerHandler, METH_VARARGS}, |
| 2710 | {"mainloop", Tkapp_MainLoop, METH_VARARGS}, |
| 2711 | {"dooneevent", Tkapp_DoOneEvent, METH_VARARGS}, |
| 2712 | {"quit", Tkapp_Quit, METH_VARARGS}, |
| 2713 | {"interpaddr", Tkapp_InterpAddr, METH_VARARGS}, |
| 2714 | {"loadtk", Tkapp_TkInit, METH_NOARGS}, |
| 2715 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2716 | }; |
| 2717 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2720 | /**** Tkapp Type Methods ****/ |
| 2721 | |
| 2722 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2723 | Tkapp_Dealloc(PyObject *self) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | /*CHECK_TCL_APPARTMENT;*/ |
| 2726 | ENTER_TCL |
| 2727 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
| 2728 | LEAVE_TCL |
| 2729 | PyObject_Del(self); |
| 2730 | DisableEventHook(); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2733 | static PyTypeObject Tkapp_Type = |
| 2734 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2736 | "tkapp", /*tp_name */ |
| 2737 | sizeof(TkappObject), /*tp_basicsize */ |
| 2738 | 0, /*tp_itemsize */ |
| 2739 | Tkapp_Dealloc, /*tp_dealloc */ |
| 2740 | 0, /*tp_print */ |
| 2741 | 0, /*tp_getattr */ |
| 2742 | 0, /*tp_setattr */ |
| 2743 | 0, /*tp_reserved */ |
| 2744 | 0, /*tp_repr */ |
| 2745 | 0, /*tp_as_number */ |
| 2746 | 0, /*tp_as_sequence */ |
| 2747 | 0, /*tp_as_mapping */ |
| 2748 | 0, /*tp_hash */ |
| 2749 | 0, /*tp_call*/ |
| 2750 | 0, /*tp_str*/ |
| 2751 | 0, /*tp_getattro*/ |
| 2752 | 0, /*tp_setattro*/ |
| 2753 | 0, /*tp_as_buffer*/ |
| 2754 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 2755 | 0, /*tp_doc*/ |
| 2756 | 0, /*tp_traverse*/ |
| 2757 | 0, /*tp_clear*/ |
| 2758 | 0, /*tp_richcompare*/ |
| 2759 | 0, /*tp_weaklistoffset*/ |
| 2760 | 0, /*tp_iter*/ |
| 2761 | 0, /*tp_iternext*/ |
| 2762 | Tkapp_methods, /*tp_methods*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2763 | }; |
| 2764 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2766 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2767 | /**** Tkinter Module ****/ |
| 2768 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2769 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | PyObject* tuple; |
| 2771 | int size; /* current size */ |
| 2772 | int maxsize; /* allocated size */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2773 | } FlattenContext; |
| 2774 | |
| 2775 | static int |
| 2776 | _bump(FlattenContext* context, int size) |
| 2777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | /* expand tuple to hold (at least) size new items. |
| 2779 | return true if successful, false if an exception was raised */ |
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 | int maxsize = context->maxsize * 2; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | if (maxsize < context->size + size) |
| 2784 | maxsize = context->size + size; |
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 | context->maxsize = maxsize; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | return _PyTuple_Resize(&context->tuple, maxsize) >= 0; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | static int |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 2792 | _flatten1(FlattenContext* context, PyObject* item, int depth) |
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 | /* add tuple or list to argument tuple (recursively) */ |
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 | int i, size; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | if (depth > 1000) { |
| 2799 | PyErr_SetString(PyExc_ValueError, |
| 2800 | "nesting too deep in _flatten"); |
| 2801 | return 0; |
| 2802 | } else if (PyList_Check(item)) { |
| 2803 | size = PyList_GET_SIZE(item); |
| 2804 | /* preallocate (assume no nesting) */ |
| 2805 | if (context->size + size > context->maxsize && |
| 2806 | !_bump(context, size)) |
| 2807 | return 0; |
| 2808 | /* copy items to output tuple */ |
| 2809 | for (i = 0; i < size; i++) { |
| 2810 | PyObject *o = PyList_GET_ITEM(item, i); |
| 2811 | if (PyList_Check(o) || PyTuple_Check(o)) { |
| 2812 | if (!_flatten1(context, o, depth + 1)) |
| 2813 | return 0; |
| 2814 | } else if (o != Py_None) { |
| 2815 | if (context->size + 1 > context->maxsize && |
| 2816 | !_bump(context, 1)) |
| 2817 | return 0; |
| 2818 | Py_INCREF(o); |
| 2819 | PyTuple_SET_ITEM(context->tuple, |
| 2820 | context->size++, o); |
| 2821 | } |
| 2822 | } |
| 2823 | } else if (PyTuple_Check(item)) { |
| 2824 | /* same, for tuples */ |
| 2825 | size = PyTuple_GET_SIZE(item); |
| 2826 | if (context->size + size > context->maxsize && |
| 2827 | !_bump(context, size)) |
| 2828 | return 0; |
| 2829 | for (i = 0; i < size; i++) { |
| 2830 | PyObject *o = PyTuple_GET_ITEM(item, i); |
| 2831 | if (PyList_Check(o) || PyTuple_Check(o)) { |
| 2832 | if (!_flatten1(context, o, depth + 1)) |
| 2833 | return 0; |
| 2834 | } else if (o != Py_None) { |
| 2835 | if (context->size + 1 > context->maxsize && |
| 2836 | !_bump(context, 1)) |
| 2837 | return 0; |
| 2838 | Py_INCREF(o); |
| 2839 | PyTuple_SET_ITEM(context->tuple, |
| 2840 | context->size++, o); |
| 2841 | } |
| 2842 | } |
| 2843 | } else { |
| 2844 | PyErr_SetString(PyExc_TypeError, "argument must be sequence"); |
| 2845 | return 0; |
| 2846 | } |
| 2847 | return 1; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | static PyObject * |
| 2851 | Tkinter_Flatten(PyObject* self, PyObject* args) |
| 2852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | FlattenContext context; |
| 2854 | PyObject* item; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2856 | if (!PyArg_ParseTuple(args, "O:_flatten", &item)) |
| 2857 | return NULL; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | context.maxsize = PySequence_Size(item); |
| 2860 | if (context.maxsize < 0) |
| 2861 | return NULL; |
| 2862 | if (context.maxsize == 0) |
| 2863 | return PyTuple_New(0); |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | context.tuple = PyTuple_New(context.maxsize); |
| 2866 | if (!context.tuple) |
| 2867 | return NULL; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | context.size = 0; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | if (!_flatten1(&context, item,0)) |
| 2872 | return NULL; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | if (_PyTuple_Resize(&context.tuple, context.size)) |
| 2875 | return NULL; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | return context.tuple; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2880 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2881 | Tkinter_Create(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | char *screenName = NULL; |
| 2884 | char *baseName = NULL; /* XXX this is not used anymore; |
| 2885 | try getting rid of it. */ |
| 2886 | char *className = NULL; |
| 2887 | int interactive = 0; |
| 2888 | int wantobjects = 0; |
| 2889 | int wantTk = 1; /* If false, then Tk_Init() doesn't get called */ |
| 2890 | int sync = 0; /* pass -sync to wish */ |
| 2891 | char *use = NULL; /* pass -use to wish */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | className = "Tk"; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | if (!PyArg_ParseTuple(args, "|zssiiiiz:create", |
| 2896 | &screenName, &baseName, &className, |
| 2897 | &interactive, &wantobjects, &wantTk, |
| 2898 | &sync, &use)) |
| 2899 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | return (PyObject *) Tkapp_New(screenName, className, |
| 2902 | interactive, wantobjects, wantTk, |
| 2903 | sync, use); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2906 | static PyObject * |
| 2907 | Tkinter_setbusywaitinterval(PyObject *self, PyObject *args) |
| 2908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | int new_val; |
| 2910 | if (!PyArg_ParseTuple(args, "i:setbusywaitinterval", &new_val)) |
| 2911 | return NULL; |
| 2912 | if (new_val < 0) { |
| 2913 | PyErr_SetString(PyExc_ValueError, |
| 2914 | "busywaitinterval must be >= 0"); |
| 2915 | return NULL; |
| 2916 | } |
| 2917 | Tkinter_busywaitinterval = new_val; |
| 2918 | Py_INCREF(Py_None); |
| 2919 | return Py_None; |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | static char setbusywaitinterval_doc[] = |
| 2923 | "setbusywaitinterval(n) -> None\n\ |
| 2924 | \n\ |
| 2925 | Set the busy-wait interval in milliseconds between successive\n\ |
| 2926 | calls to Tcl_DoOneEvent in a threaded Python interpreter.\n\ |
| 2927 | It should be set to a divisor of the maximum time between\n\ |
| 2928 | frames in an animation."; |
| 2929 | |
| 2930 | static PyObject * |
| 2931 | Tkinter_getbusywaitinterval(PyObject *self, PyObject *args) |
| 2932 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | return PyLong_FromLong(Tkinter_busywaitinterval); |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | static char getbusywaitinterval_doc[] = |
| 2937 | "getbusywaitinterval() -> int\n\ |
| 2938 | \n\ |
| 2939 | Return the current busy-wait interval between successive\n\ |
| 2940 | calls to Tcl_DoOneEvent in a threaded Python interpreter."; |
| 2941 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2942 | static PyMethodDef moduleMethods[] = |
| 2943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | {"_flatten", Tkinter_Flatten, METH_VARARGS}, |
| 2945 | {"create", Tkinter_Create, METH_VARARGS}, |
| 2946 | {"setbusywaitinterval",Tkinter_setbusywaitinterval, METH_VARARGS, |
| 2947 | setbusywaitinterval_doc}, |
| 2948 | {"getbusywaitinterval",(PyCFunction)Tkinter_getbusywaitinterval, |
| 2949 | METH_NOARGS, getbusywaitinterval_doc}, |
| 2950 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2951 | }; |
| 2952 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2953 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2954 | |
| 2955 | static int stdin_ready = 0; |
| 2956 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2957 | #ifndef MS_WINDOWS |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2958 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2959 | MyFileProc(void *clientData, int mask) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | stdin_ready = 1; |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2962 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2963 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 2964 | |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2965 | #ifdef WITH_THREAD |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2966 | static PyThreadState *event_tstate = NULL; |
Martin v. Löwis | a965649 | 2003-03-30 08:44:58 +0000 | [diff] [blame] | 2967 | #endif |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 2968 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2969 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2970 | EventHook(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2971 | { |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2972 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | int tfile; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2974 | #endif |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2975 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | PyEval_RestoreThread(event_tstate); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 2977 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | stdin_ready = 0; |
| 2979 | errorInCmd = 0; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2980 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | tfile = fileno(stdin); |
| 2982 | Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2983 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | while (!errorInCmd && !stdin_ready) { |
| 2985 | int result; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2986 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | if (_kbhit()) { |
| 2988 | stdin_ready = 1; |
| 2989 | break; |
| 2990 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 2991 | #endif |
| 2992 | #if defined(WITH_THREAD) || defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | Py_BEGIN_ALLOW_THREADS |
| 2994 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
| 2995 | tcl_tstate = event_tstate; |
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 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 2998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | tcl_tstate = NULL; |
| 3000 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
| 3001 | if (result == 0) |
| 3002 | Sleep(Tkinter_busywaitinterval); |
| 3003 | Py_END_ALLOW_THREADS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3004 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | result = Tcl_DoOneEvent(0); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3006 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | if (result < 0) |
| 3009 | break; |
| 3010 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3011 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3012 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3013 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3014 | if (errorInCmd) { |
| 3015 | errorInCmd = 0; |
| 3016 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 3017 | excInCmd = valInCmd = trbInCmd = NULL; |
| 3018 | PyErr_Print(); |
| 3019 | } |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 3020 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | PyEval_SaveThread(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 3022 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3024 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3025 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3026 | #endif |
| 3027 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3028 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3029 | EnableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3030 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3031 | #ifdef WAIT_FOR_STDIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | if (PyOS_InputHook == NULL) { |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 3033 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | event_tstate = PyThreadState_Get(); |
Guido van Rossum | e9bc62d | 1998-11-17 03:45:24 +0000 | [diff] [blame] | 3035 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | PyOS_InputHook = EventHook; |
| 3037 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3038 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3042 | DisableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3043 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3044 | #ifdef WAIT_FOR_STDIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { |
| 3046 | PyOS_InputHook = NULL; |
| 3047 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3048 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3051 | |
| 3052 | /* all errors will be checked in one fell swoop in init_tkinter() */ |
| 3053 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 3054 | ins_long(PyObject *d, char *name, long val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | PyObject *v = PyLong_FromLong(val); |
| 3057 | if (v) { |
| 3058 | PyDict_SetItemString(d, name, v); |
| 3059 | Py_DECREF(v); |
| 3060 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3061 | } |
| 3062 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 3063 | ins_string(PyObject *d, char *name, char *val) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3064 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | PyObject *v = PyUnicode_FromString(val); |
| 3066 | if (v) { |
| 3067 | PyDict_SetItemString(d, name, v); |
| 3068 | Py_DECREF(v); |
| 3069 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3073 | static struct PyModuleDef _tkintermodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | PyModuleDef_HEAD_INIT, |
| 3075 | "_tkinter", |
| 3076 | NULL, |
| 3077 | -1, |
| 3078 | moduleMethods, |
| 3079 | NULL, |
| 3080 | NULL, |
| 3081 | NULL, |
| 3082 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3083 | }; |
| 3084 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3085 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3086 | PyInit__tkinter(void) |
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 | PyObject *m, *d, *uexe, *cexe; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | if (PyType_Ready(&Tkapp_Type) < 0) |
| 3091 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3092 | |
| 3093 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | tcl_lock = PyThread_allocate_lock(); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3095 | #endif |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 3096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | m = PyModule_Create(&_tkintermodule); |
| 3098 | if (m == NULL) |
| 3099 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | d = PyModule_GetDict(m); |
| 3102 | Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL); |
| 3103 | PyDict_SetItemString(d, "TclError", Tkinter_TclError); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | ins_long(d, "READABLE", TCL_READABLE); |
| 3106 | ins_long(d, "WRITABLE", TCL_WRITABLE); |
| 3107 | ins_long(d, "EXCEPTION", TCL_EXCEPTION); |
| 3108 | ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS); |
| 3109 | ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS); |
| 3110 | ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS); |
| 3111 | ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS); |
| 3112 | ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS); |
| 3113 | ins_long(d, "DONT_WAIT", TCL_DONT_WAIT); |
| 3114 | ins_string(d, "TK_VERSION", TK_VERSION); |
| 3115 | ins_string(d, "TCL_VERSION", TCL_VERSION); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3119 | if (PyType_Ready(&Tktt_Type) < 0) |
| 3120 | return NULL; |
| 3121 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 3122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3123 | Py_TYPE(&PyTclObject_Type) = &PyType_Type; |
| 3124 | PyDict_SetItemString(d, "Tcl_Obj", (PyObject *)&PyTclObject_Type); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 3125 | |
| 3126 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems |
| 3128 | * start waking up. Note that Tcl_FindExecutable will do this, this |
| 3129 | * code must be above it! The original warning from |
| 3130 | * tkMacOSXAppInit.c is copied below. |
| 3131 | * |
| 3132 | * NB - You have to swap in the Tk Notifier BEFORE you start up the |
| 3133 | * Tcl interpreter for now. It probably should work to do this |
| 3134 | * in the other order, but for now it doesn't seem to. |
| 3135 | * |
| 3136 | */ |
| 3137 | Tk_MacOSXSetupTkNotifier(); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 3138 | #endif |
| 3139 | |
| 3140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | /* This helps the dynamic loader; in Unicode aware Tcl versions |
| 3142 | it also helps Tcl find its encodings. */ |
| 3143 | uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1); |
| 3144 | if (uexe) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3145 | cexe = PyUnicode_EncodeFSDefault(uexe); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | if (cexe) |
| 3147 | Tcl_FindExecutable(PyBytes_AsString(cexe)); |
| 3148 | Py_XDECREF(cexe); |
| 3149 | Py_DECREF(uexe); |
| 3150 | } |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 3151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | if (PyErr_Occurred()) { |
| 3153 | Py_DECREF(m); |
| 3154 | return NULL; |
| 3155 | } |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 3156 | |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 3157 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | /* This was not a good idea; through <Destroy> bindings, |
| 3159 | Tcl_Finalize() may invoke Python code but at that point the |
| 3160 | interpreter and thread state have already been destroyed! */ |
| 3161 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 3162 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | return m; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3164 | } |