Skip to content

Commit

Permalink
Remove PY2 six from test/test_console.py file (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorothykiz1 authored Mar 3, 2022
1 parent 9bbfb98 commit 28f8f55
Showing 1 changed file with 17 additions and 44 deletions.
61 changes: 17 additions & 44 deletions test/test_console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import six
import io
import os
import sys
Expand All @@ -21,53 +20,30 @@ def check_write(value, expected, stream_encoding, preferred_encoding):
locale.getpreferredencoding = lambda: preferred_encoding

# Check writing to io.StringIO
if six.PY3:
stream = io.StringIO()
_write_with_fallback(value, stream)
assert stream.getvalue() == value
else:
stream = io.StringIO()
with pytest.raises(TypeError):
_write_with_fallback(value, stream)
stream = io.StringIO()
_write_with_fallback(value, stream)
assert stream.getvalue() == value

# Check writing to a text stream
if six.PY3:
buf = io.BytesIO()
stream = io.TextIOWrapper(buf, encoding=stream_encoding)
_write_with_fallback(value, stream)
stream.flush()
got = buf.getvalue()
assert got == expected
buf = io.BytesIO()
stream = io.TextIOWrapper(buf, encoding=stream_encoding)
_write_with_fallback(value, stream)
stream.flush()
got = buf.getvalue()
assert got == expected

# Writing to io.BytesIO
if six.PY3:
stream = io.BytesIO()
with pytest.raises(TypeError):
_write_with_fallback(value, stream)
else:
stream = io.BytesIO()
stream = io.BytesIO()
with pytest.raises(TypeError):
_write_with_fallback(value, stream)
assert stream.getvalue() == expected

# Check writing to a file
fn = os.path.join(tmpdir, 'tmp.txt')
if six.PY3:
with io.open(fn, 'w', encoding=stream_encoding) as stream:
_write_with_fallback(value, stream)
with open(fn, 'rb') as stream:
got = stream.read()
assert got == expected

# Check writing to Py2 files
if not six.PY3:
if stream_encoding == preferred_encoding:
# No stream encoding: write in locale encoding
for mode in ['w', 'wb']:
with open(fn, mode) as stream:
_write_with_fallback(value, stream)
with open(fn, 'rb') as stream:
got = stream.read()
assert got == expected
with io.open(fn, 'w', encoding=stream_encoding) as stream:
_write_with_fallback(value, stream)
with open(fn, 'rb') as stream:
got = stream.read()
assert got == expected
finally:
locale.getpreferredencoding = old_getpreferredencoding

Expand All @@ -84,10 +60,7 @@ def check_write(value, expected, stream_encoding, preferred_encoding):

for pref_enc, stream_enc, s in itertools.product(encodings, encodings, strings):
expected = None
if six.PY3:
encodings = [stream_enc, pref_enc]
else:
encodings = [pref_enc]
encodings = [stream_enc, pref_enc]
for enc in encodings:
try:
expected = s.encode(enc)
Expand Down

0 comments on commit 28f8f55

Please sign in to comment.