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