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