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