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