diff options
Diffstat (limited to 'Lib/test/test_fileio.py')
-rw-r--r-- | Lib/test/test_fileio.py | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 71ec44c31e7..b5bd8d45ad8 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -1,7 +1,5 @@ # Adapted from test_file.py by Daniel Stutzbach -from __future__ import unicode_literals - import sys import os import errno @@ -9,11 +7,9 @@ import unittest from array import array from weakref import proxy from functools import wraps -from UserList import UserList -from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd -from test.test_support import py3k_bytes as bytes -from test.script_helper import run_python +from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd +from collections import UserList from _io import FileIO as _FileIO @@ -65,12 +61,12 @@ class AutoFileTests(unittest.TestCase): def testReadinto(self): # verify readinto - self.f.write(b"\x01\x02") + self.f.write(bytes([1, 2])) self.f.close() - a = array(b'b', b'x'*10) + a = array('b', b'x'*10) self.f = _FileIO(TESTFN, 'r') n = self.f.readinto(a) - self.assertEqual(array(b'b', [1, 2]), a[:n]) + self.assertEqual(array('b', [1, 2]), a[:n]) def testWritelinesList(self): l = [b'123', b'456'] @@ -91,6 +87,7 @@ class AutoFileTests(unittest.TestCase): def testWritelinesError(self): self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) self.assertRaises(TypeError, self.f.writelines, None) + self.assertRaises(TypeError, self.f.writelines, "abc") def test_none_args(self): self.f.write(b"hi\nbye\nabc") @@ -101,12 +98,15 @@ class AutoFileTests(unittest.TestCase): self.assertEqual(self.f.readline(None), b"hi\n") self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"]) + def test_reject(self): + self.assertRaises(TypeError, self.f.write, "Hello!") + def testRepr(self): - self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode='%s'>" - % (self.f.name, self.f.mode)) + self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode=%r>" + % (self.f.name, self.f.mode)) del self.f.name - self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode='%s'>" - % (self.f.fileno(), self.f.mode)) + self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode=%r>" + % (self.f.fileno(), self.f.mode)) self.f.close() self.assertEqual(repr(self.f), "<_io.FileIO [closed]>") @@ -128,8 +128,6 @@ class AutoFileTests(unittest.TestCase): methods = ['fileno', 'isatty', 'read', 'readinto', 'seek', 'tell', 'truncate', 'write', 'seekable', 'readable', 'writable'] - if sys.platform.startswith('atheos'): - methods.remove('truncate') self.f.close() self.assertTrue(self.f.closed) @@ -201,7 +199,7 @@ class AutoFileTests(unittest.TestCase): @ClosedFDRaises def testErrnoOnClosedWrite(self, f): - f.write('a') + f.write(b'a') @ClosedFDRaises def testErrnoOnClosedSeek(self, f): @@ -257,7 +255,7 @@ class AutoFileTests(unittest.TestCase): @ClosedFDRaises def testErrnoOnClosedReadinto(self, f): f = self.ReopenForRead() - a = array(b'b', b'x'*10) + a = array('b', b'x'*10) f.readinto(a) class OtherFileTests(unittest.TestCase): @@ -337,6 +335,11 @@ class OtherFileTests(unittest.TestCase): finally: os.unlink(TESTFN) + def testConstructorHandlesNULChars(self): + fn_with_NUL = 'foo\0bar' + self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w') + self.assertRaises(TypeError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w') + def testInvalidFd(self): self.assertRaises(ValueError, _FileIO, -10) self.assertRaises(OSError, _FileIO, make_bad_fd()) @@ -430,26 +433,6 @@ class OtherFileTests(unittest.TestCase): self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt") self.assertEqual(w.warnings, []) - def test_surrogates(self): - # Issue #8438: try to open a filename containing surrogates. - # It should either fail because the file doesn't exist or the filename - # can't be represented using the filesystem encoding, but not because - # of a LookupError for the error handler "surrogateescape". - filename = u'\udc80.txt' - try: - with _FileIO(filename): - pass - except (UnicodeEncodeError, IOError): - pass - # Spawn a separate Python process with a different "file system - # default encoding", to exercise this further. - env = dict(os.environ) - env[b'LC_CTYPE'] = b'C' - _, out = run_python('-c', 'import _io; _io.FileIO(%r)' % filename, env=env) - if ('UnicodeEncodeError' not in out and - 'IOError: [Errno 2] No such file or directory' not in out): - self.fail('Bad output: %r' % out) - def testUnclosedFDOnException(self): class MyException(Exception): pass class MyFileIO(_FileIO): @@ -461,6 +444,7 @@ class OtherFileTests(unittest.TestCase): self.assertRaises(MyException, MyFileIO, fd) os.close(fd) # should not raise OSError(EBADF) + def test_main(): # Historically, these tests have been sloppy about removing TESTFN. # So get rid of it no matter what. |