-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-127011: Add __str__ and __repr__ to ConfigParser #127014
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
Changes from 10 commits
b3ce3f3
d603aa8
91db566
7f3633d
5cd1d1f
5255025
dc71b39
c59d4fa
d5e944f
0883b52
d76eec4
433432b
acd31ae
43b7ff3
79e4892
913c2a4
e60ffe4
0ed179a
726b152
70fb9a5
38809c9
081c340
50838d5
2560eb9
cb53ff2
155b8ef
1a03c72
acfa969
8415943
ee219a6
63d58e0
9b107e2
50fdb2b
6427a0d
d547497
3817877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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): | ||
|
|
@@ -1040,6 +1042,35 @@ def __iter__(self): | |
| # XXX does it break when underlying container state changed? | ||
| return itertools.chain((self.default_section,), self._sections.keys()) | ||
|
|
||
| def __str__(self): | ||
| config_dict = { | ||
| section: {key: value for key, value in self.items(section, raw=True)} | ||
| for section in self.sections() | ||
| } | ||
| return str(config_dict) | ||
|
||
|
|
||
|
|
||
| def __repr__(self): | ||
| 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)", | ||
Agent-Hellboy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "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. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The ``__str__`` and ``__repr__`` methods have been added to the :class:`configparser.RawConfigParser` |
Uh oh!
There was an error while loading. Please reload this page.