From: Zac Medico Date: Sat, 28 Aug 2010 22:33:49 +0000 (-0700) Subject: For egencache --preserve-comments, probe comments in binary mode in X-Git-Tag: v2.2_rc72~8 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=678f3895b1a3d8c33977c2946aa520440c8d2461;p=portage.git For egencache --preserve-comments, probe comments in binary mode in 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. --- diff --git a/bin/egencache b/bin/egencache index b0d727e96..7a3a9f218 100755 --- a/bin/egencache +++ b/bin/egencache @@ -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('''