Commits

Anonymous committed 092e145 Draft

Add Java _io.UnsupportedOperation

Comments (0)

Files changed (3)

 
 from __future__ import (print_function, unicode_literals)
 
-import _io  # for some diagnostic code
+import _io  # Java implementations to replace this module
 
 import os
 import abc
         return open(*args, **kwargs)
 
 
-class UnsupportedOperation(ValueError, IOError):
-    pass
+from _io import UnsupportedOperation
 
 
 class _IOBase:

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

-/* Copyright (c) Jython Developers */
+/* Copyright (c)2012 Jython Developers */
 package org.python.modules._io;
 
 import org.python.core.ClassDictInit;
+import org.python.core.Py;
+import org.python.core.PyException;
 import org.python.core.PyObject;
 import org.python.core.PyString;
+import org.python.core.PyStringMap;
+import org.python.core.PyType;
+import org.python.core.imp;
 
 /**
- * The Python _fileio module.
+ * The Python _io module.
  */
 public class _io implements ClassDictInit {
 
-    public static final PyString __doc__ = new PyString("Fast implementation of io.FileIO.");
+    public static final PyString __doc__ = new PyString("Java implementation of _io.");
 
+    /**
+     * This method is called when the module is loaded, to populate the namespace (dictionary) of
+     * the module. The dictionary has been initialised at this point reflectively from the methods
+     * of this class and this method nulls those entries that ought not to be exposed.
+     *
+     * @param dict namespace of the module
+     */
     public static void classDictInit(PyObject dict) {
         dict.__setitem__("__name__", new PyString("_io"));
         dict.__setitem__("__doc__", __doc__);
         dict.__setitem__("FileIO", PyFileIO.TYPE);
 
+        // Define UnsupportedOperation exception by constructing the type
+
+        PyObject exceptions = imp.load("exceptions");
+        PyObject ValueError = exceptions.__getattr__("ValueError");
+        PyObject IOError = exceptions.__getattr__("IOError");
+        // Equivalent to class UnsupportedOperation(ValueError, IOError) : pass
+        // UnsupportedOperation = makeException(dict, "UnsupportedOperation", ValueError, IOError);
+        // XXX Work-around: slots not properly initialised unless IOError comes first
+        UnsupportedOperation = makeException(dict, "UnsupportedOperation", IOError, ValueError);
+
         // Hide from Python
         dict.__setitem__("classDictInit", null);
+        dict.__setitem__("makeException", null);
+    }
+
+    /** A Python class for the <code>UnsupportedOperation</code> exception. */
+    public static PyType UnsupportedOperation;
+
+    /**
+     * A function that returns a {@link PyException}, which is a Java exception suitable for
+     * throwing, and that will be raised as an <code>UnsupportedOperation</code> Python exception.
+     *
+     * @param message text message parameter to the Python exception
+     * @return nascent <code>UnsupportedOperation</code> Python exception
+     */
+    public static PyException UnsupportedOperation(String message) {
+        return new PyException(UnsupportedOperation, message);
+    }
+
+    /**
+     * Convenience method for constructing a type object of a Python exception, named as given, and
+     * added to the namespace of the "_io" module.
+     *
+     * @param dict module dictionary
+     * @param excname name of the exception
+     * @param bases one or more bases (superclasses)
+     * @return the constructed exception type
+     */
+    private static PyType makeException(PyObject dict, String excname, PyObject... bases) {
+        PyStringMap classDict = new PyStringMap();
+        classDict.__setitem__("__module__", Py.newString("_io"));
+        PyType type = (PyType)Py.makeClass(excname, bases, classDict);
+        dict.__setitem__(excname, type);
+        return type;
     }
 
 }

tests/java/org/python/modules/_io/_ioTest.java

+/* Copyright (c)2012 Jython Developers */
+package org.python.modules._io;
+
+import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.*;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.python.core.PyException;
+import org.python.core.PyObject;
+import org.python.util.PythonInterpreter;
+
+/**
+ * Tests of specific methods in the Python _io module (org.python.modules._io._io). There is an
+ * extensive regression test in Lib/test/test_io.py, but that is quite complex. This test case
+ * exists to exercise selected functionality in isolation.
+ */
+public class _ioTest {
+
+    /** We need the interpreter to be initialised for these tests **/
+    PythonInterpreter interp;
+
+    /**
+     * Initialisation called before each test.
+     *
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+
+        // Initialise a Jython interpreter
+        interp = new PythonInterpreter();
+
+    }
+
+    /**
+     * Test importing the _io module into the global namespace of {@link #interp}.
+     */
+    @Test
+    public void moduleImport() {
+        interp.exec("import _io");
+        PyObject _io = interp.get("_io");
+        org.junit.Assert.assertNotNull(_io);
+    }
+
+    /**
+     * Test raising a Python _io.UnsupportedOperation from Java code directly.
+     */
+    @Test
+    public void javaRaiseUnsupportedOperation() {
+
+        // Built-in modules seem not to initialise until we actually use an interpreter
+        interp.exec("import io");
+
+        // There should be a helper function
+        PyException pye = _io.UnsupportedOperation("Message from _ioTest");
+        PyObject type = pye.type;
+        String repr = type.toString();
+        assertEquals("Class name", "<class '_io.UnsupportedOperation'>", repr);
+
+        // Raise from Java into Python and catch it in a variable: _IOBase.fileno() raises it
+        interp.exec("try :\n    io.IOBase().fileno()\n" + "except Exception as e:\n    pass");
+        PyObject e = interp.get("e");
+
+        String m = e.toString();
+        assertThat(m, both(containsString("UnsupportedOperation")).and(containsString("fileno")));
+
+    }
+
+    /**
+     * Test raising a Python _io.UnsupportedOperation from Python code into Java.
+     */
+    @Test
+    public void pythonRaiseUnsupportedOperation() {
+        interp.exec("import _io");
+        try {
+            interp.exec("raise _io.UnsupportedOperation()");
+            fail("_io.UnsupportedOperation not raised when expected");
+        } catch (PyException e) {
+            assertEquals(_io.UnsupportedOperation, e.type);
+        }
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.