# HG changeset patch # User Christian Ebert # Date 1207695321 -7200 # Node ID 1bb9230b055dc0bc517d49750c6c167d5a2e12da # Parent 4bf8ea03d7ac48afe513f191c92d44ac77098bf5 Check for '\0' in entire data before acting util.binary might not be safe enough, as it fails eg. on certain pdf files (issue1066). In some cases this might even be a speed gain as there will be no vain re.sub(n)/search on huge data. diff -r 4bf8ea03d7ac -r 1bb9230b055d hgkw/keyword.py --- a/hgkw/keyword.py Mon Apr 07 18:46:48 2008 +0200 +++ b/hgkw/keyword.py Wed Apr 09 00:55:21 2008 +0200 @@ -102,6 +102,11 @@ '''Returns hgdate in cvs-like UTC format.''' return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) +def textsafe(s): + '''Safe version of util.binary with reversed logic. + Note: argument may not be None, which is allowed for util.binary.''' + return '\0' not in s + # make keyword tools accessible kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']} @@ -160,7 +165,7 @@ def expand(self, path, node, data): '''Returns data with keywords expanded.''' - if not self.restrict and self.matcher(path) and not util.binary(data): + if not self.restrict and self.matcher(path) and textsafe(data): changenode = self.getnode(path, node) return self.substitute(data, path, changenode, self.re_kw.sub) return data @@ -188,7 +193,7 @@ for f in candidates: fp = self.repo.file(f) data = fp.read(mf[f]) - if util.binary(data): + if not textsafe(data): continue if expand: changenode = node or self.getnode(f, mf[f]) @@ -208,7 +213,7 @@ def shrink(self, fname, text): '''Returns text with all keyword substitutions removed.''' - if self.matcher(fname) and not util.binary(text): + if self.matcher(fname) and textsafe(text): return self.shrinktext(text) return text @@ -216,7 +221,7 @@ '''Returns lines with keyword substitutions removed.''' if self.matcher(fname): text = ''.join(lines) - if not util.binary(text): + if textsafe(text): return self.shrinktext(text).splitlines(True) return lines