kitty_tests, shell-integration: rework getpwuid() exceptions suppression

This commit is contained in:
usertam 2023-03-20 03:31:23 +08:00
parent 5f9b520ca0
commit 08fa7f19f7
No known key found for this signature in database
GPG Key ID: C35F5FF3A6C5AC43
3 changed files with 4 additions and 5 deletions

View File

@ -176,7 +176,7 @@ env COLORTERM
try: try:
expected_login_shell = pwd.getpwuid(os.geteuid()).pw_shell expected_login_shell = pwd.getpwuid(os.geteuid()).pw_shell
except KeyError: except KeyError:
with suppress(Exception): with suppress(KeyError):
self.skipTest('Skipping login shell detection as getpwuid() failed to read login shell') self.skipTest('Skipping login shell detection as getpwuid() failed to read login shell')
if os.path.basename(expected_login_shell) == 'nologin': if os.path.basename(expected_login_shell) == 'nologin':
self.skipTest('Skipping login shell detection as login shell is set to nologin') self.skipTest('Skipping login shell detection as login shell is set to nologin')

View File

@ -67,7 +67,7 @@ using_id() {
} }
using_python() { using_python() {
detect_python && output=$(command "$python" -c "import pwd, os; print(pwd.getpwuid(os.geteuid()).pw_shell)") \ detect_python && output=$(command "$python" -c "import pwd, os; exec(\"try:print(pwd.getpwuid(os.geteuid()).pw_shell)\nexcept:print(os.environ.get('SHELL')or'/bin/sh')\")") \
&& login_shell="$output" && login_shell_is_ok && login_shell="$output" && login_shell_is_ok
} }

View File

@ -22,10 +22,9 @@ request_data = int('REQUEST_DATA')
leading_data = b'' leading_data = b''
login_shell = os.environ.get('SHELL') or '/bin/sh' login_shell = os.environ.get('SHELL') or '/bin/sh'
try: try:
login_shell = pwd.getpwuid(os.geteuid()).pw_shell or login_shell login_shell = pwd.getpwuid(os.geteuid()).pw_shell
except KeyError: except KeyError:
with contextlib.suppress(Exception): pass
print('Failed to read login shell via getpwuid() for current user, falling back to', login_shell, file=sys.stderr)
export_home_cmd = b'EXPORT_HOME_CMD' export_home_cmd = b'EXPORT_HOME_CMD'
if export_home_cmd: if export_home_cmd:
HOME = base64.standard_b64decode(export_home_cmd).decode('utf-8') HOME = base64.standard_b64decode(export_home_cmd).decode('utf-8')