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: _fileio._FileIO('.', 'r') except IOError as e: self.assertNotEqual(e.errno, 0) else: self.fail("Should have raised IOError")
def fileio_helper(): bytes_io_list = bytesio_helper() file_io_list = [] for i in xrange(len(bytes_io_list)): f = _fileio._FileIO(TEMP_READINTO_NAME % i, "w") f.write(bytes_io_list[i]) f.close() file_io_list.append(_fileio._FileIO(TEMP_READINTO_NAME % i, "r")) return file_io_list
def testReadinto(self): # verify readinto self.f.write(bytes(bytearray([1, 2]))) self.f.close() a = array('b', b'x' * 10) self.f = _fileio._FileIO(TESTFN, 'r') n = self.f.readinto(a) self.assertEquals(array('b', [1, 2]), a[:n])
def testReadinto(self): # verify readinto self.f.write(bytes(bytearray([1, 2]))) self.f.close() a = array('b', b'x'*10) self.f = _fileio._FileIO(TESTFN, 'r') n = self.f.readinto(a) self.assertEquals(array('b', [1, 2]), a[:n])
def testModeStrings(self): # check invalid mode strings for mode in ("", "aU", "wU+", "rb", "rt"): try: f = _fileio._FileIO(TESTFN, mode) except ValueError: pass else: f.close() self.fail('%r is an invalid file mode' % mode)
def testTruncate(self): f = _fileio._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)
def testModeStrings(self): # check invalid mode strings for mode in ("", "aU", "wU+", "rw", "rt"): try: f = _fileio._FileIO(TESTFN, mode) except ValueError: pass else: f.close() self.fail('%r is an invalid file mode' % mode)
def testAbles(self): try: f = _fileio._FileIO(TESTFN, "w") self.assertEquals(f.readable(), False) self.assertEquals(f.writable(), True) self.assertEquals(f.seekable(), True) f.close() f = _fileio._FileIO(TESTFN, "r") self.assertEquals(f.readable(), True) self.assertEquals(f.writable(), False) self.assertEquals(f.seekable(), True) f.close() f = _fileio._FileIO(TESTFN, "a+") self.assertEquals(f.readable(), True) self.assertEquals(f.writable(), True) self.assertEquals(f.seekable(), True) self.assertEquals(f.isatty(), False) f.close() if sys.platform != "win32": try: f = _fileio._FileIO("/dev/tty", "a") 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: f = _fileio._FileIO("/dev/tty", "a") self.assertEquals(f.readable(), False) self.assertEquals(f.writable(), True) if sys.platform != "darwin" and \ not sys.platform.startswith('freebsd') and \ not sys.platform.startswith('sunos'): # Somehow /dev/tty appears seekable on some BSDs self.assertEquals(f.seekable(), False) self.assertEquals(f.isatty(), True) f.close() finally: os.unlink(TESTFN)
def testAbles(self): try: f = _fileio._FileIO(TESTFN, "w") self.assertEquals(f.readable(), False) self.assertEquals(f.writable(), True) self.assertEquals(f.seekable(), True) f.close() f = _fileio._FileIO(TESTFN, "r") self.assertEquals(f.readable(), True) self.assertEquals(f.writable(), False) self.assertEquals(f.seekable(), True) f.close() f = _fileio._FileIO(TESTFN, "a+") self.assertEquals(f.readable(), True) self.assertEquals(f.writable(), True) self.assertEquals(f.seekable(), True) self.assertEquals(f.isatty(), False) f.close() if sys.platform != "win32": try: f = _fileio._FileIO("/dev/tty", "a") 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: f = _fileio._FileIO("/dev/tty", "a") self.assertEquals(f.readable(), False) self.assertEquals(f.writable(), True) if sys.platform != "darwin" and \ 'bsd' not in sys.platform and \ not sys.platform.startswith('sunos'): # Somehow /dev/tty appears seekable on some BSDs self.assertEquals(f.seekable(), False) self.assertEquals(f.isatty(), True) f.close() finally: os.unlink(TESTFN)
def testErrors(self): f = self.f self.assert_(not f.isatty()) self.assert_(not f.closed) #self.assertEquals(f.name, TESTFN) self.assertRaises(ValueError, f.read, 10) # Open for reading f.close() self.assert_(f.closed) f = _fileio._FileIO(TESTFN, 'r') self.assertRaises(TypeError, f.readinto, "") self.assert_(not f.closed) f.close() self.assert_(f.closed)
def bug801631(): # SF bug <http://www.python.org/sf/801631> # "file.truncate fault on windows" f = _fileio._FileIO(TESTFN, 'w') f.write(bytes(bytearray(range(11)))) f.close() f = _fileio._FileIO(TESTFN, 'r+') data = f.read(5) if data != bytes(bytearray(range(5))): 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)
def bug801631(): # SF bug <http://www.python.org/sf/801631> # "file.truncate fault on windows" f = _fileio._FileIO(TESTFN, 'w') f.write(bytes(bytearray(range(11)))) f.close() f = _fileio._FileIO(TESTFN,'r+') data = f.read(5) if data != bytes(bytearray(range(5))): 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)
def testBadModeArgument(self): # verify that we get a sensible error message for bad mode argument bad_mode = "qwerty" try: f = _fileio._FileIO(TESTFN, bad_mode) except ValueError as msg: if msg.args[0] != 0: s = str(msg) if TESTFN in s or bad_mode not in s: 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)
def testBadModeArgument(self): # verify that we get a sensible error message for bad mode argument bad_mode = "qwerty" try: f = _fileio._FileIO(TESTFN, bad_mode) except ValueError as msg: if msg.args[0] != 0: s = str(msg) if s.find(TESTFN) != -1 or s.find(bad_mode) == -1: 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)
def setUp(self): self.f = _fileio._FileIO(TESTFN, 'w')
def testUnicodeOpen(self): # verify repr works for unicode too f = _fileio._FileIO(str(TESTFN), "w") f.close() os.unlink(TESTFN)