json config files get corrupted when using Python 2.7.6 on Linux
I noticed when running calibre 1.28.0 on Fedora-21 that I got the following messages when starting calibre from a terminal:
Traceback (most recent call last):
d = self.raw_
ValueError: Extra data: line 19 column 2 - line 37 column 2 (char 349 - 698)
loaded the Generic plugin
And that my preferences were no longer honored / remembered.
I've done some debugging and the problem is that ie gui.json gets any new config settings appended rather then that then contents gets overwritten. IE gui.json looks like this after one run (abbreviated):
I've tracked this down to calibre/
doing (abbreviated again):
self.file = open(self.path, 'a+b')
When running under anything but windows. Note the a+ stands for append mode, now
On the file, but this seems to no longer lead to the desired result, given what the python docs say:
"file.seek(offset[, whence]) ... Note that if the file is opened for appending (mode 'a' or 'a+'), any seek() operations will be undone at the next write."
This is not really surprising as a truncate is a write. As the calibre code used to work fine until
recently, I can only assume that truncate used to be a loophole around this limitation, and
that this has now been fixed.
So I've audited all the users of ExclusiveFile in calibre, and whenever the write they always
first do a truncate. So there really is no need to use "a" as mode when opening the file,
so replacing the open in ExclusiveFile,
self.file = open(self.path, 'r+b')
Thanks & Regards,