from fcntl import ioctl
from termios import FIONCLEX
from termios import FIOCLEX


def set_fd_inheritable(fd, inheritable):
    """
    disable the "inheritability" of a file descriptor

    See Also:
        https://docs.python.org/3/library/os.html#inheritance-of-file-descriptors
        https://github.com/python/cpython/blob/65e6c1eff3/Python/fileutils.c#L846-L857
    """
    if inheritable:
        return ioctl(fd, FIONCLEX)
    else:
        return ioctl(fd, FIOCLEX)