Is there a way in Python to check if an entry in os.environ is a variable or shell function?

With the os module in Python, we can easily access environment variables through dict os.environ . However, I found that os.environ not only contains variables, but also globally defined shell functions (for example, from the module software package).

Is it possible from inside Python to find out if a given entry in os.environ is a function, not a variable? Note that a shell-agnostic solution is preferred , but I could justify a Bash-specific solution.

+6
source share
3 answers

This bash function is specific, so checking the exported shell function should do what Bash does. The experiment and the source code show that Bash recognizes the environment variable as a shell function at startup due to the prefix () { in its value - if the prefix is ​​missing or even slightly changed, the variable is treated as a regular data variable.

Therefore, an equivalent Python check would look like this:

 def is_env_shell_func(name): return os.environ[name].startswith('() {') 
+7
source

One solution that I find to work (but this is ridiculously clumsy) is this:

 import subprocess var = 'my_variable_name_i_want_to_check' p = subprocess.Popen('declare -f ' + var, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() if p.returncode == 0: print('function') else: print('variable') 
+3
source

Are you sure shell functions exist in os.environ?

 {master>}% function test_fn() { function> echo "Hello"; function> } {master>}% test_fn Hello {master>}% python Python 2.7.3 (default, Jan 2 2013, 13:56:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ['test_fn'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__ raise KeyError(key) KeyError: 'test_fn' >>> os.environ.keys() ['SSH_ASKPASS', 'PS_FORMAT', 'GIT_PS1_SHOWDIRTYSTATE', 'GNOME_DESKTOP_SESSION_ID', 'WINDOWPATH', 'LOGNAME', 'USER', 'GNOME_KEYRING_CONTROL', 'HOME', 'PS1', 'DISPLAY', 'PATH', 'LANG', 'TERM', 'SHELL', 'SSH_AGENT_PID', 'XAUTHORITY', 'LANGUAGE', 'GIT_PS1_SHOWSTASHSTATE', 'SHLVL', 'GIT_PS1_SHOWUPSTREAM', 'WINDOWID', 'EDITOR', 'MANPATH', 'GIT_PS1_SHOWCOLORHINTS', 'GPG_AGENT_INFO', 'USERNAME', 'WORKON_HOME', 'COLORTERM', 'WORDCHARS', 'SSH_AUTH_SOCK', 'TMUX', 'GDMSESSION', 'XDG_SESSION_COOKIE', 'LS_OPTIONS', 'DBUS_SESSION_BUS_ADDRESS', '_', 'VIRTUALENVWRAPPER_HOOK_DIR', 'VIRTUALENVWRAPPER_PROJECT_FILENAME', 'DESKTOP_SESSION', 'GIT_PS1_SHOWUNTRACKEDFILES', 'GNOME_KEYRING_PID', 'WINDOW_MANAGER', 'ZBEEP', 'PYTHONSTARTUP', 'OLDPWD', 'SESSION_MANAGER', 'XDG_DATA_DIRS', 'PWD', 'CFLAGS', 'VIRTUALENVWRAPPER_LOG_DIR', 'LS_COLORS', 'TMUX_PANE'] >>> DESKTOP_SESSION ',' GIT_PS1_SHOWUNTRACKEDFILES ',' GNOME_KEYRING_PID ',' WINDOW_MANAGER ',' ZBEEP ',' PYTHONSTARTUP ',' OLDPWD ',' SESSION_MANAGER ',' XDG_DATA_DIRS ',' PWD ',' CFLAGS ' {master>}% function test_fn() { function> echo "Hello"; function> } {master>}% test_fn Hello {master>}% python Python 2.7.3 (default, Jan 2 2013, 13:56:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ['test_fn'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__ raise KeyError(key) KeyError: 'test_fn' >>> os.environ.keys() ['SSH_ASKPASS', 'PS_FORMAT', 'GIT_PS1_SHOWDIRTYSTATE', 'GNOME_DESKTOP_SESSION_ID', 'WINDOWPATH', 'LOGNAME', 'USER', 'GNOME_KEYRING_CONTROL', 'HOME', 'PS1', 'DISPLAY', 'PATH', 'LANG', 'TERM', 'SHELL', 'SSH_AGENT_PID', 'XAUTHORITY', 'LANGUAGE', 'GIT_PS1_SHOWSTASHSTATE', 'SHLVL', 'GIT_PS1_SHOWUPSTREAM', 'WINDOWID', 'EDITOR', 'MANPATH', 'GIT_PS1_SHOWCOLORHINTS', 'GPG_AGENT_INFO', 'USERNAME', 'WORKON_HOME', 'COLORTERM', 'WORDCHARS', 'SSH_AUTH_SOCK', 'TMUX', 'GDMSESSION', 'XDG_SESSION_COOKIE', 'LS_OPTIONS', 'DBUS_SESSION_BUS_ADDRESS', '_', 'VIRTUALENVWRAPPER_HOOK_DIR', 'VIRTUALENVWRAPPER_PROJECT_FILENAME', 'DESKTOP_SESSION', 'GIT_PS1_SHOWUNTRACKEDFILES', 'GNOME_KEYRING_PID', 'WINDOW_MANAGER', 'ZBEEP', 'PYTHONSTARTUP', 'OLDPWD', 'SESSION_MANAGER', 'XDG_DATA_DIRS', 'PWD', 'CFLAGS', 'VIRTUALENVWRAPPER_LOG_DIR', 'LS_COLORS', 'TMUX_PANE'] >>> 
+1
source

Source: https://habr.com/ru/post/958972/


All Articles