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/_warnings.c at main · python/cpython Expand file tree/ Copy path_warnings.cMore file actions 1815 lines (1604 loc) · 51.9 KB / Copy path_warnings.cFile metadata and controls1815 lines (1604 loc) · 51.9 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 #include "Python.h" #include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() #include "pycore_frame.h" // PyFrameObject #include "pycore_genobject.h" // PyAsyncGenObject #include "pycore_import.h" // _PyImport_GetModules() #include "pycore_interpframe.h" // _PyFrame_GetCode() #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_object.h" // _Py_XGetRef() #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_ACQUIRE() #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_traceback.h" // _Py_DisplaySourceLine() #include "pycore_tuple.h" // _PyTuple_FromPair #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString() #include "pycore_warnings.h" // _PyWarnings_AfterFork() #include <stdbool.h> #include "clinic/_warnings.c.h" #define MODULE_NAME "_warnings" PyDoc_STRVAR(warnings__doc__, MODULE_NAME " provides basic warning filtering support.\n" "It is a helper module to speed up interpreter start-up."); /*************************************************************************/ typedef struct _warnings_runtime_state WarningsState; static inline int check_interp(PyInterpreterState *interp) { if (interp == NULL) { PyErr_SetString(PyExc_RuntimeError, "warnings_get_state: could not identify " "current interpreter"); return 0; } return 1; } static inline PyInterpreterState * get_current_interp(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); return check_interp(interp) ? interp : NULL; } static inline PyThreadState * get_current_tstate(void) { PyThreadState *tstate = _PyThreadState_GET(); if (tstate == NULL) { (void)check_interp(NULL); return NULL; } return check_interp(tstate->interp) ? tstate : NULL; } /* Given a module object, get its per-module state. */ static WarningsState * warnings_get_state(PyInterpreterState *interp) { return &interp->warnings; } /* Clear the given warnings module state. */ static void warnings_clear_state(WarningsState *st) { Py_CLEAR(st->filters); Py_CLEAR(st->once_registry); Py_CLEAR(st->default_action); Py_CLEAR(st->context); } #ifndef Py_DEBUG static PyObject * create_filter(PyObject *category, PyObject *action_str, const char *modname) { PyObject *modname_obj = NULL; /* Default to "no module name" for initial filter set */ if (modname != NULL) { modname_obj = PyUnicode_InternFromString(modname); if (modname_obj == NULL) { return NULL; } } else { modname_obj = Py_NewRef(Py_None); } /* This assumes the line number is zero for now. */ PyObject *filter = PyTuple_Pack(5, action_str, Py_None, category, modname_obj, _PyLong_GetZero()); Py_DECREF(modname_obj); return filter; } #endif static PyObject * init_filters(PyInterpreterState *interp) { #ifdef Py_DEBUG /* Py_DEBUG builds show all warnings by default */ return PyList_New(0); #else /* Other builds ignore a number of warning categories by default */ PyObject *filters = PyList_New(5); if (filters == NULL) { return NULL; } size_t pos = 0; /* Post-incremented in each use. */ #define ADD(TYPE, ACTION, MODNAME) \ PyList_SET_ITEM(filters, pos++, \ create_filter(TYPE, &_Py_ID(ACTION), MODNAME)); ADD(PyExc_DeprecationWarning, default, "__main__"); ADD(PyExc_DeprecationWarning, ignore, NULL); ADD(PyExc_PendingDeprecationWarning, ignore, NULL); ADD(PyExc_ImportWarning, ignore, NULL); ADD(PyExc_ResourceWarning, ignore, NULL); #undef ADD for (size_t x = 0; x < pos; x++) { if (PyList_GET_ITEM(filters, x) == NULL) { Py_DECREF(filters); return NULL; } } return filters; #endif } /* Initialize the given warnings module state. */ int _PyWarnings_InitState(PyInterpreterState *interp) { WarningsState *st = &interp->warnings; if (st->filters == NULL) { st->filters = init_filters(interp); if (st->filters == NULL) { return -1; } PyUnstable_EnableTryIncRef(st->filters); } if (st->once_registry == NULL) { st->once_registry = PyDict_New(); if (st->once_registry == NULL) { return -1; } PyUnstable_EnableTryIncRef(st->once_registry); } if (st->default_action == NULL) { st->default_action = PyUnicode_FromString("default"); if (st->default_action == NULL) { return -1; } PyUnstable_EnableTryIncRef(st->default_action); } if (st->context == NULL) { st->context = PyContextVar_New("_warnings_context", NULL); if (st->context == NULL) { return -1; } } st->filters_version = 0; return 0; } /*************************************************************************/ static int check_matched(PyInterpreterState *interp, PyObject *obj, PyObject *arg, PyObject *arg2) { PyObject *result; int rc; /* A 'None' filter always matches */ if (obj == Py_None) return 1; /* An internal plain text default filter must match exactly */ if (PyUnicode_CheckExact(obj)) { if (arg == NULL) { return 0; } int cmp_result = PyUnicode_Compare(obj, arg); if (cmp_result == -1 && PyErr_Occurred()) { return -1; } return !cmp_result; } /* Otherwise assume a regex filter and call its match() method */ if (arg != NULL) { result = PyObject_CallMethodOneArg(obj, &_Py_ID(match), arg); } else { PyObject *match = PyImport_ImportModuleAttrString("_py_warnings", "_match_filename"); if (match == NULL) { return -1; } result = PyObject_CallFunctionObjArgs(match, obj, arg2, NULL); Py_DECREF(match); } if (result == NULL) return -1; rc = PyObject_IsTrue(result); Py_DECREF(result); return rc; } #define GET_WARNINGS_ATTR(interp, ATTR, try_import) \ get_warnings_attr(interp, &_Py_ID(ATTR), try_import) /* Returns a new reference. A NULL return value can mean false or an error. */ static PyObject * get_warnings_attr(PyInterpreterState *interp, PyObject *attr, int try_import) { PyObject *warnings_module, *obj; /* don't try to import after the start of the Python finallization */ if (try_import && !_Py_IsInterpreterFinalizing(interp)) { warnings_module = PyImport_Import(&_Py_ID(warnings)); if (warnings_module == NULL) { /* Fallback to the C implementation if we cannot get the Python implementation */ if (PyErr_ExceptionMatches(PyExc_ImportError)) { PyErr_Clear(); } return NULL; } } else { /* if we're so late into Python finalization that the module dict is gone, then we can't even use PyImport_GetModule without triggering an interpreter abort. */ if (!_PyImport_GetModules(interp)) { return NULL; } warnings_module = PyImport_GetModule(&_Py_ID(warnings)); if (warnings_module == NULL) return NULL; } (void)PyObject_GetOptionalAttr(warnings_module, attr, &obj); Py_DECREF(warnings_module); return obj; } static inline void warnings_lock(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); _PyRecursiveMutex_Lock(&st->lock); } static inline int warnings_unlock(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); return _PyRecursiveMutex_TryUnlock(&st->lock); } static inline bool warnings_lock_held(WarningsState *st) { return PyMutex_IsLocked(&st->lock.mutex); } #ifdef HAVE_FORK void _PyWarnings_BeforeFork(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); st->lock_held_at_fork = _PyRecursiveMutex_IsLockedByCurrentThread(&st->lock); } void _PyWarnings_AfterFork(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); _PyRecursiveMutex_at_fork_reinit(&st->lock, st->lock_held_at_fork); st->lock_held_at_fork = false; } #endif static PyObject * get_warnings_context(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(PyContextVar_CheckExact(st->context)); PyObject *ctx; if (PyContextVar_Get(st->context, NULL, &ctx) < 0) { return NULL; } if (ctx == NULL) { Py_RETURN_NONE; } return ctx; } static PyObject * get_warnings_context_filters(PyInterpreterState *interp) { PyObject *ctx = get_warnings_context(interp); if (ctx == NULL) { return NULL; } if (ctx == Py_None) { Py_RETURN_NONE; } PyObject *context_filters = PyObject_GetAttr(ctx, &_Py_ID(_filters)); Py_DECREF(ctx); if (context_filters == NULL) { return NULL; } if (!PyList_Check(context_filters)) { PyErr_SetString(PyExc_ValueError, "_filters of warnings._warnings_context must be a list"); Py_DECREF(context_filters); return NULL; } return context_filters; } /* Store 'value' in the warnings state field '*ptr'. */ static void update_state_ref(PyObject **ptr, PyObject *value) { if (FT_ATOMIC_LOAD_PTR_ACQUIRE(*ptr) == value) { return; } PyUnstable_EnableTryIncRef(value); PyObject *old = _Py_atomic_exchange_ptr(ptr, Py_NewRef(value)); Py_XDECREF(old); } /* Return a new reference to the warnings state field '*ptr'. */ static PyObject * get_state_ref(PyObject **ptr) { #ifdef Py_GIL_DISABLED return _Py_XGetRef(ptr); #else return Py_XNewRef(*ptr); #endif } // Returns a new reference to the list. static PyObject * get_warnings_filters(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); PyObject *warnings_filters = GET_WARNINGS_ATTR(interp, filters, 0); if (warnings_filters == NULL) { if (PyErr_Occurred()) return NULL; warnings_filters = get_state_ref(&st->filters); } else { update_state_ref(&st->filters, warnings_filters); } PyObject *filters = warnings_filters; if (filters == NULL || !PyList_Check(filters)) { PyErr_SetString(PyExc_ValueError, MODULE_NAME ".filters must be a list"); Py_XDECREF(filters); return NULL; } return filters; } /*[clinic input] _acquire_lock as warnings_acquire_lock [clinic start generated code]*/ static PyObject * warnings_acquire_lock_impl(PyObject *module) /*[clinic end generated code: output=594313457d1bf8e1 input=46ec20e55acca52f]*/ { PyInterpreterState *interp = get_current_interp(); if (interp == NULL) { return NULL; } warnings_lock(interp); Py_RETURN_NONE; } /*[clinic input] _release_lock as warnings_release_lock [clinic start generated code]*/ static PyObject * warnings_release_lock_impl(PyObject *module) /*[clinic end generated code: output=d73d5a8789396750 input=ea01bb77870c5693]*/ { PyInterpreterState *interp = get_current_interp(); if (interp == NULL) { return NULL; } if (warnings_unlock(interp) < 0) { PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); return NULL; } Py_RETURN_NONE; } // Returns a new reference. static PyObject * get_once_registry(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); PyObject *registry = GET_WARNINGS_ATTR(interp, onceregistry, 0); if (registry == NULL) { if (PyErr_Occurred()) return NULL; registry = get_state_ref(&st->once_registry); assert(registry != NULL); return registry; } if (!PyDict_Check(registry)) { PyErr_Format(PyExc_TypeError, MODULE_NAME ".onceregistry must be a dict, " "not '%.200s'", Py_TYPE(registry)->tp_name); Py_DECREF(registry); return NULL; } update_state_ref(&st->once_registry, registry); return registry; } // Returns a new reference. static PyObject * get_default_action(PyInterpreterState *interp) { WarningsState *st = warnings_get_state(interp); assert(st != NULL); PyObject *default_action = GET_WARNINGS_ATTR(interp, defaultaction, 0); if (default_action == NULL) { if (PyErr_Occurred()) { return NULL; } default_action = get_state_ref(&st->default_action); assert(default_action != NULL); return default_action; } if (!PyUnicode_Check(default_action)) { PyErr_Format(PyExc_TypeError, MODULE_NAME ".defaultaction must be a string, " "not '%.200s'", Py_TYPE(default_action)->tp_name); Py_DECREF(default_action); return NULL; } update_state_ref(&st->default_action, default_action); return default_action; } /* Search filters list of match, returns false on error. If no match * then 'matched_action' is NULL. */ static bool filter_search(PyInterpreterState *interp, PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject *filename, char *list_name, PyObject *filters, PyObject **item, PyObject **matched_action) { bool result = true; *matched_action = NULL; /* Avoid the filters list changing while we iterate over it. */ Py_BEGIN_CRITICAL_SECTION(filters); for (Py_ssize_t i = 0; i < PyList_GET_SIZE(filters); i++) { PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; Py_ssize_t ln; int is_subclass, good_msg, good_mod; tmp_item = PyList_GET_ITEM(filters, i); if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) { PyErr_Format(PyExc_ValueError, "warnings.%s item %zd isn't a 5-tuple", list_name, i); result = false; break; } /* Python code: action, msg, cat, mod, ln = item */ Py_INCREF(tmp_item); action = PyTuple_GET_ITEM(tmp_item, 0); msg = PyTuple_GET_ITEM(tmp_item, 1); cat = PyTuple_GET_ITEM(tmp_item, 2); mod = PyTuple_GET_ITEM(tmp_item, 3); ln_obj = PyTuple_GET_ITEM(tmp_item, 4); if (!PyUnicode_Check(action)) { PyErr_Format(PyExc_TypeError, "action must be a string, not '%.200s'", Py_TYPE(action)->tp_name); Py_DECREF(tmp_item); result = false; break; } good_msg = check_matched(interp, msg, text, NULL); if (good_msg == -1) { Py_DECREF(tmp_item); result = false; break; } good_mod = check_matched(interp, mod, module, filename); if (good_mod == -1) { Py_DECREF(tmp_item); result = false; break; } is_subclass = PyObject_IsSubclass(category, cat); if (is_subclass == -1) { Py_DECREF(tmp_item); result = false; break; } ln = PyLong_AsSsize_t(ln_obj); if (ln == -1 && PyErr_Occurred()) { Py_DECREF(tmp_item); result = false; break; } if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) { *item = tmp_item; *matched_action = action; result = true; break; } Py_DECREF(tmp_item); } Py_END_CRITICAL_SECTION(); return result; } /* The returned action and the item are new references. */ static PyObject* get_filter(PyInterpreterState *interp, PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject *filename, PyObject **item) { /* check _warning_context _filters list */ PyObject *context_filters = get_warnings_context_filters(interp); if (context_filters == NULL) { return NULL; } bool use_global_filters = false; if (context_filters == Py_None) { use_global_filters = true; } else { PyObject *context_action = NULL; if (!filter_search(interp, category, text, lineno, module, filename, "_warnings_context _filters", context_filters, item, &context_action)) { Py_DECREF(context_filters); return NULL; } Py_DECREF(context_filters); if (context_action != NULL) { return Py_NewRef(context_action); } } PyObject *action; if (use_global_filters) { /* check warnings.filters list */ PyObject *filters = get_warnings_filters(interp); if (filters == NULL) { return NULL; } bool ok = filter_search(interp, category, text, lineno, module, filename, "filters", filters, item, &action); Py_DECREF(filters); if (!ok) { return NULL; } if (action != NULL) { return Py_NewRef(action); } } action = get_default_action(interp); if (action != NULL) { *item = Py_NewRef(Py_None); return action; } return NULL; } /* Returns 1 if 'registry' is stamped with 'version', 0 if not, -1 on error. */ static int registry_version_matches(PyObject *registry, Py_ssize_t version) { PyObject *version_obj; if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { return -1; } int rc = 0; if (version_obj != NULL && PyLong_CheckExact(version_obj)) { int overflow; long long v = PyLong_AsLongLongAndOverflow(version_obj, &overflow); rc = (!overflow && v == version); } Py_XDECREF(version_obj); return rc; } static int set_registry_version(PyObject *registry, Py_ssize_t version) { PyObject *version_obj = PyLong_FromSsize_t(version); if (version_obj == NULL) { return -1; } int rc = PyDict_SetItem(registry, &_Py_ID(version), version_obj); Py_DECREF(version_obj); return rc; } /* Make 'registry' usable for filters 'version', clearing it if it is stamped with a different version. Returns 1 if it can be used, 0 if 'version' is no longer current, -1 on error. */ static int check_registry_version(PyInterpreterState *interp, PyObject *registry, Py_ssize_t version) { _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(registry); WarningsState *st = warnings_get_state(interp); int rc = registry_version_matches(registry, version); if (rc != 0) { return rc; } if (version != FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version)) { // The caller's filter search is stale, don't record its result. return 0; } PyDict_Clear(registry); return set_registry_version(registry, version) < 0 ? -1 : 1; } static int registry_contains(PyObject *registry, PyObject *key) { PyObject *value; if (PyDict_GetItemRef(registry, key, &value) < 0) { return -1; } if (value == NULL) { return 0; } int rc = PyObject_IsTrue(value); Py_DECREF(value); return rc; } /* Returns 1 if 'key' is recorded in 'registry' for the current filters version, 0 if not, -1 on error. Stores the version checked in '*pversion'. */ static int already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, Py_ssize_t *pversion) { int rc; Py_BEGIN_CRITICAL_SECTION(registry); WarningsState *st = warnings_get_state(interp); Py_ssize_t version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); *pversion = version; rc = registry_version_matches(registry, version); if (rc > 0) { rc = registry_contains(registry, key); } else if (rc == 0) { /* Stamped with an old version, reset it. 'key' can't be present. */ PyDict_Clear(registry); if (set_registry_version(registry, version) < 0) { rc = -1; } } Py_END_CRITICAL_SECTION(); return rc; } /* Returns 1 if 'key' was already in 'registry', 0 if it was added, -1 on error. */ static int registry_add(PyObject *registry, PyObject *key) { int rc = registry_contains(registry, key); if (rc == 0 && PyDict_SetItem(registry, key, Py_True) < 0) { rc = -1; } return rc; } /* Record 'key' and 'altkey' (either may be NULL) in 'registry' for filters 'version'. Returns 0 if the warning should be shown, 1 if it was already recorded, -1 on error. */ static int record_warning(PyInterpreterState *interp, PyObject *registry, PyObject *key, PyObject *altkey, Py_ssize_t version) { int rc; Py_BEGIN_CRITICAL_SECTION(registry); rc = check_registry_version(interp, registry, version); if (rc > 0) { rc = 0; if (key != NULL) { rc = registry_add(registry, key); } if (rc == 0 && altkey != NULL) { rc = registry_add(registry, altkey); } if (rc >= 0) { /* The critical section may have been suspended while the keys were added, letting another thread clear and re-stamp the registry. If so, invalidate it. */ int match = registry_version_matches(registry, version); if (match < 0 || (match == 0 && set_registry_version(registry, -1) < 0)) { rc = -1; } } } Py_END_CRITICAL_SECTION(); return rc; } static void show_warning(PyThreadState *tstate, PyObject *filename, int lineno, PyObject *text, PyObject *category, PyObject *sourceline) { PyObject *f_stderr = NULL; PyObject *name; char lineno_str[128]; PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); name = PyObject_GetAttr(category, &_Py_ID(__name__)); if (name == NULL) { goto error; } if (PySys_GetOptionalAttr(&_Py_ID(stderr), &f_stderr) <= 0) { fprintf(stderr, "lost sys.stderr\n"); goto error; } /* Print "filename:lineno: category: text\n" */ if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString(lineno_str, f_stderr) < 0) goto error; if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString(": ", f_stderr) < 0) goto error; if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString("\n", f_stderr) < 0) goto error; Py_CLEAR(name); /* Print " source_line\n" */ if (sourceline) { int kind; const void *data; Py_ssize_t i, len; Py_UCS4 ch; PyObject *truncated; kind = PyUnicode_KIND(sourceline); data = PyUnicode_DATA(sourceline); len = PyUnicode_GET_LENGTH(sourceline); for (i=0; i<len; i++) { ch = PyUnicode_READ(kind, data, i); if (ch != ' ' && ch != '\t' && ch != '\014') break; } truncated = PyUnicode_Substring(sourceline, i, len); if (truncated == NULL) goto error; PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW); Py_DECREF(truncated); PyFile_WriteString("\n", f_stderr); } else { _Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL); } error: Py_XDECREF(f_stderr); Py_XDECREF(name); PyErr_Clear(); } static int call_show_warning(PyThreadState *tstate, PyObject *category, PyObject *text, PyObject *message, PyObject *filename, int lineno, PyObject *lineno_obj, PyObject *sourceline, PyObject *source, PyObject *module) { PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; PyInterpreterState *interp = tstate->interp; /* The Python implementation is able to log the traceback where the source was allocated, whereas the C implementation doesn't. */ show_fn = GET_WARNINGS_ATTR(interp, _showwarnmsg, 1); if (show_fn == NULL) { if (PyErr_Occurred()) return -1; show_warning(tstate, filename, lineno, text, category, sourceline); return 0; } if (!PyCallable_Check(show_fn)) { PyErr_SetString(PyExc_TypeError, "warnings._showwarnmsg() must be set to a callable"); goto error; } warnmsg_cls = GET_WARNINGS_ATTR(interp, WarningMessage, 0); if (warnmsg_cls == NULL) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "unable to get warnings.WarningMessage"); } goto error; } msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, filename, lineno_obj, Py_None, sourceline ? sourceline : Py_None, source ? source : Py_None, module, NULL); Py_DECREF(warnmsg_cls); if (msg == NULL) goto error; res = PyObject_CallOneArg(show_fn, msg); Py_DECREF(show_fn); Py_DECREF(msg); if (res == NULL) return -1; Py_DECREF(res); return 0; error: Py_XDECREF(show_fn); return -1; } static PyObject * warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry, PyObject *sourceline, PyObject *source) { PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; PyObject *item = NULL, *action = NULL; PyObject *altkey = NULL, *once_registry = NULL; int rc; PyInterpreterState *interp = tstate->interp; /* module can be None if a warning is emitted late during Python shutdown. In this case, the Python warnings module was probably unloaded, filters are no more available to choose as action. It is safer to ignore the warning and do nothing. */ if (module == Py_None) Py_RETURN_NONE; if (registry && !PyDict_Check(registry) && (registry != Py_None)) { PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None"); return NULL; } /* Normalize message. */ Py_INCREF(message); /* DECREF'ed in cleanup. */ if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) { text = PyObject_Str(message); if (text == NULL) goto cleanup; category = (PyObject*)Py_TYPE(message); } else { text = message; message = PyObject_CallOneArg(category, message); if (message == NULL) goto cleanup; } lineno_obj = PyLong_FromLong(lineno); if (lineno_obj == NULL) goto cleanup; if (source == Py_None) { source = NULL; } /* Create key. */ key = PyTuple_Pack(3, text, category, lineno_obj); if (key == NULL) goto cleanup; /* The warnings lock is not held. 'version' ties the registry updates to the filters searched; a result from filters that changed since is never recorded. */ Py_ssize_t version; if ((registry != NULL) && (registry != Py_None)) { rc = already_warned(interp, registry, key, &version); if (rc == -1) goto cleanup; else if (rc == 1) goto return_none; /* Else this warning hasn't been generated before. */ } else { registry = NULL; WarningsState *st = warnings_get_state(interp); version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); } action = get_filter(interp, category, text, lineno, module, filename, &item); if (action == NULL) goto cleanup; if (_PyUnicode_EqualToASCIIString(action, "error")) { PyErr_SetObject(category, message); goto cleanup; } if (_PyUnicode_EqualToASCIIString(action, "ignore")) { goto return_none; } /* Store in the registry that we've been here, *except* when the action is "always" or "all". */ if (!_PyUnicode_EqualToASCIIString(action, "always") && !_PyUnicode_EqualToASCIIString(action, "all")) { bool is_once = _PyUnicode_EqualToASCIIString(action, "once"); bool is_module = _PyUnicode_EqualToASCIIString(action, "module"); if (!is_once && !is_module && !_PyUnicode_EqualToASCIIString(action, "default")) { PyErr_Format(PyExc_RuntimeError, "Unrecognized action (%R) in warnings.filters:\n %R", action, item); goto cleanup; } if (is_once && registry == NULL) { /* WarningsState.once_registry[(text, category)] = 1 */ once_registry = get_once_registry(interp); if (once_registry == NULL) goto cleanup; altkey = _PyTuple_FromPair(text, category); if (altkey == NULL) goto cleanup; rc = record_warning(interp, once_registry, NULL, altkey, version); } else if (registry != NULL) { /* registry[key] = 1, and for "once" and "module" also registry[(text, category)] = 1 */ if (is_once || is_module) { altkey = _PyTuple_FromPair(text, category); |
| 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. |