Skip to content

Commit e60ffe4

Browse files
committed
gh-127011: Add __str__ and __repr__ to ConfigParser
1 parent ac5424d commit e60ffe4

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Lib/configparser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,14 @@ def __iter__(self):
10481048
# XXX does it break when underlying container state changed?
10491049
return itertools.chain((self.default_section,), self._sections.keys())
10501050

1051+
def __str__(self):
1052+
config_dict = {section: dict(self.items(section)) for section in self.sections()}
1053+
return str(config_dict)
1054+
1055+
def __repr__(self):
1056+
return f"<ConfigParser(default_section='{self.default_section}', interpolation={self._interpolation})>"
1057+
1058+
10511059
def _read(self, fp, fpname):
10521060
"""Parse a sectioned configuration file.
10531061

0 commit comments

Comments
 (0)