changeset 582:3fd7fb953633 trunk

fix handling of messages containing '\\n' (#171)
author fschwarz
date Fri, 03 Aug 2012 22:41:49 +0000
parents 99706377c930
children ae897a807442
files ChangeLog babel/messages/pofile.py babel/messages/tests/pofile.py
diffstat 3 files changed, 27 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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.
--- 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__':
Copyright (C) 2012-2017 Edgewall Software