[Jython-checkins] jython: FS-encode host names reported from os.uname() and platform.uname().

jeff.allen jython-checkins at python.org
Thu Jul 20 02:16:48 EDT 2017


https://hg.python.org/jython/rev/c3e2799ef812
changeset:   8120:c3e2799ef812
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Wed Jul 19 23:50:55 2017 +0100
summary:
  FS-encode host names reported from os.uname() and platform.uname().

These functions now match CPython expectations. In many cases, the root
cause was in _socket.py.

files:
  Lib/_socket.py                                |  24 ++++++++-
  NEWS                                          |   1 +
  src/org/python/modules/posix/PosixModule.java |   2 +-
  3 files changed, 23 insertions(+), 4 deletions(-)


diff --git a/Lib/_socket.py b/Lib/_socket.py
--- a/Lib/_socket.py
+++ b/Lib/_socket.py
@@ -391,6 +391,22 @@
                 args[0]._last_error = 0
     return handle_exception
 
+def _fsencode(name):
+    """Ensure that a name that may be given as a unicode object (e.g. returned
+    from Java) is converted to the expected bytes representation using the
+    file-system encoding."""
+    if isinstance(name, unicode):
+        return name.encode(sys.getfilesystemencoding())
+    return name
+
+def _fsdecode(name):
+    """Ensure that a name that may be given as a bytes object (normal for
+    Python) is converted to the Unicode representation (e.g for Java) using the
+    file-system encoding."""
+    if isinstance(name, bytes):
+        return unicode(name, sys.getfilesystemencoding())
+    return name
+
 
 # select support
 ################
@@ -1872,11 +1888,13 @@
 
 @raises_java_exception
 def gethostname():
-    return str(InetAddress.getLocalHost().getHostName())
+    """Return FS-encoded local host name."""
+    return _fsencode(InetAddress.getLocalHost().getHostName())
 
 @raises_java_exception
 def gethostbyname(name):
-    return str(InetAddress.getByName(name).getHostAddress())
+    """Return IP address as string from FS-decoded host name."""
+    return str(InetAddress.getByName(_fsdecode(name)).getHostAddress())
 
 #
 # Skeleton implementation of gethostbyname_ex
@@ -2046,7 +2064,7 @@
         return self._sock.fileno()
 
     def write(self, data):
-        data = str(data) # XXX Should really reject non-string non-buffers
+        data = str(data) # XXX Should really reject non-byte non-buffers
         if not data:
             return
         self._wbuf.append(data)
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@
 
 Jython 2.7.2b1
   Bugs fixed
+    - [ 2608 ] Encoding problems with non-ascii host name
     - [ 2599 ] Cannot handle network paths under Windows
     - [ 2600 ] subprocess doesn't have _args_from_interpreter_flags (blocks support for multiprocessing)
     - [ 2602 ] NumberFormatException in terminal on OSX 10.12.5 (ncurses related)
diff --git a/src/org/python/modules/posix/PosixModule.java b/src/org/python/modules/posix/PosixModule.java
--- a/src/org/python/modules/posix/PosixModule.java
+++ b/src/org/python/modules/posix/PosixModule.java
@@ -1168,7 +1168,7 @@
 
         PyObject[] vals = {
                 Py.newString(sysname),
-                Py.newString(uname_nodename),
+                Py.fileSystemEncode(uname_nodename),
                 Py.newString(sysrelease),
                 Py.newString(uname_sysver),
                 Py.newString(uname_machine)

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list