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
86b34da5
Commit
86b34da5
authored
Aug 06, 2015
by
Berker Peksag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #23004: mock_open() now reads binary data correctly when the type of read_data is bytes.
Initial patch by Aaron Hill.
parent
0b2833ea
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
3 deletions
+35
-3
Lib/unittest/mock.py
Lib/unittest/mock.py
+4
-3
Lib/unittest/test/testmock/testwith.py
Lib/unittest/test/testmock/testwith.py
+28
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/unittest/mock.py
View file @
86b34da5
...
...
@@ -2250,9 +2250,10 @@ def _iterate_read_data(read_data):
# Helper for mock_open:
# Retrieve lines from read_data via a generator so that separate calls to
# readline, read, and readlines are properly interleaved
data_as_list
=
[
'{}
\
n
'
.
format
(
l
)
for
l
in
read_data
.
split
(
'
\
n
'
)]
sep
=
b'
\
n
'
if
isinstance
(
read_data
,
bytes
)
else
'
\
n
'
data_as_list
=
[
l
+
sep
for
l
in
read_data
.
split
(
sep
)]
if
data_as_list
[
-
1
]
==
'
\
n
'
:
if
data_as_list
[
-
1
]
==
sep
:
# If the last line ended in a newline, the list comprehension will have an
# extra entry that's just a newline. Remove this.
data_as_list
=
data_as_list
[:
-
1
]
...
...
@@ -2286,7 +2287,7 @@ def mock_open(mock=None, read_data=''):
def
_read_side_effect
(
*
args
,
**
kwargs
):
if
handle
.
read
.
return_value
is
not
None
:
return
handle
.
read
.
return_value
return
''
.
join
(
_state
[
0
])
return
type
(
read_data
)()
.
join
(
_state
[
0
])
def
_readline_side_effect
():
if
handle
.
readline
.
return_value
is
not
None
:
...
...
Lib/unittest/test/testmock/testwith.py
View file @
86b34da5
...
...
@@ -224,6 +224,34 @@ class TestMockOpen(unittest.TestCase):
self
.
assertEqual
(
result
,
[
'foo
\
n
'
,
'bar
\
n
'
,
'baz'
])
def
test_read_bytes
(
self
):
mock
=
mock_open
(
read_data
=
b'
\
xc6
'
)
with
patch
(
'%s.open'
%
__name__
,
mock
,
create
=
True
):
with
open
(
'abc'
,
'rb'
)
as
f
:
result
=
f
.
read
()
self
.
assertEqual
(
result
,
b'
\
xc6
'
)
def
test_readline_bytes
(
self
):
m
=
mock_open
(
read_data
=
b'abc
\
n
def
\
n
ghi
\
n
'
)
with
patch
(
'%s.open'
%
__name__
,
m
,
create
=
True
):
with
open
(
'abc'
,
'rb'
)
as
f
:
line1
=
f
.
readline
()
line2
=
f
.
readline
()
line3
=
f
.
readline
()
self
.
assertEqual
(
line1
,
b'abc
\
n
'
)
self
.
assertEqual
(
line2
,
b'def
\
n
'
)
self
.
assertEqual
(
line3
,
b'ghi
\
n
'
)
def
test_readlines_bytes
(
self
):
m
=
mock_open
(
read_data
=
b'abc
\
n
def
\
n
ghi
\
n
'
)
with
patch
(
'%s.open'
%
__name__
,
m
,
create
=
True
):
with
open
(
'abc'
,
'rb'
)
as
f
:
result
=
f
.
readlines
()
self
.
assertEqual
(
result
,
[
b'abc
\
n
'
,
b'def
\
n
'
,
b'ghi
\
n
'
])
def
test_mock_open_read_with_argument
(
self
):
# At one point calling read with an argument was broken
# for mocks returned by mock_open
...
...
Misc/NEWS
View file @
86b34da5
...
...
@@ -71,6 +71,9 @@ Library
-
Issue
#
23888
:
Handle
fractional
time
in
cookie
expiry
.
Patch
by
ssh
.
-
Issue
#
23004
:
mock_open
()
now
reads
binary
data
correctly
when
the
type
of
read_data
is
bytes
.
Initial
patch
by
Aaron
Hill
.
-
Issue
#
23652
:
Make
it
possible
to
compile
the
select
module
against
the
libc
headers
from
the
Linux
Standard
Base
,
which
do
not
include
some
EPOLL
macros
.
Patch
by
Matt
Frank
.
...
...
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