MCPcopy Index your code
hub / github.com/python/cpython / warn

Function warn

Lib/_py_warnings.py:465–520  ·  view source on GitHub ↗

Issue a warning, or maybe ignore it or raise an exception.

(message, category=None, stacklevel=1, source=None,
         *, skip_file_prefixes=())

Source from the content-addressed store, hash-verified

463
464# Code typically replaced by _warnings
465def warn(message, category=None, stacklevel=1, source=None,
466 *, skip_file_prefixes=()):
467 """Issue a warning, or maybe ignore it or raise an exception."""
468 # Check if message is already a Warning object
469 if isinstance(message, Warning):
470 category = message.__class__
471 # Check category argument
472 if category is None:
473 category = UserWarning
474 elif not isinstance(category, type):
475 raise TypeError(f"category must be a Warning subclass, not "
476 f"'{type(category).__name__}'")
477 elif not issubclass(category, Warning):
478 raise TypeError(f"category must be a Warning subclass, not "
479 f"class '{category.__name__}'")
480 if not isinstance(skip_file_prefixes, tuple):
481 # The C version demands a tuple for implementation performance.
482 raise TypeError('skip_file_prefixes must be a tuple of strs.')
483 if skip_file_prefixes:
484 stacklevel = max(2, stacklevel)
485 # Get context information
486 try:
487 if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)):
488 # If frame is too small to care or if the warning originated in
489 # internal code, then do not try to hide any frames.
490 frame = sys._getframe(stacklevel)
491 else:
492 frame = sys._getframe(1)
493 # Look for one frame less since the above line starts us off.
494 for x in range(stacklevel-1):
495 frame = _next_external_frame(frame, skip_file_prefixes)
496 if frame is None:
497 raise ValueError
498 except ValueError:
499 globals = sys.__dict__
500 filename = "<sys>"
501 lineno = 0
502 else:
503 globals = frame.f_globals
504 filename = frame.f_code.co_filename
505 lineno = frame.f_lineno
506 if '__name__' in globals:
507 module = globals['__name__']
508 else:
509 module = "<string>"
510 registry = globals.setdefault("__warningregistry__", {})
511 _wm.warn_explicit(
512 message,
513 category,
514 filename,
515 lineno,
516 module,
517 registry,
518 globals,
519 source=source,
520 )
521
522

Callers 15

_get_module_detailsFunction · 0.90
_writeMethod · 0.90
adapt_dateFunction · 0.90
adapt_datetimeFunction · 0.90
convert_dateFunction · 0.90
convert_timestampFunction · 0.90
__serverMethod · 0.90
__lineMethod · 0.90
__stateMethod · 0.90
__greetingMethod · 0.90
__mailfromMethod · 0.90

Calls 3

_next_external_frameFunction · 0.85
_is_internal_frameFunction · 0.70
setdefaultMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…