Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* System module */ |
| 3 | |
| 4 | /* |
| 5 | Various bits of information used by the interpreter are collected in |
| 6 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | Function member: |
Guido van Rossum | cc8914f | 1995-03-20 15:09:40 +0000 | [diff] [blame] | 8 | - exit(sts): raise SystemExit |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | Data members: |
| 10 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | - path: module search path (list of strings) |
| 13 | - argv: script arguments (list of strings) |
| 14 | - ps1, ps2: optional primary and secondary prompts (strings) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 17 | #include "Python.h" |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 18 | #include "compile.h" |
| 19 | #include "frameobject.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 21 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 1254d79 | 1997-05-20 15:57:25 +0000 | [diff] [blame] | 23 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 27 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 28 | extern void *PyWin_DLLhModule; |
Guido van Rossum | 6c1e5f2 | 1997-09-29 23:34:23 +0000 | [diff] [blame] | 29 | /* A string loaded from the DLL at startup: */ |
| 30 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 33 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 34 | PySys_GetObject(char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 36 | PyThreadState *tstate = PyThreadState_Get(); |
| 37 | PyObject *sd = tstate->interp->sysdict; |
Guido van Rossum | be20336 | 1999-10-05 22:17:41 +0000 | [diff] [blame] | 38 | if (sd == NULL) |
| 39 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 40 | return PyDict_GetItemString(sd, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | FILE * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 44 | PySys_GetFile(char *name, FILE *def) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | { |
| 46 | FILE *fp = NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 47 | PyObject *v = PySys_GetObject(name); |
| 48 | if (v != NULL && PyFile_Check(v)) |
| 49 | fp = PyFile_AsFile(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | if (fp == NULL) |
| 51 | fp = def; |
| 52 | return fp; |
| 53 | } |
| 54 | |
| 55 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 56 | PySys_SetObject(char *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 58 | PyThreadState *tstate = PyThreadState_Get(); |
| 59 | PyObject *sd = tstate->interp->sysdict; |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 60 | if (v == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 61 | if (PyDict_GetItemString(sd, name) == NULL) |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | else |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 64 | return PyDict_DelItemString(sd, name); |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 65 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | else |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 67 | return PyDict_SetItemString(sd, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 70 | static PyObject * |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 71 | sys_displayhook(PyObject *self, PyObject *args) |
| 72 | { |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 73 | PyObject *o, *outf; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 74 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 75 | PyObject *modules = interp->modules; |
| 76 | PyObject *builtins = PyDict_GetItemString(modules, "__builtin__"); |
| 77 | |
Moshe Zadka | 03897ea | 2001-07-23 13:32:43 +0000 | [diff] [blame^] | 78 | if (builtins == NULL) { |
| 79 | PyErr_SetString(PyExc_RuntimeError, "lost __builtin__"); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 83 | /* parse arguments */ |
| 84 | if (!PyArg_ParseTuple(args, "O:displayhook", &o)) |
| 85 | return NULL; |
| 86 | |
| 87 | /* Print value except if None */ |
| 88 | /* After printing, also assign to '_' */ |
| 89 | /* Before, set '_' to None to avoid recursion */ |
| 90 | if (o == Py_None) { |
| 91 | Py_INCREF(Py_None); |
| 92 | return Py_None; |
| 93 | } |
| 94 | if (PyObject_SetAttrString(builtins, "_", Py_None) != 0) |
| 95 | return NULL; |
| 96 | if (Py_FlushLine() != 0) |
| 97 | return NULL; |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 98 | outf = PySys_GetObject("stdout"); |
| 99 | if (outf == NULL) { |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 100 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
| 101 | return NULL; |
| 102 | } |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 103 | if (PyFile_WriteObject(o, outf, 0) != 0) |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 104 | return NULL; |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 105 | PyFile_SoftSpace(outf, 1); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 106 | if (Py_FlushLine() != 0) |
| 107 | return NULL; |
| 108 | if (PyObject_SetAttrString(builtins, "_", o) != 0) |
| 109 | return NULL; |
| 110 | Py_INCREF(Py_None); |
| 111 | return Py_None; |
| 112 | } |
| 113 | |
| 114 | static char displayhook_doc[] = |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 115 | "displayhook(object) -> None\n" |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 116 | "\n" |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 117 | "Print an object to sys.stdout and also save it in __builtin__._\n"; |
| 118 | |
| 119 | static PyObject * |
| 120 | sys_excepthook(PyObject* self, PyObject* args) |
| 121 | { |
| 122 | PyObject *exc, *value, *tb; |
| 123 | if (!PyArg_ParseTuple(args, "OOO:excepthook", &exc, &value, &tb)) |
| 124 | return NULL; |
| 125 | PyErr_Display(exc, value, tb); |
| 126 | Py_INCREF(Py_None); |
| 127 | return Py_None; |
| 128 | } |
| 129 | |
| 130 | static char excepthook_doc[] = |
| 131 | "excepthook(exctype, value, traceback) -> None\n" |
| 132 | "\n" |
| 133 | "Handle an exception by displaying it with a traceback on sys.stderr.\n"; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 134 | |
| 135 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 136 | sys_exc_info(PyObject *self, PyObject *args) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 137 | { |
| 138 | PyThreadState *tstate; |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 139 | if (!PyArg_ParseTuple(args, ":exc_info")) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 140 | return NULL; |
| 141 | tstate = PyThreadState_Get(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 142 | return Py_BuildValue( |
| 143 | "(OOO)", |
| 144 | tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
| 145 | tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
| 146 | tstate->exc_traceback != NULL ? |
| 147 | tstate->exc_traceback : Py_None); |
| 148 | } |
| 149 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 150 | static char exc_info_doc[] = |
| 151 | "exc_info() -> (type, value, traceback)\n\ |
| 152 | \n\ |
| 153 | Return information about the exception that is currently being handled.\n\ |
| 154 | This should be called from inside an except clause only."; |
| 155 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 156 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | sys_exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 159 | /* Raise SystemExit so callers may catch it or clean up. */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 160 | PyErr_SetObject(PyExc_SystemExit, args); |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 161 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 164 | static char exit_doc[] = |
| 165 | "exit([status])\n\ |
| 166 | \n\ |
| 167 | Exit the interpreter by raising SystemExit(status).\n\ |
| 168 | If the status is omitted or None, it defaults to zero (i.e., success).\n\ |
| 169 | If the status numeric, it will be used as the system exit status.\n\ |
| 170 | If it is another kind of object, it will be printed and the system\n\ |
| 171 | exit status will be one (i.e., failure)."; |
| 172 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 173 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 174 | sys_getdefaultencoding(PyObject *self, PyObject *args) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 175 | { |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 176 | if (!PyArg_ParseTuple(args, ":getdefaultencoding")) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 177 | return NULL; |
| 178 | return PyString_FromString(PyUnicode_GetDefaultEncoding()); |
| 179 | } |
| 180 | |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 181 | static char getdefaultencoding_doc[] = |
| 182 | "getdefaultencoding() -> string\n\ |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 183 | \n\ |
| 184 | Return the current default string encoding used by the Unicode \n\ |
| 185 | implementation."; |
| 186 | |
| 187 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 188 | sys_setdefaultencoding(PyObject *self, PyObject *args) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 189 | { |
| 190 | char *encoding; |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 191 | if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding)) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 192 | return NULL; |
| 193 | if (PyUnicode_SetDefaultEncoding(encoding)) |
| 194 | return NULL; |
| 195 | Py_INCREF(Py_None); |
| 196 | return Py_None; |
| 197 | } |
| 198 | |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 199 | static char setdefaultencoding_doc[] = |
| 200 | "setdefaultencoding(encoding)\n\ |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 201 | \n\ |
| 202 | Set the current default string encoding used by the Unicode implementation."; |
| 203 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 204 | /* |
| 205 | * Cached interned string objects used for calling the profile and |
| 206 | * trace functions. Initialized by trace_init(). |
| 207 | */ |
| 208 | static PyObject *whatstrings[4] = {NULL, NULL, NULL, NULL}; |
| 209 | |
| 210 | static int |
| 211 | trace_init(void) |
| 212 | { |
| 213 | static char *whatnames[4] = {"call", "exception", "line", "return"}; |
| 214 | PyObject *name; |
| 215 | int i; |
| 216 | for (i = 0; i < 4; ++i) { |
| 217 | if (whatstrings[i] == NULL) { |
| 218 | name = PyString_InternFromString(whatnames[i]); |
| 219 | if (name == NULL) |
| 220 | return -1; |
| 221 | whatstrings[i] = name; |
| 222 | } |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static PyObject * |
| 229 | call_trampoline(PyThreadState *tstate, PyObject* callback, |
| 230 | PyFrameObject *frame, int what, PyObject *arg) |
| 231 | { |
| 232 | PyObject *args = PyTuple_New(3); |
| 233 | PyObject *whatstr; |
| 234 | PyObject *result; |
| 235 | |
| 236 | if (args == NULL) |
| 237 | return NULL; |
| 238 | Py_INCREF(frame); |
| 239 | whatstr = whatstrings[what]; |
| 240 | Py_INCREF(whatstr); |
| 241 | if (arg == NULL) |
| 242 | arg = Py_None; |
| 243 | Py_INCREF(arg); |
| 244 | PyTuple_SET_ITEM(args, 0, (PyObject *)frame); |
| 245 | PyTuple_SET_ITEM(args, 1, whatstr); |
| 246 | PyTuple_SET_ITEM(args, 2, arg); |
| 247 | |
| 248 | /* call the Python-level function */ |
| 249 | PyFrame_FastToLocals(frame); |
| 250 | result = PyEval_CallObject(callback, args); |
| 251 | PyFrame_LocalsToFast(frame, 1); |
| 252 | if (result == NULL) |
| 253 | PyTraceBack_Here(frame); |
| 254 | |
| 255 | /* cleanup */ |
| 256 | Py_DECREF(args); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | profile_trampoline(PyObject *self, PyFrameObject *frame, |
| 262 | int what, PyObject *arg) |
| 263 | { |
| 264 | PyThreadState *tstate = frame->f_tstate; |
| 265 | PyObject *result; |
| 266 | |
| 267 | result = call_trampoline(tstate, self, frame, what, arg); |
| 268 | if (result == NULL) { |
| 269 | PyEval_SetProfile(NULL, NULL); |
| 270 | return -1; |
| 271 | } |
| 272 | Py_DECREF(result); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int |
| 277 | trace_trampoline(PyObject *self, PyFrameObject *frame, |
| 278 | int what, PyObject *arg) |
| 279 | { |
| 280 | PyThreadState *tstate = frame->f_tstate; |
| 281 | PyObject *callback; |
| 282 | PyObject *result; |
| 283 | |
| 284 | if (what == PyTrace_CALL) |
| 285 | callback = self; |
| 286 | else |
| 287 | callback = frame->f_trace; |
| 288 | if (callback == NULL) |
| 289 | return 0; |
| 290 | result = call_trampoline(tstate, callback, frame, what, arg); |
| 291 | if (result == NULL) { |
| 292 | PyEval_SetTrace(NULL, NULL); |
| 293 | Py_XDECREF(frame->f_trace); |
| 294 | frame->f_trace = NULL; |
| 295 | return -1; |
| 296 | } |
| 297 | if (result != Py_None) { |
| 298 | PyObject *temp = frame->f_trace; |
| 299 | frame->f_trace = NULL; |
| 300 | Py_XDECREF(temp); |
| 301 | frame->f_trace = result; |
| 302 | } |
| 303 | else { |
| 304 | Py_DECREF(result); |
| 305 | } |
| 306 | return 0; |
| 307 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 308 | |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 309 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 310 | sys_settrace(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 311 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 312 | if (trace_init() == -1) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 313 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 314 | if (args == Py_None) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 315 | PyEval_SetTrace(NULL, NULL); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 316 | else |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 317 | PyEval_SetTrace(trace_trampoline, args); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 318 | Py_INCREF(Py_None); |
| 319 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 322 | static char settrace_doc[] = |
| 323 | "settrace(function)\n\ |
| 324 | \n\ |
| 325 | Set the global debug tracing function. It will be called on each\n\ |
| 326 | function call. See the debugger chapter in the library manual."; |
| 327 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 328 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 329 | sys_setprofile(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 330 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 331 | if (trace_init() == -1) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 332 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 333 | if (args == Py_None) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 334 | PyEval_SetProfile(NULL, NULL); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 335 | else |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 336 | PyEval_SetProfile(profile_trampoline, args); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 337 | Py_INCREF(Py_None); |
| 338 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 341 | static char setprofile_doc[] = |
| 342 | "setprofile(function)\n\ |
| 343 | \n\ |
| 344 | Set the profiling function. It will be called on each function call\n\ |
| 345 | and return. See the profiler chapter in the library manual."; |
| 346 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 347 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 348 | sys_setcheckinterval(PyObject *self, PyObject *args) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 349 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 350 | PyThreadState *tstate = PyThreadState_Get(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 351 | if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval)) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 352 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 353 | Py_INCREF(Py_None); |
| 354 | return Py_None; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 357 | static char setcheckinterval_doc[] = |
| 358 | "setcheckinterval(n)\n\ |
| 359 | \n\ |
| 360 | Tell the Python interpreter to check for asynchronous events every\n\ |
| 361 | n instructions. This also affects how often thread switches occur."; |
| 362 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 363 | static PyObject * |
| 364 | sys_setrecursionlimit(PyObject *self, PyObject *args) |
| 365 | { |
| 366 | int new_limit; |
| 367 | if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) |
| 368 | return NULL; |
| 369 | if (new_limit <= 0) { |
| 370 | PyErr_SetString(PyExc_ValueError, |
| 371 | "recursion limit must be positive"); |
| 372 | return NULL; |
| 373 | } |
| 374 | Py_SetRecursionLimit(new_limit); |
| 375 | Py_INCREF(Py_None); |
| 376 | return Py_None; |
| 377 | } |
| 378 | |
| 379 | static char setrecursionlimit_doc[] = |
| 380 | "setrecursionlimit(n)\n\ |
| 381 | \n\ |
| 382 | Set the maximum depth of the Python interpreter stack to n. This\n\ |
| 383 | limit prevents infinite recursion from causing an overflow of the C\n\ |
| 384 | stack and crashing Python. The highest possible limit is platform-\n\ |
| 385 | dependent."; |
| 386 | |
| 387 | static PyObject * |
| 388 | sys_getrecursionlimit(PyObject *self, PyObject *args) |
| 389 | { |
| 390 | if (!PyArg_ParseTuple(args, ":getrecursionlimit")) |
| 391 | return NULL; |
| 392 | return PyInt_FromLong(Py_GetRecursionLimit()); |
| 393 | } |
| 394 | |
| 395 | static char getrecursionlimit_doc[] = |
| 396 | "getrecursionlimit()\n\ |
| 397 | \n\ |
| 398 | Return the current value of the recursion limit, the maximum depth\n\ |
| 399 | of the Python interpreter stack. This limit prevents infinite\n\ |
| 400 | recursion from causing an overflow of the C stack and crashing Python."; |
| 401 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 402 | #ifdef HAVE_DLOPEN |
| 403 | static PyObject * |
| 404 | sys_setdlopenflags(PyObject *self, PyObject *args) |
| 405 | { |
| 406 | int new_val; |
| 407 | PyThreadState *tstate = PyThreadState_Get(); |
| 408 | if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) |
| 409 | return NULL; |
| 410 | if (!tstate) |
| 411 | return NULL; |
| 412 | tstate->interp->dlopenflags = new_val; |
| 413 | Py_INCREF(Py_None); |
| 414 | return Py_None; |
| 415 | } |
| 416 | |
| 417 | static char setdlopenflags_doc[] = |
| 418 | "setdlopenflags(n) -> None\n\ |
| 419 | \n\ |
| 420 | Set the flags that will be used for dlopen() calls. Among other\n\ |
| 421 | things, this will enable a lazy resolving of symbols when imporing\n\ |
| 422 | a module, if called as sys.setdlopenflags(0)\n\ |
| 423 | To share symols across extension modules, call as\n\ |
| 424 | sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)"; |
| 425 | |
| 426 | static PyObject * |
| 427 | sys_getdlopenflags(PyObject *self, PyObject *args) |
| 428 | { |
| 429 | PyThreadState *tstate = PyThreadState_Get(); |
| 430 | if (!PyArg_ParseTuple(args, ":getdlopenflags")) |
| 431 | return NULL; |
| 432 | if (!tstate) |
| 433 | return NULL; |
| 434 | return PyInt_FromLong(tstate->interp->dlopenflags); |
| 435 | } |
| 436 | |
| 437 | static char getdlopenflags_doc[] = |
| 438 | "getdlopenflags() -> int\n\ |
| 439 | \n\ |
| 440 | Return the current value of the flags that are used for dlopen()\n\ |
| 441 | calls. The flag constants are defined in the dl module."; |
| 442 | #endif |
| 443 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 444 | #ifdef USE_MALLOPT |
| 445 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 446 | #include <malloc.h> |
| 447 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 448 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 449 | sys_mdebug(PyObject *self, PyObject *args) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 450 | { |
| 451 | int flag; |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 452 | if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 453 | return NULL; |
| 454 | mallopt(M_DEBUG, flag); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 455 | Py_INCREF(Py_None); |
| 456 | return Py_None; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 457 | } |
| 458 | #endif /* USE_MALLOPT */ |
| 459 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 460 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 461 | sys_getrefcount(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 462 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 463 | PyObject *arg; |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 464 | if (!PyArg_ParseTuple(args, "O:getrefcount", &arg)) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 465 | return NULL; |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 466 | return PyInt_FromLong(arg->ob_refcnt); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 469 | #ifdef Py_TRACE_REFS |
| 470 | static PyObject * |
| 471 | sys_gettotalrefcount(PyObject *self, PyObject *args) |
| 472 | { |
| 473 | extern long _Py_RefTotal; |
| 474 | if (!PyArg_ParseTuple(args, ":gettotalrefcount")) |
| 475 | return NULL; |
| 476 | return PyInt_FromLong(_Py_RefTotal); |
| 477 | } |
| 478 | |
| 479 | #endif /* Py_TRACE_REFS */ |
| 480 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 481 | static char getrefcount_doc[] = |
| 482 | "getrefcount(object) -> integer\n\ |
| 483 | \n\ |
| 484 | Return the current reference count for the object. This includes the\n\ |
| 485 | temporary reference in the argument list, so it is at least 2."; |
| 486 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 487 | #ifdef COUNT_ALLOCS |
| 488 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 489 | sys_getcounts(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 490 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 491 | extern PyObject *get_counts(void); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 493 | if (!PyArg_ParseTuple(args, ":getcounts")) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | return get_counts(); |
| 496 | } |
| 497 | #endif |
| 498 | |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 499 | static char getframe_doc[] = |
| 500 | "_getframe([depth]) -> frameobject\n\ |
| 501 | \n\ |
| 502 | Return a frame object from the call stack. If optional integer depth is\n\ |
| 503 | given, return the frame object that many calls below the top of the stack.\n\ |
| 504 | If that is deeper than the call stack, ValueError is raised. The default\n\ |
| 505 | for depth is zero, returning the frame at the top of the call stack.\n\ |
| 506 | \n\ |
| 507 | This function should be used for internal and specialized\n\ |
| 508 | purposes only."; |
| 509 | |
| 510 | static PyObject * |
| 511 | sys_getframe(PyObject *self, PyObject *args) |
| 512 | { |
| 513 | PyFrameObject *f = PyThreadState_Get()->frame; |
| 514 | int depth = -1; |
| 515 | |
| 516 | if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) |
| 517 | return NULL; |
| 518 | |
| 519 | while (depth > 0 && f != NULL) { |
| 520 | f = f->f_back; |
| 521 | --depth; |
| 522 | } |
| 523 | if (f == NULL) { |
| 524 | PyErr_SetString(PyExc_ValueError, |
| 525 | "call stack is not deep enough"); |
| 526 | return NULL; |
| 527 | } |
| 528 | Py_INCREF(f); |
| 529 | return (PyObject*)f; |
| 530 | } |
| 531 | |
| 532 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 533 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 534 | /* Defined in objects.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 535 | extern PyObject *_Py_GetObjects(PyObject *, PyObject *); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 536 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 538 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 539 | /* Defined in ceval.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 540 | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 541 | #endif |
| 542 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 543 | static PyMethodDef sys_methods[] = { |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 544 | /* Might as well keep this in alphabetic order */ |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 545 | {"displayhook", sys_displayhook, 1, displayhook_doc}, |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 546 | {"exc_info", sys_exc_info, 1, exc_info_doc}, |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 547 | {"excepthook", sys_excepthook, 1, excepthook_doc}, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 548 | {"exit", sys_exit, 0, exit_doc}, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 549 | {"getdefaultencoding", sys_getdefaultencoding, 1, |
| 550 | getdefaultencoding_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 551 | #ifdef HAVE_DLOPEN |
| 552 | {"getdlopenflags", sys_getdlopenflags, 1, |
| 553 | getdlopenflags_doc}, |
| 554 | #endif |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 555 | #ifdef COUNT_ALLOCS |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 556 | {"getcounts", sys_getcounts, 1}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 557 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 558 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 559 | {"getdxp", _Py_GetDXProfile, 1}, |
| 560 | #endif |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 561 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 562 | {"getobjects", _Py_GetObjects, 1}, |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 563 | {"gettotalrefcount", sys_gettotalrefcount, 1}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 564 | #endif |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 565 | {"getrefcount", sys_getrefcount, 1, getrefcount_doc}, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 566 | {"getrecursionlimit", sys_getrecursionlimit, 1, |
| 567 | getrecursionlimit_doc}, |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 568 | {"_getframe", sys_getframe, 1, getframe_doc}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 569 | #ifdef USE_MALLOPT |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 570 | {"mdebug", sys_mdebug, 1}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 571 | #endif |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 572 | {"setdefaultencoding", sys_setdefaultencoding, 1, |
| 573 | setdefaultencoding_doc}, |
| 574 | {"setcheckinterval", sys_setcheckinterval, 1, |
| 575 | setcheckinterval_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 576 | #ifdef HAVE_DLOPEN |
| 577 | {"setdlopenflags", sys_setdlopenflags, 1, |
| 578 | setdlopenflags_doc}, |
| 579 | #endif |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 580 | {"setprofile", sys_setprofile, 0, setprofile_doc}, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 581 | {"setrecursionlimit", sys_setrecursionlimit, 1, |
| 582 | setrecursionlimit_doc}, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 583 | {"settrace", sys_settrace, 0, settrace_doc}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 584 | {NULL, NULL} /* sentinel */ |
| 585 | }; |
| 586 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 587 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 588 | list_builtin_module_names(void) |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 589 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 590 | PyObject *list = PyList_New(0); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 591 | int i; |
| 592 | if (list == NULL) |
| 593 | return NULL; |
Guido van Rossum | 25c649f | 1997-11-04 17:04:34 +0000 | [diff] [blame] | 594 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 595 | PyObject *name = PyString_FromString( |
Guido van Rossum | 25c649f | 1997-11-04 17:04:34 +0000 | [diff] [blame] | 596 | PyImport_Inittab[i].name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 597 | if (name == NULL) |
| 598 | break; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 599 | PyList_Append(list, name); |
| 600 | Py_DECREF(name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 601 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 602 | if (PyList_Sort(list) != 0) { |
| 603 | Py_DECREF(list); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 604 | list = NULL; |
| 605 | } |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 606 | if (list) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 607 | PyObject *v = PyList_AsTuple(list); |
| 608 | Py_DECREF(list); |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 609 | list = v; |
| 610 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 611 | return list; |
| 612 | } |
| 613 | |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 614 | static PyObject *warnoptions = NULL; |
| 615 | |
| 616 | void |
| 617 | PySys_ResetWarnOptions(void) |
| 618 | { |
| 619 | if (warnoptions == NULL || !PyList_Check(warnoptions)) |
| 620 | return; |
| 621 | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | PySys_AddWarnOption(char *s) |
| 626 | { |
| 627 | PyObject *str; |
| 628 | |
| 629 | if (warnoptions == NULL || !PyList_Check(warnoptions)) { |
| 630 | Py_XDECREF(warnoptions); |
| 631 | warnoptions = PyList_New(0); |
| 632 | if (warnoptions == NULL) |
| 633 | return; |
| 634 | } |
| 635 | str = PyString_FromString(s); |
| 636 | if (str != NULL) { |
| 637 | PyList_Append(warnoptions, str); |
| 638 | Py_DECREF(str); |
| 639 | } |
| 640 | } |
| 641 | |
Guido van Rossum | 40552d0 | 1998-08-06 03:34:39 +0000 | [diff] [blame] | 642 | /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
| 643 | Two literals concatenated works just fine. If you have a K&R compiler |
| 644 | or other abomination that however *does* understand longer strings, |
| 645 | get rid of the !!! comment in the middle and the quotes that surround it. */ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 646 | static char sys_doc[] = |
| 647 | "This module provides access to some objects used or maintained by the\n\ |
| 648 | interpreter and to functions that interact strongly with the interpreter.\n\ |
| 649 | \n\ |
| 650 | Dynamic objects:\n\ |
| 651 | \n\ |
| 652 | argv -- command line arguments; argv[0] is the script pathname if known\n\ |
| 653 | path -- module search path; path[0] is the script directory, else ''\n\ |
| 654 | modules -- dictionary of loaded modules\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 655 | \n\ |
| 656 | displayhook -- called to show results in an interactive session\n\ |
| 657 | excepthook -- called to handle any uncaught exception other than SystemExit\n\ |
| 658 | To customize printing in an interactive session or to install a custom\n\ |
| 659 | top-level exception handler, assign other functions to replace these.\n\ |
| 660 | \n\ |
| 661 | exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\ |
| 662 | Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 663 | \n\ |
| 664 | stdin -- standard input file object; used by raw_input() and input()\n\ |
| 665 | stdout -- standard output file object; used by the print statement\n\ |
| 666 | stderr -- standard error object; used for error messages\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 667 | By assigning other file objects (or objects that behave like files)\n\ |
| 668 | to these, it is possible to redirect all of the interpreter's I/O.\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 669 | \n\ |
| 670 | last_type -- type of last uncaught exception\n\ |
| 671 | last_value -- value of last uncaught exception\n\ |
| 672 | last_traceback -- traceback of last uncaught exception\n\ |
| 673 | These three are only available in an interactive session after a\n\ |
| 674 | traceback has been printed.\n\ |
| 675 | \n\ |
| 676 | exc_type -- type of exception currently being handled\n\ |
| 677 | exc_value -- value of exception currently being handled\n\ |
| 678 | exc_traceback -- traceback of exception currently being handled\n\ |
| 679 | The function exc_info() should be used instead of these three,\n\ |
| 680 | because it is thread-safe.\n\ |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 681 | " |
| 682 | #ifndef MS_WIN16 |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 683 | /* concatenating string here */ |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 684 | "\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 685 | Static objects:\n\ |
| 686 | \n\ |
| 687 | maxint -- the largest supported integer (the smallest is -maxint-1)\n\ |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 688 | maxunicode -- the largest supported character\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 689 | builtin_module_names -- tuple of module names built into this intepreter\n\ |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 690 | version -- the version of this interpreter as a string\n\ |
| 691 | version_info -- version information as a tuple\n\ |
| 692 | hexversion -- version information encoded as a single integer\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 693 | copyright -- copyright notice pertaining to this interpreter\n\ |
| 694 | platform -- platform identifier\n\ |
| 695 | executable -- pathname of this Python interpreter\n\ |
| 696 | prefix -- prefix used to find the Python library\n\ |
| 697 | exec_prefix -- prefix used to find the machine-specific Python library\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 698 | " |
| 699 | #ifdef MS_WINDOWS |
| 700 | /* concatenating string here */ |
| 701 | "dllhandle -- [Windows only] integer handle of the Python DLL\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 702 | winver -- [Windows only] version number of the Python DLL\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 703 | " |
| 704 | #endif /* MS_WINDOWS */ |
| 705 | "__stdin__ -- the original stdin; don't touch!\n\ |
| 706 | __stdout__ -- the original stdout; don't touch!\n\ |
| 707 | __stderr__ -- the original stderr; don't touch!\n\ |
| 708 | __displayhook__ -- the original displayhook; don't touch!\n\ |
| 709 | __excepthook__ -- the original excepthook; don't touch!\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 710 | \n\ |
| 711 | Functions:\n\ |
| 712 | \n\ |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 713 | displayhook() -- print an object to the screen, and save it in __builtin__._\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 714 | excepthook() -- print an exception and its traceback to sys.stderr\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 715 | exc_info() -- return thread-safe information about the current exception\n\ |
| 716 | exit() -- exit the interpreter by raising SystemExit\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 717 | getdlopenflags() -- returns flags to be used for dlopen() calls\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 718 | getrefcount() -- return the reference count for an object (plus one :-)\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 719 | getrecursionlimit() -- return the max recursion depth for the interpreter\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 720 | setcheckinterval() -- control how often the interpreter checks for events\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 721 | setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 722 | setprofile() -- set the global profiling function\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 723 | setrecursionlimit() -- set the max recursion depth for the interpreter\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 724 | settrace() -- set the global debug tracing function\n\ |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 725 | " |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 726 | #endif /* MS_WIN16 */ |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 727 | /* end of sys_doc */ ; |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 729 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 730 | _PySys_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 731 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 732 | PyObject *m, *v, *sysdict; |
| 733 | PyObject *sysin, *sysout, *syserr; |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 734 | char *s; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 735 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 736 | m = Py_InitModule3("sys", sys_methods, sys_doc); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 737 | sysdict = PyModule_GetDict(m); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 738 | |
| 739 | sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL); |
| 740 | sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL); |
| 741 | syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 742 | if (PyErr_Occurred()) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 743 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 744 | PyDict_SetItemString(sysdict, "stdin", sysin); |
| 745 | PyDict_SetItemString(sysdict, "stdout", sysout); |
| 746 | PyDict_SetItemString(sysdict, "stderr", syserr); |
Guido van Rossum | bd36dba | 1998-02-19 20:53:06 +0000 | [diff] [blame] | 747 | /* Make backup copies for cleanup */ |
| 748 | PyDict_SetItemString(sysdict, "__stdin__", sysin); |
| 749 | PyDict_SetItemString(sysdict, "__stdout__", sysout); |
| 750 | PyDict_SetItemString(sysdict, "__stderr__", syserr); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 751 | PyDict_SetItemString(sysdict, "__displayhook__", |
| 752 | PyDict_GetItemString(sysdict, "displayhook")); |
| 753 | PyDict_SetItemString(sysdict, "__excepthook__", |
| 754 | PyDict_GetItemString(sysdict, "excepthook")); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 755 | Py_XDECREF(sysin); |
| 756 | Py_XDECREF(sysout); |
| 757 | Py_XDECREF(syserr); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 758 | PyDict_SetItemString(sysdict, "version", |
| 759 | v = PyString_FromString(Py_GetVersion())); |
Barry Warsaw | 54892c4 | 1999-01-27 16:33:19 +0000 | [diff] [blame] | 760 | Py_XDECREF(v); |
Guido van Rossum | e0d7dae | 1999-01-03 12:55:39 +0000 | [diff] [blame] | 761 | PyDict_SetItemString(sysdict, "hexversion", |
| 762 | v = PyInt_FromLong(PY_VERSION_HEX)); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 763 | Py_XDECREF(v); |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 764 | /* |
| 765 | * These release level checks are mutually exclusive and cover |
| 766 | * the field, so don't get too fancy with the pre-processor! |
| 767 | */ |
| 768 | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 769 | s = "alpha"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 770 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 771 | s = "beta"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 772 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 773 | s = "candidate"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 774 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 775 | s = "final"; |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 776 | #endif |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 777 | PyDict_SetItemString(sysdict, "version_info", |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 778 | v = Py_BuildValue("iiisi", PY_MAJOR_VERSION, |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 779 | PY_MINOR_VERSION, |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 780 | PY_MICRO_VERSION, s, |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 781 | PY_RELEASE_SERIAL)); |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 782 | Py_XDECREF(v); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 783 | PyDict_SetItemString(sysdict, "copyright", |
| 784 | v = PyString_FromString(Py_GetCopyright())); |
| 785 | Py_XDECREF(v); |
| 786 | PyDict_SetItemString(sysdict, "platform", |
| 787 | v = PyString_FromString(Py_GetPlatform())); |
| 788 | Py_XDECREF(v); |
Guido van Rossum | b2c8ec4 | 1997-05-22 20:41:20 +0000 | [diff] [blame] | 789 | PyDict_SetItemString(sysdict, "executable", |
| 790 | v = PyString_FromString(Py_GetProgramFullPath())); |
| 791 | Py_XDECREF(v); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 792 | PyDict_SetItemString(sysdict, "prefix", |
| 793 | v = PyString_FromString(Py_GetPrefix())); |
| 794 | Py_XDECREF(v); |
| 795 | PyDict_SetItemString(sysdict, "exec_prefix", |
| 796 | v = PyString_FromString(Py_GetExecPrefix())); |
| 797 | Py_XDECREF(v); |
| 798 | PyDict_SetItemString(sysdict, "maxint", |
| 799 | v = PyInt_FromLong(PyInt_GetMax())); |
| 800 | Py_XDECREF(v); |
Martin v. Löwis | ce9b5a5 | 2001-06-27 06:28:56 +0000 | [diff] [blame] | 801 | PyDict_SetItemString(sysdict, "maxunicode", |
| 802 | v = PyInt_FromLong(PyUnicode_GetMax())); |
| 803 | Py_XDECREF(v); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 804 | PyDict_SetItemString(sysdict, "builtin_module_names", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 805 | v = list_builtin_module_names()); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 806 | Py_XDECREF(v); |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 807 | { |
| 808 | /* Assumes that longs are at least 2 bytes long. |
| 809 | Should be safe! */ |
| 810 | unsigned long number = 1; |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 811 | char *value; |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 812 | |
| 813 | s = (char *) &number; |
| 814 | if (s[0] == 0) |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 815 | value = "big"; |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 816 | else |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 817 | value = "little"; |
| 818 | PyDict_SetItemString(sysdict, "byteorder", |
Barry Warsaw | f2581c9 | 2000-08-16 23:03:57 +0000 | [diff] [blame] | 819 | v = PyString_FromString(value)); |
| 820 | Py_XDECREF(v); |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 821 | } |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 822 | #ifdef MS_COREDLL |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 823 | PyDict_SetItemString(sysdict, "dllhandle", |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 824 | v = PyLong_FromVoidPtr(PyWin_DLLhModule)); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 825 | Py_XDECREF(v); |
| 826 | PyDict_SetItemString(sysdict, "winver", |
Guido van Rossum | 6c1e5f2 | 1997-09-29 23:34:23 +0000 | [diff] [blame] | 827 | v = PyString_FromString(PyWin_DLLVersionString)); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 828 | Py_XDECREF(v); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 829 | #endif |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 830 | if (warnoptions == NULL) { |
| 831 | warnoptions = PyList_New(0); |
| 832 | } |
| 833 | else { |
| 834 | Py_INCREF(warnoptions); |
| 835 | } |
| 836 | if (warnoptions != NULL) { |
Guido van Rossum | 03df3b3 | 2001-01-13 22:06:05 +0000 | [diff] [blame] | 837 | PyDict_SetItemString(sysdict, "warnoptions", warnoptions); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 840 | if (PyErr_Occurred()) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 841 | return NULL; |
| 842 | return m; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 845 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 846 | makepathobject(char *path, int delim) |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 847 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 848 | int i, n; |
| 849 | char *p; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 850 | PyObject *v, *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 851 | |
| 852 | n = 1; |
| 853 | p = path; |
| 854 | while ((p = strchr(p, delim)) != NULL) { |
| 855 | n++; |
| 856 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 857 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 858 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 859 | if (v == NULL) |
| 860 | return NULL; |
| 861 | for (i = 0; ; i++) { |
| 862 | p = strchr(path, delim); |
| 863 | if (p == NULL) |
| 864 | p = strchr(path, '\0'); /* End of string */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 865 | w = PyString_FromStringAndSize(path, (int) (p - path)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 866 | if (w == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 867 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 868 | return NULL; |
| 869 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 870 | PyList_SetItem(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 871 | if (*p == '\0') |
| 872 | break; |
| 873 | path = p+1; |
| 874 | } |
| 875 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 879 | PySys_SetPath(char *path) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 880 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 881 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 882 | if ((v = makepathobject(path, DELIM)) == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 883 | Py_FatalError("can't create sys.path"); |
| 884 | if (PySys_SetObject("path", v) != 0) |
| 885 | Py_FatalError("can't assign sys.path"); |
| 886 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 889 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 890 | makeargvobject(int argc, char **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 891 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 892 | PyObject *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 893 | if (argc <= 0 || argv == NULL) { |
| 894 | /* Ensure at least one (empty) argument is seen */ |
| 895 | static char *empty_argv[1] = {""}; |
| 896 | argv = empty_argv; |
| 897 | argc = 1; |
| 898 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 899 | av = PyList_New(argc); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 900 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 901 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 902 | for (i = 0; i < argc; i++) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 903 | PyObject *v = PyString_FromString(argv[i]); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 904 | if (v == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 905 | Py_DECREF(av); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 906 | av = NULL; |
| 907 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 908 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 909 | PyList_SetItem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 910 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 911 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 912 | return av; |
| 913 | } |
| 914 | |
| 915 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 916 | PySys_SetArgv(int argc, char **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 917 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 918 | PyObject *av = makeargvobject(argc, argv); |
| 919 | PyObject *path = PySys_GetObject("path"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 920 | if (av == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 921 | Py_FatalError("no mem for sys.argv"); |
| 922 | if (PySys_SetObject("argv", av) != 0) |
| 923 | Py_FatalError("can't assign sys.argv"); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 924 | if (path != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 925 | char *argv0 = argv[0]; |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 926 | char *p = NULL; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 927 | int n = 0; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 928 | PyObject *a; |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 929 | #ifdef HAVE_READLINK |
| 930 | char link[MAXPATHLEN+1]; |
| 931 | char argv0copy[2*MAXPATHLEN+1]; |
| 932 | int nr = 0; |
| 933 | if (argc > 0 && argv0 != NULL) |
| 934 | nr = readlink(argv0, link, MAXPATHLEN); |
| 935 | if (nr > 0) { |
| 936 | /* It's a symlink */ |
| 937 | link[nr] = '\0'; |
| 938 | if (link[0] == SEP) |
| 939 | argv0 = link; /* Link to absolute path */ |
| 940 | else if (strchr(link, SEP) == NULL) |
| 941 | ; /* Link without path */ |
| 942 | else { |
| 943 | /* Must join(dirname(argv0), link) */ |
| 944 | char *q = strrchr(argv0, SEP); |
| 945 | if (q == NULL) |
| 946 | argv0 = link; /* argv0 without path */ |
| 947 | else { |
| 948 | /* Must make a copy */ |
| 949 | strcpy(argv0copy, argv0); |
| 950 | q = strrchr(argv0copy, SEP); |
| 951 | strcpy(q+1, link); |
| 952 | argv0 = argv0copy; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | #endif /* HAVE_READLINK */ |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 957 | #if SEP == '\\' /* Special case for MS filename syntax */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 958 | if (argc > 0 && argv0 != NULL) { |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 959 | char *q; |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 960 | p = strrchr(argv0, SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 961 | /* Test for alternate separator */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 962 | q = strrchr(p ? p : argv0, '/'); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 963 | if (q != NULL) |
| 964 | p = q; |
| 965 | if (p != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 966 | n = p + 1 - argv0; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 967 | if (n > 1 && p[-1] != ':') |
| 968 | n--; /* Drop trailing separator */ |
| 969 | } |
| 970 | } |
| 971 | #else /* All other filename syntaxes */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 972 | if (argc > 0 && argv0 != NULL) |
| 973 | p = strrchr(argv0, SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 974 | if (p != NULL) { |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 975 | #ifndef RISCOS |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 976 | n = p + 1 - argv0; |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 977 | #else /* don't include trailing separator */ |
| 978 | n = p - argv0; |
| 979 | #endif /* RISCOS */ |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 980 | #if SEP == '/' /* Special case for Unix filename syntax */ |
| 981 | if (n > 1) |
| 982 | n--; /* Drop trailing separator */ |
| 983 | #endif /* Unix */ |
| 984 | } |
| 985 | #endif /* All others */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 986 | a = PyString_FromStringAndSize(argv0, n); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 987 | if (a == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 988 | Py_FatalError("no mem for sys.path insertion"); |
| 989 | if (PyList_Insert(path, 0, a) < 0) |
| 990 | Py_FatalError("sys.path.insert(0) failed"); |
| 991 | Py_DECREF(a); |
Guido van Rossum | a63d9f4 | 1996-07-24 01:31:37 +0000 | [diff] [blame] | 992 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 993 | Py_DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 994 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 995 | |
| 996 | |
| 997 | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
| 998 | Adapted from code submitted by Just van Rossum. |
| 999 | |
| 1000 | PySys_WriteStdout(format, ...) |
| 1001 | PySys_WriteStderr(format, ...) |
| 1002 | |
| 1003 | The first function writes to sys.stdout; the second to sys.stderr. When |
| 1004 | there is a problem, they write to the real (C level) stdout or stderr; |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1005 | no exceptions are raised. |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1006 | |
| 1007 | Both take a printf-style format string as their first argument followed |
| 1008 | by a variable length argument list determined by the format string. |
| 1009 | |
| 1010 | *** WARNING *** |
| 1011 | |
| 1012 | The format should limit the total size of the formatted output string to |
| 1013 | 1000 bytes. In particular, this means that no unrestricted "%s" formats |
| 1014 | should occur; these should be limited using "%.<N>s where <N> is a |
| 1015 | decimal number calculated so that <N> plus the maximum size of other |
| 1016 | formatted text does not exceed 1000 bytes. Also watch out for "%f", |
| 1017 | which can print hundreds of digits for very large numbers. |
| 1018 | |
| 1019 | */ |
| 1020 | |
| 1021 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1022 | mywrite(char *name, FILE *fp, const char *format, va_list va) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1023 | { |
| 1024 | PyObject *file; |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1025 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1026 | |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1027 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1028 | file = PySys_GetObject(name); |
| 1029 | if (file == NULL || PyFile_AsFile(file) == fp) |
| 1030 | vfprintf(fp, format, va); |
| 1031 | else { |
| 1032 | char buffer[1001]; |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1033 | if (vsprintf(buffer, format, va) >= sizeof(buffer)) |
| 1034 | Py_FatalError("PySys_WriteStdout/err: buffer overrun"); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1035 | if (PyFile_WriteString(buffer, file) != 0) { |
| 1036 | PyErr_Clear(); |
| 1037 | fputs(buffer, fp); |
| 1038 | } |
| 1039 | } |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1040 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1044 | PySys_WriteStdout(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1045 | { |
| 1046 | va_list va; |
| 1047 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1048 | va_start(va, format); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1049 | mywrite("stdout", stdout, format, va); |
| 1050 | va_end(va); |
| 1051 | } |
| 1052 | |
| 1053 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1054 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1055 | { |
| 1056 | va_list va; |
| 1057 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1058 | va_start(va, format); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1059 | mywrite("stderr", stderr, format, va); |
| 1060 | va_end(va); |
| 1061 | } |