Skip to content
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

Remove PY2 six from test/test_console.py file #1079

Merged
merged 3 commits into from
Mar 3, 2022
Merged
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
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:
datapythonista marked this conversation as resolved.
Show resolved Hide resolved
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