Skip to content

Commit f7f0394

Browse files
committed
Be explicit about encoding file contents as UTF-8 in unit tests (issue #63)
1 parent 998bd8c commit f7f0394

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

future/tests/base.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import re
77
import warnings
8+
import io
89
if not hasattr(unittest, 'skip'):
910
import unittest2 as unittest
1011
from textwrap import dedent
@@ -172,10 +173,17 @@ def compare(self, output, expected, ignore_imports=True):
172173
173174
If ignore_imports is True, passes the code blocks into the
174175
strip_future_imports method.
176+
177+
If one code block is a unicode string and the other a
178+
byte-string, it assumes the byte-string is encoded as utf-8.
175179
"""
176180
if ignore_imports:
177181
output = self.strip_future_imports(output)
178182
expected = self.strip_future_imports(expected)
183+
if isinstance(output, bytes) and not isinstance(expected, bytes):
184+
output = output.decode('utf-8')
185+
if isinstance(expected, bytes) and not isinstance(output, bytes):
186+
expected = expected.decode('utf-8')
179187
self.assertEqual(order_future_lines(output.rstrip()),
180188
expected.rstrip())
181189

@@ -236,7 +244,7 @@ def convert_check(self, before, expected, stages=(1, 2), all_imports=False,
236244
headers = ''
237245

238246
self.compare(output, reformat_code(headers + expected),
239-
ignore_imports=ignore_imports)
247+
ignore_imports=ignore_imports)
240248

241249
def unchanged(self, code, **kwargs):
242250
"""
@@ -250,11 +258,14 @@ def _write_test_script(self, code, filename='mytestscript.py'):
250258
Dedents the given code (a multiline string) and writes it out to
251259
a file in a temporary folder like /tmp/tmpUDCn7x/mytestscript.py.
252260
"""
253-
with open(self.tempdir + filename, 'w') as f:
261+
if isinstance(code, bytes):
262+
code = code.decode('utf-8')
263+
# Be explicit about encoding the temp file as UTF-8 (issue #63):
264+
with io.open(self.tempdir + filename, 'wt', encoding='utf-8') as f:
254265
f.write(dedent(code))
255266

256267
def _read_test_script(self, filename='mytestscript.py'):
257-
with open(self.tempdir + filename) as f:
268+
with io.open(self.tempdir + filename, 'rt', encoding='utf-8') as f:
258269
newsource = f.read()
259270
return newsource
260271

future/tests/test_requests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import textwrap
1010
import sys
1111
import os
12+
import io
1213

1314

1415
# Don't import requests first. This avoids the problem we want to expose:
@@ -31,7 +32,8 @@ def __init__(self, code, tempdir):
3132

3233
def __enter__(self):
3334
print('Creating {0}test_imports_future_stdlib.py ...'.format(self.tempdir))
34-
with open(self.tempdir + 'test_imports_future_stdlib.py', 'w') as f:
35+
with io.open(self.tempdir + 'test_imports_future_stdlib.py', 'wt',
36+
encoding='utf-8') as f:
3537
f.write(textwrap.dedent(self.code))
3638
sys.path.insert(0, self.tempdir)
3739
return self

past/tests/test_translation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pprint
1111
import tempfile
1212
import os
13+
import io
1314
from subprocess import Popen, PIPE
1415

1516
from past import utils
@@ -40,7 +41,10 @@ def test_common_substring(self):
4041
def write_and_import(self, code, modulename='mymodule'):
4142
self.assertTrue('.py' not in modulename)
4243
filename = modulename + '.py'
43-
with open(self.tempdir + filename, 'w') as f:
44+
if isinstance(code, bytes):
45+
code = code.decode('utf-8')
46+
# Be explicit about encoding the temp file as UTF-8 (issue #63):
47+
with io.open(self.tempdir + filename, 'w', encoding='utf-8') as f:
4448
f.write(textwrap.dedent(code).strip() + '\n')
4549

4650
# meta_path_len = len(sys.meta_path)

0 commit comments

Comments
 (0)