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" |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 18 | #include "structseq.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 19 | #include "code.h" |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 20 | #include "frameobject.h" |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 21 | #include "eval.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 23 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 24 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 25 | #ifdef MS_WINDOWS |
| 26 | #define WIN32_LEAN_AND_MEAN |
| 27 | #include "windows.h" |
| 28 | #endif /* MS_WINDOWS */ |
| 29 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 30 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 31 | extern void *PyWin_DLLhModule; |
Guido van Rossum | 6c1e5f2 | 1997-09-29 23:34:23 +0000 | [diff] [blame] | 32 | /* A string loaded from the DLL at startup: */ |
| 33 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 36 | #ifdef __VMS |
| 37 | #include <unixlib.h> |
| 38 | #endif |
| 39 | |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 40 | #ifdef MS_WINDOWS |
| 41 | #include <windows.h> |
| 42 | #endif |
| 43 | |
| 44 | #ifdef HAVE_LANGINFO_H |
| 45 | #include <locale.h> |
| 46 | #include <langinfo.h> |
| 47 | #endif |
| 48 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 49 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 50 | PySys_GetObject(char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 52 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 53 | PyObject *sd = tstate->interp->sysdict; |
Guido van Rossum | be20336 | 1999-10-05 22:17:41 +0000 | [diff] [blame] | 54 | if (sd == NULL) |
| 55 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 56 | return PyDict_GetItemString(sd, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | FILE * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 60 | PySys_GetFile(char *name, FILE *def) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | { |
| 62 | FILE *fp = NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 63 | PyObject *v = PySys_GetObject(name); |
| 64 | if (v != NULL && PyFile_Check(v)) |
| 65 | fp = PyFile_AsFile(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | if (fp == NULL) |
| 67 | fp = def; |
| 68 | return fp; |
| 69 | } |
| 70 | |
| 71 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 72 | PySys_SetObject(char *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 74 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 75 | PyObject *sd = tstate->interp->sysdict; |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 76 | if (v == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 77 | if (PyDict_GetItemString(sd, name) == NULL) |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | else |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 80 | return PyDict_DelItemString(sd, name); |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 81 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 82 | else |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 83 | return PyDict_SetItemString(sd, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 86 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 87 | sys_displayhook(PyObject *self, PyObject *o) |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 88 | { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 89 | PyObject *outf; |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 90 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 91 | PyObject *modules = interp->modules; |
| 92 | PyObject *builtins = PyDict_GetItemString(modules, "__builtin__"); |
| 93 | |
Moshe Zadka | 03897ea | 2001-07-23 13:32:43 +0000 | [diff] [blame] | 94 | if (builtins == NULL) { |
| 95 | PyErr_SetString(PyExc_RuntimeError, "lost __builtin__"); |
| 96 | return NULL; |
| 97 | } |
| 98 | |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 99 | /* Print value except if None */ |
| 100 | /* After printing, also assign to '_' */ |
| 101 | /* Before, set '_' to None to avoid recursion */ |
| 102 | if (o == Py_None) { |
| 103 | Py_INCREF(Py_None); |
| 104 | return Py_None; |
| 105 | } |
| 106 | if (PyObject_SetAttrString(builtins, "_", Py_None) != 0) |
| 107 | return NULL; |
| 108 | if (Py_FlushLine() != 0) |
| 109 | return NULL; |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 110 | outf = PySys_GetObject("stdout"); |
| 111 | if (outf == NULL) { |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 112 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
| 113 | return NULL; |
| 114 | } |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 115 | if (PyFile_WriteObject(o, outf, 0) != 0) |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 116 | return NULL; |
Greg Stein | ceb9b7c | 2001-01-11 09:27:34 +0000 | [diff] [blame] | 117 | PyFile_SoftSpace(outf, 1); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 118 | if (Py_FlushLine() != 0) |
| 119 | return NULL; |
| 120 | if (PyObject_SetAttrString(builtins, "_", o) != 0) |
| 121 | return NULL; |
| 122 | Py_INCREF(Py_None); |
| 123 | return Py_None; |
| 124 | } |
| 125 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 126 | PyDoc_STRVAR(displayhook_doc, |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 127 | "displayhook(object) -> None\n" |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 128 | "\n" |
Brett Cannon | 5118517 | 2006-03-02 22:07:40 +0000 | [diff] [blame] | 129 | "Print an object to sys.stdout and also save it in __builtin__.\n" |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 130 | ); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 131 | |
| 132 | static PyObject * |
| 133 | sys_excepthook(PyObject* self, PyObject* args) |
| 134 | { |
| 135 | PyObject *exc, *value, *tb; |
Fred Drake | a768882 | 2001-10-24 20:47:48 +0000 | [diff] [blame] | 136 | if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | PyErr_Display(exc, value, tb); |
| 139 | Py_INCREF(Py_None); |
| 140 | return Py_None; |
| 141 | } |
| 142 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 143 | PyDoc_STRVAR(excepthook_doc, |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 144 | "excepthook(exctype, value, traceback) -> None\n" |
| 145 | "\n" |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 146 | "Handle an exception by displaying it with a traceback on sys.stderr.\n" |
| 147 | ); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 148 | |
| 149 | static PyObject * |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 150 | sys_exc_info(PyObject *self, PyObject *noargs) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 151 | { |
| 152 | PyThreadState *tstate; |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 153 | tstate = PyThreadState_GET(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 154 | return Py_BuildValue( |
| 155 | "(OOO)", |
| 156 | tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
| 157 | tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
| 158 | tstate->exc_traceback != NULL ? |
| 159 | tstate->exc_traceback : Py_None); |
| 160 | } |
| 161 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 162 | PyDoc_STRVAR(exc_info_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 163 | "exc_info() -> (type, value, traceback)\n\ |
| 164 | \n\ |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 165 | Return information about the most recent exception caught by an except\n\ |
| 166 | clause in the current stack frame or in an older stack frame." |
| 167 | ); |
| 168 | |
| 169 | static PyObject * |
| 170 | sys_exc_clear(PyObject *self, PyObject *noargs) |
| 171 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 172 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 173 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
| 174 | tmp_type = tstate->exc_type; |
| 175 | tmp_value = tstate->exc_value; |
| 176 | tmp_tb = tstate->exc_traceback; |
| 177 | tstate->exc_type = NULL; |
| 178 | tstate->exc_value = NULL; |
| 179 | tstate->exc_traceback = NULL; |
| 180 | Py_XDECREF(tmp_type); |
| 181 | Py_XDECREF(tmp_value); |
| 182 | Py_XDECREF(tmp_tb); |
| 183 | /* For b/w compatibility */ |
| 184 | PySys_SetObject("exc_type", Py_None); |
| 185 | PySys_SetObject("exc_value", Py_None); |
| 186 | PySys_SetObject("exc_traceback", Py_None); |
| 187 | Py_INCREF(Py_None); |
| 188 | return Py_None; |
| 189 | } |
| 190 | |
| 191 | PyDoc_STRVAR(exc_clear_doc, |
| 192 | "exc_clear() -> None\n\ |
| 193 | \n\ |
| 194 | Clear global information on the current exception. Subsequent calls to\n\ |
| 195 | exc_info() will return (None,None,None) until another exception is raised\n\ |
| 196 | in the current thread or the execution stack returns to a frame where\n\ |
| 197 | another exception is being handled." |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 198 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 200 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 201 | sys_exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | { |
Neal Norwitz | 0c766a0 | 2002-03-27 13:03:09 +0000 | [diff] [blame] | 203 | PyObject *exit_code = 0; |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 204 | if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) |
Neal Norwitz | 0c766a0 | 2002-03-27 13:03:09 +0000 | [diff] [blame] | 205 | return NULL; |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 206 | /* Raise SystemExit so callers may catch it or clean up. */ |
Neal Norwitz | 0c766a0 | 2002-03-27 13:03:09 +0000 | [diff] [blame] | 207 | PyErr_SetObject(PyExc_SystemExit, exit_code); |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 208 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 211 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 212 | "exit([status])\n\ |
| 213 | \n\ |
| 214 | Exit the interpreter by raising SystemExit(status).\n\ |
| 215 | 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] | 216 | 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] | 217 | 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] | 218 | exit status will be one (i.e., failure)." |
| 219 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 220 | |
Martin v. Löwis | 107b7da | 2001-11-09 20:59:39 +0000 | [diff] [blame] | 221 | #ifdef Py_USING_UNICODE |
| 222 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 223 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 224 | sys_getdefaultencoding(PyObject *self) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 225 | { |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 226 | return PyString_FromString(PyUnicode_GetDefaultEncoding()); |
| 227 | } |
| 228 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 229 | PyDoc_STRVAR(getdefaultencoding_doc, |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 230 | "getdefaultencoding() -> string\n\ |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 231 | \n\ |
| 232 | 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] | 233 | implementation." |
| 234 | ); |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 235 | |
| 236 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 237 | sys_setdefaultencoding(PyObject *self, PyObject *args) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 238 | { |
| 239 | char *encoding; |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 240 | if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding)) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 241 | return NULL; |
| 242 | if (PyUnicode_SetDefaultEncoding(encoding)) |
| 243 | return NULL; |
| 244 | Py_INCREF(Py_None); |
| 245 | return Py_None; |
| 246 | } |
| 247 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 248 | PyDoc_STRVAR(setdefaultencoding_doc, |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 249 | "setdefaultencoding(encoding)\n\ |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 250 | \n\ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 251 | Set the current default string encoding used by the Unicode implementation." |
| 252 | ); |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 253 | |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 254 | static PyObject * |
| 255 | sys_getfilesystemencoding(PyObject *self) |
| 256 | { |
| 257 | if (Py_FileSystemDefaultEncoding) |
| 258 | return PyString_FromString(Py_FileSystemDefaultEncoding); |
| 259 | Py_INCREF(Py_None); |
| 260 | return Py_None; |
| 261 | } |
| 262 | |
| 263 | PyDoc_STRVAR(getfilesystemencoding_doc, |
| 264 | "getfilesystemencoding() -> string\n\ |
| 265 | \n\ |
| 266 | Return the encoding used to convert Unicode filenames in\n\ |
| 267 | operating system filenames." |
| 268 | ); |
| 269 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 270 | #endif |
| 271 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Cached interned string objects used for calling the profile and |
| 274 | * trace functions. Initialized by trace_init(). |
| 275 | */ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 276 | static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 277 | |
| 278 | static int |
| 279 | trace_init(void) |
| 280 | { |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 281 | static char *whatnames[7] = {"call", "exception", "line", "return", |
| 282 | "c_call", "c_exception", "c_return"}; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 283 | PyObject *name; |
| 284 | int i; |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 285 | for (i = 0; i < 7; ++i) { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 286 | if (whatstrings[i] == NULL) { |
| 287 | name = PyString_InternFromString(whatnames[i]); |
| 288 | if (name == NULL) |
| 289 | return -1; |
| 290 | whatstrings[i] = name; |
| 291 | } |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static PyObject * |
| 298 | call_trampoline(PyThreadState *tstate, PyObject* callback, |
| 299 | PyFrameObject *frame, int what, PyObject *arg) |
| 300 | { |
| 301 | PyObject *args = PyTuple_New(3); |
| 302 | PyObject *whatstr; |
| 303 | PyObject *result; |
| 304 | |
| 305 | if (args == NULL) |
| 306 | return NULL; |
| 307 | Py_INCREF(frame); |
| 308 | whatstr = whatstrings[what]; |
| 309 | Py_INCREF(whatstr); |
| 310 | if (arg == NULL) |
| 311 | arg = Py_None; |
| 312 | Py_INCREF(arg); |
| 313 | PyTuple_SET_ITEM(args, 0, (PyObject *)frame); |
| 314 | PyTuple_SET_ITEM(args, 1, whatstr); |
| 315 | PyTuple_SET_ITEM(args, 2, arg); |
| 316 | |
| 317 | /* call the Python-level function */ |
| 318 | PyFrame_FastToLocals(frame); |
| 319 | result = PyEval_CallObject(callback, args); |
| 320 | PyFrame_LocalsToFast(frame, 1); |
| 321 | if (result == NULL) |
| 322 | PyTraceBack_Here(frame); |
| 323 | |
| 324 | /* cleanup */ |
| 325 | Py_DECREF(args); |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | static int |
| 330 | profile_trampoline(PyObject *self, PyFrameObject *frame, |
| 331 | int what, PyObject *arg) |
| 332 | { |
| 333 | PyThreadState *tstate = frame->f_tstate; |
| 334 | PyObject *result; |
| 335 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 336 | if (arg == NULL) |
| 337 | arg = Py_None; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 338 | result = call_trampoline(tstate, self, frame, what, arg); |
| 339 | if (result == NULL) { |
| 340 | PyEval_SetProfile(NULL, NULL); |
| 341 | return -1; |
| 342 | } |
| 343 | Py_DECREF(result); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int |
| 348 | trace_trampoline(PyObject *self, PyFrameObject *frame, |
| 349 | int what, PyObject *arg) |
| 350 | { |
| 351 | PyThreadState *tstate = frame->f_tstate; |
| 352 | PyObject *callback; |
| 353 | PyObject *result; |
| 354 | |
| 355 | if (what == PyTrace_CALL) |
| 356 | callback = self; |
| 357 | else |
| 358 | callback = frame->f_trace; |
| 359 | if (callback == NULL) |
| 360 | return 0; |
| 361 | result = call_trampoline(tstate, callback, frame, what, arg); |
| 362 | if (result == NULL) { |
| 363 | PyEval_SetTrace(NULL, NULL); |
| 364 | Py_XDECREF(frame->f_trace); |
| 365 | frame->f_trace = NULL; |
| 366 | return -1; |
| 367 | } |
| 368 | if (result != Py_None) { |
| 369 | PyObject *temp = frame->f_trace; |
| 370 | frame->f_trace = NULL; |
| 371 | Py_XDECREF(temp); |
| 372 | frame->f_trace = result; |
| 373 | } |
| 374 | else { |
| 375 | Py_DECREF(result); |
| 376 | } |
| 377 | return 0; |
| 378 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 379 | |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 380 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 381 | sys_settrace(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 382 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 383 | if (trace_init() == -1) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 384 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 385 | if (args == Py_None) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 386 | PyEval_SetTrace(NULL, NULL); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 387 | else |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 388 | PyEval_SetTrace(trace_trampoline, args); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 389 | Py_INCREF(Py_None); |
| 390 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 393 | PyDoc_STRVAR(settrace_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 394 | "settrace(function)\n\ |
| 395 | \n\ |
| 396 | 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] | 397 | function call. See the debugger chapter in the library manual." |
| 398 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 400 | static PyObject * |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 401 | sys_gettrace(PyObject *self, PyObject *args) |
| 402 | { |
| 403 | PyThreadState *tstate = PyThreadState_GET(); |
| 404 | PyObject *temp = tstate->c_traceobj; |
| 405 | |
| 406 | if (temp == NULL) |
| 407 | temp = Py_None; |
| 408 | Py_INCREF(temp); |
| 409 | return temp; |
| 410 | } |
| 411 | |
| 412 | PyDoc_STRVAR(gettrace_doc, |
| 413 | "gettrace()\n\ |
| 414 | \n\ |
| 415 | Return the global debug tracing function set with sys.settrace.\n\ |
| 416 | See the debugger chapter in the library manual." |
| 417 | ); |
| 418 | |
| 419 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 420 | sys_setprofile(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 421 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 422 | if (trace_init() == -1) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 423 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 424 | if (args == Py_None) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 425 | PyEval_SetProfile(NULL, NULL); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 426 | else |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 427 | PyEval_SetProfile(profile_trampoline, args); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 428 | Py_INCREF(Py_None); |
| 429 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 432 | PyDoc_STRVAR(setprofile_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 433 | "setprofile(function)\n\ |
| 434 | \n\ |
| 435 | 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] | 436 | and return. See the profiler chapter in the library manual." |
| 437 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 439 | static PyObject * |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 440 | sys_getprofile(PyObject *self, PyObject *args) |
| 441 | { |
| 442 | PyThreadState *tstate = PyThreadState_GET(); |
| 443 | PyObject *temp = tstate->c_profileobj; |
| 444 | |
| 445 | if (temp == NULL) |
| 446 | temp = Py_None; |
| 447 | Py_INCREF(temp); |
| 448 | return temp; |
| 449 | } |
| 450 | |
| 451 | PyDoc_STRVAR(getprofile_doc, |
| 452 | "getprofile()\n\ |
| 453 | \n\ |
| 454 | Return the profiling function set with sys.setprofile.\n\ |
| 455 | See the profiler chapter in the library manual." |
| 456 | ); |
| 457 | |
| 458 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 459 | sys_setcheckinterval(PyObject *self, PyObject *args) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 460 | { |
Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 461 | if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_Py_CheckInterval)) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 462 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 463 | Py_INCREF(Py_None); |
| 464 | return Py_None; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 467 | PyDoc_STRVAR(setcheckinterval_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 468 | "setcheckinterval(n)\n\ |
| 469 | \n\ |
| 470 | 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] | 471 | n instructions. This also affects how often thread switches occur." |
| 472 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 473 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 474 | static PyObject * |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 475 | sys_getcheckinterval(PyObject *self, PyObject *args) |
| 476 | { |
| 477 | return PyInt_FromLong(_Py_CheckInterval); |
| 478 | } |
| 479 | |
| 480 | PyDoc_STRVAR(getcheckinterval_doc, |
| 481 | "getcheckinterval() -> current check interval; see setcheckinterval()." |
| 482 | ); |
| 483 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 484 | #ifdef WITH_TSC |
| 485 | static PyObject * |
| 486 | sys_settscdump(PyObject *self, PyObject *args) |
| 487 | { |
| 488 | int bool; |
| 489 | PyThreadState *tstate = PyThreadState_Get(); |
| 490 | |
| 491 | if (!PyArg_ParseTuple(args, "i:settscdump", &bool)) |
| 492 | return NULL; |
| 493 | if (bool) |
| 494 | tstate->interp->tscdump = 1; |
| 495 | else |
| 496 | tstate->interp->tscdump = 0; |
| 497 | Py_INCREF(Py_None); |
| 498 | return Py_None; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 499 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 502 | PyDoc_STRVAR(settscdump_doc, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 503 | "settscdump(bool)\n\ |
| 504 | \n\ |
| 505 | If true, tell the Python interpreter to dump VM measurements to\n\ |
| 506 | stderr. If false, turn off dump. The measurements are based on the\n\ |
Michael W. Hudson | 800ba23 | 2004-08-12 18:19:17 +0000 | [diff] [blame] | 507 | processor's time-stamp counter." |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 508 | ); |
Neal Norwitz | 0f5aed4 | 2004-06-13 20:32:17 +0000 | [diff] [blame] | 509 | #endif /* TSC */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 510 | |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 511 | static PyObject * |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 512 | sys_setrecursionlimit(PyObject *self, PyObject *args) |
| 513 | { |
| 514 | int new_limit; |
| 515 | if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) |
| 516 | return NULL; |
| 517 | if (new_limit <= 0) { |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 518 | PyErr_SetString(PyExc_ValueError, |
| 519 | "recursion limit must be positive"); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 520 | return NULL; |
| 521 | } |
| 522 | Py_SetRecursionLimit(new_limit); |
| 523 | Py_INCREF(Py_None); |
| 524 | return Py_None; |
| 525 | } |
| 526 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 527 | PyDoc_STRVAR(setrecursionlimit_doc, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 528 | "setrecursionlimit(n)\n\ |
| 529 | \n\ |
| 530 | Set the maximum depth of the Python interpreter stack to n. This\n\ |
| 531 | limit prevents infinite recursion from causing an overflow of the C\n\ |
| 532 | 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] | 533 | dependent." |
| 534 | ); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 535 | |
| 536 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 537 | sys_getrecursionlimit(PyObject *self) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 538 | { |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 539 | return PyInt_FromLong(Py_GetRecursionLimit()); |
| 540 | } |
| 541 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 542 | PyDoc_STRVAR(getrecursionlimit_doc, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 543 | "getrecursionlimit()\n\ |
| 544 | \n\ |
| 545 | Return the current value of the recursion limit, the maximum depth\n\ |
| 546 | of the Python interpreter stack. This limit prevents infinite\n\ |
Jack Jansen | e739a0d | 2002-06-26 20:39:20 +0000 | [diff] [blame] | 547 | 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] | 548 | ); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 549 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 550 | #ifdef MS_WINDOWS |
| 551 | PyDoc_STRVAR(getwindowsversion_doc, |
| 552 | "getwindowsversion()\n\ |
| 553 | \n\ |
| 554 | Return information about the running version of Windows.\n\ |
| 555 | The result is a tuple of (major, minor, build, platform, text)\n\ |
| 556 | All elements are numbers, except text which is a string.\n\ |
| 557 | Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP\n\ |
| 558 | " |
| 559 | ); |
| 560 | |
| 561 | static PyObject * |
| 562 | sys_getwindowsversion(PyObject *self) |
| 563 | { |
| 564 | OSVERSIONINFO ver; |
| 565 | ver.dwOSVersionInfoSize = sizeof(ver); |
| 566 | if (!GetVersionEx(&ver)) |
| 567 | return PyErr_SetFromWindowsErr(0); |
| 568 | return Py_BuildValue("HHHHs", |
| 569 | ver.dwMajorVersion, |
| 570 | ver.dwMinorVersion, |
| 571 | ver.dwBuildNumber, |
| 572 | ver.dwPlatformId, |
| 573 | ver.szCSDVersion); |
| 574 | } |
| 575 | |
| 576 | #endif /* MS_WINDOWS */ |
| 577 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 578 | #ifdef HAVE_DLOPEN |
| 579 | static PyObject * |
| 580 | sys_setdlopenflags(PyObject *self, PyObject *args) |
| 581 | { |
| 582 | int new_val; |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 583 | PyThreadState *tstate = PyThreadState_GET(); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 584 | if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) |
| 585 | return NULL; |
| 586 | if (!tstate) |
| 587 | return NULL; |
| 588 | tstate->interp->dlopenflags = new_val; |
| 589 | Py_INCREF(Py_None); |
| 590 | return Py_None; |
| 591 | } |
| 592 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 593 | PyDoc_STRVAR(setdlopenflags_doc, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 594 | "setdlopenflags(n) -> None\n\ |
| 595 | \n\ |
| 596 | 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] | 597 | 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] | 598 | a module, if called as sys.setdlopenflags(0)\n\ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 599 | To share symbols across extension modules, call as\n\ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 600 | sys.setdlopenflags(dl.RTLD_NOW|dl.RTLD_GLOBAL)" |
| 601 | ); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 602 | |
| 603 | static PyObject * |
| 604 | sys_getdlopenflags(PyObject *self, PyObject *args) |
| 605 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 606 | PyThreadState *tstate = PyThreadState_GET(); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 607 | if (!tstate) |
| 608 | return NULL; |
| 609 | return PyInt_FromLong(tstate->interp->dlopenflags); |
| 610 | } |
| 611 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 612 | PyDoc_STRVAR(getdlopenflags_doc, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 613 | "getdlopenflags() -> int\n\ |
| 614 | \n\ |
| 615 | 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] | 616 | calls. The flag constants are defined in the dl module." |
| 617 | ); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 618 | #endif |
| 619 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 620 | #ifdef USE_MALLOPT |
| 621 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 622 | #include <malloc.h> |
| 623 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 624 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 625 | sys_mdebug(PyObject *self, PyObject *args) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 626 | { |
| 627 | int flag; |
Guido van Rossum | ffc0f4f | 2000-03-31 00:38:29 +0000 | [diff] [blame] | 628 | if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 629 | return NULL; |
| 630 | mallopt(M_DEBUG, flag); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 631 | Py_INCREF(Py_None); |
| 632 | return Py_None; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 633 | } |
| 634 | #endif /* USE_MALLOPT */ |
| 635 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 636 | static PyObject * |
Fred Drake | a768882 | 2001-10-24 20:47:48 +0000 | [diff] [blame] | 637 | sys_getrefcount(PyObject *self, PyObject *arg) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 638 | { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 639 | return PyInt_FromSsize_t(arg->ob_refcnt); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 642 | #ifdef Py_REF_DEBUG |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 643 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 644 | sys_gettotalrefcount(PyObject *self) |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 645 | { |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 646 | return PyInt_FromSsize_t(_Py_GetRefTotal()); |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 647 | } |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 648 | #endif /* Py_REF_DEBUG */ |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 649 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 650 | PyDoc_STRVAR(getrefcount_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 651 | "getrefcount(object) -> integer\n\ |
| 652 | \n\ |
Fred Drake | ba3ff1b | 2002-06-20 21:36:19 +0000 | [diff] [blame] | 653 | Return the reference count of object. The count returned is generally\n\ |
| 654 | one higher than you might expect, because it includes the (temporary)\n\ |
| 655 | reference as an argument to getrefcount()." |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 656 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 657 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 658 | #ifdef COUNT_ALLOCS |
| 659 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 660 | sys_getcounts(PyObject *self) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 661 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 662 | extern PyObject *get_counts(void); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 663 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 664 | return get_counts(); |
| 665 | } |
| 666 | #endif |
| 667 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 668 | PyDoc_STRVAR(getframe_doc, |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 669 | "_getframe([depth]) -> frameobject\n\ |
| 670 | \n\ |
| 671 | Return a frame object from the call stack. If optional integer depth is\n\ |
| 672 | given, return the frame object that many calls below the top of the stack.\n\ |
| 673 | If that is deeper than the call stack, ValueError is raised. The default\n\ |
| 674 | for depth is zero, returning the frame at the top of the call stack.\n\ |
| 675 | \n\ |
| 676 | This function should be used for internal and specialized\n\ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 677 | purposes only." |
| 678 | ); |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 679 | |
| 680 | static PyObject * |
| 681 | sys_getframe(PyObject *self, PyObject *args) |
| 682 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 683 | PyFrameObject *f = PyThreadState_GET()->frame; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 684 | int depth = -1; |
| 685 | |
| 686 | if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) |
| 687 | return NULL; |
| 688 | |
| 689 | while (depth > 0 && f != NULL) { |
| 690 | f = f->f_back; |
| 691 | --depth; |
| 692 | } |
| 693 | if (f == NULL) { |
| 694 | PyErr_SetString(PyExc_ValueError, |
| 695 | "call stack is not deep enough"); |
| 696 | return NULL; |
| 697 | } |
| 698 | Py_INCREF(f); |
| 699 | return (PyObject*)f; |
| 700 | } |
| 701 | |
Tim Peters | 32a8361 | 2006-07-10 21:08:24 +0000 | [diff] [blame] | 702 | PyDoc_STRVAR(current_frames_doc, |
| 703 | "_current_frames() -> dictionary\n\ |
| 704 | \n\ |
| 705 | Return a dictionary mapping each current thread T's thread id to T's\n\ |
| 706 | current stack frame.\n\ |
| 707 | \n\ |
| 708 | This function should be used for specialized purposes only." |
| 709 | ); |
| 710 | |
| 711 | static PyObject * |
| 712 | sys_current_frames(PyObject *self, PyObject *noargs) |
| 713 | { |
| 714 | return _PyThread_CurrentFrames(); |
| 715 | } |
| 716 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 717 | PyDoc_STRVAR(call_tracing_doc, |
| 718 | "call_tracing(func, args) -> object\n\ |
| 719 | \n\ |
| 720 | Call func(*args), while tracing is enabled. The tracing state is\n\ |
| 721 | saved, and restored afterwards. This is intended to be called from\n\ |
| 722 | a debugger from a checkpoint, to recursively debug some other code." |
| 723 | ); |
| 724 | |
| 725 | static PyObject * |
| 726 | sys_call_tracing(PyObject *self, PyObject *args) |
| 727 | { |
| 728 | PyObject *func, *funcargs; |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 729 | if (!PyArg_UnpackTuple(args, "call_tracing", 2, 2, &func, &funcargs)) |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 730 | return NULL; |
| 731 | return _PyEval_CallTracing(func, funcargs); |
| 732 | } |
| 733 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 734 | PyDoc_STRVAR(callstats_doc, |
| 735 | "callstats() -> tuple of integers\n\ |
| 736 | \n\ |
| 737 | Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ |
| 738 | when Python was built. Otherwise, return None.\n\ |
| 739 | \n\ |
| 740 | When enabled, this function returns detailed, implementation-specific\n\ |
| 741 | details about the number of function calls executed. The return value is\n\ |
| 742 | a 11-tuple where the entries in the tuple are counts of:\n\ |
| 743 | 0. all function calls\n\ |
| 744 | 1. calls to PyFunction_Type objects\n\ |
| 745 | 2. PyFunction calls that do not create an argument tuple\n\ |
| 746 | 3. PyFunction calls that do not create an argument tuple\n\ |
| 747 | and bypass PyEval_EvalCodeEx()\n\ |
| 748 | 4. PyMethod calls\n\ |
| 749 | 5. PyMethod calls on bound methods\n\ |
| 750 | 6. PyType calls\n\ |
| 751 | 7. PyCFunction calls\n\ |
| 752 | 8. generator calls\n\ |
| 753 | 9. All other calls\n\ |
| 754 | 10. Number of stack pops performed by call_function()" |
| 755 | ); |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 756 | |
Skip Montanaro | 53a6d1d | 2006-04-18 00:55:46 +0000 | [diff] [blame] | 757 | #ifdef __cplusplus |
| 758 | extern "C" { |
| 759 | #endif |
| 760 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 761 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 762 | /* Defined in objects.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 763 | extern PyObject *_Py_GetObjects(PyObject *, PyObject *); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 764 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 765 | |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 766 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 767 | /* Defined in ceval.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 768 | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 769 | #endif |
| 770 | |
Skip Montanaro | 53a6d1d | 2006-04-18 00:55:46 +0000 | [diff] [blame] | 771 | #ifdef __cplusplus |
| 772 | } |
| 773 | #endif |
| 774 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 775 | static PyMethodDef sys_methods[] = { |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 776 | /* Might as well keep this in alphabetic order */ |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 777 | {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 778 | callstats_doc}, |
Tim Peters | 32a8361 | 2006-07-10 21:08:24 +0000 | [diff] [blame] | 779 | {"_current_frames", sys_current_frames, METH_NOARGS, |
| 780 | current_frames_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 781 | {"displayhook", sys_displayhook, METH_O, displayhook_doc}, |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 782 | {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, |
| 783 | {"exc_clear", sys_exc_clear, METH_NOARGS, exc_clear_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 784 | {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, |
Neal Norwitz | 0c766a0 | 2002-03-27 13:03:09 +0000 | [diff] [blame] | 785 | {"exit", sys_exit, METH_VARARGS, exit_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 786 | #ifdef Py_USING_UNICODE |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 787 | {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, |
| 788 | METH_NOARGS, getdefaultencoding_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 789 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 790 | #ifdef HAVE_DLOPEN |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 791 | {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 792 | getdlopenflags_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 793 | #endif |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 794 | #ifdef COUNT_ALLOCS |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 795 | {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 796 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 797 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 798 | {"getdxp", _Py_GetDXProfile, METH_VARARGS}, |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 799 | #endif |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 800 | #ifdef Py_USING_UNICODE |
| 801 | {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 802 | METH_NOARGS, getfilesystemencoding_doc}, |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 803 | #endif |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 804 | #ifdef Py_TRACE_REFS |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 805 | {"getobjects", _Py_GetObjects, METH_VARARGS}, |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 806 | #endif |
| 807 | #ifdef Py_REF_DEBUG |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 808 | {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 809 | #endif |
Fred Drake | a768882 | 2001-10-24 20:47:48 +0000 | [diff] [blame] | 810 | {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 811 | {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 812 | getrecursionlimit_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 813 | {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 814 | #ifdef MS_WINDOWS |
| 815 | {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |
| 816 | getwindowsversion_doc}, |
| 817 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 818 | #ifdef USE_MALLOPT |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 819 | {"mdebug", sys_mdebug, METH_VARARGS}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 820 | #endif |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 821 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 822 | {"setdefaultencoding", sys_setdefaultencoding, METH_VARARGS, |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 823 | setdefaultencoding_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 824 | #endif |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 825 | {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 826 | setcheckinterval_doc}, |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 827 | {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 828 | getcheckinterval_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 829 | #ifdef HAVE_DLOPEN |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 830 | {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 831 | setdlopenflags_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 832 | #endif |
Neal Norwitz | 290d31e | 2002-03-03 15:12:58 +0000 | [diff] [blame] | 833 | {"setprofile", sys_setprofile, METH_O, setprofile_doc}, |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 834 | {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 835 | {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 836 | setrecursionlimit_doc}, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 837 | #ifdef WITH_TSC |
| 838 | {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, |
| 839 | #endif |
Neal Norwitz | 290d31e | 2002-03-03 15:12:58 +0000 | [diff] [blame] | 840 | {"settrace", sys_settrace, METH_O, settrace_doc}, |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 841 | {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 842 | {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 843 | {NULL, NULL} /* sentinel */ |
| 844 | }; |
| 845 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 846 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 847 | list_builtin_module_names(void) |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 848 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 849 | PyObject *list = PyList_New(0); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 850 | int i; |
| 851 | if (list == NULL) |
| 852 | return NULL; |
Guido van Rossum | 25c649f | 1997-11-04 17:04:34 +0000 | [diff] [blame] | 853 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 854 | PyObject *name = PyString_FromString( |
Guido van Rossum | 25c649f | 1997-11-04 17:04:34 +0000 | [diff] [blame] | 855 | PyImport_Inittab[i].name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 856 | if (name == NULL) |
| 857 | break; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 858 | PyList_Append(list, name); |
| 859 | Py_DECREF(name); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 860 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 861 | if (PyList_Sort(list) != 0) { |
| 862 | Py_DECREF(list); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 863 | list = NULL; |
| 864 | } |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 865 | if (list) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 866 | PyObject *v = PyList_AsTuple(list); |
| 867 | Py_DECREF(list); |
Guido van Rossum | 8f49e12 | 1997-01-06 22:55:54 +0000 | [diff] [blame] | 868 | list = v; |
| 869 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 870 | return list; |
| 871 | } |
| 872 | |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 873 | static PyObject *warnoptions = NULL; |
| 874 | |
| 875 | void |
| 876 | PySys_ResetWarnOptions(void) |
| 877 | { |
| 878 | if (warnoptions == NULL || !PyList_Check(warnoptions)) |
| 879 | return; |
| 880 | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
| 881 | } |
| 882 | |
| 883 | void |
| 884 | PySys_AddWarnOption(char *s) |
| 885 | { |
| 886 | PyObject *str; |
| 887 | |
| 888 | if (warnoptions == NULL || !PyList_Check(warnoptions)) { |
| 889 | Py_XDECREF(warnoptions); |
| 890 | warnoptions = PyList_New(0); |
| 891 | if (warnoptions == NULL) |
| 892 | return; |
| 893 | } |
| 894 | str = PyString_FromString(s); |
| 895 | if (str != NULL) { |
| 896 | PyList_Append(warnoptions, str); |
| 897 | Py_DECREF(str); |
| 898 | } |
| 899 | } |
| 900 | |
Guido van Rossum | 40552d0 | 1998-08-06 03:34:39 +0000 | [diff] [blame] | 901 | /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
| 902 | Two literals concatenated works just fine. If you have a K&R compiler |
| 903 | or other abomination that however *does* understand longer strings, |
| 904 | 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] | 905 | PyDoc_VAR(sys_doc) = |
| 906 | PyDoc_STR( |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 907 | "This module provides access to some objects used or maintained by the\n\ |
| 908 | interpreter and to functions that interact strongly with the interpreter.\n\ |
| 909 | \n\ |
| 910 | Dynamic objects:\n\ |
| 911 | \n\ |
| 912 | argv -- command line arguments; argv[0] is the script pathname if known\n\ |
| 913 | path -- module search path; path[0] is the script directory, else ''\n\ |
| 914 | modules -- dictionary of loaded modules\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 915 | \n\ |
| 916 | displayhook -- called to show results in an interactive session\n\ |
| 917 | excepthook -- called to handle any uncaught exception other than SystemExit\n\ |
| 918 | To customize printing in an interactive session or to install a custom\n\ |
| 919 | top-level exception handler, assign other functions to replace these.\n\ |
| 920 | \n\ |
| 921 | exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\ |
| 922 | 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] | 923 | \n\ |
| 924 | stdin -- standard input file object; used by raw_input() and input()\n\ |
| 925 | stdout -- standard output file object; used by the print statement\n\ |
| 926 | stderr -- standard error object; used for error messages\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 927 | By assigning other file objects (or objects that behave like files)\n\ |
| 928 | 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] | 929 | \n\ |
| 930 | last_type -- type of last uncaught exception\n\ |
| 931 | last_value -- value of last uncaught exception\n\ |
| 932 | last_traceback -- traceback of last uncaught exception\n\ |
| 933 | These three are only available in an interactive session after a\n\ |
| 934 | traceback has been printed.\n\ |
| 935 | \n\ |
| 936 | exc_type -- type of exception currently being handled\n\ |
| 937 | exc_value -- value of exception currently being handled\n\ |
| 938 | exc_traceback -- traceback of exception currently being handled\n\ |
| 939 | The function exc_info() should be used instead of these three,\n\ |
| 940 | because it is thread-safe.\n\ |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 941 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 942 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 943 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 944 | PyDoc_STR( |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 945 | "\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 946 | Static objects:\n\ |
| 947 | \n\ |
| 948 | 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] | 949 | maxunicode -- the largest supported character\n\ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 950 | builtin_module_names -- tuple of module names built into this interpreter\n\ |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 951 | version -- the version of this interpreter as a string\n\ |
| 952 | version_info -- version information as a tuple\n\ |
| 953 | hexversion -- version information encoded as a single integer\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 954 | copyright -- copyright notice pertaining to this interpreter\n\ |
| 955 | platform -- platform identifier\n\ |
| 956 | executable -- pathname of this Python interpreter\n\ |
| 957 | prefix -- prefix used to find the Python library\n\ |
| 958 | 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] | 959 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 960 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 961 | #ifdef MS_WINDOWS |
| 962 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 963 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 964 | "dllhandle -- [Windows only] integer handle of the Python DLL\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 965 | winver -- [Windows only] version number of the Python DLL\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 966 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 967 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 968 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 969 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 970 | "__stdin__ -- the original stdin; don't touch!\n\ |
| 971 | __stdout__ -- the original stdout; don't touch!\n\ |
| 972 | __stderr__ -- the original stderr; don't touch!\n\ |
| 973 | __displayhook__ -- the original displayhook; don't touch!\n\ |
| 974 | __excepthook__ -- the original excepthook; don't touch!\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 975 | \n\ |
| 976 | Functions:\n\ |
| 977 | \n\ |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 978 | 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] | 979 | excepthook() -- print an exception and its traceback to sys.stderr\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 980 | exc_info() -- return thread-safe information about the current exception\n\ |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 981 | exc_clear() -- clear the exception state for the current thread\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 982 | exit() -- exit the interpreter by raising SystemExit\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 983 | getdlopenflags() -- returns flags to be used for dlopen() calls\n\ |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 984 | getprofile() -- get the global profiling function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 985 | getrefcount() -- return the reference count for an object (plus one :-)\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 986 | getrecursionlimit() -- return the max recursion depth for the interpreter\n\ |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame^] | 987 | gettrace() -- get the global debug tracing function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 988 | setcheckinterval() -- control how often the interpreter checks for events\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 989 | setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 990 | setprofile() -- set the global profiling function\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 991 | setrecursionlimit() -- set the max recursion depth for the interpreter\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 992 | settrace() -- set the global debug tracing function\n\ |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 993 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 994 | ) |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 995 | /* end of sys_doc */ ; |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 996 | |
Martin v. Löwis | 8e3ca8a | 2005-01-23 09:41:49 +0000 | [diff] [blame] | 997 | static int |
| 998 | _check_and_flush (FILE *stream) |
| 999 | { |
| 1000 | int prev_fail = ferror (stream); |
| 1001 | return fflush (stream) || prev_fail ? EOF : 0; |
| 1002 | } |
| 1003 | |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1004 | /* Subversion branch and revision management */ |
| 1005 | static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION; |
| 1006 | static const char headurl[] = "$HeadURL$"; |
| 1007 | static int svn_initialized; |
| 1008 | static char patchlevel_revision[50]; /* Just the number */ |
| 1009 | static char branch[50]; |
| 1010 | static char shortbranch[50]; |
| 1011 | static const char *svn_revision; |
| 1012 | |
Tim Peters | e86e7a5 | 2006-01-06 02:42:46 +0000 | [diff] [blame] | 1013 | static void |
| 1014 | svnversion_init(void) |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1015 | { |
| 1016 | const char *python, *br_start, *br_end, *br_end2, *svnversion; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 1017 | Py_ssize_t len; |
| 1018 | int istag; |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1019 | |
| 1020 | if (svn_initialized) |
| 1021 | return; |
| 1022 | |
| 1023 | python = strstr(headurl, "/python/"); |
| 1024 | if (!python) |
| 1025 | Py_FatalError("subversion keywords missing"); |
| 1026 | |
| 1027 | br_start = python + 8; |
| 1028 | br_end = strchr(br_start, '/'); |
Neal Norwitz | 837ce93 | 2006-10-28 21:15:30 +0000 | [diff] [blame] | 1029 | assert(br_end); |
| 1030 | |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1031 | /* Works even for trunk, |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1032 | as we are in trunk/Python/sysmodule.c */ |
| 1033 | br_end2 = strchr(br_end+1, '/'); |
| 1034 | |
| 1035 | istag = strncmp(br_start, "tags", 4) == 0; |
| 1036 | if (strncmp(br_start, "trunk", 5) == 0) { |
| 1037 | strcpy(branch, "trunk"); |
| 1038 | strcpy(shortbranch, "trunk"); |
| 1039 | |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1040 | } |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1041 | else if (istag || strncmp(br_start, "branches", 8) == 0) { |
| 1042 | len = br_end2 - br_start; |
| 1043 | strncpy(branch, br_start, len); |
| 1044 | branch[len] = '\0'; |
| 1045 | |
| 1046 | len = br_end2 - (br_end + 1); |
| 1047 | strncpy(shortbranch, br_end + 1, len); |
| 1048 | shortbranch[len] = '\0'; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1049 | } |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1050 | else { |
| 1051 | Py_FatalError("bad HeadURL"); |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | svnversion = _Py_svnversion(); |
| 1057 | if (strcmp(svnversion, "exported") != 0) |
| 1058 | svn_revision = svnversion; |
| 1059 | else if (istag) { |
| 1060 | len = strlen(_patchlevel_revision); |
Neal Norwitz | 68cdf8a | 2007-04-16 07:37:55 +0000 | [diff] [blame] | 1061 | assert(len >= 13); |
| 1062 | assert(len < (sizeof(patchlevel_revision) + 13)); |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1063 | strncpy(patchlevel_revision, _patchlevel_revision + 11, |
| 1064 | len - 13); |
| 1065 | patchlevel_revision[len - 13] = '\0'; |
| 1066 | svn_revision = patchlevel_revision; |
| 1067 | } |
| 1068 | else |
| 1069 | svn_revision = ""; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1070 | |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1071 | svn_initialized = 1; |
| 1072 | } |
| 1073 | |
| 1074 | /* Return svnversion output if available. |
| 1075 | Else return Revision of patchlevel.h if on branch. |
| 1076 | Else return empty string */ |
| 1077 | const char* |
| 1078 | Py_SubversionRevision() |
| 1079 | { |
| 1080 | svnversion_init(); |
| 1081 | return svn_revision; |
| 1082 | } |
| 1083 | |
| 1084 | const char* |
| 1085 | Py_SubversionShortBranch() |
| 1086 | { |
| 1087 | svnversion_init(); |
| 1088 | return shortbranch; |
| 1089 | } |
| 1090 | |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 1091 | |
| 1092 | PyDoc_STRVAR(flags__doc__, |
| 1093 | "sys.flags\n\ |
| 1094 | \n\ |
| 1095 | Flags provided through command line arguments or environment vars."); |
| 1096 | |
| 1097 | static PyTypeObject FlagsType; |
| 1098 | |
| 1099 | static PyStructSequence_Field flags_fields[] = { |
| 1100 | {"debug", "-d"}, |
| 1101 | {"py3k_warning", "-3"}, |
| 1102 | {"division_warning", "-Q"}, |
| 1103 | {"division_new", "-Qnew"}, |
| 1104 | {"inspect", "-i"}, |
| 1105 | {"interactive", "-i"}, |
| 1106 | {"optimize", "-O or -OO"}, |
| 1107 | {"dont_write_bytecode", "-B"}, |
| 1108 | /* {"no_user_site", "-s"}, */ |
| 1109 | {"no_site", "-S"}, |
Andrew M. Kuchling | 7ce9b18 | 2008-01-15 01:29:16 +0000 | [diff] [blame] | 1110 | {"ignore_environment", "-E"}, |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 1111 | {"tabcheck", "-t or -tt"}, |
| 1112 | {"verbose", "-v"}, |
| 1113 | #ifdef RISCOS |
Andrew M. Kuchling | 7ce9b18 | 2008-01-15 01:29:16 +0000 | [diff] [blame] | 1114 | {"riscos_wimp", "???"}, |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 1115 | #endif |
| 1116 | /* {"unbuffered", "-u"}, */ |
| 1117 | {"unicode", "-U"}, |
| 1118 | /* {"skip_first", "-x"}, */ |
| 1119 | {0} |
| 1120 | }; |
| 1121 | |
| 1122 | static PyStructSequence_Desc flags_desc = { |
| 1123 | "sys.flags", /* name */ |
| 1124 | flags__doc__, /* doc */ |
| 1125 | flags_fields, /* fields */ |
| 1126 | #ifdef RISCOS |
| 1127 | 14 |
| 1128 | #else |
| 1129 | 13 |
| 1130 | #endif |
| 1131 | }; |
| 1132 | |
| 1133 | static PyObject* |
| 1134 | make_flags(void) |
| 1135 | { |
| 1136 | int pos = 0; |
| 1137 | PyObject *seq; |
| 1138 | |
| 1139 | seq = PyStructSequence_New(&FlagsType); |
| 1140 | if (seq == NULL) |
| 1141 | return NULL; |
| 1142 | |
| 1143 | #define SetFlag(flag) \ |
| 1144 | PyStructSequence_SET_ITEM(seq, pos++, PyInt_FromLong(flag)) |
| 1145 | |
| 1146 | SetFlag(Py_DebugFlag); |
| 1147 | SetFlag(Py_Py3kWarningFlag); |
| 1148 | SetFlag(Py_DivisionWarningFlag); |
| 1149 | SetFlag(_Py_QnewFlag); |
| 1150 | SetFlag(Py_InspectFlag); |
| 1151 | SetFlag(Py_InteractiveFlag); |
| 1152 | SetFlag(Py_OptimizeFlag); |
| 1153 | SetFlag(Py_DontWriteBytecodeFlag); |
| 1154 | /* SetFlag(Py_NoUserSiteDirectory); */ |
| 1155 | SetFlag(Py_NoSiteFlag); |
| 1156 | SetFlag(Py_IgnoreEnvironmentFlag); |
| 1157 | SetFlag(Py_TabcheckFlag); |
| 1158 | SetFlag(Py_VerboseFlag); |
| 1159 | #ifdef RISCOS |
| 1160 | SetFlag(Py_RISCOSWimpFlag); |
| 1161 | #endif |
| 1162 | /* SetFlag(saw_unbuffered_flag); */ |
| 1163 | SetFlag(Py_UnicodeFlag); |
| 1164 | /* SetFlag(skipfirstline); */ |
| 1165 | #undef SetFlag |
| 1166 | |
| 1167 | if (PyErr_Occurred()) { |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | |
| 1171 | Py_INCREF(seq); |
| 1172 | return seq; |
| 1173 | } |
| 1174 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1175 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1176 | _PySys_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1177 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1178 | PyObject *m, *v, *sysdict; |
| 1179 | PyObject *sysin, *sysout, *syserr; |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1180 | char *s; |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1181 | #ifdef MS_WINDOWS |
Tim Peters | 02f1d0d | 2006-06-06 00:25:07 +0000 | [diff] [blame] | 1182 | char buf[128]; |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1183 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1184 | |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1185 | m = Py_InitModule3("sys", sys_methods, sys_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1186 | if (m == NULL) |
| 1187 | return NULL; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1188 | sysdict = PyModule_GetDict(m); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1189 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 1190 | { |
| 1191 | /* XXX: does this work on Win/Win64? (see posix_fstat) */ |
| 1192 | struct stat sb; |
| 1193 | if (fstat(fileno(stdin), &sb) == 0 && |
| 1194 | S_ISDIR(sb.st_mode)) { |
Neal Norwitz | 72c2c06 | 2006-03-09 05:58:11 +0000 | [diff] [blame] | 1195 | /* There's nothing more we can do. */ |
| 1196 | /* Py_FatalError() will core dump, so just exit. */ |
| 1197 | PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); |
| 1198 | exit(EXIT_FAILURE); |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
Martin v. Löwis | 13a1fde | 2005-01-27 18:56:16 +0000 | [diff] [blame] | 1202 | /* Closing the standard FILE* if sys.std* goes aways causes problems |
| 1203 | * for embedded Python usages. Closing them when somebody explicitly |
| 1204 | * invokes .close() might be possible, but the FAQ promises they get |
| 1205 | * never closed. However, we still need to get write errors when |
| 1206 | * writing fails (e.g. because stdout is redirected), so we flush the |
| 1207 | * streams and check for errors before the file objects are deleted. |
| 1208 | * On OS X, fflush()ing stdin causes an error, so we exempt stdin |
| 1209 | * from that procedure. |
| 1210 | */ |
| 1211 | sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL); |
Martin v. Löwis | 8e3ca8a | 2005-01-23 09:41:49 +0000 | [diff] [blame] | 1212 | sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush); |
| 1213 | syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1214 | if (PyErr_Occurred()) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1215 | return NULL; |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1216 | #ifdef MS_WINDOWS |
Thomas Wouters | afea529 | 2007-01-23 13:42:00 +0000 | [diff] [blame] | 1217 | if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) { |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1218 | sprintf(buf, "cp%d", GetConsoleCP()); |
| 1219 | if (!PyFile_SetEncoding(sysin, buf)) |
| 1220 | return NULL; |
| 1221 | } |
Thomas Wouters | afea529 | 2007-01-23 13:42:00 +0000 | [diff] [blame] | 1222 | if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) { |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1223 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 1224 | if (!PyFile_SetEncoding(sysout, buf)) |
| 1225 | return NULL; |
| 1226 | } |
Thomas Wouters | afea529 | 2007-01-23 13:42:00 +0000 | [diff] [blame] | 1227 | if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) { |
Martin v. Löwis | ea62d25 | 2006-04-03 10:56:49 +0000 | [diff] [blame] | 1228 | sprintf(buf, "cp%d", GetConsoleOutputCP()); |
| 1229 | if (!PyFile_SetEncoding(syserr, buf)) |
| 1230 | return NULL; |
| 1231 | } |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1232 | #endif |
| 1233 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1234 | PyDict_SetItemString(sysdict, "stdin", sysin); |
| 1235 | PyDict_SetItemString(sysdict, "stdout", sysout); |
| 1236 | PyDict_SetItemString(sysdict, "stderr", syserr); |
Guido van Rossum | bd36dba | 1998-02-19 20:53:06 +0000 | [diff] [blame] | 1237 | /* Make backup copies for cleanup */ |
| 1238 | PyDict_SetItemString(sysdict, "__stdin__", sysin); |
| 1239 | PyDict_SetItemString(sysdict, "__stdout__", sysout); |
| 1240 | PyDict_SetItemString(sysdict, "__stderr__", syserr); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1241 | PyDict_SetItemString(sysdict, "__displayhook__", |
| 1242 | PyDict_GetItemString(sysdict, "displayhook")); |
| 1243 | PyDict_SetItemString(sysdict, "__excepthook__", |
| 1244 | PyDict_GetItemString(sysdict, "excepthook")); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1245 | Py_XDECREF(sysin); |
| 1246 | Py_XDECREF(sysout); |
| 1247 | Py_XDECREF(syserr); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1248 | PyDict_SetItemString(sysdict, "version", |
| 1249 | v = PyString_FromString(Py_GetVersion())); |
Barry Warsaw | 54892c4 | 1999-01-27 16:33:19 +0000 | [diff] [blame] | 1250 | Py_XDECREF(v); |
Guido van Rossum | e0d7dae | 1999-01-03 12:55:39 +0000 | [diff] [blame] | 1251 | PyDict_SetItemString(sysdict, "hexversion", |
| 1252 | v = PyInt_FromLong(PY_VERSION_HEX)); |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1253 | Py_XDECREF(v); |
Martin v. Löwis | 43b5780 | 2006-01-05 23:38:54 +0000 | [diff] [blame] | 1254 | svnversion_init(); |
| 1255 | v = Py_BuildValue("(ssz)", "CPython", branch, svn_revision); |
| 1256 | PyDict_SetItemString(sysdict, "subversion", v); |
Barry Warsaw | 2a38a86 | 2005-12-18 01:27:35 +0000 | [diff] [blame] | 1257 | Py_XDECREF(v); |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 1258 | PyDict_SetItemString(sysdict, "dont_write_bytecode", |
| 1259 | v = PyBool_FromLong(Py_DontWriteBytecodeFlag)); |
| 1260 | Py_XDECREF(v); |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 1261 | /* |
| 1262 | * These release level checks are mutually exclusive and cover |
| 1263 | * the field, so don't get too fancy with the pre-processor! |
| 1264 | */ |
| 1265 | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1266 | s = "alpha"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 1267 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1268 | s = "beta"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 1269 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1270 | s = "candidate"; |
Fred Drake | 592f2d6 | 2000-08-31 15:21:11 +0000 | [diff] [blame] | 1271 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1272 | s = "final"; |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 1273 | #endif |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1274 | |
| 1275 | #define SET_SYS_FROM_STRING(key, value) \ |
| 1276 | v = value; \ |
| 1277 | if (v != NULL) \ |
| 1278 | PyDict_SetItemString(sysdict, key, v); \ |
| 1279 | Py_XDECREF(v) |
| 1280 | |
| 1281 | SET_SYS_FROM_STRING("version_info", |
| 1282 | Py_BuildValue("iiisi", PY_MAJOR_VERSION, |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 1283 | PY_MINOR_VERSION, |
Fred Drake | 6d27c1e | 2000-04-13 20:03:20 +0000 | [diff] [blame] | 1284 | PY_MICRO_VERSION, s, |
Fred Drake | 93a20bf | 2000-04-13 17:44:51 +0000 | [diff] [blame] | 1285 | PY_RELEASE_SERIAL)); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1286 | SET_SYS_FROM_STRING("api_version", |
| 1287 | PyInt_FromLong(PYTHON_API_VERSION)); |
| 1288 | SET_SYS_FROM_STRING("copyright", |
| 1289 | PyString_FromString(Py_GetCopyright())); |
| 1290 | SET_SYS_FROM_STRING("platform", |
| 1291 | PyString_FromString(Py_GetPlatform())); |
| 1292 | SET_SYS_FROM_STRING("executable", |
| 1293 | PyString_FromString(Py_GetProgramFullPath())); |
| 1294 | SET_SYS_FROM_STRING("prefix", |
| 1295 | PyString_FromString(Py_GetPrefix())); |
| 1296 | SET_SYS_FROM_STRING("exec_prefix", |
| 1297 | PyString_FromString(Py_GetExecPrefix())); |
| 1298 | SET_SYS_FROM_STRING("maxint", |
| 1299 | PyInt_FromLong(PyInt_GetMax())); |
Christian Heimes | 28104c5 | 2007-11-27 23:16:44 +0000 | [diff] [blame] | 1300 | SET_SYS_FROM_STRING("py3kwarning", |
| 1301 | PyBool_FromLong(Py_Py3kWarningFlag)); |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 1302 | SET_SYS_FROM_STRING("float_info", |
| 1303 | PyFloat_GetInfo()); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1304 | #ifdef Py_USING_UNICODE |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1305 | SET_SYS_FROM_STRING("maxunicode", |
| 1306 | PyInt_FromLong(PyUnicode_GetMax())); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1307 | #endif |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1308 | SET_SYS_FROM_STRING("builtin_module_names", |
| 1309 | list_builtin_module_names()); |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 1310 | { |
| 1311 | /* Assumes that longs are at least 2 bytes long. |
| 1312 | Should be safe! */ |
| 1313 | unsigned long number = 1; |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 1314 | char *value; |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 1315 | |
| 1316 | s = (char *) &number; |
| 1317 | if (s[0] == 0) |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 1318 | value = "big"; |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 1319 | else |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 1320 | value = "little"; |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1321 | SET_SYS_FROM_STRING("byteorder", |
| 1322 | PyString_FromString(value)); |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 1323 | } |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 1324 | #ifdef MS_COREDLL |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1325 | SET_SYS_FROM_STRING("dllhandle", |
| 1326 | PyLong_FromVoidPtr(PyWin_DLLhModule)); |
| 1327 | SET_SYS_FROM_STRING("winver", |
| 1328 | PyString_FromString(PyWin_DLLVersionString)); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 1329 | #endif |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 1330 | #undef SET_SYS_FROM_STRING |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1331 | if (warnoptions == NULL) { |
| 1332 | warnoptions = PyList_New(0); |
| 1333 | } |
| 1334 | else { |
| 1335 | Py_INCREF(warnoptions); |
| 1336 | } |
| 1337 | if (warnoptions != NULL) { |
Guido van Rossum | 03df3b3 | 2001-01-13 22:06:05 +0000 | [diff] [blame] | 1338 | PyDict_SetItemString(sysdict, "warnoptions", warnoptions); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1339 | } |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1340 | |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 1341 | PyStructSequence_InitType(&FlagsType, &flags_desc); |
| 1342 | PyDict_SetItemString(sysdict, "flags", make_flags()); |
| 1343 | /* prevent user from creating new instances */ |
| 1344 | FlagsType.tp_init = NULL; |
| 1345 | FlagsType.tp_new = NULL; |
| 1346 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1347 | if (PyErr_Occurred()) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1348 | return NULL; |
| 1349 | return m; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1352 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1353 | makepathobject(char *path, int delim) |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1354 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1355 | int i, n; |
| 1356 | char *p; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1357 | PyObject *v, *w; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1358 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1359 | n = 1; |
| 1360 | p = path; |
| 1361 | while ((p = strchr(p, delim)) != NULL) { |
| 1362 | n++; |
| 1363 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1364 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1365 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1366 | if (v == NULL) |
| 1367 | return NULL; |
| 1368 | for (i = 0; ; i++) { |
| 1369 | p = strchr(path, delim); |
| 1370 | if (p == NULL) |
| 1371 | p = strchr(path, '\0'); /* End of string */ |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 1372 | w = PyString_FromStringAndSize(path, (Py_ssize_t) (p - path)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1373 | if (w == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1374 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1375 | return NULL; |
| 1376 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1377 | PyList_SetItem(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1378 | if (*p == '\0') |
| 1379 | break; |
| 1380 | path = p+1; |
| 1381 | } |
| 1382 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1386 | PySys_SetPath(char *path) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1387 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1388 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1389 | if ((v = makepathobject(path, DELIM)) == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1390 | Py_FatalError("can't create sys.path"); |
| 1391 | if (PySys_SetObject("path", v) != 0) |
| 1392 | Py_FatalError("can't assign sys.path"); |
| 1393 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1396 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1397 | makeargvobject(int argc, char **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1398 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1399 | PyObject *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 1400 | if (argc <= 0 || argv == NULL) { |
| 1401 | /* Ensure at least one (empty) argument is seen */ |
| 1402 | static char *empty_argv[1] = {""}; |
| 1403 | argv = empty_argv; |
| 1404 | argc = 1; |
| 1405 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1406 | av = PyList_New(argc); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1407 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1408 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1409 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1410 | #ifdef __VMS |
| 1411 | PyObject *v; |
| 1412 | |
| 1413 | /* argv[0] is the script pathname if known */ |
| 1414 | if (i == 0) { |
| 1415 | char* fn = decc$translate_vms(argv[0]); |
| 1416 | if ((fn == (char *)0) || fn == (char *)-1) |
| 1417 | v = PyString_FromString(argv[0]); |
| 1418 | else |
| 1419 | v = PyString_FromString( |
| 1420 | decc$translate_vms(argv[0])); |
| 1421 | } else |
| 1422 | v = PyString_FromString(argv[i]); |
| 1423 | #else |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1424 | PyObject *v = PyString_FromString(argv[i]); |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1425 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1426 | if (v == NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1427 | Py_DECREF(av); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1428 | av = NULL; |
| 1429 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1430 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1431 | PyList_SetItem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1432 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1433 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1434 | return av; |
| 1435 | } |
| 1436 | |
| 1437 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1438 | PySys_SetArgv(int argc, char **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1439 | { |
Guido van Rossum | 162e38c | 2003-02-19 15:25:10 +0000 | [diff] [blame] | 1440 | #if defined(HAVE_REALPATH) |
| 1441 | char fullpath[MAXPATHLEN]; |
| 1442 | #elif defined(MS_WINDOWS) |
Thomas Heller | 27bb71e | 2003-01-08 14:33:48 +0000 | [diff] [blame] | 1443 | char fullpath[MAX_PATH]; |
| 1444 | #endif |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1445 | PyObject *av = makeargvobject(argc, argv); |
| 1446 | PyObject *path = PySys_GetObject("path"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1447 | if (av == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1448 | Py_FatalError("no mem for sys.argv"); |
| 1449 | if (PySys_SetObject("argv", av) != 0) |
| 1450 | Py_FatalError("can't assign sys.argv"); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 1451 | if (path != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1452 | char *argv0 = argv[0]; |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 1453 | char *p = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1454 | Py_ssize_t n = 0; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1455 | PyObject *a; |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1456 | #ifdef HAVE_READLINK |
| 1457 | char link[MAXPATHLEN+1]; |
| 1458 | char argv0copy[2*MAXPATHLEN+1]; |
| 1459 | int nr = 0; |
Georg Brandl | 6953772 | 2005-09-15 13:00:34 +0000 | [diff] [blame] | 1460 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1461 | nr = readlink(argv0, link, MAXPATHLEN); |
| 1462 | if (nr > 0) { |
| 1463 | /* It's a symlink */ |
| 1464 | link[nr] = '\0'; |
| 1465 | if (link[0] == SEP) |
| 1466 | argv0 = link; /* Link to absolute path */ |
| 1467 | else if (strchr(link, SEP) == NULL) |
| 1468 | ; /* Link without path */ |
| 1469 | else { |
| 1470 | /* Must join(dirname(argv0), link) */ |
| 1471 | char *q = strrchr(argv0, SEP); |
| 1472 | if (q == NULL) |
| 1473 | argv0 = link; /* argv0 without path */ |
| 1474 | else { |
| 1475 | /* Must make a copy */ |
| 1476 | strcpy(argv0copy, argv0); |
| 1477 | q = strrchr(argv0copy, SEP); |
| 1478 | strcpy(q+1, link); |
| 1479 | argv0 = argv0copy; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | #endif /* HAVE_READLINK */ |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1484 | #if SEP == '\\' /* Special case for MS filename syntax */ |
Georg Brandl | 6953772 | 2005-09-15 13:00:34 +0000 | [diff] [blame] | 1485 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) { |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1486 | char *q; |
Thomas Heller | 27bb71e | 2003-01-08 14:33:48 +0000 | [diff] [blame] | 1487 | #ifdef MS_WINDOWS |
| 1488 | char *ptemp; |
| 1489 | if (GetFullPathName(argv0, |
| 1490 | sizeof(fullpath), |
| 1491 | fullpath, |
| 1492 | &ptemp)) { |
| 1493 | argv0 = fullpath; |
| 1494 | } |
| 1495 | #endif |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1496 | p = strrchr(argv0, SEP); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1497 | /* Test for alternate separator */ |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1498 | q = strrchr(p ? p : argv0, '/'); |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1499 | if (q != NULL) |
| 1500 | p = q; |
| 1501 | if (p != NULL) { |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1502 | n = p + 1 - argv0; |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1503 | if (n > 1 && p[-1] != ':') |
| 1504 | n--; /* Drop trailing separator */ |
| 1505 | } |
| 1506 | } |
| 1507 | #else /* All other filename syntaxes */ |
Georg Brandl | 6953772 | 2005-09-15 13:00:34 +0000 | [diff] [blame] | 1508 | if (argc > 0 && argv0 != NULL && strcmp(argv0, "-c") != 0) { |
Guido van Rossum | 162e38c | 2003-02-19 15:25:10 +0000 | [diff] [blame] | 1509 | #if defined(HAVE_REALPATH) |
| 1510 | if (realpath(argv0, fullpath)) { |
| 1511 | argv0 = fullpath; |
| 1512 | } |
| 1513 | #endif |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1514 | p = strrchr(argv0, SEP); |
Guido van Rossum | 162e38c | 2003-02-19 15:25:10 +0000 | [diff] [blame] | 1515 | } |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1516 | if (p != NULL) { |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 1517 | #ifndef RISCOS |
Guido van Rossum | c474dea | 1997-04-25 15:38:31 +0000 | [diff] [blame] | 1518 | n = p + 1 - argv0; |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 1519 | #else /* don't include trailing separator */ |
| 1520 | n = p - argv0; |
| 1521 | #endif /* RISCOS */ |
Guido van Rossum | cc88341 | 1996-09-10 14:44:21 +0000 | [diff] [blame] | 1522 | #if SEP == '/' /* Special case for Unix filename syntax */ |
| 1523 | if (n > 1) |
| 1524 | n--; /* Drop trailing separator */ |
| 1525 | #endif /* Unix */ |
| 1526 | } |
| 1527 | #endif /* All others */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1528 | a = PyString_FromStringAndSize(argv0, n); |
Guido van Rossum | 94a9667 | 1996-07-30 20:35:50 +0000 | [diff] [blame] | 1529 | if (a == NULL) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1530 | Py_FatalError("no mem for sys.path insertion"); |
| 1531 | if (PyList_Insert(path, 0, a) < 0) |
| 1532 | Py_FatalError("sys.path.insert(0) failed"); |
| 1533 | Py_DECREF(a); |
Guido van Rossum | a63d9f4 | 1996-07-24 01:31:37 +0000 | [diff] [blame] | 1534 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1535 | Py_DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1536 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1537 | |
| 1538 | |
| 1539 | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
| 1540 | Adapted from code submitted by Just van Rossum. |
| 1541 | |
| 1542 | PySys_WriteStdout(format, ...) |
| 1543 | PySys_WriteStderr(format, ...) |
| 1544 | |
| 1545 | The first function writes to sys.stdout; the second to sys.stderr. When |
| 1546 | 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] | 1547 | no exceptions are raised. |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1548 | |
| 1549 | Both take a printf-style format string as their first argument followed |
| 1550 | by a variable length argument list determined by the format string. |
| 1551 | |
| 1552 | *** WARNING *** |
| 1553 | |
| 1554 | The format should limit the total size of the formatted output string to |
| 1555 | 1000 bytes. In particular, this means that no unrestricted "%s" formats |
| 1556 | should occur; these should be limited using "%.<N>s where <N> is a |
| 1557 | decimal number calculated so that <N> plus the maximum size of other |
| 1558 | formatted text does not exceed 1000 bytes. Also watch out for "%f", |
| 1559 | which can print hundreds of digits for very large numbers. |
| 1560 | |
| 1561 | */ |
| 1562 | |
| 1563 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1564 | mywrite(char *name, FILE *fp, const char *format, va_list va) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1565 | { |
| 1566 | PyObject *file; |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1567 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1568 | |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1569 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1570 | file = PySys_GetObject(name); |
| 1571 | if (file == NULL || PyFile_AsFile(file) == fp) |
| 1572 | vfprintf(fp, format, va); |
| 1573 | else { |
| 1574 | char buffer[1001]; |
Tim Peters | 080d5b3 | 2001-12-02 08:29:16 +0000 | [diff] [blame] | 1575 | const int written = PyOS_vsnprintf(buffer, sizeof(buffer), |
| 1576 | format, va); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1577 | if (PyFile_WriteString(buffer, file) != 0) { |
| 1578 | PyErr_Clear(); |
| 1579 | fputs(buffer, fp); |
| 1580 | } |
Skip Montanaro | 53a6d1d | 2006-04-18 00:55:46 +0000 | [diff] [blame] | 1581 | if (written < 0 || (size_t)written >= sizeof(buffer)) { |
Jeremy Hylton | 5d3d134 | 2001-11-28 21:44:53 +0000 | [diff] [blame] | 1582 | const char *truncated = "... truncated"; |
| 1583 | if (PyFile_WriteString(truncated, file) != 0) { |
| 1584 | PyErr_Clear(); |
| 1585 | fputs(truncated, fp); |
| 1586 | } |
| 1587 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1588 | } |
Guido van Rossum | 8442af3 | 1998-10-12 18:22:10 +0000 | [diff] [blame] | 1589 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1593 | PySys_WriteStdout(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1594 | { |
| 1595 | va_list va; |
| 1596 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1597 | va_start(va, format); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1598 | mywrite("stdout", stdout, format, va); |
| 1599 | va_end(va); |
| 1600 | } |
| 1601 | |
| 1602 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1603 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1604 | { |
| 1605 | va_list va; |
| 1606 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1607 | va_start(va, format); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1608 | mywrite("stderr", stderr, format, va); |
| 1609 | va_end(va); |
| 1610 | } |