]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Added support for loading pyc code from a resource.
authorJack Jansen <jack.jansen@cwi.nl>
Wed, 15 Feb 1995 22:58:33 +0000 (22:58 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Wed, 15 Feb 1995 22:58:33 +0000 (22:58 +0000)
Mac/Include/macglue.h
Mac/Python/macglue.c

index a2990167d64e017d0350588f988cd80ecceada8d..695a045ef23f755855f20d1fc2225584f709140e 100644 (file)
@@ -52,6 +52,9 @@ int PyMac_Idle(void);                         /* Idle routine */
 
 char *PyMac_GetPythonDir();                                    /* Return the name of the python dir */
 
+int PyMac_FindResourceModule(char *, char *); /* Test for 'PYC ' resource in a file */
+PyObject * PyMac_LoadResourceModule(char *, char *); /* Load 'PYC ' resource from file */
+
 int PyMac_GetOSType(PyObject *, OSType *);     /* argument parser for OSType */
 PyObject *PyMac_BuildOSType(OSType);           /* Convert OSType to PyObject */
 
index c979e2a5dda37be09d8de67b0eacf7850128c635..a612a9a0f54addfda0fc3a7ac8d47e90f72a742a 100644 (file)
@@ -25,6 +25,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include "Python.h"
 
 #include "macglue.h"
+#include "marshal.h"
+#include "import.h"
 
 #include <OSUtils.h> /* for Set(Current)A5 */
 #include <Files.h>
@@ -43,6 +45,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #endif
 
 #include <signal.h>
+#include <stdio.h>
 
 #define NOPYTHON_ALERT 128
 #define YES_ITEM 1
@@ -478,6 +481,118 @@ PyMac_GetPythonDir()
        return name;
 }
 
+/*
+** Returns true if the argument has a resource fork, and it contains
+** a 'PYC ' resource of the correct name
+*/
+int
+PyMac_FindResourceModule(module, filename)
+char *module;
+char *filename;
+{
+       FSSpec fss;
+       FInfo finfo;
+       short oldrh, filerh;
+       int ok;
+       Handle h;
+       
+       if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr )
+               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 */
+       UseResFile(filerh);
+       SetResLoad(0);
+       h = Get1NamedResource('PYC ', Pstring(module));
+       SetResLoad(1);
+       ok = (h != NULL);
+       CloseResFile(filerh);
+       UseResFile(oldrh);
+       return ok;
+}
+
+/*
+** Load the specified module from a resource
+*/
+PyObject *
+PyMac_LoadResourceModule(module, filename)
+char *module;
+char *filename;
+{
+       FSSpec fss;
+       FInfo finfo;
+       short oldrh, filerh;
+       int ok;
+       Handle h;
+       OSErr err;
+       PyObject *m, *co;
+       long num, size;
+       
+       if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr )
+               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 ) {
+               err = ResError();
+               goto error;
+       }
+       UseResFile(filerh);
+       h = Get1NamedResource('PYC ', Pstring(module));
+       if ( h == NULL ) {
+               err = ResError();
+               goto error;
+       }
+       HLock(h);
+       /*
+       ** XXXX The next few lines are intimately tied to the format of pyc
+       ** files. I'm not sure whether this code should be here or in import.c -- Jack
+       */
+       size = GetHandleSize(h);
+       if ( size < 8 ) {
+               PyErr_SetString(PyExc_ImportError, "Resource too small");
+               m = NULL;
+       } else {
+               num = (*h)[0] & 0xff;
+               num = num | (((*h)[1] & 0xff) << 8);
+               num = num | (((*h)[2] & 0xff) << 16);
+               num = num | (((*h)[3] & 0xff) << 24);
+               if ( num != PyImport_GetMagicNumber() ) {
+                       PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource");
+                       co = NULL;
+               } else {
+                       co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
+               }
+       }
+       HUnlock(h);
+       CloseResFile(filerh);
+       UseResFile(oldrh);
+       if ( co ) {
+               m = PyImport_ExecCodeModule(module, co);
+               Py_DECREF(co);
+       } else {
+               m = NULL;
+       }
+       return m;
+error:
+       {
+               char buf[512];
+               
+               sprintf(buf, "%s: %s", filename, macstrerror(err));
+               PyErr_SetString(PyExc_ImportError, buf);
+               return NULL;
+       }
+}
 
 /* Convert a 4-char string object argument to an OSType value */
 int