[Jython-checkins] jython: Implement bytearray.reverse (and reversed(bytearray)) and __getitem__().

frank.wierzbicki jython-checkins at python.org
Wed Jun 13 20:44:04 CEST 2012


http://hg.python.org/jython/rev/4c3a55c73edc
changeset:   6703:4c3a55c73edc
user:        Jeff Allen <ja...py at farowl.co.uk>
date:        Wed Jun 06 14:42:08 2012 +0100
summary:
  Implement bytearray.reverse (and reversed(bytearray)) and __getitem__().
Surprisingly, the lack of an exposed __getitem__ did not show up until test_reversed(). Now scoring 2 failures and 31 errors on test_bytes.py.

files:
  src/org/python/core/PyByteArray.java |  46 ++++++++++-----
  1 files changed, 29 insertions(+), 17 deletions(-)


diff --git a/src/org/python/core/PyByteArray.java b/src/org/python/core/PyByteArray.java
--- a/src/org/python/core/PyByteArray.java
+++ b/src/org/python/core/PyByteArray.java
@@ -53,21 +53,6 @@
         init(size);
     }
 
-//    /**
-//     * Create zero-filled Python bytearray of specified size and capacity for appending to. The
-//     * capacity is the (minimum) storage allocated, while the size is the number of zero-filled
-//     * bytes it currently contains. Simple append and extend operations on a bytearray will not
-//     * shrink the allocated capacity, but insertions and deletions may cause it to be reallocated at
-//     * the size then in use.
-//     *
-//     * @param size of bytearray
-//     * @param capacity allocated
-//     */
-//    public PyByteArray(int size, int capacity) {
-//        super(TYPE);
-//        setStorage(new byte[capacity], size);
-//    }
-//
     /**
      * Construct bytearray by copying values from int[].
      *
@@ -1095,6 +1080,12 @@
         return result;
     }
 
+    @ExposedMethod(doc = BuiltinDocs.bytearray___getitem___doc)
+    final synchronized PyObject bytearray___getitem__(PyObject index) {
+        // Let the SequenceIndexDelegate take care of it
+        return delegator.checkIdxAndGetItem(index);
+    }
+
     @Override
     public PyObject __iadd__(PyObject o) {
         return bytearray___iadd__(o);
@@ -1346,6 +1337,26 @@
     }
 
     /**
+     * Reverses the contents of the byte array in place. The reverse() methods modify in place for
+     * economy of space when reversing a large array. It doesn't return the reversed array to remind
+     * you that it works by side effect.
+     */
+    public void reverse() {
+        bytearray_reverse();
+    }
+
+    @ExposedMethod(doc = BuiltinDocs.bytearray_reverse_doc)
+    final synchronized void bytearray_reverse() {
+        // In place reverse
+        int a = offset, b = offset + size;
+        while (--b > a) {
+            byte t = storage[b];
+            storage[b] = storage[a];
+            storage[a++] = t;
+        }
+    }
+
+    /**
      * Implementation of Python <code>rfind(sub)</code>. Return the highest index in the byte array
      * where byte sequence <code>sub</code> is found. Return -1 if <code>sub</code> is not found.
      *
@@ -1596,8 +1607,9 @@
     }
 
     @ExposedMethod(doc = BuiltinDocs.bytearray___setitem___doc)
-    final synchronized void bytearray___setitem__(PyObject o, PyObject def) {
-        seq___setitem__(o, def);
+    final synchronized void bytearray___setitem__(PyObject index, PyObject value) {
+        // Let the SequenceIndexDelegate take care of it
+        delegator.checkIdxAndSetItem(index, value);
     }
 
     @Override

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


More information about the Jython-checkins mailing list