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