Skip to content

When _inspec set to True (or list_values=False) leading spaces in the values are lost, and behaviour is not consistent #269

@gubenkoved

Description

@gubenkoved

When _inspec set to True the following side effects are observed:

  • values with leading whitespaces would be written w/o quotes leading to loss of leading whitespaces (whereas trailing ones will be still preserved in file and on read which is not consistent)
  • "" is interpreted literally as two double quotes symbols

This is specifically important because as for now this seems to be the only way to suppress "#" being interpreted as inline comment as per official documentation: https://configobj.readthedocs.io/en/latest/configobj.html.

Here is the snippet demonstrating this behaviour.

import configobj


def main():
    x = configobj.ConfigObj(
        'test3.cfg',
        encoding='utf-8',
        # write_empty_values=True,
        # list_values=False,
        _inspec=True,
    )

    print(dict(x))

    x['simple'] = 'value'
    x['empty'] = ''
    x['leading'] = '  value'
    x['trailing'] = 'value  '

    print(dict(x))

    x.write()
    x.reload()

    print(dict(x))


if __name__ == '__main__':
    main()

Output is as follows. Note note leading whitespaces are now lost, whereas trailing are not. Also notice how "empty" is interpreted no the way it was written.

$ python configobj_playground.py 
{'simple': 'value', 'empty': '', 'leading': '  value', 'trailing': 'value  '}
{'simple': 'value', 'empty': '""', 'leading': 'value', 'trailing': 'value  '}

$ cat test3.cfg 
simple = value
empty = ""
leading =   value
trailing = value  

Related issue: #268.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions