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