Skip to content

Commit cb53ff2

Browse files
committed
Add test and fix parsing error of news
1 parent 2560eb9 commit cb53ff2

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/configparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,12 @@ def __iter__(self):
10521052

10531053
def __str__(self):
10541054
config_dict = {
1055-
section: dict(self.items(section)) for section in self.sections()
1055+
section: {key: value for key, value in self.items(section, raw=True)}
1056+
for section in self.sections()
10561057
}
10571058
return str(config_dict)
10581059

1060+
10591061
def __repr__(self):
10601062
init_params = {
10611063
"defaults": self._defaults if self._defaults else None,

Lib/test/test_configparser.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,28 @@ def test_set_nonstring_types(self):
980980
self.assertRaises(TypeError, cf.set, "sect", 123, "invalid opt name!")
981981
self.assertRaises(TypeError, cf.add_section, 123)
982982

983+
def test_str_and_repr(self):
984+
self.maxDiff = None
985+
cf = self.config_class(allow_no_value=True, delimiters=('=',), strict=True)
986+
987+
cf.add_section("sect")
988+
cf.set("sect", "option1", "foo")
989+
cf.set("sect", "option2", "bar")
990+
991+
expected_str = "{'sect': {'option1': 'foo', 'option2': 'bar'}}"
992+
self.assertEqual(str(cf), expected_str)
993+
994+
dict_type = type(cf._dict).__name__
995+
996+
expected_repr = (
997+
f"<{cf.__class__.__name__}("
998+
f"params={{'dict_type': '{dict_type}', 'allow_no_value': True, "
999+
"'delimiters': ('=',), 'strict': True, 'default_section': 'DEFAULT', "
1000+
"'interpolation': 'BasicInterpolation'}, "
1001+
"state={'loaded_files': [], 'sections': 1})>"
1002+
)
1003+
self.assertEqual(repr(cf), expected_repr)
1004+
9831005
def test_add_section_default(self):
9841006
cf = self.newconfig()
9851007
self.assertRaises(ValueError, cf.add_section, self.default_section)
@@ -1153,6 +1175,8 @@ def test_reading(self):
11531175
self.assertEqual(cf.get("global", "hosts allow"), "127.")
11541176
self.assertEqual(cf.get("tmp", "echo command"), "cat %s; rm %s")
11551177

1178+
1179+
11561180
class ConfigParserTestCaseExtendedInterpolation(BasicTestCase, unittest.TestCase):
11571181
config_class = configparser.ConfigParser
11581182
interpolation = configparser.ExtendedInterpolation()
@@ -1300,6 +1324,8 @@ def test_case_sensitivity_conflicts(self):
13001324
eq(cf['random']['foo'], 'value redefined')
13011325
eq(cf['random']['Foo'], 'A Better Value Redefined')
13021326

1327+
1328+
13031329
def test_other_errors(self):
13041330
cf = self.fromstring("""
13051331
[interpolation fail]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The __str__ and __repr__ methods have been added to the :class:configparser.RawConfigParser
1+
The ``__str__`` and ``__repr__`` methods have been added to the :class:`configparser.RawConfigParser`

0 commit comments

Comments
 (0)