mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-66449: configparser: Add support for unnamed sections (#117273)
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
parent
d9cfe7e565
commit
54f7e14500
5 changed files with 172 additions and 31 deletions
|
|
@ -274,6 +274,11 @@ may be treated as parts of multiline values or ignored.
|
|||
By default, a valid section name can be any string that does not contain '\\n'.
|
||||
To change this, see :attr:`ConfigParser.SECTCRE`.
|
||||
|
||||
The first section name may be omitted if the parser is configured to allow an
|
||||
unnamed top level section with ``allow_unnamed_section=True``. In this case,
|
||||
the keys/values may be retrieved by :const:`UNNAMED_SECTION` as in
|
||||
``config[UNNAMED_SECTION]``.
|
||||
|
||||
Configuration files may include comments, prefixed by specific
|
||||
characters (``#`` and ``;`` by default [1]_). Comments may appear on
|
||||
their own on an otherwise empty line, possibly indented. [1]_
|
||||
|
|
@ -325,6 +330,27 @@ For example:
|
|||
# Did I mention we can indent comments, too?
|
||||
|
||||
|
||||
.. _unnamed-sections:
|
||||
|
||||
Unnamed Sections
|
||||
----------------
|
||||
|
||||
The name of the first section (or unique) may be omitted and values
|
||||
retrieved by the :const:`UNNAMED_SECTION` attribute.
|
||||
|
||||
.. doctest::
|
||||
|
||||
>>> config = """
|
||||
... option = value
|
||||
...
|
||||
... [ Section 2 ]
|
||||
... another = val
|
||||
... """
|
||||
>>> unnamed = configparser.ConfigParser(allow_unnamed_section=True)
|
||||
>>> unnamed.read_string(config)
|
||||
>>> unnamed.get(configparser.UNNAMED_SECTION, 'option')
|
||||
'value'
|
||||
|
||||
Interpolation of values
|
||||
-----------------------
|
||||
|
||||
|
|
@ -1216,6 +1242,11 @@ ConfigParser Objects
|
|||
names is stripped before :meth:`optionxform` is called.
|
||||
|
||||
|
||||
.. data:: UNNAMED_SECTION
|
||||
|
||||
A special object representing a section name used to reference the unnamed section (see :ref:`unnamed-sections`).
|
||||
|
||||
|
||||
.. data:: MAX_INTERPOLATION_DEPTH
|
||||
|
||||
The maximum depth for recursive interpolation for :meth:`~configparser.ConfigParser.get` when the *raw*
|
||||
|
|
|
|||
|
|
@ -214,6 +214,12 @@ Other Language Changes
|
|||
|
||||
(Contributed by William Woodruff in :gh:`112389`.)
|
||||
|
||||
* The :class:`configparser.ConfigParser` now accepts unnamed sections before named
|
||||
ones if configured to do so.
|
||||
|
||||
(Contributed by Pedro Sousa Lacerda in :gh:`66449`)
|
||||
|
||||
|
||||
New Modules
|
||||
===========
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue