| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ | |||
| 29 | 29 | import io | |
| 30 | 30 | import logging | |
| 31 | 31 | import logging.handlers | |
| 32 | + import os | ||
| 32 | 33 | import queue | |
| 33 | 34 | import re | |
| 34 | 35 | import struct | |
@@ -59,17 +60,26 @@ def fileConfig(fname, defaults=None, disable_existing_loggers=True, encoding=Non | |||
| 59 | 60 | configuration). | |
| 60 | 61 | """ | |
| 61 | 62 | 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") | ||
| 62 | 69 | ||
| 63 | 70 | if isinstance(fname, configparser.RawConfigParser): | |
| 64 | 71 | cp = fname | |
| 65 | 72 | 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 | + | ||
| 73 | 83 | formatters = _create_formatters(cp) | |
| 74 | 84 | ||
| 75 | 85 | # critical section | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1756,6 +1756,40 @@ def test_config_set_handler_names(self): | |||
| 1756 | 1756 | self.apply_config(test_config) | |
| 1757 | 1757 | self.assertEqual(logging.getLogger().handlers[0].name, 'hand1') | |
| 1758 | 1758 | ||
| 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 | + | ||
| 1759 | 1793 | def test_defaults_do_no_interpolation(self): | |
| 1760 | 1794 | """bpo-33802 defaults should not get interpolated""" | |
| 1761 | 1795 | ini = textwrap.dedent(""" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments