Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/configobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
14 changes: 10 additions & 4 deletions src/tests/test_configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand All @@ -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):
Expand Down Expand Up @@ -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'}}
Expand Down