(message, category=None, stacklevel=1, source=None)
Issue a warning, or maybe ignore it or raise an exception.
Issue a warning, or maybe ignore it or raise an exception.
Issue a warning or maybe ignore it or raise an exception .
def warn(message, category=None, stacklevel=1, source=None):
    """Issue a warning, or maybe ignore it or raise an exception."""
    # Check if message is already a Warning object
    if isinstance(message, Warning):
        category = message.__class__
    # Check category argument
    if category is None:
        category = UserWarning
    if not (isinstance(category, type) and issubclass(category, Warning)):
        raise TypeError("category must be a Warning subclass, "
                        "not '{:s}'".format(type(category).__name__))
    # Get context information
    try:
        if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)):
            # If frame is too small to care or if the warning originated in
            # internal code, then do not try to hide any frames.
            frame = sys._getframe(stacklevel)
        else:
            frame = sys._getframe(1)
            # Look for one frame less since the above line starts us off.
            for x in range(stacklevel-1):
                frame = _next_external_frame(frame)
                if frame is None:
                    raise ValueError
    except ValueError:
        globals = sys.__dict__
        filename = "sys"
        lineno = 1
    else:
        globals = frame.f_globals
        filename = frame.f_code.co_filename
        lineno = frame.f_lineno
    if '__name__' in globals:
        module = globals['__name__']
    else:
        module = "<string>"
    registry = globals.setdefault("__warningregistry__", {})
    warn_explicit(message, category, filename, lineno, module, registry,
                  globals, source)
def warn ( message , category = None , stacklevel = 1 , source = None ) : # Check if message is already a Warning object if isinstance ( message , Warning ) : category = message . __class__ # Check category argument if category is None : category = UserWarning if not ( isinstance ( category , type ) and issubclass ( category , Warning ) ) : raise TypeError ( "category must be a Warning subclass, " "not '{:s}'" . format ( type ( category ) . __name__ ) ) # Get context information try : if stacklevel <= 1 or _is_internal_frame ( sys . _getframe ( 1 ) ) : # If frame is too small to care or if the warning originated in # internal code, then do not try to hide any frames. frame = sys . _getframe ( stacklevel ) else : frame = sys . _getframe ( 1 ) # Look for one frame less since the above line starts us off. for x in range ( stacklevel - 1 ) : frame = _next_external_frame ( frame ) if frame is None : raise ValueError except ValueError : globals = sys . __dict__ filename = "sys" lineno = 1 else : globals = frame . f_globals filename = frame . f_code . co_filename lineno = frame . f_lineno if '__name__' in globals : module = globals [ '__name__' ] else : module = "<string>" registry = globals . setdefault ( "__warningregistry__" , { } ) warn_explicit ( message , category , filename , lineno , module , registry , globals , source )