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