示例#1
0
def _maybe_memory_map(
    handle: FileOrBuffer,
    memory_map: bool,
    encoding: str,
    mode: str,
    errors: str | None,
    decode: bool,
) -> tuple[FileOrBuffer, bool, list[Buffer]]:
    """Try to memory map file/buffer."""
    handles: list[Buffer] = []
    memory_map &= hasattr(handle, "fileno") or isinstance(handle, str)
    if not memory_map:
        return handle, memory_map, handles

    # need to open the file first
    if isinstance(handle, str):
        if encoding and "b" not in mode:
            # Encoding
            handle = open(handle,
                          mode,
                          encoding=encoding,
                          errors=errors,
                          newline="")
        else:
            # Binary mode
            handle = open(handle, mode)
        handles.append(handle)

    try:
        # error: Argument 1 to "_MMapWrapper" has incompatible type "Union[IO[Any],
        # RawIOBase, BufferedIOBase, TextIOBase, mmap]"; expected "IO[Any]"
        wrapped = cast(
            mmap.mmap,
            _MMapWrapper(handle, encoding, errors,
                         decode),  # type: ignore[arg-type]
        )
        handle.close()
        handles.remove(handle)
        handles.append(wrapped)
        handle = wrapped
    except Exception:
        # we catch any errors that may have occurred
        # because that is consistent with the lower-level
        # functionality of the C engine (pd.read_csv), so
        # leave the file handler as is then
        memory_map = False

    return handle, memory_map, handles
示例#2
0
def _maybe_memory_map(
    handle: FileOrBuffer,
    memory_map: bool,
    encoding: str,
    mode: str,
    errors: Optional[str],
) -> Tuple[FileOrBuffer, bool, List[Buffer]]:
    """Try to memory map file/buffer."""
    handles: List[Buffer] = []
    memory_map &= hasattr(handle, "fileno") or isinstance(handle, str)
    if not memory_map:
        return handle, memory_map, handles

    # need to open the file first
    if isinstance(handle, str):
        if encoding and "b" not in mode:
            # Encoding
            handle = open(handle,
                          mode,
                          encoding=encoding,
                          errors=errors,
                          newline="")
        else:
            # Binary mode
            handle = open(handle, mode)
        handles.append(handle)

    try:
        wrapped = cast(mmap.mmap,
                       _MMapWrapper(handle))  # type: ignore[arg-type]
        handle.close()
        handles.remove(handle)
        handles.append(wrapped)
        handle = wrapped
    except Exception:
        # we catch any errors that may have occurred
        # because that is consistent with the lower-level
        # functionality of the C engine (pd.read_csv), so
        # leave the file handler as is then
        memory_map = False

    return handle, memory_map, handles