annotate Mac/Python/macglue.c @ 2394:fa8cdd3cc545 legacy-trunk

use PyMac_GetArgv (in macgetargv.c) for applet inits
author Guido van Rossum <guido@python.org>
date Sun, 19 Feb 1995 15:52:17 +0000
parents 6450d8828d2b
children 72c715c004c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
1 /***********************************************************
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
3 The Netherlands.
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
4
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
5 All Rights Reserved
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
6
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
8 documentation for any purpose and without fee is hereby granted,
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
9 provided that the above copyright notice appear in all copies and that
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
10 both that copyright notice and this permission notice appear in
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
13 distribution of the software without specific, written prior permission.
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
14
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
22
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
23 ******************************************************************/
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25 #include "Python.h"
2337
71e60871c681 ported to Think C
Guido van Rossum <guido@python.org>
parents: 2333
diff changeset
26
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 #include "macglue.h"
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
28 #include "marshal.h"
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
29 #include "import.h"
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
30
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
31 #include <OSUtils.h> /* for Set(Current)A5 */
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
32 #include <Files.h>
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
33 #include <Aliases.h>
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
34 #include <Folders.h>
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
35 #include <StandardFile.h>
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 #include <Resources.h>
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 #include <Memory.h>
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
38 #include <Events.h>
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
39 #include <Windows.h>
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
40 #include <Desk.h>
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
41 #include <Traps.h>
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
42 #include <Processes.h>
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
43 #include <Fonts.h>
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
44 #include <Menus.h>
2265
4bcc92916fab added <OSEvents.h> for Think C
Guido van Rossum <guido@python.org>
parents: 2262
diff changeset
45 #ifdef THINK_C
4bcc92916fab added <OSEvents.h> for Think C
Guido van Rossum <guido@python.org>
parents: 2262
diff changeset
46 #include <OSEvents.h> /* For EvQElPtr */
4bcc92916fab added <OSEvents.h> for Think C
Guido van Rossum <guido@python.org>
parents: 2262
diff changeset
47 #endif
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
48
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
49 #ifndef HAVE_UNIVERSAL_HEADERS
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
50 #define GetResourceSizeOnDisk(x) SizeResource(x)
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
51 #endif
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
52
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
53 #include <signal.h>
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
54 #include <stdio.h>
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
55
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
56 #define NOPYTHON_ALERT 128
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
57 #define YES_ITEM 1
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
58 #define NO_ITEM 2
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
59 #define CURWD_ITEM 3
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
60
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
61 #ifdef __MWERKS__
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
62 /*
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
63 ** With MW we can pass the event off to the console window, so
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
64 ** we might as well handle all events.
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
65 */
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
66 #include <SIOUX.h>
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
67 #define MAINLOOP_EVENTMASK everyEvent
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
68 #else
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
69 /*
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
70 ** For other compilers we're more careful, since we can't handle
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
71 ** things like updates (and they'll keep coming back if we don't
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
72 ** handle them)
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
73 */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
74 #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask)
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
75 #endif /* __MWERKS__ */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
76
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
77 #include <signal.h>
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
79 /* XXX We should include Errors.h here, but it has a name conflict
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
80 ** with the python errors.h. */
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
81 #define fnfErr -43
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
82
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
83 /* Declared in macfsmodule.c: */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
84 extern FSSpec *mfs_GetFSSpecFSSpec();
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
85
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
86 /* Interrupt code variables: */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
87 static int interrupted; /* Set to true when cmd-. seen */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
88 static RETSIGTYPE intcatcher Py_PROTO((int));
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
89
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
90 /*
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
91 ** We attempt to be a good citizen by giving up the CPU periodically.
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
92 ** When in the foreground we do this less often and for shorter periods
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
93 ** than when in the background. At this time we also check for events and
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
94 ** pass them off to SIOUX, if compiling with mwerks.
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
95 ** The counts here are in ticks of 1/60th second.
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
96 ** XXXX The initial values here are not based on anything.
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
97 ** FG-python gives up the cpu for 1/60th 5 times per second,
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
98 ** BG-python for .2 second 10 times per second.
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
99 */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
100 static long interval_fg = 12;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
101 static long interval_bg = 6;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
102 static long yield_fg = 1;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
103 static long yield_bg = 12;
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
104 static long lastyield;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
105 static int in_foreground;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
106
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
107 int PyMac_DoYieldEnabled = 1; /* Don't do eventloop when false */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
108
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
109
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
110 /* Convert C to Pascal string. Returns pointer to static buffer. */
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
111 unsigned char *
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
112 Pstring(char *str)
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
113 {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
114 static Str255 buf;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
115 int len;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
116
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
117 len = strlen(str);
2209
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
118 if (len > 255)
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
119 len = 255;
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
120 buf[0] = (unsigned char)len;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
121 strncpy((char *)buf+1, str, len);
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
122 return buf;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
123 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
124
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
125 /* Like strerror() but for Mac OS error numbers */
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
126 char *PyMac_StrError(int err)
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
127 {
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
128 static char buf[256];
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
129 Handle h;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
130 char *str;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
131
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
132 h = GetResource('Estr', err);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
133 if ( h ) {
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
134 HLock(h);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
135 str = (char *)*h;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
136 memcpy(buf, str+1, (unsigned char)str[0]);
2324
faaece7ccd6e null-terminate the error string in macstrerror
Guido van Rossum <guido@python.org>
parents: 2312
diff changeset
137 buf[(unsigned char)str[0]] = '\0';
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
138 HUnlock(h);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
139 ReleaseResource(h);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
140 } else {
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
141 sprintf(buf, "Mac OS error code %d", err);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
142 }
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
143 return buf;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
144 }
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
145
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
146 /* Exception object shared by all Mac specific modules for Mac OS errors */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
147 PyObject *PyMac_OSErrException;
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
148
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
149 /* Initialize and return PyMac_OSErrException */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
150 PyObject *
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
151 PyMac_GetOSErrException()
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
152 {
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
153 if (PyMac_OSErrException == NULL)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
154 PyMac_OSErrException = PyString_FromString("Mac OS Error");
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
155 return PyMac_OSErrException;
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
156 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
157
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
158 /* Set a MAC-specific error from errno, and return NULL; return None if no error */
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
159 PyObject *
2136
c57b5c50a2d2 rename2 changes
Guido van Rossum <guido@python.org>
parents: 2035
diff changeset
160 PyErr_Mac(PyObject *eobj, int err)
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
161 {
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
162 char *msg;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
163 PyObject *v;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
164
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
165 if (err == 0 && !PyErr_Occurred()) {
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
166 Py_INCREF(Py_None);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
167 return Py_None;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
168 }
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
169 if (err == -1 && PyErr_Occurred())
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
170 return NULL;
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
171 msg = PyMac_StrError(err);
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
172 v = Py_BuildValue("(is)", err, msg);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
173 PyErr_SetObject(eobj, v);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
174 Py_DECREF(v);
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
175 return NULL;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
176 }
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
177
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
178 /* Call PyErr_Mac with PyMac_OSErrException */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
179 PyObject *
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
180 PyMac_Error(OSErr err)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
181 {
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
182 return PyErr_Mac(PyMac_GetOSErrException(), err);
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
183 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
184
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
185 /* The catcher routine (which may not be used for all compilers) */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
186 static RETSIGTYPE
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
187 intcatcher(sig)
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
188 int sig;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
189 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
190 interrupted = 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
191 signal(SIGINT, intcatcher);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
192 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
193
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
194 void
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
195 PyOS_InitInterrupts()
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
196 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
197 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
198 signal(SIGINT, intcatcher);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
199 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
200
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
201 /*
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
202 ** This routine scans the event queue looking for cmd-.
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
203 ** This is the only way to get an interrupt under THINK (since it
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
204 ** doesn't do SIGINT handling), but is also used under MW, when
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
205 ** the full-fledged event loop is disabled. This way, we can at least
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
206 ** interrupt a runaway python program.
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
207 */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
208 static void
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
209 scan_event_queue(flush)
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
210 int flush;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
211 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
212 register EvQElPtr q;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
213
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
214 q = (EvQElPtr) GetEvQHdr()->qHead;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
215
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
216 for (; q; q = (EvQElPtr)q->qLink) {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
217 if (q->evtQWhat == keyDown &&
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
218 (char)q->evtQMessage == '.' &&
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
219 (q->evtQModifiers & cmdKey) != 0) {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
220 if ( flush )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
221 FlushEvents(keyDownMask, 0);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
222 interrupted = 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
223 break;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
224 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
225 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
226 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
227
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
228 int
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
229 PyOS_InterruptOccurred()
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
230 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
231 #ifdef THINK_C
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
232 scan_event_queue(1);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
233 #endif
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
234 PyMac_Yield();
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
235 if (interrupted) {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
236 interrupted = 0;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
237 return 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
238 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
239 return 0;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
240 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
241
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
242 /* intrpeek() is like intrcheck(), but it doesn't flush the events. The
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
243 ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
244 ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
245 ** thereafter.
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
246 */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
247 static int
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
248 intrpeek()
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
249 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
250 #ifdef THINK_C
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
251 scan_event_queue(0);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
252 #endif
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
253 return interrupted;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
254 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
255
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
256 /* Check whether we are in the foreground */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
257 int
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
258 PyMac_InForeground()
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
259 {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
260 static ProcessSerialNumber ours;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
261 static inited;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
262 ProcessSerialNumber curfg;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
263 Boolean eq;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
264
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
265 if ( inited == 0 )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
266 (void)GetCurrentProcess(&ours);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
267 inited = 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
268 if ( GetFrontProcess(&curfg) < 0 )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
269 eq = 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
270 else if ( SameProcess(&ours, &curfg, &eq) < 0 )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
271 eq = 1;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
272 return (int)eq;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
273
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
274 }
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
275
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
276 /*
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
277 ** Set yield timeouts
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
278 */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
279 void
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
280 PyMac_SetYield(long fgi, long fgy, long bgi, long bgy)
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
281 {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
282 interval_fg = fgi;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
283 yield_fg = fgy;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
284 interval_bg = bgi;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
285 yield_bg = bgy;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
286 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
287
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
288 /*
2276
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
289 ** Handle an event, either one found in the mainloop eventhandler or
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
290 ** one passed back from the python program.
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
291 */
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
292 void
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
293 PyMac_HandleEvent(evp)
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
294 EventRecord *evp;
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
295 {
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
296 WindowPtr wp;
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
297
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
298 #ifdef __MWERKS__
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
299 /* If SIOUX wants it we're done */
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
300 (void)SIOUXHandleOneEvent(evp);
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
301 #else
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
302 /* Other compilers are just unlucky: we only weed out clicks in other applications */
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
303 if ( evp->what == mouseDown ) {
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
304 if ( FindWindow(evp->where, &wp) == inSysWindow )
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
305 SystemClick(evp, wp);
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
306 }
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
307 #endif /* !__MWERKS__ */
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
308 }
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
309
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
310 /*
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
311 ** Yield the CPU to other tasks.
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
312 */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
313 static
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
314 PyMac_DoYield()
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
315 {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
316 EventRecord ev;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
317 long yield;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
318 static int no_waitnextevent = -1;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
319 int gotone;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
320
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
321 if ( no_waitnextevent < 0 ) {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
322 no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) ==
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
323 NGetTrapAddress(_Unimplemented, ToolTrap));
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
324 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
325
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
326 if ( !PyMac_DoYieldEnabled ) {
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
327 #ifndef THINK_C
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
328 /* Under think this has been done before in intrcheck() or intrpeek() */
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
329 scan_event_queue(0);
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
330 #endif
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
331 return;
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
332 }
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
333
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
334 in_foreground = PyMac_InForeground();
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
335 if ( in_foreground )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
336 yield = yield_fg;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
337 else
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
338 yield = yield_bg;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
339 while ( 1 ) {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
340 if ( no_waitnextevent ) {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
341 SystemTask();
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
342 gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev);
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
343 } else {
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
344 gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL);
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
345 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
346 /* Get out quickly if nothing interesting is happening */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
347 if ( !gotone || ev.what == nullEvent )
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
348 break;
2276
66d6d13df4f8 Added HandleEvent method to macos, so python programs can hand back
Jack Jansen <jack.jansen@cwi.nl>
parents: 2265
diff changeset
349 PyMac_HandleEvent(&ev);
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
350 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
351 lastyield = TickCount();
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
352 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
353
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
354 /*
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
355 ** Yield the CPU to other tasks if opportune
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
356 */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
357 void
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
358 PyMac_Yield() {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
359 long iv;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
360
2262
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
361 if ( in_foreground )
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
362 iv = interval_fg;
03bd79432f06 - Moved interrupt code to macglue.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 2255
diff changeset
363 else
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
364 iv = interval_bg;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
365 if ( TickCount() > lastyield + iv )
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
366 PyMac_DoYield();
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
367 }
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
368
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
369 /*
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
370 ** Idle routine for busy-wait loops.
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
371 ** Gives up CPU, handles events and returns true if an interrupt is pending
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
372 ** (but not actually handled yet).
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
373 */
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
374 int
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
375 PyMac_Idle()
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
376 {
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
377 PyMac_DoYield();
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
378 return intrpeek();
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
379 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
380
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
381 /*
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
382 ** Return the name of the Python directory
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
383 */
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
384 char *
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
385 PyMac_GetPythonDir()
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
386 {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
387 int item;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
388 static char name[256];
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
389 AliasHandle handle;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
390 FSSpec dirspec;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
391 int ok = 0, exists = 0;
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
392 Boolean modified = 0, cannotmodify = 0;
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
393 StandardFileReply sfreply;
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
394 short oldrh, prefrh;
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
395 short prefdirRefNum;
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
396 long prefdirDirID;
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
397
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
398 /*
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
399 ** Remember old resource file and try to open preferences file
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
400 ** in the preferences folder. If it doesn't exist we try to create
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
401 ** it. If anything fails here we limp on, but set cannotmodify so
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
402 ** we don't try to store things later on.
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
403 */
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
404 oldrh = CurResFile();
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
405 if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
406 &prefdirDirID) != noErr ) {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
407 /* Something wrong with preferences folder */
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
408 cannotmodify = 1;
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
409 } else {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
410 (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
411 prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
412 if ( prefrh == -1 ) {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
413 /* It doesn't exist. Try to create it */
2337
71e60871c681 ported to Think C
Guido van Rossum <guido@python.org>
parents: 2333
diff changeset
414 FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0);
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
415 prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
416 if ( prefrh == -1 ) {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
417 cannotmodify = 1;
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
418 } else {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
419 UseResFile(prefrh);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
420 }
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
421 }
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
422 }
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
423 /* So, we've opened our preferences file, we hope. Look for the alias */
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
424 handle = (AliasHandle)Get1Resource('alis', 128);
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
425 if ( handle ) {
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
426 /* It exists. Resolve it (possibly updating it) */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
427 if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr )
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
428 ok = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
429 exists = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
430 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
431 if ( !ok ) {
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
432 /* No luck, so far. ask the user for help */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
433 item = Alert(NOPYTHON_ALERT, NULL);
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
434 if ( item == YES_ITEM ) {
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
435 /* The user wants to point us to a directory. Let her do so */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
436 StandardGetFile(NULL, 0, NULL, &sfreply);
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
437 if ( sfreply.sfGood ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
438 if ( sfreply.sfIsFolder ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
439 dirspec = sfreply.sfFile;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
440 ok = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
441 modified = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
442 } else {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
443 /* User selected a file. Use folder containing it */
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
444 if (FSMakeFSSpec(sfreply.sfFile.vRefNum,
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
445 sfreply.sfFile.parID, "\p", &dirspec) == 0) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
446 ok = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
447 modified = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
448 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
449 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
450 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
451 } else if ( item == CURWD_ITEM ) {
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
452 /* The user told us the current directory is fine. Build an FSSpec for it */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
453 if ( getwd(name) ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
454 if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
455 ok = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
456 modified = 1;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
457 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
458 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
459 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
460 }
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
461 if ( ok && modified && !cannotmodify) {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
462 /* We have a new, valid fsspec and we can update the preferences file. Do so. */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
463 if ( !exists ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
464 if (NewAlias(NULL, &dirspec, &handle) == 0 )
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
465 AddResource((Handle)handle, 'alis', 128, "\p");
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
466 } else {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
467 ChangedResource((Handle)handle);
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
468 }
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
469 UpdateResFile(prefrh);
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
470 }
2333
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
471 if ( !cannotmodify ) {
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
472 /* This means we have the resfile open. Close it. */
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
473 CloseResFile(prefrh);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
474 }
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
475 /* Back to the old resource file */
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
476 UseResFile(oldrh);
6468fd0251d2 Changed PyMac_GetPythonDir to use a preferences file to store the
Jack Jansen <jack.jansen@cwi.nl>
parents: 2324
diff changeset
477 /* Now turn the fsspec into a path to give back to our caller */
2312
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
478 if ( ok ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
479 ok = (nfullpath(&dirspec, name) == 0);
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
480 if ( ok ) strcat(name, ":");
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
481 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
482 if ( !ok ) {
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
483 /* If all fails, we return the current directory */
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
484 name[0] = 0;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
485 (void)getwd(name);
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
486 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
487 return name;
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
488 }
40aec48cd1e2 Added PyMac_GetPythonDir routine which obtains python home directory
Jack Jansen <jack.jansen@cwi.nl>
parents: 2276
diff changeset
489
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
490 /*
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
491 ** Returns true if the argument has a resource fork, and it contains
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
492 ** a 'PYC ' resource of the correct name
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
493 */
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
494 int
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
495 PyMac_FindResourceModule(module, filename)
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
496 char *module;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
497 char *filename;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
498 {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
499 FSSpec fss;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
500 FInfo finfo;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
501 short oldrh, filerh;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
502 int ok;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
503 Handle h;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
504
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
505 if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr )
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
506 return 0; /* It doesn't exist */
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
507 if ( FSpGetFInfo(&fss, &finfo) != noErr )
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
508 return 0; /* shouldn't happen, I guess */
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
509 oldrh = CurResFile();
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
510 filerh = FSpOpenResFile(&fss, fsRdPerm);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
511 if ( filerh == -1 )
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
512 return 0;
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
513 UseResFile(filerh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
514 SetResLoad(0);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
515 h = Get1NamedResource('PYC ', Pstring(module));
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
516 SetResLoad(1);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
517 ok = (h != NULL);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
518 CloseResFile(filerh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
519 UseResFile(oldrh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
520 return ok;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
521 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
522
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
523 /*
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
524 ** Load the specified module from a resource
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
525 */
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
526 PyObject *
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
527 PyMac_LoadResourceModule(module, filename)
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
528 char *module;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
529 char *filename;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
530 {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
531 FSSpec fss;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
532 FInfo finfo;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
533 short oldrh, filerh;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
534 int ok;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
535 Handle h;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
536 OSErr err;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
537 PyObject *m, *co;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
538 long num, size;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
539
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
540 if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr )
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
541 goto error;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
542 if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
543 goto error;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
544 oldrh = CurResFile();
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
545 filerh = FSpOpenResFile(&fss, fsRdPerm);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
546 if ( filerh == -1 ) {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
547 err = ResError();
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
548 goto error;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
549 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
550 UseResFile(filerh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
551 h = Get1NamedResource('PYC ', Pstring(module));
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
552 if ( h == NULL ) {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
553 err = ResError();
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
554 goto error;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
555 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
556 HLock(h);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
557 /*
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
558 ** XXXX The next few lines are intimately tied to the format of pyc
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
559 ** files. I'm not sure whether this code should be here or in import.c -- Jack
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
560 */
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
561 size = GetHandleSize(h);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
562 if ( size < 8 ) {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
563 PyErr_SetString(PyExc_ImportError, "Resource too small");
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
564 co = NULL;
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
565 } else {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
566 num = (*h)[0] & 0xff;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
567 num = num | (((*h)[1] & 0xff) << 8);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
568 num = num | (((*h)[2] & 0xff) << 16);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
569 num = num | (((*h)[3] & 0xff) << 24);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
570 if ( num != PyImport_GetMagicNumber() ) {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
571 PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource");
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
572 co = NULL;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
573 } else {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
574 co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
575 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
576 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
577 HUnlock(h);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
578 CloseResFile(filerh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
579 UseResFile(oldrh);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
580 if ( co ) {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
581 m = PyImport_ExecCodeModule(module, co);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
582 Py_DECREF(co);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
583 } else {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
584 m = NULL;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
585 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
586 return m;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
587 error:
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
588 {
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
589 char buf[512];
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
590
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
591 sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
2350
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
592 PyErr_SetString(PyExc_ImportError, buf);
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
593 return NULL;
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
594 }
a2054933775b Added support for loading pyc code from a resource.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2337
diff changeset
595 }
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
596
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
597 /* Convert a 4-char string object argument to an OSType value */
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
598 int
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
599 PyMac_GetOSType(PyObject *v, OSType *pr)
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
600 {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
601 if (!PyString_Check(v) || PyString_Size(v) != 4) {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
602 PyErr_SetString(PyExc_TypeError,
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
603 "OSType arg must be string of 4 chars");
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
604 return 0;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
605 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
606 memcpy((char *)pr, PyString_AsString(v), 4);
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
607 return 1;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
608 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
609
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
610 /* Convert an OSType value to a 4-char string object */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
611 PyObject *
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
612 PyMac_BuildOSType(OSType t)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
613 {
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
614 return PyString_FromStringAndSize((char *)&t, 4);
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
615 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
616
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
617
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
618 /* Convert a Python string object to a Str255 */
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
619 int
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
620 PyMac_GetStr255(PyObject *v, Str255 pbuf)
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
621 {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
622 int len;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
623 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
624 PyErr_SetString(PyExc_TypeError,
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
625 "Str255 arg must be string of at most 255 chars");
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
626 return 0;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
627 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
628 pbuf[0] = len;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
629 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
2035
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
630 return 1;
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
631 }
ce69742f2e77 New routines:
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
632
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
633 /* Convert a Str255 to a Python string object */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
634 PyObject *
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
635 PyMac_BuildStr255(Str255 s)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
636 {
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
637 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
638 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
639
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
640
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
641 /*
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
642 ** Convert a Python object to an FSSpec.
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
643 ** The object may either be a full pathname or a triple
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
644 ** (vrefnum, dirid, path).
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
645 ** NOTE: This routine will fail on pre-sys7 machines.
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
646 ** The caller is responsible for not calling this routine
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
647 ** in those cases (which is fine, since everyone calling
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
648 ** this is probably sys7 dependent anyway).
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
649 */
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
650 int
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
651 PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
652 {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
653 Str255 path;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
654 short refnum;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
655 long parid;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
656 OSErr err;
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
657 FSSpec *fs2;
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
658
2251
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
659 /* first check whether it already is an FSSpec */
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
660 fs2 = mfs_GetFSSpecFSSpec(v);
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
661 if ( fs2 ) {
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
662 fs = fs2;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
663 return 1;
e0a3242d5ae7 Added eventhandling in mainloop/busywait routine plus leaving some
Jack Jansen <jack.jansen@cwi.nl>
parents: 2231
diff changeset
664 }
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
665 if ( PyString_Check(v) ) {
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
666 /* It's a pathname */
2209
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
667 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
668 return 0;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
669 refnum = 0; /* XXXX Should get CurWD here... */
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
670 parid = 0;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
671 } else {
2209
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
672 if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
673 &refnum, &parid, PyMac_GetStr255, &path)) {
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
674 return 0;
2209
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
675 }
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
676 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
677 err = FSMakeFSSpec(refnum, parid, path, fs);
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
678 if ( err && err != fnfErr ) {
2209
4053d4cb50e0 renamed Get routines to PyMac_Get; added macfs to config.c
Guido van Rossum <guido@python.org>
parents: 2183
diff changeset
679 PyErr_Mac(PyExc_ValueError, err);
2176
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
680 return 0;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
681 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
682 return 1;
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
683 }
1e9e74944bb5 - Moved some useful routines from macosmodule.c here
Jack Jansen <jack.jansen@cwi.nl>
parents: 2136
diff changeset
684
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
685
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
686
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
687 /* Convert a Python object to a Rect.
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
688 The object must be a (left, top, right, bottom) tuple.
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
689 (This differs from the order in the struct but is consistent with
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
690 the arguments to SetRect(), and also with STDWIN). */
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
691 int
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
692 PyMac_GetRect(PyObject *v, Rect *r)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
693 {
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
694 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
695 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
696
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
697 /* Convert a Rect to a Python object */
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
698 PyObject *
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
699 PyMac_BuildRect(Rect *r)
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
700 {
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
701 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
702 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
703
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
704
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
705 /* Convert a Python object to a Point.
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
706 The object must be a (h, v) tuple.
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
707 (This differs from the order in the struct but is consistent with
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
708 the arguments to SetPoint(), and also with STDWIN). */
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
709 int
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
710 PyMac_GetPoint(PyObject *v, Point *p)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
711 {
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
712 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
2183
62bdded5f00a modernized/rationalized, added PyMac_BuildOSType
Guido van Rossum <guido@python.org>
parents: 2176
diff changeset
713 }
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
714
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
715 /* Convert a Point to a Python object */
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
716 PyObject *
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
717 PyMac_BuildPoint(Point p)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
718 {
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
719 return Py_BuildValue("(hh)", p.h, p.v);
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
720 }
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
721
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
722
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
723 /* Convert a Python object to an EventRecord.
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
724 The object must be a (what, message, when, (v, h), modifiers) tuple. */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
725 int
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
726 PyMac_GetEventRecord(PyObject *v, EventRecord *e)
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
727 {
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
728 return PyArg_Parse(v, "(hll(hh)h)",
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
729 &e->what,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
730 &e->message,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
731 &e->when,
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
732 &e->where.h,
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
733 &e->where.v,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
734 &e->modifiers);
2224
32435d33f34a cosmetics: copyright, comments
Guido van Rossum <guido@python.org>
parents: 2209
diff changeset
735 }
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
736
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
737 /* Convert a Rect to an EventRecord object */
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
738 PyObject *
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
739 PyMac_BuildEventRecord(EventRecord *e)
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
740 {
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
741 return Py_BuildValue("(hll(hh)h)",
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
742 e->what,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
743 e->message,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
744 e->when,
2255
382edef2d6d7 reverse h/v in Point and Rect
Guido van Rossum <guido@python.org>
parents: 2251
diff changeset
745 e->where.h,
2231
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
746 e->where.v,
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
747 e->modifiers);
1e8b347a056c Some layout changes; added EventRecord Get/Build functions (as a tuple).
Guido van Rossum <guido@python.org>
parents: 2224
diff changeset
748 }
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
749
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
750
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
751 /* ---------- */
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
752 /* Applet support */
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
753
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
754 /* Run a compiled Python Python script from 'PYC ' resource __main__ */
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
755 static int
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
756 run_main_resource()
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
757 {
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
758 Handle h;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
759 long size;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
760 PyObject *code;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
761 PyObject *result;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
762
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
763 h = GetNamedResource('PYC ', "\p__main__");
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
764 if (h == NULL) {
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
765 fprintf(stderr, "No 'PYC ' resource named __main__ found\n");
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
766 return 1;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
767 }
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
768 size = GetResourceSizeOnDisk(h);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
769 HLock(h);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
770 code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
771 HUnlock(h);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
772 ReleaseResource(h);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
773 if (code == NULL) {
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
774 PyErr_Print();
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
775 return 1;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
776 }
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
777 result = PyImport_ExecCodeModule("__main__", code);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
778 Py_DECREF(code);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
779 if (result == NULL) {
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
780 PyErr_Print();
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
781 return 1;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
782 }
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
783 Py_DECREF(result);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
784 return 0;
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
785 }
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
786
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
787 /* Initialization sequence for applets */
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
788 void
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
789 PyMac_InitApplet()
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
790 {
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
791 int argc;
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
792 char **argv;
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
793
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
794 argc = PyMac_GetArgv(&argv);
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
795 Py_Initialize();
2394
fa8cdd3cc545 use PyMac_GetArgv (in macgetargv.c) for applet inits
Guido van Rossum <guido@python.org>
parents: 2382
diff changeset
796 PySys_SetArgv(argc, argv);
2382
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
797 run_main_resource();
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
798 fflush(stderr);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
799 fflush(stdout);
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
800 /* XXX Should we bother to Py_Exit(sts)? */
6450d8828d2b added applet initialization;
Guido van Rossum <guido@python.org>
parents: 2350
diff changeset
801 }