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