Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 265:187ba1733b17 legacy-trunk
Added fclose to newopenfileobject() calls.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 04 Jun 1991 19:40:59 +0000 |
| parents | 914c83cfa5e1 |
| children | f900b692b37e |
| 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 | |
|
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 { | |
| 99 int sts; | |
| 100 if (!getintarg(args, &sts)) | |
| 101 return NULL; | |
| 102 goaway(sts); | |
| 103 exit(sts); /* Just in case */ | |
| 104 /* NOTREACHED */ | |
| 105 } | |
| 106 | |
| 86 | 107 static struct methodlist sys_methods[] = { |
| 108 {"exit", sys_exit}, | |
| 109 {NULL, NULL} /* sentinel */ | |
| 110 }; | |
| 111 | |
| 6 | 112 static object *sysin, *sysout, *syserr; |
| 113 | |
| 114 void | |
| 86 | 115 initsys() |
| 6 | 116 { |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
117 extern int fclose PROTO((FILE *)); |
| 86 | 118 object *m = initmodule("sys", sys_methods); |
| 119 sysdict = getmoduledict(m); | |
| 120 INCREF(sysdict); | |
| 6 | 121 /* NB keep an extra ref to the std files to avoid closing them |
| 122 when the user deletes them */ | |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
123 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
124 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
125 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
| 6 | 126 if (err_occurred()) |
| 86 | 127 fatal("can't create sys.std* file objects"); |
| 6 | 128 dictinsert(sysdict, "stdin", sysin); |
| 129 dictinsert(sysdict, "stdout", sysout); | |
| 130 dictinsert(sysdict, "stderr", syserr); | |
| 86 | 131 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 132 if (err_occurred()) |
| 133 fatal("can't insert sys.* objects in sys dict"); | |
| 86 | 134 } |
| 135 | |
| 136 static object * | |
| 137 makepathobject(path, delim) | |
| 138 char *path; | |
| 139 int delim; | |
| 140 { | |
| 141 int i, n; | |
| 142 char *p; | |
| 143 object *v, *w; | |
| 6 | 144 |
| 86 | 145 n = 1; |
| 146 p = path; | |
| 147 while ((p = strchr(p, delim)) != NULL) { | |
| 148 n++; | |
| 149 p++; | |
| 150 } | |
| 151 v = newlistobject(n); | |
| 152 if (v == NULL) | |
| 153 return NULL; | |
| 154 for (i = 0; ; i++) { | |
| 155 p = strchr(path, delim); | |
| 156 if (p == NULL) | |
| 157 p = strchr(path, '\0'); /* End of string */ | |
| 158 w = newsizedstringobject(path, (int) (p - path)); | |
| 159 if (w == NULL) { | |
| 160 DECREF(v); | |
| 161 return NULL; | |
| 162 } | |
| 163 setlistitem(v, i, w); | |
| 164 if (*p == '\0') | |
| 165 break; | |
| 166 path = p+1; | |
| 167 } | |
| 168 return v; | |
| 169 } | |
| 170 | |
| 171 void | |
| 172 setpythonpath(path) | |
| 173 char *path; | |
| 174 { | |
| 175 object *v; | |
| 176 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 177 fatal("can't create sys.path"); | |
| 178 if (sysset("path", v) != 0) | |
| 179 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
|
180 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
181 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
182 |
| 86 | 183 static object * |
| 184 makeargvobject(argc, argv) | |
| 185 int argc; | |
| 186 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
187 { |
| 86 | 188 object *av; |
| 189 if (argc < 0 || argv == NULL) | |
| 190 argc = 0; | |
| 191 av = newlistobject(argc); | |
| 192 if (av != NULL) { | |
| 193 int i; | |
| 194 for (i = 0; i < argc; i++) { | |
| 195 object *v = newstringobject(argv[i]); | |
| 196 if (v == NULL) { | |
| 197 DECREF(av); | |
| 198 av = NULL; | |
| 199 break; | |
| 200 } | |
| 201 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
|
202 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
203 } |
| 86 | 204 return av; |
| 6 | 205 } |
| 206 | |
| 207 void | |
| 86 | 208 setpythonargv(argc, argv) |
| 209 int argc; | |
| 210 char **argv; | |
| 6 | 211 { |
| 86 | 212 object *av = makeargvobject(argc, argv); |
| 213 if (av == NULL) | |
| 214 fatal("no mem for sys.argv"); | |
| 215 if (sysset("argv", av) != 0) | |
| 216 fatal("can't assign sys.argv"); | |
| 217 DECREF(av); | |
| 6 | 218 } |
