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