Skip to content

Commit c17ba23

Browse files
bpo-41963: document that ConfigParser strips off comments (GH-26197) (GH-26213)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Laura Gutierrez Funderburk <[email protected]> Co-authored-by: Jürgen Gmach <[email protected]> (cherry picked from commit 02ee819)
1 parent c05d8a6 commit c17ba23

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Doc/library/configparser.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,13 @@ ConfigParser Objects
11291129
*space_around_delimiters* is true, delimiters between
11301130
keys and values are surrounded by spaces.
11311131

1132+
.. note::
1133+
1134+
Comments in the original configuration file are not preserved when
1135+
writing the configuration back.
1136+
What is considered a comment, depends on the given values for
1137+
*comment_prefix* and *inline_comment_prefix*.
1138+
11321139

11331140
.. method:: remove_option(section, option)
11341141

Lib/configparser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,9 @@ def write(self, fp, space_around_delimiters=True):
907907
908908
If `space_around_delimiters' is True (the default), delimiters
909909
between keys and values are surrounded by spaces.
910+
911+
Please note that comments in the original configuration file are not
912+
preserved when writing the configuration back.
910913
"""
911914
if space_around_delimiters:
912915
d = " {} ".format(self._delimiters[0])
@@ -1005,7 +1008,7 @@ def _read(self, fp, fpname):
10051008
Configuration files may include comments, prefixed by specific
10061009
characters (`#' and `;' by default). Comments may appear on their own
10071010
in an otherwise empty line or may be entered in lines holding values or
1008-
section names.
1011+
section names. Please note that comments get stripped off when reading configuration files.
10091012
"""
10101013
elements_added = set()
10111014
cursect = None # None, or a dictionary
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Document that ``ConfigParser`` strips off comments when reading configuration files.

0 commit comments

Comments
 (0)