From 20c75063e4e0d3e3441b5b65d4c14ab63016b672 Mon Sep 17 00:00:00 2001 From: fuzzyman Date: Sat, 27 Feb 2010 21:38:07 +0000 Subject: Fix to avoid writing invalid newlines on Windows. --- configobj.py | 6 ++++++ docs/configobj.txt | 2 ++ functionaltests/test_configobj.py | 28 ++++++++++++++++------------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/configobj.py b/configobj.py index 7e3adee..c1f6e6d 100644 --- a/configobj.py +++ b/configobj.py @@ -2015,6 +2015,8 @@ class ConfigObj(Section): >>> a.filename = filename >>> a == ConfigObj('test.ini', raise_errors=True) 1 + >>> import os + >>> os.remove('test.ini') """ if self.indent_type is None: # this can be true if initialised from a dictionary @@ -2090,6 +2092,10 @@ class ConfigObj(Section): # Turn the list to a string, joined with correct newlines newline = self.newlines or os.linesep + if (getattr(outfile, 'mode', None) is not None and outfile.mode == 'w' + and sys.platform == 'win32' and newline == '\r\n'): + # 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) diff --git a/docs/configobj.txt b/docs/configobj.txt index 474c946..ecf2ecb 100644 --- a/docs/configobj.txt +++ b/docs/configobj.txt @@ -2401,6 +2401,8 @@ From version 4 it lists all releases and changes. ignored instead of raising an exception on fetching the item. * BUGFIX: values that use interpolation to reference members that don't exist can now be repr'd. +* BUGFIX: Fix to avoid writing '\r\r\n' on Windows when given a file opened in + text write mode ('w'). 2010/02/06 - Version 4.7.1 -------------------------- diff --git a/functionaltests/test_configobj.py b/functionaltests/test_configobj.py index 437b5a9..8afa37f 100644 --- a/functionaltests/test_configobj.py +++ b/functionaltests/test_configobj.py @@ -8,9 +8,13 @@ except ImportError: from configobj import ConfigObj -# Python 2.6 only -from warnings import catch_warnings - +try: + # Python 2.6 only + from warnings import catch_warnings +except ImportError: + # this will cause an error, but at least the other tests + # will run on Python 2.5 + catch_warnings = None class TestConfigObj(unittest.TestCase): @@ -38,13 +42,15 @@ class TestConfigObj(unittest.TestCase): self.assertFalse(c['section'] is c2['section']) self.assertFalse(c['section']['section'] is c2['section']['section']) - def test_options_deprecation(self): - with catch_warnings(record=True) as log: - ConfigObj(options={}) - - # unpack the only member of log - warning, = log - self.assertEqual(warning.category, DeprecationWarning) + if catch_warnings is not None: + # poor man's skipTest + def test_options_deprecation(self): + with catch_warnings(record=True) as log: + ConfigObj(options={}) + + # unpack the only member of log + warning, = log + self.assertEqual(warning.category, DeprecationWarning) def test_list_members(self): c = ConfigObj() @@ -92,6 +98,4 @@ item1 = 1234 # This raises a MissingInterpolationOption exception in 4.7.1 and earlier repr(c) - - -- cgit v1.2.1