For egencache --preserve-comments, probe comments in binary mode in
authorZac Medico <zmedico@gentoo.org>
Sat, 28 Aug 2010 22:33:49 +0000 (15:33 -0700)
committerZac Medico <zmedico@gentoo.org>
Sat, 28 Aug 2010 22:33:49 +0000 (15:33 -0700)
order to avoid potential character encoding issues (both in terms
of decoding and in calculating file position given variable-width
character encodings like UTF-8.

bin/egencache

index b0d727e963a253b7a6f46abc5b189503d127502c..7a3a9f218fcc0c4801fdf4d34be2f0c3f7015eed 100755 (executable)
@@ -342,7 +342,9 @@ class GenUseLocalDesc(object):
                                desc_path = self._output
 
                        if self._preserve_comments:
-                               output_mode = 'r+'
+                               # Probe in binary mode, in order to avoid
+                               # potential character encoding issues.
+                               output_mode = 'rb'
                        else:
                                output_mode = 'w'
 
@@ -359,15 +361,15 @@ class GenUseLocalDesc(object):
 
                if self._preserve_comments:
                        while True:
-                               l = output.readline()
-                               # Seek through comments, until the first non-comment.
-                               # If we hit empty line, then great.
-                               # Otherwise, we need to seek back and add an empty line.
-                               if not l.startswith('#'):
-                                       if l.rstrip() != '':
-                                               output.seek(-len(l), os.SEEK_CUR)
-                                               output.write('\n')
+                               pos = output.tell()
+                               if not output.readline().startswith(b'#'):
                                        break
+                       output.close()
+
+                       # Finished probing comments, now open for write.
+                       output = open(desc_path, 'r+')
+                       output.seek(pos)
+                       output.write('\n')
                        output.truncate()
                else:   
                        output.write('''