Title: [doc ] configparser: modifying default_section at runtime
Type: behavior Stage:
Components: Documentation, Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, lukasz.langa, r.david.murray, rk, wolma
Priority: normal Keywords: easy

Created on 2016-07-21 12:54 by rk, last changed 2021-12-09 23:38 by iritkatriel.

File name Uploaded Description Edit rk, 2016-07-21 12:54 testcase (for Py3)
Messages (5)
msg270918 - (view) Author: (rk) Date: 2016-07-21 12:54
Modifying "default_section" in the configparser at runtime does not behave as described.

The documentation says about default_section: 

When default_section is given, it specifies the name for the special section holding default values for other sections and interpolation purposes (normally named "DEFAULT"). This value can be retrieved and changed on runtime using the default_section instance attribute.

So, if I modify default_section at runtime, the default values for other sections should then come from the new default_section. But this is not the case. Instead, the default-values still come from self._default, which was set by self._read.

So, this is either a bug in the library or a bug in the documentation.

I've attached a testcase.
msg270930 - (view) Author: (rk) Date: 2016-07-21 14:31
Verified/tested with Python 2.7.9, 3.2.6, 3.3.6, 3.4.2, 3.5.1.
The bug exists in all versions, so I've added 3.2, 3.3, 3.4 again.

I've also attached an updated testcase, which now works in both Python 2 and Python 3.
msg270931 - (view) Author: (rk) Date: 2016-07-21 14:33
(removed Python 2.7, since default_section was not supported there)
msg270932 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-21 14:52
We use the versions field to indicate which versions it might get fixed in.  Since this is not a security issue, that would be 3.5 and 3.6, if it doesn't affect 2.7.  If we decide to make the code match the docs, it will probably only get fixed in 3.6, since it is a behavior change.
msg281232 - (view) Author: Wolfgang Maier (wolma) * Date: 2016-11-19 21:38
Well, you *can* change the value at runtime as you are demonstrating in your script, but you are misunderstanding the effect this will have. It *won't* cause a reevaluation of an already parsed config file. Instead it will affect the writing of the parsed settings to a new config file.
This is explained a bit further up in the documentation of the module:
where it says: "Its current value can be retrieved using the parser_instance.default_section attribute and may be modified at runtime (i.e. to convert files from one format to another)."

So maybe this hint could be repeated in the actual parameter description of to avoid confusion, but I don't think there is a bug here.
Date User Action Args
2021-12-09 23:38:35iritkatrielsetkeywords: + easy
title: configparser: modifying default_section at runtime -> [doc ] configparser: modifying default_section at runtime
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5, Python 3.6, Python 3.7
2016-11-19 23:33:08r.david.murraysetassignee: docs@python

nosy: + docs@python
components: + Documentation
versions: + Python 3.7
2016-11-19 21:38:02wolmasetnosy: + wolma
messages: + msg281232
2016-07-21 14:52:16r.david.murraysetnosy: + r.david.murray

messages: + msg270932
versions: - Python 3.2, Python 3.3, Python 3.4
2016-07-21 14:33:54rksetfiles: -
2016-07-21 14:33:36rksetmessages: + msg270931
versions: - Python 2.7
2016-07-21 14:31:13rksetfiles: +

messages: + msg270930
versions: + Python 3.2, Python 3.3, Python 3.4
2016-07-21 13:47:57r.david.murraysetnosy: + lukasz.langa

versions: - Python 3.2, Python 3.3, Python 3.4
2016-07-21 12:54:13rkcreate