[Jython-checkins] jython: Sub-classes of file are made able to override close() method.

jeff.allen jython-checkins at python.org
Sat Nov 30 17:52:54 CET 2013


http://hg.python.org/jython/rev/1a8d17fa62ae
changeset:   7163:1a8d17fa62ae
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Tue Nov 26 22:31:53 2013 +0000
summary:
  Sub-classes of file are made able to override close() method.
Adds a close() method to file.derived and provides a regenerated
PyFileDerived class. testExit in test_file2k now passes.

files:
  Lib/test/test_file2k.py                |   7 +--
  src/org/python/core/PyFile.java        |   2 +-
  src/org/python/core/PyFileDerived.java |  24 ++++++++++++++
  src/templates/file.derived             |  13 +++++++
  4 files changed, 41 insertions(+), 5 deletions(-)


diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py
--- a/Lib/test/test_file2k.py
+++ b/Lib/test/test_file2k.py
@@ -478,7 +478,6 @@
 
 class FileSubclassTests(unittest.TestCase):
 
-    @unittest.skipIf(test_support.is_jython, "FIXME: Not working on Jython")
     def testExit(self):
         # test that exiting with context calls subclass' close
         class C(file):
@@ -754,10 +753,10 @@
 
 def test_main():
     run_unittest(
-             AutoFileTests, 
-             OtherFileTests, 
+             AutoFileTests,
+             OtherFileTests,
              FileSubclassTests,
-             FileThreadingTests, 
+             FileThreadingTests,
              StdoutTests
          )
 
diff --git a/src/org/python/core/PyFile.java b/src/org/python/core/PyFile.java
--- a/src/org/python/core/PyFile.java
+++ b/src/org/python/core/PyFile.java
@@ -526,7 +526,7 @@
 
     @ExposedMethod(doc = BuiltinDocs.file___exit___doc)
     final void file___exit__(PyObject type, PyObject value, PyObject traceback) {
-        file_close();
+        close();
     }
 
     public void __exit__(PyObject type, PyObject value, PyObject traceback) {
diff --git a/src/org/python/core/PyFileDerived.java b/src/org/python/core/PyFileDerived.java
--- a/src/org/python/core/PyFileDerived.java
+++ b/src/org/python/core/PyFileDerived.java
@@ -560,6 +560,18 @@
         return super.__ne__(other);
     }
 
+    public PyObject __format__(PyObject other) {
+        PyType self_type=getType();
+        PyObject impl=self_type.lookup("__format__");
+        if (impl!=null) {
+            PyObject res=impl.__get__(this,self_type).__call__(other);
+            if (res==Py.NotImplemented)
+                return null;
+            return res;
+        }
+        return super.__format__(other);
+    }
+
     public PyObject __iadd__(PyObject other) {
         PyType self_type=getType();
         PyObject impl=self_type.lookup("__iadd__");
@@ -1109,6 +1121,18 @@
         return super.__coerce_ex__(o);
     }
 
+    // Hand-crafted in file.derived
+
+    public void close() {
+        PyType self_type=getType();
+        PyObject impl=self_type.lookup("close");
+        if (impl!=null) {
+            impl.__get__(this,self_type).__call__();
+        } else {
+            super.close();
+        }
+    }
+
     public String toString() {
         PyType self_type=getType();
         PyObject impl=self_type.lookup("__repr__");
diff --git a/src/templates/file.derived b/src/templates/file.derived
--- a/src/templates/file.derived
+++ b/src/templates/file.derived
@@ -2,3 +2,16 @@
 want_dict: true
 ctr:
 incl: object
+
+rest:
+    // Hand-crafted in file.derived
+
+    public void close() {
+        PyType self_type = getType();
+        PyObject impl = self_type.lookup("close");
+        if (impl != null) {
+            impl.__get__(this, self_type).__call__();
+        } else {
+            super.close();
+        }
+    }

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


More information about the Jython-checkins mailing list