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
68b8a94c
Commit
68b8a94c
authored
Feb 26, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #20501: fileinput module no longer reads whole file into memory when using
fileinput.hook_encoded.
parent
17a4322d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
3 deletions
+48
-3
Lib/fileinput.py
Lib/fileinput.py
+3
-2
Lib/test/test_fileinput.py
Lib/test/test_fileinput.py
+42
-1
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/fileinput.py
View file @
68b8a94c
...
...
@@ -387,9 +387,10 @@ def hook_compressed(filename, mode):
def
hook_encoded
(
encoding
):
import
codecs
import
io
def
openhook
(
filename
,
mode
):
return
codecs
.
open
(
filename
,
mode
,
encoding
)
mode
=
mode
.
replace
(
'U'
,
''
).
replace
(
'b'
,
''
)
or
'r'
return
io
.
open
(
filename
,
mode
,
encoding
=
encoding
,
newline
=
''
)
return
openhook
...
...
Lib/test/test_fileinput.py
View file @
68b8a94c
...
...
@@ -218,8 +218,49 @@ class FileInputTests(unittest.TestCase):
finally
:
remove_tempfiles
(
t1
)
def
test_readline
(
self
):
with
open
(
TESTFN
,
'wb'
)
as
f
:
f
.
write
(
'A
\
n
B
\
r
\
n
C
\
r
'
)
# Fill TextIOWrapper buffer.
f
.
write
(
'123456789
\
n
'
*
1000
)
# Issue #20501: readline() shouldn't read whole file.
f
.
write
(
'
\
x80
'
)
self
.
addCleanup
(
safe_unlink
,
TESTFN
)
fi
=
FileInput
(
files
=
TESTFN
,
openhook
=
hook_encoded
(
'ascii'
),
bufsize
=
8
)
self
.
assertEqual
(
fi
.
readline
(),
u'A
\
n
'
)
self
.
assertEqual
(
fi
.
readline
(),
u'B
\
r
\
n
'
)
self
.
assertEqual
(
fi
.
readline
(),
u'C
\
r
'
)
with
self
.
assertRaises
(
UnicodeDecodeError
):
# Read to the end of file.
list
(
fi
)
fi
.
close
()
class
Test_hook_encoded
(
unittest
.
TestCase
):
"""Unit tests for fileinput.hook_encoded()"""
def
test_modes
(
self
):
# Unlikely UTF-7 is locale encoding
with
open
(
TESTFN
,
'wb'
)
as
f
:
f
.
write
(
'A
\
n
B
\
r
\
n
C
\
r
D+IKw-'
)
t1
=
TESTFN
#t1 = writeTmp(1, ['A\nB\r\nC\rD+IKw-'], mode='wb')
self
.
addCleanup
(
safe_unlink
,
TESTFN
)
def
check
(
mode
,
expected_lines
):
fi
=
FileInput
(
files
=
TESTFN
,
mode
=
mode
,
openhook
=
hook_encoded
(
'utf-7'
))
lines
=
list
(
fi
)
fi
.
close
()
self
.
assertEqual
(
lines
,
expected_lines
)
check
(
'r'
,
[
u'A
\
n
'
,
u'B
\
r
\
n
'
,
u'C
\
r
'
,
u'D
\
u20ac
'
])
check
(
'rU'
,
[
u'A
\
n
'
,
u'B
\
r
\
n
'
,
u'C
\
r
'
,
u'D
\
u20ac
'
])
check
(
'U'
,
[
u'A
\
n
'
,
u'B
\
r
\
n
'
,
u'C
\
r
'
,
u'D
\
u20ac
'
])
check
(
'rb'
,
[
u'A
\
n
'
,
u'B
\
r
\
n
'
,
u'C
\
r
'
,
u'D
\
u20ac
'
])
def
test_main
():
run_unittest
(
BufferSizesTests
,
FileInputTests
)
run_unittest
(
BufferSizesTests
,
FileInputTests
,
Test_hook_encoded
)
if
__name__
==
"__main__"
:
test_main
()
Misc/NEWS
View file @
68b8a94c
...
...
@@ -40,6 +40,9 @@ Core and Builtins
Library
-------
-
Issue
#
20501
:
fileinput
module
no
longer
reads
whole
file
into
memory
when
using
fileinput
.
hook_encoded
.
-
Issue
#
6815
:
os
.
path
.
expandvars
()
now
supports
non
-
ASCII
Unicode
environment
variables
names
and
values
.
...
...
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