[Jython-checkins] jython: Implement conformant _io.FileIO.__repr__

jeff.allen jython-checkins at python.org
Sun Dec 16 01:20:06 CET 2012


http://hg.python.org/jython/rev/46e2e4f64981
changeset:   6901:46e2e4f64981
user:        Jeff Allen <ja...py at farowl.co.uk>
date:        Thu Dec 13 23:11:03 2012 +0000
summary:
  Implement conformant _io.FileIO.__repr__
Improved __repr__, __str__ implementation so it passes test.test_fileio.AutoFileTests.testRepr,
and removed skip of that test. Also catch BufferError in and convert to TypeError in
_io._IOBase support for i/o. Some re-ordering in PyFileIO.java. test_io score unchanged.

files:
  Lib/test/test_fileio.py                  |    1 -
  src/org/python/modules/_io/PyFileIO.java |  114 +++++-----
  src/org/python/modules/_io/PyIOBase.java |   29 ++-
  3 files changed, 79 insertions(+), 65 deletions(-)


diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -89,7 +89,6 @@
         self.assertEqual(self.f.readline(None), b"hi\n")
         self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"])
 
-    @unittest.skipIf(is_jython, "FIXME: unicode+slightly different in Jython")
     def testRepr(self):
         self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode='%s'>"
                                        % (self.f.name, self.f.mode))
diff --git a/src/org/python/modules/_io/PyFileIO.java b/src/org/python/modules/_io/PyFileIO.java
--- a/src/org/python/modules/_io/PyFileIO.java
+++ b/src/org/python/modules/_io/PyFileIO.java
@@ -61,9 +61,6 @@
     @ExposedGet
     public final boolean closefd;
 
-    /** The mode as given to the constructor */
-    private OpenMode openMode;
-
     /** The mode as a PyString based on readable and writable */
     @ExposedGet(doc = BuiltinDocs.file_mode_doc)
     public final PyString mode;
@@ -102,7 +99,6 @@
         this.ioDelegate = getFileIO(file, mode, closefd);
         this.closefd = closefd;
         this.name = file;
-        this.openMode = mode;
 
         readable = mode.reading | mode.updating;
         writable = mode.writing | mode.updating | mode.appending;
@@ -269,6 +265,23 @@
     }
 
     @Override
+    public long seek(long pos, int whence) {
+        return FileIO_seek(pos, whence);
+    }
+
+    @ExposedMethod(defaults = "0", doc = seek_doc)
+    final long FileIO_seek(long pos, int whence) {
+        if (__closed) {
+            throw closedValueError();
+        }
+        synchronized (ioDelegate) {
+            return ioDelegate.seek(pos, whence);
+        }
+    }
+
+    // _IOBase.tell() is correct for us
+
+    @Override
     public long truncate() {
         return _truncate();
     }
@@ -326,6 +339,23 @@
     }
 
     @Override
+    public boolean seekable() {
+        return FileIO_seekable();
+    }
+
+    @ExposedMethod(doc = seekable_doc)
+    final boolean FileIO_seekable() {
+        if (__closed) {
+            throw closedValueError();
+        }
+        if (!seekableKnown) {
+            seekable = ioDelegate.seek(0, 0) >= 0;
+            seekableKnown = true;
+        }
+        return seekable;
+    }
+
+    @Override
     public boolean readable() throws PyException {
         return FileIO_readable();
     }
@@ -351,47 +381,14 @@
         return writable;
     }
 
