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