From 678f3895b1a3d8c33977c2946aa520440c8d2461 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 28 Aug 2010 15:33:49 -0700 Subject: 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. --- bin/egencache | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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(''' -- cgit v1.2.3-1-g7c22