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