[Python-checkins] r76125 - sandbox/trunk/2to3/lib2to3/tests/test_parser.py

benjamin.peterson python-checkins at python.org
Thu Nov 5 22:26:55 CET 2009


Author: benjamin.peterson
Date: Thu Nov  5 22:26:55 2009
New Revision: 76125

Log:
handle newline issues better for comparing files

Modified:
   sandbox/trunk/2to3/lib2to3/tests/test_parser.py

Modified: sandbox/trunk/2to3/lib2to3/tests/test_parser.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_parser.py	(original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_parser.py	Thu Nov  5 22:26:55 2009
@@ -12,6 +12,7 @@
 
 # Python imports
 import os
+import io
 
 # Local imports
 from lib2to3.pgen2 import tokenize
@@ -149,15 +150,13 @@
         for filepath in support.all_project_files():
             with open(filepath, "rb") as fp:
                 encoding = tokenize.detect_encoding(fp.readline)[0]
-                fp.seek(0)
+            self.assertTrue(encoding is not None,
+                            "can't detect encoding for %s" % filepath)
+            with io.open(filepath, "r", encoding=encoding) as fp:
                 source = fp.read()
-                if encoding:
-                    source = source.decode(encoding)
             tree = driver.parse_string(source)
             new = unicode(tree)
-            if encoding:
-                new = new.encode(encoding)
-            if diff(filepath, new):
+            if diff(filepath, new, encoding):
                 self.fail("Idempotency failed: %s" % filepath)
 
     def test_extended_unpacking(self):
@@ -199,8 +198,8 @@
         self.validate(s)
 
 
-def diff(fn, result):
-    f = open("@", "wb")
+def diff(fn, result, encoding):
+    f = io.open("@", "w", encoding=encoding)
     try:
         f.write(result)
     finally:


More information about the Python-checkins mailing list