Example #1
0
 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()
Example #2
0
 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()
Example #3
0
 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"
Example #4
0
 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"
Example #5
0
 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()
Example #6
0
 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()
Example #7
0
 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()
Example #8
0
 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()
Example #9
0
    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()
Example #10
0
 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()
Example #11
0
 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)
Example #12
0
 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)
Example #13
0
 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()
Example #14
0
 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()
Example #15
0
 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()
Example #16
0
 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()
Example #17
0
 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()
Example #18
0
 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()