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