Issue #10335: Add tokenize.open(), detect the file encoding using
tokenize.detect_encoding() and open it in read only mode.
diff --git a/Lib/py_compile.py b/Lib/py_compile.py
index 111893e..d241434 100644
--- a/Lib/py_compile.py
+++ b/Lib/py_compile.py
@@ -104,9 +104,7 @@
     byte-compile all installed files (or all files in selected
     directories).
     """
-    with open(file, "rb") as f:
-        encoding = tokenize.detect_encoding(f.readline)[0]
-    with open(file, encoding=encoding) as f:
+    with tokenize.open(file) as f:
         try:
             timestamp = int(os.fstat(f.fileno()).st_mtime)
         except AttributeError: