8000 gh-66449: Add support to unnamed sections in ConfigParser by pslacerda · Pull Request #2735 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-66449: Add support to unnamed sections in ConfigParser #2735

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Accepting changes
  • Loading branch information
pslacerda committed Sep 22, 2022
commit 72be2b1dd55953ae202e60d2f51e6debd96d7ca3
15 changes: 9 additions & 6 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@
section proxies.

When `allow_unnamed_section` is True (default: False), options
without section are accepted: the section for these is
``configparser.UNNAMED_SECTION``.
without section are accepted: the section for this is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options without a section?

``configparser.UNNAMED_SECTION``. "In the file, they are placed before
the first explicit section header. This also allows reading and writing
files that don't contain any explicit section headers.

sections()
Return all the configuration section names, sans DEFAULT.
Expand Down Expand Up @@ -598,7 +600,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
strict=True, empty_lines_in_values=True,
default_section=DEFAULTSECT,
interpolation=_UNSET, converters=_UNSET,
allow_unnamed_section=False,):
allow_unnamed_section=False):

self._dict = dict_type
self._sections = self._dict()
Expand Down Expand Up @@ -912,17 +914,18 @@ def write(self, fp, space_around_delimiters=True):
self._write_section(fp, self.default_section,
self._defaults.items(), d)
if UNNAMED_SECTION in self._sections:
self._write_section(fp, UNNAMED_SECTION, self._sections[UNNAMED_SECTION].items(), d, unnamed=True)
self._write_section(fp, UNNAMED_SECTION,
self._sections[UNNAMED_SECTION].items(), d)

for section in self._sections:
if section is UNNAMED_SECTION:
continue
self._write_section(fp, section,
self._sections[section].items(), d)

def _write_section(self, fp, section_name, section_items, delimiter, unnamed=False):
def _write_section(self, fp, section_name, section_items, delimiter):
"""Write a single section to the specified `fp'."""
if not unnamed:
if section_name is not UNNAMED_SECTION:
fp.write("[{}]\n".format(section_name))
for key, value in section_items:
value = self._interpolation.before_write(self, section_name, key,
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,8 @@ def test_no_first_section(self):
c = 3
""")

self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg1.sections()))
self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']),
set(cfg1.sections()))
self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a'])
self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b'])
self.assertEqual('3', cfg1['sect1']['c'])
Expand All @@ -2143,7 +2144,8 @@ def test_no_first_section(self):
cfg1.write(output)
cfg2 = self.fromstring(output.getvalue())

#self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg2.sections()))
self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']),
set(cfg2.sections()))
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
self.assertEqual('3', cfg2['sect1']['c'])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:class:`configparser.ConfigParser` now accepts unnamed sections before named
ones.

Contributed by Pedro Sousa Lacerda.
0