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