1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17from __future__ import annotations
18
19import os
20from typing import BinaryIO, TextIO, TypeVar
21
22__all__ = [
23 "make_file_io_non_caching",
24]
25
26_IO = TypeVar("_IO", TextIO, BinaryIO)
27
28
29def make_file_io_non_caching(io: _IO) -> _IO:
30 try:
31 fd = io.fileno()
32 os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED)
33 except Exception:
34 # in case either file descriptor cannot be retrieved or fadvise is not available
35 # we should simply return the wrapper retrieved by FileHandler's open method
36 # the advice to the kernel is just an advice and if we cannot give it, we won't
37 pass
38 return io