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