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