# Buggy code
def clearShares(
    self,
    sharedfiles,
    bsharedfiles,
    sharedfilesstreams,
    bsharedfilesstreams,
    wordindex,
    bwordindex,
    fileindex,
    bfileindex,
    sharedmtimes,
    bsharedmtimes,
):
    try:
        if sharedfiles:
            sharedfiles.close()
        try:
            os.unlink(os.path.join(self.data_dir, "files.db"))
        except Exception:
            pass
        sharedfiles = shelve.open(os.path.join(self.data_dir, "files.db"), flag="n")

        if bsharedfiles:
            bsharedfiles.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddyfiles.db"))
        except Exception:
            pass
        bsharedfiles = shelve.open(
            os.path.join(self.data_dir, "buddyfiles.db"), flag="n"
        )

        if sharedfilesstreams:
            sharedfilesstreams.close()
        try:
            os.unlink(os.path.join(self.data_dir, "streams.db"))
        except Exception:
            pass
        sharedfilesstreams = shelve.open(
            os.path.join(self.data_dir, "streams.db"), flag="n"
        )

        if bsharedfilesstreams:
            bsharedfilesstreams.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddystreams.db"))
        except Exception:
            pass
        bsharedfilesstreams = shelve.open(
            os.path.join(self.data_dir, "buddystreams.db"), flag="n"
        )

        if wordindex:
            wordindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "wordindex.db"))
        except Exception:
            pass
        wordindex = shelve.open(os.path.join(self.data_dir, "wordindex.db"), flag="n")

        if bwordindex:
            bwordindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddywordindex.db"))
        except Exception:
            pass
        bwordindex = shelve.open(
            os.path.join(self.data_dir, "buddywordindex.db"), flag="n"
        )

        if fileindex:
            fileindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "fileindex.db"))
        except Exception:
            pass
        fileindex = shelve.open(os.path.join(self.data_dir, "fileindex.db"), flag="n")

        if bfileindex:
            bfileindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddyfileindex.db"))
        except Exception:
            pass
        bfileindex = shelve.open(
            os.path.join(self.data_dir, "buddyfileindex.db"), flag="n"
        )

        if sharedmtimes:
            sharedmtimes.close()
        try:
            os.unlink(os.path.join(self.data_dir, "mtimes.db"))
        except Exception:
            pass
        sharedmtimes = shelve.open(os.path.join(self.data_dir, "mtimes.db"), flag="n")

        if bsharedmtimes:
            bsharedmtimes.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddymtimes.db"))
        except Exception:
            pass
        bsharedmtimes = shelve.open(
            os.path.join(self.data_dir, "buddymtimes.db"), flag="n"
        )
    except Exception as error:
        log.addwarning(_("Error while writing database files: %s") % error)
        return None
    return (
        sharedfiles,
        bsharedfiles,
        sharedfilesstreams,
        bsharedfilesstreams,
        wordindex,
        bwordindex,
        fileindex,
        bfileindex,
        sharedmtimes,
        bsharedmtimes,
    )


# 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 clearShares(
    self,
    sharedfiles,
    bsharedfiles,
    sharedfilesstreams,
    bsharedfilesstreams,
    wordindex,
    bwordindex,
    fileindex,
    bfileindex,
    sharedmtimes,
    bsharedmtimes,
):
    try:
        if sharedfiles:
            sharedfiles.close()
        try:
            os.unlink(os.path.join(self.data_dir, "files.db"))
        except Exception:
            pass
        sharedfiles = shelve.open(
            os.path.join(self.data_dir, "files.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if bsharedfiles:
            bsharedfiles.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddyfiles.db"))
        except Exception:
            pass
        bsharedfiles = shelve.open(
            os.path.join(self.data_dir, "buddyfiles.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if sharedfilesstreams:
            sharedfilesstreams.close()
        try:
            os.unlink(os.path.join(self.data_dir, "streams.db"))
        except Exception:
            pass
        sharedfilesstreams = shelve.open(
            os.path.join(self.data_dir, "streams.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if bsharedfilesstreams:
            bsharedfilesstreams.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddystreams.db"))
        except Exception:
            pass
        bsharedfilesstreams = shelve.open(
            os.path.join(self.data_dir, "buddystreams.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if wordindex:
            wordindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "wordindex.db"))
        except Exception:
            pass
        wordindex = shelve.open(
            os.path.join(self.data_dir, "wordindex.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if bwordindex:
            bwordindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddywordindex.db"))
        except Exception:
            pass
        bwordindex = shelve.open(
            os.path.join(self.data_dir, "buddywordindex.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if fileindex:
            fileindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "fileindex.db"))
        except Exception:
            pass
        fileindex = shelve.open(
            os.path.join(self.data_dir, "fileindex.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if bfileindex:
            bfileindex.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddyfileindex.db"))
        except Exception:
            pass
        bfileindex = shelve.open(
            os.path.join(self.data_dir, "buddyfileindex.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if sharedmtimes:
            sharedmtimes.close()
        try:
            os.unlink(os.path.join(self.data_dir, "mtimes.db"))
        except Exception:
            pass
        sharedmtimes = shelve.open(
            os.path.join(self.data_dir, "mtimes.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )

        if bsharedmtimes:
            bsharedmtimes.close()
        try:
            os.unlink(os.path.join(self.data_dir, "buddymtimes.db"))
        except Exception:
            pass
        bsharedmtimes = shelve.open(
            os.path.join(self.data_dir, "buddymtimes.db"),
            flag="n",
            protocol=pickle.HIGHEST_PROTOCOL,
        )
    except Exception as error:
        log.addwarning(_("Error while writing database files: %s") % error)
        return None
    return (
        sharedfiles,
        bsharedfiles,
        sharedfilesstreams,
        bsharedfilesstreams,
        wordindex,
        bwordindex,
        fileindex,
        bfileindex,
        sharedmtimes,
        bsharedmtimes,
    )