# Buggy code
def writeDownloadQueue(self):
    self.config_lock.acquire()
    realfile = os.path.join(self.data_dir, "transfers.pickle")
    tmpfile = realfile + ".tmp"
    backupfile = realfile + " .backup"
    try:
        handle = open(tmpfile, "wb")
    except Exception as inst:
        log.addwarning(
            _("Something went wrong while opening your transfer list: %(error)s")
            % {"error": str(inst)}
        )
    else:
        try:
            pickle.dump(self.sections["transfers"]["downloads"], handle)
            handle.close()
            try:
                # Please let it be atomic...
                os.rename(tmpfile, realfile)
            except Exception as inst:  # noqa: F841
                # ...ugh. Okay, how about...
                try:
                    os.unlink(backupfile)
                except Exception:
                    pass
                os.rename(realfile, backupfile)
                os.rename(tmpfile, realfile)
        except Exception as inst:
            log.addwarning(
                _("Something went wrong while writing your transfer list: %(error)s")
                % {"error": str(inst)}
            )
    finally:
        try:
            handle.close()
        except Exception:
            pass
    self.config_lock.release()


# Traceback (SystemError)
Thu 07:58 Nicotine+ supports a country code blocker. This requires a (GPL'ed) library called GeoIP. You can find it here: C library:       https://github.com/maxmind/geoip-api-c Python
bindings: https://github.com/maxmind/geoip-api-python (the python bindings require the C library)
Traceback (most recent call last):
File "/usr/local/bin/nicotine", line 255, in <module>
run()
File "/usr/local/bin/nicotine", line 231, in run
app = frame.MainApp(data_dir, config, plugins, trayicon, hidden, bindip, port)
File "/usr/local/lib/python3.7/site-packages/pynicotine/gtkgui/frame.py", line 3595, in __init__
self.frame = NicotineFrame(data_dir, config, plugins, trayicon, start_hidden, bindip, port)
File "/usr/local/lib/python3.7/site-packages/pynicotine/gtkgui/frame.py", line 273, in __init__
config
File "/usr/local/lib/python3.7/site-packages/pynicotine/pynicotine.py", line 130, in __init__
self.shares = Shares(self)
File "/usr/local/lib/python3.7/site-packages/pynicotine/shares.py", line 75, in __init__
self.CompressShares("normal")
File "/usr/local/lib/python3.7/site-packages/pynicotine/shares.py", line 201, in CompressShares
m.makeNetworkMessage(nozlib=0, rebuild=True)
File "/usr/local/lib/python3.7/site-packages/pynicotine/slskmessages.py", line 1709, in makeNetworkMessage
msg = msg + self.packObject(len(list(self.list.keys())))
File "/usr/local/lib/python3.7/_collections_abc.py", line 720, in __iter__
yield from self._mapping
File "/usr/local/lib/python3.7/shelve.py", line 95, in __iter__
for k in self.dict.keys():
SystemError: Negative size passed to PyBytes_FromStringAndSize

# Fixed code
def writeDownloadQueue(self):
    self.config_lock.acquire()
    realfile = os.path.join(self.data_dir, "transfers.pickle")
    tmpfile = realfile + ".tmp"
    backupfile = realfile + " .backup"
    try:
        handle = open(tmpfile, "wb")
    except Exception as inst:
        log.addwarning(
            _("Something went wrong while opening your transfer list: %(error)s")
            % {"error": str(inst)}
        )
    else:
        try:
            pickle.dump(
                self.sections["transfers"]["downloads"],
                handle,
                protocol=pickle.HIGHEST_PROTOCOL,
            )
            handle.close()
            try:
                # Please let it be atomic...
                os.rename(tmpfile, realfile)
            except Exception as inst:  # noqa: F841
                # ...ugh. Okay, how about...
                try:
                    os.unlink(backupfile)
                except Exception:
                    pass
                os.rename(realfile, backupfile)
                os.rename(tmpfile, realfile)
        except Exception as inst:
            log.addwarning(
                _("Something went wrong while writing your transfer list: %(error)s")
                % {"error": str(inst)}
            )
    finally:
        try:
            handle.close()
        except Exception:
            pass
    self.config_lock.release()