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