def assert_file(our_file: io.FileIO, passed_file: io.FileIO): our_readable = our_file.readable() got_mode = passed_file.mode our_stat = os.fstat(our_file.fileno()) passed_stat = os.fstat(passed_file.fileno()) is_fifo = stat.S_ISFIFO(passed_stat.st_mode) expected_mode = "wb" if our_readable else "rb" reader = our_file if our_readable else passed_file writer = passed_file if our_readable else our_file # Verify that we have a pipe with its two ends if is_fifo and our_stat == passed_stat: pipe_size = fcntl.fcntl(writer.fileno(), F_GETPIPE_SZ) # Check for pending bytes in the pipe pending_bytes = bytearray(SIZEOF_INT) fcntl.ioctl(reader.fileno(), termios.FIONREAD, pending_bytes) pending_bytes = struct.unpack_from("=i", pending_bytes)[0] test_size = min(mmap.PAGESIZE, pipe_size - pending_bytes) expected_bytes = random.randbytes(test_size) writer.write(expected_bytes) writer.flush() got_bytes = reader.read(pipe_size) else: expected_bytes = None got_bytes = None assert (got_mode, is_fifo, passed_stat, got_bytes) == (expected_mode, True, our_stat, expected_bytes)
def read(cls, source: FileIO, kind: str) -> Context: """ Deserialize the given input JSON file as the specified object type. Args: source: The input JSON file. kind: The context object type. Returns: The parsed object. """ assert source.readable() _, clz = cls.resolve_ctxtype(kind) return clz.from_source(source)