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