[pypy-commit] pypy py3k: add os.device_encoding

pjenvey noreply at buildbot.pypy.org
Fri Apr 26 04:20:39 CEST 2013


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r63623:0c9a10156aae
Date: 2013-04-25 18:47 -0700
http://bitbucket.org/pypy/pypy/changeset/0c9a10156aae/

Log:	add os.device_encoding

diff --git a/pypy/module/posix/__init__.py b/pypy/module/posix/__init__.py
--- a/pypy/module/posix/__init__.py
+++ b/pypy/module/posix/__init__.py
@@ -88,6 +88,7 @@
     'kill'      : 'interp_posix.kill',
     'abort'     : 'interp_posix.abort',
     'urandom'   : 'interp_posix.urandom',
+    'device_encoding' : 'interp_posix.device_encoding',
     }
 
     if hasattr(os, 'chown'):
diff --git a/pypy/module/posix/interp_posix.py b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -1193,3 +1193,24 @@
         return space.wrapbytes(rurandom.urandom(context, n))
     except OSError, e:
         raise wrap_oserror(space, e)
+
+ at unwrap_spec(fd=int)
+def device_encoding(space, fd):
+    """device_encoding(fd) -> str
+
+    Return a string describing the encoding of the device if the output
+    is a terminal; else return None.
+    """
+    if not (rposix.is_valid_fd(fd) and os.isatty(fd)):
+        return space.w_None
+    if _WIN32:
+        if fd == 0:
+            return 'cp%d' % rwin32.GetConsoleCP()
+        if fd in (1, 2):
+            return 'cp%d' % rwin32.GetConsoleOutputCP()
+    from rpython.rlib import rlocale
+    if rlocale.HAVE_LANGINFO:
+        codeset = rlocale.nl_langinfo(rlocale.CODESET)
+        if codeset:
+            return space.wrap(codeset)
+    return space.w_None
diff --git a/pypy/module/posix/test/test_posix2.py b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -931,6 +931,12 @@
             assert False, "urandom() always returns the same string"
             # Or very unlucky
 
+    def test_device_encoding(self):
+        import sys
+        encoding = self.posix.device_encoding(sys.stdout.fileno())
+        # just ensure it returns something reasonable
+        assert encoding is None or type(encoding) is str
+
 
 class AppTestEnvironment(object):
     def setup_class(cls):


More information about the pypy-commit mailing list