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