def test_write_interrupted(self): try: from signal import alarm, signal, SIG_DFL, SIGALRM except ImportError: pytest.skip('no alarm on this platform') try: read_fd, write_fd = os.pipe() file = streamio.DiskFile(write_fd) def handler(*a): os.read(read_fd, 2000) alarm(1) signal(SIGALRM, handler) alarm(1) # Write to the pipe until it is full buf = "FILL THE PIPE" * 1000 while True: if os.write(write_fd, buf) < len(buf): break # Write more, this should block, the write() syscall is # interrupted, signal handler is called, and next write() # can succeed. file.write("hello") finally: alarm(0) signal(SIGALRM, SIG_DFL)
def diskopen(fn, mode): filemode = 0 if "r" in mode: filemode = os.O_RDONLY if "w" in mode: filemode |= os.O_WRONLY fd = os.open(fn, filemode) base = streamio.DiskFile(fd) return streamio.BufferingInputStream(base)
def test_read_interrupted(self): try: from signal import alarm, signal, SIG_DFL, SIGALRM except ImportError: pytest.skip('no alarm on this platform') try: read_fd, write_fd = os.pipe() file = streamio.DiskFile(read_fd) def handler(*a): os.write(write_fd, "hello") signal(SIGALRM, handler) alarm(1) assert file.read(10) == "hello" finally: alarm(0) signal(SIGALRM, SIG_DFL)
def test_ignore_ioerror_in_readall_if_nonempty_result(self): # this is the behavior of regular files in CPython 2.7, as # well as of _io.FileIO at least in CPython 3.3. This is # *not* the behavior of _io.FileIO in CPython 3.4 or 3.5; # see CPython's issue #21090. try: from os import openpty except ImportError: pytest.skip('no openpty on this platform') read_fd, write_fd = openpty() os.write(write_fd, 'Abc\n') os.close(write_fd) x = streamio.DiskFile(read_fd) s = x.readall() assert s == 'Abc\r\n' pytest.raises(OSError, x.readall) x.close()
def _open(fd): base = streamio.DiskFile(fd) return streamio.BufferingInputStream(base)