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