API · Python

Python os.get_inheritable Function

Learn what os.get_inheritable() checks, how to change a file descriptor's inheritable flag, and how the os function differs from the socket method.

On this page

Python’s os module provides operating-system interfaces. The os.get_inheritable() function checks the inheritable flag on a file descriptor and returns whether that flag is set. This can help you inspect how a descriptor is marked when considering its use by another process.

What os.get_inheritable() tells you

A file descriptor is an integer handle that identifies an open file or another operating-system resource in a process. The inheritable flag records whether that descriptor is marked to be inherited when a process starts another process. os.get_inheritable(fd) reads the flag; it does not change the descriptor or start a process.

The result is a Boolean: True means the flag is set, and False means it is not.

Syntax and return value

os.get_inheritable(fd)

fd must be a file descriptor. The function returns a Boolean value for that descriptor’s inheritable flag. To change the flag, use os.set_inheritable(fd, inheritable), where the second argument is the Boolean value you want to set. If fd is not a valid open descriptor, the call raises OSError instead of returning a Boolean.

Check and change a file descriptor’s inheritable flag

This example creates a temporary file, obtains its descriptor, checks the current flag, sets it to True, and checks again. The with statement manages the temporary file’s lifetime.

Run this example:

import os
import tempfile

with tempfile.TemporaryFile() as file:
    fd = file.fileno()
    print(os.get_inheritable(fd))
    os.set_inheritable(fd, True)
    print(os.get_inheritable(fd))
False
True

The printed Boolean values reflect the flag before and after the change. Keep the descriptor open while checking or changing it; once its resource is closed, that descriptor is no longer available for this purpose.

How inheritance behaves when starting another process

The flag expresses whether a file descriptor is marked as inheritable for use when another process is started. Checking the flag tells you its current setting, while setting it changes that setting. This general meaning does not by itself describe which descriptors a particular process-creation API passes on, or what a child process can do with them. Those details depend on the API and platform, so check the documentation for the process-creation method you use before relying on a descriptor in a child.

Compare os.get_inheritable() and socket.socket.get_inheritable()

These interfaces have similar names but different receivers. os.get_inheritable(fd) is a function in the os module and takes a file descriptor as its argument. socket.socket.get_inheritable() is a method called on a socket object and reports that socket’s inheritable flag. Use the os function when you have a descriptor; use the socket method when working with a socket object. Neither call starts a child process.