cpython: 6c1964dee98b (original) (raw)
Mercurial > cpython
changeset 75527:6c1964dee98b 2.7
#14161: fix the __repr__ of file objects to escape the file name. [#14161]
Ezio Melotti ezio.melotti@gmail.com | |
---|---|
date | Mon, 12 Mar 2012 01:17:02 +0200 |
parents | b6ec3b717f7e |
children | 86c749151660 |
files | Lib/test/test_file2k.py Misc/NEWS Objects/fileobject.c |
diffstat | 3 files changed, 19 insertions(+), 4 deletions(-)[+] [-] Lib/test/test_file2k.py 7 Misc/NEWS 2 Objects/fileobject.c 14 |
line wrap: on
line diff
--- 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("<open file '" + TESTFN))
# see issue #14161[](#l1.7)
# Windows doesn't like \r\n\t" in the file name, but ' is ok[](#l1.8)
fname = 'xx\rxx\nxx\'xx"xx' if sys.platform != "win32" else "xx'xx"[](#l1.9)
with open(fname, 'w') as f:[](#l1.10)
self.addCleanup(os.remove, fname)[](#l1.11)
self.assertTrue(repr(f).startswith([](#l1.12)
"<open file %r, mode 'w' at" % fname))[](#l1.13)
def testErrors(self): self.f.close()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -9,6 +9,8 @@ What's New in Python 2.7.4 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
).
--- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -635,10 +635,11 @@ file_dealloc(PyFileObject *f) static PyObject * file_repr(PyFileObject *f) {
PyObject *ret = NULL;[](#l3.11)
PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);[](#l3.12)
name = PyUnicode_AsUnicodeEscapeString(f->f_name);[](#l3.13) const char *name_str = name ? PyString_AsString(name) : "?";[](#l3.14) ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",[](#l3.15) f->f_fp == NULL ? "closed" : "open",[](#l3.16)
@@ -649,11 +650,16 @@ file_repr(PyFileObject *f) return ret; #endif } else {
return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",[](#l3.21)
name = PyObject_Repr(f->f_name);[](#l3.22)
if (name == NULL)[](#l3.23)
return NULL;[](#l3.24)
ret = PyString_FromFormat("<%s file %s, mode '%s' at %p>",[](#l3.25) f->f_fp == NULL ? "closed" : "open",[](#l3.26)
PyString_AsString(f->f_name),[](#l3.27)