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