当前位置: 首页>>代码示例>>Python>>正文


Python warnings.showwarning方法代码示例

本文整理汇总了Python中warnings.showwarning方法的典型用法代码示例。如果您正苦于以下问题:Python warnings.showwarning方法的具体用法?Python warnings.showwarning怎么用?Python warnings.showwarning使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在warnings的用法示例。


在下文中一共展示了warnings.showwarning方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _showwarning

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def _showwarning(message, category, filename, lineno, file=None, line=None):
    """
    Implementation of showwarnings which redirects to logging, which will first
    check to see if the file parameter is None. If a file is specified, it will
    delegate to the original warnings implementation of showwarning. Otherwise,
    it will call warnings.formatwarning and will log the resulting string to a
    warnings logger named "py.warnings" with level logging.WARNING.
    """
    if file is not None:
        if _warnings_showwarning is not None:
            _warnings_showwarning(message, category, filename, lineno, file, line)
    else:
        s = warnings.formatwarning(message, category, filename, lineno, line)
        logger = getLogger("py.warnings")
        if not logger.handlers:
            logger.addHandler(NullHandler())
        logger.warning("%s", s) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:19,代码来源:__init__.py

示例2: test_warnings

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def test_warnings(self):
        with warnings.catch_warnings():
            logging.captureWarnings(True)
            try:
                warnings.filterwarnings("always", category=UserWarning)
                file = cStringIO.StringIO()
                h = logging.StreamHandler(file)
                logger = logging.getLogger("py.warnings")
                logger.addHandler(h)
                warnings.warn("I'm warning you...")
                logger.removeHandler(h)
                s = file.getvalue()
                h.close()
                self.assertTrue(s.find("UserWarning: I'm warning you...\n") > 0)

                #See if an explicit file uses the original implementation
                file = cStringIO.StringIO()
                warnings.showwarning("Explicit", UserWarning, "dummy.py", 42,
                                        file, "Dummy line")
                s = file.getvalue()
                file.close()
                self.assertEqual(s,
                        "dummy.py:42: UserWarning: Explicit\n  Dummy line\n")
            finally:
                logging.captureWarnings(False) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test_logging.py

示例3: __enter__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __enter__(self):
        if self._entered:
            raise RuntimeError("Cannot enter %r twice" % self)
        self._entered = True
        self._filters = self._module.filters
        self._module.filters = self._filters[:]
        self._showwarning = self._module.showwarning
        if self._record:
            log = []

            def showwarning(*args, **kwargs):
                log.append(WarningMessage(*args, **kwargs))
            self._module.showwarning = showwarning
            return log
        else:
            return None 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:18,代码来源:utils.py

示例4: idle_showwarning

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def idle_showwarning(
        message, category, filename, lineno, file=None, line=None):
    """Show Idle-format warning (after replacing warnings.showwarning).

    The differences are the formatter called, the file=None replacement,
    which can be None, the capture of the consequence AttributeError,
    and the output of a hard-coded prompt.
    """
    if file is None:
        file = warning_stream
    try:
        file.write(idle_formatwarning(
                message, category, filename, lineno, line=line))
        file.write(">>> ")
    except (AttributeError, IOError):
        pass  # if file (probably __stderr__) is invalid, skip warning. 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:18,代码来源:PyShell.py

示例5: execute

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def execute(self):
        def showwarning(message, category, filename, fileno, file=None, line=None):
            msg = warnings.formatwarning(message, category, filename, fileno, line)
            self.log.warning(msg.strip())

        with warnings.catch_warnings():
            warnings.simplefilter("always")
            warnings.showwarning = showwarning

            try:
                return self.run()
            except SystemExit:
                raise
            except:
                self.log.critical(traceback.format_exc().strip())
                sys.exit(1) 
开发者ID:abusesa,项目名称:abusehelper,代码行数:18,代码来源:bot.py

示例6: captureWarnings

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def captureWarnings(capture):
    """
    If capture is true, redirect all warnings to the logging package.
    If capture is False, ensure that warnings are not redirected to logging
    but to their original destinations.
    """
    global _warnings_showwarning
    if capture:
        if _warnings_showwarning is None:
            _warnings_showwarning = warnings.showwarning
            warnings.showwarning = _showwarning
    else:
        if _warnings_showwarning is not None:
            warnings.showwarning = _warnings_showwarning
            _warnings_showwarning = None 
开发者ID:war-and-code,项目名称:jawfish,代码行数:17,代码来源:__init__.py

示例7: __enter__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __enter__(self):
        if self._entered:
            raise RuntimeError("cannot enter suppress_warnings twice.")

        self._orig_show = warnings.showwarning
        self._filters = warnings.filters
        warnings.filters = self._filters[:]

        self._entered = True
        self._tmp_suppressions = []
        self._tmp_modules = set()
        self._forwarded = set()

        self.log = []  # reset global log (no need to keep same list)

        for cat, mess, _, mod, log in self._suppressions:
            if log is not None:
                del log[:]  # clear the log
            if mod is None:
                warnings.filterwarnings(
                    "always", category=cat, message=mess)
            else:
                module_regex = mod.__name__.replace('.', r'\.') + '$'
                warnings.filterwarnings(
                    "always", category=cat, message=mess,
                    module=module_regex)
                self._tmp_modules.add(mod)
        warnings.showwarning = self._showwarning
        self._clear_registries()

        return self 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:33,代码来源:utils.py

示例8: __exit__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __exit__(self, *exc_info):
        warnings.showwarning = self._orig_show
        warnings.filters = self._filters
        self._clear_registries()
        self._entered = False
        del self._orig_show
        del self._filters 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:9,代码来源:utils.py

示例9: install_warning_logger

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def install_warning_logger():
    # Enable our Deprecation Warnings
    warnings.simplefilter("default", PipDeprecationWarning, append=True)

    global _warnings_showwarning

    if _warnings_showwarning is None:
        _warnings_showwarning = warnings.showwarning
        warnings.showwarning = _showwarning 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:11,代码来源:deprecation.py

示例10: __enter__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __enter__(self):
            if self._entered:
                raise RuntimeError("cannot enter suppress_warnings twice.")

            self._orig_show = warnings.showwarning
            self._filters = warnings.filters
            warnings.filters = self._filters[:]

            self._entered = True
            self._tmp_suppressions = []
            self._tmp_modules = set()
            self._forwarded = set()

            self.log = []  # reset global log (no need to keep same list)

            for cat, mess, _, mod, log in self._suppressions:
                if log is not None:
                    del log[:]  # clear the log
                if mod is None:
                    warnings.filterwarnings(
                        "always", category=cat, message=mess)
                else:
                    module_regex = mod.__name__.replace('.', r'\.') + '$'
                    warnings.filterwarnings(
                        "always", category=cat, message=mess,
                        module=module_regex)
                    self._tmp_modules.add(mod)
            warnings.showwarning = self._showwarning
            self._clear_registries()

            return self 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:33,代码来源:_numpy_compat.py

示例11: __exit__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __exit__(self, *exc_info):
            warnings.showwarning = self._orig_show
            warnings.filters = self._filters
            self._clear_registries()
            self._entered = False
            del self._orig_show
            del self._filters 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:9,代码来源:_numpy_compat.py

示例12: __exit__

# 需要导入模块: import warnings [as 别名]
# 或者: from warnings import showwarning [as 别名]
def __exit__(self):
        if not self._entered:
            raise RuntimeError("Cannot exit %r without entering first" % self)
        self._module.filters = self._filters
        self._module.showwarning = self._showwarning 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:7,代码来源:utils.py


注:本文中的warnings.showwarning方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。