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
716c444e
Commit
716c444e
authored
May 23, 2009
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #5761: Add the name of the underlying file to the repr() of various IO objects.
parent
744af440
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
106 additions
and
13 deletions
+106
-13
Lib/_pyio.py
Lib/_pyio.py
+16
-1
Lib/test/test_fileio.py
Lib/test/test_fileio.py
+7
-3
Lib/test/test_io.py
Lib/test/test_io.py
+19
-1
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_io/bufferedio.c
Modules/_io/bufferedio.c
+24
-3
Modules/_io/fileio.c
Modules/_io/fileio.c
+18
-3
Modules/_io/textio.c
Modules/_io/textio.c
+19
-2
No files found.
Lib/_pyio.py
View file @
716c444e
...
...
@@ -736,6 +736,15 @@ class _BufferedIOMixin(BufferedIOBase):
def
mode
(
self
):
return
self
.
raw
.
mode
def
__repr__
(
self
):
clsname
=
self
.
__class__
.
__name__
try
:
name
=
self
.
name
except
AttributeError
:
return
"<_pyio.{0}>"
.
format
(
clsname
)
else
:
return
"<_pyio.{0} name={1!r}>"
.
format
(
clsname
,
name
)
### Lower-level APIs ###
def
fileno
(
self
):
...
...
@@ -1455,7 +1464,13 @@ class TextIOWrapper(TextIOBase):
# - "chars_..." for integer variables that count decoded characters
def
__repr__
(
self
):
return
"<TextIOWrapper encoding={0}>"
.
format
(
self
.
encoding
)
try
:
name
=
self
.
name
except
AttributeError
:
return
"<_pyio.TextIOWrapper encoding={0!r}>"
.
format
(
self
.
encoding
)
else
:
return
"<_pyio.TextIOWrapper name={0!r} encoding={1!r}>"
.
format
(
name
,
self
.
encoding
)
@
property
def
encoding
(
self
):
...
...
Lib/test/test_fileio.py
View file @
716c444e
...
...
@@ -70,9 +70,13 @@ class AutoFileTests(unittest.TestCase):
self
.
assertEquals
(
array
(
'b'
,
[
1
,
2
]),
a
[:
n
])
def
testRepr
(
self
):
self
.
assertEquals
(
repr
(
self
.
f
),
"io.FileIO(%d, %s)"
%
(
self
.
f
.
fileno
(),
repr
(
self
.
f
.
mode
)))
self
.
assertEquals
(
repr
(
self
.
f
),
"<_io.FileIO name=%r mode=%r>"
%
(
self
.
f
.
name
,
self
.
f
.
mode
))
del
self
.
f
.
name
self
.
assertEquals
(
repr
(
self
.
f
),
"<_io.FileIO fd=%r mode=%r>"
%
(
self
.
f
.
fileno
(),
self
.
f
.
mode
))
self
.
f
.
close
()
self
.
assertEquals
(
repr
(
self
.
f
),
"<_io.FileIO [closed]>"
)
def
testErrors
(
self
):
f
=
self
.
f
...
...
Lib/test/test_io.py
View file @
716c444e
...
...
@@ -618,6 +618,16 @@ class CommonBufferedTests:
self
.
assert_
(
s
.
startswith
(
"Exception IOError: "
),
s
)
self
.
assert_
(
s
.
endswith
(
" ignored"
),
s
)
def
test_repr
(
self
):
raw
=
self
.
MockRawIO
()
b
=
self
.
tp
(
raw
)
clsname
=
"%s.%s"
%
(
self
.
tp
.
__module__
,
self
.
tp
.
__name__
)
self
.
assertEqual
(
repr
(
b
),
"<%s>"
%
clsname
)
raw
.
name
=
"dummy"
self
.
assertEqual
(
repr
(
b
),
"<%s name='dummy'>"
%
clsname
)
raw
.
name
=
b"dummy"
self
.
assertEqual
(
repr
(
b
),
"<%s name=b'dummy'>"
%
clsname
)
class
BufferedReaderTest
(
unittest
.
TestCase
,
CommonBufferedTests
):
read_mode
=
"rb"
...
...
@@ -1528,7 +1538,15 @@ class TextIOWrapperTest(unittest.TestCase):
raw
=
self
.
BytesIO
(
"hello"
.
encode
(
"utf-8"
))
b
=
self
.
BufferedReader
(
raw
)
t
=
self
.
TextIOWrapper
(
b
,
encoding
=
"utf-8"
)
self
.
assertEqual
(
repr
(
t
),
"<TextIOWrapper encoding=utf-8>"
)
modname
=
self
.
TextIOWrapper
.
__module__
self
.
assertEqual
(
repr
(
t
),
"<%s.TextIOWrapper encoding='utf-8'>"
%
modname
)
raw
.
name
=
"dummy"
self
.
assertEqual
(
repr
(
t
),
"<%s.TextIOWrapper name='dummy' encoding='utf-8'>"
%
modname
)
raw
.
name
=
b"dummy"
self
.
assertEqual
(
repr
(
t
),
"<%s.TextIOWrapper name=b'dummy' encoding='utf-8'>"
%
modname
)
def
test_line_buffering
(
self
):
r
=
self
.
BytesIO
()
...
...
Misc/NEWS
View file @
716c444e
...
...
@@ -32,6 +32,9 @@ Core and Builtins
Library
-------
- Issue #5761: Add the name of the underlying file to the repr() of various
IO objects.
- Issue #5259: smtplib plain auth login no longer gives a traceback. Fix
by Musashi Tamura, tests by Marcin Bachry.
...
...
Modules/_io/bufferedio.c
View file @
716c444e
...
...
@@ -1123,6 +1123,27 @@ Buffered_iternext(BufferedObject *self)
return
line
;
}
static
PyObject
*
Buffered_repr
(
BufferedObject
*
self
)
{
PyObject
*
nameobj
,
*
res
;
nameobj
=
PyObject_GetAttrString
((
PyObject
*
)
self
,
"name"
);
if
(
nameobj
==
NULL
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
PyErr_Clear
();
else
return
NULL
;
res
=
PyUnicode_FromFormat
(
"<%s>"
,
Py_TYPE
(
self
)
->
tp_name
);
}
else
{
res
=
PyUnicode_FromFormat
(
"<%s name=%R>"
,
Py_TYPE
(
self
)
->
tp_name
,
nameobj
);
Py_DECREF
(
nameobj
);
}
return
res
;
}
/*
* class BufferedReader
*/
...
...
@@ -1472,7 +1493,7 @@ PyTypeObject PyBufferedReader_Type = {
0
,
/*tp_getattr*/
0
,
/*tp_setattr*/
0
,
/*tp_compare */
0
,
/*tp_repr*/
(
reprfunc
)
Buffered_repr
,
/*tp_repr*/
0
,
/*tp_as_number*/
0
,
/*tp_as_sequence*/
0
,
/*tp_as_mapping*/
...
...
@@ -1828,7 +1849,7 @@ PyTypeObject PyBufferedWriter_Type = {
0
,
/*tp_getattr*/
0
,
/*tp_setattr*/
0
,
/*tp_compare */
0
,
/*tp_repr*/
(
reprfunc
)
Buffered_repr
,
/*tp_repr*/
0
,
/*tp_as_number*/
0
,
/*tp_as_sequence*/
0
,
/*tp_as_mapping*/
...
...
@@ -2219,7 +2240,7 @@ PyTypeObject PyBufferedRandom_Type = {
0
,
/*tp_getattr*/
0
,
/*tp_setattr*/
0
,
/*tp_compare */
0
,
/*tp_repr*/
(
reprfunc
)
Buffered_repr
,
/*tp_repr*/
0
,
/*tp_as_number*/
0
,
/*tp_as_sequence*/
0
,
/*tp_as_mapping*/
...
...
Modules/_io/fileio.c
View file @
716c444e
...
...
@@ -846,11 +846,26 @@ mode_string(PyFileIOObject *self)
static
PyObject
*
fileio_repr
(
PyFileIOObject
*
self
)
{
PyObject
*
nameobj
,
*
res
;
if
(
self
->
fd
<
0
)
return
PyUnicode_FromFormat
(
"
io.FileIO(-1)
"
);
return
PyUnicode_FromFormat
(
"
<_io.FileIO [closed]>
"
);
return
PyUnicode_FromFormat
(
"io.FileIO(%d, '%s')"
,
self
->
fd
,
mode_string
(
self
));
nameobj
=
PyObject_GetAttrString
((
PyObject
*
)
self
,
"name"
);
if
(
nameobj
==
NULL
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
PyErr_Clear
();
else
return
NULL
;
res
=
PyUnicode_FromFormat
(
"<_io.FileIO fd=%d mode='%s'>"
,
self
->
fd
,
mode_string
(
self
));
}
else
{
res
=
PyUnicode_FromFormat
(
"<_io.FileIO name=%R mode='%s'>"
,
nameobj
,
mode_string
(
self
));
Py_DECREF
(
nameobj
);
}
return
res
;
}
static
PyObject
*
...
...
Modules/_io/textio.c
View file @
716c444e
...
...
@@ -2308,8 +2308,25 @@ TextIOWrapper_truncate(PyTextIOWrapperObject *self, PyObject *args)
static
PyObject
*
TextIOWrapper_repr
(
PyTextIOWrapperObject
*
self
)
{
CHECK_INITIALIZED
(
self
);
return
PyUnicode_FromFormat
(
"<TextIOWrapper encoding=%S>"
,
self
->
encoding
);
PyObject
*
nameobj
,
*
res
;
CHECK_INITIALIZED
(
self
);
nameobj
=
PyObject_GetAttrString
((
PyObject
*
)
self
,
"name"
);
if
(
nameobj
==
NULL
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
PyErr_Clear
();
else
return
NULL
;
res
=
PyUnicode_FromFormat
(
"<_io.TextIOWrapper encoding=%R>"
,
self
->
encoding
);
}
else
{
res
=
PyUnicode_FromFormat
(
"<_io.TextIOWrapper name=%R encoding=%R>"
,
nameobj
,
self
->
encoding
);
Py_DECREF
(
nameobj
);
}
return
res
;
}
...
...
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