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