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