[Python-checkins] cpython (2.7): #14161: fix the __repr__ of file objects to escape the file name.
ezio.melotti
python-checkins at python.org
Mon Mar 12 00:17:17 CET 2012
http://hg.python.org/cpython/rev/6c1964dee98b
changeset: 75527:6c1964dee98b
branch: 2.7
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Mon Mar 12 01:17:02 2012 +0200
summary:
#14161: fix the __repr__ of file objects to escape the file name.
files:
Lib/test/test_file2k.py | 7 +++++++
Misc/NEWS | 2 ++
Objects/fileobject.c | 14 ++++++++++----
3 files changed, 19 insertions(+), 4 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
@@ -89,6 +89,13 @@
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"
+ with open(fname, 'w') as f:
+ self.addCleanup(os.remove, fname)
+ self.assertTrue(repr(f).startswith(
+ "<open file %r, mode 'w' at" % fname))
def testErrors(self):
self.f.close()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,8 @@
Core and Builtins
-----------------
+- Issue #14161: fix the __repr__ of file objects to escape the file name.
+
- Issue #1469629: Allow cycles through an object's __dict__ slot to be
collected. (For example if ``x.__dict__ is x``).
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -635,10 +635,11 @@
static PyObject *
file_repr(PyFileObject *f)
{
+ PyObject *ret = NULL;
+ PyObject *name = NULL;
if (PyUnicode_Check(f->f_name)) {
#ifdef Py_USING_UNICODE
- PyObject *ret = NULL;
- PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);
+ name = PyUnicode_AsUnicodeEscapeString(f->f_name);
const char *name_str = name ? PyString_AsString(name) : "?";
ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
f->f_fp == NULL ? "closed" : "open",
@@ -649,11 +650,16 @@
return ret;
#endif
} else {
- return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
+ name = PyObject_Repr(f->f_name);
+ if (name == NULL)
+ return NULL;
+ ret = PyString_FromFormat("<%s file %s, mode '%s' at %p>",
f->f_fp == NULL ? "closed" : "open",
- PyString_AsString(f->f_name),
+ PyString_AsString(name),
PyString_AsString(f->f_mode),
f);
+ Py_XDECREF(name);
+ return ret;
}
}
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list