bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25145)

* test_asyncio
* test_bz2
* test_math
* test_cmath
* test_cmd_line
* test_cmd_line_script
* test_compile
* test_contextlib
* test_profile
* ctypes/test/test_find
* test_multiprocessing
* test_configparser
* test_csv
* test_dbm_dumb
* test_decimal
* test_difflib
* os.fdopen() calls io.text_encoding() to emit EncodingWarning for right place.
This commit is contained in:
Inada Naoki 2021-04-04 09:01:23 +09:00 committed by GitHub
parent dc6d3e1e4c
commit 35715d1e72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 78 additions and 75 deletions

View file

@ -133,7 +133,7 @@ class Test_Csv(unittest.TestCase):
def _write_test(self, fields, expect, **kwargs):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, **kwargs)
writer.writerow(fields)
fileobj.seek(0)
@ -141,7 +141,7 @@ class Test_Csv(unittest.TestCase):
expect + writer.dialect.lineterminator)
def _write_error_test(self, exc, fields, **kwargs):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, **kwargs)
with self.assertRaises(exc):
writer.writerow(fields)
@ -232,7 +232,7 @@ class Test_Csv(unittest.TestCase):
writer = csv.writer(BrokenFile())
self.assertRaises(OSError, writer.writerows, [['a']])
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
self.assertRaises(TypeError, writer.writerows, None)
writer.writerows([['a', 'b'], ['c', 'd']])
@ -240,26 +240,26 @@ class Test_Csv(unittest.TestCase):
self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n")
def test_writerows_with_none(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
writer.writerows([['a', None], [None, 'd']])
fileobj.seek(0)
self.assertEqual(fileobj.read(), "a,\r\n,d\r\n")
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
writer.writerows([[None], ['a']])
fileobj.seek(0)
self.assertEqual(fileobj.read(), '""\r\na\r\n')
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
writer.writerows([['a'], [None]])
fileobj.seek(0)
self.assertEqual(fileobj.read(), 'a\r\n""\r\n')
def test_writerows_errors(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
self.assertRaises(TypeError, writer.writerows, None)
self.assertRaises(OSError, writer.writerows, BadIterable())
@ -270,7 +270,7 @@ class Test_Csv(unittest.TestCase):
def test_writerows_legacy_strings(self):
import _testcapi
c = _testcapi.unicode_legacy_string('a')
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
writer.writerows([[c]])
fileobj.seek(0)
@ -367,7 +367,7 @@ class Test_Csv(unittest.TestCase):
self.assertEqual(r.line_num, 3)
def test_roundtrip_quoteed_newlines(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj)
rows = [['a\nb','b'],['c','x\r\nd']]
writer.writerows(rows)
@ -376,7 +376,7 @@ class Test_Csv(unittest.TestCase):
self.assertEqual(row, rows[i])
def test_roundtrip_escaped_unquoted_newlines(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj,quoting=csv.QUOTE_NONE,escapechar="\\")
rows = [['a\nb','b'],['c','x\r\nd']]
writer.writerows(rows)
@ -432,7 +432,7 @@ class TestDialectRegistry(unittest.TestCase):
quoting = csv.QUOTE_NONE
escapechar = "\\"
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("abc def\nc1ccccc1 benzene\n")
fileobj.seek(0)
reader = csv.reader(fileobj, dialect=space())
@ -493,7 +493,7 @@ class TestDialectRegistry(unittest.TestCase):
class TestCsvBase(unittest.TestCase):
def readerAssertEqual(self, input, expected_result):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
fileobj.write(input)
fileobj.seek(0)
reader = csv.reader(fileobj, dialect = self.dialect)
@ -501,7 +501,7 @@ class TestCsvBase(unittest.TestCase):
self.assertEqual(fields, expected_result)
def writerAssertEqual(self, input, expected_result):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, dialect = self.dialect)
writer.writerows(input)
fileobj.seek(0)
@ -643,13 +643,13 @@ class TestDictFields(unittest.TestCase):
### "long" means the row is longer than the number of fieldnames
### "short" means there are fewer elements in the row than fieldnames
def test_writeheader_return_value(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
writeheader_return_value = writer.writeheader()
self.assertEqual(writeheader_return_value, 10)
def test_write_simple_dict(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
writer.writeheader()
fileobj.seek(0)
@ -674,7 +674,7 @@ class TestDictFields(unittest.TestCase):
self.assertRaises(TypeError, csv.DictWriter, fileobj)
def test_write_fields_not_in_fieldnames(self):
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
# Of special note is the non-string key (issue 19449)
with self.assertRaises(ValueError) as cx:
@ -704,7 +704,7 @@ class TestDictFields(unittest.TestCase):
self.assertEqual(fileobj.getvalue(), "1,2\r\n")
def test_read_dict_fields(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("1,2,abc\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj,
@ -712,7 +712,7 @@ class TestDictFields(unittest.TestCase):
self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'})
def test_read_dict_no_fieldnames(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj)
@ -722,7 +722,7 @@ class TestDictFields(unittest.TestCase):
# Two test cases to make sure existing ways of implicitly setting
# fieldnames continue to work. Both arise from discussion in issue3436.
def test_read_dict_fieldnames_from_file(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj,
@ -732,7 +732,7 @@ class TestDictFields(unittest.TestCase):
def test_read_dict_fieldnames_chain(self):
import itertools
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("f1,f2,f3\r\n1,2,abc\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj)
@ -742,7 +742,7 @@ class TestDictFields(unittest.TestCase):
self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'})
def test_read_long(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("1,2,abc,4,5,6\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj,
@ -751,7 +751,7 @@ class TestDictFields(unittest.TestCase):
None: ["abc", "4", "5", "6"]})
def test_read_long_with_rest(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("1,2,abc,4,5,6\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj,
@ -760,7 +760,7 @@ class TestDictFields(unittest.TestCase):
"_rest": ["abc", "4", "5", "6"]})
def test_read_long_with_rest_no_fieldnames(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("f1,f2\r\n1,2,abc,4,5,6\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj, restkey="_rest")
@ -769,7 +769,7 @@ class TestDictFields(unittest.TestCase):
"_rest": ["abc", "4", "5", "6"]})
def test_read_short(self):
with TemporaryFile("w+") as fileobj:
with TemporaryFile("w+", encoding="utf-8") as fileobj:
fileobj.write("1,2,abc,4,5,6\r\n1,2,abc\r\n")
fileobj.seek(0)
reader = csv.DictReader(fileobj,
@ -818,7 +818,7 @@ class TestArrayWrites(unittest.TestCase):
contents = [(20-i) for i in range(20)]
a = array.array('i', contents)
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, dialect="excel")
writer.writerow(a)
expected = ",".join([str(i) for i in a])+"\r\n"
@ -829,7 +829,7 @@ class TestArrayWrites(unittest.TestCase):
import array
contents = [(20-i)*0.1 for i in range(20)]
a = array.array('d', contents)
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, dialect="excel")
writer.writerow(a)
expected = ",".join([str(i) for i in a])+"\r\n"
@ -840,7 +840,7 @@ class TestArrayWrites(unittest.TestCase):
import array
contents = [(20-i)*0.1 for i in range(20)]
a = array.array('f', contents)
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, dialect="excel")
writer.writerow(a)
expected = ",".join([str(i) for i in a])+"\r\n"
@ -851,7 +851,7 @@ class TestArrayWrites(unittest.TestCase):
import array, string
a = array.array('u', string.ascii_letters)
with TemporaryFile("w+", newline='') as fileobj:
with TemporaryFile("w+", encoding="utf-8", newline='') as fileobj:
writer = csv.writer(fileobj, dialect="excel")
writer.writerow(a)
expected = ",".join(a)+"\r\n"