]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Support for frozen scripts; added -i option.
authorGuido van Rossum <guido@python.org>
Thu, 1 Apr 1993 20:59:32 +0000 (20:59 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 1 Apr 1993 20:59:32 +0000 (20:59 +0000)
Include/marshal.h
Modules/config.c.in
Python/frozenmain.c [new file with mode: 0644]
Python/import.c
Python/marshal.c
Python/pythonmain.c

index 9b16227af58564846aa7618d7f84fb77c9b8e29b..1adc2adf51fd312e480f96f5c844ba1a3fbe0572 100644 (file)
@@ -31,3 +31,4 @@ void wr_object PROTO((object *, FILE *));
 long rd_long PROTO((FILE *));
 int rd_short PROTO((FILE *));
 object *rd_object PROTO((FILE *));
+object *rds_object PROTO((char *, int));
index 7dba37ce0c0a86ffdf0393fb5f037de2f237f2d9..fe48023d3f2b2171977ab02d9ebe9f28154c9f06 100644 (file)
@@ -82,6 +82,8 @@ char version[80];
 
 char *argv0; /* For dynamic loading in import.c */
 
+extern char verbose;
+
 /*ARGSUSED*/
 void
 initargs(p_argc, p_argv)
@@ -98,7 +100,11 @@ initargs(p_argc, p_argv)
 #endif
        wargs(p_argc, p_argv);
 #endif /* USE_STDWIN */
-       if (*p_argc < 2 && isatty(0) && isatty(1))
+#ifdef USE_FROZEN
+       if (verbose)
+#else
+       if (verbose || *p_argc < 2 && isatty(0) && isatty(1))
+#endif
        {
                printf("Python %s.\n", version);
                printf(
@@ -448,3 +454,15 @@ struct {
 
        {0,             0}              /* Sentinel */
 };
+
+#ifdef USE_FROZEN
+#include "frozen.c"
+#else
+struct frozen {
+       char *name;
+       char *code;
+       int size;
+} frozen_modules[] = {
+       {0, 0, 0}
+};
+#endif
diff --git a/Python/frozenmain.c b/Python/frozenmain.c
new file mode 100644 (file)
index 0000000..90d1623
--- /dev/null
@@ -0,0 +1,57 @@
+/***********************************************************
+Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
+Amsterdam, The Netherlands.
+
+                        All Rights Reserved
+
+Permission to use, copy, modify, and distribute this software and its 
+documentation for any purpose and without fee is hereby granted, 
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in 
+supporting documentation, and that the names of Stichting Mathematisch
+Centrum or CWI not be used in advertising or publicity pertaining to
+distribution of the software without specific, written prior permission.
+
+STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
+THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
+FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+******************************************************************/
+
+/* Python interpreter main program for frozen scripts */
+
+#include "allobjects.h"
+
+extern char *getenv();
+
+extern int debugging;
+extern int verbose;
+
+main(argc, argv)
+       int argc;
+       char **argv;
+{
+       char *p;
+       int n;
+       if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
+               debugging = 1;
+       if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
+               verbose = 1;
+       initargs(&argc, &argv); /* Defined in config*.c */
+       initall();
+       setpythonargv(argc, argv);
+       n = init_frozen("__main__");
+       if (n == 0)
+               fatal("__main__ not frozen");
+       if (n < 0) {
+               print_error();
+               goaway(1);
+       }
+       else
+               goaway(0);
+       /*NOTREACHED*/
+}
index 2171f4b323596d8488c88e81c15f391d394f72b2..4310afa1f8ceaba57a2956c5134c63fd9834d46f 100644 (file)
@@ -52,7 +52,7 @@ extern int verbose; /* Defined in pythonmain.c */
 extern char *argv0;
 #endif
 
-/* Magic word to reject pre-0.9.9 .pyc files */
+/* Magic word to reject .pyc files generated by other Python versions */
 
 #define MAGIC 0x99BE3AL
 
@@ -325,8 +325,11 @@ import_module(name)
        char *name;
 {
        object *m;
+       int n;
        if ((m = dictlookup(modules, name)) == NULL) {
-               if (init_builtin(name)) {
+               if ((n = init_builtin(name)) || (n = init_frozen(name))) {
+                       if (n < 0)
+                               return NULL;
                        if ((m = dictlookup(modules, name)) == NULL)
                                err_setstr(SystemError,
                                           "builtin module missing");
@@ -411,3 +414,38 @@ init_builtin(name)
        }
        return 0;
 }
+
+extern struct frozen {
+       char *name;
+       char *code;
+       int size;
+} frozen_modules[];
+
+int
+init_frozen(name)
+       char *name;
+{
+       struct frozen *p;
+       codeobject *co;
+       object *m, *d, *v;
+       for (p = frozen_modules; ; p++) {
+               if (p->name == NULL)
+                       return 0;
+               if (strcmp(p->name, name) == 0)
+                       break;
+       }
+       if (verbose)
+               fprintf(stderr, "import %s # frozen\n", name);
+       co = (codeobject *) rds_object(p->code, p->size);
+       if (co == NULL)
+               return -1;
+       if ((m = add_module(name)) == NULL ||
+           (d = getmoduledict(m)) == NULL ||
+           (v = eval_code(co, d, d, (object *)NULL)) == NULL) {
+               DECREF(co);
+               return -1;
+       }
+       DECREF(co);
+       DECREF(v);
+       return 1;
+}
index 65f7f2d47fce4b663a126e38e2465c7c732ff386..3853ff9fb3d19960cd25857915f3c5394701d776 100644 (file)
@@ -225,7 +225,8 @@ wr_object(x, fp)
 typedef WFILE RFILE; /* Same struct with different invariants */
 
 #define r_byte(p) ((p)->fp ? getc((p)->fp) \
-                         : ((p)->ptr != (p)->end) ? *(p)->ptr++ : EOF)
+                         : ((p)->ptr != (p)->end) ? \
+                           (unsigned char)*(p)->ptr++ : EOF)
 
 static int
 r_string(s, n, p)
@@ -425,6 +426,19 @@ rd_object(fp)
        return r_object(&rf);
 }
 
+object *
+rds_object(str, len)
+       char *str;
+       int len;
+{
+       RFILE rf;
+       rf.fp = NULL;
+       rf.str = NULL;
+       rf.ptr = str;
+       rf.end = str + len;
+       return r_object(&rf);
+}
+
 /* And an interface for Python programs... */
 
 static object *
index 171861127f23adea925aa4889f6dbdd51470c004..760e5dab2e4f6ef4c7f1a7ac91a7d2b146de3b42 100644 (file)
@@ -34,6 +34,8 @@ extern int optind;
 extern char *optarg;
 extern int getopt PROTO((int, char **, char *));
 
+extern char *getenv();
+
 main(argc, argv)
        int argc;
        char **argv;
@@ -43,10 +45,17 @@ main(argc, argv)
        char *command = NULL;
        char *filename = NULL;
        FILE *fp = stdin;
+       char *p;
+       int inspect = 0;
+
+       if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
+               debugging = 1;
+       if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
+               verbose = 1;
        
        initargs(&argc, &argv); /* Defined in config*.c */
 
-       while ((c = getopt(argc, argv, "c:dv")) != EOF) {
+       while ((c = getopt(argc, argv, "c:div")) != EOF) {
                if (c == 'c') {
                        /* -c is the last option; following arguments
                           that look like options are left for the
@@ -64,6 +73,10 @@ main(argc, argv)
                        debugging++;
                        break;
 
+               case 'i':
+                       inspect++;
+                       break;
+
                case 'v':
                        verbose++;
                        break;
@@ -118,6 +131,10 @@ main(argc, argv)
                sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
        }
 
+       if (inspect && isatty((int)fileno(stdin)) &&
+           (filename != NULL || command != NULL))
+               sts = run(stdin, "<stdin>") != 0;
+
        goaway(sts);
        /*NOTREACHED*/
 }