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, \ |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 259 | "Calling Tcl from different apartment"); \ |
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 | static PyObject * |
| 868 | PyTclObject_richcompare(PyObject *self, PyObject *other, int op) |
Martin v. Löwis | dd6cd65 | 2003-05-03 09:45:12 +0000 | [diff] [blame] | 869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | int result; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | /* neither argument should be NULL, unless something's gone wrong */ |
| 873 | if (self == NULL || other == NULL) { |
| 874 | PyErr_BadInternalCall(); |
| 875 | return NULL; |
| 876 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | /* both arguments should be instances of PyTclObject */ |
| 879 | if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 880 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | if (self == other) |
| 884 | /* fast path when self and other are identical */ |
| 885 | result = 0; |
| 886 | else |
| 887 | result = strcmp(Tcl_GetString(((PyTclObject *)self)->value), |
| 888 | Tcl_GetString(((PyTclObject *)other)->value)); |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 889 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Martin v. Löwis | dd6cd65 | 2003-05-03 09:45:12 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 892 | PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type"); |
| 893 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 894 | static PyObject* |
| 895 | get_typename(PyTclObject* obj, void* ignored) |
| 896 | { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 897 | return unicodeFromTclString(obj->value->typePtr->name); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Martin v. Löwis | 3919571 | 2003-01-04 00:33:13 +0000 | [diff] [blame] | 900 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 901 | static PyGetSetDef PyTclObject_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | {"typename", (getter)get_typename, NULL, get_typename__doc__}, |
| 903 | {"string", (getter)PyTclObject_string, NULL, |
| 904 | PyTclObject_string__doc__}, |
| 905 | {0}, |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 906 | }; |
| 907 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 908 | static PyType_Slot PyTclObject_Type_slots[] = { |
| 909 | {Py_tp_dealloc, (destructor)PyTclObject_dealloc}, |
| 910 | {Py_tp_repr, (reprfunc)PyTclObject_repr}, |
| 911 | {Py_tp_str, (reprfunc)PyTclObject_str}, |
Andrew Svetlov | d2217a8 | 2012-10-30 22:49:16 +0200 | [diff] [blame] | 912 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 913 | {Py_tp_richcompare, PyTclObject_richcompare}, |
| 914 | {Py_tp_getset, PyTclObject_getsetlist}, |
| 915 | {0, 0} |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 916 | }; |
| 917 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 918 | static PyType_Spec PyTclObject_Type_spec = { |
| 919 | "_tkinter.Tcl_Obj", |
| 920 | sizeof(PyTclObject), |
| 921 | 0, |
| 922 | Py_TPFLAGS_DEFAULT, |
| 923 | PyTclObject_Type_slots, |
| 924 | }; |
| 925 | |
| 926 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 927 | #if SIZE_MAX > INT_MAX |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 928 | #define CHECK_STRING_LENGTH(s) do { \ |
| 929 | if (s != NULL && strlen(s) >= INT_MAX) { \ |
| 930 | PyErr_SetString(PyExc_OverflowError, "string is too long"); \ |
| 931 | return NULL; \ |
| 932 | } } while(0) |
| 933 | #else |
| 934 | #define CHECK_STRING_LENGTH(s) |
| 935 | #endif |
| 936 | |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 937 | #ifdef HAVE_LIBTOMMAMTH |
| 938 | static Tcl_Obj* |
| 939 | asBignumObj(PyObject *value) |
| 940 | { |
| 941 | Tcl_Obj *result; |
| 942 | int neg; |
| 943 | PyObject *hexstr; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 944 | const char *hexchars; |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 945 | mp_int bigValue; |
| 946 | |
| 947 | neg = Py_SIZE(value) < 0; |
| 948 | hexstr = _PyLong_Format(value, 16); |
| 949 | if (hexstr == NULL) |
| 950 | return NULL; |
| 951 | hexchars = PyUnicode_AsUTF8(hexstr); |
| 952 | if (hexchars == NULL) { |
| 953 | Py_DECREF(hexstr); |
| 954 | return NULL; |
| 955 | } |
| 956 | hexchars += neg + 2; /* skip sign and "0x" */ |
| 957 | mp_init(&bigValue); |
| 958 | if (mp_read_radix(&bigValue, hexchars, 16) != MP_OKAY) { |
| 959 | mp_clear(&bigValue); |
| 960 | Py_DECREF(hexstr); |
| 961 | PyErr_NoMemory(); |
| 962 | return NULL; |
| 963 | } |
| 964 | Py_DECREF(hexstr); |
| 965 | bigValue.sign = neg ? MP_NEG : MP_ZPOS; |
| 966 | result = Tcl_NewBignumObj(&bigValue); |
| 967 | mp_clear(&bigValue); |
| 968 | if (result == NULL) { |
| 969 | PyErr_NoMemory(); |
| 970 | return NULL; |
| 971 | } |
| 972 | return result; |
| 973 | } |
| 974 | #endif |
| 975 | |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 976 | static Tcl_Obj* |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 977 | AsObj(PyObject *value) |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 978 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | Tcl_Obj *result; |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 980 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 981 | if (PyBytes_Check(value)) { |
| 982 | if (PyBytes_GET_SIZE(value) >= INT_MAX) { |
| 983 | PyErr_SetString(PyExc_OverflowError, "bytes object is too long"); |
| 984 | return NULL; |
| 985 | } |
Serhiy Storchaka | 74596a8 | 2014-07-30 18:33:13 +0300 | [diff] [blame] | 986 | return Tcl_NewByteArrayObj((unsigned char *)PyBytes_AS_STRING(value), |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 987 | (int)PyBytes_GET_SIZE(value)); |
| 988 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 989 | |
| 990 | if (PyBool_Check(value)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | return Tcl_NewBooleanObj(PyObject_IsTrue(value)); |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 992 | |
| 993 | if (PyLong_CheckExact(value)) { |
| 994 | int overflow; |
| 995 | long longValue; |
| 996 | #ifdef TCL_WIDE_INT_TYPE |
| 997 | Tcl_WideInt wideValue; |
| 998 | #endif |
| 999 | longValue = PyLong_AsLongAndOverflow(value, &overflow); |
| 1000 | if (!overflow) { |
| 1001 | return Tcl_NewLongObj(longValue); |
| 1002 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | /* If there is an overflow in the long conversion, |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1004 | fall through to wideInt handling. */ |
| 1005 | #ifdef TCL_WIDE_INT_TYPE |
| 1006 | if (_PyLong_AsByteArray((PyLongObject *)value, |
| 1007 | (unsigned char *)(void *)&wideValue, |
| 1008 | sizeof(wideValue), |
| 1009 | PY_LITTLE_ENDIAN, |
| 1010 | /* signed */ 1) == 0) { |
| 1011 | return Tcl_NewWideIntObj(wideValue); |
| 1012 | } |
| 1013 | PyErr_Clear(); |
| 1014 | #endif |
| 1015 | /* If there is an overflow in the wideInt conversion, |
| 1016 | fall through to bignum handling. */ |
| 1017 | #ifdef HAVE_LIBTOMMAMTH |
| 1018 | return asBignumObj(value); |
| 1019 | #endif |
| 1020 | /* If there is no wideInt or bignum support, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | fall through to default object handling. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1023 | |
| 1024 | if (PyFloat_Check(value)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1026 | |
Serhiy Storchaka | 4c7dc48 | 2015-04-02 18:49:14 +0300 | [diff] [blame] | 1027 | if (PyTuple_Check(value) || PyList_Check(value)) { |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1028 | Tcl_Obj **argv; |
| 1029 | Py_ssize_t size, i; |
| 1030 | |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1031 | size = PySequence_Fast_GET_SIZE(value); |
Serhiy Storchaka | abf68ce | 2014-09-11 10:57:13 +0300 | [diff] [blame] | 1032 | if (size == 0) |
| 1033 | return Tcl_NewListObj(0, NULL); |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1034 | if (!CHECK_SIZE(size, sizeof(Tcl_Obj *))) { |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1035 | PyErr_SetString(PyExc_OverflowError, |
| 1036 | PyTuple_Check(value) ? "tuple is too long" : |
| 1037 | "list is too long"); |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1040 | argv = (Tcl_Obj **) PyMem_Malloc(((size_t)size) * sizeof(Tcl_Obj *)); |
| 1041 | if (!argv) { |
Victor Stinner | 60a64d6 | 2014-09-04 17:29:52 +0200 | [diff] [blame] | 1042 | PyErr_NoMemory(); |
| 1043 | return NULL; |
| 1044 | } |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1045 | for (i = 0; i < size; i++) |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1046 | argv[i] = AsObj(PySequence_Fast_GET_ITEM(value,i)); |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1047 | result = Tcl_NewListObj((int)size, argv); |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1048 | PyMem_Free(argv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | return result; |
| 1050 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1051 | |
| 1052 | if (PyUnicode_Check(value)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1053 | void *inbuf; |
| 1054 | Py_ssize_t size; |
| 1055 | int kind; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | Tcl_UniChar *outbuf = NULL; |
| 1057 | Py_ssize_t i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1058 | size_t allocsize; |
| 1059 | |
| 1060 | if (PyUnicode_READY(value) == -1) |
| 1061 | return NULL; |
| 1062 | |
| 1063 | inbuf = PyUnicode_DATA(value); |
| 1064 | size = PyUnicode_GET_LENGTH(value); |
Serhiy Storchaka | abf68ce | 2014-09-11 10:57:13 +0300 | [diff] [blame] | 1065 | if (size == 0) |
| 1066 | return Tcl_NewUnicodeObj((const void *)"", 0); |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1067 | if (!CHECK_SIZE(size, sizeof(Tcl_UniChar))) { |
| 1068 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 1069 | return NULL; |
| 1070 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1071 | kind = PyUnicode_KIND(value); |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1072 | if (kind == sizeof(Tcl_UniChar)) |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1073 | return Tcl_NewUnicodeObj(inbuf, (int)size); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1074 | allocsize = ((size_t)size) * sizeof(Tcl_UniChar); |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1075 | outbuf = (Tcl_UniChar*)PyMem_Malloc(allocsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | /* Else overflow occurred, and we take the next exit */ |
| 1077 | if (!outbuf) { |
| 1078 | PyErr_NoMemory(); |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | for (i = 0; i < size; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1082 | Py_UCS4 ch = PyUnicode_READ(kind, inbuf, i); |
| 1083 | /* We cannot test for sizeof(Tcl_UniChar) directly, |
| 1084 | so we test for UTF-8 size instead. */ |
| 1085 | #if TCL_UTF_MAX == 3 |
| 1086 | if (ch >= 0x10000) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | /* Tcl doesn't do UTF-16, yet. */ |
Serhiy Storchaka | 59f5dee | 2013-02-18 13:01:52 +0200 | [diff] [blame] | 1088 | PyErr_Format(Tkinter_TclError, |
Victor Stinner | 7ab4192 | 2011-11-04 00:36:46 +0100 | [diff] [blame] | 1089 | "character U+%x is above the range " |
| 1090 | "(U+0000-U+FFFF) allowed by Tcl", |
Victor Stinner | 3d7acb0 | 2011-11-04 09:49:24 +0100 | [diff] [blame] | 1091 | ch); |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1092 | PyMem_Free(outbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | return NULL; |
| 1094 | } |
Andrew Svetlov | 80823d7 | 2012-07-22 13:56:54 +0300 | [diff] [blame] | 1095 | #endif |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | outbuf[i] = ch; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | } |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1098 | result = Tcl_NewUnicodeObj(outbuf, (int)size); |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1099 | PyMem_Free(outbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1102 | |
| 1103 | if (PyTclObject_Check(value)) { |
Miss Islington (bot) | f7cefb4 | 2018-10-23 14:46:32 -0700 | [diff] [blame] | 1104 | return ((PyTclObject*)value)->value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1106 | |
| 1107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | PyObject *v = PyObject_Str(value); |
| 1109 | if (!v) |
| 1110 | return 0; |
| 1111 | result = AsObj(v); |
| 1112 | Py_DECREF(v); |
| 1113 | return result; |
| 1114 | } |
Guido van Rossum | a1f0a8f | 2000-03-31 00:51:37 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Serhiy Storchaka | f7de3dd | 2015-04-02 10:35:57 +0300 | [diff] [blame] | 1117 | static PyObject * |
| 1118 | fromBoolean(PyObject* tkapp, Tcl_Obj *value) |
| 1119 | { |
| 1120 | int boolValue; |
| 1121 | if (Tcl_GetBooleanFromObj(Tkapp_Interp(tkapp), value, &boolValue) == TCL_ERROR) |
| 1122 | return Tkinter_Error(tkapp); |
| 1123 | return PyBool_FromLong(boolValue); |
| 1124 | } |
| 1125 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1126 | static PyObject* |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1127 | fromWideIntObj(PyObject* tkapp, Tcl_Obj *value) |
| 1128 | { |
| 1129 | Tcl_WideInt wideValue; |
| 1130 | if (Tcl_GetWideIntFromObj(Tkapp_Interp(tkapp), value, &wideValue) == TCL_OK) { |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1131 | if (sizeof(wideValue) <= SIZEOF_LONG_LONG) |
| 1132 | return PyLong_FromLongLong(wideValue); |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1133 | return _PyLong_FromByteArray((unsigned char *)(void *)&wideValue, |
| 1134 | sizeof(wideValue), |
| 1135 | PY_LITTLE_ENDIAN, |
| 1136 | /* signed */ 1); |
| 1137 | } |
| 1138 | return NULL; |
| 1139 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1140 | |
| 1141 | #ifdef HAVE_LIBTOMMAMTH |
| 1142 | static PyObject* |
| 1143 | fromBignumObj(PyObject* tkapp, Tcl_Obj *value) |
| 1144 | { |
| 1145 | mp_int bigValue; |
| 1146 | unsigned long numBytes; |
| 1147 | unsigned char *bytes; |
| 1148 | PyObject *res; |
| 1149 | |
| 1150 | if (Tcl_GetBignumFromObj(Tkapp_Interp(tkapp), value, &bigValue) != TCL_OK) |
| 1151 | return Tkinter_Error(tkapp); |
| 1152 | numBytes = mp_unsigned_bin_size(&bigValue); |
| 1153 | bytes = PyMem_Malloc(numBytes); |
| 1154 | if (bytes == NULL) { |
| 1155 | mp_clear(&bigValue); |
| 1156 | return PyErr_NoMemory(); |
| 1157 | } |
| 1158 | if (mp_to_unsigned_bin_n(&bigValue, bytes, |
| 1159 | &numBytes) != MP_OKAY) { |
| 1160 | mp_clear(&bigValue); |
| 1161 | PyMem_Free(bytes); |
| 1162 | return PyErr_NoMemory(); |
| 1163 | } |
| 1164 | res = _PyLong_FromByteArray(bytes, numBytes, |
| 1165 | /* big-endian */ 0, |
| 1166 | /* unsigned */ 0); |
| 1167 | PyMem_Free(bytes); |
| 1168 | if (res != NULL && bigValue.sign == MP_NEG) { |
| 1169 | PyObject *res2 = PyNumber_Negative(res); |
| 1170 | Py_DECREF(res); |
| 1171 | res = res2; |
| 1172 | } |
| 1173 | mp_clear(&bigValue); |
| 1174 | return res; |
| 1175 | } |
| 1176 | #endif |
| 1177 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1178 | static PyObject* |
| 1179 | FromObj(PyObject* tkapp, Tcl_Obj *value) |
| 1180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | PyObject *result = NULL; |
| 1182 | TkappObject *app = (TkappObject*)tkapp; |
Serhiy Storchaka | f07a4b6 | 2015-03-23 00:47:45 +0200 | [diff] [blame] | 1183 | Tcl_Interp *interp = Tkapp_Interp(tkapp); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | if (value->typePtr == NULL) { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1186 | return unicodeFromTclStringAndSize(value->bytes, value->length); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1188 | |
Serhiy Storchaka | f07a4b6 | 2015-03-23 00:47:45 +0200 | [diff] [blame] | 1189 | if (value->typePtr == app->BooleanType || |
| 1190 | value->typePtr == app->OldBooleanType) { |
Serhiy Storchaka | f7de3dd | 2015-04-02 10:35:57 +0300 | [diff] [blame] | 1191 | return fromBoolean(tkapp, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | if (value->typePtr == app->ByteArrayType) { |
| 1195 | int size; |
| 1196 | char *data = (char*)Tcl_GetByteArrayFromObj(value, &size); |
| 1197 | return PyBytes_FromStringAndSize(data, size); |
| 1198 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | if (value->typePtr == app->DoubleType) { |
| 1201 | return PyFloat_FromDouble(value->internalRep.doubleValue); |
| 1202 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (value->typePtr == app->IntType) { |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1205 | long longValue; |
| 1206 | if (Tcl_GetLongFromObj(interp, value, &longValue) == TCL_OK) |
| 1207 | return PyLong_FromLong(longValue); |
| 1208 | /* If there is an error in the long conversion, |
| 1209 | fall through to wideInt handling. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1211 | |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1212 | if (value->typePtr == app->IntType || |
| 1213 | value->typePtr == app->WideIntType) { |
| 1214 | result = fromWideIntObj(tkapp, value); |
| 1215 | if (result != NULL || PyErr_Occurred()) |
| 1216 | return result; |
| 1217 | Tcl_ResetResult(interp); |
| 1218 | /* If there is an error in the wideInt conversion, |
| 1219 | fall through to bignum handling. */ |
| 1220 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1221 | |
| 1222 | #ifdef HAVE_LIBTOMMAMTH |
| 1223 | if (value->typePtr == app->IntType || |
| 1224 | value->typePtr == app->WideIntType || |
| 1225 | value->typePtr == app->BignumType) { |
| 1226 | return fromBignumObj(tkapp, value); |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | if (value->typePtr == app->ListType) { |
| 1231 | int size; |
| 1232 | int i, status; |
| 1233 | PyObject *elem; |
| 1234 | Tcl_Obj *tcl_elem; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1235 | |
Serhiy Storchaka | f07a4b6 | 2015-03-23 00:47:45 +0200 | [diff] [blame] | 1236 | status = Tcl_ListObjLength(interp, value, &size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | if (status == TCL_ERROR) |
| 1238 | return Tkinter_Error(tkapp); |
| 1239 | result = PyTuple_New(size); |
| 1240 | if (!result) |
| 1241 | return NULL; |
| 1242 | for (i = 0; i < size; i++) { |
Serhiy Storchaka | f07a4b6 | 2015-03-23 00:47:45 +0200 | [diff] [blame] | 1243 | status = Tcl_ListObjIndex(interp, value, i, &tcl_elem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | if (status == TCL_ERROR) { |
| 1245 | Py_DECREF(result); |
| 1246 | return Tkinter_Error(tkapp); |
| 1247 | } |
| 1248 | elem = FromObj(tkapp, tcl_elem); |
| 1249 | if (!elem) { |
| 1250 | Py_DECREF(result); |
| 1251 | return NULL; |
| 1252 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 1253 | PyTuple_SET_ITEM(result, i, elem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | } |
| 1255 | return result; |
| 1256 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | if (value->typePtr == app->ProcBodyType) { |
| 1259 | /* fall through: return tcl object. */ |
| 1260 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | if (value->typePtr == app->StringType) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1263 | return PyUnicode_FromKindAndData( |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1264 | sizeof(Tcl_UniChar), Tcl_GetUnicode(value), |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1265 | Tcl_GetCharLength(value)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1267 | |
Serhiy Storchaka | 3af7a38 | 2015-04-22 10:53:08 +0300 | [diff] [blame] | 1268 | #if TK_HEX_VERSION >= 0x08050000 |
Serhiy Storchaka | f7de3dd | 2015-04-02 10:35:57 +0300 | [diff] [blame] | 1269 | if (app->BooleanType == NULL && |
| 1270 | strcmp(value->typePtr->name, "booleanString") == 0) { |
| 1271 | /* booleanString type is not registered in Tcl */ |
| 1272 | app->BooleanType = value->typePtr; |
| 1273 | return fromBoolean(tkapp, value); |
| 1274 | } |
| 1275 | #endif |
| 1276 | |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1277 | #ifdef HAVE_LIBTOMMAMTH |
| 1278 | if (app->BignumType == NULL && |
| 1279 | strcmp(value->typePtr->name, "bignum") == 0) { |
| 1280 | /* bignum type is not registered in Tcl */ |
| 1281 | app->BignumType = value->typePtr; |
| 1282 | return fromBignumObj(tkapp, value); |
| 1283 | } |
| 1284 | #endif |
| 1285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | return newPyTclObject(value); |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1289 | /* This mutex synchronizes inter-thread command calls. */ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1290 | TCL_DECLARE_MUTEX(call_mutex) |
| 1291 | |
| 1292 | typedef struct Tkapp_CallEvent { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | Tcl_Event ev; /* Must be first */ |
| 1294 | TkappObject *self; |
| 1295 | PyObject *args; |
| 1296 | int flags; |
| 1297 | PyObject **res; |
| 1298 | PyObject **exc_type, **exc_value, **exc_tb; |
| 1299 | Tcl_Condition *done; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1300 | } Tkapp_CallEvent; |
| 1301 | |
| 1302 | void |
| 1303 | Tkapp_CallDeallocArgs(Tcl_Obj** objv, Tcl_Obj** objStore, int objc) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | int i; |
| 1306 | for (i = 0; i < objc; i++) |
| 1307 | Tcl_DecrRefCount(objv[i]); |
| 1308 | if (objv != objStore) |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1309 | PyMem_Free(objv); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1310 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1311 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1312 | /* Convert Python objects to Tcl objects. This must happen in the |
| 1313 | interpreter thread, which may or may not be the calling thread. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1314 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1315 | static Tcl_Obj** |
| 1316 | Tkapp_CallArgs(PyObject *args, Tcl_Obj** objStore, int *pobjc) |
| 1317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | Tcl_Obj **objv = objStore; |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1319 | Py_ssize_t objc = 0, i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (args == NULL) |
| 1321 | /* do nothing */; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1322 | |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1323 | else if (!(PyTuple_Check(args) || PyList_Check(args))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | objv[0] = AsObj(args); |
Serhiy Storchaka | 0b3ec19 | 2017-03-23 17:53:47 +0200 | [diff] [blame] | 1325 | if (objv[0] == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | goto finally; |
| 1327 | objc = 1; |
| 1328 | Tcl_IncrRefCount(objv[0]); |
| 1329 | } |
| 1330 | else { |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1331 | objc = PySequence_Fast_GET_SIZE(args); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | if (objc > ARGSZ) { |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1334 | if (!CHECK_SIZE(objc, sizeof(Tcl_Obj *))) { |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1335 | PyErr_SetString(PyExc_OverflowError, |
| 1336 | PyTuple_Check(args) ? "tuple is too long" : |
| 1337 | "list is too long"); |
Serhiy Storchaka | 9e6b975 | 2013-08-21 21:38:21 +0300 | [diff] [blame] | 1338 | return NULL; |
| 1339 | } |
Victor Stinner | ee6c3c7 | 2014-09-11 17:50:21 +0200 | [diff] [blame] | 1340 | objv = (Tcl_Obj **)PyMem_Malloc(((size_t)objc) * sizeof(Tcl_Obj *)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | if (objv == NULL) { |
| 1342 | PyErr_NoMemory(); |
| 1343 | objc = 0; |
| 1344 | goto finally; |
| 1345 | } |
| 1346 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | for (i = 0; i < objc; i++) { |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 1349 | PyObject *v = PySequence_Fast_GET_ITEM(args, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | if (v == Py_None) { |
| 1351 | objc = i; |
| 1352 | break; |
| 1353 | } |
| 1354 | objv[i] = AsObj(v); |
| 1355 | if (!objv[i]) { |
| 1356 | /* Reset objc, so it attempts to clear |
| 1357 | objects only up to i. */ |
| 1358 | objc = i; |
| 1359 | goto finally; |
| 1360 | } |
| 1361 | Tcl_IncrRefCount(objv[i]); |
| 1362 | } |
| 1363 | } |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1364 | *pobjc = (int)objc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | return objv; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1366 | finally: |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 1367 | Tkapp_CallDeallocArgs(objv, objStore, (int)objc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1369 | } |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 1370 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1371 | /* Convert the results of a command call into a Python objects. */ |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1372 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1373 | static PyObject* |
| 1374 | Tkapp_CallResult(TkappObject *self) |
| 1375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | PyObject *res = NULL; |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1377 | Tcl_Obj *value = Tcl_GetObjResult(self->interp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | if(self->wantobjects) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | /* Not sure whether the IncrRef is necessary, but something |
| 1380 | may overwrite the interpreter result while we are |
| 1381 | converting it. */ |
| 1382 | Tcl_IncrRefCount(value); |
| 1383 | res = FromObj((PyObject*)self, value); |
| 1384 | Tcl_DecrRefCount(value); |
| 1385 | } else { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1386 | res = unicodeFromTclObj(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | } |
| 1388 | return res; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1389 | } |
Guido van Rossum | 632de27 | 2000-03-29 00:19:50 +0000 | [diff] [blame] | 1390 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1391 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1392 | /* Tkapp_CallProc is the event procedure that is executed in the context of |
| 1393 | the Tcl interpreter thread. Initially, it holds the Tcl lock, and doesn't |
| 1394 | hold the Python lock. */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1395 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1396 | static int |
| 1397 | Tkapp_CallProc(Tkapp_CallEvent *e, int flags) |
| 1398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | Tcl_Obj *objStore[ARGSZ]; |
| 1400 | Tcl_Obj **objv; |
| 1401 | int objc; |
| 1402 | int i; |
| 1403 | ENTER_PYTHON |
| 1404 | objv = Tkapp_CallArgs(e->args, objStore, &objc); |
| 1405 | if (!objv) { |
| 1406 | PyErr_Fetch(e->exc_type, e->exc_value, e->exc_tb); |
| 1407 | *(e->res) = NULL; |
| 1408 | } |
| 1409 | LEAVE_PYTHON |
| 1410 | if (!objv) |
| 1411 | goto done; |
| 1412 | i = Tcl_EvalObjv(e->self->interp, objc, objv, e->flags); |
| 1413 | ENTER_PYTHON |
| 1414 | if (i == TCL_ERROR) { |
| 1415 | *(e->res) = NULL; |
| 1416 | *(e->exc_type) = NULL; |
| 1417 | *(e->exc_tb) = NULL; |
| 1418 | *(e->exc_value) = PyObject_CallFunction( |
| 1419 | Tkinter_TclError, "s", |
| 1420 | Tcl_GetStringResult(e->self->interp)); |
| 1421 | } |
| 1422 | else { |
| 1423 | *(e->res) = Tkapp_CallResult(e->self); |
| 1424 | } |
| 1425 | LEAVE_PYTHON |
Guilherme Polo | 491aee2 | 2009-02-06 23:16:11 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
Guilherme Polo | 491aee2 | 2009-02-06 23:16:11 +0000 | [diff] [blame] | 1428 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | /* Wake up calling thread. */ |
| 1430 | Tcl_MutexLock(&call_mutex); |
| 1431 | Tcl_ConditionNotify(e->done); |
| 1432 | Tcl_MutexUnlock(&call_mutex); |
| 1433 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1436 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1437 | /* This is the main entry point for calling a Tcl command. |
| 1438 | It supports three cases, with regard to threading: |
| 1439 | 1. Tcl is not threaded: Must have the Tcl lock, then can invoke command in |
| 1440 | the context of the calling thread. |
| 1441 | 2. Tcl is threaded, caller of the command is in the interpreter thread: |
| 1442 | Execute the command in the calling thread. Since the Tcl lock will |
| 1443 | not be used, we can merge that with case 1. |
| 1444 | 3. Tcl is threaded, caller is in a different thread: Must queue an event to |
| 1445 | the interpreter thread. Allocation of Tcl objects needs to occur in the |
| 1446 | interpreter thread, so we ship the PyObject* args to the target thread, |
| 1447 | and perform processing there. */ |
| 1448 | |
| 1449 | static PyObject * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1450 | Tkapp_Call(PyObject *selfptr, PyObject *args) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | Tcl_Obj *objStore[ARGSZ]; |
| 1453 | Tcl_Obj **objv = NULL; |
| 1454 | int objc, i; |
| 1455 | PyObject *res = NULL; |
| 1456 | TkappObject *self = (TkappObject*)selfptr; |
| 1457 | int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | /* If args is a single tuple, replace with contents of tuple */ |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 1460 | if (PyTuple_GET_SIZE(args) == 1) { |
| 1461 | PyObject *item = PyTuple_GET_ITEM(args, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | if (PyTuple_Check(item)) |
| 1463 | args = item; |
| 1464 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 1466 | /* We cannot call the command directly. Instead, we must |
| 1467 | marshal the parameters to the interpreter thread. */ |
| 1468 | Tkapp_CallEvent *ev; |
| 1469 | Tcl_Condition cond = NULL; |
| 1470 | PyObject *exc_type, *exc_value, *exc_tb; |
| 1471 | if (!WaitForMainloop(self)) |
| 1472 | return NULL; |
Serhiy Storchaka | 0794088 | 2014-09-11 10:38:54 +0300 | [diff] [blame] | 1473 | ev = (Tkapp_CallEvent*)attemptckalloc(sizeof(Tkapp_CallEvent)); |
| 1474 | if (ev == NULL) { |
| 1475 | PyErr_NoMemory(); |
| 1476 | return NULL; |
| 1477 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; |
| 1479 | ev->self = self; |
| 1480 | ev->args = args; |
| 1481 | ev->res = &res; |
| 1482 | ev->exc_type = &exc_type; |
| 1483 | ev->exc_value = &exc_value; |
| 1484 | ev->exc_tb = &exc_tb; |
| 1485 | ev->done = &cond; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &call_mutex); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | if (res == NULL) { |
| 1490 | if (exc_type) |
| 1491 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 1492 | else |
| 1493 | PyErr_SetObject(Tkinter_TclError, exc_value); |
| 1494 | } |
| 1495 | Tcl_ConditionFinalize(&cond); |
| 1496 | } |
| 1497 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | { |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | objv = Tkapp_CallArgs(args, objStore, &objc); |
| 1501 | if (!objv) |
| 1502 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | ENTER_TCL |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | i = Tcl_EvalObjv(self->interp, objc, objv, flags); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | ENTER_OVERLAP |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (i == TCL_ERROR) |
| 1511 | Tkinter_Error(selfptr); |
| 1512 | else |
| 1513 | res = Tkapp_CallResult(self); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | LEAVE_OVERLAP_TCL |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | Tkapp_CallDeallocArgs(objv, objStore, objc); |
| 1518 | } |
| 1519 | return res; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1523 | /*[clinic input] |
| 1524 | _tkinter.tkapp.eval |
| 1525 | |
| 1526 | script: str |
| 1527 | / |
| 1528 | |
| 1529 | [clinic start generated code]*/ |
| 1530 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1531 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1532 | _tkinter_tkapp_eval_impl(TkappObject *self, const char *script) |
| 1533 | /*[clinic end generated code: output=24b79831f700dea0 input=481484123a455f22]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | PyObject *res = NULL; |
| 1536 | int err; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1537 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1538 | CHECK_STRING_LENGTH(script); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | ENTER_TCL |
| 1542 | err = Tcl_Eval(Tkapp_Interp(self), script); |
| 1543 | ENTER_OVERLAP |
| 1544 | if (err == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1545 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | else |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1547 | res = unicodeFromTclString(Tkapp_Result(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | LEAVE_OVERLAP_TCL |
| 1549 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1552 | /*[clinic input] |
| 1553 | _tkinter.tkapp.evalfile |
| 1554 | |
| 1555 | fileName: str |
| 1556 | / |
| 1557 | |
| 1558 | [clinic start generated code]*/ |
| 1559 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1560 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1561 | _tkinter_tkapp_evalfile_impl(TkappObject *self, const char *fileName) |
| 1562 | /*[clinic end generated code: output=63be88dcee4f11d3 input=873ab707e5e947e1]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1563 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | PyObject *res = NULL; |
| 1565 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1566 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1567 | CHECK_STRING_LENGTH(fileName); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | ENTER_TCL |
| 1571 | err = Tcl_EvalFile(Tkapp_Interp(self), fileName); |
| 1572 | ENTER_OVERLAP |
| 1573 | if (err == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1574 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | else |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1576 | res = unicodeFromTclString(Tkapp_Result(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | LEAVE_OVERLAP_TCL |
| 1578 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1581 | /*[clinic input] |
| 1582 | _tkinter.tkapp.record |
| 1583 | |
| 1584 | script: str |
| 1585 | / |
| 1586 | |
| 1587 | [clinic start generated code]*/ |
| 1588 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1589 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1590 | _tkinter_tkapp_record_impl(TkappObject *self, const char *script) |
| 1591 | /*[clinic end generated code: output=0ffe08a0061730df input=c0b0db5a21412cac]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1592 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | PyObject *res = NULL; |
| 1594 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1595 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1596 | CHECK_STRING_LENGTH(script); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | ENTER_TCL |
| 1600 | err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); |
| 1601 | ENTER_OVERLAP |
| 1602 | if (err == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1603 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | else |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1605 | res = unicodeFromTclString(Tkapp_Result(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | LEAVE_OVERLAP_TCL |
| 1607 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1610 | /*[clinic input] |
Serhiy Storchaka | 929b40a | 2017-10-03 21:37:22 +0300 | [diff] [blame] | 1611 | _tkinter.tkapp.adderrorinfo |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1612 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1613 | msg: str |
| 1614 | / |
| 1615 | |
| 1616 | [clinic start generated code]*/ |
| 1617 | |
| 1618 | static PyObject * |
Serhiy Storchaka | 929b40a | 2017-10-03 21:37:22 +0300 | [diff] [blame] | 1619 | _tkinter_tkapp_adderrorinfo_impl(TkappObject *self, const char *msg) |
| 1620 | /*[clinic end generated code: output=52162eaca2ee53cb input=f4b37aec7c7e8c77]*/ |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1621 | { |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1622 | CHECK_STRING_LENGTH(msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | ENTER_TCL |
| 1626 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
| 1627 | LEAVE_TCL |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1628 | |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 1629 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1634 | /** Tcl Variable **/ |
| 1635 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1636 | typedef PyObject* (*EventFunc)(PyObject*, PyObject *args, int flags); |
| 1637 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1638 | TCL_DECLARE_MUTEX(var_mutex) |
| 1639 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1640 | typedef struct VarEvent { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | Tcl_Event ev; /* must be first */ |
| 1642 | PyObject *self; |
| 1643 | PyObject *args; |
| 1644 | int flags; |
| 1645 | EventFunc func; |
| 1646 | PyObject **res; |
| 1647 | PyObject **exc_type; |
| 1648 | PyObject **exc_val; |
| 1649 | Tcl_Condition *cond; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1650 | } VarEvent; |
| 1651 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1652 | /*[python] |
| 1653 | |
| 1654 | class varname_converter(CConverter): |
| 1655 | type = 'const char *' |
| 1656 | converter = 'varname_converter' |
| 1657 | |
| 1658 | [python]*/ |
| 1659 | /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ |
| 1660 | |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1661 | static int |
| 1662 | varname_converter(PyObject *in, void *_out) |
| 1663 | { |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1664 | const char *s; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1665 | const char **out = (const char**)_out; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | if (PyBytes_Check(in)) { |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 1667 | if (PyBytes_GET_SIZE(in) > INT_MAX) { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1668 | PyErr_SetString(PyExc_OverflowError, "bytes object is too long"); |
| 1669 | return 0; |
| 1670 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 1671 | s = PyBytes_AS_STRING(in); |
| 1672 | if (strlen(s) != (size_t)PyBytes_GET_SIZE(in)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1673 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | *out = s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | return 1; |
| 1678 | } |
| 1679 | if (PyUnicode_Check(in)) { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1680 | Py_ssize_t size; |
| 1681 | s = PyUnicode_AsUTF8AndSize(in, &size); |
Christian Heimes | d33491e | 2014-02-05 00:29:17 +0100 | [diff] [blame] | 1682 | if (s == NULL) { |
| 1683 | return 0; |
| 1684 | } |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1685 | if (size > INT_MAX) { |
| 1686 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 1687 | return 0; |
| 1688 | } |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1689 | if (strlen(s) != (size_t)size) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1690 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | *out = s; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | return 1; |
| 1695 | } |
| 1696 | if (PyTclObject_Check(in)) { |
| 1697 | *out = PyTclObject_TclString(in); |
| 1698 | return 1; |
| 1699 | } |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1700 | PyErr_Format(PyExc_TypeError, |
| 1701 | "must be str, bytes or Tcl_Obj, not %.50s", |
| 1702 | in->ob_type->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | return 0; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1704 | } |
Martin v. Löwis | 1869ec5 | 2003-05-01 05:47:00 +0000 | [diff] [blame] | 1705 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1706 | |
Martin v. Löwis | 59683e8 | 2008-06-13 07:50:45 +0000 | [diff] [blame] | 1707 | static void |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1708 | var_perform(VarEvent *ev) |
| 1709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | *(ev->res) = ev->func(ev->self, ev->args, ev->flags); |
| 1711 | if (!*(ev->res)) { |
| 1712 | PyObject *exc, *val, *tb; |
| 1713 | PyErr_Fetch(&exc, &val, &tb); |
| 1714 | PyErr_NormalizeException(&exc, &val, &tb); |
| 1715 | *(ev->exc_type) = exc; |
| 1716 | *(ev->exc_val) = val; |
Serhiy Storchaka | 3ec5f42 | 2016-10-28 12:14:34 +0300 | [diff] [blame] | 1717 | Py_XDECREF(tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | } |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1719 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | static int |
| 1723 | var_proc(VarEvent* ev, int flags) |
| 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | ENTER_PYTHON |
| 1726 | var_perform(ev); |
| 1727 | Tcl_MutexLock(&var_mutex); |
| 1728 | Tcl_ConditionNotify(ev->cond); |
| 1729 | Tcl_MutexUnlock(&var_mutex); |
| 1730 | LEAVE_PYTHON |
| 1731 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 1734 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1735 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1736 | var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags) |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | TkappObject *self = (TkappObject*)selfptr; |
| 1739 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | VarEvent *ev; |
| 1741 | PyObject *res, *exc_type, *exc_val; |
| 1742 | Tcl_Condition cond = NULL; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | /* The current thread is not the interpreter thread. Marshal |
| 1745 | the call to the interpreter thread, then wait for |
| 1746 | completion. */ |
| 1747 | if (!WaitForMainloop(self)) |
| 1748 | return NULL; |
Martin v. Löwis | ee24e9c | 2003-04-15 20:33:20 +0000 | [diff] [blame] | 1749 | |
Serhiy Storchaka | 0794088 | 2014-09-11 10:38:54 +0300 | [diff] [blame] | 1750 | ev = (VarEvent*)attemptckalloc(sizeof(VarEvent)); |
| 1751 | if (ev == NULL) { |
| 1752 | PyErr_NoMemory(); |
| 1753 | return NULL; |
| 1754 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | ev->self = selfptr; |
| 1756 | ev->args = args; |
| 1757 | ev->flags = flags; |
| 1758 | ev->func = func; |
| 1759 | ev->res = &res; |
| 1760 | ev->exc_type = &exc_type; |
| 1761 | ev->exc_val = &exc_val; |
| 1762 | ev->cond = &cond; |
| 1763 | ev->ev.proc = (Tcl_EventProc*)var_proc; |
| 1764 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex); |
| 1765 | Tcl_ConditionFinalize(&cond); |
| 1766 | if (!res) { |
| 1767 | PyErr_SetObject(exc_type, exc_val); |
| 1768 | Py_DECREF(exc_type); |
| 1769 | Py_DECREF(exc_val); |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | return res; |
| 1773 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | /* Tcl is not threaded, or this is the interpreter thread. */ |
| 1775 | return func(selfptr, args, flags); |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1778 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1779 | SetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1780 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1781 | const char *name1, *name2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | PyObject *newValue; |
| 1783 | PyObject *res = NULL; |
| 1784 | Tcl_Obj *newval, *ok; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1785 | |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1786 | switch (PyTuple_GET_SIZE(args)) { |
| 1787 | case 2: |
| 1788 | if (!PyArg_ParseTuple(args, "O&O:setvar", |
| 1789 | varname_converter, &name1, &newValue)) |
| 1790 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | /* XXX Acquire tcl lock??? */ |
| 1792 | newval = AsObj(newValue); |
| 1793 | if (newval == NULL) |
| 1794 | return NULL; |
| 1795 | ENTER_TCL |
| 1796 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, NULL, |
| 1797 | newval, flags); |
| 1798 | ENTER_OVERLAP |
| 1799 | if (!ok) |
| 1800 | Tkinter_Error(self); |
| 1801 | else { |
| 1802 | res = Py_None; |
| 1803 | Py_INCREF(res); |
| 1804 | } |
| 1805 | LEAVE_OVERLAP_TCL |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1806 | break; |
| 1807 | case 3: |
| 1808 | if (!PyArg_ParseTuple(args, "ssO:setvar", |
| 1809 | &name1, &name2, &newValue)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | return NULL; |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1811 | CHECK_STRING_LENGTH(name1); |
| 1812 | CHECK_STRING_LENGTH(name2); |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1813 | /* XXX must hold tcl lock already??? */ |
| 1814 | newval = AsObj(newValue); |
| 1815 | ENTER_TCL |
| 1816 | ok = Tcl_SetVar2Ex(Tkapp_Interp(self), name1, name2, newval, flags); |
| 1817 | ENTER_OVERLAP |
| 1818 | if (!ok) |
| 1819 | Tkinter_Error(self); |
| 1820 | else { |
| 1821 | res = Py_None; |
| 1822 | Py_INCREF(res); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | } |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1824 | LEAVE_OVERLAP_TCL |
| 1825 | break; |
| 1826 | default: |
| 1827 | PyErr_SetString(PyExc_TypeError, "setvar requires 2 to 3 arguments"); |
| 1828 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | } |
| 1830 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1834 | Tkapp_SetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | return var_invoke(SetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1840 | Tkapp_GlobalSetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | 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] | 1843 | } |
| 1844 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1847 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1848 | GetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1849 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1850 | const char *name1, *name2=NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | PyObject *res = NULL; |
| 1852 | Tcl_Obj *tres; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | if (!PyArg_ParseTuple(args, "O&|s:getvar", |
| 1855 | varname_converter, &name1, &name2)) |
| 1856 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1857 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1858 | CHECK_STRING_LENGTH(name2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | ENTER_TCL |
| 1860 | tres = Tcl_GetVar2Ex(Tkapp_Interp(self), name1, name2, flags); |
| 1861 | ENTER_OVERLAP |
| 1862 | if (tres == NULL) { |
Andrew Svetlov | d0ad0b3 | 2012-10-16 22:50:34 +0300 | [diff] [blame] | 1863 | PyErr_SetString(Tkinter_TclError, |
| 1864 | Tcl_GetStringResult(Tkapp_Interp(self))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | } else { |
| 1866 | if (((TkappObject*)self)->wantobjects) { |
| 1867 | res = FromObj(self, tres); |
| 1868 | } |
| 1869 | else { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 1870 | res = unicodeFromTclObj(tres); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | LEAVE_OVERLAP_TCL |
| 1874 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1878 | Tkapp_GetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | return var_invoke(GetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1884 | Tkapp_GlobalGetVar(PyObject *self, PyObject *args) |
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 | 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] | 1887 | } |
| 1888 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1891 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1892 | UnsetVar(PyObject *self, PyObject *args, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1893 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | char *name1, *name2=NULL; |
| 1895 | int code; |
| 1896 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | if (!PyArg_ParseTuple(args, "s|s:unsetvar", &name1, &name2)) |
| 1899 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1900 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 1901 | CHECK_STRING_LENGTH(name1); |
| 1902 | CHECK_STRING_LENGTH(name2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | ENTER_TCL |
| 1904 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
| 1905 | ENTER_OVERLAP |
| 1906 | if (code == TCL_ERROR) |
| 1907 | res = Tkinter_Error(self); |
| 1908 | else { |
| 1909 | Py_INCREF(Py_None); |
| 1910 | res = Py_None; |
| 1911 | } |
| 1912 | LEAVE_OVERLAP_TCL |
| 1913 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1917 | Tkapp_UnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | return var_invoke(UnsetVar, self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 1923 | Tkapp_GlobalUnsetVar(PyObject *self, PyObject *args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1924 | { |
Andrew Svetlov | d0ad0b3 | 2012-10-16 22:50:34 +0300 | [diff] [blame] | 1925 | return var_invoke(UnsetVar, self, args, |
| 1926 | TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1931 | /** Tcl to Python **/ |
| 1932 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1933 | /*[clinic input] |
| 1934 | _tkinter.tkapp.getint |
| 1935 | |
| 1936 | arg: object |
| 1937 | / |
| 1938 | |
| 1939 | [clinic start generated code]*/ |
| 1940 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1941 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1942 | _tkinter_tkapp_getint(TkappObject *self, PyObject *arg) |
| 1943 | /*[clinic end generated code: output=88cf293fae307cfe input=034026997c5b91f8]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1944 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | char *s; |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1946 | Tcl_Obj *value; |
| 1947 | PyObject *result; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1948 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1949 | if (PyLong_Check(arg)) { |
| 1950 | Py_INCREF(arg); |
| 1951 | return arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | } |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1953 | |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 1954 | if (PyTclObject_Check(arg)) { |
| 1955 | value = ((PyTclObject*)arg)->value; |
| 1956 | Tcl_IncrRefCount(value); |
| 1957 | } |
| 1958 | else { |
| 1959 | if (!PyArg_Parse(arg, "s:getint", &s)) |
| 1960 | return NULL; |
| 1961 | CHECK_STRING_LENGTH(s); |
| 1962 | value = Tcl_NewStringObj(s, -1); |
| 1963 | if (value == NULL) |
| 1964 | return Tkinter_Error((PyObject *)self); |
| 1965 | } |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1966 | /* Don't use Tcl_GetInt() because it returns ambiguous result for value |
| 1967 | in ranges -2**32..-2**31-1 and 2**31..2**32-1 (on 32-bit platform). |
| 1968 | |
| 1969 | Prefer bignum because Tcl_GetWideIntFromObj returns ambiguous result for |
| 1970 | value in ranges -2**64..-2**63-1 and 2**63..2**64-1 (on 32-bit platform). |
| 1971 | */ |
| 1972 | #ifdef HAVE_LIBTOMMAMTH |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1973 | result = fromBignumObj((PyObject *)self, value); |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1974 | #else |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1975 | result = fromWideIntObj((PyObject *)self, value); |
Serhiy Storchaka | ea134da | 2015-04-02 18:46:50 +0300 | [diff] [blame] | 1976 | #endif |
| 1977 | Tcl_DecrRefCount(value); |
| 1978 | if (result != NULL || PyErr_Occurred()) |
| 1979 | return result; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1980 | return Tkinter_Error((PyObject *)self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1983 | /*[clinic input] |
| 1984 | _tkinter.tkapp.getdouble |
| 1985 | |
| 1986 | arg: object |
| 1987 | / |
| 1988 | |
| 1989 | [clinic start generated code]*/ |
| 1990 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1991 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1992 | _tkinter_tkapp_getdouble(TkappObject *self, PyObject *arg) |
| 1993 | /*[clinic end generated code: output=c52b138bd8b956b9 input=22015729ce9ef7f8]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | char *s; |
| 1996 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1997 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 1998 | if (PyFloat_Check(arg)) { |
| 1999 | Py_INCREF(arg); |
| 2000 | return arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | } |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2002 | |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 2003 | if (PyNumber_Check(arg)) { |
| 2004 | return PyNumber_Float(arg); |
| 2005 | } |
| 2006 | |
| 2007 | if (PyTclObject_Check(arg)) { |
| 2008 | if (Tcl_GetDoubleFromObj(Tkapp_Interp(self), |
| 2009 | ((PyTclObject*)arg)->value, |
| 2010 | &v) == TCL_ERROR) |
| 2011 | return Tkinter_Error((PyObject *)self); |
| 2012 | return PyFloat_FromDouble(v); |
| 2013 | } |
| 2014 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2015 | if (!PyArg_Parse(arg, "s:getdouble", &s)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | return NULL; |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2017 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2019 | return Tkinter_Error((PyObject *)self); |
Serhiy Storchaka | 645058d | 2015-05-06 14:00:04 +0300 | [diff] [blame] | 2020 | return PyFloat_FromDouble(v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2023 | /*[clinic input] |
| 2024 | _tkinter.tkapp.getboolean |
| 2025 | |
| 2026 | arg: object |
| 2027 | / |
| 2028 | |
| 2029 | [clinic start generated code]*/ |
| 2030 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2031 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2032 | _tkinter_tkapp_getboolean(TkappObject *self, PyObject *arg) |
| 2033 | /*[clinic end generated code: output=726a9ae445821d91 input=7f11248ef8f8776e]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | char *s; |
| 2036 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2037 | |
Serhiy Storchaka | 9a6e201 | 2015-04-04 12:43:01 +0300 | [diff] [blame] | 2038 | if (PyLong_Check(arg)) { /* int or bool */ |
| 2039 | return PyBool_FromLong(Py_SIZE(arg) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | } |
Serhiy Storchaka | 9a6e201 | 2015-04-04 12:43:01 +0300 | [diff] [blame] | 2041 | |
| 2042 | if (PyTclObject_Check(arg)) { |
| 2043 | if (Tcl_GetBooleanFromObj(Tkapp_Interp(self), |
| 2044 | ((PyTclObject*)arg)->value, |
| 2045 | &v) == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2046 | return Tkinter_Error((PyObject *)self); |
Serhiy Storchaka | 9a6e201 | 2015-04-04 12:43:01 +0300 | [diff] [blame] | 2047 | return PyBool_FromLong(v); |
| 2048 | } |
| 2049 | |
| 2050 | if (!PyArg_Parse(arg, "s:getboolean", &s)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | return NULL; |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2052 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2054 | return Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | return PyBool_FromLong(v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2058 | /*[clinic input] |
| 2059 | _tkinter.tkapp.exprstring |
| 2060 | |
| 2061 | s: str |
| 2062 | / |
| 2063 | |
| 2064 | [clinic start generated code]*/ |
| 2065 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2066 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2067 | _tkinter_tkapp_exprstring_impl(TkappObject *self, const char *s) |
| 2068 | /*[clinic end generated code: output=beda323d3ed0abb1 input=fa78f751afb2f21b]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | PyObject *res = NULL; |
| 2071 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2072 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2073 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | ENTER_TCL |
| 2077 | retval = Tcl_ExprString(Tkapp_Interp(self), s); |
| 2078 | ENTER_OVERLAP |
| 2079 | if (retval == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2080 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2081 | else |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 2082 | res = unicodeFromTclString(Tkapp_Result(self)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | LEAVE_OVERLAP_TCL |
| 2084 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2087 | /*[clinic input] |
| 2088 | _tkinter.tkapp.exprlong |
| 2089 | |
| 2090 | s: str |
| 2091 | / |
| 2092 | |
| 2093 | [clinic start generated code]*/ |
| 2094 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2095 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2096 | _tkinter_tkapp_exprlong_impl(TkappObject *self, const char *s) |
| 2097 | /*[clinic end generated code: output=5d6a46b63c6ebcf9 input=11bd7eee0c57b4dc]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | PyObject *res = NULL; |
| 2100 | int retval; |
| 2101 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2102 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2103 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | ENTER_TCL |
| 2107 | retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); |
| 2108 | ENTER_OVERLAP |
| 2109 | if (retval == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2110 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | else |
Serhiy Storchaka | 8d0f620 | 2015-05-06 14:19:22 +0300 | [diff] [blame] | 2112 | res = PyLong_FromLong(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | LEAVE_OVERLAP_TCL |
| 2114 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2117 | /*[clinic input] |
| 2118 | _tkinter.tkapp.exprdouble |
| 2119 | |
| 2120 | s: str |
| 2121 | / |
| 2122 | |
| 2123 | [clinic start generated code]*/ |
| 2124 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2125 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2126 | _tkinter_tkapp_exprdouble_impl(TkappObject *self, const char *s) |
| 2127 | /*[clinic end generated code: output=ff78df1081ea4158 input=ff02bc11798832d5]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | PyObject *res = NULL; |
| 2130 | double v; |
| 2131 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2132 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2133 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | CHECK_TCL_APPARTMENT; |
| 2135 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
| 2136 | ENTER_TCL |
| 2137 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
| 2138 | ENTER_OVERLAP |
| 2139 | PyFPE_END_PROTECT(retval) |
| 2140 | if (retval == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2141 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | else |
Serhiy Storchaka | 8d0f620 | 2015-05-06 14:19:22 +0300 | [diff] [blame] | 2143 | res = PyFloat_FromDouble(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | LEAVE_OVERLAP_TCL |
| 2145 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2148 | /*[clinic input] |
| 2149 | _tkinter.tkapp.exprboolean |
| 2150 | |
| 2151 | s: str |
| 2152 | / |
| 2153 | |
| 2154 | [clinic start generated code]*/ |
| 2155 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2156 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2157 | _tkinter_tkapp_exprboolean_impl(TkappObject *self, const char *s) |
| 2158 | /*[clinic end generated code: output=8b28038c22887311 input=c8c66022bdb8d5d3]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | PyObject *res = NULL; |
| 2161 | int retval; |
| 2162 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2163 | |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 2164 | CHECK_STRING_LENGTH(s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | CHECK_TCL_APPARTMENT; |
| 2166 | ENTER_TCL |
| 2167 | retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); |
| 2168 | ENTER_OVERLAP |
| 2169 | if (retval == TCL_ERROR) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2170 | res = Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | else |
Serhiy Storchaka | 8d0f620 | 2015-05-06 14:19:22 +0300 | [diff] [blame] | 2172 | res = PyLong_FromLong(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | LEAVE_OVERLAP_TCL |
| 2174 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2179 | /*[clinic input] |
| 2180 | _tkinter.tkapp.splitlist |
| 2181 | |
| 2182 | arg: object |
| 2183 | / |
| 2184 | |
| 2185 | [clinic start generated code]*/ |
| 2186 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2187 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2188 | _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg) |
| 2189 | /*[clinic end generated code: output=13b51d34386d36fb input=2b2e13351e3c0b53]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | char *list; |
| 2192 | int argc; |
Serhiy Storchaka | 6716d60 | 2014-07-30 19:19:21 +0300 | [diff] [blame] | 2193 | const char **argv; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2194 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2196 | |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2197 | if (PyTclObject_Check(arg)) { |
| 2198 | int objc; |
| 2199 | Tcl_Obj **objv; |
| 2200 | if (Tcl_ListObjGetElements(Tkapp_Interp(self), |
| 2201 | ((PyTclObject*)arg)->value, |
| 2202 | &objc, &objv) == TCL_ERROR) { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2203 | return Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | } |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2205 | if (!(v = PyTuple_New(objc))) |
| 2206 | return NULL; |
| 2207 | for (i = 0; i < objc; i++) { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2208 | PyObject *s = FromObj((PyObject*)self, objv[i]); |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2209 | if (!s) { |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2210 | Py_DECREF(v); |
| 2211 | return NULL; |
| 2212 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2213 | PyTuple_SET_ITEM(v, i, s); |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2214 | } |
| 2215 | return v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | } |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2217 | if (PyTuple_Check(arg)) { |
| 2218 | Py_INCREF(arg); |
| 2219 | return arg; |
| 2220 | } |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 2221 | if (PyList_Check(arg)) { |
| 2222 | return PySequence_Tuple(arg); |
| 2223 | } |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2224 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2225 | if (!PyArg_Parse(arg, "et:splitlist", "utf-8", &list)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2227 | |
Serhiy Storchaka | 27c623c | 2017-10-03 22:39:55 +0300 | [diff] [blame] | 2228 | if (strlen(list) >= INT_MAX) { |
| 2229 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 2230 | PyMem_Free(list); |
| 2231 | return NULL; |
| 2232 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | if (Tcl_SplitList(Tkapp_Interp(self), list, |
| 2234 | &argc, &argv) == TCL_ERROR) { |
| 2235 | PyMem_Free(list); |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2236 | return Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | if (!(v = PyTuple_New(argc))) |
| 2240 | goto finally; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | for (i = 0; i < argc; i++) { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 2243 | PyObject *s = unicodeFromTclString(argv[i]); |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2244 | if (!s) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2245 | Py_DECREF(v); |
| 2246 | v = NULL; |
| 2247 | goto finally; |
| 2248 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2249 | PyTuple_SET_ITEM(v, i, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2251 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2252 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | ckfree(FREECAST argv); |
| 2254 | PyMem_Free(list); |
| 2255 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2258 | /*[clinic input] |
| 2259 | _tkinter.tkapp.split |
| 2260 | |
| 2261 | arg: object |
| 2262 | / |
| 2263 | |
| 2264 | [clinic start generated code]*/ |
| 2265 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2266 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2267 | _tkinter_tkapp_split(TkappObject *self, PyObject *arg) |
| 2268 | /*[clinic end generated code: output=e08ad832363facfd input=a1c78349eacaa140]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2269 | { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2270 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2272 | |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2273 | if (PyTclObject_Check(arg)) { |
| 2274 | Tcl_Obj *value = ((PyTclObject*)arg)->value; |
| 2275 | int objc; |
| 2276 | Tcl_Obj **objv; |
| 2277 | int i; |
| 2278 | if (Tcl_ListObjGetElements(Tkapp_Interp(self), value, |
| 2279 | &objc, &objv) == TCL_ERROR) { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2280 | return FromObj((PyObject*)self, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | } |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2282 | if (objc == 0) |
| 2283 | return PyUnicode_FromString(""); |
| 2284 | if (objc == 1) |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2285 | return FromObj((PyObject*)self, objv[0]); |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2286 | if (!(v = PyTuple_New(objc))) |
| 2287 | return NULL; |
| 2288 | for (i = 0; i < objc; i++) { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2289 | PyObject *s = FromObj((PyObject*)self, objv[i]); |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2290 | if (!s) { |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2291 | Py_DECREF(v); |
| 2292 | return NULL; |
| 2293 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2294 | PyTuple_SET_ITEM(v, i, s); |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2295 | } |
| 2296 | return v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | } |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 2298 | if (PyTuple_Check(arg) || PyList_Check(arg)) |
Serhiy Storchaka | 203eb31 | 2013-08-22 17:40:31 +0300 | [diff] [blame] | 2299 | return SplitObj(arg); |
| 2300 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2301 | if (!PyArg_Parse(arg, "et:split", "utf-8", &list)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | return NULL; |
Serhiy Storchaka | 27c623c | 2017-10-03 22:39:55 +0300 | [diff] [blame] | 2303 | if (strlen(list) >= INT_MAX) { |
| 2304 | PyErr_SetString(PyExc_OverflowError, "string is too long"); |
| 2305 | PyMem_Free(list); |
| 2306 | return NULL; |
| 2307 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | v = Split(list); |
| 2309 | PyMem_Free(list); |
| 2310 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2315 | /** Tcl Command **/ |
| 2316 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2317 | /* Client data struct */ |
| 2318 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | PyObject *self; |
| 2320 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2321 | } PythonCmd_ClientData; |
| 2322 | |
| 2323 | static int |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2324 | PythonCmd_Error(Tcl_Interp *interp) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | errorInCmd = 1; |
| 2327 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2328 | LEAVE_PYTHON |
| 2329 | return TCL_ERROR; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2332 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2333 | * function or method. |
| 2334 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2335 | static int |
Serhiy Storchaka | 6716d60 | 2014-07-30 19:19:21 +0300 | [diff] [blame] | 2336 | PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2337 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 2339 | PyObject *func, *arg, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | int i, rv; |
| 2341 | Tcl_Obj *obj_res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | /* TBD: no error checking here since we know, via the |
| 2346 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 2347 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | func = data->func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | /* Create argument list (argv1, ..., argvN) */ |
| 2351 | if (!(arg = PyTuple_New(argc - 1))) |
| 2352 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | for (i = 0; i < (argc - 1); i++) { |
Serhiy Storchaka | 1317e14 | 2014-02-03 21:24:07 +0200 | [diff] [blame] | 2355 | PyObject *s = unicodeFromTclString(argv[i + 1]); |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2356 | if (!s) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | Py_DECREF(arg); |
| 2358 | return PythonCmd_Error(interp); |
| 2359 | } |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 2360 | PyTuple_SET_ITEM(arg, i, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | } |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 2362 | res = PyObject_Call(func, arg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | if (res == NULL) |
| 2366 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | obj_res = AsObj(res); |
| 2369 | if (obj_res == NULL) { |
| 2370 | Py_DECREF(res); |
| 2371 | return PythonCmd_Error(interp); |
| 2372 | } |
| 2373 | else { |
| 2374 | Tcl_SetObjResult(interp, obj_res); |
| 2375 | rv = TCL_OK; |
| 2376 | } |
Guido van Rossum | 2834b97 | 2000-10-06 16:58:26 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | Py_DECREF(res); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | LEAVE_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | return rv; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2386 | PythonCmdDelete(ClientData clientData) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | ENTER_PYTHON |
| 2391 | Py_XDECREF(data->self); |
| 2392 | Py_XDECREF(data->func); |
| 2393 | PyMem_DEL(data); |
| 2394 | LEAVE_PYTHON |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2399 | |
| 2400 | TCL_DECLARE_MUTEX(command_mutex) |
| 2401 | |
| 2402 | typedef struct CommandEvent{ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | Tcl_Event ev; |
| 2404 | Tcl_Interp* interp; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2405 | const char *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | int create; |
| 2407 | int *status; |
| 2408 | ClientData *data; |
| 2409 | Tcl_Condition *done; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2410 | } CommandEvent; |
| 2411 | |
| 2412 | static int |
| 2413 | Tkapp_CommandProc(CommandEvent *ev, int flags) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | if (ev->create) |
| 2416 | *ev->status = Tcl_CreateCommand( |
| 2417 | ev->interp, ev->name, PythonCmd, |
| 2418 | ev->data, PythonCmdDelete) == NULL; |
| 2419 | else |
| 2420 | *ev->status = Tcl_DeleteCommand(ev->interp, ev->name); |
| 2421 | Tcl_MutexLock(&command_mutex); |
| 2422 | Tcl_ConditionNotify(ev->done); |
| 2423 | Tcl_MutexUnlock(&command_mutex); |
| 2424 | return 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2427 | /*[clinic input] |
| 2428 | _tkinter.tkapp.createcommand |
| 2429 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2430 | name: str |
| 2431 | func: object |
| 2432 | / |
| 2433 | |
| 2434 | [clinic start generated code]*/ |
| 2435 | |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2436 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2437 | _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name, |
| 2438 | PyObject *func) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2439 | /*[clinic end generated code: output=2a1c79a4ee2af410 input=255785cb70edc6a0]*/ |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | PythonCmd_ClientData *data; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | int err; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2443 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2444 | CHECK_STRING_LENGTH(name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | if (!PyCallable_Check(func)) { |
| 2446 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
| 2447 | return NULL; |
| 2448 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread() && |
| 2451 | !WaitForMainloop(self)) |
| 2452 | return NULL; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | data = PyMem_NEW(PythonCmd_ClientData, 1); |
| 2455 | if (!data) |
| 2456 | return PyErr_NoMemory(); |
| 2457 | Py_INCREF(self); |
| 2458 | Py_INCREF(func); |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2459 | data->self = (PyObject *) self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | data->func = func; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2462 | Tcl_Condition cond = NULL; |
Serhiy Storchaka | 0794088 | 2014-09-11 10:38:54 +0300 | [diff] [blame] | 2463 | CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent)); |
| 2464 | if (ev == NULL) { |
| 2465 | PyErr_NoMemory(); |
| 2466 | PyMem_DEL(data); |
| 2467 | return NULL; |
| 2468 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2470 | ev->interp = self->interp; |
| 2471 | ev->create = 1; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2472 | ev->name = name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | ev->data = (ClientData)data; |
| 2474 | ev->status = &err; |
| 2475 | ev->done = &cond; |
| 2476 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &command_mutex); |
| 2477 | Tcl_ConditionFinalize(&cond); |
| 2478 | } |
| 2479 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | { |
| 2481 | ENTER_TCL |
| 2482 | err = Tcl_CreateCommand( |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2483 | Tkapp_Interp(self), name, PythonCmd, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | (ClientData)data, PythonCmdDelete) == NULL; |
| 2485 | LEAVE_TCL |
| 2486 | } |
| 2487 | if (err) { |
| 2488 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
| 2489 | PyMem_DEL(data); |
| 2490 | return NULL; |
| 2491 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2492 | |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2493 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2498 | /*[clinic input] |
| 2499 | _tkinter.tkapp.deletecommand |
| 2500 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2501 | name: str |
| 2502 | / |
| 2503 | |
| 2504 | [clinic start generated code]*/ |
| 2505 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2506 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2507 | _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2508 | /*[clinic end generated code: output=a67e8cb5845e0d2d input=53e9952eae1f85f5]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2511 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2512 | CHECK_STRING_LENGTH(name); |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 2513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) { |
| 2515 | Tcl_Condition cond = NULL; |
| 2516 | CommandEvent *ev; |
Serhiy Storchaka | 0794088 | 2014-09-11 10:38:54 +0300 | [diff] [blame] | 2517 | ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent)); |
| 2518 | if (ev == NULL) { |
| 2519 | PyErr_NoMemory(); |
| 2520 | return NULL; |
| 2521 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; |
| 2523 | ev->interp = self->interp; |
| 2524 | ev->create = 0; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2525 | ev->name = name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | ev->status = &err; |
| 2527 | ev->done = &cond; |
| 2528 | Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, |
| 2529 | &command_mutex); |
| 2530 | Tcl_ConditionFinalize(&cond); |
| 2531 | } |
| 2532 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | { |
| 2534 | ENTER_TCL |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2535 | err = Tcl_DeleteCommand(self->interp, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | LEAVE_TCL |
| 2537 | } |
| 2538 | if (err == -1) { |
| 2539 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 2540 | return NULL; |
| 2541 | } |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2542 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2546 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2547 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2548 | /** File Handler **/ |
| 2549 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2550 | typedef struct _fhcdata { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | PyObject *func; |
| 2552 | PyObject *file; |
| 2553 | int id; |
| 2554 | struct _fhcdata *next; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2555 | } FileHandler_ClientData; |
| 2556 | |
| 2557 | static FileHandler_ClientData *HeadFHCD; |
| 2558 | |
| 2559 | static FileHandler_ClientData * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2560 | NewFHCD(PyObject *func, PyObject *file, int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | FileHandler_ClientData *p; |
| 2563 | p = PyMem_NEW(FileHandler_ClientData, 1); |
| 2564 | if (p != NULL) { |
| 2565 | Py_XINCREF(func); |
| 2566 | Py_XINCREF(file); |
| 2567 | p->func = func; |
| 2568 | p->file = file; |
| 2569 | p->id = id; |
| 2570 | p->next = HeadFHCD; |
| 2571 | HeadFHCD = p; |
| 2572 | } |
| 2573 | return p; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2577 | DeleteFHCD(int id) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | FileHandler_ClientData *p, **pp; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | pp = &HeadFHCD; |
| 2582 | while ((p = *pp) != NULL) { |
| 2583 | if (p->id == id) { |
| 2584 | *pp = p->next; |
| 2585 | Py_XDECREF(p->func); |
| 2586 | Py_XDECREF(p->file); |
| 2587 | PyMem_DEL(p); |
| 2588 | } |
| 2589 | else |
| 2590 | pp = &p->next; |
| 2591 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 2594 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2595 | FileHandler(ClientData clientData, int mask) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | FileHandler_ClientData *data = (FileHandler_ClientData *)clientData; |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 2598 | PyObject *func, *file, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | ENTER_PYTHON |
| 2601 | func = data->func; |
| 2602 | file = data->file; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2603 | |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 2604 | res = PyObject_CallFunction(func, "Oi", file, mask); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | if (res == NULL) { |
| 2606 | errorInCmd = 1; |
| 2607 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2608 | } |
| 2609 | Py_XDECREF(res); |
| 2610 | LEAVE_PYTHON |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2613 | /*[clinic input] |
| 2614 | _tkinter.tkapp.createfilehandler |
| 2615 | |
| 2616 | file: object |
| 2617 | mask: int |
| 2618 | func: object |
| 2619 | / |
| 2620 | |
| 2621 | [clinic start generated code]*/ |
| 2622 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2623 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2624 | _tkinter_tkapp_createfilehandler_impl(TkappObject *self, PyObject *file, |
| 2625 | int mask, PyObject *func) |
| 2626 | /*[clinic end generated code: output=f73ce82de801c353 input=84943a5286e47947]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | FileHandler_ClientData *data; |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2629 | int tfile; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | tfile = PyObject_AsFileDescriptor(file); |
| 2634 | if (tfile < 0) |
| 2635 | return NULL; |
| 2636 | if (!PyCallable_Check(func)) { |
| 2637 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
| 2638 | return NULL; |
| 2639 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | data = NewFHCD(func, file, tfile); |
| 2642 | if (data == NULL) |
| 2643 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | /* Ought to check for null Tcl_File object... */ |
| 2646 | ENTER_TCL |
| 2647 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
| 2648 | LEAVE_TCL |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2649 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2652 | /*[clinic input] |
| 2653 | _tkinter.tkapp.deletefilehandler |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2654 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2655 | file: object |
| 2656 | / |
| 2657 | |
| 2658 | [clinic start generated code]*/ |
| 2659 | |
| 2660 | static PyObject * |
| 2661 | _tkinter_tkapp_deletefilehandler(TkappObject *self, PyObject *file) |
| 2662 | /*[clinic end generated code: output=b53cc96ebf9476fd input=abbec19d66312e2a]*/ |
| 2663 | { |
| 2664 | int tfile; |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | CHECK_TCL_APPARTMENT; |
Neal Norwitz | 12e2217 | 2003-03-03 21:16:39 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | tfile = PyObject_AsFileDescriptor(file); |
| 2669 | if (tfile < 0) |
| 2670 | return NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | DeleteFHCD(tfile); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | /* Ought to check for null Tcl_File object... */ |
| 2675 | ENTER_TCL |
| 2676 | Tcl_DeleteFileHandler(tfile); |
| 2677 | LEAVE_TCL |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2678 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2679 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2680 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2682 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2683 | /**** Tktt Object (timer token) ****/ |
| 2684 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 2685 | static PyObject *Tktt_Type; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2686 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2687 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | PyObject_HEAD |
| 2689 | Tcl_TimerToken token; |
| 2690 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2691 | } TkttObject; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2692 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2693 | /*[clinic input] |
| 2694 | _tkinter.tktimertoken.deletetimerhandler |
| 2695 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2696 | [clinic start generated code]*/ |
| 2697 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2698 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2699 | _tkinter_tktimertoken_deletetimerhandler_impl(TkttObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2700 | /*[clinic end generated code: output=bd7fe17f328cfa55 input=40bd070ff85f5cf3]*/ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2701 | { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2702 | TkttObject *v = self; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | PyObject *func = v->func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | if (v->token != NULL) { |
| 2706 | Tcl_DeleteTimerHandler(v->token); |
| 2707 | v->token = NULL; |
| 2708 | } |
| 2709 | if (func != NULL) { |
| 2710 | v->func = NULL; |
| 2711 | Py_DECREF(func); |
| 2712 | Py_DECREF(v); /* See Tktt_New() */ |
| 2713 | } |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2714 | Py_RETURN_NONE; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2717 | static TkttObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2718 | Tktt_New(PyObject *func) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | TkttObject *v; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2721 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 2722 | v = PyObject_New(TkttObject, (PyTypeObject *) Tktt_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | if (v == NULL) |
| 2724 | return NULL; |
Antoine Pitrou | 584e815 | 2013-08-11 00:22:30 +0200 | [diff] [blame] | 2725 | Py_INCREF(Tktt_Type); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | Py_INCREF(func); |
| 2728 | v->token = NULL; |
| 2729 | v->func = func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | /* Extra reference, deleted when called or when handler is deleted */ |
| 2732 | Py_INCREF(v); |
| 2733 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2737 | Tktt_Dealloc(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | TkttObject *v = (TkttObject *)self; |
| 2740 | PyObject *func = v->func; |
Antoine Pitrou | 584e815 | 2013-08-11 00:22:30 +0200 | [diff] [blame] | 2741 | PyObject *tp = (PyObject *) Py_TYPE(self); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | Py_XDECREF(func); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | PyObject_Del(self); |
Antoine Pitrou | 584e815 | 2013-08-11 00:22:30 +0200 | [diff] [blame] | 2746 | Py_DECREF(tp); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 2749 | static PyObject * |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2750 | Tktt_Repr(PyObject *self) |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | TkttObject *v = (TkttObject *)self; |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 2753 | return PyUnicode_FromFormat("<tktimertoken at %p%s>", |
| 2754 | v, |
| 2755 | v->func == NULL ? ", handler deleted" : ""); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2758 | /** Timer Handler **/ |
| 2759 | |
| 2760 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 2761 | TimerHandler(ClientData clientData) |
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 *)clientData; |
| 2764 | PyObject *func = v->func; |
| 2765 | PyObject *res; |
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 | if (func == NULL) |
| 2768 | return; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | v->func = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | ENTER_PYTHON |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2773 | |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 2774 | res = _PyObject_CallNoArg(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | Py_DECREF(func); |
| 2776 | Py_DECREF(v); /* See Tktt_New() */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | if (res == NULL) { |
| 2779 | errorInCmd = 1; |
| 2780 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 2781 | } |
| 2782 | else |
| 2783 | Py_DECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | LEAVE_PYTHON |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2788 | /*[clinic input] |
| 2789 | _tkinter.tkapp.createtimerhandler |
| 2790 | |
| 2791 | milliseconds: int |
| 2792 | func: object |
| 2793 | / |
| 2794 | |
| 2795 | [clinic start generated code]*/ |
| 2796 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2797 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2798 | _tkinter_tkapp_createtimerhandler_impl(TkappObject *self, int milliseconds, |
| 2799 | PyObject *func) |
| 2800 | /*[clinic end generated code: output=2da5959b9d031911 input=ba6729f32f0277a5]*/ |
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 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | if (!PyCallable_Check(func)) { |
| 2805 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
| 2806 | return NULL; |
| 2807 | } |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | CHECK_TCL_APPARTMENT; |
Martin v. Löwis | 7f13489 | 2003-03-03 10:40:01 +0000 | [diff] [blame] | 2810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | v = Tktt_New(func); |
| 2812 | if (v) { |
| 2813 | v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 2814 | (ClientData)v); |
| 2815 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | return (PyObject *) v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2821 | /** Event Loop **/ |
| 2822 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2823 | /*[clinic input] |
| 2824 | _tkinter.tkapp.mainloop |
| 2825 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2826 | threshold: int = 0 |
| 2827 | / |
| 2828 | |
| 2829 | [clinic start generated code]*/ |
| 2830 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2831 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2832 | _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 2833 | /*[clinic end generated code: output=0ba8eabbe57841b0 input=036bcdcf03d5eca0]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | PyThreadState *tstate = PyThreadState_Get(); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | CHECK_TCL_APPARTMENT; |
| 2838 | self->dispatching = 1; |
Martin v. Löwis | b5bfb9f | 2002-12-12 17:07:58 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | quitMainLoop = 0; |
| 2841 | while (Tk_GetNumMainWindows() > threshold && |
| 2842 | !quitMainLoop && |
| 2843 | !errorInCmd) |
| 2844 | { |
| 2845 | int result; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | if (self->threaded) { |
| 2848 | /* Allow other Python threads to run. */ |
| 2849 | ENTER_TCL |
| 2850 | result = Tcl_DoOneEvent(0); |
| 2851 | LEAVE_TCL |
| 2852 | } |
| 2853 | else { |
| 2854 | Py_BEGIN_ALLOW_THREADS |
| 2855 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
| 2856 | tcl_tstate = tstate; |
| 2857 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 2858 | tcl_tstate = NULL; |
| 2859 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
| 2860 | if (result == 0) |
| 2861 | Sleep(Tkinter_busywaitinterval); |
| 2862 | Py_END_ALLOW_THREADS |
| 2863 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | if (PyErr_CheckSignals() != 0) { |
| 2866 | self->dispatching = 0; |
| 2867 | return NULL; |
| 2868 | } |
| 2869 | if (result < 0) |
| 2870 | break; |
| 2871 | } |
| 2872 | self->dispatching = 0; |
| 2873 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | if (errorInCmd) { |
| 2876 | errorInCmd = 0; |
| 2877 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 2878 | excInCmd = valInCmd = trbInCmd = NULL; |
| 2879 | return NULL; |
| 2880 | } |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2881 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2884 | /*[clinic input] |
| 2885 | _tkinter.tkapp.dooneevent |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2886 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2887 | flags: int = 0 |
| 2888 | / |
| 2889 | |
| 2890 | [clinic start generated code]*/ |
| 2891 | |
| 2892 | static PyObject * |
| 2893 | _tkinter_tkapp_dooneevent_impl(TkappObject *self, int flags) |
| 2894 | /*[clinic end generated code: output=27c6b2aa464cac29 input=6542b928e364b793]*/ |
| 2895 | { |
| 2896 | int rv; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | ENTER_TCL |
| 2899 | rv = Tcl_DoOneEvent(flags); |
| 2900 | LEAVE_TCL |
Serhiy Storchaka | 8d0f620 | 2015-05-06 14:19:22 +0300 | [diff] [blame] | 2901 | return PyLong_FromLong(rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2904 | /*[clinic input] |
| 2905 | _tkinter.tkapp.quit |
| 2906 | [clinic start generated code]*/ |
| 2907 | |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 2908 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2909 | _tkinter_tkapp_quit_impl(TkappObject *self) |
| 2910 | /*[clinic end generated code: output=7f21eeff481f754f input=e03020dc38aff23c]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | quitMainLoop = 1; |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2913 | Py_RETURN_NONE; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2916 | /*[clinic input] |
| 2917 | _tkinter.tkapp.interpaddr |
| 2918 | [clinic start generated code]*/ |
| 2919 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2920 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2921 | _tkinter_tkapp_interpaddr_impl(TkappObject *self) |
| 2922 | /*[clinic end generated code: output=6caaae3273b3c95a input=2dd32cbddb55a111]*/ |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2923 | { |
Victor Stinner | e1040e2 | 2013-09-05 00:22:24 +0200 | [diff] [blame] | 2924 | return PyLong_FromVoidPtr(Tkapp_Interp(self)); |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2927 | /*[clinic input] |
| 2928 | _tkinter.tkapp.loadtk |
| 2929 | [clinic start generated code]*/ |
| 2930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2931 | static PyObject * |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2932 | _tkinter_tkapp_loadtk_impl(TkappObject *self) |
| 2933 | /*[clinic end generated code: output=e9e10a954ce46d2a input=b5e82afedd6354f0]*/ |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2935 | Tcl_Interp *interp = Tkapp_Interp(self); |
| 2936 | const char * _tk_exists = NULL; |
| 2937 | int err; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2938 | |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2939 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | /* Up to Tk 8.4.13, Tk_Init deadlocks on the second call when the |
| 2941 | * first call failed. |
| 2942 | * To avoid the deadlock, we just refuse the second call through |
| 2943 | * a static variable. |
| 2944 | */ |
| 2945 | if (tk_load_failed) { |
| 2946 | PyErr_SetString(Tkinter_TclError, TKINTER_LOADTK_ERRMSG); |
| 2947 | return NULL; |
| 2948 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2949 | #endif |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 2950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | /* We want to guard against calling Tk_Init() multiple times */ |
| 2952 | CHECK_TCL_APPARTMENT; |
| 2953 | ENTER_TCL |
| 2954 | err = Tcl_Eval(Tkapp_Interp(self), "info exists tk_version"); |
| 2955 | ENTER_OVERLAP |
| 2956 | if (err == TCL_ERROR) { |
| 2957 | /* This sets an exception, but we cannot return right |
| 2958 | away because we need to exit the overlap first. */ |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2959 | Tkinter_Error((PyObject *)self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | } else { |
| 2961 | _tk_exists = Tkapp_Result(self); |
| 2962 | } |
| 2963 | LEAVE_OVERLAP_TCL |
| 2964 | if (err == TCL_ERROR) { |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | if (_tk_exists == NULL || strcmp(_tk_exists, "1") != 0) { |
| 2968 | if (Tk_Init(interp) == TCL_ERROR) { |
Andrew Svetlov | d0ad0b3 | 2012-10-16 22:50:34 +0300 | [diff] [blame] | 2969 | PyErr_SetString(Tkinter_TclError, |
| 2970 | Tcl_GetStringResult(Tkapp_Interp(self))); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2971 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | tk_load_failed = 1; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 2973 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | return NULL; |
| 2975 | } |
| 2976 | } |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2977 | Py_RETURN_NONE; |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 2978 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2979 | |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2980 | static PyObject * |
| 2981 | Tkapp_WantObjects(PyObject *self, PyObject *args) |
| 2982 | { |
| 2983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | int wantobjects = -1; |
| 2985 | if (!PyArg_ParseTuple(args, "|i:wantobjects", &wantobjects)) |
| 2986 | return NULL; |
| 2987 | if (wantobjects == -1) |
| 2988 | return PyBool_FromLong(((TkappObject*)self)->wantobjects); |
| 2989 | ((TkappObject*)self)->wantobjects = wantobjects; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2990 | |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 2991 | Py_RETURN_NONE; |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2994 | /*[clinic input] |
| 2995 | _tkinter.tkapp.willdispatch |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 2996 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 2997 | [clinic start generated code]*/ |
| 2998 | |
| 2999 | static PyObject * |
| 3000 | _tkinter_tkapp_willdispatch_impl(TkappObject *self) |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 3001 | /*[clinic end generated code: output=0e3f46d244642155 input=d88f5970843d6dab]*/ |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3002 | { |
| 3003 | self->dispatching = 1; |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 3004 | |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 3005 | Py_RETURN_NONE; |
Martin v. Löwis | 5b26abb | 2002-12-28 09:23:09 +0000 | [diff] [blame] | 3006 | } |
Martin v. Löwis | ffad633 | 2002-11-26 09:28:05 +0000 | [diff] [blame] | 3007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3009 | /**** Tkapp Type Methods ****/ |
| 3010 | |
| 3011 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 3012 | Tkapp_Dealloc(PyObject *self) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3013 | { |
Antoine Pitrou | 584e815 | 2013-08-11 00:22:30 +0200 | [diff] [blame] | 3014 | PyObject *tp = (PyObject *) Py_TYPE(self); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | /*CHECK_TCL_APPARTMENT;*/ |
| 3016 | ENTER_TCL |
| 3017 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
| 3018 | LEAVE_TCL |
| 3019 | PyObject_Del(self); |
Antoine Pitrou | 584e815 | 2013-08-11 00:22:30 +0200 | [diff] [blame] | 3020 | Py_DECREF(tp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | DisableEventHook(); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3026 | /**** Tkinter Module ****/ |
| 3027 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3028 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | PyObject* tuple; |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3030 | Py_ssize_t size; /* current size */ |
| 3031 | Py_ssize_t maxsize; /* allocated size */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3032 | } FlattenContext; |
| 3033 | |
| 3034 | static int |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3035 | _bump(FlattenContext* context, Py_ssize_t size) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | /* expand tuple to hold (at least) size new items. |
| 3038 | return true if successful, false if an exception was raised */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3039 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3040 | Py_ssize_t maxsize = context->maxsize * 2; /* never overflows */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | if (maxsize < context->size + size) |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3043 | maxsize = context->size + size; /* never overflows */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3045 | context->maxsize = maxsize; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | return _PyTuple_Resize(&context->tuple, maxsize) >= 0; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | static int |
Andrew M. Kuchling | 288e97b | 2000-06-19 00:55:09 +0000 | [diff] [blame] | 3051 | _flatten1(FlattenContext* context, PyObject* item, int depth) |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | /* add tuple or list to argument tuple (recursively) */ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3054 | |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 3055 | Py_ssize_t i, size; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3057 | if (depth > 1000) { |
| 3058 | PyErr_SetString(PyExc_ValueError, |
| 3059 | "nesting too deep in _flatten"); |
| 3060 | return 0; |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 3061 | } else if (PyTuple_Check(item) || PyList_Check(item)) { |
| 3062 | size = PySequence_Fast_GET_SIZE(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | /* preallocate (assume no nesting) */ |
| 3064 | if (context->size + size > context->maxsize && |
| 3065 | !_bump(context, size)) |
| 3066 | return 0; |
| 3067 | /* copy items to output tuple */ |
| 3068 | for (i = 0; i < size; i++) { |
Serhiy Storchaka | 2b00c49 | 2014-05-21 17:12:21 +0300 | [diff] [blame] | 3069 | PyObject *o = PySequence_Fast_GET_ITEM(item, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | if (PyList_Check(o) || PyTuple_Check(o)) { |
| 3071 | if (!_flatten1(context, o, depth + 1)) |
| 3072 | return 0; |
| 3073 | } else if (o != Py_None) { |
| 3074 | if (context->size + 1 > context->maxsize && |
| 3075 | !_bump(context, 1)) |
| 3076 | return 0; |
| 3077 | Py_INCREF(o); |
| 3078 | PyTuple_SET_ITEM(context->tuple, |
| 3079 | context->size++, o); |
| 3080 | } |
| 3081 | } |
| 3082 | } else { |
| 3083 | PyErr_SetString(PyExc_TypeError, "argument must be sequence"); |
| 3084 | return 0; |
| 3085 | } |
| 3086 | return 1; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3089 | /*[clinic input] |
| 3090 | _tkinter._flatten |
| 3091 | |
| 3092 | item: object |
| 3093 | / |
| 3094 | |
| 3095 | [clinic start generated code]*/ |
| 3096 | |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3097 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3098 | _tkinter__flatten(PyObject *module, PyObject *item) |
| 3099 | /*[clinic end generated code: output=cad02a3f97f29862 input=6b9c12260aa1157f]*/ |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | FlattenContext context; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | context.maxsize = PySequence_Size(item); |
| 3104 | if (context.maxsize < 0) |
| 3105 | return NULL; |
| 3106 | if (context.maxsize == 0) |
| 3107 | return PyTuple_New(0); |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 3108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | context.tuple = PyTuple_New(context.maxsize); |
| 3110 | if (!context.tuple) |
| 3111 | return NULL; |
Guido van Rossum | 82c0dfa | 2007-11-21 20:09:18 +0000 | [diff] [blame] | 3112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | context.size = 0; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | if (!_flatten1(&context, item,0)) |
| 3116 | return NULL; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | if (_PyTuple_Resize(&context.tuple, context.size)) |
| 3119 | return NULL; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | return context.tuple; |
Andrew M. Kuchling | e475e70 | 2000-06-18 18:45:50 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3124 | /*[clinic input] |
| 3125 | _tkinter.create |
| 3126 | |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 3127 | screenName: str(accept={str, NoneType}) = NULL |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3128 | baseName: str = NULL |
| 3129 | className: str = "Tk" |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 3130 | interactive: bool(accept={int}) = False |
| 3131 | wantobjects: bool(accept={int}) = False |
| 3132 | wantTk: bool(accept={int}) = True |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3133 | if false, then Tk_Init() doesn't get called |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 3134 | sync: bool(accept={int}) = False |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3135 | if true, then pass -sync to wish |
Larry Hastings | dbfdc38 | 2015-05-04 06:59:46 -0700 | [diff] [blame] | 3136 | use: str(accept={str, NoneType}) = NULL |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3137 | if not None, then pass -use to wish |
| 3138 | / |
| 3139 | |
| 3140 | [clinic start generated code]*/ |
| 3141 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3142 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3143 | _tkinter_create_impl(PyObject *module, const char *screenName, |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3144 | const char *baseName, const char *className, |
| 3145 | int interactive, int wantobjects, int wantTk, int sync, |
| 3146 | const char *use) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 3147 | /*[clinic end generated code: output=e3315607648e6bb4 input=431907c134c80085]*/ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3148 | { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3149 | /* XXX baseName is not used anymore; |
| 3150 | * try getting rid of it. */ |
Serhiy Storchaka | 79851d7 | 2014-05-30 14:24:03 +0300 | [diff] [blame] | 3151 | CHECK_STRING_LENGTH(screenName); |
| 3152 | CHECK_STRING_LENGTH(baseName); |
| 3153 | CHECK_STRING_LENGTH(className); |
| 3154 | CHECK_STRING_LENGTH(use); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | return (PyObject *) Tkapp_New(screenName, className, |
Andrew Svetlov | d0ad0b3 | 2012-10-16 22:50:34 +0300 | [diff] [blame] | 3157 | interactive, wantobjects, wantTk, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3158 | sync, use); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3161 | /*[clinic input] |
| 3162 | _tkinter.setbusywaitinterval |
| 3163 | |
| 3164 | new_val: int |
| 3165 | / |
| 3166 | |
| 3167 | Set the busy-wait interval in milliseconds between successive calls to Tcl_DoOneEvent in a threaded Python interpreter. |
| 3168 | |
| 3169 | It should be set to a divisor of the maximum time between frames in an animation. |
| 3170 | [clinic start generated code]*/ |
| 3171 | |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3172 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3173 | _tkinter_setbusywaitinterval_impl(PyObject *module, int new_val) |
| 3174 | /*[clinic end generated code: output=42bf7757dc2d0ab6 input=deca1d6f9e6dae47]*/ |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3176 | if (new_val < 0) { |
| 3177 | PyErr_SetString(PyExc_ValueError, |
| 3178 | "busywaitinterval must be >= 0"); |
| 3179 | return NULL; |
| 3180 | } |
| 3181 | Tkinter_busywaitinterval = new_val; |
Andrew Svetlov | 9dc5bdd | 2012-10-07 12:08:38 +0300 | [diff] [blame] | 3182 | Py_RETURN_NONE; |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3185 | /*[clinic input] |
| 3186 | _tkinter.getbusywaitinterval -> int |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3187 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3188 | Return the current busy-wait interval between successive calls to Tcl_DoOneEvent in a threaded Python interpreter. |
| 3189 | [clinic start generated code]*/ |
| 3190 | |
| 3191 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3192 | _tkinter_getbusywaitinterval_impl(PyObject *module) |
| 3193 | /*[clinic end generated code: output=23b72d552001f5c7 input=a695878d2d576a84]*/ |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3194 | { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3195 | return Tkinter_busywaitinterval; |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3198 | #include "clinic/_tkinter.c.h" |
| 3199 | |
| 3200 | static PyMethodDef Tktt_methods[] = |
| 3201 | { |
| 3202 | _TKINTER_TKTIMERTOKEN_DELETETIMERHANDLER_METHODDEF |
| 3203 | {NULL, NULL} |
| 3204 | }; |
| 3205 | |
Larry Hastings | 2d0a69a | 2015-05-03 14:49:19 -0700 | [diff] [blame] | 3206 | static PyType_Slot Tktt_Type_slots[] = { |
| 3207 | {Py_tp_dealloc, Tktt_Dealloc}, |
| 3208 | {Py_tp_repr, Tktt_Repr}, |
| 3209 | {Py_tp_methods, Tktt_methods}, |
| 3210 | {0, 0} |
| 3211 | }; |
| 3212 | |
| 3213 | static PyType_Spec Tktt_Type_spec = { |
| 3214 | "_tkinter.tktimertoken", |
| 3215 | sizeof(TkttObject), |
| 3216 | 0, |
| 3217 | Py_TPFLAGS_DEFAULT, |
| 3218 | Tktt_Type_slots, |
| 3219 | }; |
| 3220 | |
| 3221 | |
| 3222 | /**** Tkapp Method List ****/ |
| 3223 | |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3224 | static PyMethodDef Tkapp_methods[] = |
| 3225 | { |
| 3226 | _TKINTER_TKAPP_WILLDISPATCH_METHODDEF |
| 3227 | {"wantobjects", Tkapp_WantObjects, METH_VARARGS}, |
| 3228 | {"call", Tkapp_Call, METH_VARARGS}, |
| 3229 | _TKINTER_TKAPP_EVAL_METHODDEF |
| 3230 | _TKINTER_TKAPP_EVALFILE_METHODDEF |
| 3231 | _TKINTER_TKAPP_RECORD_METHODDEF |
Serhiy Storchaka | 929b40a | 2017-10-03 21:37:22 +0300 | [diff] [blame] | 3232 | _TKINTER_TKAPP_ADDERRORINFO_METHODDEF |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3233 | {"setvar", Tkapp_SetVar, METH_VARARGS}, |
| 3234 | {"globalsetvar", Tkapp_GlobalSetVar, METH_VARARGS}, |
| 3235 | {"getvar", Tkapp_GetVar, METH_VARARGS}, |
| 3236 | {"globalgetvar", Tkapp_GlobalGetVar, METH_VARARGS}, |
| 3237 | {"unsetvar", Tkapp_UnsetVar, METH_VARARGS}, |
| 3238 | {"globalunsetvar", Tkapp_GlobalUnsetVar, METH_VARARGS}, |
| 3239 | _TKINTER_TKAPP_GETINT_METHODDEF |
| 3240 | _TKINTER_TKAPP_GETDOUBLE_METHODDEF |
| 3241 | _TKINTER_TKAPP_GETBOOLEAN_METHODDEF |
| 3242 | _TKINTER_TKAPP_EXPRSTRING_METHODDEF |
| 3243 | _TKINTER_TKAPP_EXPRLONG_METHODDEF |
| 3244 | _TKINTER_TKAPP_EXPRDOUBLE_METHODDEF |
| 3245 | _TKINTER_TKAPP_EXPRBOOLEAN_METHODDEF |
| 3246 | _TKINTER_TKAPP_SPLITLIST_METHODDEF |
| 3247 | _TKINTER_TKAPP_SPLIT_METHODDEF |
| 3248 | _TKINTER_TKAPP_CREATECOMMAND_METHODDEF |
| 3249 | _TKINTER_TKAPP_DELETECOMMAND_METHODDEF |
| 3250 | _TKINTER_TKAPP_CREATEFILEHANDLER_METHODDEF |
| 3251 | _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF |
| 3252 | _TKINTER_TKAPP_CREATETIMERHANDLER_METHODDEF |
| 3253 | _TKINTER_TKAPP_MAINLOOP_METHODDEF |
| 3254 | _TKINTER_TKAPP_DOONEEVENT_METHODDEF |
| 3255 | _TKINTER_TKAPP_QUIT_METHODDEF |
| 3256 | _TKINTER_TKAPP_INTERPADDR_METHODDEF |
| 3257 | _TKINTER_TKAPP_LOADTK_METHODDEF |
| 3258 | {NULL, NULL} |
| 3259 | }; |
Martin v. Löwis | 28e9ce9 | 2003-05-09 08:19:48 +0000 | [diff] [blame] | 3260 | |
Larry Hastings | 2d0a69a | 2015-05-03 14:49:19 -0700 | [diff] [blame] | 3261 | static PyType_Slot Tkapp_Type_slots[] = { |
| 3262 | {Py_tp_dealloc, Tkapp_Dealloc}, |
| 3263 | {Py_tp_methods, Tkapp_methods}, |
| 3264 | {0, 0} |
| 3265 | }; |
| 3266 | |
| 3267 | |
| 3268 | static PyType_Spec Tkapp_Type_spec = { |
| 3269 | "_tkinter.tkapp", |
| 3270 | sizeof(TkappObject), |
| 3271 | 0, |
| 3272 | Py_TPFLAGS_DEFAULT, |
| 3273 | Tkapp_Type_slots, |
| 3274 | }; |
| 3275 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3276 | static PyMethodDef moduleMethods[] = |
| 3277 | { |
Serhiy Storchaka | 5abdf484 | 2015-05-03 15:49:47 +0300 | [diff] [blame] | 3278 | _TKINTER__FLATTEN_METHODDEF |
| 3279 | _TKINTER_CREATE_METHODDEF |
| 3280 | _TKINTER_SETBUSYWAITINTERVAL_METHODDEF |
| 3281 | _TKINTER_GETBUSYWAITINTERVAL_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3283 | }; |
| 3284 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3285 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3286 | |
| 3287 | static int stdin_ready = 0; |
| 3288 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3289 | #ifndef MS_WINDOWS |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3290 | static void |
Fred Drake | 509d79a | 2000-07-08 04:04:38 +0000 | [diff] [blame] | 3291 | MyFileProc(void *clientData, int mask) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | stdin_ready = 1; |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3294 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3295 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3296 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3297 | static PyThreadState *event_tstate = NULL; |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 3298 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3299 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3300 | EventHook(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3301 | { |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3302 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | int tfile; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3304 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | PyEval_RestoreThread(event_tstate); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | stdin_ready = 0; |
| 3307 | errorInCmd = 0; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3308 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | tfile = fileno(stdin); |
| 3310 | Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3311 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | while (!errorInCmd && !stdin_ready) { |
| 3313 | int result; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3314 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | if (_kbhit()) { |
| 3316 | stdin_ready = 1; |
| 3317 | break; |
| 3318 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3319 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | Py_BEGIN_ALLOW_THREADS |
| 3321 | if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); |
| 3322 | tcl_tstate = event_tstate; |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 3323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | dc1adab | 1998-10-09 20:51:18 +0000 | [diff] [blame] | 3325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | tcl_tstate = NULL; |
| 3327 | if(tcl_lock)PyThread_release_lock(tcl_lock); |
| 3328 | if (result == 0) |
| 3329 | Sleep(Tkinter_busywaitinterval); |
| 3330 | Py_END_ALLOW_THREADS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | if (result < 0) |
| 3333 | break; |
| 3334 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3335 | #ifndef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 3337 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3338 | if (errorInCmd) { |
| 3339 | errorInCmd = 0; |
| 3340 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 3341 | excInCmd = valInCmd = trbInCmd = NULL; |
| 3342 | PyErr_Print(); |
| 3343 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | PyEval_SaveThread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3346 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3347 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3348 | #endif |
| 3349 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3350 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3351 | EnableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3352 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3353 | #ifdef WAIT_FOR_STDIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | if (PyOS_InputHook == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | event_tstate = PyThreadState_Get(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | PyOS_InputHook = EventHook; |
| 3357 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3358 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 3362 | DisableEventHook(void) |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3363 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3364 | #ifdef WAIT_FOR_STDIN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { |
| 3366 | PyOS_InputHook = NULL; |
| 3367 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 3368 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 3371 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3372 | static struct PyModuleDef _tkintermodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | PyModuleDef_HEAD_INIT, |
| 3374 | "_tkinter", |
| 3375 | NULL, |
| 3376 | -1, |
| 3377 | moduleMethods, |
| 3378 | NULL, |
| 3379 | NULL, |
| 3380 | NULL, |
| 3381 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3382 | }; |
| 3383 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3384 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3385 | PyInit__tkinter(void) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3386 | { |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3387 | PyObject *m, *uexe, *cexe, *o; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | tcl_lock = PyThread_allocate_lock(); |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3390 | if (tcl_lock == NULL) |
| 3391 | return NULL; |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | m = PyModule_Create(&_tkintermodule); |
| 3394 | if (m == NULL) |
| 3395 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3396 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3397 | o = PyErr_NewException("_tkinter.TclError", NULL, NULL); |
| 3398 | if (o == NULL) { |
Jesus Cea | ef86d12 | 2012-07-19 21:18:07 +0200 | [diff] [blame] | 3399 | Py_DECREF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | return NULL; |
Jesus Cea | ef86d12 | 2012-07-19 21:18:07 +0200 | [diff] [blame] | 3401 | } |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3402 | Py_INCREF(o); |
| 3403 | if (PyModule_AddObject(m, "TclError", o)) { |
| 3404 | Py_DECREF(o); |
| 3405 | Py_DECREF(m); |
| 3406 | return NULL; |
| 3407 | } |
| 3408 | Tkinter_TclError = o; |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 3409 | |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3410 | if (PyModule_AddIntConstant(m, "READABLE", TCL_READABLE)) { |
| 3411 | Py_DECREF(m); |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | if (PyModule_AddIntConstant(m, "WRITABLE", TCL_WRITABLE)) { |
| 3415 | Py_DECREF(m); |
| 3416 | return NULL; |
| 3417 | } |
| 3418 | if (PyModule_AddIntConstant(m, "EXCEPTION", TCL_EXCEPTION)) { |
| 3419 | Py_DECREF(m); |
| 3420 | return NULL; |
| 3421 | } |
| 3422 | if (PyModule_AddIntConstant(m, "WINDOW_EVENTS", TCL_WINDOW_EVENTS)) { |
| 3423 | Py_DECREF(m); |
| 3424 | return NULL; |
| 3425 | } |
| 3426 | if (PyModule_AddIntConstant(m, "FILE_EVENTS", TCL_FILE_EVENTS)) { |
| 3427 | Py_DECREF(m); |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | if (PyModule_AddIntConstant(m, "TIMER_EVENTS", TCL_TIMER_EVENTS)) { |
| 3431 | Py_DECREF(m); |
| 3432 | return NULL; |
| 3433 | } |
| 3434 | if (PyModule_AddIntConstant(m, "IDLE_EVENTS", TCL_IDLE_EVENTS)) { |
| 3435 | Py_DECREF(m); |
| 3436 | return NULL; |
| 3437 | } |
| 3438 | if (PyModule_AddIntConstant(m, "ALL_EVENTS", TCL_ALL_EVENTS)) { |
| 3439 | Py_DECREF(m); |
| 3440 | return NULL; |
| 3441 | } |
| 3442 | if (PyModule_AddIntConstant(m, "DONT_WAIT", TCL_DONT_WAIT)) { |
| 3443 | Py_DECREF(m); |
| 3444 | return NULL; |
| 3445 | } |
| 3446 | if (PyModule_AddStringConstant(m, "TK_VERSION", TK_VERSION)) { |
| 3447 | Py_DECREF(m); |
| 3448 | return NULL; |
| 3449 | } |
| 3450 | if (PyModule_AddStringConstant(m, "TCL_VERSION", TCL_VERSION)) { |
| 3451 | Py_DECREF(m); |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | |
| 3455 | o = PyType_FromSpec(&Tkapp_Type_spec); |
| 3456 | if (o == NULL) { |
| 3457 | Py_DECREF(m); |
| 3458 | return NULL; |
| 3459 | } |
Serhiy Storchaka | e3f1b09 | 2016-05-08 20:46:22 +0300 | [diff] [blame] | 3460 | ((PyTypeObject *)o)->tp_new = NULL; |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3461 | if (PyModule_AddObject(m, "TkappType", o)) { |
| 3462 | Py_DECREF(o); |
| 3463 | Py_DECREF(m); |
| 3464 | return NULL; |
| 3465 | } |
| 3466 | Tkapp_Type = o; |
| 3467 | |
| 3468 | o = PyType_FromSpec(&Tktt_Type_spec); |
| 3469 | if (o == NULL) { |
| 3470 | Py_DECREF(m); |
| 3471 | return NULL; |
| 3472 | } |
Serhiy Storchaka | e3f1b09 | 2016-05-08 20:46:22 +0300 | [diff] [blame] | 3473 | ((PyTypeObject *)o)->tp_new = NULL; |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3474 | if (PyModule_AddObject(m, "TkttType", o)) { |
| 3475 | Py_DECREF(o); |
| 3476 | Py_DECREF(m); |
| 3477 | return NULL; |
| 3478 | } |
| 3479 | Tktt_Type = o; |
| 3480 | |
| 3481 | o = PyType_FromSpec(&PyTclObject_Type_spec); |
| 3482 | if (o == NULL) { |
| 3483 | Py_DECREF(m); |
| 3484 | return NULL; |
| 3485 | } |
Serhiy Storchaka | e3f1b09 | 2016-05-08 20:46:22 +0300 | [diff] [blame] | 3486 | ((PyTypeObject *)o)->tp_new = NULL; |
Andrew Svetlov | 11dc6c3 | 2012-10-14 16:54:35 +0300 | [diff] [blame] | 3487 | if (PyModule_AddObject(m, "Tcl_Obj", o)) { |
| 3488 | Py_DECREF(o); |
| 3489 | Py_DECREF(m); |
| 3490 | return NULL; |
| 3491 | } |
| 3492 | PyTclObject_Type = o; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 3493 | |
| 3494 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | /* Tk_MacOSXSetupTkNotifier must be called before Tcl's subsystems |
| 3496 | * start waking up. Note that Tcl_FindExecutable will do this, this |
| 3497 | * code must be above it! The original warning from |
| 3498 | * tkMacOSXAppInit.c is copied below. |
| 3499 | * |
| 3500 | * NB - You have to swap in the Tk Notifier BEFORE you start up the |
| 3501 | * Tcl interpreter for now. It probably should work to do this |
| 3502 | * in the other order, but for now it doesn't seem to. |
| 3503 | * |
| 3504 | */ |
| 3505 | Tk_MacOSXSetupTkNotifier(); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 3506 | #endif |
| 3507 | |
| 3508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | /* This helps the dynamic loader; in Unicode aware Tcl versions |
| 3510 | it also helps Tcl find its encodings. */ |
| 3511 | uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1); |
| 3512 | if (uexe) { |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 3513 | cexe = PyUnicode_EncodeFSDefault(uexe); |
Zachary Ware | 7dc9dea | 2015-05-22 11:36:53 -0500 | [diff] [blame] | 3514 | if (cexe) { |
| 3515 | #ifdef MS_WINDOWS |
| 3516 | int set_var = 0; |
| 3517 | PyObject *str_path; |
| 3518 | wchar_t *wcs_path; |
| 3519 | DWORD ret; |
| 3520 | |
| 3521 | ret = GetEnvironmentVariableW(L"TCL_LIBRARY", NULL, 0); |
| 3522 | |
| 3523 | if (!ret && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
| 3524 | str_path = _get_tcl_lib_path(); |
| 3525 | if (str_path == NULL && PyErr_Occurred()) { |
| 3526 | return NULL; |
| 3527 | } |
| 3528 | if (str_path != NULL) { |
| 3529 | wcs_path = PyUnicode_AsWideCharString(str_path, NULL); |
| 3530 | if (wcs_path == NULL) { |
| 3531 | return NULL; |
| 3532 | } |
| 3533 | SetEnvironmentVariableW(L"TCL_LIBRARY", wcs_path); |
| 3534 | set_var = 1; |
| 3535 | } |
| 3536 | } |
| 3537 | |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 3538 | Tcl_FindExecutable(PyBytes_AS_STRING(cexe)); |
Zachary Ware | 7dc9dea | 2015-05-22 11:36:53 -0500 | [diff] [blame] | 3539 | |
| 3540 | if (set_var) { |
| 3541 | SetEnvironmentVariableW(L"TCL_LIBRARY", NULL); |
| 3542 | PyMem_Free(wcs_path); |
| 3543 | } |
| 3544 | #else |
Serhiy Storchaka | 696c8af | 2016-06-19 11:22:47 +0300 | [diff] [blame] | 3545 | Tcl_FindExecutable(PyBytes_AS_STRING(cexe)); |
Zachary Ware | 7dc9dea | 2015-05-22 11:36:53 -0500 | [diff] [blame] | 3546 | #endif /* MS_WINDOWS */ |
| 3547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | Py_XDECREF(cexe); |
| 3549 | Py_DECREF(uexe); |
| 3550 | } |
Guido van Rossum | e187b0e | 2000-03-27 21:46:29 +0000 | [diff] [blame] | 3551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3552 | if (PyErr_Occurred()) { |
| 3553 | Py_DECREF(m); |
| 3554 | return NULL; |
| 3555 | } |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 3556 | |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 3557 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3558 | /* This was not a good idea; through <Destroy> bindings, |
| 3559 | Tcl_Finalize() may invoke Python code but at that point the |
| 3560 | interpreter and thread state have already been destroyed! */ |
| 3561 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 3562 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | return m; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 3564 | } |