Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
11f8b687
Commit
11f8b687
authored
Mar 12, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#14161: fix the __repr__ of file objects to escape the file name.
parent
f60845b7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
4 deletions
+19
-4
Lib/test/test_file2k.py
Lib/test/test_file2k.py
+7
-0
Misc/NEWS
Misc/NEWS
+2
-0
Objects/fileobject.c
Objects/fileobject.c
+10
-4
No files found.
Lib/test/test_file2k.py
View file @
11f8b687
...
@@ -89,6 +89,13 @@ class AutoFileTests(unittest.TestCase):
...
@@ -89,6 +89,13 @@ class AutoFileTests(unittest.TestCase):
def
testRepr
(
self
):
def
testRepr
(
self
):
# verify repr works
# verify repr works
self
.
assertTrue
(
repr
(
self
.
f
).
startswith
(
"<open file '"
+
TESTFN
))
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
\
r
xx
\
n
xx
\
'
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
):
def
testErrors
(
self
):
self
.
f
.
close
()
self
.
f
.
close
()
...
...
Misc/NEWS
View file @
11f8b687
...
@@ -9,6 +9,8 @@ What's New in Python 2.7.4
...
@@ -9,6 +9,8 @@ What's New in Python 2.7.4
Core and Builtins
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
- Issue #1469629: Allow cycles through an object'
s
__dict__
slot
to
be
collected
.
(
For
example
if
``
x
.
__dict__
is
x
``).
collected
.
(
For
example
if
``
x
.
__dict__
is
x
``).
...
...
Objects/fileobject.c
View file @
11f8b687
...
@@ -635,10 +635,11 @@ file_dealloc(PyFileObject *f)
...
@@ -635,10 +635,11 @@ file_dealloc(PyFileObject *f)
static
PyObject
*
static
PyObject
*
file_repr
(
PyFileObject
*
f
)
file_repr
(
PyFileObject
*
f
)
{
{
PyObject
*
ret
=
NULL
;
PyObject
*
name
=
NULL
;
if
(
PyUnicode_Check
(
f
->
f_name
))
{
if
(
PyUnicode_Check
(
f
->
f_name
))
{
#ifdef Py_USING_UNICODE
#ifdef Py_USING_UNICODE
PyObject
*
ret
=
NULL
;
name
=
PyUnicode_AsUnicodeEscapeString
(
f
->
f_name
);
PyObject
*
name
=
PyUnicode_AsUnicodeEscapeString
(
f
->
f_name
);
const
char
*
name_str
=
name
?
PyString_AsString
(
name
)
:
"?"
;
const
char
*
name_str
=
name
?
PyString_AsString
(
name
)
:
"?"
;
ret
=
PyString_FromFormat
(
"<%s file u'%s', mode '%s' at %p>"
,
ret
=
PyString_FromFormat
(
"<%s file u'%s', mode '%s' at %p>"
,
f
->
f_fp
==
NULL
?
"closed"
:
"open"
,
f
->
f_fp
==
NULL
?
"closed"
:
"open"
,
...
@@ -649,11 +650,16 @@ file_repr(PyFileObject *f)
...
@@ -649,11 +650,16 @@ file_repr(PyFileObject *f)
return
ret
;
return
ret
;
#endif
#endif
}
else
{
}
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"
,
f
->
f_fp
==
NULL
?
"closed"
:
"open"
,
PyString_AsString
(
f
->
f_
name
),
PyString_AsString
(
name
),
PyString_AsString
(
f
->
f_mode
),
PyString_AsString
(
f
->
f_mode
),
f
);
f
);
Py_XDECREF
(
name
);
return
ret
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment