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