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