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