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