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" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 18 | #include "code.h" |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 19 | #include "frameobject.h" |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 20 | #include "pythread.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 22 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 23 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 24 | #ifdef MS_WINDOWS |
| 25 | #define WIN32_LEAN_AND_MEAN |
Amaury Forgeot d'Arc | 06cfe95 | 2007-11-10 13:55:44 +0000 | [diff] [blame] | 26 | #include <windows.h> |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 27 | #endif /* MS_WINDOWS */ |
| 28 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 29 | #ifdef MS_COREDLL |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 30 | extern void *PyWin_DLLhModule; |
Guido van Rossum | 6c1e5f2 | 1997-09-29 23:34:23 +0000 | [diff] [blame] | 31 | /* A string loaded from the DLL at startup: */ |
| 32 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 35 | #ifdef __VMS |
| 36 | #include <unixlib.h> |
| 37 | #endif |
| 38 | |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 39 | #ifdef HAVE_LANGINFO_H |
| 40 | #include <locale.h> |
| 41 | #include <langinfo.h> |
| 42 | #endif |
| 43 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 44 | PyObject * |
Neal Norwitz | f308132 | 2007-08-25 00:32:45 +0000 | [diff] [blame] | 45 | PySys_GetObject(const char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | PyThreadState *tstate = PyThreadState_GET(); |
| 48 | PyObject *sd = tstate->interp->sysdict; |
| 49 | if (sd == NULL) |
| 50 | return NULL; |
| 51 | return PyDict_GetItemString(sd, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | int |
Neal Norwitz | f308132 | 2007-08-25 00:32:45 +0000 | [diff] [blame] | 55 | PySys_SetObject(const char *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | PyThreadState *tstate = PyThreadState_GET(); |
| 58 | PyObject *sd = tstate->interp->sysdict; |
| 59 | if (v == NULL) { |
| 60 | if (PyDict_GetItemString(sd, name) == NULL) |
| 61 | return 0; |
| 62 | else |
| 63 | return PyDict_DelItemString(sd, name); |
| 64 | } |
| 65 | else |
| 66 | return PyDict_SetItemString(sd, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 69 | /* Write repr(o) to sys.stdout using sys.stdout.encoding and 'backslashreplace' |
| 70 | error handler. If sys.stdout has a buffer attribute, use |
| 71 | sys.stdout.buffer.write(encoded), otherwise redecode the string and use |
| 72 | sys.stdout.write(redecoded). |
| 73 | |
| 74 | Helper function for sys_displayhook(). */ |
| 75 | static int |
| 76 | sys_displayhook_unencodable(PyObject *outf, PyObject *o) |
| 77 | { |
| 78 | PyObject *stdout_encoding = NULL; |
| 79 | PyObject *encoded, *escaped_str, *repr_str, *buffer, *result; |
| 80 | char *stdout_encoding_str; |
| 81 | int ret; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 82 | _Py_IDENTIFIER(encoding); |
| 83 | _Py_IDENTIFIER(buffer); |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 84 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 85 | stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding); |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 86 | if (stdout_encoding == NULL) |
| 87 | goto error; |
| 88 | stdout_encoding_str = _PyUnicode_AsString(stdout_encoding); |
| 89 | if (stdout_encoding_str == NULL) |
| 90 | goto error; |
| 91 | |
| 92 | repr_str = PyObject_Repr(o); |
| 93 | if (repr_str == NULL) |
| 94 | goto error; |
| 95 | encoded = PyUnicode_AsEncodedString(repr_str, |
| 96 | stdout_encoding_str, |
| 97 | "backslashreplace"); |
| 98 | Py_DECREF(repr_str); |
| 99 | if (encoded == NULL) |
| 100 | goto error; |
| 101 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 102 | buffer = _PyObject_GetAttrId(outf, &PyId_buffer); |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 103 | if (buffer) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 104 | _Py_IDENTIFIER(write); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 105 | result = _PyObject_CallMethodId(buffer, &PyId_write, "(O)", encoded); |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 106 | Py_DECREF(buffer); |
| 107 | Py_DECREF(encoded); |
| 108 | if (result == NULL) |
| 109 | goto error; |
| 110 | Py_DECREF(result); |
| 111 | } |
| 112 | else { |
| 113 | PyErr_Clear(); |
| 114 | escaped_str = PyUnicode_FromEncodedObject(encoded, |
| 115 | stdout_encoding_str, |
| 116 | "strict"); |
| 117 | Py_DECREF(encoded); |
| 118 | if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) { |
| 119 | Py_DECREF(escaped_str); |
| 120 | goto error; |
| 121 | } |
| 122 | Py_DECREF(escaped_str); |
| 123 | } |
| 124 | ret = 0; |
| 125 | goto finally; |
| 126 | |
| 127 | error: |
| 128 | ret = -1; |
| 129 | finally: |
| 130 | Py_XDECREF(stdout_encoding); |
| 131 | return ret; |
| 132 | } |
| 133 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 134 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 135 | sys_displayhook(PyObject *self, PyObject *o) |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | PyObject *outf; |
| 138 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
| 139 | PyObject *modules = interp->modules; |
| 140 | PyObject *builtins = PyDict_GetItemString(modules, "builtins"); |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 141 | int err; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 142 | _Py_IDENTIFIER(_); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | if (builtins == NULL) { |
| 145 | PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); |
| 146 | return NULL; |
| 147 | } |
Moshe Zadka | 03897ea | 2001-07-23 13:32:43 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* Print value except if None */ |
| 150 | /* After printing, also assign to '_' */ |
| 151 | /* Before, set '_' to None to avoid recursion */ |
| 152 | if (o == Py_None) { |
| 153 | Py_INCREF(Py_None); |
| 154 | return Py_None; |
| 155 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 156 | if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | return NULL; |
| 158 | outf = PySys_GetObject("stdout"); |
| 159 | if (outf == NULL || outf == Py_None) { |
| 160 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
| 161 | return NULL; |
| 162 | } |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 163 | if (PyFile_WriteObject(o, outf, 0) != 0) { |
| 164 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 165 | /* repr(o) is not encodable to sys.stdout.encoding with |
| 166 | * sys.stdout.errors error handler (which is probably 'strict') */ |
| 167 | PyErr_Clear(); |
| 168 | err = sys_displayhook_unencodable(outf, o); |
| 169 | if (err) |
| 170 | return NULL; |
| 171 | } |
| 172 | else { |
| 173 | return NULL; |
| 174 | } |
| 175 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | if (PyFile_WriteString("\n", outf) != 0) |
| 177 | return NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 178 | if (_PyObject_SetAttrId(builtins, &PyId__, o) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | return NULL; |
| 180 | Py_INCREF(Py_None); |
| 181 | return Py_None; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 184 | PyDoc_STRVAR(displayhook_doc, |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 185 | "displayhook(object) -> None\n" |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 186 | "\n" |
Florent Xicluna | 5749e85 | 2010-03-03 11:54:54 +0000 | [diff] [blame] | 187 | "Print an object to sys.stdout and also save it in builtins._\n" |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 188 | ); |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 189 | |
| 190 | static PyObject * |
| 191 | sys_excepthook(PyObject* self, PyObject* args) |
| 192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | PyObject *exc, *value, *tb; |
| 194 | if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb)) |
| 195 | return NULL; |
| 196 | PyErr_Display(exc, value, tb); |
| 197 | Py_INCREF(Py_None); |
| 198 | return Py_None; |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 201 | PyDoc_STRVAR(excepthook_doc, |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 202 | "excepthook(exctype, value, traceback) -> None\n" |
| 203 | "\n" |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 204 | "Handle an exception by displaying it with a traceback on sys.stderr.\n" |
| 205 | ); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 206 | |
| 207 | static PyObject * |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 208 | sys_exc_info(PyObject *self, PyObject *noargs) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | PyThreadState *tstate; |
| 211 | tstate = PyThreadState_GET(); |
| 212 | return Py_BuildValue( |
| 213 | "(OOO)", |
| 214 | tstate->exc_type != NULL ? tstate->exc_type : Py_None, |
| 215 | tstate->exc_value != NULL ? tstate->exc_value : Py_None, |
| 216 | tstate->exc_traceback != NULL ? |
| 217 | tstate->exc_traceback : Py_None); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 220 | PyDoc_STRVAR(exc_info_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 221 | "exc_info() -> (type, value, traceback)\n\ |
| 222 | \n\ |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 223 | Return information about the most recent exception caught by an except\n\ |
| 224 | clause in the current stack frame or in an older stack frame." |
| 225 | ); |
| 226 | |
| 227 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 228 | sys_exit(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | PyObject *exit_code = 0; |
| 231 | if (!PyArg_UnpackTuple(args, "exit", 0, 1, &exit_code)) |
| 232 | return NULL; |
| 233 | /* Raise SystemExit so callers may catch it or clean up. */ |
| 234 | PyErr_SetObject(PyExc_SystemExit, exit_code); |
| 235 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 238 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 239 | "exit([status])\n\ |
| 240 | \n\ |
| 241 | Exit the interpreter by raising SystemExit(status).\n\ |
| 242 | 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] | 243 | 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] | 244 | 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] | 245 | exit status will be one (i.e., failure)." |
| 246 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 247 | |
Martin v. Löwis | 107b7da | 2001-11-09 20:59:39 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 249 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 250 | sys_getdefaultencoding(PyObject *self) |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | return PyUnicode_FromString(PyUnicode_GetDefaultEncoding()); |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 255 | PyDoc_STRVAR(getdefaultencoding_doc, |
Marc-André Lemburg | 99964b8 | 2000-06-07 09:13:41 +0000 | [diff] [blame] | 256 | "getdefaultencoding() -> string\n\ |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 257 | \n\ |
| 258 | 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] | 259 | implementation." |
| 260 | ); |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 261 | |
| 262 | static PyObject * |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 263 | sys_getfilesystemencoding(PyObject *self) |
| 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | if (Py_FileSystemDefaultEncoding) |
| 266 | return PyUnicode_FromString(Py_FileSystemDefaultEncoding); |
Victor Stinner | 27181ac | 2011-03-31 13:39:03 +0200 | [diff] [blame] | 267 | PyErr_SetString(PyExc_RuntimeError, |
| 268 | "filesystem encoding is not initialized"); |
| 269 | return NULL; |
Martin v. Löwis | 73d538b | 2003-03-05 15:13:47 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | PyDoc_STRVAR(getfilesystemencoding_doc, |
| 273 | "getfilesystemencoding() -> string\n\ |
| 274 | \n\ |
| 275 | Return the encoding used to convert Unicode filenames in\n\ |
| 276 | operating system filenames." |
| 277 | ); |
| 278 | |
Martin v. Löwis | 04dc25c | 2008-10-03 16:09:28 +0000 | [diff] [blame] | 279 | static PyObject * |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 280 | sys_intern(PyObject *self, PyObject *args) |
| 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | PyObject *s; |
| 283 | if (!PyArg_ParseTuple(args, "U:intern", &s)) |
| 284 | return NULL; |
| 285 | if (PyUnicode_CheckExact(s)) { |
| 286 | Py_INCREF(s); |
| 287 | PyUnicode_InternInPlace(&s); |
| 288 | return s; |
| 289 | } |
| 290 | else { |
| 291 | PyErr_Format(PyExc_TypeError, |
| 292 | "can't intern %.400s", s->ob_type->tp_name); |
| 293 | return NULL; |
| 294 | } |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | PyDoc_STRVAR(intern_doc, |
| 298 | "intern(string) -> string\n\ |
| 299 | \n\ |
| 300 | ``Intern'' the given string. This enters the string in the (global)\n\ |
| 301 | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
| 302 | Return the string itself or the previously interned string object with the\n\ |
| 303 | same value."); |
| 304 | |
| 305 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 306 | /* |
| 307 | * Cached interned string objects used for calling the profile and |
| 308 | * trace functions. Initialized by trace_init(). |
| 309 | */ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 310 | static PyObject *whatstrings[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 311 | |
| 312 | static int |
| 313 | trace_init(void) |
| 314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | static char *whatnames[7] = {"call", "exception", "line", "return", |
| 316 | "c_call", "c_exception", "c_return"}; |
| 317 | PyObject *name; |
| 318 | int i; |
| 319 | for (i = 0; i < 7; ++i) { |
| 320 | if (whatstrings[i] == NULL) { |
| 321 | name = PyUnicode_InternFromString(whatnames[i]); |
| 322 | if (name == NULL) |
| 323 | return -1; |
| 324 | whatstrings[i] = name; |
| 325 | } |
| 326 | } |
| 327 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
| 331 | static PyObject * |
| 332 | call_trampoline(PyThreadState *tstate, PyObject* callback, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | PyFrameObject *frame, int what, PyObject *arg) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyObject *args = PyTuple_New(3); |
| 336 | PyObject *whatstr; |
| 337 | PyObject *result; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | if (args == NULL) |
| 340 | return NULL; |
| 341 | Py_INCREF(frame); |
| 342 | whatstr = whatstrings[what]; |
| 343 | Py_INCREF(whatstr); |
| 344 | if (arg == NULL) |
| 345 | arg = Py_None; |
| 346 | Py_INCREF(arg); |
| 347 | PyTuple_SET_ITEM(args, 0, (PyObject *)frame); |
| 348 | PyTuple_SET_ITEM(args, 1, whatstr); |
| 349 | PyTuple_SET_ITEM(args, 2, arg); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | /* call the Python-level function */ |
| 352 | PyFrame_FastToLocals(frame); |
| 353 | result = PyEval_CallObject(callback, args); |
| 354 | PyFrame_LocalsToFast(frame, 1); |
| 355 | if (result == NULL) |
| 356 | PyTraceBack_Here(frame); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | /* cleanup */ |
| 359 | Py_DECREF(args); |
| 360 | return result; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static int |
| 364 | profile_trampoline(PyObject *self, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | int what, PyObject *arg) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | PyThreadState *tstate = frame->f_tstate; |
| 368 | PyObject *result; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | if (arg == NULL) |
| 371 | arg = Py_None; |
| 372 | result = call_trampoline(tstate, self, frame, what, arg); |
| 373 | if (result == NULL) { |
| 374 | PyEval_SetProfile(NULL, NULL); |
| 375 | return -1; |
| 376 | } |
| 377 | Py_DECREF(result); |
| 378 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int |
| 382 | trace_trampoline(PyObject *self, PyFrameObject *frame, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | int what, PyObject *arg) |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | PyThreadState *tstate = frame->f_tstate; |
| 386 | PyObject *callback; |
| 387 | PyObject *result; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (what == PyTrace_CALL) |
| 390 | callback = self; |
| 391 | else |
| 392 | callback = frame->f_trace; |
| 393 | if (callback == NULL) |
| 394 | return 0; |
| 395 | result = call_trampoline(tstate, callback, frame, what, arg); |
| 396 | if (result == NULL) { |
| 397 | PyEval_SetTrace(NULL, NULL); |
| 398 | Py_XDECREF(frame->f_trace); |
| 399 | frame->f_trace = NULL; |
| 400 | return -1; |
| 401 | } |
| 402 | if (result != Py_None) { |
| 403 | PyObject *temp = frame->f_trace; |
| 404 | frame->f_trace = NULL; |
| 405 | Py_XDECREF(temp); |
| 406 | frame->f_trace = result; |
| 407 | } |
| 408 | else { |
| 409 | Py_DECREF(result); |
| 410 | } |
| 411 | return 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 412 | } |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 413 | |
Fred Drake | 8b4d01d | 2000-05-09 19:57:01 +0000 | [diff] [blame] | 414 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 415 | sys_settrace(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | if (trace_init() == -1) |
| 418 | return NULL; |
| 419 | if (args == Py_None) |
| 420 | PyEval_SetTrace(NULL, NULL); |
| 421 | else |
| 422 | PyEval_SetTrace(trace_trampoline, args); |
| 423 | Py_INCREF(Py_None); |
| 424 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 427 | PyDoc_STRVAR(settrace_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 428 | "settrace(function)\n\ |
| 429 | \n\ |
| 430 | 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] | 431 | function call. See the debugger chapter in the library manual." |
| 432 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 434 | static PyObject * |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 435 | sys_gettrace(PyObject *self, PyObject *args) |
| 436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | PyThreadState *tstate = PyThreadState_GET(); |
| 438 | PyObject *temp = tstate->c_traceobj; |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (temp == NULL) |
| 441 | temp = Py_None; |
| 442 | Py_INCREF(temp); |
| 443 | return temp; |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | PyDoc_STRVAR(gettrace_doc, |
| 447 | "gettrace()\n\ |
| 448 | \n\ |
| 449 | Return the global debug tracing function set with sys.settrace.\n\ |
| 450 | See the debugger chapter in the library manual." |
| 451 | ); |
| 452 | |
| 453 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 454 | sys_setprofile(PyObject *self, PyObject *args) |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | if (trace_init() == -1) |
| 457 | return NULL; |
| 458 | if (args == Py_None) |
| 459 | PyEval_SetProfile(NULL, NULL); |
| 460 | else |
| 461 | PyEval_SetProfile(profile_trampoline, args); |
| 462 | Py_INCREF(Py_None); |
| 463 | return Py_None; |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 466 | PyDoc_STRVAR(setprofile_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 467 | "setprofile(function)\n\ |
| 468 | \n\ |
| 469 | 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] | 470 | and return. See the profiler chapter in the library manual." |
| 471 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 472 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 473 | static PyObject * |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 474 | sys_getprofile(PyObject *self, PyObject *args) |
| 475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | PyThreadState *tstate = PyThreadState_GET(); |
| 477 | PyObject *temp = tstate->c_profileobj; |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | if (temp == NULL) |
| 480 | temp = Py_None; |
| 481 | Py_INCREF(temp); |
| 482 | return temp; |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | PyDoc_STRVAR(getprofile_doc, |
| 486 | "getprofile()\n\ |
| 487 | \n\ |
| 488 | Return the profiling function set with sys.setprofile.\n\ |
| 489 | See the profiler chapter in the library manual." |
| 490 | ); |
| 491 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 492 | static int _check_interval = 100; |
| 493 | |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 494 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 495 | sys_setcheckinterval(PyObject *self, PyObject *args) |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 498 | "sys.getcheckinterval() and sys.setcheckinterval() " |
| 499 | "are deprecated. Use sys.setswitchinterval() " |
| 500 | "instead.", 1) < 0) |
| 501 | return NULL; |
| 502 | if (!PyArg_ParseTuple(args, "i:setcheckinterval", &_check_interval)) |
| 503 | return NULL; |
| 504 | Py_INCREF(Py_None); |
| 505 | return Py_None; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 508 | PyDoc_STRVAR(setcheckinterval_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 509 | "setcheckinterval(n)\n\ |
| 510 | \n\ |
| 511 | 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] | 512 | n instructions. This also affects how often thread switches occur." |
| 513 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 514 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 515 | static PyObject * |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 516 | sys_getcheckinterval(PyObject *self, PyObject *args) |
| 517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 519 | "sys.getcheckinterval() and sys.setcheckinterval() " |
| 520 | "are deprecated. Use sys.getswitchinterval() " |
| 521 | "instead.", 1) < 0) |
| 522 | return NULL; |
| 523 | return PyLong_FromLong(_check_interval); |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | PyDoc_STRVAR(getcheckinterval_doc, |
| 527 | "getcheckinterval() -> current check interval; see setcheckinterval()." |
| 528 | ); |
| 529 | |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 530 | #ifdef WITH_THREAD |
| 531 | static PyObject * |
| 532 | sys_setswitchinterval(PyObject *self, PyObject *args) |
| 533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | double d; |
| 535 | if (!PyArg_ParseTuple(args, "d:setswitchinterval", &d)) |
| 536 | return NULL; |
| 537 | if (d <= 0.0) { |
| 538 | PyErr_SetString(PyExc_ValueError, |
| 539 | "switch interval must be strictly positive"); |
| 540 | return NULL; |
| 541 | } |
| 542 | _PyEval_SetSwitchInterval((unsigned long) (1e6 * d)); |
| 543 | Py_INCREF(Py_None); |
| 544 | return Py_None; |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | PyDoc_STRVAR(setswitchinterval_doc, |
| 548 | "setswitchinterval(n)\n\ |
| 549 | \n\ |
| 550 | Set the ideal thread switching delay inside the Python interpreter\n\ |
| 551 | The actual frequency of switching threads can be lower if the\n\ |
| 552 | interpreter executes long sequences of uninterruptible code\n\ |
| 553 | (this is implementation-specific and workload-dependent).\n\ |
| 554 | \n\ |
| 555 | The parameter must represent the desired switching delay in seconds\n\ |
| 556 | A typical value is 0.005 (5 milliseconds)." |
| 557 | ); |
| 558 | |
| 559 | static PyObject * |
| 560 | sys_getswitchinterval(PyObject *self, PyObject *args) |
| 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | return PyFloat_FromDouble(1e-6 * _PyEval_GetSwitchInterval()); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | PyDoc_STRVAR(getswitchinterval_doc, |
| 566 | "getswitchinterval() -> current thread switch interval; see setswitchinterval()." |
| 567 | ); |
| 568 | |
| 569 | #endif /* WITH_THREAD */ |
| 570 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 571 | #ifdef WITH_TSC |
| 572 | static PyObject * |
| 573 | sys_settscdump(PyObject *self, PyObject *args) |
| 574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | int bool; |
| 576 | PyThreadState *tstate = PyThreadState_Get(); |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | if (!PyArg_ParseTuple(args, "i:settscdump", &bool)) |
| 579 | return NULL; |
| 580 | if (bool) |
| 581 | tstate->interp->tscdump = 1; |
| 582 | else |
| 583 | tstate->interp->tscdump = 0; |
| 584 | Py_INCREF(Py_None); |
| 585 | return Py_None; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 586 | |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 589 | PyDoc_STRVAR(settscdump_doc, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 590 | "settscdump(bool)\n\ |
| 591 | \n\ |
| 592 | If true, tell the Python interpreter to dump VM measurements to\n\ |
| 593 | 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] | 594 | processor's time-stamp counter." |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 595 | ); |
Neal Norwitz | 0f5aed4 | 2004-06-13 20:32:17 +0000 | [diff] [blame] | 596 | #endif /* TSC */ |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 597 | |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 598 | static PyObject * |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 599 | sys_setrecursionlimit(PyObject *self, PyObject *args) |
| 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | int new_limit; |
| 602 | if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit)) |
| 603 | return NULL; |
| 604 | if (new_limit <= 0) { |
| 605 | PyErr_SetString(PyExc_ValueError, |
| 606 | "recursion limit must be positive"); |
| 607 | return NULL; |
| 608 | } |
| 609 | Py_SetRecursionLimit(new_limit); |
| 610 | Py_INCREF(Py_None); |
| 611 | return Py_None; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 614 | static PyTypeObject Hash_InfoType; |
| 615 | |
| 616 | PyDoc_STRVAR(hash_info_doc, |
| 617 | "hash_info\n\ |
| 618 | \n\ |
| 619 | A struct sequence providing parameters used for computing\n\ |
| 620 | numeric hashes. The attributes are read only."); |
| 621 | |
| 622 | static PyStructSequence_Field hash_info_fields[] = { |
| 623 | {"width", "width of the type used for hashing, in bits"}, |
| 624 | {"modulus", "prime number giving the modulus on which the hash " |
| 625 | "function is based"}, |
| 626 | {"inf", "value to be used for hash of a positive infinity"}, |
| 627 | {"nan", "value to be used for hash of a nan"}, |
| 628 | {"imag", "multiplier used for the imaginary part of a complex number"}, |
| 629 | {NULL, NULL} |
| 630 | }; |
| 631 | |
| 632 | static PyStructSequence_Desc hash_info_desc = { |
| 633 | "sys.hash_info", |
| 634 | hash_info_doc, |
| 635 | hash_info_fields, |
| 636 | 5, |
| 637 | }; |
| 638 | |
Matthias Klose | d885e95 | 2010-07-06 10:53:30 +0000 | [diff] [blame] | 639 | static PyObject * |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 640 | get_hash_info(void) |
| 641 | { |
| 642 | PyObject *hash_info; |
| 643 | int field = 0; |
| 644 | hash_info = PyStructSequence_New(&Hash_InfoType); |
| 645 | if (hash_info == NULL) |
| 646 | return NULL; |
| 647 | PyStructSequence_SET_ITEM(hash_info, field++, |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 648 | PyLong_FromLong(8*sizeof(Py_hash_t))); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 649 | PyStructSequence_SET_ITEM(hash_info, field++, |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 650 | PyLong_FromSsize_t(_PyHASH_MODULUS)); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 651 | PyStructSequence_SET_ITEM(hash_info, field++, |
| 652 | PyLong_FromLong(_PyHASH_INF)); |
| 653 | PyStructSequence_SET_ITEM(hash_info, field++, |
| 654 | PyLong_FromLong(_PyHASH_NAN)); |
| 655 | PyStructSequence_SET_ITEM(hash_info, field++, |
| 656 | PyLong_FromLong(_PyHASH_IMAG)); |
| 657 | if (PyErr_Occurred()) { |
| 658 | Py_CLEAR(hash_info); |
| 659 | return NULL; |
| 660 | } |
| 661 | return hash_info; |
| 662 | } |
| 663 | |
| 664 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 665 | PyDoc_STRVAR(setrecursionlimit_doc, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 666 | "setrecursionlimit(n)\n\ |
| 667 | \n\ |
| 668 | Set the maximum depth of the Python interpreter stack to n. This\n\ |
| 669 | limit prevents infinite recursion from causing an overflow of the C\n\ |
| 670 | 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] | 671 | dependent." |
| 672 | ); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 673 | |
| 674 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 675 | sys_getrecursionlimit(PyObject *self) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | return PyLong_FromLong(Py_GetRecursionLimit()); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 680 | PyDoc_STRVAR(getrecursionlimit_doc, |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 681 | "getrecursionlimit()\n\ |
| 682 | \n\ |
| 683 | Return the current value of the recursion limit, the maximum depth\n\ |
| 684 | of the Python interpreter stack. This limit prevents infinite\n\ |
Jack Jansen | e739a0d | 2002-06-26 20:39:20 +0000 | [diff] [blame] | 685 | 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] | 686 | ); |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 687 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 688 | #ifdef MS_WINDOWS |
| 689 | PyDoc_STRVAR(getwindowsversion_doc, |
| 690 | "getwindowsversion()\n\ |
| 691 | \n\ |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 692 | Return information about the running version of Windows as a named tuple.\n\ |
| 693 | The members are named: major, minor, build, platform, service_pack,\n\ |
| 694 | service_pack_major, service_pack_minor, suite_mask, and product_type. For\n\ |
Ezio Melotti | 4969f70 | 2011-03-15 05:59:46 +0200 | [diff] [blame] | 695 | backward compatibility, only the first 5 items are available by indexing.\n\ |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 696 | All elements are numbers, except service_pack which is a string. Platform\n\ |
| 697 | may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7,\n\ |
| 698 | 3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain\n\ |
| 699 | controller, 3 for a server." |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 700 | ); |
| 701 | |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 702 | static PyTypeObject WindowsVersionType = {0, 0, 0, 0, 0, 0}; |
| 703 | |
| 704 | static PyStructSequence_Field windows_version_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | {"major", "Major version number"}, |
| 706 | {"minor", "Minor version number"}, |
| 707 | {"build", "Build number"}, |
| 708 | {"platform", "Operating system platform"}, |
| 709 | {"service_pack", "Latest Service Pack installed on the system"}, |
| 710 | {"service_pack_major", "Service Pack major version number"}, |
| 711 | {"service_pack_minor", "Service Pack minor version number"}, |
| 712 | {"suite_mask", "Bit mask identifying available product suites"}, |
| 713 | {"product_type", "System product type"}, |
| 714 | {0} |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
| 717 | static PyStructSequence_Desc windows_version_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | "sys.getwindowsversion", /* name */ |
| 719 | getwindowsversion_doc, /* doc */ |
| 720 | windows_version_fields, /* fields */ |
| 721 | 5 /* For backward compatibility, |
| 722 | only the first 5 items are accessible |
| 723 | via indexing, the rest are name only */ |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 724 | }; |
| 725 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 726 | static PyObject * |
| 727 | sys_getwindowsversion(PyObject *self) |
| 728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | PyObject *version; |
| 730 | int pos = 0; |
| 731 | OSVERSIONINFOEX ver; |
| 732 | ver.dwOSVersionInfoSize = sizeof(ver); |
| 733 | if (!GetVersionEx((OSVERSIONINFO*) &ver)) |
| 734 | return PyErr_SetFromWindowsErr(0); |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | version = PyStructSequence_New(&WindowsVersionType); |
| 737 | if (version == NULL) |
| 738 | return NULL; |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMajorVersion)); |
| 741 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwMinorVersion)); |
| 742 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwBuildNumber)); |
| 743 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.dwPlatformId)); |
| 744 | PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion)); |
| 745 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMajor)); |
| 746 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wServicePackMinor)); |
| 747 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wSuiteMask)); |
| 748 | PyStructSequence_SET_ITEM(version, pos++, PyLong_FromLong(ver.wProductType)); |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | return version; |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | #endif /* MS_WINDOWS */ |
| 754 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 755 | #ifdef HAVE_DLOPEN |
| 756 | static PyObject * |
| 757 | sys_setdlopenflags(PyObject *self, PyObject *args) |
| 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | int new_val; |
| 760 | PyThreadState *tstate = PyThreadState_GET(); |
| 761 | if (!PyArg_ParseTuple(args, "i:setdlopenflags", &new_val)) |
| 762 | return NULL; |
| 763 | if (!tstate) |
| 764 | return NULL; |
| 765 | tstate->interp->dlopenflags = new_val; |
| 766 | Py_INCREF(Py_None); |
| 767 | return Py_None; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 770 | PyDoc_STRVAR(setdlopenflags_doc, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 771 | "setdlopenflags(n) -> None\n\ |
| 772 | \n\ |
Alexandre Vassalotti | 260484d | 2009-07-17 11:43:26 +0000 | [diff] [blame] | 773 | Set the flags used by the interpreter for dlopen calls, such as when the\n\ |
| 774 | interpreter loads extension modules. Among other things, this will enable\n\ |
| 775 | a lazy resolving of symbols when importing a module, if called as\n\ |
| 776 | sys.setdlopenflags(0). To share symbols across extension modules, call as\n\ |
| 777 | sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n\ |
Victor Stinner | f4afa43 | 2011-10-31 11:48:09 +0100 | [diff] [blame] | 778 | can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY)."); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 779 | |
| 780 | static PyObject * |
| 781 | sys_getdlopenflags(PyObject *self, PyObject *args) |
| 782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | PyThreadState *tstate = PyThreadState_GET(); |
| 784 | if (!tstate) |
| 785 | return NULL; |
| 786 | return PyLong_FromLong(tstate->interp->dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 789 | PyDoc_STRVAR(getdlopenflags_doc, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 790 | "getdlopenflags() -> int\n\ |
| 791 | \n\ |
Alexandre Vassalotti | 260484d | 2009-07-17 11:43:26 +0000 | [diff] [blame] | 792 | Return the current value of the flags that are used for dlopen calls.\n\ |
| 793 | The flag constants are defined in the ctypes and DLFCN modules."); |
| 794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | #endif /* HAVE_DLOPEN */ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 797 | #ifdef USE_MALLOPT |
| 798 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 799 | #include <malloc.h> |
| 800 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 801 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 802 | sys_mdebug(PyObject *self, PyObject *args) |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | int flag; |
| 805 | if (!PyArg_ParseTuple(args, "i:mdebug", &flag)) |
| 806 | return NULL; |
| 807 | mallopt(M_DEBUG, flag); |
| 808 | Py_INCREF(Py_None); |
| 809 | return Py_None; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 810 | } |
| 811 | #endif /* USE_MALLOPT */ |
| 812 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 813 | static PyObject * |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 814 | sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyObject *res = NULL; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 817 | static PyObject *gc_head_size = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | static char *kwlist[] = {"object", "default", 0}; |
| 819 | PyObject *o, *dflt = NULL; |
| 820 | PyObject *method; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 821 | _Py_IDENTIFIER(__sizeof__); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", |
| 824 | kwlist, &o, &dflt)) |
| 825 | return NULL; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | /* Initialize static variable for GC head size */ |
| 828 | if (gc_head_size == NULL) { |
| 829 | gc_head_size = PyLong_FromSsize_t(sizeof(PyGC_Head)); |
| 830 | if (gc_head_size == NULL) |
| 831 | return NULL; |
| 832 | } |
Benjamin Peterson | a5758c0 | 2009-05-09 18:15:04 +0000 | [diff] [blame] | 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | /* Make sure the type is initialized. float gets initialized late */ |
| 835 | if (PyType_Ready(Py_TYPE(o)) < 0) |
| 836 | return NULL; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 837 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 838 | method = _PyObject_LookupSpecial(o, &PyId___sizeof__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | if (method == NULL) { |
| 840 | if (!PyErr_Occurred()) |
| 841 | PyErr_Format(PyExc_TypeError, |
| 842 | "Type %.100s doesn't define __sizeof__", |
| 843 | Py_TYPE(o)->tp_name); |
| 844 | } |
| 845 | else { |
| 846 | res = PyObject_CallFunctionObjArgs(method, NULL); |
| 847 | Py_DECREF(method); |
| 848 | } |
| 849 | |
| 850 | /* Has a default value been given */ |
| 851 | if ((res == NULL) && (dflt != NULL) && |
| 852 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 853 | { |
| 854 | PyErr_Clear(); |
| 855 | Py_INCREF(dflt); |
| 856 | return dflt; |
| 857 | } |
| 858 | else if (res == NULL) |
| 859 | return res; |
| 860 | |
| 861 | /* add gc_head size */ |
| 862 | if (PyObject_IS_GC(o)) { |
| 863 | PyObject *tmp = res; |
| 864 | res = PyNumber_Add(tmp, gc_head_size); |
| 865 | Py_DECREF(tmp); |
| 866 | } |
| 867 | return res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | PyDoc_STRVAR(getsizeof_doc, |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 871 | "getsizeof(object, default) -> int\n\ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 872 | \n\ |
| 873 | Return the size of object in bytes."); |
| 874 | |
| 875 | static PyObject * |
Fred Drake | a768882 | 2001-10-24 20:47:48 +0000 | [diff] [blame] | 876 | sys_getrefcount(PyObject *self, PyObject *arg) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | return PyLong_FromSsize_t(arg->ob_refcnt); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 881 | #ifdef Py_REF_DEBUG |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 882 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 883 | sys_gettotalrefcount(PyObject *self) |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | return PyLong_FromSsize_t(_Py_GetRefTotal()); |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 886 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 887 | #endif /* Py_REF_DEBUG */ |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 888 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 889 | PyDoc_STRVAR(getrefcount_doc, |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 890 | "getrefcount(object) -> integer\n\ |
| 891 | \n\ |
Fred Drake | ba3ff1b | 2002-06-20 21:36:19 +0000 | [diff] [blame] | 892 | Return the reference count of object. The count returned is generally\n\ |
| 893 | one higher than you might expect, because it includes the (temporary)\n\ |
| 894 | reference as an argument to getrefcount()." |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 895 | ); |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 896 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 897 | #ifdef COUNT_ALLOCS |
| 898 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 899 | sys_getcounts(PyObject *self) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | extern PyObject *get_counts(void); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | return get_counts(); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 904 | } |
| 905 | #endif |
| 906 | |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 907 | PyDoc_STRVAR(getframe_doc, |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 908 | "_getframe([depth]) -> frameobject\n\ |
| 909 | \n\ |
| 910 | Return a frame object from the call stack. If optional integer depth is\n\ |
| 911 | given, return the frame object that many calls below the top of the stack.\n\ |
| 912 | If that is deeper than the call stack, ValueError is raised. The default\n\ |
| 913 | for depth is zero, returning the frame at the top of the call stack.\n\ |
| 914 | \n\ |
| 915 | This function should be used for internal and specialized\n\ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 916 | purposes only." |
| 917 | ); |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 918 | |
| 919 | static PyObject * |
| 920 | sys_getframe(PyObject *self, PyObject *args) |
| 921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | PyFrameObject *f = PyThreadState_GET()->frame; |
| 923 | int depth = -1; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | if (!PyArg_ParseTuple(args, "|i:_getframe", &depth)) |
| 926 | return NULL; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | while (depth > 0 && f != NULL) { |
| 929 | f = f->f_back; |
| 930 | --depth; |
| 931 | } |
| 932 | if (f == NULL) { |
| 933 | PyErr_SetString(PyExc_ValueError, |
| 934 | "call stack is not deep enough"); |
| 935 | return NULL; |
| 936 | } |
| 937 | Py_INCREF(f); |
| 938 | return (PyObject*)f; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 941 | PyDoc_STRVAR(current_frames_doc, |
| 942 | "_current_frames() -> dictionary\n\ |
| 943 | \n\ |
| 944 | Return a dictionary mapping each current thread T's thread id to T's\n\ |
| 945 | current stack frame.\n\ |
| 946 | \n\ |
| 947 | This function should be used for specialized purposes only." |
| 948 | ); |
| 949 | |
| 950 | static PyObject * |
| 951 | sys_current_frames(PyObject *self, PyObject *noargs) |
| 952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | return _PyThread_CurrentFrames(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 956 | PyDoc_STRVAR(call_tracing_doc, |
| 957 | "call_tracing(func, args) -> object\n\ |
| 958 | \n\ |
| 959 | Call func(*args), while tracing is enabled. The tracing state is\n\ |
| 960 | saved, and restored afterwards. This is intended to be called from\n\ |
| 961 | a debugger from a checkpoint, to recursively debug some other code." |
| 962 | ); |
| 963 | |
| 964 | static PyObject * |
| 965 | sys_call_tracing(PyObject *self, PyObject *args) |
| 966 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | PyObject *func, *funcargs; |
| 968 | if (!PyArg_ParseTuple(args, "OO!:call_tracing", &func, &PyTuple_Type, &funcargs)) |
| 969 | return NULL; |
| 970 | return _PyEval_CallTracing(func, funcargs); |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 973 | PyDoc_STRVAR(callstats_doc, |
| 974 | "callstats() -> tuple of integers\n\ |
| 975 | \n\ |
| 976 | Return a tuple of function call statistics, if CALL_PROFILE was defined\n\ |
| 977 | when Python was built. Otherwise, return None.\n\ |
| 978 | \n\ |
| 979 | When enabled, this function returns detailed, implementation-specific\n\ |
| 980 | details about the number of function calls executed. The return value is\n\ |
| 981 | a 11-tuple where the entries in the tuple are counts of:\n\ |
| 982 | 0. all function calls\n\ |
| 983 | 1. calls to PyFunction_Type objects\n\ |
| 984 | 2. PyFunction calls that do not create an argument tuple\n\ |
| 985 | 3. PyFunction calls that do not create an argument tuple\n\ |
| 986 | and bypass PyEval_EvalCodeEx()\n\ |
| 987 | 4. PyMethod calls\n\ |
| 988 | 5. PyMethod calls on bound methods\n\ |
| 989 | 6. PyType calls\n\ |
| 990 | 7. PyCFunction calls\n\ |
| 991 | 8. generator calls\n\ |
| 992 | 9. All other calls\n\ |
| 993 | 10. Number of stack pops performed by call_function()" |
| 994 | ); |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 995 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 996 | #ifdef __cplusplus |
| 997 | extern "C" { |
| 998 | #endif |
| 999 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 1000 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 1001 | /* Defined in objects.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1002 | extern PyObject *_Py_GetObjects(PyObject *, PyObject *); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1003 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 1004 | |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1005 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1006 | /* Defined in ceval.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1007 | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1008 | #endif |
| 1009 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1010 | #ifdef __cplusplus |
| 1011 | } |
| 1012 | #endif |
| 1013 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1014 | static PyObject * |
| 1015 | sys_clear_type_cache(PyObject* self, PyObject* args) |
| 1016 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | PyType_ClearCache(); |
| 1018 | Py_RETURN_NONE; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | PyDoc_STRVAR(sys_clear_type_cache__doc__, |
| 1022 | "_clear_type_cache() -> None\n\ |
| 1023 | Clear the internal type lookup cache."); |
| 1024 | |
| 1025 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1026 | static PyMethodDef sys_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | /* Might as well keep this in alphabetic order */ |
| 1028 | {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, |
| 1029 | callstats_doc}, |
| 1030 | {"_clear_type_cache", sys_clear_type_cache, METH_NOARGS, |
| 1031 | sys_clear_type_cache__doc__}, |
| 1032 | {"_current_frames", sys_current_frames, METH_NOARGS, |
| 1033 | current_frames_doc}, |
| 1034 | {"displayhook", sys_displayhook, METH_O, displayhook_doc}, |
| 1035 | {"exc_info", sys_exc_info, METH_NOARGS, exc_info_doc}, |
| 1036 | {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, |
| 1037 | {"exit", sys_exit, METH_VARARGS, exit_doc}, |
| 1038 | {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, |
| 1039 | METH_NOARGS, getdefaultencoding_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1040 | #ifdef HAVE_DLOPEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | {"getdlopenflags", (PyCFunction)sys_getdlopenflags, METH_NOARGS, |
| 1042 | getdlopenflags_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1043 | #endif |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1044 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | {"getcounts", (PyCFunction)sys_getcounts, METH_NOARGS}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1046 | #endif |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1047 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | {"getdxp", _Py_GetDXProfile, METH_VARARGS}, |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1049 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | {"getfilesystemencoding", (PyCFunction)sys_getfilesystemencoding, |
| 1051 | METH_NOARGS, getfilesystemencoding_doc}, |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 1052 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | {"getobjects", _Py_GetObjects, METH_VARARGS}, |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 1054 | #endif |
| 1055 | #ifdef Py_REF_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1057 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | {"getrefcount", (PyCFunction)sys_getrefcount, METH_O, getrefcount_doc}, |
| 1059 | {"getrecursionlimit", (PyCFunction)sys_getrecursionlimit, METH_NOARGS, |
| 1060 | getrecursionlimit_doc}, |
| 1061 | {"getsizeof", (PyCFunction)sys_getsizeof, |
| 1062 | METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, |
| 1063 | {"_getframe", sys_getframe, METH_VARARGS, getframe_doc}, |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 1064 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | {"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS, |
| 1066 | getwindowsversion_doc}, |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 1067 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | {"intern", sys_intern, METH_VARARGS, intern_doc}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 1069 | #ifdef USE_MALLOPT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | {"mdebug", sys_mdebug, METH_VARARGS}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 1071 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | {"setcheckinterval", sys_setcheckinterval, METH_VARARGS, |
| 1073 | setcheckinterval_doc}, |
| 1074 | {"getcheckinterval", sys_getcheckinterval, METH_NOARGS, |
| 1075 | getcheckinterval_doc}, |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 1076 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | {"setswitchinterval", sys_setswitchinterval, METH_VARARGS, |
| 1078 | setswitchinterval_doc}, |
| 1079 | {"getswitchinterval", sys_getswitchinterval, METH_NOARGS, |
| 1080 | getswitchinterval_doc}, |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 1081 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1082 | #ifdef HAVE_DLOPEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | {"setdlopenflags", sys_setdlopenflags, METH_VARARGS, |
| 1084 | setdlopenflags_doc}, |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1085 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | {"setprofile", sys_setprofile, METH_O, setprofile_doc}, |
| 1087 | {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, |
| 1088 | {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, |
| 1089 | setrecursionlimit_doc}, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1090 | #ifdef WITH_TSC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | {"settscdump", sys_settscdump, METH_VARARGS, settscdump_doc}, |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 1092 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | {"settrace", sys_settrace, METH_O, settrace_doc}, |
| 1094 | {"gettrace", sys_gettrace, METH_NOARGS, gettrace_doc}, |
| 1095 | {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc}, |
| 1096 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1097 | }; |
| 1098 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1099 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1100 | list_builtin_module_names(void) |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | PyObject *list = PyList_New(0); |
| 1103 | int i; |
| 1104 | if (list == NULL) |
| 1105 | return NULL; |
| 1106 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 1107 | PyObject *name = PyUnicode_FromString( |
| 1108 | PyImport_Inittab[i].name); |
| 1109 | if (name == NULL) |
| 1110 | break; |
| 1111 | PyList_Append(list, name); |
| 1112 | Py_DECREF(name); |
| 1113 | } |
| 1114 | if (PyList_Sort(list) != 0) { |
| 1115 | Py_DECREF(list); |
| 1116 | list = NULL; |
| 1117 | } |
| 1118 | if (list) { |
| 1119 | PyObject *v = PyList_AsTuple(list); |
| 1120 | Py_DECREF(list); |
| 1121 | list = v; |
| 1122 | } |
| 1123 | return list; |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1126 | static PyObject *warnoptions = NULL; |
| 1127 | |
| 1128 | void |
| 1129 | PySys_ResetWarnOptions(void) |
| 1130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | if (warnoptions == NULL || !PyList_Check(warnoptions)) |
| 1132 | return; |
| 1133 | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | void |
Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 1137 | PySys_AddWarnOptionUnicode(PyObject *unicode) |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | if (warnoptions == NULL || !PyList_Check(warnoptions)) { |
| 1140 | Py_XDECREF(warnoptions); |
| 1141 | warnoptions = PyList_New(0); |
| 1142 | if (warnoptions == NULL) |
| 1143 | return; |
| 1144 | } |
Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 1145 | PyList_Append(warnoptions, unicode); |
| 1146 | } |
| 1147 | |
| 1148 | void |
| 1149 | PySys_AddWarnOption(const wchar_t *s) |
| 1150 | { |
| 1151 | PyObject *unicode; |
| 1152 | unicode = PyUnicode_FromWideChar(s, -1); |
| 1153 | if (unicode == NULL) |
| 1154 | return; |
| 1155 | PySys_AddWarnOptionUnicode(unicode); |
| 1156 | Py_DECREF(unicode); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1159 | int |
| 1160 | PySys_HasWarnOptions(void) |
| 1161 | { |
| 1162 | return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; |
| 1163 | } |
| 1164 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1165 | static PyObject *xoptions = NULL; |
| 1166 | |
| 1167 | static PyObject * |
| 1168 | get_xoptions(void) |
| 1169 | { |
| 1170 | if (xoptions == NULL || !PyDict_Check(xoptions)) { |
| 1171 | Py_XDECREF(xoptions); |
| 1172 | xoptions = PyDict_New(); |
| 1173 | } |
| 1174 | return xoptions; |
| 1175 | } |
| 1176 | |
| 1177 | void |
| 1178 | PySys_AddXOption(const wchar_t *s) |
| 1179 | { |
| 1180 | PyObject *opts; |
| 1181 | PyObject *name = NULL, *value = NULL; |
| 1182 | const wchar_t *name_end; |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1183 | |
| 1184 | opts = get_xoptions(); |
| 1185 | if (opts == NULL) |
| 1186 | goto error; |
| 1187 | |
| 1188 | name_end = wcschr(s, L'='); |
| 1189 | if (!name_end) { |
| 1190 | name = PyUnicode_FromWideChar(s, -1); |
| 1191 | value = Py_True; |
| 1192 | Py_INCREF(value); |
| 1193 | } |
| 1194 | else { |
| 1195 | name = PyUnicode_FromWideChar(s, name_end - s); |
| 1196 | value = PyUnicode_FromWideChar(name_end + 1, -1); |
| 1197 | } |
| 1198 | if (name == NULL || value == NULL) |
| 1199 | goto error; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1200 | PyDict_SetItem(opts, name, value); |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1201 | Py_DECREF(name); |
| 1202 | Py_DECREF(value); |
| 1203 | return; |
| 1204 | |
| 1205 | error: |
| 1206 | Py_XDECREF(name); |
| 1207 | Py_XDECREF(value); |
| 1208 | /* No return value, therefore clear error state if possible */ |
| 1209 | if (_Py_atomic_load_relaxed(&_PyThreadState_Current)) |
| 1210 | PyErr_Clear(); |
| 1211 | } |
| 1212 | |
| 1213 | PyObject * |
| 1214 | PySys_GetXOptions(void) |
| 1215 | { |
| 1216 | return get_xoptions(); |
| 1217 | } |
| 1218 | |
Guido van Rossum | 40552d0 | 1998-08-06 03:34:39 +0000 | [diff] [blame] | 1219 | /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
| 1220 | Two literals concatenated works just fine. If you have a K&R compiler |
| 1221 | or other abomination that however *does* understand longer strings, |
| 1222 | 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] | 1223 | PyDoc_VAR(sys_doc) = |
| 1224 | PyDoc_STR( |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1225 | "This module provides access to some objects used or maintained by the\n\ |
| 1226 | interpreter and to functions that interact strongly with the interpreter.\n\ |
| 1227 | \n\ |
| 1228 | Dynamic objects:\n\ |
| 1229 | \n\ |
| 1230 | argv -- command line arguments; argv[0] is the script pathname if known\n\ |
| 1231 | path -- module search path; path[0] is the script directory, else ''\n\ |
| 1232 | modules -- dictionary of loaded modules\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1233 | \n\ |
| 1234 | displayhook -- called to show results in an interactive session\n\ |
| 1235 | excepthook -- called to handle any uncaught exception other than SystemExit\n\ |
| 1236 | To customize printing in an interactive session or to install a custom\n\ |
| 1237 | top-level exception handler, assign other functions to replace these.\n\ |
| 1238 | \n\ |
Benjamin Peterson | 06157a4 | 2008-07-15 00:28:36 +0000 | [diff] [blame] | 1239 | stdin -- standard input file object; used by input()\n\ |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 1240 | stdout -- standard output file object; used by print()\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1241 | stderr -- standard error object; used for error messages\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1242 | By assigning other file objects (or objects that behave like files)\n\ |
| 1243 | 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] | 1244 | \n\ |
| 1245 | last_type -- type of last uncaught exception\n\ |
| 1246 | last_value -- value of last uncaught exception\n\ |
| 1247 | last_traceback -- traceback of last uncaught exception\n\ |
| 1248 | These three are only available in an interactive session after a\n\ |
| 1249 | traceback has been printed.\n\ |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 1250 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1251 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1252 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1253 | PyDoc_STR( |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 1254 | "\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1255 | Static objects:\n\ |
| 1256 | \n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 1257 | builtin_module_names -- tuple of module names built into this interpreter\n\ |
| 1258 | copyright -- copyright notice pertaining to this interpreter\n\ |
| 1259 | exec_prefix -- prefix used to find the machine-specific Python library\n\ |
Petri Lehtinen | 4b0eab6 | 2012-02-02 21:23:15 +0200 | [diff] [blame] | 1260 | executable -- absolute path of the executable binary of the Python interpreter\n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 1261 | float_info -- a struct sequence with information about the float implementation.\n\ |
| 1262 | float_repr_style -- string indicating the style of repr() output for floats\n\ |
| 1263 | hexversion -- version information encoded as a single integer\n\ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 1264 | int_info -- a struct sequence with information about the int implementation.\n\ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 1265 | maxsize -- the largest supported length of containers.\n\ |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 1266 | maxunicode -- the value of the largest Unicode codepoint\n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 1267 | platform -- platform identifier\n\ |
| 1268 | prefix -- prefix used to find the Python library\n\ |
| 1269 | thread_info -- a struct sequence with information about the thread implementation.\n\ |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 1270 | version -- the version of this interpreter as a string\n\ |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1271 | version_info -- version information as a named tuple\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1272 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1273 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1274 | #ifdef MS_WINDOWS |
| 1275 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1276 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1277 | "dllhandle -- [Windows only] integer handle of the Python DLL\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1278 | winver -- [Windows only] version number of the Python DLL\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1279 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1280 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1281 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1282 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1283 | "__stdin__ -- the original stdin; don't touch!\n\ |
| 1284 | __stdout__ -- the original stdout; don't touch!\n\ |
| 1285 | __stderr__ -- the original stderr; don't touch!\n\ |
| 1286 | __displayhook__ -- the original displayhook; don't touch!\n\ |
| 1287 | __excepthook__ -- the original excepthook; don't touch!\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1288 | \n\ |
| 1289 | Functions:\n\ |
| 1290 | \n\ |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1291 | displayhook() -- print an object to the screen, and save it in builtins._\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 1292 | excepthook() -- print an exception and its traceback to sys.stderr\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1293 | exc_info() -- return thread-safe information about the current exception\n\ |
| 1294 | exit() -- exit the interpreter by raising SystemExit\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1295 | getdlopenflags() -- returns flags to be used for dlopen() calls\n\ |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 1296 | getprofile() -- get the global profiling function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1297 | getrefcount() -- return the reference count for an object (plus one :-)\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 1298 | getrecursionlimit() -- return the max recursion depth for the interpreter\n\ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 1299 | getsizeof() -- return the size of an object in bytes\n\ |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 1300 | gettrace() -- get the global debug tracing function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1301 | setcheckinterval() -- control how often the interpreter checks for events\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1302 | setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1303 | setprofile() -- set the global profiling function\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 1304 | setrecursionlimit() -- set the max recursion depth for the interpreter\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1305 | settrace() -- set the global debug tracing function\n\ |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 1306 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 1307 | ) |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 1308 | /* end of sys_doc */ ; |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1309 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1310 | |
| 1311 | PyDoc_STRVAR(flags__doc__, |
| 1312 | "sys.flags\n\ |
| 1313 | \n\ |
| 1314 | Flags provided through command line arguments or environment vars."); |
| 1315 | |
| 1316 | static PyTypeObject FlagsType; |
| 1317 | |
| 1318 | static PyStructSequence_Field flags_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | {"debug", "-d"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | {"inspect", "-i"}, |
| 1321 | {"interactive", "-i"}, |
| 1322 | {"optimize", "-O or -OO"}, |
| 1323 | {"dont_write_bytecode", "-B"}, |
| 1324 | {"no_user_site", "-s"}, |
| 1325 | {"no_site", "-S"}, |
| 1326 | {"ignore_environment", "-E"}, |
| 1327 | {"verbose", "-v"}, |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1328 | #ifdef RISCOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | {"riscos_wimp", "???"}, |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1330 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | /* {"unbuffered", "-u"}, */ |
| 1332 | /* {"skip_first", "-x"}, */ |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 1333 | {"bytes_warning", "-b"}, |
| 1334 | {"quiet", "-q"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | {0} |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1336 | }; |
| 1337 | |
| 1338 | static PyStructSequence_Desc flags_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | "sys.flags", /* name */ |
| 1340 | flags__doc__, /* doc */ |
| 1341 | flags_fields, /* fields */ |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1342 | #ifdef RISCOS |
Raymond Hettinger | 90e8f8c | 2011-01-05 20:08:25 +0000 | [diff] [blame] | 1343 | 12 |
Éric Araujo | be3bd57 | 2011-03-26 01:55:15 +0100 | [diff] [blame] | 1344 | #else |
| 1345 | 11 |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1346 | #endif |
| 1347 | }; |
| 1348 | |
| 1349 | static PyObject* |
| 1350 | make_flags(void) |
| 1351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | int pos = 0; |
| 1353 | PyObject *seq; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | seq = PyStructSequence_New(&FlagsType); |
| 1356 | if (seq == NULL) |
| 1357 | return NULL; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1358 | |
| 1359 | #define SetFlag(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | SetFlag(Py_DebugFlag); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | SetFlag(Py_InspectFlag); |
| 1364 | SetFlag(Py_InteractiveFlag); |
| 1365 | SetFlag(Py_OptimizeFlag); |
| 1366 | SetFlag(Py_DontWriteBytecodeFlag); |
| 1367 | SetFlag(Py_NoUserSiteDirectory); |
| 1368 | SetFlag(Py_NoSiteFlag); |
| 1369 | SetFlag(Py_IgnoreEnvironmentFlag); |
| 1370 | SetFlag(Py_VerboseFlag); |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1371 | #ifdef RISCOS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | SetFlag(Py_RISCOSWimpFlag); |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | /* SetFlag(saw_unbuffered_flag); */ |
| 1375 | /* SetFlag(skipfirstline); */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1376 | SetFlag(Py_BytesWarningFlag); |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 1377 | SetFlag(Py_QuietFlag); |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1378 | #undef SetFlag |
| 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | if (PyErr_Occurred()) { |
| 1381 | return NULL; |
| 1382 | } |
| 1383 | return seq; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1386 | PyDoc_STRVAR(version_info__doc__, |
| 1387 | "sys.version_info\n\ |
| 1388 | \n\ |
| 1389 | Version information as a named tuple."); |
| 1390 | |
| 1391 | static PyTypeObject VersionInfoType; |
| 1392 | |
| 1393 | static PyStructSequence_Field version_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | {"major", "Major release number"}, |
| 1395 | {"minor", "Minor release number"}, |
| 1396 | {"micro", "Patch release number"}, |
| 1397 | {"releaselevel", "'alpha', 'beta', 'candidate', or 'release'"}, |
| 1398 | {"serial", "Serial release number"}, |
| 1399 | {0} |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1400 | }; |
| 1401 | |
| 1402 | static PyStructSequence_Desc version_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | "sys.version_info", /* name */ |
| 1404 | version_info__doc__, /* doc */ |
| 1405 | version_info_fields, /* fields */ |
| 1406 | 5 |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1407 | }; |
| 1408 | |
| 1409 | static PyObject * |
| 1410 | make_version_info(void) |
| 1411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | PyObject *version_info; |
| 1413 | char *s; |
| 1414 | int pos = 0; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | version_info = PyStructSequence_New(&VersionInfoType); |
| 1417 | if (version_info == NULL) { |
| 1418 | return NULL; |
| 1419 | } |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | /* |
| 1422 | * These release level checks are mutually exclusive and cover |
| 1423 | * the field, so don't get too fancy with the pre-processor! |
| 1424 | */ |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1425 | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | s = "alpha"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1427 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | s = "beta"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1429 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | s = "candidate"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1431 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | s = "final"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1433 | #endif |
| 1434 | |
| 1435 | #define SetIntItem(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1437 | #define SetStrItem(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag)) |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | SetIntItem(PY_MAJOR_VERSION); |
| 1441 | SetIntItem(PY_MINOR_VERSION); |
| 1442 | SetIntItem(PY_MICRO_VERSION); |
| 1443 | SetStrItem(s); |
| 1444 | SetIntItem(PY_RELEASE_SERIAL); |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1445 | #undef SetIntItem |
| 1446 | #undef SetStrItem |
| 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (PyErr_Occurred()) { |
| 1449 | Py_CLEAR(version_info); |
| 1450 | return NULL; |
| 1451 | } |
| 1452 | return version_info; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1455 | static struct PyModuleDef sysmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | PyModuleDef_HEAD_INIT, |
| 1457 | "sys", |
| 1458 | sys_doc, |
| 1459 | -1, /* multiple "initialization" just copies the module dict. */ |
| 1460 | sys_methods, |
| 1461 | NULL, |
| 1462 | NULL, |
| 1463 | NULL, |
| 1464 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1465 | }; |
| 1466 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1467 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1468 | _PySys_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | PyObject *m, *v, *sysdict; |
| 1471 | char *s; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | m = PyModule_Create(&sysmodule); |
| 1474 | if (m == NULL) |
| 1475 | return NULL; |
| 1476 | sysdict = PyModule_GetDict(m); |
| 1477 | #define SET_SYS_FROM_STRING(key, value) \ |
| 1478 | v = value; \ |
| 1479 | if (v != NULL) \ |
| 1480 | PyDict_SetItemString(sysdict, key, v); \ |
| 1481 | Py_XDECREF(v) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | /* Check that stdin is not a directory |
| 1484 | Using shell redirection, you can redirect stdin to a directory, |
| 1485 | crashing the Python interpreter. Catch this common mistake here |
| 1486 | and output a useful error message. Note that under MS Windows, |
| 1487 | the shell already prevents that. */ |
Martin v. Löwis | ec59d04 | 2009-01-12 07:59:10 +0000 | [diff] [blame] | 1488 | #if !defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | { |
| 1490 | struct stat sb; |
| 1491 | if (fstat(fileno(stdin), &sb) == 0 && |
| 1492 | S_ISDIR(sb.st_mode)) { |
| 1493 | /* There's nothing more we can do. */ |
| 1494 | /* Py_FatalError() will core dump, so just exit. */ |
| 1495 | PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); |
| 1496 | exit(EXIT_FAILURE); |
| 1497 | } |
| 1498 | } |
Martin v. Löwis | ec59d04 | 2009-01-12 07:59:10 +0000 | [diff] [blame] | 1499 | #endif |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | /* stdin/stdout/stderr are now set by pythonrun.c */ |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | PyDict_SetItemString(sysdict, "__displayhook__", |
| 1504 | PyDict_GetItemString(sysdict, "displayhook")); |
| 1505 | PyDict_SetItemString(sysdict, "__excepthook__", |
| 1506 | PyDict_GetItemString(sysdict, "excepthook")); |
| 1507 | SET_SYS_FROM_STRING("version", |
| 1508 | PyUnicode_FromString(Py_GetVersion())); |
| 1509 | SET_SYS_FROM_STRING("hexversion", |
| 1510 | PyLong_FromLong(PY_VERSION_HEX)); |
Georg Brandl | 1ca2e79 | 2011-03-05 20:51:24 +0100 | [diff] [blame] | 1511 | SET_SYS_FROM_STRING("_mercurial", |
| 1512 | Py_BuildValue("(szz)", "CPython", _Py_hgidentifier(), |
| 1513 | _Py_hgversion())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | SET_SYS_FROM_STRING("dont_write_bytecode", |
| 1515 | PyBool_FromLong(Py_DontWriteBytecodeFlag)); |
| 1516 | SET_SYS_FROM_STRING("api_version", |
| 1517 | PyLong_FromLong(PYTHON_API_VERSION)); |
| 1518 | SET_SYS_FROM_STRING("copyright", |
| 1519 | PyUnicode_FromString(Py_GetCopyright())); |
| 1520 | SET_SYS_FROM_STRING("platform", |
| 1521 | PyUnicode_FromString(Py_GetPlatform())); |
| 1522 | SET_SYS_FROM_STRING("executable", |
| 1523 | PyUnicode_FromWideChar( |
| 1524 | Py_GetProgramFullPath(), -1)); |
| 1525 | SET_SYS_FROM_STRING("prefix", |
| 1526 | PyUnicode_FromWideChar(Py_GetPrefix(), -1)); |
| 1527 | SET_SYS_FROM_STRING("exec_prefix", |
| 1528 | PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); |
| 1529 | SET_SYS_FROM_STRING("maxsize", |
| 1530 | PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 1531 | SET_SYS_FROM_STRING("float_info", |
| 1532 | PyFloat_GetInfo()); |
| 1533 | SET_SYS_FROM_STRING("int_info", |
| 1534 | PyLong_GetInfo()); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 1535 | /* initialize hash_info */ |
| 1536 | if (Hash_InfoType.tp_name == 0) |
| 1537 | PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc); |
| 1538 | SET_SYS_FROM_STRING("hash_info", |
| 1539 | get_hash_info()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | SET_SYS_FROM_STRING("maxunicode", |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 1541 | PyLong_FromLong(0x10FFFF)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | SET_SYS_FROM_STRING("builtin_module_names", |
| 1543 | list_builtin_module_names()); |
| 1544 | { |
| 1545 | /* Assumes that longs are at least 2 bytes long. |
| 1546 | Should be safe! */ |
| 1547 | unsigned long number = 1; |
| 1548 | char *value; |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | s = (char *) &number; |
| 1551 | if (s[0] == 0) |
| 1552 | value = "big"; |
| 1553 | else |
| 1554 | value = "little"; |
| 1555 | SET_SYS_FROM_STRING("byteorder", |
| 1556 | PyUnicode_FromString(value)); |
| 1557 | } |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 1558 | #ifdef MS_COREDLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | SET_SYS_FROM_STRING("dllhandle", |
| 1560 | PyLong_FromVoidPtr(PyWin_DLLhModule)); |
| 1561 | SET_SYS_FROM_STRING("winver", |
| 1562 | PyUnicode_FromString(PyWin_DLLVersionString)); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 1563 | #endif |
Barry Warsaw | 8cf4eae | 2010-10-16 01:04:07 +0000 | [diff] [blame] | 1564 | #ifdef ABIFLAGS |
| 1565 | SET_SYS_FROM_STRING("abiflags", |
| 1566 | PyUnicode_FromString(ABIFLAGS)); |
| 1567 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (warnoptions == NULL) { |
| 1569 | warnoptions = PyList_New(0); |
| 1570 | } |
| 1571 | else { |
| 1572 | Py_INCREF(warnoptions); |
| 1573 | } |
| 1574 | if (warnoptions != NULL) { |
| 1575 | PyDict_SetItemString(sysdict, "warnoptions", warnoptions); |
| 1576 | } |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1577 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1578 | v = get_xoptions(); |
| 1579 | if (v != NULL) { |
| 1580 | PyDict_SetItemString(sysdict, "_xoptions", v); |
| 1581 | } |
| 1582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | /* version_info */ |
| 1584 | if (VersionInfoType.tp_name == 0) |
| 1585 | PyStructSequence_InitType(&VersionInfoType, &version_info_desc); |
| 1586 | SET_SYS_FROM_STRING("version_info", make_version_info()); |
| 1587 | /* prevent user from creating new instances */ |
| 1588 | VersionInfoType.tp_init = NULL; |
| 1589 | VersionInfoType.tp_new = NULL; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | /* flags */ |
| 1592 | if (FlagsType.tp_name == 0) |
| 1593 | PyStructSequence_InitType(&FlagsType, &flags_desc); |
| 1594 | SET_SYS_FROM_STRING("flags", make_flags()); |
| 1595 | /* prevent user from creating new instances */ |
| 1596 | FlagsType.tp_init = NULL; |
| 1597 | FlagsType.tp_new = NULL; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 1598 | |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 1599 | |
| 1600 | #if defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | /* getwindowsversion */ |
| 1602 | if (WindowsVersionType.tp_name == 0) |
| 1603 | PyStructSequence_InitType(&WindowsVersionType, &windows_version_desc); |
| 1604 | /* prevent user from creating new instances */ |
| 1605 | WindowsVersionType.tp_init = NULL; |
| 1606 | WindowsVersionType.tp_new = NULL; |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 1607 | #endif |
| 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 1610 | #ifndef PY_NO_SHORT_FLOAT_REPR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | SET_SYS_FROM_STRING("float_repr_style", |
| 1612 | PyUnicode_FromString("short")); |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 1613 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | SET_SYS_FROM_STRING("float_repr_style", |
| 1615 | PyUnicode_FromString("legacy")); |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 1616 | #endif |
| 1617 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 1618 | #ifdef WITH_THREAD |
| 1619 | SET_SYS_FROM_STRING("thread_info", PyThread_GetInfo()); |
| 1620 | #endif |
| 1621 | |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 1622 | #undef SET_SYS_FROM_STRING |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | if (PyErr_Occurred()) |
| 1624 | return NULL; |
| 1625 | return m; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1628 | static PyObject * |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1629 | makepathobject(const wchar_t *path, wchar_t delim) |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 1630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | int i, n; |
| 1632 | const wchar_t *p; |
| 1633 | PyObject *v, *w; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 1634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | n = 1; |
| 1636 | p = path; |
| 1637 | while ((p = wcschr(p, delim)) != NULL) { |
| 1638 | n++; |
| 1639 | p++; |
| 1640 | } |
| 1641 | v = PyList_New(n); |
| 1642 | if (v == NULL) |
| 1643 | return NULL; |
| 1644 | for (i = 0; ; i++) { |
| 1645 | p = wcschr(path, delim); |
| 1646 | if (p == NULL) |
| 1647 | p = path + wcslen(path); /* End of string */ |
| 1648 | w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); |
| 1649 | if (w == NULL) { |
| 1650 | Py_DECREF(v); |
| 1651 | return NULL; |
| 1652 | } |
| 1653 | PyList_SetItem(v, i, w); |
| 1654 | if (*p == '\0') |
| 1655 | break; |
| 1656 | path = p+1; |
| 1657 | } |
| 1658 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1662 | PySys_SetPath(const wchar_t *path) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | PyObject *v; |
| 1665 | if ((v = makepathobject(path, DELIM)) == NULL) |
| 1666 | Py_FatalError("can't create sys.path"); |
| 1667 | if (PySys_SetObject("path", v) != 0) |
| 1668 | Py_FatalError("can't assign sys.path"); |
| 1669 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1672 | static PyObject * |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1673 | makeargvobject(int argc, wchar_t **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | PyObject *av; |
| 1676 | if (argc <= 0 || argv == NULL) { |
| 1677 | /* Ensure at least one (empty) argument is seen */ |
| 1678 | static wchar_t *empty_argv[1] = {L""}; |
| 1679 | argv = empty_argv; |
| 1680 | argc = 1; |
| 1681 | } |
| 1682 | av = PyList_New(argc); |
| 1683 | if (av != NULL) { |
| 1684 | int i; |
| 1685 | for (i = 0; i < argc; i++) { |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1686 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | PyObject *v; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | /* argv[0] is the script pathname if known */ |
| 1690 | if (i == 0) { |
| 1691 | char* fn = decc$translate_vms(argv[0]); |
| 1692 | if ((fn == (char *)0) || fn == (char *)-1) |
| 1693 | v = PyUnicode_FromString(argv[0]); |
| 1694 | else |
| 1695 | v = PyUnicode_FromString( |
| 1696 | decc$translate_vms(argv[0])); |
| 1697 | } else |
| 1698 | v = PyUnicode_FromString(argv[i]); |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1699 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | PyObject *v = PyUnicode_FromWideChar(argv[i], -1); |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 1701 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | if (v == NULL) { |
| 1703 | Py_DECREF(av); |
| 1704 | av = NULL; |
| 1705 | break; |
| 1706 | } |
| 1707 | PyList_SetItem(av, i, v); |
| 1708 | } |
| 1709 | } |
| 1710 | return av; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 1713 | #define _HAVE_SCRIPT_ARGUMENT(argc, argv) \ |
| 1714 | (argc > 0 && argv0 != NULL && \ |
| 1715 | wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0) |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1716 | |
| 1717 | static void |
| 1718 | sys_update_path(int argc, wchar_t **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1719 | { |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1720 | wchar_t *argv0; |
| 1721 | wchar_t *p = NULL; |
| 1722 | Py_ssize_t n = 0; |
| 1723 | PyObject *a; |
| 1724 | PyObject *path; |
| 1725 | #ifdef HAVE_READLINK |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1726 | wchar_t link[MAXPATHLEN+1]; |
| 1727 | wchar_t argv0copy[2*MAXPATHLEN+1]; |
| 1728 | int nr = 0; |
| 1729 | #endif |
Guido van Rossum | 162e38c | 2003-02-19 15:25:10 +0000 | [diff] [blame] | 1730 | #if defined(HAVE_REALPATH) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | wchar_t fullpath[MAXPATHLEN]; |
Martin v. Löwis | ec59d04 | 2009-01-12 07:59:10 +0000 | [diff] [blame] | 1732 | #elif defined(MS_WINDOWS) && !defined(MS_WINCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | wchar_t fullpath[MAX_PATH]; |
Thomas Heller | 27bb71e | 2003-01-08 14:33:48 +0000 | [diff] [blame] | 1734 | #endif |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1735 | |
| 1736 | path = PySys_GetObject("path"); |
| 1737 | if (path == NULL) |
| 1738 | return; |
| 1739 | |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1740 | argv0 = argv[0]; |
| 1741 | |
| 1742 | #ifdef HAVE_READLINK |
| 1743 | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) |
| 1744 | nr = _Py_wreadlink(argv0, link, MAXPATHLEN); |
| 1745 | if (nr > 0) { |
| 1746 | /* It's a symlink */ |
| 1747 | link[nr] = '\0'; |
| 1748 | if (link[0] == SEP) |
| 1749 | argv0 = link; /* Link to absolute path */ |
| 1750 | else if (wcschr(link, SEP) == NULL) |
| 1751 | ; /* Link without path */ |
| 1752 | else { |
| 1753 | /* Must join(dirname(argv0), link) */ |
| 1754 | wchar_t *q = wcsrchr(argv0, SEP); |
| 1755 | if (q == NULL) |
| 1756 | argv0 = link; /* argv0 without path */ |
| 1757 | else { |
| 1758 | /* Must make a copy */ |
| 1759 | wcscpy(argv0copy, argv0); |
| 1760 | q = wcsrchr(argv0copy, SEP); |
| 1761 | wcscpy(q+1, link); |
| 1762 | argv0 = argv0copy; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | #endif /* HAVE_READLINK */ |
| 1767 | #if SEP == '\\' /* Special case for MS filename syntax */ |
| 1768 | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) { |
| 1769 | wchar_t *q; |
| 1770 | #if defined(MS_WINDOWS) && !defined(MS_WINCE) |
| 1771 | /* This code here replaces the first element in argv with the full |
| 1772 | path that it represents. Under CE, there are no relative paths so |
| 1773 | the argument must be the full path anyway. */ |
| 1774 | wchar_t *ptemp; |
| 1775 | if (GetFullPathNameW(argv0, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1776 | Py_ARRAY_LENGTH(fullpath), |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1777 | fullpath, |
| 1778 | &ptemp)) { |
| 1779 | argv0 = fullpath; |
| 1780 | } |
| 1781 | #endif |
| 1782 | p = wcsrchr(argv0, SEP); |
| 1783 | /* Test for alternate separator */ |
| 1784 | q = wcsrchr(p ? p : argv0, '/'); |
| 1785 | if (q != NULL) |
| 1786 | p = q; |
| 1787 | if (p != NULL) { |
| 1788 | n = p + 1 - argv0; |
| 1789 | if (n > 1 && p[-1] != ':') |
| 1790 | n--; /* Drop trailing separator */ |
| 1791 | } |
| 1792 | } |
| 1793 | #else /* All other filename syntaxes */ |
| 1794 | if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) { |
| 1795 | #if defined(HAVE_REALPATH) |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1796 | if (_Py_wrealpath(argv0, fullpath, PATH_MAX)) { |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1797 | argv0 = fullpath; |
| 1798 | } |
| 1799 | #endif |
| 1800 | p = wcsrchr(argv0, SEP); |
| 1801 | } |
| 1802 | if (p != NULL) { |
| 1803 | n = p + 1 - argv0; |
| 1804 | #if SEP == '/' /* Special case for Unix filename syntax */ |
| 1805 | if (n > 1) |
| 1806 | n--; /* Drop trailing separator */ |
| 1807 | #endif /* Unix */ |
| 1808 | } |
| 1809 | #endif /* All others */ |
| 1810 | a = PyUnicode_FromWideChar(argv0, n); |
| 1811 | if (a == NULL) |
| 1812 | Py_FatalError("no mem for sys.path insertion"); |
| 1813 | if (PyList_Insert(path, 0, a) < 0) |
| 1814 | Py_FatalError("sys.path.insert(0) failed"); |
| 1815 | Py_DECREF(a); |
| 1816 | } |
| 1817 | |
| 1818 | void |
| 1819 | PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) |
| 1820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | PyObject *av = makeargvobject(argc, argv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | if (av == NULL) |
| 1823 | Py_FatalError("no mem for sys.argv"); |
| 1824 | if (PySys_SetObject("argv", av) != 0) |
| 1825 | Py_FatalError("can't assign sys.argv"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | Py_DECREF(av); |
Victor Stinner | c08ec9f | 2010-10-06 22:44:06 +0000 | [diff] [blame] | 1827 | if (updatepath) |
| 1828 | sys_update_path(argc, argv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1829 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1830 | |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 1831 | void |
| 1832 | PySys_SetArgv(int argc, wchar_t **argv) |
| 1833 | { |
| 1834 | PySys_SetArgvEx(argc, argv, 1); |
| 1835 | } |
| 1836 | |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1837 | /* Reimplementation of PyFile_WriteString() no calling indirectly |
| 1838 | PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ |
| 1839 | |
| 1840 | static int |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1841 | sys_pyfile_write_unicode(PyObject *unicode, PyObject *file) |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1842 | { |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1843 | PyObject *writer = NULL, *args = NULL, *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | int err; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1845 | _Py_IDENTIFIER(write); |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1846 | |
Victor Stinner | ecccc4f | 2010-06-08 20:46:00 +0000 | [diff] [blame] | 1847 | if (file == NULL) |
| 1848 | return -1; |
| 1849 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1850 | writer = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | if (writer == NULL) |
| 1852 | goto error; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | args = PyTuple_Pack(1, unicode); |
| 1855 | if (args == NULL) |
| 1856 | goto error; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | result = PyEval_CallObject(writer, args); |
| 1859 | if (result == NULL) { |
| 1860 | goto error; |
| 1861 | } else { |
| 1862 | err = 0; |
| 1863 | goto finally; |
| 1864 | } |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1865 | |
| 1866 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | err = -1; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1868 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | Py_XDECREF(writer); |
| 1870 | Py_XDECREF(args); |
| 1871 | Py_XDECREF(result); |
| 1872 | return err; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1875 | static int |
| 1876 | sys_pyfile_write(const char *text, PyObject *file) |
| 1877 | { |
| 1878 | PyObject *unicode = NULL; |
| 1879 | int err; |
| 1880 | |
| 1881 | if (file == NULL) |
| 1882 | return -1; |
| 1883 | |
| 1884 | unicode = PyUnicode_FromString(text); |
| 1885 | if (unicode == NULL) |
| 1886 | return -1; |
| 1887 | |
| 1888 | err = sys_pyfile_write_unicode(unicode, file); |
| 1889 | Py_DECREF(unicode); |
| 1890 | return err; |
| 1891 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1892 | |
| 1893 | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
| 1894 | Adapted from code submitted by Just van Rossum. |
| 1895 | |
| 1896 | PySys_WriteStdout(format, ...) |
| 1897 | PySys_WriteStderr(format, ...) |
| 1898 | |
| 1899 | The first function writes to sys.stdout; the second to sys.stderr. When |
| 1900 | 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] | 1901 | no exceptions are raised. |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1902 | |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1903 | PyErr_CheckSignals() is not called to avoid the execution of the Python |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1904 | signal handlers: they may raise a new exception whereas sys_write() |
| 1905 | ignores all exceptions. |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 1906 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1907 | Both take a printf-style format string as their first argument followed |
| 1908 | by a variable length argument list determined by the format string. |
| 1909 | |
| 1910 | *** WARNING *** |
| 1911 | |
| 1912 | The format should limit the total size of the formatted output string to |
| 1913 | 1000 bytes. In particular, this means that no unrestricted "%s" formats |
| 1914 | should occur; these should be limited using "%.<N>s where <N> is a |
| 1915 | decimal number calculated so that <N> plus the maximum size of other |
| 1916 | formatted text does not exceed 1000 bytes. Also watch out for "%f", |
| 1917 | which can print hundreds of digits for very large numbers. |
| 1918 | |
| 1919 | */ |
| 1920 | |
| 1921 | static void |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1922 | sys_write(char *name, FILE *fp, const char *format, va_list va) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | PyObject *file; |
| 1925 | PyObject *error_type, *error_value, *error_traceback; |
| 1926 | char buffer[1001]; |
| 1927 | int written; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 1930 | file = PySys_GetObject(name); |
| 1931 | written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); |
| 1932 | if (sys_pyfile_write(buffer, file) != 0) { |
| 1933 | PyErr_Clear(); |
| 1934 | fputs(buffer, fp); |
| 1935 | } |
| 1936 | if (written < 0 || (size_t)written >= sizeof(buffer)) { |
| 1937 | const char *truncated = "... truncated"; |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1938 | if (sys_pyfile_write(truncated, file) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | fputs(truncated, fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | } |
| 1941 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1945 | PySys_WriteStdout(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | va_list va; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | va_start(va, format); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1950 | sys_write("stdout", stdout, format, va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | va_end(va); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1955 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | va_list va; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | va_start(va, format); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 1960 | sys_write("stderr", stderr, format, va); |
| 1961 | va_end(va); |
| 1962 | } |
| 1963 | |
| 1964 | static void |
| 1965 | sys_format(char *name, FILE *fp, const char *format, va_list va) |
| 1966 | { |
| 1967 | PyObject *file, *message; |
| 1968 | PyObject *error_type, *error_value, *error_traceback; |
| 1969 | char *utf8; |
| 1970 | |
| 1971 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 1972 | file = PySys_GetObject(name); |
| 1973 | message = PyUnicode_FromFormatV(format, va); |
| 1974 | if (message != NULL) { |
| 1975 | if (sys_pyfile_write_unicode(message, file) != 0) { |
| 1976 | PyErr_Clear(); |
| 1977 | utf8 = _PyUnicode_AsString(message); |
| 1978 | if (utf8 != NULL) |
| 1979 | fputs(utf8, fp); |
| 1980 | } |
| 1981 | Py_DECREF(message); |
| 1982 | } |
| 1983 | PyErr_Restore(error_type, error_value, error_traceback); |
| 1984 | } |
| 1985 | |
| 1986 | void |
| 1987 | PySys_FormatStdout(const char *format, ...) |
| 1988 | { |
| 1989 | va_list va; |
| 1990 | |
| 1991 | va_start(va, format); |
| 1992 | sys_format("stdout", stdout, format, va); |
| 1993 | va_end(va); |
| 1994 | } |
| 1995 | |
| 1996 | void |
| 1997 | PySys_FormatStderr(const char *format, ...) |
| 1998 | { |
| 1999 | va_list va; |
| 2000 | |
| 2001 | va_start(va, format); |
| 2002 | sys_format("stderr", stderr, format, va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | va_end(va); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2004 | } |