From c2480dc8caa1914b7a6b1ea47097d2da516c2865 Mon Sep 17 00:00:00 2001 From: Gene Wood Date: Thu, 5 Jun 2025 10:07:57 -0700 Subject: [PATCH 1/2] When writing an inline comment with a leading `#` put a space between the value and the `#` Fixes #261 --- src/configobj/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/configobj/__init__.py b/src/configobj/__init__.py index 3f91127..0d76ffc 100644 --- a/src/configobj/__init__.py +++ b/src/configobj/__init__.py @@ -2000,6 +2000,8 @@ def _handle_comment(self, comment): start = self.indent_type if not comment.startswith('#'): start += self._a_to_u(' # ') + else: + start += self._a_to_u(' ') return (start + comment) From eea828703defcd52b80671c4606e935a40362ca7 Mon Sep 17 00:00:00 2001 From: Gene Wood Date: Thu, 5 Jun 2025 10:45:54 -0700 Subject: [PATCH 2/2] Add test and update tests for inline comment with preceding space --- src/tests/test_configobj.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tests/test_configobj.py b/src/tests/test_configobj.py index 017428a..eeb0a1c 100644 --- a/src/tests/test_configobj.py +++ b/src/tests/test_configobj.py @@ -542,13 +542,13 @@ def test_validate(self, val): 'key1 = Hello', '', '# section comment', - '[section]# inline comment', + '[section] # inline comment', '# key1 comment', 'key1 = 6', '# key2 comment', 'key2 = True', '# subsection comment', - '[[sub-section]]# inline comment', + '[[sub-section]] # inline comment', '# another key1 comment', 'key1 = 3.0' ] @@ -560,9 +560,9 @@ def test_writing_empty_values(self): 'key2 =# a comment', ] cfg = ConfigObj(config_with_empty_values) - assert cfg.write() == ['', 'key1 = ""', 'key2 = ""# a comment'] + assert cfg.write() == ['', 'key1 = ""', 'key2 = "" # a comment'] cfg.write_empty_values = True - assert cfg.write() == ['', 'key1 = ', 'key2 = # a comment'] + assert cfg.write() == ['', 'key1 = ', 'key2 = # a comment'] class TestUnrepr(object): @@ -1164,6 +1164,12 @@ def test_inline_comments(self): c.inline_comments['foo'] = 'Nice bar' assert c.write() == ['foo = bar # Nice bar'] + def test_inline_comments_with_leading_number_sign(self): + c = ConfigObj() + c['foo'] = 'bar' + c.inline_comments['foo'] = '# Nice bar' + assert c.write() == ['foo = bar # Nice bar'] + def test_unrepr_comments(self, comment_filled_cfg): c = ConfigObj(comment_filled_cfg, unrepr=True) assert c == { 'key': 'value', 'section': { 'key': 'value'}}