Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 549:4efff6cea90f legacy-trunk
Raise SystemExit instead of calling goaway().
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 31 Dec 1991 13:15:35 +0000 |
| parents | bec963f99bc9 |
| children | e18f1fa398a3 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
| 2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The | |
| 3 Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* System module */ |
| 26 | |
| 27 /* | |
| 28 Various bits of information used by the interpreter are collected in | |
| 29 module 'sys'. | |
| 86 | 30 Function member: |
| 31 - exit(sts): call (C, POSIX) exit(sts) | |
| 6 | 32 Data members: |
| 33 - stdin, stdout, stderr: standard file objects | |
| 86 | 34 - modules: the table of modules (dictionary) |
| 6 | 35 - path: module search path (list of strings) |
| 86 | 36 - argv: script arguments (list of strings) |
| 37 - ps1, ps2: optional primary and secondary prompts (strings) | |
| 6 | 38 */ |
| 39 | |
| 86 | 40 #include "allobjects.h" |
| 41 | |
| 42 #include "sysmodule.h" | |
| 43 #include "import.h" | |
| 44 #include "modsupport.h" | |
| 45 | |
| 46 /* Define delimiter used in $PYTHONPATH */ | |
| 6 | 47 |
|
293
f900b692b37e
Generalize to macintosh.
Guido van Rossum <guido@python.org>
parents:
265
diff
changeset
|
48 #ifdef macintosh |
| 86 | 49 #define DELIM ' ' |
| 50 #endif | |
| 51 | |
|
234
914c83cfa5e1
Defined path delimiter for MS-DOS as semicolon
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
52 #ifdef MSDOS |
|
914c83cfa5e1
Defined path delimiter for MS-DOS as semicolon
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
53 #define DELIM ';' |
|
914c83cfa5e1
Defined path delimiter for MS-DOS as semicolon
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
54 #endif |
|
914c83cfa5e1
Defined path delimiter for MS-DOS as semicolon
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
55 |
| 86 | 56 #ifndef DELIM |
| 57 #define DELIM ':' | |
| 58 #endif | |
| 6 | 59 |
| 60 static object *sysdict; | |
| 61 | |
| 62 object * | |
| 63 sysget(name) | |
| 64 char *name; | |
| 65 { | |
| 66 return dictlookup(sysdict, name); | |
| 67 } | |
| 68 | |
| 69 FILE * | |
| 70 sysgetfile(name, def) | |
| 71 char *name; | |
| 72 FILE *def; | |
| 73 { | |
| 74 FILE *fp = NULL; | |
| 75 object *v = sysget(name); | |
| 76 if (v != NULL) | |
| 77 fp = getfilefile(v); | |
| 78 if (fp == NULL) | |
| 79 fp = def; | |
| 80 return fp; | |
| 81 } | |
| 82 | |
| 83 int | |
| 84 sysset(name, v) | |
| 85 char *name; | |
| 86 object *v; | |
| 87 { | |
| 88 if (v == NULL) | |
| 89 return dictremove(sysdict, name); | |
| 90 else | |
| 91 return dictinsert(sysdict, name, v); | |
| 92 } | |
| 93 | |
| 94 static object * | |
| 95 sys_exit(self, args) | |
| 96 object *self; | |
| 97 object *args; | |
| 98 { | |
|
549
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
99 /* Raise SystemExit so callers may catch it or clean up. */ |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
100 err_setval(SystemExit, args); |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
101 return NULL; |
| 6 | 102 } |
| 103 | |
| 86 | 104 static struct methodlist sys_methods[] = { |
| 105 {"exit", sys_exit}, | |
| 106 {NULL, NULL} /* sentinel */ | |
| 107 }; | |
| 108 | |
| 6 | 109 static object *sysin, *sysout, *syserr; |
| 110 | |
| 111 void | |
| 86 | 112 initsys() |
| 6 | 113 { |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
114 extern int fclose PROTO((FILE *)); |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
115 extern char version[]; |
|
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
116 object *v = newstringobject(version); |
| 86 | 117 object *m = initmodule("sys", sys_methods); |
| 118 sysdict = getmoduledict(m); | |
| 119 INCREF(sysdict); | |
| 6 | 120 /* NB keep an extra ref to the std files to avoid closing them |
| 121 when the user deletes them */ | |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
122 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
123 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
124 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
| 6 | 125 if (err_occurred()) |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
126 fatal("can't create sys.* objects"); |
| 6 | 127 dictinsert(sysdict, "stdin", sysin); |
| 128 dictinsert(sysdict, "stdout", sysout); | |
| 129 dictinsert(sysdict, "stderr", syserr); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
130 dictinsert(sysdict, "version", v); |
| 86 | 131 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 132 if (err_occurred()) |
| 133 fatal("can't insert sys.* objects in sys dict"); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
134 DECREF(v); |
| 86 | 135 } |
| 136 | |
| 137 static object * | |
| 138 makepathobject(path, delim) | |
| 139 char *path; | |
| 140 int delim; | |
| 141 { | |
| 142 int i, n; | |
| 143 char *p; | |
| 144 object *v, *w; | |
| 6 | 145 |
| 86 | 146 n = 1; |
| 147 p = path; | |
| 148 while ((p = strchr(p, delim)) != NULL) { | |
| 149 n++; | |
| 150 p++; | |
| 151 } | |
| 152 v = newlistobject(n); | |
| 153 if (v == NULL) | |
| 154 return NULL; | |
| 155 for (i = 0; ; i++) { | |
| 156 p = strchr(path, delim); | |
| 157 if (p == NULL) | |
| 158 p = strchr(path, '\0'); /* End of string */ | |
| 159 w = newsizedstringobject(path, (int) (p - path)); | |
| 160 if (w == NULL) { | |
| 161 DECREF(v); | |
| 162 return NULL; | |
| 163 } | |
| 164 setlistitem(v, i, w); | |
| 165 if (*p == '\0') | |
| 166 break; | |
| 167 path = p+1; | |
| 168 } | |
| 169 return v; | |
| 170 } | |
| 171 | |
| 172 void | |
| 173 setpythonpath(path) | |
| 174 char *path; | |
| 175 { | |
| 176 object *v; | |
| 177 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 178 fatal("can't create sys.path"); | |
| 179 if (sysset("path", v) != 0) | |
| 180 fatal("can't assign sys.path"); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
181 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
182 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
183 |
| 86 | 184 static object * |
| 185 makeargvobject(argc, argv) | |
| 186 int argc; | |
| 187 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
188 { |
| 86 | 189 object *av; |
| 190 if (argc < 0 || argv == NULL) | |
| 191 argc = 0; | |
| 192 av = newlistobject(argc); | |
| 193 if (av != NULL) { | |
| 194 int i; | |
| 195 for (i = 0; i < argc; i++) { | |
| 196 object *v = newstringobject(argv[i]); | |
| 197 if (v == NULL) { | |
| 198 DECREF(av); | |
| 199 av = NULL; | |
| 200 break; | |
| 201 } | |
| 202 setlistitem(av, i, v); | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
203 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
204 } |
| 86 | 205 return av; |
| 6 | 206 } |
| 207 | |
| 208 void | |
| 86 | 209 setpythonargv(argc, argv) |
| 210 int argc; | |
| 211 char **argv; | |
| 6 | 212 { |
| 86 | 213 object *av = makeargvobject(argc, argv); |
| 214 if (av == NULL) | |
| 215 fatal("no mem for sys.argv"); | |
| 216 if (sysset("argv", av) != 0) | |
| 217 fatal("can't assign sys.argv"); | |
| 218 DECREF(av); | |
| 6 | 219 } |
