[Jython-checkins] jython: Avoid PyString range-test in operations involving sub-strings.

jeff.allen jython-checkins at python.org
Tue Dec 2 23:11:53 CET 2014


https://hg.python.org/jython/rev/720e34a4d5be
changeset:   7428:720e34a4d5be
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Tue Dec 02 20:44:45 2014 +0000
summary:
  Avoid PyString range-test in operations involving sub-strings.

Uses the private "no-check"constructor for these common operations:
intends to restore performance in most applications.

files:
  NEWS                              |   4 ++++
  src/org/python/core/Py.java       |  14 ++++++++++----
  src/org/python/core/PyString.java |  12 +++++++-----
  3 files changed, 21 insertions(+), 9 deletions(-)


diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,9 @@
 Jython NEWS
 
+Jython 2.7b4
+  Bugs Fixed
+    - [ 2037 ] Byte-string containing elements greater than 255
+
 Jython 2.7b3
   Bugs Fixed
     - [ 2225 ] Jython+django-jython - no module named site
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
@@ -633,7 +633,7 @@
         }
         return new PyStringMap();
     }
-    
+
     public static PyUnicode newUnicode(char c) {
         return (PyUnicode) makeCharacter(c, true);
     }
@@ -1661,14 +1661,20 @@
     }
 
     public static final PyString makeCharacter(char c) {
-        return makeCharacter(c, false);
+        if (c <= 255) {
+            return letters[c];
+        } else {
+            // This will throw IllegalArgumentException since non-byte value
+            return new PyString(c);
+        }
     }
 
     static final PyString makeCharacter(int codepoint, boolean toUnicode) {
         if (toUnicode) {
             return new PyUnicode(codepoint);
-        } else if (codepoint > 255) {
-            throw new IllegalArgumentException("Cannot create PyString with non-byte value");
+        } else if (codepoint < 0 || codepoint > 255) {
+            // This will throw IllegalArgumentException since non-byte value
+            return new PyString('\uffff');
         }
         return letters[codepoint];
     }
diff --git a/src/org/python/core/PyString.java b/src/org/python/core/PyString.java
--- a/src/org/python/core/PyString.java
+++ b/src/org/python/core/PyString.java
@@ -727,7 +727,8 @@
 
     @Override
     protected PyObject pyget(int i) {
-        return Py.newString(getString().charAt(i));
+        // Method is overridden in PyUnicode, so definitely a PyString
+        return Py.makeCharacter(string.charAt(i));
     }
 
     @Override
@@ -1263,7 +1264,7 @@
             // It ought to be None, null, some kind of bytes with the buffer API.
             String stripChars = asStringNullOrError(chars, "strip");
             // Strip specified characters or whitespace if stripChars == null
-            return new PyString(_strip(stripChars));
+            return new PyString(_strip(stripChars), true);
         }
     }
 
@@ -1433,7 +1434,7 @@
             // It ought to be None, null, some kind of bytes with the buffer API.
             String stripChars = asStringNullOrError(chars, "lstrip");
             // Strip specified characters or whitespace if stripChars == null
-            return new PyString(_lstrip(stripChars));
+            return new PyString(_lstrip(stripChars), true);
         }
     }
 
@@ -1522,7 +1523,7 @@
             // It ought to be None, null, some kind of bytes with the buffer API.
             String stripChars = asStringNullOrError(chars, "rstrip");
             // Strip specified characters or whitespace if stripChars == null
-            return new PyString(_rstrip(stripChars));
+            return new PyString(_rstrip(stripChars), true);
         }
     }
 
@@ -2231,7 +2232,8 @@
      * @return new object.
      */
     protected PyString fromSubstring(int begin, int end) {
-        return createInstance(getString().substring(begin, end), true);
+        // Method is overridden in PyUnicode, so definitely a PyString
+        return new PyString(getString().substring(begin, end), true);
     }
 
     /**

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


More information about the Jython-checkins mailing list