# HG changeset patch # User cmlenz # Date 1180992652 0 # Node ID 06b876ed550137f497240b408a73729102545f89 # Parent 3b23c60191e0ccc7a868b1bd158220de88ca0aa7 Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes. diff --git a/babel/catalog/extract.py b/babel/catalog/extract.py --- a/babel/catalog/extract.py +++ b/babel/catalog/extract.py @@ -229,7 +229,8 @@ elif tok == STRING: if lineno is None: lineno = stup[0] - buf.append(value[1:-1]) + # Unwrap quotes in a safe manner + buf.append(eval(value, {'__builtins__':{}}, {})) elif tok == OP and value == ',': messages.append(''.join(buf)) del buf[:] diff --git a/babel/catalog/tests/extract.py b/babel/catalog/tests/extract.py --- a/babel/catalog/tests/extract.py +++ b/babel/catalog/tests/extract.py @@ -12,13 +12,24 @@ # history and logs, available at http://babel.edgewall.org/log/. import doctest +from StringIO import StringIO import unittest from babel.catalog import extract + +class ExtractPythonTestCase(unittest.TestCase): + + def test_unicode_string_arg(self): + buf = StringIO("msg = _(u'Foo Bar')") + messages = list(extract.extract_python(buf, ('_',), {})) + self.assertEqual('Foo Bar', messages[0][2]) + + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(extract)) + suite.addTest(unittest.makeSuite(ExtractPythonTestCase)) return suite if __name__ == '__main__':