FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103606: Improve error message from logging.config.FileConfig · python/cpython@2c08f30 · GitHub

/ cpython Public

Commit 2c08f30

Browse files
committed
gh-103606: Improve error message from logging.config.FileConfig
1 parent d1e4917 commit 2c08f30

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

‎Lib/logging/config.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io
3030
import logging
3131
import logging.handlers
32+
import os
3233
import queue
3334
import re
3435
import struct
@@ -59,17 +60,26 @@ def fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=Non
5960
configuration).
6061
"""
6162
import configparser
63+
64+
if isinstance(fname, str):
65+
if not os.path.exists(fname):
66+
raise FileNotFoundError(f"{fname} doesn't exit")
67+
elif not os.path.getsize(fname):
68+
raise ValueError(f"{fname} configuration file is empty")
6269

6370
if isinstance(fname, configparser.RawConfigParser):
6471
cp = fname
6572
else:
66-
cp = configparser.ConfigParser(defaults)
67-
if hasattr(fname, 'readline'):
68-
cp.read_file(fname)
69-
else:
70-
encoding = io.text_encoding(encoding)
71-
cp.read(fname, encoding=encoding)
72-
73+
try:
74+
cp = configparser.ConfigParser(defaults)
75+
if hasattr(fname, 'readline'):
76+
cp.read_file(fname)
77+
else:
78+
encoding = io.text_encoding(encoding)
79+
cp.read(fname, encoding=encoding)
80+
except configparser.ParsingError as e:
81+
raise ValueError(f"{fname} is invalid, errored with: {e}")
82+
7383
formatters = _create_formatters(cp)
7484

7585
# critical section

‎Lib/test/test_logging.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,40 @@ def test_config_set_handler_names(self):
17561756
self.apply_config(test_config)
17571757
self.assertEqual(logging.getLogger().handlers[0].name, 'hand1')
17581758

1759+
def test_exception_if_confg_file_is_invalid(self):
1760+
test_config = """
1761+
[loggers]
1762+
keys=root
1763+
1764+
[handlers]
1765+
keys=hand1
1766+
1767+
[formatters]
1768+
keys=form1
1769+
1770+
[logger_root]
1771+
handlers=hand1
1772+
1773+
[handler_hand1]
1774+
class=StreamHandler
1775+
formatter=form1
1776+
1777+
[formatter_form1]
1778+
format=%(levelname)s ++ %(message)s
1779+
1780+
prince
1781+
"""
1782+
1783+
file = io.StringIO(textwrap.dedent(test_config))
1784+
self.assertRaises(ValueError, logging.config.fileConfig, file)
1785+
1786+
def test_exception_if_confg_file_is_empty(self):
1787+
_, filename = tempfile.mkstemp()
1788+
self.assertRaises(ValueError, logging.config.fileConfig, filename)
1789+
1790+
def test_exception_if_confg_file_does_not_exists(self):
1791+
self.assertRaises(FileNotFoundError, logging.config.fileConfig, 'filenotfound')
1792+
17591793
def test_defaults_do_no_interpolation(self):
17601794
"""bpo-33802 defaults should not get interpolated"""
17611795
ini = textwrap.dedent("""

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL