From 4e6e1efd2342fb2b929bd0a3e1b5dff6f860340a Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 16 Jul 2020 14:35:27 +0200 Subject: ipc: Handle failing getpass.getuser() on Windows (cherry picked from commit a03348f95111af5c86819f1397c5b633cec1a968) --- qutebrowser/misc/ipc.py | 17 ++++++++++++++--- tests/unit/misc/test_ipc.py | 12 ++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/qutebrowser/misc/ipc.py b/qutebrowser/misc/ipc.py index c4cd4f792..4ee0e8b91 100644 --- a/qutebrowser/misc/ipc.py +++ b/qutebrowser/misc/ipc.py @@ -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) diff --git a/tests/unit/misc/test_ipc.py b/tests/unit/misc/test_ipc.py index 95858f837..dd4a5cc40 100644 --- a/tests/unit/misc/test_ipc.py +++ b/tests/unit/misc/test_ipc.py @@ -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)'), ] -- cgit v1.2.3-54-g00ecf