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