ipc: Handle failing getpass.getuser() on Windows

(cherry picked from commit a03348f951)
This commit is contained in:
Florian Bruhin 2020-07-16 14:35:27 +02:00
parent 2b72f43d9e
commit 4e6e1efd23
2 changed files with 24 additions and 5 deletions

View File

@ -46,7 +46,19 @@ server = None
def _get_socketname_windows(basedir):
"""Get a socketname to use for Windows."""
parts = ['qutebrowser', getpass.getuser()]
try:
username = getpass.getuser()
except ImportError:
# getpass.getuser() first tries a couple of environment variables. If
# none of those are set (i.e., USERNAME is missing), it tries to import
# the "pwd" module which is unavailable on Windows.
raise Error("Could not find username. This should only happen if "
"there is a bug in the application launching qutebrowser, "
"preventing the USERNAME environment variable from being "
"passed. If you know more about when this happens, please "
"report this to mail@qutebrowser.org.")
parts = ['qutebrowser', username]
if basedir is not None:
md5 = hashlib.md5(basedir.encode('utf-8')).hexdigest()
parts.append(md5)
@ -484,7 +496,6 @@ def display_error(exc, args):
"""Display a message box with an IPC error."""
error.handle_fatal_exc(
exc, "Error while connecting to running instance!",
post_text="Maybe another instance is running but frozen?",
no_err_windows=args.no_err_windows)
@ -499,8 +510,8 @@ def send_or_listen(args):
None if an instance was running and received our request.
"""
global server
socketname = _get_socketname(args.basedir)
try:
socketname = _get_socketname(args.basedir)
try:
sent = send_to_running_instance(socketname, args.command,
args.target)

View File

@ -198,6 +198,14 @@ class TestSocketName:
socketname = ipc._get_socketname_windows(basedir)
assert socketname == expected
def test_windows_broken_getpass(self, monkeypatch):
def _fake_username():
raise ImportError
monkeypatch.setattr(ipc.getpass, 'getuser', _fake_username)
with pytest.raises(ipc.Error, match='USERNAME'):
ipc._get_socketname_windows(basedir=None)
@pytest.mark.mac
@pytest.mark.parametrize('basedir, expected', [
(None, 'i-{}'.format(md5('testusername'))),
@ -725,7 +733,7 @@ class TestSendOrListen:
'',
'title: Error while connecting to running instance!',
'pre_text: ',
'post_text: Maybe another instance is running but frozen?',
'post_text: ',
'exception text: {}'.format(exc_msg),
]
assert caplog.messages == ['\n'.join(error_msgs)]
@ -746,7 +754,7 @@ class TestSendOrListen:
'',
'title: Error while connecting to running instance!',
'pre_text: ',
'post_text: Maybe another instance is running but frozen?',
'post_text: ',
('exception text: Error while listening to IPC server: Error '
'string (error 4)'),
]