[issue44892] Configparser fails when the file contains $ inside a comment

Terry J. Reedy report at bugs.python.org
Tue Sep 7 01:15:36 EDT 2021


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Diego, I repaired the defacement, which included removing you as nosy.

Relevant doc: https://docs.python.org/3/library/configparser.html#configparser.BasicInterpolation

To me, the following line implies that % may be used freely in comments.
  gain: 80%%  # use a %% to escape the % sign (% is the only character ...
However, IDLE uses .cfg files and configparser, and when I added #% to my C:/Users/Terry/.idlerc/config-main.cfg, IDLE startup fails with the following, confirming the behavior.

Traceback (most recent call last):
  File "f:\dev\3x\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\__main__.py", line 7, in <module>
    idlelib.pyshell.main()
    ^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\pyshell.py", line 1648, in main
    shell = flist.open_shell()
            ^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\pyshell.py", line 335, in open_shell
    self.pyshell = PyShell(self)
                   ^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\pyshell.py", line 898, in __init__
    OutputWindow.__init__(self, flist, None, None)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\outwin.py", line 79, in __init__
    EditorWindow.__init__(self, *args)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\editor.py", line 215, in __init__
    text['font'] = idleConf.GetFont(self.root, 'main', 'EditorWindow')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\config.py", line 742, in GetFont
    bold = self.GetOption(configType, section, 'font-bold', default=0,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\config.py", line 229, in GetOption
    return self.userCfg[configType].Get(section, option,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\idlelib\config.py", line 61, in Get
    return self.getboolean(section, option)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 829, in getboolean
    return self._get_conv(section, option, self._convert_to_boolean,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 809, in _get_conv
    return self._get(section, conv, option, raw=raw, vars=vars,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 804, in _get
    return conv(self.get(section, option, **kwargs))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 800, in get
    return self._interpolation.before_get(self, section, option, value,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 395, in before_get
    self._interpolate_some(parser, option, L, value, section, defaults, 1)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "f:\dev\3x\lib\configparser.py", line 442, in _interpolate_some
    raise InterpolationSyntaxError(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%'

When changed one of the lines in my file see what a valid interpolation would do,
   font-size = 12  # for %(font)s
I got

"Warning: config.py - IdleConf.GetOption -
 invalid 'int' value for configuration option 'font-size'
 from section 'EditorWindow': '12  # for source code pro'"

configparser interpreted the comment as part of the value, and started IDLE with the default size 10.
The same happened when I used %%: the bad value was '12  # for %'
And again with just '#'.  Values read as string are also disabled by '#'.
"name = Custom Dark # %(parenstyle)s" resulted in the default light theme.
Conclusion: 1. Comments are currently worse than useless. 2. Test suite is deficient.

Diego, good PRs are welcome.  Please sign CLA first.  For this, the problem is comments not being recognized and stripped.  Fix that and % in comments will not be an issue.

----------
nosy: +DiddiLeija, terry.reedy, uranusjr, zach.ware
stage:  -> needs patch
title: [configparser] Module configparser fails when the config file contains a "%" inside a commentary -> Configparser fails when the file contains $ inside a comment
versions: +Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44892>
_______________________________________


More information about the Python-bugs-list mailing list