Python os.get_handle_inheritable Method

Python os.get_handle_inheritable Method

  1. File and Directory Descriptor in Python
  2. Syntax of Python the os.get_handle_inheritable() Method
  3. Example 1: Use the os.get_handle_inheritable() Method in Python
  4. Example 2: Explore the os.get_handle_inheritable() Method Inside the os Module

In Python, the OS module provides several methods to work and communicate with the operating system. The os import includes and inherits many methods, which help us do different operations with the operating system.

One of the methods is os.get_handle_inheritable(), which gets the inheritable flag of the specified handle. The official Python site says this method is only available on Windows.

Python has os.get_inheritable() method for descriptors and os.get_handle_inheritable() method for handles. The method is used for socket handles in a factor.

Now the question here is, what are a file and a directory descriptor?

File and Directory Descriptor in Python

To communicate directly with the OS kernel, we have low-level file descriptors. These file descriptors are in the form of an integer that finds the open file in a table of all open files stored by the kernel of each process.

Several system calls can accept file descriptors (that are recommended to interact with) that have:

  1. Manual error handling
  2. Several retries in few conditions
  3. Requires fixed-sized buffers

To make interacting with files convenient and with less error, we need Python classes that are file objects which wrap file descriptors.

If you see a file descriptor referring to a directory, then dir_fd isn’t None, and the operational path is relative to that directory. The dir_fd is ignored only if the path is absolute.

We can also check the availability and support of the dir_fd by using the command os.supports_dir_fd on our platform. The platform will show and raise an error NotImplementedError if dir_fd is unavailable.

The symbolic link will operate on a method if the last item of the path to work on is a symbolic link and if follow_symlinks is False.

Similar to the file and directory descriptor availability for a particular platform, we can also check the follow_symlinks support on our platform for a method by using the command os.supports_follow_symlinks.

The exception NotImplementedError will raise if the symlinks are unavailable.

Implementation

During implementation, if we want to check whether the provided path can be known as a file descriptor for a particular method on your operating system, we can use os.supports_fd.

If it is unavailable for your operating system, an exception will be raised by showing the NotImplementedError exception.

Syntax of Python the os.get_handle_inheritable() Method

os.get_handle_inheritable(handle)

Parameters

handle - these are file handles having datatype integer containing 0, 1 or 2. This is a single parameter of the get_handle_inheritable() method.

Return

This method does not return anything; instead, it gets the inheritable flag, a Boolean value of either True or False.

Example 1: Use the os.get_handle_inheritable() Method in Python

# this is how you can use the os.get_handle_inheritable() method
def get_inheritable(self):
  return os.get_handle_inheritable(self.fileno())

The method os.get_handle_inheritable() gets the file descriptor’s inherited flag. This flag shows and highlights whether the descriptor can be inherited by its child processes or not.

In Python version 3.4 (by default), the file descriptor created by Python is non-inherited.

Windows:

In child processes, file handles and non-inherited handles are closed except only for the original streams that are always inherited (the file handles 0, 1 and 2, i.e., stdin, stdout and stderr).

Unix:

When a new program is executed, the non-inherited file descriptors are closed in the child processes. In contrast, the other (file descriptors) are inherited.

Example 2: Explore the os.get_handle_inheritable() Method Inside the os Module

As we have discussed, the os module provides different methods to work with if you forget or want to explore all the methods inside the os module.

You can always use the dir() method inside the print statement to output all these methods. See the source code below to find the different methods and the os.get_handle_inheritable() method.

# importing the os module for using inside methods
import os
# use the dir() method to explore other methods
print(dir(os))

Output:

['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_inheritable Method