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