[Python-checkins] r67511 - in python/trunk: Doc/library/logging.rst Lib/logging/__init__.py Lib/test/test_logging.py Misc/NEWS

Nick Coghlan ncoghlan at gmail.com
Thu Dec 4 12:07:02 CET 2008


vinay.sajip wrote:
> +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:
> +        import warnings
> +        s = warnings.formatwarning(message, category, filename, lineno, line)
> +        logger = getLogger("py.warnings")
> +        if not logger.handlers:
> +            logger.addHandler(NullHandler())
> +        logger.warning("%s", s)

I'd be careful here - this could deadlock if a thread spawned as a side
effect of importing a module happens to trigger a warning.

warnings is pulled into sys.modules as part of the interpreter startup -
having a global "import warnings" shouldn't have any real effect on
logging's import time.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-checkins mailing list