2008-07-18 11:14:41 -03:00
|
|
|
# Adapted from test_file.py by Daniel Stutzbach
|
2009-06-12 17:14:08 -03:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2009-06-12 17:14:08 -03:00
|
|
|
import errno
|
2008-07-18 11:14:41 -03:00
|
|
|
import unittest
|
|
|
|
from array import array
|
|
|
|
from weakref import proxy
|
2009-06-12 17:14:08 -03:00
|
|
|
from functools import wraps
|
2012-10-16 17:57:11 -03:00
|
|
|
from UserList import UserList
|
2008-07-18 11:14:41 -03:00
|
|
|
|
2010-02-07 13:03:15 -04:00
|
|
|
from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd
|
2009-06-12 17:14:08 -03:00
|
|
|
from test.test_support import py3k_bytes as bytes
|
2010-04-19 15:52:43 -03:00
|
|
|
from test.script_helper import run_python
|
2008-07-18 11:14:41 -03:00
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
from _io import FileIO as _FileIO
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
class AutoFileTests(unittest.TestCase):
|
|
|
|
# file tests for which a test file is automatically set up
|
|
|
|
|
|
|
|
def setUp(self):
|
2009-06-12 17:14:08 -03:00
|
|
|
self.f = _FileIO(TESTFN, 'w')
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.f:
|
|
|
|
self.f.close()
|
|
|
|
os.remove(TESTFN)
|
|
|
|
|
|
|
|
def testWeakRefs(self):
|
|
|
|
# verify weak references
|
|
|
|
p = proxy(self.f)
|
|
|
|
p.write(bytes(range(10)))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), p.tell())
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.close()
|
|
|
|
self.f = None
|
|
|
|
self.assertRaises(ReferenceError, getattr, p, 'tell')
|
|
|
|
|
|
|
|
def testSeekTell(self):
|
2009-06-12 17:14:08 -03:00
|
|
|
self.f.write(bytes(range(20)))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 20)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.seek(0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 0)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.seek(10)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 10)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.seek(5, 1)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 15)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.seek(-5, 1)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 10)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.seek(-5, 2)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self.f.tell(), 15)
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
def testAttributes(self):
|
|
|
|
# verify expected attributes exist
|
|
|
|
f = self.f
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.mode, "wb")
|
|
|
|
self.assertEqual(f.closed, False)
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
# verify the attributes are readonly
|
|
|
|
for attr in 'mode', 'closed':
|
|
|
|
self.assertRaises((AttributeError, TypeError),
|
|
|
|
setattr, f, attr, 'oops')
|
|
|
|
|
|
|
|
def testReadinto(self):
|
|
|
|
# verify readinto
|
2009-06-12 17:14:08 -03:00
|
|
|
self.f.write(b"\x01\x02")
|
2008-07-18 11:14:41 -03:00
|
|
|
self.f.close()
|
2009-06-12 17:14:08 -03:00
|
|
|
a = array(b'b', b'x'*10)
|
|
|
|
self.f = _FileIO(TESTFN, 'r')
|
2008-07-18 11:14:41 -03:00
|
|
|
n = self.f.readinto(a)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(array(b'b', [1, 2]), a[:n])
|
2008-07-18 11:14:41 -03:00
|
|
|
|
2012-10-16 17:57:11 -03:00
|
|
|
def testWritelinesList(self):
|
|
|
|
l = [b'123', b'456']
|
|
|
|
self.f.writelines(l)
|
|
|
|
self.f.close()
|
|
|
|
self.f = _FileIO(TESTFN, 'rb')
|
|
|
|
buf = self.f.read()
|
|
|
|
self.assertEqual(buf, b'123456')
|
|
|
|
|
|
|
|
def testWritelinesUserList(self):
|
|
|
|
l = UserList([b'123', b'456'])
|
|
|
|
self.f.writelines(l)
|
|
|
|
self.f.close()
|
|
|
|
self.f = _FileIO(TESTFN, 'rb')
|
|
|
|
buf = self.f.read()
|
|
|
|
self.assertEqual(buf, b'123456')
|
|
|
|
|
|
|
|
def testWritelinesError(self):
|
|
|
|
self.assertRaises(TypeError, self.f.writelines, [1, 2, 3])
|
|
|
|
self.assertRaises(TypeError, self.f.writelines, None)
|
|
|
|
|
2009-12-13 15:19:07 -04:00
|
|
|
def test_none_args(self):
|
|
|
|
self.f.write(b"hi\nbye\nabc")
|
|
|
|
self.f.close()
|
|
|
|
self.f = _FileIO(TESTFN, 'r')
|
|
|
|
self.assertEqual(self.f.read(None), b"hi\nbye\nabc")
|
|
|
|
self.f.seek(0)
|
|
|
|
self.assertEqual(self.f.readline(None), b"hi\n")
|
|
|
|
self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"])
|
|
|
|
|
2008-07-18 11:14:41 -03:00
|
|
|
def testRepr(self):
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode='%s'>"
|
|
|
|
% (self.f.name, self.f.mode))
|
2009-06-12 17:14:08 -03:00
|
|
|
del self.f.name
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode='%s'>"
|
|
|
|
% (self.f.fileno(), self.f.mode))
|
2009-06-12 17:14:08 -03:00
|
|
|
self.f.close()
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(repr(self.f), "<_io.FileIO [closed]>")
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
def testErrors(self):
|
|
|
|
f = self.f
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(not f.isatty())
|
|
|
|
self.assertTrue(not f.closed)
|
2010-11-21 09:34:58 -04:00
|
|
|
#self.assertEqual(f.name, TESTFN)
|
2008-07-18 11:14:41 -03:00
|
|
|
self.assertRaises(ValueError, f.read, 10) # Open for reading
|
|
|
|
f.close()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(f.closed)
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, 'r')
|
2008-07-18 11:14:41 -03:00
|
|
|
self.assertRaises(TypeError, f.readinto, "")
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(not f.closed)
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(f.closed)
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
def testMethods(self):
|
|
|
|
methods = ['fileno', 'isatty', 'read', 'readinto',
|
|
|
|
'seek', 'tell', 'truncate', 'write', 'seekable',
|
|
|
|
'readable', 'writable']
|
|
|
|
if sys.platform.startswith('atheos'):
|
|
|
|
methods.remove('truncate')
|
|
|
|
|
|
|
|
self.f.close()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(self.f.closed)
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
for methodname in methods:
|
|
|
|
method = getattr(self.f, methodname)
|
|
|
|
# should raise on closed file
|
|
|
|
self.assertRaises(ValueError, method)
|
|
|
|
|
2008-09-01 11:13:43 -03:00
|
|
|
def testOpendir(self):
|
|
|
|
# Issue 3703: opening a directory should fill the errno
|
|
|
|
# Windows always returns "[Errno 13]: Permission denied
|
|
|
|
# Unix calls dircheck() and returns "[Errno 21]: Is a directory"
|
|
|
|
try:
|
2009-06-12 17:14:08 -03:00
|
|
|
_FileIO('.', 'r')
|
2008-09-01 11:13:43 -03:00
|
|
|
except IOError as e:
|
|
|
|
self.assertNotEqual(e.errno, 0)
|
2008-12-29 13:56:58 -04:00
|
|
|
self.assertEqual(e.filename, ".")
|
2008-09-01 11:13:43 -03:00
|
|
|
else:
|
|
|
|
self.fail("Should have raised IOError")
|
|
|
|
|
2012-07-06 13:48:24 -03:00
|
|
|
@unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system")
|
|
|
|
def testOpenDirFD(self):
|
|
|
|
fd = os.open('.', os.O_RDONLY)
|
|
|
|
with self.assertRaises(IOError) as cm:
|
|
|
|
_FileIO(fd, 'r')
|
|
|
|
os.close(fd)
|
|
|
|
self.assertEqual(cm.exception.errno, errno.EISDIR)
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
#A set of functions testing that we get expected behaviour if someone has
|
|
|
|
#manually closed the internal file descriptor. First, a decorator:
|
|
|
|
def ClosedFD(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(self):
|
|
|
|
#forcibly close the fd before invoking the problem function
|
|
|
|
f = self.f
|
|
|
|
os.close(f.fileno())
|
|
|
|
try:
|
|
|
|
func(self, f)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self.f.close()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def ClosedFDRaises(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(self):
|
|
|
|
#forcibly close the fd before invoking the problem function
|
|
|
|
f = self.f
|
|
|
|
os.close(f.fileno())
|
|
|
|
try:
|
|
|
|
func(self, f)
|
|
|
|
except IOError as e:
|
|
|
|
self.assertEqual(e.errno, errno.EBADF)
|
|
|
|
else:
|
|
|
|
self.fail("Should have raised IOError")
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self.f.close()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClose(self, f):
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedWrite(self, f):
|
|
|
|
f.write('a')
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedSeek(self, f):
|
|
|
|
f.seek(0)
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedTell(self, f):
|
|
|
|
f.tell()
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedTruncate(self, f):
|
|
|
|
f.truncate(0)
|
|
|
|
|
|
|
|
@ClosedFD
|
|
|
|
def testErrnoOnClosedSeekable(self, f):
|
|
|
|
f.seekable()
|
|
|
|
|
|
|
|
@ClosedFD
|
|
|
|
def testErrnoOnClosedReadable(self, f):
|
|
|
|
f.readable()
|
|
|
|
|
|
|
|
@ClosedFD
|
|
|
|
def testErrnoOnClosedWritable(self, f):
|
|
|
|
f.writable()
|
|
|
|
|
|
|
|
@ClosedFD
|
|
|
|
def testErrnoOnClosedFileno(self, f):
|
|
|
|
f.fileno()
|
|
|
|
|
|
|
|
@ClosedFD
|
|
|
|
def testErrnoOnClosedIsatty(self, f):
|
|
|
|
self.assertEqual(f.isatty(), False)
|
|
|
|
|
|
|
|
def ReopenForRead(self):
|
|
|
|
try:
|
|
|
|
self.f.close()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
self.f = _FileIO(TESTFN, 'r')
|
|
|
|
os.close(self.f.fileno())
|
|
|
|
return self.f
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedRead(self, f):
|
|
|
|
f = self.ReopenForRead()
|
|
|
|
f.read(1)
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedReadall(self, f):
|
|
|
|
f = self.ReopenForRead()
|
|
|
|
f.readall()
|
|
|
|
|
|
|
|
@ClosedFDRaises
|
|
|
|
def testErrnoOnClosedReadinto(self, f):
|
|
|
|
f = self.ReopenForRead()
|
|
|
|
a = array(b'b', b'x'*10)
|
|
|
|
f.readinto(a)
|
2008-07-18 11:14:41 -03:00
|
|
|
|
|
|
|
class OtherFileTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def testAbles(self):
|
|
|
|
try:
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, "w")
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.readable(), False)
|
|
|
|
self.assertEqual(f.writable(), True)
|
|
|
|
self.assertEqual(f.seekable(), True)
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, "r")
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.readable(), True)
|
|
|
|
self.assertEqual(f.writable(), False)
|
|
|
|
self.assertEqual(f.seekable(), True)
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, "a+")
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.readable(), True)
|
|
|
|
self.assertEqual(f.writable(), True)
|
|
|
|
self.assertEqual(f.seekable(), True)
|
|
|
|
self.assertEqual(f.isatty(), False)
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
if sys.platform != "win32":
|
|
|
|
try:
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO("/dev/tty", "a")
|
2008-07-18 11:14:41 -03:00
|
|
|
except EnvironmentError:
|
|
|
|
# When run in a cron job there just aren't any
|
|
|
|
# ttys, so skip the test. This also handles other
|
|
|
|
# OS'es that don't support /dev/tty.
|
|
|
|
pass
|
|
|
|
else:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.readable(), False)
|
|
|
|
self.assertEqual(f.writable(), True)
|
2008-07-18 11:14:41 -03:00
|
|
|
if sys.platform != "darwin" and \
|
2009-05-23 13:32:32 -03:00
|
|
|
'bsd' not in sys.platform and \
|
2008-07-18 11:14:41 -03:00
|
|
|
not sys.platform.startswith('sunos'):
|
|
|
|
# Somehow /dev/tty appears seekable on some BSDs
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.seekable(), False)
|
|
|
|
self.assertEqual(f.isatty(), True)
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
|
|
|
def testModeStrings(self):
|
|
|
|
# check invalid mode strings
|
2008-11-21 21:59:15 -04:00
|
|
|
for mode in ("", "aU", "wU+", "rw", "rt"):
|
2008-07-18 11:14:41 -03:00
|
|
|
try:
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, mode)
|
2008-07-18 11:14:41 -03:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
self.fail('%r is an invalid file mode' % mode)
|
|
|
|
|
|
|
|
def testUnicodeOpen(self):
|
|
|
|
# verify repr works for unicode too
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(str(TESTFN), "w")
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
def testBytesOpen(self):
|
|
|
|
# Opening a bytes filename
|
|
|
|
try:
|
|
|
|
fn = TESTFN.encode("ascii")
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
# Skip test
|
|
|
|
return
|
|
|
|
f = _FileIO(fn, "w")
|
|
|
|
try:
|
|
|
|
f.write(b"abc")
|
|
|
|
f.close()
|
|
|
|
with open(TESTFN, "rb") as f:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(f.read(), b"abc")
|
2009-06-12 17:14:08 -03:00
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
2009-01-18 20:08:08 -04:00
|
|
|
def testInvalidFd(self):
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(ValueError, _FileIO, -10)
|
|
|
|
self.assertRaises(OSError, _FileIO, make_bad_fd())
|
2010-09-04 18:24:42 -03:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
import msvcrt
|
|
|
|
self.assertRaises(IOError, msvcrt.get_osfhandle, make_bad_fd())
|
2009-01-18 20:08:08 -04:00
|
|
|
|
2008-07-18 11:14:41 -03:00
|
|
|
def testBadModeArgument(self):
|
|
|
|
# verify that we get a sensible error message for bad mode argument
|
|
|
|
bad_mode = "qwerty"
|
|
|
|
try:
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, bad_mode)
|
2008-07-18 11:14:41 -03:00
|
|
|
except ValueError as msg:
|
|
|
|
if msg.args[0] != 0:
|
|
|
|
s = str(msg)
|
2010-03-17 11:22:34 -03:00
|
|
|
if TESTFN in s or bad_mode not in s:
|
2008-07-18 11:14:41 -03:00
|
|
|
self.fail("bad error message for invalid mode: %s" % s)
|
|
|
|
# if msg.args[0] == 0, we're probably on Windows where there may be
|
|
|
|
# no obvious way to discover why open() failed.
|
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
self.fail("no error for invalid mode: %s" % bad_mode)
|
|
|
|
|
2010-01-31 18:26:04 -04:00
|
|
|
def testTruncate(self):
|
|
|
|
f = _FileIO(TESTFN, 'w')
|
|
|
|
f.write(bytes(bytearray(range(10))))
|
|
|
|
self.assertEqual(f.tell(), 10)
|
|
|
|
f.truncate(5)
|
|
|
|
self.assertEqual(f.tell(), 10)
|
|
|
|
self.assertEqual(f.seek(0, os.SEEK_END), 5)
|
|
|
|
f.truncate(15)
|
|
|
|
self.assertEqual(f.tell(), 5)
|
|
|
|
self.assertEqual(f.seek(0, os.SEEK_END), 15)
|
2010-10-31 10:05:48 -03:00
|
|
|
f.close()
|
2010-01-31 18:26:04 -04:00
|
|
|
|
2008-07-18 11:14:41 -03:00
|
|
|
def testTruncateOnWindows(self):
|
|
|
|
def bug801631():
|
|
|
|
# SF bug <http://www.python.org/sf/801631>
|
|
|
|
# "file.truncate fault on windows"
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN, 'w')
|
|
|
|
f.write(bytes(range(11)))
|
2008-07-18 11:14:41 -03:00
|
|
|
f.close()
|
|
|
|
|
2009-06-12 17:14:08 -03:00
|
|
|
f = _FileIO(TESTFN,'r+')
|
2008-07-18 11:14:41 -03:00
|
|
|
data = f.read(5)
|
2009-06-12 17:14:08 -03:00
|
|
|
if data != bytes(range(5)):
|
2008-07-18 11:14:41 -03:00
|
|
|
self.fail("Read on file opened for update failed %r" % data)
|
|
|
|
if f.tell() != 5:
|
|
|
|
self.fail("File pos after read wrong %d" % f.tell())
|
|
|
|
|
|
|
|
f.truncate()
|
|
|
|
if f.tell() != 5:
|
|
|
|
self.fail("File pos after ftruncate wrong %d" % f.tell())
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
size = os.path.getsize(TESTFN)
|
|
|
|
if size != 5:
|
|
|
|
self.fail("File size after ftruncate wrong %d" % size)
|
|
|
|
|
|
|
|
try:
|
|
|
|
bug801631()
|
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
|
|
|
def testAppend(self):
|
|
|
|
try:
|
|
|
|
f = open(TESTFN, 'wb')
|
|
|
|
f.write(b'spam')
|
|
|
|
f.close()
|
|
|
|
f = open(TESTFN, 'ab')
|
|
|
|
f.write(b'eggs')
|
|
|
|
f.close()
|
|
|
|
f = open(TESTFN, 'rb')
|
|
|
|
d = f.read()
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(d, b'spameggs')
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2008-08-24 19:03:05 -03:00
|
|
|
def testInvalidInit(self):
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(TypeError, _FileIO, "1", 0, 0)
|
2008-08-24 19:03:05 -03:00
|
|
|
|
2008-11-03 18:34:57 -04:00
|
|
|
def testWarnings(self):
|
2010-03-07 08:18:33 -04:00
|
|
|
with check_warnings(quiet=True) as w:
|
2008-11-03 18:34:57 -04:00
|
|
|
self.assertEqual(w.warnings, [])
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(TypeError, _FileIO, [])
|
2008-11-03 18:34:57 -04:00
|
|
|
self.assertEqual(w.warnings, [])
|
2009-06-12 17:14:08 -03:00
|
|
|
self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt")
|
2008-11-03 18:34:57 -04:00
|
|
|
self.assertEqual(w.warnings, [])
|
|
|
|
|
2010-04-19 15:52:43 -03:00
|
|
|
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)
|
2008-08-24 19:03:05 -03:00
|
|
|
|
2012-06-21 14:45:19 -03:00
|
|
|
def testUnclosedFDOnException(self):
|
|
|
|
class MyException(Exception): pass
|
|
|
|
class MyFileIO(_FileIO):
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if name == "name":
|
|
|
|
raise MyException("blocked setting name")
|
|
|
|
return super(MyFileIO, self).__setattr__(name, value)
|
|
|
|
fd = os.open(__file__, os.O_RDONLY)
|
|
|
|
self.assertRaises(MyException, MyFileIO, fd)
|
|
|
|
os.close(fd) # should not raise OSError(EBADF)
|
|
|
|
|
2008-07-18 11:14:41 -03:00
|
|
|
def test_main():
|
|
|
|
# Historically, these tests have been sloppy about removing TESTFN.
|
|
|
|
# So get rid of it no matter what.
|
|
|
|
try:
|
|
|
|
run_unittest(AutoFileTests, OtherFileTests)
|
|
|
|
finally:
|
|
|
|
if os.path.exists(TESTFN):
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|