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