From 11f8b6872a779b6b03fc070ad64ed778c835435a Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Mon, 12 Mar 2012 01:17:02 +0200 Subject: [PATCH] #14161: fix the __repr__ of file objects to escape the file name. --- 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 index 399f119b81b..0c892bd2baf 100644 --- a/Lib/test/test_file2k.py +++ b/Lib/test/test_file2k.py @@ -89,6 +89,13 @@ class AutoFileTests(unittest.TestCase): def testRepr(self): # verify repr works self.assertTrue(repr(self.f).startswith("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 @@ file_repr(PyFileObject *f) 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; } }