]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
* Import/pythonrun.h, Python/{import,pythonrun}.c,
authorGuido van Rossum <guido@python.org>
Wed, 14 Sep 1994 13:31:04 +0000 (13:31 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 14 Sep 1994 13:31:04 +0000 (13:31 +0000)
mac/macsetfiletype.c: changes by Jack to execute .pyc file passed
as command line argument.  On the Mac .pyc files are given a
special type so they can be double-clicked

Python/import.c
Python/pythonrun.c

index 1a13fcfaf3c52571c32180fbd207900f8e834f08..f65504193349cceb2561d6982c4013daeeb9a00a 100644 (file)
@@ -176,6 +176,14 @@ static object *modules;
 /* Forward */
 static int init_builtin PROTO((char *));
 
+/* Helper for reading .pyc files */
+
+long
+get_pyc_magic()
+{
+       return MAGIC;
+}
+
 /* Initialization */
 
 void
@@ -482,6 +490,9 @@ get_module(m, name, m_ret)
                /* Now write the code object to the ".pyc" file */
                strcpy(namebuf + len, "c");
                fpc = fopen(namebuf, "wb");
+#ifdef macintosh
+               setfiletype(namebuf, 'PYTH', 'PYC ');
+#endif
                if (fpc == NULL) {
                        if (verbose)
                                fprintf(stderr,
index 8387eed027bc5610c48b2bd3a43c68838eada78e..e547f9238a0c11599127780a286723855f80e699 100644 (file)
@@ -37,6 +37,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #include "ceval.h"
 #include "pythonrun.h"
 #include "import.h"
+#include "marshal.h"
 
 #ifdef HAVE_SIGNAL_H
 #include <signal.h>
@@ -192,11 +193,24 @@ run_script(fp, filename)
        char *filename;
 {
        object *m, *d, *v;
+       char *ext;
+
        m = add_module("__main__");
        if (m == NULL)
                return -1;
        d = getmoduledict(m);
-       v = run_file(fp, filename, file_input, d, d);
+       ext = filename + strlen(filename) - 4;
+       if ( strcmp(ext, ".pyc") == 0 ) {
+               /* Try to run a pyc file. First, re-open in binary */
+               fclose(fp);
+               if( (fp = fopen(filename, "rb")) == NULL ) {
+                       fprintf(stderr, "python: Can't reopen .pyc file\n");
+                       return -1;
+               }
+               v = run_pyc_file(fp, filename, d, d);
+       } else {
+               v = run_file(fp, filename, file_input, d, d);
+       }
        flushline();
        if (v == NULL) {
                print_error();
@@ -356,6 +370,38 @@ run_node(n, filename, globals, locals)
        return v;
 }
 
+object *
+run_pyc_file(fp, filename, globals, locals)
+       FILE *fp;
+       char *filename;
+       object *globals, *locals;
+{
+       codeobject *co;
+       object *v;
+       long magic;
+       long get_pyc_magic();
+
+       magic = rd_long(fp);
+       if (magic != get_pyc_magic()) {
+               err_setstr(RuntimeError,
+                          "Bad magic number in .pyc file");
+               return NULL;
+       }
+       (void) rd_long(fp);
+       v = rd_object(fp);
+       fclose(fp);
+       if (v == NULL || !is_codeobject(v)) {
+               XDECREF(v);
+               err_setstr(RuntimeError,
+                          "Bad code object in .pyc file");
+               return NULL;
+       }
+       co = (codeobject *)v;
+       v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
+       DECREF(co);
+       return v;
+}
+
 object *
 compile_string(str, filename, start)
        char *str;