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