[Jython-checkins] jython: Allow for non-ascii directory names in PathPackageManager, fixes #2397.

jeff.allen jython-checkins at python.org
Sat Oct 10 09:58:11 CEST 2015


https://hg.python.org/jython/rev/b6180b5dc4c6
changeset:   7748:b6180b5dc4c6
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Sat Sep 19 10:57:49 2015 +0100
summary:
  Allow for non-ascii directory names in PathPackageManager, fixes #2397.

This fixes a couple of places where the attempt to make a non-ascii
PyString bytes us. One in a series involving paths.

files:
  Lib/test/test_import_jy.py                               |   4 +-
  src/org/python/core/Py.java                              |  30 +++++++---
  src/org/python/core/packagecache/PathPackageManager.java |   6 +-
  3 files changed, 26 insertions(+), 14 deletions(-)


diff --git a/Lib/test/test_import_jy.py b/Lib/test/test_import_jy.py
--- a/Lib/test/test_import_jy.py
+++ b/Lib/test/test_import_jy.py
@@ -173,8 +173,8 @@
         self.assertRaises(Exception, getattr, anygui, 'abc')
 
     def test_import_star(self):
-        self.assertEquals(subprocess.call([sys.executable,
-        test_support.findfile("import_star_from_java.py")]), 0)
+        self.assertEquals(0, subprocess.call(
+                [sys.executable, test_support.findfile("import_star_from_java.py")]))
 
     def test_selfreferential_classes(self):
         from org.python.tests.inbred import Metis
diff --git a/src/org/python/core/Py.java b/src/org/python/core/Py.java
--- a/src/org/python/core/Py.java
+++ b/src/org/python/core/Py.java
@@ -645,19 +645,31 @@
         return new PyString(s);
     }
 
-    // Use if s may contain Unicode characters,
-    // but we prefer to return a PyString
+    /**
+     * Return a {@link PyString} for the given Java <code>String</code>, if it can be represented as
+     * US-ASCII, and a {@link PyUnicode} otherwise.
+     *
+     * @param s string content
+     * @return <code>PyString</code> or <code>PyUnicode</code> according to content of
+     *         <code>s</code>.
+     */
     public static PyString newStringOrUnicode(String s) {
         return newStringOrUnicode(Py.EmptyString, s);
     }
 
-    // Use when returning a PyString or PyUnicode is based on what "kind" is,
-    // but always fallback to PyUnicode if s contains Unicode characters.
-    public static PyString newStringOrUnicode(PyObject kind, String s) {
-        if (kind instanceof PyUnicode) {
-            return Py.newUnicode(s);
-        }
-        if (CharMatcher.ASCII.matchesAllOf(s)) {
+    /**
+     * Return a {@link PyString} for the given Java <code>String</code>, if it can be represented as
+     * US-ASCII and if a preceding object is not a <code>PyUnicode</code>, and a {@link PyUnicode}
+     * otherwise. In some contexts, we want the result to be a <code>PyUnicode</code> if some
+     * preceding result is a <code>PyUnicode</code>.
+     *
+     * @param precedent string of which the type sets a precedent
+     * @param s string content
+     * @return <code>PyString</code> or <code>PyUnicode</code> according to content of
+     *         <code>s</code>.
+     */
+    public static PyString newStringOrUnicode(PyObject precedent, String s) {
+        if (!(precedent instanceof PyUnicode) && CharMatcher.ASCII.matchesAllOf(s)) {
             return Py.newString(s);
         } else {
             return Py.newUnicode(s);
diff --git a/src/org/python/core/packagecache/PathPackageManager.java b/src/org/python/core/packagecache/PathPackageManager.java
--- a/src/org/python/core/packagecache/PathPackageManager.java
+++ b/src/org/python/core/packagecache/PathPackageManager.java
@@ -95,7 +95,7 @@
 
     /**
      * Helper for {@link #doDir(PyJavaPackage,boolean,boolean)}. Scans for
-     * package jpkg content over the directories in path. Add to ret the founded
+     * package jpkg content over the directories in path. Add to ret the found
      * classes/pkgs. Filter out classes using {@link #filterByName},{@link #filterByAccess}.
      */
     protected void doDir(PyList path, PyList ret, PyJavaPackage jpkg,
@@ -142,7 +142,7 @@
                 }
 
                 jname = jname.substring(0, jlen);
-                PyString name = new PyString(jname);
+                PyString name = Py.newStringOrUnicode(jname);
 
                 if (filterByName(jname, pkgCand)) {
                     continue;
@@ -200,7 +200,7 @@
             if (dir.getPath().length() == 0) {
                 this.searchPath.append(Py.EmptyString);
             } else {
-                this.searchPath.append(new PyString(dir.getCanonicalPath()));
+                this.searchPath.append(Py.newStringOrUnicode(dir.getCanonicalPath()));
             }
         } catch (IOException e) {
             warning("skipping bad directory, '" + dir + "'");

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


More information about the Jython-checkins mailing list