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; |
| 1129 | OSVERSIONINFOEX 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); |
| 1137 | if (!GetVersionEx((OSVERSIONINFO*) &ver)) |
| 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)); |
| 1148 | PyStructSequence_SET_ITEM(version, pos++, PyUnicode_FromString(ver.szCSDVersion)); |
| 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. |
| 1162 | hKernel32 = GetModuleHandleW(L"kernel32.dll"); |
| 1163 | if (hKernel32 && GetModuleFileNameW(hKernel32, kernel32_path, MAX_PATH) && |
| 1164 | (verblock_size = GetFileVersionInfoSizeW(kernel32_path, NULL)) && |
| 1165 | (verblock = PyMem_RawMalloc(verblock_size))) { |
| 1166 | VS_FIXEDFILEINFO *ffi; |
| 1167 | UINT ffi_len; |
| 1168 | |
| 1169 | if (GetFileVersionInfoW(kernel32_path, 0, verblock_size, verblock) && |
| 1170 | VerQueryValueW(verblock, L"", (LPVOID)&ffi, &ffi_len)) { |
| 1171 | realMajor = HIWORD(ffi->dwProductVersionMS); |
| 1172 | realMinor = LOWORD(ffi->dwProductVersionMS); |
| 1173 | realBuild = HIWORD(ffi->dwProductVersionLS); |
| 1174 | } |
| 1175 | PyMem_RawFree(verblock); |
| 1176 | } |
Segev Finer | 48fb766 | 2017-06-04 20:52:27 +0300 | [diff] [blame] | 1177 | PyStructSequence_SET_ITEM(version, pos++, Py_BuildValue("(kkk)", |
| 1178 | realMajor, |
| 1179 | realMinor, |
| 1180 | realBuild |
Steve Dower | 74f4af7 | 2016-09-17 17:27:48 -0700 | [diff] [blame] | 1181 | )); |
| 1182 | |
Serhiy Storchaka | 48d761e | 2013-12-17 15:11:24 +0200 | [diff] [blame] | 1183 | if (PyErr_Occurred()) { |
| 1184 | Py_DECREF(version); |
| 1185 | return NULL; |
| 1186 | } |
Steve Dower | 74f4af7 | 2016-09-17 17:27:48 -0700 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | return version; |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 1191 | #pragma warning(pop) |
| 1192 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1193 | /*[clinic input] |
| 1194 | sys._enablelegacywindowsfsencoding |
| 1195 | |
| 1196 | Changes the default filesystem encoding to mbcs:replace. |
| 1197 | |
| 1198 | This is done for consistency with earlier versions of Python. See PEP |
| 1199 | 529 for more information. |
| 1200 | |
| 1201 | This is equivalent to defining the PYTHONLEGACYWINDOWSFSENCODING |
| 1202 | environment variable before launching Python. |
| 1203 | [clinic start generated code]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1204 | |
| 1205 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1206 | sys__enablelegacywindowsfsencoding_impl(PyObject *module) |
| 1207 | /*[clinic end generated code: output=f5c3855b45e24fe9 input=2bfa931a20704492]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1208 | { |
Victor Stinner | b2457ef | 2018-08-29 13:25:36 +0200 | [diff] [blame] | 1209 | PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE(); |
| 1210 | _PyCoreConfig *config = &interp->core_config; |
| 1211 | |
| 1212 | /* Set the filesystem encoding to mbcs/replace (PEP 529) */ |
| 1213 | char *encoding = _PyMem_RawStrdup("mbcs"); |
| 1214 | char *errors = _PyMem_RawStrdup("replace"); |
| 1215 | if (encoding == NULL || errors == NULL) { |
| 1216 | PyMem_Free(encoding); |
| 1217 | PyMem_Free(errors); |
| 1218 | PyErr_NoMemory(); |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | |
| 1222 | PyMem_RawFree(config->filesystem_encoding); |
| 1223 | config->filesystem_encoding = encoding; |
| 1224 | PyMem_RawFree(config->filesystem_errors); |
| 1225 | config->filesystem_errors = errors; |
| 1226 | |
| 1227 | if (_Py_SetFileSystemEncoding(config->filesystem_encoding, |
| 1228 | config->filesystem_errors) < 0) { |
| 1229 | PyErr_NoMemory(); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1233 | Py_RETURN_NONE; |
| 1234 | } |
| 1235 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 1236 | #endif /* MS_WINDOWS */ |
| 1237 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1238 | #ifdef HAVE_DLOPEN |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1239 | |
| 1240 | /*[clinic input] |
| 1241 | sys.setdlopenflags |
| 1242 | |
| 1243 | flags as new_val: int |
| 1244 | / |
| 1245 | |
| 1246 | Set the flags used by the interpreter for dlopen calls. |
| 1247 | |
| 1248 | This is used, for example, when the interpreter loads extension |
| 1249 | modules. Among other things, this will enable a lazy resolving of |
| 1250 | symbols when importing a module, if called as sys.setdlopenflags(0). |
| 1251 | To share symbols across extension modules, call as |
| 1252 | sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag |
| 1253 | modules can be found in the os module (RTLD_xxx constants, e.g. |
| 1254 | os.RTLD_LAZY). |
| 1255 | [clinic start generated code]*/ |
| 1256 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1257 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1258 | sys_setdlopenflags_impl(PyObject *module, int new_val) |
| 1259 | /*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1260 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1261 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
| 1262 | interp->dlopenflags = new_val; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1263 | Py_RETURN_NONE; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1266 | |
| 1267 | /*[clinic input] |
| 1268 | sys.getdlopenflags |
| 1269 | |
| 1270 | Return the current value of the flags that are used for dlopen calls. |
| 1271 | |
| 1272 | The flag constants are defined in the os module. |
| 1273 | [clinic start generated code]*/ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1274 | |
| 1275 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1276 | sys_getdlopenflags_impl(PyObject *module) |
| 1277 | /*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1278 | { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 1279 | PyInterpreterState *interp = _PyInterpreterState_Get(); |
| 1280 | return PyLong_FromLong(interp->dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | #endif /* HAVE_DLOPEN */ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 1284 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 1285 | #ifdef USE_MALLOPT |
| 1286 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 1287 | #include <malloc.h> |
| 1288 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1289 | /*[clinic input] |
| 1290 | sys.mdebug |
| 1291 | |
| 1292 | flag: int |
| 1293 | / |
| 1294 | [clinic start generated code]*/ |
| 1295 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1296 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1297 | sys_mdebug_impl(PyObject *module, int flag) |
| 1298 | /*[clinic end generated code: output=5431d545847c3637 input=151d150ae1636f8a]*/ |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 1299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | int flag; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | mallopt(M_DEBUG, flag); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1302 | Py_RETURN_NONE; |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 1303 | } |
| 1304 | #endif /* USE_MALLOPT */ |
| 1305 | |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1306 | size_t |
| 1307 | _PySys_GetSizeOf(PyObject *o) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 1308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | PyObject *res = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | PyObject *method; |
Serhiy Storchaka | 030e92d | 2014-11-15 13:21:37 +0200 | [diff] [blame] | 1311 | Py_ssize_t size; |
Benjamin Peterson | a5758c0 | 2009-05-09 18:15:04 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | /* Make sure the type is initialized. float gets initialized late */ |
| 1314 | if (PyType_Ready(Py_TYPE(o)) < 0) |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1315 | return (size_t)-1; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1316 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1317 | method = _PyObject_LookupSpecial(o, &PyId___sizeof__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | if (method == NULL) { |
| 1319 | if (!PyErr_Occurred()) |
| 1320 | PyErr_Format(PyExc_TypeError, |
| 1321 | "Type %.100s doesn't define __sizeof__", |
| 1322 | Py_TYPE(o)->tp_name); |
| 1323 | } |
| 1324 | else { |
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 1325 | res = _PyObject_CallNoArg(method); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | Py_DECREF(method); |
| 1327 | } |
| 1328 | |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1329 | if (res == NULL) |
| 1330 | return (size_t)-1; |
| 1331 | |
Serhiy Storchaka | 030e92d | 2014-11-15 13:21:37 +0200 | [diff] [blame] | 1332 | size = PyLong_AsSsize_t(res); |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1333 | Py_DECREF(res); |
Serhiy Storchaka | 030e92d | 2014-11-15 13:21:37 +0200 | [diff] [blame] | 1334 | if (size == -1 && PyErr_Occurred()) |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1335 | return (size_t)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | |
Serhiy Storchaka | 030e92d | 2014-11-15 13:21:37 +0200 | [diff] [blame] | 1337 | if (size < 0) { |
| 1338 | PyErr_SetString(PyExc_ValueError, "__sizeof__() should return >= 0"); |
| 1339 | return (size_t)-1; |
| 1340 | } |
| 1341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | /* add gc_head size */ |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1343 | if (PyObject_IS_GC(o)) |
Serhiy Storchaka | 030e92d | 2014-11-15 13:21:37 +0200 | [diff] [blame] | 1344 | return ((size_t)size) + sizeof(PyGC_Head); |
| 1345 | return (size_t)size; |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | static PyObject * |
| 1349 | sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) |
| 1350 | { |
| 1351 | static char *kwlist[] = {"object", "default", 0}; |
| 1352 | size_t size; |
| 1353 | PyObject *o, *dflt = NULL; |
| 1354 | |
| 1355 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof", |
| 1356 | kwlist, &o, &dflt)) |
| 1357 | return NULL; |
| 1358 | |
| 1359 | size = _PySys_GetSizeOf(o); |
| 1360 | |
| 1361 | if (size == (size_t)-1 && PyErr_Occurred()) { |
| 1362 | /* Has a default value been given */ |
| 1363 | if (dflt != NULL && PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 1364 | PyErr_Clear(); |
| 1365 | Py_INCREF(dflt); |
| 1366 | return dflt; |
| 1367 | } |
| 1368 | else |
| 1369 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | } |
Serhiy Storchaka | 547d3bc | 2014-08-14 22:21:18 +0300 | [diff] [blame] | 1371 | |
| 1372 | return PyLong_FromSize_t(size); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | PyDoc_STRVAR(getsizeof_doc, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1376 | "getsizeof(object [, default]) -> int\n\ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 1377 | \n\ |
| 1378 | Return the size of object in bytes."); |
| 1379 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1380 | /*[clinic input] |
| 1381 | sys.getrefcount -> Py_ssize_t |
| 1382 | |
| 1383 | object: object |
| 1384 | / |
| 1385 | |
| 1386 | Return the reference count of object. |
| 1387 | |
| 1388 | The count returned is generally one higher than you might expect, |
| 1389 | because it includes the (temporary) reference as an argument to |
| 1390 | getrefcount(). |
| 1391 | [clinic start generated code]*/ |
| 1392 | |
| 1393 | static Py_ssize_t |
| 1394 | sys_getrefcount_impl(PyObject *module, PyObject *object) |
| 1395 | /*[clinic end generated code: output=5fd477f2264b85b2 input=bf474efd50a21535]*/ |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1396 | { |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1397 | return object->ob_refcnt; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 1400 | #ifdef Py_REF_DEBUG |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1401 | /*[clinic input] |
| 1402 | sys.gettotalrefcount -> Py_ssize_t |
| 1403 | [clinic start generated code]*/ |
| 1404 | |
| 1405 | static Py_ssize_t |
| 1406 | sys_gettotalrefcount_impl(PyObject *module) |
| 1407 | /*[clinic end generated code: output=4103886cf17c25bc input=53b744faa5d2e4f6]*/ |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 1408 | { |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1409 | return _Py_GetRefTotal(); |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 1410 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1411 | #endif /* Py_REF_DEBUG */ |
Mark Hammond | 440d898 | 2000-06-20 08:12:48 +0000 | [diff] [blame] | 1412 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1413 | /*[clinic input] |
| 1414 | sys.getallocatedblocks -> Py_ssize_t |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 1415 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1416 | Return the number of memory blocks currently allocated. |
| 1417 | [clinic start generated code]*/ |
| 1418 | |
| 1419 | static Py_ssize_t |
| 1420 | sys_getallocatedblocks_impl(PyObject *module) |
| 1421 | /*[clinic end generated code: output=f0c4e873f0b6dcf7 input=dab13ee346a0673e]*/ |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1422 | { |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1423 | return _Py_GetAllocatedBlocks(); |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1426 | #ifdef COUNT_ALLOCS |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1427 | /*[clinic input] |
| 1428 | sys.getcounts |
| 1429 | [clinic start generated code]*/ |
| 1430 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1431 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1432 | sys_getcounts_impl(PyObject *module) |
| 1433 | /*[clinic end generated code: output=20df00bc164f43cb input=ad2ec7bda5424953]*/ |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1434 | { |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 1435 | extern PyObject *_Py_get_counts(void); |
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 | return _Py_get_counts(); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1438 | } |
| 1439 | #endif |
| 1440 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1441 | /*[clinic input] |
| 1442 | sys._getframe |
| 1443 | |
| 1444 | depth: int = 0 |
| 1445 | / |
| 1446 | |
| 1447 | Return a frame object from the call stack. |
| 1448 | |
| 1449 | If optional integer depth is given, return the frame object that many |
| 1450 | calls below the top of the stack. If that is deeper than the call |
| 1451 | stack, ValueError is raised. The default for depth is zero, returning |
| 1452 | the frame at the top of the call stack. |
| 1453 | |
| 1454 | This function should be used for internal and specialized purposes |
| 1455 | only. |
| 1456 | [clinic start generated code]*/ |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 1457 | |
| 1458 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1459 | sys__getframe_impl(PyObject *module, int depth) |
| 1460 | /*[clinic end generated code: output=d438776c04d59804 input=c1be8a6464b11ee5]*/ |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 1461 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1462 | PyFrameObject *f = _PyThreadState_GET()->frame; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | while (depth > 0 && f != NULL) { |
| 1465 | f = f->f_back; |
| 1466 | --depth; |
| 1467 | } |
| 1468 | if (f == NULL) { |
| 1469 | PyErr_SetString(PyExc_ValueError, |
| 1470 | "call stack is not deep enough"); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | Py_INCREF(f); |
| 1474 | return (PyObject*)f; |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1477 | /*[clinic input] |
| 1478 | sys._current_frames |
| 1479 | |
| 1480 | Return a dict mapping each thread's thread id to its current stack frame. |
| 1481 | |
| 1482 | This function should be used for specialized purposes only. |
| 1483 | [clinic start generated code]*/ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1484 | |
| 1485 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1486 | sys__current_frames_impl(PyObject *module) |
| 1487 | /*[clinic end generated code: output=d2a41ac0a0a3809a input=2a9049c5f5033691]*/ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | return _PyThread_CurrentFrames(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1492 | /*[clinic input] |
| 1493 | sys.call_tracing |
| 1494 | |
| 1495 | func: object |
| 1496 | args as funcargs: object(subclass_of='&PyTuple_Type') |
| 1497 | / |
| 1498 | |
| 1499 | Call func(*args), while tracing is enabled. |
| 1500 | |
| 1501 | The tracing state is saved, and restored afterwards. This is intended |
| 1502 | to be called from a debugger from a checkpoint, to recursively debug |
| 1503 | some other code. |
| 1504 | [clinic start generated code]*/ |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1505 | |
| 1506 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1507 | sys_call_tracing_impl(PyObject *module, PyObject *func, PyObject *funcargs) |
| 1508 | /*[clinic end generated code: output=7e4999853cd4e5a6 input=5102e8b11049f92f]*/ |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | return _PyEval_CallTracing(func, funcargs); |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1513 | /*[clinic input] |
| 1514 | sys.callstats |
| 1515 | |
| 1516 | Return a tuple of function call statistics. |
| 1517 | |
| 1518 | A tuple is returned only if CALL_PROFILE was defined when Python was |
| 1519 | built. Otherwise, this returns None. |
| 1520 | |
| 1521 | When enabled, this function returns detailed, implementation-specific |
| 1522 | details about the number of function calls executed. The return value |
| 1523 | is a 11-tuple where the entries in the tuple are counts of: |
| 1524 | 0. all function calls |
| 1525 | 1. calls to PyFunction_Type objects |
| 1526 | 2. PyFunction calls that do not create an argument tuple |
| 1527 | 3. PyFunction calls that do not create an argument tuple |
| 1528 | and bypass PyEval_EvalCodeEx() |
| 1529 | 4. PyMethod calls |
| 1530 | 5. PyMethod calls on bound methods |
| 1531 | 6. PyType calls |
| 1532 | 7. PyCFunction calls |
| 1533 | 8. generator calls |
| 1534 | 9. All other calls |
| 1535 | 10. Number of stack pops performed by call_function() |
| 1536 | [clinic start generated code]*/ |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 1537 | |
Victor Stinner | 048afd9 | 2016-11-28 11:59:04 +0100 | [diff] [blame] | 1538 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1539 | sys_callstats_impl(PyObject *module) |
| 1540 | /*[clinic end generated code: output=edc4a74957fa8def input=d447d8d224d5d175]*/ |
Victor Stinner | 048afd9 | 2016-11-28 11:59:04 +0100 | [diff] [blame] | 1541 | { |
| 1542 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1543 | "sys.callstats() has been deprecated in Python 3.7 " |
| 1544 | "and will be removed in the future", 1) < 0) { |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | |
| 1548 | Py_RETURN_NONE; |
| 1549 | } |
| 1550 | |
| 1551 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1552 | #ifdef __cplusplus |
| 1553 | extern "C" { |
| 1554 | #endif |
| 1555 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1556 | /*[clinic input] |
| 1557 | sys._debugmallocstats |
| 1558 | |
| 1559 | Print summary info to stderr about the state of pymalloc's structures. |
| 1560 | |
| 1561 | In Py_DEBUG mode, also perform some expensive internal consistency |
| 1562 | checks. |
| 1563 | [clinic start generated code]*/ |
| 1564 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1565 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1566 | sys__debugmallocstats_impl(PyObject *module) |
| 1567 | /*[clinic end generated code: output=ec3565f8c7cee46a input=33c0c9c416f98424]*/ |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1568 | { |
| 1569 | #ifdef WITH_PYMALLOC |
Victor Stinner | 6bf992a | 2017-12-06 17:26:10 +0100 | [diff] [blame] | 1570 | if (_PyObject_DebugMallocStats(stderr)) { |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1571 | fputc('\n', stderr); |
| 1572 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1573 | #endif |
| 1574 | _PyObject_DebugTypeStats(stderr); |
| 1575 | |
| 1576 | Py_RETURN_NONE; |
| 1577 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1578 | |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 1579 | #ifdef Py_TRACE_REFS |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 1580 | /* Defined in objects.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1581 | extern PyObject *_Py_GetObjects(PyObject *, PyObject *); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1582 | #endif |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 1583 | |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1584 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 1585 | /* Defined in ceval.c because it uses static globals if that file */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1586 | extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *); |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1587 | #endif |
| 1588 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1589 | #ifdef __cplusplus |
| 1590 | } |
| 1591 | #endif |
| 1592 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1593 | |
| 1594 | /*[clinic input] |
| 1595 | sys._clear_type_cache |
| 1596 | |
| 1597 | Clear the internal type lookup cache. |
| 1598 | [clinic start generated code]*/ |
| 1599 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1600 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1601 | sys__clear_type_cache_impl(PyObject *module) |
| 1602 | /*[clinic end generated code: output=20e48ca54a6f6971 input=127f3e04a8d9b555]*/ |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | PyType_ClearCache(); |
| 1605 | Py_RETURN_NONE; |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1608 | /*[clinic input] |
| 1609 | sys.is_finalizing |
| 1610 | |
| 1611 | Return True if Python is exiting. |
| 1612 | [clinic start generated code]*/ |
| 1613 | |
Antoine Pitrou | 5db1bb8 | 2014-12-07 01:28:27 +0100 | [diff] [blame] | 1614 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1615 | sys_is_finalizing_impl(PyObject *module) |
| 1616 | /*[clinic end generated code: output=735b5ff7962ab281 input=f0df747a039948a5]*/ |
Antoine Pitrou | 5db1bb8 | 2014-12-07 01:28:27 +0100 | [diff] [blame] | 1617 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1618 | return PyBool_FromLong(_Py_IsFinalizing()); |
Antoine Pitrou | 5db1bb8 | 2014-12-07 01:28:27 +0100 | [diff] [blame] | 1619 | } |
| 1620 | |
Victor Stinner | d6958ac | 2016-12-02 01:13:46 +0100 | [diff] [blame] | 1621 | #ifdef ANDROID_API_LEVEL |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1622 | /*[clinic input] |
| 1623 | sys.getandroidapilevel |
| 1624 | |
| 1625 | Return the build time API version of Android as an integer. |
| 1626 | [clinic start generated code]*/ |
Victor Stinner | d6958ac | 2016-12-02 01:13:46 +0100 | [diff] [blame] | 1627 | |
| 1628 | static PyObject * |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1629 | sys_getandroidapilevel_impl(PyObject *module) |
| 1630 | /*[clinic end generated code: output=214abf183a1c70c1 input=3e6d6c9fcdd24ac6]*/ |
Victor Stinner | d6958ac | 2016-12-02 01:13:46 +0100 | [diff] [blame] | 1631 | { |
| 1632 | return PyLong_FromLong(ANDROID_API_LEVEL); |
| 1633 | } |
| 1634 | #endif /* ANDROID_API_LEVEL */ |
| 1635 | |
| 1636 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1637 | static PyMethodDef sys_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | /* Might as well keep this in alphabetic order */ |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1639 | {"breakpointhook", (PyCFunction)(void(*)(void))sys_breakpointhook, |
Barry Warsaw | 36c1d1f | 2017-10-05 12:11:18 -0400 | [diff] [blame] | 1640 | METH_FASTCALL | METH_KEYWORDS, breakpointhook_doc}, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1641 | SYS_CALLSTATS_METHODDEF |
| 1642 | SYS__CLEAR_TYPE_CACHE_METHODDEF |
| 1643 | SYS__CURRENT_FRAMES_METHODDEF |
| 1644 | SYS_DISPLAYHOOK_METHODDEF |
| 1645 | SYS_EXC_INFO_METHODDEF |
| 1646 | SYS_EXCEPTHOOK_METHODDEF |
| 1647 | SYS_EXIT_METHODDEF |
| 1648 | SYS_GETDEFAULTENCODING_METHODDEF |
| 1649 | SYS_GETDLOPENFLAGS_METHODDEF |
| 1650 | SYS_GETALLOCATEDBLOCKS_METHODDEF |
| 1651 | SYS_GETCOUNTS_METHODDEF |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1652 | #ifdef DYNAMIC_EXECUTION_PROFILE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | {"getdxp", _Py_GetDXProfile, METH_VARARGS}, |
Guido van Rossum | 43f1b8d | 1997-01-24 04:07:45 +0000 | [diff] [blame] | 1654 | #endif |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1655 | SYS_GETFILESYSTEMENCODING_METHODDEF |
| 1656 | SYS_GETFILESYSTEMENCODEERRORS_METHODDEF |
Guido van Rossum | 7f3f2c1 | 1996-05-23 22:45:41 +0000 | [diff] [blame] | 1657 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | {"getobjects", _Py_GetObjects, METH_VARARGS}, |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 1659 | #endif |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1660 | SYS_GETTOTALREFCOUNT_METHODDEF |
| 1661 | SYS_GETREFCOUNT_METHODDEF |
| 1662 | SYS_GETRECURSIONLIMIT_METHODDEF |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1663 | {"getsizeof", (PyCFunction)(void(*)(void))sys_getsizeof, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | METH_VARARGS | METH_KEYWORDS, getsizeof_doc}, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1665 | SYS__GETFRAME_METHODDEF |
| 1666 | SYS_GETWINDOWSVERSION_METHODDEF |
| 1667 | SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF |
| 1668 | SYS_INTERN_METHODDEF |
| 1669 | SYS_IS_FINALIZING_METHODDEF |
| 1670 | SYS_MDEBUG_METHODDEF |
| 1671 | SYS_SETCHECKINTERVAL_METHODDEF |
| 1672 | SYS_GETCHECKINTERVAL_METHODDEF |
| 1673 | SYS_SETSWITCHINTERVAL_METHODDEF |
| 1674 | SYS_GETSWITCHINTERVAL_METHODDEF |
| 1675 | SYS_SETDLOPENFLAGS_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | {"setprofile", sys_setprofile, METH_O, setprofile_doc}, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1677 | SYS_GETPROFILE_METHODDEF |
| 1678 | SYS_SETRECURSIONLIMIT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | {"settrace", sys_settrace, METH_O, settrace_doc}, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1680 | SYS_GETTRACE_METHODDEF |
| 1681 | SYS_CALL_TRACING_METHODDEF |
| 1682 | SYS__DEBUGMALLOCSTATS_METHODDEF |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1683 | SYS_SET_COROUTINE_ORIGIN_TRACKING_DEPTH_METHODDEF |
| 1684 | SYS_GET_COROUTINE_ORIGIN_TRACKING_DEPTH_METHODDEF |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1685 | SYS_SET_COROUTINE_WRAPPER_METHODDEF |
| 1686 | SYS_GET_COROUTINE_WRAPPER_METHODDEF |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1687 | {"set_asyncgen_hooks", (PyCFunction)(void(*)(void))sys_set_asyncgen_hooks, |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1688 | METH_VARARGS | METH_KEYWORDS, set_asyncgen_hooks_doc}, |
Tal Einat | ede0b6f | 2018-12-31 17:12:08 +0200 | [diff] [blame] | 1689 | SYS_GET_ASYNCGEN_HOOKS_METHODDEF |
| 1690 | SYS_GETANDROIDAPILEVEL_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1692 | }; |
| 1693 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 1694 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1695 | list_builtin_module_names(void) |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | PyObject *list = PyList_New(0); |
| 1698 | int i; |
| 1699 | if (list == NULL) |
| 1700 | return NULL; |
| 1701 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 1702 | PyObject *name = PyUnicode_FromString( |
| 1703 | PyImport_Inittab[i].name); |
| 1704 | if (name == NULL) |
| 1705 | break; |
| 1706 | PyList_Append(list, name); |
| 1707 | Py_DECREF(name); |
| 1708 | } |
| 1709 | if (PyList_Sort(list) != 0) { |
| 1710 | Py_DECREF(list); |
| 1711 | list = NULL; |
| 1712 | } |
| 1713 | if (list) { |
| 1714 | PyObject *v = PyList_AsTuple(list); |
| 1715 | Py_DECREF(list); |
| 1716 | list = v; |
| 1717 | } |
| 1718 | return list; |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1721 | /* Pre-initialization support for sys.warnoptions and sys._xoptions |
| 1722 | * |
| 1723 | * Modern internal code paths: |
| 1724 | * These APIs get called after _Py_InitializeCore and get to use the |
| 1725 | * regular CPython list, dict, and unicode APIs. |
| 1726 | * |
| 1727 | * Legacy embedding code paths: |
| 1728 | * The multi-phase initialization API isn't public yet, so embedding |
| 1729 | * apps still need to be able configure sys.warnoptions and sys._xoptions |
| 1730 | * before they call Py_Initialize. To support this, we stash copies of |
| 1731 | * the supplied wchar * sequences in linked lists, and then migrate the |
| 1732 | * contents of those lists to the sys module in _PyInitializeCore. |
| 1733 | * |
| 1734 | */ |
| 1735 | |
| 1736 | struct _preinit_entry { |
| 1737 | wchar_t *value; |
| 1738 | struct _preinit_entry *next; |
| 1739 | }; |
| 1740 | |
| 1741 | typedef struct _preinit_entry *_Py_PreInitEntry; |
| 1742 | |
| 1743 | static _Py_PreInitEntry _preinit_warnoptions = NULL; |
| 1744 | static _Py_PreInitEntry _preinit_xoptions = NULL; |
| 1745 | |
| 1746 | static _Py_PreInitEntry |
| 1747 | _alloc_preinit_entry(const wchar_t *value) |
| 1748 | { |
| 1749 | /* To get this to work, we have to initialize the runtime implicitly */ |
| 1750 | _PyRuntime_Initialize(); |
| 1751 | |
| 1752 | /* Force default allocator, so we can ensure that it also gets used to |
| 1753 | * destroy the linked list in _clear_preinit_entries. |
| 1754 | */ |
| 1755 | PyMemAllocatorEx old_alloc; |
| 1756 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1757 | |
| 1758 | _Py_PreInitEntry node = PyMem_RawCalloc(1, sizeof(*node)); |
| 1759 | if (node != NULL) { |
| 1760 | node->value = _PyMem_RawWcsdup(value); |
| 1761 | if (node->value == NULL) { |
| 1762 | PyMem_RawFree(node); |
| 1763 | node = NULL; |
| 1764 | }; |
| 1765 | }; |
| 1766 | |
| 1767 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1768 | return node; |
| 1769 | }; |
| 1770 | |
| 1771 | static int |
| 1772 | _append_preinit_entry(_Py_PreInitEntry *optionlist, const wchar_t *value) |
| 1773 | { |
| 1774 | _Py_PreInitEntry new_entry = _alloc_preinit_entry(value); |
| 1775 | if (new_entry == NULL) { |
| 1776 | return -1; |
| 1777 | } |
| 1778 | /* We maintain the linked list in this order so it's easy to play back |
| 1779 | * the add commands in the same order later on in _Py_InitializeCore |
| 1780 | */ |
| 1781 | _Py_PreInitEntry last_entry = *optionlist; |
| 1782 | if (last_entry == NULL) { |
| 1783 | *optionlist = new_entry; |
| 1784 | } else { |
| 1785 | while (last_entry->next != NULL) { |
| 1786 | last_entry = last_entry->next; |
| 1787 | } |
| 1788 | last_entry->next = new_entry; |
| 1789 | } |
| 1790 | return 0; |
| 1791 | }; |
| 1792 | |
| 1793 | static void |
| 1794 | _clear_preinit_entries(_Py_PreInitEntry *optionlist) |
| 1795 | { |
| 1796 | _Py_PreInitEntry current = *optionlist; |
| 1797 | *optionlist = NULL; |
| 1798 | /* Deallocate the nodes and their contents using the default allocator */ |
| 1799 | PyMemAllocatorEx old_alloc; |
| 1800 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1801 | while (current != NULL) { |
| 1802 | _Py_PreInitEntry next = current->next; |
| 1803 | PyMem_RawFree(current->value); |
| 1804 | PyMem_RawFree(current); |
| 1805 | current = next; |
| 1806 | } |
| 1807 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 1808 | }; |
| 1809 | |
| 1810 | static void |
| 1811 | _clear_all_preinit_options(void) |
| 1812 | { |
| 1813 | _clear_preinit_entries(&_preinit_warnoptions); |
| 1814 | _clear_preinit_entries(&_preinit_xoptions); |
| 1815 | } |
| 1816 | |
| 1817 | static int |
| 1818 | _PySys_ReadPreInitOptions(void) |
| 1819 | { |
| 1820 | /* Rerun the add commands with the actual sys module available */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1821 | PyThreadState *tstate = _PyThreadState_GET(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1822 | if (tstate == NULL) { |
| 1823 | /* Still don't have a thread state, so something is wrong! */ |
| 1824 | return -1; |
| 1825 | } |
| 1826 | _Py_PreInitEntry entry = _preinit_warnoptions; |
| 1827 | while (entry != NULL) { |
| 1828 | PySys_AddWarnOption(entry->value); |
| 1829 | entry = entry->next; |
| 1830 | } |
| 1831 | entry = _preinit_xoptions; |
| 1832 | while (entry != NULL) { |
| 1833 | PySys_AddXOption(entry->value); |
| 1834 | entry = entry->next; |
| 1835 | } |
| 1836 | |
| 1837 | _clear_all_preinit_options(); |
| 1838 | return 0; |
| 1839 | }; |
| 1840 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1841 | static PyObject * |
| 1842 | get_warnoptions(void) |
| 1843 | { |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1844 | PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1845 | if (warnoptions == NULL || !PyList_Check(warnoptions)) { |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1846 | /* PEP432 TODO: we can reach this if warnoptions is NULL in the main |
| 1847 | * interpreter config. When that happens, we need to properly set |
| 1848 | * the `warnoptions` reference in the main interpreter config as well. |
| 1849 | * |
| 1850 | * For Python 3.7, we shouldn't be able to get here due to the |
| 1851 | * combination of how _PyMainInterpreter_ReadConfig and _PySys_EndInit |
| 1852 | * work, but we expect 3.8+ to make the _PyMainInterpreter_ReadConfig |
| 1853 | * call optional for embedding applications, thus making this |
| 1854 | * reachable again. |
| 1855 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1856 | warnoptions = PyList_New(0); |
| 1857 | if (warnoptions == NULL) |
| 1858 | return NULL; |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1859 | if (_PySys_SetObjectId(&PyId_warnoptions, warnoptions)) { |
| 1860 | Py_DECREF(warnoptions); |
| 1861 | return NULL; |
| 1862 | } |
| 1863 | Py_DECREF(warnoptions); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1864 | } |
| 1865 | return warnoptions; |
| 1866 | } |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1867 | |
| 1868 | void |
| 1869 | PySys_ResetWarnOptions(void) |
| 1870 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1871 | PyThreadState *tstate = _PyThreadState_GET(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1872 | if (tstate == NULL) { |
| 1873 | _clear_preinit_entries(&_preinit_warnoptions); |
| 1874 | return; |
| 1875 | } |
| 1876 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1877 | PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | if (warnoptions == NULL || !PyList_Check(warnoptions)) |
| 1879 | return; |
| 1880 | PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Victor Stinner | e1b2995 | 2018-10-30 14:31:42 +0100 | [diff] [blame] | 1883 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1884 | _PySys_AddWarnOptionWithError(PyObject *option) |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1885 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1886 | PyObject *warnoptions = get_warnoptions(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1887 | if (warnoptions == NULL) { |
| 1888 | return -1; |
| 1889 | } |
| 1890 | if (PyList_Append(warnoptions, option)) { |
| 1891 | return -1; |
| 1892 | } |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | void |
| 1897 | PySys_AddWarnOptionUnicode(PyObject *option) |
| 1898 | { |
Victor Stinner | e1b2995 | 2018-10-30 14:31:42 +0100 | [diff] [blame] | 1899 | if (_PySys_AddWarnOptionWithError(option) < 0) { |
| 1900 | /* No return value, therefore clear error state if possible */ |
| 1901 | if (_PyThreadState_UncheckedGet()) { |
| 1902 | PyErr_Clear(); |
| 1903 | } |
| 1904 | } |
Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | void |
| 1908 | PySys_AddWarnOption(const wchar_t *s) |
| 1909 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1910 | PyThreadState *tstate = _PyThreadState_GET(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1911 | if (tstate == NULL) { |
| 1912 | _append_preinit_entry(&_preinit_warnoptions, s); |
| 1913 | return; |
| 1914 | } |
Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 1915 | PyObject *unicode; |
| 1916 | unicode = PyUnicode_FromWideChar(s, -1); |
| 1917 | if (unicode == NULL) |
| 1918 | return; |
| 1919 | PySys_AddWarnOptionUnicode(unicode); |
| 1920 | Py_DECREF(unicode); |
Guido van Rossum | 23fff91 | 2000-12-15 22:02:05 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1923 | int |
| 1924 | PySys_HasWarnOptions(void) |
| 1925 | { |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1926 | PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); |
Serhiy Storchaka | dffccc6 | 2018-12-10 13:50:22 +0200 | [diff] [blame] | 1927 | return (warnoptions != NULL && PyList_Check(warnoptions) |
| 1928 | && PyList_GET_SIZE(warnoptions) > 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1931 | static PyObject * |
| 1932 | get_xoptions(void) |
| 1933 | { |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1934 | PyObject *xoptions = _PySys_GetObjectId(&PyId__xoptions); |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1935 | if (xoptions == NULL || !PyDict_Check(xoptions)) { |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1936 | /* PEP432 TODO: we can reach this if xoptions is NULL in the main |
| 1937 | * interpreter config. When that happens, we need to properly set |
| 1938 | * the `xoptions` reference in the main interpreter config as well. |
| 1939 | * |
| 1940 | * For Python 3.7, we shouldn't be able to get here due to the |
| 1941 | * combination of how _PyMainInterpreter_ReadConfig and _PySys_EndInit |
| 1942 | * work, but we expect 3.8+ to make the _PyMainInterpreter_ReadConfig |
| 1943 | * call optional for embedding applications, thus making this |
| 1944 | * reachable again. |
| 1945 | */ |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1946 | xoptions = PyDict_New(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1947 | if (xoptions == NULL) |
| 1948 | return NULL; |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 1949 | if (_PySys_SetObjectId(&PyId__xoptions, xoptions)) { |
| 1950 | Py_DECREF(xoptions); |
| 1951 | return NULL; |
| 1952 | } |
| 1953 | Py_DECREF(xoptions); |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1954 | } |
| 1955 | return xoptions; |
| 1956 | } |
| 1957 | |
Victor Stinner | e1b2995 | 2018-10-30 14:31:42 +0100 | [diff] [blame] | 1958 | static int |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1959 | _PySys_AddXOptionWithError(const wchar_t *s) |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1960 | { |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1961 | PyObject *name = NULL, *value = NULL; |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1962 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1963 | PyObject *opts = get_xoptions(); |
| 1964 | if (opts == NULL) { |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1965 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1966 | } |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1967 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1968 | const wchar_t *name_end = wcschr(s, L'='); |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1969 | if (!name_end) { |
| 1970 | name = PyUnicode_FromWideChar(s, -1); |
| 1971 | value = Py_True; |
| 1972 | Py_INCREF(value); |
| 1973 | } |
| 1974 | else { |
| 1975 | name = PyUnicode_FromWideChar(s, name_end - s); |
| 1976 | value = PyUnicode_FromWideChar(name_end + 1, -1); |
| 1977 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1978 | if (name == NULL || value == NULL) { |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1979 | goto error; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1980 | } |
| 1981 | if (PyDict_SetItem(opts, name, value) < 0) { |
| 1982 | goto error; |
| 1983 | } |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1984 | Py_DECREF(name); |
| 1985 | Py_DECREF(value); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1986 | return 0; |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 1987 | |
| 1988 | error: |
| 1989 | Py_XDECREF(name); |
| 1990 | Py_XDECREF(value); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 1991 | return -1; |
| 1992 | } |
| 1993 | |
| 1994 | void |
| 1995 | PySys_AddXOption(const wchar_t *s) |
| 1996 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 1997 | PyThreadState *tstate = _PyThreadState_GET(); |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 1998 | if (tstate == NULL) { |
| 1999 | _append_preinit_entry(&_preinit_xoptions, s); |
| 2000 | return; |
| 2001 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2002 | if (_PySys_AddXOptionWithError(s) < 0) { |
| 2003 | /* No return value, therefore clear error state if possible */ |
| 2004 | if (_PyThreadState_UncheckedGet()) { |
| 2005 | PyErr_Clear(); |
| 2006 | } |
Victor Stinner | 0cae609 | 2016-11-11 01:43:56 +0100 | [diff] [blame] | 2007 | } |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | PyObject * |
| 2011 | PySys_GetXOptions(void) |
| 2012 | { |
| 2013 | return get_xoptions(); |
| 2014 | } |
| 2015 | |
Guido van Rossum | 40552d0 | 1998-08-06 03:34:39 +0000 | [diff] [blame] | 2016 | /* XXX This doc string is too long to be a single string literal in VC++ 5.0. |
| 2017 | Two literals concatenated works just fine. If you have a K&R compiler |
| 2018 | or other abomination that however *does* understand longer strings, |
| 2019 | 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] | 2020 | PyDoc_VAR(sys_doc) = |
| 2021 | PyDoc_STR( |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2022 | "This module provides access to some objects used or maintained by the\n\ |
| 2023 | interpreter and to functions that interact strongly with the interpreter.\n\ |
| 2024 | \n\ |
| 2025 | Dynamic objects:\n\ |
| 2026 | \n\ |
| 2027 | argv -- command line arguments; argv[0] is the script pathname if known\n\ |
| 2028 | path -- module search path; path[0] is the script directory, else ''\n\ |
| 2029 | modules -- dictionary of loaded modules\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2030 | \n\ |
| 2031 | displayhook -- called to show results in an interactive session\n\ |
| 2032 | excepthook -- called to handle any uncaught exception other than SystemExit\n\ |
| 2033 | To customize printing in an interactive session or to install a custom\n\ |
| 2034 | top-level exception handler, assign other functions to replace these.\n\ |
| 2035 | \n\ |
Benjamin Peterson | 06157a4 | 2008-07-15 00:28:36 +0000 | [diff] [blame] | 2036 | stdin -- standard input file object; used by input()\n\ |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 2037 | stdout -- standard output file object; used by print()\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2038 | stderr -- standard error object; used for error messages\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2039 | By assigning other file objects (or objects that behave like files)\n\ |
| 2040 | 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] | 2041 | \n\ |
| 2042 | last_type -- type of last uncaught exception\n\ |
| 2043 | last_value -- value of last uncaught exception\n\ |
| 2044 | last_traceback -- traceback of last uncaught exception\n\ |
| 2045 | These three are only available in an interactive session after a\n\ |
| 2046 | traceback has been printed.\n\ |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 2047 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2048 | ) |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2049 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2050 | PyDoc_STR( |
Guido van Rossum | a71b5f4 | 1999-01-14 19:07:00 +0000 | [diff] [blame] | 2051 | "\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2052 | Static objects:\n\ |
| 2053 | \n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2054 | builtin_module_names -- tuple of module names built into this interpreter\n\ |
| 2055 | copyright -- copyright notice pertaining to this interpreter\n\ |
| 2056 | exec_prefix -- prefix used to find the machine-specific Python library\n\ |
Petri Lehtinen | 4b0eab6 | 2012-02-02 21:23:15 +0200 | [diff] [blame] | 2057 | executable -- absolute path of the executable binary of the Python interpreter\n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2058 | float_info -- a struct sequence with information about the float implementation.\n\ |
| 2059 | 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] | 2060 | hash_info -- a struct sequence with information about the hash algorithm.\n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2061 | hexversion -- version information encoded as a single integer\n\ |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2062 | implementation -- Python implementation information.\n\ |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 2063 | int_info -- a struct sequence with information about the int implementation.\n\ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 2064 | maxsize -- the largest supported length of containers.\n\ |
Serhiy Storchaka | d3faf43 | 2015-01-18 11:28:37 +0200 | [diff] [blame] | 2065 | maxunicode -- the value of the largest Unicode code point\n\ |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2066 | platform -- platform identifier\n\ |
| 2067 | prefix -- prefix used to find the Python library\n\ |
| 2068 | thread_info -- a struct sequence with information about the thread implementation.\n\ |
Fred Drake | 801c08d | 2000-04-13 15:29:10 +0000 | [diff] [blame] | 2069 | version -- the version of this interpreter as a string\n\ |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2070 | version_info -- version information as a named tuple\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2071 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2072 | ) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2073 | #ifdef MS_COREDLL |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2074 | /* concatenating string here */ |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2075 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2076 | "dllhandle -- [Windows only] integer handle of the Python DLL\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2077 | winver -- [Windows only] version number of the Python DLL\n\ |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2078 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2079 | ) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2080 | #endif /* MS_COREDLL */ |
| 2081 | #ifdef MS_WINDOWS |
| 2082 | /* concatenating string here */ |
| 2083 | PyDoc_STR( |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 2084 | "_enablelegacywindowsfsencoding -- [Windows only]\n\ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2085 | " |
| 2086 | ) |
| 2087 | #endif |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2088 | PyDoc_STR( |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 2089 | "__stdin__ -- the original stdin; don't touch!\n\ |
| 2090 | __stdout__ -- the original stdout; don't touch!\n\ |
| 2091 | __stderr__ -- the original stderr; don't touch!\n\ |
| 2092 | __displayhook__ -- the original displayhook; don't touch!\n\ |
| 2093 | __excepthook__ -- the original excepthook; don't touch!\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2094 | \n\ |
| 2095 | Functions:\n\ |
| 2096 | \n\ |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 2097 | 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] | 2098 | excepthook() -- print an exception and its traceback to sys.stderr\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2099 | exc_info() -- return thread-safe information about the current exception\n\ |
| 2100 | exit() -- exit the interpreter by raising SystemExit\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 2101 | getdlopenflags() -- returns flags to be used for dlopen() calls\n\ |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 2102 | getprofile() -- get the global profiling function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2103 | getrefcount() -- return the reference count for an object (plus one :-)\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 2104 | getrecursionlimit() -- return the max recursion depth for the interpreter\n\ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2105 | getsizeof() -- return the size of an object in bytes\n\ |
Christian Heimes | 9bd667a | 2008-01-20 15:14:11 +0000 | [diff] [blame] | 2106 | gettrace() -- get the global debug tracing function\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2107 | setcheckinterval() -- control how often the interpreter checks for events\n\ |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 2108 | setdlopenflags() -- set the flags to be used for dlopen() calls\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2109 | setprofile() -- set the global profiling function\n\ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 2110 | setrecursionlimit() -- set the max recursion depth for the interpreter\n\ |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2111 | settrace() -- set the global debug tracing function\n\ |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 2112 | " |
Martin v. Löwis | a3fb4f7 | 2002-06-09 13:33:54 +0000 | [diff] [blame] | 2113 | ) |
Fred Drake | ccede59 | 2000-08-14 20:59:57 +0000 | [diff] [blame] | 2114 | /* end of sys_doc */ ; |
Guido van Rossum | c3bc31e | 1998-06-27 19:43:25 +0000 | [diff] [blame] | 2115 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2116 | |
| 2117 | PyDoc_STRVAR(flags__doc__, |
| 2118 | "sys.flags\n\ |
| 2119 | \n\ |
| 2120 | Flags provided through command line arguments or environment vars."); |
| 2121 | |
| 2122 | static PyTypeObject FlagsType; |
| 2123 | |
| 2124 | static PyStructSequence_Field flags_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | {"debug", "-d"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | {"inspect", "-i"}, |
| 2127 | {"interactive", "-i"}, |
| 2128 | {"optimize", "-O or -OO"}, |
| 2129 | {"dont_write_bytecode", "-B"}, |
| 2130 | {"no_user_site", "-s"}, |
| 2131 | {"no_site", "-S"}, |
| 2132 | {"ignore_environment", "-E"}, |
| 2133 | {"verbose", "-v"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | /* {"unbuffered", "-u"}, */ |
| 2135 | /* {"skip_first", "-x"}, */ |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 2136 | {"bytes_warning", "-b"}, |
| 2137 | {"quiet", "-q"}, |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 2138 | {"hash_randomization", "-R"}, |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 2139 | {"isolated", "-I"}, |
Victor Stinner | 5e3806f | 2017-11-30 11:40:24 +0100 | [diff] [blame] | 2140 | {"dev_mode", "-X dev"}, |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 2141 | {"utf8_mode", "-X utf8"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | {0} |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2143 | }; |
| 2144 | |
| 2145 | static PyStructSequence_Desc flags_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | "sys.flags", /* name */ |
| 2147 | flags__doc__, /* doc */ |
| 2148 | flags_fields, /* fields */ |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 2149 | 15 |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2150 | }; |
| 2151 | |
| 2152 | static PyObject* |
| 2153 | make_flags(void) |
| 2154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | int pos = 0; |
| 2156 | PyObject *seq; |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2157 | const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | seq = PyStructSequence_New(&FlagsType); |
| 2160 | if (seq == NULL) |
| 2161 | return NULL; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2162 | |
| 2163 | #define SetFlag(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | PyStructSequence_SET_ITEM(seq, pos++, PyLong_FromLong(flag)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2165 | |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2166 | SetFlag(config->parser_debug); |
| 2167 | SetFlag(config->inspect); |
| 2168 | SetFlag(config->interactive); |
| 2169 | SetFlag(config->optimization_level); |
| 2170 | SetFlag(!config->write_bytecode); |
| 2171 | SetFlag(!config->user_site_directory); |
| 2172 | SetFlag(!config->site_import); |
| 2173 | SetFlag(!config->use_environment); |
| 2174 | SetFlag(config->verbose); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | /* SetFlag(saw_unbuffered_flag); */ |
| 2176 | /* SetFlag(skipfirstline); */ |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2177 | SetFlag(config->bytes_warning); |
| 2178 | SetFlag(config->quiet); |
| 2179 | SetFlag(config->use_hash_seed == 0 || config->hash_seed != 0); |
| 2180 | SetFlag(config->isolated); |
| 2181 | PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->dev_mode)); |
| 2182 | SetFlag(config->utf8_mode); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 2183 | #undef SetFlag |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | if (PyErr_Occurred()) { |
Serhiy Storchaka | 87a854d | 2013-12-17 14:59:42 +0200 | [diff] [blame] | 2186 | Py_DECREF(seq); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | return NULL; |
| 2188 | } |
| 2189 | return seq; |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2192 | PyDoc_STRVAR(version_info__doc__, |
| 2193 | "sys.version_info\n\ |
| 2194 | \n\ |
| 2195 | Version information as a named tuple."); |
| 2196 | |
| 2197 | static PyTypeObject VersionInfoType; |
| 2198 | |
| 2199 | static PyStructSequence_Field version_info_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | {"major", "Major release number"}, |
| 2201 | {"minor", "Minor release number"}, |
| 2202 | {"micro", "Patch release number"}, |
Ned Deily | da4887a | 2016-11-04 17:03:34 -0400 | [diff] [blame] | 2203 | {"releaselevel", "'alpha', 'beta', 'candidate', or 'final'"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | {"serial", "Serial release number"}, |
| 2205 | {0} |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2206 | }; |
| 2207 | |
| 2208 | static PyStructSequence_Desc version_info_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | "sys.version_info", /* name */ |
| 2210 | version_info__doc__, /* doc */ |
| 2211 | version_info_fields, /* fields */ |
| 2212 | 5 |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2213 | }; |
| 2214 | |
| 2215 | static PyObject * |
| 2216 | make_version_info(void) |
| 2217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | PyObject *version_info; |
| 2219 | char *s; |
| 2220 | int pos = 0; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2222 | version_info = PyStructSequence_New(&VersionInfoType); |
| 2223 | if (version_info == NULL) { |
| 2224 | return NULL; |
| 2225 | } |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | /* |
| 2228 | * These release level checks are mutually exclusive and cover |
| 2229 | * the field, so don't get too fancy with the pre-processor! |
| 2230 | */ |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2231 | #if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | s = "alpha"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2233 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | s = "beta"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2235 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | s = "candidate"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2237 | #elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | s = "final"; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2239 | #endif |
| 2240 | |
| 2241 | #define SetIntItem(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | PyStructSequence_SET_ITEM(version_info, pos++, PyLong_FromLong(flag)) |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2243 | #define SetStrItem(flag) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | PyStructSequence_SET_ITEM(version_info, pos++, PyUnicode_FromString(flag)) |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | SetIntItem(PY_MAJOR_VERSION); |
| 2247 | SetIntItem(PY_MINOR_VERSION); |
| 2248 | SetIntItem(PY_MICRO_VERSION); |
| 2249 | SetStrItem(s); |
| 2250 | SetIntItem(PY_RELEASE_SERIAL); |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2251 | #undef SetIntItem |
| 2252 | #undef SetStrItem |
| 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | if (PyErr_Occurred()) { |
| 2255 | Py_CLEAR(version_info); |
| 2256 | return NULL; |
| 2257 | } |
| 2258 | return version_info; |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Brett Cannon | 3adc7b7 | 2012-07-09 14:22:12 -0400 | [diff] [blame] | 2261 | /* sys.implementation values */ |
| 2262 | #define NAME "cpython" |
| 2263 | const char *_PySys_ImplName = NAME; |
Victor Stinner | cf01b68 | 2015-11-05 11:21:38 +0100 | [diff] [blame] | 2264 | #define MAJOR Py_STRINGIFY(PY_MAJOR_VERSION) |
| 2265 | #define MINOR Py_STRINGIFY(PY_MINOR_VERSION) |
Ned Deily | 529ea5d | 2014-06-30 23:31:14 -0700 | [diff] [blame] | 2266 | #define TAG NAME "-" MAJOR MINOR |
Brett Cannon | 3adc7b7 | 2012-07-09 14:22:12 -0400 | [diff] [blame] | 2267 | const char *_PySys_ImplCacheTag = TAG; |
| 2268 | #undef NAME |
Brett Cannon | 3adc7b7 | 2012-07-09 14:22:12 -0400 | [diff] [blame] | 2269 | #undef MAJOR |
| 2270 | #undef MINOR |
| 2271 | #undef TAG |
| 2272 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2273 | static PyObject * |
| 2274 | make_impl_info(PyObject *version_info) |
| 2275 | { |
| 2276 | int res; |
| 2277 | PyObject *impl_info, *value, *ns; |
| 2278 | |
| 2279 | impl_info = PyDict_New(); |
| 2280 | if (impl_info == NULL) |
| 2281 | return NULL; |
| 2282 | |
| 2283 | /* populate the dict */ |
| 2284 | |
Brett Cannon | 3adc7b7 | 2012-07-09 14:22:12 -0400 | [diff] [blame] | 2285 | value = PyUnicode_FromString(_PySys_ImplName); |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2286 | if (value == NULL) |
| 2287 | goto error; |
| 2288 | res = PyDict_SetItemString(impl_info, "name", value); |
| 2289 | Py_DECREF(value); |
| 2290 | if (res < 0) |
| 2291 | goto error; |
| 2292 | |
Brett Cannon | 3adc7b7 | 2012-07-09 14:22:12 -0400 | [diff] [blame] | 2293 | value = PyUnicode_FromString(_PySys_ImplCacheTag); |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2294 | if (value == NULL) |
| 2295 | goto error; |
| 2296 | res = PyDict_SetItemString(impl_info, "cache_tag", value); |
| 2297 | Py_DECREF(value); |
| 2298 | if (res < 0) |
| 2299 | goto error; |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2300 | |
| 2301 | res = PyDict_SetItemString(impl_info, "version", version_info); |
| 2302 | if (res < 0) |
| 2303 | goto error; |
| 2304 | |
| 2305 | value = PyLong_FromLong(PY_VERSION_HEX); |
| 2306 | if (value == NULL) |
| 2307 | goto error; |
| 2308 | res = PyDict_SetItemString(impl_info, "hexversion", value); |
| 2309 | Py_DECREF(value); |
| 2310 | if (res < 0) |
| 2311 | goto error; |
| 2312 | |
doko@ubuntu.com | 5553231 | 2016-06-14 08:55:19 +0200 | [diff] [blame] | 2313 | #ifdef MULTIARCH |
| 2314 | value = PyUnicode_FromString(MULTIARCH); |
| 2315 | if (value == NULL) |
| 2316 | goto error; |
| 2317 | res = PyDict_SetItemString(impl_info, "_multiarch", value); |
| 2318 | Py_DECREF(value); |
| 2319 | if (res < 0) |
| 2320 | goto error; |
| 2321 | #endif |
| 2322 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2323 | /* dict ready */ |
| 2324 | |
| 2325 | ns = _PyNamespace_New(impl_info); |
| 2326 | Py_DECREF(impl_info); |
| 2327 | return ns; |
| 2328 | |
| 2329 | error: |
| 2330 | Py_CLEAR(impl_info); |
| 2331 | return NULL; |
| 2332 | } |
| 2333 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2334 | static struct PyModuleDef sysmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | PyModuleDef_HEAD_INIT, |
| 2336 | "sys", |
| 2337 | sys_doc, |
| 2338 | -1, /* multiple "initialization" just copies the module dict. */ |
| 2339 | sys_methods, |
| 2340 | NULL, |
| 2341 | NULL, |
| 2342 | NULL, |
| 2343 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2344 | }; |
| 2345 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2346 | /* Updating the sys namespace, returning NULL pointer on error */ |
Victor Stinner | 8fea252 | 2013-10-27 17:15:42 +0100 | [diff] [blame] | 2347 | #define SET_SYS_FROM_STRING_BORROW(key, value) \ |
Victor Stinner | 5804960 | 2013-07-22 22:40:00 +0200 | [diff] [blame] | 2348 | do { \ |
Victor Stinner | 5804960 | 2013-07-22 22:40:00 +0200 | [diff] [blame] | 2349 | PyObject *v = (value); \ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2350 | if (v == NULL) { \ |
| 2351 | goto err_occurred; \ |
| 2352 | } \ |
Victor Stinner | 5804960 | 2013-07-22 22:40:00 +0200 | [diff] [blame] | 2353 | res = PyDict_SetItemString(sysdict, key, v); \ |
| 2354 | if (res < 0) { \ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2355 | goto err_occurred; \ |
Victor Stinner | 8fea252 | 2013-10-27 17:15:42 +0100 | [diff] [blame] | 2356 | } \ |
| 2357 | } while (0) |
| 2358 | #define SET_SYS_FROM_STRING(key, value) \ |
| 2359 | do { \ |
Victor Stinner | 8fea252 | 2013-10-27 17:15:42 +0100 | [diff] [blame] | 2360 | PyObject *v = (value); \ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2361 | if (v == NULL) { \ |
| 2362 | goto err_occurred; \ |
| 2363 | } \ |
Victor Stinner | 8fea252 | 2013-10-27 17:15:42 +0100 | [diff] [blame] | 2364 | res = PyDict_SetItemString(sysdict, key, v); \ |
| 2365 | Py_DECREF(v); \ |
| 2366 | if (res < 0) { \ |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2367 | goto err_occurred; \ |
Victor Stinner | 5804960 | 2013-07-22 22:40:00 +0200 | [diff] [blame] | 2368 | } \ |
| 2369 | } while (0) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2370 | |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2371 | static _PyInitError |
| 2372 | _PySys_InitCore(PyObject *sysdict) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2373 | { |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2374 | PyObject *version_info; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2375 | int res; |
| 2376 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 2377 | /* stdin/stdout/stderr are set in pylifecycle.c */ |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 2378 | |
Victor Stinner | 8fea252 | 2013-10-27 17:15:42 +0100 | [diff] [blame] | 2379 | SET_SYS_FROM_STRING_BORROW("__displayhook__", |
| 2380 | PyDict_GetItemString(sysdict, "displayhook")); |
| 2381 | SET_SYS_FROM_STRING_BORROW("__excepthook__", |
| 2382 | PyDict_GetItemString(sysdict, "excepthook")); |
Barry Warsaw | 36c1d1f | 2017-10-05 12:11:18 -0400 | [diff] [blame] | 2383 | SET_SYS_FROM_STRING_BORROW( |
| 2384 | "__breakpointhook__", |
| 2385 | PyDict_GetItemString(sysdict, "breakpointhook")); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | SET_SYS_FROM_STRING("version", |
| 2387 | PyUnicode_FromString(Py_GetVersion())); |
| 2388 | SET_SYS_FROM_STRING("hexversion", |
| 2389 | PyLong_FromLong(PY_VERSION_HEX)); |
Ned Deily | 5c4b0d0 | 2017-03-04 00:19:55 -0500 | [diff] [blame] | 2390 | SET_SYS_FROM_STRING("_git", |
| 2391 | Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(), |
| 2392 | _Py_gitversion())); |
INADA Naoki | 6b42eb1 | 2017-06-29 15:31:38 +0900 | [diff] [blame] | 2393 | SET_SYS_FROM_STRING("_framework", PyUnicode_FromString(_PYTHONFRAMEWORK)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | SET_SYS_FROM_STRING("api_version", |
| 2395 | PyLong_FromLong(PYTHON_API_VERSION)); |
| 2396 | SET_SYS_FROM_STRING("copyright", |
| 2397 | PyUnicode_FromString(Py_GetCopyright())); |
| 2398 | SET_SYS_FROM_STRING("platform", |
| 2399 | PyUnicode_FromString(Py_GetPlatform())); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | SET_SYS_FROM_STRING("maxsize", |
| 2401 | PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 2402 | SET_SYS_FROM_STRING("float_info", |
| 2403 | PyFloat_GetInfo()); |
| 2404 | SET_SYS_FROM_STRING("int_info", |
| 2405 | PyLong_GetInfo()); |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2406 | /* initialize hash_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2407 | if (Hash_InfoType.tp_name == NULL) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2408 | if (PyStructSequence_InitType2(&Hash_InfoType, &hash_info_desc) < 0) { |
| 2409 | goto type_init_failed; |
| 2410 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2411 | } |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 2412 | SET_SYS_FROM_STRING("hash_info", |
| 2413 | get_hash_info()); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | SET_SYS_FROM_STRING("maxunicode", |
Ezio Melotti | 48a2f8f | 2011-09-29 00:18:19 +0300 | [diff] [blame] | 2415 | PyLong_FromLong(0x10FFFF)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | SET_SYS_FROM_STRING("builtin_module_names", |
| 2417 | list_builtin_module_names()); |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 2418 | #if PY_BIG_ENDIAN |
| 2419 | SET_SYS_FROM_STRING("byteorder", |
| 2420 | PyUnicode_FromString("big")); |
| 2421 | #else |
| 2422 | SET_SYS_FROM_STRING("byteorder", |
| 2423 | PyUnicode_FromString("little")); |
| 2424 | #endif |
Fred Drake | 099325e | 2000-08-14 15:47:03 +0000 | [diff] [blame] | 2425 | |
Guido van Rossum | 8b9ea87 | 1996-08-23 18:14:47 +0000 | [diff] [blame] | 2426 | #ifdef MS_COREDLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | SET_SYS_FROM_STRING("dllhandle", |
| 2428 | PyLong_FromVoidPtr(PyWin_DLLhModule)); |
| 2429 | SET_SYS_FROM_STRING("winver", |
| 2430 | PyUnicode_FromString(PyWin_DLLVersionString)); |
Guido van Rossum | c606fe1 | 1996-04-09 02:37:57 +0000 | [diff] [blame] | 2431 | #endif |
Barry Warsaw | 8cf4eae | 2010-10-16 01:04:07 +0000 | [diff] [blame] | 2432 | #ifdef ABIFLAGS |
| 2433 | SET_SYS_FROM_STRING("abiflags", |
| 2434 | PyUnicode_FromString(ABIFLAGS)); |
| 2435 | #endif |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 2436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | /* version_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2438 | if (VersionInfoType.tp_name == NULL) { |
| 2439 | if (PyStructSequence_InitType2(&VersionInfoType, |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2440 | &version_info_desc) < 0) { |
| 2441 | goto type_init_failed; |
| 2442 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2443 | } |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2444 | version_info = make_version_info(); |
| 2445 | SET_SYS_FROM_STRING("version_info", version_info); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | /* prevent user from creating new instances */ |
| 2447 | VersionInfoType.tp_init = NULL; |
| 2448 | VersionInfoType.tp_new = NULL; |
Antoine Pitrou | 871dfc4 | 2014-04-28 13:07:06 +0200 | [diff] [blame] | 2449 | res = PyDict_DelItemString(VersionInfoType.tp_dict, "__new__"); |
| 2450 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 2451 | PyErr_Clear(); |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 2452 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 2453 | /* implementation */ |
| 2454 | SET_SYS_FROM_STRING("implementation", make_impl_info(version_info)); |
| 2455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | /* flags */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2457 | if (FlagsType.tp_name == 0) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2458 | if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0) { |
| 2459 | goto type_init_failed; |
| 2460 | } |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2461 | } |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2462 | /* Set flags to their default values */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | SET_SYS_FROM_STRING("flags", make_flags()); |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 2464 | |
| 2465 | #if defined(MS_WINDOWS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | /* getwindowsversion */ |
| 2467 | if (WindowsVersionType.tp_name == 0) |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 2468 | if (PyStructSequence_InitType2(&WindowsVersionType, |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2469 | &windows_version_desc) < 0) { |
| 2470 | goto type_init_failed; |
| 2471 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2472 | /* prevent user from creating new instances */ |
| 2473 | WindowsVersionType.tp_init = NULL; |
| 2474 | WindowsVersionType.tp_new = NULL; |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2475 | assert(!PyErr_Occurred()); |
Antoine Pitrou | 871dfc4 | 2014-04-28 13:07:06 +0200 | [diff] [blame] | 2476 | res = PyDict_DelItemString(WindowsVersionType.tp_dict, "__new__"); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2477 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { |
Antoine Pitrou | 871dfc4 | 2014-04-28 13:07:06 +0200 | [diff] [blame] | 2478 | PyErr_Clear(); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2479 | } |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 2480 | #endif |
| 2481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | /* float repr style: 0.03 (short) vs 0.029999999999999999 (legacy) */ |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 2483 | #ifndef PY_NO_SHORT_FLOAT_REPR |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | SET_SYS_FROM_STRING("float_repr_style", |
| 2485 | PyUnicode_FromString("short")); |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 2486 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | SET_SYS_FROM_STRING("float_repr_style", |
| 2488 | PyUnicode_FromString("legacy")); |
Mark Dickinson | b08a53a | 2009-04-16 19:52:09 +0000 | [diff] [blame] | 2489 | #endif |
| 2490 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2491 | SET_SYS_FROM_STRING("thread_info", PyThread_GetInfo()); |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 2492 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2493 | /* initialize asyncgen_hooks */ |
| 2494 | if (AsyncGenHooksType.tp_name == NULL) { |
| 2495 | if (PyStructSequence_InitType2( |
| 2496 | &AsyncGenHooksType, &asyncgen_hooks_desc) < 0) { |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2497 | goto type_init_failed; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 2498 | } |
| 2499 | } |
| 2500 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2501 | if (PyErr_Occurred()) { |
| 2502 | goto err_occurred; |
| 2503 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 2504 | return _Py_INIT_OK(); |
| 2505 | |
| 2506 | type_init_failed: |
| 2507 | return _Py_INIT_ERR("failed to initialize a type"); |
| 2508 | |
| 2509 | err_occurred: |
| 2510 | return _Py_INIT_ERR("can't initialize sys module"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2513 | #undef SET_SYS_FROM_STRING |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2514 | |
| 2515 | /* Updating the sys namespace, returning integer error codes */ |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2516 | #define SET_SYS_FROM_STRING_INT_RESULT(key, value) \ |
| 2517 | do { \ |
| 2518 | PyObject *v = (value); \ |
| 2519 | if (v == NULL) \ |
| 2520 | return -1; \ |
| 2521 | res = PyDict_SetItemString(sysdict, key, v); \ |
| 2522 | Py_DECREF(v); \ |
| 2523 | if (res < 0) { \ |
| 2524 | return res; \ |
| 2525 | } \ |
| 2526 | } while (0) |
| 2527 | |
| 2528 | int |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2529 | _PySys_InitMain(PyInterpreterState *interp) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2530 | { |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2531 | PyObject *sysdict = interp->sysdict; |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2532 | const _PyCoreConfig *core_config = &interp->core_config; |
| 2533 | const _PyMainInterpreterConfig *config = &interp->config; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2534 | int res; |
| 2535 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2536 | /* _PyMainInterpreterConfig_Read() must set all these variables */ |
| 2537 | assert(config->module_search_path != NULL); |
| 2538 | assert(config->executable != NULL); |
| 2539 | assert(config->prefix != NULL); |
| 2540 | assert(config->base_prefix != NULL); |
| 2541 | assert(config->exec_prefix != NULL); |
| 2542 | assert(config->base_exec_prefix != NULL); |
| 2543 | |
Victor Stinner | 37cd982 | 2018-11-16 11:55:35 +0100 | [diff] [blame] | 2544 | #define COPY_LIST(KEY, ATTR) \ |
| 2545 | do { \ |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2546 | assert(PyList_Check(ATTR)); \ |
| 2547 | PyObject *list = PyList_GetSlice(ATTR, 0, PyList_GET_SIZE(ATTR)); \ |
Victor Stinner | 37cd982 | 2018-11-16 11:55:35 +0100 | [diff] [blame] | 2548 | if (list == NULL) { \ |
| 2549 | return -1; \ |
| 2550 | } \ |
| 2551 | SET_SYS_FROM_STRING_BORROW(KEY, list); \ |
| 2552 | Py_DECREF(list); \ |
| 2553 | } while (0) |
| 2554 | |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2555 | COPY_LIST("path", config->module_search_path); |
Victor Stinner | 37cd982 | 2018-11-16 11:55:35 +0100 | [diff] [blame] | 2556 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2557 | SET_SYS_FROM_STRING_BORROW("executable", config->executable); |
| 2558 | SET_SYS_FROM_STRING_BORROW("prefix", config->prefix); |
| 2559 | SET_SYS_FROM_STRING_BORROW("base_prefix", config->base_prefix); |
| 2560 | SET_SYS_FROM_STRING_BORROW("exec_prefix", config->exec_prefix); |
| 2561 | SET_SYS_FROM_STRING_BORROW("base_exec_prefix", config->base_exec_prefix); |
| 2562 | |
Carl Meyer | b193fa9 | 2018-06-15 22:40:56 -0600 | [diff] [blame] | 2563 | if (config->pycache_prefix != NULL) { |
| 2564 | SET_SYS_FROM_STRING_BORROW("pycache_prefix", config->pycache_prefix); |
| 2565 | } else { |
| 2566 | PyDict_SetItemString(sysdict, "pycache_prefix", Py_None); |
| 2567 | } |
| 2568 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2569 | if (config->argv != NULL) { |
| 2570 | SET_SYS_FROM_STRING_BORROW("argv", config->argv); |
| 2571 | } |
| 2572 | if (config->warnoptions != NULL) { |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2573 | COPY_LIST("warnoptions", config->warnoptions); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2574 | } |
| 2575 | if (config->xoptions != NULL) { |
Victor Stinner | 37cd982 | 2018-11-16 11:55:35 +0100 | [diff] [blame] | 2576 | PyObject *dict = PyDict_Copy(config->xoptions); |
| 2577 | if (dict == NULL) { |
| 2578 | return -1; |
| 2579 | } |
| 2580 | SET_SYS_FROM_STRING_BORROW("_xoptions", dict); |
| 2581 | Py_DECREF(dict); |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Victor Stinner | 37cd982 | 2018-11-16 11:55:35 +0100 | [diff] [blame] | 2584 | #undef COPY_LIST |
| 2585 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2586 | /* Set flags to their final values */ |
| 2587 | SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags()); |
| 2588 | /* prevent user from creating new instances */ |
| 2589 | FlagsType.tp_init = NULL; |
| 2590 | FlagsType.tp_new = NULL; |
| 2591 | res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); |
| 2592 | if (res < 0) { |
| 2593 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 2594 | return res; |
| 2595 | } |
| 2596 | PyErr_Clear(); |
| 2597 | } |
| 2598 | |
| 2599 | SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode", |
Victor Stinner | fbca908 | 2018-08-30 00:50:45 +0200 | [diff] [blame] | 2600 | PyBool_FromLong(!core_config->write_bytecode)); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2601 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 2602 | if (get_warnoptions() == NULL) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2603 | return -1; |
Victor Stinner | 865de27 | 2017-06-08 13:27:47 +0200 | [diff] [blame] | 2604 | |
Eric Snow | dae0276 | 2017-09-14 00:35:58 -0700 | [diff] [blame] | 2605 | if (get_xoptions() == NULL) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 2606 | return -1; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2607 | |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 2608 | /* Transfer any sys.warnoptions and sys._xoptions set directly |
| 2609 | * by an embedding application from the linked list to the module. */ |
| 2610 | if (_PySys_ReadPreInitOptions() != 0) |
| 2611 | return -1; |
| 2612 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2613 | if (PyErr_Occurred()) |
| 2614 | return -1; |
| 2615 | return 0; |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2616 | |
| 2617 | err_occurred: |
| 2618 | return -1; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
Victor Stinner | 41264f1 | 2017-12-15 02:05:29 +0100 | [diff] [blame] | 2621 | #undef SET_SYS_FROM_STRING_BORROW |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2622 | #undef SET_SYS_FROM_STRING_INT_RESULT |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 2623 | |
Victor Stinner | ab67281 | 2019-01-23 15:04:40 +0100 | [diff] [blame^] | 2624 | |
| 2625 | /* Set up a preliminary stderr printer until we have enough |
| 2626 | infrastructure for the io module in place. |
| 2627 | |
| 2628 | Use UTF-8/surrogateescape and ignore EAGAIN errors. */ |
| 2629 | _PyInitError |
| 2630 | _PySys_SetPreliminaryStderr(PyObject *sysdict) |
| 2631 | { |
| 2632 | PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr)); |
| 2633 | if (pstderr == NULL) { |
| 2634 | goto error; |
| 2635 | } |
| 2636 | if (_PyDict_SetItemId(sysdict, &PyId_stderr, pstderr) < 0) { |
| 2637 | goto error; |
| 2638 | } |
| 2639 | if (PyDict_SetItemString(sysdict, "__stderr__", pstderr) < 0) { |
| 2640 | goto error; |
| 2641 | } |
| 2642 | Py_DECREF(pstderr); |
| 2643 | return _Py_INIT_OK(); |
| 2644 | |
| 2645 | error: |
| 2646 | Py_XDECREF(pstderr); |
| 2647 | return _Py_INIT_ERR("can't set preliminary stderr"); |
| 2648 | } |
| 2649 | |
| 2650 | |
| 2651 | /* Create sys module without all attributes: _PySys_InitMain() should be called |
| 2652 | later to add remaining attributes. */ |
| 2653 | _PyInitError |
| 2654 | _PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p) |
| 2655 | { |
| 2656 | PyObject *modules = PyDict_New(); |
| 2657 | if (modules == NULL) { |
| 2658 | return _Py_INIT_ERR("can't make modules dictionary"); |
| 2659 | } |
| 2660 | interp->modules = modules; |
| 2661 | |
| 2662 | PyObject *sysmod = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION); |
| 2663 | if (sysmod == NULL) { |
| 2664 | return _Py_INIT_ERR("failed to create a module object"); |
| 2665 | } |
| 2666 | |
| 2667 | PyObject *sysdict = PyModule_GetDict(sysmod); |
| 2668 | if (sysdict == NULL) { |
| 2669 | return _Py_INIT_ERR("can't initialize sys dict"); |
| 2670 | } |
| 2671 | Py_INCREF(sysdict); |
| 2672 | interp->sysdict = sysdict; |
| 2673 | |
| 2674 | if (PyDict_SetItemString(sysdict, "modules", interp->modules) < 0) { |
| 2675 | return _Py_INIT_ERR("can't initialize sys module"); |
| 2676 | } |
| 2677 | |
| 2678 | _PyInitError err = _PySys_SetPreliminaryStderr(sysdict); |
| 2679 | if (_Py_INIT_FAILED(err)) { |
| 2680 | return err; |
| 2681 | } |
| 2682 | |
| 2683 | err = _PySys_InitCore(sysdict); |
| 2684 | if (_Py_INIT_FAILED(err)) { |
| 2685 | return err; |
| 2686 | } |
| 2687 | |
| 2688 | _PyImport_FixupBuiltin(sysmod, "sys", interp->modules); |
| 2689 | |
| 2690 | *sysmod_p = sysmod; |
| 2691 | return _Py_INIT_OK(); |
| 2692 | } |
| 2693 | |
| 2694 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 2695 | static PyObject * |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2696 | makepathobject(const wchar_t *path, wchar_t delim) |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 2697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | int i, n; |
| 2699 | const wchar_t *p; |
| 2700 | PyObject *v, *w; |
Tim Peters | 216b78b | 2006-01-06 02:40:53 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | n = 1; |
| 2703 | p = path; |
| 2704 | while ((p = wcschr(p, delim)) != NULL) { |
| 2705 | n++; |
| 2706 | p++; |
| 2707 | } |
| 2708 | v = PyList_New(n); |
| 2709 | if (v == NULL) |
| 2710 | return NULL; |
| 2711 | for (i = 0; ; i++) { |
| 2712 | p = wcschr(path, delim); |
| 2713 | if (p == NULL) |
| 2714 | p = path + wcslen(path); /* End of string */ |
| 2715 | w = PyUnicode_FromWideChar(path, (Py_ssize_t)(p - path)); |
| 2716 | if (w == NULL) { |
| 2717 | Py_DECREF(v); |
| 2718 | return NULL; |
| 2719 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 2720 | PyList_SET_ITEM(v, i, w); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | if (*p == '\0') |
| 2722 | break; |
| 2723 | path = p+1; |
| 2724 | } |
| 2725 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2729 | PySys_SetPath(const wchar_t *path) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | PyObject *v; |
| 2732 | if ((v = makepathobject(path, DELIM)) == NULL) |
| 2733 | Py_FatalError("can't create sys.path"); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2734 | if (_PySys_SetObjectId(&PyId_path, v) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | Py_FatalError("can't assign sys.path"); |
| 2736 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 2739 | static PyObject * |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 2740 | makeargvobject(int argc, wchar_t **argv) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | PyObject *av; |
| 2743 | if (argc <= 0 || argv == NULL) { |
| 2744 | /* Ensure at least one (empty) argument is seen */ |
| 2745 | static wchar_t *empty_argv[1] = {L""}; |
| 2746 | argv = empty_argv; |
| 2747 | argc = 1; |
| 2748 | } |
| 2749 | av = PyList_New(argc); |
| 2750 | if (av != NULL) { |
| 2751 | int i; |
| 2752 | for (i = 0; i < argc; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | PyObject *v = PyUnicode_FromWideChar(argv[i], -1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | if (v == NULL) { |
| 2755 | Py_DECREF(av); |
| 2756 | av = NULL; |
| 2757 | break; |
| 2758 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2759 | PyList_SET_ITEM(av, i, v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | return av; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2765 | void |
| 2766 | PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2767 | { |
| 2768 | PyObject *av = makeargvobject(argc, argv); |
| 2769 | if (av == NULL) { |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2770 | Py_FatalError("no mem for sys.argv"); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2771 | } |
| 2772 | if (PySys_SetObject("argv", av) != 0) { |
| 2773 | Py_DECREF(av); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2774 | Py_FatalError("can't assign sys.argv"); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2775 | } |
| 2776 | Py_DECREF(av); |
| 2777 | |
| 2778 | if (updatepath) { |
| 2779 | /* If argv[0] is not '-c' nor '-m', prepend argv[0] to sys.path. |
| 2780 | If argv[0] is a symlink, use the real path. */ |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2781 | PyObject *argv0 = _PyPathConfig_ComputeArgv0(argc, argv); |
| 2782 | if (argv0 == NULL) { |
| 2783 | Py_FatalError("can't compute path0 from argv"); |
| 2784 | } |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2785 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 2786 | PyObject *sys_path = _PySys_GetObjectId(&PyId_path); |
| 2787 | if (sys_path != NULL) { |
| 2788 | if (PyList_Insert(sys_path, 0, argv0) < 0) { |
| 2789 | Py_DECREF(argv0); |
| 2790 | Py_FatalError("can't prepend path0 to sys.path"); |
| 2791 | } |
| 2792 | } |
| 2793 | Py_DECREF(argv0); |
Victor Stinner | d5dda98 | 2017-12-13 17:31:16 +0100 | [diff] [blame] | 2794 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2795 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 2797 | void |
| 2798 | PySys_SetArgv(int argc, wchar_t **argv) |
| 2799 | { |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 2800 | PySys_SetArgvEx(argc, argv, Py_IsolatedFlag == 0); |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2803 | /* Reimplementation of PyFile_WriteString() no calling indirectly |
| 2804 | PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ |
| 2805 | |
| 2806 | static int |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2807 | sys_pyfile_write_unicode(PyObject *unicode, PyObject *file) |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2808 | { |
Victor Stinner | c3ccaae | 2016-08-20 01:24:22 +0200 | [diff] [blame] | 2809 | PyObject *writer = NULL, *result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | int err; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2811 | |
Victor Stinner | ecccc4f | 2010-06-08 20:46:00 +0000 | [diff] [blame] | 2812 | if (file == NULL) |
| 2813 | return -1; |
| 2814 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 2815 | writer = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | if (writer == NULL) |
| 2817 | goto error; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2818 | |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 2819 | result = PyObject_CallFunctionObjArgs(writer, unicode, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | if (result == NULL) { |
| 2821 | goto error; |
| 2822 | } else { |
| 2823 | err = 0; |
| 2824 | goto finally; |
| 2825 | } |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2826 | |
| 2827 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | err = -1; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2829 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | Py_XDECREF(writer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | Py_XDECREF(result); |
| 2832 | return err; |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2835 | static int |
| 2836 | sys_pyfile_write(const char *text, PyObject *file) |
| 2837 | { |
| 2838 | PyObject *unicode = NULL; |
| 2839 | int err; |
| 2840 | |
| 2841 | if (file == NULL) |
| 2842 | return -1; |
| 2843 | |
| 2844 | unicode = PyUnicode_FromString(text); |
| 2845 | if (unicode == NULL) |
| 2846 | return -1; |
| 2847 | |
| 2848 | err = sys_pyfile_write_unicode(unicode, file); |
| 2849 | Py_DECREF(unicode); |
| 2850 | return err; |
| 2851 | } |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
| 2854 | Adapted from code submitted by Just van Rossum. |
| 2855 | |
| 2856 | PySys_WriteStdout(format, ...) |
| 2857 | PySys_WriteStderr(format, ...) |
| 2858 | |
| 2859 | The first function writes to sys.stdout; the second to sys.stderr. When |
| 2860 | 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] | 2861 | no exceptions are raised. |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2862 | |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2863 | PyErr_CheckSignals() is not called to avoid the execution of the Python |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2864 | signal handlers: they may raise a new exception whereas sys_write() |
| 2865 | ignores all exceptions. |
Victor Stinner | 14284c2 | 2010-04-23 12:02:30 +0000 | [diff] [blame] | 2866 | |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2867 | Both take a printf-style format string as their first argument followed |
| 2868 | by a variable length argument list determined by the format string. |
| 2869 | |
| 2870 | *** WARNING *** |
| 2871 | |
| 2872 | The format should limit the total size of the formatted output string to |
| 2873 | 1000 bytes. In particular, this means that no unrestricted "%s" formats |
| 2874 | should occur; these should be limited using "%.<N>s where <N> is a |
| 2875 | decimal number calculated so that <N> plus the maximum size of other |
| 2876 | formatted text does not exceed 1000 bytes. Also watch out for "%f", |
| 2877 | which can print hundreds of digits for very large numbers. |
| 2878 | |
| 2879 | */ |
| 2880 | |
| 2881 | static void |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 2882 | 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] | 2883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | PyObject *file; |
| 2885 | PyObject *error_type, *error_value, *error_traceback; |
| 2886 | char buffer[1001]; |
| 2887 | int written; |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 2890 | file = _PySys_GetObjectId(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); |
| 2892 | if (sys_pyfile_write(buffer, file) != 0) { |
| 2893 | PyErr_Clear(); |
| 2894 | fputs(buffer, fp); |
| 2895 | } |
| 2896 | if (written < 0 || (size_t)written >= sizeof(buffer)) { |
| 2897 | const char *truncated = "... truncated"; |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2898 | if (sys_pyfile_write(truncated, file) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | fputs(truncated, fp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | } |
| 2901 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2905 | PySys_WriteStdout(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | va_list va; |
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_start(va, format); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2910 | sys_write(&PyId_stdout, stdout, format, va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | va_end(va); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | void |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2915 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | va_list va; |
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_start(va, format); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2920 | sys_write(&PyId_stderr, stderr, format, va); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2921 | va_end(va); |
| 2922 | } |
| 2923 | |
| 2924 | static void |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 2925 | 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] | 2926 | { |
| 2927 | PyObject *file, *message; |
| 2928 | PyObject *error_type, *error_value, *error_traceback; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2929 | const char *utf8; |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2930 | |
| 2931 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Victor Stinner | 0905437 | 2013-11-06 22:41:44 +0100 | [diff] [blame] | 2932 | file = _PySys_GetObjectId(key); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2933 | message = PyUnicode_FromFormatV(format, va); |
| 2934 | if (message != NULL) { |
| 2935 | if (sys_pyfile_write_unicode(message, file) != 0) { |
| 2936 | PyErr_Clear(); |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2937 | utf8 = PyUnicode_AsUTF8(message); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2938 | if (utf8 != NULL) |
| 2939 | fputs(utf8, fp); |
| 2940 | } |
| 2941 | Py_DECREF(message); |
| 2942 | } |
| 2943 | PyErr_Restore(error_type, error_value, error_traceback); |
| 2944 | } |
| 2945 | |
| 2946 | void |
| 2947 | PySys_FormatStdout(const char *format, ...) |
| 2948 | { |
| 2949 | va_list va; |
| 2950 | |
| 2951 | va_start(va, format); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2952 | sys_format(&PyId_stdout, stdout, format, va); |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 2953 | va_end(va); |
| 2954 | } |
| 2955 | |
| 2956 | void |
| 2957 | PySys_FormatStderr(const char *format, ...) |
| 2958 | { |
| 2959 | va_list va; |
| 2960 | |
| 2961 | va_start(va, format); |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 2962 | sys_format(&PyId_stderr, stderr, format, va); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | va_end(va); |
Guido van Rossum | a890e68 | 1998-05-12 14:59:24 +0000 | [diff] [blame] | 2964 | } |