Fix #19546: onfigparser exceptions expose implementation details. Pa… · python/cpython@949053b · GitHub
Skip to content

Commit 949053b

Browse files
committed
Fix #19546: onfigparser exceptions expose implementation details. Patch by Claudiu Popa.
1 parent f294681 commit 949053b

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

Lib/configparser.py

Lines changed: 6 additions & 6 deletions

Lib/test/test_configparser.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,58 @@ def test_stripping(self):
17631763
self.assertEqual(s['k2'], 'v2')
17641764
self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
17651765

1766+
class ExceptionContextTestCase(unittest.TestCase):
1767+
""" Test that implementation details doesn't leak
1768+
through raising exceptions. """
1769+
1770+
def test_get_basic_interpolation(self):
1771+
parser = configparser.ConfigParser()
1772+
parser.read_string("""
1773+
[Paths]
1774+
home_dir: /Users
1775+
my_dir: %(home_dir1)s/lumberjack
1776+
my_pictures: %(my_dir)s/Pictures
1777+
""")
1778+
cm = self.assertRaises(configparser.InterpolationMissingOptionError)
1779+
with cm:
1780+
parser.get('Paths', 'my_dir')
1781+
self.assertIs(cm.exception.__suppress_context__, True)
1782+
1783+
def test_get_extended_interpolation(self):
1784+
parser = configparser.ConfigParser(
1785+
interpolation=configparser.ExtendedInterpolation())
1786+
parser.read_string("""
1787+
[Paths]
1788+
home_dir: /Users
1789+
my_dir: ${home_dir1}/lumberjack
1790+
my_pictures: ${my_dir}/Pictures
1791+
""")
1792+
cm = self.assertRaises(configparser.InterpolationMissingOptionError)
1793+
with cm:
1794+
parser.get('Paths', 'my_dir')
1795+
self.assertIs(cm.exception.__suppress_context__, True)
1796+
1797+
def test_missing_options(self):
1798+
parser = configparser.ConfigParser()
1799+
parser.read_string("""
1800+
[Paths]
1801+
home_dir: /Users
1802+
""")
1803+
with self.assertRaises(configparser.NoSectionError) as cm:
1804+
parser.options('test')
1805+
self.assertIs(cm.exception.__suppress_context__, True)
1806+
1807+
def test_missing_section(self):
1808+
config = configparser.ConfigParser()
1809+
with self.assertRaises(configparser.NoSectionError) as cm:
1810+
config.set('Section1', 'an_int', '15')
1811+
self.assertIs(cm.exception.__suppress_context__, True)
1812+
1813+
def test_remove_option(self):
1814+
config = configparser.ConfigParser()
1815+
with self.assertRaises(configparser.NoSectionError) as cm:
1816+
config.remove_option('Section1', 'an_int')
1817+
self.assertIs(cm.exception.__suppress_context__, True)
17661818

17671819
if __name__ == '__main__':
17681820
unittest.main()

Misc/NEWS

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)