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