Commits

Anonymous committed a307089 Merge

Merge fixes from test_memoryio

Comments (0)

Files changed (5)

 
     def seekable(self):
         self._checkInitialized()    # Jython: to forbid use in an invalid state
-        self._checkClosed()         # Jython: compatibility with C implementation
         return self._seekable
 
     def readable(self):
         self._checkInitialized()    # Jython: to forbid use in an invalid state
-        self._checkClosed()         # Jython: compatibility with C implementation
         return self.buffer.readable()
 
     def writable(self):
         self._checkInitialized()    # Jython: to forbid use in an invalid state
-        self._checkClosed()         # Jython: compatibility with C implementation
         return self.buffer.writable()
 
     def flush(self):
-        self._checkInitialized()       # Jython: to forbid use in an invalid state
-        self._checkClosed()         # Jython: compatibility with C implementation
+        self._checkInitialized()    # Jython: to forbid use in an invalid state
         self.buffer.flush()
         self._telling = self._seekable
 
         pass
 
 
-@unittest.skipIf(os.name == 'nt' or 
-                 (sys.platform[:4] == 'java' and os._name == 'nt'),
-                 'POSIX signals required for this test.')
+@unittest.skipIf(support.is_jython, "Jython does not support os.pipe()")
+@unittest.skipIf(os.name == 'nt', 'POSIX signals required for this test.')
 class SignalsTest(unittest.TestCase):
 
     def setUp(self):

Lib/test/test_memoryio.py

 
 class MemoryTestMixin:
 
-    # This test isn't working on Ubuntu on an Apple Intel powerbook,
-    # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
-    # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
-    @unittest.skipIf(support.is_jython,
-                     "FIXME: Currently not working on jython")
     def test_detach(self):
         buf = self.ioclass()
         self.assertRaises(self.UnsupportedOperation, buf.detach)
         memio.close()
         self.assertRaises(ValueError, memio.read)
 
-    # This test isn't working on Ubuntu on an Apple Intel powerbook,
-    # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
-    # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
-    @unittest.skipIf(support.is_jython,
-                     "FIXME: Currently not working on jython")
     def test_readline(self):
         buf = self.buftype("1234567890\n")
         memio = self.ioclass(buf * 2)
 
         self.assertEqual(memio.flush(), None)
 
-    # This test isn't working on Ubuntu on an Apple Intel powerbook,
-    # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
-    # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
-    @unittest.skipIf(support.is_jython,
-                     "FIXME: Currently not working on jython")
     def test_flags(self):
         memio = self.ioclass()
 
 
     UnsupportedOperation = pyio.UnsupportedOperation
 
+    # When Jython tries to use UnsupportedOperation as _pyio defines it, it runs
+    # into a problem with multiple inheritance and the slots array: issue 1996.
+    # Override the affected test version just so we can skip it visibly.
+    @unittest.skipIf(support.is_jython, "FIXME: Jython issue 1996")
+    def test_detach(self):
+        pass
+
     @staticmethod
     def buftype(s):
         return s.encode("ascii")
         memio.seek(1, 1)
         self.assertEqual(memio.read(), buf[1:])
 
-    # This test isn't working on Ubuntu on an Apple Intel powerbook,
-    # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
-    # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
-    @unittest.skipIf(support.is_jython,
-                     "FIXME: Currently not working on jython")
     def test_unicode(self):
         memio = self.ioclass()
 
     UnsupportedOperation = pyio.UnsupportedOperation
     EOF = ""
 
