# Buggy code
def on_current_changed(self, idx):
    """Set last-focused-tab and leave hinting mode when focus changed."""
    mode_on_change = config.val.tabs.mode_on_change
    modes_to_save = [usertypes.KeyMode.insert, usertypes.KeyMode.passthrough]
    if idx == -1 or self.shutting_down:
        # closing the last tab (before quitting) or shutting down
        return
    tab = self.widget(idx)
    if tab is None:
        log.webview.debug(
            "on_current_changed got called with invalid index {}".format(idx)
        )
        return
    if self._now_focused is not None and mode_on_change == "restore":
        current_mode = modeman.instance(self._win_id).mode
        if current_mode not in modes_to_save:
            current_mode = usertypes.KeyMode.normal
        self._now_focused.data.input_mode = current_mode

    log.modes.debug("Current tab changed, focusing {!r}".format(tab))
    tab.setFocus()

    modes_to_leave = [usertypes.KeyMode.hint, usertypes.KeyMode.caret]
    if mode_on_change != "persist":
        modes_to_leave += modes_to_save
    for mode in modes_to_leave:
        modeman.leave(self._win_id, mode, "tab changed", maybe=True)
    if mode_on_change == "restore":
        modeman.enter(self._win_id, tab.data.input_mode, "restore input mode for tab")
    if self._now_focused is not None:
        objreg.register(
            "last-focused-tab",
            self._now_focused,
            update=True,
            scope="window",
            window=self._win_id,
        )
    self._now_focused = tab
    self.current_tab_changed.emit(tab)
    QTimer.singleShot(0, self._update_window_title)
    self._tab_insert_idx_left = self.currentIndex()
    self._tab_insert_idx_right = self.currentIndex() + 1


# Traceback (ValueError)
16:39:44 DEBUG    init       app:_init_modules:468 Initializing websettings...
16:39:45 ERROR    misc       crashsignal:exception_hook:216 Uncaught exception
Traceback (most recent call last):
File "/bin/qutebrowser", line 11, in <module>
load_entry_point('qutebrowser==1.1.1', 'gui_scripts', 'qutebrowser')()
File "/usr/lib/python3.6/site-packages/qutebrowser/qutebrowser.py", line 188, in main
return app.run(args)
File "/usr/lib/python3.6/site-packages/qutebrowser/app.py", line 137, in run
init(args, crash_handler)
File "/usr/lib/python3.6/site-packages/qutebrowser/app.py", line 163, in init
_init_modules(args, crash_handler)
File "/usr/lib/python3.6/site-packages/qutebrowser/app.py", line 469, in _init_modules
websettings.init(args)
File "/usr/lib/python3.6/site-packages/qutebrowser/config/websettings.py", line 215, in init
webenginesettings.init(args)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/webenginesettings.py", line 297, in init
websettings.init_mappings(MAPPINGS)
File "/usr/lib/python3.6/site-packages/qutebrowser/config/websettings.py", line 198, in init_mappings
mapping.set(value)
File "/usr/lib/python3.6/site-packages/qutebrowser/config/websettings.py", line 72, in set
self._set(value, settings=settings)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/webenginesettings.py", line 154, in _set
filenames = [self._find_installed(code) for code in value]
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/webenginesettings.py", line 154, in <listcomp>
filenames = [self._find_installed(code) for code in value]
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/webenginesettings.py", line 143, in _find_installed
local_filename = spell.local_filename(code)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/spell.py", line 64, in local_filename
all_installed = local_files(code)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/spell.py", line 51, in local_files
for matching_dict in sorted(matching_dicts, key=version, reverse=True):
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/spell.py", line 36, in version
.format(filename))
ValueError: the given dictionary file name is malformed: /usr/share/qt/qtwebengine_dictionaries/en-US.bdic

# Fixed code
def on_current_changed(self, idx):
    """Set last-focused-tab and leave hinting mode when focus changed."""
    mode_on_change = config.val.tabs.mode_on_change
    modes_to_save = [usertypes.KeyMode.insert, usertypes.KeyMode.passthrough]
    if idx == -1 or self.shutting_down:
        # closing the last tab (before quitting) or shutting down
        return
    tab = self.widget.widget(idx)
    if tab is None:
        log.webview.debug(
            "on_current_changed got called with invalid index {}".format(idx)
        )
        return
    if self._now_focused is not None and mode_on_change == "restore":
        current_mode = modeman.instance(self._win_id).mode
        if current_mode not in modes_to_save:
            current_mode = usertypes.KeyMode.normal
        self._now_focused.data.input_mode = current_mode

    log.modes.debug("Current tab changed, focusing {!r}".format(tab))
    tab.setFocus()

    modes_to_leave = [usertypes.KeyMode.hint, usertypes.KeyMode.caret]
    if mode_on_change != "persist":
        modes_to_leave += modes_to_save
    for mode in modes_to_leave:
        modeman.leave(self._win_id, mode, "tab changed", maybe=True)
    if mode_on_change == "restore":
        modeman.enter(self._win_id, tab.data.input_mode, "restore input mode for tab")
    if self._now_focused is not None:
        objreg.register(
            "last-focused-tab",
            self._now_focused,
            update=True,
            scope="window",
            window=self._win_id,
        )
    self._now_focused = tab
    self.current_tab_changed.emit(tab)
    QTimer.singleShot(0, self._update_window_title)
    self._tab_insert_idx_left = self.widget.currentIndex()
    self._tab_insert_idx_right = self.widget.currentIndex() + 1