Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* System module */ |
| 26 | |
| 27 | /* |
| 28 | Various bits of information used by the interpreter are collected in |
| 29 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | Function member: |
Guido van Rossum | cc8914f | 1995-03-20 15:09:40 +0000 | [diff] [blame] | 31 | - exit(sts): raise SystemExit |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | Data members: |
| 33 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | - path: module search path (list of strings) |
| 36 | - argv: script arguments (list of strings) |
| 37 | - ps1, ps2: optional primary and secondary prompts (strings) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | #include "sysmodule.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | #include "modsupport.h" |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 45 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 47 | object *sys_trace, *sys_profile; |
Guido van Rossum | 9a1f388 | 1995-03-30 11:01:58 +0000 | [diff] [blame] | 48 | int sys_checkinterval = 10; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
| 50 | static object *sysdict; |
| 51 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 52 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 53 | extern void *PyWin_DLLhModule; |
| 54 | #endif |
| 55 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | object * |
| 57 | sysget(name) |
| 58 | char *name; |
| 59 | { |
| 60 | return dictlookup(sysdict, name); |
| 61 | } |
| 62 | |
| 63 | FILE * |
| 64 | sysgetfile(name, def) |
| 65 | char *name; |
| 66 | FILE *def; |
| 67 | { |
| 68 | FILE *fp = NULL; |
| 69 | object *v = sysget(name); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 70 | if (v != NULL && is_fileobject(v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | fp = getfilefile(v); |
| 72 | if (fp == NULL) |
| 73 | fp = def; |
| 74 | return fp; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | sysset(name, v) |
| 79 | char *name; |
| 80 | object *v; |
| 81 | { |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 82 | if (v == NULL) { |
| 83 | if (dictlookup(sysdict, name) == NULL) |
| 84 | return 0; |
| 85 | else |
| 86 | return dictremove(sysdict, name); |
| 87 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | else |
| 89 | return dictinsert(sysdict, name, v); |
| 90 | } |
| 91 | |
| 92 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | sys_exit(self, args) |
| 94 | object *self; |
| 95 | object *args; |
| 96 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 97 | /* Raise SystemExit so callers may catch it or clean up. */ |
| 98 | err_setval(SystemExit, args); |
| 99 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 102 | static object * |
| 103 | sys_settrace(self, args) |
| 104 | object *self; |
| 105 | object *args; |
| 106 | { |
| 107 | if (args == None) |
| 108 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 109 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 110 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 111 | XDECREF(sys_trace); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 112 | sys_trace = args; |
| 113 | INCREF(None); |
| 114 | return None; |
| 115 | } |
| 116 | |
| 117 | static object * |
| 118 | sys_setprofile(self, args) |
| 119 | object *self; |
| 120 | object *args; |
| 121 | { |
| 122 | if (args == None) |
| 123 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 124 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 125 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 126 | XDECREF(sys_profile); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 127 | sys_profile = args; |
| 128 | INCREF(None); |
| 129 | return None; |
| 130 | } |
| 131 | |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 132 | static object * |
| 133 | sys_setcheckinterval(self, args) |
| 134 | object *self; |
| 135 | object *args; |
| 136 | { |
| 137 | if (!newgetargs(args, "i", &sys_checkinterval)) |
| 138 | return NULL; |
| 139 | INCREF(None); |
| 140 | return None; |
| 141 | } |
| 142 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 143 | #ifdef USE_MALLOPT |
| 144 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 145 | #include <malloc.h> |
| 146 | |
| 147 | static object * |
| 148 | sys_mdebug(self, args) |
| 149 | object *self; |
| 150 | object *args; |
| 151 | { |
| 152 | int flag; |
| 153 | if (!getargs(args, "i", &flag)) |
| 154 | return NULL; |
| 155 | mallopt(M_DEBUG, flag); |
| 156 | INCREF(None); |
| 157 | return None; |
| 158 | } |
| 159 | #endif /* USE_MALLOPT */ |
| 160 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 161 | static object * |
| 162 | sys_getrefcount(self, args) |
| 163 | object *self; |
| 164 | object *args; |
| 165 | { |
| 166 | object *arg; |
| 167 | if (!getargs(args, "O", &arg)) |
| 168 | return NULL; |
| 169 | return newintobject((long) arg->ob_refcnt); |
| 170 | } |
| 171 | |
| 172 | #ifdef COUNT_ALLOCS |
| 173 | static PyObject * |
| 174 | sys_getcounts(self, args) |
| 175 | PyObject *self, *args; |
| 176 | { |
| 177 | extern PyObject *get_counts Py_PROTO((void)); |
| 178 | |
| 179 | if (!PyArg_Parse(args, "")) |
| 180 | return NULL; |
| 181 | return get_counts(); |
| 182 | } |
| 183 | #endif |
| 184 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 185 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 186 | /* Defined in objects.c because it uses static globals if that file */ |
| 187 | extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *)); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 188 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 190 | static struct methodlist sys_methods[] = { |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 191 | {"exit", sys_exit, 0}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 192 | {"getrefcount", sys_getrefcount, 0}, |
| 193 | #ifdef COUNT_ALLOCS |
| 194 | {"getcounts", sys_getcounts, 0}, |
| 195 | #endif |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 196 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 197 | {"getobjects", _Py_GetObjects, 1}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 198 | #endif |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 199 | #ifdef USE_MALLOPT |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 200 | {"mdebug", sys_mdebug, 0}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 201 | #endif |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 202 | {"setprofile", sys_setprofile, 0}, |
| 203 | {"settrace", sys_settrace, 0}, |
Guido van Rossum | b0352fa | 1996-01-12 01:15:01 +0000 | [diff] [blame] | 204 | {"setcheckinterval", sys_setcheckinterval, 1}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 205 | {NULL, NULL} /* sentinel */ |
| 206 | }; |
| 207 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | static object *sysin, *sysout, *syserr; |
| 209 | |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 210 | static object * |
| 211 | list_builtin_module_names() |
| 212 | { |
| 213 | object *list = newlistobject(0); |
| 214 | int i; |
| 215 | if (list == NULL) |
| 216 | return NULL; |
| 217 | for (i = 0; inittab[i].name != NULL; i++) { |
| 218 | object *name = newstringobject(inittab[i].name); |
| 219 | if (name == NULL) |
| 220 | break; |
| 221 | addlistitem(list, name); |
| 222 | DECREF(name); |
| 223 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 224 | if (sortlist(list) != 0) { |
| 225 | DECREF(list); |
| 226 | list = NULL; |
| 227 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 228 | return list; |
| 229 | } |
| 230 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 232 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 234 | extern long getmaxint PROTO((void)); |
| 235 | extern char *getversion PROTO((void)); |
| 236 | extern char *getcopyright PROTO((void)); |
Guido van Rossum | aaf1839 | 1995-07-07 22:45:02 +0000 | [diff] [blame] | 237 | extern char *getplatform PROTO((void)); |
Guido van Rossum | 8fa9b6f | 1996-06-17 17:02:48 +0000 | [diff] [blame] | 238 | extern char *Py_GetPrefix PROTO((void)); |
| 239 | extern char *Py_GetExecPrefix PROTO((void)); |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 240 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 241 | object *m = initmodule("sys", sys_methods); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 242 | object *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 243 | sysdict = getmoduledict(m); |
| 244 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | /* NB keep an extra ref to the std files to avoid closing them |
| 246 | when the user deletes them */ |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 247 | sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
| 248 | sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
| 249 | syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | if (err_occurred()) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 251 | fatal("can't initialize sys.std{in,out,err}"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | dictinsert(sysdict, "stdin", sysin); |
| 253 | dictinsert(sysdict, "stdout", sysout); |
| 254 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 255 | dictinsert(sysdict, "version", v = newstringobject(getversion())); |
| 256 | XDECREF(v); |
| 257 | dictinsert(sysdict, "copyright", v = newstringobject(getcopyright())); |
| 258 | XDECREF(v); |
Guido van Rossum | aaf1839 | 1995-07-07 22:45:02 +0000 | [diff] [blame] | 259 | dictinsert(sysdict, "platform", v = newstringobject(getplatform())); |
| 260 | XDECREF(v); |
Guido van Rossum | 8fa9b6f | 1996-06-17 17:02:48 +0000 | [diff] [blame] | 261 | dictinsert(sysdict, "prefix", v = newstringobject(Py_GetPrefix())); |
| 262 | XDECREF(v); |
| 263 | dictinsert(sysdict, "exec_prefix", |
| 264 | v = newstringobject(Py_GetExecPrefix())); |
| 265 | XDECREF(v); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 266 | dictinsert(sysdict, "maxint", v = newintobject(getmaxint())); |
| 267 | XDECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 268 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 269 | dictinsert(sysdict, "builtin_module_names", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 270 | v = list_builtin_module_names()); |
| 271 | XDECREF(v); |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 272 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 273 | dictinsert(sysdict, "dllhandle", v = newintobject((int)PyWin_DLLhModule)); |
| 274 | XDECREF(v); |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 275 | dictinsert(sysdict, "winver", v = newstringobject(MS_DLL_ID)); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 276 | XDECREF(v); |
| 277 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | if (err_occurred()) |
| 279 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 282 | static object * |
| 283 | makepathobject(path, delim) |
| 284 | char *path; |
| 285 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 286 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 287 | int i, n; |
| 288 | char *p; |
| 289 | object *v, *w; |
| 290 | |
| 291 | n = 1; |
| 292 | p = path; |
| 293 | while ((p = strchr(p, delim)) != NULL) { |
| 294 | n++; |
| 295 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 296 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 297 | v = newlistobject(n); |
| 298 | if (v == NULL) |
| 299 | return NULL; |
| 300 | for (i = 0; ; i++) { |
| 301 | p = strchr(path, delim); |
| 302 | if (p == NULL) |
| 303 | p = strchr(path, '\0'); /* End of string */ |
| 304 | w = newsizedstringobject(path, (int) (p - path)); |
| 305 | if (w == NULL) { |
| 306 | DECREF(v); |
| 307 | return NULL; |
| 308 | } |
| 309 | setlistitem(v, i, w); |
| 310 | if (*p == '\0') |
| 311 | break; |
| 312 | path = p+1; |
| 313 | } |
| 314 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 318 | setpythonpath(path) |
| 319 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 321 | object *v; |
| 322 | if ((v = makepathobject(path, DELIM)) == NULL) |
| 323 | fatal("can't create sys.path"); |
| 324 | if (sysset("path", v) != 0) |
| 325 | fatal("can't assign sys.path"); |
| 326 | DECREF(v); |
| 327 | } |
| 328 | |
| 329 | static object * |
| 330 | makeargvobject(argc, argv) |
| 331 | int argc; |
| 332 | char **argv; |
| 333 | { |
| 334 | object *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 335 | if (argc <= 0 || argv == NULL) { |
| 336 | /* Ensure at least one (empty) argument is seen */ |
| 337 | static char *empty_argv[1] = {""}; |
| 338 | argv = empty_argv; |
| 339 | argc = 1; |
| 340 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 341 | av = newlistobject(argc); |
| 342 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 343 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 344 | for (i = 0; i < argc; i++) { |
| 345 | object *v = newstringobject(argv[i]); |
| 346 | if (v == NULL) { |
| 347 | DECREF(av); |
| 348 | av = NULL; |
| 349 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 350 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 351 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 352 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 353 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 354 | return av; |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | setpythonargv(argc, argv) |
| 359 | int argc; |
| 360 | char **argv; |
| 361 | { |
| 362 | object *av = makeargvobject(argc, argv); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 363 | object *path = sysget("path"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 364 | if (av == NULL) |
| 365 | fatal("no mem for sys.argv"); |
| 366 | if (sysset("argv", av) != 0) |
| 367 | fatal("can't assign sys.argv"); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 368 | if (path != NULL) { |
| 369 | char *p = NULL; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 370 | int n = 0; |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 371 | object *a; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 372 | #if SEP == '\\' /* Special case for MS filename syntax */ |
| 373 | if (argc > 0 && argv[0] != NULL) { |
| 374 | char *q; |
| 375 | p = strrchr(argv[0], SEP); |
| 376 | /* Test for alternate separator */ |
| 377 | q = strrchr(p ? p : argv[0], '/'); |
| 378 | if (q != NULL) |
| 379 | p = q; |
| 380 | if (p != NULL) { |
| 381 | n = p + 1 - argv[0]; |
| 382 | if (n > 1 && p[-1] != ':') |
| 383 | n--; /* Drop trailing separator */ |
| 384 | } |
| 385 | } |
| 386 | #else /* All other filename syntaxes */ |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 387 | if (argc > 0 && argv[0] != NULL) |
| 388 | p = strrchr(argv[0], SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 389 | if (p != NULL) { |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 390 | n = p + 1 - argv[0]; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 391 | #if SEP == '/' /* Special case for Unix filename syntax */ |
| 392 | if (n > 1) |
| 393 | n--; /* Drop trailing separator */ |
| 394 | #endif /* Unix */ |
| 395 | } |
| 396 | #endif /* All others */ |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 397 | a = newsizedstringobject(argv[0], n); |
| 398 | if (a == NULL) |
| 399 | fatal("no mem for sys.path insertion"); |
| 400 | if (inslistitem(path, 0, a) < 0) |
| 401 | fatal("sys.path.insert(0) failed"); |
| 402 | DECREF(a); |
Guido van Rossum | a63d9f4 | 1996-07-24 01:31:37 +0000 | [diff] [blame] | 403 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 404 | DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 405 | } |