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