Skip to content

bpo-41086: Add exception for uninstantiated interpolation (configparser) #21062

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 4 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ def __init__(self, defaults=None, dict_type=_default_dict,
self._interpolation = self._DEFAULT_INTERPOLATION
if self._interpolation is None:
self._interpolation = Interpolation()
if not isinstance(self._interpolation, Interpolation):
raise TypeError("interpolation is not an instance of Interpolation")
if converters is not _UNSET:
self._converters.update(converters)
if defaults:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,14 @@ def test_set_malformatted_interpolation(self):
self.assertEqual(cf.get("sect", "option2"), "foo%%bar")


class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase):
def test_error_on_wrong_type_for_interpolation(self):
for value in [configparser.ExtendedInterpolation, 42, "a string"]:
with self.subTest(value=value):
with self.assertRaises(TypeError):
configparser.ConfigParser(interpolation=value)


class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase):
delimiters = (':=', '$')
comment_prefixes = ('//', '"')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the :class:`configparser.ConfigParser` constructor raise :exc:`TypeError` if the ``interpolation`` parameter is not of type :class:`configparser.Interpolation`