diff options
author | Rob Dennis <rdennis@gmail.com> | 2014-03-10 20:11:59 -0400 |
---|---|---|
committer | Rob Dennis <rdennis@gmail.com> | 2014-03-10 20:11:59 -0400 |
commit | 6e5c39fddc8681538f348b13cc22ebf4241738e7 (patch) | |
tree | 751b55552fccae06c02a46b048abf9a50268402f | |
parent | 4644d06021d82cf5f1922c660cd65301b1a1922c (diff) | |
parent | dfda0e5d88a566d6f899eca5caae163b3db8d32f (diff) | |
download | configobj-git-6e5c39fddc8681538f348b13cc22ebf4241738e7.tar.gz |
Merge pull request #39 from robdennis/master
configs write in binary mode AND are encoded in the event of unicode
-rw-r--r-- | configobj.py | 8 | ||||
-rw-r--r-- | docs/configobj.html | 2 | ||||
-rw-r--r-- | test_configobj.py | 56 | ||||
-rw-r--r-- | tests/test_configobj.py | 56 |
4 files changed, 60 insertions, 62 deletions
diff --git a/configobj.py b/configobj.py index c11bc33..ef064e4 100644 --- a/configobj.py +++ b/configobj.py @@ -2117,10 +2117,10 @@ class ConfigObj(Section): if outfile is not None: outfile.write(output) else: - h = open(self.filename, 'w') - h.write(output) - h.close() - + with open(self.filename, 'wb') as h: + h.write(output.encode(self.encoding or + self.default_encoding or + 'ascii')) def validate(self, validator, preserve_errors=False, copy=False, section=None): diff --git a/docs/configobj.html b/docs/configobj.html index 9023a7e..1cb1abe 100644 --- a/docs/configobj.html +++ b/docs/configobj.html @@ -933,7 +933,7 @@ empty values. See <a class="reference internal" href="#empty-values">Empty Value <h3><a class="toc-backref" href="#id59">5.2.11 newlines</a></h3> <p>When a config file is read, ConfigObj records the type of newline separators in the file and uses this separator when writing. It defaults to <tt class="docutils literal">None</tt>, and ConfigObj -uses the system default (<tt class="docutils literal">os.sep</tt>) if write is called without newlines having +uses the system default (<tt class="docutils literal">os.linesep</tt>) if write is called without newlines having been set.</p> </div> </div> diff --git a/test_configobj.py b/test_configobj.py index bb75cb6..00279ab 100644 --- a/test_configobj.py +++ b/test_configobj.py @@ -45,62 +45,6 @@ def _test_configobj(): """ Testing ConfigObj - >>> filename = a.filename - >>> a.filename = None - >>> values = a.write() - >>> index = 0 - >>> while index < 23: - ... index += 1 - ... line = values[index-1] - ... assert line.endswith('# comment ' + str(index)) - >>> a.filename = filename - - >>> start_comment = ['# Initial Comment', '', '#'] - >>> end_comment = ['', '#', '# Final Comment'] - >>> newconfig = start_comment + testconfig1.split('\\n') + end_comment - >>> nc = ConfigObj(newconfig) - >>> nc.initial_comment - ['# Initial Comment', '', '#'] - >>> nc.final_comment - ['', '#', '# Final Comment'] - >>> nc.initial_comment == start_comment - 1 - >>> nc.final_comment == end_comment - 1 - - Test the _handle_comment method - - >>> c = ConfigObj() - >>> c['foo'] = 'bar' - >>> c.inline_comments['foo'] = 'Nice bar' - >>> c.write() - ['foo = bar # Nice bar'] - - tekNico: FIXME: use StringIO instead of real files - - >>> filename = a.filename - >>> a.filename = 'test.ini' - >>> a.write() - >>> a.filename = filename - >>> a == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> b.filename = 'test.ini' - >>> b.write() - >>> b == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> i.filename = 'test.ini' - >>> i.write() - >>> i == ConfigObj('test.ini', raise_errors=True) - 1 - >>> os.remove('test.ini') - >>> a = ConfigObj() - >>> a['DEFAULT'] = {'a' : 'fish'} - >>> a['a'] = '%(a)s' - >>> a.write() - ['a = %(a)s', '[DEFAULT]', 'a = fish'] - Test indentation handling >>> ConfigObj({'sect': {'sect': {'foo': 'bar'}}}).write() diff --git a/tests/test_configobj.py b/tests/test_configobj.py index ac976a9..d8a1140 100644 --- a/tests/test_configobj.py +++ b/tests/test_configobj.py @@ -1029,4 +1029,58 @@ def test_multiline_comments(i): 'name2': ' another single line value ', 'name3': ' a single line value ', 'name1': ' a single line value ', - }
\ No newline at end of file + } + + +class TestComments(object): + def test_starting_and_ending_comments(self, a, testconfig1): + + filename = a.filename + a.filename = None + values = a.write() + index = 0 + while index < 23: + index += 1 + line = values[index-1] + assert line.endswith('# comment ' + str(index)) + a.filename = filename + + start_comment = ['# Initial Comment', '', '#'] + end_comment = ['', '#', '# Final Comment'] + newconfig = start_comment + testconfig1.splitlines() + end_comment + nc = ConfigObj(newconfig) + assert nc.initial_comment == ['# Initial Comment', '', '#'] + assert nc.final_comment == ['', '#', '# Final Comment'] + assert nc.initial_comment == start_comment + assert nc.final_comment == end_comment + + def test_inline_comments(self): + c = ConfigObj() + c['foo'] = 'bar' + c.inline_comments['foo'] = 'Nice bar' + assert c.write() == ['foo = bar # Nice bar'] + + +def test_overwriting_filenames(a, b, i): + #TODO: I'm not entirely sure what this test is actually asserting + filename = a.filename + a.filename = 'test.ini' + a.write() + a.filename = filename + assert a == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + b.filename = 'test.ini' + b.write() + assert b == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + i.filename = 'test.ini' + i.write() + assert i == ConfigObj('test.ini', raise_errors=True) + os.remove('test.ini') + + +def test_interpolation_using_default_sections(): + c = ConfigObj() + c['DEFAULT'] = {'a' : 'fish'} + c['a'] = '%(a)s' + assert c.write() == ['a = %(a)s', '[DEFAULT]', 'a = fish'] |