diff options
author | Rob Dennis <rdennis@gmail.com> | 2014-04-25 21:36:00 -0400 |
---|---|---|
committer | Rob Dennis <rdennis@gmail.com> | 2014-04-25 21:51:44 -0400 |
commit | be2e73e14d32a0a5de48fdaeb80e629e78612fec (patch) | |
tree | 753987648e9c121e9a4396730314aea0b16f94c8 | |
parent | 81079e281e2b26096f53c291226de1ebd3bc4791 (diff) | |
download | configobj-git-be2e73e14d32a0a5de48fdaeb80e629e78612fec.tar.gz |
fixes #55
-rw-r--r-- | configobj.py | 24 | ||||
-rw-r--r-- | tests/test_configobj.py | 19 |
2 files changed, 24 insertions, 19 deletions
diff --git a/configobj.py b/configobj.py index d730a13..2139e7f 100644 --- a/configobj.py +++ b/configobj.py @@ -2107,21 +2107,25 @@ class ConfigObj(Section): # Windows specific hack to avoid writing '\r\r\n' newline = '\n' output = self._a_to_u(newline).join(out) - if self.encoding: - output = output.encode(self.encoding) - if self.BOM and ((self.encoding is None) or match_utf8(self.encoding)): - # Add the UTF8 BOM - output = BOM_UTF8 + output - if not output.endswith(newline): output += newline + + if isinstance(output, six.binary_type): + output_bytes = output + else: + output_bytes = output.encode(self.encoding or + self.default_encoding or + 'ascii') + + if self.BOM and ((self.encoding is None) or match_utf8(self.encoding)): + # Add the UTF8 BOM + output_bytes = BOM_UTF8 + output_bytes + if outfile is not None: - outfile.write(output) + outfile.write(output_bytes) else: with open(self.filename, 'wb') as h: - h.write(output.encode(self.encoding or - self.default_encoding or - 'ascii')) + h.write(output_bytes) def validate(self, validator, preserve_errors=False, copy=False, section=None): diff --git a/tests/test_configobj.py b/tests/test_configobj.py index c37bd73..be6a7cb 100644 --- a/tests/test_configobj.py +++ b/tests/test_configobj.py @@ -217,7 +217,7 @@ class TestEncoding(object): assert isinstance(c['test'], str) #TODO: this can be made more explicit if we switch to unicode_literals - assert c['test'] == b'\xf0\x9f\x90\x9c'.decode('utf8') + assert c['test'] == '\U0001f41c' #issue #44 def test_encoding_in_subsections(self, ant_cfg, cfg_contents): @@ -235,6 +235,7 @@ class TestEncoding(object): cfg = ConfigObj(cfg_file.name, encoding='utf-8') assert isinstance(cfg['tags']['bug']['translated'], six.text_type) + cfg.write() @pytest.fixture def testconfig1(): @@ -515,7 +516,7 @@ def test_unicode_handling(): uc = ConfigObj(u) assert uc.newlines == '\r\n' uc.newlines = '\r' - file_like = six.StringIO() + file_like = six.BytesIO() uc.write(file_like) file_like.seek(0) uc2 = ConfigObj(file_like) @@ -811,7 +812,7 @@ class TestReloading(object): return content def test_handle_no_filename(self): - for bad_args in ([six.StringIO()], [], [[]]): + for bad_args in ([six.BytesIO()], [], [[]]): cfg = ConfigObj(*bad_args) with pytest.raises(ReloadError) as excinfo: cfg.reload() @@ -1264,23 +1265,23 @@ class TestEdgeCasesWhenWritingOut(object): def test_newline_terminated(self, empty_cfg): empty_cfg.newlines = '\n' empty_cfg['a'] = 'b' - collector = six.StringIO() + collector = six.BytesIO() empty_cfg.write(collector) - assert collector.getvalue() == 'a = b\n' + assert collector.getvalue() == b'a = b\n' def test_hash_escaping(self, empty_cfg): empty_cfg.newlines = '\n' empty_cfg['#a'] = 'b # something' - collector = six.StringIO() + collector = six.BytesIO() empty_cfg.write(collector) - assert collector.getvalue() == '"#a" = "b # something"\n' + assert collector.getvalue() == b'"#a" = "b # something"\n' empty_cfg = ConfigObj() empty_cfg.newlines = '\n' empty_cfg['a'] = 'b # something', 'c # something' - collector = six.StringIO() + collector = six.BytesIO() empty_cfg.write(collector) - assert collector.getvalue() == 'a = "b # something", "c # something"\n' + assert collector.getvalue() == b'a = "b # something", "c # something"\n' def test_detecting_line_endings_from_existing_files(self): for expected_line_ending in ('\r\n', '\n'): |