GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/sysmodule.c at main · python/cpython Expand file tree/ Copy pathsysmodule.cMore file actions 4784 lines (4052 loc) · 130 KB / Copy pathsysmodule.cFile metadata and controls4784 lines (4052 loc) · 130 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* System module */ /* Various bits of information used by the interpreter are collected in module 'sys'. Function member: - exit(sts): raise SystemExit Data members: - stdin, stdout, stderr: standard file objects - modules: the table of modules (dictionary) - path: module search path (list of strings) - argv: script arguments (list of strings) - ps1, ps2: optional primary and secondary prompts (strings) */ #include "Python.h" #include "pycore_audit.h" // _Py_AuditHookEntry #include "pycore_call.h" // _PyObject_CallNoArgs() #include "pycore_ceval.h" // _PyEval_SetAsyncGenFinalizer() #include "pycore_frame.h" // _PyInterpreterFrame #include "pycore_import.h" // _PyImport_SetDLOpenFlags() #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() #include "pycore_interpframe.h" // _PyFrame_GetFirstComplete() #include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD #include "pycore_modsupport.h" // _PyModule_CreateInitialized() #include "pycore_namespace.h" // _PyNamespace_New() #include "pycore_object.h" // _PyObject_DebugTypeStats() #include "pycore_optimizer.h" // _PyDumpExecutors() #include "pycore_pathconfig.h" // _PyPathConfig_ComputeSysPath0() #include "pycore_pyerrors.h" // _PyErr_GetRaisedException() #include "pycore_pylifecycle.h" // _PyErr_WriteUnraisableDefaultHook() #include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR #include "pycore_pymem.h" // _PyMem_DefaultRawFree() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_pystats.h" // _Py_PrintSpecializationStats() #include "pycore_runtime.h" // _PyRuntimeState_Get*() #include "pycore_structseq.h" // _PyStructSequence_InitBuiltinWithFlags() #include "pycore_sysmodule.h" // export _PySys_GetSizeOf() #include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal() #include "pydtrace.h" // PyDTrace_AUDIT() #include "osdefs.h" // DELIM #include "stdlib_module_names.h" // _Py_stdlib_module_names #ifdef HAVE_UNISTD_H # include <unistd.h> // getpid() #endif #ifdef MS_WINDOWS # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN # endif # include <windows.h> #endif /* MS_WINDOWS */ #ifdef MS_COREDLL extern void *PyWin_DLLhModule; /* A string loaded from the DLL at startup: */ extern const char *PyWin_DLLVersionString; #endif #ifdef __EMSCRIPTEN__ # include <emscripten.h> #endif #ifdef __wasi__ # include <wasi/version.h> #endif #ifdef HAVE_FCNTL_H # include <fcntl.h> #endif /*[clinic input] module sys [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3726b388feee8cea]*/ #include "clinic/sysmodule.c.h" PyObject * PySys_GetAttr(PyObject *name) { if (!PyUnicode_Check(name)) { PyErr_Format(PyExc_TypeError, "attribute name must be string, not '%T'", name); return NULL; } PyThreadState *tstate = _PyThreadState_GET(); PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { PyErr_SetString(PyExc_RuntimeError, "no sys module"); return NULL; } PyObject *value; if (PyDict_GetItemRef(sysdict, name, &value) == 0) { PyErr_Format(PyExc_RuntimeError, "lost sys.%U", name); } return value; } PyObject * PySys_GetAttrString(const char *name) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { PyErr_SetString(PyExc_RuntimeError, "no sys module"); return NULL; } PyObject *value; if (PyDict_GetItemStringRef(sysdict, name, &value) == 0) { PyErr_Format(PyExc_RuntimeError, "lost sys.%s", name); } return value; } int PySys_GetOptionalAttr(PyObject *name, PyObject **value) { if (!PyUnicode_Check(name)) { PyErr_Format(PyExc_TypeError, "attribute name must be string, not '%T'", name); *value = NULL; return -1; } PyThreadState *tstate = _PyThreadState_GET(); PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { *value = NULL; return 0; } return PyDict_GetItemRef(sysdict, name, value); } int PySys_GetOptionalAttrString(const char *name, PyObject **value) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { *value = NULL; return 0; } return PyDict_GetItemStringRef(sysdict, name, value); } PyObject * PySys_GetObject(const char *name) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { return NULL; } PyObject *exc = _PyErr_GetRaisedException(tstate); PyObject *value; (void) PyDict_GetItemStringRef(sysdict, name, &value); /* XXX Suppress a new exception if it was raised and restore * the old one. */ if (_PyErr_Occurred(tstate)) { PyErr_FormatUnraisable("Exception ignored in PySys_GetObject()"); } _PyErr_SetRaisedException(tstate, exc); Py_XDECREF(value); // return a borrowed reference return value; } static int sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v) { if (key == NULL) { return -1; } PyObject *sd = interp->sysdict; if (sd == NULL) { PyErr_SetString(PyExc_RuntimeError, "no sys module"); return -1; } if (v == NULL) { if (PyDict_Pop(sd, key, NULL) < 0) { return -1; } return 0; } else { return PyDict_SetItem(sd, key, v); } } int _PySys_SetAttr(PyObject *key, PyObject *v) { PyInterpreterState *interp = _PyInterpreterState_GET(); return sys_set_object(interp, key, v); } static int sys_set_object_str(PyInterpreterState *interp, const char *name, PyObject *v) { PyObject *key = v ? PyUnicode_InternFromString(name) : PyUnicode_FromString(name); int r = sys_set_object(interp, key, v); Py_XDECREF(key); return r; } int PySys_SetObject(const char *name, PyObject *v) { PyInterpreterState *interp = _PyInterpreterState_GET(); return sys_set_object_str(interp, name, v); } int _PySys_ClearAttrString(PyInterpreterState *interp, const char *name, int verbose) { if (verbose) { PySys_WriteStderr("# clear sys.%s\n", name); } /* To play it safe, we set the attr to None instead of deleting it. */ if (PyDict_SetItemString(interp->sysdict, name, Py_None) < 0) { return -1; } return 0; } static int should_audit(PyInterpreterState *interp) { /* interp must not be NULL, but test it just in case for extra safety */ assert(interp != NULL); if (!interp) { return 0; } return (interp->runtime->audit_hooks.head || interp->audit_hooks || PyDTrace_AUDIT_ENABLED()); } static int sys_audit_tstate(PyThreadState *ts, const char *event, const char *argFormat, va_list vargs) { assert(event != NULL); assert(!argFormat || !strchr(argFormat, 'N')); if (!ts) { /* Audit hooks cannot be called with a NULL thread state */ return 0; } /* The current implementation cannot be called if tstate is not the current Python thread state. */ assert(ts == _PyThreadState_GET()); /* Early exit when no hooks are registered */ PyInterpreterState *is = ts->interp; if (!should_audit(is)) { return 0; } PyObject *eventName = NULL; PyObject *eventArgs = NULL; PyObject *hooks = NULL; PyObject *hook = NULL; int res = -1; int dtrace = PyDTrace_AUDIT_ENABLED(); PyObject *exc = _PyErr_GetRaisedException(ts); /* Initialize event args now */ if (argFormat && argFormat[0]) { eventArgs = Py_VaBuildValue(argFormat, vargs); if (eventArgs && !PyTuple_Check(eventArgs)) { PyObject *argTuple = PyTuple_Pack(1, eventArgs); Py_SETREF(eventArgs, argTuple); } } else { eventArgs = PyTuple_New(0); } if (!eventArgs) { goto exit; } /* Call global hooks * * We don't worry about any races on hooks getting added, * since that would not leave is in an inconsistent state. */ _Py_AuditHookEntry *e = is->runtime->audit_hooks.head; for (; e; e = e->next) { if (e->hookCFunction(event, eventArgs, e->userData) < 0) { goto exit; } } /* Dtrace USDT point */ if (dtrace) { PyDTrace_AUDIT(event, (void *)eventArgs); } /* Call interpreter hooks */ if (is->audit_hooks) { eventName = PyUnicode_FromString(event); if (!eventName) { goto exit; } hooks = PyObject_GetIter(is->audit_hooks); if (!hooks) { goto exit; } /* Disallow tracing in hooks unless explicitly enabled */ PyThreadState_EnterTracing(ts); while ((hook = PyIter_Next(hooks)) != NULL) { PyObject *o; int canTrace = PyObject_GetOptionalAttr(hook, &_Py_ID(__cantrace__), &o); if (o) { canTrace = PyObject_IsTrue(o); Py_DECREF(o); } if (canTrace < 0) { break; } if (canTrace) { PyThreadState_LeaveTracing(ts); } PyObject* args[2] = {eventName, eventArgs}; o = _PyObject_VectorcallTstate(ts, hook, args, 2, NULL); if (canTrace) { PyThreadState_EnterTracing(ts); } if (!o) { break; } Py_DECREF(o); Py_CLEAR(hook); } PyThreadState_LeaveTracing(ts); if (_PyErr_Occurred(ts)) { goto exit; } } res = 0; exit: Py_XDECREF(hook); Py_XDECREF(hooks); Py_XDECREF(eventName); Py_XDECREF(eventArgs); if (!res) { _PyErr_SetRaisedException(ts, exc); } else { assert(_PyErr_Occurred(ts)); Py_XDECREF(exc); } return res; } int _PySys_Audit(PyThreadState *tstate, const char *event, const char *argFormat, ...) { va_list vargs; va_start(vargs, argFormat); int res = sys_audit_tstate(tstate, event, argFormat, vargs); va_end(vargs); return res; } int PySys_Audit(const char *event, const char *argFormat, ...) { PyThreadState *tstate = _PyThreadState_GET(); va_list vargs; va_start(vargs, argFormat); int res = sys_audit_tstate(tstate, event, argFormat, vargs); va_end(vargs); return res; } int PySys_AuditTuple(const char *event, PyObject *args) { if (args == NULL) { return PySys_Audit(event, NULL); } if (!PyTuple_Check(args)) { PyErr_Format(PyExc_TypeError, "args must be tuple, got %s", Py_TYPE(args)->tp_name); return -1; } return PySys_Audit(event, "O", args); } /* We expose this function primarily for our own cleanup during * finalization. In general, it should not need to be called, * and as such the function is not exported. * * Must be finalizing to clear hooks */ void _PySys_ClearAuditHooks(PyThreadState *ts) { assert(ts != NULL); if (!ts) { return; } _PyRuntimeState *runtime = ts->interp->runtime; /* The hooks are global so we have to check for runtime finalization. */ PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); assert(finalizing == ts); if (finalizing != ts) { return; } const PyConfig *config = _PyInterpreterState_GetConfig(ts->interp); if (config->verbose) { PySys_WriteStderr("# clear sys.audit hooks\n"); } /* Hooks can abort later hooks for this event, but cannot abort the clear operation itself. */ _PySys_Audit(ts, "cpython._PySys_ClearAuditHooks", NULL); _PyErr_Clear(ts); /* We don't worry about the very unlikely race right here, * since it's entirely benign. Nothing else removes entries * from the list and adding an entry right now would not cause * any trouble. */ _Py_AuditHookEntry *e = runtime->audit_hooks.head, *n; runtime->audit_hooks.head = NULL; while (e) { n = e->next; free(e); e = n; } } static void add_audit_hook_entry_unlocked(_PyRuntimeState *runtime, _Py_AuditHookEntry *entry) { if (runtime->audit_hooks.head == NULL) { runtime->audit_hooks.head = entry; } else { _Py_AuditHookEntry *last = runtime->audit_hooks.head; while (last->next) { last = last->next; } last->next = entry; } } int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData) { /* tstate can be NULL, so access directly _PyRuntime: PySys_AddAuditHook() can be called before Python is initialized. */ _PyRuntimeState *runtime = &_PyRuntime; PyThreadState *tstate; if (_PyRuntimeState_GetInitialized(runtime)) { tstate = _PyThreadState_GET(); } else { tstate = NULL; } /* Invoke existing audit hooks to allow them an opportunity to abort. */ /* Cannot invoke hooks until we are initialized */ if (tstate != NULL) { if (_PySys_Audit(tstate, "sys.addaudithook", NULL) < 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_RuntimeError)) { /* We do not report errors derived from RuntimeError */ _PyErr_Clear(tstate); return 0; } return -1; } } /* Use malloc(), not PyMem_RawMalloc(): a hook can be added before Py_Initialize(), and the entry is freed during finalization, after PYTHONMALLOC may have changed the raw allocator (and, in debug builds, the allocator under the debug hooks). */ _Py_AuditHookEntry *e = (_Py_AuditHookEntry*)malloc( sizeof(_Py_AuditHookEntry)); if (!e) { if (tstate != NULL) { _PyErr_NoMemory(tstate); } return -1; } e->next = NULL; e->hookCFunction = (Py_AuditHookFunction)hook; e->userData = userData; PyMutex_Lock(&runtime->audit_hooks.mutex); add_audit_hook_entry_unlocked(runtime, e); PyMutex_Unlock(&runtime->audit_hooks.mutex); return 0; } /*[clinic input] sys.addaudithook hook: object Adds a new audit hook callback. [clinic start generated code]*/ static PyObject * sys_addaudithook_impl(PyObject *module, PyObject *hook) /*[clinic end generated code: output=4f9c17aaeb02f44e input=0f3e191217a45e34]*/ { PyThreadState *tstate = _PyThreadState_GET(); /* Invoke existing audit hooks to allow them an opportunity to abort. */ if (_PySys_Audit(tstate, "sys.addaudithook", NULL) < 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_RuntimeError)) { /* We do not report errors derived from RuntimeError */ _PyErr_Clear(tstate); Py_RETURN_NONE; } return NULL; } PyInterpreterState *interp = tstate->interp; if (interp->audit_hooks == NULL) { interp->audit_hooks = PyList_New(0); if (interp->audit_hooks == NULL) { return NULL; } /* Avoid having our list of hooks show up in the GC module */ PyObject_GC_UnTrack(interp->audit_hooks); } if (PyList_Append(interp->audit_hooks, hook) < 0) { return NULL; } Py_RETURN_NONE; } /*[clinic input] sys.audit event: str / *args: tuple Passes the event to any audit hooks that are attached. [clinic start generated code]*/ static PyObject * sys_audit_impl(PyObject *module, const char *event, PyObject *args) /*[clinic end generated code: output=1d0fc82da768f49d input=ec3b688527945109]*/ { PyThreadState *tstate = _PyThreadState_GET(); _Py_EnsureTstateNotNULL(tstate); if (!should_audit(tstate->interp)) { Py_RETURN_NONE; } int res = _PySys_Audit(tstate, event, "O", args); if (res < 0) { return NULL; } Py_RETURN_NONE; } static PyObject * sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords) { PyThreadState *tstate = _PyThreadState_GET(); assert(!_PyErr_Occurred(tstate)); char *envar = Py_GETENV("PYTHONBREAKPOINT"); if (envar == NULL || strlen(envar) == 0) { envar = "pdb.set_trace"; } else if (!strcmp(envar, "0")) { /* The breakpoint is explicitly no-op'd. */ Py_RETURN_NONE; } /* According to POSIX the string returned by getenv() might be invalidated * or the string content might be overwritten by a subsequent call to * getenv(). Since importing a module can performs the getenv() calls, * we need to save a copy of envar. */ envar = _PyMem_RawStrdup(envar); if (envar == NULL) { _PyErr_NoMemory(tstate); return NULL; } const char *last_dot = strrchr(envar, '.'); const char *attrname = NULL; PyObject *modulepath = NULL; if (last_dot == NULL) { /* The breakpoint is a built-in, e.g. PYTHONBREAKPOINT=int */ modulepath = &_Py_ID(builtins); attrname = envar; } else if (last_dot != envar) { /* Split on the last dot; */ modulepath = PyUnicode_FromStringAndSize(envar, last_dot - envar); attrname = last_dot + 1; } else { goto warn; } if (modulepath == NULL) { PyMem_RawFree(envar); return NULL; } PyObject *module = PyImport_Import(modulepath); Py_DECREF(modulepath); if (module == NULL) { if (_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) { goto warn; } PyMem_RawFree(envar); return NULL; } PyObject *hook = PyObject_GetAttrString(module, attrname); Py_DECREF(module); if (hook == NULL) { if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { goto warn; } PyMem_RawFree(envar); return NULL; } PyMem_RawFree(envar); PyObject *retval = PyObject_Vectorcall(hook, args, nargs, keywords); Py_DECREF(hook); return retval; warn: /* If any of the imports went wrong, then warn and ignore. */ _PyErr_Clear(tstate); int status = PyErr_WarnFormat( PyExc_RuntimeWarning, 0, "Ignoring unimportable $PYTHONBREAKPOINT: \"%s\"", envar); PyMem_RawFree(envar); if (status < 0) { /* Printing the warning raised an exception. */ return NULL; } /* The warning was (probably) issued. */ Py_RETURN_NONE; } PyDoc_STRVAR(breakpointhook_doc, "breakpointhook($module, /, *args, **kwargs)\n" "--\n" "\n" "This hook function is called by built-in breakpoint().\n" ); /* Write repr(o) to sys.stdout using sys.stdout.encoding and 'backslashreplace' error handler. If sys.stdout has a buffer attribute, use sys.stdout.buffer.write(encoded), otherwise redecode the string and use sys.stdout.write(redecoded). Helper function for sys_displayhook(). */ static int sys_displayhook_unencodable(PyObject *outf, PyObject *o) { PyObject *stdout_encoding = NULL; PyObject *encoded, *escaped_str, *repr_str, *buffer, *result; const char *stdout_encoding_str; int ret; stdout_encoding = PyObject_GetAttr(outf, &_Py_ID(encoding)); if (stdout_encoding == NULL) goto error; stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding); if (stdout_encoding_str == NULL) goto error; repr_str = PyObject_Repr(o); if (repr_str == NULL) goto error; encoded = PyUnicode_AsEncodedString(repr_str, stdout_encoding_str, "backslashreplace"); Py_DECREF(repr_str); if (encoded == NULL) goto error; if (PyObject_GetOptionalAttr(outf, &_Py_ID(buffer), &buffer) < 0) { Py_DECREF(encoded); goto error; } if (buffer) { result = PyObject_CallMethodOneArg(buffer, &_Py_ID(write), encoded); Py_DECREF(buffer); Py_DECREF(encoded); if (result == NULL) goto error; Py_DECREF(result); } else { escaped_str = PyUnicode_FromEncodedObject(encoded, stdout_encoding_str, "strict"); Py_DECREF(encoded); if (PyFile_WriteObject(escaped_str, outf, Py_PRINT_RAW) != 0) { Py_DECREF(escaped_str); goto error; } Py_DECREF(escaped_str); } ret = 0; goto finally; error: ret = -1; finally: Py_XDECREF(stdout_encoding); return ret; } /*[clinic input] sys.displayhook object as o: object / Print an object to sys.stdout and also save it in builtins._ [clinic start generated code]*/ static PyObject * sys_displayhook(PyObject *module, PyObject *o) /*[clinic end generated code: output=347477d006df92ed input=08ba730166d7ef72]*/ { PyObject *outf; PyObject *builtins; PyThreadState *tstate = _PyThreadState_GET(); builtins = PyImport_GetModule(&_Py_ID(builtins)); if (builtins == NULL) { if (!_PyErr_Occurred(tstate)) { _PyErr_SetString(tstate, PyExc_RuntimeError, "lost builtins module"); } return NULL; } Py_DECREF(builtins); /* Print value except if None */ /* After printing, also assign to '_' */ /* Before, set '_' to None to avoid recursion */ if (o == Py_None) { Py_RETURN_NONE; } if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), Py_None) != 0) return NULL; outf = PySys_GetAttr(&_Py_ID(stdout)); if (outf == NULL) { return NULL; } if (outf == Py_None) { _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.stdout"); Py_DECREF(outf); return NULL; } if (PyFile_WriteObject(o, outf, 0) != 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_UnicodeEncodeError)) { int err; /* repr(o) is not encodable to sys.stdout.encoding with * sys.stdout.errors error handler (which is probably 'strict') */ _PyErr_Clear(tstate); err = sys_displayhook_unencodable(outf, o); if (err) { Py_DECREF(outf); return NULL; } } else { Py_DECREF(outf); return NULL; } } if (PyFile_WriteObject(_Py_LATIN1_CHR('\n'), outf, Py_PRINT_RAW) != 0) { Py_DECREF(outf); return NULL; } Py_DECREF(outf); if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), o) != 0) { return NULL; } Py_RETURN_NONE; } /*[clinic input] sys.excepthook exctype: object value: object traceback: object / Handle an exception by displaying it with a traceback on sys.stderr. [clinic start generated code]*/ static PyObject * sys_excepthook_impl(PyObject *module, PyObject *exctype, PyObject *value, PyObject *traceback) /*[clinic end generated code: output=18d99fdda21b6b5e input=ecf606fa826f19d9]*/ { PyErr_Display(NULL, value, traceback); Py_RETURN_NONE; } /*[clinic input] sys.exception Return the current exception. Return the most recent exception caught by an except clause in the current stack frame or in an older stack frame, or None if no such exception exists. [clinic start generated code]*/ static PyObject * sys_exception_impl(PyObject *module) /*[clinic end generated code: output=2381ee2f25953e40 input=c88fbb94b6287431]*/ { _PyErr_StackItem *err_info = _PyErr_GetTopmostException(_PyThreadState_GET()); if (err_info->exc_value != NULL) { return Py_NewRef(err_info->exc_value); } Py_RETURN_NONE; } /*[clinic input] sys.exc_info Return current exception information: (type, value, traceback). Return information about the most recent exception caught by an except clause in the current stack frame or in an older stack frame. [clinic start generated code]*/ static PyObject * sys_exc_info_impl(PyObject *module) /*[clinic end generated code: output=3afd0940cf3a4d30 input=b5c5bf077788a3e5]*/ { _PyErr_StackItem *err_info = _PyErr_GetTopmostException(_PyThreadState_GET()); return _PyErr_StackItemToExcInfoTuple(err_info); } /*[clinic input] sys.unraisablehook unraisable: object / Handle an unraisable exception. The unraisable argument has the following attributes: * exc_type: Exception type. * exc_value: Exception value, can be None. * exc_traceback: Exception traceback, can be None. * err_msg: Error message, can be None. * object: Object causing the exception, can be None. [clinic start generated code]*/ static PyObject * sys_unraisablehook(PyObject *module, PyObject *unraisable) /*[clinic end generated code: output=bb92838b32abaa14 input=ec3af148294af8d3]*/ { return _PyErr_WriteUnraisableDefaultHook(unraisable); } /*[clinic input] sys.exit status: object = None / Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure). [clinic start generated code]*/ static PyObject * sys_exit_impl(PyObject *module, PyObject *status) /*[clinic end generated code: output=13870986c1ab2ec0 input=b86ca9497baa94f2]*/ { /* Raise SystemExit so callers may catch it or clean up. */ PyErr_SetObject(PyExc_SystemExit, status); return NULL; } static PyObject * get_utf8_unicode(void) { _Py_DECLARE_STR(utf_8, "utf-8"); PyObject *ret = &_Py_STR(utf_8); return Py_NewRef(ret); } /*[clinic input] sys.getdefaultencoding Return the current default encoding used by the Unicode implementation. [clinic start generated code]*/ static PyObject * sys_getdefaultencoding_impl(PyObject *module) /*[clinic end generated code: output=256d19dfcc0711e6 input=d416856ddbef6909]*/ { return get_utf8_unicode(); } /*[clinic input] sys.getfilesystemencoding Return the encoding used to convert Unicode filenames to OS filenames. [clinic start generated code]*/ static PyObject * sys_getfilesystemencoding_impl(PyObject *module) /*[clinic end generated code: output=1dc4bdbe9be44aa7 input=8475f8649b8c7d8c]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); const PyConfig *config = _PyInterpreterState_GetConfig(interp); if (wcscmp(config->filesystem_encoding, L"utf-8") == 0) { return get_utf8_unicode(); } PyObject *u = PyUnicode_FromWideChar(config->filesystem_encoding, -1); if (u == NULL) { return NULL; } _PyUnicode_InternImmortal(interp, &u); return u; } /*[clinic input] sys.getfilesystemencodeerrors Return the error mode used Unicode to OS filename conversion. [clinic start generated code]*/ static PyObject * sys_getfilesystemencodeerrors_impl(PyObject *module) /*[clinic end generated code: output=ba77b36bbf7c96f5 input=22a1e8365566f1e5]*/ { PyInterpreterState *interp = _PyInterpreterState_GET(); const PyConfig *config = _PyInterpreterState_GetConfig(interp); PyObject *u = PyUnicode_FromWideChar(config->filesystem_errors, -1); if (u == NULL) { return NULL; } _PyUnicode_InternImmortal(interp, &u); return u; } /*[clinic input] sys.intern |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |