Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 24 | |
Jack Jansen | bf05d4c | 1996-08-19 15:11:45 +0000 | [diff] [blame] | 25 | #ifdef __CFM68K__ |
| 26 | /* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this |
| 27 | ** and defines it as GetEvQHdr (which is correct for PPC). This fix is for |
| 28 | ** CW9, check that the workaround is still needed for the next release. |
| 29 | */ |
| 30 | #define GetEvQHdr GetEventQueue |
| 31 | #endif /* __CFM68K__ */ |
| 32 | |
| 33 | #include <Events.h> |
| 34 | |
| 35 | #ifdef __CFM68K__ |
| 36 | #undef GetEventQueue |
| 37 | #endif /* __CFM68K__ */ |
| 38 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 39 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 40 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 41 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 42 | #include "marshal.h" |
| 43 | #include "import.h" |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 44 | #include "importdl.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 45 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 46 | #include "pythonresources.h" |
| 47 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 48 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 49 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 50 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 51 | #include <Resources.h> |
| 52 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 53 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 54 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 55 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 56 | #include <Fonts.h> |
| 57 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 58 | #include <TextUtils.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 59 | #ifdef THINK_C |
| 60 | #include <OSEvents.h> /* For EvQElPtr */ |
| 61 | #endif |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 62 | #ifdef __MWERKS__ |
| 63 | #include <SIOUX.h> |
| 64 | #endif |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 65 | #ifdef USE_GUSI |
| 66 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 67 | #include <LowMem.h> /* For SetSFCurDir, etc */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 68 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 69 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 70 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 71 | /* The ID of the Sioux apple menu */ |
| 72 | #define SIOUX_APPLEID 32000 |
| 73 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 74 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 75 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 76 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 77 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 78 | ** When less than this amount of stackspace is left we |
| 79 | ** raise a MemoryError. |
| 80 | */ |
| 81 | #ifndef MINIMUM_STACK_SIZE |
| 82 | #ifdef __powerc |
| 83 | #define MINIMUM_STACK_SIZE 8192 |
| 84 | #else |
| 85 | #define MINIMUM_STACK_SIZE 4096 |
| 86 | #endif |
| 87 | #endif |
| 88 | |
| 89 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 90 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 91 | ** things like updates (and they'll keep coming back if we don't |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 92 | ** handle them). Note that we don't know who has windows open, so |
| 93 | ** even handing updates off to SIOUX under MW isn't going to work. |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 94 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 95 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 96 | |
| 97 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 99 | /* XXX We should include Errors.h here, but it has a name conflict |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 100 | ** with the python errors.h. */ |
| 101 | #define fnfErr -43 |
| 102 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 103 | /* Declared in macfsmodule.c: */ |
| 104 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 105 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 106 | /* Interrupt code variables: */ |
| 107 | static int interrupted; /* Set to true when cmd-. seen */ |
| 108 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 109 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 110 | static int PyMac_DoYield Py_PROTO((int, int)); |
| 111 | static int PyMac_Yield Py_PROTO((void)); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 112 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 113 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 114 | ** These are the real scheduling parameters that control what we check |
| 115 | ** in the event loop, and how often we check. The values are initialized |
| 116 | ** from pyMac_SchedParamStruct. |
| 117 | */ |
| 118 | |
| 119 | struct real_sched_param_struct { |
| 120 | int check_interrupt; /* if true check for command-dot */ |
| 121 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 122 | int besocial; /* if nonzero be a little social with CPU */ |
| 123 | unsigned long check_interval; /* how often to check, in ticks */ |
| 124 | unsigned long bg_yield; /* yield so long when in background */ |
| 125 | /* these are computed from previous and clock and such */ |
| 126 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 127 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 128 | }; |
| 129 | |
| 130 | static struct real_sched_param_struct schedparams = |
| 131 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 132 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 133 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 134 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 135 | */ |
| 136 | int PyMac_ConsoleIsDead; |
| 137 | |
| 138 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 139 | ** Sioux menu bar, saved early so we can restore it |
| 140 | */ |
| 141 | static Handle sioux_mbar; |
| 142 | |
| 143 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 144 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 145 | */ |
| 146 | struct hook_args { |
| 147 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 148 | char *prompt; /* The prompt */ |
| 149 | }; |
| 150 | static DlgHookYDUPP myhook_upp; |
| 151 | static int upp_inited = 0; |
| 152 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 153 | /* |
| 154 | ** The python-code event handler |
| 155 | */ |
| 156 | static PyObject *python_event_handler; |
| 157 | |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 158 | #ifdef USE_GUSI |
| 159 | /* |
| 160 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 161 | ** the working directory. Hence, we call this routine after each call |
| 162 | ** to chdir() to rectify things. |
| 163 | */ |
| 164 | void |
| 165 | PyMac_FixGUSIcd() |
| 166 | { |
| 167 | WDPBRec pb; |
| 168 | FSSpec curdirfss; |
| 169 | |
| 170 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 171 | return; |
| 172 | |
| 173 | /* Set MacOS "working directory" */ |
| 174 | pb.ioNamePtr= "\p"; |
| 175 | pb.ioVRefNum= curdirfss.vRefNum; |
| 176 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 177 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 178 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 179 | } |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 180 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 181 | /* |
| 182 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 183 | ** provide a dummy here. |
| 184 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 185 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 186 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 187 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 188 | /* |
| 189 | ** Replacement GUSI Spin function |
| 190 | */ |
| 191 | static int |
| 192 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 193 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 194 | static Boolean inForeground = true; |
| 195 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 196 | |
| 197 | if (PyMac_ConsoleIsDead) return 0; |
| 198 | #if 0 |
| 199 | if (inForeground) |
| 200 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 201 | #endif |
| 202 | |
| 203 | if (interrupted) return -1; |
| 204 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 205 | if ( msg == SP_AUTO_SPIN ) |
| 206 | maxsleep = 0; |
| 207 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 208 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 209 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 210 | PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | PyMac_SetGUSISpin() { |
| 217 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 218 | } |
| 219 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 220 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 221 | void |
| 222 | PyMac_StopGUSISpin() { |
| 223 | PyMac_ConsoleIsDead = 1; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | ** Replacement routines for the PLstr... functions so we don't need |
| 228 | ** StdCLib. Moreover, that implementation is broken under cfm68k... |
| 229 | */ |
| 230 | void |
| 231 | PLstrcpy(to, fr) |
| 232 | unsigned char *to, *fr; |
| 233 | { |
| 234 | memcpy(to, fr, fr[0]+1); |
| 235 | } |
| 236 | |
| 237 | int |
| 238 | PLstrcmp(s1, s2) |
| 239 | unsigned char *s1, *s2; |
| 240 | { |
| 241 | int res; |
| 242 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 243 | |
| 244 | res = memcmp(s1+1, s2+1, l); |
| 245 | if ( res != 0 ) |
| 246 | return res; |
| 247 | |
| 248 | if ( s1 < s2 ) |
| 249 | return -1; |
| 250 | else if ( s1 > s2 ) |
| 251 | return 1; |
| 252 | else |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | unsigned char * |
| 257 | PLstrrchr(str, chr) |
| 258 | unsigned char *str; |
| 259 | unsigned char chr; |
| 260 | { |
| 261 | unsigned char *ptr = 0; |
| 262 | unsigned char *p; |
| 263 | |
| 264 | for(p=str+1; p<str+str[0]; p++) |
| 265 | if ( *p == chr ) |
| 266 | ptr = p; |
| 267 | return ptr; |
| 268 | } |
| 269 | |
| 270 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 271 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 272 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 273 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 274 | unsigned char * |
| 275 | Pstring(char *str) |
| 276 | { |
| 277 | static Str255 buf; |
| 278 | int len; |
| 279 | |
| 280 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 281 | if (len > 255) |
| 282 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 283 | buf[0] = (unsigned char)len; |
| 284 | strncpy((char *)buf+1, str, len); |
| 285 | return buf; |
| 286 | } |
| 287 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 288 | /* Like strerror() but for Mac OS error numbers */ |
| 289 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 290 | { |
| 291 | static char buf[256]; |
| 292 | Handle h; |
| 293 | char *str; |
| 294 | |
| 295 | h = GetResource('Estr', err); |
| 296 | if ( h ) { |
| 297 | HLock(h); |
| 298 | str = (char *)*h; |
| 299 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 300 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 301 | HUnlock(h); |
| 302 | ReleaseResource(h); |
| 303 | } else { |
| 304 | sprintf(buf, "Mac OS error code %d", err); |
| 305 | } |
| 306 | return buf; |
| 307 | } |
| 308 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 309 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 310 | PyObject *PyMac_OSErrException; |
| 311 | |
| 312 | /* Initialize and return PyMac_OSErrException */ |
| 313 | PyObject * |
| 314 | PyMac_GetOSErrException() |
| 315 | { |
| 316 | if (PyMac_OSErrException == NULL) |
| 317 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 318 | return PyMac_OSErrException; |
| 319 | } |
| 320 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 321 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 322 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 323 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 324 | { |
| 325 | char *msg; |
| 326 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 328 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 329 | Py_INCREF(Py_None); |
| 330 | return Py_None; |
| 331 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 332 | if (err == -1 && PyErr_Occurred()) |
| 333 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 334 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 335 | v = Py_BuildValue("(is)", err, msg); |
| 336 | PyErr_SetObject(eobj, v); |
| 337 | Py_DECREF(v); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 341 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 342 | PyObject * |
| 343 | PyMac_Error(OSErr err) |
| 344 | { |
| 345 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 346 | } |
| 347 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 348 | #ifdef USE_STACKCHECK |
| 349 | /* Check for stack overflow */ |
| 350 | int |
| 351 | PyOS_CheckStack() |
| 352 | { |
| 353 | long left; |
| 354 | |
| 355 | left = StackSpace(); |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 356 | if ( left < MINIMUM_STACK_SIZE ) |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 357 | return -1; |
| 358 | return 0; |
| 359 | } |
| 360 | #endif /* USE_STACKCHECK */ |
| 361 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 362 | /* The catcher routine (which may not be used for all compilers) */ |
| 363 | static RETSIGTYPE |
| 364 | intcatcher(sig) |
| 365 | int sig; |
| 366 | { |
| 367 | interrupted = 1; |
| 368 | signal(SIGINT, intcatcher); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | PyOS_InitInterrupts() |
| 373 | { |
| 374 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 375 | signal(SIGINT, intcatcher); |
| 376 | } |
| 377 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 378 | void |
| 379 | PyOS_FiniInterrupts() |
| 380 | { |
| 381 | } |
| 382 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 383 | /* |
| 384 | ** This routine scans the event queue looking for cmd-. |
| 385 | ** This is the only way to get an interrupt under THINK (since it |
| 386 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 387 | ** the full-fledged event loop is disabled. This way, we can at least |
| 388 | ** interrupt a runaway python program. |
| 389 | */ |
| 390 | static void |
| 391 | scan_event_queue(flush) |
| 392 | int flush; |
| 393 | { |
| 394 | register EvQElPtr q; |
| 395 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 396 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 397 | |
| 398 | for (; q; q = (EvQElPtr)q->qLink) { |
| 399 | if (q->evtQWhat == keyDown && |
| 400 | (char)q->evtQMessage == '.' && |
| 401 | (q->evtQModifiers & cmdKey) != 0) { |
| 402 | if ( flush ) |
| 403 | FlushEvents(keyDownMask, 0); |
| 404 | interrupted = 1; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 411 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 412 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 413 | if (schedparams.enabled) { |
| 414 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 415 | if ( PyMac_Yield() < 0) |
| 416 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 417 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 418 | + schedparams.check_interval; |
| 419 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 420 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 421 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 422 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 423 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 430 | int |
| 431 | PyOS_InterruptOccurred() |
| 432 | { |
| 433 | scan_event_queue(1); |
| 434 | return interrupted; |
| 435 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 436 | /* Check whether we are in the foreground */ |
| 437 | int |
| 438 | PyMac_InForeground() |
| 439 | { |
| 440 | static ProcessSerialNumber ours; |
| 441 | static inited; |
| 442 | ProcessSerialNumber curfg; |
| 443 | Boolean eq; |
| 444 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 445 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 446 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 447 | inited = 1; |
| 448 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 449 | if ( GetFrontProcess(&curfg) < 0 ) |
| 450 | eq = 1; |
| 451 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 452 | eq = 1; |
| 453 | return (int)eq; |
| 454 | |
| 455 | } |
| 456 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 457 | int |
| 458 | PyMac_SetEventHandler(PyObject *evh) |
| 459 | { |
| 460 | if ( evh && python_event_handler ) { |
| 461 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 462 | return 0; |
| 463 | } |
| 464 | if ( python_event_handler ) |
| 465 | Py_DECREF(python_event_handler); |
| 466 | if ( evh ) |
| 467 | Py_INCREF(evh); |
| 468 | python_event_handler = evh; |
| 469 | return 1; |
| 470 | } |
| 471 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 472 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 473 | ** Handle an event, either one found in the mainloop eventhandler or |
| 474 | ** one passed back from the python program. |
| 475 | */ |
| 476 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 477 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 478 | EventRecord *evp; |
| 479 | { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 480 | if ( evp->what == mouseDown ) { |
| 481 | WindowPtr wp; |
| 482 | |
| 483 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 484 | SystemClick(evp, wp); |
| 485 | return; |
| 486 | } |
| 487 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 488 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 489 | { |
| 490 | int siouxdidit; |
| 491 | |
| 492 | /* If SIOUX wants it we're done */ |
| 493 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 494 | if ( siouxdidit ) |
| 495 | return; |
| 496 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 497 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 498 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 499 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 504 | ** event handler. |
| 505 | */ |
| 506 | int |
| 507 | PyMac_HandleEvent(evp) |
| 508 | EventRecord *evp; |
| 509 | { |
| 510 | PyObject *rv; |
| 511 | |
| 512 | if ( python_event_handler ) { |
| 513 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 514 | PyMac_BuildEventRecord, evp); |
| 515 | if ( rv ) |
| 516 | Py_DECREF(rv); |
| 517 | else |
| 518 | return -1; /* Propagate exception */ |
| 519 | } else { |
| 520 | PyMac_HandleEventIntern(evp); |
| 521 | } |
| 522 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 526 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 527 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 528 | static int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 529 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 530 | { |
| 531 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 532 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 533 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 534 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 535 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 536 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 537 | /* |
| 538 | ** First check for interrupts, if wanted. |
| 539 | ** This sets a flag that will be picked up at an appropriate |
| 540 | ** moment in the mainloop. |
| 541 | */ |
| 542 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 543 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 544 | |
| 545 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 546 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 547 | /* |
| 548 | ** Check which of the eventloop cases we have: |
| 549 | ** - process events |
| 550 | ** - don't process events but do yield |
| 551 | ** - do neither |
| 552 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 553 | if( in_here > 1 || !schedparams.process_events || |
| 554 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 555 | if ( maxsleep >= 0 ) { |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 556 | SystemTask(); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 557 | } |
| 558 | } else { |
| 559 | latest_time_ready = LMGetTicks() + maxsleep; |
| 560 | while ( maxsleep >= 0 ) { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 561 | /* XXXX Hack by Jack. |
| 562 | ** In time.sleep() you can click to another application |
| 563 | ** once only. If you come back to Python you cannot get away |
| 564 | ** again. |
| 565 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 566 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 567 | /* Get out quickly if nothing interesting is happening */ |
| 568 | if ( !gotone || ev.what == nullEvent ) |
| 569 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 570 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 571 | in_here--; |
| 572 | return -1; |
| 573 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 574 | maxsleep = latest_time_ready - LMGetTicks(); |
| 575 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 576 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 577 | in_here--; |
| 578 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 582 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 583 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 584 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 585 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 586 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 587 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 588 | if( PyMac_InForeground() ) |
| 589 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 590 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 591 | maxsleep = schedparams.bg_yield; |
| 592 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 593 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 597 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 598 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 599 | void |
| 600 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 601 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 602 | sp->check_interrupt = schedparams.check_interrupt; |
| 603 | sp->process_events = schedparams.process_events; |
| 604 | sp->besocial = schedparams.besocial; |
| 605 | sp->check_interval = schedparams.check_interval / 60.0; |
| 606 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 607 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 608 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 609 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 610 | ** Set current scheduler parameters |
| 611 | */ |
| 612 | void |
| 613 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 614 | { |
| 615 | schedparams.check_interrupt = sp->check_interrupt; |
| 616 | schedparams.process_events = sp->process_events; |
| 617 | schedparams.besocial = sp->besocial; |
| 618 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 619 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 620 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 621 | schedparams.besocial ) |
| 622 | schedparams.enabled = 1; |
| 623 | else |
| 624 | schedparams.enabled = 0; |
| 625 | schedparams.next_check = 0; /* Check immedeately */ |
| 626 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 627 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 628 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 629 | ** Install our menu bar. |
| 630 | */ |
| 631 | void |
| 632 | PyMac_InitMenuBar() |
| 633 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 634 | MenuHandle applemenu; |
| 635 | |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 636 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 637 | /* Sioux menu not installed yet. Do so */ |
| 638 | SIOUXSetupMenus(); |
| 639 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 640 | return; |
| 641 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 642 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 643 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 644 | } |
| 645 | |
| 646 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 647 | ** Restore sioux menu bar |
| 648 | */ |
| 649 | void |
| 650 | PyMac_RestoreMenuBar() |
| 651 | { |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 652 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 653 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 654 | DrawMenuBar(); |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 655 | } else |
| 656 | PyMac_InitMenuBar(); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 661 | ** Our replacement about box |
| 662 | */ |
| 663 | void |
| 664 | SIOUXDoAboutBox(void) |
| 665 | { |
| 666 | DialogPtr theDialog; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 667 | WindowRef theWindow; |
| 668 | CGrafPtr thePort; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 669 | short item; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 670 | short xpos, ypos, width, height, swidth, sheight; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 671 | |
| 672 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 673 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 674 | theWindow = GetDialogWindow(theDialog); |
| 675 | thePort = GetWindowPort(theWindow); |
| 676 | width = thePort->portRect.right - thePort->portRect.left; |
| 677 | height = thePort->portRect.bottom - thePort->portRect.top; |
| 678 | swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left; |
| 679 | sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight(); |
| 680 | xpos = (swidth-width)/2; |
Jack Jansen | 0e06e7e | 1996-09-23 15:51:06 +0000 | [diff] [blame] | 681 | ypos = (sheight-height)/5 + LMGetMBarHeight(); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 682 | MoveWindow(theWindow, xpos, ypos, 0); |
| 683 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 684 | ModalDialog(NULL, &item); |
| 685 | DisposeDialog(theDialog); |
| 686 | } |
| 687 | |
| 688 | /* |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 689 | ** Returns true if the argument has a resource fork, and it contains |
| 690 | ** a 'PYC ' resource of the correct name |
| 691 | */ |
| 692 | int |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 693 | PyMac_FindResourceModule(obj, module, filename) |
| 694 | PyStringObject *obj; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 695 | char *module; |
| 696 | char *filename; |
| 697 | { |
| 698 | FSSpec fss; |
| 699 | FInfo finfo; |
| 700 | short oldrh, filerh; |
| 701 | int ok; |
| 702 | Handle h; |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 703 | |
| 704 | #ifdef INTERN_STRINGS |
| 705 | /* |
| 706 | ** If we have interning find_module takes care of interning all |
| 707 | ** sys.path components. We then keep a record of all sys.path |
| 708 | ** components for which GetFInfo has failed (usually because the |
| 709 | ** component in question is a folder), and we don't try opening these |
| 710 | ** as resource files again. |
| 711 | */ |
| 712 | #define MAXPATHCOMPONENTS 32 |
| 713 | static PyStringObject *not_a_file[MAXPATHCOMPONENTS]; |
| 714 | static int max_not_a_file = 0; |
| 715 | int i; |
| 716 | |
| 717 | if ( obj->ob_sinterned ) { |
| 718 | for( i=0; i< max_not_a_file; i++ ) |
| 719 | if ( obj == not_a_file[i] ) |
| 720 | return 0; |
| 721 | } |
| 722 | #endif /* INTERN_STRINGS */ |
| 723 | |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 724 | if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) { |
| 725 | /* |
| 726 | ** Special case: the application itself. Use a shortcut to |
| 727 | ** forestall opening and closing the application numerous times |
| 728 | ** (which is dead slow when running from CDROM) |
| 729 | */ |
| 730 | oldrh = CurResFile(); |
| 731 | UseResFile(PyMac_AppRefNum); |
| 732 | filerh = -1; |
| 733 | } else { |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 734 | if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr || |
| 735 | FSpGetFInfo(&fss, &finfo) != noErr ) { |
| 736 | #ifdef INTERN_STRINGS |
| 737 | if ( max_not_a_file < MAXPATHCOMPONENTS && obj->ob_sinterned ) |
| 738 | not_a_file[max_not_a_file++] = obj; |
| 739 | #endif /* INTERN_STRINGS */ |
| 740 | /* doesn't exist or is folder */ |
| 741 | return 0; |
| 742 | } |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 743 | oldrh = CurResFile(); |
| 744 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 745 | if ( filerh == -1 ) |
| 746 | return 0; |
| 747 | UseResFile(filerh); |
| 748 | } |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 749 | SetResLoad(0); |
| 750 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 751 | SetResLoad(1); |
| 752 | ok = (h != NULL); |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 753 | if ( filerh != -1 ) |
| 754 | CloseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 755 | UseResFile(oldrh); |
| 756 | return ok; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | ** Load the specified module from a resource |
| 761 | */ |
| 762 | PyObject * |
| 763 | PyMac_LoadResourceModule(module, filename) |
| 764 | char *module; |
| 765 | char *filename; |
| 766 | { |
| 767 | FSSpec fss; |
| 768 | FInfo finfo; |
| 769 | short oldrh, filerh; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 770 | Handle h; |
| 771 | OSErr err; |
| 772 | PyObject *m, *co; |
| 773 | long num, size; |
| 774 | |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 775 | if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) { |
| 776 | /* |
| 777 | ** Special case: the application itself. Use a shortcut to |
| 778 | ** forestall opening and closing the application numerous times |
| 779 | ** (which is dead slow when running from CDROM) |
| 780 | */ |
| 781 | oldrh = CurResFile(); |
| 782 | UseResFile(PyMac_AppRefNum); |
| 783 | filerh = -1; |
| 784 | } else { |
| 785 | if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr ) |
| 786 | goto error; |
| 787 | if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) |
| 788 | goto error; |
| 789 | oldrh = CurResFile(); |
| 790 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 791 | if ( filerh == -1 ) { |
| 792 | err = ResError(); |
| 793 | goto error; |
| 794 | } |
| 795 | UseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 796 | } |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 797 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 798 | if ( h == NULL ) { |
| 799 | err = ResError(); |
| 800 | goto error; |
| 801 | } |
| 802 | HLock(h); |
| 803 | /* |
| 804 | ** XXXX The next few lines are intimately tied to the format of pyc |
| 805 | ** files. I'm not sure whether this code should be here or in import.c -- Jack |
| 806 | */ |
| 807 | size = GetHandleSize(h); |
| 808 | if ( size < 8 ) { |
| 809 | PyErr_SetString(PyExc_ImportError, "Resource too small"); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 810 | co = NULL; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 811 | } else { |
| 812 | num = (*h)[0] & 0xff; |
| 813 | num = num | (((*h)[1] & 0xff) << 8); |
| 814 | num = num | (((*h)[2] & 0xff) << 16); |
| 815 | num = num | (((*h)[3] & 0xff) << 24); |
| 816 | if ( num != PyImport_GetMagicNumber() ) { |
| 817 | PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource"); |
| 818 | co = NULL; |
| 819 | } else { |
| 820 | co = PyMarshal_ReadObjectFromString((*h)+8, size-8); |
| 821 | } |
| 822 | } |
| 823 | HUnlock(h); |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 824 | if ( filerh != -1 ) |
| 825 | CloseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 826 | UseResFile(oldrh); |
| 827 | if ( co ) { |
| 828 | m = PyImport_ExecCodeModule(module, co); |
| 829 | Py_DECREF(co); |
| 830 | } else { |
| 831 | m = NULL; |
| 832 | } |
| 833 | return m; |
| 834 | error: |
| 835 | { |
| 836 | char buf[512]; |
| 837 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 838 | sprintf(buf, "%s: %s", filename, PyMac_StrError(err)); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 839 | PyErr_SetString(PyExc_ImportError, buf); |
| 840 | return NULL; |
| 841 | } |
| 842 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 843 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 844 | /* |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 845 | ** Look for a module in a single folder. Upon entry buf and len |
| 846 | ** point to the folder to search, upon exit they refer to the full |
| 847 | ** pathname of the module found (if any). |
| 848 | */ |
| 849 | struct filedescr * |
| 850 | PyMac_FindModuleExtension(char *buf, int *lenp, char *module) |
| 851 | { |
| 852 | struct filedescr *fdp; |
| 853 | unsigned char fnbuf[64]; |
| 854 | int modnamelen = strlen(module); |
| 855 | FSSpec fss; |
| 856 | short refnum; |
| 857 | long dirid; |
| 858 | |
| 859 | /* |
| 860 | ** Copy the module name to the buffer (already :-terminated) |
| 861 | ** We also copy the first suffix, if this matches immedeately we're |
| 862 | ** lucky and return immedeately. |
| 863 | */ |
| 864 | if ( !_PyImport_Filetab[0].suffix ) |
| 865 | return 0; |
| 866 | |
Jack Jansen | c73f83c | 1997-10-07 21:48:31 +0000 | [diff] [blame] | 867 | #if 0 |
| 868 | /* Pre 1.5a4 */ |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 869 | strcpy(buf+*lenp, module); |
| 870 | strcpy(buf+*lenp+modnamelen, _PyImport_Filetab[0].suffix); |
Jack Jansen | c73f83c | 1997-10-07 21:48:31 +0000 | [diff] [blame] | 871 | #else |
| 872 | strcpy(buf+*lenp, _PyImport_Filetab[0].suffix); |
| 873 | #endif |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 874 | if ( FSMakeFSSpec(0, 0, Pstring(buf), &fss) == noErr ) |
| 875 | return _PyImport_Filetab; |
| 876 | /* |
| 877 | ** We cannot check for fnfErr (unfortunately), it can mean either that |
| 878 | ** the file doesn't exist (fine, we try others) or the path leading to it. |
| 879 | */ |
| 880 | refnum = fss.vRefNum; |
| 881 | dirid = fss.parID; |
| 882 | if ( refnum == 0 || dirid == 0 ) /* Fail on nonexistent dir */ |
| 883 | return 0; |
| 884 | /* |
| 885 | ** We now have the folder parameters. Setup the field for the filename |
| 886 | */ |
| 887 | if ( modnamelen > 54 ) return 0; /* Leave room for extension */ |
| 888 | strcpy((char *)fnbuf+1, module); |
| 889 | |
| 890 | for( fdp = _PyImport_Filetab+1; fdp->suffix; fdp++ ) { |
| 891 | strcpy((char *)fnbuf+1+modnamelen, fdp->suffix); |
| 892 | fnbuf[0] = strlen((char *)fnbuf+1); |
| 893 | if (Py_VerboseFlag > 1) |
| 894 | fprintf(stderr, "# trying %s%s\n", buf, fdp->suffix); |
| 895 | if ( FSMakeFSSpec(refnum, dirid, fnbuf, &fss) == noErr ) { |
| 896 | /* Found it. */ |
Jack Jansen | c73f83c | 1997-10-07 21:48:31 +0000 | [diff] [blame] | 897 | #if 0 |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 898 | strcpy(buf+*lenp+modnamelen, fdp->suffix); |
Jack Jansen | c73f83c | 1997-10-07 21:48:31 +0000 | [diff] [blame] | 899 | #else |
Jack Jansen | 2136490 | 1997-10-08 15:32:46 +0000 | [diff] [blame] | 900 | strcpy(buf+*lenp, fdp->suffix); |
Jack Jansen | c73f83c | 1997-10-07 21:48:31 +0000 | [diff] [blame] | 901 | #endif |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 902 | *lenp = strlen(buf); |
| 903 | return fdp; |
| 904 | } |
| 905 | } |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | #if 0 |
| 910 | int |
| 911 | PyMac_FileExists(char *name) |
| 912 | { |
| 913 | FSSpec fss; |
| 914 | |
| 915 | if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr ) |
| 916 | return 1; |
| 917 | return 0; |
| 918 | } |
| 919 | #endif |
| 920 | |
| 921 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 922 | ** Helper routine for GetDirectory |
| 923 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 924 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 925 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 926 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 927 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 928 | Handle prompth; |
| 929 | short type; |
| 930 | Rect rect; |
| 931 | |
| 932 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 933 | if ( prompth ) |
| 934 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 935 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 936 | if ( item == SELECTCUR_ITEM ) { |
| 937 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 938 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 939 | } |
| 940 | return item; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | ** Ask the user for a directory. I still can't understand |
| 945 | ** why Apple doesn't provide a standard solution for this... |
| 946 | */ |
| 947 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 948 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 949 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 950 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 951 | { |
| 952 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 953 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 954 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 955 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 956 | |
| 957 | if ( !upp_inited ) { |
| 958 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 959 | upp_inited = 1; |
| 960 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 961 | if ( prompt && *prompt ) |
| 962 | hook_args.prompt = (char *)Pstring(prompt); |
| 963 | else |
| 964 | hook_args.prompt = NULL; |
| 965 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 966 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 967 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 968 | |
| 969 | reply.sfFile.name[0] = 0; |
| 970 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 971 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 972 | return hook_args.selectcur_hit; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 977 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 978 | StandardFileReply *reply, char *prompt) |
| 979 | { |
| 980 | static Point where = {-1, -1}; |
| 981 | struct hook_args hook_args; |
| 982 | |
| 983 | if ( !upp_inited ) { |
| 984 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 985 | upp_inited = 1; |
| 986 | } |
| 987 | if ( prompt && *prompt ) |
| 988 | hook_args.prompt = (char *)Pstring(prompt); |
| 989 | else |
| 990 | hook_args.prompt = NULL; |
| 991 | hook_args.selectcur_hit = 0; |
| 992 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 993 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 994 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 995 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 996 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 997 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 998 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 999 | { |
| 1000 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 1001 | PyErr_SetString(PyExc_TypeError, |
| 1002 | "OSType arg must be string of 4 chars"); |
| 1003 | return 0; |
| 1004 | } |
| 1005 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1009 | /* Convert an OSType value to a 4-char string object */ |
| 1010 | PyObject * |
| 1011 | PyMac_BuildOSType(OSType t) |
| 1012 | { |
| 1013 | return PyString_FromStringAndSize((char *)&t, 4); |
| 1014 | } |
| 1015 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 1016 | /* Convert an NumVersion value to a 4-element tuple */ |
| 1017 | PyObject * |
| 1018 | PyMac_BuildNumVersion(NumVersion t) |
| 1019 | { |
| 1020 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 1021 | } |
| 1022 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1023 | |
| 1024 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1025 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1026 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1027 | { |
| 1028 | int len; |
| 1029 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 1030 | PyErr_SetString(PyExc_TypeError, |
| 1031 | "Str255 arg must be string of at most 255 chars"); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | pbuf[0] = len; |
| 1035 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 1036 | return 1; |
| 1037 | } |
| 1038 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1039 | /* Convert a Str255 to a Python string object */ |
| 1040 | PyObject * |
| 1041 | PyMac_BuildStr255(Str255 s) |
| 1042 | { |
| 1043 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 1044 | } |
| 1045 | |
| 1046 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1047 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1048 | ** Convert a Python object to an FSSpec. |
| 1049 | ** The object may either be a full pathname or a triple |
| 1050 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1051 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 1052 | ** The caller is responsible for not calling this routine |
| 1053 | ** in those cases (which is fine, since everyone calling |
| 1054 | ** this is probably sys7 dependent anyway). |
| 1055 | */ |
| 1056 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1057 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1058 | { |
| 1059 | Str255 path; |
| 1060 | short refnum; |
| 1061 | long parid; |
| 1062 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 1063 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1064 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 1065 | /* first check whether it already is an FSSpec */ |
| 1066 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 1067 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 1068 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 1069 | return 1; |
| 1070 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1071 | if ( PyString_Check(v) ) { |
| 1072 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 1073 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1074 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 1075 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1076 | parid = 0; |
| 1077 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 1078 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 1079 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1080 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 1081 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1082 | } |
| 1083 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 1084 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 1085 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1091 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1092 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1093 | The object must be a (left, top, right, bottom) tuple. |
| 1094 | (This differs from the order in the struct but is consistent with |
| 1095 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1096 | int |
| 1097 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1098 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1099 | return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom); |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1100 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1101 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1102 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1103 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1104 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1105 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1106 | return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1111 | The object must be a (h, v) tuple. |
| 1112 | (This differs from the order in the struct but is consistent with |
| 1113 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1114 | int |
| 1115 | PyMac_GetPoint(PyObject *v, Point *p) |
| 1116 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1117 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* Convert a Point to a Python object */ |
| 1121 | PyObject * |
| 1122 | PyMac_BuildPoint(Point p) |
| 1123 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1124 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | /* Convert a Python object to an EventRecord. |
| 1129 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 1130 | int |
| 1131 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 1132 | { |
| 1133 | return PyArg_Parse(v, "(hll(hh)h)", |
| 1134 | &e->what, |
| 1135 | &e->message, |
| 1136 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1137 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1138 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1139 | &e->modifiers); |
| 1140 | } |
| 1141 | |
| 1142 | /* Convert a Rect to an EventRecord object */ |
| 1143 | PyObject * |
| 1144 | PyMac_BuildEventRecord(EventRecord *e) |
| 1145 | { |
| 1146 | return Py_BuildValue("(hll(hh)h)", |
| 1147 | e->what, |
| 1148 | e->message, |
| 1149 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1150 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1151 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1152 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1153 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* Convert Python object to Fixed */ |
| 1156 | int |
| 1157 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 1158 | { |
| 1159 | double d; |
| 1160 | |
| 1161 | if( !PyArg_Parse(v, "d", &d)) |
| 1162 | return 0; |
| 1163 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 1164 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | /* Convert a Point to a Python object */ |
| 1168 | PyObject * |
| 1169 | PyMac_BuildFixed(Fixed f) |
| 1170 | { |
| 1171 | double d; |
| 1172 | |
| 1173 | d = f; |
| 1174 | d = d / 0x10000; |
| 1175 | return Py_BuildValue("d", d); |
| 1176 | } |
| 1177 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1178 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 1179 | int |
| 1180 | PyMac_Getwide(PyObject *v, wide *rv) |
| 1181 | { |
| 1182 | if (PyInt_Check(v)) { |
| 1183 | rv->hi = 0; |
| 1184 | rv->lo = PyInt_AsLong(v); |
| 1185 | if( rv->lo & 0x80000000 ) |
| 1186 | rv->hi = -1; |
| 1187 | return 1; |
| 1188 | } |
| 1189 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 1190 | } |
| 1191 | |
| 1192 | |
| 1193 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1194 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1195 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1196 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 1197 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 1198 | return PyInt_FromLong(w->lo); |
| 1199 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1200 | } |