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