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