Commits

Joe Amenta committed 763e8d9

Changing builtin "open()" to "io.open()"

Comments (0)

Files changed (2)

lib3to2/fixes/fix_open.py

+"""
+Fixer for open(...) -> io.open(...)
+"""
+
+from lib2to3 import fixer_base
+from ..fixer_util import touch_import, is_probably_builtin
+
+class FixOpen(fixer_base.BaseFix):
+
+    PATTERN = """'open'"""
+
+    def transform(self, node, results):
+
+        if is_probably_builtin(node):
+            touch_import("io", "open", node)

lib3to2/tests/test_open.py

+from test_all_fixers import lib3to2FixerTestCase
+
+class Test_open(lib3to2FixerTestCase):
+    fixer = "open"
+
+    def test_imports(self):
+        b = """new_file = open("some_filename", newline="\\r")"""
+        a = """from io import open\nnew_file = open("some_filename", newline="\\r")"""
+        self.check(b, a)
+
+    def test_doesnt_import(self):
+        s = """new_file = nothing.open("some_filename")"""
+        self.unchanged(s)