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