Title: configparser: remove broken `__name__` support
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: lukasz.langa Nosy List: eric.araujo, fdrake, georg.brandl, lukasz.langa, michael.foord
Priority: normal Keywords: easy, needs review, patch

Created on 2010-11-21 12:30 by lukasz.langa, last changed 2010-11-21 14:00 by lukasz.langa. This issue is now closed.

File name Uploaded Description Edit
issue10489.diff lukasz.langa, 2010-11-21 13:06 Patch for removal of the broken `__name__` key
Messages (4)
msg121912 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-11-21 12:30
I want to sum up all strange things about the behaviour of `__name__`, a special key present in every section of a parser instance.

1. There is a special `__name__` key in every section.
2. Except for the DEFAULTSECT.
3. `__name__` key is set for every section read from a file.
4. and not when adding by `add_section()`.
5. if `__name__` does exist, it's not visible in `parser.options('section')`
6. but it is visible here: `parser.has_option('section', '__name__') == True`
7. and can be obtained by `parser.get('section', '__name__')`
8. and can be changed by `parser.set('section', '__name__', 'ANY VALUE')`
9. and can be removed by `parser.remove_option('section', '__name__')`
10. even if the value is changed by `parser.set()`, it won't be written back to a file with `parser.write()`

All this looks like a feature that was not particularly complete and well defined when it was first created. Or possibly, it became rotten with time and now nobody is using it anyway. That way or the other, I couldn't come up with a valid use case for `__name__` with the current implementation. It doesn't serve any internal purpose and the *only* way you can actually get it is to `parser.get('section', '__name__')` which returns 'section' anyway. About as useless as it gets. Of course, one can go and take the internal parser._sections data structure of the parser but that's evil.

I want simply remove all mentions of a special `__name__` key in Backwards compatibility is not a concern here because in this case we have a concept that is so broken that you can't actually use it.
msg121913 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-11-21 12:33
For the record, I wrote about this problem in July on the mailing list [1], there were no replies.

msg121920 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-11-21 13:06
Patch for the cleanup attached. 47 lines removed, about a half of these were in tests explicitly made to check for these inconsistencies. 4 lines added, all of them are reformattings due to removal of the other lines.

Syntax highlighted view of the patch:
msg121928 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-11-21 14:00
Committed in rev 86638. A public API for getting the name of the section from a SectionProxy instance was introduced in 86639. This is useful because you can assing a section proxy to a variable and pass it around:

>>> from configparser import SafeConfigParser
>>> parser = SafeConfigParser()
>>> parser.read_string("""
... [section1]
... value = A
... value2 = B
... [section2]
... value = C
... value2 = D
... """)
>>> section1 = parser['section1']
>>> section1['value']
>>> section2 = parser['section2']
>>> section2['value']
>>> = 'cannot do that, this attribute is read-only on a SectionProxy'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
Date User Action Args
2010-11-21 14:00:34lukasz.langasetstatus: open -> closed
resolution: accepted
messages: + msg121928

stage: resolved
2010-11-21 13:06:53lukasz.langasetfiles: + issue10489.diff
keywords: + patch
messages: + msg121920
2010-11-21 12:33:16lukasz.langasetmessages: + msg121913
2010-11-21 12:31:17lukasz.langasetnosy: + fdrake, georg.brandl, eric.araujo, michael.foord
2010-11-21 12:30:53lukasz.langacreate