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