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