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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* System module */ |
| 33 | |
| 34 | /* |
| 35 | Various bits of information used by the interpreter are collected in |
| 36 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | Function member: |
Guido van Rossum | cc8914f | 1995-03-20 15:09:40 +0000 | [diff] [blame] | 38 | - exit(sts): raise SystemExit |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | Data members: |
| 40 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | - path: module search path (list of strings) |
| 43 | - argv: script arguments (list of strings) |
| 44 | - ps1, ps2: optional primary and secondary prompts (strings) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 47 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 49 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 1254d79 | 1997-05-20 15:57:25 +0000 | [diff] [blame] | 51 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 52 | #include <unistd.h> |
| 53 | #endif |
| 54 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 55 | static PyObject *sysdict; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 57 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 58 | extern void *PyWin_DLLhModule; |
| 59 | #endif |
| 60 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 61 | PyObject * |
| 62 | PySys_GetObject(name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | char *name; |
| 64 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 65 | return PyDict_GetItemString(sysdict, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | FILE * |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 69 | PySys_GetFile(name, def) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | char *name; |
| 71 | FILE *def; |
| 72 | { |
| 73 | FILE *fp = NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 74 | PyObject *v = PySys_GetObject(name); |
| 75 | if (v != NULL && PyFile_Check(v)) |
| 76 | fp = PyFile_AsFile(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | if (fp == NULL) |
| 78 | fp = def; |
| 79 | return fp; |
| 80 | } |
| 81 | |
| 82 | int |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 83 | PySys_SetObject(name, v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | char *name; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 85 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | { |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 87 | if (v == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 88 | if (PyDict_GetItemString(sysdict, name) == NULL) |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | else |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 91 | return PyDict_DelItemString(sysdict, name); |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 92 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | else |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 94 | return PyDict_SetItemString(sysdict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 97 | static PyObject * |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 98 | sys_exc_info(self, args) |
| 99 | PyObject *self; |
| 100 | PyObject *args; |
| 101 | { |
| 102 | PyThreadState *tstate; |
| 103 | if (!PyArg_Parse(args, "")) |
| 104 | return NULL; |
| 105 | tstate = PyThreadState_Get(); |
| 106 | if (tstate == NULL) |
| 107 | Py_FatalError("sys.exc_info(): no thread state"); |
| 108 | return Py_BuildValue( |
| 109 | "(OOO)", |
| 110 | tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
| 111 | tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
| 112 | tstate->exc_traceback != NULL ? |
| 113 | tstate->exc_traceback : Py_None); |
| 114 | } |
| 115 | |
| 116 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | sys_exit(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 118 | PyObject *self; |
| 119 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 121 | /* Raise SystemExit so callers may catch it or clean up. */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 122 | PyErr_SetObject(PyExc_SystemExit, args); |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 123 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 126 | static PyObject * |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 127 | sys_settrace(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 128 | PyObject *self; |
| 129 | PyObject *args; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 130 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 131 | PyThreadState *tstate = PyThreadState_Get(); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 132 | if (args == Py_None) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 133 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 134 | else |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 135 | Py_XINCREF(args); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 136 | Py_XDECREF(tstate->sys_tracefunc); |
| 137 | tstate->sys_tracefunc = args; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 138 | Py_INCREF(Py_None); |
| 139 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 142 | static PyObject * |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 143 | sys_setprofile(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 144 | PyObject *self; |
| 145 | PyObject *args; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 147 | PyThreadState *tstate = PyThreadState_Get(); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 148 | if (args == Py_None) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 149 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 150 | else |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 151 | Py_XINCREF(args); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 152 | Py_XDECREF(tstate->sys_profilefunc); |
| 153 | tstate->sys_profilefunc = args; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 154 | Py_INCREF(Py_None); |
| 155 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 158 | static PyObject * |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 159 | sys_setcheckinterval(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 160 | PyObject *self; |
| 161 | PyObject *args; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 162 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 163 | PyThreadState *tstate = PyThreadState_Get(); |
| 164 | if (!PyArg_ParseTuple(args, "i", &tstate->sys_checkinterval)) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 165 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 166 | Py_INCREF(Py_None); |
| 167 | return Py_None; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 170 | #ifdef USE_MALLOPT |
| 171 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 172 | #include <malloc.h> |
| 173 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 174 | static PyObject * |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 175 | sys_mdebug(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 176 | PyObject *self; |
| 177 | PyObject *args; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 178 | { |
| 179 | int flag; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 180 | if (!PyArg_Parse(args, "i", &flag)) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 181 | return NULL; |
| 182 | mallopt(M_DEBUG, flag); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 183 | Py_INCREF(Py_None); |
| 184 | return Py_None; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 185 | } |
| 186 | #endif /* USE_MALLOPT */ |
| 187 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 188 | static PyObject * |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 189 | sys_getrefcount(self, args) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 190 | PyObject *self; |
| 191 | PyObject *args; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 193 | PyObject *arg; |
| 194 | if (!PyArg_Parse(args, "O", &arg)) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 195 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 196 | return PyInt_FromLong((long) arg->ob_refcnt); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | #ifdef COUNT_ALLOCS |
| 200 | static PyObject * |
| 201 | sys_getcounts(self, args) |
| 202 | PyObject *self, *args; |
| 203 | { |
| 204 | extern PyObject *get_counts Py_PROTO((void)); |
| 205 | |
| 206 | if (!PyArg_Parse(args, "")) |
| 207 | return NULL; |
| 208 | return get_counts(); |
| 209 | } |
| 210 | #endif |
| 211 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 212 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 213 | /* Defined in objects.c because it uses static globals if that file */ |
| 214 | extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *)); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 215 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 217 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 218 | /* Defined in ceval.c because it uses static globals if that file */ |
| 219 | extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *)); |
| 220 | #endif |
| 221 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 222 | static PyMethodDef sys_methods[] = { |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 223 | /* Might as well keep this in alphabetic order */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 224 | {"exc_info", sys_exc_info, 0}, |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 225 | {"exit", sys_exit, 0}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 226 | #ifdef COUNT_ALLOCS |
| 227 | {"getcounts", sys_getcounts, 0}, |
| 228 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 229 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 230 | {"getdxp", _Py_GetDXProfile, 1}, |
| 231 | #endif |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 232 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 233 | {"getobjects", _Py_GetObjects, 1}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 234 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 235 | {"getrefcount", sys_getrefcount, 0}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 236 | #ifdef USE_MALLOPT |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 237 | {"mdebug", sys_mdebug, 0}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 238 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 239 | {"setcheckinterval", sys_setcheckinterval, 1}, |
Guido van Rossum | 4f508cc | 1995-02-21 21:03:24 +0000 | [diff] [blame] | 240 | {"setprofile", sys_setprofile, 0}, |
| 241 | {"settrace", sys_settrace, 0}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 242 | {NULL, NULL} /* sentinel */ |
| 243 | }; |
| 244 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 245 | static PyObject *sysin, *sysout, *syserr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 247 | static PyObject * |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 248 | list_builtin_module_names() |
| 249 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 250 | PyObject *list = PyList_New(0); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 251 | int i; |
| 252 | if (list == NULL) |
| 253 | return NULL; |
Guido van Rossum | bb5c6f8 | 1997-04-29 20:42:30 +0000 | [diff] [blame] | 254 | for (i = 0; _PyImport_Inittab[i].name != NULL; i++) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 255 | PyObject *name = PyString_FromString( |
| 256 | _PyImport_Inittab[i].name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 257 | if (name == NULL) |
| 258 | break; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 259 | PyList_Append(list, name); |
| 260 | Py_DECREF(name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 261 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 262 | if (PyList_Sort(list) != 0) { |
| 263 | Py_DECREF(list); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 264 | list = NULL; |
| 265 | } |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 266 | if (list) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 267 | PyObject *v = PyList_AsTuple(list); |
| 268 | Py_DECREF(list); |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 269 | list = v; |
| 270 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 271 | return list; |
| 272 | } |
| 273 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | void |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 275 | PySys_Init() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 276 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 277 | extern long PyInt_GetMax Py_PROTO((void)); |
| 278 | extern char *Py_GetVersion Py_PROTO((void)); |
| 279 | extern char *Py_GetCopyright Py_PROTO((void)); |
| 280 | extern char *Py_GetPlatform Py_PROTO((void)); |
Guido van Rossum | b2c8ec4 | 1997-05-22 20:41:20 +0000 | [diff] [blame^] | 281 | extern char *Py_GetProgramFullPath Py_PROTO((void)); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 282 | extern char *Py_GetPrefix Py_PROTO((void)); |
| 283 | extern char *Py_GetExecPrefix Py_PROTO((void)); |
| 284 | extern int fclose Py_PROTO((FILE *)); |
| 285 | PyObject *m = Py_InitModule("sys", sys_methods); |
| 286 | PyObject *v; |
| 287 | sysdict = PyModule_GetDict(m); |
| 288 | Py_INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 289 | /* NB keep an extra ref to the std files to avoid closing them |
| 290 | when the user deletes them */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 291 | sysin = PyFile_FromFile(stdin, "<stdin>", "r", fclose); |
| 292 | sysout = PyFile_FromFile(stdout, "<stdout>", "w", fclose); |
| 293 | syserr = PyFile_FromFile(stderr, "<stderr>", "w", fclose); |
| 294 | if (PyErr_Occurred()) |
| 295 | Py_FatalError("can't initialize sys.std{in,out,err}"); |
| 296 | PyDict_SetItemString(sysdict, "stdin", sysin); |
| 297 | PyDict_SetItemString(sysdict, "stdout", sysout); |
| 298 | PyDict_SetItemString(sysdict, "stderr", syserr); |
| 299 | PyDict_SetItemString(sysdict, "version", |
| 300 | v = PyString_FromString(Py_GetVersion())); |
| 301 | Py_XDECREF(v); |
| 302 | PyDict_SetItemString(sysdict, "copyright", |
| 303 | v = PyString_FromString(Py_GetCopyright())); |
| 304 | Py_XDECREF(v); |
| 305 | PyDict_SetItemString(sysdict, "platform", |
| 306 | v = PyString_FromString(Py_GetPlatform())); |
| 307 | Py_XDECREF(v); |
Guido van Rossum | b2c8ec4 | 1997-05-22 20:41:20 +0000 | [diff] [blame^] | 308 | PyDict_SetItemString(sysdict, "executable", |
| 309 | v = PyString_FromString(Py_GetProgramFullPath())); |
| 310 | Py_XDECREF(v); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 311 | PyDict_SetItemString(sysdict, "prefix", |
| 312 | v = PyString_FromString(Py_GetPrefix())); |
| 313 | Py_XDECREF(v); |
| 314 | PyDict_SetItemString(sysdict, "exec_prefix", |
| 315 | v = PyString_FromString(Py_GetExecPrefix())); |
| 316 | Py_XDECREF(v); |
| 317 | PyDict_SetItemString(sysdict, "maxint", |
| 318 | v = PyInt_FromLong(PyInt_GetMax())); |
| 319 | Py_XDECREF(v); |
| 320 | PyDict_SetItemString(sysdict, "modules", PyImport_GetModuleDict()); |
| 321 | PyDict_SetItemString(sysdict, "builtin_module_names", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 322 | v = list_builtin_module_names()); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 323 | Py_XDECREF(v); |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 324 | #ifdef MS_COREDLL |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 325 | PyDict_SetItemString(sysdict, "dllhandle", |
| 326 | v = PyInt_FromLong((int)PyWin_DLLhModule)); |
| 327 | Py_XDECREF(v); |
| 328 | PyDict_SetItemString(sysdict, "winver", |
| 329 | v = PyString_FromString(MS_DLL_ID)); |
| 330 | Py_XDECREF(v); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 331 | #endif |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 332 | if (PyErr_Occurred()) |
| 333 | Py_FatalError("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 336 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 337 | makepathobject(path, delim) |
| 338 | char *path; |
| 339 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 340 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 341 | int i, n; |
| 342 | char *p; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 343 | PyObject *v, *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 344 | |
| 345 | n = 1; |
| 346 | p = path; |
| 347 | while ((p = strchr(p, delim)) != NULL) { |
| 348 | n++; |
| 349 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 350 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 351 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 352 | if (v == NULL) |
| 353 | return NULL; |
| 354 | for (i = 0; ; i++) { |
| 355 | p = strchr(path, delim); |
| 356 | if (p == NULL) |
| 357 | p = strchr(path, '\0'); /* End of string */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 358 | w = PyString_FromStringAndSize(path, (int) (p - path)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 359 | if (w == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 360 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 363 | PyList_SetItem(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 364 | if (*p == '\0') |
| 365 | break; |
| 366 | path = p+1; |
| 367 | } |
| 368 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 372 | PySys_SetPath(path) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 373 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 374 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 375 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 376 | if ((v = makepathobject(path, DELIM)) == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 377 | Py_FatalError("can't create sys.path"); |
| 378 | if (PySys_SetObject("path", v) != 0) |
| 379 | Py_FatalError("can't assign sys.path"); |
| 380 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 383 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 384 | makeargvobject(argc, argv) |
| 385 | int argc; |
| 386 | char **argv; |
| 387 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 388 | PyObject *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 389 | if (argc <= 0 || argv == NULL) { |
| 390 | /* Ensure at least one (empty) argument is seen */ |
| 391 | static char *empty_argv[1] = {""}; |
| 392 | argv = empty_argv; |
| 393 | argc = 1; |
| 394 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 395 | av = PyList_New(argc); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 397 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 398 | for (i = 0; i < argc; i++) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 399 | PyObject *v = PyString_FromString(argv[i]); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 400 | if (v == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 401 | Py_DECREF(av); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 402 | av = NULL; |
| 403 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 404 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 405 | PyList_SetItem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 406 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 407 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 408 | return av; |
| 409 | } |
| 410 | |
| 411 | void |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 412 | PySys_SetArgv(argc, argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 413 | int argc; |
| 414 | char **argv; |
| 415 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 416 | PyObject *av = makeargvobject(argc, argv); |
| 417 | PyObject *path = PySys_GetObject("path"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 418 | if (av == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 419 | Py_FatalError("no mem for sys.argv"); |
| 420 | if (PySys_SetObject("argv", av) != 0) |
| 421 | Py_FatalError("can't assign sys.argv"); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 422 | if (path != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 423 | char *argv0 = argv[0]; |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 424 | char *p = NULL; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 425 | int n = 0; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 426 | PyObject *a; |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 427 | #ifdef HAVE_READLINK |
| 428 | char link[MAXPATHLEN+1]; |
| 429 | char argv0copy[2*MAXPATHLEN+1]; |
| 430 | int nr = 0; |
| 431 | if (argc > 0 && argv0 != NULL) |
| 432 | nr = readlink(argv0, link, MAXPATHLEN); |
| 433 | if (nr > 0) { |
| 434 | /* It's a symlink */ |
| 435 | link[nr] = '\0'; |
| 436 | if (link[0] == SEP) |
| 437 | argv0 = link; /* Link to absolute path */ |
| 438 | else if (strchr(link, SEP) == NULL) |
| 439 | ; /* Link without path */ |
| 440 | else { |
| 441 | /* Must join(dirname(argv0), link) */ |
| 442 | char *q = strrchr(argv0, SEP); |
| 443 | if (q == NULL) |
| 444 | argv0 = link; /* argv0 without path */ |
| 445 | else { |
| 446 | /* Must make a copy */ |
| 447 | strcpy(argv0copy, argv0); |
| 448 | q = strrchr(argv0copy, SEP); |
| 449 | strcpy(q+1, link); |
| 450 | argv0 = argv0copy; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | #endif /* HAVE_READLINK */ |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 455 | #if SEP == '\\' /* Special case for MS filename syntax */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 456 | if (argc > 0 && argv0 != NULL) { |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 457 | char *q; |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 458 | p = strrchr(argv0, SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 459 | /* Test for alternate separator */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 460 | q = strrchr(p ? p : argv0, '/'); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 461 | if (q != NULL) |
| 462 | p = q; |
| 463 | if (p != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 464 | n = p + 1 - argv0; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 465 | if (n > 1 && p[-1] != ':') |
| 466 | n--; /* Drop trailing separator */ |
| 467 | } |
| 468 | } |
| 469 | #else /* All other filename syntaxes */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 470 | if (argc > 0 && argv0 != NULL) |
| 471 | p = strrchr(argv0, SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 472 | if (p != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 473 | n = p + 1 - argv0; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 474 | #if SEP == '/' /* Special case for Unix filename syntax */ |
| 475 | if (n > 1) |
| 476 | n--; /* Drop trailing separator */ |
| 477 | #endif /* Unix */ |
| 478 | } |
| 479 | #endif /* All others */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 480 | a = PyString_FromStringAndSize(argv0, n); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 481 | if (a == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 482 | Py_FatalError("no mem for sys.path insertion"); |
| 483 | if (PyList_Insert(path, 0, a) < 0) |
| 484 | Py_FatalError("sys.path.insert(0) failed"); |
| 485 | Py_DECREF(a); |
Guido van Rossum | a63d9f4 | 1996-07-24 01:31:37 +0000 | [diff] [blame] | 486 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 487 | Py_DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 488 | } |