Mercurial > cpython-lru_cache-weakref
annotate Modules/main.c @ 16798:258dcafe2d0b legacy-trunk
Gustavo Niemeyer <niemeyer@conectiva.com>:
Fixed recno support (keys are integers rather than strings).
Work around DB bug that cause stdin to be closed by rnopen() when the
DB file needed to exist but did not (no longer segfaults).
This closes SF tracker patch #403445.
Also wrapped some long lines and added whitespace around operators -- FLD.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Tue, 27 Feb 2001 18:56:46 +0000 |
| parents | 5c3aedc969da |
| children | 4baa5a955532 |
| rev | line source |
|---|---|
| 2896 | 1 /* Python interpreter main program */ |
| 2 | |
| 3 #include "Python.h" | |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
4 #include "osdefs.h" |
| 2896 | 5 |
|
4620
98c08f0c4a2e
Include unistd.h to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
6 #ifdef HAVE_UNISTD_H |
|
98c08f0c4a2e
Include unistd.h to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
7 #include <unistd.h> |
|
98c08f0c4a2e
Include unistd.h to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
8 #endif |
| 2896 | 9 |
|
5000
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
10 #ifdef MS_WINDOWS |
|
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
11 #include <fcntl.h> |
|
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
12 #endif |
|
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
13 |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
14 #if defined(PYOS_OS2) || defined(MS_WINDOWS) |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
15 #define PYTHONHOMEHELP "<prefix>\\lib" |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
16 #else |
|
13482
000a1c7eb09f
- changed 1.5 to 2.0 in the help text
Fredrik Lundh <fredrik@pythonware.com>
parents:
13256
diff
changeset
|
17 #define PYTHONHOMEHELP "<prefix>/python2.0" |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
18 #endif |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
19 |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
20 #include "pygetopt.h" |
|
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
21 |
|
14411
a707a5f17139
Instead of printing all 9 lines of copyright info, print
Guido van Rossum <guido@python.org>
parents:
14375
diff
changeset
|
22 #define COPYRIGHT \ |
|
a707a5f17139
Instead of printing all 9 lines of copyright info, print
Guido van Rossum <guido@python.org>
parents:
14375
diff
changeset
|
23 "Type \"copyright\", \"credits\" or \"license\" for more information." |
|
a707a5f17139
Instead of printing all 9 lines of copyright info, print
Guido van Rossum <guido@python.org>
parents:
14375
diff
changeset
|
24 |
|
3773
6e40a2a3e6ef
New style names for getprogramname and getargcargv
Guido van Rossum <guido@python.org>
parents:
3701
diff
changeset
|
25 /* For Py_GetArgcArgv(); set by main() */ |
| 2896 | 26 static char **orig_argv; |
| 27 static int orig_argc; | |
| 28 | |
| 29 /* Short usage message (with %s for argv0) */ | |
| 30 static char *usage_line = | |
|
9986
d4b8ddd9a4fd
Document -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents:
9601
diff
changeset
|
31 "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n"; |
| 2896 | 32 |
| 33 /* Long usage message, split into parts < 512 bytes */ | |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
34 static char *usage_top = "\ |
| 2896 | 35 Options and arguments (and corresponding environment variables):\n\ |
| 36 -d : debug output from parser (also PYTHONDEBUG=x)\n\ | |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
37 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ |
|
7966
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
38 and force prompts, even if stdin does not appear to be a terminal\n\ |
|
9284
faa31b1c0b54
Documented PYTHONOPTIMIZE; by Marc Lemburg.
Guido van Rossum <guido@python.org>
parents:
8044
diff
changeset
|
39 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\ |
|
9986
d4b8ddd9a4fd
Document -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents:
9601
diff
changeset
|
40 -OO : remove doc-strings in addition to the -O optimizations\n\ |
|
6203
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
41 -S : don't imply 'import site' on initialization\n\ |
|
7966
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
42 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ |
|
6203
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
43 "; |
|
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
44 static char *usage_mid = "\ |
|
7966
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
45 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
| 12537 | 46 -U : Unicode literals: treats '...' literals like u'...'\n\ |
| 2896 | 47 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
48 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
49 -h : print this help message and exit\n\ |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
50 -V : print the Python version number and exit\n\ |
| 15593 | 51 -W arg : warning control (arg is action:message:category:module:lineno)\n\ |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
52 -c cmd : program passed in as string (terminates option list)\n\ |
| 2896 | 53 file : program read from script file\n\ |
| 54 - : program read from stdin (default; interactive mode if a tty)\n\ | |
|
6203
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
55 "; |
|
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
56 static char *usage_bot = "\ |
|
7966
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
57 arg ...: arguments passed to program in sys.argv[1:]\n\ |
| 2896 | 58 Other environment variables:\n\ |
| 59 PYTHONSTARTUP: file executed on interactive startup (no default)\n\ | |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
60 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
| 2896 | 61 default module search path. The result is sys.path.\n\ |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
62 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\ |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
63 The default module search path uses %s.\n\ |
|
16731
86c7683b606a
Add a line to "python -h" output about PYTHONCASEOK.
Tim Peters <tim.peters@gmail.com>
parents:
15593
diff
changeset
|
64 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ |
| 2896 | 65 "; |
| 66 | |
| 67 | |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
68 static void |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
69 usage(int exitcode, char* program) |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
70 { |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
71 fprintf(stderr, usage_line, program); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
72 fprintf(stderr, usage_top); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
73 fprintf(stderr, usage_mid); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
74 fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
75 exit(exitcode); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
76 /*NOTREACHED*/ |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
77 } |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
78 |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
79 |
| 2896 | 80 /* Main program */ |
| 81 | |
|
9601
0f3f9e366356
Py_Main() must be DL_EXPORT too.
Guido van Rossum <guido@python.org>
parents:
9284
diff
changeset
|
82 DL_EXPORT(int) |
|
13482
000a1c7eb09f
- changed 1.5 to 2.0 in the help text
Fredrik Lundh <fredrik@pythonware.com>
parents:
13256
diff
changeset
|
83 Py_Main(int argc, char **argv) |
| 2896 | 84 { |
| 85 int c; | |
| 86 int sts; | |
| 87 char *command = NULL; | |
| 88 char *filename = NULL; | |
| 89 FILE *fp = stdin; | |
| 90 char *p; | |
| 91 int inspect = 0; | |
| 92 int unbuffered = 0; | |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
93 int skipfirstline = 0; |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
94 int stdin_is_interactive = 0; |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
95 int help = 0; |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
96 int version = 0; |
| 2896 | 97 |
|
3773
6e40a2a3e6ef
New style names for getprogramname and getargcargv
Guido van Rossum <guido@python.org>
parents:
3701
diff
changeset
|
98 orig_argc = argc; /* For Py_GetArgcArgv() */ |
| 2896 | 99 orig_argv = argv; |
| 100 | |
| 101 if ((p = getenv("PYTHONINSPECT")) && *p != '\0') | |
| 102 inspect = 1; | |
| 103 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') | |
| 104 unbuffered = 1; | |
| 105 | |
| 15593 | 106 PySys_ResetWarnOptions(); |
| 107 | |
| 108 while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhVW:")) != EOF) { | |
| 2896 | 109 if (c == 'c') { |
| 110 /* -c is the last option; following arguments | |
| 111 that look like options are left for the | |
| 112 the command to interpret. */ | |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
113 command = malloc(strlen(_PyOS_optarg) + 2); |
| 2896 | 114 if (command == NULL) |
| 115 Py_FatalError( | |
| 116 "not enough memory to copy -c argument"); | |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
117 strcpy(command, _PyOS_optarg); |
| 2896 | 118 strcat(command, "\n"); |
| 119 break; | |
| 120 } | |
| 121 | |
| 122 switch (c) { | |
| 123 | |
| 124 case 'd': | |
| 125 Py_DebugFlag++; | |
| 126 break; | |
| 127 | |
| 128 case 'i': | |
| 129 inspect++; | |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
130 Py_InteractiveFlag++; |
| 2896 | 131 break; |
| 132 | |
|
5166
7151bb727776
Add -O option which sets Py_OptimizeFlag to avoid emitting SET_LINENO.
Guido van Rossum <guido@python.org>
parents:
5113
diff
changeset
|
133 case 'O': |
|
7151bb727776
Add -O option which sets Py_OptimizeFlag to avoid emitting SET_LINENO.
Guido van Rossum <guido@python.org>
parents:
5113
diff
changeset
|
134 Py_OptimizeFlag++; |
|
7151bb727776
Add -O option which sets Py_OptimizeFlag to avoid emitting SET_LINENO.
Guido van Rossum <guido@python.org>
parents:
5113
diff
changeset
|
135 break; |
|
7151bb727776
Add -O option which sets Py_OptimizeFlag to avoid emitting SET_LINENO.
Guido van Rossum <guido@python.org>
parents:
5113
diff
changeset
|
136 |
|
6203
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
137 case 'S': |
|
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
138 Py_NoSiteFlag++; |
|
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
139 break; |
|
f57ae68aa3ae
Added -X option to suppress default import of site.py. Also split the
Guido van Rossum <guido@python.org>
parents:
6199
diff
changeset
|
140 |
|
7966
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
141 case 't': |
|
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
142 Py_TabcheckFlag++; |
|
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
143 break; |
|
8c268d0e8822
Add -t option to set the Py_TabcheckFlag flag.
Guido van Rossum <guido@python.org>
parents:
7571
diff
changeset
|
144 |
| 2896 | 145 case 'u': |
| 146 unbuffered++; | |
| 147 break; | |
| 148 | |
| 149 case 'v': | |
| 150 Py_VerboseFlag++; | |
| 151 break; | |
| 152 | |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
153 case 'x': |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
154 skipfirstline = 1; |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
155 break; |
|
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
156 |
| 12537 | 157 case 'U': |
| 158 Py_UnicodeFlag++; | |
| 159 break; | |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
160 case 'h': |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
161 help++; |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
162 break; |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
163 case 'V': |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
164 version++; |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
165 break; |
| 12537 | 166 |
| 15593 | 167 case 'W': |
| 168 PySys_AddWarnOption(_PyOS_optarg); | |
| 169 break; | |
| 170 | |
| 2896 | 171 /* This space reserved for other options */ |
| 172 | |
| 173 default: | |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
174 usage(2, argv[0]); |
| 2896 | 175 /*NOTREACHED*/ |
| 176 | |
| 177 } | |
| 178 } | |
| 179 | |
|
14603
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
180 if (help) |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
181 usage(0, argv[0]); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
182 |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
183 if (version) { |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
184 fprintf(stderr, "Python %s\n", PY_VERSION); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
185 exit(0); |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
186 } |
|
80a782086a57
Py_Main(), usage(), usage_mid: Add -h and -V flags to print the usage
Barry Warsaw <barry@python.org>
parents:
14411
diff
changeset
|
187 |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
188 if (command == NULL && _PyOS_optind < argc && |
|
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
189 strcmp(argv[_PyOS_optind], "-") != 0) |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
190 { |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
191 filename = argv[_PyOS_optind]; |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
192 if (filename != NULL) { |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
193 if ((fp = fopen(filename, "r")) == NULL) { |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
194 fprintf(stderr, "%s: can't open file '%s'\n", |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
195 argv[0], filename); |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
196 exit(2); |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
197 } |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
198 else if (skipfirstline) { |
|
10621
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
199 int ch; |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
200 /* Push back first newline so line numbers |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
201 remain the same */ |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
202 while ((ch = getc(fp)) != EOF) { |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
203 if (ch == '\n') { |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
204 (void)ungetc(ch, fp); |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
205 break; |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
206 } |
|
5b170f0da9fe
Patch from Tim Peters to repare a the problem that tracebacks are off
Guido van Rossum <guido@python.org>
parents:
10130
diff
changeset
|
207 } |
|
6763
12824fe04a08
Two changes by Jeff Rush (slightly tweaked):
Guido van Rossum <guido@python.org>
parents:
6538
diff
changeset
|
208 } |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
209 } |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
210 } |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
211 |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
212 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
213 |
| 2896 | 214 if (unbuffered) { |
|
4934
6b61912d5f35
On Windows, -u implies binary mode for stdin/stdout
Guido van Rossum <guido@python.org>
parents:
4620
diff
changeset
|
215 #ifdef MS_WINDOWS |
|
5000
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
216 _setmode(fileno(stdin), O_BINARY); |
|
7c7a186b6d86
Fix the _setmode() patch for MS_WINDOWS: include <fcntl.h> and use
Guido van Rossum <guido@python.org>
parents:
4934
diff
changeset
|
217 _setmode(fileno(stdout), O_BINARY); |
|
4934
6b61912d5f35
On Windows, -u implies binary mode for stdin/stdout
Guido van Rossum <guido@python.org>
parents:
4620
diff
changeset
|
218 #endif |
| 2896 | 219 #ifndef MPW |
|
7571
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
220 #ifdef HAVE_SETVBUF |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
221 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
222 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
223 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
|
7571
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
224 #else /* !HAVE_SETVBUF */ |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
225 setbuf(stdin, (char *)NULL); |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
226 setbuf(stdout, (char *)NULL); |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
227 setbuf(stderr, (char *)NULL); |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
228 #endif /* !HAVE_SETVBUF */ |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
229 #else /* MPW */ |
| 2896 | 230 /* On MPW (3.2) unbuffered seems to hang */ |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
231 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 2896 | 232 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 233 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); | |
|
7571
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
234 #endif /* MPW */ |
| 2896 | 235 } |
|
5319
74a1bc099827
Change in when and how stdin and stdout are set to line-buffering.
Guido van Rossum <guido@python.org>
parents:
5166
diff
changeset
|
236 else if (Py_InteractiveFlag) { |
|
5320
c3ddb84a7761
OK, I lied. On Windows, _IOLBF seems to be the same as full
Guido van Rossum <guido@python.org>
parents:
5319
diff
changeset
|
237 #ifdef MS_WINDOWS |
|
c3ddb84a7761
OK, I lied. On Windows, _IOLBF seems to be the same as full
Guido van Rossum <guido@python.org>
parents:
5319
diff
changeset
|
238 /* Doesn't have to have line-buffered -- use unbuffered */ |
|
10130
e569f9b0d969
On Windows, -i shouldn't call set[v]buf(stdin, ...) because it screws
Guido van Rossum <guido@python.org>
parents:
9986
diff
changeset
|
239 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
|
5320
c3ddb84a7761
OK, I lied. On Windows, _IOLBF seems to be the same as full
Guido van Rossum <guido@python.org>
parents:
5319
diff
changeset
|
240 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
|
7571
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
241 #else /* !MS_WINDOWS */ |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
242 #ifdef HAVE_SETVBUF |
|
5320
c3ddb84a7761
OK, I lied. On Windows, _IOLBF seems to be the same as full
Guido van Rossum <guido@python.org>
parents:
5319
diff
changeset
|
243 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
|
c3ddb84a7761
OK, I lied. On Windows, _IOLBF seems to be the same as full
Guido van Rossum <guido@python.org>
parents:
5319
diff
changeset
|
244 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
|
7571
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
245 #endif /* HAVE_SETVBUF */ |
|
968aaba281c5
Don't use setvbuf unless HAVE_SETVBUF is defined.
Guido van Rossum <guido@python.org>
parents:
6763
diff
changeset
|
246 #endif /* !MS_WINDOWS */ |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
247 /* Leave stderr alone - it should be unbuffered anyway. */ |
|
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
248 } |
| 2896 | 249 |
|
5851
edaf1cb63dc0
This is no longer the real main program; it now defines Py_Main(), so
Guido van Rossum <guido@python.org>
parents:
5547
diff
changeset
|
250 Py_SetProgramName(argv[0]); |
|
edaf1cb63dc0
This is no longer the real main program; it now defines Py_Main(), so
Guido van Rossum <guido@python.org>
parents:
5547
diff
changeset
|
251 Py_Initialize(); |
|
edaf1cb63dc0
This is no longer the real main program; it now defines Py_Main(), so
Guido van Rossum <guido@python.org>
parents:
5547
diff
changeset
|
252 |
| 2896 | 253 if (Py_VerboseFlag || |
|
5113
fb8cefcefd81
Add parentheses around && within || as gcc -Wall advises.
Guido van Rossum <guido@python.org>
parents:
5109
diff
changeset
|
254 (command == NULL && filename == NULL && stdin_is_interactive)) |
|
5547
98d4e97daac0
Add platform to welcome message.
Guido van Rossum <guido@python.org>
parents:
5394
diff
changeset
|
255 fprintf(stderr, "Python %s on %s\n%s\n", |
|
14411
a707a5f17139
Instead of printing all 9 lines of copyright info, print
Guido van Rossum <guido@python.org>
parents:
14375
diff
changeset
|
256 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT); |
| 2896 | 257 |
| 258 | |
| 259 if (command != NULL) { | |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
260 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ |
|
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
261 _PyOS_optind--; |
|
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
262 argv[_PyOS_optind] = "-c"; |
| 2896 | 263 } |
| 264 | |
|
15374
a826a46bae54
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents:
14603
diff
changeset
|
265 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); |
| 2896 | 266 |
|
6316
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
267 if ((inspect || (command == NULL && filename == NULL)) && |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
268 isatty(fileno(stdin))) { |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
269 PyObject *v; |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
270 v = PyImport_ImportModule("readline"); |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
271 if (v == NULL) |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
272 PyErr_Clear(); |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
273 else |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
274 Py_DECREF(v); |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
275 } |
|
c2359665be80
Move the "import readline" to an earlier place so it is also done when
Guido van Rossum <guido@python.org>
parents:
6203
diff
changeset
|
276 |
| 2896 | 277 if (command) { |
| 278 sts = PyRun_SimpleString(command) != 0; | |
|
5922
44ddc711ee2d
Free the malloc'ed buffer that holds the command once we're done with it.
Guido van Rossum <guido@python.org>
parents:
5863
diff
changeset
|
279 free(command); |
| 2896 | 280 } |
| 281 else { | |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
282 if (filename == NULL && stdin_is_interactive) { |
| 2896 | 283 char *startup = getenv("PYTHONSTARTUP"); |
| 284 if (startup != NULL && startup[0] != '\0') { | |
| 285 FILE *fp = fopen(startup, "r"); | |
| 286 if (fp != NULL) { | |
| 287 (void) PyRun_SimpleFile(fp, startup); | |
| 288 PyErr_Clear(); | |
| 289 fclose(fp); | |
| 290 } | |
| 291 } | |
| 292 } | |
|
14228
20377f15b96a
Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents:
13482
diff
changeset
|
293 sts = PyRun_AnyFileEx( |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
294 fp, |
|
14228
20377f15b96a
Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents:
13482
diff
changeset
|
295 filename == NULL ? "<stdin>" : filename, |
|
20377f15b96a
Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents:
13482
diff
changeset
|
296 filename != NULL) != 0; |
| 2896 | 297 } |
| 298 | |
|
5109
0bbf97861cd9
My version of Lee Busby's patches to make '-i' pretend stdin is a tty
Guido van Rossum <guido@python.org>
parents:
5000
diff
changeset
|
299 if (inspect && stdin_is_interactive && |
| 2896 | 300 (filename != NULL || command != NULL)) |
| 301 sts = PyRun_AnyFile(stdin, "<stdin>") != 0; | |
| 302 | |
|
5940
eef76b3cd9ae
Py_Cleanup() is now Py_Finalize().
Guido van Rossum <guido@python.org>
parents:
5922
diff
changeset
|
303 Py_Finalize(); |
|
16752
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
304 |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
305 #ifdef __INSURE__ |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
306 /* Insure++ is a memory analysis tool that aids in discovering |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
307 * memory leaks and other memory problems. On Python exit, the |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
308 * interned string dictionary is flagged as being in use at exit |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
309 * (which it is). Under normal circumstances, this is fine because |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
310 * the memory will be automatically reclaimed by the system. Under |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
311 * memory debugging, it's a huge source of useless noise, so we |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
312 * trade off slower shutdown for less distraction in the memory |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
313 * reports. -baw |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
314 */ |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
315 _Py_ReleaseInternedStrings(); |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
316 #endif /* __INSURE__ */ |
|
5c3aedc969da
Py_Main(): When compiled by Insure (i.e. __INSURE__ is defined), call
Barry Warsaw <barry@python.org>
parents:
16731
diff
changeset
|
317 |
|
5922
44ddc711ee2d
Free the malloc'ed buffer that holds the command once we're done with it.
Guido van Rossum <guido@python.org>
parents:
5863
diff
changeset
|
318 return sts; |
| 2896 | 319 } |
| 320 | |
| 321 | |
| 322 /* Make the *original* argc/argv available to other modules. | |
| 323 This is rare, but it is needed by the secureware extension. */ | |
| 324 | |
| 325 void | |
|
13482
000a1c7eb09f
- changed 1.5 to 2.0 in the help text
Fredrik Lundh <fredrik@pythonware.com>
parents:
13256
diff
changeset
|
326 Py_GetArgcArgv(int *argc, char ***argv) |
| 2896 | 327 { |
| 328 *argc = orig_argc; | |
| 329 *argv = orig_argv; | |
| 330 } |
