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