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
f4b6186d
Commit
f4b6186d
authored
Feb 26, 2010
by
Florent Xicluna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#691291: codecs.open() should not convert end of lines on reading and writing.
parent
c994186d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
4 deletions
+24
-4
Lib/codecs.py
Lib/codecs.py
+9
-4
Lib/test/test_codecs.py
Lib/test/test_codecs.py
+15
-0
No files found.
Lib/codecs.py
View file @
f4b6186d
...
...
@@ -858,10 +858,15 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
parameter.
"""
if
encoding
is
not
None
and
\
'b'
not
in
mode
:
# Force opening of the file in binary mode
mode
=
mode
+
'b'
if
encoding
is
not
None
:
if
'U'
in
mode
:
# No automatic conversion of '\n' is done on reading and writing
mode
=
mode
.
strip
().
replace
(
'U'
,
''
)
if
mode
[:
1
]
not
in
set
(
'rwa'
):
mode
=
'r'
+
mode
if
'b'
not
in
mode
:
# Force opening of the file in binary mode
mode
=
mode
+
'b'
file
=
__builtin__
.
open
(
filename
,
mode
,
buffering
)
if
encoding
is
None
:
return
file
...
...
Lib/test/test_codecs.py
View file @
f4b6186d
...
...
@@ -437,6 +437,21 @@ class UTF16Test(ReadTest):
def
test_errors
(
self
):
self
.
assertRaises
(
UnicodeDecodeError
,
codecs
.
utf_16_decode
,
"
\
xff
"
,
"strict"
,
True
)
def
test_bug691291
(
self
):
# Files are always opened in binary mode, even if no binary mode was
# specified. This means that no automatic conversion of '\n' is done
# on reading and writing.
s1
=
u'Hello
\
r
\
n
world
\
r
\
n
'
s
=
s1
.
encode
(
self
.
encoding
)
try
:
with
open
(
test_support
.
TESTFN
,
'wb'
)
as
fp
:
fp
.
write
(
s
)
with
codecs
.
open
(
test_support
.
TESTFN
,
'U'
,
encoding
=
self
.
encoding
)
as
reader
:
self
.
assertEqual
(
reader
.
read
(),
s1
)
finally
:
test_support
.
unlink
(
test_support
.
TESTFN
)
class
UTF16LETest
(
ReadTest
):
encoding
=
"utf-16-le"
...
...
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