]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
fix handling of messages containing '\\n' (#171)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Fri, 3 Aug 2012 22:41:49 +0000 (22:41 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Fri, 3 Aug 2012 22:41:49 +0000 (22:41 +0000)
ChangeLog
babel/messages/pofile.py
babel/messages/tests/pofile.py

index 905e43f5328ff5b6c02c56528f08615784fd66e0..fdbaed2ad3715fac3985566e8283bb8ead28f84e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -43,6 +43,7 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
    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
index 889e4d3a6b690d5dd37fac96a269771e2ba74ea5..a44a6914dffb74fdafadd0c8aa1a15e3bb80c853 100644 (file)
@@ -40,11 +40,17 @@ def unescape(string):
     :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.
index 698d2ca2bbeb1f3e556d40961c7fcd606a5d0cd9..64d92742a3be9aa5d5cd8c5dd164042c3cd43550 100644 (file)
@@ -526,11 +526,26 @@ msgstr ""''')
         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__':