Kaydet (Commit) c3d1c8e9 authored tarafından Guido van Rossum's avatar Guido van Rossum

added applet initialization;

fewer restrictions on loading modules from resources (+ fix!);
macstrerror -> MacOS_StrError
üst f71a9a9c
...@@ -40,10 +40,16 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -40,10 +40,16 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <Desk.h> #include <Desk.h>
#include <Traps.h> #include <Traps.h>
#include <Processes.h> #include <Processes.h>
#include <Fonts.h>
#include <Menus.h>
#ifdef THINK_C #ifdef THINK_C
#include <OSEvents.h> /* For EvQElPtr */ #include <OSEvents.h> /* For EvQElPtr */
#endif #endif
#ifndef HAVE_UNIVERSAL_HEADERS
#define GetResourceSizeOnDisk(x) SizeResource(x)
#endif
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
...@@ -116,8 +122,8 @@ Pstring(char *str) ...@@ -116,8 +122,8 @@ Pstring(char *str)
return buf; return buf;
} }
/* Replace strerror with something that might work */ /* Like strerror() but for Mac OS error numbers */
char *macstrerror(int err) char *PyMac_StrError(int err)
{ {
static char buf[256]; static char buf[256];
Handle h; Handle h;
...@@ -162,7 +168,7 @@ PyErr_Mac(PyObject *eobj, int err) ...@@ -162,7 +168,7 @@ PyErr_Mac(PyObject *eobj, int err)
} }
if (err == -1 && PyErr_Occurred()) if (err == -1 && PyErr_Occurred())
return NULL; return NULL;
msg = macstrerror(err); msg = PyMac_StrError(err);
v = Py_BuildValue("(is)", err, msg); v = Py_BuildValue("(is)", err, msg);
PyErr_SetObject(eobj, v); PyErr_SetObject(eobj, v);
Py_DECREF(v); Py_DECREF(v);
...@@ -500,12 +506,10 @@ char *filename; ...@@ -500,12 +506,10 @@ char *filename;
return 0; /* It doesn't exist */ return 0; /* It doesn't exist */
if ( FSpGetFInfo(&fss, &finfo) != noErr ) if ( FSpGetFInfo(&fss, &finfo) != noErr )
return 0; /* shouldn't happen, I guess */ return 0; /* shouldn't happen, I guess */
if ( finfo.fdType != 'rsrc' || finfo.fdCreator != 'PYTH' )
return 0; /* Not the right type */
oldrh = CurResFile(); oldrh = CurResFile();
filerh = FSpOpenResFile(&fss, fsRdPerm); filerh = FSpOpenResFile(&fss, fsRdPerm);
if ( filerh == -1 ) if ( filerh == -1 )
return 0; /* Again, shouldn't happen */ return 0;
UseResFile(filerh); UseResFile(filerh);
SetResLoad(0); SetResLoad(0);
h = Get1NamedResource('PYC ', Pstring(module)); h = Get1NamedResource('PYC ', Pstring(module));
...@@ -537,10 +541,6 @@ char *filename; ...@@ -537,10 +541,6 @@ char *filename;
goto error; goto error;
if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
goto error; goto error;
if ( finfo.fdType != 'rsrc' || finfo.fdCreator != 'PYTH' ) {
PyErr_SetString(PyExc_ImportError, "Incorrect typed file in sys.path");
return NULL;
}
oldrh = CurResFile(); oldrh = CurResFile();
filerh = FSpOpenResFile(&fss, fsRdPerm); filerh = FSpOpenResFile(&fss, fsRdPerm);
if ( filerh == -1 ) { if ( filerh == -1 ) {
...@@ -561,7 +561,7 @@ char *filename; ...@@ -561,7 +561,7 @@ char *filename;
size = GetHandleSize(h); size = GetHandleSize(h);
if ( size < 8 ) { if ( size < 8 ) {
PyErr_SetString(PyExc_ImportError, "Resource too small"); PyErr_SetString(PyExc_ImportError, "Resource too small");
m = NULL; co = NULL;
} else { } else {
num = (*h)[0] & 0xff; num = (*h)[0] & 0xff;
num = num | (((*h)[1] & 0xff) << 8); num = num | (((*h)[1] & 0xff) << 8);
...@@ -588,7 +588,7 @@ error: ...@@ -588,7 +588,7 @@ error:
{ {
char buf[512]; char buf[512];
sprintf(buf, "%s: %s", filename, macstrerror(err)); sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
PyErr_SetString(PyExc_ImportError, buf); PyErr_SetString(PyExc_ImportError, buf);
return NULL; return NULL;
} }
...@@ -746,3 +746,65 @@ PyMac_BuildEventRecord(EventRecord *e) ...@@ -746,3 +746,65 @@ PyMac_BuildEventRecord(EventRecord *e)
e->where.v, e->where.v,
e->modifiers); e->modifiers);
} }
/* What follows is used only by applets. */
static void
init_mac_world()
{
MaxApplZone();
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
TEInit();
InitDialogs((long)0);
InitMenus();
InitCursor();
}
static int
run_main_resource()
{
Handle h;
long size;
PyObject *code;
PyObject *result;
h = GetNamedResource('PYC ', "\p__main__");
if (h == NULL) {
fprintf(stderr, "No 'PYC ' resource named __main__ found\n");
return 1;
}
size = GetResourceSizeOnDisk(h);
HLock(h);
code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8));
HUnlock(h);
ReleaseResource(h);
if (code == NULL) {
PyErr_Print();
return 1;
}
result = PyImport_ExecCodeModule("__main__", code);
Py_DECREF(code);
if (result == NULL) {
PyErr_Print();
return 1;
}
Py_DECREF(result);
return 0;
}
void
PyMac_InitApplet()
{
static char *argv[] = {"__main__", NULL};
init_mac_world();
Py_Initialize();
PySys_SetArgv((sizeof argv / sizeof argv[0]) - 1, argv);
run_main_resource();
fflush(stderr);
fflush(stdout);
/* XXX Should we bother to Py_Exit(sts)? */
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment