-
-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Description of your problem
Given a config file that does not end with a newline character, calling add_section() and then update_file() results in an incorrectly formatted file.
If we start with the following example config, where the final line does not end with a newline character:
[section1]
option1 = value1
option2 = value2then running the following code:
conf = ConfigUpdater()
conf.read("config.ini")
if not conf.has_section("section2"):
conf.add_section("section2")
conf.update_file()will result in the new section being appended to the value of option2:
[section1]
option1 = value1
option2 = value2[section2]
If the code is run a second time, the new section will be parsed as part of the value for option2, and the resulting config file will be:
[section1]
option1 = value1
option2 = value2[section2]
[section2]Note that many popular text editors will display two files identically if the only difference is the presence or absence of a newline character at the end, so this bug may be difficult to reproduce if such an editor is used. Though many automatically add a newline to the end of a file when saving for compatibility reasons, this is not always the case (and is how I first encountered this bug).
Since the parser does not throw an exception when reading a file that does not end with a newline, it is unexpected for it to fail when saving that file. I believe that the issue could be solved if the parser assumed an implicit newline at the end of a file even if none is present. Though this would technically result in a change to the file only from calling read() followed by update_file(), this same behaviour is shared by many text editors by default. Despite adding a character to the end of the file, it would not change the formatting in any visible way (adding/removing spaces/comments/etc) and so I believe this would fit with the intent of the library.
Versions and main components
- ConfigUpdater Version: 3.1.1
- Python Version: 3.11.4
- Operating system: Ubuntu 22.04
- How did you install ConfigUpdater: pip