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