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