Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 145:853b736a6a9e legacy-trunk
Added copyright notice.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 19 Feb 1991 12:39:46 +0000 |
| parents | a6934380c6e7 |
| children | 914c83cfa5e1 |
| 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 |
| 86 | 48 #ifdef THINK_C |
| 49 #define DELIM ' ' | |
| 50 #endif | |
| 51 | |
| 52 #ifndef DELIM | |
| 53 #define DELIM ':' | |
| 54 #endif | |
| 6 | 55 |
| 56 static object *sysdict; | |
| 57 | |
| 58 object * | |
| 59 sysget(name) | |
| 60 char *name; | |
| 61 { | |
| 62 return dictlookup(sysdict, name); | |
| 63 } | |
| 64 | |
| 65 FILE * | |
| 66 sysgetfile(name, def) | |
| 67 char *name; | |
| 68 FILE *def; | |
| 69 { | |
| 70 FILE *fp = NULL; | |
| 71 object *v = sysget(name); | |
| 72 if (v != NULL) | |
| 73 fp = getfilefile(v); | |
| 74 if (fp == NULL) | |
| 75 fp = def; | |
| 76 return fp; | |
| 77 } | |
| 78 | |
| 79 int | |
| 80 sysset(name, v) | |
| 81 char *name; | |
| 82 object *v; | |
| 83 { | |
| 84 if (v == NULL) | |
| 85 return dictremove(sysdict, name); | |
| 86 else | |
| 87 return dictinsert(sysdict, name, v); | |
| 88 } | |
| 89 | |
| 90 static object * | |
| 91 sys_exit(self, args) | |
| 92 object *self; | |
| 93 object *args; | |
| 94 { | |
| 95 int sts; | |
| 96 if (!getintarg(args, &sts)) | |
| 97 return NULL; | |
| 98 goaway(sts); | |
| 99 exit(sts); /* Just in case */ | |
| 100 /* NOTREACHED */ | |
| 101 } | |
| 102 | |
| 86 | 103 static struct methodlist sys_methods[] = { |
| 104 {"exit", sys_exit}, | |
| 105 {NULL, NULL} /* sentinel */ | |
| 106 }; | |
| 107 | |
| 6 | 108 static object *sysin, *sysout, *syserr; |
| 109 | |
| 110 void | |
| 86 | 111 initsys() |
| 6 | 112 { |
| 86 | 113 object *m = initmodule("sys", sys_methods); |
| 114 sysdict = getmoduledict(m); | |
| 115 INCREF(sysdict); | |
| 6 | 116 /* NB keep an extra ref to the std files to avoid closing them |
| 117 when the user deletes them */ | |
| 86 | 118 /* XXX File objects should have a "don't close" flag instead */ |
| 6 | 119 sysin = newopenfileobject(stdin, "<stdin>", "r"); |
| 120 sysout = newopenfileobject(stdout, "<stdout>", "w"); | |
| 121 syserr = newopenfileobject(stderr, "<stderr>", "w"); | |
| 122 if (err_occurred()) | |
| 86 | 123 fatal("can't create sys.std* file objects"); |
| 6 | 124 dictinsert(sysdict, "stdin", sysin); |
| 125 dictinsert(sysdict, "stdout", sysout); | |
| 126 dictinsert(sysdict, "stderr", syserr); | |
| 86 | 127 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 128 if (err_occurred()) |
| 129 fatal("can't insert sys.* objects in sys dict"); | |
| 86 | 130 } |
| 131 | |
| 132 static object * | |
| 133 makepathobject(path, delim) | |
| 134 char *path; | |
| 135 int delim; | |
| 136 { | |
| 137 int i, n; | |
| 138 char *p; | |
| 139 object *v, *w; | |
| 6 | 140 |
| 86 | 141 n = 1; |
| 142 p = path; | |
| 143 while ((p = strchr(p, delim)) != NULL) { | |
| 144 n++; | |
| 145 p++; | |
| 146 } | |
| 147 v = newlistobject(n); | |
| 148 if (v == NULL) | |
| 149 return NULL; | |
| 150 for (i = 0; ; i++) { | |
| 151 p = strchr(path, delim); | |
| 152 if (p == NULL) | |
| 153 p = strchr(path, '\0'); /* End of string */ | |
| 154 w = newsizedstringobject(path, (int) (p - path)); | |
| 155 if (w == NULL) { | |
| 156 DECREF(v); | |
| 157 return NULL; | |
| 158 } | |
| 159 setlistitem(v, i, w); | |
| 160 if (*p == '\0') | |
| 161 break; | |
| 162 path = p+1; | |
| 163 } | |
| 164 return v; | |
| 165 } | |
| 166 | |
| 167 void | |
| 168 setpythonpath(path) | |
| 169 char *path; | |
| 170 { | |
| 171 object *v; | |
| 172 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 173 fatal("can't create sys.path"); | |
| 174 if (sysset("path", v) != 0) | |
| 175 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
|
176 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
177 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
178 |
| 86 | 179 static object * |
| 180 makeargvobject(argc, argv) | |
| 181 int argc; | |
| 182 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
183 { |
| 86 | 184 object *av; |
| 185 if (argc < 0 || argv == NULL) | |
| 186 argc = 0; | |
| 187 av = newlistobject(argc); | |
| 188 if (av != NULL) { | |
| 189 int i; | |
| 190 for (i = 0; i < argc; i++) { | |
| 191 object *v = newstringobject(argv[i]); | |
| 192 if (v == NULL) { | |
| 193 DECREF(av); | |
| 194 av = NULL; | |
| 195 break; | |
| 196 } | |
| 197 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
|
198 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
199 } |
| 86 | 200 return av; |
| 6 | 201 } |
| 202 | |
| 203 void | |
| 86 | 204 setpythonargv(argc, argv) |
| 205 int argc; | |
| 206 char **argv; | |
| 6 | 207 { |
| 86 | 208 object *av = makeargvobject(argc, argv); |
| 209 if (av == NULL) | |
| 210 fatal("no mem for sys.argv"); | |
| 211 if (sysset("argv", av) != 0) | |
| 212 fatal("can't assign sys.argv"); | |
| 213 DECREF(av); | |
| 6 | 214 } |
