[Jython-checkins] jython: bytearray: make acceptable as string argument to re.find() etc..

jeff.allen jython-checkins at python.org
Wed Aug 1 10:12:29 CEST 2012


http://hg.python.org/jython/rev/37355d490a85
changeset:   6817:37355d490a85
user:        Jeff Allen <ja...py at farowl.co.uk>
date:        Sun Jul 29 16:23:38 2012 +0100
summary:
  bytearray: make acceptable as string argument to re.find() etc..

files:
  src/org/python/core/util/StringUtil.java      |  14 +++
  src/org/python/modules/sre/PatternObject.java |  39 +++++++--
  2 files changed, 44 insertions(+), 9 deletions(-)


diff --git a/src/org/python/core/util/StringUtil.java b/src/org/python/core/util/StringUtil.java
--- a/src/org/python/core/util/StringUtil.java
+++ b/src/org/python/core/util/StringUtil.java
@@ -4,8 +4,10 @@
 import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
 
+import org.python.core.BufferPointer;
 import org.python.core.Py;
 import org.python.core.BaseBytes;
+import org.python.core.PyBuffer;
 
 /**
  * String Utility methods.
@@ -67,6 +69,18 @@
     }
 
     /**
+     * Return a new String with chars corresponding to buf, which is a byte-oriented buffer obtained
+     * through the buffer API.
+     * 
+     * @param buf a PyBuffer of bytes
+     * @return a new String corresponding to the bytes in buf
+     */
+    public static String fromBytes(PyBuffer buf) {
+        BufferPointer bp = buf.getBuf();
+        return fromBytes(bp.storage, bp.offset, bp.size);
+    }
+
+    /**
      * Return a new String with chars corresponding to b.
      *
      * @param b a BaseBytes containing bytes
diff --git a/src/org/python/modules/sre/PatternObject.java b/src/org/python/modules/sre/PatternObject.java
--- a/src/org/python/modules/sre/PatternObject.java
+++ b/src/org/python/modules/sre/PatternObject.java
@@ -18,6 +18,7 @@
 
 import java.util.*;
 import org.python.core.*;
+import org.python.core.util.StringUtil;
 
 public class PatternObject extends PyObject {
     int[] code; /* link to the code string object */
@@ -362,17 +363,37 @@
         _error(status);
         return null;
     }
-    
-    private static PyString extractPyString(ArgParser ap, int pos){
+
+    private static PyString extractPyString(ArgParser ap, int pos) {
         PyObject obj = ap.getPyObject(pos);
-        if(!(obj instanceof PyString)){
-            if (obj instanceof PyArray) {
-                return new PyString(obj.toString());
+
+        if (obj instanceof PyString) {
+            // Easy case
+            return (PyString)obj;
+
+        } else if (obj instanceof BufferProtocol) {
+            // Try to get a simple byte-oriented buffer
+            PyBuffer buf = null;
+            try {
+                buf = ((BufferProtocol)obj).getBuffer(PyBUF.SIMPLE);
+                // ... and treat those bytes as a PyString
+                String s = StringUtil.fromBytes(buf);
+                return new PyString(s);
+            } catch (Exception e) {
+                // Wrong kind of buffer: generic error message will do
+            } finally {
+                // If we got a buffer, we should release it
+                if (buf != null) {
+                    buf.release();
+                }
             }
-            throw Py.TypeError("expected str or unicode but got " + obj.getType());
+
+        } else if (obj instanceof PyArray) {
+            // PyArray can do something similar
+            return new PyString(obj.toString());
         }
-        return (PyString)obj;
+
+        // None of those things worked
+        throw Py.TypeError("expected string or buffer, but got " + obj.getType());
     }
 }
-
-

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


More information about the Jython-checkins mailing list