diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-03-11 23:17:02 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-03-11 23:17:02 (GMT) |
commit | 11f8b6872a779b6b03fc070ad64ed778c835435a (patch) | |
tree | c6eabf7b222c7bcb0093f0eaac4aec0639f75df2 /Objects/fileobject.c | |
parent | f60845b70a78c0e29dc0e865c6cd9e2e35e635f5 (diff) | |
download | cpython-11f8b6872a779b6b03fc070ad64ed778c835435a.zip cpython-11f8b6872a779b6b03fc070ad64ed778c835435a.tar.gz cpython-11f8b6872a779b6b03fc070ad64ed778c835435a.tar.bz2 |
#14161: fix the __repr__ of file objects to escape the file name.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 737ebb7..79b9aad 100644 --- 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; + 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 @@ 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; } } |