Skip to content

Replaced open with Context Mgrs in Parser Tests #21105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pandas/tests/io/parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,21 @@ def test_bad_stream_exception(self):
# and C engine will raise UnicodeDecodeError instead of
# c engine raising ParserError and swallowing exception
# that caused read to fail.
handle = open(self.csv_shiftjs, "rb")
codec = codecs.lookup("utf-8")
utf8 = codecs.lookup('utf-8')
# stream must be binary UTF8
stream = codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter)

if compat.PY3:
msg = "'utf-8' codec can't decode byte"
else:
msg = "'utf8' codec can't decode byte"
with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)
stream.close()

# stream must be binary UTF8
with open(self.csv_shiftjs, "rb") as handle, codecs.StreamRecoder(
handle, utf8.encode, utf8.decode, codec.streamreader,
codec.streamwriter) as stream:

with tm.assert_raises_regex(UnicodeDecodeError, msg):
self.read_csv(stream)

def test_read_csv(self):
if not compat.PY3:
Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/io/parser/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ def test_read_csv_infer_compression(self):
# see gh-9770
expected = self.read_csv(self.csv1, index_col=0, parse_dates=True)

inputs = [self.csv1, self.csv1 + '.gz',
self.csv1 + '.bz2', open(self.csv1)]
with open(self.csv1) as f:
inputs = [self.csv1, self.csv1 + '.gz',
self.csv1 + '.bz2', f]

for f in inputs:
df = self.read_csv(f, index_col=0, parse_dates=True,
compression='infer')

tm.assert_frame_equal(expected, df)
for inp in inputs:
df = self.read_csv(inp, index_col=0, parse_dates=True,
compression='infer')

inputs[3].close()
tm.assert_frame_equal(expected, df)

def test_read_csv_compressed_utf16_example(self):
# GH18071
Expand Down
12 changes: 3 additions & 9 deletions pandas/tests/io/parser/test_textreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,18 @@ def setup_method(self, method):
self.xls1 = os.path.join(self.dirpath, 'test.xls')

def test_file_handle(self):
try:
f = open(self.csv1, 'rb')
with open(self.csv1, 'rb') as f:
reader = TextReader(f)
result = reader.read() # noqa
finally:
f.close()
reader.read()

def test_string_filename(self):
reader = TextReader(self.csv1, header=None)
reader.read()

def test_file_handle_mmap(self):
try:
f = open(self.csv1, 'rb')
with open(self.csv1, 'rb') as f:
reader = TextReader(f, memory_map=True, header=None)
reader.read()
finally:
f.close()

def test_StringIO(self):
with open(self.csv1, 'rb') as f:
Expand Down