Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r63625:49a2889ff518
Date: 2013-04-25 19:19 -0700
http://bitbucket.org/pypy/pypy/changeset/49a2889ff518/

Log:    attempt to get the default encoding from os.device_encoding

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -268,10 +268,24 @@
 )
 
 
-def _determine_encoding(space, encoding):
+def _determine_encoding(space, encoding, w_buffer):
     if encoding is not None:
         return space.wrap(encoding)
 
+    # Try os.device_encoding(fileno)
+    try:
+        w_fileno = space.call_method(w_buffer, 'fileno')
+    except OperationError as e:
+        from pypy.module._io.interp_io import Cache
+        if not (e.match(space, space.w_AttributeError) or
+                e.match(space, space.fromcache(Cache).w_unsupportedoperation)):
+            raise
+    else:
+        w_os = space.call_method(space.builtin, '__import__', space.wrap('os'))
+        w_encoding = space.call_method(w_os, 'device_encoding', w_fileno)
+        if space.isinstance_w(w_encoding, space.w_unicode):
+            return w_encoding
+
     try:
         w_locale = space.call_method(space.builtin, '__import__',
                                      space.wrap('locale'))
@@ -357,7 +371,7 @@
                    write_through=0):
         self.state = STATE_ZERO
         self.w_buffer = w_buffer
-        self.w_encoding = _determine_encoding(space, encoding)
+        self.w_encoding = _determine_encoding(space, encoding, w_buffer)
 
         if space.is_none(w_errors):
             w_errors = space.wrap("strict")
diff --git a/pypy/module/_io/test/test_textio.py 
b/pypy/module/_io/test/test_textio.py
--- a/pypy/module/_io/test/test_textio.py
+++ b/pypy/module/_io/test/test_textio.py
@@ -431,3 +431,14 @@
         _check(dec)
         dec = _io.IncrementalNewlineDecoder(None, translate=True)
         _check(dec)
+
+    def test_device_encoding(self):
+        import os
+        import sys
+        encoding = os.device_encoding(sys.stderr.fileno())
+        if not encoding:
+            skip("Requires a result from "
+                 "os.device_encoding(sys.stderr.fileno())")
+        import _io
+        f = _io.TextIOWrapper(sys.stderr.buffer)
+        assert f.encoding == encoding
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to