Commits

strontium committed fd87596

Repalce extract.py doctests with unit tests in tests/extract.py for python 3
all tests/extract.py tests pass for Python 3.2, 2.6 and 2.7
Modified string.decode(encoding) to be skipped in extract.py for Py3.0+ because it is redundant for Python 3
Fixed binary+string concatenation for python 3 in tests/extract.py as that cant be done for python 3

Comments (0)

Files changed (3)

babel/messages/extract.py

             continue
         elif call_stack == -1 and tok == COMMENT:
             # Strip the comment token from the line
-            value = value.decode(encoding)[1:].strip()
+            if sys.version_info < (3, 0):
+                value = value.decode(encoding)
+            value = value[1:].strip()
             if in_translator_comments and \
                     translator_comments[-1][0] == lineno - 1:
                 # We're already inside a translator comment, continue appending
                 # aid=617979&group_id=5470
                 value = eval('# coding=%s\n%s' % (encoding, value),
                              {'__builtins__':{}}, {})
-                if isinstance(value, str):
+                if isinstance(value, str) and sys.version_info < (3, 0):
                     value = value.decode(encoding)
                 buf.append(value)
             elif tok == OP and value == ',':
     last_token = None
     call_stack = -1
 
-    for token in tokenize(fileobj.read().decode(encoding)):
+    if sys.version_info < (3, 0):
+       def fileobj_reader():
+           return fileobj.read().decode(encoding)
+    else:
+       def fileobj_reader():
+           return fileobj.read()
+
+    for token in tokenize(fileobj_reader()):
         if token.type == 'operator' and token.value == '(':
             if funcname:
                 message_lineno = token.lineno

babel/messages/tests/extract.py

         self.assertEqual([u'NOTE: hello'], messages[0][3])
 
     def test_utf8_message_with_utf8_bom(self):
-        buf = StringIO(codecs.BOM_UTF8 + """
+        if sys.version_info < (3, 0):
+            buf = StringIO(codecs.BOM_UTF8 + """
 # NOTE: hello
 msg = _('Bonjour à tous')
 """)
+        else:
+            m = codecs.BOM_UTF8 + """
+# NOTE: hello
+msg = _('Bonjour à tous')
+""".encode(encoding="utf-8")
+            buf = StringIO(m.decode(encoding="utf-8")) 
+            
         messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
         self.assertEqual(u'Bonjour à tous', messages[0][2])
         self.assertEqual([u'NOTE: hello'], messages[0][3])
 
     def test_utf8_raw_strings_match_unicode_strings(self):
-        buf = StringIO(codecs.BOM_UTF8 + """
+        if sys.version_info < (3, 0):
+            buf = StringIO(codecs.BOM_UTF8 + """
 msg = _('Bonjour à tous')
 msgu = _(u'Bonjour à tous')
 """)
+        else:
+            m = codecs.BOM_UTF8 + """
+msg = _('Bonjour à tous')
+msgu = _(u'Bonjour à tous')
+""".encode(encoding="utf-8")
+            buf = StringIO(m.decode(encoding="utf-8")) 
+
         messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {}))
         self.assertEqual(u'Bonjour à tous', messages[0][2])
         self.assertEqual(messages[0][2], messages[1][2])
         finally:
             sys.stderr = stderr
 
+class messagesExtractDocTest(unittest.TestCase):
+    def test_messages_extract_extract(self):
+        # Extract messages from the given file-like object using the specified
+        #extraction method.
+        source = \
+'''\
+# foo module
+def run(argv):
+    print _('Hello, world!')
+'''
+        from StringIO import StringIO
+        msgcnt = 0
+        for message in extract.extract('python', StringIO(source)):
+            msgcnt+=1
+        self.assertEqual(msgcnt, 1)
+        self.assertEqual(message, (3, u'Hello, world!', []))
+
+    def test_messages_extract_extract_from_dir(self):
+        return
 
 def suite():
     suite = unittest.TestSuite()
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(extract))
+    suite.addTest(unittest.makeSuite(messagesExtractDocTest))
     suite.addTest(unittest.makeSuite(ExtractPythonTestCase))
     suite.addTest(unittest.makeSuite(ExtractJavaScriptTestCase))
     suite.addTest(unittest.makeSuite(ExtractTestCase))
     fp.seek(0)
     try:
         line1 = fp.readline()
+        if sys.version_info >= (3, 0):
+            line1 = line1.encode()
         has_bom = line1.startswith(codecs.BOM_UTF8)
         if has_bom:
             line1 = line1[len(codecs.BOM_UTF8):]
+        if sys.version_info >= (3, 0):
+            line1 = line1.decode()
 
         m = PYTHON_MAGIC_COMMENT_re.match(line1)
         if not m:
             return dict.pop(self, key)
         elif key not in self:
             return default
-        # In py3 keys are views
         self._keys.remove(key)
         return dict.pop(self, key, default)