mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Support for frozen scripts; added -i option.
This commit is contained in:
parent
41ffccbba7
commit
f56e3db1dd
6 changed files with 150 additions and 5 deletions
|
@ -31,3 +31,4 @@ void wr_object PROTO((object *, FILE *));
|
||||||
long rd_long PROTO((FILE *));
|
long rd_long PROTO((FILE *));
|
||||||
int rd_short PROTO((FILE *));
|
int rd_short PROTO((FILE *));
|
||||||
object *rd_object PROTO((FILE *));
|
object *rd_object PROTO((FILE *));
|
||||||
|
object *rds_object PROTO((char *, int));
|
||||||
|
|
|
@ -82,6 +82,8 @@ char version[80];
|
||||||
|
|
||||||
char *argv0; /* For dynamic loading in import.c */
|
char *argv0; /* For dynamic loading in import.c */
|
||||||
|
|
||||||
|
extern char verbose;
|
||||||
|
|
||||||
/*ARGSUSED*/
|
/*ARGSUSED*/
|
||||||
void
|
void
|
||||||
initargs(p_argc, p_argv)
|
initargs(p_argc, p_argv)
|
||||||
|
@ -98,7 +100,11 @@ initargs(p_argc, p_argv)
|
||||||
#endif
|
#endif
|
||||||
wargs(p_argc, p_argv);
|
wargs(p_argc, p_argv);
|
||||||
#endif /* USE_STDWIN */
|
#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("Python %s.\n", version);
|
||||||
printf(
|
printf(
|
||||||
|
@ -448,3 +454,15 @@ struct {
|
||||||
|
|
||||||
{0, 0} /* Sentinel */
|
{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
57
Python/frozenmain.c
Normal 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*/
|
||||||
|
}
|
|
@ -52,7 +52,7 @@ extern int verbose; /* Defined in pythonmain.c */
|
||||||
extern char *argv0;
|
extern char *argv0;
|
||||||
#endif
|
#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
|
#define MAGIC 0x99BE3AL
|
||||||
|
|
||||||
|
@ -325,8 +325,11 @@ import_module(name)
|
||||||
char *name;
|
char *name;
|
||||||
{
|
{
|
||||||
object *m;
|
object *m;
|
||||||
|
int n;
|
||||||
if ((m = dictlookup(modules, name)) == NULL) {
|
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)
|
if ((m = dictlookup(modules, name)) == NULL)
|
||||||
err_setstr(SystemError,
|
err_setstr(SystemError,
|
||||||
"builtin module missing");
|
"builtin module missing");
|
||||||
|
@ -411,3 +414,38 @@ init_builtin(name)
|
||||||
}
|
}
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -225,7 +225,8 @@ wr_object(x, fp)
|
||||||
typedef WFILE RFILE; /* Same struct with different invariants */
|
typedef WFILE RFILE; /* Same struct with different invariants */
|
||||||
|
|
||||||
#define r_byte(p) ((p)->fp ? getc((p)->fp) \
|
#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
|
static int
|
||||||
r_string(s, n, p)
|
r_string(s, n, p)
|
||||||
|
@ -425,6 +426,19 @@ rd_object(fp)
|
||||||
return r_object(&rf);
|
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... */
|
/* And an interface for Python programs... */
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
|
|
|
@ -34,6 +34,8 @@ extern int optind;
|
||||||
extern char *optarg;
|
extern char *optarg;
|
||||||
extern int getopt PROTO((int, char **, char *));
|
extern int getopt PROTO((int, char **, char *));
|
||||||
|
|
||||||
|
extern char *getenv();
|
||||||
|
|
||||||
main(argc, argv)
|
main(argc, argv)
|
||||||
int argc;
|
int argc;
|
||||||
char **argv;
|
char **argv;
|
||||||
|
@ -43,10 +45,17 @@ main(argc, argv)
|
||||||
char *command = NULL;
|
char *command = NULL;
|
||||||
char *filename = NULL;
|
char *filename = NULL;
|
||||||
FILE *fp = stdin;
|
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 */
|
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') {
|
if (c == 'c') {
|
||||||
/* -c is the last option; following arguments
|
/* -c is the last option; following arguments
|
||||||
that look like options are left for the
|
that look like options are left for the
|
||||||
|
@ -64,6 +73,10 @@ main(argc, argv)
|
||||||
debugging++;
|
debugging++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'i':
|
||||||
|
inspect++;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
break;
|
||||||
|
@ -118,6 +131,10 @@ main(argc, argv)
|
||||||
sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
|
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);
|
goaway(sts);
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue