kspi / dmenu-frecency

A dmenu-based desktop application launcher
MIT License
33 stars 8 forks source link

Fix: save state correctly #14

Closed SteVwonder closed 8 years ago

SteVwonder commented 8 years ago

Pass filename of tempfile to gzip.open rather than the file-like object

kspi commented 8 years ago

What are the problems with the original approach?

https://docs.python.org/3/library/gzip.html#gzip.open

The filename argument can be an actual filename (a str or bytes object), or an existing file object to read from or write to.

SteVwonder commented 8 years ago

It seems that the gzip.open method works differently with python 2.7 (which is what I happen to be running with). It requires the filename rather than the fileobj. Creating an instance of the GzipFile directly allows for use of the fileobj.

This change shouldn't break anything in python 3, but does make it work in 2.7. An alternative would be to create the GzipFile instance directly, which should be cross-compatible as well.

https://docs.python.org/2.7/library/gzip.html#gzip.open

gzip.open(filename[, mode[, compresslevel]]) This is a shorthand for GzipFile(filename, mode, compresslevel). The filename argument is required; mode defaults to 'rb' and compresslevel defaults to 9.

https://docs.python.org/2.7/library/gzip.html#gzip.GzipFile

class gzip.GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]]) The new class instance is based on fileobj, which can be a regular file, a StringIO object, or any other object which simulates a file. It defaults to None, in which case filename is opened to provide a file object.