def test_unicode(self): import _file, os f = _file.file(self.temppath, "w") f.write(u"hello\n") f.close() f = _file.file(self.temppath, "r") res = f.read() assert res == "hello\n" assert type(res) is str f.close()
def test_newlines(self): import _file, os f = _file.file(self.temppath, "wb") f.write("\r\n") assert f.newlines is None f.close() f = _file.file(self.temppath, "rU") res = f.read() assert res == "\n" assert f.newlines == "\r\n"
def test_simple(self): import _file f = _file.file(self.temppath, "w") f.write("foo") f.close() f = _file.file(self.temppath, "r") raises(TypeError, f.read, None) try: s = f.read() assert s == "foo" finally: f.close()
def test_readlines(self): import _file f = _file.file(self.temppath, "w") try: f.write("foo\nbar\n") finally: f.close() f = _file.file(self.temppath, "r") raises(TypeError, f.readlines, None) try: s = f.readlines() assert s == ["foo\n", "bar\n"] finally: f.close()
def test_concurrent_writes(self): # check that f.write() is atomic import thread, _file, time f = _file.file(self.temppath, "w+b") def writer(i): for j in range(150): f.write('%3d %3d\n' % (i, j)) locks[i].release() locks = [] for i in range(10): lock = thread.allocate_lock() lock.acquire() locks.append(lock) for i in range(10): thread.start_new_thread(writer, (i, )) # wait until all threads are done for i in range(10): locks[i].acquire() f.seek(0) lines = f.readlines() lines.sort() assert lines == [ '%3d %3d\n' % (i, j) for i in range(10) for j in range(150) ] f.close()
def test_fdopen(self): import _file, os f = _file.file(self.temppath, "w") try: f.write("foo") finally: f.close() fd = os.open(self.temppath, os.O_WRONLY | os.O_CREAT) f2 = _file.file.fdopen(fd, "a") f2.seek(0, 2) f2.write("bar") f2.close() # don't close fd, will get a whining __del__ f = _file.file(self.temppath, "r") try: s = f.read() assert s == "foobar" finally: f.close()
def test_correct_file_mode(self): import _file, os f = _file.file(self.temppath, "w") umask = os.umask(18) os.umask(umask) try: f.write("foo") finally: f.close() assert oct(os.stat(self.temppath).st_mode & 0777 | umask) == oct(0666)
def test_large_seek_offsets(self): import _file FAR = 0x122223333 f = _file.file(self.temppath, "w+b") f.write("hello world") f.seek(FAR) assert f.tell() == FAR f.seek(-10, 1) assert f.tell() == FAR - 10 f.seek(0x123456789, 1) assert f.tell() == FAR - 10 + 0x123456789 f.seek(-FAR, 1) assert f.tell() == -10 + 0x123456789 f.seek(FAR, 2) assert f.tell() == len("hello world") + FAR f.close()
def test_concurrent_writes(self): # check that f.write() is atomic import thread, _file, time f = _file.file(self.temppath, "w+b") def writer(i): for j in range(150): f.write('%3d %3d\n' % (i, j)) locks[i].release() locks = [] for i in range(10): lock = thread.allocate_lock() lock.acquire() locks.append(lock) for i in range(10): thread.start_new_thread(writer, (i,)) # wait until all threads are done for i in range(10): locks[i].acquire() f.seek(0) lines = f.readlines() lines.sort() assert lines == ['%3d %3d\n' % (i, j) for i in range(10) for j in range(150)] f.close()
def test_large_sparse(self): import _file FAR = 0x122223333 f = _file.file(self.temppath, "w+b") f.seek(FAR) f.write('end') f.seek(0) data = f.read(1234) assert data == 1234 * '\x00' f.seek(FAR-2-1234, 1) data = f.read(4321) assert data == '\x00\x00end' f.seek(-1, 2) assert f.tell() == FAR + 2 f.truncate() f.seek(0, 2) assert f.tell() == FAR + 2 f.truncate(FAR + 1) f.seek(FAR-2, 0) data = f.read(1) assert data == '\x00' assert f.tell() == FAR - 1 data = f.read(1) assert data == '\x00' data = f.read(1) assert data == 'e' data = f.read(1) assert data == '' assert f.tell() == FAR + 1 import sys if FAR > sys.maxint: f.seek(0) raises(OverflowError, f.read, FAR) raises(OverflowError, f.readline, FAR) raises(OverflowError, f.readlines, FAR) f.close()
def test_large_sparse(self): import _file FAR = 0x122223333 f = _file.file(self.temppath, "w+b") f.seek(FAR) f.write('end') f.seek(0) data = f.read(1234) assert data == 1234 * '\x00' f.seek(FAR - 2 - 1234, 1) data = f.read(4321) assert data == '\x00\x00end' f.seek(-1, 2) assert f.tell() == FAR + 2 f.truncate() f.seek(0, 2) assert f.tell() == FAR + 2 f.truncate(FAR + 1) f.seek(FAR - 2, 0) data = f.read(1) assert data == '\x00' assert f.tell() == FAR - 1 data = f.read(1) assert data == '\x00' data = f.read(1) assert data == 'e' data = f.read(1) assert data == '' assert f.tell() == FAR + 1 import sys if FAR > sys.maxint: f.seek(0) raises(OverflowError, f.read, FAR) raises(OverflowError, f.readline, FAR) raises(OverflowError, f.readlines, FAR) f.close()