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