Skip to content

[3.9] bpo-41963: document that ConfigParser strips off comments (GH-26197) #26213

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

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions Doc/library/configparser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,13 @@ ConfigParser Objects
*space_around_delimiters* is true, delimiters between
keys and values are surrounded by spaces.

.. note::

Comments in the original configuration file are not preserved when
writing the configuration back.
What is considered a comment, depends on the given values for
*comment_prefix* and *inline_comment_prefix*.


.. method:: remove_option(section, option)

Expand Down
5 changes: 4 additions & 1 deletion Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ def write(self, fp, space_around_delimiters=True):

If `space_around_delimiters' is True (the default), delimiters
between keys and values are surrounded by spaces.

Please note that comments in the original configuration file are not
preserved when writing the configuration back.
"""
if space_around_delimiters:
d = " {} ".format(self._delimiters[0])
Expand Down Expand Up @@ -1005,7 +1008,7 @@ def _read(self, fp, fpname):
Configuration files may include comments, prefixed by specific
characters (`#' and `;' by default). Comments may appear on their own
in an otherwise empty line or may be entered in lines holding values or
section names.
section names. Please note that comments get stripped off when reading configuration files.
"""
elements_added = set()
cursect = None # None, or a dictionary
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document that ``ConfigParser`` strips off comments when reading configuration files.