Set sys.stdout.encoding properly.
Always set LC_CTYPE on interpreter startup.
Add device_encoding function.
diff --git a/Lib/io.py b/Lib/io.py
index 1ee465a..4ee7cef 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -971,8 +971,13 @@
         if newline not in (None, "\n", "\r\n"):
             raise ValueError("illegal newline value: %r" % (newline,))
         if encoding is None:
-            # XXX This is questionable
-            encoding = sys.getfilesystemencoding() or "latin-1"
+            try:
+                encoding = os.device_encoding(buffer.fileno())
+            except AttributeError:
+                pass
+            if encoding is None:
+                import locale
+                encoding = locale.getpreferredencoding()
 
         self.buffer = buffer
         self._encoding = encoding