Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 708:8bcb50f436fa legacy-trunk
New trace implementation; and profile (in a similat vein).
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 23 Mar 1992 18:19:28 +0000 |
| parents | f5a2bc39e10e |
| children | bc5c77fa8006 |
| 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 { | |
|
631
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
88 if (v == NULL) { |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
89 if (dictlookup(sysdict, name) == NULL) |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
90 return 0; |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
91 else |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
92 return dictremove(sysdict, name); |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
93 } |
| 6 | 94 else |
| 95 return dictinsert(sysdict, name, v); | |
| 96 } | |
| 97 | |
| 98 static object * | |
| 99 sys_exit(self, args) | |
| 100 object *self; | |
| 101 object *args; | |
| 102 { | |
|
549
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
103 /* 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
|
104 err_setval(SystemExit, args); |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
105 return NULL; |
| 6 | 106 } |
| 107 | |
| 86 | 108 static struct methodlist sys_methods[] = { |
| 109 {"exit", sys_exit}, | |
| 110 {NULL, NULL} /* sentinel */ | |
| 111 }; | |
| 112 | |
| 6 | 113 static object *sysin, *sysout, *syserr; |
| 114 | |
| 115 void | |
| 86 | 116 initsys() |
| 6 | 117 { |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
118 extern int fclose PROTO((FILE *)); |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
119 extern char version[]; |
|
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
120 object *v = newstringobject(version); |
| 86 | 121 object *m = initmodule("sys", sys_methods); |
| 122 sysdict = getmoduledict(m); | |
| 123 INCREF(sysdict); | |
| 6 | 124 /* NB keep an extra ref to the std files to avoid closing them |
| 125 when the user deletes them */ | |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
126 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
127 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
128 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
| 6 | 129 if (err_occurred()) |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
130 fatal("can't create sys.* objects"); |
| 6 | 131 dictinsert(sysdict, "stdin", sysin); |
| 132 dictinsert(sysdict, "stdout", sysout); | |
| 133 dictinsert(sysdict, "stderr", syserr); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
134 dictinsert(sysdict, "version", v); |
| 86 | 135 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 136 if (err_occurred()) |
| 137 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
|
138 DECREF(v); |
| 86 | 139 } |
| 140 | |
| 141 static object * | |
| 142 makepathobject(path, delim) | |
| 143 char *path; | |
| 144 int delim; | |
| 145 { | |
| 146 int i, n; | |
| 147 char *p; | |
| 148 object *v, *w; | |
| 6 | 149 |
| 86 | 150 n = 1; |
| 151 p = path; | |
| 152 while ((p = strchr(p, delim)) != NULL) { | |
| 153 n++; | |
| 154 p++; | |
| 155 } | |
| 156 v = newlistobject(n); | |
| 157 if (v == NULL) | |
| 158 return NULL; | |
| 159 for (i = 0; ; i++) { | |
| 160 p = strchr(path, delim); | |
| 161 if (p == NULL) | |
| 162 p = strchr(path, '\0'); /* End of string */ | |
| 163 w = newsizedstringobject(path, (int) (p - path)); | |
| 164 if (w == NULL) { | |
| 165 DECREF(v); | |
| 166 return NULL; | |
| 167 } | |
| 168 setlistitem(v, i, w); | |
| 169 if (*p == '\0') | |
| 170 break; | |
| 171 path = p+1; | |
| 172 } | |
| 173 return v; | |
| 174 } | |
| 175 | |
| 176 void | |
| 177 setpythonpath(path) | |
| 178 char *path; | |
| 179 { | |
| 180 object *v; | |
| 181 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 182 fatal("can't create sys.path"); | |
| 183 if (sysset("path", v) != 0) | |
| 184 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
|
185 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
186 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
187 |
| 86 | 188 static object * |
| 189 makeargvobject(argc, argv) | |
| 190 int argc; | |
| 191 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
192 { |
| 86 | 193 object *av; |
|
596
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
194 if (argc <= 0 || argv == NULL) { |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
195 /* Ensure at least one (empty) argument is seen */ |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
196 static char *empty_argv[1] = {""}; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
197 argv = empty_argv; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
198 argc = 1; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
199 } |
| 86 | 200 av = newlistobject(argc); |
| 201 if (av != NULL) { | |
| 202 int i; | |
| 203 for (i = 0; i < argc; i++) { | |
| 204 object *v = newstringobject(argv[i]); | |
| 205 if (v == NULL) { | |
| 206 DECREF(av); | |
| 207 av = NULL; | |
| 208 break; | |
| 209 } | |
| 210 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
|
211 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
212 } |
| 86 | 213 return av; |
| 6 | 214 } |
| 215 | |
| 216 void | |
| 86 | 217 setpythonargv(argc, argv) |
| 218 int argc; | |
| 219 char **argv; | |
| 6 | 220 { |
| 86 | 221 object *av = makeargvobject(argc, argv); |
| 222 if (av == NULL) | |
| 223 fatal("no mem for sys.argv"); | |
| 224 if (sysset("argv", av) != 0) | |
| 225 fatal("can't assign sys.argv"); | |
| 226 DECREF(av); | |
| 6 | 227 } |
