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