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