Skip to content

Commit

Permalink
change approach for repr
Browse files Browse the repository at this point in the history
  • Loading branch information
Agent-Hellboy committed Nov 22, 2024
1 parent 5255025 commit dc71b39
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
if defaults:
self._read_defaults(defaults)
self._allow_unnamed_section = allow_unnamed_section
self._loaded_files = []

def defaults(self):
return self._defaults
Expand Down Expand Up @@ -750,6 +751,7 @@ def read(self, filenames, encoding=None):
if isinstance(filename, os.PathLike):
filename = os.fspath(filename)
read_ok.append(filename)
self._loaded_files.extend(read_ok)
return read_ok

def read_file(self, f, source=None):
Expand Down Expand Up @@ -1041,12 +1043,31 @@ def __iter__(self):
return itertools.chain((self.default_section,), self._sections.keys())

def __str__(self):
config_dict = {section: dict(self.items(section)) for section in self.sections()}
config_dict = {
section: dict(self.items(section)) for section in self.sections()
}
return str(config_dict)

def __repr__(self):
return f"<ConfigParser(default_section='{self.default_section}', interpolation={self._interpolation})>"

init_params = {
"defaults": self._defaults if self._defaults else None,
"dict_type": type(self._dict).__name__,
"allow_no_value": self._allow_no_value,
"delimiters": self._delimiters,
"strict": self._strict,
"default_section": self.default_section,
"interpolation": type(self._interpolation).__name__,
}
init_params = {k: v for k, v in init_params.items() if v is not None}

state_summary = {
"loaded_files": self._loaded_files if hasattr(self, '_loaded_files') else "(no files loaded)",
"sections": len(self._sections),
}

return (f"<{self.__class__.__name__}("
f"params={init_params}, "
f"state={state_summary})>")

def _read(self, fp, fpname):
"""Parse a sectioned configuration file.
Expand Down

0 comments on commit dc71b39

Please sign in to comment.