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