def test_fileno(self, mock_splice): """Test handling of file-descriptors""" splice(1, None, 2, None, 3, 0) self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, 0), {})) mock_splice.reset_mock() with open("/dev/zero", "r") as fd: splice(fd, None, fd, None, 3, 0) self.assertEqual(mock_splice.call_args, ((fd.fileno(), None, fd.fileno(), None, 3, 0), {}))
def test_flags_list(self, mock_splice): """Test handling of flag lists""" splice(1, None, 2, None, 3, [splice.SPLICE_F_MOVE, splice.SPLICE_F_NONBLOCK]) flags = splice.SPLICE_F_MOVE | splice.SPLICE_F_NONBLOCK self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, flags), {})) mock_splice.reset_mock() splice(1, None, 2, None, 3, []) self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, 0), {}))
def test_fileno(self, mock_splice): '''Test handling of file-descriptors''' splice(1, None, 2, None, 3, 0) self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, 0), {})) mock_splice.reset_mock() with open('/dev/zero', 'r') as fd: splice(fd, None, fd, None, 3, 0) self.assertEqual( mock_splice.call_args, ((fd.fileno(), None, fd.fileno(), None, 3, 0), {}))
def test_flags_list(self, mock_splice): '''Test handling of flag lists''' splice(1, None, 2, None, 3, [splice.SPLICE_F_MOVE, splice.SPLICE_F_NONBLOCK]) flags = splice.SPLICE_F_MOVE | splice.SPLICE_F_NONBLOCK self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, flags), {})) mock_splice.reset_mock() splice(1, None, 2, None, 3, []) self.assertEqual(mock_splice.call_args, ((1, None, 2, None, 3, 0), {}))
def test_errno(self): '''Test handling of failures''' # Invoke EBADF by using a read-only FD as fd_out with open('/dev/null', 'r') as fd: err = errno.EBADF msg = r'\[Errno %d\] splice: %s' % (err, os.strerror(err)) try: splice(fd, None, fd, None, 3, 0) except IOError as e: self.assertTrue(re.match(msg, str(e))) else: self.fail('Expected IOError was not raised') self.assertEqual(ctypes.get_errno(), 0)
def test_errno(self): """Test handling of failures""" # Invoke EBADF by using a read-only FD as fd_out with open("/dev/null", "r") as fd: err = errno.EBADF msg = r"\[Errno %d\] splice: %s" % (err, os.strerror(err)) try: splice(fd, None, fd, None, 3, 0) except IOError as e: self.assertTrue(re.match(msg, str(e))) else: self.fail("Expected IOError was not raised") self.assertEqual(ctypes.get_errno(), 0)
def test_splice_pipe_to_file(self): '''Test `splice` from a pipe to a file''' with tempfile.NamedTemporaryFile(bufsize=0) as fd, pipe() as (pa, pb): os.write(pb, 'abcdef') res = splice(pa, None, fd, None, 3, 0) self.assertEqual(res, (3, None, None)) self.assertEqual(fd.tell(), 3) fd.seek(0, os.SEEK_SET) res = splice(pa, None, fd, 3, 3, 0) self.assertEqual(res, (3, None, 6)) self.assertEqual(fd.tell(), 0) self.assertEqual(fd.read(6), 'abcdef')
def test_splice_pipe_to_file(self): '''Test `splice` from a pipe to a file''' with NamedTemporaryFile() as fd: with pipe() as (pa, pb): os.write(pb, b'abcdef') res = splice(pa, None, fd, None, 3, 0) self.assertEqual(res, (3, None, None)) self.assertEqual(fd.tell(), 3) fd.seek(0, os.SEEK_SET) res = splice(pa, None, fd, 3, 3, 0) self.assertEqual(res, (3, None, 6)) self.assertEqual(fd.tell(), 0) self.assertEqual(fd.read(6), b'abcdef')
def test_splice_file_to_pipe(self): '''Test `splice` from a file to a pipe''' with tempfile.NamedTemporaryFile(bufsize=0) as fd, pipe() as (pa, pb): fd.write('abcdef') fd.seek(0, os.SEEK_SET) res = splice(fd, None, pb, None, 3, 0) self.assertEqual(res, (3, None, None)) # `fd.tell()` isn't updated... self.assertEqual(os.lseek(fd.fileno(), 0, os.SEEK_CUR), 3) fd.seek(0, os.SEEK_SET) res = splice(fd, 3, pb, None, 3, 0) self.assertEqual(res, (3, 6, None)) self.assertEqual(os.lseek(fd.fileno(), 0, os.SEEK_CUR), 0) self.assertEquals(os.read(pa, 6), 'abcdef')
def test_splice_pipe_to_pipe(self): '''Test `splice` from a pipe to a pipe''' with pipe() as (p1a, p1b), pipe() as (p2a, p2b): os.write(p1b, 'abcdef') res = splice(p1a, None, p2b, None, 3, 0) self.assertEqual(res, (3, None, None)) self.assertEqual(os.read(p2a, 3), 'abc') self.assertEqual(os.read(p1a, 3), 'def')
def test_splice_pipe_to_pipe(self): '''Test `splice` from a pipe to a pipe''' with pipe() as (p1a, p1b): with pipe() as (p2a, p2b): os.write(p1b, b'abcdef') res = splice(p1a, None, p2b, None, 3, 0) self.assertEqual(res, (3, None, None)) self.assertEqual(os.read(p2a, 3), b'abc') self.assertEqual(os.read(p1a, 3), b'def')
def test_splice_file_to_pipe(self): '''Test `splice` from a file to a pipe''' with NamedTemporaryFile() as fd: with pipe() as (pa, pb): fd.write(b'abcdef') fd.seek(0, os.SEEK_SET) res = splice(fd, None, pb, None, 3, 0) self.assertEqual(res, (3, None, None)) # `fd.tell()` isn't updated... self.assertEqual(os.lseek(fd.fileno(), 0, os.SEEK_CUR), 3) fd.seek(0, os.SEEK_SET) res = splice(fd, 3, pb, None, 3, 0) self.assertEqual(res, (3, 6, None)) self.assertEqual(os.lseek(fd.fileno(), 0, os.SEEK_CUR), 0) self.assertEqual(os.read(pa, 6), b'abcdef')
def test_splice_pipe_to_pipe(self): """Test `splice` from a pipe to a pipe""" with pipe() as (p1a, p1b): with pipe() as (p2a, p2b): os.write(p1b, "abcdef") res = splice(p1a, None, p2b, None, 3, 0) self.assertEqual(res, (3, None, None)) self.assertEqual(os.read(p2a, 3), "abc") self.assertEqual(os.read(p1a, 3), "def")