-    @ExposedMethod(defaults = {"0"}, doc = BuiltinDocs.file_seek_doc)
-    final synchronized PyObject FileIO_seek(long pos, int how) {
-        if (__closed) {
-            throw closedValueError();
-        }
-        return Py.java2py(ioDelegate.seek(pos, how));
+    @Override
+    public PyObject fileno() {
+        return FileIO_fileno();
     }
 
-    @Override
-    public boolean seekable() {
-        return FileIO_seekable();
-    }
-
-    @ExposedMethod(doc = "True if file supports random-access.")
-    final boolean FileIO_seekable() {
-        if (__closed) {
-            throw closedValueError();
-        }
-        if (!seekableKnown) {
-            seekable = ioDelegate.seek(0, 0) >= 0;
-            seekableKnown = true;
-        }
-        return seekable;
-    }
-
-    @ExposedMethod(doc = BuiltinDocs.file_tell_doc)
-    final synchronized long FileIO_tell() {
-        if (__closed) {
-            throw closedValueError();
-        }
-        return ioDelegate.tell();
-    }
-
-    @Override
-    public long tell() {
-        return FileIO_tell();
-    }
-
-    @Override
-    public boolean isatty() {
-        return FileIO_isatty();
+    @ExposedMethod(doc = fileno_doc)
+    final PyObject FileIO_fileno() {
+        return PyJavaType.wrapJavaObject(ioDelegate.fileno());
     }
 
     @ExposedMethod(doc = isatty_doc)
@@ -402,19 +399,6 @@
         return ioDelegate.isatty();
     }
 
-    @Override
-    public PyObject fileno() {
-        return FileIO_fileno();
-    }
-
-    @ExposedMethod(doc = BuiltinDocs.file_fileno_doc)
-    final PyObject FileIO_fileno() {
-        if (__closed) {
-            throw closedValueError();
-        }
-        return PyJavaType.wrapJavaObject(ioDelegate.fileno());
-    }
-
     // fileio.c has no flush(), but why not, when there is fdflush()?
     // And it is a no-op for Jython io.FileIO, but why when there is FileChannel.force()?
     @Override
@@ -431,18 +415,24 @@
         }
     }
 
-    @ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.file___str___doc)
+    @ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.object___str___doc)
     final String FileIO_toString() {
-        if (name instanceof PyUnicode) {
-            String escapedName = PyString.encode_UnicodeEscape(name.toString(), false);
-            return String.format("<_io.FileIO name='%s', mode='%s'>", escapedName, mode);
+        if (closed()) {
+            return "<_io.FileIO [closed]>";
+        } else if (name instanceof PyString) {
+            String xname = name.asString();
+            if (name instanceof PyUnicode) {
+                xname = PyString.encode_UnicodeEscape(xname, false);
+            }
+            return String.format("<_io.FileIO name='%s' mode='%s'>", xname, mode);
+        } else {
+            return String.format("<_io.FileIO fd=%s mode='%s'>", fileno(), mode);
         }
-        return String.format("<_io.FileIO name='%s', mode='%s'>", name, mode);
     }
 
     @Override
     public String toString() {
-        return FileIO_toString();
+        return FileIO_toString().toString();
     }
 
     /**
diff --git a/src/org/python/modules/_io/PyIOBase.java b/src/org/python/modules/_io/PyIOBase.java
--- a/src/org/python/modules/_io/PyIOBase.java
+++ b/src/org/python/modules/_io/PyIOBase.java
@@ -115,6 +115,13 @@
         return _IOBase_seek(pos, whence);
     }
 
+    /**
+     * Position the read or write pointer at a given byte offset <code>pos</code> relative to the
+     * start.
+     *
+     * @param pos relative to the start
+     * @return the new current position
+     */
     public final long seek(long pos) {
         return seek(pos, 0);
     }
@@ -713,7 +720,16 @@
      */
     protected static PyBuffer readablePyBuffer(PyObject obj) throws PyException {
         if (obj instanceof BufferProtocol) {
-            return ((BufferProtocol)obj).getBuffer(PyBUF.SIMPLE);
+            try {
+                return ((BufferProtocol)obj).getBuffer(PyBUF.SIMPLE);
+            } catch (PyException pye) {
+                if (pye.match(Py.BufferError)) {
+                    // If we can't get a buffer on the object, say it's the wrong type
+                    throw Py.TypeError(String.format("(BufferError) %s", pye.getMessage()));
+                } else {
+                    throw pye;
+                }
+            }
         } else {
             // Something else we can view as a String?
             String s;
@@ -741,7 +757,16 @@
      */
     protected static PyBuffer writablePyBuffer(PyObject obj) throws PyException {
         if (obj instanceof BufferProtocol) {
-            return ((BufferProtocol)obj).getBuffer(PyBUF.WRITABLE);
+            try {
+                return ((BufferProtocol)obj).getBuffer(PyBUF.WRITABLE);
+            } catch (PyException pye) {
+                if (pye.match(Py.BufferError)) {
+                    // If we can't get a buffer on the object, say it's the wrong type
+                    throw Py.TypeError(String.format("(BufferError) %s", pye.getMessage()));
+                } else {
+                    throw pye;
+                }
+            }
         } else {
             // Can't be a buffer: complain
             String fmt = "object must be read-write buffer, not %.100s";

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


More information about the Jython-checkins mailing list