Message9279

Author zyasoft
Recipients Arfrever, jeff.allen, zyasoft
Date 2014-12-30.19:23:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1419967382.3.0.983113322398.issue2239@psf.upfronthosting.co.za>
In-reply-to
Content
Something like the following would work, with all current unit tests passing. (More unit tests to replicate this failing behavior would need to be added, of course.)

Note that bestString and castString are not the greatest names. Also, we have similar requirements in a variety of other places in the code, so it's reasonable to consider refactoring and placing in say org.python.core.Py. 

diff -r 07959ff1d8e9 src/org/python/modules/posix/PosixModule.java
--- a/src/org/python/modules/posix/PosixModule.java	Mon Dec 29 23:44:05 2014 -0700
+++ b/src/org/python/modules/posix/PosixModule.java	Tue Dec 30 12:19:50 2014 -0700
@@ -17,6 +17,7 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import com.google.common.base.CharMatcher;
 import jnr.constants.Constant;
 import jnr.constants.platform.Errno;
 import jnr.posix.FileStat;
@@ -40,6 +41,7 @@
 import org.python.core.PyString;
 import org.python.core.PySystemState;
 import org.python.core.PyTuple;
+import org.python.core.PyUnicode;
 import org.python.core.imp;
 import org.python.core.io.FileDescriptors;
 import org.python.core.io.FileIO;
@@ -474,6 +476,18 @@
         "path: path of directory to list\n\n" +
         "The list is in arbitrary order.  It does not include the special\n" +
         "entries '.' and '..' even if they are present in the directory.");
+
+    private static PyString bestString(PyObject kind, String s) {
+        if (kind instanceof PyUnicode) {
+            return Py.newUnicode(s);
+        }
+        if (CharMatcher.ASCII.matchesAllOf(s)) {
+            return Py.newString(s);
+        } else {
+            return Py.newUnicode(s);
+        }
+    }
+
     public static PyList listdir(PyObject path) {
         String absolutePath = absolutePath(path);
         File file = new File(absolutePath);
@@ -494,9 +508,8 @@
         }
 
         PyList list = new PyList();
-        PyString string = (PyString) path;
         for (String name : names) {
-            list.append(string.createInstance(name));
+            list.append(bestString(path, name));
         }
         return list;
     }
@@ -884,6 +897,15 @@
      * Initialize the environ dict from System.getenv. environ may be empty when the
      * security policy doesn't grant us access.
      */
+
+    private static PyString castString(String s) {
+        if (CharMatcher.ASCII.matchesAllOf(s)) {
+            return Py.newString(s);
+        } else {
+            return Py.newUnicode(s);
+        }
+    }
+
     private static PyObject getEnviron() {
         PyObject environ = new PyDictionary();
         Map<String, String> env;
@@ -893,7 +915,7 @@
             return environ;
         }
         for (Map.Entry<String, String> entry : env.entrySet()) {
-            environ.__setitem__(Py.newString(entry.getKey()), Py.newString(entry.getValue()));
+            environ.__setitem__(castString(entry.getKey()), castString(entry.getValue()));
         }
         return environ;
     }
History
Date User Action Args
2014-12-30 19:23:02zyasoftsetmessageid: <1419967382.3.0.983113322398.issue2239@psf.upfronthosting.co.za>
2014-12-30 19:23:02zyasoftsetrecipients: + zyasoft, jeff.allen, Arfrever
2014-12-30 19:23:02zyasoftlinkissue2239 messages
2014-12-30 19:23:01zyasoftcreate