diff Mac/Python/macglue.c @ 2382:6450d8828d2b legacy-trunk

added applet initialization; fewer restrictions on loading modules from resources (+ fix!); macstrerror -> MacOS_StrError
author Guido van Rossum <guido@python.org>
date Sat, 18 Feb 1995 15:01:31 +0000
parents a2054933775b
children fa8cdd3cc545
line wrap: on
line diff
--- a/Mac/Python/macglue.c	Sat Feb 18 14:59:48 1995 +0000
+++ b/Mac/Python/macglue.c	Sat Feb 18 15:01:31 1995 +0000
@@ -40,10 +40,16 @@
 #include <Desk.h>
 #include <Traps.h>
 #include <Processes.h>
+#include <Fonts.h>
+#include <Menus.h>
 #ifdef THINK_C
 #include <OSEvents.h> /* For EvQElPtr */
 #endif
 
+#ifndef HAVE_UNIVERSAL_HEADERS
+#define GetResourceSizeOnDisk(x) SizeResource(x)
+#endif
+
 #include <signal.h>
 #include <stdio.h>
 
@@ -116,8 +122,8 @@
 	return buf;
 }
 
-/* Replace strerror with something that might work */
-char *macstrerror(int err)
+/* Like strerror() but for Mac OS error numbers */
+char *PyMac_StrError(int err)
 {
 	static char buf[256];
 	Handle h;
@@ -162,7 +168,7 @@
 	}
 	if (err == -1 && PyErr_Occurred())
 		return NULL;
-	msg = macstrerror(err);
+	msg = PyMac_StrError(err);
 	v = Py_BuildValue("(is)", err, msg);
 	PyErr_SetObject(eobj, v);
 	Py_DECREF(v);
@@ -500,12 +506,10 @@
 		return 0;			/* It doesn't exist */
 	if ( FSpGetFInfo(&fss, &finfo) != noErr )
 		return 0;			/* shouldn't happen, I guess */
-	if ( finfo.fdType != 'rsrc' || finfo.fdCreator != 'PYTH' )
-		return 0;			/* Not the right type */
 	oldrh = CurResFile();
 	filerh = FSpOpenResFile(&fss, fsRdPerm);
 	if ( filerh == -1 )
-		return 0;			/* Again, shouldn't happen */
+		return 0;
 	UseResFile(filerh);
 	SetResLoad(0);
 	h = Get1NamedResource('PYC ', Pstring(module));
@@ -537,10 +541,6 @@
 		goto error;
 	if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
 		goto error;
-	if ( finfo.fdType != 'rsrc' || finfo.fdCreator != 'PYTH' ) {
-		PyErr_SetString(PyExc_ImportError, "Incorrect typed file in sys.path");
-		return NULL;
-	}
 	oldrh = CurResFile();
 	filerh = FSpOpenResFile(&fss, fsRdPerm);
 	if ( filerh == -1 ) {
@@ -561,7 +561,7 @@
 	size = GetHandleSize(h);
 	if ( size < 8 ) {
 		PyErr_SetString(PyExc_ImportError, "Resource too small");
-		m = NULL;
+		co = NULL;
 	} else {
 		num = (*h)[0] & 0xff;
 		num = num | (((*h)[1] & 0xff) << 8);
@@ -588,7 +588,7 @@
 	{
 		char buf[512];
 		
-		sprintf(buf, "%s: %s", filename, macstrerror(err));
+		sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
 		PyErr_SetString(PyExc_ImportError, buf);
 		return NULL;
 	}
@@ -746,3 +746,65 @@
 	                     e->where.v,
 	                     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)? */
+}