# HG changeset patch # User fschwarz # Date 1344033709 0 # Node ID 3fd7fb9536339688445369aa4ac7e2d7ef3a6406 # Parent 99706377c9301285386b924b9c5be3f8467cbd66 fix handling of messages containing '\\n' (#171) diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -43,6 +43,7 @@ with more than 7 significant digits (#183) * fix format_date() with datetime parameter (#282, patch from Xavier Morel) * fix format_decimal() with small Decimal values (#214, patch from George Lund) + * fix handling of messages containing '\\n' (#171) Version 0.9.6 diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -40,11 +40,17 @@ :return: the unescaped string :rtype: `str` or `unicode` """ - return string[1:-1].replace('\\\\', '\\') \ - .replace('\\t', '\t') \ - .replace('\\r', '\r') \ - .replace('\\n', '\n') \ - .replace('\\"', '\"') + def replace_escapes(match): + m = match.group(1) + if m == 'n': + return '\n' + elif m == 't': + return '\t' + elif m == 'r': + return '\r' + # m is \ or " + return m + return re.compile(r'\\([\\trn"])').sub(replace_escapes, string[1:-1]) def denormalize(string): r"""Reverse the normalization done by the `normalize` function. diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -526,11 +526,26 @@ self.assertEqual(catalog['missing line number'].locations, []) self.assertEqual(catalog['broken line number'].locations, []) + +class PofileFunctionsTestCase(unittest.TestCase): + + def test_unescape(self): + escaped = u'"Say:\\n \\"hello, world!\\"\\n"' + unescaped = u'Say:\n "hello, world!"\n' + self.assertNotEqual(unescaped, escaped) + self.assertEqual(unescaped, pofile.unescape(escaped)) + + def test_unescape_of_quoted_newline(self): + # regression test for #198 + self.assertEqual(r'\n', pofile.unescape(r'"\\n"')) + + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(pofile, optionflags=doctest.ELLIPSIS)) suite.addTest(unittest.makeSuite(ReadPoTestCase)) suite.addTest(unittest.makeSuite(WritePoTestCase)) + suite.addTest(unittest.makeSuite(PofileFunctionsTestCase)) return suite if __name__ == '__main__':