Skip to content

bpo-33802: Do not interpolate in ConfigParser while reading defaults #7524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,8 +1208,16 @@ def add_section(self, section):

def _read_defaults(self, defaults):
"""Reads the defaults passed in the initializer, implicitly converting
values to strings like the rest of the API."""
self.read_dict({self.default_section: defaults})
values to strings like the rest of the API.

Does not perform interpolation for backwards compatibility.
"""
try:
hold_interpolation = self._interpolation
self._interpolation = Interpolation()
self.read_dict({self.default_section: defaults})
finally:
self._interpolation = hold_interpolation


class SafeConfigParser(ConfigParser):
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,49 @@ def test_logger_disabling(self):
self.apply_config(self.disable_test, disable_existing_loggers=False)
self.assertFalse(logger.disabled)

def test_defaults_do_no_interpolation(self):
"""bpo-33802 defaults should not get interpolated"""
ini = textwrap.dedent("""
[formatters]
keys=default

[formatter_default]

[handlers]
keys=console

[handler_console]
class=logging.StreamHandler
args=tuple()

[loggers]
keys=root

[logger_root]
formatter=default
handlers=console
""").strip()
fd, fn = tempfile.mkstemp(prefix='test_logging_', suffix='.ini')
try:
os.write(fd, ini.encode('ascii'))
os.close(fd)
logging.config.fileConfig(
fn,
defaults=dict(
version=1,
disable_existing_loggers=False,
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter"
},
},
)
)
finally:
os.unlink(fn)


class SocketHandlerTest(BaseTest):

Expand Down