From 07155f38a4f60cd8d3322bbc302f221355d6795a Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 17 Aug 2012 19:47:51 +0100 Subject: [PATCH] Make 0alias work with binary files under Python 3 Otherwise you get a UnicodeDecodeError when it tries to check whether something is a script or not. --- tests/testalias.py | 12 +++++++++++- zeroinstall/alias.py | 33 ++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/tests/testalias.py b/tests/testalias.py index 9d7b982..ef62ff0 100755 --- a/tests/testalias.py +++ b/tests/testalias.py @@ -1,6 +1,6 @@ #!/usr/bin/env python from basetest import BaseTest, StringIO -import sys, tempfile +import sys, tempfile, os import unittest sys.path.insert(0, '..') @@ -96,6 +96,16 @@ class TestAlias(BaseTest): self.assertEqual('a\'\'\\test', main) def testParseException(self): + tmp = tempfile.NamedTemporaryFile(mode = 'wb', delete = False) + tmp.write(bytes([240])) + tmp.close() + try: + alias.parse_script(tmp.name) + assert False + except alias.NotAnAliasScript: + pass + os.unlink(tmp.name) + tmp = tempfile.NamedTemporaryFile(mode = 'wt') tmp.write('hi' + expected_script) tmp.flush() diff --git a/zeroinstall/alias.py b/zeroinstall/alias.py index eb4152b..4b564d9 100644 --- a/zeroinstall/alias.py +++ b/zeroinstall/alias.py @@ -42,21 +42,24 @@ def parse_script(pathname): @raise NotAnAliasScript: if we can't parse the script """ with open(pathname, 'rt') as stream: - template_header = _template[:_template.index("%s'")] - actual_header = stream.read(len(template_header)) - stream.seek(0) - if template_header == actual_header: - # If it's a 0alias script, it should be quite short! - rest = stream.read() - line = rest.split('\n')[1] - else: - old_template_header = \ - _old_template[:_old_template.index("-gd '")] - actual_header = stream.read(len(old_template_header)) - if old_template_header != actual_header: - raise NotAnAliasScript(_("'%s' does not look like a script created by 0alias") % pathname) - rest = stream.read() - line = rest.split('\n')[2] + try: + template_header = _template[:_template.index("%s'")] + actual_header = stream.read(len(template_header)) + stream.seek(0) + if template_header == actual_header: + # If it's a 0alias script, it should be quite short! + rest = stream.read() + line = rest.split('\n')[1] + else: + old_template_header = \ + _old_template[:_old_template.index("-gd '")] + actual_header = stream.read(len(old_template_header)) + if old_template_header != actual_header: + raise NotAnAliasScript(_("'%s' does not look like a script created by 0alias") % pathname) + rest = stream.read() + line = rest.split('\n')[2] + except UnicodeDecodeError as ex: + raise NotAnAliasScript(repr(ex)) info = ScriptInfo() split = line.rfind("' '") -- 2.11.4.GIT