annotate Python/pythonmain.c @ 1263:65a1e4e9cd54 legacy-trunk

* Fixed some subtleties with fastlocals. You can no longer access f_fastlocals in a traceback object (this is a core dump hazard if there are <nil> entries), but instead eval_code() merges the fast locals back into the locals dictionary if it looks like the local variables will be retained. Also, the merge routines save exceptions since this is sometimes needed (alas!). * Added id() to bltinmodule.c, which returns an object's address (identity). Useful to walk arbitrary data structures containing cycles. * Added compile() to bltinmodule.c and compile_string() to pythonrun.[ch]: support to exec/eval arbitrary code objects. The code that defaults globals and locals is moved from run_node in pythonrun.c (which is now identical to eval_node) to eval_code in ceval.c. [XXX For elegance a clean-up session is necessary.]
author Guido van Rossum <guido@python.org>
date Tue, 30 Mar 1993 17:46:03 +0000
parents 1cabc2b6c970
children 14369a5e6167
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
1 /***********************************************************
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1014
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1014
diff changeset
3 Amsterdam, The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 94
diff changeset
24
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Python interpreter main program */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
206
3cc54972ab52 Move allobjects.h to front (needed by THINK C precompiled headers)
Guido van Rossum <guido@python.org>
parents: 179
diff changeset
27 #include "allobjects.h"
156
43cabac5023d Include patchlevel.h (to ensure it is distributed with the rest).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
28
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 891
diff changeset
29 extern int debugging; /* Needed by parser.c */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 891
diff changeset
30 extern int verbose; /* Needed by import.c */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
32 /* Interface to getopt(): */
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
33 extern int optind;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
34 extern char *optarg;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
35 extern int getopt PROTO((int, char **, char *));
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
36
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 main(argc, argv)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 int argc;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 char **argv;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 {
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
41 int c;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
42 int sts;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
43 char *command = NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 char *filename = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 FILE *fp = stdin;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 81
diff changeset
46
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
47 initargs(&argc, &argv); /* Defined in config*.c */
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
48
728
76d92ebb4d82 Add -v option (verbose) and get rid of finaloutput() function (unused)
Guido van Rossum <guido@python.org>
parents: 690
diff changeset
49 while ((c = getopt(argc, argv, "c:dv")) != EOF) {
560
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
50 if (c == 'c') {
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
51 /* -c is the last option; following arguments
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
52 that look like options are left for the
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
53 the command to interpret. */
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
54 command = malloc(strlen(optarg) + 2);
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
55 /* Ignore malloc errors this early... */
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
56 strcpy(command, optarg);
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
57 strcat(command, "\n");
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
58 break;
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
59 }
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
60
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
61 switch (c) {
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
62
618
189ee3beebb5 Added -d option (increments debugging variable); mostly to get rid
Guido van Rossum <guido@python.org>
parents: 593
diff changeset
63 case 'd':
189ee3beebb5 Added -d option (increments debugging variable); mostly to get rid
Guido van Rossum <guido@python.org>
parents: 593
diff changeset
64 debugging++;
189ee3beebb5 Added -d option (increments debugging variable); mostly to get rid
Guido van Rossum <guido@python.org>
parents: 593
diff changeset
65 break;
189ee3beebb5 Added -d option (increments debugging variable); mostly to get rid
Guido van Rossum <guido@python.org>
parents: 593
diff changeset
66
728
76d92ebb4d82 Add -v option (verbose) and get rid of finaloutput() function (unused)
Guido van Rossum <guido@python.org>
parents: 690
diff changeset
67 case 'v':
76d92ebb4d82 Add -v option (verbose) and get rid of finaloutput() function (unused)
Guido van Rossum <guido@python.org>
parents: 690
diff changeset
68 verbose++;
76d92ebb4d82 Add -v option (verbose) and get rid of finaloutput() function (unused)
Guido van Rossum <guido@python.org>
parents: 690
diff changeset
69 break;
76d92ebb4d82 Add -v option (verbose) and get rid of finaloutput() function (unused)
Guido van Rossum <guido@python.org>
parents: 690
diff changeset
70
560
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
71 /* This space reserved for other options */
0614d887d10a Stop option processing immediately after "-c command",
Guido van Rossum <guido@python.org>
parents: 547
diff changeset
72
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
73 default:
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
74 fprintf(stderr,
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
75 "usage: %s [-c cmd | file | -] [arg] ...\n",
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
76 argv[0]);
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
77 exit(2);
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
78 /*NOTREACHED*/
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
79
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
80 }
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
81 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
83 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
84 filename = argv[optind];
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 81
diff changeset
85
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if (filename != NULL) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if ((fp = fopen(filename, "r")) == NULL) {
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
88 fprintf(stderr, "%s: can't open file '%s'\n",
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
89 argv[0], filename);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 exit(2);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
891
a8ca720fd921 Removed confusing, incorrect comment.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
94 initall();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 81
diff changeset
95
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
96 if (command != NULL) {
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
97 /* Backup optind and force sys.argv[0] = '-c' */
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
98 optind--;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
99 argv[optind] = "-c";
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
100 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents: 891
diff changeset
101
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
102 setpythonargv(argc-optind, argv+optind);
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
103
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
104 if (command) {
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
105 sts = run_command(command) != 0;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
106 }
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
107 else {
1014
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
108 if (filename == NULL && isatty((int)fileno(fp))) {
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
109 char *startup = getenv("PYTHONSTARTUP");
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
110 if (startup != NULL && startup[0] != '\0') {
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
111 FILE *fp = fopen(startup, "r");
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
112 if (fp != NULL) {
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
113 (void) run_script(fp, startup);
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
114 err_clear();
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
115 }
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
116 }
96acb35bb92b Add $PYTHONSTARTUP feature.
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
117 }
282
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
118 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
119 }
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
120
d5c9ae422b41 Add run_command() to implement "-c command".
Guido van Rossum <guido@python.org>
parents: 262
diff changeset
121 goaway(sts);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 81
diff changeset
122 /*NOTREACHED*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 81
diff changeset
123 }