Support for frozen scripts; added -i option.

This commit is contained in:
Guido van Rossum 1993-04-01 20:59:32 +00:00
parent 41ffccbba7
commit f56e3db1dd
6 changed files with 150 additions and 5 deletions

View 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));

View 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

57
Python/frozenmain.c Normal file
View File

@ -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*/
}

View 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;
}

View 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 *

View 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*/
}