Mercurial > cpython-lru_cache-weakref
annotate Python/sysmodule.c @ 901:fea3f594df77 legacy-trunk
sysmodule.c: calling sys.settrace() or sys.setprofile() without
arguments crashed in INCREF() calls which should be XINCREF() calls.
timemodule.c: fix for SEQUENT port (sys/select, struct timezone) by
Jaap Vermeulen
xxobject.c: include modsupport.h
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 19 Jun 1992 13:57:27 +0000 |
| parents | ba4ec13c0f9f |
| children | a1ce5a949828 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 145 | 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" | |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
45 #include "osdefs.h" |
| 86 | 46 |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
47 object *sys_trace, *sys_profile; |
| 6 | 48 |
| 49 static object *sysdict; | |
| 50 | |
| 51 object * | |
| 52 sysget(name) | |
| 53 char *name; | |
| 54 { | |
| 55 return dictlookup(sysdict, name); | |
| 56 } | |
| 57 | |
| 58 FILE * | |
| 59 sysgetfile(name, def) | |
| 60 char *name; | |
| 61 FILE *def; | |
| 62 { | |
| 63 FILE *fp = NULL; | |
| 64 object *v = sysget(name); | |
| 65 if (v != NULL) | |
| 66 fp = getfilefile(v); | |
| 67 if (fp == NULL) | |
| 68 fp = def; | |
| 69 return fp; | |
| 70 } | |
| 71 | |
| 72 int | |
| 73 sysset(name, v) | |
| 74 char *name; | |
| 75 object *v; | |
| 76 { | |
|
631
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
77 if (v == NULL) { |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
78 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
|
79 return 0; |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
80 else |
|
f5a2bc39e10e
sysset(name, NULL) does nothing if sys.name is undefined
Guido van Rossum <guido@python.org>
parents:
596
diff
changeset
|
81 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
|
82 } |
| 6 | 83 else |
| 84 return dictinsert(sysdict, name, v); | |
| 85 } | |
| 86 | |
| 87 static object * | |
| 88 sys_exit(self, args) | |
| 89 object *self; | |
| 90 object *args; | |
| 91 { | |
|
549
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
92 /* 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
|
93 err_setval(SystemExit, args); |
|
4efff6cea90f
Raise SystemExit instead of calling goaway().
Guido van Rossum <guido@python.org>
parents:
476
diff
changeset
|
94 return NULL; |
| 6 | 95 } |
| 96 | |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
97 static object * |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
98 sys_settrace(self, args) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
99 object *self; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
100 object *args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
101 { |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
102 if (args == None) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
103 args = NULL; |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
104 else |
|
901
fea3f594df77
sysmodule.c: calling sys.settrace() or sys.setprofile() without
Guido van Rossum <guido@python.org>
parents:
788
diff
changeset
|
105 XINCREF(args); |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
106 XDECREF(sys_trace); |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
107 sys_trace = args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
108 INCREF(None); |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
109 return None; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
110 } |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
111 |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
112 static object * |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
113 sys_setprofile(self, args) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
114 object *self; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
115 object *args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
116 { |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
117 if (args == None) |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
118 args = NULL; |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
119 else |
|
901
fea3f594df77
sysmodule.c: calling sys.settrace() or sys.setprofile() without
Guido van Rossum <guido@python.org>
parents:
788
diff
changeset
|
120 XINCREF(args); |
|
788
ba4ec13c0f9f
Fix reference counts of sys_trace and sys_profile
Guido van Rossum <guido@python.org>
parents:
710
diff
changeset
|
121 XDECREF(sys_profile); |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
122 sys_profile = args; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
123 INCREF(None); |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
124 return None; |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
125 } |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
126 |
| 86 | 127 static struct methodlist sys_methods[] = { |
| 128 {"exit", sys_exit}, | |
|
710
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
129 {"setprofile", sys_setprofile}, |
|
bc5c77fa8006
Added settrace() and setprofile().
Guido van Rossum <guido@python.org>
parents:
631
diff
changeset
|
130 {"settrace", sys_settrace}, |
| 86 | 131 {NULL, NULL} /* sentinel */ |
| 132 }; | |
| 133 | |
| 6 | 134 static object *sysin, *sysout, *syserr; |
| 135 | |
| 136 void | |
| 86 | 137 initsys() |
| 6 | 138 { |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
139 extern int fclose PROTO((FILE *)); |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
140 extern char version[]; |
|
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
141 object *v = newstringobject(version); |
| 86 | 142 object *m = initmodule("sys", sys_methods); |
| 143 sysdict = getmoduledict(m); | |
| 144 INCREF(sysdict); | |
| 6 | 145 /* NB keep an extra ref to the std files to avoid closing them |
| 146 when the user deletes them */ | |
|
265
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
147 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
148 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
|
187ba1733b17
Added fclose to newopenfileobject() calls.
Guido van Rossum <guido@python.org>
parents:
234
diff
changeset
|
149 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
| 6 | 150 if (err_occurred()) |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
151 fatal("can't create sys.* objects"); |
| 6 | 152 dictinsert(sysdict, "stdin", sysin); |
| 153 dictinsert(sysdict, "stdout", sysout); | |
| 154 dictinsert(sysdict, "stderr", syserr); | |
|
476
bec963f99bc9
Added sys.version object.
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
155 dictinsert(sysdict, "version", v); |
| 86 | 156 dictinsert(sysdict, "modules", get_modules()); |
| 6 | 157 if (err_occurred()) |
| 158 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
|
159 DECREF(v); |
| 86 | 160 } |
| 161 | |
| 162 static object * | |
| 163 makepathobject(path, delim) | |
| 164 char *path; | |
| 165 int delim; | |
| 166 { | |
| 167 int i, n; | |
| 168 char *p; | |
| 169 object *v, *w; | |
| 6 | 170 |
| 86 | 171 n = 1; |
| 172 p = path; | |
| 173 while ((p = strchr(p, delim)) != NULL) { | |
| 174 n++; | |
| 175 p++; | |
| 176 } | |
| 177 v = newlistobject(n); | |
| 178 if (v == NULL) | |
| 179 return NULL; | |
| 180 for (i = 0; ; i++) { | |
| 181 p = strchr(path, delim); | |
| 182 if (p == NULL) | |
| 183 p = strchr(path, '\0'); /* End of string */ | |
| 184 w = newsizedstringobject(path, (int) (p - path)); | |
| 185 if (w == NULL) { | |
| 186 DECREF(v); | |
| 187 return NULL; | |
| 188 } | |
| 189 setlistitem(v, i, w); | |
| 190 if (*p == '\0') | |
| 191 break; | |
| 192 path = p+1; | |
| 193 } | |
| 194 return v; | |
| 195 } | |
| 196 | |
| 197 void | |
| 198 setpythonpath(path) | |
| 199 char *path; | |
| 200 { | |
| 201 object *v; | |
| 202 if ((v = makepathobject(path, DELIM)) == NULL) | |
| 203 fatal("can't create sys.path"); | |
| 204 if (sysset("path", v) != 0) | |
| 205 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
|
206 DECREF(v); |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
207 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
208 |
| 86 | 209 static object * |
| 210 makeargvobject(argc, argv) | |
| 211 int argc; | |
| 212 char **argv; | |
|
83
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
213 { |
| 86 | 214 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
|
215 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
|
216 /* 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
|
217 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
|
218 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
|
219 argc = 1; |
|
e18f1fa398a3
Ensure that sys.argv[0] always exists (maybe as empty string).
Guido van Rossum <guido@python.org>
parents:
549
diff
changeset
|
220 } |
| 86 | 221 av = newlistobject(argc); |
| 222 if (av != NULL) { | |
| 223 int i; | |
| 224 for (i = 0; i < argc; i++) { | |
| 225 object *v = newstringobject(argv[i]); | |
| 226 if (v == NULL) { | |
| 227 DECREF(av); | |
| 228 av = NULL; | |
| 229 break; | |
| 230 } | |
| 231 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
|
232 } |
|
f6bcf3c1d714
Empty all modules' symbol tables, so most circular references are
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
233 } |
| 86 | 234 return av; |
| 6 | 235 } |
| 236 | |
| 237 void | |
| 86 | 238 setpythonargv(argc, argv) |
| 239 int argc; | |
| 240 char **argv; | |
| 6 | 241 { |
| 86 | 242 object *av = makeargvobject(argc, argv); |
| 243 if (av == NULL) | |
| 244 fatal("no mem for sys.argv"); | |
| 245 if (sysset("argv", av) != 0) | |
| 246 fatal("can't assign sys.argv"); | |
| 247 DECREF(av); | |
| 6 | 248 } |
