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