mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
gh-103606: Improve error message from logging.config.FileConfig (GH-103628)
This commit is contained in:
parent
c5b670efd1
commit
152227b569
3 changed files with 60 additions and 6 deletions
|
@ -87,6 +87,10 @@ in :mod:`logging` itself) and defining handlers which are declared either in
|
||||||
provides a mechanism to present the choices and load the chosen
|
provides a mechanism to present the choices and load the chosen
|
||||||
configuration).
|
configuration).
|
||||||
|
|
||||||
|
It will raise :exc:`FileNotFoundError` if the file
|
||||||
|
doesn't exist and :exc:`ValueError` if the file is invalid or
|
||||||
|
empty.
|
||||||
|
|
||||||
:param fname: A filename, or a file-like object, or an instance derived
|
:param fname: A filename, or a file-like object, or an instance derived
|
||||||
from :class:`~configparser.RawConfigParser`. If a
|
from :class:`~configparser.RawConfigParser`. If a
|
||||||
``RawConfigParser``-derived instance is passed, it is used as
|
``RawConfigParser``-derived instance is passed, it is used as
|
||||||
|
@ -126,6 +130,10 @@ in :mod:`logging` itself) and defining handlers which are declared either in
|
||||||
.. versionadded:: 3.10
|
.. versionadded:: 3.10
|
||||||
The *encoding* parameter is added.
|
The *encoding* parameter is added.
|
||||||
|
|
||||||
|
.. versionadded:: 3.12
|
||||||
|
An exception will be thrown if the provided file
|
||||||
|
doesn't exist or is invalid or empty.
|
||||||
|
|
||||||
.. function:: listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)
|
.. function:: listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)
|
||||||
|
|
||||||
Starts up a socket server on the specified port, and listens for new
|
Starts up a socket server on the specified port, and listens for new
|
||||||
|
|
|
@ -29,6 +29,7 @@ import functools
|
||||||
import io
|
import io
|
||||||
import logging
|
import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
|
import os
|
||||||
import queue
|
import queue
|
||||||
import re
|
import re
|
||||||
import struct
|
import struct
|
||||||
|
@ -60,15 +61,24 @@ def fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=Non
|
||||||
"""
|
"""
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
|
if isinstance(fname, str):
|
||||||
|
if not os.path.exists(fname):
|
||||||
|
raise FileNotFoundError(f"{fname} doesn't exist")
|
||||||
|
elif not os.path.getsize(fname):
|
||||||
|
raise ValueError(f'{fname} is an empty file')
|
||||||
|
|
||||||
if isinstance(fname, configparser.RawConfigParser):
|
if isinstance(fname, configparser.RawConfigParser):
|
||||||
cp = fname
|
cp = fname
|
||||||
else:
|
else:
|
||||||
cp = configparser.ConfigParser(defaults)
|
try:
|
||||||
if hasattr(fname, 'readline'):
|
cp = configparser.ConfigParser(defaults)
|
||||||
cp.read_file(fname)
|
if hasattr(fname, 'readline'):
|
||||||
else:
|
cp.read_file(fname)
|
||||||
encoding = io.text_encoding(encoding)
|
else:
|
||||||
cp.read(fname, encoding=encoding)
|
encoding = io.text_encoding(encoding)
|
||||||
|
cp.read(fname, encoding=encoding)
|
||||||
|
except configparser.ParsingError as e:
|
||||||
|
raise ValueError(f'{fname} is invalid: {e}')
|
||||||
|
|
||||||
formatters = _create_formatters(cp)
|
formatters = _create_formatters(cp)
|
||||||
|
|
||||||
|
|
|
@ -1756,6 +1756,42 @@ class ConfigFileTest(BaseTest):
|
||||||
self.apply_config(test_config)
|
self.apply_config(test_config)
|
||||||
self.assertEqual(logging.getLogger().handlers[0].name, 'hand1')
|
self.assertEqual(logging.getLogger().handlers[0].name, 'hand1')
|
||||||
|
|
||||||
|
def test_exception_if_confg_file_is_invalid(self):
|
||||||
|
test_config = """
|
||||||
|
[loggers]
|
||||||
|
keys=root
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys=hand1
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys=form1
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
handlers=hand1
|
||||||
|
|
||||||
|
[handler_hand1]
|
||||||
|
class=StreamHandler
|
||||||
|
formatter=form1
|
||||||
|
|
||||||
|
[formatter_form1]
|
||||||
|
format=%(levelname)s ++ %(message)s
|
||||||
|
|
||||||
|
prince
|
||||||
|
"""
|
||||||
|
|
||||||
|
file = io.StringIO(textwrap.dedent(test_config))
|
||||||
|
self.assertRaises(ValueError, logging.config.fileConfig, file)
|
||||||
|
|
||||||
|
def test_exception_if_confg_file_is_empty(self):
|
||||||
|
fd, fn = tempfile.mkstemp(prefix='test_empty_', suffix='.ini')
|
||||||
|
os.close(fd)
|
||||||
|
self.assertRaises(ValueError, logging.config.fileConfig, fn)
|
||||||
|
os.remove(fn)
|
||||||
|
|
||||||
|
def test_exception_if_config_file_does_not_exist(self):
|
||||||
|
self.assertRaises(FileNotFoundError, logging.config.fileConfig, 'filenotfound')
|
||||||
|
|
||||||
def test_defaults_do_no_interpolation(self):
|
def test_defaults_do_no_interpolation(self):
|
||||||
"""bpo-33802 defaults should not get interpolated"""
|
"""bpo-33802 defaults should not get interpolated"""
|
||||||
ini = textwrap.dedent("""
|
ini = textwrap.dedent("""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue