什么时候(以及为什么)从 `ConfigParser` 插值中移除了 `__name__` 魔法变量?

When (and why) has been `__name__` magic variable removed from `ConfigParser` interpolation?

在 Python 2.7 ConfigParser.ConfigParser 内插模式 %(__name__)s 作为部分的名称。

在 Python 3.6(以及 3.3)中 configparser.ConfigParser 在相同的插值中失败,出现 InterpolationMissingOptionError 异常。

插值行为何时发生变化?该决定的理由是什么(因为它损害了向后兼容性)?

how to interpolate the section-name with configparser 相反,我 不是 问我怎样才能在 Python 3.x.

中得到那个插值

在python3.2 老ConfigParser class (which implemented __name__ interpolation) was removed and replaced by what had previously been the SafeConfigParser class. From the What's New file:

The configparser module was modified to improve usability and predictability of the default parser and its supported INI syntax. The old ConfigParser class was removed in favor of SafeConfigParser which has in turn been renamed to ConfigParser.

详细的动机好像在this bug report中有描述:

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 configparser.py. 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.