[Jython-checkins] jython: Fix repr(file) to use repr(filename) with escapes and quotes.

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


http://hg.python.org/jython/rev/884533bda7d7
changeset:   7157:884533bda7d7
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Wed Nov 20 22:21:53 2013 +0000
summary:
  Fix repr(file) to use repr(filename) with escapes and quotes.
Removes a skip in test_file2k, and aligns Jython behaviour with CPython 2.7.5,
amending test_repr to require %r not '%s' filename format.

files:
  Lib/test/test_file2k.py         |   8 +++++---
  Lib/test/test_repr.py           |   4 ++--
  src/org/python/core/PyFile.java |  10 +++++++---
  3 files changed, 14 insertions(+), 8 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
@@ -87,13 +87,15 @@
         self.assertRaises(TypeError, self.f.writelines,
                           [NonString(), NonString()])
 
-    @unittest.skipIf(test_support.is_jython, "FIXME: Not working on Jython")
     def testRepr(self):
         # verify repr works
         self.assertTrue(repr(self.f).startswith("<open file '" + TESTFN))
         # see issue #14161
-        # Windows doesn't like \r\n\t" in the file name, but ' is ok
-        fname = 'xx\rxx\nxx\'xx"xx' if sys.platform != "win32" else "xx'xx"
+        if sys.platform == "win32" or (test_support.is_jython and os._name == "nt"):
+            # Windows doesn't like \r\n\t" in the file name, but ' is ok
+            fname = "xx'xx"
+        else:
+            fname = 'xx\rxx\nxx\'xx"xx'
         with open(fname, 'w') as f:
             self.addCleanup(os.remove, fname)
             self.assertTrue(repr(f).startswith(
diff --git a/Lib/test/test_repr.py b/Lib/test/test_repr.py
--- a/Lib/test/test_repr.py
+++ b/Lib/test/test_repr.py
@@ -130,10 +130,10 @@
     def test_file(self):
         fp = open(unittest.__file__)
         self.assertTrue(repr(fp).startswith(
-            "<open file '%s', mode 'r' at 0x" % unittest.__file__))
+            "<open file %r, mode 'r' at 0x" % unittest.__file__))
         fp.close()
         self.assertTrue(repr(fp).startswith(
-            "<closed file '%s', mode 'r' at 0x" % unittest.__file__))
+            "<closed file %r, mode 'r' at 0x" % unittest.__file__))
 
     def test_lambda(self):
         self.assertTrue(repr(lambda x: x).startswith(
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
@@ -521,11 +521,15 @@
     final String file_toString() {
         String state = file.closed() ? "closed" : "open";
         String id = Py.idstr(this);
+        String escapedName;
         if (name instanceof PyUnicode) {
-            String escapedName = PyString.encode_UnicodeEscape(name.toString(), false);
-            return String.format("<%s file u'%s', mode '%s' at %s>", state, escapedName, mode, id);
+            // unicode: always uses the format u'%s', and the escaped value thus:
+            escapedName = "u'"+PyString.encode_UnicodeEscape(name.toString(), false)+"'";
+        } else {
+            // anything else: uses repr(), which for str (common case) is smartly quoted
+            escapedName = name.__repr__().getString();
         }
-        return String.format("<%s file '%s', mode '%s' at %s>", state, name, mode, id);
+        return String.format("<%s file %s, mode '%s' at %s>", state, escapedName, mode, id);
     }
 
     @Override

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


More information about the Jython-checkins mailing list