+    # When Jython tries to use UnsupportedOperation as _pyio defines it, it runs
+    # into a problem with multiple inheritance and the slots array: issue 1996.
+    # Override the affected test version just so we can skip it visibly.
+    @unittest.skipIf(support.is_jython, "FIXME: Jython issue 1996")
+    def test_detach(self):
+        pass
+
 
 class PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
     """Test if pickle restores properly the internal state of StringIO.
         "array.array() does not have the new buffer API"
     )(PyBytesIOTest.test_bytes_array)
 
+    # Re-instate test_detach skipped by Jython in PyBytesIOTest
+    if support.is_jython: # FIXME: Jython issue 1996
+        test_detach = MemoryTestMixin.test_detach
 
     # This test isn't working on Ubuntu on an Apple Intel powerbook,
     # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
     # XXX: For the Python version of io.StringIO, this is highly
     # dependent on the encoding used for the underlying buffer.
 
-    # This test isn't working on Ubuntu on an Apple Intel powerbook,
-    # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
-    # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
-    @unittest.skipIf(support.is_jython,
-                     "FIXME: Currently not working on jython")
+    # Re-instate test_detach skipped by Jython in PyBytesIOTest
+    if support.is_jython: # FIXME: Jython issue 1996
+        test_detach = MemoryTestMixin.test_detach
+
+    # This test checks that tell() results are consistent with the length of
+    # text written, but this is not documented in the API: only that seek()
+    # accept what tell() returns.
+    @unittest.skipIf(support.is_jython, "Exact value of tell() is CPython specific")
     def test_widechar(self):
         buf = self.buftype("\U0002030a\U00020347")
         memio = self.ioclass(buf)
         self.assertEqual(memio.tell(), len(buf) * 2)
         self.assertEqual(memio.getvalue(), buf + buf)
 
+    # This test checks that seek() accepts what tell() returns, without requiring
+    # that tell() return a particular absolute value. Conceived for Jython, but
+    # probably universal.
+    def test_widechar_seek(self):
+        buf = self.buftype("\U0002030aX\u00ca\U00020347\u05d1Y\u0628Z")
+        memio = self.ioclass(buf)
+        self.assertEqual(memio.getvalue(), buf)
+
+        # For each character in buf, read it back from memio and its tell value
+        chars = list(buf)
+        tells = list()
+        for ch in chars :
+            tells.append(memio.tell())
+            self.assertEqual(memio.read(1), ch)
+
+        # For each character in buf, seek to it and check it's there
+        chpos = zip(chars, tells)
+        chpos.reverse()
+        for ch, pos in chpos:
+            memio.seek(pos)
+            self.assertEqual(memio.read(1), ch)
+
+        # Check write after seek to end
+        memio.seek(0, 2)
+        self.assertEqual(memio.write(buf), len(buf))
+        self.assertEqual(memio.getvalue(), buf + buf)
+
     # This test isn't working on Ubuntu on an Apple Intel powerbook,
     # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47) 
     # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09

src/org/python/core/PyByteArray.java

 
     /**
      * Sets the given range of elements according to Python slice assignment semantics from a
-     * {@link PyString}.
+     * {@link PyString} that is not a {@link PyUnicode}.
      *
      * @see #setslice(int, int, int, PyObject)
      * @param start the position of the first element.
      * @param step the step size.
      * @param value a PyString object consistent with the slice assignment
      * @throws PyException (SliceSizeError) if the value size is inconsistent with an extended slice
+     * @throws PyException (ValueError) if the value is a <code>PyUnicode</code>
      */
     private void setslice(int start, int stop, int step, PyString value) throws PyException {
-        String v = value.asString();
-        int len = v.length();
-        if (step == 1) {
-            // Delete this[start:stop] and open a space of the right size
-            storageReplace(start, stop - start, len);
-            setBytes(start, v);
+        if (value instanceof PyUnicode) {
+            // Has to be 8-bit PyString
+            throw Py.TypeError("can't set bytearray slice from unicode");
         } else {
-            // This is an extended slice which means we are replacing elements
-            int n = sliceLength(start, stop, step);
-            if (n != len) {
-                throw SliceSizeError("bytes", len, n);
+            // Assignment is from 8-bit data
+            String v = value.asString();
+            int len = v.length();
+            if (step == 1) {
+                // Delete this[start:stop] and open a space of the right size
+                storageReplace(start, stop - start, len);
+                setBytes(start, v);
+            } else {
+                // This is an extended slice which means we are replacing elements
+                int n = sliceLength(start, stop, step);
+                if (n != len) {
+                    throw SliceSizeError("bytes", len, n);
+                }
+                setBytes(start, step, v);
             }
-            setBytes(start, step, v);
         }
     }
 

src/org/python/modules/_io/PyIOBase.java

     }
 
     @ExposedMethod(doc = flush_doc)
-    final void _IOBase_flush() {}
+    final void _IOBase_flush() {
+        // Even types for which this remains a no-op must complain if closed (e.g. BytesIO)
+        _checkClosed();
+    }
 
     /**
      * True if the object is closed to further <b>client</b> operations. It is the state accessed by
              */
             PyByteArray res = new PyByteArray();
 
-            while (remainingLimit > 0) {
+            while (--remainingLimit >= 0) {
 
                 /*
                  * read() returns a str of one byte, doing at most one read to refill, or it returns