Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright (C) 1994 Steen Lumholt. |
| 3 | Copyright 1994-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 4 | The Netherlands. |
| 5 | |
| 6 | All Rights Reserved |
| 7 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 8 | Permission to use, copy, modify, and distribute this software and its |
| 9 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 10 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 11 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 12 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 13 | Centrum or CWI or Corporation for National Research Initiatives or |
| 14 | CNRI not be used in advertising or publicity pertaining to |
| 15 | distribution of the software without specific, written prior |
| 16 | permission. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 18 | While CWI is the initial source for this software, a modified version |
| 19 | is made available by the Corporation for National Research Initiatives |
| 20 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 21 | |
| 22 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 23 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 24 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 25 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 26 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 27 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 28 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 29 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 845547d | 1996-06-26 18:26:04 +0000 | [diff] [blame] | 30 | |
| 31 | ******************************************************************/ |
| 32 | |
| 33 | /* _tkinter.c -- Interface to libtk.a and libtcl.a. */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 35 | /* TCL/TK VERSION INFO: |
| 36 | |
| 37 | Unix: |
Guido van Rossum | 496f8f6 | 1997-07-19 19:57:42 +0000 | [diff] [blame] | 38 | Tcl/Tk 8.0 (even alpha or beta) or 7.6/4.2 are recommended. |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 39 | Versions 7.5/4.1 are the earliest versions still supported. |
| 40 | Versions 7.4/4.0 or Tk 3.x are no longer supported. |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 41 | |
| 42 | Mac and Windows: |
Guido van Rossum | 496f8f6 | 1997-07-19 19:57:42 +0000 | [diff] [blame] | 43 | Use Tcl 8.0 if available (even alpha or beta). |
| 44 | The oldest usable version is 4.1p1/7.5p1. |
| 45 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 46 | XXX Further speed-up ideas, involving Tcl 8.0 features: |
| 47 | |
| 48 | - In Tcl_Call(), create Tcl objects from the arguments, possibly using |
| 49 | intelligent mappings between Python objects and Tcl objects (e.g. ints, |
| 50 | floats and Tcl window pointers could be handled specially). |
| 51 | |
| 52 | - Register a new Tcl type, "Python callable", which can be called more |
| 53 | efficiently and passed to Tcl_EvalObj() directly (if this is possible). |
| 54 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 58 | #include "Python.h" |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 59 | #include <ctype.h> |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 61 | #ifdef WITH_THREAD |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame^] | 62 | #include "pythread.h" |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 65 | #ifdef MS_WINDOWS |
| 66 | #include <windows.h> |
| 67 | #endif |
| 68 | |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 69 | #ifdef macintosh |
| 70 | #define MAC_TCL |
Guido van Rossum | 290283b | 1997-06-02 22:16:43 +0000 | [diff] [blame] | 71 | #include "myselect.h" |
Guido van Rossum | bf0dc9f | 1996-08-20 20:49:56 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame^] | 74 | #ifdef PYOS_OS2 |
| 75 | #include "myselect.h" |
| 76 | #endif |
| 77 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 78 | #include <tcl.h> |
| 79 | #include <tk.h> |
| 80 | |
Guido van Rossum | 3e819a7 | 1997-08-01 19:29:02 +0000 | [diff] [blame] | 81 | #define TKMAJORMINOR (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) |
| 82 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 83 | #if TKMAJORMINOR < 4001 |
| 84 | #error "Tk 4.0 or 3.x are not supported -- use 4.1 or higher" |
| 85 | #endif |
| 86 | |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 87 | #if TKMAJORMINOR >= 8000 && defined(macintosh) |
| 88 | /* Sigh, we have to include this to get at the tcl qd pointer */ |
| 89 | #include <tkMac.h> |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 90 | /* And this one we need to clear the menu bar */ |
| 91 | #include <Menus.h> |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Guido van Rossum | b010544 | 1997-10-08 15:25:37 +0000 | [diff] [blame] | 94 | #if TKMAJORMINOR < 8000 || !defined(MS_WINDOWS) |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 95 | #define HAVE_CREATEFILEHANDLER |
| 96 | #endif |
| 97 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 98 | #ifdef HAVE_CREATEFILEHANDLER |
| 99 | |
| 100 | /* Tcl_CreateFileHandler() changed several times; these macros deal with the |
| 101 | messiness. In Tcl 8.0 and later, it is not available on Windows (and on |
| 102 | Unix, only because Jack added it back); when available on Windows, it only |
| 103 | applies to sockets. */ |
| 104 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 105 | #ifdef MS_WINDOWS |
| 106 | #define FHANDLETYPE TCL_WIN_SOCKET |
| 107 | #else |
| 108 | #define FHANDLETYPE TCL_UNIX_FD |
| 109 | #endif |
| 110 | |
| 111 | #if TKMAJORMINOR < 8000 |
| 112 | #define FHANDLE Tcl_File |
| 113 | #define MAKEFHANDLE(fd) Tcl_GetFile((ClientData)(fd), FHANDLETYPE) |
| 114 | #else |
| 115 | #define FHANDLE int |
| 116 | #define MAKEFHANDLE(fd) (fd) |
| 117 | #endif |
| 118 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 119 | /* If Tcl can wait for a Unix file descriptor, define the EventHook() routine |
| 120 | which uses this to handle Tcl events while the user is typing commands. */ |
| 121 | |
| 122 | #if FHANDLETYPE == TCL_UNIX_FD |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 123 | #define WAIT_FOR_STDIN |
| 124 | #endif |
| 125 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 126 | #endif /* HAVE_CREATEFILEHANDLER */ |
| 127 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 128 | #ifdef MS_WINDOWS |
| 129 | #include <conio.h> |
| 130 | #define WAIT_FOR_STDIN |
| 131 | #endif |
| 132 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 133 | #ifdef WITH_THREAD |
| 134 | |
| 135 | /* The threading situation is complicated. Tcl is not thread-safe, except for |
| 136 | Tcl 8.1, which will probably remain in alpha status for another 6 months |
| 137 | (and the README says that Tk will probably remain thread-unsafe forever). |
| 138 | So we need to use a lock around all uses of Tcl. Previously, the Python |
| 139 | interpreter lock was used for this. However, this causes problems when |
| 140 | other Python threads need to run while Tcl is blocked waiting for events. |
| 141 | |
| 142 | To solve this problem, a separate lock for Tcl is introduced. Holding it |
| 143 | is incompatible with holding Python's interpreter lock. The following four |
| 144 | macros manipulate both locks together. |
| 145 | |
| 146 | ENTER_TCL and LEAVE_TCL are brackets, just like Py_BEGIN_ALLOW_THREADS and |
| 147 | Py_END_ALLOW_THREADS. They should be used whenever a call into Tcl is made |
| 148 | that could call an event handler, or otherwise affect the state of a Tcl |
| 149 | interpreter. These assume that the surrounding code has the Python |
| 150 | interpreter lock; inside the brackets, the Python interpreter lock has been |
| 151 | released and the lock for Tcl has been acquired. |
| 152 | |
Guido van Rossum | 5e97783 | 1998-06-15 14:03:52 +0000 | [diff] [blame] | 153 | Sometimes, it is necessary to have both the Python lock and the Tcl lock. |
| 154 | (For example, when transferring data from the Tcl interpreter result to a |
| 155 | Python string object.) This can be done by using different macros to close |
| 156 | the ENTER_TCL block: ENTER_OVERLAP reacquires the Python lock (and restores |
| 157 | the thread state) but doesn't release the Tcl lock; LEAVE_OVERLAP_TCL |
| 158 | releases the Tcl lock. |
| 159 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 160 | By contrast, ENTER_PYTHON(tstate) and LEAVE_PYTHON are used in Tcl event |
| 161 | handlers when the handler needs to use Python. Such event handlers are |
| 162 | entered while the lock for Tcl is held; the event handler presumably needs |
| 163 | to use Python. ENTER_PYTHON(tstate) releases the lock for Tcl and acquires |
| 164 | the Python interpreter lock, restoring the appropriate thread state, and |
| 165 | LEAVE_PYTHON releases the Python interpreter lock and re-acquires the lock |
| 166 | for Tcl. It is okay for ENTER_TCL/LEAVE_TCL pairs to be contained inside |
| 167 | the code between ENTER_PYTHON(tstate) and LEAVE_PYTHON. |
| 168 | |
| 169 | These locks expand to several statements and brackets; they should not be |
| 170 | used in branches of if statements and the like. |
| 171 | |
| 172 | */ |
| 173 | |
| 174 | static type_lock tcl_lock = 0; |
| 175 | |
| 176 | #define ENTER_TCL \ |
| 177 | Py_BEGIN_ALLOW_THREADS acquire_lock(tcl_lock, 1); |
| 178 | |
| 179 | #define LEAVE_TCL \ |
| 180 | release_lock(tcl_lock); Py_END_ALLOW_THREADS |
| 181 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 182 | #define ENTER_OVERLAP \ |
| 183 | Py_END_ALLOW_THREADS |
| 184 | |
| 185 | #define LEAVE_OVERLAP_TCL \ |
| 186 | release_lock(tcl_lock); |
| 187 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 188 | #define ENTER_PYTHON(tstate) \ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 189 | release_lock(tcl_lock); PyEval_RestoreThread((tstate)); |
| 190 | |
| 191 | #define LEAVE_PYTHON \ |
| 192 | PyEval_SaveThread(); acquire_lock(tcl_lock, 1); |
| 193 | |
| 194 | #else |
| 195 | |
| 196 | #define ENTER_TCL |
| 197 | #define LEAVE_TCL |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 198 | #define ENTER_OVERLAP |
| 199 | #define LEAVE_OVERLAP_TCL |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 200 | #define ENTER_PYTHON(tstate) |
| 201 | #define LEAVE_PYTHON |
| 202 | |
| 203 | #endif |
| 204 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 205 | extern int Tk_GetNumMainWindows(); |
| 206 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 207 | #ifdef macintosh |
| 208 | |
| 209 | /* |
| 210 | ** Additional cruft needed by Tcl/Tk on the Mac. |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 211 | ** This is for Tcl 7.5 and Tk 4.1 (patch release 1). |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 212 | */ |
| 213 | |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 214 | /* ckfree() expects a char* */ |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 215 | #define FREECAST (char *) |
| 216 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 217 | #include <Events.h> /* For EventRecord */ |
| 218 | |
| 219 | typedef int (*TclMacConvertEventPtr) Py_PROTO((EventRecord *eventPtr)); |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 220 | /* They changed the name... */ |
| 221 | #if TKMAJORMINOR < 8000 |
| 222 | #define Tcl_MacSetEventProc TclMacSetEventProc |
| 223 | #endif |
| 224 | void Tcl_MacSetEventProc Py_PROTO((TclMacConvertEventPtr procPtr)); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 225 | int TkMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 226 | |
| 227 | staticforward int PyMacConvertEvent Py_PROTO((EventRecord *eventPtr)); |
| 228 | |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 229 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 230 | #pragma import on |
| 231 | #endif |
| 232 | |
| 233 | #include <SIOUX.h> |
| 234 | extern int SIOUXIsAppWindow(WindowPtr); |
| 235 | |
| 236 | #if defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) |
| 237 | #pragma import reset |
| 238 | #endif |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 239 | #endif /* macintosh */ |
| 240 | |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 241 | #ifndef FREECAST |
Guido van Rossum | 7ffa761 | 1996-08-13 21:10:16 +0000 | [diff] [blame] | 242 | #define FREECAST (char *) |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 243 | #endif |
| 244 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 245 | /**** Tkapp Object Declaration ****/ |
| 246 | |
| 247 | staticforward PyTypeObject Tkapp_Type; |
| 248 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 249 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 250 | PyObject_HEAD |
| 251 | Tcl_Interp *interp; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 252 | } TkappObject; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 253 | |
| 254 | #define Tkapp_Check(v) ((v)->ob_type == &Tkapp_Type) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 255 | #define Tkapp_Interp(v) (((TkappObject *) (v))->interp) |
| 256 | #define Tkapp_Result(v) (((TkappObject *) (v))->interp->result) |
| 257 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 258 | #define DEBUG_REFCNT(v) (printf("DEBUG: id=%p, refcnt=%i\n", \ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 259 | (void *) v, ((PyObject *) v)->ob_refcnt)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 260 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 261 | |
| 262 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 263 | /**** Error Handling ****/ |
| 264 | |
| 265 | static PyObject *Tkinter_TclError; |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 266 | static int quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 267 | static int errorInCmd = 0; |
| 268 | static PyObject *excInCmd; |
| 269 | static PyObject *valInCmd; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 270 | static PyObject *trbInCmd; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 271 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 272 | |
| 273 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 274 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 275 | Tkinter_Error(v) |
| 276 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 277 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 278 | PyErr_SetString(Tkinter_TclError, Tkapp_Result(v)); |
| 279 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 282 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 284 | /**** Utils ****/ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 285 | |
| 286 | #ifdef WITH_THREAD |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 287 | #ifndef MS_WINDOWS |
Guido van Rossum | 541f241 | 1998-08-13 13:29:22 +0000 | [diff] [blame] | 288 | #include "mytime.h" |
| 289 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 290 | /* Millisecond sleep() for Unix platforms. */ |
| 291 | |
| 292 | static void |
| 293 | Sleep(milli) |
| 294 | int milli; |
| 295 | { |
| 296 | /* XXX Too bad if you don't have select(). */ |
| 297 | struct timeval t; |
| 298 | double frac; |
| 299 | t.tv_sec = milli/1000; |
| 300 | t.tv_usec = (milli%1000) * 1000; |
| 301 | select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 302 | } |
Guido van Rossum | 2a5119b | 1998-05-29 01:28:40 +0000 | [diff] [blame] | 303 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 304 | #endif /* WITH_THREAD */ |
| 305 | |
| 306 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 307 | static char * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 308 | AsString(value, tmp) |
| 309 | PyObject *value; |
| 310 | PyObject *tmp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 311 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 312 | if (PyString_Check(value)) |
| 313 | return PyString_AsString(value); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 314 | else { |
| 315 | PyObject *v = PyObject_Str(value); |
| 316 | PyList_Append(tmp, v); |
| 317 | Py_DECREF(v); |
| 318 | return PyString_AsString(v); |
| 319 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 322 | |
| 323 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 324 | #define ARGSZ 64 |
| 325 | |
| 326 | static char * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 327 | Merge(args) |
| 328 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 329 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 330 | PyObject *tmp = NULL; |
| 331 | char *argvStore[ARGSZ]; |
| 332 | char **argv = NULL; |
| 333 | int fvStore[ARGSZ]; |
| 334 | int *fv = NULL; |
| 335 | int argc = 0, i; |
| 336 | char *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 337 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 338 | if (!(tmp = PyList_New(0))) |
| 339 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 340 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 341 | argv = argvStore; |
| 342 | fv = fvStore; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 343 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 344 | if (args == NULL) |
| 345 | argc = 0; |
| 346 | |
| 347 | else if (!PyTuple_Check(args)) { |
| 348 | argc = 1; |
| 349 | fv[0] = 0; |
| 350 | argv[0] = AsString(args, tmp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 351 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 352 | else { |
| 353 | argc = PyTuple_Size(args); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 354 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 355 | if (argc > ARGSZ) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 356 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 357 | fv = (int *)ckalloc(argc * sizeof(int)); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 358 | if (argv == NULL || fv == NULL) { |
| 359 | PyErr_NoMemory(); |
| 360 | goto finally; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | for (i = 0; i < argc; i++) { |
| 365 | PyObject *v = PyTuple_GetItem(args, i); |
| 366 | if (PyTuple_Check(v)) { |
| 367 | fv[i] = 1; |
| 368 | if (!(argv[i] = Merge(v))) |
| 369 | goto finally; |
| 370 | } |
| 371 | else if (v == Py_None) { |
| 372 | argc = i; |
| 373 | break; |
| 374 | } |
| 375 | else { |
| 376 | fv[i] = 0; |
| 377 | argv[i] = AsString(v, tmp); |
| 378 | } |
| 379 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 380 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 381 | res = Tcl_Merge(argc, argv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 382 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 383 | finally: |
| 384 | for (i = 0; i < argc; i++) |
| 385 | if (fv[i]) { |
| 386 | ckfree(argv[i]); |
| 387 | } |
| 388 | if (argv != argvStore) |
| 389 | ckfree(FREECAST argv); |
| 390 | if (fv != fvStore) |
| 391 | ckfree(FREECAST fv); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 392 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 393 | Py_DECREF(tmp); |
| 394 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 397 | |
| 398 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 399 | static PyObject * |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 400 | Split(list) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 401 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 402 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 403 | int argc; |
| 404 | char **argv; |
| 405 | PyObject *v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 406 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 407 | if (list == NULL) { |
| 408 | Py_INCREF(Py_None); |
| 409 | return Py_None; |
| 410 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 412 | if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 413 | /* Not a list. |
| 414 | * Could be a quoted string containing funnies, e.g. {"}. |
| 415 | * Return the string itself. |
| 416 | */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 417 | return PyString_FromString(list); |
| 418 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 419 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 420 | if (argc == 0) |
| 421 | v = PyString_FromString(""); |
| 422 | else if (argc == 1) |
| 423 | v = PyString_FromString(argv[0]); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 424 | else if ((v = PyTuple_New(argc)) != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 425 | int i; |
| 426 | PyObject *w; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 427 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 428 | for (i = 0; i < argc; i++) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 429 | if ((w = Split(argv[i])) == NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 430 | Py_DECREF(v); |
| 431 | v = NULL; |
| 432 | break; |
| 433 | } |
| 434 | PyTuple_SetItem(v, i, w); |
| 435 | } |
| 436 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 437 | Tcl_Free(FREECAST argv); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 438 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 441 | |
| 442 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 443 | /**** Tkapp Object ****/ |
| 444 | |
| 445 | #ifndef WITH_APPINIT |
| 446 | int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 447 | Tcl_AppInit(interp) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 448 | Tcl_Interp *interp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 449 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 450 | Tk_Window main; |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 451 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 452 | main = Tk_MainWindow(interp); |
| 453 | if (Tcl_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 454 | PySys_WriteStderr("Tcl_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 455 | return TCL_ERROR; |
| 456 | } |
| 457 | if (Tk_Init(interp) == TCL_ERROR) { |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 458 | PySys_WriteStderr("Tk_Init error: %s\n", interp->result); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 459 | return TCL_ERROR; |
| 460 | } |
| 461 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 462 | } |
| 463 | #endif /* !WITH_APPINIT */ |
| 464 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 465 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 466 | |
| 467 | |
| 468 | /* Initialize the Tk application; see the `main' function in |
| 469 | * `tkMain.c'. |
| 470 | */ |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 471 | |
| 472 | static void EnableEventHook(); /* Forward */ |
| 473 | static void DisableEventHook(); /* Forward */ |
| 474 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 475 | static TkappObject * |
| 476 | Tkapp_New(screenName, baseName, className, interactive) |
| 477 | char *screenName; |
| 478 | char *baseName; |
| 479 | char *className; |
| 480 | int interactive; |
| 481 | { |
| 482 | TkappObject *v; |
| 483 | char *argv0; |
| 484 | |
| 485 | v = PyObject_NEW(TkappObject, &Tkapp_Type); |
| 486 | if (v == NULL) |
| 487 | return NULL; |
| 488 | |
| 489 | v->interp = Tcl_CreateInterp(); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 491 | #if defined(macintosh) && TKMAJORMINOR >= 8000 |
| 492 | /* This seems to be needed since Tk 8.0 */ |
| 493 | ClearMenuBar(); |
| 494 | TkMacInitMenus(v->interp); |
| 495 | #endif |
Guido van Rossum | 1c0d315 | 1998-02-19 21:28:49 +0000 | [diff] [blame] | 496 | /* Delete the 'exit' command, which can screw things up */ |
| 497 | Tcl_DeleteCommand(v->interp, "exit"); |
| 498 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 499 | if (screenName != NULL) |
| 500 | Tcl_SetVar2(v->interp, "env", "DISPLAY", |
| 501 | screenName, TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 502 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 503 | if (interactive) |
| 504 | Tcl_SetVar(v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY); |
| 505 | else |
| 506 | Tcl_SetVar(v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 507 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 508 | /* This is used to get the application class for Tk 4.1 and up */ |
| 509 | argv0 = (char*)ckalloc(strlen(className) + 1); |
| 510 | if (!argv0) { |
| 511 | PyErr_NoMemory(); |
| 512 | Py_DECREF(v); |
| 513 | return NULL; |
| 514 | } |
Guido van Rossum | 97867b2 | 1996-08-08 19:09:53 +0000 | [diff] [blame] | 515 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 516 | strcpy(argv0, className); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 517 | if (isupper((int)(argv0[0]))) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 518 | argv0[0] = tolower(argv0[0]); |
| 519 | Tcl_SetVar(v->interp, "argv0", argv0, TCL_GLOBAL_ONLY); |
| 520 | ckfree(argv0); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 521 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 522 | if (Tcl_AppInit(v->interp) != TCL_OK) |
Guido van Rossum | c821d1e | 1998-07-07 22:25:47 +0000 | [diff] [blame] | 523 | return (TkappObject *)Tkinter_Error((PyObject *)v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 525 | EnableEventHook(); |
| 526 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 527 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 530 | |
| 531 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 532 | /** Tcl Eval **/ |
| 533 | |
| 534 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 535 | Tkapp_Call(self, args) |
| 536 | PyObject *self; |
| 537 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 538 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 539 | /* This is copied from Merge() */ |
| 540 | PyObject *tmp = NULL; |
| 541 | char *argvStore[ARGSZ]; |
| 542 | char **argv = NULL; |
| 543 | int fvStore[ARGSZ]; |
| 544 | int *fv = NULL; |
| 545 | int argc = 0, i; |
| 546 | PyObject *res = NULL; /* except this has a different type */ |
| 547 | Tcl_CmdInfo info; /* and this is added */ |
| 548 | Tcl_Interp *interp = Tkapp_Interp(self); /* and this too */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 550 | if (!(tmp = PyList_New(0))) |
| 551 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 553 | argv = argvStore; |
| 554 | fv = fvStore; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 556 | if (args == NULL) |
| 557 | argc = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 559 | else if (!PyTuple_Check(args)) { |
| 560 | argc = 1; |
| 561 | fv[0] = 0; |
| 562 | argv[0] = AsString(args, tmp); |
| 563 | } |
| 564 | else { |
| 565 | argc = PyTuple_Size(args); |
| 566 | |
| 567 | if (argc > ARGSZ) { |
| 568 | argv = (char **)ckalloc(argc * sizeof(char *)); |
| 569 | fv = (int *)ckalloc(argc * sizeof(int)); |
| 570 | if (argv == NULL || fv == NULL) { |
| 571 | PyErr_NoMemory(); |
| 572 | goto finally; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | for (i = 0; i < argc; i++) { |
| 577 | PyObject *v = PyTuple_GetItem(args, i); |
| 578 | if (PyTuple_Check(v)) { |
| 579 | fv[i] = 1; |
| 580 | if (!(argv[i] = Merge(v))) |
| 581 | goto finally; |
| 582 | } |
| 583 | else if (v == Py_None) { |
| 584 | argc = i; |
| 585 | break; |
| 586 | } |
| 587 | else { |
| 588 | fv[i] = 0; |
| 589 | argv[i] = AsString(v, tmp); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | /* End code copied from Merge() */ |
| 594 | |
| 595 | /* All this to avoid a call to Tcl_Merge() and the corresponding call |
| 596 | to Tcl_SplitList() inside Tcl_Eval()... It can save a bundle! */ |
| 597 | if (Py_VerboseFlag >= 2) { |
| 598 | for (i = 0; i < argc; i++) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 599 | PySys_WriteStderr("%s ", argv[i]); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 600 | } |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 601 | ENTER_TCL |
| 602 | info.proc = NULL; |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 603 | if (argc < 1 || |
| 604 | !Tcl_GetCommandInfo(interp, argv[0], &info) || |
| 605 | info.proc == NULL) |
| 606 | { |
| 607 | char *cmd; |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 608 | cmd = Tcl_Merge(argc, argv); |
| 609 | i = Tcl_Eval(interp, cmd); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 610 | ckfree(cmd); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 611 | } |
| 612 | else { |
| 613 | Tcl_ResetResult(interp); |
| 614 | i = (*info.proc)(info.clientData, interp, argc, argv); |
| 615 | } |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 616 | ENTER_OVERLAP |
| 617 | if (info.proc == NULL && Py_VerboseFlag >= 2) |
| 618 | PySys_WriteStderr("... use TclEval "); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 619 | if (i == TCL_ERROR) { |
| 620 | if (Py_VerboseFlag >= 2) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 621 | PySys_WriteStderr("... error: '%s'\n", |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 622 | interp->result); |
| 623 | Tkinter_Error(self); |
| 624 | } |
| 625 | else { |
| 626 | if (Py_VerboseFlag >= 2) |
Guido van Rossum | b41addf | 1998-05-12 15:02:41 +0000 | [diff] [blame] | 627 | PySys_WriteStderr("-> '%s'\n", interp->result); |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 628 | res = PyString_FromString(interp->result); |
| 629 | } |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 630 | LEAVE_OVERLAP_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 632 | /* Copied from Merge() again */ |
| 633 | finally: |
| 634 | for (i = 0; i < argc; i++) |
| 635 | if (fv[i]) { |
| 636 | ckfree(argv[i]); |
| 637 | } |
| 638 | if (argv != argvStore) |
| 639 | ckfree(FREECAST argv); |
| 640 | if (fv != fvStore) |
| 641 | ckfree(FREECAST fv); |
| 642 | |
| 643 | Py_DECREF(tmp); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 644 | return res; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | static PyObject * |
| 649 | Tkapp_GlobalCall(self, args) |
| 650 | PyObject *self; |
| 651 | PyObject *args; |
| 652 | { |
Guido van Rossum | 212643f | 1998-04-29 16:22:14 +0000 | [diff] [blame] | 653 | /* Could do the same here as for Tkapp_Call(), but this is not used |
| 654 | much, so I can't be bothered. Unfortunately Tcl doesn't export a |
| 655 | way for the user to do what all its Global* variants do (save and |
| 656 | reset the scope pointer, call the local version, restore the saved |
| 657 | scope pointer). */ |
| 658 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 659 | char *cmd; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 660 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 662 | cmd = Merge(args); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 663 | if (!cmd) |
| 664 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 665 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 666 | else { |
| 667 | int err; |
| 668 | ENTER_TCL |
| 669 | err = Tcl_GlobalEval(Tkapp_Interp(self), cmd); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 670 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 671 | if (err == TCL_ERROR) |
| 672 | res = Tkinter_Error(self); |
| 673 | else |
| 674 | res = PyString_FromString(Tkapp_Result(self)); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 675 | LEAVE_OVERLAP_TCL |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 676 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 677 | |
| 678 | if (cmd) |
| 679 | ckfree(cmd); |
| 680 | |
| 681 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 685 | Tkapp_Eval(self, args) |
| 686 | PyObject *self; |
| 687 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 688 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 689 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 690 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 691 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 693 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 694 | return NULL; |
| 695 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 696 | ENTER_TCL |
| 697 | err = Tcl_Eval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 698 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 699 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 700 | res = Tkinter_Error(self); |
| 701 | else |
| 702 | res = PyString_FromString(Tkapp_Result(self)); |
| 703 | LEAVE_OVERLAP_TCL |
| 704 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 708 | Tkapp_GlobalEval(self, args) |
| 709 | PyObject *self; |
| 710 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 711 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 712 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 713 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 714 | int err; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 716 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 717 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 719 | ENTER_TCL |
| 720 | err = Tcl_GlobalEval(Tkapp_Interp(self), script); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 721 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 722 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 723 | res = Tkinter_Error(self); |
| 724 | else |
| 725 | res = PyString_FromString(Tkapp_Result(self)); |
| 726 | LEAVE_OVERLAP_TCL |
| 727 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 731 | Tkapp_EvalFile(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 732 | PyObject *self; |
| 733 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 734 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 735 | char *fileName; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 736 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 737 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 739 | if (!PyArg_ParseTuple(args, "s", &fileName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 740 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 741 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 742 | ENTER_TCL |
| 743 | err = Tcl_EvalFile(Tkapp_Interp(self), fileName); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 744 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 745 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 746 | res = Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 747 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 748 | else |
| 749 | res = PyString_FromString(Tkapp_Result(self)); |
| 750 | LEAVE_OVERLAP_TCL |
| 751 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 755 | Tkapp_Record(self, args) |
| 756 | PyObject *self; |
| 757 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 758 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 759 | char *script; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 760 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 761 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 763 | if (!PyArg_ParseTuple(args, "s", &script)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 764 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 765 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 766 | ENTER_TCL |
| 767 | err = Tcl_RecordAndEval(Tkapp_Interp(self), script, TCL_NO_EVAL); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 768 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 769 | if (err == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 770 | res = Tkinter_Error(self); |
| 771 | else |
| 772 | res = PyString_FromString(Tkapp_Result(self)); |
| 773 | LEAVE_OVERLAP_TCL |
| 774 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 778 | Tkapp_AddErrorInfo(self, args) |
| 779 | PyObject *self; |
| 780 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 781 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 782 | char *msg; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 784 | if (!PyArg_ParseTuple(args, "s", &msg)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 785 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 786 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 787 | Tcl_AddErrorInfo(Tkapp_Interp(self), msg); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 788 | LEAVE_TCL |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 789 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 790 | Py_INCREF(Py_None); |
| 791 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 794 | |
| 795 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 796 | /** Tcl Variable **/ |
| 797 | |
| 798 | static PyObject * |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 799 | SetVar(self, args, flags) |
| 800 | PyObject *self; |
| 801 | PyObject *args; |
| 802 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 803 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 804 | char *name1, *name2, *ok, *s; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 805 | PyObject *newValue; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 806 | PyObject *tmp; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 808 | tmp = PyList_New(0); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 809 | if (!tmp) |
| 810 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 812 | if (PyArg_ParseTuple(args, "sO", &name1, &newValue)) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 813 | /* XXX Merge? */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 814 | s = AsString(newValue, tmp); |
| 815 | ENTER_TCL |
| 816 | ok = Tcl_SetVar(Tkapp_Interp(self), name1, s, flags); |
| 817 | LEAVE_TCL |
| 818 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 819 | else { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 820 | PyErr_Clear(); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 821 | if (PyArg_ParseTuple(args, "ssO", &name1, &name2, &newValue)) { |
| 822 | s = AsString (newValue, tmp); |
| 823 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 824 | ok = Tcl_SetVar2(Tkapp_Interp(self), name1, name2, |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 825 | s, flags); |
| 826 | LEAVE_TCL |
| 827 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 828 | else { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 829 | Py_DECREF(tmp); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 830 | return NULL; |
| 831 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 832 | } |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 833 | Py_DECREF(tmp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 834 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 835 | if (!ok) |
| 836 | return Tkinter_Error(self); |
| 837 | |
| 838 | Py_INCREF(Py_None); |
| 839 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 843 | Tkapp_SetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 844 | PyObject *self; |
| 845 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 846 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 847 | return SetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 851 | Tkapp_GlobalSetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 852 | PyObject *self; |
| 853 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 854 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 855 | return SetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 858 | |
| 859 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 860 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 861 | GetVar(self, args, flags) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 862 | PyObject *self; |
| 863 | PyObject *args; |
| 864 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 865 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 866 | char *name1, *name2=NULL, *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 867 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 868 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 869 | if (!PyArg_ParseTuple(args, "s|s", &name1, &name2)) |
| 870 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 871 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 872 | if (name2 == NULL) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 873 | s = Tcl_GetVar(Tkapp_Interp(self), name1, flags); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 874 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 875 | else |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 876 | s = Tcl_GetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 877 | ENTER_OVERLAP |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 878 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 879 | if (s == NULL) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 880 | res = Tkinter_Error(self); |
| 881 | else |
| 882 | res = PyString_FromString(s); |
| 883 | LEAVE_OVERLAP_TCL |
| 884 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 888 | Tkapp_GetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 889 | PyObject *self; |
| 890 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 891 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 892 | return GetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 896 | Tkapp_GlobalGetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 897 | PyObject *self; |
| 898 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 899 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 900 | return GetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 903 | |
| 904 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 905 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 906 | UnsetVar(self, args, flags) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 907 | PyObject *self; |
| 908 | PyObject *args; |
| 909 | int flags; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 910 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 911 | char *name1, *name2=NULL; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 912 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 913 | int code; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 915 | if (!PyArg_ParseTuple(args, "s|s", &name1, &name2)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 916 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 917 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 918 | if (name2 == NULL) |
| 919 | code = Tcl_UnsetVar(Tkapp_Interp(self), name1, flags); |
| 920 | |
| 921 | else |
| 922 | code = Tcl_UnsetVar2(Tkapp_Interp(self), name1, name2, flags); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 923 | ENTER_OVERLAP |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 924 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 925 | if (code == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 926 | res = Tkinter_Error(self); |
| 927 | else { |
| 928 | Py_INCREF(Py_None); |
| 929 | res = Py_None; |
| 930 | } |
| 931 | LEAVE_OVERLAP_TCL |
| 932 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 936 | Tkapp_UnsetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 937 | PyObject *self; |
| 938 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 939 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 940 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 944 | Tkapp_GlobalUnsetVar(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 945 | PyObject *self; |
| 946 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 947 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 948 | return UnsetVar(self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 951 | |
| 952 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 953 | /** Tcl to Python **/ |
| 954 | |
| 955 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 956 | Tkapp_GetInt(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 957 | PyObject *self; |
| 958 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 959 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 960 | char *s; |
| 961 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 963 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 964 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 965 | if (Tcl_GetInt(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 966 | return Tkinter_Error(self); |
| 967 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 971 | Tkapp_GetDouble(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 972 | PyObject *self; |
| 973 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 974 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 975 | char *s; |
| 976 | double v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 977 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 978 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 979 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 980 | if (Tcl_GetDouble(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 981 | return Tkinter_Error(self); |
| 982 | return Py_BuildValue("d", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 986 | Tkapp_GetBoolean(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 987 | PyObject *self; |
| 988 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 989 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 990 | char *s; |
| 991 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 992 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 993 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 994 | return NULL; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 995 | if (Tcl_GetBoolean(Tkapp_Interp(self), s, &v) == TCL_ERROR) |
| 996 | return Tkinter_Error(self); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 997 | return Py_BuildValue("i", v); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1001 | Tkapp_ExprString(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1002 | PyObject *self; |
| 1003 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1004 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1005 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1006 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1007 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1008 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1009 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1010 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1011 | ENTER_TCL |
| 1012 | retval = Tcl_ExprString(Tkapp_Interp(self), s); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1013 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1014 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1015 | res = Tkinter_Error(self); |
| 1016 | else |
| 1017 | res = Py_BuildValue("s", Tkapp_Result(self)); |
| 1018 | LEAVE_OVERLAP_TCL |
| 1019 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1023 | Tkapp_ExprLong(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1024 | PyObject *self; |
| 1025 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1026 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1027 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1028 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1029 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1030 | long v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1031 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1032 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1033 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1034 | ENTER_TCL |
| 1035 | retval = Tcl_ExprLong(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1036 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1037 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1038 | res = Tkinter_Error(self); |
| 1039 | else |
| 1040 | res = Py_BuildValue("l", v); |
| 1041 | LEAVE_OVERLAP_TCL |
| 1042 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1046 | Tkapp_ExprDouble(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1047 | PyObject *self; |
| 1048 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1049 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1050 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1051 | PyObject *res = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1052 | double v; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1053 | int retval; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1054 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1055 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1056 | return NULL; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1057 | PyFPE_START_PROTECT("Tkapp_ExprDouble", return 0) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1058 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1059 | retval = Tcl_ExprDouble(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1060 | ENTER_OVERLAP |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1061 | PyFPE_END_PROTECT(retval) |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1062 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1063 | res = Tkinter_Error(self); |
| 1064 | else |
| 1065 | res = Py_BuildValue("d", v); |
| 1066 | LEAVE_OVERLAP_TCL |
| 1067 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1071 | Tkapp_ExprBoolean(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1072 | PyObject *self; |
| 1073 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1074 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1075 | char *s; |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1076 | PyObject *res = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1077 | int retval; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1078 | int v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1080 | if (!PyArg_ParseTuple(args, "s", &s)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1081 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1082 | ENTER_TCL |
| 1083 | retval = Tcl_ExprBoolean(Tkapp_Interp(self), s, &v); |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1084 | ENTER_OVERLAP |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1085 | if (retval == TCL_ERROR) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1086 | res = Tkinter_Error(self); |
| 1087 | else |
| 1088 | res = Py_BuildValue("i", v); |
| 1089 | LEAVE_OVERLAP_TCL |
| 1090 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1093 | |
| 1094 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1095 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1096 | Tkapp_SplitList(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1097 | PyObject *self; |
| 1098 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1099 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1100 | char *list; |
| 1101 | int argc; |
| 1102 | char **argv; |
| 1103 | PyObject *v; |
| 1104 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1105 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1106 | if (!PyArg_ParseTuple(args, "s", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1107 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1108 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1109 | if (Tcl_SplitList(Tkapp_Interp(self), list, &argc, &argv) == TCL_ERROR) |
| 1110 | return Tkinter_Error(self); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1111 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1112 | if (!(v = PyTuple_New(argc))) |
| 1113 | return NULL; |
| 1114 | |
| 1115 | for (i = 0; i < argc; i++) { |
| 1116 | PyObject *s = PyString_FromString(argv[i]); |
| 1117 | if (!s || PyTuple_SetItem(v, i, s)) { |
| 1118 | Py_DECREF(v); |
| 1119 | v = NULL; |
| 1120 | goto finally; |
| 1121 | } |
| 1122 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1123 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1124 | finally: |
| 1125 | ckfree(FREECAST argv); |
| 1126 | return v; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1130 | Tkapp_Split(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1131 | PyObject *self; |
| 1132 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1133 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1134 | char *list; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1135 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1136 | if (!PyArg_ParseTuple(args, "s", &list)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1137 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1138 | return Split(list); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1142 | Tkapp_Merge(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1143 | PyObject *self; |
| 1144 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1145 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1146 | char *s = Merge(args); |
| 1147 | PyObject *res = NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1148 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1149 | if (s) { |
| 1150 | res = PyString_FromString(s); |
| 1151 | ckfree(s); |
| 1152 | } |
| 1153 | else |
| 1154 | PyErr_SetString(Tkinter_TclError, "merge failed"); |
| 1155 | |
| 1156 | return res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1159 | |
| 1160 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1161 | /** Tcl Command **/ |
| 1162 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1163 | /* Client data struct */ |
| 1164 | typedef struct { |
| 1165 | PyThreadState *tstate; |
| 1166 | PyObject *self; |
| 1167 | PyObject *func; |
| 1168 | } PythonCmd_ClientData; |
| 1169 | |
| 1170 | static int |
| 1171 | PythonCmd_Error(interp) |
| 1172 | Tcl_Interp *interp; |
| 1173 | { |
| 1174 | errorInCmd = 1; |
| 1175 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1176 | LEAVE_PYTHON |
| 1177 | return TCL_ERROR; |
| 1178 | } |
| 1179 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1180 | /* This is the Tcl command that acts as a wrapper for Python |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1181 | * function or method. |
| 1182 | */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1183 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1184 | PythonCmd(clientData, interp, argc, argv) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1185 | ClientData clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1186 | Tcl_Interp *interp; |
| 1187 | int argc; |
| 1188 | char *argv[]; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1189 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1190 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1191 | PyObject *self, *func, *arg, *res, *tmp; |
| 1192 | int i; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1193 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1194 | /* XXX Should create fresh thread state? */ |
| 1195 | ENTER_PYTHON(data->tstate) |
| 1196 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1197 | /* TBD: no error checking here since we know, via the |
| 1198 | * Tkapp_CreateCommand() that the client data is a two-tuple |
| 1199 | */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1200 | self = data->self; |
| 1201 | func = data->func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1202 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1203 | /* Create argument list (argv1, ..., argvN) */ |
| 1204 | if (!(arg = PyTuple_New(argc - 1))) |
| 1205 | return PythonCmd_Error(interp); |
Guido van Rossum | d308e2b | 1994-07-07 09:25:12 +0000 | [diff] [blame] | 1206 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1207 | for (i = 0; i < (argc - 1); i++) { |
| 1208 | PyObject *s = PyString_FromString(argv[i + 1]); |
| 1209 | if (!s || PyTuple_SetItem(arg, i, s)) { |
| 1210 | Py_DECREF(arg); |
Guido van Rossum | f766e23 | 1998-06-19 04:28:10 +0000 | [diff] [blame] | 1211 | return PythonCmd_Error(interp); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | res = PyEval_CallObject(func, arg); |
| 1215 | Py_DECREF(arg); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1216 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1217 | if (res == NULL) |
| 1218 | return PythonCmd_Error(interp); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1219 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1220 | if (!(tmp = PyList_New(0))) { |
| 1221 | Py_DECREF(res); |
| 1222 | return PythonCmd_Error(interp); |
| 1223 | } |
| 1224 | |
| 1225 | Tcl_SetResult(Tkapp_Interp(self), AsString(res, tmp), TCL_VOLATILE); |
| 1226 | Py_DECREF(res); |
| 1227 | Py_DECREF(tmp); |
| 1228 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1229 | LEAVE_PYTHON |
| 1230 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1231 | return TCL_OK; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1235 | PythonCmdDelete(clientData) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1236 | ClientData clientData; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1237 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1238 | PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData; |
| 1239 | |
| 1240 | ENTER_PYTHON(data->tstate) |
| 1241 | Py_XDECREF(data->self); |
| 1242 | Py_XDECREF(data->func); |
| 1243 | PyMem_DEL(data); |
| 1244 | LEAVE_PYTHON |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1247 | |
| 1248 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1249 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1250 | Tkapp_CreateCommand(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1251 | PyObject *self; |
| 1252 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1253 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1254 | PythonCmd_ClientData *data; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1255 | char *cmdName; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1256 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1257 | Tcl_Command err; |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (!PyArg_ParseTuple(args, "sO", &cmdName, &func)) |
| 1260 | return NULL; |
| 1261 | if (!PyCallable_Check(func)) { |
| 1262 | PyErr_SetString(PyExc_TypeError, "command not callable"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1263 | return NULL; |
| 1264 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1266 | data = PyMem_NEW(PythonCmd_ClientData, 1); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1267 | if (!data) |
| 1268 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1269 | data->tstate = PyThreadState_Get(); |
| 1270 | Py_XINCREF(self); |
| 1271 | Py_XINCREF(func); |
| 1272 | data->self = self; |
| 1273 | data->func = func; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1274 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1275 | ENTER_TCL |
| 1276 | err = Tcl_CreateCommand(Tkapp_Interp(self), cmdName, PythonCmd, |
| 1277 | (ClientData)data, PythonCmdDelete); |
| 1278 | LEAVE_TCL |
| 1279 | if (err == NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1280 | PyErr_SetString(Tkinter_TclError, "can't create Tcl command"); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1281 | PyMem_DEL(data); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1282 | return NULL; |
| 1283 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1284 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1285 | Py_INCREF(Py_None); |
| 1286 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1289 | |
| 1290 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1291 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1292 | Tkapp_DeleteCommand(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1293 | PyObject *self; |
| 1294 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1295 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1296 | char *cmdName; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1297 | int err; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1298 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1299 | if (!PyArg_ParseTuple(args, "s", &cmdName)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1300 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1301 | ENTER_TCL |
| 1302 | err = Tcl_DeleteCommand(Tkapp_Interp(self), cmdName); |
| 1303 | LEAVE_TCL |
| 1304 | if (err == -1) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1305 | PyErr_SetString(Tkinter_TclError, "can't delete Tcl command"); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | Py_INCREF(Py_None); |
| 1309 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1314 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1315 | /** File Handler **/ |
| 1316 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1317 | typedef struct _fhcdata { |
| 1318 | PyThreadState *tstate; |
| 1319 | PyObject *func; |
| 1320 | PyObject *file; |
| 1321 | int id; |
| 1322 | struct _fhcdata *next; |
| 1323 | } FileHandler_ClientData; |
| 1324 | |
| 1325 | static FileHandler_ClientData *HeadFHCD; |
| 1326 | |
| 1327 | static FileHandler_ClientData * |
| 1328 | NewFHCD(func, file, id) |
| 1329 | PyObject *func; |
| 1330 | PyObject *file; |
| 1331 | int id; |
| 1332 | { |
| 1333 | FileHandler_ClientData *p; |
| 1334 | p = PyMem_NEW(FileHandler_ClientData, 1); |
| 1335 | if (p != NULL) { |
| 1336 | Py_XINCREF(func); |
| 1337 | Py_XINCREF(file); |
| 1338 | p->tstate = PyThreadState_Get(); |
| 1339 | p->func = func; |
| 1340 | p->file = file; |
| 1341 | p->id = id; |
| 1342 | p->next = HeadFHCD; |
| 1343 | HeadFHCD = p; |
| 1344 | } |
| 1345 | return p; |
| 1346 | } |
| 1347 | |
| 1348 | static void |
| 1349 | DeleteFHCD(id) |
| 1350 | int id; |
| 1351 | { |
| 1352 | FileHandler_ClientData *p, **pp; |
| 1353 | |
| 1354 | pp = &HeadFHCD; |
| 1355 | while ((p = *pp) != NULL) { |
| 1356 | if (p->id == id) { |
| 1357 | *pp = p->next; |
| 1358 | Py_XDECREF(p->func); |
| 1359 | Py_XDECREF(p->file); |
| 1360 | PyMem_DEL(p); |
| 1361 | } |
| 1362 | else |
| 1363 | pp = &p->next; |
| 1364 | } |
| 1365 | } |
| 1366 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1367 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1368 | FileHandler(clientData, mask) |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1369 | ClientData clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1370 | int mask; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1371 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1372 | FileHandler_ClientData *data = (FileHandler_ClientData *)clientData; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1373 | PyObject *func, *file, *arg, *res; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1374 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1375 | /* XXX Should create fresh thread state? */ |
| 1376 | ENTER_PYTHON(data->tstate) |
| 1377 | func = data->func; |
| 1378 | file = data->file; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1379 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1380 | arg = Py_BuildValue("(Oi)", file, (long) mask); |
| 1381 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1382 | Py_DECREF(arg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (res == NULL) { |
| 1385 | errorInCmd = 1; |
| 1386 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1387 | } |
| 1388 | Py_XDECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1389 | LEAVE_PYTHON |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1393 | GetFileNo(file) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1394 | /* Either an int >= 0 or an object with a |
| 1395 | *.fileno() method that returns an int >= 0 |
| 1396 | */ |
| 1397 | PyObject *file; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1398 | { |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 1399 | PyObject *meth, *args, *res; |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1400 | int id; |
| 1401 | if (PyInt_Check(file)) { |
| 1402 | id = PyInt_AsLong(file); |
| 1403 | if (id < 0) |
| 1404 | PyErr_SetString(PyExc_ValueError, "invalid file id"); |
| 1405 | return id; |
| 1406 | } |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1407 | args = PyTuple_New(0); |
| 1408 | if (args == NULL) |
| 1409 | return -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1410 | |
| 1411 | meth = PyObject_GetAttrString(file, "fileno"); |
| 1412 | if (meth == NULL) { |
| 1413 | Py_DECREF(args); |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1417 | res = PyEval_CallObject(meth, args); |
| 1418 | Py_DECREF(args); |
| 1419 | Py_DECREF(meth); |
| 1420 | if (res == NULL) |
| 1421 | return -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1422 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1423 | if (PyInt_Check(res)) |
| 1424 | id = PyInt_AsLong(res); |
| 1425 | else |
| 1426 | id = -1; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1427 | |
Guido van Rossum | 9bb4fd6 | 1994-08-09 14:15:19 +0000 | [diff] [blame] | 1428 | if (id < 0) |
| 1429 | PyErr_SetString(PyExc_ValueError, |
| 1430 | "invalid fileno() return value"); |
| 1431 | Py_DECREF(res); |
| 1432 | return id; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1436 | Tkapp_CreateFileHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1437 | PyObject *self; |
| 1438 | PyObject *args; /* Is (file, mask, func) */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1439 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1440 | FileHandler_ClientData *data; |
| 1441 | PyObject *file, *func; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1442 | int mask, id; |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1443 | FHANDLE tfile; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1444 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1445 | if (!PyArg_ParseTuple(args, "OiO", &file, &mask, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1446 | return NULL; |
| 1447 | id = GetFileNo(file); |
| 1448 | if (id < 0) |
| 1449 | return NULL; |
| 1450 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1451 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | } |
| 1454 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1455 | data = NewFHCD(func, file, id); |
| 1456 | if (data == NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1457 | return NULL; |
| 1458 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1459 | tfile = MAKEFHANDLE(id); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1460 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1461 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1462 | Tcl_CreateFileHandler(tfile, mask, FileHandler, (ClientData) data); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1463 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1464 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1465 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1469 | Tkapp_DeleteFileHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1470 | PyObject *self; |
| 1471 | PyObject *args; /* Args: file */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1472 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1473 | PyObject *file; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1474 | FileHandler_ClientData *data; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1475 | int id; |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1476 | FHANDLE tfile; |
Guido van Rossum | 6878436 | 1996-05-16 17:17:31 +0000 | [diff] [blame] | 1477 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1478 | if (!PyArg_ParseTuple(args, "O", &file)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1479 | return NULL; |
| 1480 | id = GetFileNo(file); |
| 1481 | if (id < 0) |
| 1482 | return NULL; |
| 1483 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1484 | DeleteFHCD(id); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1485 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1486 | tfile = MAKEFHANDLE(id); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1487 | /* Ought to check for null Tcl_File object... */ |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1488 | ENTER_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1489 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1490 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1491 | Py_INCREF(Py_None); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1492 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1493 | } |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1494 | #endif /* HAVE_CREATEFILEHANDLER */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1495 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1496 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1497 | /**** Tktt Object (timer token) ****/ |
| 1498 | |
| 1499 | staticforward PyTypeObject Tktt_Type; |
| 1500 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1501 | typedef struct { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1502 | PyObject_HEAD |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1503 | Tcl_TimerToken token; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1504 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1505 | PyThreadState *tstate; |
| 1506 | } TkttObject; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1507 | |
| 1508 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1509 | Tktt_DeleteTimerHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1510 | PyObject *self; |
| 1511 | PyObject *args; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1512 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1513 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1514 | PyObject *func = v->func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1515 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1516 | if (!PyArg_ParseTuple(args, "")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1517 | return NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1518 | if (v->token != NULL) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1519 | Tcl_DeleteTimerHandler(v->token); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1520 | v->token = NULL; |
| 1521 | } |
| 1522 | if (func != NULL) { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1523 | v->func = NULL; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1524 | Py_DECREF(func); |
| 1525 | Py_DECREF(v); /* See Tktt_New() */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | Py_INCREF(Py_None); |
| 1528 | return Py_None; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | static PyMethodDef Tktt_methods[] = |
| 1532 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1533 | {"deletetimerhandler", Tktt_DeleteTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1534 | {NULL, NULL} |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1535 | }; |
| 1536 | |
| 1537 | static TkttObject * |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1538 | Tktt_New(func) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1539 | PyObject *func; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1540 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1541 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1542 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1543 | v = PyObject_NEW(TkttObject, &Tktt_Type); |
| 1544 | if (v == NULL) |
| 1545 | return NULL; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1546 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1547 | Py_INCREF(func); |
| 1548 | v->token = NULL; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1549 | v->func = func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1550 | v->tstate = PyThreadState_Get(); |
| 1551 | |
| 1552 | /* Extra reference, deleted when called or when handler is deleted */ |
| 1553 | Py_INCREF(v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1554 | return v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1558 | Tktt_Dealloc(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1559 | PyObject *self; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1560 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1561 | TkttObject *v = (TkttObject *)self; |
| 1562 | PyObject *func = v->func; |
| 1563 | |
| 1564 | Py_XDECREF(func); |
| 1565 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1566 | PyMem_DEL(self); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1569 | static PyObject * |
| 1570 | Tktt_Repr(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1571 | PyObject *self; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1572 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1573 | TkttObject *v = (TkttObject *)self; |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1574 | char buf[100]; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1576 | sprintf(buf, "<tktimertoken at 0x%lx%s>", (long)v, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1577 | v->func == NULL ? ", handler deleted" : ""); |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1578 | return PyString_FromString(buf); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1582 | Tktt_GetAttr(self, name) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1583 | PyObject *self; |
| 1584 | char *name; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1585 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1586 | return Py_FindMethod(Tktt_methods, self, name); |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | static PyTypeObject Tktt_Type = |
| 1590 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1591 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1592 | 0, /*ob_size */ |
| 1593 | "tktimertoken", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1594 | sizeof(TkttObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1595 | 0, /*tp_itemsize */ |
| 1596 | Tktt_Dealloc, /*tp_dealloc */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1597 | 0, /*tp_print */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1598 | Tktt_GetAttr, /*tp_getattr */ |
| 1599 | 0, /*tp_setattr */ |
| 1600 | 0, /*tp_compare */ |
Guido van Rossum | 597ac20 | 1998-05-12 14:36:19 +0000 | [diff] [blame] | 1601 | Tktt_Repr, /*tp_repr */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1602 | 0, /*tp_as_number */ |
| 1603 | 0, /*tp_as_sequence */ |
| 1604 | 0, /*tp_as_mapping */ |
| 1605 | 0, /*tp_hash */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1606 | }; |
| 1607 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1608 | |
| 1609 | |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1610 | /** Timer Handler **/ |
| 1611 | |
| 1612 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1613 | TimerHandler(clientData) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1614 | ClientData clientData; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1615 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1616 | TkttObject *v = (TkttObject *)clientData; |
| 1617 | PyObject *func = v->func; |
| 1618 | PyObject *res; |
| 1619 | |
| 1620 | if (func == NULL) |
| 1621 | return; |
| 1622 | |
| 1623 | v->func = NULL; |
| 1624 | |
| 1625 | ENTER_PYTHON(v->tstate) |
| 1626 | |
| 1627 | res = PyEval_CallObject(func, NULL); |
| 1628 | Py_DECREF(func); |
| 1629 | Py_DECREF(v); /* See Tktt_New() */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1630 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1631 | if (res == NULL) { |
| 1632 | errorInCmd = 1; |
| 1633 | PyErr_Fetch(&excInCmd, &valInCmd, &trbInCmd); |
| 1634 | } |
| 1635 | else |
| 1636 | Py_DECREF(res); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1637 | |
| 1638 | LEAVE_PYTHON |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1642 | Tkapp_CreateTimerHandler(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1643 | PyObject *self; |
| 1644 | PyObject *args; /* Is (milliseconds, func) */ |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1645 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1646 | int milliseconds; |
| 1647 | PyObject *func; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1648 | TkttObject *v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1649 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1650 | if (!PyArg_ParseTuple(args, "iO", &milliseconds, &func)) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1651 | return NULL; |
| 1652 | if (!PyCallable_Check(func)) { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1653 | PyErr_SetString(PyExc_TypeError, "bad argument list"); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1654 | return NULL; |
| 1655 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1656 | v = Tktt_New(func); |
| 1657 | v->token = Tcl_CreateTimerHandler(milliseconds, TimerHandler, |
| 1658 | (ClientData)v); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1659 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1660 | return (PyObject *) v; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1664 | /** Event Loop **/ |
| 1665 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1666 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1667 | Tkapp_MainLoop(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1668 | PyObject *self; |
| 1669 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1670 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1671 | int threshold = 0; |
Guido van Rossum | f34cadd | 1994-11-10 22:50:21 +0000 | [diff] [blame] | 1672 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1673 | if (!PyArg_ParseTuple(args, "|i", &threshold)) |
| 1674 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1675 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1676 | quitMainLoop = 0; |
| 1677 | while (Tk_GetNumMainWindows() > threshold && |
| 1678 | !quitMainLoop && |
| 1679 | !errorInCmd) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1680 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1681 | int result; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1682 | |
| 1683 | #ifdef WITH_THREAD |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1684 | Py_BEGIN_ALLOW_THREADS |
| 1685 | acquire_lock(tcl_lock, 1); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1686 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1687 | release_lock(tcl_lock); |
| 1688 | if (result == 0) |
| 1689 | Sleep(20); |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1690 | Py_END_ALLOW_THREADS |
Guido van Rossum | 5b02078 | 1997-08-19 01:00:50 +0000 | [diff] [blame] | 1691 | #else |
| 1692 | result = Tcl_DoOneEvent(0); |
| 1693 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1695 | if (PyErr_CheckSignals() != 0) |
| 1696 | return NULL; |
| 1697 | if (result < 0) |
| 1698 | break; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1699 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1700 | quitMainLoop = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1701 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1702 | if (errorInCmd) { |
| 1703 | errorInCmd = 0; |
| 1704 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 1705 | excInCmd = valInCmd = trbInCmd = NULL; |
| 1706 | return NULL; |
| 1707 | } |
| 1708 | Py_INCREF(Py_None); |
| 1709 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1713 | Tkapp_DoOneEvent(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1714 | PyObject *self; |
| 1715 | PyObject *args; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1716 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1717 | int flags = 0; |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1718 | int rv; |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1719 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1720 | if (!PyArg_ParseTuple(args, "|i", &flags)) |
| 1721 | return NULL; |
| 1722 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1723 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1724 | rv = Tcl_DoOneEvent(flags); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1725 | LEAVE_TCL |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1726 | return Py_BuildValue("i", rv); |
Guido van Rossum | 062cfb0 | 1995-01-10 17:42:51 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1730 | Tkapp_Quit(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1731 | PyObject *self; |
| 1732 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1733 | { |
| 1734 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1735 | if (!PyArg_ParseTuple(args, "")) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1736 | return NULL; |
| 1737 | |
| 1738 | quitMainLoop = 1; |
| 1739 | Py_INCREF(Py_None); |
| 1740 | return Py_None; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1743 | static PyObject * |
| 1744 | Tkapp_InterpAddr(self, args) |
| 1745 | PyObject *self; |
| 1746 | PyObject *args; |
| 1747 | { |
| 1748 | |
| 1749 | if (!PyArg_ParseTuple(args, "")) |
| 1750 | return NULL; |
| 1751 | |
| 1752 | return PyInt_FromLong((long)Tkapp_Interp(self)); |
| 1753 | } |
| 1754 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1755 | |
| 1756 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1757 | /**** Tkapp Method List ****/ |
| 1758 | |
| 1759 | static PyMethodDef Tkapp_methods[] = |
| 1760 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1761 | {"call", Tkapp_Call, 0}, |
| 1762 | {"globalcall", Tkapp_GlobalCall, 0}, |
| 1763 | {"eval", Tkapp_Eval, 1}, |
| 1764 | {"globaleval", Tkapp_GlobalEval, 1}, |
| 1765 | {"evalfile", Tkapp_EvalFile, 1}, |
| 1766 | {"record", Tkapp_Record, 1}, |
| 1767 | {"adderrorinfo", Tkapp_AddErrorInfo, 1}, |
| 1768 | {"setvar", Tkapp_SetVar, 1}, |
| 1769 | {"globalsetvar", Tkapp_GlobalSetVar, 1}, |
| 1770 | {"getvar", Tkapp_GetVar, 1}, |
| 1771 | {"globalgetvar", Tkapp_GlobalGetVar, 1}, |
| 1772 | {"unsetvar", Tkapp_UnsetVar, 1}, |
| 1773 | {"globalunsetvar", Tkapp_GlobalUnsetVar, 1}, |
| 1774 | {"getint", Tkapp_GetInt, 1}, |
| 1775 | {"getdouble", Tkapp_GetDouble, 1}, |
| 1776 | {"getboolean", Tkapp_GetBoolean, 1}, |
| 1777 | {"exprstring", Tkapp_ExprString, 1}, |
| 1778 | {"exprlong", Tkapp_ExprLong, 1}, |
| 1779 | {"exprdouble", Tkapp_ExprDouble, 1}, |
| 1780 | {"exprboolean", Tkapp_ExprBoolean, 1}, |
| 1781 | {"splitlist", Tkapp_SplitList, 1}, |
| 1782 | {"split", Tkapp_Split, 1}, |
| 1783 | {"merge", Tkapp_Merge, 0}, |
| 1784 | {"createcommand", Tkapp_CreateCommand, 1}, |
| 1785 | {"deletecommand", Tkapp_DeleteCommand, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1786 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1787 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1788 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1789 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1790 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1791 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1792 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1793 | {"quit", Tkapp_Quit, 1}, |
Guido van Rossum | 9d1b7ae | 1998-04-29 16:17:01 +0000 | [diff] [blame] | 1794 | {"interpaddr", Tkapp_InterpAddr, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1795 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1796 | }; |
| 1797 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1798 | |
| 1799 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1800 | /**** Tkapp Type Methods ****/ |
| 1801 | |
| 1802 | static void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1803 | Tkapp_Dealloc(self) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1804 | PyObject *self; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1805 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1806 | ENTER_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1807 | Tcl_DeleteInterp(Tkapp_Interp(self)); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1808 | LEAVE_TCL |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1809 | PyMem_DEL(self); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1810 | DisableEventHook(); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1814 | Tkapp_GetAttr(self, name) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1815 | PyObject *self; |
| 1816 | char *name; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1817 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1818 | return Py_FindMethod(Tkapp_methods, self, name); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | static PyTypeObject Tkapp_Type = |
| 1822 | { |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1823 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1824 | 0, /*ob_size */ |
| 1825 | "tkapp", /*tp_name */ |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1826 | sizeof(TkappObject), /*tp_basicsize */ |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1827 | 0, /*tp_itemsize */ |
| 1828 | Tkapp_Dealloc, /*tp_dealloc */ |
| 1829 | 0, /*tp_print */ |
| 1830 | Tkapp_GetAttr, /*tp_getattr */ |
| 1831 | 0, /*tp_setattr */ |
| 1832 | 0, /*tp_compare */ |
| 1833 | 0, /*tp_repr */ |
| 1834 | 0, /*tp_as_number */ |
| 1835 | 0, /*tp_as_sequence */ |
| 1836 | 0, /*tp_as_mapping */ |
| 1837 | 0, /*tp_hash */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1838 | }; |
| 1839 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1840 | |
| 1841 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1842 | /**** Tkinter Module ****/ |
| 1843 | |
| 1844 | static PyObject * |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1845 | Tkinter_Create(self, args) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1846 | PyObject *self; |
| 1847 | PyObject *args; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1848 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1849 | char *screenName = NULL; |
| 1850 | char *baseName = NULL; |
| 1851 | char *className = NULL; |
| 1852 | int interactive = 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1853 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1854 | baseName = strrchr(Py_GetProgramName(), '/'); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1855 | if (baseName != NULL) |
| 1856 | baseName++; |
| 1857 | else |
| 1858 | baseName = Py_GetProgramName(); |
| 1859 | className = "Tk"; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1860 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1861 | if (!PyArg_ParseTuple(args, "|zssi", |
| 1862 | &screenName, &baseName, &className, |
| 1863 | &interactive)) |
| 1864 | return NULL; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1865 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1866 | return (PyObject *) Tkapp_New(screenName, baseName, className, |
| 1867 | interactive); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static PyMethodDef moduleMethods[] = |
| 1871 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1872 | {"create", Tkinter_Create, 1}, |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 1873 | #ifdef HAVE_CREATEFILEHANDLER |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1874 | {"createfilehandler", Tkapp_CreateFileHandler, 1}, |
| 1875 | {"deletefilehandler", Tkapp_DeleteFileHandler, 1}, |
Guido van Rossum | 02c0467 | 1997-08-07 00:12:22 +0000 | [diff] [blame] | 1876 | #endif |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1877 | {"createtimerhandler", Tkapp_CreateTimerHandler, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1878 | {"mainloop", Tkapp_MainLoop, 1}, |
| 1879 | {"dooneevent", Tkapp_DoOneEvent, 1}, |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1880 | {"quit", Tkapp_Quit, 1}, |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1881 | {NULL, NULL} |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1882 | }; |
| 1883 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1884 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1885 | |
| 1886 | static int stdin_ready = 0; |
| 1887 | |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1888 | #ifndef MS_WINDOWS |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1889 | static void |
| 1890 | MyFileProc(clientData, mask) |
| 1891 | void *clientData; |
| 1892 | int mask; |
| 1893 | { |
| 1894 | stdin_ready = 1; |
| 1895 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1896 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1897 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1898 | static PyThreadState *event_tstate = NULL; |
Guido van Rossum | 0e8457c | 1997-10-07 18:51:41 +0000 | [diff] [blame] | 1899 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1900 | static int |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 1901 | EventHook() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1902 | { |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1903 | #ifndef MS_WINDOWS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1904 | FHANDLE tfile; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1905 | #endif |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1906 | PyEval_RestoreThread(event_tstate); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1907 | stdin_ready = 0; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1908 | errorInCmd = 0; |
| 1909 | #ifndef MS_WINDOWS |
| 1910 | tfile = MAKEFHANDLE(fileno(stdin)); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1911 | Tcl_CreateFileHandler(tfile, TCL_READABLE, MyFileProc, NULL); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1912 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1913 | while (!errorInCmd && !stdin_ready) { |
| 1914 | int result; |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1915 | #ifdef MS_WINDOWS |
| 1916 | if (_kbhit()) { |
| 1917 | stdin_ready = 1; |
| 1918 | break; |
| 1919 | } |
| 1920 | #endif |
| 1921 | #if defined(WITH_THREAD) || defined(MS_WINDOWS) |
Guido van Rossum | 62320c9 | 1998-06-15 04:36:09 +0000 | [diff] [blame] | 1922 | Py_BEGIN_ALLOW_THREADS |
| 1923 | acquire_lock(tcl_lock, 1); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1924 | result = Tcl_DoOneEvent(TCL_DONT_WAIT); |
| 1925 | release_lock(tcl_lock); |
| 1926 | if (result == 0) |
| 1927 | Sleep(20); |
| 1928 | Py_END_ALLOW_THREADS |
| 1929 | #else |
| 1930 | result = Tcl_DoOneEvent(0); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1931 | #endif |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1932 | |
| 1933 | if (result < 0) |
| 1934 | break; |
| 1935 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1936 | #ifndef MS_WINDOWS |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1937 | Tcl_DeleteFileHandler(tfile); |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1938 | #endif |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1939 | if (errorInCmd) { |
| 1940 | errorInCmd = 0; |
| 1941 | PyErr_Restore(excInCmd, valInCmd, trbInCmd); |
| 1942 | excInCmd = valInCmd = trbInCmd = NULL; |
| 1943 | PyErr_Print(); |
| 1944 | } |
Guido van Rossum | ad4db17 | 1998-06-13 13:56:28 +0000 | [diff] [blame] | 1945 | PyEval_SaveThread(); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1946 | return 0; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1947 | } |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1948 | |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1949 | #endif |
| 1950 | |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1951 | static void |
| 1952 | EnableEventHook() |
| 1953 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1954 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1955 | if (PyOS_InputHook == NULL) { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1956 | event_tstate = PyThreadState_Get(); |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1957 | PyOS_InputHook = EventHook; |
| 1958 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1959 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static void |
| 1963 | DisableEventHook() |
| 1964 | { |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1965 | #ifdef WAIT_FOR_STDIN |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1966 | if (Tk_GetNumMainWindows() == 0 && PyOS_InputHook == EventHook) { |
| 1967 | PyOS_InputHook = NULL; |
| 1968 | } |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 1969 | #endif |
Guido van Rossum | 7bf1564 | 1998-05-22 18:28:17 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* all errors will be checked in one fell swoop in init_tkinter() */ |
| 1974 | static void |
| 1975 | ins_long(d, name, val) |
| 1976 | PyObject *d; |
| 1977 | char *name; |
| 1978 | long val; |
| 1979 | { |
| 1980 | PyObject *v = PyInt_FromLong(val); |
| 1981 | if (v) { |
| 1982 | PyDict_SetItemString(d, name, v); |
| 1983 | Py_DECREF(v); |
| 1984 | } |
| 1985 | } |
| 1986 | static void |
| 1987 | ins_string(d, name, val) |
| 1988 | PyObject *d; |
| 1989 | char *name; |
| 1990 | char *val; |
| 1991 | { |
| 1992 | PyObject *v = PyString_FromString(val); |
| 1993 | if (v) { |
| 1994 | PyDict_SetItemString(d, name, v); |
| 1995 | Py_DECREF(v); |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2000 | void |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2001 | init_tkinter() |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2002 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2003 | PyObject *m, *d; |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2004 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2005 | Tkapp_Type.ob_type = &PyType_Type; |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2006 | |
| 2007 | #ifdef WITH_THREAD |
| 2008 | tcl_lock = allocate_lock(); |
| 2009 | #endif |
Guido van Rossum | ae92f01 | 1996-08-21 19:03:36 +0000 | [diff] [blame] | 2010 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2011 | m = Py_InitModule("_tkinter", moduleMethods); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2012 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2013 | d = PyModule_GetDict(m); |
| 2014 | Tkinter_TclError = Py_BuildValue("s", "TclError"); |
| 2015 | PyDict_SetItemString(d, "TclError", Tkinter_TclError); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2016 | |
Guido van Rossum | 35d4337 | 1997-08-02 00:09:09 +0000 | [diff] [blame] | 2017 | ins_long(d, "READABLE", TCL_READABLE); |
| 2018 | ins_long(d, "WRITABLE", TCL_WRITABLE); |
| 2019 | ins_long(d, "EXCEPTION", TCL_EXCEPTION); |
| 2020 | ins_long(d, "WINDOW_EVENTS", TCL_WINDOW_EVENTS); |
| 2021 | ins_long(d, "FILE_EVENTS", TCL_FILE_EVENTS); |
| 2022 | ins_long(d, "TIMER_EVENTS", TCL_TIMER_EVENTS); |
| 2023 | ins_long(d, "IDLE_EVENTS", TCL_IDLE_EVENTS); |
| 2024 | ins_long(d, "ALL_EVENTS", TCL_ALL_EVENTS); |
| 2025 | ins_long(d, "DONT_WAIT", TCL_DONT_WAIT); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2026 | ins_string(d, "TK_VERSION", TK_VERSION); |
| 2027 | ins_string(d, "TCL_VERSION", TCL_VERSION); |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 2029 | PyDict_SetItemString(d, "TkappType", (PyObject *)&Tkapp_Type); |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2030 | |
| 2031 | Tktt_Type.ob_type = &PyType_Type; |
Guido van Rossum | 83551bf | 1997-09-13 00:44:23 +0000 | [diff] [blame] | 2032 | PyDict_SetItemString(d, "TkttType", (PyObject *)&Tktt_Type); |
| 2033 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2034 | if (PyErr_Occurred()) |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2035 | return; |
| 2036 | |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 2037 | #if 0 |
| 2038 | /* This was not a good idea; through <Destroy> bindings, |
| 2039 | Tcl_Finalize() may invoke Python code but at that point the |
| 2040 | interpreter and thread state have already been destroyed! */ |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 2041 | #if TKMAJORMINOR >= 8000 |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2042 | Py_AtExit(Tcl_Finalize); |
Guido van Rossum | 2621637 | 1998-04-20 18:47:52 +0000 | [diff] [blame] | 2043 | #endif |
Guido van Rossum | 43ff868 | 1998-07-14 18:02:13 +0000 | [diff] [blame] | 2044 | #endif |
Guido van Rossum | 07e9fbf | 1998-02-06 22:35:46 +0000 | [diff] [blame] | 2045 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2046 | #ifdef macintosh |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2047 | /* |
| 2048 | ** Part of this code is stolen from MacintoshInit in tkMacAppInit. |
| 2049 | ** Most of the initializations in that routine (toolbox init calls and |
| 2050 | ** such) have already been done for us, so we only need these. |
| 2051 | */ |
| 2052 | #if TKMAJORMINOR >= 8000 |
| 2053 | tcl_macQdPtr = &qd; |
| 2054 | #endif |
| 2055 | |
| 2056 | Tcl_MacSetEventProc(PyMacConvertEvent); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2057 | #if GENERATINGCFM |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2058 | mac_addlibresources(); |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2059 | #endif /* GENERATINGCFM */ |
| 2060 | #endif /* macintosh */ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 2061 | } |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2062 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2063 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2064 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2065 | #ifdef macintosh |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2066 | |
| 2067 | /* |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2068 | ** Anyone who embeds Tcl/Tk on the Mac must define panic(). |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2069 | */ |
| 2070 | |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2071 | void |
| 2072 | panic(char * format, ...) |
| 2073 | { |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2074 | va_list varg; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2075 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2076 | va_start(varg, format); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2077 | |
Guido van Rossum | 227cf76 | 1998-08-05 13:53:32 +0000 | [diff] [blame] | 2078 | vfprintf(stderr, format, varg); |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2079 | (void) fflush(stderr); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2080 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2081 | va_end(varg); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2082 | |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2083 | Py_FatalError("Tcl/Tk panic"); |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2084 | } |
Jack Jansen | 40b546d | 1995-11-14 10:34:45 +0000 | [diff] [blame] | 2085 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2086 | /* |
| 2087 | ** Pass events to SIOUX before passing them to Tk. |
| 2088 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2089 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2090 | static int |
| 2091 | PyMacConvertEvent(eventPtr) |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2092 | EventRecord *eventPtr; |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2093 | { |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 2094 | WindowPtr frontwin; |
| 2095 | /* |
Guido van Rossum | 00d9306 | 1998-05-28 23:06:38 +0000 | [diff] [blame] | 2096 | ** Sioux eats too many events, so we don't pass it everything. We |
| 2097 | ** always pass update events to Sioux, and we only pass other events if |
Guido van Rossum | 2ea1c94 | 1998-04-28 16:12:43 +0000 | [diff] [blame] | 2098 | ** the Sioux window is frontmost. This means that Tk menus don't work |
| 2099 | ** in that case, but at least we can scroll the sioux window. |
| 2100 | ** Note that the SIOUXIsAppWindow() routine we use here is not really |
| 2101 | ** part of the external interface of Sioux... |
| 2102 | */ |
| 2103 | frontwin = FrontWindow(); |
| 2104 | if ( eventPtr->what == updateEvt || SIOUXIsAppWindow(frontwin) ) { |
| 2105 | if (SIOUXHandleOneEvent(eventPtr)) |
| 2106 | return 0; /* Nothing happened to the Tcl event queue */ |
| 2107 | } |
Barry Warsaw | fa701a8 | 1997-01-16 00:15:11 +0000 | [diff] [blame] | 2108 | return TkMacConvertEvent(eventPtr); |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Guido van Rossum | 0d2390c | 1997-08-14 19:57:07 +0000 | [diff] [blame] | 2111 | #if defined(USE_GUSI) && TKMAJORMINOR < 8000 |
Guido van Rossum | 290283b | 1997-06-02 22:16:43 +0000 | [diff] [blame] | 2112 | /* |
| 2113 | * For Python we have to override this routine (from TclMacNotify), |
| 2114 | * since we use GUSI for our sockets, not Tcl streams. Hence, we have |
| 2115 | * to use GUSI select to see whether our socket is ready. Note that |
| 2116 | * createfilehandler (above) sets the type to TCL_UNIX_FD for our |
| 2117 | * files and sockets. |
| 2118 | * |
| 2119 | * NOTE: this code was lifted from Tcl 7.6, it may need to be modified |
| 2120 | * for other versions. */ |
| 2121 | |
| 2122 | int |
| 2123 | Tcl_FileReady(file, mask) |
| 2124 | Tcl_File file; /* File handle for a stream. */ |
| 2125 | int mask; /* OR'ed combination of TCL_READABLE, |
| 2126 | * TCL_WRITABLE, and TCL_EXCEPTION: |
| 2127 | * indicates conditions caller cares about. */ |
| 2128 | { |
| 2129 | int type; |
| 2130 | int fd; |
| 2131 | |
| 2132 | fd = (int) Tcl_GetFileInfo(file, &type); |
| 2133 | |
| 2134 | if (type == TCL_MAC_SOCKET) { |
| 2135 | return TclMacSocketReady(file, mask); |
| 2136 | } else if (type == TCL_MAC_FILE) { |
| 2137 | /* |
| 2138 | * Under the Macintosh, files are always ready, so we just |
| 2139 | * return the mask that was passed in. |
| 2140 | */ |
| 2141 | |
| 2142 | return mask; |
| 2143 | } else if (type == TCL_UNIX_FD) { |
| 2144 | fd_set readset, writeset, excset; |
| 2145 | struct timeval tv; |
| 2146 | |
| 2147 | FD_ZERO(&readset); |
| 2148 | FD_ZERO(&writeset); |
| 2149 | FD_ZERO(&excset); |
| 2150 | |
| 2151 | if ( mask & TCL_READABLE ) FD_SET(fd, &readset); |
| 2152 | if ( mask & TCL_WRITABLE ) FD_SET(fd, &writeset); |
| 2153 | if ( mask & TCL_EXCEPTION ) FD_SET(fd, &excset); |
| 2154 | |
| 2155 | tv.tv_sec = tv.tv_usec = 0; |
| 2156 | if ( select(fd+1, &readset, &writeset, &excset, &tv) <= 0 ) |
| 2157 | return 0; |
| 2158 | |
| 2159 | mask = 0; |
| 2160 | if ( FD_ISSET(fd, &readset) ) mask |= TCL_READABLE; |
| 2161 | if ( FD_ISSET(fd, &writeset) ) mask |= TCL_WRITABLE; |
| 2162 | if ( FD_ISSET(fd, &excset) ) mask |= TCL_EXCEPTION; |
| 2163 | |
| 2164 | return mask; |
| 2165 | } |
| 2166 | |
| 2167 | return 0; |
| 2168 | } |
| 2169 | #endif /* USE_GUSI */ |
| 2170 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2171 | #if GENERATINGCFM |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | ** Additional Mac specific code for dealing with shared libraries. |
| 2175 | */ |
| 2176 | |
| 2177 | #include <Resources.h> |
| 2178 | #include <CodeFragments.h> |
| 2179 | |
| 2180 | static int loaded_from_shlib = 0; |
| 2181 | static FSSpec library_fss; |
Guido van Rossum | 9722ad8 | 1995-09-22 23:49:28 +0000 | [diff] [blame] | 2182 | |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2183 | /* |
| 2184 | ** If this module is dynamically loaded the following routine should |
| 2185 | ** be the init routine. It takes care of adding the shared library to |
| 2186 | ** the resource-file chain, so that the tk routines can find their |
| 2187 | ** resources. |
| 2188 | */ |
| 2189 | OSErr pascal |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2190 | init_tkinter_shlib(CFragInitBlockPtr data) |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2191 | { |
Guido van Rossum | c9970ee | 1996-08-26 14:37:15 +0000 | [diff] [blame] | 2192 | __initialize(); |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2193 | if ( data == nil ) return noErr; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2194 | if ( data->fragLocator.where == kDataForkCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2195 | library_fss = *data->fragLocator.u.onDisk.fileSpec; |
| 2196 | loaded_from_shlib = 1; |
Guido van Rossum | 8ec9e63 | 1997-04-29 15:49:04 +0000 | [diff] [blame] | 2197 | } else if ( data->fragLocator.where == kResourceCFragLocator ) { |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2198 | library_fss = *data->fragLocator.u.inSegs.fileSpec; |
| 2199 | loaded_from_shlib = 1; |
| 2200 | } |
| 2201 | return noErr; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | ** Insert the library resources into the search path. Put them after |
| 2206 | ** the resources from the application. Again, we ignore errors. |
| 2207 | */ |
Guido van Rossum | dfd428d | 1996-02-25 04:46:40 +0000 | [diff] [blame] | 2208 | static |
Jack Jansen | 34cc5c3 | 1995-10-31 16:15:12 +0000 | [diff] [blame] | 2209 | mac_addlibresources() |
| 2210 | { |
| 2211 | if ( !loaded_from_shlib ) |
| 2212 | return; |
| 2213 | (void)FSpOpenResFile(&library_fss, fsRdPerm); |
| 2214 | } |
| 2215 | |
Guido van Rossum | ec22c92 | 1996-02-25 04:50:29 +0000 | [diff] [blame] | 2216 | #endif /* GENERATINGCFM */ |
| 2217 | #endif /* macintosh */ |