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